<?php

	// Dismiss Session
	PHPVC_SESSION::VAR_STOP();
	
	// 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. This may slow down server, uncomment this if you requires an extra security
	// PHPCUS_PARAM::CUS_HAS_PERMISSION(PHPCUS_SYSTEM::AUTHORITY_VIEW_ONLY, 'record_invoice');
	
	if
	(
		PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
		(
			[
					'PARAM_REC_INVOICE_CODE',
					'PARAM_ACC_GROUP_DESCRIPTION',
			        'PARAM_KEY'
			], True
		)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------	
			$PARAM_REC_INVOICE_CODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_INVOICE_CODE');
			$PARAM_ACC_GROUP_DESCRIPTION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_GROUP_DESCRIPTION');
			$PARAM_KEY = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_KEY');
	
			// Invalid Keys
			// ------------------------------------------------------------------
			if($PARAM_KEY != 'HkJC08NC8d{C,B=1aY@_;~YMU7;1>aui')
			{
			    PHPRC_HEADERS::REF_404_NOT_FOUND();
			}
			
			// Get Group Description
			// ------------------------------------------------------------------
			$GET_MAP_ARRAY = PHPCUS_ACCOUNT_GROUP::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			$GET_MAP_ARRAY['PARAM_ACC_GROUP_DESCRIPTION'] = $PARAM_ACC_GROUP_DESCRIPTION;
			
			PHPCUS_SP::CUS_AUTHENTICATION_OVERRIDE('system', '$2y$10$NtfBfHpQXwMS/KYTpvV8b.J18cUajJ7/ubCCjltAOSCvJZuS4anAS');
			$DATA_SET = PHPCUS_ACCOUNT_GROUP::CUS_GET_MA($GET_MAP_ARRAY);
			PHPCUS_SP::CUS_AUTHENTICATION_OVERRIDE_CLEAR();
			
			$PARAM_REC_INVOICE_ACC_GROUP_ID = 0;
			if(empty($DATA_SET) === False)
			{
			    $PARAM_REC_INVOICE_ACC_GROUP_ID = $DATA_SET[0]['ACC_GROUP_ID'];
			}
			else 
			{
			    PHPCUS_PARAM::CUS_JSON_SUCCESS('');
			}
			
			
			// Populate Map Array
			// ...........................................
			$GET_MAP_ARRAY = PHPCUS_RECORD_INVOICE_CUSTOM::CUS_PREPARE_MAPPED_ARRAY_FOR_AS_PO();
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_CODE'] = $PARAM_REC_INVOICE_CODE;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_ACC_GROUP_ID'] = $PARAM_REC_INVOICE_ACC_GROUP_ID;
			$GET_MAP_ARRAY["PARAM_LIMIT"] = 1;
			
			
			// Execute Stored Procedure
			// ...........................................
			$DATA_SET = PHPCUS_RECORD_INVOICE_CUSTOM::CUS_AS_PO_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
	{
	    PHPRC_HEADERS::REF_404_NOT_FOUND();
	}

?>