

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

$sql_del = "delete from HR_EM_HEADER";
    $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
                EM_ID
                ,EM_CODE
                ,CONTRACT_CODE
                ,CONTRACT_DATE
                ,CONTRACT_TYPE
                ,POSITION_ID
                ,BRANCH_ID
                ,SEX
                ,FIRSTNAME
                ,LASTNAME
                ,NICKNAME
                ,BIRTHDAY
                ,AGE
                ,NATIONALITY
                ,RACE
                ,RELIGION
                ,CARD_NO
                ,CARD_BY
                ,CARD_PROVINCE
                ,CARD_DATE
                ,CARD_EXPIRE
                ,ADDRESS1
                ,DISTRICT1
                ,PROVINCE1
                ,ZIPCODE1
                ,COUNTRY1
                ,TEL1
                ,ADDRESS2
                ,DISTRICT2
                ,PROVINCE2
                ,ZIPCODE2
                ,COUNTRY2
                ,IMEI
                ,DETAILS
                ,EM_STATUS
                ,IMAGE_PATH
                ,MASTER_COURSE
                ,VEHICLE_PLATE
                ,OWNER_FIRSTNAME
                ,OWNER_LASTNAME
                ,PAYMENT_METHOD
                ,CARD_IMAGE_PART
                ,LICENSE_IMAGE_PART
                ,LOGIN_USERNAME
                ,LOGIN_PASSWORD
                ,LICENSE_CARD_NO
                ,LICENSE_CARD_DATE
                ,LICENSE_CARD_EXPIRE
                ,LOG_INSERT
                ,LOG_UPDATE
                ,EM_INSURANCE_LIMIT
                ,MAX_COD
                ,BR_ID_SUB
                ,TYPE_TAX
                ,TYPE_COMPANY
                ,USER_BANK
                ,BANK_NAME
                ,BANK_ACCOUNT
                ,WAGE_INSURANCE
                ,EM_OUTOFF
                ,SUBDISTRICT1
                ,SUBDISTRICT2
                ,ADDRESSNAME3
                ,ADDRESS3
                ,SUBDISTRICT3
                ,DISTRICT3
                ,PROVINCE3
                ,ZIPCODE3
                ,TEL3
                ,ADDRESSNAME4
                ,ADDRESS4
                ,SUBDISTRICT4
                ,DISTRICT4
                ,PROVINCE4
                ,ZIPCODE4
                ,TEL4
                ,OWNER_TAX_ID
                ,AD_GEN_ID
                ,VHC_CODE_EM
                ,EM_CODE_EXPRESS
                ,EM_NAME_EXPRESS
                ,EM_CONTACK_TYPE
                ,EM_CONTACK_SARTDATE
                ,EM_CONTACK_ENDDATE
                ,EM_CONTACK_ANOTATION
                from  HR_EM_HEADER "; 
            // where doc_date between '20.05.2563' and '20.06.2564' 
    // echo $sql1;
    $rst1 = ibase_query($connFB, $sql1);
    // die("Stop");
    $x = 0;
    $data = array();
    while($arr1   = ibase_fetch_object($rst1)) {

      $data[] = array( 

                     "EM_ID"                      => $arr1->EM_ID
                    ,"EM_CODE"                    => $arr1->EM_CODE
                    ,"CONTRACT_CODE"              => $arr1->CONTRACT_CODE
                    ,"CONTRACT_DATE"              => $arr1->CONTRACT_DATE
                    ,"CONTRACT_TYPE"              => $arr1->CONTRACT_TYPE
                    ,"POSITION_ID"                => $arr1->POSITION_ID
                    ,"BRANCH_ID"                  => $arr1->BRANCH_ID
                    ,"SEX"                        => $arr1->SEX
                    ,"FIRSTNAME"                  => $arr1->FIRSTNAME
                    ,"LASTNAME"                   => $arr1->LASTNAME
                    ,"NICKNAME"                   => $arr1->NICKNAME
                    ,"BIRTHDAY"                   => $arr1->BIRTHDAY
                    ,"AGE"                        => $arr1->AGE
                    ,"NATIONALITY"                => $arr1->NATIONALITY
                    ,"RACE"                       => $arr1->RACE
                    ,"RELIGION"                   => $arr1->RELIGION
                    ,"CARD_NO"                    => $arr1->CARD_NO
                    ,"CARD_BY"                    => $arr1->CARD_BY
                    ,"CARD_PROVINCE"              => $arr1->CARD_PROVINCE
                    ,"CARD_DATE"                  => $arr1->CARD_DATE
                    ,"CARD_EXPIRE"                => $arr1->CARD_EXPIRE
                    ,"ADDRESS1"                   => $arr1->ADDRESS1
                    ,"DISTRICT1"                  => $arr1->DISTRICT1
                    ,"PROVINCE1"                  => $arr1->PROVINCE1
                    ,"ZIPCODE1"                   => $arr1->ZIPCODE1
                    ,"COUNTRY1"                   => $arr1->COUNTRY1
                    ,"TEL1"                       => $arr1->TEL1
                    ,"ADDRESS2"                   => $arr1->ADDRESS2
                    ,"DISTRICT2"                  => $arr1->DISTRICT2
                    ,"PROVINCE2"                  => $arr1->PROVINCE2
                    ,"ZIPCODE2"                   => $arr1->ZIPCODE2
                    ,"COUNTRY2"                   => $arr1->COUNTRY2
                    ,"IMEI"                       => $arr1->IMEI
                    ,"DETAILS"                    => $arr1->DETAILS
                    ,"EM_STATUS"    				      => $arr1->EM_STATUS
                    ,"IMAGE_PATH"                 => $arr1->IMAGE_PATH
                    ,"MASTER_COURSE"              => $arr1->MASTER_COURSE
                    ,"VEHICLE_PLATE"              => $arr1->VEHICLE_PLATE
                    ,"OWNER_FIRSTNAME"            => $arr1->OWNER_FIRSTNAME
                    ,"OWNER_LASTNAME"             => $arr1->OWNER_LASTNAME
                    ,"PAYMENT_METHOD"             => $arr1->PAYMENT_METHOD
                    ,"CARD_IMAGE_PART"            => $arr1->CARD_IMAGE_PART
                    ,"LICENSE_IMAGE_PART"         => $arr1->LICENSE_IMAGE_PART
                    ,"LOGIN_USERNAME"             => $arr1->LOGIN_USERNAME
                    ,"LOGIN_PASSWORD"             => $arr1->LOGIN_PASSWORD
                    ,"LICENSE_CARD_NO"            => $arr1->LICENSE_CARD_NO
                    ,"LICENSE_CARD_DATE"          => $arr1->LICENSE_CARD_DATE
                    ,"LICENSE_CARD_EXPIRE"        => $arr1->LICENSE_CARD_EXPIRE
                    ,"LOG_INSERT"                 => $arr1->LOG_INSERT
                    ,"LOG_UPDATE"                 => $arr1->LOG_UPDATE
                    ,"EM_INSURANCE_LIMIT"         => $arr1->EM_INSURANCE_LIMIT
                    ,"MAX_COD"                    => $arr1->MAX_COD
                    ,"BR_ID_SUB"                  => $arr1->BR_ID_SUB
                    ,"TYPE_TAX"                   => $arr1->TYPE_TAX
                    ,"TYPE_COMPANY"               => $arr1->TYPE_COMPANY
                    ,"USER_BANK"                  => $arr1->USER_BANK
                    ,"BANK_NAME"                  => $arr1->BANK_NAME
                    ,"BANK_ACCOUNT"               => $arr1->BANK_ACCOUNT
                    ,"WAGE_INSURANCE"             => $arr1->WAGE_INSURANCE
                    ,"EM_OUTOFF"                  => $arr1->EM_OUTOFF
                    ,"SUBDISTRICT1"               => $arr1->SUBDISTRICT1
                    ,"SUBDISTRICT2"               => $arr1->SUBDISTRICT2
                    ,"ADDRESSNAME3"               => $arr1->ADDRESSNAME3
                    ,"ADDRESS3"                   => $arr1->ADDRESS3
                    ,"SUBDISTRICT3"               => $arr1->SUBDISTRICT3
                    ,"DISTRICT3"                  => $arr1->DISTRICT3
                    ,"PROVINCE3"                  => $arr1->PROVINCE3
                    ,"ZIPCODE3"                   => $arr1->ZIPCODE3
                    ,"TEL3"                       => $arr1->TEL3
                    ,"ADDRESSNAME4"               => $arr1->ADDRESSNAME4
                    ,"ADDRESS4"                   => $arr1->ADDRESS4
                    ,"SUBDISTRICT4"               => $arr1->SUBDISTRICT4
                    ,"DISTRICT4"                  => $arr1->DISTRICT4
                    ,"PROVINCE4"                  => $arr1->PROVINCE4
                    ,"ZIPCODE4"                   => $arr1->ZIPCODE4
                    ,"TEL4"                       => $arr1->TEL4
                    ,"OWNER_TAX_ID"               => $arr1->OWNER_TAX_ID
                    ,"AD_GEN_ID"                  => $arr1->AD_GEN_ID
                    ,"VHC_CODE_EM"                => $arr1->VHC_CODE_EM
                    ,"EM_CODE_EXPRESS"            => $arr1->EM_CODE_EXPRESS
                    ,"EM_NAME_EXPRESS"            => $arr1->EM_NAME_EXPRESS
                    ,"EM_CONTACK_TYPE"            => $arr1->EM_CONTACK_TYPE
                    ,"EM_CONTACK_SARTDATE"        => $arr1->EM_CONTACK_SARTDATE
                    ,"EM_CONTACK_ENDDATE"         => $arr1->EM_CONTACK_ENDDATE
                    ,"EM_CONTACK_ANOTATION"       => $arr1->EM_CONTACK_ANOTATION

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

                // $TID                = $data[$i]["TID"];
                // $TYPE_NAME          = $data[$i]["TYPE_NAME"];

                    $EM_ID                      = $data[$i]["EM_ID"];
                    $EM_CODE                    = $data[$i]["EM_CODE"];
                    $CONTRACT_CODE              = $data[$i]["CONTRACT_CODE"];
                    $CONTRACT_DATE              = $data[$i]["CONTRACT_DATE"];
                    $CONTRACT_TYPE              = $data[$i]["CONTRACT_TYPE"];
                    $POSITION_ID                = $data[$i]["POSITION_ID"];
                    $BRANCH_ID                  = $data[$i]["BRANCH_ID"];
                    $SEX                        = $data[$i]["SEX"];
                    $FIRSTNAME                  = $data[$i]["FIRSTNAME"];
                    $LASTNAME                   = $data[$i]["LASTNAME"];
                    $NICKNAME                   = $data[$i]["NICKNAME"];
                    $BIRTHDAY                   = $data[$i]["BIRTHDAY"];
                    $AGE                        = $data[$i]["AGE"];
                    $NATIONALITY                = $data[$i]["NATIONALITY"];
                    $RACE                       = $data[$i]["RACE"];
                    $RELIGION                   = $data[$i]["RELIGION"];
                    $CARD_NO                    = $data[$i]["CARD_NO"];
                    $CARD_BY                    = $data[$i]["CARD_BY"];
                    $CARD_PROVINCE              = $data[$i]["CARD_PROVINCE"];
                    $CARD_DATE                  = $data[$i]["CARD_DATE"];
                    $CARD_EXPIRE                = $data[$i]["CARD_EXPIRE"];
                    $ADDRESS1                   = $data[$i]["ADDRESS1"];
                    $DISTRICT1                  = $data[$i]["DISTRICT1"];
                    $PROVINCE1                  = $data[$i]["PROVINCE1"];
                    $ZIPCODE1                   = $data[$i]["ZIPCODE1"];
                    $COUNTRY1                   = $data[$i]["COUNTRY1"];
                    $TEL1                       = $data[$i]["TEL1"];
                    $ADDRESS2                   = $data[$i]["ADDRESS2"];
                    $DISTRICT2                  = $data[$i]["DISTRICT2"];
                    $PROVINCE2                  = $data[$i]["PROVINCE2"];
                    $ZIPCODE2                   = $data[$i]["ZIPCODE2"];
                    $COUNTRY2                   = $data[$i]["COUNTRY2"];
                    $IMEI                       = $data[$i]["IMEI"];
                    $DETAILS                    = $data[$i]["DETAILS"];
                    $EM_STATUS    				      = trim($data[$i]["EM_STATUS"]);
                    $IMAGE_PATH                 = $data[$i]["IMAGE_PATH"];
                    $MASTER_COURSE              = $data[$i]["MASTER_COURSE"];
                    $VEHICLE_PLATE              = $data[$i]["VEHICLE_PLATE"];
                    $OWNER_FIRSTNAME            = $data[$i]["OWNER_FIRSTNAME"];
                    $OWNER_LASTNAME             = $data[$i]["OWNER_LASTNAME"];
                    $PAYMENT_METHOD             = $data[$i]["PAYMENT_METHOD"];
                    $CARD_IMAGE_PART            = $data[$i]["CARD_IMAGE_PART"];
                    $LICENSE_IMAGE_PART         = $data[$i]["LICENSE_IMAGE_PART"];
                    $LOGIN_USERNAME             = $data[$i]["LOGIN_USERNAME"];
                    $LOGIN_PASSWORD             = $data[$i]["LOGIN_PASSWORD"];
                    $LICENSE_CARD_NO            = $data[$i]["LICENSE_CARD_NO"];
                    $LICENSE_CARD_DATE          = $data[$i]["LICENSE_CARD_DATE"];
                    $LICENSE_CARD_EXPIRE        = $data[$i]["LICENSE_CARD_EXPIRE"];
                    $LOG_INSERT                 = $data[$i]["LOG_INSERT"];
                    $LOG_UPDATE                 = $data[$i]["LOG_UPDATE"];
                    $EM_INSURANCE_LIMIT         = $data[$i]["EM_INSURANCE_LIMIT"];
                    $MAX_COD                    = $data[$i]["MAX_COD"];
                    $BR_ID_SUB                  = $data[$i]["BR_ID_SUB"];
                    $TYPE_TAX                   = $data[$i]["TYPE_TAX"];
                    $TYPE_COMPANY               = $data[$i]["TYPE_COMPANY"];
                    $USER_BANK                  = $data[$i]["USER_BANK"];
                    $BANK_NAME                  = $data[$i]["BANK_NAME"];
                    $BANK_ACCOUNT               = $data[$i]["BANK_ACCOUNT"];
                    $WAGE_INSURANCE             = $data[$i]["WAGE_INSURANCE"];
                    $EM_OUTOFF                  = $data[$i]["EM_OUTOFF"];
                    $SUBDISTRICT1                = $data[$i]["SUBDISTRICT1"];
                    $SUBDISTRICT2                = $data[$i]["SUBDISTRICT2"];
                    $ADDRESSNAME3                = $data[$i]["ADDRESSNAME3"];
                    $ADDRESS3                    = $data[$i]["ADDRESS3"];
                    $SUBDISTRICT3                = $data[$i]["SUBDISTRICT3"];
                    $DISTRICT3                   = $data[$i]["DISTRICT3"];
                    $PROVINCE3                   = $data[$i]["PROVINCE3"];
                    $ZIPCODE3                    = $data[$i]["ZIPCODE3"];
                    $TEL3                        = $data[$i]["TEL3"];
                    $ADDRESSNAME4                = $data[$i]["ADDRESSNAME4"];
                    $ADDRESS4                    = $data[$i]["ADDRESS4"];
                    $SUBDISTRICT4                = $data[$i]["SUBDISTRICT4"];
                    $DISTRICT4                   = $data[$i]["DISTRICT4"];
                    $PROVINCE4                   = $data[$i]["PROVINCE4"];
                    $ZIPCODE4                    = $data[$i]["ZIPCODE4"];
                    $TEL4                        = $data[$i]["TEL4"];
                    $OWNER_TAX_ID                = $data[$i]["OWNER_TAX_ID"];
                    $AD_GEN_ID                   = $data[$i]["AD_GEN_ID"];
                    $VHC_CODE_EM                  = $data[$i]["VHC_CODE_EM"];
                    $EM_CODE_EXPRESS              = $data[$i]["EM_CODE_EXPRESS"];
                    $EM_NAME_EXPRESS              = $data[$i]["EM_NAME_EXPRESS"];
                    $EM_CONTACK_TYPE              = $data[$i]["EM_CONTACK_TYPE"];
                    $EM_CONTACK_SARTDATE          = $data[$i]["EM_CONTACK_SARTDATE"];
                    $EM_CONTACK_ENDDATE           = $data[$i]["EM_CONTACK_ENDDATE"];
                    $EM_CONTACK_ANOTATION         = $data[$i]["EM_CONTACK_ANOTATION"];

                  if($CONTRACT_DATE!=""){
                    $CONTRACT_DATE = CONVERT_DATE543($CONTRACT_DATE);
                    $CONTRACT_DATE = " '".$CONTRACT_DATE."' ";
                  }else{
                    $CONTRACT_DATE ="NULL";
                  }
                  if($BIRTHDAY!=""){
                    $BIRTHDAY = CONVERT_DATE543($BIRTHDAY);
                    $BIRTHDAY = " '".$BIRTHDAY."' ";
                    }else{
                      $BIRTHDAY ="NULL";
                    }
                  if($CARD_DATE!=""){
                    $CARD_DATE = CONVERT_DATE543($CARD_DATE);
                    $CARD_DATE = " '".$CARD_DATE."' ";
                  }else{
                    $CARD_DATE ="NULL";
                  }
                if($CARD_EXPIRE!=""){
                    $CARD_EXPIRE = CONVERT_DATE543($CARD_EXPIRE);
                    $CARD_EXPIRE = " '".$CARD_EXPIRE."' ";
                  }else{
                    $CARD_EXPIRE ="NULL";
                  }
                if($LICENSE_CARD_DATE!=""){
                    $LICENSE_CARD_DATE = CONVERT_DATE543($LICENSE_CARD_DATE);
                    $LICENSE_CARD_DATE = " '".$LICENSE_CARD_DATE."' ";
                  }else{
                    $LICENSE_CARD_DATE ="NULL";
                  }
                if($LICENSE_CARD_EXPIRE!=""){
                    $LICENSE_CARD_EXPIRE = CONVERT_DATE543($LICENSE_CARD_EXPIRE);
                    $LICENSE_CARD_EXPIRE = " '".$LICENSE_CARD_EXPIRE."' ";
                  }else{
                    $LICENSE_CARD_EXPIRE ="NULL";
                  }
                  if($EM_INSURANCE_LIMIT==""){
                    $EM_INSURANCE_LIMIT ="NULL";
                  }
                  if($BR_ID_SUB==""){
                    $BR_ID_SUB ="NULL";
                  }
                  if($WAGE_INSURANCE==""){
                    $WAGE_INSURANCE ="NULL";
                  }
                  if($EM_OUTOFF==""){
                    $EM_OUTOFF ="25";
                  }

                    $sql_brname = "INSERT into  HR_EM_HEADER (EM_ID ,EM_CODE ,CONTRACT_CODE ,CONTRACT_DATE ,CONTRACT_TYPE ,POSITION_ID  ,BRANCH_ID  ,SEX  ,FIRSTNAME  ,LASTNAME  ,NICKNAME  ,BIRTHDAY  ,AGE  ,NATIONALITY  
                                                ,RACE  ,RELIGION  ,CARD_NO  ,CARD_BY, CARD_DATE, CARD_EXPIRE, ADDRESS1, DISTRICT1, PROVINCE1, ZIPCODE1, COUNTRY1, TEL1, ADDRESS2
                                                ,DISTRICT2, PROVINCE2, ZIPCODE2, COUNTRY2, IMEI, DETAILS, EM_STATUS, IMAGE_PATH, MASTER_COURSE, VEHICLE_PLATE, OWNER_FIRSTNAME
                                                ,OWNER_LASTNAME, PAYMENT_METHOD, CARD_IMAGE_PART, LICENSE_IMAGE_PART, LOGIN_USERNAME, LOGIN_PASSWORD, LICENSE_CARD_NO, LICENSE_CARD_DATE
                                                ,LICENSE_CARD_EXPIRE, LOG_INSERT, LOG_UPDATE, EM_INSURANCE_LIMIT, MAX_COD, BR_ID_SUB, TYPE_TAX, TYPE_COMPANY, USER_BANK, BANK_NAME, BANK_ACCOUNT, WAGE_INSURANCE, EM_OUTOFF
                                                ,SUBDISTRICT1 , SUBDISTRICT2, ADDRESSNAME3, ADDRESS3, SUBDISTRICT3, DISTRICT3, PROVINCE3, ZIPCODE3, TEL3, ADDRESSNAME4, ADDRESS4, SUBDISTRICT4, DISTRICT4, PROVINCE4, ZIPCODE4, TEL4, OWNER_TAX_ID, AD_GEN_ID
                                                , VHC_CODE_EM, EM_CODE_EXPRESS, EM_NAME_EXPRESS, EM_CONTACK_TYPE, EM_CONTACK_SARTDATE, EM_CONTACK_ENDDATE, EM_CONTACK_ANOTATION)  
                                  values ('".$EM_ID."', '".$EM_CODE."', '".$CONTRACT_CODE."', $CONTRACT_DATE, '".$CONTRACT_TYPE."', '".$POSITION_ID."', '".$BRANCH_ID."', '".$SEX."', '".$FIRSTNAME."', '".$LASTNAME."'
                                        , '".$NICKNAME."', $BIRTHDAY, '".$AGE."', '".$NATIONALITY."', '".$RACE."', '".$RELIGION."', '".$CARD_NO."', '".$CARD_BY."', $CARD_DATE
                                        ,   $CARD_EXPIRE, '".$ADDRESS1."', '".$DISTRICT1."', '".$PROVINCE1."', '".$ZIPCODE1."', '".$COUNTRY1."', '".$TEL1."', '".$ADDRESS2."', '".$DISTRICT2."', '".$PROVINCE2."'
                                        , '".$ZIPCODE2."', '".$COUNTRY2."', '".$IMEI."', '".$DETAILS."', REPLACE(REPLACE(REPLACE(REPLACE(".$EM_STATUS.", CHAR(9), ''), CHAR(10), ''), CHAR(13), ''), CHAR(32), ''), '".$IMAGE_PATH."', '".$MASTER_COURSE."', '".$VEHICLE_PLATE."', '".$OWNER_FIRSTNAME."'
                                        , '".$OWNER_LASTNAME."', '".$PAYMENT_METHOD."', '".$CARD_IMAGE_PART."', '".$LICENSE_IMAGE_PART."', '".$LOGIN_USERNAME."', '".$LOGIN_PASSWORD."', '".$LICENSE_CARD_NO."', $LICENSE_CARD_DATE
                                        ,  $LICENSE_CARD_EXPIRE, '".$LOG_INSERT."', '".$LOG_UPDATE."', ".$EM_INSURANCE_LIMIT.", '".$MAX_COD."', ".$BR_ID_SUB.", '".$TYPE_TAX."', '".$TYPE_COMPANY."'
                                        , '".$USER_BANK."', '".$BANK_NAME."', '".$BANK_ACCOUNT."', ".$WAGE_INSURANCE.", '".$EM_OUTOFF."' , '".$SUBDISTRICT1."', '".$SUBDISTRICT2."', '".$ADDRESSNAME3."', '".$ADDRESS3."', '".$SUBDISTRICT3."'
                                        , '".$DISTRICT3."', '".$PROVINCE3."', '".$ZIPCODE3."', '".$TEL3."', '".$ADDRESSNAME4."', '".$ADDRESS4."', '".$SUBDISTRICT4."', '".$DISTRICT4."', '".$PROVINCE4."'
                                        , '".$ZIPCODE4."', '".$TEL4."', '".$OWNER_TAX_ID."', '".$AD_GEN_ID."', '".$VHC_CODE_EM."', '".$EM_CODE_EXPRESS."', '".$EM_NAME_EXPRESS."', '".$EM_CONTACK_TYPE."', '".$EM_CONTACK_SARTDATE."', '".$EM_CONTACK_ENDDATE."', '".$EM_CONTACK_ANOTATION."') "; 
                  // 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_commit($ConnYS);
            // sqlsrv_rollback($ConnYS);
            $result = array("STATUS" => "1", "RESULT" => 'Seuccess :'.$i);
            write_text("HR_EM_HEADER",$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 = "HR_EM_HEADER";
            INSERT_SYNLOG($connLog,"ไม่บันทึกดาต้าเบส",$TABLE,"FAIL");
        } 
        sqlsrv_close($ConnYS);
    }

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

// write_text("HR_EM_HEADER",$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($HR_EM_HEADER,$current_time,$htime){
    $t_string = "$HR_EM_HEADER".'  '."$current_time".' '."$htime";
    $strFileName = "LogSynDB.txt";
    $objFopen = fopen($strFileName, 'a');
    $strText1 = "$t_string\r\n";
    fwrite($objFopen, $strText1);
}

// function write_text2($text){
//   $t_string = "$text";
//     $strFileName = "LogSynDB.txt";
//     $objFopen = fopen($strFileName, 'a');
//     $strText1 = "$t_string\r\n";
//   fwrite($objFopen, $strText1);
// }
                
 ?>
	







