

<?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'); 

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

$yearY = date("Y"); 
$yearM = date("m"); 
$yearD = date("d"); 
$htime = date("H:i:sa");
$current_time = $yearD.'.'.$yearM.'.'.($yearY);
$s_trime = $yearY.$yearM.$yearD;
$DateStart=date($s_trime);
$strtime = strtotime($DateStart);
  $caltime=strtotime("-4 Month",$strtime);
//$caltime=strtotime("-30 day",$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);  
// $s_trime = "20.05.2564"; 

//----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("-4 Month",$st_sql_strtime);
//$st_sql_caltime=strtotime("-30 day",$st_sql_strtime);
$st_sql_ResultDate=date("Y-m-d", $st_sql_caltime);
$en_sql_ResultDate = $st_sql_tdate; 
//----sql
?>


<?php

 $sql_del = "delete from T_DELIVERY_HEADER";
//$sql_del = "DELETE from T_DELIVERY_HEADER  where cast(DH_DATETIME as date) between '$st_sql_ResultDate' and '$en_sql_ResultDate'";
//$sql_del = "DELETE from T_DELIVERY_HEADER  where CONVERT(nvarchar(10),DH_DATETIME,120) between '$st_sql_ResultDate' and '$en_sql_ResultDate'";
// echo $sql_del;
$stm_del = sqlsrv_prepare ($ConnYS, $sql_del);
if (!$stm_del) {
    throw new Exception("sql_del  T_DELIVERY_HEADER#". $sql_del); 
}
if (!sqlsrv_execute($stm_del)) {
    throw new Exception("sql_del T_DELIVERY_HEADER ไม่สำเหร็จ#" . $sql_del);
}

sqlsrv_commit($ConnYS);

$sql1 = " SELECT
            DH_DOC_CODE
            ,DH_DATETIME
            ,DH_C1_DOC_CODE
            ,DH_VHC_CODE
            ,DH_SUM_CURRENCY
            ,DH_CREATE_BY
            ,DH_SIGN_PART
            ,DH_RECORD_STATUS
            from  T_DELIVERY_HEADER  where DH_DATETIME 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)) {
    $DH_DOC_CODE            = $arr1["DH_DOC_CODE"];
    $DH_DATETIME            = $arr1["DH_DATETIME"];
    $DH_C1_DOC_CODE         = $arr1["DH_C1_DOC_CODE"];
    $DH_VHC_CODE            = $arr1["DH_VHC_CODE"];
    $DH_SUM_CURRENCY        = $arr1["DH_SUM_CURRENCY"];
    $DH_CREATE_BY           = $arr1["DH_CREATE_BY"];
    $DH_SIGN_PART           = $arr1["DH_SIGN_PART"];
    $DH_RECORD_STATUS       = $arr1["DH_RECORD_STATUS"];
   
$DH_DATETIME = DH_DATETIME($DH_DATETIME); 

        if ($ConnYS) {
            sqlsrv_begin_transaction($ConnYS);
                try {


                     $sql_plan = "INSERT into  T_DELIVERY_HEADER (DH_DOC_CODE ,DH_DATETIME ,DH_C1_DOC_CODE ,DH_VHC_CODE ,DH_SUM_CURRENCY ,DH_CREATE_BY ,DH_SIGN_PART ,DH_RECORD_STATUS )  
                                                        values ('".$DH_DOC_CODE."', '".$DH_DATETIME."', '".$DH_C1_DOC_CODE."', '".$DH_VHC_CODE."', '".$DH_SUM_CURRENCY."', '".$DH_CREATE_BY."', '".$DH_SIGN_PART."', '".$DH_RECORD_STATUS."') "; 
                            // echo $sql_plan."<br>".$x;
                        $stm_plan = sqlsrv_prepare ($ConnYS, $sql_plan);
                        if (!$stm_plan) {
                            throw new Exception("sql_plan T_DELIVERY_HEADER#". $sql_plan); 
                        }
                        if (!sqlsrv_execute($stm_plan)) {
                            throw new Exception("sql_plan T_DELIVERY_HEADER ไม่สำเหร็จ#" . $sql_plan);
                        }
       
                        // sqlsrv_rollback($ConnYS);

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

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

function DH_DATETIME($DH_DATETIME){
    $key_str = explode(" ", $DH_DATETIME);
    $sub_str0 = trim($key_str[0]);
    //----
    $key_vol = explode("-", $sub_str0);
    $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;
     }
    $resulte_action_date = $resulte_date.' ' .trim($key_str[1]);   
return $resulte_action_date;
}

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

 ?>
	







