
<?php 
ini_set('max_execution_time', 0); //300 seconds = 5 minutes 
ini_set('memory_limit', '4095M'); 
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 = substr(date("Y")+543, 4).date("Y"); 
// $yearM = substr(date("m")+543, 3).date("m"); 
// $yearD = substr(date("d")+543, 3).date("d"); 
// $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("-30 day",$strtime);
$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";      

// $start_date = "SUBSTRING('".$s_trime."', 1, 2)+'.'+SUBSTRING('".$s_trime."', 1, 2)+'.'+SUBSTRING('".$s_trime."', 7, 4)";
// $end_date = "SUBSTRING('".$current_time."', 1, 2)+'.'+SUBSTRING('".$current_time."', 1, 2)+'.'+SUBSTRING('".$current_time."', 7, 4)";
// echo $s_trime;

//----sql
$st_sql_tdate = date('Y-m-d');
$st_sql_DateStart=date($st_sql_tdate);
$st_sql_strtime = strtotime($st_sql_DateStart);
$st_sql_caltime=strtotime("-2 Month",$st_sql_strtime);
// $st_sql_caltime=strtotime("-30 day",$st_sql_strtime);
$st_sql_ResultDate=date("Y-m-d", $st_sql_caltime);
$st_sql_ResultDate;
$en_sql_ResultDate = $st_sql_tdate; 
//----sql
// die();
?>


