<?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();
	
	
	if
	(
		PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
		(
			[
					'PARAM_CAT_PUROK_ID',
					'PARAM_CAT_PUROK_GLOBAL_ID',
					'PARAM_CAT_PUROK_CAT_BARANGAY_ID',
					'PARAM_CAT_PUROK_CODE',
					'PARAM_CAT_PUROK_CODE_B',
					'PARAM_CAT_PUROK_CODE_C',
					'PARAM_CAT_PUROK_DESCRIPTION',
					'PARAM_CAT_PUROK_DESCRIPTION_B',
					'PARAM_CAT_PUROK_DESCRIPTION_C',
					'PARAM_CAT_PUROK_STATUS',
					'PARAM_CAT_PUROK_SYSTEM_GENERATED',
					
					'PARAM_ORDER_FIELD',
					'PARAM_ORDER_TYPE',
					'PARAM_SKIP',
					'PARAM_LIMIT'
			], True
		)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------	
			$PARAM_CAT_PUROK_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PUROK_ID');
			$PARAM_CAT_PUROK_GLOBAL_ID = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PUROK_GLOBAL_ID');
			$PARAM_CAT_PUROK_CAT_BARANGAY_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_PUROK_CAT_BARANGAY_ID');
			$PARAM_CAT_PUROK_CODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PUROK_CODE');
			$PARAM_CAT_PUROK_CODE_B = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PUROK_CODE_B');
			$PARAM_CAT_PUROK_CODE_C = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PUROK_CODE_C');
			$PARAM_CAT_PUROK_DESCRIPTION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PUROK_DESCRIPTION');
			$PARAM_CAT_PUROK_DESCRIPTION_B = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PUROK_DESCRIPTION_B');
			$PARAM_CAT_PUROK_DESCRIPTION_C = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_PUROK_DESCRIPTION_C');
			$PARAM_CAT_PUROK_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_CAT_PUROK_STATUS');
			$PARAM_CAT_PUROK_SYSTEM_GENERATED = PHPCUS_PARAM::CUS_READ_INT('PARAM_CAT_PUROK_SYSTEM_GENERATED');
			
			$PARAM_ORDER_FIELD = PHPCUS_PARAM::CUS_READ_SORT_FIELD
			(
					'PARAM_ORDER_FIELD',
					[
						'CAT_PUROK_ID', 
						'CAT_PUROK_GLOBAL_ID', 
						'CAT_PUROK_CAT_BARANGAY_ID', 
						'CAT_PUROK_CODE', 
						'CAT_PUROK_CODE_B', 
						'CAT_PUROK_CODE_C', 
						'CAT_PUROK_DESCRIPTION', 
						'CAT_PUROK_DESCRIPTION_B', 
						'CAT_PUROK_DESCRIPTION_C', 
						'CAT_PUROK_STATUS', 
						'CAT_PUROK_SYSTEM_GENERATED'
						, 
						'CAT_BARANGAY_ID',		
						'CAT_BARANGAY_GLOBAL_ID',		
						'CAT_BARANGAY_CAT_MUNICIPALITY_ID',		
						'CAT_BARANGAY_CODE',		
						'CAT_BARANGAY_CODE_B',		
						'CAT_BARANGAY_CODE_C',		
						'CAT_BARANGAY_DESCRIPTION',		
						'CAT_BARANGAY_DESCRIPTION_B',		
						'CAT_BARANGAY_DESCRIPTION_C',		
						'CAT_BARANGAY_STATUS',		
						'CAT_BARANGAY_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_CATEGORY_ADDRESS_PUROK::CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			
			$GET_MAP_ARRAY['PARAM_CAT_PUROK_ID'] = $PARAM_CAT_PUROK_ID;
			$GET_MAP_ARRAY['PARAM_CAT_PUROK_GLOBAL_ID'] = $PARAM_CAT_PUROK_GLOBAL_ID;
			$GET_MAP_ARRAY['PARAM_CAT_PUROK_CAT_BARANGAY_ID'] = $PARAM_CAT_PUROK_CAT_BARANGAY_ID;
			$GET_MAP_ARRAY['PARAM_CAT_PUROK_CODE'] = $PARAM_CAT_PUROK_CODE;
			$GET_MAP_ARRAY['PARAM_CAT_PUROK_CODE_B'] = $PARAM_CAT_PUROK_CODE_B;
			$GET_MAP_ARRAY['PARAM_CAT_PUROK_CODE_C'] = $PARAM_CAT_PUROK_CODE_C;
			$GET_MAP_ARRAY['PARAM_CAT_PUROK_DESCRIPTION'] = $PARAM_CAT_PUROK_DESCRIPTION;
			$GET_MAP_ARRAY['PARAM_CAT_PUROK_DESCRIPTION_B'] = $PARAM_CAT_PUROK_DESCRIPTION_B;
			$GET_MAP_ARRAY['PARAM_CAT_PUROK_DESCRIPTION_C'] = $PARAM_CAT_PUROK_DESCRIPTION_C;
			$GET_MAP_ARRAY['PARAM_CAT_PUROK_STATUS'] = $PARAM_CAT_PUROK_STATUS;
			$GET_MAP_ARRAY['PARAM_CAT_PUROK_SYSTEM_GENERATED'] = $PARAM_CAT_PUROK_SYSTEM_GENERATED;
			
			$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_CATEGORY_ADDRESS_PUROK::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');
	}

?>