

<?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 = 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_C1_BILL";
$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
            C1_DOCCODE
            ,BR_CODE
            ,INV_NO
            ,BILL_DOCID
            ,BILL_ACTIVE
            ,ACTION_DATE
            ,ACTION_BY
            ,TRANS_INCOME
            ,TRANS_PAY
            from  SITEA_C1_BILL  where ACTION_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)) {
    $C1_DOCCODE         = $arr1["C1_DOCCODE"];
    $BR_CODE            = $arr1["BR_CODE"];
    $INV_NO             = $arr1["INV_NO"];
    $BILL_DOCID         = $arr1["BILL_DOCID"];
    $BILL_ACTIVE        = $arr1["BILL_ACTIVE"];
    $ACTION_DATE        = $arr1["ACTION_DATE"];
    $ACTION_BY          = $arr1["ACTION_BY"];
    $TRANS_INCOME       = $arr1["TRANS_INCOME"];
    $TRANS_PAY          = $arr1["TRANS_PAY"];
   
$ACTION_DATE = ACTION_DATE($ACTION_DATE); 

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


                     $sql_plan = "INSERT into  SITEA_C1_BILL (C1_DOCCODE ,BR_CODE ,INV_NO ,BILL_ACTIVE ,ACTION_DATE ,ACTION_BY ,TRANS_INCOME ,TRANS_PAY )  
                                                        values ('".$C1_DOCCODE."', '".$BR_CODE."', '".$INV_NO."', '".$BILL_ACTIVE."', '".$ACTION_DATE."', '".$ACTION_BY."', '".$TRANS_INCOME."', '".$TRANS_PAY."') "; 
                            // 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++;
    // if($x==10){
    //     die("Stop10");
    // }
}
// echo "Success".$x;
echo "3";

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

function ACTION_DATE($ACTION_DATE){
        $key_str = explode(" ", $ACTION_DATE);
        $sub_str0 = trim($key_str[0]);
        //----
        $key_vol = explode("-", $sub_str0);
            $str0 = trim($key_vol[0]);
            $str1 = trim($key_vol[1]);
            $str2 = trim($key_vol[2]);
        $resulte_date = $str2.'.'.$str1.'.'.$str0.' ' .trim($key_str[1]);   
    return $resulte_date;
}

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

 ?>
	







