-- CUSTOMIZED END OF STATEMENT, WHERE REGULAR (;) IS USED BY STORED PROCEDURE
DELIMITER ;; 

DROP PROCEDURE IF EXISTS `RECORD_POS_PAYMENT_GET`;;
CREATE PROCEDURE `RECORD_POS_PAYMENT_GET`
(
	IN `PARAM_REC_POSPAY_ID` BIGINT,	
	IN `PARAM_REC_POSPAY_ACC_GROUP_ID` BIGINT,	
	IN `PARAM_REC_POSPAY_REC_POSDEVICE_ID` BIGINT,	
	IN `PARAM_REC_POSPAY_REC_POSSALE_ID` BIGINT,	
	IN `PARAM_REC_POSPAY_REC_CLIENT_ID` BIGINT,	
	IN `PARAM_REC_POSPAY_CAT_PAYTYPE_ID` BIGINT,	
	IN `PARAM_REC_POSPAY_AMOUNT_PAID_FROM` DECIMAL(18, 2),
	IN `PARAM_REC_POSPAY_AMOUNT_PAID_TO` DECIMAL(18, 2),
	IN `PARAM_REC_POSPAY_CHANGE_FROM` DECIMAL(18, 2),
	IN `PARAM_REC_POSPAY_CHANGE_TO` DECIMAL(18, 2),
	IN `PARAM_REC_POSPAY_NUMBER` VARCHAR(150),	
	IN `PARAM_REC_POSPAY_PAYEE` VARCHAR(400),	
	IN `PARAM_REC_POSPAY_CAT_BANK_ID` BIGINT,	
	IN `PARAM_REC_POSPAY_CAT_CARDTYPE_ID` BIGINT,	
	IN `PARAM_REC_POSPAY_REC_LOYALTY_ID` BIGINT,	
	IN `PARAM_REC_POSPAY_REC_VOUCHER_ID` BIGINT,	
	IN `PARAM_REC_POSPAY_CHECK_DATE_FROM` DATETIME(6),
	IN `PARAM_REC_POSPAY_CHECK_DATE_TO` DATETIME(6),
	IN `PARAM_REC_POSPAY_REMARKS` VARCHAR(1000),	
	IN `PARAM_REC_POSPAY_ACC_ACCOUNT_ID` BIGINT,	
	IN `PARAM_REC_POSPAY_DATE_PROCESSED_FROM` DATETIME(6),
	IN `PARAM_REC_POSPAY_DATE_PROCESSED_TO` DATETIME(6),
	IN `PARAM_REC_POSPAY_IS_INITIAL_PAY` SMALLINT(6),	
	IN `PARAM_REC_POSPAY_STATUS` SMALLINT(6),	
		
	IN `PARAM_ORDER_FIELD` VARCHAR(500),
	IN `PARAM_ORDER_TYPE` VARCHAR(500),
	IN `PARAM_SKIP` BIGINT,
	IN `PARAM_LIMIT` BIGINT,

	IN `PARAM_REQUESTER_USERNAME` VARCHAR(500),
	IN `PARAM_REQUESTER_PASSWORD` VARCHAR(500),
	IN `PARAM_REQUESTER_PROXY_ID` BIGINT,
	IN `PARAM_REQUESTER_PROXY_PASSWORD` VARCHAR(1000),
	IN `PARAM_REQUESTER_DEVICE_PUBLIC_KEY` VARCHAR(1000),
	IN `PARAM_REQUESTER_DEVICE_PRIVATE_KEY` VARCHAR(1000),
	
	IN `PARAM_REQUESTER_IP` VARCHAR(1000),
	IN `PARAM_UNIQUE_SESSION_ID` VARCHAR(1000),
	IN `PARAM_SUPPRESS_RETURN` BIT
)
SP_GET:
BEGIN

	-- Status
	-- =========================================================================================
	DECLARE STATUS_INACTIVE TINYINT DEFAULT 1;
	DECLARE STATUS_ACTIVE TINYINT DEFAULT 2;
	DECLARE STATUS_CLOSED TINYINT DEFAULT 3;
	DECLARE STATUS_COMPLETED TINYINT DEFAULT 4;
	
	-- Audit Handler
	-- =========================================================================================
	DECLARE TEMP_RECORD_EXISTS BIT DEFAULT 0;
	DECLARE TEMP_MODULE_KEYWORD VARCHAR(500) DEFAULT 'record_pos_payment';
	DECLARE TEMP_SP_OBJECTIVE VARCHAR(500) DEFAULT 'Retrieve Payments record';
	DECLARE TEMP_ERR_MESSAGE VARCHAR(500) DEFAULT '';
	DECLARE ERR BIGINT DEFAULT 0;
	DECLARE TEMP_RECORD_CHANGES VARCHAR(16000) DEFAULT '';
	DECLARE TEMP_RECORD_BINDED BIT DEFAULT 0;
	
	DECLARE TEMP_RECORD_BINDED_ID BIGINT DEFAULT 0;
	DECLARE TEMP_RECORD_BINDED_SERVER_ID BIGINT DEFAULT 0;
	
	-- Authentication
	-- =========================================================================================
	DECLARE TEMP_ACCESS_RIGHTS_DENIED BIT DEFAULT 0;
	DECLARE TEMP_AUTHENTICATED_ACCOUNT_ID BIGINT DEFAULT `ACCOUNT_MEMBER@ACC_ACCOUNT_ID_FROM_LOGIN`(PARAM_REQUESTER_USERNAME, PARAM_REQUESTER_PASSWORD);
	DECLARE TEMP_AUTHENTICATED_ACCOUNT_LEVEL BIGINT DEFAULT 0;
	DECLARE TEMP_AUTHENTICATED_ACCOUNT_GROUP BIGINT DEFAULT 0;
	
	-- Local Parameters
	-- =========================================================================================
	DECLARE TEMP_NOW DATETIME(6) DEFAULT `SYSTEM_VAR@NOW`('', 1);
	DECLARE TEMP_RECORD_COUNT BIGINT DEFAULT 0;
	DECLARE TEMPMS_REC_POSPAY_IS_INITIAL_PAY_01 TINYINT DEFAULT `FUN_TINYINT_INDEX_OF`(PARAM_REC_POSPAY_IS_INITIAL_PAY, 1);
	DECLARE TEMPMS_REC_POSPAY_IS_INITIAL_PAY_02 TINYINT DEFAULT `FUN_TINYINT_INDEX_OF`(PARAM_REC_POSPAY_IS_INITIAL_PAY, 2);
	DECLARE TEMPMS_REC_POSPAY_IS_INITIAL_PAY_03 TINYINT DEFAULT `FUN_TINYINT_INDEX_OF`(PARAM_REC_POSPAY_IS_INITIAL_PAY, 3);
	DECLARE TEMPMS_REC_POSPAY_IS_INITIAL_PAY_04 TINYINT DEFAULT `FUN_TINYINT_INDEX_OF`(PARAM_REC_POSPAY_IS_INITIAL_PAY, 4);
	DECLARE TEMPMS_REC_POSPAY_STATUS_01 TINYINT DEFAULT `FUN_TINYINT_INDEX_OF`(PARAM_REC_POSPAY_STATUS, 1);
	DECLARE TEMPMS_REC_POSPAY_STATUS_02 TINYINT DEFAULT `FUN_TINYINT_INDEX_OF`(PARAM_REC_POSPAY_STATUS, 2);
	DECLARE TEMPMS_REC_POSPAY_STATUS_03 TINYINT DEFAULT `FUN_TINYINT_INDEX_OF`(PARAM_REC_POSPAY_STATUS, 3);
	DECLARE TEMPMS_REC_POSPAY_STATUS_04 TINYINT DEFAULT `FUN_TINYINT_INDEX_OF`(PARAM_REC_POSPAY_STATUS, 4);
	
	-- Check NULLs
	-- =========================================================================================
	DECLARE IS_NULL_REC_POSPAY_ID BIT DEFAULT `SYSTEM_VAR@IS_NULL@BIGINT`(PARAM_REC_POSPAY_ID, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_ACC_GROUP_ID BIT DEFAULT `SYSTEM_VAR@IS_NULL@BIGINT`(PARAM_REC_POSPAY_ACC_GROUP_ID, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_REC_POSDEVICE_ID BIT DEFAULT `SYSTEM_VAR@IS_NULL@BIGINT`(PARAM_REC_POSPAY_REC_POSDEVICE_ID, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_REC_POSSALE_ID BIT DEFAULT `SYSTEM_VAR@IS_NULL@BIGINT`(PARAM_REC_POSPAY_REC_POSSALE_ID, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_REC_CLIENT_ID BIT DEFAULT `SYSTEM_VAR@IS_NULL@BIGINT`(PARAM_REC_POSPAY_REC_CLIENT_ID, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_CAT_PAYTYPE_ID BIT DEFAULT `SYSTEM_VAR@IS_NULL@BIGINT`(PARAM_REC_POSPAY_CAT_PAYTYPE_ID, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_AMOUNT_PAID_FROM BIT DEFAULT `SYSTEM_VAR@IS_NULL@DECIMAL`(PARAM_REC_POSPAY_AMOUNT_PAID_FROM, 0, 1);
	DECLARE IS_NULL_REC_POSPAY_AMOUNT_PAID_TO BIT DEFAULT `SYSTEM_VAR@IS_NULL@DECIMAL`(PARAM_REC_POSPAY_AMOUNT_PAID_TO, 0, 1);
	DECLARE IS_NULL_REC_POSPAY_CHANGE_FROM BIT DEFAULT `SYSTEM_VAR@IS_NULL@DECIMAL`(PARAM_REC_POSPAY_CHANGE_FROM, 0, 1);
	DECLARE IS_NULL_REC_POSPAY_CHANGE_TO BIT DEFAULT `SYSTEM_VAR@IS_NULL@DECIMAL`(PARAM_REC_POSPAY_CHANGE_TO, 0, 1);
	DECLARE IS_NULL_REC_POSPAY_NUMBER BIT DEFAULT `SYSTEM_VAR@IS_NULL@NVARCHAR`(PARAM_REC_POSPAY_NUMBER, 0);
	DECLARE IS_NULL_REC_POSPAY_PAYEE BIT DEFAULT `SYSTEM_VAR@IS_NULL@NVARCHAR`(PARAM_REC_POSPAY_PAYEE, 0);
	DECLARE IS_NULL_REC_POSPAY_CAT_BANK_ID BIT DEFAULT `SYSTEM_VAR@IS_NULL@BIGINT`(PARAM_REC_POSPAY_CAT_BANK_ID, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_CAT_CARDTYPE_ID BIT DEFAULT `SYSTEM_VAR@IS_NULL@BIGINT`(PARAM_REC_POSPAY_CAT_CARDTYPE_ID, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_REC_LOYALTY_ID BIT DEFAULT `SYSTEM_VAR@IS_NULL@BIGINT`(PARAM_REC_POSPAY_REC_LOYALTY_ID, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_REC_VOUCHER_ID BIT DEFAULT `SYSTEM_VAR@IS_NULL@BIGINT`(PARAM_REC_POSPAY_REC_VOUCHER_ID, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_CHECK_DATE_FROM BIT DEFAULT `SYSTEM_VAR@IS_NULL@DATETIME`(PARAM_REC_POSPAY_CHECK_DATE_FROM, 1);
	DECLARE IS_NULL_REC_POSPAY_CHECK_DATE_TO BIT DEFAULT `SYSTEM_VAR@IS_NULL@DATETIME`(PARAM_REC_POSPAY_CHECK_DATE_TO, 1);				
	DECLARE IS_NULL_REC_POSPAY_REMARKS BIT DEFAULT `SYSTEM_VAR@IS_NULL@NVARCHAR`(PARAM_REC_POSPAY_REMARKS, 0);
	DECLARE IS_NULL_REC_POSPAY_ACC_ACCOUNT_ID BIT DEFAULT `SYSTEM_VAR@IS_NULL@BIGINT`(PARAM_REC_POSPAY_ACC_ACCOUNT_ID, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_DATE_PROCESSED_FROM BIT DEFAULT `SYSTEM_VAR@IS_NULL@DATETIME`(PARAM_REC_POSPAY_DATE_PROCESSED_FROM, 1);
	DECLARE IS_NULL_REC_POSPAY_DATE_PROCESSED_TO BIT DEFAULT `SYSTEM_VAR@IS_NULL@DATETIME`(PARAM_REC_POSPAY_DATE_PROCESSED_TO, 1);				
	DECLARE IS_NULL_REC_POSPAY_IS_INITIAL_PAY BIT DEFAULT `SYSTEM_VAR@IS_NULL@TINYINT`(PARAM_REC_POSPAY_IS_INITIAL_PAY, 1, 1);
	DECLARE IS_NULL_REC_POSPAY_STATUS BIT DEFAULT `SYSTEM_VAR@IS_NULL@TINYINT`(PARAM_REC_POSPAY_STATUS, 1, 1);

	DECLARE IS_NOT_LIKE_REC_POSPAY_NUMBER BIT DEFAULT `FUN_CONTAINS_LEFT`(PARAM_REC_POSPAY_NUMBER, '#RAISE_FLAG:NOT_LIKE;');
	DECLARE IS_NOT_LIKE_REC_POSPAY_PAYEE BIT DEFAULT `FUN_CONTAINS_LEFT`(PARAM_REC_POSPAY_PAYEE, '#RAISE_FLAG:NOT_LIKE;');
	DECLARE IS_NOT_LIKE_REC_POSPAY_REMARKS BIT DEFAULT `FUN_CONTAINS_LEFT`(PARAM_REC_POSPAY_REMARKS, '#RAISE_FLAG:NOT_LIKE;');
	
	DECLARE IS_EXACT_REC_POSPAY_NUMBER BIT DEFAULT `FUN_CONTAINS_LEFT`(PARAM_REC_POSPAY_NUMBER, '#RAISE_FLAG:EXACT;');
	DECLARE IS_EXACT_REC_POSPAY_PAYEE BIT DEFAULT `FUN_CONTAINS_LEFT`(PARAM_REC_POSPAY_PAYEE, '#RAISE_FLAG:EXACT;');
	DECLARE IS_EXACT_REC_POSPAY_REMARKS BIT DEFAULT `FUN_CONTAINS_LEFT`(PARAM_REC_POSPAY_REMARKS, '#RAISE_FLAG:EXACT;');
	
	-- Error Handler
	-- =========================================================================================
	DECLARE EXIT HANDLER FOR SQLEXCEPTION
	BEGIN
		RESIGNAL;
	END;
	
	-- Ignore Time from DateTime (Set Time 00:00:00.000000 and 23:59:59.999999)
	-- =========================================================================================
		
	-- Retrieve data for NOT LIKE searches
	-- =========================================================================================
	IF IS_NOT_LIKE_REC_POSPAY_NUMBER = 1 THEN
		SET PARAM_REC_POSPAY_NUMBER = `FUN_DLEFT`(PARAM_REC_POSPAY_NUMBER, LENGTH('#RAISE_FLAG:NOT_LIKE;'));
	END IF;
	IF IS_NOT_LIKE_REC_POSPAY_PAYEE = 1 THEN
		SET PARAM_REC_POSPAY_PAYEE = `FUN_DLEFT`(PARAM_REC_POSPAY_PAYEE, LENGTH('#RAISE_FLAG:NOT_LIKE;'));
	END IF;
	IF IS_NOT_LIKE_REC_POSPAY_REMARKS = 1 THEN
		SET PARAM_REC_POSPAY_REMARKS = `FUN_DLEFT`(PARAM_REC_POSPAY_REMARKS, LENGTH('#RAISE_FLAG:NOT_LIKE;'));
	END IF;
	
	-- Retrieve data for EXACT searches
	-- =========================================================================================
	IF IS_EXACT_REC_POSPAY_NUMBER = 1 THEN
		SET PARAM_REC_POSPAY_NUMBER = `FUN_DLEFT`(PARAM_REC_POSPAY_NUMBER, LENGTH('#RAISE_FLAG:EXACT;'));
	END IF;
	IF IS_EXACT_REC_POSPAY_PAYEE = 1 THEN
		SET PARAM_REC_POSPAY_PAYEE = `FUN_DLEFT`(PARAM_REC_POSPAY_PAYEE, LENGTH('#RAISE_FLAG:EXACT;'));
	END IF;
	IF IS_EXACT_REC_POSPAY_REMARKS = 1 THEN
		SET PARAM_REC_POSPAY_REMARKS = `FUN_DLEFT`(PARAM_REC_POSPAY_REMARKS, LENGTH('#RAISE_FLAG:EXACT;'));
	END IF;
	
	-- Prepare Strings
	-- =========================================================================================
	IF IS_EXACT_REC_POSPAY_NUMBER = 0 THEN
		SET PARAM_REC_POSPAY_NUMBER = CONCAT_WS('%', '', PARAM_REC_POSPAY_NUMBER, '');
	END IF;
	IF IS_EXACT_REC_POSPAY_PAYEE = 0 THEN
		SET PARAM_REC_POSPAY_PAYEE = CONCAT_WS('%', '', PARAM_REC_POSPAY_PAYEE, '');
	END IF;
	IF IS_EXACT_REC_POSPAY_REMARKS = 0 THEN
		SET PARAM_REC_POSPAY_REMARKS = CONCAT_WS('%', '', PARAM_REC_POSPAY_REMARKS, '');
	END IF;
	
	-- Access Rights [Device]
	-- =========================================================================================
	IF `ACCOUNT_DEVICE@HAS_PERMISSION`(PARAM_REQUESTER_DEVICE_PUBLIC_KEY, PARAM_REQUESTER_DEVICE_PRIVATE_KEY) = 0 THEN
		SET TEMP_ERR_MESSAGE = 'Your device is not authorized yet to access this server.';
		SET ERR = `RAISERROR`(TEMP_ERR_MESSAGE);		
	END IF;
		
		
	-- Access Rights [Account]
	-- =========================================================================================
	-- Check if accessing a record outside own group.
	SET TEMP_AUTHENTICATED_ACCOUNT_GROUP = `ACCOUNT_MEMBER@ACC_ACCOUNT_GROUP_FROM_LOGIN`(PARAM_REQUESTER_USERNAME, PARAM_REQUESTER_PASSWORD);
	IF TEMP_AUTHENTICATED_ACCOUNT_GROUP <> PARAM_REC_POSPAY_ACC_GROUP_ID THEN
		IF 
			`ACCOUNT_ACCESS_RIGHTS@HAS_PERMISSION`(PARAM_REQUESTER_USERNAME, PARAM_REQUESTER_PASSWORD, 0, TEMP_MODULE_KEYWORD, 5) = 0 AND
			`ACCOUNT_ACCESS_RIGHTS@PROXY_PERMISSION`(PARAM_REQUESTER_PROXY_ID, PARAM_REQUESTER_PROXY_PASSWORD, 0, TEMP_MODULE_KEYWORD, 5) = 0
		THEN
			SET TEMP_ACCESS_RIGHTS_DENIED = 1;
			CALL `ACCOUNT_ACCESS_RIGHTS_DENY`
			(
				0, TEMP_MODULE_KEYWORD, 0, 0, TEMP_SP_OBJECTIVE, TEMP_RECORD_CHANGES, 
				PARAM_REQUESTER_USERNAME, PARAM_REQUESTER_PASSWORD, 
				PARAM_REQUESTER_PROXY_ID, PARAM_REQUESTER_PROXY_PASSWORD,
				PARAM_REQUESTER_DEVICE_PUBLIC_KEY, PARAM_REQUESTER_DEVICE_PRIVATE_KEY,
				PARAM_REQUESTER_IP, PARAM_UNIQUE_SESSION_ID,
				0, PARAM_SUPPRESS_RETURN
			);
		END IF;					
	END IF;
		
	IF 
		`ACCOUNT_ACCESS_RIGHTS@HAS_PERMISSION`(PARAM_REQUESTER_USERNAME, PARAM_REQUESTER_PASSWORD, 0, TEMP_MODULE_KEYWORD, 1) = 0 AND
		`ACCOUNT_ACCESS_RIGHTS@PROXY_PERMISSION`(PARAM_REQUESTER_PROXY_ID, PARAM_REQUESTER_PROXY_PASSWORD, 0, TEMP_MODULE_KEYWORD, 1) = 0
	THEN
		SET TEMP_ACCESS_RIGHTS_DENIED = 1;
		CALL `ACCOUNT_ACCESS_RIGHTS_DENY`
		(
			0, TEMP_MODULE_KEYWORD, 0, 0, TEMP_SP_OBJECTIVE, TEMP_RECORD_CHANGES, 
			PARAM_REQUESTER_USERNAME, PARAM_REQUESTER_PASSWORD, 
			PARAM_REQUESTER_PROXY_ID, PARAM_REQUESTER_PROXY_PASSWORD,
			PARAM_REQUESTER_DEVICE_PUBLIC_KEY, PARAM_REQUESTER_DEVICE_PRIVATE_KEY,
			PARAM_REQUESTER_IP, PARAM_UNIQUE_SESSION_ID,
			0, PARAM_SUPPRESS_RETURN
		);
	END IF;
	
	
	
	-- COUNT RECORD
	-- =========================================================================================
	
	-- SELECT COLUMNS
	-- ####################################################
	SELECT
		COUNT(DISTINCT `record_pos_payment`.`REC_POSPAY_ID`)
	INTO
		TEMP_RECORD_COUNT

	FROM `record_pos_payment`
				
		-- Add Inner Joins (Per Foreign-key)
		INNER JOIN `account_group` ON
		`account_group`.`ACC_GROUP_ID` = `record_pos_payment`.`REC_POSPAY_ACC_GROUP_ID`
			
		-- ~~~~~	
			
		INNER JOIN `record_posdevice` ON
		`record_posdevice`.`REC_POSDEVICE_ID` = `record_pos_payment`.`REC_POSPAY_REC_POSDEVICE_ID`
			
		-- ~~~~~	
			
		INNER JOIN `record_pos_sale` ON
		`record_pos_sale`.`REC_POSSALE_ID` = `record_pos_payment`.`REC_POSPAY_REC_POSSALE_ID`
			
		-- ~~~~~	
			
		INNER JOIN `record_client` ON
		`record_client`.`REC_CLIENT_ID` = `record_pos_payment`.`REC_POSPAY_REC_CLIENT_ID`
			
		-- ~~~~~	
			
		INNER JOIN `category_payment_type` ON
		`category_payment_type`.`CAT_PAYTYPE_ID` = `record_pos_payment`.`REC_POSPAY_CAT_PAYTYPE_ID`
			
		-- ~~~~~	
			
		INNER JOIN `category_bank` ON
		`category_bank`.`CAT_BANK_ID` = `record_pos_payment`.`REC_POSPAY_CAT_BANK_ID`
			
		-- ~~~~~	
			
		INNER JOIN `category_card_type` ON
		`category_card_type`.`CAT_CARDTYPE_ID` = `record_pos_payment`.`REC_POSPAY_CAT_CARDTYPE_ID`
			
		-- ~~~~~	
			
		INNER JOIN `record_loyalty` ON
		`record_loyalty`.`REC_LOYALTY_ID` = `record_pos_payment`.`REC_POSPAY_REC_LOYALTY_ID`
			
		-- ~~~~~	
			
		INNER JOIN `record_voucher` ON
		`record_voucher`.`REC_VOUCHER_ID` = `record_pos_payment`.`REC_POSPAY_REC_VOUCHER_ID`
			
		-- ~~~~~	
			
		INNER JOIN `account_member` ON
		`account_member`.`ACC_ACCOUNT_ID` = `record_pos_payment`.`REC_POSPAY_ACC_ACCOUNT_ID`
			
			
		
	-- CONDITIONS
	-- ####################################################
	WHERE
		(
			IS_NULL_REC_POSPAY_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_ID` = PARAM_REC_POSPAY_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_ACC_GROUP_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_ACC_GROUP_ID` = PARAM_REC_POSPAY_ACC_GROUP_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_REC_POSDEVICE_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_REC_POSDEVICE_ID` = PARAM_REC_POSPAY_REC_POSDEVICE_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_REC_POSSALE_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_REC_POSSALE_ID` = PARAM_REC_POSPAY_REC_POSSALE_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_REC_CLIENT_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_REC_CLIENT_ID` = PARAM_REC_POSPAY_REC_CLIENT_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_CAT_PAYTYPE_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_CAT_PAYTYPE_ID` = PARAM_REC_POSPAY_CAT_PAYTYPE_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_AMOUNT_PAID_FROM = 1 OR
			`record_pos_payment`.`REC_POSPAY_AMOUNT_PAID` >= PARAM_REC_POSPAY_AMOUNT_PAID_FROM			
		) AND
		(
			IS_NULL_REC_POSPAY_AMOUNT_PAID_TO = 1 OR
			`record_pos_payment`.`REC_POSPAY_AMOUNT_PAID` <= PARAM_REC_POSPAY_AMOUNT_PAID_TO			
		) AND 		
		(
			IS_NULL_REC_POSPAY_CHANGE_FROM = 1 OR
			`record_pos_payment`.`REC_POSPAY_CHANGE` >= PARAM_REC_POSPAY_CHANGE_FROM			
		) AND
		(
			IS_NULL_REC_POSPAY_CHANGE_TO = 1 OR
			`record_pos_payment`.`REC_POSPAY_CHANGE` <= PARAM_REC_POSPAY_CHANGE_TO			
		) AND 		
		(
			IS_NULL_REC_POSPAY_NUMBER = 1 OR
			(
				IS_NOT_LIKE_REC_POSPAY_NUMBER = 0 AND
				`record_pos_payment`.`REC_POSPAY_NUMBER` LIKE PARAM_REC_POSPAY_NUMBER			
			) OR
			(
				IS_NOT_LIKE_REC_POSPAY_NUMBER = 1 AND
				`record_pos_payment`.`REC_POSPAY_NUMBER` NOT LIKE PARAM_REC_POSPAY_NUMBER		
			)
		) AND 		
		(
			IS_NULL_REC_POSPAY_PAYEE = 1 OR
			(
				IS_NOT_LIKE_REC_POSPAY_PAYEE = 0 AND
				`record_pos_payment`.`REC_POSPAY_PAYEE` LIKE PARAM_REC_POSPAY_PAYEE			
			) OR
			(
				IS_NOT_LIKE_REC_POSPAY_PAYEE = 1 AND
				`record_pos_payment`.`REC_POSPAY_PAYEE` NOT LIKE PARAM_REC_POSPAY_PAYEE		
			)
		) AND 		
		(
			IS_NULL_REC_POSPAY_CAT_BANK_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_CAT_BANK_ID` = PARAM_REC_POSPAY_CAT_BANK_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_CAT_CARDTYPE_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_CAT_CARDTYPE_ID` = PARAM_REC_POSPAY_CAT_CARDTYPE_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_REC_LOYALTY_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_REC_LOYALTY_ID` = PARAM_REC_POSPAY_REC_LOYALTY_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_REC_VOUCHER_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_REC_VOUCHER_ID` = PARAM_REC_POSPAY_REC_VOUCHER_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_CHECK_DATE_FROM = 1 OR
			`record_pos_payment`.`REC_POSPAY_CHECK_DATE` >= PARAM_REC_POSPAY_CHECK_DATE_FROM			
		) AND
		(
			IS_NULL_REC_POSPAY_CHECK_DATE_TO = 1 OR
			`record_pos_payment`.`REC_POSPAY_CHECK_DATE` <= PARAM_REC_POSPAY_CHECK_DATE_TO			
		) AND 		
		(
			IS_NULL_REC_POSPAY_REMARKS = 1 OR
			(
				IS_NOT_LIKE_REC_POSPAY_REMARKS = 0 AND
				`record_pos_payment`.`REC_POSPAY_REMARKS` LIKE PARAM_REC_POSPAY_REMARKS			
			) OR
			(
				IS_NOT_LIKE_REC_POSPAY_REMARKS = 1 AND
				`record_pos_payment`.`REC_POSPAY_REMARKS` NOT LIKE PARAM_REC_POSPAY_REMARKS		
			)
		) AND 		
		(
			IS_NULL_REC_POSPAY_ACC_ACCOUNT_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_ACC_ACCOUNT_ID` = PARAM_REC_POSPAY_ACC_ACCOUNT_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_DATE_PROCESSED_FROM = 1 OR
			`record_pos_payment`.`REC_POSPAY_DATE_PROCESSED` >= PARAM_REC_POSPAY_DATE_PROCESSED_FROM			
		) AND
		(
			IS_NULL_REC_POSPAY_DATE_PROCESSED_TO = 1 OR
			`record_pos_payment`.`REC_POSPAY_DATE_PROCESSED` <= PARAM_REC_POSPAY_DATE_PROCESSED_TO			
		) AND 		
		(
			IS_NULL_REC_POSPAY_IS_INITIAL_PAY = 1 OR
			`record_pos_payment`.`REC_POSPAY_IS_INITIAL_PAY` = TEMPMS_REC_POSPAY_IS_INITIAL_PAY_01 OR	
			`record_pos_payment`.`REC_POSPAY_IS_INITIAL_PAY` = TEMPMS_REC_POSPAY_IS_INITIAL_PAY_02	OR	
			`record_pos_payment`.`REC_POSPAY_IS_INITIAL_PAY` = TEMPMS_REC_POSPAY_IS_INITIAL_PAY_03	OR
			`record_pos_payment`.`REC_POSPAY_IS_INITIAL_PAY` = TEMPMS_REC_POSPAY_IS_INITIAL_PAY_04
		)AND 				
		(
			IS_NULL_REC_POSPAY_STATUS = 1 OR
			`record_pos_payment`.`REC_POSPAY_STATUS` = TEMPMS_REC_POSPAY_STATUS_01 OR	
			`record_pos_payment`.`REC_POSPAY_STATUS` = TEMPMS_REC_POSPAY_STATUS_02	OR	
			`record_pos_payment`.`REC_POSPAY_STATUS` = TEMPMS_REC_POSPAY_STATUS_03	OR
			`record_pos_payment`.`REC_POSPAY_STATUS` = TEMPMS_REC_POSPAY_STATUS_04
		)				
;	

		
		
	-- SELECT RECORDS
	-- =========================================================================================
	
	-- FETCH COLUMNS
	-- ####################################################
	SELECT
		-- RECORD_POS_PAYMENT DETAILS
		`record_pos_payment`.`REC_POSPAY_ID`, 
		`record_pos_payment`.`REC_POSPAY_ACC_GROUP_ID`, 
		`record_pos_payment`.`REC_POSPAY_REC_POSDEVICE_ID`, 
		`record_pos_payment`.`REC_POSPAY_REC_POSSALE_ID`, 
		`record_pos_payment`.`REC_POSPAY_REC_CLIENT_ID`, 
		`record_pos_payment`.`REC_POSPAY_CAT_PAYTYPE_ID`, 
		`record_pos_payment`.`REC_POSPAY_AMOUNT_PAID`, 
		`record_pos_payment`.`REC_POSPAY_CHANGE`, 
		`record_pos_payment`.`REC_POSPAY_NUMBER`, 
		`record_pos_payment`.`REC_POSPAY_PAYEE`, 
		`record_pos_payment`.`REC_POSPAY_CAT_BANK_ID`, 
		`record_pos_payment`.`REC_POSPAY_CAT_CARDTYPE_ID`, 
		`record_pos_payment`.`REC_POSPAY_REC_LOYALTY_ID`, 
		`record_pos_payment`.`REC_POSPAY_REC_VOUCHER_ID`, 
		`record_pos_payment`.`REC_POSPAY_CHECK_DATE`, 
		`record_pos_payment`.`REC_POSPAY_REMARKS`, 
		`record_pos_payment`.`REC_POSPAY_ACC_ACCOUNT_ID`, 
		`record_pos_payment`.`REC_POSPAY_DATE_PROCESSED`, 
		`record_pos_payment`.`REC_POSPAY_IS_INITIAL_PAY`, 
		`record_pos_payment`.`REC_POSPAY_STATUS`
		
		-- Sum of Child [Numerics, non-primary/foreign keys, nor status, active/inactive, and system generated.]
		
		
		-- Inner join fields
		, 
		`account_group`.`ACC_GROUP_ID`,	
		`account_group`.`ACC_GROUP_DESCRIPTION` AS REC_POSPAY_ACC_GROUP_ID_DESCRIPTION,
		`account_group`.`ACC_GROUP_DESCRIPTION`,	
		`account_group`.`ACC_GROUP_STATUS`,	
		`account_group`.`ACC_GROUP_SYSTEM_GENERATED`,	
		`record_posdevice`.`REC_POSDEVICE_ID`,	
		`record_posdevice`.`REC_POSDEVICE_ACC_GROUP_ID`,	
		`record_posdevice`.`REC_POSDEVICE_DESCRIPTION` AS REC_POSPAY_REC_POSDEVICE_ID_DESCRIPTION,
		`record_posdevice`.`REC_POSDEVICE_DESCRIPTION`,	
		`record_posdevice`.`REC_POSDEVICE_MODEL`,	
		`record_posdevice`.`REC_POSDEVICE_OS`,	
		`record_posdevice`.`REC_POSDEVICE_MACHINE_ID`,	
		`record_posdevice`.`REC_POSDEVICE_PERMIT_NUMBER`,	
		`record_posdevice`.`REC_POSDEVICE_DATE_ACCREDITED`,	
		`record_posdevice`.`REC_POSDEVICE_ADDRESS`,	
		`record_posdevice`.`REC_POSDEVICE_CONTACT`,	
		`record_posdevice`.`REC_POSDEVICE_TIN`,	
		`record_posdevice`.`REC_POSDEVICE_RESET_COUNT`,	
		`record_posdevice`.`REC_POSDEVICE_REGISTRATION_ACC_ACCOUNT_ID`,	
		`record_posdevice`.`REC_POSDEVICE_DATE_REGISTERED`,	
		`record_posdevice`.`REC_POSDEVICE_PROCESSED_ACC_ACCOUNT_ID`,	
		`record_posdevice`.`REC_POSDEVICE_DATE_PROCESSED`,	
		`record_posdevice`.`REC_POSDEVICE_STATUS`,	
		`record_pos_sale`.`REC_POSSALE_ID`,	
		`record_pos_sale`.`REC_POSSALE_ACC_GROUP_ID`,	
		`record_pos_sale`.`REC_POSSALE_REC_POSDEVICE_ID`,	
		`record_pos_sale`.`REC_POSSALE_REC_CLIENT_ID`,	
		`record_pos_sale`.`REC_POSSALE_REC_LOYALTY_ID`,	
		`record_pos_sale`.`REC_POSSALE_CODE` AS REC_POSPAY_REC_POSSALE_ID_DESCRIPTION,
		`record_pos_sale`.`REC_POSSALE_CODE`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_SUBTOTAL`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_DISCOUNT`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_VATABLE`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_VATEXEMPT`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_NONVAT`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_ZERORATED`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_VATOTHER`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_VAT`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_VAT_LESS`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_POINT`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_RTN_SUBTOTAL`,	
		`record_pos_sale`.`REC_POSSALE_TOTAL_PAID`,	
		`record_pos_sale`.`REC_POSSALE_DATE_PAID`,	
		`record_pos_sale`.`REC_POSSALE_DATE_DUE`,	
		`record_pos_sale`.`REC_POSSALE_REMARKS`,	
		`record_pos_sale`.`REC_POSSALE_ACC_ACCOUNT_ID`,	
		`record_pos_sale`.`REC_POSSALE_DATE_TRANSACT`,	
		`record_pos_sale`.`REC_POSSALE_STATUS`,	
		`record_client`.`REC_CLIENT_ID`,	
		`record_client`.`REC_CLIENT_CODE` AS REC_POSPAY_REC_CLIENT_ID_DESCRIPTION,
		`record_client`.`REC_CLIENT_CODE`,	
		CONCAT_WS
		(
			' ',
			`record_client`.`REC_CLIENT_FIRSTNAME`,
			`record_client`.`REC_CLIENT_MIDDLENAME`,
			`record_client`.`REC_CLIENT_LASTNAME`
		) AS REC_CLIENT_NAME,
		`record_client`.`REC_CLIENT_LASTNAME`,
		`record_client`.`REC_CLIENT_STORE_NAME`,	
		`record_client`.`REC_CLIENT_FIRSTNAME`,	
		`record_client`.`REC_CLIENT_MIDDLENAME`,	
		`record_client`.`REC_CLIENT_CAT_SUFFIX_ID`,	
		`record_client`.`REC_CLIENT_SYS_GENDER_ID`,	
		`record_client`.`REC_CLIENT_CONTACT`,	
		`record_client`.`REC_CLIENT_EMAIL`,	
		`record_client`.`REC_CLIENT_CAT_COMPANY_ID`,	
		`record_client`.`REC_CLIENT_CAT_PRICEMDL_ID`,	
		`record_client`.`REC_CLIENT_SENIOR_CODE`,	
		`record_client`.`REC_CLIENT_PWD_CODE`,	
		`record_client`.`REC_CLIENT_TIN`,	
		`record_client`.`REC_CLIENT_DATE_ISSUED`,	
		`record_client`.`REC_CLIENT_ADDRESS`,	
		`record_client`.`REC_CLIENT_REMARKS`,	
		`record_client`.`REC_CLIENT_DATE_REGISTER`,	
		`record_client`.`REC_CLIENT_STATUS`,	
		`record_client`.`REC_CLIENT_SYSTEM_GENERATED`,	
		`category_payment_type`.`CAT_PAYTYPE_ID`,	
		`category_payment_type`.`CAT_PAYTYPE_CODE`,	
		`category_payment_type`.`CAT_PAYTYPE_DESCRIPTION` AS REC_POSPAY_CAT_PAYTYPE_ID_DESCRIPTION,
		`category_payment_type`.`CAT_PAYTYPE_DESCRIPTION`,	
		`category_payment_type`.`CAT_PAYTYPE_STATUS`,	
		`category_bank`.`CAT_BANK_ID`,	
		`category_bank`.`CAT_BANK_CODE`,	
		`category_bank`.`CAT_BANK_DESCRIPTION` AS REC_POSPAY_CAT_BANK_ID_DESCRIPTION,
		`category_bank`.`CAT_BANK_DESCRIPTION`,	
		`category_bank`.`CAT_BANK_REMARKS`,	
		`category_bank`.`CAT_BANK_STATUS`,	
		`category_bank`.`CAT_BANK_SYSTEM_GENERATED`,	
		`category_card_type`.`CAT_CARDTYPE_ID`,	
		`category_card_type`.`CAT_CARDTYPE_DESCRIPTION` AS REC_POSPAY_CAT_CARDTYPE_ID_DESCRIPTION,
		`category_card_type`.`CAT_CARDTYPE_DESCRIPTION`,	
		`category_card_type`.`CAT_CARDTYPE_STATUS`,	
		`record_loyalty`.`REC_LOYALTY_ID`,	
		`record_loyalty`.`REC_LOYALTY_REC_CLIENT_ID`,	
		`record_loyalty`.`REC_LOYALTY_REC_LOYALTY_ID`,	
		`record_loyalty`.`REC_LOYALTY_CODE` AS REC_POSPAY_REC_LOYALTY_ID_DESCRIPTION,
		`record_loyalty`.`REC_LOYALTY_CODE`,	
		`record_loyalty`.`REC_LOYALTY_BALANCE`,	
		`record_loyalty`.`REC_LOYALTY_DATE_EFFECTIVE`,	
		`record_loyalty`.`REC_LOYALTY_DATE_EXPIRY`,	
		`record_loyalty`.`REC_LOYALTY_STATUS`,	
		`record_loyalty`.`REC_LOYALTY_SYSTEM_GENERATED`,	
		`record_voucher`.`REC_VOUCHER_ID`,	
		`record_voucher`.`REC_VOUCHER_CODE` AS REC_POSPAY_REC_VOUCHER_ID_DESCRIPTION,
		`record_voucher`.`REC_VOUCHER_CODE`,	
		`record_voucher`.`REC_VOUCHER_IS_VOUCHER`,	
		`record_voucher`.`REC_VOUCHER_CAT_DISCOUNT_ID`,	
		`record_voucher`.`REC_VOUCHER_AMOUNT`,	
		`record_voucher`.`REC_VOUCHER_DATE_BEGIN`,	
		`record_voucher`.`REC_VOUCHER_DATE_END`,	
		`record_voucher`.`REC_VOUCHER_IS_UNLIMITED`,	
		`record_voucher`.`REC_VOUCHER_STATUS`,	
		`record_voucher`.`REC_VOUCHER_SYSTEM_GENERATED`,
		`record_pos_sale`.`REC_POSSALE_CODE`,	
		`account_member`.`ACC_ACCOUNT_ID`,	
		`account_member`.`ACC_ACCOUNT_SERVER_ID`,	
		`account_member`.`ACC_ACCOUNT_ACC_LEVEL_ID`,	
		`account_member`.`ACC_ACCOUNT_ACC_GROUP_ID`,	
		`account_member`.`ACC_ACCOUNT_ADD_RESOURCE_ID`,	
		CONCAT_WS
		( 
					' ',
			`account_member`.`ACC_ACCOUNT_LAST_NAME`, ', ',
				`account_member`.`ACC_ACCOUNT_FIRST_NAME`,
			`account_member`.`ACC_ACCOUNT_MIDDLE_NAME`
		) AS ACC_ACCOUNT_NAME,
		`account_member`.`ACC_ACCOUNT_FIRST_NAME`, 				
		`account_member`.`ACC_ACCOUNT_MIDDLE_NAME`,	
		`account_member`.`ACC_ACCOUNT_LAST_NAME`,	
		`account_member`.`ACC_ACCOUNT_POSITION`,	
		`account_member`.`ACC_ACCOUNT_SYS_GENDER_ID`,	
		`account_member`.`ACC_ACCOUNT_STATUS`,	
		`account_member`.`ACC_ACCOUNT_SYSTEM_GENERATED`	
		
		-- Left join fields
		
		, TEMP_NOW AS SERVER_TIME,
		TEMP_RECORD_COUNT AS RECORD_COUNT

	FROM `record_pos_payment`
	
		-- Add Inner Joins (Per Foreign-key)
		INNER JOIN `account_group` ON
		`account_group`.`ACC_GROUP_ID` = `record_pos_payment`.`REC_POSPAY_ACC_GROUP_ID`
			
		-- ~~~~~	
			
		INNER JOIN `record_posdevice` ON
		`record_posdevice`.`REC_POSDEVICE_ID` = `record_pos_payment`.`REC_POSPAY_REC_POSDEVICE_ID`
			
		-- ~~~~~	
			
		INNER JOIN `record_pos_sale` ON
		`record_pos_sale`.`REC_POSSALE_ID` = `record_pos_payment`.`REC_POSPAY_REC_POSSALE_ID`
			
		-- ~~~~~	
			
		INNER JOIN `record_client` ON
		`record_client`.`REC_CLIENT_ID` = `record_pos_payment`.`REC_POSPAY_REC_CLIENT_ID`
			
		-- ~~~~~	
			
		INNER JOIN `category_payment_type` ON
		`category_payment_type`.`CAT_PAYTYPE_ID` = `record_pos_payment`.`REC_POSPAY_CAT_PAYTYPE_ID`
			
		-- ~~~~~	
			
		INNER JOIN `category_bank` ON
		`category_bank`.`CAT_BANK_ID` = `record_pos_payment`.`REC_POSPAY_CAT_BANK_ID`
			
		-- ~~~~~	
			
		INNER JOIN `category_card_type` ON
		`category_card_type`.`CAT_CARDTYPE_ID` = `record_pos_payment`.`REC_POSPAY_CAT_CARDTYPE_ID`
			
		-- ~~~~~	
			
		INNER JOIN `record_loyalty` ON
		`record_loyalty`.`REC_LOYALTY_ID` = `record_pos_payment`.`REC_POSPAY_REC_LOYALTY_ID`
			
		-- ~~~~~	
			
		INNER JOIN `record_voucher` ON
		`record_voucher`.`REC_VOUCHER_ID` = `record_pos_payment`.`REC_POSPAY_REC_VOUCHER_ID`
			
		-- ~~~~~	
			
		INNER JOIN `account_member` ON
		`account_member`.`ACC_ACCOUNT_ID` = `record_pos_payment`.`REC_POSPAY_ACC_ACCOUNT_ID`

			
			
		
		-- Add Optional Left Joins (Per Optional Foreign-key)
		
	-- CONDITIONS
	-- ####################################################
	WHERE
		(
			IS_NULL_REC_POSPAY_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_ID` = PARAM_REC_POSPAY_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_ACC_GROUP_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_ACC_GROUP_ID` = PARAM_REC_POSPAY_ACC_GROUP_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_REC_POSDEVICE_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_REC_POSDEVICE_ID` = PARAM_REC_POSPAY_REC_POSDEVICE_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_REC_POSSALE_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_REC_POSSALE_ID` = PARAM_REC_POSPAY_REC_POSSALE_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_REC_CLIENT_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_REC_CLIENT_ID` = PARAM_REC_POSPAY_REC_CLIENT_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_CAT_PAYTYPE_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_CAT_PAYTYPE_ID` = PARAM_REC_POSPAY_CAT_PAYTYPE_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_AMOUNT_PAID_FROM = 1 OR
			`record_pos_payment`.`REC_POSPAY_AMOUNT_PAID` >= PARAM_REC_POSPAY_AMOUNT_PAID_FROM			
		) AND
		(
			IS_NULL_REC_POSPAY_AMOUNT_PAID_TO = 1 OR
			`record_pos_payment`.`REC_POSPAY_AMOUNT_PAID` <= PARAM_REC_POSPAY_AMOUNT_PAID_TO			
		) AND 
		(
			IS_NULL_REC_POSPAY_CHANGE_FROM = 1 OR
			`record_pos_payment`.`REC_POSPAY_CHANGE` >= PARAM_REC_POSPAY_CHANGE_FROM			
		) AND
		(
			IS_NULL_REC_POSPAY_CHANGE_TO = 1 OR
			`record_pos_payment`.`REC_POSPAY_CHANGE` <= PARAM_REC_POSPAY_CHANGE_TO			
		) AND 
		(
			IS_NULL_REC_POSPAY_NUMBER = 1 OR
			(
				IS_NOT_LIKE_REC_POSPAY_NUMBER = 0 AND
				`record_pos_payment`.`REC_POSPAY_NUMBER` LIKE PARAM_REC_POSPAY_NUMBER			
			) OR
			(
				IS_NOT_LIKE_REC_POSPAY_NUMBER = 1 AND
				`record_pos_payment`.`REC_POSPAY_NUMBER` NOT LIKE PARAM_REC_POSPAY_NUMBER		
			)
		) AND 	
		(
			IS_NULL_REC_POSPAY_PAYEE = 1 OR
			(
				IS_NOT_LIKE_REC_POSPAY_PAYEE = 0 AND
				`record_pos_payment`.`REC_POSPAY_PAYEE` LIKE PARAM_REC_POSPAY_PAYEE			
			) OR
			(
				IS_NOT_LIKE_REC_POSPAY_PAYEE = 1 AND
				`record_pos_payment`.`REC_POSPAY_PAYEE` NOT LIKE PARAM_REC_POSPAY_PAYEE		
			)
		) AND 	
		(
			IS_NULL_REC_POSPAY_CAT_BANK_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_CAT_BANK_ID` = PARAM_REC_POSPAY_CAT_BANK_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_CAT_CARDTYPE_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_CAT_CARDTYPE_ID` = PARAM_REC_POSPAY_CAT_CARDTYPE_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_REC_LOYALTY_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_REC_LOYALTY_ID` = PARAM_REC_POSPAY_REC_LOYALTY_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_REC_VOUCHER_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_REC_VOUCHER_ID` = PARAM_REC_POSPAY_REC_VOUCHER_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_CHECK_DATE_FROM = 1 OR
			`record_pos_payment`.`REC_POSPAY_CHECK_DATE` >= PARAM_REC_POSPAY_CHECK_DATE_FROM			
		) AND
		(
			IS_NULL_REC_POSPAY_CHECK_DATE_TO = 1 OR
			`record_pos_payment`.`REC_POSPAY_CHECK_DATE` <= PARAM_REC_POSPAY_CHECK_DATE_TO			
		) AND 
		(
			IS_NULL_REC_POSPAY_REMARKS = 1 OR
			(
				IS_NOT_LIKE_REC_POSPAY_REMARKS = 0 AND
				`record_pos_payment`.`REC_POSPAY_REMARKS` LIKE PARAM_REC_POSPAY_REMARKS			
			) OR
			(
				IS_NOT_LIKE_REC_POSPAY_REMARKS = 1 AND
				`record_pos_payment`.`REC_POSPAY_REMARKS` NOT LIKE PARAM_REC_POSPAY_REMARKS		
			)
		) AND 	
		(
			IS_NULL_REC_POSPAY_ACC_ACCOUNT_ID = 1 OR
			`record_pos_payment`.`REC_POSPAY_ACC_ACCOUNT_ID` = PARAM_REC_POSPAY_ACC_ACCOUNT_ID			
		) AND
		(
			IS_NULL_REC_POSPAY_DATE_PROCESSED_FROM = 1 OR
			`record_pos_payment`.`REC_POSPAY_DATE_PROCESSED` >= PARAM_REC_POSPAY_DATE_PROCESSED_FROM			
		) AND
		(
			IS_NULL_REC_POSPAY_DATE_PROCESSED_TO = 1 OR
			`record_pos_payment`.`REC_POSPAY_DATE_PROCESSED` <= PARAM_REC_POSPAY_DATE_PROCESSED_TO			
		) AND 
		(
			IS_NULL_REC_POSPAY_IS_INITIAL_PAY = 1 OR
			`record_pos_payment`.`REC_POSPAY_IS_INITIAL_PAY` = TEMPMS_REC_POSPAY_IS_INITIAL_PAY_01 OR	
			`record_pos_payment`.`REC_POSPAY_IS_INITIAL_PAY` = TEMPMS_REC_POSPAY_IS_INITIAL_PAY_02 OR	
			`record_pos_payment`.`REC_POSPAY_IS_INITIAL_PAY` = TEMPMS_REC_POSPAY_IS_INITIAL_PAY_03 OR
			`record_pos_payment`.`REC_POSPAY_IS_INITIAL_PAY` = TEMPMS_REC_POSPAY_IS_INITIAL_PAY_04
		)AND 		
		(
			IS_NULL_REC_POSPAY_STATUS = 1 OR
			`record_pos_payment`.`REC_POSPAY_STATUS` = TEMPMS_REC_POSPAY_STATUS_01 OR	
			`record_pos_payment`.`REC_POSPAY_STATUS` = TEMPMS_REC_POSPAY_STATUS_02 OR	
			`record_pos_payment`.`REC_POSPAY_STATUS` = TEMPMS_REC_POSPAY_STATUS_03 OR
			`record_pos_payment`.`REC_POSPAY_STATUS` = TEMPMS_REC_POSPAY_STATUS_04
		)		

	-- ORDER BY
	-- ####################################################
	ORDER BY
		-- RECORD_POS_PAYMENT DETAILS
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_ID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_ACC_GROUP_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_ACC_GROUP_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_ACC_GROUP_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_ACC_GROUP_ID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REC_POSDEVICE_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_REC_POSDEVICE_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REC_POSDEVICE_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_REC_POSDEVICE_ID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REC_POSSALE_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_REC_POSSALE_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REC_POSSALE_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_REC_POSSALE_ID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REC_CLIENT_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_REC_CLIENT_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REC_CLIENT_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_REC_CLIENT_ID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_CAT_PAYTYPE_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_CAT_PAYTYPE_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_CAT_PAYTYPE_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_CAT_PAYTYPE_ID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_AMOUNT_PAID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_AMOUNT_PAID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_AMOUNT_PAID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_AMOUNT_PAID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_CHANGE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_CHANGE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_CHANGE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_CHANGE` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_NUMBER' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_NUMBER` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_NUMBER' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_NUMBER` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_PAYEE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_PAYEE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_PAYEE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_PAYEE` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_CAT_BANK_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_CAT_BANK_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_CAT_BANK_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_CAT_BANK_ID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_CAT_CARDTYPE_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_CAT_CARDTYPE_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_CAT_CARDTYPE_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_CAT_CARDTYPE_ID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REC_LOYALTY_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_REC_LOYALTY_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REC_LOYALTY_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_REC_LOYALTY_ID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REC_VOUCHER_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_REC_VOUCHER_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REC_VOUCHER_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_REC_VOUCHER_ID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_CHECK_DATE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_CHECK_DATE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_CHECK_DATE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_CHECK_DATE` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REMARKS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_REMARKS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_REMARKS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_REMARKS` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_ACC_ACCOUNT_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_ACC_ACCOUNT_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_ACC_ACCOUNT_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_ACC_ACCOUNT_ID` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_DATE_PROCESSED' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_DATE_PROCESSED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_DATE_PROCESSED' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_DATE_PROCESSED` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_IS_INITIAL_PAY' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_IS_INITIAL_PAY` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_IS_INITIAL_PAY' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_IS_INITIAL_PAY` END DESC,	
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_STATUS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_payment`.`REC_POSPAY_STATUS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSPAY_STATUS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_payment`.`REC_POSPAY_STATUS` END DESC,	

		-- Inner joins
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_GROUP_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_group`.`ACC_GROUP_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_GROUP_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_group`.`ACC_GROUP_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_GROUP_DESCRIPTION' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_group`.`ACC_GROUP_DESCRIPTION` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_GROUP_DESCRIPTION' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_group`.`ACC_GROUP_DESCRIPTION` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_GROUP_STATUS' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_group`.`ACC_GROUP_STATUS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_GROUP_STATUS' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_group`.`ACC_GROUP_STATUS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_GROUP_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_group`.`ACC_GROUP_SYSTEM_GENERATED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_GROUP_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_group`.`ACC_GROUP_SYSTEM_GENERATED` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_ACC_GROUP_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_ACC_GROUP_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_ACC_GROUP_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_ACC_GROUP_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_DESCRIPTION' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_DESCRIPTION` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_DESCRIPTION' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_DESCRIPTION` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_MODEL' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_MODEL` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_MODEL' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_MODEL` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_OS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_OS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_OS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_OS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_MACHINE_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_MACHINE_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_MACHINE_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_MACHINE_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_PERMIT_NUMBER' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_PERMIT_NUMBER` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_PERMIT_NUMBER' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_PERMIT_NUMBER` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_DATE_ACCREDITED' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_DATE_ACCREDITED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_DATE_ACCREDITED' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_DATE_ACCREDITED` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_ADDRESS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_ADDRESS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_ADDRESS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_ADDRESS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_CONTACT' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_CONTACT` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_CONTACT' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_CONTACT` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_TIN' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_TIN` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_TIN' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_TIN` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_RESET_COUNT' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_RESET_COUNT` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_RESET_COUNT' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_RESET_COUNT` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_REGISTRATION_ACC_ACCOUNT_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_REGISTRATION_ACC_ACCOUNT_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_REGISTRATION_ACC_ACCOUNT_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_REGISTRATION_ACC_ACCOUNT_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_DATE_REGISTERED' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_DATE_REGISTERED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_DATE_REGISTERED' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_DATE_REGISTERED` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_PROCESSED_ACC_ACCOUNT_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_PROCESSED_ACC_ACCOUNT_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_PROCESSED_ACC_ACCOUNT_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_PROCESSED_ACC_ACCOUNT_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_DATE_PROCESSED' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_DATE_PROCESSED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_DATE_PROCESSED' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_DATE_PROCESSED` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_STATUS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_posdevice`.`REC_POSDEVICE_STATUS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSDEVICE_STATUS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_posdevice`.`REC_POSDEVICE_STATUS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_ACC_GROUP_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_ACC_GROUP_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_ACC_GROUP_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_ACC_GROUP_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_REC_POSDEVICE_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_REC_POSDEVICE_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_REC_POSDEVICE_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_REC_POSDEVICE_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_REC_CLIENT_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_REC_CLIENT_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_REC_CLIENT_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_REC_CLIENT_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_REC_LOYALTY_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_REC_LOYALTY_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_REC_LOYALTY_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_REC_LOYALTY_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_CODE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_CODE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_CODE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_CODE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_SUBTOTAL' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_SUBTOTAL` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_SUBTOTAL' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_SUBTOTAL` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_DISCOUNT' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_DISCOUNT` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_DISCOUNT' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_DISCOUNT` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_VATABLE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_VATABLE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_VATABLE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_VATABLE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_VATEXEMPT' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_VATEXEMPT` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_VATEXEMPT' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_VATEXEMPT` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_NONVAT' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_NONVAT` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_NONVAT' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_NONVAT` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_ZERORATED' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_ZERORATED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_ZERORATED' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_ZERORATED` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_VATOTHER' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_VATOTHER` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_VATOTHER' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_VATOTHER` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_VAT' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_VAT` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_VAT' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_VAT` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_VAT_LESS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_VAT_LESS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_VAT_LESS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_VAT_LESS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_POINT' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_POINT` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_POINT' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_POINT` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_RTN_SUBTOTAL' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_RTN_SUBTOTAL` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_RTN_SUBTOTAL' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_RTN_SUBTOTAL` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_PAID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_PAID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_TOTAL_PAID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_TOTAL_PAID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_DATE_PAID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_DATE_PAID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_DATE_PAID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_DATE_PAID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_DATE_DUE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_DATE_DUE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_DATE_DUE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_DATE_DUE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_REMARKS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_REMARKS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_REMARKS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_REMARKS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_ACC_ACCOUNT_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_ACC_ACCOUNT_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_ACC_ACCOUNT_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_ACC_ACCOUNT_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_DATE_TRANSACT' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_DATE_TRANSACT` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_DATE_TRANSACT' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_DATE_TRANSACT` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_STATUS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_pos_sale`.`REC_POSSALE_STATUS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_POSSALE_STATUS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_pos_sale`.`REC_POSSALE_STATUS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_CODE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_CODE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_CODE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_CODE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_LASTNAME' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_LASTNAME` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_LASTNAME' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_LASTNAME` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_FIRSTNAME' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_FIRSTNAME` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_FIRSTNAME' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_FIRSTNAME` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_MIDDLENAME' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_MIDDLENAME` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_MIDDLENAME' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_MIDDLENAME` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_CAT_SUFFIX_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_CAT_SUFFIX_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_CAT_SUFFIX_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_CAT_SUFFIX_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_SYS_GENDER_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_SYS_GENDER_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_SYS_GENDER_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_SYS_GENDER_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_CONTACT' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_CONTACT` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_CONTACT' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_CONTACT` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_EMAIL' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_EMAIL` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_EMAIL' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_EMAIL` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_CAT_COMPANY_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_CAT_COMPANY_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_CAT_COMPANY_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_CAT_COMPANY_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_CAT_PRICEMDL_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_CAT_PRICEMDL_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_CAT_PRICEMDL_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_CAT_PRICEMDL_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_SENIOR_CODE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_SENIOR_CODE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_SENIOR_CODE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_SENIOR_CODE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_PWD_CODE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_PWD_CODE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_PWD_CODE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_PWD_CODE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_TIN' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_TIN` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_TIN' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_TIN` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_DATE_ISSUED' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_DATE_ISSUED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_DATE_ISSUED' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_DATE_ISSUED` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_ADDRESS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_ADDRESS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_ADDRESS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_ADDRESS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_REMARKS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_REMARKS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_REMARKS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_REMARKS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_DATE_REGISTER' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_DATE_REGISTER` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_DATE_REGISTER' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_DATE_REGISTER` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_STATUS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_STATUS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_STATUS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_STATUS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_client`.`REC_CLIENT_SYSTEM_GENERATED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_CLIENT_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_client`.`REC_CLIENT_SYSTEM_GENERATED` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_PAYTYPE_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_payment_type`.`CAT_PAYTYPE_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_PAYTYPE_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_payment_type`.`CAT_PAYTYPE_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_PAYTYPE_CODE' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_payment_type`.`CAT_PAYTYPE_CODE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_PAYTYPE_CODE' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_payment_type`.`CAT_PAYTYPE_CODE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_PAYTYPE_DESCRIPTION' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_payment_type`.`CAT_PAYTYPE_DESCRIPTION` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_PAYTYPE_DESCRIPTION' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_payment_type`.`CAT_PAYTYPE_DESCRIPTION` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_PAYTYPE_STATUS' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_payment_type`.`CAT_PAYTYPE_STATUS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_PAYTYPE_STATUS' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_payment_type`.`CAT_PAYTYPE_STATUS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_bank`.`CAT_BANK_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_bank`.`CAT_BANK_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_CODE' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_bank`.`CAT_BANK_CODE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_CODE' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_bank`.`CAT_BANK_CODE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_DESCRIPTION' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_bank`.`CAT_BANK_DESCRIPTION` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_DESCRIPTION' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_bank`.`CAT_BANK_DESCRIPTION` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_REMARKS' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_bank`.`CAT_BANK_REMARKS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_REMARKS' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_bank`.`CAT_BANK_REMARKS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_STATUS' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_bank`.`CAT_BANK_STATUS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_STATUS' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_bank`.`CAT_BANK_STATUS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_bank`.`CAT_BANK_SYSTEM_GENERATED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_BANK_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_bank`.`CAT_BANK_SYSTEM_GENERATED` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_CARDTYPE_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_card_type`.`CAT_CARDTYPE_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_CARDTYPE_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_card_type`.`CAT_CARDTYPE_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_CARDTYPE_DESCRIPTION' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_card_type`.`CAT_CARDTYPE_DESCRIPTION` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_CARDTYPE_DESCRIPTION' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_card_type`.`CAT_CARDTYPE_DESCRIPTION` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_CARDTYPE_STATUS' AND PARAM_ORDER_TYPE = 'ASC' THEN `category_card_type`.`CAT_CARDTYPE_STATUS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'CAT_CARDTYPE_STATUS' AND PARAM_ORDER_TYPE = 'DESC' THEN `category_card_type`.`CAT_CARDTYPE_STATUS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_loyalty`.`REC_LOYALTY_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_loyalty`.`REC_LOYALTY_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_REC_CLIENT_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_loyalty`.`REC_LOYALTY_REC_CLIENT_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_REC_CLIENT_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_loyalty`.`REC_LOYALTY_REC_CLIENT_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_REC_LOYALTY_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_loyalty`.`REC_LOYALTY_REC_LOYALTY_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_REC_LOYALTY_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_loyalty`.`REC_LOYALTY_REC_LOYALTY_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_CODE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_loyalty`.`REC_LOYALTY_CODE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_CODE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_loyalty`.`REC_LOYALTY_CODE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_BALANCE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_loyalty`.`REC_LOYALTY_BALANCE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_BALANCE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_loyalty`.`REC_LOYALTY_BALANCE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_DATE_EFFECTIVE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_loyalty`.`REC_LOYALTY_DATE_EFFECTIVE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_DATE_EFFECTIVE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_loyalty`.`REC_LOYALTY_DATE_EFFECTIVE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_DATE_EXPIRY' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_loyalty`.`REC_LOYALTY_DATE_EXPIRY` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_DATE_EXPIRY' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_loyalty`.`REC_LOYALTY_DATE_EXPIRY` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_STATUS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_loyalty`.`REC_LOYALTY_STATUS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_STATUS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_loyalty`.`REC_LOYALTY_STATUS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_loyalty`.`REC_LOYALTY_SYSTEM_GENERATED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_LOYALTY_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_loyalty`.`REC_LOYALTY_SYSTEM_GENERATED` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_voucher`.`REC_VOUCHER_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_voucher`.`REC_VOUCHER_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_CODE' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_voucher`.`REC_VOUCHER_CODE` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_CODE' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_voucher`.`REC_VOUCHER_CODE` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_IS_VOUCHER' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_voucher`.`REC_VOUCHER_IS_VOUCHER` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_IS_VOUCHER' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_voucher`.`REC_VOUCHER_IS_VOUCHER` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_CAT_DISCOUNT_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_voucher`.`REC_VOUCHER_CAT_DISCOUNT_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_CAT_DISCOUNT_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_voucher`.`REC_VOUCHER_CAT_DISCOUNT_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_AMOUNT' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_voucher`.`REC_VOUCHER_AMOUNT` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_AMOUNT' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_voucher`.`REC_VOUCHER_AMOUNT` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_DATE_BEGIN' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_voucher`.`REC_VOUCHER_DATE_BEGIN` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_DATE_BEGIN' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_voucher`.`REC_VOUCHER_DATE_BEGIN` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_DATE_END' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_voucher`.`REC_VOUCHER_DATE_END` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_DATE_END' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_voucher`.`REC_VOUCHER_DATE_END` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_IS_UNLIMITED' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_voucher`.`REC_VOUCHER_IS_UNLIMITED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_IS_UNLIMITED' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_voucher`.`REC_VOUCHER_IS_UNLIMITED` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_STATUS' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_voucher`.`REC_VOUCHER_STATUS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_STATUS' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_voucher`.`REC_VOUCHER_STATUS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'ASC' THEN `record_voucher`.`REC_VOUCHER_SYSTEM_GENERATED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'REC_VOUCHER_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'DESC' THEN `record_voucher`.`REC_VOUCHER_SYSTEM_GENERATED` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_SERVER_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_SERVER_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_SERVER_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_SERVER_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_ACC_LEVEL_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_ACC_LEVEL_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_ACC_LEVEL_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_ACC_LEVEL_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_ACC_GROUP_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_ACC_GROUP_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_ACC_GROUP_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_ACC_GROUP_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_ADD_RESOURCE_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_ADD_RESOURCE_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_ADD_RESOURCE_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_ADD_RESOURCE_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_FIRST_NAME' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_FIRST_NAME` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_FIRST_NAME' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_FIRST_NAME` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_MIDDLE_NAME' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_MIDDLE_NAME` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_MIDDLE_NAME' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_MIDDLE_NAME` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_LAST_NAME' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_LAST_NAME` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_LAST_NAME' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_LAST_NAME` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_POSITION' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_POSITION` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_POSITION' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_POSITION` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_SYS_GENDER_ID' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_SYS_GENDER_ID` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_SYS_GENDER_ID' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_SYS_GENDER_ID` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_STATUS' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_STATUS` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_STATUS' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_STATUS` END DESC,
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'ASC' THEN `account_member`.`ACC_ACCOUNT_SYSTEM_GENERATED` END ASC, 
		CASE WHEN PARAM_ORDER_FIELD = 'ACC_ACCOUNT_SYSTEM_GENERATED' AND PARAM_ORDER_TYPE = 'DESC' THEN `account_member`.`ACC_ACCOUNT_SYSTEM_GENERATED` END DESC,
		
		-- EXTRA
		CASE WHEN PARAM_ORDER_FIELD <> 'REC_POSPAY_DATE_PROCESSED' THEN `record_pos_payment`.`REC_POSPAY_DATE_PROCESSED` END ASC
		
	-- LIMIT & OFFSET
	-- ####################################################		
	LIMIT PARAM_SKIP, PARAM_LIMIT;
	
	
	
	-- EXIT PROCEDURE
	-- =========================================================================================
	LEAVE SP_GET;
			
END;;