<?php
defined("BASEPATH") or exit("No direct script access allowed");
class Autosamplingverif
{
  function __construct()
  {
    $CI = &get_instance();
    $this->db_onedev = $CI->load->database("default", true);
  }

  function clean_mysqli_connection( $dbc )
{
    while( mysqli_more_results($dbc) )
    {
        if(mysqli_next_result($dbc))
        {
            $result = mysqli_use_result($dbc);
            
            if( get_class($result) == 'mysqli_stmt' )
            {
                mysqli_stmt_free_result($result);
            }
            else
            {
                unset($result);
            }
        }
    }
}

  function doaction($rst_id,$samplingtime,$userid){
        $sql = "call sp_fo_barcode_generate_again_not_exist(" . $rst_id . ")";
		$this->db_onedev->query($sql);
		$this->clean_mysqli_connection($this->db_onedev->conn_id);

		$sql = "SELECT * 
				FROM t_orderheader
				JOIN t_orderheaderaddon ON T_OrderHeaderAddOnT_OrderHeaderID = T_OrderHeaderID
				WHERE T_OrderHeaderID = {$rst_id}";
		//echo $sql;
		$row_addon = $this->db_onedev->query($sql)->row_array();
		
        $readytime = date('Y-m-d H:i:s', strtotime($samplingtime));
        //echo $readytime;
        $sql = "UPDATE t_ordersample 
                SET
                    T_OrderSampleSampling = 'Y',
                    T_OrderSampleSamplingDate = DATE('{$samplingtime}'),
                    T_OrderSampleSamplingTime = TIME('{$samplingtime}'),
                    T_OrderSampleSamplingUserID = {$userid},
                    T_OrderSampleReceive = 'Y',
                    T_OrderSampleReceiveDate = DATE('{$samplingtime}'),
                    T_OrderSampleReceiveTime = TIME('{$samplingtime}'),
                    T_OrderSampleReadyToProcessDateTime = '{$readytime}',
                    T_OrderSampleVerification = 'Y',
                    T_OrderSampleVerificationDate = CURDATE(),
                    T_OrderSampleVerificationTime = CURTIME(),
                    T_OrderSampleVerificationUserID = {$userid},
                    T_OrderSampleSendHandling = 'Y',
                    T_OrderSampleSendHandlingDate = CURDATE(),
                    T_OrderSampleSendHandlingTime = CURTIME(),
                    T_OrderSampleSendHandlingUserID =  {$userid},
                    T_OrderSampleReceiveUserID = {$userid},
                    T_OrderSampleReceiveHandling = 'Y',
                    T_OrderSampleReceiveHandlingDate = CURDATE(),
                    T_OrderSampleReceiveHandlingTime = CURTIME(),
                    T_OrderSampleReceiveHandlingUserID = {$userid}
                WHERE
                    T_OrderSampleT_OrderHeaderID = {$rst_id}";
        $upd_ordersample = $this->db_onedev->query($sql);
        //echo $sql;
        $sql = "SELECT *
                FROM t_ordersample
                JOIN t_barcodelab ON T_OrderSampleT_BarcodeLabID = T_BarcodeLabID AND T_BarcodeLabIsActive = 'Y'
                JOIN t_sampletype ON T_SampleTypeID = T_OrderSampleT_SampleTypeID
                JOIN t_bahan ON T_SampleTypeT_BahanID = T_BahanID
                JOIN t_samplestation ON T_BahanT_SampleStationID = T_SampleStationID 
                WHERE
                    T_OrderSampleT_OrderHeaderID = {$rst_id} AND 
                    T_OrderSampleIsActive = 'Y'";
        $data_loop = $this->db_onedev->query($sql)->result_array();
        if ($data_loop) {
            foreach ($data_loop as $ks => $vs) {
                $sql = "INSERT INTO t_ordersamplereq(
                            T_OrderSampleReqT_OrderHeaderID,
                            T_OrderSampleReqT_SampleStationID,
                            T_OrderSampleReqT_OrderSampleID,
                            T_OrderSampleReqNat_PositionID,
                            T_OrderSampleReqStatus,
                            T_OrderSampleReqs,
                            T_OrderSampleReqUserID,
                            T_OrderSampleReqCreated
                        )
                        VALUES(
                            {$rst_id},
                            {$vs['T_SampleStationID']},
                            {$vs['T_OrderSampleID']},
                            '2',
                            'Y',
                            '[]',
                            {$userid},
                            NOW()
                        )ON DUPLICATE KEY UPDATE 
                                T_OrderSampleReqStatus = 'Y',
                                T_OrderSampleReqs = '[]',
                                T_OrderSampleReqUserID = {$userid}";
                //echo $sql;
                $this->db_onedev->query($sql);

                $sql = "INSERT INTO sample_by_step(
                            SampleByStepM_StatusSampleCode,
                            SampleByStepT_OrderHeaderID,
                            SampleByStepT_BarcodeLabID,
                            SampleByStepRequirementStatus,
                            SampleByStepRequirements,
                            SampleByStepUserID,
                            SampleByStepDateTime
                        )
                        VALUES(
                            'SAMPLING.Sampling.Sampled',
                            {$rst_id},
                            {$vs['T_BarcodeLabID']},
                            'Y',
                            '[]',
                            {$userid},
                            '{$row_addon['T_OrderHeaderAddOnOnlySampleTime']}'
                        )";
                $this->db_onedev->query($sql);

                $sql = "INSERT INTO sample_by_step(
                                SampleByStepM_StatusSampleCode,
                                SampleByStepT_OrderHeaderID,
                                SampleByStepT_BarcodeLabID,
                                SampleByStepRequirementStatus,
                                SampleByStepRequirements,
                                SampleByStepUserID,
                                SampleByStepDateTime
                            )
                            VALUES(
                                'SAMPLING.Sampling.Received',
                                {$rst_id},
                                {$vs['T_BarcodeLabID']},
                                'Y',
                                '[]',
                                {$userid},
                                '{$row_addon['T_OrderHeaderAddOnOnlySampleTime']}'
                            )";
                $this->db_onedev->query($sql);

