

<?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("-3 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+543);  
// $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("-3 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
?>


<?php

    // $sql_del = "delete from SITEA_C1_VDRIVER";
    $sql_del = "DELETE vdr from SITEA_C1_VDRIVER  vdr   
                        inner join  SITEA_C1_HEADER sc1 
                        on vdr.c1_doccode=sc1.doc_code 
                        and vdr.br_code=sc1.br_code
                    and sc1.doc_date between '$st_sql_ResultDate' and '$en_sql_ResultDate' "; 
    $stm_del = sqlsrv_prepare ($ConnYS, $sql_del);

    if (!$stm_del) {
        throw new Exception("sql_del  SITEA_C1_VDRIVER#". $sql_del); 
    }
    if (!sqlsrv_execute($stm_del)) {
        throw new Exception("sql_del SITEA_C1_VDRIVER ไม่สำเหร็จ#" . $sql_del);
    }

        $sql1 = " SELECT
                vdr.C1_DOCCODE
                , vdr.BR_CODE
                , vdr.VHC_CODE
                , vdr.EM_CODE
                , vdr.EM_NAME
                , vdr.EM_POSITIONNAME 
                from SITEA_C1_VDRIVER  vdr   
                inner join  SITEA_C1_HEADER sc1 
                on vdr.c1_doccode=sc1.doc_code and vdr.br_code=sc1.br_code
                and sc1.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);

        $x = 0;
        while($arr1  = ibase_fetch_object($rst1)) {

            $data[] = array( 
                            "C1_DOCCODE"                 => $arr1->C1_DOCCODE
                            ,"BR_CODE"                    => $arr1->BR_CODE
                            ,"VHC_CODE"                   => $arr1->VHC_CODE
                            ,"EM_CODE"                    => $arr1->EM_CODE
                            ,"EM_NAME"                    => $arr1->EM_NAME
                            ,"EM_POSITIONNAME"            => $arr1->EM_POSITIONNAME
                        );
            $x++;

        }
    // print_r($data);

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

                $C1_DOCCODE                 = $data[$i]["C1_DOCCODE"];
                $BR_CODE                    = $data[$i]["BR_CODE"];
                $VHC_CODE                   = $data[$i]["VHC_CODE"];
                $EM_CODE                    = $data[$i]["EM_CODE"];
                $EM_NAME                    = $data[$i]["EM_NAME"];
                $EM_POSITIONNAME            = $data[$i]["EM_POSITIONNAME"];

                $sql_plan = "INSERT into  SITEA_C1_VDRIVER (C1_DOCCODE ,BR_CODE ,VHC_CODE ,EM_CODE ,EM_NAME ,EM_POSITIONNAME )  
                                values ('".$C1_DOCCODE."', '".$BR_CODE."', '".$VHC_CODE."', '".$EM_CODE."', '".$EM_NAME."', '".$EM_POSITIONNAME."') "; 
                // echo $sql_plan."<br>".$x;
                $stm_plan = sqlsrv_prepare ($ConnYS, $sql_plan);
                if (!$stm_plan) {
                    throw new Exception("sql_plan  SITEA_C1_VDRIVER#". $sql_plan); 
                }
                if (!sqlsrv_execute($stm_plan)) {
                    throw new Exception("sql_plan SITEA_C1_VDRIVER#" . $sql_plan);
                }
            }

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

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

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

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

                
 ?>
	







