<?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);
    $s_trime = $yearY.$yearM.$yearD;
    $DateStart=date($s_trime);
    $strtime = strtotime($DateStart);
    $caltime=strtotime("-2 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("-2 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

    // $sql_del = "delete from T_DELIVERY_DETAIL";
    $sql_del = "DELETE tdd from T_DELIVERY_HEADER tdh
                    inner join T_DELIVERY_DETAIL  tdd
                    on tdh.dh_doc_code = tdd.dh_doc_code
                where cast(tdh.DH_DATETIME as date ) between '$st_sql_ResultDate' and '$en_sql_ResultDate' ";
    $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);

    $sql1 = "   SELECT
                    tdd.DH_DOC_CODE
                    ,tdd.DT_TOPIC_NO
                    ,tdd.DD_VALUE
                    ,tdd.DD_RECORD_STATUS
                    from  T_DELIVERY_HEADER  tdh
                inner join T_DELIVERY_DETAIL  tdd
                on tdh.dh_doc_code = tdd.dh_doc_code
                where cast(tdh.DH_DATETIME as 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_object($rst1)) {

        $data[] = array( 
                         "DH_DOC_CODE"            => $arr1->DH_DOC_CODE
                        ,"DT_TOPIC_NO"            => $arr1->DT_TOPIC_NO
                        ,"DD_VALUE"               => $arr1->DD_VALUE
                        ,"DD_RECORD_STATUS"       => $arr1->DD_RECORD_STATUS
                    );
        $x++;
    
    }

     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++){

                $DH_DOC_CODE            =  $data[$i]["DH_DOC_CODE"];
                $DT_TOPIC_NO            =  $data[$i]["DT_TOPIC_NO"];
                $DD_VALUE               =  $data[$i]["DD_VALUE"];
                $DD_RECORD_STATUS       =  $data[$i]["DD_RECORD_STATUS"];

                $sql_plan = "INSERT into  T_DELIVERY_DETAIL (DH_DOC_CODE ,DT_TOPIC_NO ,DD_VALUE ,DD_RECORD_STATUS )  
                            values ('".$DH_DOC_CODE."', '".$DT_TOPIC_NO."', '".$DD_VALUE."', '".$DD_RECORD_STATUS."') "; 

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

// echo "13";

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

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

 ?>
	







