

<?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("-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
            DT_TOPIC_NO
            ,DT_TOPIC
            ,DT_UNIT
            ,DT_BR_CODE
            ,DT_ORDER_ROW
            ,DT_DATA_TYPE
            ,DT_IS_IMPORTANT
            ,DT_IS_CURRENCY
            ,DT_IS_BEFORE_DELIVERY
            from  L_DELIVERY_DETAIL_TOPIC
        where   DT_TOPIC_NO  between '".$start_id."' and '".$end_id."' "; 
    // echo $sql1;
    $rst1 = ibase_query($connFB, $sql1);
    // die("Stop");
    $x = 0;
    $data = array();
    while($arr1   = ibase_fetch_object($rst1)) {

        $data[] = array( 
                        "DT_TOPIC_NO"                 => $arr1->DT_TOPIC_NO
                        ,"DT_TOPIC"                   => $arr1->DT_TOPIC
                        ,"DT_UNIT"                    => $arr1->DT_UNIT
                        ,"DT_BR_CODE"                 => $arr1->DT_BR_CODE
                        ,"DT_ORDER_ROW"               => $arr1->DT_ORDER_ROW
                        ,"DT_DATA_TYPE"               => $arr1->DT_DATA_TYPE
                        ,"DT_IS_IMPORTANT"            => $arr1->DT_IS_IMPORTANT
                        ,"DT_IS_CURRENCY"             => $arr1->DT_IS_CURRENCY
                        ,"DT_IS_BEFORE_DELIVERY"      => $arr1->DT_IS_BEFORE_DELIVERY
                );
        $x++;
    }

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

                $DT_TOPIC_NO                = $data[$i]["DT_TOPIC_NO"];
                $DT_TOPIC                   = $data[$i]["DT_TOPIC"];
                $DT_UNIT                    = $data[$i]["DT_UNIT"];
                $DT_BR_CODE                 = $data[$i]["DT_BR_CODE"];
                $DT_ORDER_ROW               = $data[$i]["DT_ORDER_ROW"];
                $DT_DATA_TYPE               = $data[$i]["DT_DATA_TYPE"];
                $DT_IS_IMPORTANT            = $data[$i]["DT_IS_IMPORTANT"];
                $DT_IS_CURRENCY             = $data[$i]["DT_IS_CURRENCY"];
                $DT_IS_BEFORE_DELIVERY      = $data[$i]["DT_IS_BEFORE_DELIVERY"];

                $sql_name = "INSERT into  L_DELIVERY_DETAIL_TOPIC (DT_TOPIC_NO ,DT_TOPIC, DT_UNIT, DT_BR_CODE, DT_ORDER_ROW, DT_DATA_TYPE, DT_IS_IMPORTANT, DT_IS_CURRENCY, DT_IS_BEFORE_DELIVERY )  
                            values ('".$DT_TOPIC_NO."', '".$DT_TOPIC."', '".$DT_UNIT."', '".$DT_BR_CODE."', '".$DT_ORDER_ROW."', '".$DT_DATA_TYPE."', '".$DT_IS_IMPORTANT."', '".$DT_IS_CURRENCY."', '".$DT_IS_BEFORE_DELIVERY."') "; 
                    // 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_commit($ConnYS);
            // sqlsrv_rollback($ConnYS);
            $result = array("STATUS" => "1", "RESULT" => 'Seuccess :'.$i);
            write_text("L_DELIVERY_DETAIL_TOPIC",$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 = "L_DELIVERY_DETAIL_TOPIC";
            INSERT_SYNLOG($connLog,"ไม่บันทึกดาต้าเบส",$TABLE,"FAIL");
        }
        sqlsrv_close($ConnYS); 
    }

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

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

function write_text($L_DELIVERY_DETAIL_TOPIC,$current_time,$htime){
    $t_string = "$L_DELIVERY_DETAIL_TOPIC".'  '."$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(DT_TOPIC_NO) as END_INDEX from  L_DELIVERY_DETAIL_TOPIC";
    $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(DT_TOPIC_NO) is NULL THEN 0 ELSE max(DT_TOPIC_NO)+1 END) as START_INDEX from L_DELIVERY_DETAIL_TOPIC ";
    $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;
}
                
 ?>
	







