<?php

	// Perform Login
    $SHORTCODE_STATUS = PHPCUS_SHORTCODE::CUS_QUICK_LOGIN();
	
	// Check Array Input
	PHPCUS_PARAM::CUS_DOES_NOT_SUPPORT_ARRAY();
	
	// Check already authenticated
	PHPCUS_PARAM::CUS_REQUIRES_AUTHENTICATION();
	// Check access rights
	PHPCUS_PARAM::CUS_HAS_PERMISSION(PHPCUS_SYSTEM::AUTHORITY_VIEW_ONLY, 'record_delivery');
	
	if
	(
		PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
		(
			[
					'PARAM_REC_DELIVERY_ID',
					'PARAM_REC_DELIVERY_CAT_COMPANY_ID',
					'PARAM_REC_DELIVERY_CODE',
					'PARAM_REC_DELIVERY_COMPANY_NAME',
					'PARAM_REC_DELIVERY_ADDRESS',
					'PARAM_REC_DELIVERY_CONTACT',
					'PARAM_REC_DELIVERY_AMOUNT_TOTAL_FROM',
					'PARAM_REC_DELIVERY_AMOUNT_TOTAL_TO',
					'PARAM_REC_DELIVERY_DATE_DELIVERED_FROM',
					'PARAM_REC_DELIVERY_DATE_DELIVERED_TO',
					'PARAM_REC_DELIVERY_COURIER_ACC_ACCOUNT_ID',
					'PARAM_REC_DELIVERY_DATE_REGISTERED_FROM',
					'PARAM_REC_DELIVERY_DATE_REGISTERED_TO',
					'PARAM_REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID',
					'PARAM_REC_DELIVERY_STATUS',
					
					'PARAM_ORDER_FIELD',
					'PARAM_ORDER_TYPE',
					'PARAM_SKIP',
					'PARAM_LIMIT'
			], True
		)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------	
			$PARAM_REC_DELIVERY_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_DELIVERY_ID');
			$PARAM_REC_DELIVERY_CAT_COMPANY_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_DELIVERY_CAT_COMPANY_ID');
			$PARAM_REC_DELIVERY_CODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_DELIVERY_CODE');
			$PARAM_REC_DELIVERY_COMPANY_NAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_DELIVERY_COMPANY_NAME');
			$PARAM_REC_DELIVERY_ADDRESS = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_DELIVERY_ADDRESS');
			$PARAM_REC_DELIVERY_CONTACT = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_DELIVERY_CONTACT');
			$PARAM_REC_DELIVERY_AMOUNT_TOTAL_FROM = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_DELIVERY_AMOUNT_TOTAL_FROM');
			$PARAM_REC_DELIVERY_AMOUNT_TOTAL_TO = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_DELIVERY_AMOUNT_TOTAL_TO');
			$PARAM_REC_DELIVERY_DATE_DELIVERED_FROM = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_DELIVERY_DATE_DELIVERED_FROM');
			$PARAM_REC_DELIVERY_DATE_DELIVERED_TO = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_DELIVERY_DATE_DELIVERED_TO');
			$PARAM_REC_DELIVERY_COURIER_ACC_ACCOUNT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_DELIVERY_COURIER_ACC_ACCOUNT_ID');
			$PARAM_REC_DELIVERY_DATE_REGISTERED_FROM = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_DELIVERY_DATE_REGISTERED_FROM');
			$PARAM_REC_DELIVERY_DATE_REGISTERED_TO = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_DELIVERY_DATE_REGISTERED_TO');
			$PARAM_REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID');
			$PARAM_REC_DELIVERY_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_REC_DELIVERY_STATUS');
			
			$PARAM_ORDER_FIELD = PHPCUS_PARAM::CUS_READ_SORT_FIELD
			(
					'PARAM_ORDER_FIELD',
					[
						'REC_DELIVERY_ID', 
						'REC_DELIVERY_CAT_COMPANY_ID', 
						'REC_DELIVERY_CODE', 
						'REC_DELIVERY_COMPANY_NAME', 
						'REC_DELIVERY_ADDRESS', 
						'REC_DELIVERY_CONTACT', 
						'REC_DELIVERY_AMOUNT_TOTAL', 
						'REC_DELIVERY_DATE_DELIVERED', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID', 
						'REC_DELIVERY_DATE_REGISTERED', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID', 
						'REC_DELIVERY_STATUS'
						, 
						'CAT_COMPANY_ID',		
						'CAT_COMPANY_CODE',		
						'CAT_COMPANY_DESCRIPTION',		
						'CAT_COMPANY_TIN',		
						'CAT_COMPANY_ADDRESS',		
						'CAT_COMPANY_MOBILE',		
						'CAT_COMPANY_PHONE',		
						'CAT_COMPANY_FAX',		
						'CAT_COMPANY_EMAIL',		
						'CAT_COMPANY_WEBSITE',		
						'CAT_COMPANY_SNS_PAGE',		
						'CAT_COMPANY_CAT_COMPANY_TYPE_ID',		
						'CAT_COMPANY_REMARKS',		
						'CAT_COMPANY_STATUS',		
						'CAT_COMPANY_SYSTEM_GENERATED',		
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_ID', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_SERVER_ID', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_ACC_LEVEL_ID', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_ACC_GROUP_ID', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_ADD_RESOURCE_ID', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_USERNAME', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_PASSWORD', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_AUTH_CODE', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_FIRST_NAME', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_MIDDLE_NAME', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_LAST_NAME', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_POSITION', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_SYS_GENDER_ID', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_STATUS', 
						'REC_DELIVERY_COURIER_ACC_ACCOUNT_ID_ACC_ACCOUNT_SYSTEM_GENERATED', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_ID', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_SERVER_ID', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_ACC_LEVEL_ID', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_ACC_GROUP_ID', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_ADD_RESOURCE_ID', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_USERNAME', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_PASSWORD', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_AUTH_CODE', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_FIRST_NAME', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_MIDDLE_NAME', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_LAST_NAME', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_POSITION', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_SYS_GENDER_ID', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_STATUS', 
						'REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID_ACC_ACCOUNT_SYSTEM_GENERATED'
					]
			);		
			$PARAM_ORDER_TYPE = PHPCUS_PARAM::CUS_READ_SORT_TYPE('PARAM_ORDER_TYPE');
			$PARAM_SKIP	= PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_SKIP');
			$PARAM_LIMIT = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_LIMIT');
			
			// ------------------------------------------------------------------
			
			
			// Populate Map Array
			// ...........................................
			$GET_MAP_ARRAY = PHPCUS_RECORD_DELIVERY::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_ID'] = $PARAM_REC_DELIVERY_ID;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_CAT_COMPANY_ID'] = $PARAM_REC_DELIVERY_CAT_COMPANY_ID;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_CODE'] = $PARAM_REC_DELIVERY_CODE;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_COMPANY_NAME'] = $PARAM_REC_DELIVERY_COMPANY_NAME;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_ADDRESS'] = $PARAM_REC_DELIVERY_ADDRESS;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_CONTACT'] = $PARAM_REC_DELIVERY_CONTACT;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_AMOUNT_TOTAL_FROM'] = $PARAM_REC_DELIVERY_AMOUNT_TOTAL_FROM;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_AMOUNT_TOTAL_TO'] = $PARAM_REC_DELIVERY_AMOUNT_TOTAL_TO;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_DATE_DELIVERED_FROM'] = $PARAM_REC_DELIVERY_DATE_DELIVERED_FROM;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_DATE_DELIVERED_TO'] = $PARAM_REC_DELIVERY_DATE_DELIVERED_TO;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_COURIER_ACC_ACCOUNT_ID'] = $PARAM_REC_DELIVERY_COURIER_ACC_ACCOUNT_ID;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_DATE_REGISTERED_FROM'] = $PARAM_REC_DELIVERY_DATE_REGISTERED_FROM;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_DATE_REGISTERED_TO'] = $PARAM_REC_DELIVERY_DATE_REGISTERED_TO;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID'] = $PARAM_REC_DELIVERY_PREPARE_ACC_ACCOUNT_ID;
			$GET_MAP_ARRAY['PARAM_REC_DELIVERY_STATUS'] = $PARAM_REC_DELIVERY_STATUS;
			
			$GET_MAP_ARRAY["PARAM_ORDER_FIELD"] = $PARAM_ORDER_FIELD;
			$GET_MAP_ARRAY["PARAM_ORDER_TYPE"] = $PARAM_ORDER_TYPE;
			$GET_MAP_ARRAY["PARAM_SKIP"] = $PARAM_SKIP;
			$GET_MAP_ARRAY["PARAM_LIMIT"] = $PARAM_LIMIT;
			
			
			// Execute Stored Procedure
			// ...........................................
			$DATA_SET = PHPCUS_RECORD_DELIVERY::CUS_GET_MA($GET_MAP_ARRAY);
			
						
			// Return JSON response
			// ...........................................
			if(empty($DATA_SET) === False)
			{
				PHPCUS_PARAM::CUS_JSON_SUCCESS($DATA_SET);
			}
			else
			{
				PHPCUS_PARAM::CUS_JSON_SUCCESS('');
			}
			
		}
		Catch(Exception $ERR)
		{		
			PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
			if(isset($_SERVER['HTTP_ORIGIN']))
			{
			    PHPVC_LOG::VAR_ERROR('HTTP_ORIGIN', $_SERVER['HTTP_ORIGIN']);
			}
			if(isset($_SERVER['HTTP_HOST']))
			{
			    PHPVC_LOG::VAR_ERROR('HTTP_HOST', $_SERVER['HTTP_HOST']);
			}		
			if(isset($_SERVER['HTTP_REFERER']))
			{
			    PHPVC_LOG::VAR_ERROR('HTTP_REFERER', $_SERVER['HTTP_REFERER']);
			}
			if(isset($_SERVER['REMOTE_ADDR']))
			{
			    PHPVC_LOG::VAR_ERROR('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
			}
			
			PHPCUS_PARAM::CUS_JSON_ERROR($ERR->getMessage());
		}
	}
	else
	{
		PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
	}

?>