

<?php 
ini_set('max_execution_time', 0); //300 seconds = 5 minutes 
date_default_timezone_set("Asia/Bangkok");
// echo $_SESSION['company_id'];
include('../include/connectDB.php'); 
include('../include/connectDB_Bill.php'); 
include('INSERT_LOG.php'); 

$currentdate = date('Y-m-d');

$yearY = date("Y"); 
$yearM = date("m"); 
$yearD = date("d");
$htime = date("H:i:sa"); 
$current_time = $yearD.'.'.$yearM.'.'.($yearY+543);
$s_trime = $yearY.$yearM.$yearD;
$DateStart=date($s_trime);
$strtime = strtotime($DateStart);
$caltime=strtotime("-2 Month",$strtime);
$ResultDate=date("Y-m-d", $caltime);
$yearY = substr(date("$ResultDate"), 0,-6); 
$yearM = substr(date("$ResultDate"), 5,-3); 
$yearD = substr(date("$ResultDate"), -2); 
$s_trime = $yearD.'.'.$yearM.'.'.($yearY+543);  
// $s_trime = "20.05.2564"; 
?>


<?php

// $sql_del = "delete from POD_CF_DETAILS";
//     $stm_del = sqlsrv_prepare ($ConnYS, $sql_del);
//         if (!$stm_del) {
//             throw new Exception("sql_del  plan#". $sql_del); 
//         }
//         if (!sqlsrv_execute($stm_del)) {
//             throw new Exception("sql_del นำเข้า plan ไม่สำเหร็จ#" . $sql_del);
//         }

// sqlsrv_commit($ConnYS);
$start_brid = start_brid($ConnYS);
$end_brid = end_brid($connFB);

// echo "start_brid".$start_brid;
// echo "<BR>";
// echo "end_brid".$end_brid;
// die("stop");

