DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D003SUM_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D003SUM_1`(P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(2000), P26 tinyint(1), P27 tinyint(1), P28 tinyint(1))
    DETERMINISTIC
BEGIN
SET @ORDERBY = "";
SET @SORT = IF(P24 = "A-Z", " ASC ", " DESC ");
CASE P23
WHEN "Item Id" THEN SET @ORDERBY = " T1.P_ITE ";
WHEN "Item Code" THEN SET @ORDERBY = " T1.P_ITE ";
WHEN "Item Name" THEN SET @ORDERBY = " T1.P_ITE_NAME ";
WHEN "Quantity" THEN SET @ORDERBY = " T1.P_QTY ";
WHEN "Sub Total" THEN SET @ORDERBY = " T1.P_SUB ";
WHEN "Disc." THEN SET @ORDERBY = " T1.P_DISC ";
WHEN "Tax" THEN SET @ORDERBY = " T1.P_TAX ";
WHEN "Grand Total" THEN SET @ORDERBY = " T1.P_NET ";
ELSE SET @ORDERBY = " T1.P_ITE ";
END CASE;
SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_ITE ", @SORT);
SET @SQL_EXC = CONCAT("SELECT * FROM (SELECT IF(COALESCE(T2.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE,  IF(COALESCE(T2.var_itemid, '') = '', T2.var_accid, T2.var_itemid) AS P_ITE, IF(COALESCE(T2.var_itemid, '') = '', T4.var_name, T3.var_name) AS P_ITE_NAME, T2.var_uomid AS P_UOM, SUM(T2.dou_qty) AS P_QTY, SUM(T2.dou_qty * T2.dou_price) AS P_SUB, SUM(T2.dou_disdol) AS P_DISC, SUM(T2.dou_taxdol) AS P_TAX, SUM((T2.dou_qty * T2.dou_price) - T2.dou_disdol + T2.dou_taxdol) AS P_NET
FROM ame_ap_in T1
LEFT JOIN (ame_ap_in_det T2 LEFT JOIN ame_mt_ite T3 ON T2.var_itemid = T3.var_id LEFT JOIN ame_mt_acc T4 ON T2.var_accid = T4.var_id LEFT JOIN ame_mt_cla T5 ON T2.var_claid = T5.var_id) ON T1.var_id = T2.var_id
LEFT JOIN ame_mt_ven T6 ON T1.var_venid = T6.var_id
WHERE
(CASE
WHEN '", P1, "' = 'ALL' THEN TRUE
WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
AND
IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
AND
IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
AND
IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
AND
IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
AND
IF('", P9, "' = 'ALL', TRUE, T6.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T6.var_groupid <= '", P10, "')
AND
IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
AND
IF('", P13, "' = 'ALL', TRUE, T2.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T2.var_claid <= '", P14, "')
AND
IF('", P15, "' = 'ALL', TRUE, T2.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T2.var_locid <= '", P16, "')
AND
IF('", P17, "' = 'ALL', TRUE, T3.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T3.var_groupid <= '", P18, "')
AND
IF('", P19, "' = 'ALL', TRUE, T2.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T2.var_itemid <= '", P20, "')
AND
IF('", P21, "' = 'ALL', TRUE, T2.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T2.var_accid <= '", P22, "')
GROUP BY T2.var_itemid, T2.var_accid, T2.var_uomid) AS T1", @ORDERBY);
PREPARE STM FROM @SQL_EXC;
EXECUTE STM;
DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D003_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D003_M1`(D1 varchar(100),D2 varchar(100),D3 varchar(100),D4 varchar(100),D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(100), P26 varchar(100), P27 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_user_role T1 LEFT JOIN ame_user_per_oth T2 ON T1.int_id = T2.int_id WHERE T1.var_id = D1 AND T2.var_modid = 'HCI' AND COALESCE(T2.tin_per1, 0) = 1;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Location Id" THEN SET @ORDERBY = " T1.P_LOC ";
		WHEN "Location Name" THEN SET @ORDERBY = " T1.P_LOC_NAME ";
		WHEN "Sub Total" THEN SET @ORDERBY = " T1.P_SUB ";
		WHEN "Discount" THEN SET @ORDERBY = " T1.P_DISC ";
		WHEN "VAT" THEN SET @ORDERBY = " T1.P_TAX1 ";
		WHEN "WHT" THEN SET @ORDERBY = " T1.P_TAX2 ";
		WHEN "Grand Total" THEN SET @ORDERBY = " T1.P_NET ";
		ELSE SET @ORDERBY = " T1.P_LOC ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.P_SORTKEY, T1.P_MODULE, T1.P_TYPE, T1.P_ID, T1.P_REF, T1.P_DATE, T1.P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_DESC_1, T1.P_STATUS, T1.P_STATUS_PMT,
				T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_LOC, T1.P_LOC_NAME, T1.P_SUB_1, T1.P_DISC_1, T1.P_TAX1_1, T1.P_TAX2_1, T1.P_NET_1, T1.P_TERM,
				COALESCE(T1.PMT_DISC, 0) AS PMT_DISC, COALESCE(T1.PMT_AMT, 0) AS PMT_AMT, COALESCE(T1.PMT_APP, 0) AS PMT_APP, T1.P_USER, T1.P_CUR, T1.P_RATE
				, T1.P_LINE, T1.P_ITE, T1.P_ITE_NAME, T1.P_DESC, T1.P_CLA, T1.P_CLA_NAME, T1.P_UOM, SUM(T1.P_QTY) AS P_QTY, SUM(T1.P_PRICE) AS P_PRICE, SUM(T1.P_SUB) AS P_SUB, SUM(T1.P_DISC) AS P_DISC, SUM(T1.P_TAX1) AS P_TAX1, SUM(T1.P_TAX2) AS P_TAX2, SUM(T1.P_NET) AS P_NET FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE, '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(DATE(NOW())) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_DESC_1, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_LOC, '' AS P_LOC_NAME, 0.0 AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, 0.0 AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE
				, 0 AS P_LINE, '' AS 'P_ITE', '' AS P_ITE_NAME, '' AS P_DESC, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_PRICE, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, 0.0 AS P_NET LIMIT 0)", 
				# Purchase Order
				IF(P23 = "0", "", CONCAT("
				UNION ALL (SELECT 1 AS P_SORTKEY, 'AP-PO' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol - T5.dou_funtaxdol1) AS P_NET
				FROM ame_ap_po T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_po_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Purchase Receive / Inventory
				IF(P24 = "0", "", CONCAT("
				UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol - T5.dou_funtaxdol1) AS P_NET
				FROM ame_ap_in T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Purchase Return
				IF(P25 = "0", "", CONCAT("
				UNION ALL (SELECT 3 AS P_SORTKEY, 'AP-RET' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_funsub) AS P_SUB, -T5.dou_fundisdol AS P_DISC, -T5.dou_funtaxdol AS P_TAX1, -T5.dou_funtaxdol1 AS P_TAX2, -(T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol - T5.dou_funtaxdol1) AS P_NET
				FROM ame_ap_ret T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Debit Note
				IF(P26 = "0", "", CONCAT("
				UNION ALL (SELECT 4 AS P_SORTKEY, 'AP-DN' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_funsub) AS P_SUB, -T5.dou_fundisdol AS P_DISC, -T5.dou_funtaxdol AS P_TAX1, -T5.dou_funtaxdol1 AS P_TAX2, -(T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol - T5.dou_funtaxdol1) AS P_NET
				FROM ame_ap_dn T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Prepayment
				IF(P27 = "0", "", CONCAT("
				UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE, 'Account' AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, '' AS P_LOC, '' AS P_LOC_NAME, T1.dou_amt AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, T1.dou_net AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, T5.var_docid AS 'P_ITE', T7.var_name AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, -T5.dou_funamt AS P_PRICE, -T5.dou_funamt AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, -T5.dou_funamt AS P_NET
				FROM ame_ap_py T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, -SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status <> 'Deleted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5 LEFT JOIN ame_mt_acc T7 ON T5.var_docid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				") AS T1 
				GROUP BY COALESCE(T1.P_LOC, '')", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT T1.P_SORTKEY, T1.P_MODULE, T1.P_TYPE, T1.P_ID, T1.P_REF, T1.P_DATE, T1.P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_DESC_1, T1.P_STATUS, T1.P_STATUS_PMT,
				T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_LOC, T1.P_LOC_NAME, T1.P_SUB_1, T1.P_DISC_1, T1.P_TAX1_1, T1.P_TAX2_1, T1.P_NET_1, T1.P_TERM,
				COALESCE(T1.PMT_DISC, 0) AS PMT_DISC, COALESCE(T1.PMT_AMT, 0) AS PMT_AMT, COALESCE(T1.PMT_APP, 0) AS PMT_APP, T1.P_USER, T1.P_CUR, T1.P_RATE
				, T1.P_LINE, T1.P_ITE, T1.P_ITE_NAME, T1.P_DESC, T1.P_CLA, T1.P_CLA_NAME, T1.P_UOM, SUM(T1.P_QTY) AS P_QTY, SUM(T1.P_PRICE) AS P_PRICE, SUM(T1.P_SUB) AS P_SUB, SUM(T1.P_DISC) AS P_DISC, SUM(T1.P_TAX1) AS P_TAX1, SUM(T1.P_TAX2) AS P_TAX2, SUM(T1.P_NET) AS P_NET FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE, '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(DATE(NOW())) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_DESC_1, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_LOC, '' AS P_LOC_NAME, 0.0 AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, 0.0 AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE
				, 0 AS P_LINE, '' AS 'P_ITE', '' AS P_ITE_NAME, '' AS P_DESC, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_PRICE, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, 0.0 AS P_NET LIMIT 0)", 
				# Purchase Order
				IF(P23 = "0", "", CONCAT("
				UNION ALL (SELECT 1, 'AP-PO' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_sub) AS P_SUB, T5.dou_disdol AS P_DISC, T5.dou_taxdol AS P_TAX1, T5.dou_taxdol1 AS P_TAX2, T5.dou_sub - T5.dou_disdol + T5.dou_taxdol - T5.dou_taxdol1 AS P_NET
				FROM ame_ap_po T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_po_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Purchase Receive / Inventory
				IF(P24 = "0", "", CONCAT("
				UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_sub) AS P_SUB, T5.dou_disdol AS P_DISC, T5.dou_taxdol AS P_TAX1, T5.dou_taxdol1 AS P_TAX2, T5.dou_sub - T5.dou_disdol + T5.dou_taxdol - T5.dou_taxdol1 AS P_NET
				FROM ame_ap_in T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Purchase Return
				IF(P25 = "0", "", CONCAT("
				UNION ALL (SELECT 3 AS P_SORTKEY, 'AP-RET' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_sub) AS P_SUB, -T5.dou_disdol AS P_DISC, -T5.dou_taxdol AS P_TAX1, -T5.dou_taxdol1 AS P_TAX2, -(T5.dou_sub - T5.dou_disdol + T5.dou_taxdol - T5.dou_taxdol1) AS P_NET
				FROM ame_ap_ret T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Debit Note
				IF(P26 = "0", "", CONCAT("
				UNION ALL (SELECT 4 AS P_SORTKEY, 'AP-DN' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_sub) AS P_SUB, -T5.dou_disdol AS P_DISC, -T5.dou_taxdol AS P_TAX1, -T5.dou_taxdol1 AS P_TAX2, -(T5.dou_sub - T5.dou_disdol + T5.dou_taxdol - T5.dou_taxdol1) AS P_NET
				FROM ame_ap_dn T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Prepayment
				IF(P27 = "0", "", CONCAT("
				UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE, 'Account' AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, '' AS P_LOC, '' AS P_LOC_NAME, T1.dou_amt AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, T1.dou_net AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, T5.var_docid AS 'P_ITE', T7.var_name AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, -T5.dou_amt AS P_PRICE, -T5.dou_amt AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, -T5.dou_amt AS P_NET
				FROM ame_ap_py T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5 LEFT JOIN ame_mt_acc T7 ON T5.var_docid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				") AS T1
				GROUP BY COALESCE(T1.P_LOC, '')", @ORDERBY);
		END IF;	
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D004DET_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D004DET_1`(P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(2000), P26 tinyint(1), P27 tinyint(1), P28 tinyint(1))
    DETERMINISTIC
BEGIN
DECLARE P_DEC_AMT INTEGER DEFAULT 0;
# DECIMAL
BEGIN DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS int_amt FROM ame_sys_decimal T WHERE T.var_id = 'AP';OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF;CLOSE C1;END;
SET @ORDERBY = "";
SET @SORT = IF(P24 = "A-Z", " ASC ", " DESC ");
CASE P23
WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
WHEN "Reference" THEN SET @ORDERBY = " T1.P_VEN ";
WHEN "Transaction Type" THEN SET @ORDERBY = " T1.P_TYPE_TRANS ";
WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
WHEN "Location" THEN SET @ORDERBY = " T1.P_LOC ";
WHEN "Class Name" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
WHEN "Term" THEN SET @ORDERBY = " T1.P_TERM ";
WHEN "Due Date" THEN SET @ORDERBY = " T1.P_DATE_DUE ";
WHEN "Status" THEN SET @ORDERBY = " T1.P_STATUS ";
WHEN "Payment Status" THEN SET @ORDERBY = " T1.P_STATUS_PMT ";
WHEN "User" THEN SET @ORDERBY = " T1.P_USER ";
WHEN "Original Amt" THEN SET @ORDERBY = " T1.P_SUB ";
ELSE SET @ORDERBY = " T1.P_ID ";
END CASE;
SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_ID ", @SORT);
SET @SQL_EXC = CONCAT("SELECT * FROM (SELECT 1, 'AP-IN' AS P_TYPE,  T1.var_type AS P_TYPE_TRANS, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_TAX, T1.dou_net AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_modify AS P_USER
FROM ame_ap_in T1
LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc) AS PMT_DISC, SUM(T1.dou_amt) AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_det T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_type = 'Apply to Purchase Invoice' AND T2.var_status = 'Posted' AND IF('", P1, "' = 'All', TRUE, DATE(T2.dat_date) <= DATE('", P3, "')) GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
WHERE
(CASE
WHEN '", P1, "' = 'ALL' THEN TRUE
WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
AND
IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
AND
IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
AND
IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
AND
IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
AND
IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
AND
IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
AND
IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
AND
IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
AND
IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
AND
IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
AND
IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
AND T1.var_status = 'Posted' AND ROUND(T1.dou_net, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id) AS T1", @ORDERBY);
PREPARE STM FROM @SQL_EXC;
EXECUTE STM;
DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D004SUM_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D004SUM_1`(P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(2000), P26 tinyint(1), P27 tinyint(1), P28 tinyint(1))
    DETERMINISTIC
BEGIN
DECLARE P_DEC_AMT INTEGER DEFAULT 0;
# DECIMAL
BEGIN
DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS int_amt FROM ame_sys_decimal T WHERE T.var_id = 'AP';
OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF;CLOSE C1;
END;
SET @ORDERBY = "";
SET @SORT = IF(P24 = "A-Z", " ASC ", " DESC ");
CASE P23
WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
WHEN "1 - 30 Days" THEN SET @ORDERBY = " T1.1_30 ";
WHEN "31 - 60 Days" THEN SET @ORDERBY = " T1.31_60 ";
WHEN "61 - 90 Days" THEN SET @ORDERBY = " T1.61_90 ";
WHEN ">90" THEN SET @ORDERBY = " T1.OVER_90 ";
WHEN "Original Amt" THEN SET @ORDERBY = " T1.P_NET ";
ELSE SET @ORDERBY = " T1.P_VEN ";
END CASE;
SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_VEN ", @SORT);
SET @SQL_EXC = CONCAT("SELECT * FROM (SELECT T1.P_VEN, T1.P_VEN_NAME, SUM(T1.P_NET) AS P_NET, SUM(T1.1_30) AS 1_30, SUM(T1.31_60) AS 31_60, SUM(T1.61_90) AS 61_90, SUM(T1.OVER_90) AS OVER_90 FROM (SELECT T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_net, 0) AS P_NET,
IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 30, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 30, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 60, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 90, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90
FROM ame_ap_in T1 LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc) AS PMT_DISC, SUM(T1.dou_amt) AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_det T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_type = 'Apply to Purchase Invoice' AND T2.var_status = 'Posted' AND IF('", P1, "' = 'All', TRUE, DATE(T2.dat_date) <= DATE('", P3, "')) GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
WHERE
(CASE
WHEN '", P1, "' = 'ALL' THEN TRUE
WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
AND
IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
AND
IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
AND
IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
AND
IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
AND
IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
AND
IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
AND
IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
AND
IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
AND
IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
AND
IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
AND
IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
AND T1.var_status = 'Posted' AND ROUND(T1.dou_net, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id) AS T1 GROUP BY T1.P_VEN) AS T1", @ORDERBY);
PREPARE STM FROM @SQL_EXC;
EXECUTE STM;
DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D004_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D004_M1`(D1 varchar(100),D2 varchar(100),D3 varchar(100),D4 varchar(100),D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(100), P26 varchar(100), P27 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_user_role T1 LEFT JOIN ame_user_per_oth T2 ON T1.int_id = T2.int_id WHERE T1.var_id = D1 AND T2.var_modid = 'HCI' AND COALESCE(T2.tin_per1, 0) = 1;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Item/Acc. Id" THEN SET @ORDERBY = " T1.P_ITE ";
		WHEN "Item/Acc. Name" THEN SET @ORDERBY = " T1.P_ITE_NAME ";
		WHEN "Description" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Location" THEN SET @ORDERBY = " T1.P_LOC ";
		WHEN "Class" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
		WHEN "Sub Total" THEN SET @ORDERBY = " T1.P_SUB ";
		WHEN "Discount" THEN SET @ORDERBY = " T1.P_DISC ";
		WHEN "VAT" THEN SET @ORDERBY = " T1.P_TAX1 ";
		WHEN "WHT" THEN SET @ORDERBY = " T1.P_TAX2 ";
		WHEN "Grand Total" THEN SET @ORDERBY = " T1.P_NET ";
		ELSE SET @ORDERBY = " T1.P_LINE ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY T1.P_SORTKEY, ", @ORDERBY, @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.P_SORTKEY, T1.P_MODULE, T1.P_TYPE, T1.P_ID, T1.P_REF, T1.P_DATE, T1.P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_DESC_1, T1.P_STATUS, T1.P_STATUS_PMT,
				T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_LOC, T1.P_SUB_1, T1.P_DISC_1, T1.P_TAX1_1, T1.P_TAX2_1, T1.P_NET_1, T1.P_TERM,
				COALESCE(T1.PMT_DISC, 0) AS PMT_DISC, COALESCE(T1.PMT_AMT, 0) AS PMT_AMT, COALESCE(T1.PMT_APP, 0) AS PMT_APP, T1.P_USER, T1.P_CUR, T1.P_RATE
				, T1.P_LINE, T1.P_ITE, T1.P_ITE_NAME, T1.P_DESC, T1.P_CLA, T1.P_CLA_NAME, T1.P_UOM, SUM(T1.P_QTY) AS P_QTY, SUM(T1.P_FOC) AS P_FOC, SUM(T1.P_PRICE) AS P_PRICE, SUM(T1.P_SUB) AS P_SUB, SUM(T1.P_DISC) AS P_DISC, SUM(T1.P_TAX1) AS P_TAX1, SUM(T1.P_TAX2) AS P_TAX2, SUM(T1.P_NET) AS P_NET FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE, '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(DATE(NOW())) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_DESC_1, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_LOC, 0.0 AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, 0.0 AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE
				, 0 AS P_LINE, '' AS 'P_ITE', '' AS P_ITE_NAME, '' AS P_DESC, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_FOC, 0.0 AS P_PRICE, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, 0.0 AS P_NET LIMIT 0)", 
				# Purchase Order
				IF(P23 = "0", "", CONCAT("
				UNION ALL (SELECT 1 AS P_SORTKEY, 'AP-PO' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol - T5.dou_funtaxdol1) AS P_NET
				FROM ame_ap_po T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_po_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Purchase Receive / Inventory
				IF(P24 = "0", "", CONCAT("
				UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol - T5.dou_funtaxdol1) AS P_NET
				FROM ame_ap_in T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Purchase Return
				IF(P25 = "0", "", CONCAT("
				UNION ALL (SELECT 3 AS P_SORTKEY, 'AP-RET' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_foc AS P_FOC, -T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_funsub) AS P_SUB, -T5.dou_fundisdol AS P_DISC, -T5.dou_funtaxdol AS P_TAX1, -T5.dou_funtaxdol1 AS P_TAX2, -(T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol - T5.dou_funtaxdol1) AS P_NET
				FROM ame_ap_ret T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Debit Note
				IF(P26 = "0", "", CONCAT("
				UNION ALL (SELECT 4 AS P_SORTKEY, 'AP-DN' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_foc AS P_FOC, -T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_funsub) AS P_SUB, -T5.dou_fundisdol AS P_DISC, -T5.dou_funtaxdol AS P_TAX1, -T5.dou_funtaxdol1 AS P_TAX2, -(T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol - T5.dou_funtaxdol1) AS P_NET
				FROM ame_ap_dn T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Prepayment
				IF(P27 = "0", "", CONCAT("
				UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE, 'Account' AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, '' AS P_LOC, T1.dou_amt AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, T1.dou_net AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, T5.var_docid AS 'P_ITE', T7.var_name AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_FOC, -T5.dou_funamt AS P_PRICE, -T5.dou_funamt AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, -T5.dou_funamt AS P_NET
				FROM ame_ap_py T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, -SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status <> 'Deleted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5 LEFT JOIN ame_mt_acc T7 ON T5.var_docid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				") AS T1 
				GROUP BY T1.P_ITE", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT T1.P_SORTKEY, T1.P_MODULE, T1.P_TYPE, T1.P_ID, T1.P_REF, T1.P_DATE, T1.P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_DESC_1, T1.P_STATUS, T1.P_STATUS_PMT,
				T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_LOC, T1.P_SUB_1, T1.P_DISC_1, T1.P_TAX1_1, T1.P_TAX2_1, T1.P_NET_1, T1.P_TERM,
				COALESCE(T1.PMT_DISC, 0) AS PMT_DISC, COALESCE(T1.PMT_AMT, 0) AS PMT_AMT, COALESCE(T1.PMT_APP, 0) AS PMT_APP, T1.P_USER, T1.P_CUR, T1.P_RATE
				, T1.P_LINE, T1.P_ITE, T1.P_ITE_NAME, T1.P_DESC, T1.P_CLA, T1.P_CLA_NAME, T1.P_UOM, SUM(T1.P_QTY) AS P_QTY, SUM(T1.P_FOC) AS P_FOC, SUM(T1.P_PRICE) AS P_PRICE, SUM(T1.P_SUB) AS P_SUB, SUM(T1.P_DISC) AS P_DISC, SUM(T1.P_TAX1) AS P_TAX1, SUM(T1.P_TAX2) AS P_TAX2, SUM(T1.P_NET) AS P_NET FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE, '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(DATE(NOW())) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_DESC_1, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_LOC, 0.0 AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, 0.0 AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE
				, 0 AS P_LINE, '' AS 'P_ITE', '' AS P_ITE_NAME, '' AS P_DESC, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_FOC, 0.0 AS P_PRICE, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, 0.0 AS P_NET LIMIT 0)", 
				# Purchase Order
				IF(P23 = "0", "", CONCAT("
				UNION ALL (SELECT 1, 'AP-PO' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_sub) AS P_SUB, T5.dou_disdol AS P_DISC, T5.dou_taxdol AS P_TAX1, T5.dou_taxdol1 AS P_TAX2, T5.dou_sub - T5.dou_disdol + T5.dou_taxdol - T5.dou_taxdol1 AS P_NET
				FROM ame_ap_po T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_po_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Purchase Receive / Inventory
				IF(P24 = "0", "", CONCAT("
				UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_sub) AS P_SUB, T5.dou_disdol AS P_DISC, T5.dou_taxdol AS P_TAX1, T5.dou_taxdol1 AS P_TAX2, T5.dou_sub - T5.dou_disdol + T5.dou_taxdol - T5.dou_taxdol1 AS P_NET
				FROM ame_ap_in T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Purchase Return
				IF(P25 = "0", "", CONCAT("
				UNION ALL (SELECT 3 AS P_SORTKEY, 'AP-RET' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_foc AS P_FOC, -T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_sub) AS P_SUB, -T5.dou_disdol AS P_DISC, -T5.dou_taxdol AS P_TAX1, -T5.dou_taxdol1 AS P_TAX2, -(T5.dou_sub - T5.dou_disdol + T5.dou_taxdol - T5.dou_taxdol1) AS P_NET
				FROM ame_ap_ret T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Debit Note
				IF(P26 = "0", "", CONCAT("
				UNION ALL (SELECT 4 AS P_SORTKEY, 'AP-DN' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_foc AS P_FOC, -T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_sub) AS P_SUB, -T5.dou_disdol AS P_DISC, -T5.dou_taxdol AS P_TAX1, -T5.dou_taxdol1 AS P_TAX2, -(T5.dou_sub - T5.dou_disdol + T5.dou_taxdol - T5.dou_taxdol1) AS P_NET
				FROM ame_ap_dn T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Prepayment
				IF(P27 = "0", "", CONCAT("
				UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE, 'Account' AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, '' AS P_LOC, T1.dou_amt AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, T1.dou_net AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, T5.var_docid AS 'P_ITE', T7.var_name AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_FOC, -T5.dou_amt AS P_PRICE, -T5.dou_amt AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, -T5.dou_amt AS P_NET
				FROM ame_ap_py T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5 LEFT JOIN ame_mt_acc T7 ON T5.var_docid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				") AS T1
				GROUP BY T1.P_ITE", @ORDERBY);
		END IF;	
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D005DET_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D005DET_1`(P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(2000), P26 tinyint(1), P27 tinyint(1), P28 tinyint(1))
    DETERMINISTIC
BEGIN
DECLARE P_DEC_AMT INTEGER DEFAULT 0;
# DECIMAL
BEGIN DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS int_amt FROM ame_sys_decimal T WHERE T.var_id = 'AP';OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF;CLOSE C1;END;
SET @ORDERBY = "";
SET @SORT = IF(P24 = "A-Z", " ASC ", " DESC ");
CASE P23
WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
WHEN "Reference" THEN SET @ORDERBY = " T1.P_VEN ";
WHEN "Transaction Type" THEN SET @ORDERBY = " T1.P_TYPE_TRANS ";
WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
WHEN "Class Name" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
WHEN "Term" THEN SET @ORDERBY = " T1.P_TERM ";
WHEN "Due Date" THEN SET @ORDERBY = " T1.P_DATE_DUE ";
WHEN "Debit" THEN SET @ORDERBY = " T1.P_DR ";
WHEN "Credit" THEN SET @ORDERBY = " T1.P_CR ";
ELSE SET @ORDERBY = " T1.P_SEQ ";
END CASE;
SET @OPEN_BAL = CONCAT("(SELECT 0 AS P_SEQ, '' AS P_TYPE, '' AS P_TYPE_TRANS, 'Opening Balance' AS P_ID, '' AS P_REF, DATE('", P2, "') AS P_DATE, NULL AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, '' AS P_DESC, '' AS P_PMT_TYPE, '' AS P_PMT_REF,
'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_TERM,
0.0 AS P_DR, 0.0 AS P_CR, SUM(COALESCE(T1.dou_cr, 0) - COALESCE(T1.dou_dr, 0)) AS P_BAL
FROM ame_sl_ap AS T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
WHERE
(CASE
WHEN '", P1, "' = 'ALL' THEN FALSE
ELSE DATE(T1.dat_date) < DATE('", P2, "') END)
AND
IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
AND
IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
AND
COALESCE(T1.dou_cr, 0) <> 0 AND COALESCE(T1.dou_dr, 0) <> 0
GROUP BY T1.var_venid) UNION ALL ");
IF P26 = 1 THEN SET @OPEN_BAL = ""; END IF;
SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_SEQ ", @SORT);
SET @SQL_EXC = CONCAT("SELECT * FROM (", @OPEN_BAL, "(SELECT T1.int_line AS P_SEQ, T1.var_modid AS P_TYPE, T1.var_doctype AS P_TYPE_TRANS, T1.var_docid AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_pmttype AS P_PMT_TYPE, T1.var_pmtref AS P_PMT_REF,
T1.var_claid AS P_CLA, COALESCE(T3.var_name, '') AS P_CLA_NAME, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
COALESCE(T1.dou_dr, 0) AS P_DR, COALESCE(T1.dou_cr, 0) AS P_CR, 0.0 AS P_BAL
FROM ame_sl_ap_his T1
LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id
LEFT JOIN ame_mt_acc T4 ON T1.var_apaccid = T4.var_id
WHERE
(CASE
WHEN '", P1, "' = 'ALL' THEN TRUE
WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH(NOW())
WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR(NOW())
ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
AND
IF('", P4, "' = 'ALL', TRUE, T1.var_doctype = '", P4, "')
AND
IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
AND
IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
AND
IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
AND
IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
AND
IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
AND
IF('", P21, "' = 'ALL', TRUE, T1.var_apaccid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T1.var_apaccid <= '", P22, "')
ORDER BY T1.int_line)) AS T1", @ORDERBY);
PREPARE STM FROM @SQL_EXC;
EXECUTE STM;
DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D005SUM_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D005SUM_1`(P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(2000), P26 tinyint(1), P27 tinyint(1), P28 tinyint(1))
    DETERMINISTIC
BEGIN
DECLARE P_DEC_AMT INTEGER DEFAULT 0;
# DECIMAL
BEGIN DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS int_amt FROM ame_sys_decimal T WHERE T.var_id = 'AP';OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF;CLOSE C1;END;
SET @ORDERBY = "";
SET @SORT = IF(P24 = "A-Z", " ASC ", " DESC ");
CASE P23
WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
WHEN "Debit" THEN SET @ORDERBY = " T1.P_DR ";
WHEN "Credit" THEN SET @ORDERBY = " T1.P_CR ";
ELSE SET @ORDERBY = " T1.P_VEN ";
END CASE;
SET @OPEN_BAL = CONCAT("(SELECT T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME,
0.0 AS P_DR, 0.0 AS P_CR, SUM(COALESCE(T1.dou_cr, 0) - COALESCE(T1.dou_dr, 0)) AS P_BAL
FROM ame_sl_ap AS T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
WHERE
(CASE
WHEN '", P1, "' = 'ALL' THEN FALSE
ELSE DATE(T1.dat_date) < DATE('", P2, "') END)
AND
IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
AND
IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
AND
COALESCE(T1.dou_cr, 0) <> 0 AND COALESCE(T1.dou_dr, 0) <> 0
GROUP BY T1.var_venid) UNION ALL ");
IF P26 = 1 THEN SET @OPEN_BAL = ""; END IF;
SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_VEN ", @SORT);
SET @SQL_EXC = CONCAT("SELECT * FROM (SELECT T1.P_VEN, T1.P_VEN_NAME, SUM(T1.P_DR) AS P_DR, SUM(T1.P_CR) AS P_CR, SUM(T1.P_BAL) AS P_BAL FROM (", @OPEN_BAL, "(SELECT T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_dr, 0) AS P_DR, COALESCE(T1.dou_cr, 0) AS P_CR, 0.0 AS P_BAL
FROM ame_sl_ap_his T1
LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
WHERE
(CASE
WHEN '", P1, "' = 'ALL' THEN TRUE
WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH(NOW())
WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR(NOW())
ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
AND
IF('", P4, "' = 'ALL', TRUE, T1.var_doctype = '", P4, "')
AND
IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
AND
IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
AND
IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
AND
IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
AND
IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
AND
IF('", P21, "' = 'ALL', TRUE, T1.var_apaccid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T1.var_apaccid <= '", P22, "')
)) AS T1 GROUP BY T1.P_VEN) AS T1", @ORDERBY);
PREPARE STM FROM @SQL_EXC;
EXECUTE STM;
DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D005_D1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D005_D1`(D1 varchar(100),D2 varchar(100),D3 varchar(100),D4 varchar(100),D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(100), P26 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		DECLARE P_DEC_AMT INTEGER DEFAULT 0;
		# DECIMAL
		BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = D3;OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF;CLOSE C1;END;
		SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_user_role T1 LEFT JOIN ame_user_per_oth T2 ON T1.int_id = T2.int_id WHERE T1.var_id = D1 AND T2.var_modid = 'HCI' AND COALESCE(T2.tin_per1, 0) = 1;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_VEN ";
		WHEN "Type" THEN SET @ORDERBY = " T1.P_TYPE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
		WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
		WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Class Id" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
		WHEN "Term" THEN SET @ORDERBY = " T1.P_TERM ";
		WHEN "Due Date" THEN SET @ORDERBY = " T1.P_DATE_DUE ";
		WHEN "Status" THEN SET @ORDERBY = " T1.P_STATUS ";
		WHEN "Payment Status" THEN SET @ORDERBY = " T1.P_STATUS_PMT ";
		WHEN "User" THEN SET @ORDERBY = " T1.P_USER ";
		WHEN "Orginal Amount" THEN SET @ORDERBY = " T1.P_NET ";
		WHEN "1 - 30 Days" THEN SET @ORDERBY = " T1.PMT_APP ";
		WHEN "31 - 60 Days" THEN SET @ORDERBY = " T1.PMT_DISC ";
		WHEN "61 - 90 Days" THEN SET @ORDERBY = " T1.PMT_AMT ";
		WHEN "> 90 Days" THEN SET @ORDERBY = " T1.PMT_AMT ";
		WHEN "Balance" THEN SET @ORDERBY = " T1.P_BAL ";
		ELSE SET @ORDERBY = " T1.P_ID ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY T1.P_SORTKEY, ", @ORDERBY, @SORT, ", T1.P_ID ", @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.* FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE,  '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_DESC, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE, 0 AS DAY_AGING, 0 AS DAY_DUE,
				0.0 AS P_NET, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, 0.0 AS P_PAID, 0.0 AS LESS_1, 0.0 AS 1_30, 0.0 AS 31_60, 0.0 AS 61_90, 0.0 OVER_90, 0.0 P_BAL LIMIT 0)", 		
				# PURCHASE
				IF(P23 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) AS DAY_AGING, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) AS DAY_DUE, 
				T1.dou_funnet AS P_NET,	COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP,	ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") AS P_PAID,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90, 
				ROUND(T1.dou_funnet - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, ") AS P_BAL
				FROM ame_ap_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_fundisc) AS PMT_DISC, SUM(T1.dou_funamt) AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_det T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id)")),
				# PURCHASE RETURN
				IF(P24 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 AS P_SORTKEY, 'AP-RET' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) AS DAY_AGING, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) AS DAY_DUE, 
				-T1.dou_funnet AS P_NET,	-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -COALESCE(T2.PMT_APP, 0) AS PMT_APP,	-ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") AS P_PAID,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90, 
				-ROUND(T1.dou_funnet - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, ") AS P_BAL
				FROM ame_ap_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Purchase Return' AND T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id)")),
				# DEBIT NOTE
				IF(P25 = "0", "", CONCAT(
				"UNION ALL (SELECT 4 AS P_SORTKEY, 'AP-DN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) AS DAY_AGING, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) AS DAY_DUE, 
				-T1.dou_funnet AS P_NET,	-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -COALESCE(T2.PMT_APP, 0) AS PMT_APP,	-ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") AS P_PAID,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90, 
				-ROUND(T1.dou_funnet - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, ") AS P_BAL
				FROM ame_ap_dn T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Debit Note' AND T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id)")),
				# PREPAYMENT
				IF(P26 = "0", "", CONCAT(
				"UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_date AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, '' AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) AS DAY_AGING, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) AS DAY_DUE, 
				-T1.dou_funnet AS P_NET,	-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -COALESCE(T2.PMT_APP, 0) AS PMT_APP,	-ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") AS P_PAID,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 30, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 60, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 90, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90, 
				-ROUND(T1.dou_funnet - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, ") AS P_BAL
				FROM ame_ap_py T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Prepayment' AND T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id)")),
				") AS T1", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT T1.* FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE,  '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_DESC, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE, 0 AS DAY_AGING, 0 AS DAY_DUE, 
				0.0 AS P_NET, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, 0.0 AS P_PAID, 0.0 AS LESS_1, 0.0 AS 1_30, 0.0 AS 31_60, 0.0 AS 61_90, 0.0 OVER_90, 0.0 P_BAL LIMIT 0)", 		
				# PURCHASE
				IF(P23 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) AS DAY_AGING, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) AS DAY_DUE,  
				T1.dou_net AS P_NET,	COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP,	ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") AS P_PAID,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 0, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90, 
				ROUND(T1.dou_net - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, ") AS P_BAL
				FROM ame_ap_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc) AS PMT_DISC, SUM(T1.dou_amt) AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_det T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id)")),
				# PURCHASE RETURN
				IF(P24 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 AS P_SORTKEY, 'AP-RET' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) AS DAY_AGING, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) AS DAY_DUE, 
				-T1.dou_net AS P_NET,	-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -COALESCE(T2.PMT_APP, 0) AS PMT_APP,	-ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") AS P_PAID,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 0, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90, 
				-ROUND(T1.dou_net - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, ") AS P_BAL
				FROM ame_ap_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Purchase Return' AND T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id)")),
				# DEBIT NOTE
				IF(P25 = "0", "", CONCAT(
				"UNION ALL (SELECT 4 AS P_SORTKEY, 'AP-DN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) AS DAY_AGING, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) AS DAY_DUE,  
				-T1.dou_net AS P_NET,	-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -COALESCE(T2.PMT_APP, 0) AS PMT_APP,	-ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") AS P_PAID,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 0, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90, 
				-ROUND(T1.dou_net - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, ") AS P_BAL
				FROM ame_ap_dn T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Debit Note' AND T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id)")),
				# DEBIT NOTE
				IF(P26 = "0", "", CONCAT(
				"UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_date AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, '' AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) AS DAY_AGING, DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) AS DAY_DUE, 
				-T1.dou_net AS P_NET,	-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -COALESCE(T2.PMT_APP, 0) AS PMT_APP,	-ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") AS P_PAID,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 0, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 0, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 30, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 60, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				-IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 90, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90, 
				-ROUND(T1.dou_net - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, ") AS P_BAL
				FROM ame_ap_py T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Prepayment' AND T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id)")),
				") AS T1", @ORDERBY);
		END IF;		
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D005_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D005_M1`(D1 varchar(100),D2 varchar(100),D3 varchar(100),D4 varchar(100),D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(100), P26 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_DEC_AMT INTEGER DEFAULT 0;
		# DECIMAL
		BEGIN
				DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = D3;
				OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF;CLOSE C1;
		END;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Vendor Id" THEN SET @ORDERBY = " T1.P_VEN ";
		WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
		WHEN "1 - 30 Days" THEN SET @ORDERBY = " T1.1_30 ";
		WHEN "31 - 60 Days" THEN SET @ORDERBY = " T1.31_60 ";
		WHEN "61 - 90 Days" THEN SET @ORDERBY = " T1.61_90 ";
		WHEN "> 90 Days" THEN SET @ORDERBY = " T1.OVER_90 ";
		WHEN "Original Amount" THEN SET @ORDERBY = " T1.P_NET ";
		WHEN "Balance" THEN SET @ORDERBY = " T1.P_BAL ";
		ELSE SET @ORDERBY = " T1.P_VEN ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_VEN ", @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.P_VEN, T1.P_VEN_NAME, SUM(T1.P_NET) AS P_NET, SUM(T1.LESS_1) AS LESS_1, SUM(T1.1_30) AS 1_30, SUM(T1.31_60) AS 31_60, SUM(T1.61_90) AS 61_90, SUM(T1.OVER_90) AS OVER_90, SUM(P_BAL) AS P_BAL FROM (
				(SELECT '' AS P_VEN, '' AS P_VEN_NAME, 0.0 AS P_NET, 0.0 AS LESS_1, 0.0 AS 1_30, 0.0 AS 31_60, 0.0 AS 61_90, 0.0 AS OVER_90, 0.0 AS P_BAL LIMIT 0)", 
				IF(P23 = "0", "", CONCAT(
				"UNION ALL (SELECT T1.P_VEN, T1.P_VEN_NAME, SUM(T1.P_NET) AS P_NET, SUM(T1.LESS_1) AS LESS_1, SUM(T1.1_30) AS 1_30, SUM(T1.31_60) AS 31_60, SUM(T1.61_90) AS 61_90, SUM(T1.OVER_90) AS OVER_90, SUM(T1.LESS_1 + T1.1_30 + T1.31_60 + T1.61_90 + T1.OVER_90) AS P_BAL FROM (SELECT T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_funnet, 0) AS P_NET,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 0,ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90
				FROM ame_ap_in T1 LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_fundisc) AS PMT_DISC, SUM(T1.dou_funamt) AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_det T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_type = 'Apply to Purchase Invoice' AND T2.var_status = 'Posted' AND IF('", P1, "' = 'All', TRUE, DATE(T2.dat_date) <= DATE('", P3, "')) GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id) AS T1 GROUP BY T1.P_VEN)")),
				IF(P24 = "0", "", CONCAT(
				"UNION ALL (SELECT T1.P_VEN, T1.P_VEN_NAME, -SUM(T1.P_NET) AS P_NET, -SUM(T1.LESS_1) AS LESS_1, -SUM(T1.1_30) AS 1_30, -SUM(T1.31_60) AS 31_60, -SUM(T1.61_90) AS 61_90, -SUM(T1.OVER_90) AS OVER_90, -SUM(T1.LESS_1 + T1.1_30 + T1.31_60 + T1.61_90 + T1.OVER_90) AS P_BAL FROM (SELECT T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_funnet, 0) AS P_NET,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90
				FROM ame_ap_ret T1 LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Purchase Return' AND T2.var_status = 'Posted' AND IF('", P1, "' = 'All', TRUE, DATE(T2.dat_date) <= DATE('", P3, "')) GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id) AS T1 GROUP BY T1.P_VEN)")),
				IF(P25 = "0", "", CONCAT(
				"UNION ALL (SELECT T1.P_VEN, T1.P_VEN_NAME, -SUM(T1.P_NET) AS P_NET, -SUM(T1.LESS_1) AS LESS_1, -SUM(T1.1_30) AS 1_30, -SUM(T1.31_60) AS 31_60, -SUM(T1.61_90) AS 61_90, -SUM(T1.OVER_90) AS OVER_90, -SUM(T1.LESS_1 + T1.1_30 + T1.31_60 + T1.61_90 + T1.OVER_90) AS P_BAL FROM (SELECT T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_funnet, 0) AS P_NET,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90
				FROM ame_ap_dn T1 LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Debit Note' AND T2.var_status = 'Posted' AND IF('", P1, "' = 'All', TRUE, DATE(T2.dat_date) <= DATE('", P3, "')) GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id) AS T1 GROUP BY T1.P_VEN)")),
				IF(P26 = "0", "", CONCAT(
				"UNION ALL (SELECT T1.P_VEN, T1.P_VEN_NAME, -SUM(T1.P_NET) AS P_NET, -SUM(T1.LESS_1) AS LESS_1, -SUM(T1.1_30) AS 1_30, -SUM(T1.31_60) AS 31_60, -SUM(T1.61_90) AS 61_90, -SUM(T1.OVER_90) AS OVER_90, -SUM(T1.LESS_1 + T1.1_30 + T1.31_60 + T1.61_90 + T1.OVER_90) AS P_BAL FROM (SELECT T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_funnet, 0) AS P_NET,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 30, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 60, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 90, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90
				FROM ame_ap_py T1 LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Prepayment' AND T2.var_status = 'Posted' AND IF('", P1, "' = 'All', TRUE, DATE(T2.dat_date) <= DATE('", P3, "')) GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_funnet, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id) AS T1 GROUP BY T1.P_VEN)")),
				") AS T1 GROUP BY T1.P_VEN", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT T1.P_VEN, T1.P_VEN_NAME, SUM(T1.P_NET) AS P_NET, SUM(T1.LESS_1) AS LESS_1, SUM(T1.1_30) AS 1_30, SUM(T1.31_60) AS 31_60, SUM(T1.61_90) AS 61_90, SUM(T1.OVER_90) AS OVER_90, SUM(P_BAL) AS P_BAL FROM (
				(SELECT '' AS P_VEN, '' AS P_VEN_NAME, 0.0 AS P_NET, 0.0 AS LESS_1, 0.0 AS 1_30, 0.0 AS 31_60, 0.0 AS 61_90, 0.0 AS OVER_90, 0.0 AS P_BAL LIMIT 0)", 
				IF(P23 = "0", "", CONCAT(
				"UNION ALL (SELECT T1.P_VEN, T1.P_VEN_NAME, SUM(T1.P_NET) AS P_NET, SUM(T1.LESS_1) AS LESS_1, SUM(T1.1_30) AS 1_30, SUM(T1.31_60) AS 31_60, SUM(T1.61_90) AS 61_90, SUM(T1.OVER_90) AS OVER_90, SUM(T1.LESS_1 + T1.1_30 + T1.31_60 + T1.61_90 + T1.OVER_90) AS P_BAL FROM (SELECT T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_net, 0) AS P_NET,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 0, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90
				FROM ame_ap_in T1 LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc) AS PMT_DISC, SUM(T1.dou_amt) AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_det T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_type = 'Apply to Purchase Invoice' AND T2.var_status = 'Posted' AND IF('", P1, "' = 'All', TRUE, DATE(T2.dat_date) <= DATE('", P3, "')) GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_net, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id) AS T1 GROUP BY T1.P_VEN)")),
				IF(P24 = "0", "", CONCAT(
				"UNION ALL (SELECT T1.P_VEN, T1.P_VEN_NAME, -SUM(T1.P_NET) AS P_NET, -SUM(T1.LESS_1) AS LESS_1, -SUM(T1.1_30) AS 1_30, -SUM(T1.31_60) AS 31_60, -SUM(T1.61_90) AS 61_90, -SUM(T1.OVER_90) AS OVER_90, -SUM(T1.LESS_1 + T1.1_30 + T1.31_60 + T1.61_90 + T1.OVER_90) AS P_BAL FROM (SELECT T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_net, 0) AS P_NET,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 0, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90
				FROM ame_ap_ret T1 LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Purchase Return' AND T2.var_status = 'Posted' AND IF('", P1, "' = 'All', TRUE, DATE(T2.dat_date) <= DATE('", P3, "')) GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_net, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id) AS T1 GROUP BY T1.P_VEN)")),
				IF(P25 = "0", "", CONCAT(
				"UNION ALL (SELECT T1.P_VEN, T1.P_VEN_NAME, -SUM(T1.P_NET) AS P_NET, -SUM(T1.LESS_1) AS LESS_1, -SUM(T1.1_30) AS 1_30, -SUM(T1.31_60) AS 31_60, -SUM(T1.61_90) AS 61_90, -SUM(T1.OVER_90) AS OVER_90, -SUM(T1.LESS_1 + T1.1_30 + T1.31_60 + T1.61_90 + T1.OVER_90) AS P_BAL FROM (SELECT T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_net, 0) AS P_NET,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 0, ROUND(COALESCE(T1.dou_funnet, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 0, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 30, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 60, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_due)) > 90, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90
				FROM ame_ap_dn T1 LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Debit Note' AND T2.var_status = 'Posted' AND IF('", P1, "' = 'All', TRUE, DATE(T2.dat_date) <= DATE('", P3, "')) GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status = 'Posted', T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_net, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id) AS T1 GROUP BY T1.P_VEN)")),
				IF(P26 = "0", "", CONCAT(
				"UNION ALL (SELECT T1.P_VEN, T1.P_VEN_NAME, -SUM(T1.P_NET) AS P_NET, -SUM(T1.LESS_1) AS LESS_1, -SUM(T1.1_30) AS 1_30, -SUM(T1.31_60) AS 31_60, -SUM(T1.61_90) AS 61_90, -SUM(T1.OVER_90) AS OVER_90, -SUM(T1.LESS_1 + T1.1_30 + T1.31_60 + T1.61_90 + T1.OVER_90) AS P_BAL FROM (SELECT T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_net, 0) AS P_NET,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 0, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS LESS_1,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 30 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 0, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 1_30,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 60 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 30, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 31_60,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) <= 90 AND DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 60, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS 61_90,
				IF(DATEDIFF(DATE('", P3, "'), DATE(T1.dat_date)) > 90, ROUND(COALESCE(T1.dou_net, 0) - (COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0)), ", P_DEC_AMT, "), 0) AS OVER_90
				FROM ame_ap_py T1 LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Prepayment' AND T2.var_status = 'Posted' AND IF('", P1, "' = 'All', TRUE, DATE(T2.dat_date) <= DATE('", P3, "')) GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) <= DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) <= MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) <= YEAR(NOW())
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				AND T1.var_status = 'Posted' AND ROUND(T1.dou_net, ", P_DEC_AMT, ") > ROUND(COALESCE(T2.PMT_DISC, 0) + COALESCE(T2.PMT_AMT, 0) + COALESCE(T2.PMT_APP, 0), ", P_DEC_AMT, ") GROUP BY T1.var_id) AS T1 GROUP BY T1.P_VEN)")),
				") AS T1 GROUP BY T1.P_VEN", @ORDERBY);
		END IF;
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D006DET_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D006DET_1`(P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(2000), P26 tinyint(1), P27 tinyint(1), P28 tinyint(1))
    DETERMINISTIC
BEGIN
SET @ORDERBY = "";
SET @SORT = IF(P24 = "A-Z", " ASC ", " DESC ");
CASE P23
WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
WHEN "Reference" THEN SET @ORDERBY = " T1.P_VEN ";
WHEN "Transaction Type" THEN SET @ORDERBY = " T1.P_TYPE_TRANS ";
WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
WHEN "Class Name" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
WHEN "Due Date" THEN SET @ORDERBY = " T1.P_DATE_DUE ";
WHEN "Status" THEN SET @ORDERBY = " T1.P_STATUS ";
WHEN "User" THEN SET @ORDERBY = " T1.P_USER ";
WHEN "Applied Pmt" THEN SET @ORDERBY = " T1.PMT_APP ";
WHEN "Pmt Disc." THEN SET @ORDERBY = " T1.PMT_DISC ";
WHEN "Cash" THEN SET @ORDERBY = " T1.PMT_AMT ";
ELSE SET @ORDERBY = " T1.P_ID ";
END CASE;
SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_ID ", @SORT);
SET @SQL_EXC = CONCAT("SELECT * FROM (SELECT T1.var_type AS P_TYPE_TRANS, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS,
T1.var_claid AS P_CLA, COALESCE(T3.var_name, '') AS P_CLA_NAME, T8.var_name AS P_CLA_NAME_DET, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T1.var_accid AS P_CASH_ACC, T5.var_name AS P_CASH_ACC_NAME, T4.var_docid AS P_DOC_ID, T6.var_term AS P_DOC_TERM, IF(T1.var_type = 'Apply to Purchase Invoice', T6.var_ref, T7.var_name) AS P_DOC_REF, T6.dat_date AS P_DOC_DATE, T6.dat_due AS P_DOC_DUE,
T4.dou_disc AS PMT_DISC, T4.dou_amt AS PMT_AMT, T4.dou_app AS PMT_APP, T1.var_modify AS P_USER, T4.var_desc AS P_DESC_DET
FROM ame_ap_py T1
LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id
LEFT JOIN ame_ap_py_det T4 ON T1.var_id = T4.var_id
LEFT JOIN ame_mt_acc T5 ON T1.var_accid = T5.var_id
LEFT JOIN ame_ap_in T6 ON T4.var_docid = T6.var_id
LEFT JOIN ame_mt_acc T7 ON T4.var_docid = T7.var_id
LEFT JOIN ame_mt_cla T8 ON T1.var_claid = T8.var_id
WHERE
(CASE
WHEN '", P1, "' = 'ALL' THEN TRUE
WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
AND
IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
AND
IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
AND
IF('", P6, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P6, "')
AND
IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
AND
IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
AND
IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
AND
IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
AND
IF('", P15, "' = 'ALL', TRUE, T1.var_accid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_accid <= '", P16, "')
AND
IF('", P17, "' = 'ALL', TRUE, T4.var_docid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T4.var_docid <= '", P18, "')
AND
IF('", P19, "' = 'ALL', TRUE, T4.var_docid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T4.var_docid <= '", P20, "')
ORDER BY T4.int_line ) AS T1", @ORDERBY);
PREPARE STM FROM @SQL_EXC;
EXECUTE STM;
DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D006SUM_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D006SUM_1`(P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(2000), P26 tinyint(1), P27 tinyint(1), P28 tinyint(1))
    DETERMINISTIC
BEGIN
SET @ORDERBY = "";
SET @SORT = IF(P24 = "A-Z", " ASC ", " DESC ");
CASE P23
WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
WHEN "Reference" THEN SET @ORDERBY = " T1.P_VEN ";
WHEN "Transaction Type" THEN SET @ORDERBY = " T1.P_TYPE_TRANS ";
WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
WHEN "Class Name" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
WHEN "Due Date" THEN SET @ORDERBY = " T1.P_DATE_DUE ";
WHEN "Status" THEN SET @ORDERBY = " T1.P_STATUS ";
WHEN "User" THEN SET @ORDERBY = " T1.P_USER ";
WHEN "Applied Pmt" THEN SET @ORDERBY = " T1.PMT_APP ";
WHEN "Pmt Disc." THEN SET @ORDERBY = " T1.PMT_DISC ";
WHEN "Cash" THEN SET @ORDERBY = " T1.PMT_AMT ";
ELSE SET @ORDERBY = " T1.P_ID ";
END CASE;
SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_ID ", @SORT);
SET @SQL_EXC = CONCAT("SELECT * FROM (SELECT T1.var_type AS P_TYPE_TRANS, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS,
T1.var_claid AS P_CLA, COALESCE(T3.var_name, '') AS P_CLA_NAME, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T1.var_accid AS P_CASH_ACC, T5.var_name AS P_CASH_ACC_NAME,
SUM(T4.dou_disc) AS PMT_DISC, SUM(T4.dou_amt) AS PMT_AMT, SUM(T4.dou_app) AS PMT_APP, T1.var_modify AS P_USER
FROM ame_ap_py T1
LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id
LEFT JOIN ame_ap_py_det T4 ON T1.var_id = T4.var_id
LEFT JOIN ame_mt_acc T5 ON T1.var_accid = T5.var_id
WHERE
(CASE
WHEN '", P1, "' = 'ALL' THEN TRUE
WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
AND
IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
AND
IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
AND
IF('", P6, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P6, "')
AND
IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
AND
IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
AND
IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
AND
IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
AND
IF('", P15, "' = 'ALL', TRUE, T1.var_accid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_accid <= '", P16, "')
AND
IF('", P17, "' = 'ALL', TRUE, T4.var_docid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T4.var_docid <= '", P18, "')
AND
IF('", P19, "' = 'ALL', TRUE, T4.var_docid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T4.var_docid <= '", P20, "')
GROUP BY T1.var_id) AS T1", @ORDERBY);
PREPARE STM FROM @SQL_EXC;
EXECUTE STM;
DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D006_D1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D006_D1`(D1 varchar(100), D2 varchar(100), D3 varchar(100), D4 varchar(100), D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_DEC_AMT INTEGER DEFAULT 0;
		DECLARE P_WHERE_RF VARCHAR(2000) DEFAULT "";
		# DECIMAL
		BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = D3;OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF;CLOSE C1;END;
		IF P15 = "1" = 1 THEN SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AP-IN',"); END IF;
		IF P16 = "1" THEN SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AP-RET',"); END IF;
		IF P17 = "1" THEN SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AP-DN',"); END IF;
		IF P18 = "1" THEN SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AP-PY',"); END IF;
		IF P_WHERE_RF = "" THEN		
				SET P_WHERE_RF = " AND FALSE ";
		ELSE
				SET P_WHERE_RF = SUBSTR(P_WHERE_RF FROM 1 FOR LENGTH(P_WHERE_RF) - 1);
				SET P_WHERE_RF = CONCAT(" AND (T1.var_modid IN(", P_WHERE_RF, ")) ");
		END IF;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Module" THEN SET @ORDERBY = " T1.P_MODULE ";
		WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
		WHEN "Type" THEN SET @ORDERBY = " T1.P_TYPE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
		WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
		WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Class" THEN SET @ORDERBY = " T1.P_CLA ";
		WHEN "Term" THEN SET @ORDERBY = " T1.P_TERM ";
		WHEN "Due Date" THEN SET @ORDERBY = " T1.P_DATE_DUE ";
		WHEN "Debit" THEN SET @ORDERBY = " T1.P_DR ";
		WHEN "Credit" THEN SET @ORDERBY = " T1.P_CR ";
		ELSE SET @ORDERBY = " T1.P_SORTKEY ";
		END CASE;
		IF D2 = "0" THEN
				SET @OPEN_BAL = CONCAT("(SELECT 0 AS P_SORTKEY, '' AS P_MODULE, '' AS P_TYPE, 'Opening Balance' AS P_ID, '' AS P_REF, DATE('", P2, "') AS P_DATE, NULL AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, '' AS P_DESC, '' AS P_PMT_METH, '' AS P_PMT_REF,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_TERM,
				0.0 AS P_DR, 0.0 AS P_CR, SUM(COALESCE(T1.dou_funcr, 0) - COALESCE(T1.dou_fundr, 0)) AS P_BAL
				FROM ame_sl_ap AS T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN FALSE
				ELSE DATE(T1.dat_date) < DATE('", P2, "') END)
				AND
				IF('", P7, "' = 'ALL', TRUE, T2.var_groupid >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T2.var_groupid <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T1.var_venid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T1.var_venid <= '", P10, "')
				GROUP BY T1.var_venid) UNION ALL ");
				IF P19 = "1" THEN SET @OPEN_BAL = ""; END IF;
				SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_SORTKEY ", @SORT);
				SET @SQL_EXC = CONCAT("SELECT * FROM (", @OPEN_BAL, "(SELECT T1.int_line AS P_SORTKEY, T1.var_modid AS P_MODULE, T1.var_doctype AS P_TYPE, T1.var_docid AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_pmttype AS P_PMT_METH, T1.var_pmtref AS P_PMT_REF,
				T1.var_claid AS P_CLA, COALESCE(T3.var_name, '') AS P_CLA_NAME, T1.var_term AS P_TERM,
				COALESCE(T1.dou_fundr, 0) AS P_DR, COALESCE(T1.dou_funcr, 0) AS P_CR, 0.0 AS P_BAL
				FROM ame_sl_ap_his T1
				LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id
				LEFT JOIN ame_mt_acc T4 ON T1.var_apaccid = T4.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR(NOW())
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_doctype = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T2.var_groupid >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T2.var_groupid <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T1.var_venid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T1.var_venid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_claid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_claid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_apaccid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_apaccid <= '", P14, "')
				AND CONCAT(T1.var_modid, ' ',T1.var_doctype) <> 'AP-PY Apply to GL Account'
				", P_WHERE_RF, " ORDER BY T1.int_line)) AS T1", @ORDERBY);
		ELSE
				SET @OPEN_BAL = CONCAT("(SELECT 0 AS P_SORTKEY, '' AS P_MODULE, '' AS P_TYPE, 'Opening Balance' AS P_ID, '' AS P_REF, DATE('", P2, "') AS P_DATE, NULL AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, '' AS P_DESC, '' AS P_PMT_METH, '' AS P_PMT_REF,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_TERM,
				0.0 AS P_DR, 0.0 AS P_CR, SUM(COALESCE(T1.dou_cr, 0) - COALESCE(T1.dou_dr, 0)) AS P_BAL
				FROM ame_sl_ap AS T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN FALSE
				ELSE DATE(T1.dat_date) < DATE('", P2, "') END)
				AND
				IF('", P7, "' = 'ALL', TRUE, T2.var_groupid >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T2.var_groupid <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T1.var_venid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T1.var_venid <= '", P10, "')
				GROUP BY T1.var_venid) UNION ALL ");
				IF P19 = "1" THEN SET @OPEN_BAL = ""; END IF;
				SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_SORTKEY ", @SORT);
				SET @SQL_EXC = CONCAT("SELECT * FROM (", @OPEN_BAL, "(SELECT T1.int_line AS P_SORTKEY, T1.var_modid AS P_MODULE, T1.var_doctype AS P_TYPE, T1.var_docid AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_pmttype AS P_PMT_METH, T1.var_pmtref AS P_PMT_REF,
				T1.var_claid AS P_CLA, COALESCE(T3.var_name, '') AS P_CLA_NAME, T1.var_term AS P_TERM,
				COALESCE(T1.dou_dr, 0) AS P_DR, COALESCE(T1.dou_cr, 0) AS P_CR, 0.0 AS P_BAL
				FROM ame_sl_ap_his T1
				LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id
				LEFT JOIN ame_mt_acc T4 ON T1.var_apaccid = T4.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR(NOW())
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_doctype = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T2.var_groupid >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T2.var_groupid <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T1.var_venid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T1.var_venid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_claid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_claid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_apaccid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_apaccid <= '", P14, "')
				AND CONCAT(T1.var_modid, ' ',T1.var_doctype) <> 'AP-PY Apply to GL Account'
				", P_WHERE_RF, " ORDER BY T1.int_line)) AS T1", @ORDERBY);
		END IF;
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D006_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D006_M1`(D1 varchar(100), D2 varchar(100), D3 varchar(100), D4 varchar(100), D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_DEC_AMT INTEGER DEFAULT 0;
		DECLARE P_WHERE_RF VARCHAR(2000) DEFAULT "";
		# DECIMAL
		BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = D3;OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF;CLOSE C1;END;
		IF P15 = "1" = 1 THEN SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AP-IN',"); END IF;
		IF P16 = "1" THEN SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AP-RET',"); END IF;
		IF P17 = "1" THEN SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AP-DN',"); END IF;
		IF P18 = "1" THEN SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AP-PY',"); END IF;
		IF P_WHERE_RF = "" THEN		
				SET P_WHERE_RF = " AND FALSE ";
		ELSE
				SET P_WHERE_RF = SUBSTR(P_WHERE_RF FROM 1 FOR LENGTH(P_WHERE_RF) - 1);
				SET P_WHERE_RF = CONCAT(" AND (T1.var_modid IN(", P_WHERE_RF, ")) ");
		END IF;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
		WHEN "Debit" THEN SET @ORDERBY = " T1.P_DR ";
		WHEN "Credit" THEN SET @ORDERBY = " T1.P_CR ";
		WHEN "Balance" THEN SET @ORDERBY = " T1.P_BAL ";
		ELSE SET @ORDERBY = " T1.P_VEN ";
		END CASE;
		IF D2 = "0" THEN
				SET @OPEN_BAL = CONCAT("(SELECT T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME,
				0.0 AS P_DR, 0.0 AS P_CR, SUM(COALESCE(T1.dou_funcr, 0) - COALESCE(T1.dou_fundr, 0)) AS P_BAL
				FROM ame_sl_ap AS T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN FALSE
				ELSE DATE(T1.dat_date) < DATE('", P2, "') END)
				AND
				IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				GROUP BY T1.var_venid) UNION ALL ");
				IF P19 = "1" THEN SET @OPEN_BAL = ""; END IF;
				SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_VEN ", @SORT);
				SET @SQL_EXC = CONCAT("SELECT * FROM (
				SELECT T1.P_VEN, T1.P_VEN_NAME, SUM(T1.P_DR) AS P_DR, SUM(T1.P_CR) AS P_CR, SUM(T1.P_BAL) AS P_BAL FROM (", @OPEN_BAL, "(SELECT T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_fundr, 0) AS P_DR, COALESCE(T1.dou_funcr, 0) AS P_CR, 0.0 AS P_BAL
				FROM ame_sl_ap_his T1
				LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR(NOW())
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_doctype = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T2.var_groupid >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T2.var_groupid <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T1.var_venid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T1.var_venid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_claid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_claid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_apaccid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_apaccid <= '", P14, "')
				AND CONCAT(T1.var_modid, ' ',T1.var_doctype) <> 'AP-PY Apply to GL Account'
				", P_WHERE_RF, ")) AS T1 GROUP BY T1.P_VEN) AS T1", @ORDERBY);
		ELSE
				SET @OPEN_BAL = CONCAT("(SELECT T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME,
				0.0 AS P_DR, 0.0 AS P_CR, SUM(COALESCE(T1.dou_funcr, 0) - COALESCE(T1.dou_fundr, 0)) AS P_BAL
				FROM ame_sl_ap AS T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN FALSE
				ELSE DATE(T1.dat_date) < DATE('", P2, "') END)
				AND
				IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				GROUP BY T1.var_venid) UNION ALL ");
				IF P19 = "1" THEN SET @OPEN_BAL = ""; END IF;
				SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_VEN ", @SORT);
				SET @SQL_EXC = CONCAT("SELECT * FROM (
				SELECT T1.P_VEN, T1.P_VEN_NAME, SUM(T1.P_DR) AS P_DR, SUM(T1.P_CR) AS P_CR, SUM(T1.P_BAL) AS P_BAL FROM (", @OPEN_BAL, "(SELECT T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, COALESCE(T1.dou_fundr, 0) AS P_DR, COALESCE(T1.dou_funcr, 0) AS P_CR, 0.0 AS P_BAL
				FROM ame_sl_ap_his T1
				LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH(NOW())
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR(NOW())
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_doctype = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T2.var_groupid >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T2.var_groupid <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T1.var_venid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T1.var_venid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_claid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_claid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_apaccid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_apaccid <= '", P14, "')
				AND CONCAT(T1.var_modid, ' ',T1.var_doctype) <> 'AP-PY Apply to GL Account'
				", P_WHERE_RF, ")) AS T1 GROUP BY T1.P_VEN) AS T1", @ORDERBY);
		END IF;
		
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D007_D1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D007_D1`(D1 varchar(100), D2 varchar(100), D3 varchar(100), D4 varchar(100), D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100))
    DETERMINISTIC
BEGIN
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_VEN ";
		WHEN "Transaction Type" THEN SET @ORDERBY = " T1.P_TYPE_TRANS ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
		WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
		WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Class" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
		WHEN "Due Date" THEN SET @ORDERBY = " T1.P_DATE_DUE ";
		WHEN "Status" THEN SET @ORDERBY = " T1.P_STATUS ";
		WHEN "User" THEN SET @ORDERBY = " T1.P_USER ";
		WHEN "Applied Pmt" THEN SET @ORDERBY = " T1.PMT_APP ";
		WHEN "Pmt Disc." THEN SET @ORDERBY = " T1.PMT_DISC ";
		WHEN "Cash" THEN SET @ORDERBY = " T1.PMT_AMT ";
		ELSE SET @ORDERBY = " T1.P_ID ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_ID ", @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.* FROM (SELECT T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T3.var_name, '') AS P_CLA_NAME_1, T4.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T1.var_accid AS P_CASH_ACC, T5.var_name AS P_CASH_ACC_NAME, T4.var_docid AS P_DOC_ID, T6.var_term AS P_DOC_TERM, IF(T1.var_type = 'Apply to Purchase Invoice', T6.var_ref, T7.var_name) AS P_DOC_REF, T6.dat_date AS P_DOC_DATE, T6.dat_due AS P_DOC_DUE,
				T4.dou_fundisc AS PMT_DISC, T4.dou_funamt AS PMT_AMT, T4.dou_funapp AS PMT_APP, (T4.dou_fundisc + T4.dou_funamt + T4.dou_funapp) AS P_PAID, T1.var_create AS P_USER, T4.var_desc AS P_DESC_DET, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_py T1
				LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id
				LEFT JOIN ame_ap_py_det T4 ON T1.var_id = T4.var_id
				LEFT JOIN ame_mt_acc T5 ON T1.var_accid = T5.var_id
				LEFT JOIN ame_ap_in T6 ON T4.var_docid = T6.var_id
				LEFT JOIN ame_mt_acc T7 ON T4.var_docid = T7.var_id
				LEFT JOIN ame_mt_cla T8 ON T4.var_claid = T8.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_accid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_accid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T4.var_docid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T4.var_docid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T4.var_docid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T4.var_docid <= '", P20, "')
				ORDER BY T4.int_line) AS T1", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT T1.* FROM (SELECT T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T3.var_name, '') AS P_CLA_NAME_1, T4.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T1.var_accid AS P_CASH_ACC, T5.var_name AS P_CASH_ACC_NAME, T4.var_docid AS P_DOC_ID, T6.var_term AS P_DOC_TERM, IF(T1.var_type = 'Apply to Purchase Invoice', T6.var_ref, T7.var_name) AS P_DOC_REF, T6.dat_date AS P_DOC_DATE, T6.dat_due AS P_DOC_DUE,
				T4.dou_disc AS PMT_DISC, T4.dou_amt AS PMT_AMT, T4.dou_app AS PMT_APP, (T4.dou_disc + T4.dou_amt + T4.dou_app) AS P_PAID, T1.var_create AS P_USER, T4.var_desc AS P_DESC_DET, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_py T1
				LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id
				LEFT JOIN ame_ap_py_det T4 ON T1.var_id = T4.var_id
				LEFT JOIN ame_mt_acc T5 ON T1.var_accid = T5.var_id
				LEFT JOIN ame_ap_in T6 ON T4.var_docid = T6.var_id
				LEFT JOIN ame_mt_acc T7 ON T4.var_docid = T7.var_id
				LEFT JOIN ame_mt_cla T8 ON T4.var_claid = T8.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_accid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_accid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T4.var_docid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T4.var_docid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T4.var_docid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T4.var_docid <= '", P20, "')
				ORDER BY T4.int_line) AS T1", @ORDERBY);
		END IF;
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D007_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D007_M1`(D1 varchar(100), D2 varchar(100), D3 varchar(100), D4 varchar(100), D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100))
    DETERMINISTIC
BEGIN
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
		WHEN "Type" THEN SET @ORDERBY = " T1.P_TYPE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
		WHEN "Vendor Id" THEN SET @ORDERBY = " T1.P_VEN ";
		WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
		WHEN "Cash Account Id" THEN SET @ORDERBY = " T1.P_CASH_ACC ";
		WHEN "Cash Account Name" THEN SET @ORDERBY = " T1.P_CASH_ACC_NAME ";
		WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Class" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
		WHEN "Status" THEN SET @ORDERBY = " T1.P_STATUS ";
		WHEN "User" THEN SET @ORDERBY = " T1.P_USER ";
		WHEN "Applied PMT" THEN SET @ORDERBY = " T1.PMT_APP ";
		WHEN "Discount PMT" THEN SET @ORDERBY = " T1.PMT_DISC ";
		WHEN "Cash PMT" THEN SET @ORDERBY = " T1.PMT_AMT ";
		WHEN "Total Paid" THEN SET @ORDERBY = " T1.P_PAID ";
		ELSE SET @ORDERBY = " T1.P_ID ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_ID ", @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.* FROM (SELECT T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T3.var_name, '') AS P_CLA_NAME, T1.var_pmttype AS P_PMT_METH, T1.var_pmtref AS P_PMT_REF, T1.var_accid AS P_CASH_ACC, T5.var_name AS P_CASH_ACC_NAME,
				SUM(T4.dou_fundisc) AS PMT_DISC, SUM(T4.dou_funamt) AS PMT_AMT, SUM(T4.dou_funapp) AS PMT_APP, SUM(T4.dou_fundisc + T4.dou_funamt + T4.dou_funapp) AS P_PAID, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_py T1
				LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id
				LEFT JOIN ame_ap_py_det T4 ON T1.var_id = T4.var_id
				LEFT JOIN ame_mt_acc T5 ON T1.var_accid = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_accid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_accid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T4.var_docid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T4.var_docid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T4.var_docid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T4.var_docid <= '", P20, "')
				GROUP BY T1.var_id) AS T1", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT T1.* FROM (SELECT T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.var_venid AS P_VEN, COALESCE(T2.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS,
				T1.var_claid AS P_CLA, COALESCE(T3.var_name, '') AS P_CLA_NAME, T1.var_pmttype AS P_PMT_METH, T1.var_pmtref AS P_PMT_REF, T1.var_accid AS P_CASH_ACC, T5.var_name AS P_CASH_ACC_NAME,
				SUM(T4.dou_disc) AS PMT_DISC, SUM(T4.dou_amt) AS PMT_AMT, SUM(T4.dou_app) AS PMT_APP, SUM(T4.dou_disc + T4.dou_amt + T4.dou_app) AS P_PAID, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_py T1
				LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
				LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id
				LEFT JOIN ame_ap_py_det T4 ON T1.var_id = T4.var_id
				LEFT JOIN ame_mt_acc T5 ON T1.var_accid = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', TRUE, T1.var_pmttype = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_accid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_accid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T4.var_docid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T4.var_docid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T4.var_docid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T4.var_docid <= '", P20, "')
				GROUP BY T1.var_id) AS T1", @ORDERBY);
		END IF;
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M002D001_D1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M002D001_D1`(D1 varchar(100), D2 varchar(100), D3 varchar(100), D4 varchar(100), D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(100), P26 varchar(100), P27 varchar(100), P28 varchar(100), P29 varchar(100), P30 varchar(100), P31 varchar(100), P32 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_user_role T1 LEFT JOIN ame_user_per_oth T2 ON T1.int_id = T2.int_id WHERE T1.var_id = D1 AND T2.var_modid = 'HCI' AND COALESCE(T2.tin_per1, 0) = 1;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Item/Acc. Id" THEN SET @ORDERBY = " T1.P_ITE ";
		WHEN "Item/Acc. Name" THEN SET @ORDERBY = " T1.P_ITE_NAME ";
		WHEN "Description" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Location" THEN SET @ORDERBY = " T1.P_LOC ";
		WHEN "Class" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
		WHEN "Sub Total" THEN SET @ORDERBY = " T1.P_SUB ";
		WHEN "Discount" THEN SET @ORDERBY = " T1.P_DISC ";
		WHEN "VAT" THEN SET @ORDERBY = " T1.P_TAX1 ";
		WHEN "WHT" THEN SET @ORDERBY = " T1.P_TAX2 ";
		WHEN "Grand Total" THEN SET @ORDERBY = " T1.P_NET ";
		ELSE SET @ORDERBY = " T1.P_LINE ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY T1.P_SORTKEY, ", @ORDERBY, @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.* FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE, '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(DATE(NOW())) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_DESC_1, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_LOC, 0.0 AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, 0.0 AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE
				, 0 AS P_LINE, '' AS 'P_ITE', '' AS P_ITE_NAME, '' AS P_DESC, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_FOC, 0.0 AS P_QTY, 0.0 AS P_PRICE, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, 0.0 AS P_NET LIMIT 0)", 
				# QUOTATION
				IF(P27 = "0", "", CONCAT("
				UNION ALL (SELECT 0 AS P_SORTKEY, 'AR-QUO' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T9.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB_1, T1.dou_fundisdol AS P_DISC_1, T1.dou_funtaxdol AS P_TAX1_1, T1.dou_funtaxdol1 AS P_TAX2_1, T1.dou_funnet AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_foc AS P_FOC, T5.dou_qty AS P_QTY, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_quo T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_quo_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T9 ON T1.var_empid = T9.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T9.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T9.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES ORDER
				IF(P28 = "0", "", CONCAT("
				UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-SO' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T9.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB_1, T1.dou_fundisdol AS P_DISC_1, T1.dou_funtaxdol AS P_TAX1_1, T1.dou_funtaxdol1 AS P_TAX2_1, T1.dou_funnet AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_foc AS P_FOC, T5.dou_qty AS P_QTY, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_so T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_so_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T9 ON T1.var_empid = T9.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T9.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T9.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES
				IF(P29 = "0", "", CONCAT("
				UNION ALL (SELECT 2 AS P_SORTKEY, 'AR-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T9.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB_1, T1.dou_fundisdol AS P_DISC_1, T1.dou_funtaxdol AS P_TAX1_1, T1.dou_funtaxdol1 AS P_TAX2_1, T1.dou_funnet AS P_NET_1, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_foc AS P_FOC, T5.dou_qty AS P_QTY, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_fundisc) AS PMT_DISC, SUM(T1.dou_funamt) AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_det T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T9 ON T1.var_empid = T9.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T9.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T9.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES RETURN
				IF(P30 = "0", "", CONCAT("
				UNION ALL (SELECT 3 AS P_SORTKEY, 'AR-RET' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) + COALESCE(T9.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_foc AS P_FOC, -T5.dou_qty AS P_QTY, -T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_funsub) AS P_SUB, -T5.dou_fundisdol AS P_DISC, -T5.dou_funtaxdol AS P_TAX1, -T5.dou_funtaxdol1 AS P_TAX2, -(T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, -SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Sales Return' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, -SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-RET' GROUP BY T1.var_docid) AS T9 ON T1.var_id = T9.var_docid
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T10.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T10.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# CREDIT NOTE
				IF(P31 = "0", "", CONCAT("
				UNION ALL (SELECT 4 AS P_SORTKEY, 'AR-CN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB_1, T1.dou_fundisdol AS P_DISC_1, T1.dou_funtaxdol AS P_TAX1_1, T1.dou_funtaxdol1 AS P_TAX2_1, T1.dou_funnet AS P_NET_1, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) + COALESCE(T9.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_foc AS P_FOC, -T5.dou_qty AS P_QTY, -T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_funsub) AS P_SUB, -T5.dou_fundisdol AS P_DISC, -T5.dou_funtaxdol AS P_TAX1, -T5.dou_funtaxdol1 AS P_TAX2, -(T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_cn T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, -SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Credit Note' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_cn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, -SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AP-DN' GROUP BY T1.var_docid) AS T9 ON T1.var_id = T9.var_docid
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T10.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T10.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# PREPAYMENT
				IF(P32 = "0", "", CONCAT("
				UNION ALL (SELECT 5 AS P_SORTKEY, 'AR-PY' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, '' AS P_LOC, T1.dou_amt AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, T1.dou_net AS P_NET_1, '' AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) + COALESCE(T9.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, T5.var_docid AS 'P_ITE', T7.var_name AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_FOC, 0.0 AS P_QTY, -T5.dou_funamt AS P_PRICE, -T5.dou_funamt AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, -T5.dou_funamt AS P_NET
				FROM ame_ar_py T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, -SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_py_det T5 LEFT JOIN ame_mt_acc T7 ON T5.var_docid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, -SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-PY' GROUP BY T1.var_docid) AS T9 ON T1.var_id = T9.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P18, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_docid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_docid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				") AS T1", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT T1.* FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE, '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(DATE(NOW())) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_DESC_1, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_LOC, 0.0 AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, 0.0 AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE
				, 0 AS P_LINE, '' AS 'P_ITE', '' AS P_ITE_NAME, '' AS P_DESC, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_FOC, 0.0 AS P_QTY, 0.0 AS P_PRICE, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, 0.0 AS P_NET LIMIT 0)", 
				# QUOTATION
				IF(P27 = "0", "", CONCAT("
				UNION ALL (SELECT 0 AS P_SORTKEY, 'AR-QUO' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T9.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_foc AS P_FOC, T5.dou_qty AS P_QTY, T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_sub) AS P_SUB, T5.dou_disdol AS P_DISC, T5.dou_taxdol AS P_TAX1, T5.dou_taxdol1 AS P_TAX2, (T5.dou_sub - T5.dou_disdol + T5.dou_taxdol + T5.dou_taxdol1) AS P_NET
				FROM ame_ar_quo T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_quo_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T9 ON T1.var_empid = T9.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T9.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T9.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES ORDER
				IF(P28 = "0", "", CONCAT("
				UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-SO' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T9.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_foc AS P_FOC, T5.dou_qty AS P_QTY, T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_sub) AS P_SUB, T5.dou_disdol AS P_DISC, T5.dou_taxdol AS P_TAX1, T5.dou_taxdol1 AS P_TAX2, (T5.dou_sub - T5.dou_disdol + T5.dou_taxdol + T5.dou_taxdol1) AS P_NET
				FROM ame_ar_so T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_so_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T9 ON T1.var_empid = T9.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T9.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T9.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES
				IF(P29 = "0", "", CONCAT("
				UNION ALL (SELECT 2 AS P_SORTKEY, 'AR-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T9.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_foc AS P_FOC, T5.dou_qty AS P_QTY, T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_sub) AS P_SUB, T5.dou_disdol AS P_DISC, T5.dou_taxdol AS P_TAX1, T5.dou_taxdol1 AS P_TAX2, (T5.dou_sub - T5.dou_disdol + T5.dou_taxdol + T5.dou_taxdol1) AS P_NET
				FROM ame_ar_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc) AS PMT_DISC, SUM(T1.dou_amt) AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_det T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T9 ON T1.var_empid = T9.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T9.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T9.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES RETURN
				IF(P30 = "0", "", CONCAT("
				UNION ALL (SELECT 3 AS P_SORTKEY, 'AR-RET' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) + COALESCE(T9.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_foc AS P_FOC, -T5.dou_qty AS P_QTY, -T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_sub) AS P_SUB, -T5.dou_disdol AS P_DISC, -T5.dou_taxdol AS P_TAX1, -T5.dou_taxdol1 AS P_TAX2, -(T5.dou_sub - T5.dou_disdol + T5.dou_taxdol + T5.dou_taxdol1) AS P_NET
				FROM ame_ar_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, -SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Sales Return' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, -SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-RET' GROUP BY T1.var_docid) AS T9 ON T1.var_id = T9.var_docid
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T10.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T10.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# CREDIT NOTE
				IF(P31 = "0", "", CONCAT("
				UNION ALL (SELECT 4 AS P_SORTKEY, 'AR-CN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) + COALESCE(T9.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_foc AS P_FOC, -T5.dou_qty AS P_QTY, -T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_sub) AS P_SUB, -T5.dou_disdol AS P_DISC, -T5.dou_taxdol AS P_TAX1, -T5.dou_taxdol1 AS P_TAX2, -(T5.dou_sub - T5.dou_disdol + T5.dou_taxdol + T5.dou_taxdol1) AS P_NET
				FROM ame_ar_cn T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, -SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Credit Note' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_cn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, -SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-CN' GROUP BY T1.var_docid) AS T9 ON T1.var_id = T9.var_docid
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T10.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T10.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				# Prepayment
				IF(P32 = "0", "", CONCAT("
				UNION ALL (SELECT 5 AS P_SORTKEY, 'AR-PY' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, '' AS P_LOC, T1.dou_amt AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, T1.dou_net AS P_NET_1, '' AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) + COALESCE(T9.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, T5.var_docid AS 'P_ITE', T7.var_name AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_FOC, 0.0 AS P_QTY, -T5.dou_amt AS P_PRICE, -T5.dou_amt AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, -T5.dou_amt AS P_NET
				FROM ame_ar_py T1
				LEFT JOIN (SELECT T1.var_docid, 0.0 AS PMT_DISC, 0.0 AS PMT_AMT, -SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_py_det T5 LEFT JOIN ame_mt_acc T7 ON T5.var_docid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, -SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-PY' GROUP BY T1.var_docid) AS T9 ON T1.var_id = T9.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P18, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_docid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_docid <= '", P26, "')
				ORDER BY T1.var_id, T5.int_line)")),
				") AS T1", @ORDERBY);
		END IF;	
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M002D001_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M002D001_M1`(D1 varchar(100), D2 varchar(100), D3 varchar(100), D4 varchar(100), D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(100), P26 varchar(100), P27 varchar(100), P28 varchar(100), P29 varchar(100), P30 varchar(100), P31 varchar(100), P32 varchar(100))
    DETERMINISTIC
BEGIN
		#  '','', '', '','', 'All', '2018-06-11', '2018-06-11', '','', '', '','', '', '', '',  '','','','', '', '', '','' ,'' ,'' ,'' ,'', '' ,'' ,'' ,'', '','', '', '' ,''
		DECLARE P_ROW INTEGER DEFAULT 0;
		SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_user_role T1 LEFT JOIN ame_user_per_oth T2 ON T1.int_id = T2.int_id WHERE T1.var_id = D1 AND T2.var_modid = 'HCI' AND COALESCE(T2.tin_per1, 0) = 1;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_VEN ";
		WHEN "Type" THEN SET @ORDERBY = " T1.P_TYPE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
		WHEN "Customer Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
		WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Location" THEN SET @ORDERBY = " T1.P_LOC ";
		WHEN "Class Id" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
		WHEN "Term" THEN SET @ORDERBY = " T1.P_TERM ";
		WHEN "Due Date" THEN SET @ORDERBY = " T1.P_DATE_DUE ";
		WHEN "Status" THEN SET @ORDERBY = " T1.P_STATUS ";
		WHEN "Payment Status" THEN SET @ORDERBY = " T1.P_STATUS_PMT ";
		WHEN "User" THEN SET @ORDERBY = " T1.P_USER ";
		WHEN "Sub Total" THEN SET @ORDERBY = " T1.P_SUB ";
		WHEN "Discount" THEN SET @ORDERBY = " T1.P_DISC ";
		WHEN "VAT" THEN SET @ORDERBY = " T1.P_VAT ";
		WHEN "Grand Total" THEN SET @ORDERBY = " T1.P_NET ";
		WHEN "Applied PMT" THEN SET @ORDERBY = " T1.PMT_APP ";
		WHEN "Discount PMT" THEN SET @ORDERBY = " T1.PMT_DISC ";
		WHEN "Cash PMT" THEN SET @ORDERBY = " T1.PMT_AMT ";
		ELSE SET @ORDERBY = " T1.P_ID ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY T1.P_SORTKEY, ", @ORDERBY, @SORT, ", T1.P_ID ", @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.* FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE,  '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_DESC, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE LIMIT 0)", 		
				# QUOTATION
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 0 AS P_SORTKEY, 'AR-QUO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T7.var_name AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB, T1.dou_fundisdol AS P_DISC, T1.dou_funtaxdol AS P_VAT, T1.dou_funtaxdol1 AS P_WHT, T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_quo T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_quo_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T7.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T7.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				GROUP BY T1.var_id)")),
				# SALES ORDER
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-SO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T7.var_name AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB, T1.dou_fundisdol AS P_DISC, T1.dou_funtaxdol AS P_VAT, T1.dou_funtaxdol1 AS P_WHT, T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_so T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_so_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T7.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T7.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				GROUP BY T1.var_id)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 AS P_SORTKEY, 'AR-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T7.var_name AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB, T1.dou_fundisdol AS P_DISC, T1.dou_funtaxdol AS P_VAT, T1.dou_funtaxdol1 AS P_WHT, T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_fundisc) AS PMT_DISC, SUM(T1.dou_funamt) AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_det T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T7.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T7.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				GROUP BY T1.var_id)")),
				# SALES RETURN
				IF(P30 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 AS P_SORTKEY, 'AR-RET' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T8.var_name AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_funsub AS P_SUB, -T1.dou_fundisdol AS P_DISC, -T1.dou_funtaxdol AS P_VAT, -T1.dou_funtaxdol1 AS P_WHT, -T1.dou_funnet AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Sales Return' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-RET' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T8.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T8.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				GROUP BY T1.var_id)")),
				# CREDIT NOTE
				IF(P31 = "0", "", CONCAT(
				"UNION ALL (SELECT 4 AS P_SORTKEY, 'AR-CN' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T8.var_name AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_funsub AS P_SUB, -T1.dou_fundisdol AS P_DISC, -T1.dou_funtaxdol AS P_VAT, -T1.dou_funtaxdol1 AS P_WHT, -T1.dou_funnet AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_cn T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Credit Note' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_cn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-DN' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T8.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T8.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				GROUP BY T1.var_id)")),
				# PAYMENT
				IF(P32 = "0", "", CONCAT(
				"UNION ALL (SELECT 5 AS P_SORTKEY, 'AR-PY' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, '' AS P_LOC, -T1.dou_funnet AS P_SUB, 0 AS P_DISC, 0 AS P_VAT, 0 AS P_WHT, -dou_funnet AS P_NET, '' AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_py T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_py_det T5) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-PY' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P18, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_docid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_docid <= '", P26, "')
				GROUP BY T1.var_id)")),
				") AS T1", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT T1.* FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE,  '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_DESC, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE LIMIT 0)", 		
				# QUOTATION
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 0 AS P_SORTKEY, 'AR-QUO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T7.var_name AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_quo T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_quo_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T7.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T7.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				GROUP BY T1.var_id)")),
				# SALES ORDER
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-SO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T7.var_name AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_so T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_so_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T7.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T7.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				GROUP BY T1.var_id)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 AS P_SORTKEY, 'AR-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T7.var_name AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc) AS PMT_DISC, SUM(T1.dou_amt) AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_det T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T7.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T7.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				GROUP BY T1.var_id)")),
				# SALES RETURN
				IF(P30 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 AS P_SORTKEY, 'AR-RET' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T8.var_name AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_sub AS P_SUB, -T1.dou_disdol AS P_DISC, -T1.dou_taxdol AS P_VAT, -T1.dou_taxdol1 AS P_WHT, -T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Sales Return' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-RET' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T8.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T8.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				GROUP BY T1.var_id)")),
				# CREDIT NOTE
				IF(P31 = "0", "", CONCAT(
				"UNION ALL (SELECT 4 AS P_SORTKEY, 'AR-CN' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T8.var_name AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_sub AS P_SUB, -T1.dou_disdol AS P_DISC, -T1.dou_taxdol AS P_VAT, -T1.dou_taxdol1 AS P_WHT, -T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_cn T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Credit Note' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_cn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-CN' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T8.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T8.var_groupid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T1.var_empid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T1.var_empid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P16, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_locid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_locid <= '", P18, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T6.var_groupid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T6.var_groupid <= '", P20, "')
				AND
				IF('", P23, "' = 'ALL', TRUE, T5.var_itemid >= '", P23, "') AND IF('", P24, "' = 'ALL', TRUE, T5.var_itemid <= '", P24, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_accid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_accid <= '", P26, "')
				GROUP BY T1.var_id)")),
				# PAYMENT
				IF(P32 = "0", "", CONCAT(
				"UNION ALL (SELECT 5 AS P_SORTKEY, 'AR-PY' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, '' AS P_LOC, -T1.dou_net AS P_SUB, 0 AS P_DISC, 0 AS P_VAT, 0 AS P_WHT, -dou_net AS P_NET, '' AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_py T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_py_det T5) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-PY' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T1.var_claid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T1.var_claid <= '", P18, "')
				AND
				IF('", P25, "' = 'ALL', TRUE, T5.var_docid >= '", P25, "') AND IF('", P26, "' = 'ALL', TRUE, T5.var_docid <= '", P26, "')
				GROUP BY T1.var_id)")),
				") AS T1", @ORDERBY);
		END IF;		
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M002D002_D5`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M002D002_D5`(D1 varchar(100), D2 varchar(100), D3 varchar(100), D4 varchar(100), D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(100), P26 varchar(100), P27 varchar(100), P28 varchar(100), P29 varchar(100), P30 varchar(100), P31 varchar(100), P32 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		DECLARE P_WHERE_APP VARCHAR(2000) DEFAULT "";
		DECLARE P_WHERE_RF VARCHAR(2000) DEFAULT "";
		IF P30 = "1" THEN SET P_WHERE_APP = CONCAT(P_WHERE_APP, "'Sales Return',"); SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AR-RET',"); END IF;
		IF P31 = "1" THEN SET P_WHERE_APP = CONCAT(P_WHERE_APP, "'Credit Note',"); SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AR-CN',"); END IF;
		IF P32 = "1" THEN SET P_WHERE_APP = CONCAT(P_WHERE_APP, "'Prepayment',"); SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AR-PY',"); END IF;
		IF P_WHERE_APP = "" THEN		
				SET P_WHERE_APP = " = TRUE ";
				SET P_WHERE_RF = " = TRUE ";
		ELSE
				SET P_WHERE_APP = SUBSTR(P_WHERE_APP FROM 1 FOR LENGTH(P_WHERE_APP) - 1);
				SET P_WHERE_APP = CONCAT(" IN(", P_WHERE_APP, ") ");

				SET P_WHERE_RF = SUBSTR(P_WHERE_RF FROM 1 FOR LENGTH(P_WHERE_RF) - 1);
				SET P_WHERE_RF = CONCAT(" IN(", P_WHERE_RF, ") ");
		END IF;
		SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_user_role T1 LEFT JOIN ame_user_per_oth T2 ON T1.int_id = T2.int_id WHERE T1.var_id = D1 AND T2.var_modid = 'HCI' AND COALESCE(T2.tin_per1, 0) = 1;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Item/Acc. Id" THEN SET @ORDERBY = " T1.P_ITE ";
		WHEN "Item/Acc. Name" THEN SET @ORDERBY = " T1.P_ITE_NAME ";
		WHEN "Description" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Location" THEN SET @ORDERBY = " T1.P_LOC ";
		WHEN "Class" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
		WHEN "Sub Total" THEN SET @ORDERBY = " T1.P_SUB ";
		WHEN "Discount" THEN SET @ORDERBY = " T1.P_DISC ";
		WHEN "VAT" THEN SET @ORDERBY = " T1.P_TAX1 ";
		WHEN "WHT" THEN SET @ORDERBY = " T1.P_TAX2 ";
		WHEN "Grand Total" THEN SET @ORDERBY = " T1.P_NET ";
		ELSE SET @ORDERBY = " T1.P_LINE ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY T1.P_SORTKEY, ", @ORDERBY, @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.P_SORTKEY, T1.P_MODULE, T1.P_TYPE, T1.P_ID, T1.P_REF, T1.P_DATE, T1.P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_EMP, T1.P_EMP_NAME, T1.P_DESC_1, T1.P_STATUS, T1.P_STATUS_PMT,
				T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_LOC, T1.P_LOC_NAME, T1.P_SUB_1, T1.P_DISC_1, T1.P_TAX1_1, T1.P_TAX2_1, T1.P_NET_1, T1.P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0)  + COALESCE(T3.PMT_APP, 0) + COALESCE(T4.PMT_APP, 0) AS PMT_APP, T1.P_USER, T1.P_CUR, T1.P_RATE
				, T1.P_LINE, T1.P_ITE, T1.P_ITE_NAME, T1.P_DESC, T1.P_CLA, T1.P_CLA_NAME, T1.P_UOM, T1.P_QTY, T1.P_FOC, T1.P_PRICE, T1.P_SUB, T1.P_DISC, T1.P_TAX1, T1.P_TAX2, T1.P_NET FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE, '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(DATE(NOW())) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_DESC_1, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_LOC, '' AS P_LOC_NAME, 0.0 AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, 0.0 AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE
				, 0 AS P_LINE, '' AS 'P_ITE', '' AS P_ITE_NAME, '' AS P_DESC, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_FOC, 0.0 AS P_PRICE, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, 0.0 AS P_NET LIMIT 0)", 
				# QUOTATION
				IF(P27 = "0", "", CONCAT("
				UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-SO' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_quo T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_quo_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				(
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				OR
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				)
				
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES ORDER
				IF(P28 = "0", "", CONCAT("
				UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-SO' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_so T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_so_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				(
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				OR
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				)
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES
				IF(P29 = "0", "", CONCAT("
				UNION ALL (SELECT 2 AS P_SORTKEY, 'AR-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				(
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				OR
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				)
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES RETURN
				IF(P30 = "0", "", CONCAT("
				UNION ALL (SELECT 3 AS P_SORTKEY, 'AR-RET' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_foc AS P_FOC, -T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_funsub) AS P_SUB, -T5.dou_fundisdol AS P_DISC, -T5.dou_funtaxdol AS P_TAX1, -T5.dou_funtaxdol1 AS P_TAX2, -(T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				(
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				OR
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				)
			
				ORDER BY T1.var_id, T5.int_line)")),
				# CREDIT NOTE
				IF(P31 = "0", "", CONCAT("
				UNION ALL (SELECT 4 AS P_SORTKEY, 'AR-CN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_foc AS P_FOC, -T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_funsub) AS P_SUB, -T5.dou_fundisdol AS P_DISC, -T5.dou_funtaxdol AS P_TAX1, -T5.dou_funtaxdol1 AS P_TAX2, -(T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_cn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				(
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				OR
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				)
				
				ORDER BY T1.var_id, T5.int_line)")),
				# PREPAYMENT
				IF(P32 = "0", "", CONCAT("
				UNION ALL (SELECT 5 AS P_SORTKEY, 'AR-PY' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, '' AS P_LOC, '' AS P_LOC_NAME, T1.dou_amt AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, T1.dou_net AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, T5.var_docid AS 'P_ITE', T7.var_name AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_FOC, -T5.dou_funamt AS P_PRICE, -T5.dou_funamt AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, -T5.dou_funamt AS P_NET
				FROM ame_ar_py T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_py_det T5 LEFT JOIN ame_mt_acc T7 ON T5.var_docid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND                                                                             
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				") AS T1 
				LEFT JOIN (SELECT T2.var_cusid, SUM(T1.dou_fundisc) AS PMT_DISC, SUM(T1.dou_funamt) AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_det T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_type = 'Apply to Sale Invoice' AND  T2.var_status = 'Posted' GROUP BY T2.var_cusid) AS T2 ON T1.P_VEN = T2.var_cusid 
				LEFT JOIN (SELECT T2.var_cusid, -SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype ", P_WHERE_APP, " GROUP BY T2.var_cusid) AS T3 ON T1.P_VEN = T3.var_cusid 
				LEFT JOIN (SELECT T1.var_perid, -SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype ", P_WHERE_RF, " GROUP BY T1.var_perid) AS T4 ON T1.P_VEN = T4.var_perid
				", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT T1.P_SORTKEY, T1.P_MODULE, T1.P_TYPE, T1.P_ID, T1.P_REF, T1.P_DATE, T1.P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_EMP, T1.P_EMP_NAME, T1.P_DESC_1, T1.P_STATUS, T1.P_STATUS_PMT,
				T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_LOC, T1.P_LOC_NAME, T1.P_SUB_1, T1.P_DISC_1, T1.P_TAX1_1, T1.P_TAX2_1, T1.P_NET_1, T1.P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0)  + COALESCE(T3.PMT_APP, 0) + COALESCE(T4.PMT_APP, 0) AS PMT_APP, T1.P_USER, T1.P_CUR, T1.P_RATE
				, T1.P_LINE, T1.P_ITE, T1.P_ITE_NAME, T1.P_DESC, T1.P_CLA, T1.P_CLA_NAME, T1.P_UOM, T1.P_QTY, T1.P_FOC, T1.P_PRICE, T1.P_SUB, T1.P_DISC, T1.P_TAX1, T1.P_TAX2, T1.P_NET FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE, '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(DATE(NOW())) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_DESC_1, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_LOC, '' AS P_LOC_NAME, 0.0 AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, 0.0 AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE
				, 0 AS P_LINE, '' AS 'P_ITE', '' AS P_ITE_NAME, '' AS P_DESC, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_FOC, 0.0 AS P_PRICE, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, 0.0 AS P_NET LIMIT 0)", 
				# QUOTATION
				IF(P27 = "0", "", CONCAT("
				UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-SO' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_sub) AS P_SUB, T5.dou_disdol AS P_DISC, T5.dou_taxdol AS P_TAX1, T5.dou_taxdol1 AS P_TAX2, (T5.dou_sub - T5.dou_disdol + T5.dou_taxdol + T5.dou_taxdol1) AS P_NET
				FROM ame_ar_quo T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_quo_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES ORDER
				IF(P28 = "0", "", CONCAT("
				UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-SO' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_sub) AS P_SUB, T5.dou_disdol AS P_DISC, T5.dou_taxdol AS P_TAX1, T5.dou_taxdol1 AS P_TAX2, (T5.dou_sub - T5.dou_disdol + T5.dou_taxdol + T5.dou_taxdol1) AS P_NET
				FROM ame_ar_so T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_so_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES
				IF(P29 = "0", "", CONCAT("
				UNION ALL (SELECT 2 AS P_SORTKEY, 'AR-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_sub) AS P_SUB, T5.dou_disdol AS P_DISC, T5.dou_taxdol AS P_TAX1, T5.dou_taxdol1 AS P_TAX2, (T5.dou_sub - T5.dou_disdol + T5.dou_taxdol + T5.dou_taxdol1) AS P_NET
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, COALESCE(T5.var_locid, '') <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES RETURN
				IF(P30 = "0", "", CONCAT("
				UNION ALL (SELECT 3 AS P_SORTKEY, 'AR-RET' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_foc AS P_FOC, -T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_sub) AS P_SUB, -T5.dou_disdol AS P_DISC, -T5.dou_taxdol AS P_TAX1, -T5.dou_taxdol1 AS P_TAX2, -(T5.dou_sub - T5.dou_disdol + T5.dou_taxdol + T5.dou_taxdol1) AS P_NET
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				
				ORDER BY T1.var_id, T5.int_line)")),
				# CREDIT NOTE
				IF(P31 = "0", "", CONCAT("
				UNION ALL (SELECT 4 AS P_SORTKEY, 'AR-CN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T10.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_foc AS P_FOC, -T5.dou_price AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_sub) AS P_SUB, -T5.dou_disdol AS P_DISC, -T5.dou_taxdol AS P_TAX1, -T5.dou_taxdol1 AS P_TAX2, -(T5.dou_sub - T5.dou_disdol + T5.dou_taxdol + T5.dou_taxdol1) AS P_NET
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_cn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T10 ON T1.var_empid = T10.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				
				ORDER BY T1.var_id, T5.int_line)")),
				# PREPAYMENT
				IF(P32 = "0", "", CONCAT("
				UNION ALL (SELECT 5 AS P_SORTKEY, 'AR-PY' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, '' AS P_LOC, '' AS P_LOC_NAME, T1.dou_amt AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, T1.dou_net AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, T5.var_docid AS 'P_ITE', T7.var_name AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_FOC, -T5.dou_amt AS P_PRICE, -T5.dou_amt AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, -T5.dou_amt AS P_NET
				FROM ame_ar_py T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_py_det T5 LEFT JOIN ame_mt_acc T7 ON T5.var_docid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				") AS T1 
				LEFT JOIN (SELECT T2.var_cusid, SUM(T1.dou_disc) AS PMT_DISC, SUM(T1.dou_amt) AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_det T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_type = 'Apply to Sale Invoice' AND  T2.var_status = 'Posted' GROUP BY T2.var_cusid) AS T2 ON T1.P_VEN = T2.var_cusid 
				LEFT JOIN (SELECT T2.var_cusid, -SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype ", P_WHERE_APP, " GROUP BY T2.var_cusid) AS T3 ON T1.P_VEN = T3.var_cusid 
				LEFT JOIN (SELECT T1.var_perid, -SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype ", P_WHERE_RF, " GROUP BY T1.var_perid) AS T4 ON T1.P_VEN = T4.var_perid
				", @ORDERBY);
		END IF;	
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M002D002_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M002D002_M1`(D1 varchar(100), D2 varchar(100), D3 varchar(100), D4 varchar(100), D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(100), P26 varchar(100), P27 varchar(100), P28 varchar(100), P29 varchar(100), P30 varchar(100), P31 varchar(100), P32 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_user_role T1 LEFT JOIN ame_user_per_oth T2 ON T1.int_id = T2.int_id WHERE T1.var_id = D1 AND T2.var_modid = 'HCI' AND COALESCE(T2.tin_per1, 0) = 1;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_VEN ";
		WHEN "Type" THEN SET @ORDERBY = " T1.P_TYPE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
		WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
		WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Location" THEN SET @ORDERBY = " T1.P_LOC ";
		WHEN "Class Id" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
		WHEN "Term" THEN SET @ORDERBY = " T1.P_TERM ";
		WHEN "Due Date" THEN SET @ORDERBY = " T1.P_DATE_DUE ";
		WHEN "Status" THEN SET @ORDERBY = " T1.P_STATUS ";
		WHEN "Payment Status" THEN SET @ORDERBY = " T1.P_STATUS_PMT ";
		WHEN "User" THEN SET @ORDERBY = " T1.P_USER ";
		WHEN "Sub Total" THEN SET @ORDERBY = " T1.P_SUB ";
		WHEN "Discount" THEN SET @ORDERBY = " T1.P_DISC ";
		WHEN "VAT" THEN SET @ORDERBY = " T1.P_VAT ";
		WHEN "Grand Total" THEN SET @ORDERBY = " T1.P_NET ";
		WHEN "Applied PMT" THEN SET @ORDERBY = " T1.PMT_APP ";
		WHEN "Discount PMT" THEN SET @ORDERBY = " T1.PMT_DISC ";
		WHEN "Cash PMT" THEN SET @ORDERBY = " T1.PMT_AMT ";
		ELSE SET @ORDERBY = " T1.P_ID ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY T1.P_SORTKEY, ", @ORDERBY, @SORT, ", T1.P_ID ", @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.P_SORTKEY, T1.P_MODULE,  T1.P_TYPE, T1.P_ID, T1.P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_EMP, T1.P_EMP_NAME, T1.P_DESC, T1.P_STATUS, T1.P_STATUS_PMT,
				T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, SUM(T1.P_SUB) AS P_SUB, SUM(T1.P_DISC) AS P_DISC, SUM(T1.P_VAT) AS P_VAT, SUM(T1.P_WHT) AS P_WHT, SUM(T1.P_NET) AS P_NET, T1.P_TERM,
				SUM(T1.PMT_DISC) AS PMT_DISC, SUM(T1.PMT_AMT) AS PMT_AMT, SUM(T1.PMT_APP) AS PMT_APP, T1.P_USER, T1.P_CUR, T1.P_RATE FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE,  '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_DESC, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE LIMIT 0)", 		
				# QUOTATION
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 0 AS P_SORTKEY, 'AR-QUO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T7.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB, T1.dou_fundisdol AS P_DISC, T1.dou_funtaxdol AS P_VAT, T1.dou_funtaxdol1 AS P_WHT, T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_quo T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_quo_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# SALES ORDER
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-SO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T7.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB, T1.dou_fundisdol AS P_DISC, T1.dou_funtaxdol AS P_VAT, T1.dou_funtaxdol1 AS P_WHT, T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_so T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_so_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T7.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB, T1.dou_fundisdol AS P_DISC, T1.dou_funtaxdol AS P_VAT, T1.dou_funtaxdol1 AS P_WHT, T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_fundisc) AS PMT_DISC, SUM(T1.dou_funamt) AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_det T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# SALES RETURN
				IF(P30 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 AS P_SORTKEY, 'AP-RET' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T8.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_funsub AS P_SUB, -T1.dou_fundisdol AS P_DISC, -T1.dou_funtaxdol AS P_VAT, -T1.dou_funtaxdol1 AS P_WHT, -T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Sales Return' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-RET' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# CEDITE NOTE
				IF(P31 = "0", "", CONCAT(
				"UNION ALL (SELECT 4 AS P_SORTKEY, 'AP-DN' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T8.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_funsub AS P_SUB, -T1.dou_fundisdol AS P_DISC, -T1.dou_funtaxdol AS P_VAT, -T1.dou_funtaxdol1 AS P_WHT, -T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_cn T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Credit Note' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_cn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-CN' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PREPAYMENT
				IF(P32 = "0", "", CONCAT(
				"UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, '' AS P_LOC, -T1.dou_funnet AS P_SUB, 0 AS P_DISC, 0 AS P_VAT, 0 AS P_WHT, -dou_funnet AS P_NET, '' AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_py T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_py_det T5) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-PY' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				GROUP BY T1.var_id)")),
				") AS T1 GROUP BY T1.P_VEN ", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT T1.P_SORTKEY, T1.P_MODULE,  T1.P_TYPE, T1.P_ID, T1.P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_EMP, T1.P_EMP_NAME, T1.P_DESC, T1.P_STATUS, T1.P_STATUS_PMT,
				T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, SUM(T1.P_SUB) AS P_SUB, SUM(T1.P_DISC) AS P_DISC, SUM(T1.P_VAT) AS P_VAT, SUM(T1.P_WHT) AS P_WHT, SUM(T1.P_NET) AS P_NET, T1.P_TERM,
				SUM(T1.PMT_DISC) AS PMT_DISC, SUM(T1.PMT_AMT) AS PMT_AMT, SUM(T1.PMT_APP) AS PMT_APP, T1.P_USER, T1.P_CUR, T1.P_RATE FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE,  '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_DESC, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE LIMIT 0)", 		
				# QUOTATION
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 0 AS P_SORTKEY, 'AR-QUO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T7.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_quo T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_quo_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# SALES ORDER
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-SO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T7.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_so T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_so_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				
				GROUP BY T1.var_id)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T7.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc) AS PMT_DISC, SUM(T1.dou_amt) AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_det T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T7 ON T1.var_empid = T7.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')

				GROUP BY T1.var_id)")),
				# SALES RETURN
				IF(P30 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 AS P_SORTKEY, 'AR-RET' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T8.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_sub AS P_SUB, -T1.dou_disdol AS P_DISC, -T1.dou_taxdol AS P_VAT, -T1.dou_taxdol1 AS P_WHT, -T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Sales Return' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-RET' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# CEDITE NOTE
				IF(P31 = "0", "", CONCAT(
				"UNION ALL (SELECT 4 AS P_SORTKEY, 'AR-CN' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T8.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_sub AS P_SUB, -T1.dou_disdol AS P_DISC, -T1.dou_taxdol AS P_VAT, -T1.dou_taxdol1 AS P_WHT, -T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_cn T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Credit Note' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_cn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-CN' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PAYMENT
				IF(P32 = "0", "", CONCAT(
				"UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, '' AS P_LOC, -T1.dou_net AS P_SUB, 0 AS P_DISC, 0 AS P_VAT, 0 AS P_WHT, -dou_net AS P_NET, '' AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ar_py T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_py_det T5) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AR-PY' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				GROUP BY T1.var_id)")),
				") AS T1 GROUP BY T1.P_VEN ", @ORDERBY);
		END IF;		
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M002D002_M3`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M002D002_M3`(D1 varchar(100), D2 varchar(100), D3 varchar(100), D4 varchar(100), D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(100), P26 varchar(100), P27 varchar(100), P28 varchar(100), P29 varchar(100), P30 varchar(100), P31 varchar(100), P32 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		DECLARE P_WHERE_APP VARCHAR(2000) DEFAULT "";
		DECLARE P_WHERE_RF VARCHAR(2000) DEFAULT "";
		IF P30 = "1" THEN SET P_WHERE_APP = CONCAT(P_WHERE_APP, "'Sales Return',"); SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AR-RET',"); END IF;
		IF P31 = "1" THEN SET P_WHERE_APP = CONCAT(P_WHERE_APP, "'Credit Note',"); SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AR-CN',"); END IF;
		IF P32 = "1" THEN SET P_WHERE_APP = CONCAT(P_WHERE_APP, "'Prepayment',"); SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AR-PY',"); END IF;
		IF P_WHERE_APP = "" THEN		
				SET P_WHERE_APP = " = TRUE ";
				SET P_WHERE_RF = " = TRUE ";
		ELSE
				SET P_WHERE_APP = SUBSTR(P_WHERE_APP FROM 1 FOR LENGTH(P_WHERE_APP) - 1);
				SET P_WHERE_APP = CONCAT(" IN(", P_WHERE_APP, ") ");

				SET P_WHERE_RF = SUBSTR(P_WHERE_RF FROM 1 FOR LENGTH(P_WHERE_RF) - 1);
				SET P_WHERE_RF = CONCAT(" IN(", P_WHERE_RF, ") ");
		END IF;
		SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_user_role T1 LEFT JOIN ame_user_per_oth T2 ON T1.int_id = T2.int_id WHERE T1.var_id = D1 AND T2.var_modid = 'HCI' AND COALESCE(T2.tin_per1, 0) = 1;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Item/Acc. Id" THEN SET @ORDERBY = " T1.P_ITE ";
		WHEN "Item/Acc. Name" THEN SET @ORDERBY = " T1.P_ITE_NAME ";
		WHEN "Description" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Location" THEN SET @ORDERBY = " T1.P_LOC ";
		WHEN "Class" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
		WHEN "Sub Total" THEN SET @ORDERBY = " T1.P_SUB ";
		WHEN "Discount" THEN SET @ORDERBY = " T1.P_DISC ";
		WHEN "VAT" THEN SET @ORDERBY = " T1.P_TAX1 ";
		WHEN "WHT" THEN SET @ORDERBY = " T1.P_TAX2 ";
		WHEN "Grand Total" THEN SET @ORDERBY = " T1.P_NET ";
		ELSE SET @ORDERBY = " T1.P_LINE ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY T1.P_SORTKEY, ", @ORDERBY, @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.P_SORTKEY, T1.P_MODULE, T1.P_TYPE, T1.P_ID, T1.P_REF, T1.P_DATE, T1.P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_EMP, T1.P_EMP_NAME, T1.P_DESC_1, T1.P_STATUS, T1.P_STATUS_PMT,
				T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_LOC, T1.P_SUB_1, T1.P_DISC_1, T1.P_TAX1_1, T1.P_TAX2_1, T1.P_NET_1, T1.P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0)  + COALESCE(T3.PMT_APP, 0) + COALESCE(T4.PMT_APP, 0) AS PMT_APP, T1.P_USER, T1.P_CUR, T1.P_RATE
				, T1.P_LINE, T1.P_ITE, T1.P_ITE_NAME, T1.P_DESC, T1.P_CLA, T1.P_CLA_NAME, T1.P_UOM, SUM(T1.P_QTY) AS P_QTY, SUM(T1.P_FOC) AS P_FOC, SUM(T1.P_PRICE) AS P_PRICE, SUM(T1.P_SUB) AS P_SUB, SUM(T1.P_DISC) AS P_DISC, SUM(T1.P_TAX1) AS P_TAX1, SUM(T1.P_TAX2) AS P_TAX2, SUM(T1.P_NET) AS P_NET FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE, '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(DATE(NOW())) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_DESC_1, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_LOC, 0.0 AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, 0.0 AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE
				, 0 AS P_LINE, '' AS 'P_ITE', '' AS P_ITE_NAME, '' AS P_DESC, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_FOC, 0.0 AS P_PRICE, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, 0.0 AS P_NET LIMIT 0)", 
				# QUOTATION
				IF(P27 = "0", "", CONCAT("
				UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-QUO' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T9.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_quo T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_quo_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T9 ON T1.var_empid = T9.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES ORDER
				IF(P28 = "0", "", CONCAT("
				UNION ALL (SELECT 1 AS P_SORTKEY, 'AR-SO' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T9.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_so T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_so_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T9 ON T1.var_empid = T9.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES
				IF(P29 = "0", "", CONCAT("
				UNION ALL (SELECT 2 AS P_SORTKEY, 'AR-IN' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T9.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_foc AS P_FOC, T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, T5.dou_funsub) AS P_SUB, T5.dou_fundisdol AS P_DISC, T5.dou_funtaxdol AS P_TAX1, T5.dou_funtaxdol1 AS P_TAX2, (T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T9 ON T1.var_empid = T9.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				
				ORDER BY T1.var_id, T5.int_line)")),
				# SALES RETURN
				IF(P30 = "0", "", CONCAT("
				UNION ALL (SELECT 3 AS P_SORTKEY, 'AR-RET' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T9.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_foc AS P_FOC, -T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_funsub) AS P_SUB, -T5.dou_fundisdol AS P_DISC, -T5.dou_funtaxdol AS P_TAX1, -T5.dou_funtaxdol1 AS P_TAX2, -(T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T9 ON T1.var_empid = T9.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				
				ORDER BY T1.var_id, T5.int_line)")),
				# CREDIT NOTE
				IF(P31 = "0", "", CONCAT("
				UNION ALL (SELECT 4 AS P_SORTKEY, 'AR-CN' AS P_MODULE, IF(COALESCE(T5.var_itemid, '') = '', 'Account', 'Item') AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, COALESCE(T9.var_name, '') AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB_1, T1.dou_disdol AS P_DISC_1, T1.dou_taxdol AS P_TAX1_1, T1.dou_taxdol1 AS P_TAX2_1, T1.dou_net AS P_NET_1, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, T5.var_uomid AS P_UOM, -T5.dou_qty AS P_QTY, -T5.dou_foc AS P_FOC, -T5.dou_funprice AS P_PRICE, IF(", P_ROW, " > 0, 0, -T5.dou_funsub) AS P_SUB, -T5.dou_fundisdol AS P_DISC, -T5.dou_funtaxdol AS P_TAX1, -T5.dou_funtaxdol1 AS P_TAX2, -(T5.dou_funsub - T5.dou_fundisdol + T5.dou_funtaxdol + T5.dou_funtaxdol1) AS P_NET
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_cn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN ame_mt_emp T9 ON T1.var_empid = T9.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				
				ORDER BY T1.var_id, T5.int_line)")),
				# PREPAYMENT
				IF(P32 = "0", "", CONCAT("
				UNION ALL (SELECT 5 AS P_SORTKEY, 'AR-PY' AS P_MODULE, 'Account' AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_cusid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, '' AS P_EMP, '' AS P_EMP_NAME, T1.var_desc AS P_DESC_1, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA_1, COALESCE(T4.var_name, '') AS P_CLA_NAME_1, '' AS P_LOC, T1.dou_amt AS P_SUB_1, 0.0 AS P_DISC_1, 0.0 AS P_TAX1_1, 0.0 AS P_TAX2_1, T1.dou_net AS P_NET_1, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				, T5.int_line AS P_LINE, T5.var_docid AS 'P_ITE', T7.var_name AS P_ITE_NAME, T5.var_desc AS P_DESC, T5.var_claid AS P_CLA, COALESCE(T8.var_name, '') AS P_CLA_NAME, '' AS P_UOM, 0.0 AS P_QTY, 0.0 AS P_FOC, -T5.dou_funamt AS P_PRICE, -T5.dou_funamt AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_TAX1, 0.0 AS P_TAX2, -T5.dou_funamt AS P_NET
				FROM ame_ar_py T1
				LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ar_py_det T5 LEFT JOIN ame_mt_acc T7 ON T5.var_docid = T7.var_id LEFT JOIN ame_mt_cla T8 ON T5.var_claid = T8.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_cusid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_cusid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				ORDER BY T1.var_id, T5.int_line)")),
				") AS T1 
				LEFT JOIN (SELECT T2.var_cusid, SUM(T1.dou_fundisc) AS PMT_DISC, SUM(T1.dou_funamt) AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_det T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_type = 'Apply to Sale Invoice' AND  T2.var_status = 'Posted' GROUP BY T2.var_cusid) AS T2 ON T1.P_VEN = T2.var_cusid 
				LEFT JOIN (SELECT T2.var_cusid, -SUM(T1.dou_funapp) AS PMT_APP FROM ame_ar_py_app T1 LEFT JOIN  ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype ", P_WHERE_APP, " GROUP BY T2.var_cusid) AS T3 ON T1.P_VEN = T3.var_cusid 
				LEFT JOIN (SELECT T1.var_perid, -SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype ", P_WHERE_RF, " GROUP BY T1.var_perid) AS T4 ON T1.P_VEN = T4.var_perid
				GROUP BY T1.P_VEN, T1.P_ITE", @ORDERBY);
		ELSE