                $sql = "INSERT INTO sample_by_step(
                    SampleByStepM_StatusSampleCode,
                    SampleByStepT_OrderHeaderID,
                    SampleByStepT_BarcodeLabID,
                    SampleByStepRequirementStatus,
                    SampleByStepRequirements,
                    SampleByStepUserID,
                    SampleByStepDateTime
                )
                VALUES(
                    'SAMPLING.Verification.Verify',
                    {$prm['sample']['T_OrderHeaderID']},
                    {$prm['sample']['T_BarcodeLabID']},
                    '{$prm['sample']['requirement_status']}',
                    '{$requirements}',
                    {$userid},
                    NOW()
                )";
                $this->db_onedev->query($sql);

                $sql = "INSERT INTO sample_by_step(
                    SampleByStepM_StatusSampleCode,
                    SampleByStepT_OrderHeaderID,
                    SampleByStepT_BarcodeLabID,
                    SampleByStepRequirementStatus,
                    SampleByStepRequirements,
                    SampleByStepUserID,
                    SampleByStepDateTime
                )
                VALUES(
                    'SAMPLING.Verification.To.Handling',
                    {$prm['sample']['T_OrderHeaderID']},
                    {$prm['sample']['T_BarcodeLabID']},
                    '{$prm['sample']['requirement_status']}',
                    '{$requirements}',
                    {$userid},
                    NOW()
                )";
                $this->db_onedev->query($sql);

                $sql = "INSERT INTO sample_by_step(
                    SampleByStepM_StatusSampleCode,
                    SampleByStepT_OrderHeaderID,
                    SampleByStepT_BarcodeLabID,
                    SampleByStepRequirementStatus,
                    SampleByStepRequirements,
                    SampleByStepUserID,
                    SampleByStepDateTime
                )
                VALUES(
                    'SAMPLING.Handling.From.Verification',
                    {$prm['sample']['T_OrderHeaderID']},
                    {$prm['sample']['T_BarcodeLabID']},
                    '{$prm['sample']['requirement_status']}',
                    '{$requirements}',
                    {$userid},
                    NOW()
                )";
                $this->db_onedev->query($sql);

                $query = "	INSERT INTO  t_sampling_queue_last_status (
                                T_SamplingQueueLastStatusT_SampleStationID,
                                T_SamplingQueueLastStatusT_OrderHeaderID,
                                T_SamplingQueueLastStatusT_SamplingQueueStatusID,
                                T_SamplingQueueLastStatusUserID)
                            VALUES(
                                {$vs['T_SampleStationID']},
                                {$rst_id},
                                '5',
                                {$userid}) 
                            ON DUPLICATE KEY UPDATE 
                                    T_SamplingQueueLastStatusT_SamplingQueueStatusID = 5,
                                    T_SamplingQueueLastStatusUserID = {$userid}";
                //echo $query;
                $this->db_onedev->query($query);
            }
        }
		
  }

}
