<?php

	// Perform Login
    $CURRENT_MODULE_KEY = 'record_job_order';
    $SHORTCODE_STATUS = PHPCUS_SHORTCODE::CUS_QUICK_LOGIN();
	
	// Check nonce
	if($SHORTCODE_STATUS == false)
    {
		PHPCUS_PARAM::CUS_REQUIRES_NONCE();
	}
	
	// Check array size
	PHPCUS_PARAM::CUS_ARRAY_TOO_LARGE();
	
	// Check already authenticated
	PHPCUS_PARAM::CUS_REQUIRES_AUTHENTICATION();

	// Check access rights
	PHPCUS_PARAM::CUS_HAS_PERMISSION(PHPCUS_SYSTEM::AUTHORITY_VIEW_EDIT, $CURRENT_MODULE_KEY);
	
	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
							'PARAM_REC_JORDER_ID',
							'PARAM_REC_JORDER_ACC_GROUP_ID', 
							'PARAM_REC_JORDER_REC_CLIENT_ID', 
							'PARAM_REC_JORDER_REFERENCE_NUMBER', 
							'PARAM_REC_JORDER_DESCRIPTION', 
							'PARAM_REC_JORDER_AMOUNT', 
							'PARAM_REC_JORDER_REMARKS', 
							'PARAM_REC_JORDER_REMARKS_ASSIGN', 
							'PARAM_REC_JORDER_FOR_COMPLETION', 
							'PARAM_REC_JORDER_USER_ASSIGN', 
							'PARAM_REC_JORDER_LONG_A', 
							'PARAM_REC_JORDER_LONG_B', 
							'PARAM_REC_JORDER_DECIMAL_A', 
							'PARAM_REC_JORDER_DECIMAL_B', 
							'PARAM_REC_JORDER_VARCHAR_A', 
							'PARAM_REC_JORDER_VARCHAR_B', 
							'PARAM_REC_JORDER_DATETIME_A', 
							'PARAM_REC_JORDER_DATETIME_B', 
							'PARAM_REC_JORDER_STATUS'
							// , 'PARAM_REC_JORDER_ID_FILE'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_REC_JORDER_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_JORDER_ID', true);
			$PARAM_REC_JORDER_ACC_GROUP_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_JORDER_ACC_GROUP_ID', true);
			$PARAM_REC_JORDER_REC_CLIENT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_JORDER_REC_CLIENT_ID', true);
			$PARAM_REC_JORDER_REFERENCE_NUMBER = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_JORDER_REFERENCE_NUMBER', true);
			$PARAM_REC_JORDER_DESCRIPTION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_JORDER_DESCRIPTION', true);
			$PARAM_REC_JORDER_AMOUNT = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_JORDER_AMOUNT', true);
			$PARAM_REC_JORDER_REMARKS = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_JORDER_REMARKS', true);
			$PARAM_REC_JORDER_REMARKS_ASSIGN = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_JORDER_REMARKS_ASSIGN', true);
			$PARAM_REC_JORDER_FOR_COMPLETION = PHPCUS_PARAM::CUS_READ_INT('PARAM_REC_JORDER_FOR_COMPLETION', true);
			$PARAM_REC_JORDER_USER_ASSIGN = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_JORDER_USER_ASSIGN', true);
			$PARAM_REC_JORDER_LONG_A = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_JORDER_LONG_A', true);
			$PARAM_REC_JORDER_LONG_B = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_JORDER_LONG_B', true);
			$PARAM_REC_JORDER_DECIMAL_A = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_JORDER_DECIMAL_A', true);
			$PARAM_REC_JORDER_DECIMAL_B = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_JORDER_DECIMAL_B', true);
			$PARAM_REC_JORDER_VARCHAR_A = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_JORDER_VARCHAR_A', true);
			$PARAM_REC_JORDER_VARCHAR_B = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_JORDER_VARCHAR_B', true);
			$PARAM_REC_JORDER_DATETIME_A = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_JORDER_DATETIME_A', true);
			$PARAM_REC_JORDER_DATETIME_B = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_JORDER_DATETIME_B', true);
			$PARAM_REC_JORDER_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_REC_JORDER_STATUS', true);
			
			$PARAM_REC_GROSSCALL_DATE = null;
			if(PHPRC_HTTP_PARAM::REF_ISSET('PARAM_REC_GROSSCALL_DATE'))
			{
			    $PARAM_REC_GROSSCALL_DATE = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_GROSSCALL_DATE', true);
			}
			
			//$PARAM_REC_JORDER_ID_FILE = PHPCUS_PARAM::CUS_READ_FILES('PARAM_REC_JORDER_ID_FILE');
			$PARAM_ARRAY_COUNT = count($PARAM_REC_JORDER_ID);
			
				
			// Validate if array has same size
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
			(
					[
							$PARAM_REC_JORDER_ID,
							$PARAM_REC_JORDER_ACC_GROUP_ID, 
							$PARAM_REC_JORDER_REC_CLIENT_ID, 
							$PARAM_REC_JORDER_REFERENCE_NUMBER, 
							$PARAM_REC_JORDER_DESCRIPTION, 
							$PARAM_REC_JORDER_AMOUNT, 
							$PARAM_REC_JORDER_REMARKS, 
							$PARAM_REC_JORDER_REMARKS_ASSIGN, 
							$PARAM_REC_JORDER_FOR_COMPLETION, 
							$PARAM_REC_JORDER_USER_ASSIGN, 
							$PARAM_REC_JORDER_LONG_A, 
							$PARAM_REC_JORDER_LONG_B, 
							$PARAM_REC_JORDER_DECIMAL_A, 
							$PARAM_REC_JORDER_DECIMAL_B, 
							$PARAM_REC_JORDER_VARCHAR_A, 
							$PARAM_REC_JORDER_VARCHAR_B, 
							$PARAM_REC_JORDER_DATETIME_A, 
							$PARAM_REC_JORDER_DATETIME_B, 
							$PARAM_REC_JORDER_STATUS
							// ,$PARAM_REC_JORDER_ID_FILE['name']
					]
			);
			if(empty($PARAM_REC_GROSSCALL_DATE) == false)
			{
			    PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
			    (
			        [
			            $PARAM_REC_JORDER_ID,
			            $PARAM_REC_GROSSCALL_DATE
			        ]
			    );
			}
	
			// Abort if already processed
			// ------------------------------------------------------------------
			$PARAM_REQUEST_UNIQUE_VALUE = null;
			if(PHPRC_HTTP_PARAM::REF_ISSET('PARAM_REQUEST_UNIQUE_VALUE'))
			{
			    $PARAM_REQUEST_UNIQUE_VALUE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REQUEST_UNIQUE_VALUE', false);
			}
			
			if(PHPVC_STR::VAR_IS_BLANK($PARAM_REQUEST_UNIQUE_VALUE) == false)
			{
			    $ARR_INDEX = array_search($CURRENT_MODULE_KEY, PHPCUS_Account_Login_Session::$SESSION_REQUEST_KEY);
			    if
			    (
			        $ARR_INDEX !== false &&
			        PHPCUS_Account_Login_Session::$SESSION_REQUEST_LAST_VALUE[$ARR_INDEX] == $PARAM_REQUEST_UNIQUE_VALUE
			        )
			    {
			        PHPCUS_PARAM::CUS_JSON_ERROR('Given Request is already processed.');
			    }
			}
			
			
			// Prepare PDO Handler
			// ------------------------------------------------------------------
			$FULL_DATA_SET = [];
			$ARRAY_CTR = 0;
			
			$SAVE_MAP_ARRAY = [];
			$PDO_HANDLE = PHPRC_PDO_MYSQL::REF_CONNECT();
			PHPRC_PDO_MYSQL::REF_TRANSACTION_BEGIN($PDO_HANDLE);
			
		
			// Execute Stored Procedure
			// ------------------------------------------------------------------
			Try
			{
				while($ARRAY_CTR < $PARAM_ARRAY_COUNT)
				{
					$SAVE_MAP_ARRAY = PHPCUS_RECORD_JOB_ORDER::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
					
					$SAVE_MAP_ARRAY['PARAM_REC_JORDER_ID'] = $PARAM_REC_JORDER_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_ACC_GROUP_ID'] = $PARAM_REC_JORDER_ACC_GROUP_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_REC_CLIENT_ID'] = $PARAM_REC_JORDER_REC_CLIENT_ID[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_REFERENCE_NUMBER'] = $PARAM_REC_JORDER_REFERENCE_NUMBER[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_DESCRIPTION'] = $PARAM_REC_JORDER_DESCRIPTION[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_AMOUNT'] = $PARAM_REC_JORDER_AMOUNT[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_REMARKS'] = $PARAM_REC_JORDER_REMARKS[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_REMARKS_ASSIGN'] = $PARAM_REC_JORDER_REMARKS_ASSIGN[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_FOR_COMPLETION'] = $PARAM_REC_JORDER_FOR_COMPLETION[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_USER_ASSIGN'] = $PARAM_REC_JORDER_USER_ASSIGN[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_NOT_FOUND'] = PHPRC_SQL_FLAGS::FLAG_BIGINT_NULL;
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_LONG_A'] = $PARAM_REC_JORDER_LONG_A[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_LONG_B'] = $PARAM_REC_JORDER_LONG_B[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_DECIMAL_A'] = $PARAM_REC_JORDER_DECIMAL_A[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_DECIMAL_B'] = $PARAM_REC_JORDER_DECIMAL_B[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_VARCHAR_A'] = $PARAM_REC_JORDER_VARCHAR_A[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_VARCHAR_B'] = $PARAM_REC_JORDER_VARCHAR_B[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_DATETIME_A'] = $PARAM_REC_JORDER_DATETIME_A[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_DATETIME_B'] = $PARAM_REC_JORDER_DATETIME_B[$ARRAY_CTR];
                    $SAVE_MAP_ARRAY['PARAM_REC_JORDER_STATUS'] = $PARAM_REC_JORDER_STATUS[$ARRAY_CTR];
							
					$DATA_SET = PHPCUS_RECORD_JOB_ORDER::CUS_SAVE_MA
					(
							$SAVE_MAP_ARRAY,
							true,
							$PDO_HANDLE
					);
			
					/*
					if
					(
						isset($PARAM_REC_JORDER_ID_FILE['name'][$ARRAY_CTR]) && 
						empty($DATA_SET) == false && 
						isset($DATA_SET[0]['INSERTED_ID'])
					)
					{		
						$FILE_PATH = PHPRC_UPLOAD::REF_SUGGEST_PATH
						(
							PHPSET::$ROOT . '/upload/priv/Record/job_order/', 
							$DATA_SET[0]['INSERTED_ID'],
							$PARAM_REC_JORDER_ID_FILE['name'][$ARRAY_CTR]
						);
						PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_MOVEFILE
						(
							$PARAM_REC_JORDER_ID_FILE['tmp_name'][$ARRAY_CTR],
							$FILE_PATH,
							true
						);
					}
					*/
					
					// Save gross call
					if
					(
					    empty($PARAM_REC_GROSSCALL_DATE) == false && 
					    $PARAM_REC_GROSSCALL_DATE[$ARRAY_CTR] > PHPVC_DATETIME::VAR_TO_DATETIME('1001-01-01 00:00:00.000000') &&
					    $PARAM_REC_JORDER_ID[$ARRAY_CTR] <= 0
					)
					{
					    $SAVE_MAP_ARRAY = PHPCUS_RECORD_GROSS_CALL::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
					    
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_ID'] = -1;
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_ACC_GROUP_ID'] = $PARAM_REC_JORDER_ACC_GROUP_ID[$ARRAY_CTR];
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_REC_CLIENT_ID'] = $PARAM_REC_JORDER_REC_CLIENT_ID[$ARRAY_CTR];
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_DATE'] = $PARAM_REC_GROSSCALL_DATE[$ARRAY_CTR];
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_REMARKS'] = '';
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_STATUS'] = 4;
					    
					    PHPCUS_RECORD_GROSS_CALL::CUS_SAVE_MA
					    (
					        $SAVE_MAP_ARRAY,
					        true,
					        $PDO_HANDLE
					    );
					}
					else if($PARAM_REC_JORDER_ID[$ARRAY_CTR] <= 0)
					{
					    $SAVE_MAP_ARRAY = PHPCUS_RECORD_GROSS_CALL::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
					    
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_ID'] = -1;
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_ACC_GROUP_ID'] = $PARAM_REC_JORDER_ACC_GROUP_ID[$ARRAY_CTR];
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_REC_CLIENT_ID'] = $PARAM_REC_JORDER_REC_CLIENT_ID[$ARRAY_CTR];
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_DATE'] = PHPVC_DATETIME::VAR_NOW();
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_REMARKS'] = '';           
					    $SAVE_MAP_ARRAY['PARAM_REC_GROSSCALL_STATUS'] = 4;
					    
					    PHPCUS_RECORD_GROSS_CALL::CUS_SAVE_MA
					    (
					        $SAVE_MAP_ARRAY,
					        true,
					        $PDO_HANDLE
					    );
					}
					
					$FULL_DATA_SET = PHPRC_PDO_MYSQL_ARRAY::REF_MERGE($FULL_DATA_SET, $DATA_SET);
					$ARRAY_CTR ++;
				}
					
			}
			Catch(Exception $PDO_ERR)
			{
				PHPRC_PDO_MYSQL::REF_TRANSACTION_ROLLBACK($PDO_HANDLE);
				//PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_ROLLBACK();
				PHPCUS_PARAM::CUS_JSON_ERROR($PDO_ERR);
			}
			PHPRC_PDO_MYSQL::REF_TRANSACTION_COMMIT($PDO_HANDLE);
			//PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_COMMIT();
						
			
			// Update processed request key
			// ------------------------------------------------------------------
			if(PHPVC_STR::VAR_IS_BLANK($PARAM_REQUEST_UNIQUE_VALUE) == false)
			{
			    $ARR_INDEX = array_search($CURRENT_MODULE_KEY, PHPCUS_Account_Login_Session::$SESSION_REQUEST_KEY);
			    if($ARR_INDEX !== false)
			    {
			        PHPCUS_Account_Login_Session::$SESSION_REQUEST_LAST_VALUE[$ARR_INDEX] = $PARAM_REQUEST_UNIQUE_VALUE;
			    }
			    else
			    {
			        array_push(PHPCUS_Account_Login_Session::$SESSION_REQUEST_KEY, $CURRENT_MODULE_KEY);
			        array_push(PHPCUS_Account_Login_Session::$SESSION_REQUEST_LAST_VALUE, $PARAM_REQUEST_UNIQUE_VALUE);
			    }
			}
			
			
			// Return JSON response
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_JSON_SUCCESS($FULL_DATA_SET);
			
		}
		Catch(Exception $ERR)
		{
			PHPCUS_PARAM::CUS_JSON_ERROR($ERR);
		}
	}
	else
	{
		PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
	}

?>