

<?php 
ini_set('max_execution_time', 0); //300 seconds = 5 minutes 
// echo $_SESSION['company_id'];
include('../include/connectDB.php'); 
include('../include/connectDB_Bill.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("-3 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 SITEA_BILLHEADER";
$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);
}

$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
            ,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
            from  SITEA_BILLHEADER where DOC_DATE between '$s_trime' and '$current_time' "; 
        // where doc_date between '20.05.2563' and '20.06.2564' 
// echo $sql1;

$rst1 = ibase_query($connFB, $sql1);
// die("Stop");
$x = 0;
while($arr1         = ibase_fetch_assoc($rst1)) {
   
    $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"];
   
    $DOC_DATE = DOC_DATE($DOC_DATE);

        if ($ConnYS) {
            sqlsrv_begin_transaction($ConnYS);
                try {
    
                    // $sql_plan = "UPDATE bill_master_planned SET plan_status='Inactive' Where  plan_br_code='".$FB_BR_CODE."' and plan_c1='".$FB_C1_DOCCODE."' and plan_invoiceno='".$FB_INV_NO."' "; 
                    //         // echo $sql_plan."<br>".$x;
                    //     $stm_plan = sqlsrv_prepare ($ConnYS, $sql_plan);
                    //     if (!$stm_plan) {
                    //         throw new Exception("sql_plan  plan#". $sql_plan); 
                    //     }
                    //     if (!sqlsrv_execute($stm_plan)) {
                    //         throw new Exception("sql_plan นำเข้า plan ไม่สำเหร็จ#" . $sql_plan);
                    //     }

            

                     $sql_plan = "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 )  
                                                        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."') "; 
                            // echo $sql_plan."<br>".$x;
                        $stm_plan = sqlsrv_prepare ($ConnYS, $sql_plan);
                        if (!$stm_plan) {
                            throw new Exception("sql_plan  plan#". $sql_plan); 
                        }
                        if (!sqlsrv_execute($stm_plan)) {
                            throw new Exception("sql_plan นำเข้า plan ไม่สำเหร็จ#" . $sql_plan);
                        }
       
                        // sqlsrv_rollback($ConnYS);

                sqlsrv_commit($ConnYS);
                // echo "Success";
            } catch (Exception $ex) {
                sqlsrv_rollback($ConnYS);
                 sqlsrv_close($ConnYS);
                echo $ex."ไม่บันทึกดาต้าเบส";
            }
        }

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

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

function DOC_DATE($DOC_DATE){
    $key_vol = explode("-", $DOC_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($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);
}
                
 ?>
	







