

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

$start_id = start_id($ConnYS);
$end_id = end_id($connFB);

// echo "start_id".$start_id;
// echo "<BR>";
// echo "end_id".$end_id;
// die("stop");

$sql1 = " SELECT
            COST_ID
            ,COST_DOC
            ,TRANVEL_NO
            ,TRUCK_NO
            ,DOC_NO
            ,PV_NO
            ,PV_GROUP
            ,RESON_CANCEL
            ,COST_GROUP
            ,COST_STATUS
            ,TRAVEL_DATE_OUT
            ,TRUCK_TYPE
            ,MILE_TOTAL
            ,TOTAL_AMT
            ,COMPANY
            ,PATHFINDER
            ,DOC_DATE
            ,DRIVENNAME
            ,TRANSFER_DATE
            ,REMARKCANCEL
            ,CREATEDATE
            ,CREATEBY
            ,UPDATEDATE
            ,UPDATEBY
            ,APPROVEDATE
            ,APPROVEBY
            ,CHECKEDBY
            ,CHECKEDDATE
            ,CHECKEDSTATUS
            ,COST_MONTH
            ,COST_YEAR

            from  TRANS_BWCOSTC1
            where   COST_ID  between '".$start_id."' and '".$end_id."' "; 
// echo $sql1;
$rst1 = ibase_query($connFB, $sql1);
// die("Stop");
$x = 0;
while($arr1         = ibase_fetch_assoc($rst1)) {
   
    $COST_ID                  = $arr1["COST_ID"];
    $COST_DOC               = $arr1["COST_DOC"];
    $TRANVEL_NO             = $arr1["TRANVEL_NO"];
    $TRUCK_NO               = $arr1["TRUCK_NO"];
    $DOC_NO                 = $arr1["DOC_NO"];
    $PV_NO                  = $arr1["PV_NO"];
    $PV_GROUP               = $arr1["PV_GROUP"];
    $RESON_CANCEL           = $arr1["RESON_CANCEL"];
    $COST_GROUP             = $arr1["COST_GROUP"];
    $COST_STATUS            = $arr1["COST_STATUS"];
    $TRAVEL_DATE_OUT        = $arr1["TRAVEL_DATE_OUT"];
    $TRUCK_TYPE             = $arr1["TRUCK_TYPE"];
    $MILE_TOTAL             = $arr1["MILE_TOTAL"];
    $TOTAL_AMT              = $arr1["TOTAL_AMT"];
    $COMPANY                = $arr1["COMPANY"];
    $PATHFINDER             = $arr1["PATHFINDER"];
    $DOC_DATE               = $arr1["DOC_DATE"];
    $DRIVENNAME             = $arr1["DRIVENNAME"];
    $TRANSFER_DATE          = $arr1["TRANSFER_DATE"];
    $REMARKCANCEL           = $arr1["REMARKCANCEL"];
    $CREATEDATE             = $arr1["CREATEDATE"];
    $CREATEBY               = $arr1["CREATEBY"];
    $UPDATEDATE             = $arr1["UPDATEDATE"];
    $UPDATEBY               = $arr1["UPDATEBY"];
    $APPROVEDATE            = $arr1["APPROVEDATE"];
    $APPROVEBY              = $arr1["APPROVEBY"];
    $CHECKEDBY              = $arr1["CHECKEDBY"];
    $CHECKEDDATE            = $arr1["CHECKEDDATE"];
    $CHECKEDSTATUS          = $arr1["CHECKEDSTATUS"];
    $COST_MONTH             = $arr1["COST_MONTH"];
    $COST_YEAR              = $arr1["COST_YEAR"];

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

                     $sql_name = "INSERT into  TRANS_BWCOSTC1 (COST_ID ,COST_DOC, TRANVEL_NO, TRUCK_NO, DOC_NO, PV_NO, PV_GROUP, RESON_CANCEL, COST_GROUP, COST_STATUS, TRAVEL_DATE_OUT, TRUCK_TYPE
                                                , MILE_TOTAL, TOTAL_AMT, COMPANY, PATHFINDER, DOC_DATE, DRIVENNAME, TRANSFER_DATE, REMARKCANCEL, CREATEDATE, CREATEBY
                                                , UPDATEDATE, UPDATEBY, APPROVEDATE, APPROVEBY, CHECKEDBY, CHECKEDDATE, CHECKEDSTATUS, COST_MONTH, COST_YEAR)  
                                         values ('".$COST_ID."', '".$COST_DOC."', '".$TRANVEL_NO."', '".$TRUCK_NO."', '".$DOC_NO."', '".$PV_NO."', '".$PV_GROUP."'
                                                , '".$RESON_CANCEL."', '".$COST_GROUP."', '".$COST_STATUS."', '".$TRAVEL_DATE_OUT."', '".$TRUCK_TYPE."', '".$MILE_TOTAL."', '".$TOTAL_AMT."'
                                                , '".$COMPANY."', '".$PATHFINDER."', '".$DOC_DATE."', '".$DRIVENNAME."', '".$TRANSFER_DATE."', '".$REMARKCANCEL."', '".$CREATEDATE."'
                                                , '".$CREATEBY."', '".$UPDATEDATE."', '".$UPDATEBY."', '".$APPROVEDATE."', '".$APPROVEBY."', '".$CHECKEDBY."', '".$CHECKEDDATE."', '".$CHECKEDSTATUS."'
                                                , '".$COST_MONTH."', '".$COST_YEAR."') "; 
                            // echo $sql_name."<br>".$x;
                        $stm_plan = sqlsrv_prepare ($ConnYS, $sql_name);
                        if (!$stm_plan) {
                            throw new Exception("sql_name  plan#". $sql_name); 
                        }
                        if (!sqlsrv_execute($stm_plan)) {
                            throw new Exception("sql_name นำเข้า plan ไม่สำเหร็จ#" . $sql_name);
                        }
       
                        // sqlsrv_rollback($ConnYS);

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


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

write_text("TRANS_BWCOSTC1",$current_time,$htime);
// write_text2("....................................");

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


function end_id($connFB){
    $sql_idex = "SELECT max(COST_ID) as END_INDEX from  TRANS_BWCOSTC1";
    $rs_idex = ibase_query($connFB, $sql_idex);
			$arr_idex = ibase_fetch_assoc($rs_idex); 
			$end_em_idex =  ($arr_idex["END_INDEX"]);
		return $end_em_idex;
}

function start_id($ConnYS){
    $sql_idex = "SELECT (CASE WHEN max(COST_ID) is NULL THEN 0 ELSE max(COST_ID)+1 END) as START_INDEX from TRANS_BWCOSTC1 ";
    $rs_br_idex = sqlsrv_query($ConnYS, $sql_idex);
			$arr_idex = sqlsrv_fetch_array($rs_br_idex); 
			$start_idex =  ($arr_idex["START_INDEX"]);
        return $start_idex;
}
                
 ?>
	







