<?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_ID',
					'PARAM_REC_INVOICE_CODE',
					'PARAM_REC_INVOICE_REFERENCE',
					'PARAM_REC_INVOICE_ACC_GROUP_ID',
					'PARAM_REC_INVOICE_LINE_TOTAL',
					'PARAM_REC_INVOICE_LINE_DISCOUNT_FROM',
					'PARAM_REC_INVOICE_LINE_DISCOUNT_TO',
					'PARAM_REC_INVOICE_DISCOUNT_OTHER_FROM',
					'PARAM_REC_INVOICE_DISCOUNT_OTHER_TO',
					'PARAM_REC_INVOICE_AMOUNT_DUE_FROM',
					'PARAM_REC_INVOICE_AMOUNT_DUE_TO',
					'PARAM_REC_INVOICE_AMOUNT_RETURN_FROM',
					'PARAM_REC_INVOICE_AMOUNT_RETURN_TO',
					'PARAM_REC_INVOICE_AMOUNT_ADJUSTMENT_FROM',
					'PARAM_REC_INVOICE_AMOUNT_ADJUSTMENT_TO',
					'PARAM_REC_INVOICE_AMOUNT_PAID_FROM',
					'PARAM_REC_INVOICE_AMOUNT_PAID_TO',
					'PARAM_REC_INVOICE_REMARKS',
					'PARAM_REC_INVOICE_REC_EMPLOYEE_ID',
					'PARAM_REC_INVOICE_DATE_DELIVERY_FROM',
					'PARAM_REC_INVOICE_DATE_DELIVERY_TO',
					'PARAM_REC_INVOICE_DATE_ISSUED_FROM',
					'PARAM_REC_INVOICE_DATE_ISSUED_TO',
					'PARAM_REC_INVOICE_DATE_DUE_FROM',
					'PARAM_REC_INVOICE_DATE_DUE_TO',
					'PARAM_REC_INVOICE_DATE_FULLPAY_FROM',
					'PARAM_REC_INVOICE_DATE_FULLPAY_TO',
					'PARAM_REC_INVOICE_ACC_ACCOUNT_ID',
					'PARAM_REC_INVOICE_PAYMENT_STATUS',
					'PARAM_REC_INVOICE_STATUS',
					
					'PARAM_ORDER_FIELD',
					'PARAM_ORDER_TYPE',
					'PARAM_SKIP',
					'PARAM_LIMIT'
			], True
		)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------	
			$PARAM_REC_INVOICE_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_INVOICE_ID');
			$PARAM_REC_INVOICE_CODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_INVOICE_CODE');
			$PARAM_REC_INVOICE_REFERENCE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_INVOICE_REFERENCE');
			$PARAM_REC_INVOICE_ACC_GROUP_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_INVOICE_ACC_GROUP_ID');
			$PARAM_REC_INVOICE_LINE_TOTAL = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_LINE_TOTAL');
			$PARAM_REC_INVOICE_LINE_DISCOUNT_FROM = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_LINE_DISCOUNT_FROM');
			$PARAM_REC_INVOICE_LINE_DISCOUNT_TO = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_LINE_DISCOUNT_TO');
			$PARAM_REC_INVOICE_DISCOUNT_OTHER_FROM = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_DISCOUNT_OTHER_FROM');
			$PARAM_REC_INVOICE_DISCOUNT_OTHER_TO = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_DISCOUNT_OTHER_TO');
			$PARAM_REC_INVOICE_AMOUNT_DUE_FROM = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_AMOUNT_DUE_FROM');
			$PARAM_REC_INVOICE_AMOUNT_DUE_TO = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_AMOUNT_DUE_TO');
			$PARAM_REC_INVOICE_AMOUNT_RETURN_FROM = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_AMOUNT_RETURN_FROM');
			$PARAM_REC_INVOICE_AMOUNT_RETURN_TO = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_AMOUNT_RETURN_TO');
			$PARAM_REC_INVOICE_AMOUNT_ADJUSTMENT_FROM = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_AMOUNT_ADJUSTMENT_FROM');
			$PARAM_REC_INVOICE_AMOUNT_ADJUSTMENT_TO = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_AMOUNT_ADJUSTMENT_TO');
			$PARAM_REC_INVOICE_AMOUNT_PAID_FROM = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_AMOUNT_PAID_FROM');
			$PARAM_REC_INVOICE_AMOUNT_PAID_TO = PHPCUS_PARAM::CUS_READ_DECIMAL('PARAM_REC_INVOICE_AMOUNT_PAID_TO');
			$PARAM_REC_INVOICE_REMARKS = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REC_INVOICE_REMARKS');
			$PARAM_REC_INVOICE_REC_EMPLOYEE_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_INVOICE_REC_EMPLOYEE_ID');
			$PARAM_REC_INVOICE_DATE_DELIVERY_FROM = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_INVOICE_DATE_DELIVERY_FROM');
			$PARAM_REC_INVOICE_DATE_DELIVERY_TO = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_INVOICE_DATE_DELIVERY_TO');
			$PARAM_REC_INVOICE_DATE_ISSUED_FROM = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_INVOICE_DATE_ISSUED_FROM');
			$PARAM_REC_INVOICE_DATE_ISSUED_TO = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_INVOICE_DATE_ISSUED_TO');
			$PARAM_REC_INVOICE_DATE_DUE_FROM = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_INVOICE_DATE_DUE_FROM');
			$PARAM_REC_INVOICE_DATE_DUE_TO = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_INVOICE_DATE_DUE_TO');
			$PARAM_REC_INVOICE_DATE_FULLPAY_FROM = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_INVOICE_DATE_FULLPAY_FROM');
			$PARAM_REC_INVOICE_DATE_FULLPAY_TO = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_REC_INVOICE_DATE_FULLPAY_TO');
			$PARAM_REC_INVOICE_ACC_ACCOUNT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_REC_INVOICE_ACC_ACCOUNT_ID');
			$PARAM_REC_INVOICE_PAYMENT_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_REC_INVOICE_PAYMENT_STATUS');
			$PARAM_REC_INVOICE_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_REC_INVOICE_STATUS');
			
			$PARAM_ORDER_FIELD = PHPCUS_PARAM::CUS_READ_SORT_FIELD
			(
					'PARAM_ORDER_FIELD',
					[
						'REC_INVOICE_ID', 
						'REC_INVOICE_CODE', 
						'REC_INVOICE_REFERENCE', 
						'REC_INVOICE_ACC_GROUP_ID', 
						'REC_INVOICE_LINE_TOTAL', 
						'REC_INVOICE_LINE_DISCOUNT', 
						'REC_INVOICE_DISCOUNT_OTHER', 
						'REC_INVOICE_AMOUNT_DUE', 
						'REC_INVOICE_AMOUNT_RETURN', 
						'REC_INVOICE_AMOUNT_ADJUSTMENT', 
						'REC_INVOICE_AMOUNT_PAID', 
						'REC_INVOICE_REMARKS', 
						'REC_INVOICE_REC_EMPLOYEE_ID', 
						'REC_INVOICE_DATE_DELIVERY', 
						'REC_INVOICE_DATE_ISSUED', 
						'REC_INVOICE_DATE_DUE', 
						'REC_INVOICE_DATE_FULLPAY', 
						'REC_INVOICE_ACC_ACCOUNT_ID', 
						'REC_INVOICE_PAYMENT_STATUS', 
						'REC_INVOICE_STATUS'
						, 
						'ACC_GROUP_ID',		
						'ACC_GROUP_DESCRIPTION',		
						'ACC_GROUP_STATUS',		
						'ACC_GROUP_SYSTEM_GENERATED',		
						'REC_EMPLOYEE_ID',		
						'REC_EMPLOYEE_NAME',		
						'REC_EMPLOYEE_STATUS',		
						'REC_EMPLOYEE_SYSTEM_GENERATED',		
						'ACC_ACCOUNT_ID',		
						'ACC_ACCOUNT_SERVER_ID',		
						'ACC_ACCOUNT_ACC_LEVEL_ID',		
						'ACC_ACCOUNT_ACC_GROUP_ID',		
						'ACC_ACCOUNT_ADD_RESOURCE_ID',		
						'ACC_ACCOUNT_USERNAME',		
						'ACC_ACCOUNT_PASSWORD',		
						'ACC_ACCOUNT_AUTH_CODE',		
						'ACC_ACCOUNT_FIRST_NAME',		
						'ACC_ACCOUNT_MIDDLE_NAME',		
						'ACC_ACCOUNT_LAST_NAME',		
						'ACC_ACCOUNT_POSITION',		
						'ACC_ACCOUNT_SYS_GENDER_ID',		
						'ACC_ACCOUNT_STATUS',		
						'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_INVOICE::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_ID'] = $PARAM_REC_INVOICE_ID;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_CODE'] = $PARAM_REC_INVOICE_CODE;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_REFERENCE'] = $PARAM_REC_INVOICE_REFERENCE;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_ACC_GROUP_ID'] = $PARAM_REC_INVOICE_ACC_GROUP_ID;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_LINE_TOTAL'] = $PARAM_REC_INVOICE_LINE_TOTAL;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_LINE_DISCOUNT_FROM'] = $PARAM_REC_INVOICE_LINE_DISCOUNT_FROM;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_LINE_DISCOUNT_TO'] = $PARAM_REC_INVOICE_LINE_DISCOUNT_TO;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_DISCOUNT_OTHER_FROM'] = $PARAM_REC_INVOICE_DISCOUNT_OTHER_FROM;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_DISCOUNT_OTHER_TO'] = $PARAM_REC_INVOICE_DISCOUNT_OTHER_TO;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_AMOUNT_DUE_FROM'] = $PARAM_REC_INVOICE_AMOUNT_DUE_FROM;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_AMOUNT_DUE_TO'] = $PARAM_REC_INVOICE_AMOUNT_DUE_TO;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_AMOUNT_RETURN_FROM'] = $PARAM_REC_INVOICE_AMOUNT_RETURN_FROM;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_AMOUNT_RETURN_TO'] = $PARAM_REC_INVOICE_AMOUNT_RETURN_TO;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_AMOUNT_ADJUSTMENT_FROM'] = $PARAM_REC_INVOICE_AMOUNT_ADJUSTMENT_FROM;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_AMOUNT_ADJUSTMENT_TO'] = $PARAM_REC_INVOICE_AMOUNT_ADJUSTMENT_TO;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_AMOUNT_PAID_FROM'] = $PARAM_REC_INVOICE_AMOUNT_PAID_FROM;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_AMOUNT_PAID_TO'] = $PARAM_REC_INVOICE_AMOUNT_PAID_TO;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_REMARKS'] = $PARAM_REC_INVOICE_REMARKS;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_REC_EMPLOYEE_ID'] = $PARAM_REC_INVOICE_REC_EMPLOYEE_ID;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_DATE_DELIVERY_FROM'] = $PARAM_REC_INVOICE_DATE_DELIVERY_FROM;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_DATE_DELIVERY_TO'] = $PARAM_REC_INVOICE_DATE_DELIVERY_TO;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_DATE_ISSUED_FROM'] = $PARAM_REC_INVOICE_DATE_ISSUED_FROM;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_DATE_ISSUED_TO'] = $PARAM_REC_INVOICE_DATE_ISSUED_TO;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_DATE_DUE_FROM'] = $PARAM_REC_INVOICE_DATE_DUE_FROM;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_DATE_DUE_TO'] = $PARAM_REC_INVOICE_DATE_DUE_TO;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_DATE_FULLPAY_FROM'] = $PARAM_REC_INVOICE_DATE_FULLPAY_FROM;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_DATE_FULLPAY_TO'] = $PARAM_REC_INVOICE_DATE_FULLPAY_TO;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_ACC_ACCOUNT_ID'] = $PARAM_REC_INVOICE_ACC_ACCOUNT_ID;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_PAYMENT_STATUS'] = $PARAM_REC_INVOICE_PAYMENT_STATUS;
			$GET_MAP_ARRAY['PARAM_REC_INVOICE_STATUS'] = $PARAM_REC_INVOICE_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_INVOICE::CUS_GET_SUM_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');
	}

?>