<?php

    $sql_del = "delete from SITEA_BILLHEADER where DOC_DATE between '$st_sql_ResultDate' and '$en_sql_ResultDate' ";

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

    $sql1 = " SELECT
            DOC_ID
            ,DOC_CODE
            ,DOC_DATE
            ,DOC_TIME
            ,DOC_INVTYPE
            ,CUS_NO
            ,CUS_NAME
            ,CUS_LOCALNAME
            ,CUS_BILLADDR1
            ,CUS_BILLADDR2
            ,CUS_BILLADDR3
            ,CUS_BILLADDR4
            ,CUS_BILLADDR5
            ,CUS_SHIPADDR
            ,CUS_SHIPSUBDISTRICT
            ,CUS_SHIPDISTRICT
            ,CUS_SHIPPROVINCE
            ,CUS_SHIPZIPCODE
            ,CUS_SHIPCOUNTRY
            ,CUS_SHIPDC
            ,ORDER_NO
            ,DN_DATE
            ,ORDER_DATE
            ,REF_NO
            ,INV_NO
            ,char_length(INV_NO) as L_INVNO
            ,INV_DATE
            ,INV_SENDDATE
            ,DC
            ,SUM_ITEMS
            ,SUM_WEIGHT
            ,SUM_CAPACITY
            ,CHECK_BY
            ,CREATE_BY
            ,DOC_REMARK
            ,DOC_STATUS
            ,BR_CODE
            ,C1
            ,DELIVERY_STATE
            ,BILL_RETURN_DRIVER
            ,BILL_RETURN_CUST
            ,PICK_CODE
            ,ROUTE_NO
            ,ROUTE_NAME
            ,ZONE_NO
            ,ZONE_NAME
            ,CN_NO
            ,PLAN_SENDDATE
            ,PLAN_SENDBYADMIN
            ,BILL_OWNERCODE
            ,PLAN_SENDDATE_TIME
            ,BILL_ISSUE_RESPOND
            ,REF_NO2
            ,RECEIVE_GOODS
            ,INCOME_RATE
            ,IMPORT_DATETIME
            ,INV_TIME
            ,REMARK1
            ,REMARK2
            ,REMARK3
            ,HUB_BR_CODE
            ,OWNER_BR_CODE
            ,REF_NO3
            ,ACTION_UPDATE
            ,REF_NO4
            ,PAYMENTSTATUS
            ,COD_PRODUCTCOST
            ,COD_SHIPPINGCOST
            ,COD_TOTAL
            ,COD_PERCENTAGE
            ,PAYMENTTYPE
            ,DELIVERYMOBILE
            ,BILLING_STATUS
            ,REMARK4
            from  SITEA_BILLHEADER where DOC_DATE between '$s_trime' and '$current_time' "; 
            // where doc_date between '20.05.2563' and '20.06.2564' 

        $rst1 = ibase_query($connFB, $sql1);
        // die("Stop");
        $x = 0;
        while($arr1         = ibase_fetch_object($rst1)) {

        $data[] = array( 
        
                 "DOC_ID"                 => $arr1->DOC_ID
                ,"DOC_CODE"               => $arr1->DOC_CODE
                ,"DOC_DATE"               => $arr1->DOC_DATE
                ,"DOC_TIME"               => $arr1->DOC_TIME
                ,"DOC_INVTYPE"            => $arr1->DOC_INVTYPE
                ,"CUS_NO"                 => $arr1->CUS_NO
                ,"CUS_NAME"               => $arr1->CUS_NAME
                ,"CUS_LOCALNAME"          => $arr1->CUS_LOCALNAME
                ,"CUS_BILLADDR1"          => $arr1->CUS_BILLADDR1
                ,"CUS_BILLADDR2"          => $arr1->CUS_BILLADDR2
                ,"CUS_BILLADDR3"          => $arr1->CUS_BILLADDR3
                ,"CUS_BILLADDR4"          => $arr1->CUS_BILLADDR4
                ,"CUS_BILLADDR5"          => $arr1->CUS_BILLADDR5
                ,"CUS_SHIPADDR"           => $arr1->CUS_SHIPADDR
                ,"CUS_SHIPSUBDISTRICT"    => $arr1->CUS_SHIPSUBDISTRICT
                ,"CUS_SHIPDISTRICT"       => $arr1->CUS_SHIPDISTRICT
                ,"CUS_SHIPPROVINCE"       => $arr1->CUS_SHIPPROVINCE
                ,"CUS_SHIPZIPCODE"        => $arr1->CUS_SHIPZIPCODE
                ,"CUS_SHIPCOUNTRY"        => $arr1->CUS_SHIPCOUNTRY
                ,"CUS_SHIPDC"             => $arr1->CUS_SHIPDC
                ,"ORDER_NO"               => $arr1->ORDER_NO
                ,"DN_DATE"                => $arr1->DN_DATE
                ,"ORDER_DATE"             => $arr1->ORDER_DATE
                ,"REF_NO"                 => $arr1->REF_NO
                ,"INV_NO"                 => $arr1->INV_NO
                ,"INV_DATE"               => $arr1->INV_DATE
                ,"INV_SENDDATE"           => $arr1->INV_SENDDATE
                ,"DC"                     => $arr1->DC
                ,"SUM_ITEMS"              => $arr1->SUM_ITEMS
                ,"SUM_WEIGHT"             => $arr1->SUM_WEIGHT
                ,"SUM_CAPACITY"           => $arr1->SUM_CAPACITY
                ,"CHECK_BY"               => $arr1->CHECK_BY
                ,"CREATE_BY"              => $arr1->CREATE_BY
                ,"DOC_REMARK"             => $arr1->DOC_REMARK
                ,"DOC_STATUS"             => $arr1->DOC_STATUS
                ,"BR_CODE"                => $arr1->BR_CODE
                ,"C1"                     => $arr1->C1
                ,"DELIVERY_STATE"         => $arr1->DELIVERY_STATE
                ,"BILL_RETURN_DRIVER"     => $arr1->BILL_RETURN_DRIVER
                ,"BILL_RETURN_CUST"       => $arr1->BILL_RETURN_CUST
                ,"PICK_CODE"              => $arr1->PICK_CODE
                ,"ROUTE_NO"               => $arr1->ROUTE_NO
                ,"ROUTE_NAME"             => $arr1->ROUTE_NAME
                ,"ZONE_NO"                => $arr1->ZONE_NO
                ,"ZONE_NAME"              => $arr1->ZONE_NAME
                ,"CN_NO"                  => $arr1->CN_NO
                ,"PLAN_SENDDATE"          => $arr1->PLAN_SENDDATE
                ,"PLAN_SENDBYADMIN"       => $arr1->PLAN_SENDBYADMIN
                ,"BILL_OWNERCODE"         => $arr1->BILL_OWNERCODE
                ,"PLAN_SENDDATE_TIME"     => $arr1->PLAN_SENDDATE_TIME
                ,"BILL_ISSUE_RESPOND"     => $arr1->BILL_ISSUE_RESPOND
                ,"REF_NO2"                => $arr1->REF_NO2
                ,"RECEIVE_GOODS"          => $arr1->RECEIVE_GOODS
                ,"INCOME_RATE"            => $arr1->INCOME_RATE
                ,"IMPORT_DATETIME"        => $arr1->IMPORT_DATETIME
                ,"INV_TIME"               => $arr1->INV_TIME
                ,"REMARK1"                => $arr1->REMARK1
                ,"REMARK2"                => $arr1->REMARK2
                ,"REMARK3"                => $arr1->REMARK3
                ,"HUB_BR_CODE"            => $arr1->HUB_BR_CODE
                ,"OWNER_BR_CODE"          => $arr1->OWNER_BR_CODE
                ,"REF_NO3"                => $arr1->REF_NO3
                ,"ACTION_UPDATE"          => $arr1->ACTION_UPDATE
                ,"REF_NO4"                => $arr1->REF_NO4
                ,"PAYMENTSTATUS"          => $arr1->PAYMENTSTATUS
                ,"COD_PRODUCTCOST"        => $arr1->COD_PRODUCTCOST
                ,"COD_SHIPPINGCOST"       => $arr1->COD_SHIPPINGCOST
                ,"COD_TOTAL"              => $arr1->COD_TOTAL
                ,"COD_PERCENTAGE"         => $arr1->COD_PERCENTAGE
                ,"PAYMENTTYPE"            => $arr1->PAYMENTTYPE
                ,"DELIVERYMOBILE"         => $arr1->DELIVERYMOBILE
                ,"BILLING_STATUS"         => $arr1->BILLING_STATUS
                ,"L_INVNO"                => $arr1->L_INVNO
                ,"REMARK4"                => $arr1->REMARK4

            );
            $x++;
        }
    // echo "Success".$x;
    // echo "1";

    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"];

                $DOC_ID                = $data[$i]["DOC_ID"];
                $DOC_CODE              = $data[$i]["DOC_CODE"];
                $DOC_DATE              = $data[$i]["DOC_DATE"];
                $DOC_TIME              = $data[$i]["DOC_TIME"];
                $DOC_INVTYPE           = $data[$i]["DOC_INVTYPE"];
                $CUS_NO                = $data[$i]["CUS_NO"];
                $CUS_NAME              = $data[$i]["CUS_NAME"];
                $CUS_LOCALNAME         = $data[$i]["CUS_LOCALNAME"];
                $CUS_BILLADDR1         = $data[$i]["CUS_BILLADDR1"];
                $CUS_BILLADDR2         = $data[$i]["CUS_BILLADDR2"];
                $CUS_BILLADDR3         = $data[$i]["CUS_BILLADDR3"];
                $CUS_BILLADDR4         = $data[$i]["CUS_BILLADDR4"];
                $CUS_BILLADDR5         = $data[$i]["CUS_BILLADDR5"];
                $CUS_SHIPADDR          = $data[$i]["CUS_SHIPADDR"];
                $CUS_SHIPSUBDISTRICT   = $data[$i]["CUS_SHIPSUBDISTRICT"];
                $CUS_SHIPDISTRICT      = $data[$i]["CUS_SHIPDISTRICT"];
                $CUS_SHIPPROVINCE      = $data[$i]["CUS_SHIPPROVINCE"];
                $CUS_SHIPZIPCODE       = $data[$i]["CUS_SHIPZIPCODE"];
                $CUS_SHIPCOUNTRY       = $data[$i]["CUS_SHIPCOUNTRY"];
                $CUS_SHIPDC            = $data[$i]["CUS_SHIPDC"];
                $ORDER_NO              = $data[$i]["ORDER_NO"];
                $DN_DATE               = $data[$i]["DN_DATE"];
                $ORDER_DATE            = $data[$i]["ORDER_DATE"];
                $REF_NO                = $data[$i]["REF_NO"];
                $INV_NO                = $data[$i]["INV_NO"];
                $INV_DATE              = $data[$i]["INV_DATE"];
                $INV_SENDDATE          = $data[$i]["INV_SENDDATE"];
                $DC                    = $data[$i]["DC"];
                $SUM_ITEMS             = $data[$i]["SUM_ITEMS"];
                $SUM_WEIGHT            = $data[$i]["SUM_WEIGHT"];
                $SUM_CAPACITY          = $data[$i]["SUM_CAPACITY"];
                $CHECK_BY              = $data[$i]["CHECK_BY"];
                $CREATE_BY             = $data[$i]["CREATE_BY"];
                $DOC_REMARK            = $data[$i]["DOC_REMARK"];
                $DOC_STATUS            = $data[$i]["DOC_STATUS"];
                $BR_CODE               = $data[$i]["BR_CODE"];
                $C1                    = $data[$i]["C1"];
                $DELIVERY_STATE        = $data[$i]["DELIVERY_STATE"];
                $BILL_RETURN_DRIVER    = $data[$i]["BILL_RETURN_DRIVER"];
                $BILL_RETURN_CUST      = $data[$i]["BILL_RETURN_CUST"];
                $PICK_CODE             = $data[$i]["PICK_CODE"];
                $ROUTE_NO              = $data[$i]["ROUTE_NO"];
                $ROUTE_NAME            = $data[$i]["ROUTE_NAME"];
                $ZONE_NO               = $data[$i]["ZONE_NO"];
                $ZONE_NAME             = $data[$i]["ZONE_NAME"];
                $CN_NO                 = $data[$i]["CN_NO"];
                $PLAN_SENDDATE         = $data[$i]["PLAN_SENDDATE"];
                $PLAN_SENDBYADMIN      = $data[$i]["PLAN_SENDBYADMIN"];
                $BILL_OWNERCODE        = $data[$i]["BILL_OWNERCODE"];
                $PLAN_SENDDATE_TIME    = $data[$i]["PLAN_SENDDATE_TIME"];
                $BILL_ISSUE_RESPOND    = $data[$i]["BILL_ISSUE_RESPOND"];
                $REF_NO2               = $data[$i]["REF_NO2"];
                $RECEIVE_GOODS         = $data[$i]["RECEIVE_GOODS"];
                $INCOME_RATE           = $data[$i]["INCOME_RATE"];
                $IMPORT_DATETIME       = $data[$i]["IMPORT_DATETIME"];
                $INV_TIME              = $data[$i]["INV_TIME"];
                $REMARK1               = $data[$i]["REMARK1"];
                $REMARK2               = $data[$i]["REMARK2"];
                $REMARK3               = $data[$i]["REMARK3"];
                $HUB_BR_CODE           = $data[$i]["HUB_BR_CODE"];
                $OWNER_BR_CODE         = $data[$i]["OWNER_BR_CODE"];
                $REF_NO3               = $data[$i]["REF_NO3"];
                $ACTION_UPDATE         = $data[$i]["ACTION_UPDATE"];
                $REF_NO4               = $data[$i]["REF_NO4"];
                $PAYMENTSTATUS         = $data[$i]["PAYMENTSTATUS"];
                $COD_PRODUCTCOST       = $data[$i]["COD_PRODUCTCOST"];
                $COD_SHIPPINGCOST      = $data[$i]["COD_SHIPPINGCOST"];
                $COD_TOTAL             = $data[$i]["COD_TOTAL"];
                $COD_PERCENTAGE        = $data[$i]["COD_PERCENTAGE"];
                $PAYMENTTYPE           = $data[$i]["PAYMENTTYPE"];
                $DELIVERYMOBILE        = $data[$i]["DELIVERYMOBILE"];
                $BILLING_STATUS        = $data[$i]["BILLING_STATUS"];
                $L_INVNO               = $data[$i]["L_INVNO"];
                $REMARK4               = $data[$i]["REMARK4"];


                if($L_INVNO <=30){
                    $INV_NO = $INV_NO;
                }else{
                    $INV_NO  = substr($INV_NO,1,30);  
                    $INV_NO =  htmlspecialchars( $INV_NO );
                }
                // $DOC_DATE = CONVERT_DATE543($DOC_DATE);
                if($DOC_DATE!=""){
                    $DOC_DATE = CONVERT_DATE543($DOC_DATE);
                    $DOC_DATE = " '".$DOC_DATE."' ";
                }else{
                    $DOC_DATE ="NULL";
                }

                if($DN_DATE!=""){
                    $DN_DATE = CONVERT_DATE543($DN_DATE);
                    $DN_DATE = " '".$DN_DATE."' ";
                }else{
                    $DN_DATE ="NULL";
                }

                if($ORDER_DATE!=""){
                    $ORDER_DATE = CONVERT_DATE543($ORDER_DATE);
                    $ORDER_DATE = " '".$ORDER_DATE."' ";
                }else{
                    $ORDER_DATE ="NULL";
                }

                if($INV_DATE!=""){
                    $INV_DATE = CONVERT_DATE543($INV_DATE);
                    $INV_DATE = " '".$INV_DATE."' ";
                }else{
                    $INV_DATE ="NULL";
                }

                if($INV_SENDDATE!=""){
                    $INV_SENDDATE = CONVERT_DATE543($INV_SENDDATE);
                    $INV_SENDDATE = " '".$INV_SENDDATE."' ";
                }else{
                    $INV_SENDDATE ="NULL";
                }

                if($PLAN_SENDDATE!=""){
                    $PLAN_SENDDATE = CONVERT_DATE543($PLAN_SENDDATE);
                    $PLAN_SENDDATE = " '".$PLAN_SENDDATE."' ";
                }else{
                    $PLAN_SENDDATE ="NULL";
                }
                

                $sql_BILL = "INSERT into  SITEA_BILLHEADER (DOC_ID ,DOC_CODE ,DOC_DATE ,DOC_TIME ,DOC_INVTYPE ,CUS_NO ,CUS_NAME ,CUS_LOCALNAME, CUS_BILLADDR1, CUS_BILLADDR2, CUS_BILLADDR3, CUS_BILLADDR4, CUS_BILLADDR5, CUS_SHIPADDR
                                        , CUS_SHIPSUBDISTRICT, CUS_SHIPDISTRICT, CUS_SHIPPROVINCE, CUS_SHIPZIPCODE, CUS_SHIPCOUNTRY, CUS_SHIPDC
                                        ,ORDER_NO, DN_DATE, ORDER_DATE, REF_NO, INV_NO, INV_DATE, INV_SENDDATE, DC, SUM_ITEMS, SUM_WEIGHT, SUM_CAPACITY, CHECK_BY, CREATE_BY, DOC_REMARK, DOC_STATUS, BR_CODE, C1, DELIVERY_STATE, BILL_RETURN_DRIVER, BILL_RETURN_CUST
                                        ,PICK_CODE, ROUTE_NO, ROUTE_NAME, ZONE_NO, ZONE_NAME, CN_NO, PLAN_SENDDATE, PLAN_SENDBYADMIN, BILL_OWNERCODE, PLAN_SENDDATE_TIME, BILL_ISSUE_RESPOND, REF_NO2, RECEIVE_GOODS, INCOME_RATE, IMPORT_DATETIME, INV_TIME
                                        ,REMARK1, REMARK2, REMARK3, HUB_BR_CODE, OWNER_BR_CODE, REF_NO3, ACTION_UPDATE, REF_NO4, PAYMENTSTATUS, COD_PRODUCTCOST, COD_SHIPPINGCOST, COD_TOTAL, COD_PERCENTAGE, PAYMENTTYPE, DELIVERYMOBILE, BILLING_STATUS, REMARK4 )  
                                    values ('".$DOC_ID."', '".$DOC_CODE."', $DOC_DATE, '".$DOC_TIME."', '".$DOC_INVTYPE."', '".$CUS_NO."', '".$CUS_NAME."', '".$CUS_LOCALNAME."', '".$CUS_BILLADDR1."', '".$CUS_BILLADDR2."', '".$CUS_BILLADDR3."', '".$CUS_BILLADDR4."', '".$CUS_BILLADDR5."', '".$CUS_SHIPADDR."'
                                        , '".$CUS_SHIPSUBDISTRICT."', '".$CUS_SHIPDISTRICT."', '".$CUS_SHIPPROVINCE."', '".$CUS_SHIPZIPCODE."', '".$CUS_SHIPCOUNTRY."', '".$CUS_SHIPDC."', '".$ORDER_NO."', $DN_DATE, $ORDER_DATE, '".$REF_NO."', '".$INV_NO."', $INV_DATE, $INV_SENDDATE, '".$DC."', '".$SUM_ITEMS."'
                                        , '".$SUM_WEIGHT."', '".$SUM_CAPACITY."', '".$CHECK_BY."', '".$CREATE_BY."', '".$DOC_REMARK."', '".$DOC_STATUS."', '".$BR_CODE."', '".$C1."', '".$DELIVERY_STATE."', '".$BILL_RETURN_DRIVER."', '".$BILL_RETURN_CUST."', '".$PICK_CODE."', '".$ROUTE_NO."', '".$ROUTE_NAME."', '".$ZONE_NO."'
                                        , '".$ZONE_NAME."', '".$CN_NO."', $PLAN_SENDDATE, '".$PLAN_SENDBYADMIN."', '".$BILL_OWNERCODE."', '".$PLAN_SENDDATE_TIME."', '".$BILL_ISSUE_RESPOND."', '".$REF_NO2."', '".$RECEIVE_GOODS."', '".$INCOME_RATE."', '".$IMPORT_DATETIME."', '".$INV_TIME."', '".$REMARK1."', '".$REMARK2."'
                                        , '".$REMARK3."', '".$HUB_BR_CODE."', '".$OWNER_BR_CODE."', '".$REF_NO3."', '".$ACTION_UPDATE."', '".$REF_NO4."', '".$PAYMENTSTATUS."', '".$COD_PRODUCTCOST."', '".$COD_SHIPPINGCOST."', '".$COD_TOTAL."', '".$COD_PERCENTAGE."', '".$PAYMENTTYPE."', '".$DELIVERYMOBILE."' , '".$BILLING_STATUS."', '".$REMARK4."') "; 
                            // echo $sql_BILL."<br>".$x;
                        $stm_BILL = sqlsrv_prepare ($ConnYS, $sql_BILL);
                        if (!$stm_BILL) {
                            throw new Exception("sql_BILL  SITEA_BILLHEADER#". $sql_BILL); 
                        }
                        if (!sqlsrv_execute($stm_BILL)) {
                            throw new Exception("sql_BILL นำเข้า SITEA_BILLHEADER ไม่สำเหร็จ#" . $sql_BILL);
                        }
            }

                // sqlsrv_rollback($ConnYS);
                sqlsrv_commit($ConnYS);
                $result = array("STATUS" => "1", "RESULT" => 'Seuccess :'.$i);
                write_text("SITEA_BILLHEADER",$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 = "SITEA_BILLHEADER";
            INSERT_SYNLOG($connLog,"ไม่บันทึกดาต้าเบส",$TABLE,"FAIL");
        }
        sqlsrv_close($ConnYS); 
    }

function CONVERT_DATE543($VAL_DATE){
    $key_vol = explode("-", $VAL_DATE);
    $strY = trim($key_vol[0]);
    $strM = trim($key_vol[1]);
    $strD = trim($key_vol[2]);
    $sub_y = substr($strY,0,2);
    if($sub_y=="25"){
        $resulte_date = ($strY-543).'-'.$strM.'-'.$strD;
     }else{
        $resulte_date = ($strY).'-'.$strM.'-'.$strD;
     }
return $resulte_date;
}

function CONVERT_DATE($VAL_DATE){
    $key_vol = explode("-", $VAL_DATE);
        $strY = trim($key_vol[0]);
        $strM = trim($key_vol[1]);
        $strD = trim($key_vol[2]);
    $resulte_date = $strY.'-'.$strM.'-'.$strD;   
return $resulte_date;
}

// write_text("SITEA_BILLHEADER",$current_time,$htime);

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







