<?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_eod_product');
	
	if
	(
		PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
		(
			[
					'PARAM_REC_EODPROD_ID',
					'PARAM_REC_EODPROD_DATE_REPORT_FROM',
					'PARAM_REC_EODPROD_DATE_REPORT_TO',
					'PARAM_REC_EODPROD_ACC_GROUP_ID',
					'PARAM_REC_EODPROD_ACC_GROUP_DESCRIPTION',
					'PARAM_REC_EODPROD_CAT_PRODUCT_ID',
					'PARAM_REC_EODPROD_CAT_PRODUCT_DESCRIPTION',
					'PARAM_REC_EODPROD_UOM',
					
					'PARAM_ORDER_FIELD',
					'PARAM_ORDER_TYPE',
					'PARAM_SKIP',
					'PARAM_LIMIT'
			], True
		)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------	
			$PARAM_REC_EODPROD_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_EODPROD_ID');
			$PARAM_REC_EODPROD_DATE_REPORT_FROM = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_EODPROD_DATE_REPORT_FROM');
			$PARAM_REC_EODPROD_DATE_REPORT_TO = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_EODPROD_DATE_REPORT_TO');
			$PARAM_REC_EODPROD_ACC_GROUP_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_EODPROD_ACC_GROUP_ID');
			$PARAM_REC_EODPROD_ACC_GROUP_DESCRIPTION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_EODPROD_ACC_GROUP_DESCRIPTION');
			$PARAM_REC_EODPROD_CAT_PRODUCT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_EODPROD_CAT_PRODUCT_ID');
			$PARAM_REC_EODPROD_CAT_PRODUCT_DESCRIPTION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_EODPROD_CAT_PRODUCT_DESCRIPTION');
			$PARAM_REC_EODPROD_UOM = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_EODPROD_UOM');

			$PARAM_ORDER_FIELD = PHPCUS_PARAM::CUS_READ_SORT_FIELD
			(
					'PARAM_ORDER_FIELD',
					[
						'REC_EODPROD_ID', 
						'REC_EODPROD_DATE_REPORT', 
						'REC_EODPROD_ACC_GROUP_ID', 
						'REC_EODPROD_ACC_GROUP_DESCRIPTION', 
						'REC_EODPROD_CAT_PRODUCT_ID', 
						'REC_EODPROD_CAT_PRODUCT_DESCRIPTION', 
						'REC_EODPROD_UOM', 
						'REC_EODPROD_AMOUNT_PRICE', 
						'REC_EODPROD_BBAL_STORE', 
						'REC_EODPROD_EBAL_STORE', 
						'REC_EODPROD_ABAL_STORE', 
						'REC_EODPROD_SF_BADORDER', 
						'REC_EODPROD_SF_DELIVER_RECV', 
						'REC_EODPROD_SF_DELIVER_SENT', 
						'REC_EODPROD_SF_DESCREPANCY_MINUS', 
						'REC_EODPROD_SF_DESCREPANCY_PLUS', 
						'REC_EODPROD_SF_SALES_RETURN', 
						'REC_EODPROD_SF_SOLD_ITEM', 
						'REC_EODPROD_SF_STOCK_IN', 
						'REC_EODPROD_SF_STOCK_OUT', 
						'REC_EODPROD_SF_PO', 
						'REC_EODPROD_SF_DELIVERY_RTRN', 
						'REC_EODPROD_SF_CUSA_DESC', 
						'REC_EODPROD_SF_CUSA_AMOUNT', 
						'REC_EODPROD_SF_CUSB_DESC', 
						'REC_EODPROD_SF_CUSB_AMOUNT', 
						'REC_EODPROD_SF_CUSC_DESC', 
						'REC_EODPROD_SF_CUSC_AMOUNT', 
						'REC_EODPROD_SF_CUSD_DESC', 
						'REC_EODPROD_SF_CUSD_AMOUNT', 
						'REC_EODPROD_SF_CUSE_DESC', 
						'REC_EODPROD_SF_CUSE_AMOUNT', 
						'REC_EODPROD_SF_OTHER', 
						'REC_EODPROD_DC_SENIOR_20P', 
						'REC_EODPROD_DC_SENIOR_5P', 
						'REC_EODPROD_DC_PWD_20P', 
						'REC_EODPROD_DC_PWD_5P', 
						'REC_EODPROD_DC_GOV_5P', 
						'REC_EODPROD_DC_CUSA_DESC', 
						'REC_EODPROD_DC_CUSA_AMOUNT', 
						'REC_EODPROD_DC_CUSB_DESC', 
						'REC_EODPROD_DC_CUSB_AMOUNT', 
						'REC_EODPROD_DC_CUSC_DESC', 
						'REC_EODPROD_DC_CUSC_AMOUNT', 
						'REC_EODPROD_DC_CUSD_DESC', 
						'REC_EODPROD_DC_CUSD_AMOUNT', 
						'REC_EODPROD_DC_CUSE_DESC', 
						'REC_EODPROD_DC_CUSE_AMOUNT', 
						'REC_EODPROD_DC_OTHER', 
						'REC_EODPROD_TOTAL_SUBTOTAL', 
						'REC_EODPROD_TOTAL_VATABLE', 
						'REC_EODPROD_TOTAL_VATEXEMPT', 
						'REC_EODPROD_TOTAL_NONVAT', 
						'REC_EODPROD_TOTAL_ZERORATED', 
						'REC_EODPROD_TOTAL_VATOTHER', 
						'REC_EODPROD_TOTAL_VAT', 
						'REC_EODPROD_TOTAL_VAT_LESS', 
						'REC_EODPROD_POINT', 
						'REC_EODPROD_STATUS'	
					]
			);		
			$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_EOD_PRODUCT::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			
			$GET_MAP_ARRAY['PARAM_REC_EODPROD_ID'] = $PARAM_REC_EODPROD_ID;
			$GET_MAP_ARRAY['PARAM_REC_EODPROD_DATE_REPORT_FROM'] = $PARAM_REC_EODPROD_DATE_REPORT_FROM;
			$GET_MAP_ARRAY['PARAM_REC_EODPROD_DATE_REPORT_TO'] = $PARAM_REC_EODPROD_DATE_REPORT_TO;
			$GET_MAP_ARRAY['PARAM_REC_EODPROD_ACC_GROUP_ID'] = $PARAM_REC_EODPROD_ACC_GROUP_ID;
			$GET_MAP_ARRAY['PARAM_REC_EODPROD_ACC_GROUP_DESCRIPTION'] = $PARAM_REC_EODPROD_ACC_GROUP_DESCRIPTION;
			$GET_MAP_ARRAY['PARAM_REC_EODPROD_CAT_PRODUCT_ID'] = $PARAM_REC_EODPROD_CAT_PRODUCT_ID;
			$GET_MAP_ARRAY['PARAM_REC_EODPROD_CAT_PRODUCT_DESCRIPTION'] = $PARAM_REC_EODPROD_CAT_PRODUCT_DESCRIPTION;
			$GET_MAP_ARRAY['PARAM_REC_EODPROD_UOM'] = $PARAM_REC_EODPROD_UOM;

			$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_EOD_PRODUCT::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');
	}

?>