$sql1 = " SELECT
            BRID
            ,BRCODE
            ,BRNAME
            ,BMANAGER
            ,BADDRESS
            ,BDISTRICT
            ,BPROVINCE
            ,BTEL1
            ,BTEL2
            ,BFAX
            ,BEMAIL
            ,BSTATUS
            ,ORG_CODE
            ,BRLAT
            ,BRLNG
            ,BR_CHKIN_DISTANCE
            ,RECORD_STATUS
            ,HUB_BR_CODE
            ,HUB_GROUP
            from  BRANCH_TABLE
            where   BRID  between '".$start_brid."' and '".$end_brid."' "; 
        // where doc_date between '20.05.2563' and '20.06.2564' 
    // echo $sql1;
    $rst1 = ibase_query($connFB, $sql1);
    // die("Stop");
    $x = 0;
    $data = array();
    while($arr1   = ibase_fetch_object($rst1)) {

        $data[] = array( 

                         "BRID"                       => $arr1->BRID
                        ,"BRCODE"                     => $arr1->BRCODE
                        ,"BRNAME"                     => $arr1->BRNAME
                        ,"BMANAGER"                  => $arr1->BMANAGER
                        ,"BADDRESS"                   => $arr1->BADDRESS
                        ,"BDISTRICT"                  => $arr1->BDISTRICT
                        ,"BPROVINCE"                  => $arr1->BPROVINCE
                        ,"BTEL1"                      => $arr1->BTEL1
                        ,"BTEL2"                      => $arr1->BTEL2
                        ,"BFAX"                       => $arr1->BFAX
                        ,"BEMAIL"                     => $arr1->BEMAIL
                        ,"BSTATUS"                    => $arr1->BSTATUS
                        ,"ORG_CODE"                   => $arr1->ORG_CODE
                        ,"BRLAT"                      => $arr1->BRLAT
                        ,"BRLNG"                      => $arr1->BRLNG
                        ,"BR_CHKIN_DISTANCE"          => $arr1->BR_CHKIN_DISTANCE
                        ,"RECORD_STATUS"              => $arr1->RECORD_STATUS
                        ,"HUB_BR_CODE"                => $arr1->HUB_BR_CODE
                        ,"HUB_GROUP"                  => $arr1->HUB_GROUP

                    );
        $x++;
    }

    echo insert_sqlServer($ConnYS,$data,$current_time,$htime,$connLog);

    function  insert_sqlServer($ConnYS,$data,$current_time,$htime,$connLog){


        sqlsrv_begin_transaction($ConnYS);
        try {

            for($i=0;$i<sizeof($data);$i++){

                $BRID               = $data[$i]["BRID"];
                $BRCODE             = $data[$i]["BRCODE"];
                $BRNAME             = $data[$i]["BRNAME"];
                $BMANAGER           = $data[$i]["BMANAGER"];
                $BADDRESS           = $data[$i]["BADDRESS"];
                $BDISTRICT          = $data[$i]["BDISTRICT"];
                $BPROVINCE          = $data[$i]["BPROVINCE"];
                $BTEL1              = $data[$i]["BTEL1"];
                $BTEL2              = $data[$i]["BTEL2"];
                $BFAX               = $data[$i]["BFAX"];
                $BEMAIL             = $data[$i]["BEMAIL"];
                $BSTATUS            = $data[$i]["BSTATUS"];
                $ORG_CODE           = $data[$i]["ORG_CODE"];
                $BRLAT              = $data[$i]["BRLAT"];
                $BRLNG              = $data[$i]["BRLNG"];
                $BR_CHKIN_DISTANCE  = $data[$i]["BR_CHKIN_DISTANCE"];
                $RECORD_STATUS      = $data[$i]["RECORD_STATUS"];
                $HUB_BR_CODE        = $data[$i]["HUB_BR_CODE"];
                $HUB_GROUP          = $data[$i]["HUB_GROUP"];

                $sql_brname = "INSERT into  BRANCH_TABLE (BRID ,BRCODE ,BRNAME ,BMANAGER ,BADDRESS ,BDISTRICT  ,BPROVINCE  ,BTEL1  ,BTEL2  ,BFAX  ,BEMAIL  ,BSTATUS  ,ORG_CODE  ,BRLAT  ,BRLNG  ,BR_CHKIN_DISTANCE  ,RECORD_STATUS  ,HUB_BR_CODE, HUB_GROUP)  
                values ('".$BRID."', '".$BRCODE."', '".$BRNAME."', '".$BMANAGER."', '".$BADDRESS."', '".$BDISTRICT."', '".$BPROVINCE."', '".$BTEL1."', '".$BTEL2."', '".$BFAX."'
                    , '".$BEMAIL."', '".$BSTATUS."', '".$ORG_CODE."', '".$BRLAT."', '".$BRLNG."', '".$BR_CHKIN_DISTANCE."', '".$RECORD_STATUS."', '".$HUB_BR_CODE."', '".$HUB_GROUP."') "; 
                // echo $sql_brname."<br>".$x;
                $stm_plan = sqlsrv_prepare ($ConnYS, $sql_brname);
                if (!$stm_plan) {
                    throw new Exception("sql_brname  plan#". $sql_brname); 
                }
                if (!sqlsrv_execute($stm_plan)) {
                    throw new Exception("sql_brname นำเข้า plan ไม่สำเร็จ#" . $sql_brname);
                }
            }

                sqlsrv_commit($ConnYS);
                // sqlsrv_rollback($ConnYS);
                $result = array("STATUS" => "1", "RESULT" => 'Seuccess :'.$i);
                write_text("BRANCH_TABLE",$current_time,$htime);
                return json_encode($result,JSON_UNESCAPED_UNICODE);
        } catch (Exception $ex) {
            $result = "";
            sqlsrv_rollback($ConnYS);
            sqlsrv_close($ConnYS);
            $result = array("STATUS" => "0", "RESULT" => explode('#', $ex->getMessage())); 
            write_text($ex->getMessage(),$current_time,$htime);
            return json_encode($result,JSON_UNESCAPED_UNICODE);
            $TABLE = "BRANCH_TABLE";
            INSERT_SYNLOG($connLog,"ไม่บันทึกดาต้าเบส",$TABLE,"FAIL");
        } 
        sqlsrv_close($ConnYS);
    }

// echo "Success".$x;
// echo "6";

// write_text("BRANCH_TABLE",$current_time,$htime);
// write_text2("....................................");

function CF_DATE($CF_DATE){
    $key_vol = explode("-", $CF_DATE);
        $str0 = trim($key_vol[0]);
        $str1 = trim($key_vol[1]);
        $str2 = trim($key_vol[2]);
    $resulte_date = $str2.'.'.$str1.'.'.$str0;   
return $resulte_date;
}

function write_text($BRANCH_TABLE,$current_time,$htime){
    $t_string = "$BRANCH_TABLE".'  '."$current_time".' '."$htime";
    $strFileName = "LogSynDB.txt";
    $objFopen = fopen($strFileName, 'a');
    $strText1 = "$t_string\r\n";
    fwrite($objFopen, $strText1);
}


function end_brid($connFB){
    $sql_br_idex = "SELECT max(brid) as END_INDEX_BR from  BRANCH_TABLE";
    $rs_br_idex = ibase_query($connFB, $sql_br_idex);
			$arr_br_idex = ibase_fetch_assoc($rs_br_idex); 
			$end_em_idex =  ($arr_br_idex["END_INDEX_BR"]);
		return $end_em_idex;
}

function start_brid($ConnYS){
    $sql_br_idex = "SELECT (CASE WHEN max(BRID) is NULL THEN 0 ELSE max(BRID)+1 END) as START_INDEX_BR from BRANCH_TABLE ";
    $rs_br_idex = sqlsrv_query($ConnYS, $sql_br_idex);
			$arr_br_idex = sqlsrv_fetch_array($rs_br_idex); 
			$start_em_idex =  ($arr_br_idex["START_INDEX_BR"]);
        return $start_em_idex;
}
                
 ?>
	







