

<?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 HR_MASTEREMPLOYEE";
    $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
            ,EM_CARDID
            ,EM_FIRSTNAME
            ,EM_LASTNAME
            , EM_NICKNAME
            , EM_POSITIONID
            , EM_BRANCHID
            , EM_CONTRACTDATE
            , EM_MOBILE
            , EM_EMAIL
            , EM_LINE
            , EM_EDUCATIONID
            , EM_RACE
            , EM_NATION
            , EM_GENDER
            , EM_BIRTHDATE
            , EM_ADDRESS
            , EM_SUBDISTRICT
            , EM_DISTRICT
            , EM_PROVINCE
            , EM_POSTALCODE
            , EM_IMAGE_PERSON
            , EM_IMAGE_CARDID
            , EM_IMAGE_EDUCATION
            , EM_IMAGE_HOUSE_REGISTRATION
            , EM_IMAGE_ACCOUNT_BOOK
            , EM_IMAGE_DRIVER_LICENSE
            , EM_STATUS
            , EM_APPROVE_STATUS
            , EM_APPROVE_BY
            , EM_CREATEDATE
            , EM_CREATEBY
            , EM_UPDATEDATE
            , EM_STATUS
            , EM_UPDATEBY
            , EM_BLACKLIST
           
            from  HR_MASTEREMPLOYEE "; 
        // 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)) {
   
    $EM_ID                      = $arr1["EM_ID"];
    $EM_CODE                    = $arr1["EM_CODE"];
    $EM_CARDID                  = $arr1["EM_CARDID"];
    $EM_FIRSTNAME               = $arr1["EM_FIRSTNAME"];
    $EM_LASTNAME                = $arr1["EM_LASTNAME"];
    $EM_NICKNAME                = $arr1["EM_NICKNAME"];
    $EM_POSITIONID              = $arr1["EM_POSITIONID"];
    $EM_BRANCHID                = $arr1["EM_BRANCHID"];
    $EM_CONTRACTDATE            = $arr1["EM_CONTRACTDATE"];
    $EM_MOBILE                  = $arr1["EM_MOBILE"];
    $EM_EMAIL                   = $arr1["EM_EMAIL"];
    $EM_LINE                    = $arr1["EM_LINE"];
    $EM_EDUCATIONID             = $arr1["EM_EDUCATIONID"];
    $EM_RACE                    = $arr1["EM_RACE"];
    $EM_NATION                  = $arr1["EM_NATION"];
    $EM_GENDER                  = $arr1["EM_GENDER"];
    $EM_BIRTHDATE               = $arr1["EM_BIRTHDATE"];
    $EM_ADDRESS                 = $arr1["EM_ADDRESS"];
    $EM_SUBDISTRICT             = $arr1["EM_SUBDISTRICT"];
    $EM_DISTRICT                = $arr1["EM_DISTRICT"];
    $EM_PROVINCE                = $arr1["EM_PROVINCE"];
    $EM_POSTALCODE              = $arr1["EM_POSTALCODE"];
    $EM_IMAGE_PERSON            = $arr1["EM_IMAGE_PERSON"];
    $EM_IMAGE_CARDID            = $arr1["EM_IMAGE_CARDID"];
    $EM_IMAGE_EDUCATION         = $arr1["EM_IMAGE_EDUCATION"];
    $EM_IMAGE_HOUSE_REGISTRATION  = $arr1["EM_IMAGE_HOUSE_REGISTRATION"];
    $EM_IMAGE_ACCOUNT_BOOK      = $arr1["EM_IMAGE_ACCOUNT_BOOK"];
    $EM_IMAGE_DRIVER_LICENSE    = $arr1["EM_IMAGE_DRIVER_LICENSE"];
    $EM_STATUS                  = $arr1["EM_STATUS"];
    $EM_APPROVE_STATUS          = $arr1["EM_APPROVE_STATUS"];
    $EM_APPROVE_BY              = $arr1["EM_APPROVE_BY"];
    $EM_CREATEDATE              = $arr1["EM_CREATEDATE"];
    $EM_CREATEBY                = $arr1["EM_CREATEBY"];
    $EM_UPDATEDATE              = $arr1["EM_UPDATEDATE"];
    $EM_UPDATEBY                = $arr1["EM_UPDATEBY"];
    $EM_BLACKLIST               = $arr1["EM_BLACKLIST"];
   

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

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

                     $sql_brname = "INSERT into  HR_MASTEREMPLOYEE (EM_ID ,EM_CODE ,EM_CARDID ,EM_FIRSTNAME ,EM_LASTNAME ,EM_NICKNAME  ,EM_POSITIONID  ,EM_BRANCHID  ,EM_CONTRACTDATE  ,EM_MOBILE  ,EM_EMAIL  ,EM_LINE  ,EM_EDUCATIONID  ,EM_RACE  
                                                                ,EM_NATION  ,EM_GENDER  ,EM_BIRTHDATE  ,EM_ADDRESS, EM_DISTRICT, EM_PROVINCE, EM_POSTALCODE, EM_IMAGE_PERSON, EM_IMAGE_CARDID, EM_IMAGE_EDUCATION, EM_IMAGE_HOUSE_REGISTRATION, EM_IMAGE_ACCOUNT_BOOK, EM_IMAGE_DRIVER_LICENSE
                                                                ,EM_STATUS, EM_APPROVE_STATUS, EM_APPROVE_BY, EM_CREATEDATE, EM_CREATEBY, EM_UPDATEDATE, EM_UPDATEBY, EM_BLACKLIST)  
                                         values ('".$EM_ID."', '".$EM_CODE."', '".$EM_CARDID."', '".$EM_FIRSTNAME."', '".$EM_LASTNAME."', '".$EM_NICKNAME."', '".$EM_POSITIONID."', '".$EM_BRANCHID."', $EM_CONTRACTDATE, '".$EM_MOBILE."'
                                                , '".$EM_EMAIL."', '".$EM_LINE."', '".$EM_EDUCATIONID."', '".$EM_RACE."', '".$EM_NATION."', '".$EM_GENDER."', $EM_BIRTHDATE, '".$EM_ADDRESS."', '".$EM_DISTRICT."'
                                                , '".$EM_PROVINCE."', '".$EM_POSTALCODE."', '".$EM_IMAGE_PERSON."', '".$EM_IMAGE_CARDID."', '".$EM_IMAGE_EDUCATION."', '".$EM_IMAGE_HOUSE_REGISTRATION."', '".$EM_IMAGE_ACCOUNT_BOOK."', '".$EM_IMAGE_DRIVER_LICENSE."', '".$EM_STATUS."', '".$EM_APPROVE_STATUS."'
                                                , '".$EM_APPROVE_BY."', '".$EM_CREATEDATE."', '".$EM_CREATEBY."', '".$EM_UPDATEDATE."', '".$EM_UPDATEBY."', '".$EM_BLACKLIST."') "; 
                            // 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 "11";

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







