

<?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);
// // $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("-2 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


//DELETE//----sql ใช้กับที่ดึงวันที่จาก DOC_CODE
$YM1 = date("Y")+543;
$en1_del_tdate = date($YM1.'-m-d');
$st_del_tdate = date('Y-m-d');
$st_del_DateStart=date($st_del_tdate);
$st_del_strtime = strtotime($st_del_DateStart);
$st_sql_caltime=strtotime("-2 Month",$st_del_strtime);
// $st_sql_caltime=strtotime("-30 day",$st_del_strtime);
$st_del_ResultDate=date("Y-m-d", $st_sql_caltime);
$key_del = explode("-", $st_del_ResultDate);
$str_del = (($key_del[0])+543).($key_del[1]).($key_del[2]);
//----sql
// str_replace("-","",$st_del_tdate);
$start_del = substr($str_del,2,4);
$end_del = substr(str_replace("-","",$en1_del_tdate),2,4);

?>


<?php

$sql_del = "DELETE scb 
            from SITEA_C1_BILL  scb   
            where SUBSTRING(scb.C1_DOCCODE, LEN(scb.BR_CODE)+1, 4) between '".$start_del."' and '".$end_del."' ";
// $sql_del = "delete from SITEA_C1_BILL where ACTION_DATE between '".$st_sql_ResultDate."' and '".$en_sql_ResultDate."' ";
// $sql_del = "delete from SITEA_C1_BILL";
// echo $sql_del; die();
$stm_del = sqlsrv_prepare ($ConnYS, $sql_del);
if (!$stm_del) {
    throw new Exception("sql_del  SITEA_C1_BILL#". $sql_del); 
}
if (!sqlsrv_execute($stm_del)) {
    throw new Exception("sql_del SITEA_C1_BILL ไม่สำเหร็จ#" . $sql_del);
}


sqlsrv_commit($ConnYS);

$sql1 = " SELECT
            scb.C1_DOCCODE
            ,scb.BR_CODE
            ,scb.INV_NO
            ,char_length(scb.INV_NO) as L_INVNO
            ,scb.BILL_DOCID
            ,scb.BILL_ACTIVE
            ,scb.ACTION_DATE
            ,scb.ACTION_BY
            ,scb.TRANS_INCOME
            ,scb.TRANS_PAY
            from SITEA_C1_BILL  scb 
            where substring(scb.C1_DOCCODE from char_length(scb.BR_CODE)+1 for 4) between '".$start_del."' and '".$end_del."' ";
           // where ACTION_DATE between '".$s_trime."' and '".$current_time."'        
// 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             = trim($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"];
    $L_INVNO            = $arr1["L_INVNO"];

    if($L_INVNO <=30){
        $INV_NO = $INV_NO;
    }else{
        $INV_NO  = substr($INV_NO,1,30);  
        $INV_NO =  htmlspecialchars( $INV_NO );
    }
// $INV_NO = INV_NO($INV_NO); 
// echo $INV_NO;
// die();
$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_DOCID,BILL_ACTIVE ,ACTION_DATE ,ACTION_BY ,TRANS_INCOME ,TRANS_PAY )  
                                                        values ('".$C1_DOCCODE."', '".$BR_CODE."', '".$INV_NO."', '".$BILL_DOCID."', '".$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."ไม่บันทึกดาต้าเบส";
                $TABLE = "SITEA_C1_BILL";
                INSERT_SYNLOG($connLog,"ไม่บันทึกดาต้าเบส",$TABLE,"FAIL");
            }
        }
 
    $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 ACTION_DATE($ACTION_DATE){
    $key_str = explode(" ", $ACTION_DATE);
    $sub_str0 = trim($key_str[0]);
    //----
    $key_vol = explode("-", $sub_str0);
    $strY = trim($key_vol[0]);
    $strM = trim($key_vol[1]);
    $strD = trim($key_vol[2]);
    $sub_y = substr($strY,0,2);
    if($sub_y=="25"){
        $resulte_date = ($strY-543).'-'.$strM.'-'.$strD;
     }else{
        $resulte_date = ($strY).'-'.$strM.'-'.$strD;
     }
    $resulte_action_date = $resulte_date.' ' .trim($key_str[1]);   
return $resulte_action_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);
    }

  function INV_NO($INV_NO){
    // echo  $INV_NO;
        if(strlen($INV_NO)>30){
              //  return   $INV_NO  = substr($INV_NO,1,strlen($INV_NO));
               $INV_NO  = substr($INV_NO,1,30);  
               echo  htmlspecialchars( $INV_NO );
        }else{
            echo  htmlspecialchars( $INV_NO );
        } 
    }

 ?>
	







