

<?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');

$yearY = date("Y"); 
$yearM = date("m"); 
$yearD = date("d");
$htime = date("H:i:sa"); 
$current_time = $yearD.'.'.$yearM.'.'.($yearY+543);

$currentdate = date('Y-m-d');

?>


<?php

$sql_del = "DELETE from O_TRAINING_EMPLOYEE";
    $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 O_TRAINING_EMPLOYEE ไม่สำเร็จ#" . $sql_del);
        }

sqlsrv_commit($ConnYS);


$sql1 = " SELECT TRAINING_EMP_ID
                 ,TRAINING_EMP_CODE
                 ,TRAINING_CODE
                 ,TRAINING_EMP_DATE
                 ,TRAINING_EMP_STATUS
                 ,TRAINING_EMP_STAFF
                 ,TRAINING_EMP_SCORE
                 ,TRAINING_EMP_ANNOTATION
                 ,TRAINING_EMP_INSERT_LOG
                 ,TRAINING_EMP_INSERT_DATE
                 ,TRAINING_EMP_UPDATE_LOG
                 ,TRAINING_EMP_UPDATE_DATE
                FROM O_TRAINING_EMPLOYEE
            "; 
// echo $sql1;
$rst1 = ibase_query($connFB, $sql1);
// die("Stop");
$x = 0;
while($arr1         = ibase_fetch_assoc($rst1)) {
   
    $TRAINING_EMP_ID                     = $arr1["TRAINING_EMP_ID"];
    $TRAINING_EMP_CODE                   = $arr1["TRAINING_EMP_CODE"];
    $TRAINING_CODE                       = $arr1["TRAINING_CODE"];
    $TRAINING_EMP_DATE                   = $arr1["TRAINING_EMP_DATE"];
    $TRAINING_EMP_STATUS                 = $arr1["TRAINING_EMP_STATUS"];
    $TRAINING_EMP_STAFF                  = $arr1["TRAINING_EMP_STAFF"];
    $TRAINING_EMP_SCORE                  = $arr1["TRAINING_EMP_SCORE"];
    $TRAINING_EMP_ANNOTATION             = $arr1["TRAINING_EMP_ANNOTATION"];
    $TRAINING_EMP_INSERT_LOG             = $arr1["TRAINING_EMP_INSERT_LOG"];
    $TRAINING_EMP_INSERT_DATE            = $arr1["TRAINING_EMP_INSERT_DATE"];
    $TRAINING_EMP_UPDATE_LOG             = $arr1["TRAINING_EMP_UPDATE_LOG"];
    $TRAINING_EMP_UPDATE_DATE            = $arr1["TRAINING_EMP_UPDATE_DATE"];
    

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

                     $sql_name = " SET IDENTITY_INSERT O_TRAINING_EMPLOYEE ON 
                                    INSERT into  O_TRAINING_EMPLOYEE (TRAINING_EMP_ID ,TRAINING_EMP_CODE, TRAINING_CODE, TRAINING_EMP_DATE, TRAINING_EMP_STATUS, TRAINING_EMP_STAFF, TRAINING_EMP_SCORE, TRAINING_EMP_ANNOTATION
                                        ,TRAINING_EMP_INSERT_LOG, TRAINING_EMP_INSERT_DATE, TRAINING_EMP_UPDATE_LOG, TRAINING_EMP_UPDATE_DATE)  
                                    values ('$TRAINING_EMP_ID', '".$TRAINING_EMP_CODE."', '".$TRAINING_CODE."', '".$TRAINING_EMP_DATE."', '".$TRAINING_EMP_STATUS."', '".$TRAINING_EMP_STAFF."', '".$TRAINING_EMP_SCORE."'
                                        , '".$TRAINING_EMP_ANNOTATION."', '".$TRAINING_EMP_INSERT_LOG."', '".$TRAINING_EMP_INSERT_DATE."', '".$TRAINING_EMP_UPDATE_LOG."', '".$TRAINING_EMP_UPDATE_DATE."') "; 
                            // 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 นำเข้า O_TRAINING_EMPLOYEE ไม่สำเร็จ#" . $sql_name);
                        }
       
                        // sqlsrv_rollback($ConnYS);

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


// echo "Success".$x;
echo "20";
INSERT_SYNLOG($connLog,"","","SUCCESS");

write_text("O_TRAINING_EMPLOYEE",$current_time,$htime);
write_text2("....................................");

function write_text($O_TRAINING_EMPLOYEE,$current_time,$htime){
    $t_string = "$O_TRAINING_EMPLOYEE".'  '."$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);
  }
                
 ?>
	







