

<?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

$sql_del = "delete from FLEET_VEHICLE_SPEC";
    $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
            VID
            ,VHC_CODE
            ,VHC_OWNED_DATE
            ,VHC_AGE
            ,VHC_BRID
            ,VHC_GROUPID
            ,VHC_TYPEID
            ,VHC_FEATUREID
            ,VHC_PLATE_ALP
            ,VHC_PLATE
            ,VHC_PLATE_PROVINCE
            ,VHC_MAKE
            ,VHC_SERIES
            ,VHC_HP
            ,VHC_PLUNGER_QTY
            ,VHC_ENGINE_MANU
            ,VHC_ENGINE_NO
            ,VHC_CHASSIS_NO
            ,VHC_FUEL_TYPE
            ,VHC_WEIGHT
            ,VHC_LOAD_WEIGHT
            ,VHC_SUM_WEIGHT
            ,VHC_SHAFT_QTY
            ,VHC_WHEEL_QTY
            ,VHC_TIRE_QTY
            ,VHC_RIMS_SIZE
            ,VHC_TIRE_SIZE
            ,VHC_GEAR_SERIES
            ,VHC_FINALGEAR_SERIES
            ,VHC_STATUS
            ,VHC_FRONT_IMG_PART
            ,VHC_REAR_IMG_PART
            ,VHC_LEFT_IMG_PART
            ,VHC_RIGHT_IMG_PART
            ,OWNER_VHC_CODE
            ,VHC_ACTION_LOG
            ,VHC_SPECIAL_ADDING
            ,VHC_WIDTH
            ,VHC_HEIGHT
            ,VHC_LENGTH
            ,VHC_GPS_BRAND
            ,VHC_GPS_COST
            ,VHC_RESPON_PERSON
            ,VHC_FLOATING_BALL
            ,VHC_CARD_READER
            ,VHC_CAMERA
            ,VHC_YEAR
            from  FLEET_VEHICLE_SPEC "; 
        // 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)) {
   
    $VID                        = $arr1["VID"];
    $VHC_CODE                   = $arr1["VHC_CODE"];
    $VHC_OWNED_DATE             = $arr1["VHC_OWNED_DATE"];
    $VHC_AGE                    = $arr1["VHC_AGE"];
    $VHC_BRID                   = $arr1["VHC_BRID"];
    $VHC_GROUPID                = $arr1["VHC_GROUPID"];
    $VHC_TYPEID                 = $arr1["VHC_TYPEID"];
    $VHC_FEATUREID              = $arr1["VHC_FEATUREID"];
    $VHC_PLATE_ALP              = $arr1["VHC_PLATE_ALP"];
    $VHC_PLATE                  = $arr1["VHC_PLATE"];
    $VHC_PLATE_PROVINCE         = $arr1["VHC_PLATE_PROVINCE"];
    $VHC_MAKE                   = $arr1["VHC_MAKE"];
    $VHC_SERIES                 = $arr1["VHC_SERIES"];
    $VHC_HP                     = $arr1["VHC_HP"];
    $VHC_PLUNGER_QTY            = $arr1["VHC_PLUNGER_QTY"];
    $VHC_ENGINE_MANU            = $arr1["VHC_ENGINE_MANU"];
    $VHC_ENGINE_NO              = $arr1["VHC_ENGINE_NO"];
    $VHC_CHASSIS_NO             = $arr1["VHC_CHASSIS_NO"];
    $VHC_FUEL_TYPE              = $arr1["VHC_FUEL_TYPE"];
    $VHC_WEIGHT                 = $arr1["VHC_WEIGHT"];
    $VHC_LOAD_WEIGHT            = $arr1["VHC_LOAD_WEIGHT"];
    $VHC_SUM_WEIGHT             = $arr1["VHC_SUM_WEIGHT"];
    $VHC_SHAFT_QTY              = $arr1["VHC_SHAFT_QTY"];
    $VHC_WHEEL_QTY              = $arr1["VHC_WHEEL_QTY"];
    $VHC_TIRE_QTY               = $arr1["VHC_TIRE_QTY"];
    $VHC_RIMS_SIZE              = $arr1["VHC_RIMS_SIZE"];
    $VHC_TIRE_SIZE              = $arr1["VHC_TIRE_SIZE"];
    $VHC_GEAR_SERIES            = $arr1["VHC_GEAR_SERIES"];
    $VHC_FINALGEAR_SERIES       = $arr1["VHC_FINALGEAR_SERIES"];
    $VHC_STATUS                 = $arr1["VHC_STATUS"];
    $VHC_FRONT_IMG_PART         = $arr1["VHC_FRONT_IMG_PART"];
    $VHC_REAR_IMG_PART          = $arr1["VHC_REAR_IMG_PART"];
    $VHC_LEFT_IMG_PART          = $arr1["VHC_LEFT_IMG_PART"];
    $VHC_RIGHT_IMG_PART         = $arr1["VHC_RIGHT_IMG_PART"];
    $OWNER_VHC_CODE             = $arr1["OWNER_VHC_CODE"];
    $VHC_ACTION_LOG             = $arr1["VHC_ACTION_LOG"];
    $VHC_SPECIAL_ADDING         = $arr1["VHC_SPECIAL_ADDING"];
    $VHC_WIDTH                  = $arr1["VHC_WIDTH"];
    $VHC_HEIGHT                 = $arr1["VHC_HEIGHT"];
    $VHC_LENGTH                 = $arr1["VHC_LENGTH"];
    $VHC_GPS_BRAND              = $arr1["VHC_GPS_BRAND"];
    $VHC_GPS_COST               = $arr1["VHC_GPS_COST"];
    $VHC_RESPON_PERSON          = $arr1["VHC_RESPON_PERSON"];
    $VHC_FLOATING_BALL          = $arr1["VHC_FLOATING_BALL"];
    $VHC_CARD_READER            = $arr1["VHC_CARD_READER"];
    $VHC_CAMERA                 = $arr1["VHC_CAMERA"];
    $VHC_YEAR                   = $arr1["VHC_YEAR"];


