<?php 
    header('Content-Type: application/json');
    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');

    $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_object($rst1)) {

        $data[] = array( 
                         "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
                    );
 
        $x++;
    }


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

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

                $TRAINING_EMP_ID                     = $data[$i]["TRAINING_EMP_ID"];
                $TRAINING_EMP_CODE                   = $data[$i]["TRAINING_EMP_CODE"];
                $TRAINING_CODE                       = $data[$i]["TRAINING_CODE"];
                $TRAINING_EMP_DATE                   = $data[$i]["TRAINING_EMP_DATE"];
                $TRAINING_EMP_STATUS                 = $data[$i]["TRAINING_EMP_STATUS"];
                $TRAINING_EMP_STAFF                  = $data[$i]["TRAINING_EMP_STAFF"];
                $TRAINING_EMP_SCORE                  = $data[$i]["TRAINING_EMP_SCORE"];
                $TRAINING_EMP_ANNOTATION             = $data[$i]["TRAINING_EMP_ANNOTATION"];
                $TRAINING_EMP_INSERT_LOG             = $data[$i]["TRAINING_EMP_INSERT_LOG"];
                $TRAINING_EMP_INSERT_DATE            = $data[$i]["TRAINING_EMP_INSERT_DATE"];
                $TRAINING_EMP_UPDATE_LOG             = $data[$i]["TRAINING_EMP_UPDATE_LOG"];
                $TRAINING_EMP_UPDATE_DATE            = $data[$i]["TRAINING_EMP_UPDATE_DATE"];

                $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."') "; 

                $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_commit($ConnYS);
            // sqlsrv_rollback($ConnYS);
            $result = array("STATUS" => "1", "RESULT" => 'Seuccess :'.$i);
            write_text("O_TRAINING_EMPLOYEE",$current_time,$htime);
            write_text2("....................................");
            INSERT_SYNLOG($connLog,"","","SUCCESS");
            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 = "O_TRAINING_EMPLOYEE";
            INSERT_SYNLOG($connLog,"ไม่บันทึกดาต้าเบส",$TABLE,"FAIL");
        }
        sqlsrv_close($ConnYS); 
    }

    // 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);
  }
                
 ?>
	