if($VHC_OWNED_DATE!=""){
    $VHC_OWNED_DATE = CONVERT_DATE543($VHC_OWNED_DATE);
    $VHC_OWNED_DATE = " '".$VHC_OWNED_DATE."' ";
  }else{
    $VHC_OWNED_DATE ="NULL";
  }

  if($VHC_YEAR!=""){
    $VHC_YEAR = CONVERT_DATE($VHC_YEAR);
    $VHC_YEAR = " '".$VHC_YEAR."' ";
  }else{
    $VHC_YEAR = "NULL";
  }
// echo $VHC_YEAR;

//  echo $VHC_YEAR = CONVERT_DATE($VHC_YEAR);

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

                     $sql_brname = "INSERT into  FLEET_VEHICLE_SPEC (VID ,VHC_CODE ,VHC_OWNED_DATE ,VHC_AGE ,VHC_BRID ,VHC_GROUPID  ,VHC_TYPEID  ,VHC_FEATUREID  ,VHC_PLATE_ALP  ,VHC_PLATE  ,VHC_PLATE_PROVINCE  ,VHC_MAKE  ,VHC_SERIES  ,VHC_HP  
                                                                ,VHC_PLUNGER_QTY  ,VHC_ENGINE_MANU  ,VHC_ENGINE_NO  ,VHC_CHASSIS_NO, VHC_WEIGHT, VHC_LOAD_WEIGHT, VHC_SUM_WEIGHT, VHC_SHAFT_QTY, VHC_WHEEL_QTY, VHC_TIRE_QTY, VHC_RIMS_SIZE, VHC_TIRE_SIZE, VHC_GEAR_SERIES
                                                                ,VHC_FINALGEAR_SERIES, VHC_STATUS, VHC_FRONT_IMG_PART, VHC_REAR_IMG_PART, VHC_LEFT_IMG_PART, VHC_RIGHT_IMG_PART, OWNER_VHC_CODE, VHC_ACTION_LOG, VHC_SPECIAL_ADDING, VHC_WIDTH, VHC_HEIGHT
                                                                ,VHC_LENGTH, VHC_GPS_BRAND, VHC_GPS_COST, VHC_RESPON_PERSON, VHC_FLOATING_BALL, VHC_CARD_READER, VHC_CAMERA, VHC_YEAR)  
                                         values ('".$VID."', '".$VHC_CODE."', $VHC_OWNED_DATE, '".$VHC_AGE."', '".$VHC_BRID."', '".$VHC_GROUPID."', '".$VHC_TYPEID."', '".$VHC_FEATUREID."', '".$VHC_PLATE_ALP."', '".$VHC_PLATE."'
                                                , '".$VHC_PLATE_PROVINCE."', '".$VHC_MAKE."', '".$VHC_SERIES."', '".$VHC_HP."', '".$VHC_PLUNGER_QTY."', '".$VHC_ENGINE_MANU."', '".$VHC_ENGINE_NO."', '".$VHC_CHASSIS_NO."', '".$VHC_WEIGHT."'
                                                , '".$VHC_LOAD_WEIGHT."', '".$VHC_SUM_WEIGHT."', '".$VHC_SHAFT_QTY."', '".$VHC_WHEEL_QTY."', '".$VHC_TIRE_QTY."', '".$VHC_RIMS_SIZE."', '".$VHC_TIRE_SIZE."', '".$VHC_GEAR_SERIES."', '".$VHC_FINALGEAR_SERIES."', '".$VHC_STATUS."'
                                                , '".$VHC_FRONT_IMG_PART."', '".$VHC_REAR_IMG_PART."', '".$VHC_LEFT_IMG_PART."', '".$VHC_RIGHT_IMG_PART."', '".$OWNER_VHC_CODE."', '".$VHC_ACTION_LOG."', '".$VHC_SPECIAL_ADDING."', '".$VHC_WIDTH."', '".$VHC_HEIGHT."'
                                                , '".$VHC_LENGTH."', '".$VHC_GPS_BRAND."', '".$VHC_GPS_COST."', '".$VHC_RESPON_PERSON."', '".$VHC_FLOATING_BALL."', '".$VHC_CARD_READER."', '".$VHC_CAMERA."', $VHC_YEAR) "; 
                            // echo $sql_brname."<br>".$x;
                        $stm_plan = sqlsrv_prepare ($ConnYS, $sql_brname);
                        if (!$stm_plan) {
                            throw new Exception("sql_brname  plan#". $sql_brname); 
                        }
                        if (!sqlsrv_execute($stm_plan)) {
                            throw new Exception("sql_brname นำเข้า plan ไม่สำเหร็จ#" . $sql_brname);
                        }
       
                        // sqlsrv_rollback($ConnYS);

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


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

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

function CONVERT_DATE543($VAL_DATE){
    $key_vol = explode("-", $VAL_DATE);
    $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;
     }
return $resulte_date;
}
function CONVERT_DATE($VAL_DATE){
    $key_vol = explode("-", $VAL_DATE);
        $strY = trim($key_vol[0]);
        $strM = trim($key_vol[1]);
        $strD = trim($key_vol[2]);
    $resulte_date = $strY.'-'.$strM.'-'.$strD;   
return $resulte_date;
}

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







