				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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, SUM(T5.dou_sub) AS P_SUB, SUM(T5.dou_disdol) AS P_DISC, SUM(T5.dou_taxdol) AS P_VAT, SUM(T5.dou_taxdol1) AS P_WHT, SUM(T5.dou_net) AS P_NET, 
				SUM(T5.dou_uc) / T1.dou_rate AS P_UC, SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5)  / T1.dou_rate AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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)")),
				") 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_M002D010_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M002D010_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))
    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.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_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, SUM(T5.dou_funsub) AS P_SUB, SUM(T5.dou_fundisdol) AS P_DISC, SUM(T5.dou_funtaxdol) AS P_VAT, SUM(T5.dou_funtaxdol1) AS P_WHT, SUM(T5.dou_funnet) AS P_NET, 
				SUM(T5.dou_uc) AS P_UC, SUM(T5.dou_ac1) AS P_AC1, SUM(T5.dou_ac2) AS P_AC2, SUM(T5.dou_ac3) AS P_AC3, SUM(T5.dou_ac4) AS P_AC4, SUM(T5.dou_ac5) AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid)")),
				") 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, T1.P_DATE, T1.P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_EMP, T1.P_EMP_NAME, T1.P_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, SUM(T5.dou_sub) AS P_SUB, SUM(T5.dou_disdol) AS P_DISC, SUM(T5.dou_taxdol) AS P_VAT, SUM(T5.dou_taxdol1) AS P_WHT, SUM(T5.dou_net) AS P_NET, 
				SUM(T5.dou_uc) / T1.dou_rate AS P_UC, SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5)  / T1.dou_rate AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid)")),
				") 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_M002D010_M3`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M002D010_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))
    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_ITE ";
		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_EMP, T1.P_EMP_NAME, T1.P_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, T5.dou_funprice AS P_PRICE, SUM(T5.dou_funsub) AS P_SUB, SUM(T5.dou_fundisdol) AS P_DISC, SUM(T5.dou_funtaxdol) AS P_VAT, SUM(T5.dou_funtaxdol1) AS P_WHT, SUM(T5.dou_funnet) AS P_NET, 
				SUM(T5.dou_uc) AS P_UC, SUM(T5.dou_ac1) AS P_AC1, SUM(T5.dou_ac2) AS P_AC2, SUM(T5.dou_ac3) AS P_AC3, SUM(T5.dou_ac4) AS P_AC4, SUM(T5.dou_ac5) AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid, T5.var_itemid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_funprice AS P_PRICE, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid, T5.var_itemid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_funprice AS P_PRICE, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid, T5.var_itemid)")),
				") AS T1 GROUP BY T1.P_VEN, 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_EMP, T1.P_EMP_NAME, T1.P_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, T5.dou_price AS P_PRICE, SUM(T5.dou_sub) AS P_SUB, SUM(T5.dou_disdol) AS P_DISC, SUM(T5.dou_taxdol) AS P_VAT, SUM(T5.dou_taxdol1) AS P_WHT, SUM(T5.dou_net) AS P_NET, 
				SUM(T5.dou_uc) / T1.dou_rate AS P_UC, SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5)  / T1.dou_rate AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid, T5.var_itemid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_price AS P_PRICE, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid, T5.var_itemid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_price AS P_PRICE, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_cusid, T5.var_itemid)")),
				") AS T1 GROUP BY T1.P_VEN, 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_M002D011_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M002D011_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))
    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.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_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, SUM(T5.dou_funsub) AS P_SUB, SUM(T5.dou_fundisdol) AS P_DISC, SUM(T5.dou_funtaxdol) AS P_VAT, SUM(T5.dou_funtaxdol1) AS P_WHT, SUM(T5.dou_funnet) AS P_NET, 
				SUM(T5.dou_uc) AS P_UC, SUM(T5.dou_ac1) AS P_AC1, SUM(T5.dou_ac2) AS P_AC2, SUM(T5.dou_ac3) AS P_AC3, SUM(T5.dou_ac4) AS P_AC4, SUM(T5.dou_ac5) AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid)")),
				") AS T1 GROUP BY T1.P_EMP", @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_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, SUM(T5.dou_sub) AS P_SUB, SUM(T5.dou_disdol) AS P_DISC, SUM(T5.dou_taxdol) AS P_VAT, SUM(T5.dou_taxdol1) AS P_WHT, SUM(T5.dou_net) AS P_NET, 
				SUM(T5.dou_uc) / T1.dou_rate AS P_UC, SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5)  / T1.dou_rate AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid)")),
				") AS T1 GROUP BY T1.P_EMP", @ORDERBY);
		END IF;		
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M002D011_M3`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M002D011_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))
    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_ITE ";
		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_EMP, T1.P_EMP_NAME, T1.P_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, T5.dou_funprice AS P_PRICE, SUM(T5.dou_funsub) AS P_SUB, SUM(T5.dou_fundisdol) AS P_DISC, SUM(T5.dou_funtaxdol) AS P_VAT, SUM(T5.dou_funtaxdol1) AS P_WHT, SUM(T5.dou_funnet) AS P_NET, 
				SUM(T5.dou_uc) AS P_UC, SUM(T5.dou_ac1) AS P_AC1, SUM(T5.dou_ac2) AS P_AC2, SUM(T5.dou_ac3) AS P_AC3, SUM(T5.dou_ac4) AS P_AC4, SUM(T5.dou_ac5) AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid, T5.var_itemid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_funprice AS P_PRICE, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid, T5.var_itemid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_funprice AS P_PRICE, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid, T5.var_itemid)")),
				") AS T1 GROUP BY T1.P_EMP, 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_EMP, T1.P_EMP_NAME, T1.P_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, T5.dou_price AS P_PRICE, SUM(T5.dou_sub) AS P_SUB, SUM(T5.dou_disdol) AS P_DISC, SUM(T5.dou_taxdol) AS P_VAT, SUM(T5.dou_taxdol1) AS P_WHT, SUM(T5.dou_net) AS P_NET, 
				SUM(T5.dou_uc) / T1.dou_rate AS P_UC, SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5)  / T1.dou_rate AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid, T5.var_itemid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_price AS P_PRICE, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid, T5.var_itemid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T5.var_locid AS P_LOC, T9.var_name AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_price AS P_PRICE, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid, T5.var_itemid)")),
				") AS T1 GROUP BY T1.P_EMP, 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_M002D012_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M002D012_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))
    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.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_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, SUM(T5.dou_funsub) AS P_SUB, SUM(T5.dou_fundisdol) AS P_DISC, SUM(T5.dou_funtaxdol) AS P_VAT, SUM(T5.dou_funtaxdol1) AS P_WHT, SUM(T5.dou_funnet) AS P_NET, 
				SUM(T5.dou_uc) AS P_UC, SUM(T5.dou_ac1) AS P_AC1, SUM(T5.dou_ac2) AS P_AC2, SUM(T5.dou_ac3) AS P_AC3, SUM(T5.dou_ac4) AS P_AC4, SUM(T5.dou_ac5) AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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 T5.var_locid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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 T5.var_locid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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 T5.var_locid)")),
				") AS T1 GROUP BY 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_EMP, T1.P_EMP_NAME, T1.P_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, SUM(T5.dou_sub) AS P_SUB, SUM(T5.dou_disdol) AS P_DISC, SUM(T5.dou_taxdol) AS P_VAT, SUM(T5.dou_taxdol1) AS P_WHT, SUM(T5.dou_net) AS P_NET, 
				SUM(T5.dou_uc) / T1.dou_rate AS P_UC, SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5)  / T1.dou_rate AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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 T5.var_locid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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 T5.var_locid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_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, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = 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, "' = '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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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 T5.var_locid)")),
				") AS T1 GROUP BY 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_M002D013_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M002D013_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))
    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_ITE ";
		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_EMP, T1.P_EMP_NAME, T1.P_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, T5.dou_funprice AS P_PRICE, SUM(T5.dou_funsub) AS P_SUB, SUM(T5.dou_fundisdol) AS P_DISC, SUM(T5.dou_funtaxdol) AS P_VAT, SUM(T5.dou_funtaxdol1) AS P_WHT, SUM(T5.dou_funnet) AS P_NET, 
				SUM(T5.dou_uc) AS P_UC, SUM(T5.dou_ac1) AS P_AC1, SUM(T5.dou_ac2) AS P_AC2, SUM(T5.dou_ac3) AS P_AC3, SUM(T5.dou_ac4) AS P_AC4, SUM(T5.dou_ac5) AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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 T5.var_itemid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_funprice AS P_PRICE, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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 T5.var_itemid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_funprice AS P_PRICE, -SUM(T5.dou_funsub) AS P_SUB, -SUM(T5.dou_fundisdol) AS P_DISC, -SUM(T5.dou_funtaxdol) AS P_VAT, -SUM(T5.dou_funtaxdol1) AS P_WHT, -SUM(T5.dou_funnet) AS P_NET, 
				-SUM(T5.dou_uc) AS P_UC, -SUM(T5.dou_ac1) AS P_AC1, -SUM(T5.dou_ac2) AS P_AC2, -SUM(T5.dou_ac3) AS P_AC3, -SUM(T5.dou_ac4) AS P_AC4, -SUM(T5.dou_ac5) AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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_empid, T5.var_itemid)")),
				") 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_EMP, T1.P_EMP_NAME, T1.P_STATUS, T1.P_STATUS_PMT, T1.P_TERM, T1.P_USER, T1.P_CUR, T1.P_RATE ,
				T1.P_DESC_1, T1.P_DESC, T1.P_CLA_1, T1.P_CLA_NAME_1, T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, T1.P_LOC_NAME, T1.P_ITE, T1.P_ITE_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_TAX_1) AS P_TAX_1, SUM(T1.P_TAX_2) AS P_TAX_2, SUM(T1.P_NET) AS P_NET, 
				SUM(T1.P_UC) AS P_UC, SUM(T1.P_AC1) AS P_AC1, SUM(T1.P_AC2) AS P_AC2, SUM(T1.P_AC3) AS P_AC3, SUM(T1.P_AC4) AS P_AC4, SUM(T1.P_AC5) AS P_AC5, SUM(T1.P_TC) AS P_TC 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_STATUS, '' AS P_STATUS_PMT, '' AS P_TERM, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE ,
				'' AS P_DESC_1, '' AS P_DESC, '' AS P_CLA_1, '' AS P_CLA_NAME_1, '' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, '' AS P_LOC_NAME, '' AS 'P_ITE', '' AS P_ITE_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_TAX_1, 0.0 AS P_TAX_2, 0.0 AS P_NET, 
				0.0 AS P_UC, 0.0 AS P_AC1, 0.0 AS P_AC2, 0.0 AS P_AC3, 0.0 AS P_AC4, 0.0 AS P_AC5, 0.0 P_TC LIMIT 0)", 		
				# SALES
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, T5.dou_price AS P_PRICE, SUM(T5.dou_sub) AS P_SUB, SUM(T5.dou_disdol) AS P_DISC, SUM(T5.dou_taxdol) AS P_VAT, SUM(T5.dou_taxdol1) AS P_WHT, SUM(T5.dou_net) AS P_NET, 
				SUM(T5.dou_uc) / T1.dou_rate AS P_UC, SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5)  / T1.dou_rate AS P_TC
				FROM ame_ar_in T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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  T5.var_itemid)")),
				# SALES RETURN
				IF(P28 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_price AS P_PRICE, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_ret T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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 T5.var_itemid)")),
				# SALES
				IF(P29 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 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(T2.var_name, '') AS P_VEN_NAME, T1.var_empid AS P_EMP, T3.var_name AS P_EMP_NAME, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT, T1.var_term AS P_TERM, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE,
				T1.var_desc AS P_DESC_1, T5.var_desc AS P_DESC, T1.var_claid AS P_CLA_1, T4.var_name AS P_CLA_NAME_1, T5.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, COALESCE(T5.var_locid, '') AS P_LOC, COALESCE(T9.var_name, '') AS P_LOC_NAME, IF(COALESCE(T5.var_itemid, '') <> '', T5.var_itemid, T5.var_accid) AS P_ITE, IF(COALESCE(T5.var_itemid, '') <> '', T6.var_name, T7.var_name) AS P_ITE_NAME, T6.var_uomid AS P_UOM, 
				-SUM(T5.dou_qty * T10.dou_conv) AS P_QTY, -SUM(T5.dou_foc * T10.dou_conv) AS P_FOC, -T5.dou_price AS P_PRICE, -SUM(T5.dou_sub) AS P_SUB, -SUM(T5.dou_disdol) AS P_DISC, -SUM(T5.dou_taxdol) AS P_VAT, -SUM(T5.dou_taxdol1) AS P_WHT, -SUM(T5.dou_net) AS P_NET, 
				-SUM(T5.dou_uc) / T1.dou_rate AS P_UC, -SUM(T5.dou_ac1) / T1.dou_rate AS P_AC1, -SUM(T5.dou_ac2) / T1.dou_rate AS P_AC2, -SUM(T5.dou_ac3) / T1.dou_rate AS P_AC3, -SUM(T5.dou_ac4) / T1.dou_rate AS P_AC4, -SUM(T5.dou_ac5) / T1.dou_rate AS P_AC5, -SUM(T5.dou_uc + T5.dou_ac1 + T5.dou_ac2 + T5.dou_ac3 + T5.dou_ac4 + T5.dou_ac5) / T1.dou_rate AS P_TC
				FROM ame_ar_cn T1
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id
				LEFT JOIN ame_mt_emp T3 ON T1.var_empid = 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_locid = T8.var_id
				LEFT JOIN ame_mt_loc T9 ON T5.var_locid = T9.var_id 
				LEFT JOIN ame_mt_ite_uom T10 ON (T5.var_itemid = T10.var_id AND T5.var_uomid = T10.var_uomid)
				) ON T1.var_id = 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', 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, T2.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T2.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, T3.var_groupid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T3.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 T5.var_itemid)")),
				") 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_M003D001_D1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D001_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))
    DETERMINISTIC
BEGIN
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY SortOrder, int_id, int_line, locid, itemid, doc_date ";
	SET @OPENNING = "";
	SET @DETAIL = CONCAT( 
	"SELECT
	1 AS SortOrder, int_id, int_line,
	COALESCE(ic.var_locid,'') AS  'locid',
	COALESCE(l.var_name,'') AS  'locname',
	COALESCE(i.var_groupid,'') AS  'itemgroupid',
	COALESCE(ig.var_name,'') AS  'itemgroupname',
	COALESCE(ic.var_itemid,'') AS  'itemid',
	COALESCE(i.var_name,'') AS  'itemname',
	COALESCE(i.var_uomid,'') AS  'uomid',
	COALESCE(ic.int_batchno,0) AS  'batchno',
	DATE(ic.dat_date) AS 'doc_date',
	COALESCE(ic.dou_qty,0) AS  'qty',
	COALESCE(ic.dou_uc,0) AS  'cost',
	COALESCE(ic.dou_ac1,0) AS  'ac1',
	COALESCE(ic.dou_ac2,0) AS  'ac2',
	COALESCE(ic.dou_ac3,0) AS  'ac3',
	COALESCE(ic.dou_ac4,0) AS  'ac4',
	COALESCE(ic.dou_ac5,0) AS  'ac5',
	COALESCE(ic.dou_uc,0) + COALESCE(ic.dou_ac1,0) + COALESCE(ic.dou_ac2,0) + COALESCE(ic.dou_ac3,0) + COALESCE(ic.dou_ac4,0) + COALESCE(ic.dou_ac5,0) AS 'total_cost',
	COALESCE(ic.var_modid,'') AS  'modid',
	COALESCE(ic.var_doctype,'') AS  'doctype',
	COALESCE(ic.var_docid,'') AS  'docid',
	COALESCE(ic.var_venid,'') AS  'venid',
	COALESCE(v.var_name,'') AS  'venname',
	COALESCE(ic.var_cusid,'') AS  'cusid',
	COALESCE(c.var_name,'') AS  'cusname',
	COALESCE(ic.var_empid,'') AS  'empid',
	COALESCE(e.var_name,'') AS  'empname',
	COALESCE(ic.var_claid,'') AS  'claid',
	COALESCE(cl.var_name,'') AS  'claname',
	COALESCE(ic.var_ref,'') AS  'ref',
	COALESCE(ic.var_desc,'') AS  'desc',
	COALESCE(ic.var_desc_det,'') AS  'desc_det'
	FROM ame_sl_ic_his ic
	LEFT JOIN ame_mt_ite i ON ic.var_itemid = i.var_id
	LEFT JOIN ame_mt_ite_gro ig ON i.var_groupid = ig.var_id
	LEFT JOIN ame_mt_ven v ON ic.var_venid = v.var_id
	LEFT JOIN ame_mt_cus c ON ic.var_cusid = c.var_id
	LEFT JOIN ame_mt_emp e ON	 ic.var_empid = e.var_id
	LEFT JOIN ame_mt_cla cl ON ic.var_claid = cl.var_id 
	LEFT JOIN ame_mt_loc l ON ic.var_locid = l.var_id 
	WHERE
	(CASE
	WHEN '", P1, "' = 'ALL' THEN TRUE
	WHEN '", P1, "' = 'THIS TODAY' THEN DATE(ic.dat_date) = DATE(NOW())
	WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(ic.dat_date) = MONTH('", P2, "')
	WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(ic.dat_date) = YEAR('", P2, "')
	ELSE DATE(ic.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END) 
	AND
	IF('", P4, "' = 'ALL', TRUE, ic.var_locid >= '", P4, "')
	AND
	IF('", P5, "' = 'ALL', TRUE, ic.var_locid <= '", P5, "') 
	AND
	IF('", P6, "' = 'ALL', TRUE, i.var_groupid >= '", P6, "')
	AND
	IF('", P7, "' = 'ALL', TRUE, i.var_groupid <= '", P7, "')
	AND
	IF('", P8, "' = 'ALL', TRUE, ic.var_itemid >= '", P8, "')
	AND
	IF('", P9, "' = 'ALL', TRUE, ic.var_itemid <= '", P9, "') 
	AND
	IF('", P10, "' = 'ALL', TRUE, ic.var_claid >= '", P10, "')
	AND
	IF('", P11, "' = 'ALL', TRUE, ic.var_claid <= '", P11, "') 
	AND
	IF('", P12, "' = 'ALL', TRUE, ic.var_venid >= '", P12, "')
	AND
	IF('", P13, "' = 'ALL', TRUE, ic.var_venid <= '", P13, "')
	AND
	IF('", P14, "' = 'ALL', TRUE, ic.var_cusid >= '", P14, "')
	AND
	IF('", P15, "' = 'ALL', TRUE, ic.var_cusid <= '", P15, "')
	AND
	IF('", P16, "' = 'ALL', TRUE, ic.var_empid >= '", P16, "')
	AND
	IF('", P17, "' = 'ALL', TRUE, ic.var_empid <= '", P17, "')"
	);
	IF P18 = "0" AND P1 <> 'ALL' THEN
		SET @OPENNING = CONCAT( 
		"SELECT
		0 AS SortOrder, 0 AS int_id, 0 AS int_line,
		COALESCE(ic.var_locid,'') AS  'locid',
		COALESCE(l.var_name,'') AS  'locname',
		COALESCE(i.var_groupid,'') AS  'itemgroupid',
		COALESCE(ig.var_name,'') AS  'itemgroupname',
		COALESCE(ic.var_itemid,'') AS  'itemid',
		COALESCE(i.var_name,'') AS  'itemname',
		COALESCE(i.var_uomid,'') AS  'uomid',
		0 AS  'batchno',
		DATE('",P2,"') AS 'doc_date',
		SUM(COALESCE(ic.dou_qty,0)) AS  'qty',
		SUM(COALESCE(ic.dou_uc,0)) AS  'cost',
		SUM(COALESCE(ic.dou_ac1,0)) AS  'ac1',
		SUM(COALESCE(ic.dou_ac2,0)) AS  'ac2',
		SUM(COALESCE(ic.dou_ac3,0)) AS  'ac3',
		SUM(COALESCE(ic.dou_ac4,0)) AS  'ac4',
		SUM(COALESCE(ic.dou_ac5,0)) AS  'ac5',
		SUM(COALESCE(ic.dou_uc,0)) + SUM(COALESCE(ic.dou_ac1,0)) + SUM(COALESCE(ic.dou_ac2,0)) + SUM(COALESCE(ic.dou_ac3,0)) + SUM(COALESCE(ic.dou_ac4,0)) + SUM(COALESCE(ic.dou_ac5,0)) AS 'total_cost',
		'' AS  'modid',
		'' AS  'doctype',
		'Openning Balance' AS  'docid',
		'' AS  'venid',
		'' AS  'venname',
		'' AS  'cusid',
		'' AS  'cusname',
		'' AS  'empid',
		'' AS  'empname',
		'' AS  'claid',
		'' AS  'claname',
		'' AS  'ref',
		'' AS  'desc',
		'' AS  'desc_det'
		FROM ame_sl_ic_his ic
		LEFT JOIN ame_mt_ite i ON ic.var_itemid = i.var_id
		LEFT JOIN ame_mt_ite_gro ig ON i.var_groupid = ig.var_id
		LEFT JOIN ame_mt_loc l ON ic.var_locid = l.var_id 
		WHERE
	  DATE(ic.dat_date) < DATE('", P2, "')
		AND
		IF('", P4, "' = 'ALL', TRUE, ic.var_locid >= '", P4, "')
		AND
		IF('", P5, "' = 'ALL', TRUE, ic.var_locid <= '", P5, "') 
		AND
		IF('", P6, "' = 'ALL', TRUE, i.var_groupid >= '", P6, "')
		AND
		IF('", P7, "' = 'ALL', TRUE, i.var_groupid <= '", P7, "')
		AND
		IF('", P8, "' = 'ALL', TRUE, ic.var_itemid >= '", P8, "')
		AND
		IF('", P9, "' = 'ALL', TRUE, ic.var_itemid <= '", P9, "') 
		AND
		IF('", P10, "' = 'ALL', TRUE, ic.var_claid >= '", P10, "')
		AND
		IF('", P11, "' = 'ALL', TRUE, ic.var_claid <= '", P11, "') 
		AND
		IF('", P12, "' = 'ALL', TRUE, ic.var_venid >= '", P12, "')
		AND
		IF('", P13, "' = 'ALL', TRUE, ic.var_venid <= '", P13, "')
		AND
		IF('", P14, "' = 'ALL', TRUE, ic.var_cusid >= '", P14, "')
		AND
		IF('", P15, "' = 'ALL', TRUE, ic.var_cusid <= '", P15, "')
		AND
		IF('", P16, "' = 'ALL', TRUE, ic.var_empid >= '", P16, "')
		AND
		IF('", P17, "' = 'ALL', TRUE, ic.var_empid <= '", P17, "') 
		GROUP BY ic.var_locid,ic.var_itemid");
	ELSE
		SET @OPENNING = CONCAT( 
		"SELECT
		0 AS SortOrder, 0 AS int_id, 0 AS int_line,
		'' AS  'locid',
		'' AS  'locname',
		'' AS  'itemgroupid',
		'' AS  'itemgroupname',
		'' AS  'itemid',
		'' AS  'itemname',
		'' AS 'uomid',
		0 AS  'batchno',
		DATE('",P2,"') AS 'doc_date',
		0 AS  'qty',
		0 AS  'cost',
		0 AS  'ac1',
		0 AS  'ac2',
		0 AS  'ac3',
		0 AS  'ac4',
		0 AS  'ac5',
		0 AS 'total_cost',
		'' AS  'modid',
		'' AS  'doctype',
		'' AS  'docid',
		'' AS  'venid',
		'' AS  'venname',
		'' AS  'cusid',
		'' AS  'cusname',
		'' AS  'empid',
		'' AS  'empname',
		'' AS  'claid',
		'' AS  'claname',
		'' AS  'ref',
		'' AS  'desc',
		'' AS  'desc_det'
		LIMIT 0 ");
	END IF;
	SET @SQL_EXC = CONCAT("(",@OPENNING,") UNION ALL (",@DETAIL,") ",@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D001_D1_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D001_D1_1`(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
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY SortOrder,int_id,locid,itemid,doc_date ";
	SET @OPENNING = "";
	SET @DETAIL = CONCAT( 
	"SELECT
	1 AS SortOrder,int_id,
	COALESCE(ic.var_locid,'') AS  'locid',
	COALESCE(l.var_name,'') AS  'locname',
	COALESCE(i.var_groupid,'') AS  'itemgroupid',
	COALESCE(ig.var_name,'') AS  'itemgroupname',
	COALESCE(ic.var_itemid,'') AS  'itemid',
	COALESCE(i.var_name,'') AS  'itemname',
	COALESCE(i.var_uomid,'') AS  'uomid',
	COALESCE(ic.int_batchno,0) AS  'batchno',
	DATE(ic.dat_date) AS 'doc_date',
	COALESCE(ic.dou_qty,0) AS  'qty',
	COALESCE(ic.dou_uc,0) AS  'cost',
	COALESCE(ic.dou_ac1,0) AS  'ac1',
	COALESCE(ic.dou_ac2,0) AS  'ac2',
	COALESCE(ic.dou_ac3,0) AS  'ac3',
	COALESCE(ic.dou_ac4,0) AS  'ac4',
	COALESCE(ic.dou_ac5,0) AS  'ac5',
	COALESCE(ic.dou_uc,0) + COALESCE(ic.dou_ac1,0) + COALESCE(ic.dou_ac2,0) + COALESCE(ic.dou_ac3,0) + COALESCE(ic.dou_ac4,0) + COALESCE(ic.dou_ac5,0) AS 'total_cost',
	COALESCE(ic.var_modid,'') AS  'modid',
	COALESCE(ic.var_doctype,'') AS  'doctype',
	COALESCE(ic.var_docid,'') AS  'docid',
	COALESCE(ic.var_venid,'') AS  'venid',
	COALESCE(v.var_name,'') AS  'venname',
	COALESCE(ic.var_cusid,'') AS  'cusid',
	COALESCE(c.var_name,'') AS  'cusname',
	COALESCE(ic.var_empid,'') AS  'empid',
	COALESCE(e.var_name,'') AS  'empname',
	COALESCE(ic.var_claid,'') AS  'claid',
	COALESCE(cl.var_name,'') AS  'claname',
	COALESCE(ic.var_ref,'') AS  'ref',
	COALESCE(ic.var_desc,'') AS  'desc',
	COALESCE(ic.var_desc_det,'') AS  'desc_det'
	FROM ame_sl_ic_his ic
	LEFT JOIN ame_mt_ite i ON ic.var_itemid = i.var_id
	LEFT JOIN ame_mt_ite_gro ig ON i.var_groupid = ig.var_id
	LEFT JOIN ame_mt_ven v ON ic.var_venid = v.var_id
	LEFT JOIN ame_mt_cus c ON ic.var_cusid = c.var_id
	LEFT JOIN ame_mt_emp e ON	 ic.var_empid = e.var_id
	LEFT JOIN ame_mt_cla cl ON ic.var_claid = cl.var_id 
	LEFT JOIN ame_mt_loc l ON ic.var_locid = l.var_id 
	WHERE
	(CASE
	WHEN '", P1, "' = 'ALL' THEN TRUE
	WHEN '", P1, "' = 'THIS TODAY' THEN DATE(ic.dat_date) = DATE(NOW())
	WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(ic.dat_date) = MONTH('", P2, "')
	WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(ic.dat_date) = YEAR('", P2, "')
	ELSE DATE(ic.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END) 
	AND
	IF('", P4, "' = 'ALL', TRUE, ic.var_locid >= '", P4, "')
	AND
	IF('", P5, "' = 'ALL', TRUE, ic.var_locid <= '", P5, "') 
	AND
	IF('", P6, "' = 'ALL', TRUE, i.var_groupid >= '", P6, "')
	AND
	IF('", P7, "' = 'ALL', TRUE, i.var_groupid <= '", P7, "')
	AND
	IF('", P8, "' = 'ALL', TRUE, ic.var_itemid >= '", P8, "')
	AND
	IF('", P9, "' = 'ALL', TRUE, ic.var_itemid <= '", P9, "') 
	AND
	IF('", P10, "' = 'ALL', TRUE, ic.var_claid >= '", P10, "')
	AND
	IF('", P11, "' = 'ALL', TRUE, ic.var_claid <= '", P11, "') 
	AND
	IF('", P12, "' = 'ALL', TRUE, ic.var_venid >= '", P12, "')
	AND
	IF('", P13, "' = 'ALL', TRUE, ic.var_venid <= '", P13, "')
	AND
	IF('", P14, "' = 'ALL', TRUE, ic.var_cusid >= '", P14, "')
	AND
	IF('", P15, "' = 'ALL', TRUE, ic.var_cusid <= '", P15, "')
	AND
	IF('", P16, "' = 'ALL', TRUE, ic.var_empid >= '", P16, "')
	AND
	IF('", P17, "' = 'ALL', TRUE, ic.var_empid <= '", P17, "')
	AND
	IF('", P18, "' = 'ALL', TRUE, ic.int_batchno >= '", P18, "')
	AND
	IF('", P19, "' = 'ALL', TRUE, ic.int_batchno <= '", P19, "')"
	);
	IF P20 = "0" AND P1 <> 'ALL' THEN
		SET @OPENNING = CONCAT( 
		"SELECT
		0 AS SortOrder,0 AS int_id,
		COALESCE(ic.var_locid,'') AS  'locid',
		COALESCE(l.var_name,'') AS  'locname',
		COALESCE(i.var_groupid,'') AS  'itemgroupid',
		COALESCE(ig.var_name,'') AS  'itemgroupname',
		COALESCE(ic.var_itemid,'') AS  'itemid',
		COALESCE(i.var_name,'') AS  'itemname',
		COALESCE(i.var_uomid,'') AS  'uomid',
		0 AS  'batchno',
		DATE('",P2,"') AS 'doc_date',
		SUM(COALESCE(ic.dou_qty,0)) AS  'qty',
		SUM(COALESCE(ic.dou_uc,0)) AS  'cost',
		SUM(COALESCE(ic.dou_ac1,0)) AS  'ac1',
		SUM(COALESCE(ic.dou_ac2,0)) AS  'ac2',
		SUM(COALESCE(ic.dou_ac3,0)) AS  'ac3',
		SUM(COALESCE(ic.dou_ac4,0)) AS  'ac4',
		SUM(COALESCE(ic.dou_ac5,0)) AS  'ac5',
		SUM(COALESCE(ic.dou_uc,0)) + SUM(COALESCE(ic.dou_ac1,0)) + SUM(COALESCE(ic.dou_ac2,0)) + SUM(COALESCE(ic.dou_ac3,0)) + SUM(COALESCE(ic.dou_ac4,0)) + SUM(COALESCE(ic.dou_ac5,0)) AS 'total_cost',
		'' AS  'modid',
		'' AS  'doctype',
		'Openning Balance' AS  'docid',
		'' AS  'venid',
		'' AS  'venname',
		'' AS  'cusid',
		'' AS  'cusname',
		'' AS  'empid',
		'' AS  'empname',
		'' AS  'claid',
		'' AS  'claname',
		'' AS  'ref',
		'' AS  'desc',
		'' AS  'desc_det'
		FROM ame_sl_ic_his ic
		LEFT JOIN ame_mt_ite i ON ic.var_itemid = i.var_id
		LEFT JOIN ame_mt_ite_gro ig ON i.var_groupid = ig.var_id
		LEFT JOIN ame_mt_loc l ON ic.var_locid = l.var_id 
		WHERE
	  DATE(ic.dat_date) < DATE('", P2, "')
		AND
		IF('", P4, "' = 'ALL', TRUE, ic.var_locid >= '", P4, "')
		AND
		IF('", P5, "' = 'ALL', TRUE, ic.var_locid <= '", P5, "') 
		AND
		IF('", P6, "' = 'ALL', TRUE, i.var_groupid >= '", P6, "')
		AND
		IF('", P7, "' = 'ALL', TRUE, i.var_groupid <= '", P7, "')
		AND
		IF('", P8, "' = 'ALL', TRUE, ic.var_itemid >= '", P8, "')
		AND
		IF('", P9, "' = 'ALL', TRUE, ic.var_itemid <= '", P9, "') 
		AND
		IF('", P10, "' = 'ALL', TRUE, ic.var_claid >= '", P10, "')
		AND
		IF('", P11, "' = 'ALL', TRUE, ic.var_claid <= '", P11, "') 
		AND
		IF('", P12, "' = 'ALL', TRUE, ic.var_venid >= '", P12, "')
		AND
		IF('", P13, "' = 'ALL', TRUE, ic.var_venid <= '", P13, "')
		AND
		IF('", P14, "' = 'ALL', TRUE, ic.var_cusid >= '", P14, "')
		AND
		IF('", P15, "' = 'ALL', TRUE, ic.var_cusid <= '", P15, "')
		AND
		IF('", P16, "' = 'ALL', TRUE, ic.var_empid >= '", P16, "')
		AND
		IF('", P17, "' = 'ALL', TRUE, ic.var_empid <= '", P17, "') 
		AND
		IF('", P18, "' = 'ALL', TRUE, ic.int_batchno >= '", P18, "')
		AND
		IF('", P19, "' = 'ALL', TRUE, ic.int_batchno <= '", P19, "')
		GROUP BY ic.var_locid,ic.var_itemid");
	ELSE
		SET @OPENNING = CONCAT( 
		"SELECT
		0 AS SortOrder, 0 AS int_id,
		'' AS  'locid',
		'' AS  'locname',
		'' AS  'itemgroupid',
		'' AS  'itemgroupname',
		'' AS  'itemid',
		'' AS  'itemname',
		'' AS 'uomid',
		0 AS  'batchno',
		DATE('",P2,"') AS 'doc_date',
		0 AS  'qty',
		0 AS  'cost',
		0 AS  'ac1',
		0 AS  'ac2',
		0 AS  'ac3',
		0 AS  'ac4',
		0 AS  'ac5',
		0 AS 'total_cost',
		'' AS  'modid',
		'' AS  'doctype',
		'' AS  'docid',
		'' AS  'venid',
		'' AS  'venname',
		'' AS  'cusid',
		'' AS  'cusname',
		'' AS  'empid',
		'' AS  'empname',
		'' AS  'claid',
		'' AS  'claname',
		'' AS  'ref',
		'' AS  'desc',
		'' AS  'desc_det'
		LIMIT 0 ");
	END IF;
	SET @SQL_EXC = CONCAT("(",@OPENNING,") UNION ALL (",@DETAIL,") ",@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D001_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D001_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))
    DETERMINISTIC
BEGIN
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY T1.locid,T1.itemid";
	SET @OPENNING = "";
	SET @FLD = "T1.locid,
							T1.locname,
							T1.itemgroupid,
							T1.itemgroupname,
							T1.itemid,
							T1.itemname,
							T1.uomid,
							SUM(T1.QTY_OPEN) AS 'QTY_OPEN',
							SUM(T1.QTY_IN) AS 'QTY_IN',
							SUM(T1.QTY_OUT) AS 'QTY_OUT' ";
	SET @DETAIL = CONCAT( 
	"SELECT
	COALESCE(ic.var_locid,'') AS  'locid',
	COALESCE(l.var_name,'') AS  'locname',
	COALESCE(i.var_groupid,'') AS  'itemgroupid',
	COALESCE(ig.var_name,'') AS  'itemgroupname',
	COALESCE(ic.var_itemid,'') AS  'itemid',
	COALESCE(i.var_name,'') AS  'itemname',
	COALESCE(i.var_uomid,'') AS  'uomid',
	0 AS 'QTY_OPEN',
	SUM(IF(COALESCE(ic.dou_qty,0) < 0 ,0,COALESCE(ic.dou_qty,0))) AS  'QTY_IN',
	SUM(IF(COALESCE(ic.dou_qty,0) > 0 ,0,COALESCE(ic.dou_qty,0))) AS  'QTY_OUT'
	FROM ame_sl_ic_his ic
	LEFT JOIN ame_mt_ite i ON ic.var_itemid = i.var_id
	LEFT JOIN ame_mt_ite_gro ig ON i.var_groupid = ig.var_id
	LEFT JOIN ame_mt_loc l ON ic.var_locid = l.var_id 
	WHERE
	(CASE
	WHEN '", P1, "' = 'ALL' THEN TRUE
	WHEN '", P1, "' = 'THIS TODAY' THEN DATE(ic.dat_date) = DATE(NOW())
	WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(ic.dat_date) = MONTH('", P2, "')
	WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(ic.dat_date) = YEAR('", P2, "')
	ELSE DATE(ic.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END) 
	AND
	IF('", P4, "' = 'ALL', TRUE, ic.var_locid >= '", P4, "')
	AND
	IF('", P5, "' = 'ALL', TRUE, ic.var_locid <= '", P5, "') 
	AND
	IF('", P6, "' = 'ALL', TRUE, i.var_groupid >= '", P6, "')
	AND
	IF('", P7, "' = 'ALL', TRUE, i.var_groupid <= '", P7, "')
	AND
	IF('", P8, "' = 'ALL', TRUE, ic.var_itemid >= '", P8, "')
	AND
	IF('", P9, "' = 'ALL', TRUE, ic.var_itemid <= '", P9, "') 
	AND
	IF('", P10, "' = 'ALL', TRUE, ic.var_claid >= '", P10, "')
	AND
	IF('", P11, "' = 'ALL', TRUE, ic.var_claid <= '", P11, "') 
	AND
	IF('", P12, "' = 'ALL', TRUE, ic.var_venid >= '", P12, "')
	AND
	IF('", P13, "' = 'ALL', TRUE, ic.var_venid <= '", P13, "')
	AND
	IF('", P14, "' = 'ALL', TRUE, ic.var_cusid >= '", P14, "')
	AND
	IF('", P15, "' = 'ALL', TRUE, ic.var_cusid <= '", P15, "')
	AND
	IF('", P16, "' = 'ALL', TRUE, ic.var_empid >= '", P16, "')
	AND
	IF('", P17, "' = 'ALL', TRUE, ic.var_empid <= '", P17, "')
	GROUP BY ic.var_locid,ic.var_itemid ");
	IF P18 = "0" AND P1 <> 'ALL' THEN
		SET @OPENNING = CONCAT( 
		"SELECT
		COALESCE(ic.var_locid,'') AS  'locid',
		COALESCE(l.var_name,'') AS  'locname',
		COALESCE(i.var_groupid,'') AS  'itemgroupid',
		COALESCE(ig.var_name,'') AS  'itemgroupname',
		COALESCE(ic.var_itemid,'') AS  'itemid',
		COALESCE(i.var_name,'') AS  'itemname',
		COALESCE(i.var_uomid,'') AS  'uomid',
		SUM(COALESCE(ic.dou_qty,0)) AS 'QTY_OPEN',
		0 AS  'QTY_IN',
		0 AS  'QTY_OUT'
		FROM ame_sl_ic_his ic
		LEFT JOIN ame_mt_ite i ON ic.var_itemid = i.var_id
		LEFT JOIN ame_mt_ite_gro ig ON i.var_groupid = ig.var_id
		LEFT JOIN ame_mt_loc l ON ic.var_locid = l.var_id 
		WHERE
	  DATE(ic.dat_date) < DATE('", P2, "')
		AND
		IF('", P4, "' = 'ALL', TRUE, ic.var_locid >= '", P4, "')
		AND
		IF('", P5, "' = 'ALL', TRUE, ic.var_locid <= '", P5, "') 
		AND
		IF('", P6, "' = 'ALL', TRUE, i.var_groupid >= '", P6, "')
		AND
		IF('", P7, "' = 'ALL', TRUE, i.var_groupid <= '", P7, "')
		AND
		IF('", P8, "' = 'ALL', TRUE, ic.var_itemid >= '", P8, "')
		AND
		IF('", P9, "' = 'ALL', TRUE, ic.var_itemid <= '", P9, "') 
		AND
		IF('", P10, "' = 'ALL', TRUE, ic.var_claid >= '", P10, "')
		AND
		IF('", P11, "' = 'ALL', TRUE, ic.var_claid <= '", P11, "') 
		AND
		IF('", P12, "' = 'ALL', TRUE, ic.var_venid >= '", P12, "')
		AND
		IF('", P13, "' = 'ALL', TRUE, ic.var_venid <= '", P13, "')
		AND
		IF('", P14, "' = 'ALL', TRUE, ic.var_cusid >= '", P14, "')
		AND
		IF('", P15, "' = 'ALL', TRUE, ic.var_cusid <= '", P15, "')
		AND
		IF('", P16, "' = 'ALL', TRUE, ic.var_empid >= '", P16, "')
		AND
		IF('", P17, "' = 'ALL', TRUE, ic.var_empid <= '", P17, "') 
		GROUP BY ic.var_locid,ic.var_itemid");
	ELSE
		SET @OPENNING = CONCAT( 
		"SELECT
		'' AS  'locid',
		'' AS  'locname',
		'' AS  'itemgroupid',
		'' AS  'itemgroupname',
		'' AS  'itemid',
		'' AS  'itemname',
		'' AS  'uomid',
		0 AS 'QTY_OPEN',
		0 AS  'QTY_IN',
		0 AS  'QTY_OUT'
		LIMIT 0 ");
	END IF;
	SET @SQL_EXC = CONCAT("SELECT ",@FLD," FROM ((",@OPENNING,") UNION ALL (",@DETAIL,")) T1 GROUP BY T1.locid,T1.itemid ",@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D002_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D002_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))
    DETERMINISTIC
BEGIN
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY T1.locid,T1.itemid";
	SET @OPENNING = "";
	SET @FLD = "T1.locid,
							T1.locname,
							T1.itemgroupid,
							T1.itemgroupname,
							T1.itemid,
							T1.itemname,
							T1.uomid,
							SUM(T1.OPENING) AS 'OPENING',
							SUM(T1.AP_IN) AS 'AP_IN',
							SUM(T1.AP_RET) AS 'AP_RET',
							SUM(T1.AP_DN) AS 'AP_DN',
							SUM(T1.IC_TF_IN) AS 'IC_TF_IN',
							SUM(T1.AR_IN) AS 'AR_IN',
							SUM(T1.AR_RET) AS 'AR_RET',
							SUM(T1.AR_CN) AS 'AR_CN',
							SUM(T1.IC_TF_OUT) AS 'IC_TF_OUT',
							SUM(T1.OTHER) AS 'OTHER',
							SUM(T1.ONHAND) AS 'ONHAND',
							SUM(T1.total_cost) AS 'total_cost' ";
	SET @DETAIL = CONCAT( 
	"SELECT
	COALESCE(ic.var_locid,'') AS  'locid',
	COALESCE(l.var_name,'') AS  'locname',
	COALESCE(i.var_groupid,'') AS  'itemgroupid',
	COALESCE(ig.var_name,'') AS  'itemgroupname',
	COALESCE(ic.var_itemid,'') AS  'itemid',
	COALESCE(i.var_name,'') AS  'itemname',
	COALESCE(i.var_uomid,'') AS  'uomid',
	0 AS 'OPENING',
	IF(ic.var_modid = 'AP-IN',COALESCE(ic.dou_qty,0),0) AS 'AP_IN',
	IF(ic.var_modid = 'AP-RET',COALESCE(ic.dou_qty,0),0) AS 'AP_RET',
	IF(ic.var_modid = 'AP-DN',COALESCE(ic.dou_qty,0),0) AS 'AP_DN',
	IF(ic.var_modid = 'IC-TF' AND COALESCE(ic.dou_qty,0) > 0,COALESCE(ic.dou_qty,0),0) AS 'IC_TF_IN',
	IF(ic.var_modid = 'AR-IN',COALESCE(ic.dou_qty,0),0) AS 'AR_IN',
	IF(ic.var_modid = 'AR-RET',COALESCE(ic.dou_qty,0),0) AS 'AR_RET',
	IF(ic.var_modid = 'AR-CN',COALESCE(ic.dou_qty,0),0) AS 'AR_CN',
	IF(ic.var_modid = 'IC-TF' AND COALESCE(ic.dou_qty,0) < 0,COALESCE(ic.dou_qty,0),0) AS 'IC_TF_OUT',
	IF(ic.var_modid NOT IN('AP-IN','AP-RET','AP-DN','IC-TF','AR-IN','AR-RET','AR-CN'),COALESCE(ic.dou_qty,0),0) AS 'OTHER',
	COALESCE(ic.dou_qty,0) AS 'ONHAND',
	COALESCE(ic.dou_uc,0) + COALESCE(ic.dou_ac1,0) + COALESCE(ic.dou_ac2,0) + COALESCE(ic.dou_ac3,0) + COALESCE(ic.dou_ac4,0) + COALESCE(ic.dou_ac5,0) AS 'total_cost'
	FROM ame_sl_ic_his ic
	LEFT JOIN ame_mt_ite i ON ic.var_itemid = i.var_id
	LEFT JOIN ame_mt_ite_gro ig ON i.var_groupid = ig.var_id
	LEFT JOIN ame_mt_loc l ON ic.var_locid = l.var_id
	WHERE
	(CASE
	WHEN '", P1, "' = 'ALL' THEN TRUE
	WHEN '", P1, "' = 'THIS TODAY' THEN DATE(ic.dat_date) = DATE(NOW())
	WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(ic.dat_date) = MONTH('", P2, "')
	WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(ic.dat_date) = YEAR('", P2, "')
	ELSE DATE(ic.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END) 
	AND
	IF('", P4, "' = 'ALL', TRUE, ic.var_locid >= '", P4, "')
	AND
	IF('", P5, "' = 'ALL', TRUE, ic.var_locid <= '", P5, "') 
	AND
	IF('", P6, "' = 'ALL', TRUE, i.var_groupid >= '", P6, "')
	AND
	IF('", P7, "' = 'ALL', TRUE, i.var_groupid <= '", P7, "')
	AND
	IF('", P8, "' = 'ALL', TRUE, ic.var_itemid >= '", P8, "')
	AND
	IF('", P9, "' = 'ALL', TRUE, ic.var_itemid <= '", P9, "') 
	AND
	IF('", P10, "' = 'ALL', TRUE, ic.var_claid >= '", P10, "')
	AND
	IF('", P11, "' = 'ALL', TRUE, ic.var_claid <= '", P11, "') 
	AND
	IF('", P12, "' = 'ALL', TRUE, ic.var_venid >= '", P12, "')
	AND
	IF('", P13, "' = 'ALL', TRUE, ic.var_venid <= '", P13, "')
	AND
	IF('", P14, "' = 'ALL', TRUE, ic.var_cusid >= '", P14, "')
	AND
	IF('", P15, "' = 'ALL', TRUE, ic.var_cusid <= '", P15, "')
	AND
	IF('", P16, "' = 'ALL', TRUE, ic.var_empid >= '", P16, "')
	AND
	IF('", P17, "' = 'ALL', TRUE, ic.var_empid <= '", P17, "') ");
	IF P18 = "0" AND P1 <> 'ALL' THEN
		SET @OPENNING = CONCAT( 
		"SELECT
		COALESCE(ic.var_locid,'') AS  'locid',
		COALESCE(l.var_name,'') AS  'locname',
		COALESCE(i.var_groupid,'') AS  'itemgroupid',
		COALESCE(ig.var_name,'') AS  'itemgroupname',
		COALESCE(ic.var_itemid,'') AS  'itemid',
		COALESCE(i.var_name,'') AS  'itemname',
		COALESCE(i.var_uomid,'') AS  'uomid',
		SUM(COALESCE(ic.dou_qty,0)) AS 'OPENING',
		0 AS 'AP_IN',
		0 AS 'AP_RET',
		0 AS 'AP_DN',
		0 AS 'IC_TF_IN',
		0 AS 'AR_IN',
		0 AS 'AR_RET',
		0 AS 'AR_CN',
		0 AS 'IC_TF_OUT',
		0 AS 'OTHER',
		SUM(COALESCE(ic.dou_qty,0)) AS 'ONHAND',
		SUM(COALESCE(ic.dou_uc,0)) + SUM(COALESCE(ic.dou_ac1,0)) + SUM(COALESCE(ic.dou_ac2,0)) + SUM(COALESCE(ic.dou_ac3,0)) + SUM(COALESCE(ic.dou_ac4,0)) + SUM(COALESCE(ic.dou_ac5,0)) AS 'total_cost'
		FROM ame_sl_ic_his ic
		LEFT JOIN ame_mt_ite i ON ic.var_itemid = i.var_id
		LEFT JOIN ame_mt_ite_gro ig ON i.var_groupid = ig.var_id
		LEFT JOIN ame_mt_loc l ON ic.var_locid = l.var_id
		WHERE
	  DATE(ic.dat_date) < DATE('", P2, "')
		AND
		IF('", P4, "' = 'ALL', TRUE, ic.var_locid >= '", P4, "')
		AND
		IF('", P5, "' = 'ALL', TRUE, ic.var_locid <= '", P5, "') 
		AND
		IF('", P6, "' = 'ALL', TRUE, i.var_groupid >= '", P6, "')
		AND
		IF('", P7, "' = 'ALL', TRUE, i.var_groupid <= '", P7, "')
		AND
		IF('", P8, "' = 'ALL', TRUE, ic.var_itemid >= '", P8, "')
		AND
		IF('", P9, "' = 'ALL', TRUE, ic.var_itemid <= '", P9, "') 
		AND
		IF('", P10, "' = 'ALL', TRUE, ic.var_claid >= '", P10, "')
		AND
		IF('", P11, "' = 'ALL', TRUE, ic.var_claid <= '", P11, "') 
		AND
		IF('", P12, "' = 'ALL', TRUE, ic.var_venid >= '", P12, "')
		AND
		IF('", P13, "' = 'ALL', TRUE, ic.var_venid <= '", P13, "')
		AND
		IF('", P14, "' = 'ALL', TRUE, ic.var_cusid >= '", P14, "')
		AND
		IF('", P15, "' = 'ALL', TRUE, ic.var_cusid <= '", P15, "')
		AND
		IF('", P16, "' = 'ALL', TRUE, ic.var_empid >= '", P16, "')
		AND
		IF('", P17, "' = 'ALL', TRUE, ic.var_empid <= '", P17, "') 
		GROUP BY ic.var_locid,ic.var_itemid");
	ELSE
		SET @OPENNING = CONCAT( 
		"SELECT
		'' AS  'locid',
		'' AS  'locname',
		'' AS  'itemgroupid',
		'' AS  'itemgroupname',
		'' AS  'itemid',
		'' AS  'itemname',
		'' AS  'uomid',
		0 AS 'OPENING',
		0 AS 'AP_IN',
		0 AS 'AP_RET',
		0 AS 'AP_DN',
		0 AS 'IC_TF_IN',
		0 AS 'AR_IN',
		0 AS 'AR_RET',
		0 AS 'AR_CN',
		0 AS 'IC_TF_OUT',
		0 AS 'OTHER',
		0 AS 'ONHAND',
		0 AS 'total_cost'
		LIMIT 0 ");
	END IF;
	SET @SQL_EXC = CONCAT("SELECT ",@FLD," FROM ((",@OPENNING,") UNION ALL (",@DETAIL,")) T1 GROUP BY T1.locid,T1.itemid ",@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D003_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D003_M1`(D1 varchar(100),D2 varchar(100),D3 varchar(100),D4 varchar(100),D5 varchar(100),P1 varchar(100),P2 varchar(100),P3 varchar(100),P4 varchar(100),P5 varchar(100),P6 varchar(100))
    DETERMINISTIC
BEGIN
		#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
		SET @ORDERBY = " ORDER BY ic.var_locid,ic.var_itemid";
		SET @DETAIL = CONCAT( 
		"SELECT 
		COALESCE(ic.var_locid,'') AS 'locid',
		COALESCE(l.var_name,'') AS 'locname',
		COALESCE(ic.var_itemid,'') AS 'itemid',
		COALESCE(i.var_name,'') AS 'itemname',
		COALESCE(i.var_itemcode,'') AS 'itemcode',
		COALESCE(i.var_groupid,'') AS 'itemgroupid',
		COALESCE(ig.var_name,'') AS 'itemgroupname',
		COALESCE(i.var_uomid,'') AS 'uomid',
		SUM(COALESCE(ic.dou_qty,0)) AS 'qty'
		FROM ame_sl_ic_batch ic
		LEFT JOIN ame_mt_loc l ON ic.var_locid = l.var_id
		LEFT JOIN ame_mt_ite i ON ic.var_itemid = i.var_id
		LEFT JOIN ame_mt_ite_gro ig ON i.var_groupid = ig.var_id
		WHERE
		IF('", P1, "' = 'ALL', TRUE, ic.var_locid >= '", P1, "')
		AND
		IF('", P2, "' = 'ALL', TRUE, ic.var_locid <= '", P2, "') 
		AND
		IF('", P3, "' = 'ALL', TRUE, i.var_groupid >= '", P3, "')
		AND
		IF('", P4, "' = 'ALL', TRUE, i.var_groupid <= '", P4, "')
		AND
		IF('", P5, "' = 'ALL', TRUE, ic.var_itemid >= '", P5, "')
		AND
		IF('", P6, "' = 'ALL', TRUE, ic.var_itemid <= '", P6, "') 
		GROUP BY ic.var_locid,ic.var_itemid ");
		SET @SQL_EXC = CONCAT(@DETAIL,@ORDERBY);
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D003_M4`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D003_M4`(D1 varchar(100),D2 varchar(100),D3 varchar(100),D4 varchar(100),D5 varchar(100),P1 varchar(100),P2 varchar(100),P3 varchar(100),P4 varchar(100),P5 varchar(100),P6 varchar(100))
    DETERMINISTIC
BEGIN
		#'','','','','', '','','','','',''
		SET @ORDERBY = " ORDER BY T1.var_itemid, T1.int_batchno, T1.var_locid";
		SET @DETAIL = CONCAT( 
		"SELECT 
		COALESCE(T1.var_locid, '') AS P_LOC,
		COALESCE(T2.var_name, '') AS P_LOC_NAME,
		COALESCE(T1.var_itemid, '') AS P_ITE,
		COALESCE(T3.var_name, '') AS P_ITE_NAME,
		COALESCE(T3.var_itemcode, '') AS P_ITE_CODE,
		COALESCE(T3.var_groupid, '') AS P_ITE_GRO,
		COALESCE(T4.var_name, '') AS P_ITE_GRO_NAME,
		COALESCE(T1.int_batchno, 0) AS P_BATCH,
		COALESCE(T3.var_uomid, '') AS P_UOM,
		COALESCE(T1.dou_qty, 0) AS P_QTY,
		COALESCE(T1.dou_druc - T1.dou_cruc, 0) AS P_UC,
		COALESCE(T1.dou_drac1 - T1.dou_crac1, 0) AS P_AC1,
		COALESCE(T1.dou_drac2 - T1.dou_crac2, 0) AS P_AC2,
		COALESCE(T1.dou_drac3 - T1.dou_crac3, 0) AS P_AC3,
		COALESCE(T1.dou_drac4 - T1.dou_crac4, 0) AS P_AC4,
		COALESCE(T1.dou_drac5 - T1.dou_crac5, 0) AS P_AC5,
		COALESCE((T1.dou_druc - T1.dou_cruc) + (T1.dou_drac1 - T1.dou_crac1) + (T1.dou_drac2 - T1.dou_crac2) + (T1.dou_drac3 - T1.dou_crac3) + (T1.dou_drac4 - T1.dou_crac4) + (T1.dou_drac5 - T1.dou_crac5), 0) AS P_TUC,
		IF(T1.dou_qty = 0, 0, COALESCE((T1.dou_druc - T1.dou_cruc) + (T1.dou_drac1 - T1.dou_crac1) + (T1.dou_drac2 - T1.dou_crac2) + (T1.dou_drac3 - T1.dou_crac3) + (T1.dou_drac4 - T1.dou_crac4) + (T1.dou_drac5 - T1.dou_crac5), 0) / T1.dou_qty) AS P_AVG
		FROM ame_sl_ic_batch T1
		LEFT JOIN ame_mt_loc T2 ON T1.var_locid = T2.var_id
		LEFT JOIN ame_mt_ite T3 ON T1.var_itemid = T3.var_id
		LEFT JOIN ame_mt_ite_gro T4 ON T3.var_groupid = T4.var_id
		WHERE 
				IF('", P1, "' = 'ALL', TRUE, T1.var_locid >= '", P1, "') AND IF('", P2, "' = 'ALL', TRUE, T1.var_locid <= '", P2, "') 
		AND
				IF('", P3, "' = 'ALL', TRUE, T3.var_groupid >= '", P3, "') AND IF('", P4, "' = 'ALL', TRUE, T3.var_groupid <= '", P4, "')
		AND
				IF('", P5, "' = 'ALL', TRUE, T1.var_itemid >= '", P5, "') AND IF('", P6, "' = 'ALL', TRUE, T1.var_itemid <= '", P6, "') 
		GROUP BY 
				T1.var_locid, T1.var_itemid, T1.int_batchno");
		SET @SQL_EXC = CONCAT(@DETAIL, @ORDERBY);
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D004_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D004_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))
    DETERMINISTIC
BEGIN
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY ic.var_locid,ic.var_itemid";
	SET @DETAIL = CONCAT( 
	"SELECT 
	COALESCE(ic.var_locid,'') AS 'locid',
	COALESCE(l.var_name,'') AS 'locname',
	COALESCE(ic.var_itemid,'') AS 'itemid',
	COALESCE(i.var_name,'') AS 'itemname',
	COALESCE(i.var_itemcode,'') AS 'itemcode',
	COALESCE(i.var_groupid,'') AS 'itemgroupid',
	COALESCE(ig.var_name,'') AS 'itemgroupname',
	COALESCE(i.var_uomid,'') AS 'uomid',
	SUM(COALESCE(ic.dou_drqty,0)) - SUM(COALESCE(ic.dou_crqty,0)) AS 'qty'
	FROM ame_sl_ic ic
	LEFT JOIN ame_mt_loc l ON ic.var_locid = l.var_id
	LEFT JOIN ame_mt_ite i ON ic.var_itemid = i.var_id
	LEFT JOIN ame_mt_ite_gro ig ON i.var_groupid = ig.var_id
	WHERE
	(CASE
	WHEN '", P1, "' = 'ALL' THEN TRUE
	ELSE DATE(ic.dat_date) <= DATE('", P3, "') END) 
	AND
	IF('", P4, "' = 'ALL', TRUE, ic.var_locid >= '", P4, "')
	AND
	IF('", P5, "' = 'ALL', TRUE, ic.var_locid <= '", P5, "') 
	AND
	IF('", P6, "' = 'ALL', TRUE, i.var_groupid >= '", P6, "')
	AND
	IF('", P7, "' = 'ALL', TRUE, i.var_groupid <= '", P7, "')
	AND
	IF('", P8, "' = 'ALL', TRUE, ic.var_itemid >= '", P8, "')
	AND
	IF('", P9, "' = 'ALL', TRUE, ic.var_itemid <= '", P9, "') 
	GROUP BY ic.var_locid,ic.var_itemid ");
	SET @SQL_EXC = CONCAT(@DETAIL,@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D004_M2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D004_M2`(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))
    DETERMINISTIC
BEGIN
		#'','','','','', '','','','','',''
		SET @ORDERBY = " ORDER BY T1.var_itemid";
		SET @DETAIL = CONCAT( 
		"SELECT 
		COALESCE(T1.var_locid, '') AS P_LOC,
		COALESCE(T2.var_name, '') AS P_LOC_NAME,
		COALESCE(T1.var_itemid, '') AS P_ITE,
		COALESCE(T3.var_name, '') AS P_ITE_NAME,
		COALESCE(T3.var_itemcode, '') AS P_ITE_CODE,
		COALESCE(T3.var_groupid, '') AS P_ITE_GRO,
		COALESCE(T4.var_name, '') AS P_ITE_GRO_NAME,
		COALESCE(T1.int_batchno, 0) AS P_BATCH,
		COALESCE(T3.var_uomid, '') AS P_UOM,
		COALESCE(SUM(T1.dou_drqty - T1.dou_crqty), 0) AS P_QTY,
		COALESCE(SUM(T1.dou_druc - T1.dou_cruc), 0) AS P_UC,
		COALESCE(SUM(T1.dou_drac1 - T1.dou_crac1), 0) AS P_AC1,
		COALESCE(SUM(T1.dou_drac2 - T1.dou_crac2), 0) AS P_AC2,
		COALESCE(SUM(T1.dou_drac3 - T1.dou_crac3), 0) AS P_AC3,
		COALESCE(SUM(T1.dou_drac4 - T1.dou_crac4), 0) AS P_AC4,
		COALESCE(SUM(T1.dou_drac5 - T1.dou_crac5), 0) AS P_AC5,
		COALESCE(SUM((T1.dou_druc - T1.dou_cruc) + (T1.dou_drac1 - T1.dou_crac1) + (T1.dou_drac2 - T1.dou_crac2) + (T1.dou_drac3 - T1.dou_crac3) + (T1.dou_drac4 - T1.dou_crac4) + (T1.dou_drac5 - T1.dou_crac5)), 0) AS P_TUC,
		IF(SUM(T1.dou_drqty - T1.dou_crqty) = 0, 0, COALESCE(SUM((T1.dou_druc - T1.dou_cruc) + (T1.dou_drac1 - T1.dou_crac1) + (T1.dou_drac2 - T1.dou_crac2) + (T1.dou_drac3 - T1.dou_crac3) + (T1.dou_drac4 - T1.dou_crac4) + (T1.dou_drac5 - T1.dou_crac5)), 0) / SUM(T1.dou_drqty - T1.dou_crqty)) AS P_AVG
		FROM ame_sl_ic T1
		LEFT JOIN ame_mt_loc T2 ON T1.var_locid = T2.var_id
		LEFT JOIN ame_mt_ite T3 ON T1.var_itemid = T3.var_id
		LEFT JOIN ame_mt_ite_gro T4 ON T3.var_groupid = T4.var_id
		WHERE 
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') 
				END) 
		AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_locid >= '", P4, "') AND IF('", P5, "' = 'ALL', TRUE, T1.var_locid <= '", P5, "') 
		AND
				IF('", P6, "' = 'ALL', TRUE, T3.var_groupid >= '", P6, "') AND IF('", P7, "' = 'ALL', TRUE, T3.var_groupid <= '", P7, "')
		AND
				IF('", P8, "' = 'ALL', TRUE, T1.var_itemid >= '", P8, "') AND IF('", P9, "' = 'ALL', TRUE, T1.var_itemid <= '", P9, "') 
		GROUP BY 
				T1.var_itemid");
		SET @SQL_EXC = CONCAT(@DETAIL, @ORDERBY);
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D004_M3`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D004_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))
    DETERMINISTIC
BEGIN
		#'','','','','', '','','','','',''
		SET @ORDERBY = " ORDER BY T1.var_locid, T1.var_itemid";
		SET @DETAIL = CONCAT( 
		"SELECT 
		COALESCE(T1.var_locid, '') AS P_LOC,
		COALESCE(T2.var_name, '') AS P_LOC_NAME,
		COALESCE(T1.var_itemid, '') AS P_ITE,
		COALESCE(T3.var_name, '') AS P_ITE_NAME,
		COALESCE(T3.var_itemcode, '') AS P_ITE_CODE,
		COALESCE(T3.var_groupid, '') AS P_ITE_GRO,
		COALESCE(T4.var_name, '') AS P_ITE_GRO_NAME,
		COALESCE(T1.int_batchno, 0) AS P_BATCH,
		COALESCE(T3.var_uomid, '') AS P_UOM,
		COALESCE(SUM(T1.dou_drqty - T1.dou_crqty), 0) AS P_QTY,
		COALESCE(SUM(T1.dou_druc - T1.dou_cruc), 0) AS P_UC,
		COALESCE(SUM(T1.dou_drac1 - T1.dou_crac1), 0) AS P_AC1,
		COALESCE(SUM(T1.dou_drac2 - T1.dou_crac2), 0) AS P_AC2,
		COALESCE(SUM(T1.dou_drac3 - T1.dou_crac3), 0) AS P_AC3,
		COALESCE(SUM(T1.dou_drac4 - T1.dou_crac4), 0) AS P_AC4,
		COALESCE(SUM(T1.dou_drac5 - T1.dou_crac5), 0) AS P_AC5,
		COALESCE(SUM((T1.dou_druc - T1.dou_cruc) + (T1.dou_drac1 - T1.dou_crac1) + (T1.dou_drac2 - T1.dou_crac2) + (T1.dou_drac3 - T1.dou_crac3) + (T1.dou_drac4 - T1.dou_crac4) + (T1.dou_drac5 - T1.dou_crac5)), 0) AS P_TUC,
		IF(SUM(T1.dou_drqty - T1.dou_crqty) = 0, 0, COALESCE(SUM((T1.dou_druc - T1.dou_cruc) + (T1.dou_drac1 - T1.dou_crac1) + (T1.dou_drac2 - T1.dou_crac2) + (T1.dou_drac3 - T1.dou_crac3) + (T1.dou_drac4 - T1.dou_crac4) + (T1.dou_drac5 - T1.dou_crac5)), 0) / SUM(T1.dou_drqty - T1.dou_crqty)) AS P_AVG
		FROM ame_sl_ic T1
		LEFT JOIN ame_mt_loc T2 ON T1.var_locid = T2.var_id
		LEFT JOIN ame_mt_ite T3 ON T1.var_itemid = T3.var_id
		LEFT JOIN ame_mt_ite_gro T4 ON T3.var_groupid = T4.var_id
		WHERE 
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') 
				END) 
		AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_locid >= '", P4, "') AND IF('", P5, "' = 'ALL', TRUE, T1.var_locid <= '", P5, "') 
		AND
				IF('", P6, "' = 'ALL', TRUE, T3.var_groupid >= '", P6, "') AND IF('", P7, "' = 'ALL', TRUE, T3.var_groupid <= '", P7, "')
		AND
				IF('", P8, "' = 'ALL', TRUE, T1.var_itemid >= '", P8, "') AND IF('", P9, "' = 'ALL', TRUE, T1.var_itemid <= '", P9, "') 
		GROUP BY 
				T1.var_locid, T1.var_itemid");
		SET @SQL_EXC = CONCAT(@DETAIL, @ORDERBY);
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D004_M4`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D004_M4`(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))
    DETERMINISTIC
BEGIN
		#'','','','','', '','','','','',''
		SET @ORDERBY = " ORDER BY T1.var_itemid, T1.int_batchno, T1.var_locid";
		SET @DETAIL = CONCAT( 
		"SELECT 
		COALESCE(T1.var_locid, '') AS P_LOC,
		COALESCE(T2.var_name, '') AS P_LOC_NAME,
		COALESCE(T1.var_itemid, '') AS P_ITE,
		COALESCE(T3.var_name, '') AS P_ITE_NAME,
		COALESCE(T3.var_itemcode, '') AS P_ITE_CODE,
		COALESCE(T3.var_groupid, '') AS P_ITE_GRO,
		COALESCE(T4.var_name, '') AS P_ITE_GRO_NAME,
		COALESCE(T1.int_batchno, 0) AS P_BATCH,
		COALESCE(T3.var_uomid, '') AS P_UOM,
		COALESCE(SUM(T1.dou_drqty - T1.dou_crqty), 0) AS P_QTY,
		COALESCE(SUM(T1.dou_druc - T1.dou_cruc), 0) AS P_UC,
		COALESCE(SUM(T1.dou_drac1 - T1.dou_crac1), 0) AS P_AC1,
		COALESCE(SUM(T1.dou_drac2 - T1.dou_crac2), 0) AS P_AC2,
		COALESCE(SUM(T1.dou_drac3 - T1.dou_crac3), 0) AS P_AC3,
		COALESCE(SUM(T1.dou_drac4 - T1.dou_crac4), 0) AS P_AC4,
		COALESCE(SUM(T1.dou_drac5 - T1.dou_crac5), 0) AS P_AC5,
		COALESCE(SUM((T1.dou_druc - T1.dou_cruc) + (T1.dou_drac1 - T1.dou_crac1) + (T1.dou_drac2 - T1.dou_crac2) + (T1.dou_drac3 - T1.dou_crac3) + (T1.dou_drac4 - T1.dou_crac4) + (T1.dou_drac5 - T1.dou_crac5)), 0) AS P_TUC,
		IF(SUM((T1.dou_drqty - T1.dou_crqty)) = 0, 0, COALESCE(SUM((T1.dou_druc - T1.dou_cruc) + (T1.dou_drac1 - T1.dou_crac1) + (T1.dou_drac2 - T1.dou_crac2) + (T1.dou_drac3 - T1.dou_crac3) + (T1.dou_drac4 - T1.dou_crac4) + (T1.dou_drac5 - T1.dou_crac5)), 0) / SUM((T1.dou_drqty - T1.dou_crqty))) AS P_AVG
		FROM ame_sl_ic T1
		LEFT JOIN ame_mt_loc T2 ON T1.var_locid = T2.var_id
		LEFT JOIN ame_mt_ite T3 ON T1.var_itemid = T3.var_id
		LEFT JOIN ame_mt_ite_gro T4 ON T3.var_groupid = T4.var_id
		WHERE 
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				ELSE DATE(T1.dat_date) <= DATE('", P3, "') 
				END) 
		AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_locid >= '", P4, "') AND IF('", P5, "' = 'ALL', TRUE, T1.var_locid <= '", P5, "') 
		AND
				IF('", P6, "' = 'ALL', TRUE, T3.var_groupid >= '", P6, "') AND IF('", P7, "' = 'ALL', TRUE, T3.var_groupid <= '", P7, "')
		AND
				IF('", P8, "' = 'ALL', TRUE, T1.var_itemid >= '", P8, "') AND IF('", P9, "' = 'ALL', TRUE, T1.var_itemid <= '", P9, "') 
		GROUP BY 
				T1.var_locid, T1.var_itemid, T1.int_batchno");
		SET @SQL_EXC = CONCAT(@DETAIL, @ORDERBY);
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D005_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D005_M1`(D1 varchar(100),D2 varchar(100),D3 varchar(100),D4 varchar(100),D5 varchar(100),P1 varchar(100),P2 varchar(100),P3 varchar(100),P4 varchar(100),P5 varchar(100),P6 varchar(100),P7 varchar(100),P8 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE var_pc VARCHAR(255) DEFAULT "";
		DECLARE D1 INTEGER DEFAULT 0;
		DECLARE int_max_pc INTEGER(11) DEFAULT 0;
		DECLARE C1 CURSOR FOR SELECT DISTINCT COALESCE(pc.var_id,'') AS var_id FROM ame_mt_pri_cod pc ORDER BY pc.var_id;
		DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
		SET SESSION group_concat_max_len = 1000000;
		SET @sql1 = "";
		SET @sql2 = "";
		SET @sql3 = "";
		SET @IND = 0;
		SET int_max_pc = 10;
		OPEN C1;
			L1: LOOP FETCH C1 INTO var_pc;
				IF D1 = 1 THEN LEAVE L1; END IF;
					SET @IND = @IND + 1;
					SET @sql1 = CONCAT(@sql1,"COALESCE(SUM(`COLUMN_",@IND,"`),0) AS `COLUMN_", @IND,"`,");
					SET @sql2 = CONCAT(@sql2,"IF(ip.var_pricecode = '",var_pc,"',ip.dou_price,0) AS `COLUMN_",@IND,"`,");
				END LOOP L1;
		CLOSE C1;
		WHILE(@IND < int_max_pc) DO
			SET @IND = @IND + 1;
			SET @sql1 = CONCAT(@sql1,"COALESCE(SUM(`COLUMN_",@IND,"`),0) AS `COLUMN_", @IND,"`,");
			SET @sql2 = CONCAT(@sql2,"IF(ip.var_pricecode = '",var_pc,"',ip.dou_price,0) AS `COLUMN_",@IND,"`,");
		END WHILE;
		IF @sql1 <> "" THEN
			SET @sql1 =CONCAT(",",LEFT(@sql1, CHAR_LENGTH(@sql1) -1));
		END IF;
		IF @sql2 <> "" THEN
			SET @sql2 = CONCAT(",",LEFT(@sql2, CHAR_LENGTH(@sql2) -1));
		END IF;
		SET @sql3 = 
		CONCAT("SELECT COALESCE(i.var_groupid,'') AS `itemgroupid`,T1.var_id AS `itemid`,COALESCE(i.var_name,'') AS `itemname`,T1.var_uomid,T1.var_cur",@sql1," FROM (SELECT ip.var_id,ip.var_uomid,ip.var_cur",@sql2," FROM ame_mt_ite_pri ip 
		WHERE 
		IF('", P3, "' = 'ALL', TRUE, ip.var_id >= '", P3, "')
		AND
		IF('", P4, "' = 'ALL', TRUE, ip.var_id  <= '", P4, "')
		AND
		IF('", P5, "' = 'ALL', TRUE, ip.var_pricecode >= '", P5, "')
		AND
		IF('", P6, "' = 'ALL', TRUE, ip.var_pricecode <= '", P6, "') 
		AND
		IF('", P7, "' = 'ALL', TRUE, ip.var_cur >= '", P7, "')
		AND
		IF('", P8, "' = 'ALL', TRUE, ip.var_cur <= '", P8, "') 
		) AS T1 LEFT JOIN ame_mt_ite i ON T1.var_id = i.var_id  
		WHERE
		IF('", P1, "' = 'ALL', TRUE, i.var_groupid >= '", P1, "')
		AND
		IF('", P2, "' = 'ALL', TRUE, i.var_groupid <= '", P2, "') 
		GROUP BY T1.var_id,T1.var_uomid,T1.var_cur ORDER BY T1.var_id,T1.var_uomid,T1.var_cur");
		PREPARE stmt FROM @sql3;
		EXECUTE stmt;
		DEALLOCATE PREPARE stmt;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D006_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D006_M1`(D1 varchar(100),D2 varchar(100),D3 varchar(100),D4 varchar(100),D5 varchar(100),P1 varchar(100),P2 varchar(100),P3 varchar(100),P4 varchar(100),P5 varchar(100))
    DETERMINISTIC
BEGIN
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY i.var_groupid,iu.var_id";
	SET @DETAIL = CONCAT( 
	"SELECT 
	COALESCE(i.var_groupid,'') AS 'itemgroupid',
	COALESCE(ig.var_name,'') AS 'itemgroupname',
	COALESCE(i.var_id,'') AS 'itemid',
	COALESCE(i.var_name,'') AS 'itemname',
	COALESCE(i.var_itemcode,'') AS 'itemcode',
	COALESCE(i.var_barcode,'') AS 'barcode',
	COALESCE(iu.var_uomid,'') AS 'uomid'
	FROM ame_mt_ite i
	LEFT JOIN ame_mt_ite_gro ig ON i.var_groupid = ig.var_id
	LEFT JOIN ame_mt_ite_uom iu ON i.var_id = iu.var_id
	WHERE
	IF('", P1, "' = 'ALL', TRUE, i.var_groupid >= '", P1, "')
	AND
	IF('", P2, "' = 'ALL', TRUE, i.var_groupid <= '", P2, "')
	AND
	IF('", P3, "' = 'ALL', TRUE, i.var_id >= '", P3, "')
	AND
	IF('", P4, "' = 'ALL', TRUE, i.var_id <= '", P4, "') 
	AND
	IF('", P5, "' = 'ALL', TRUE, i.var_type = '", P5, "')");
	SET @SQL_EXC = CONCAT(@DETAIL,@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D007_D2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D007_D2`(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))
    DETERMINISTIC
BEGIN
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY tfd.var_itemid,tfd.var_uomid";
	SET @DETAIL = CONCAT( 
	"SELECT 
	COALESCE(tf.var_id,'') AS 'Trafer_ID',
	COALESCE(tf.var_ref,'') AS 'ref',
	DATE(tf.dat_date) AS 'Trasfer_Date',
	COALESCE(tfd.int_line,0) AS 'LineNo',
	COALESCE(tfd.var_claid,'') AS 'ClaID',
  COALESCE(i.var_groupid,'') AS 'itemgroupid',
	COALESCE(tfd.var_itemid,'') AS 'itemid',
	COALESCE(i.var_name,'') AS 'itemname',
	COALESCE(i.var_itemcode,'') AS 'itemcode',
	COALESCE(tfd.var_uomid,'') AS 'uomid',
	COALESCE(tfd.var_locid,'') AS 'From_LocID',
	COALESCE(tfd.var_tolocid,'') AS 'To_LocID',
	COALESCE(tfd.dou_qty,0) AS 'QTY',
	COALESCE(tf.var_status,'') AS 'PostStatus'
	FROM ame_ic_tf tf
	INNER JOIN ame_ic_tf_det tfd ON tf.var_id = tfd.var_id
	LEFT JOIN ame_mt_ite i ON tfd.var_itemid = i.var_id
	WHERE
	(CASE
	WHEN '", P1, "' = 'ALL' THEN TRUE
	WHEN '", P1, "' = 'THIS TODAY' THEN DATE(tf.dat_date) = DATE(NOW())
	WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(tf.dat_date) = MONTH('", P2, "')
	WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(tf.dat_date) = YEAR('", P2, "')
	ELSE DATE(tf.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END) 
	AND
	IF('", P4, "' = 'ALL', tf.var_status IN('Open', 'Posted'), tf.var_status = '", P4, "')
	AND
	IF('", P5, "' = 'ALL', TRUE, tfd.var_locid>= '", P5, "')
	AND
	IF('", P6, "' = 'ALL', TRUE, tfd.var_locid <= '", P6, "')
	AND
	IF('", P7, "' = 'ALL', TRUE, tfd.var_tolocid >= '", P7, "')
	AND
	IF('", P8, "' = 'ALL', TRUE, tfd.var_tolocid <= '", P8, "') 
	AND
	IF('", P9, "' = 'ALL', TRUE, i.var_groupid >= '", P9, "')
	AND
	IF('", P10, "' = 'ALL', TRUE, i.var_groupid <= '", P10, "')
	AND
	IF('", P11, "' = 'ALL', TRUE, tfd.var_itemid >= '", P11, "')
	AND
	IF('", P12, "' = 'ALL', TRUE, tfd.var_itemid <= '", P12, "') 
	AND
	IF('", P13, "' = 'ALL', TRUE, tfd.var_claid >= '", P13, "')
	AND
	IF('", P14, "' = 'ALL', TRUE, tfd.var_claid <= '", P14, "') ");
	SET @SQL_EXC = CONCAT(@DETAIL,@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D007_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D007_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))
    DETERMINISTIC
BEGIN
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY tf.var_id,tfd.int_line";
	SET @DETAIL = CONCAT( 
	"SELECT 
	COALESCE(tf.var_id,'') AS 'Trafer_ID',
	DATE(tf.dat_date) AS 'Trasfer_Date',
	COALESCE(tfd.int_line,0) AS 'LineNo',
	COALESCE(tfd.var_claid,'') AS 'ClaID',
  COALESCE(i.var_groupid,'') AS 'itemgroupid',
	COALESCE(tfd.var_itemid,'') AS 'itemid',
	COALESCE(i.var_name,'') AS 'itemname',
	COALESCE(i.var_itemcode,'') AS 'itemcode',
	COALESCE(tfd.var_uomid,'') AS 'uomid',
	COALESCE(tfd.var_locid,'') AS 'From_LocID',
	COALESCE(tfd.var_tolocid,'') AS 'To_LocID',
	COALESCE(tfd.dou_qty,0) AS 'QTY',
	COALESCE(tf.var_status,'') AS 'PostStatus'
	FROM ame_ic_tf tf
	INNER JOIN ame_ic_tf_det tfd ON tf.var_id = tfd.var_id
	LEFT JOIN ame_mt_ite i ON tfd.var_itemid = i.var_id
	WHERE
	(CASE
	WHEN '", P1, "' = 'ALL' THEN TRUE
	WHEN '", P1, "' = 'THIS TODAY' THEN DATE(tf.dat_date) = DATE(NOW())
	WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(tf.dat_date) = MONTH('", P2, "')
	WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(tf.dat_date) = YEAR('", P2, "')
	ELSE DATE(tf.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END) 
	AND
	IF('", P4, "' = 'ALL', tf.var_status IN('Open', 'Posted'), tf.var_status = '", P4, "')
	AND
	IF('", P5, "' = 'ALL', TRUE, tfd.var_locid>= '", P5, "')
	AND
	IF('", P6, "' = 'ALL', TRUE, tfd.var_locid <= '", P6, "')
	AND
	IF('", P7, "' = 'ALL', TRUE, tfd.var_tolocid >= '", P7, "')
	AND
	IF('", P8, "' = 'ALL', TRUE, tfd.var_tolocid <= '", P8, "') 
	AND
	IF('", P9, "' = 'ALL', TRUE, i.var_groupid >= '", P9, "')
	AND
	IF('", P10, "' = 'ALL', TRUE, i.var_groupid <= '", P10, "')
	AND
	IF('", P11, "' = 'ALL', TRUE, tfd.var_itemid >= '", P11, "')
	AND
	IF('", P12, "' = 'ALL', TRUE, tfd.var_itemid <= '", P12, "') 
	AND
	IF('", P13, "' = 'ALL', TRUE, tfd.var_claid >= '", P13, "')
	AND
	IF('", P14, "' = 'ALL', TRUE, tfd.var_claid <= '", P14, "') ");
	SET @SQL_EXC = CONCAT(@DETAIL,@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D007_M2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D007_M2`(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))
    DETERMINISTIC
BEGIN
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY tfd.var_itemid,tfd.var_uomid,tfd.var_locid,tfd.var_tolocid";
	SET @DETAIL = CONCAT( 
	"SELECT 
  COALESCE(i.var_groupid,'') AS 'itemgroupid',
	COALESCE(tfd.var_itemid,'') AS 'itemid',
	COALESCE(i.var_name,'') AS 'itemname',
	COALESCE(i.var_itemcode,'') AS 'itemcode',
	COALESCE(tfd.var_uomid,'') AS 'uomid',
	COALESCE(tfd.var_locid,'') AS 'From_LocID',
	COALESCE(tfd.var_tolocid,'') AS 'To_LocID',
	SUM(COALESCE(tfd.dou_qty,0)) AS 'QTY'
	FROM ame_ic_tf tf
	INNER JOIN ame_ic_tf_det tfd ON tf.var_id = tfd.var_id
	LEFT JOIN ame_mt_ite i ON tfd.var_itemid = i.var_id
	WHERE
	(CASE
	WHEN '", P1, "' = 'ALL' THEN TRUE
	WHEN '", P1, "' = 'THIS TODAY' THEN DATE(tf.dat_date) = DATE(NOW())
	WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(tf.dat_date) = MONTH('", P2, "')
	WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(tf.dat_date) = YEAR('", P2, "')
	ELSE DATE(tf.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END) 
	AND
	IF('", P4, "' = 'ALL', tf.var_status IN('Open', 'Posted'), tf.var_status = '", P4, "')
	AND
	IF('", P5, "' = 'ALL', TRUE, tfd.var_locid>= '", P5, "')
	AND
	IF('", P6, "' = 'ALL', TRUE, tfd.var_locid <= '", P6, "')
	AND
	IF('", P7, "' = 'ALL', TRUE, tfd.var_tolocid >= '", P7, "')
	AND
	IF('", P8, "' = 'ALL', TRUE, tfd.var_tolocid <= '", P8, "') 
	AND
	IF('", P9, "' = 'ALL', TRUE, i.var_groupid >= '", P9, "')
	AND
	IF('", P10, "' = 'ALL', TRUE, i.var_groupid <= '", P10, "')
	AND
	IF('", P11, "' = 'ALL', TRUE, tfd.var_itemid >= '", P11, "')
	AND
	IF('", P12, "' = 'ALL', TRUE, tfd.var_itemid <= '", P12, "') 
	AND
	IF('", P13, "' = 'ALL', TRUE, tfd.var_claid >= '", P13, "')
	AND
	IF('", P14, "' = 'ALL', TRUE, tfd.var_claid <= '", P14, "') 
	GROUP BY tfd.var_itemid,tfd.var_uomid,tfd.var_locid,tfd.var_tolocid ");
	SET @SQL_EXC = CONCAT(@DETAIL,@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D008_D2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D008_D2`(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))
    DETERMINISTIC
BEGIN
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY tfd.var_itemid,tfd.var_uomid";
	SET @DETAIL = CONCAT( 
	"SELECT 
	COALESCE(tf.var_id,'') AS 'Trafer_ID',
	COALESCE(tf.var_ref,'') AS 'ref',
	DATE(tf.dat_date) AS 'Trasfer_Date',
	COALESCE(tfd.int_line,0) AS 'LineNo',
	COALESCE(tfd.var_claid,'') AS 'ClaID',
  COALESCE(i.var_groupid,'') AS 'itemgroupid',
	COALESCE(tfd.var_itemid,'') AS 'itemid',
	COALESCE(i.var_name,'') AS 'itemname',
	COALESCE(i.var_itemcode,'') AS 'itemcode',
	COALESCE(tfd.var_uomid,'') AS 'uomid',
	COALESCE(tfd.var_locid,'') AS 'From_LocID',
	COALESCE(tfd.dou_qty,0) AS 'QTY',
	COALESCE(tf.var_status,'') AS 'PostStatus'
	FROM ame_ic_iu tf
	INNER JOIN ame_ic_iu_det tfd ON tf.var_id = tfd.var_id
	LEFT JOIN ame_mt_ite i ON tfd.var_itemid = i.var_id
	WHERE
	(CASE
	WHEN '", P1, "' = 'ALL' THEN TRUE
	WHEN '", P1, "' = 'THIS TODAY' THEN DATE(tf.dat_date) = DATE(NOW())
	WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(tf.dat_date) = MONTH('", P2, "')
	WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(tf.dat_date) = YEAR('", P2, "')
	ELSE DATE(tf.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END) 
	AND
	IF('", P4, "' = 'ALL', tf.var_status IN('Open', 'Posted'), tf.var_status = '", P4, "')
	AND
	IF('", P5, "' = 'ALL', TRUE, tfd.var_locid>= '", P5, "')
	AND
	IF('", P6, "' = 'ALL', TRUE, tfd.var_locid <= '", P6, "')
	AND
	IF('", P7, "' = 'ALL', TRUE, i.var_groupid >= '", P7, "')
	AND
	IF('", P8, "' = 'ALL', TRUE, i.var_groupid <= '", P8, "')
	AND
	IF('", P9, "' = 'ALL', TRUE, tfd.var_itemid >= '", P9, "')
	AND
	IF('", P10, "' = 'ALL', TRUE, tfd.var_itemid <= '", P10, "') 
	AND
	IF('", P11, "' = 'ALL', TRUE, tfd.var_claid >= '", P11, "')
	AND
	IF('", P12, "' = 'ALL', TRUE, tfd.var_claid <= '", P12, "') ");
	SET @SQL_EXC = CONCAT(@DETAIL,@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D008_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D008_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))
    DETERMINISTIC
BEGIN
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY iu.var_id,iud.int_line";
	SET @DETAIL = CONCAT( 
	"SELECT 
	COALESCE(iu.var_id,'') AS 'Trafer_ID',
	DATE(iu.dat_date) AS 'Trasfer_Date',
	COALESCE(iud.int_line,0) AS 'LineNo',
	COALESCE(iud.var_claid,'') AS 'ClaID',
	COALESCE(i.var_groupid,'') AS 'itemgroupid',
	COALESCE(iud.var_itemid,'') AS 'itemid',
	COALESCE(i.var_name,'') AS 'itemname',
	COALESCE(i.var_itemcode,'') AS 'itemcode',
	COALESCE(iud.var_uomid,'') AS 'uomid',
	COALESCE(iud.var_locid,'') AS 'From_LocID',
	COALESCE(iud.dou_qty,0) AS 'QTY',
	COALESCE(iu.var_status,'') AS 'PostStatus'
	FROM ame_ic_iu iu
	INNER JOIN ame_ic_iu_det iud ON iu.var_id = iud.var_id
	LEFT JOIN ame_mt_ite i ON iud.var_itemid = i.var_id
	WHERE
	(CASE
	WHEN '", P1, "' = 'ALL' THEN TRUE
	WHEN '", P1, "' = 'THIS TODAY' THEN DATE(iu.dat_date) = DATE(NOW())
	WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(iu.dat_date) = MONTH('", P2, "')
	WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(iu.dat_date) = YEAR('", P2, "')
	ELSE DATE(iu.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END) 
	AND
	IF('", P4, "' = 'ALL', iu.var_status IN('Open', 'Posted'), iu.var_status = '", P4, "')
	AND
	IF('", P5, "' = 'ALL', TRUE, iud.var_locid>= '", P5, "')
	AND
	IF('", P6, "' = 'ALL', TRUE, iud.var_locid <= '", P6, "')
	AND
	IF('", P7, "' = 'ALL', TRUE, i.var_groupid >= '", P7, "')
	AND
	IF('", P8, "' = 'ALL', TRUE, i.var_groupid <= '", P8, "')
	AND
	IF('", P9, "' = 'ALL', TRUE, iud.var_itemid >= '", P9, "')
	AND
	IF('", P10, "' = 'ALL', TRUE, iud.var_itemid <= '", P10, "') 
	AND
	IF('", P11, "' = 'ALL', TRUE, iud.var_claid >= '", P11, "')
	AND
	IF('", P12, "' = 'ALL', TRUE, iud.var_claid <= '", P12, "') ");
	SET @SQL_EXC = CONCAT(@DETAIL,@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M003D008_M2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M003D008_M2`(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))
    DETERMINISTIC
BEGIN
	#'','','','','','ALL','2018-06-06','2016-06-06','','','','','','','','','','','','','','',''
	SET @ORDERBY = " ORDER BY tfd.var_itemid,tfd.var_uomid,tfd.var_locid";
	SET @DETAIL = CONCAT( 
	"SELECT 
  COALESCE(i.var_groupid,'') AS 'itemgroupid',
	COALESCE(tfd.var_itemid,'') AS 'itemid',
	COALESCE(i.var_name,'') AS 'itemname',
	COALESCE(i.var_itemcode,'') AS 'itemcode',
	COALESCE(tfd.var_uomid,'') AS 'uomid',
	COALESCE(tfd.var_locid,'') AS 'From_LocID',
	SUM(COALESCE(tfd.dou_qty,0)) AS 'QTY'
	FROM ame_ic_iu tf
	INNER JOIN ame_ic_iu_det tfd ON tf.var_id = tfd.var_id
	LEFT JOIN ame_mt_ite i ON tfd.var_itemid = i.var_id
	WHERE
	(CASE
	WHEN '", P1, "' = 'ALL' THEN TRUE
	WHEN '", P1, "' = 'THIS TODAY' THEN DATE(tf.dat_date) = DATE(NOW())
	WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(tf.dat_date) = MONTH('", P2, "')
	WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(tf.dat_date) = YEAR('", P2, "')
	ELSE DATE(tf.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END) 
	AND
	IF('", P4, "' = 'ALL', tf.var_status IN('Open', 'Posted'), tf.var_status = '", P4, "')
	AND
	IF('", P5, "' = 'ALL', TRUE, tfd.var_locid>= '", P5, "')
	AND
	IF('", P6, "' = 'ALL', TRUE, tfd.var_locid <= '", P6, "')
	AND
	IF('", P7, "' = 'ALL', TRUE, i.var_groupid >= '", P7, "')
	AND
	IF('", P8, "' = 'ALL', TRUE, i.var_groupid <= '", P8, "')
	AND
	IF('", P9, "' = 'ALL', TRUE, tfd.var_itemid >= '", P9, "')
	AND
	IF('", P10, "' = 'ALL', TRUE, tfd.var_itemid <= '", P10, "') 
	AND
	IF('", P11, "' = 'ALL', TRUE, tfd.var_claid >= '", P11, "')
	AND
	IF('", P12, "' = 'ALL', TRUE, tfd.var_claid <= '", P12, "') 
	GROUP BY tfd.var_itemid,tfd.var_uomid,tfd.var_locid ");
	SET @SQL_EXC = CONCAT(@DETAIL,@ORDERBY);
	PREPARE STM FROM @SQL_EXC;
	EXECUTE STM;
	DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M004D001_D1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M004D001_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))
    DETERMINISTIC
BEGIN

	(SELECT 
	0 AS 'SortOr',
	COALESCE(g.var_accid,'') AS 'AccID',
	COALESCE(a.var_name,'') AS 'AccName',
	'' AS 'ClassID',
	DATE(P2) AS 'DocDate',
	'' AS 'ModuleID',
	'' AS 'JID',
	'Openning Balance' AS 'DocID',
	'' AS 'DocRef',
	'' AS 'VendName',
	'' AS 'CustName',
	'' AS 'EmpName',
	'' AS 'ItemName',
	'' AS 'var_desc',
	0 AS 'Dr',
	0 AS'Cr',
	CAST(SUM(g.dou_fundr) + SUM(g.dou_funcr) AS DECIMAL(16,2)) AS 'Bal'
	FROM ame_gl g
	LEFT JOIN ame_mt_acc a ON g.var_accid = a.var_id
	WHERE IF(P1 = 'ALL' OR P1 = 'AS OF' OR P1 = 'AS OF RANGE', FALSE, DATE(g.dat_date) < P2) AND
				IF(P4 = 'ALL',TRUE,g.var_accid >= P4) AND IF(P5 = 'ALL',TRUE,g.var_accid <= P5)
  GROUP BY g.var_accid)

	UNION ALL

	(SELECT 
	1 AS 'SortOr',
	COALESCE(g.var_accid,'') AS 'AccID',
	COALESCE(a.var_name,'') AS 'AccName',
	COALESCE(g.var_claid,'') AS 'ClassID',
	DATE(g.dat_date) AS 'DocDate',
	COALESCE(g.var_modid,'') AS 'ModuleID',
	COALESCE(g.int_jid,0) AS 'JID',
	COALESCE(g.var_docid,'') AS 'DocID',
	COALESCE(g.var_docref,'') AS 'DocRef',
	COALESCE(v.var_name,'') AS 'VendName',
	COALESCE(c.var_name,'') AS 'CustName',
	COALESCE(e.var_name,'') AS 'EmpName',
	COALESCE(i.var_name,'') AS 'ItemName',
	COALESCE(g.var_desc,'') AS 'var_desc',
	IF(g.var_drcr = 'Dr',COALESCE(g.dou_funamt,0),0) AS 'Dr',
	IF(g.var_drcr = 'Cr',-COALESCE(g.dou_funamt,0),0) AS 'Cr',
	0 AS 'Bal'
	FROM ame_gl_his g
	LEFT JOIN ame_mt_cus c ON g.var_cusid = c.var_id
	LEFT JOIN ame_mt_ven v ON g.var_venid = v.var_id
	LEFT JOIN ame_mt_acc a ON g.var_accid = a.var_id
	LEFT JOIN ame_mt_emp e ON g.var_empid = e.var_id
	LEFT JOIN ame_mt_ite i ON g.var_itemid = i.var_id
	WHERE ( CASE
					WHEN P1 = 'ALL' THEN TRUE
					WHEN P1 = 'TODAY' THEN DATE(g.dat_date) = DATE(NOW())
				  WHEN P1 = 'THIS PERIOD' THEN MONTH(g.dat_date) = MONTH(P3)
				  WHEN P1 = 'THIS YEAR' THEN YEAR(g.dat_date) = YEAR(P3)
					WHEN P1 = 'AS OF' THEN DATE(g.dat_date) <= DATE(P3)
					WHEN P1 = 'AS OF RANGE' THEN DATE(g.dat_date) BETWEEN DATE(P2) AND DATE(P3)
				  ELSE DATE(g.dat_date) BETWEEN DATE(P2) AND DATE(P3) END
				) AND
				IF(P4 = 'ALL',TRUE,g.var_accid >= P4) AND IF(P5 = 'ALL',TRUE,g.var_accid <= P5) AND
				IF(P6 = 'ALL',TRUE,g.var_claid >= P6) AND IF(P7 = 'ALL',TRUE,g.var_claid <= P7) AND 
				IF(P8 = 'ALL',TRUE,g.var_venid >= P8) AND IF(P9 = 'ALL',TRUE,g.var_venid <= P9) AND
				IF(P10 = 'ALL',TRUE,g.var_cusid >= P10) AND IF(P11 = 'ALL',TRUE,g.var_cusid <= P11) AND
				IF(P12 = 'ALL',TRUE,g.var_empid >= P12) AND IF(P13 = 'ALL',TRUE,g.var_empid <= P13) AND
				IF(P14 = 'ALL',TRUE,g.var_itemid >= P14) AND IF(P15 = 'ALL',TRUE,g.var_itemid <= P15)
	)
	ORDER BY AccID,SortOr,DocDate;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D001_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D001_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))
    DETERMINISTIC
BEGIN
		SELECT T1.var_id AS P_ID, T1.var_name AS P_NAME, IF(T3.P_AMT >= 0, T3.P_AMT, 0) AS P_DR, IF(T3.P_AMT < 0, -T3.P_AMT, 0) AS P_CR
		FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id 
		LEFT JOIN (SELECT T1.var_accid, COALESCE(SUM(T1.dou_fundr), 0) + COALESCE(SUM(T1.dou_funcr), 0) AS P_AMT FROM ame_gl T1 
		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_accid >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T1.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T1.var_claid <= P7)
		GROUP BY T1.var_accid) T3 ON T1.var_id = T3.var_accid
		WHERE 
		IF(P4 = 'ALL', TRUE, T1.var_id >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_id <= P5)
		AND IF(P8 = "0", TRUE, T3.P_AMT <> 0)
		GROUP BY T1.var_id;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D002_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D002_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))
    DETERMINISTIC
BEGIN
		DECLARE P_CY INTEGER DEFAULT 0;
		SET P_CY = YEAR(P3);
		SELECT T1.P_ID, T1.P_NAME,
		T1.M1,
		IF(P1 = "All" OR MONTH(P3) >= 2, T1.M1 + T1.M2, 0) AS M2,
		IF(P1 = "All" OR MONTH(P3) >= 3, T1.M1 + T1.M2 + M3, 0) AS M3,
		IF(P1 = "All" OR MONTH(P3) >= 4, T1.M1 + T1.M2 + M3 + M4, 0) AS M4,
		IF(P1 = "All" OR MONTH(P3) >= 5, T1.M1 + T1.M2 + M3 + M4 + M5, 0) AS M5,
		IF(P1 = "All" OR MONTH(P3) >= 6, T1.M1 + T1.M2 + M3 + M4 + M5 + M6, 0) AS M6,
		IF(P1 = "All" OR MONTH(P3) >= 7, T1.M1 + T1.M2 + M3 + M4 + M5 + M6 + M7, 0) AS M7,
		IF(P1 = "All" OR MONTH(P3) >= 8, T1.M1 + T1.M2 + M3 + M4 + M5 + M6 + M7 + M8, 0) AS M8,
		IF(P1 = "All" OR MONTH(P3) >= 9, T1.M1 + T1.M2 + M3 + M4 + M5 + M6 + M7 + M8 + M9, 0) AS M9,
		IF(P1 = "All" OR MONTH(P3) >= 10, T1.M1 + T1.M2 + M3 + M4 + M5 + M6 + M7 + M8 + M9 + M10, 0) AS M10,
		IF(P1 = "All" OR MONTH(P3) >= 11, T1.M1 + T1.M2 + M3 + M4 + M5 + M6 + M7 + M8 + M9 + M10 + M11, 0) AS M11,
		IF(P1 = "All" OR MONTH(P3) >= 12, T1.M1 + T1.M2 + M3 + M4 + M5 + M6 + M7 + M8 + M9 + M10 + M11 + M12, 0) AS M12
		FROM (
		SELECT T1.P_ID, T1.P_NAME, 
		SUM(IF((T1.P_M = 1 AND T1.P_Y = P_CY) OR T1.P_Y < P_CY, T1.P_AMT, 0)) AS M1, 
		SUM(IF(T1.P_M = 2 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M2,
		SUM(IF(T1.P_M = 3 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M3, 
		SUM(IF(T1.P_M = 4 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M4,
		SUM(IF(T1.P_M = 5 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M5,
		SUM(IF(T1.P_M = 6 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M6,
		SUM(IF(T1.P_M = 7 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M7,
		SUM(IF(T1.P_M = 8 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M8,
		SUM(IF(T1.P_M = 9 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M9,
		SUM(IF(T1.P_M = 10 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M10,
		SUM(IF(T1.P_M = 11 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M11,
		SUM(IF(T1.P_M = 12 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M12
		FROM 
		(SELECT YEAR(T1.dat_date) AS P_Y, MONTH(T1.dat_date) AS P_M, T3.var_id AS P_ID,T3.var_name AS P_NAME,		
		COALESCE(SUM(T1.dou_fundr + T1.dou_funcr), 0) AS P_AMT
    FROM ame_mt_acc T3 LEFT JOIN ame_mt_acc_type T2 ON T3.int_typeid = T2.var_id LEFT JOIN
		ame_gl T1 ON T3.var_id = T1.var_accid
		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_accid >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T1.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T1.var_claid <= P7)
		AND IF(P8 = "0", TRUE, COALESCE(T1.dou_fundr + T1.dou_funcr, 0) <> 0)
		GROUP BY T3.var_id, YEAR(T1.dat_date), MONTH(T1.dat_date)) T1
		GROUP BY T1.P_ID) T1;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D003_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D003_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))
    DETERMINISTIC
BEGIN
		# '', '','', '', '', 'All', '2018-06-30', '2018-06-30', 'All', 'All','All','All', ''
		DECLARE P_CLA VARCHAR(250) DEFAULT "";
		DECLARE D1 INTEGER DEFAULT 0;
		DECLARE P_CLA_MAX INTEGER DEFAULT 0;
		DECLARE C1 CURSOR FOR SELECT DISTINCT COALESCE(T1.var_id,'') AS var_id FROM ame_mt_cla T1 ORDER BY T1.var_id;
		DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
		SET SESSION group_concat_max_len = 1000000;
		SET @sql1 = "";
		SET @sql2 = "";
		SET @sql3 = "";
		SET @IND = 0;
		SET P_CLA_MAX = 9;
		OPEN C1;
				L1: LOOP FETCH C1 INTO P_CLA;
						IF D1 = 1 THEN LEAVE L1; END IF;
								SET @IND = @IND + 1;
								SET @sql1 = CONCAT(@sql1,"COALESCE(SUM(`C",@IND,"`),0) AS `C", @IND,"`,");
								SET @sql2 = CONCAT(@sql2,"IF(T3.var_claid = '",P_CLA,"', COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0),0) AS `C",@IND,"`,");
				END LOOP L1;
		CLOSE C1;
		WHILE(@IND < P_CLA_MAX) DO
			SET @IND = @IND + 1;
			SET @sql1 = CONCAT(@sql1," 0.0 AS `C", @IND,"`,");
			SET @sql2 = CONCAT(@sql2," 0.0 AS `C",@IND,"`,");
		END WHILE;

		SET @sql1 = CONCAT(@sql1,"COALESCE(SUM(`C10`),0) AS `C10`,");
		SET @sql2 = CONCAT(@sql2,"IF(COALESCE(T3.var_claid,'') = '',COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0),0) AS `C10`,");

		IF @sql1 <> "" THEN SET @sql1 =CONCAT(",",LEFT(@sql1, CHAR_LENGTH(@sql1) -1)); END IF;
		IF @sql2 <> "" THEN SET @sql2 = CONCAT(",",LEFT(@sql2, CHAR_LENGTH(@sql2) -1));END IF;
		SET @sql3 = CONCAT("SELECT T1.var_accid AS P_ID, T1.var_name AS P_NAME ",@sql1," FROM
		(SELECT COALESCE(T1.var_id,'') AS var_accid, COALESCE(T1.var_name,'') AS var_name, COALESCE(T3.var_claid,'') AS var_claid",@sql2," FROM 
		ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id LEFT JOIN ame_gl T3 ON T1.var_id = T3.var_accid
		WHERE
		(CASE
		WHEN '",P1,"' = 'ALL' THEN TRUE
		WHEN '",P1,"' = 'TODAY' THEN DATE(T3.dat_date) <= DATE(NOW())
		WHEN '",P1,"' = 'THIS PERIOD' THEN MONTH(T3.dat_date) <= MONTH(NOW())
		WHEN '",P1,"' = 'THIS YEAR' THEN YEAR(T3.dat_date) <= YEAR(NOW())
		ELSE DATE(T3.dat_date) <= DATE('",P3,"') END)
		AND
		IF('",P4,"' = 'ALL', TRUE, T3.var_accid >= '",P4,"') AND IF('",P5,"' = 'ALL', TRUE, T3.var_accid <= '",P5,"') 
		AND
		IF('",P6,"' = 'ALL', TRUE, T3.var_claid >= '",P6,"') AND IF('",P7,"' = 'ALL', TRUE, T3.var_claid <= '",P7,"')
		AND IF('",P8,"' = '0', TRUE, COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0) <> 0)
		) AS T1 
		GROUP BY T1.var_accid ORDER BY T1.var_accid");
		PREPARE stmt FROM @sql3;
		EXECUTE stmt;
		DEALLOCATE PREPARE stmt;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D004_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D004_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))
    DETERMINISTIC
BEGIN
		# '', '','', '', '', 'All', '2018-06-30', '2018-06-30', 'All', 'All','All','All', ''
		(
		SELECT T1.var_id AS P_ID, T1.var_name AS P_NAME, T2.var_id AS P_TYPE_ID, T2.var_name AS P_TYPE_NAME,
		T2.var_group1 AS P_GRO_1, T2.var_group2 AS P_GRO_2, T2.var_group3 AS P_GRO_3, T2.var_group4 AS P_GRO_4, T2.var_group5 AS P_GRO_5, 
		-COALESCE(T3.P_AMT, 0) AS P_AMT
		FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id 
		LEFT JOIN (SELECT T1.var_accid, COALESCE(SUM(T1.dou_fundr), 0) + COALESCE(SUM(T1.dou_funcr), 0) AS P_AMT FROM ame_gl T1 
		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_accid >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T1.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T1.var_claid <= P7)
		GROUP BY T1.var_accid) T3 ON T1.var_id = T3.var_accid
		WHERE 
		IF(P4 = 'ALL', TRUE, T1.var_id >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_id <= P5)
		AND IF(P8 = "0", TRUE, T3.P_AMT <> 0)
		AND T2.var_group3 = 'P'
		GROUP BY T1.var_id
		) 
		;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D005_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D005_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))
    DETERMINISTIC
BEGIN
		# '', '','', '', '', 'All', '2018-06-30', '2018-06-30', 'All', 'All','All','All', ''
		(
		SELECT T1.var_id AS P_ID, T1.var_name AS P_NAME, T2.var_id AS P_TYPE_ID, T2.var_name AS P_TYPE_NAME,
		T2.var_group1 AS P_GRO_1, T2.var_group2 AS P_GRO_2, T2.var_group3 AS P_GRO_3, T2.var_group4 AS P_GRO_4, T2.var_group5 AS P_GRO_5,  
		SUM(IF(T3.P_M = 1, T3.P_AMT, 0)) AS M1, 
		SUM(IF(T3.P_M = 2, T3.P_AMT, 0)) AS M2,
		SUM(IF(T3.P_M = 3, T3.P_AMT, 0)) AS M3, 
		SUM(IF(T3.P_M = 4, T3.P_AMT, 0)) AS M4,
		SUM(IF(T3.P_M = 5, T3.P_AMT, 0)) AS M5,
		SUM(IF(T3.P_M = 6, T3.P_AMT, 0)) AS M6,
		SUM(IF(T3.P_M = 7, T3.P_AMT, 0)) AS M7,
		SUM(IF(T3.P_M = 8, T3.P_AMT, 0)) AS M8,
		SUM(IF(T3.P_M = 9, T3.P_AMT, 0)) AS M9,
		SUM(IF(T3.P_M = 10, T3.P_AMT, 0)) AS M10,
		SUM(IF(T3.P_M = 11, T3.P_AMT, 0)) AS M11,
		SUM(IF(T3.P_M = 12, T3.P_AMT, 0)) AS M12
		FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id 
		LEFT JOIN (SELECT YEAR(T1.dat_date) AS P_Y, MONTH(T1.dat_date) AS P_M, T1.var_accid, -COALESCE(SUM(T1.dou_fundr), 0) -COALESCE(SUM(T1.dou_funcr), 0) AS P_AMT FROM ame_gl T1 
		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 YEAR(T1.dat_date) = YEAR(P3)
		AND
		IF(P4 = 'ALL', TRUE, T1.var_accid >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T1.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T1.var_claid <= P7)
		GROUP BY T1.var_accid, YEAR(T1.dat_date), MONTH(T1.dat_date)) T3 ON T1.var_id = T3.var_accid
		WHERE 
		IF(P4 = 'ALL', TRUE, T1.var_id >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_id <= P5)
		AND IF(P8 = "0", TRUE, T3.P_AMT <> 0)
		AND T2.var_group3 = 'P'
		GROUP BY T1.var_id
		)
		;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D006_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D006_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))
    DETERMINISTIC
BEGIN
		# '', '','', '', '', 'All', '2018-06-30', '2018-06-30', 'All', 'All','All','All', ''
		DECLARE P_CLA VARCHAR(250) DEFAULT "";
		DECLARE D1 INTEGER DEFAULT 0;
		DECLARE P_CLA_MAX INTEGER DEFAULT 0;
		DECLARE C1 CURSOR FOR SELECT DISTINCT COALESCE(T1.var_id,'') AS var_id FROM ame_mt_cla T1 ORDER BY T1.var_id;
		DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
		SET SESSION group_concat_max_len = 1000000;
		SET @sql1 = "";
		SET @sql2 = "";
		SET @sql3 = "";
		SET @IND = 0;
		SET P_CLA_MAX = 9;
		OPEN C1;
				L1: LOOP FETCH C1 INTO P_CLA;
						IF D1 = 1 THEN LEAVE L1; END IF;
								SET @IND = @IND + 1;
								SET @sql1 = CONCAT(@sql1,"COALESCE(SUM(`C",@IND,"`),0) AS `C", @IND,"`,");
								SET @sql2 = CONCAT(@sql2,"IF(T3.var_claid = '",P_CLA,"', -(COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0)),0) AS `C",@IND,"`,");
				END LOOP L1;
		CLOSE C1;
		WHILE(@IND < P_CLA_MAX) DO
			SET @IND = @IND + 1;
			SET @sql1 = CONCAT(@sql1," 0.0 AS `C", @IND,"`,");
			SET @sql2 = CONCAT(@sql2," 0.0 AS `C",@IND,"`,");
		END WHILE;

		SET @sql1 = CONCAT(@sql1,"COALESCE(SUM(`C10`),0) AS `C10`,");
		SET @sql2 = CONCAT(@sql2,"IF(COALESCE(T3.var_claid,'') = '', -(COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0)),0) AS `C10`,");

		IF @sql1 <> "" THEN SET @sql1 =CONCAT(",",LEFT(@sql1, CHAR_LENGTH(@sql1) -1)); END IF;
		IF @sql2 <> "" THEN SET @sql2 = CONCAT(",",LEFT(@sql2, CHAR_LENGTH(@sql2) -1));END IF;
		SET @sql3 = CONCAT("SELECT T1.var_accid AS P_ID, T1.var_name AS P_NAME, T1.P_TYPE_ID, T1.P_TYPE_NAME,
		T1.P_GRO_1, T1.P_GRO_2, T1.P_GRO_3, T1.P_GRO_4, T1.P_GRO_5 ",@sql1," FROM
		(SELECT COALESCE(T1.var_id,'') AS var_accid, COALESCE(T1.var_name,'') AS var_name, T2.var_id AS P_TYPE_ID, T2.var_name AS P_TYPE_NAME,
		T2.var_group1 AS P_GRO_1, T2.var_group2 AS P_GRO_2, T2.var_group3 AS P_GRO_3, T2.var_group4 AS P_GRO_4, T2.var_group5 AS P_GRO_5, COALESCE(T3.var_claid,'') AS var_claid",@sql2," FROM 
		ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id LEFT JOIN ame_gl T3 ON T1.var_id = T3.var_accid
		WHERE
		(CASE
		WHEN '",P1,"' = 'ALL' THEN TRUE
		WHEN '",P1,"' = 'TODAY' THEN DATE(T3.dat_date) <= DATE(NOW())
		WHEN '",P1,"' = 'THIS PERIOD' THEN MONTH(T3.dat_date) <= MONTH(NOW())
		WHEN '",P1,"' = 'THIS YEAR' THEN YEAR(T3.dat_date) <= YEAR(NOW())
		ELSE DATE(T3.dat_date) <= DATE('",P3,"') END)
		AND
		IF('",P4,"' = 'ALL', TRUE, T3.var_accid >= '",P4,"') AND IF('",P5,"' = 'ALL', TRUE, T3.var_accid <= '",P5,"') 
		AND
		IF('",P6,"' = 'ALL', TRUE, T3.var_claid >= '",P6,"') AND IF('",P7,"' = 'ALL', TRUE, T3.var_claid <= '",P7,"')
		AND IF('",P8,"' = '0', TRUE, COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0) <> 0)
		AND T2.var_group3 = 'P'
		) AS T1 
		GROUP BY T1.var_accid ORDER BY T1.var_accid");
		PREPARE stmt FROM @sql3;
		EXECUTE stmt;
		DEALLOCATE PREPARE stmt;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D007_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D007_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))
    DETERMINISTIC
BEGIN
		# '', '','', '', '', 'All', '2018-06-30', '2018-06-30', 'All', 'All','All','All', ''
		DECLARE P_ACC_TYPE_ID INTEGER DEFAULT 0;
		DECLARE P_ACC_TYPE_NAME VARCHAR(250) DEFAULT "";
		DECLARE P_ACC VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_NAME VARCHAR(250) DEFAULT "";
		DECLARE P_ACC_GRO_1 VARCHAR(250) DEFAULT "";
		DECLARE P_ACC_GRO_2 VARCHAR(250) DEFAULT "";
		DECLARE P_ACC_GRO_3 VARCHAR(250) DEFAULT "";
		DECLARE P_ACC_GRO_4 VARCHAR(250) DEFAULT "";
		DECLARE P_ACC_GRO_5 VARCHAR(250) DEFAULT "";
		BEGIN
				DECLARE C1 CURSOR FOR SELECT T1.var_id AS P_ID,  T1.var_name AS P_NAME, T2.var_id AS P_TYPE_ID, T2.var_name AS P_TYPE_NAME,
				T2.var_group1 AS P_GRO_1, T2.var_group2 AS P_GRO_2, T2.var_group3 AS P_GRO_3, T2.var_group4 AS P_GRO_4, T2.var_group5 AS P_GRO_5 FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id WHERE T1.var_id = AME_FUN_ACC("OE", "DEFAULT", "DEFAULT");
				OPEN C1;
						IF FOUND_ROWS() > 0 THEN
								FETCH C1 INTO P_ACC, P_ACC_NAME, P_ACC_TYPE_ID, P_ACC_TYPE_NAME, P_ACC_GRO_1, P_ACC_GRO_2, P_ACC_GRO_3, P_ACC_GRO_4, P_ACC_GRO_5;
						END IF;
				CLOSE C1;
		END;
		(
		SELECT T1.var_id AS P_ID, T1.var_name AS P_NAME, T2.var_id AS P_TYPE_ID, T2.var_name AS P_TYPE_NAME,
		T2.var_group1 AS P_GRO_1, T2.var_group2 AS P_GRO_2, T2.var_group3 AS P_GRO_3, T2.var_group4 AS P_GRO_4, T2.var_group5 AS P_GRO_5, 
		IF(T2.var_group2 = 'Assets', COALESCE(T3.P_AMT, 0), -COALESCE(T3.P_AMT, 0)) AS P_AMT
		FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id 
		LEFT JOIN (SELECT T1.var_accid, COALESCE(SUM(T1.dou_fundr), 0) + COALESCE(SUM(T1.dou_funcr), 0) AS P_AMT FROM ame_gl T1 
		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_accid >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T1.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T1.var_claid <= P7)
		GROUP BY T1.var_accid) T3 ON T1.var_id = T3.var_accid
		WHERE 
		IF(P4 = 'ALL', TRUE, T1.var_id >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_id <= P5)
		AND IF(P8 = "0", TRUE, T3.P_AMT <> 0)
		AND T2.var_group3 = 'B'
		GROUP BY T1.var_id
		) 
		UNION ALL
		(
		SELECT P_ACC AS P_ID, P_ACC_NAME AS P_NAME, P_ACC_TYPE_ID AS P_TYPE_ID, P_ACC_TYPE_NAME AS P_TYPE_NAME,
		P_ACC_GRO_1 AS P_GRO_1, P_ACC_GRO_2 AS P_GRO_2, P_ACC_GRO_3 AS P_GRO_3, P_ACC_GRO_4 AS P_GRO_4, P_ACC_GRO_5 AS P_GRO_5,
		-COALESCE(SUM(T3.dou_fundr), 0) - COALESCE(SUM(T3.dou_funcr), 0) AS P_AMT
		FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id 
		LEFT JOIN ame_gl T3 ON T1.var_id = T3.var_accid
		WHERE
		(CASE
		WHEN P1 = 'ALL' THEN TRUE
		WHEN P1 = 'TODAY' THEN DATE(T3.dat_date) <= DATE(NOW())
		WHEN P1 = 'THIS PERIOD' THEN MONTH(T3.dat_date) <= MONTH(NOW())
		WHEN P1 = 'THIS YEAR' THEN YEAR(T3.dat_date) <= YEAR(NOW())
		ELSE DATE(T3.dat_date) <= DATE(P3) END)
		AND YEAR(T3.dat_date) < YEAR(P3)
		AND
		IF(P4 = 'ALL', TRUE, T3.var_accid >= P4) AND IF(P5 = 'ALL', TRUE, T3.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T3.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T3.var_claid <= P7)
		AND T2.var_group3 = 'P'
		)
		UNION ALL
		(
		SELECT '999999' AS P_ID, 'Retained Earnings - Current Year' AS P_NAME, 350 AS P_TYPE_ID, 'Retained Earnings' AS P_TYPE_NAME,
		'Owner\'s Equity' AS P_GRO_1, 'Liabilities & Equity' AS P_GRO_2, 'B' AS P_GRO_3, '' AS P_GRO_4, '' AS P_GRO_5, 
		-COALESCE(SUM(T3.dou_fundr), 0) - COALESCE(SUM(T3.dou_funcr), 0) AS P_AMT
		FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id 
		LEFT JOIN ame_gl T3 ON T1.var_id = T3.var_accid
		WHERE
		(CASE
		WHEN P1 = 'ALL' THEN TRUE
		WHEN P1 = 'TODAY' THEN DATE(T3.dat_date) <= DATE(NOW())
		WHEN P1 = 'THIS PERIOD' THEN MONTH(T3.dat_date) <= MONTH(NOW())
		WHEN P1 = 'THIS YEAR' THEN YEAR(T3.dat_date) <= YEAR(NOW())
		ELSE DATE(T3.dat_date) <= DATE(P3) END)
		AND YEAR(T3.dat_date) = YEAR(P3)
		AND
		IF(P4 = 'ALL', TRUE, T3.var_accid >= P4) AND IF(P5 = 'ALL', TRUE, T3.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T3.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T3.var_claid <= P7)
		AND T2.var_group3 = 'P'
		)
		;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D008_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D008_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))
    DETERMINISTIC
BEGIN
		# '', '','', '', '', 'All', '2018-06-30', '2018-06-30', 'All', 'All','All','All', ''
		DECLARE P_CY INTEGER DEFAULT 0;
		DECLARE P_ACC_TYPE_ID INTEGER DEFAULT 0;
		DECLARE P_ACC_TYPE_NAME VARCHAR(250) DEFAULT "";
		DECLARE P_ACC VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_NAME VARCHAR(250) DEFAULT "";
		DECLARE P_ACC_GRO_1 VARCHAR(250) DEFAULT "";
		DECLARE P_ACC_GRO_2 VARCHAR(250) DEFAULT "";
		DECLARE P_ACC_GRO_3 VARCHAR(250) DEFAULT "";
		DECLARE P_ACC_GRO_4 VARCHAR(250) DEFAULT "";
		DECLARE P_ACC_GRO_5 VARCHAR(250) DEFAULT "";
		BEGIN
				DECLARE C1 CURSOR FOR SELECT T1.var_id AS P_ID,  T1.var_name AS P_NAME, T2.var_id AS P_TYPE_ID, T2.var_name AS P_TYPE_NAME,
				T2.var_group1 AS P_GRO_1, T2.var_group2 AS P_GRO_2, T2.var_group3 AS P_GRO_3, T2.var_group4 AS P_GRO_4, T2.var_group5 AS P_GRO_5 FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id WHERE T1.var_id = AME_FUN_ACC("OE", "DEFAULT", "DEFAULT");
				OPEN C1;
						IF FOUND_ROWS() > 0 THEN
								FETCH C1 INTO P_ACC, P_ACC_NAME, P_ACC_TYPE_ID, P_ACC_TYPE_NAME, P_ACC_GRO_1, P_ACC_GRO_2, P_ACC_GRO_3, P_ACC_GRO_4, P_ACC_GRO_5;
						END IF;
				CLOSE C1;
		END;
		SET P_CY = YEAR(P3);
		SELECT T1.P_ID, T1.P_NAME, T1.P_TYPE_ID, T1.P_TYPE_NAME,
		T1.P_GRO_1, T1.P_GRO_2, T1.P_GRO_3, T1.P_GRO_4, T1.P_GRO_5, 
		COALESCE(T1.M1, 0) AS M1,
		IF(P1 = "All" OR MONTH(P3) >= 2, COALESCE(T1.M1, 0) + COALESCE(T1.M2, 0), 0) AS M2,
		IF(P1 = "All" OR MONTH(P3) >= 3, COALESCE(T1.M1, 0) + COALESCE(T1.M2, 0) + COALESCE(M3, 0), 0) AS M3,
		IF(P1 = "All" OR MONTH(P3) >= 4, COALESCE(T1.M1, 0) + COALESCE(T1.M2, 0) + COALESCE(M3, 0) + COALESCE(M4, 0), 0) AS M4,
		IF(P1 = "All" OR MONTH(P3) >= 5, COALESCE(T1.M1, 0) + COALESCE(T1.M2, 0) + COALESCE(M3, 0) + COALESCE(M4, 0) + COALESCE(M5, 0), 0) AS M5,
		IF(P1 = "All" OR MONTH(P3) >= 6, COALESCE(T1.M1, 0) + COALESCE(T1.M2, 0) + COALESCE(M3, 0) + COALESCE(M4, 0) + COALESCE(M5, 0) + COALESCE(M6, 0), 0) AS M6,
		IF(P1 = "All" OR MONTH(P3) >= 7, COALESCE(T1.M1, 0) + COALESCE(T1.M2, 0) + COALESCE(M3, 0) + COALESCE(M4, 0) + COALESCE(M5, 0) + COALESCE(M6, 0) + COALESCE(M7, 0), 0) AS M7,
		IF(P1 = "All" OR MONTH(P3) >= 8, COALESCE(T1.M1, 0) + COALESCE(T1.M2, 0) + COALESCE(M3, 0) + COALESCE(M4, 0) + COALESCE(M5, 0) + COALESCE(M6, 0) + COALESCE(M7, 0) + COALESCE(M8, 0), 0) AS M8,
		IF(P1 = "All" OR MONTH(P3) >= 9, COALESCE(T1.M1, 0) + COALESCE(T1.M2, 0) + COALESCE(M3, 0) + COALESCE(M4, 0) + COALESCE(M5, 0) + COALESCE(M6, 0) + COALESCE(M7, 0) + COALESCE(M8, 0) + COALESCE(M9, 0), 0) AS M9,
		IF(P1 = "All" OR MONTH(P3) >= 10, COALESCE(T1.M1, 0) + COALESCE(T1.M2, 0) + COALESCE(M3, 0) + COALESCE(M4, 0) + COALESCE(M5, 0) + COALESCE(M6, 0) + COALESCE(M7, 0) + COALESCE(M8, 0) + COALESCE(M9, 0) + COALESCE(M10, 0), 0) AS M10,
		IF(P1 = "All" OR MONTH(P3) >= 11, COALESCE(T1.M1, 0) + COALESCE(T1.M2, 0) + COALESCE(M3, 0) + COALESCE(M4, 0) + COALESCE(M5, 0) + COALESCE(M6, 0) + COALESCE(M7, 0) + COALESCE(M8, 0) + COALESCE(M9, 0) + COALESCE(M10, 0) + COALESCE(M11, 0), 0) AS M11,
		IF(P1 = "All" OR MONTH(P3) >= 12, COALESCE(T1.M1, 0) + COALESCE(T1.M2, 0) + COALESCE(M3, 0) + COALESCE(M4, 0) + COALESCE(M5, 0) + COALESCE(M6, 0) + COALESCE(M7, 0) + COALESCE(M8, 0) + COALESCE(M9, 0) + COALESCE(M10, 0) + COALESCE(M11, 0) + COALESCE(M12, 0), 0) AS M12
		FROM ((
		SELECT T1.P_ID, T1.P_NAME, T1.P_TYPE_ID, T1.P_TYPE_NAME,
		T1.P_GRO_1, T1.P_GRO_2, T1.P_GRO_3, T1.P_GRO_4, T1.P_GRO_5,  
		SUM(IF((T1.P_M = 1 AND T1.P_Y = P_CY) OR T1.P_Y < P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M1, 
		SUM(IF(T1.P_M = 2 AND T1.P_Y = P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M2,
		SUM(IF(T1.P_M = 3 AND T1.P_Y = P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M3, 
		SUM(IF(T1.P_M = 4 AND T1.P_Y = P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M4,
		SUM(IF(T1.P_M = 5 AND T1.P_Y = P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M5,
		SUM(IF(T1.P_M = 6 AND T1.P_Y = P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M6,
		SUM(IF(T1.P_M = 7 AND T1.P_Y = P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M7,
		SUM(IF(T1.P_M = 8 AND T1.P_Y = P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M8,
		SUM(IF(T1.P_M = 9 AND T1.P_Y = P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M9,
		SUM(IF(T1.P_M = 10 AND T1.P_Y = P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M10,
		SUM(IF(T1.P_M = 11 AND T1.P_Y = P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M11,
		SUM(IF(T1.P_M = 12 AND T1.P_Y = P_CY, IF(T1.P_GRO_2 = "Assets", T1.P_AMT, -T1.P_AMT), 0)) AS M12
		FROM 
		(
				SELECT YEAR(T3.dat_date) AS P_Y, MONTH(T3.dat_date) AS P_M, T1.var_id AS P_ID,  T1.var_name AS P_NAME, T2.var_id AS P_TYPE_ID, T2.var_name AS P_TYPE_NAME,
				T2.var_group1 AS P_GRO_1, T2.var_group2 AS P_GRO_2, T2.var_group3 AS P_GRO_3, T2.var_group4 AS P_GRO_4, T2.var_group5 AS P_GRO_5,
				COALESCE(SUM(T3.dou_fundr), 0) + COALESCE(SUM(T3.dou_funcr), 0) AS P_AMT 
				FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id LEFT JOIN ame_gl T3 ON T1.var_id = T3.var_accid
				WHERE
				(CASE
				WHEN P1 = 'ALL' THEN TRUE
				WHEN P1 = 'TODAY' THEN DATE(T3.dat_date) <= DATE(NOW())
				WHEN P1 = 'THIS PERIOD' THEN MONTH(T3.dat_date) <= MONTH(NOW())
				WHEN P1 = 'THIS YEAR' THEN YEAR(T3.dat_date) <= YEAR(NOW())
				ELSE DATE(T3.dat_date) <= DATE(P3) END)
				AND
				IF(P4 = 'ALL', TRUE, T1.var_id >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_id <= P5) 
				AND
				IF(P6 = 'ALL', TRUE, T3.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T3.var_claid <= P7)
				AND IF(P8 = "0", TRUE, COALESCE(T3.dou_fundr + T3.dou_funcr, 0) <> 0)
				AND T2.var_group3 = 'B'
				GROUP BY T1.var_id, YEAR(T3.dat_date), MONTH(T3.dat_date)
		) T1 GROUP BY T1.P_ID
		)
		UNION ALL
		(
		SELECT P_ACC AS P_ID, P_ACC_NAME AS P_NAME, P_ACC_TYPE_ID AS P_TYPE_ID, P_ACC_TYPE_NAME AS P_TYPE_NAME,
		P_ACC_GRO_1 AS P_GRO_1, P_ACC_GRO_2 AS P_GRO_2, P_ACC_GRO_3 AS P_GRO_3, P_ACC_GRO_4 AS P_GRO_4, P_ACC_GRO_5 AS P_GRO_5,   
		SUM(IF((T1.P_M = 1 AND T1.P_Y = P_CY) OR T1.P_Y < P_CY, T1.P_AMT, 0)) AS M1, 
		SUM(IF(T1.P_M = 2 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M2,
		SUM(IF(T1.P_M = 3 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M3, 
		SUM(IF(T1.P_M = 4 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M4,
		SUM(IF(T1.P_M = 5 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M5,
		SUM(IF(T1.P_M = 6 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M6,
		SUM(IF(T1.P_M = 7 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M7,
		SUM(IF(T1.P_M = 8 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M8,
		SUM(IF(T1.P_M = 9 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M9,
		SUM(IF(T1.P_M = 10 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M10,
		SUM(IF(T1.P_M = 11 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M11,
		SUM(IF(T1.P_M = 12 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M12
		FROM 
		(
				SELECT YEAR(T3.dat_date) AS P_Y, MONTH(T3.dat_date) AS P_M, T1.var_id AS P_ID,  T1.var_name AS P_NAME, T2.var_id AS P_TYPE_ID, T2.var_name AS P_TYPE_NAME,
				T2.var_group1 AS P_GRO_1, T2.var_group2 AS P_GRO_2, T2.var_group3 AS P_GRO_3, T2.var_group4 AS P_GRO_4, T2.var_group5 AS P_GRO_5,
				-COALESCE(SUM(T3.dou_fundr), 0) - COALESCE(SUM(T3.dou_funcr), 0) AS P_AMT 
				FROM ame_mt_acc T1 INNER JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id INNER JOIN ame_gl T3 ON T1.var_id = T3.var_accid
				WHERE
				(CASE
				WHEN P1 = 'ALL' THEN TRUE
				WHEN P1 = 'TODAY' THEN DATE(T3.dat_date) <= DATE(NOW())
				WHEN P1 = 'THIS PERIOD' THEN MONTH(T3.dat_date) <= MONTH(NOW())
				WHEN P1 = 'THIS YEAR' THEN YEAR(T3.dat_date) <= YEAR(NOW())
				ELSE DATE(T3.dat_date) <= DATE(P3) END)
				AND YEAR(T3.dat_date) < YEAR(P3)
				AND
				IF(P4 = 'ALL', TRUE, T1.var_id >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_id <= P5) 
				AND
				IF(P6 = 'ALL', TRUE, T3.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T3.var_claid <= P7)
				AND COALESCE(T3.dou_fundr + T3.dou_funcr, 0) <> 0
				AND T2.var_group3 = 'P'
				GROUP BY T1.var_id, YEAR(T3.dat_date), MONTH(T3.dat_date)
		) T1
		)
		UNION ALL
		(
		SELECT '999999' AS P_ID, 'Retained Earnings - Current Year' AS P_NAME, 350 AS P_TYPE_ID, 'Retained Earnings' AS P_TYPE_NAME,
		'Owner\'s Equity' AS P_GRO_1, 'Liabilities & Equity' AS P_GRO_2, 'B' AS P_GRO_3, '' AS P_GRO_4, '' AS P_GRO_5, 
		SUM(IF((T1.P_M = 1 AND T1.P_Y = P_CY) OR T1.P_Y < P_CY, T1.P_AMT, 0)) AS M1, 
		SUM(IF(T1.P_M = 2 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M2,
		SUM(IF(T1.P_M = 3 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M3, 
		SUM(IF(T1.P_M = 4 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M4,
		SUM(IF(T1.P_M = 5 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M5,
		SUM(IF(T1.P_M = 6 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M6,
		SUM(IF(T1.P_M = 7 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M7,
		SUM(IF(T1.P_M = 8 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M8,
		SUM(IF(T1.P_M = 9 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M9,
		SUM(IF(T1.P_M = 10 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M10,
		SUM(IF(T1.P_M = 11 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M11,
		SUM(IF(T1.P_M = 12 AND T1.P_Y = P_CY, T1.P_AMT, 0)) AS M12
		FROM 
		(
				SELECT YEAR(T3.dat_date) AS P_Y, MONTH(T3.dat_date) AS P_M, T1.var_id AS P_ID,  T1.var_name AS P_NAME, T2.var_id AS P_TYPE_ID, T2.var_name AS P_TYPE_NAME,
				T2.var_group1 AS P_GRO_1, T2.var_group2 AS P_GRO_2, T2.var_group3 AS P_GRO_3, T2.var_group4 AS P_GRO_4, T2.var_group5 AS P_GRO_5,
				-COALESCE(SUM(T3.dou_fundr), 0) - COALESCE(SUM(T3.dou_funcr), 0) AS P_AMT 
				FROM ame_mt_acc T1 INNER JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id INNER JOIN ame_gl T3 ON T1.var_id = T3.var_accid
				WHERE
				(CASE
				WHEN P1 = 'ALL' THEN TRUE
				WHEN P1 = 'TODAY' THEN DATE(T3.dat_date) <= DATE(NOW())
				WHEN P1 = 'THIS PERIOD' THEN MONTH(T3.dat_date) <= MONTH(NOW())
				WHEN P1 = 'THIS YEAR' THEN YEAR(T3.dat_date) <= YEAR(NOW())
				ELSE DATE(T3.dat_date) <= DATE(P3) END)
				AND YEAR(T3.dat_date) = YEAR(P3)
				AND
				IF(P4 = 'ALL', TRUE, T1.var_id >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_id <= P5) 
				AND
				IF(P6 = 'ALL', TRUE, T3.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T3.var_claid <= P7)
				AND COALESCE(T3.dou_fundr + T3.dou_funcr, 0) <> 0
				AND T2.var_group3 = 'P'
				GROUP BY T1.var_id, YEAR(T3.dat_date), MONTH(T3.dat_date)
		) T1
		)
		) AS T1;
	#SELECT @P_ACC;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D009_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D009_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))
    DETERMINISTIC
BEGIN
		# '', '','', '', '', 'All', '2018-06-30', '2018-06-30', 'All', 'All','All','All', ''
		SET @SQL = "";
		BEGIN
				DECLARE P_CLA VARCHAR(250) DEFAULT "";
				DECLARE D1 INTEGER DEFAULT 0;
				DECLARE P_CLA_MAX INTEGER DEFAULT 0;
				DECLARE C1 CURSOR FOR SELECT DISTINCT COALESCE(T1.var_id,'') AS var_id FROM ame_mt_cla T1 ORDER BY T1.var_id;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				SET SESSION group_concat_max_len = 1000000;
				SET @sql1 = "";
				SET @sql2 = "";
				SET @sql3 = "";
				SET @IND = 0;
				SET P_CLA_MAX = 9;
				OPEN C1;
						L1: LOOP FETCH C1 INTO P_CLA;
								IF D1 = 1 THEN LEAVE L1; END IF;
										SET @IND = @IND + 1;
										SET @sql1 = CONCAT(@sql1,"COALESCE(SUM(`C",@IND,"`),0) AS `C", @IND,"`,");
										SET @sql2 = CONCAT(@sql2,"IF(T3.var_claid = '",P_CLA,"', IF(T2.var_group2 = 'Assets', COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0), -(COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0))), 0) AS `C",@IND,"`,");
						END LOOP L1;
				CLOSE C1;
				WHILE(@IND < P_CLA_MAX) DO
					SET @IND = @IND + 1;
					SET @sql1 = CONCAT(@sql1," 0.0 AS `C", @IND,"`,");
					SET @sql2 = CONCAT(@sql2," 0.0 AS `C",@IND,"`,");
				END WHILE;

				SET @sql1 = CONCAT(@sql1,"COALESCE(SUM(`C10`),0) AS `C10`,");
				SET @sql2 = CONCAT(@sql2,"IF(COALESCE(T3.var_claid,'') = '', IF(T2.var_group2 = 'Assets', COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0), -(COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0))), 0) AS `C10`,");

				IF @sql1 <> "" THEN SET @sql1 =CONCAT(",",LEFT(@sql1, CHAR_LENGTH(@sql1) -1)); END IF;
				IF @sql2 <> "" THEN SET @sql2 = CONCAT(",",LEFT(@sql2, CHAR_LENGTH(@sql2) -1));END IF;
				SET @sql3 = CONCAT("SELECT T1.var_accid AS P_ID, T1.var_name AS P_NAME, T1.P_TYPE_ID, T1.P_TYPE_NAME,
				T1.P_GRO_1, T1.P_GRO_2, T1.P_GRO_3, T1.P_GRO_4, T1.P_GRO_5 ",@sql1," FROM
				(SELECT COALESCE(T1.var_id,'') AS var_accid, COALESCE(T1.var_name,'') AS var_name, T2.var_id AS P_TYPE_ID, T2.var_name AS P_TYPE_NAME,
				T2.var_group1 AS P_GRO_1, T2.var_group2 AS P_GRO_2, T2.var_group3 AS P_GRO_3, T2.var_group4 AS P_GRO_4, T2.var_group5 AS P_GRO_5, COALESCE(T3.var_claid,'') AS var_claid",@sql2," FROM 
				ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id LEFT JOIN ame_gl T3 ON T1.var_id = T3.var_accid
				WHERE
				(CASE
				WHEN '",P1,"' = 'ALL' THEN TRUE
				WHEN '",P1,"' = 'TODAY' THEN DATE(T3.dat_date) <= DATE(NOW())
				WHEN '",P1,"' = 'THIS PERIOD' THEN MONTH(T3.dat_date) <= MONTH(NOW())
				WHEN '",P1,"' = 'THIS YEAR' THEN YEAR(T3.dat_date) <= YEAR(NOW())
				ELSE DATE(T3.dat_date) <= DATE('",P3,"') END)
				AND
				IF('",P4,"' = 'ALL', TRUE, T3.var_accid >= '",P4,"') AND IF('",P5,"' = 'ALL', TRUE, T3.var_accid <= '",P5,"') 
				AND
				IF('",P6,"' = 'ALL', TRUE, T3.var_claid >= '",P6,"') AND IF('",P7,"' = 'ALL', TRUE, T3.var_claid <= '",P7,"')
				AND IF('",P8,"' = '0', TRUE, COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0) <> 0)
				AND T2.var_group3 = 'B'
				) AS T1 
				GROUP BY T1.var_accid ORDER BY T1.var_accid");
				SET @SQL = CONCAT(@SQL,@sql3);
		END;	
		BEGIN
				DECLARE P_CLA VARCHAR(250) DEFAULT "";
				DECLARE D1 INTEGER DEFAULT 0;
				DECLARE P_CLA_MAX INTEGER DEFAULT 0;
				DECLARE C1 CURSOR FOR SELECT DISTINCT COALESCE(T1.var_id,'') AS var_id FROM ame_mt_cla T1 ORDER BY T1.var_id;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				SET SESSION group_concat_max_len = 1000000;
				SET @sql1 = "";
				SET @sql2 = "";
				SET @sql3 = "";
				SET @IND = 0;
				SET P_CLA_MAX = 9;
				OPEN C1;
						L1: LOOP FETCH C1 INTO P_CLA;
								IF D1 = 1 THEN LEAVE L1; END IF;
										SET @IND = @IND + 1;
										SET @sql1 = CONCAT(@sql1,"COALESCE(SUM(`C",@IND,"`),0) AS `C", @IND,"`,");
										SET @sql2 = CONCAT(@sql2,"IF(T3.var_claid = '",P_CLA,"', -(COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0)),0) AS `C",@IND,"`,");
						END LOOP L1;
				CLOSE C1;
				WHILE(@IND < P_CLA_MAX) DO
					SET @IND = @IND + 1;
					SET @sql1 = CONCAT(@sql1," 0.0 AS `C", @IND,"`,");
					SET @sql2 = CONCAT(@sql2," 0.0 AS `C",@IND,"`,");
				END WHILE;

				SET @sql1 = CONCAT(@sql1,"COALESCE(SUM(`C10`),0) AS `C10`,");
				SET @sql2 = CONCAT(@sql2,"IF(COALESCE(T3.var_claid,'') = '', -(COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0)),0) AS `C10`,");

				IF @sql1 <> "" THEN SET @sql1 =CONCAT(",",LEFT(@sql1, CHAR_LENGTH(@sql1) -1)); END IF;
				IF @sql2 <> "" THEN SET @sql2 = CONCAT(",",LEFT(@sql2, CHAR_LENGTH(@sql2) -1));END IF;
				SET @sql3 = CONCAT("SELECT '999999' AS P_ID, 'Retained Earnings - Current Year' AS P_NAME, 350 AS P_TYPE_ID, 'Retained Earnings' AS P_TYPE_NAME,
				""Owner's Equity"" AS P_GRO_1, 'Liabilities & Equity' AS P_GRO_2, 'B' AS P_GRO_3, '' AS P_GRO_4, '' AS P_GRO_5 ",@sql1," FROM
				(SELECT COALESCE(T1.var_id,'') AS var_accid, COALESCE(T1.var_name,'') AS var_name, T2.var_id AS P_TYPE_ID, T2.var_name AS P_TYPE_NAME,
				T2.var_group1 AS P_GRO_1, T2.var_group2 AS P_GRO_2, T2.var_group3 AS P_GRO_3, T2.var_group4 AS P_GRO_4, T2.var_group5 AS P_GRO_5, COALESCE(T3.var_claid,'') AS var_claid",@sql2," FROM 
				ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id LEFT JOIN ame_gl T3 ON T1.var_id = T3.var_accid
				WHERE
				(CASE
				WHEN '",P1,"' = 'ALL' THEN TRUE
				WHEN '",P1,"' = 'TODAY' THEN DATE(T3.dat_date) <= DATE(NOW())
				WHEN '",P1,"' = 'THIS PERIOD' THEN MONTH(T3.dat_date) <= MONTH(NOW())
				WHEN '",P1,"' = 'THIS YEAR' THEN YEAR(T3.dat_date) <= YEAR(NOW())
				ELSE DATE(T3.dat_date) <= DATE('",P3,"') END)
				AND
				IF('",P4,"' = 'ALL', TRUE, T3.var_accid >= '",P4,"') AND IF('",P5,"' = 'ALL', TRUE, T3.var_accid <= '",P5,"') 
				AND
				IF('",P6,"' = 'ALL', TRUE, T3.var_claid >= '",P6,"') AND IF('",P7,"' = 'ALL', TRUE, T3.var_claid <= '",P7,"')
				AND IF('",P8,"' = '0', TRUE, COALESCE(T3.dou_fundr,0) + COALESCE(T3.dou_funcr,0) <> 0)
				AND T2.var_group3 = 'P'
				) AS T1 
				");
				SET @SQL = CONCAT("(", @SQL, ") UNION ALL (",@sql3, ")");
		END;
		PREPARE stmt FROM @SQL;
		EXECUTE stmt;
		DEALLOCATE PREPARE stmt;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D010_M1_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D010_M1_1`(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))
    DETERMINISTIC
BEGIN
		# '', '','', '', '', 'All', '2018-06-30', '2018-06-30', 'All', 'All','All','All', ''
		SELECT T1.P_ID, T1.P_NAME,
		T1.P_GRO_2, T1.P_GRO_4, 
		SUM(IF(T1.P_GRO_2 = "Assets", -(T1.P_AMT_1 - T1.P_AMT), (T1.P_AMT_1 - T1.P_AMT))) AS P_AMT
		FROM ((
		SELECT T1.var_id AS P_ID, T1.var_name AS P_NAME,
		T2.var_group2 AS P_GRO_2, T2.var_group4 AS P_GRO_4, 
		IF(T2.var_group2 = 'Assets', COALESCE(T3.P_AMT, 0), -COALESCE(T3.P_AMT, 0)) AS P_AMT, 0 AS P_AMT_1
		FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id 
		LEFT JOIN (SELECT T1.var_accid, COALESCE(SUM(T1.dou_fundr), 0) + COALESCE(SUM(T1.dou_funcr), 0) AS P_AMT FROM ame_gl T1 
		WHERE
		(CASE
		WHEN P1 = 'ALL' THEN FALSE
		ELSE DATE(T1.dat_date) < DATE(P2) END)
		AND
		IF(P4 = 'ALL', TRUE, T1.var_accid >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T1.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T1.var_claid <= P7)
		GROUP BY T1.var_accid) T3 ON T1.var_id = T3.var_accid
		WHERE 
		IF(P4 = 'ALL', TRUE, T1.var_id >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_id <= P5)
		AND IF(P8 = "0", TRUE, T3.P_AMT <> 0)
		AND NOT ISNULL(T2.var_group4)
		GROUP BY T1.var_id
		) 
		UNION ALL
		(
		SELECT T1.var_id AS P_ID, T1.var_name AS P_NAME,
		T2.var_group2 AS P_GRO_2, T2.var_group4 AS P_GRO_4, 
		0.0 AS P_AMT, IF(T2.var_group2 = 'Assets', COALESCE(T3.P_AMT, 0), -COALESCE(T3.P_AMT, 0)) AS P_AMT_1
		FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id 
		LEFT JOIN (SELECT T1.var_accid, COALESCE(SUM(T1.dou_fundr), 0) + COALESCE(SUM(T1.dou_funcr), 0) AS P_AMT FROM ame_gl T1 
		WHERE
		(CASE
		WHEN P1 = 'ALL' THEN TRUE
		ELSE DATE(T1.dat_date) <= DATE(P3) END)
		AND
		IF(P4 = 'ALL', TRUE, T1.var_accid >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T1.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T1.var_claid <= P7)
		GROUP BY T1.var_accid) T3 ON T1.var_id = T3.var_accid
		WHERE 
		IF(P4 = 'ALL', TRUE, T1.var_id >= P4) AND IF(P5 = 'ALL', TRUE, T1.var_id <= P5)
		AND IF(P8 = "0", TRUE, T3.P_AMT <> 0)
		AND NOT ISNULL(T2.var_group4)
		GROUP BY T1.var_id
		)) AS T1 GROUP BY T1.P_ID
		;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D010_M1_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D010_M1_2`(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))
    DETERMINISTIC
BEGIN
		# '', '','', '', '', 'All', '2018-06-30', '2018-06-30', 'All', 'All','All','All', ''
		(
		SELECT 'Net Income' AS P_ID, -COALESCE(SUM(T3.dou_fundr), 0) - COALESCE(SUM(T3.dou_funcr), 0) AS P_AMT
		FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id 
		LEFT JOIN ame_gl T3 ON T1.var_id = T3.var_accid
		WHERE
		(CASE
		WHEN P1 = 'ALL' THEN TRUE
		WHEN P1 = 'TODAY' THEN DATE(T3.dat_date) = DATE(NOW())
		WHEN P1 = 'THIS PERIOD' THEN MONTH(T3.dat_date) = MONTH(NOW())
		WHEN P1 = 'THIS YEAR' THEN YEAR(T3.dat_date) = YEAR(NOW())
		ELSE DATE(T3.dat_date) BETWEEN DATE(P2) AND DATE(P3) END)
		AND
		IF(P4 = 'ALL', TRUE, T3.var_accid >= P4) AND IF(P5 = 'ALL', TRUE, T3.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T3.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T3.var_claid <= P7)
		AND T2.var_group3 = 'P'
		)
		;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D010_M1_3`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D010_M1_3`(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))
    DETERMINISTIC
BEGIN
		# '', '','', '', '', 'All', '2018-06-30', '2018-06-30', 'All', 'All','All','All', ''
		(
		SELECT 'Cash at beginning of period' AS P_ID, COALESCE(SUM(T3.dou_fundr), 0) + COALESCE(SUM(T3.dou_funcr), 0) AS P_AMT
		FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id 
		LEFT JOIN ame_gl T3 ON T1.var_id = T3.var_accid
		WHERE
		(CASE
		WHEN P1 = 'ALL' THEN FALSE
		ELSE DATE(T3.dat_date) < DATE(P2) END)
		AND
		IF(P4 = 'ALL', TRUE, T3.var_accid >= P4) AND IF(P5 = 'ALL', TRUE, T3.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T3.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T3.var_claid <= P7)
		AND T2.var_group5 = 'A10'
		)
		;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M005D010_M1_4`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M005D010_M1_4`(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))
    DETERMINISTIC
BEGIN
		# '', '','', '', '', 'All', '2018-06-30', '2018-06-30', 'All', 'All','All','All', ''
		(
		SELECT 'Cash at end of period' AS P_ID, COALESCE(SUM(T3.dou_fundr), 0) + COALESCE(SUM(T3.dou_funcr), 0) AS P_AMT
		FROM ame_mt_acc T1 LEFT JOIN ame_mt_acc_type T2 ON T1.int_typeid = T2.var_id 
		LEFT JOIN ame_gl T3 ON T1.var_id = T3.var_accid
		WHERE
		(CASE
		WHEN P1 = 'ALL' THEN TRUE
		ELSE DATE(T3.dat_date) <= DATE(P3) END)
		AND
		IF(P4 = 'ALL', TRUE, T3.var_accid >= P4) AND IF(P5 = 'ALL', TRUE, T3.var_accid <= P5) 
		AND
		IF(P6 = 'ALL', TRUE, T3.var_claid >= P6) AND IF(P7 = 'ALL', TRUE, T3.var_claid <= P7)
		AND T2.var_group5 = 'A10'
		)
		;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_PY`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_PY`(P1 varchar(100), P3 integer, P5 varchar(100), P6 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		# DECIMAL
		BEGIN DECLARE C1 CURSOR FOR SELECT int_amt FROM ame_sys_decimal T WHERE T.var_id = P3; 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_mt_cur T1 WHERE T1.var_id = P6 AND T1.tin_default = 1; 
		CASE P5
		WHEN "AP-PY" THEN
				IF P_ROW = 0 THEN
						SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, T1.var_venid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, T1.var_desc AS P_DESC, T3.var_desc AS P_DESC_D, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T3.var_docid AS P_DOC, IF(T1.var_type = 'Apply to Purchase Invoice', T4.var_ref, T5.var_name) AS P_DOC_REF, DATE(T4.dat_date) AS P_DOC_DATE, T3.dou_disc AS P_DISC, T3.dou_amt AS P_AMT, T3.dou_app AS P_APP, T1.var_claid AS P_CLA_M, T7.var_name AS P_CLA_M_NAME, T3.var_claid AS P_CLA_D, T6.var_name AS P_CLA_D_NAME
						FROM ame_ap_py T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id LEFT JOIN (ame_ap_py_det T3 LEFT JOIN ame_ap_in T4 ON T3.var_docid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_docid = T5.var_id LEFT JOIN ame_mt_cla T6 ON T3.var_claid = T6.var_id) ON T1.var_id = T3.var_id LEFT JOIN ame_mt_cla T7 ON T1.var_claid = T7.var_id
						WHERE T1.var_id = P1 AND IF(P3 = 0, TRUE, AME_FUN_ROUND(T3.dou_amt + T3.dou_disc + T3.dou_app, P_DEC_AMT) <> 0);
				ELSE
						SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, T1.var_venid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, T1.var_desc AS P_DESC, T3.var_desc AS P_DESC_D, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T3.var_docid AS P_DOC, IF(T1.var_type = 'Apply to Purchase Invoice', T4.var_ref, T5.var_name) AS P_DOC_REF, DATE(T4.dat_date) AS P_DOC_DATE, T3.dou_fundisc AS P_DISC, T3.dou_funamt AS P_AMT, T3.dou_funapp AS P_APP, T1.var_claid AS P_CLA_M, T7.var_name AS P_CLA_M_NAME, T3.var_claid AS P_CLA_D, T6.var_name AS P_CLA_D_NAME
						FROM ame_ap_py T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id LEFT JOIN (ame_ap_py_det T3 LEFT JOIN ame_ap_in T4 ON T3.var_docid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_docid = T5.var_id LEFT JOIN ame_mt_cla T6 ON T3.var_claid = T6.var_id) ON T1.var_id = T3.var_id LEFT JOIN ame_mt_cla T7 ON T1.var_claid = T7.var_id
						WHERE T1.var_id = P1 AND IF(P3 = 0, TRUE, AME_FUN_ROUND(T3.dou_amt + T3.dou_disc + T3.dou_app, P_DEC_AMT) <> 0);
				END IF;		
		WHEN "AR-PY" THEN
				IF P_ROW = 0 THEN
						SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, T1.var_cusid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, T1.var_desc AS P_DESC, T3.var_desc AS P_DESC_D, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T3.var_docid AS P_DOC, IF(T1.var_type = 'Apply to Sale Invoice', T4.var_ref, T5.var_name) AS P_DOC_REF, DATE(T4.dat_date) AS P_DOC_DATE, T3.dou_disc AS P_DISC, T3.dou_amt AS P_AMT, T3.dou_app AS P_APP, T1.var_claid AS P_CLA_M, T7.var_name AS P_CLA_M_NAME, T3.var_claid AS P_CLA_D, T6.var_name AS P_CLA_D_NAME
						FROM ame_ar_py T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id LEFT JOIN (ame_ar_py_det T3 LEFT JOIN ame_ar_in T4 ON T3.var_docid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_docid = T5.var_id LEFT JOIN ame_mt_cla T6 ON T3.var_claid = T6.var_id) ON T1.var_id = T3.var_id LEFT JOIN ame_mt_cla T7 ON T1.var_claid = T7.var_id
						WHERE T1.var_id = P1 AND IF(P3 = 0, TRUE, AME_FUN_ROUND(T3.dou_amt + T3.dou_disc + T3.dou_app, P_DEC_AMT) <> 0);
				ELSE
						SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, T1.var_cusid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, T1.var_desc AS P_DESC, T3.var_desc AS P_DESC_D, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T3.var_docid AS P_DOC, IF(T1.var_type = 'Apply to Sale Invoice', T4.var_ref, T5.var_name) AS P_DOC_REF, DATE(T4.dat_date) AS P_DOC_DATE, T3.dou_fundisc AS P_DISC, T3.dou_funamt AS P_AMT, T3.dou_funapp AS P_APP, T1.var_claid AS P_CLA_M, T7.var_name AS P_CLA_M_NAME, T3.var_claid AS P_CLA_D, T6.var_name AS P_CLA_D_NAME
						FROM ame_ar_py T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id LEFT JOIN (ame_ar_py_det T3 LEFT JOIN ame_ar_in T4 ON T3.var_docid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_docid = T5.var_id LEFT JOIN ame_mt_cla T6 ON T3.var_claid = T6.var_id) ON T1.var_id = T3.var_id LEFT JOIN ame_mt_cla T7 ON T1.var_claid = T7.var_id
						WHERE T1.var_id = P1 AND IF(P3 = 0, TRUE, AME_FUN_ROUND(T3.dou_amt + T3.dou_disc + T3.dou_app, P_DEC_AMT) <> 0);
				END IF;	
		ELSE
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, T1.var_venid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, T1.var_desc AS P_DESC, T3.var_desc AS P_DESC_D, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T3.var_docid AS P_DOC, IF(T1.var_type = 'Apply to Purchase Invoice', T4.var_ref, T5.var_name) AS P_DOC_REF, DATE(T4.dat_date) AS P_DOC_DATE, T3.dou_disc AS P_DISC, T3.dou_amt AS P_AMT, T3.dou_app AS P_APP, T1.var_claid AS P_CLA_M, T7.var_name AS P_CLA_M_NAME, T3.var_claid AS P_CLA_D, T6.var_name AS P_CLA_D_NAME
				FROM ame_ap_py T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id LEFT JOIN (ame_ap_py_det T3 LEFT JOIN ame_ap_in T4 ON T3.var_docid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_docid = T5.var_id LEFT JOIN ame_mt_cla T6 ON T3.var_claid = T6.var_id) ON T1.var_id = T3.var_id LEFT JOIN ame_mt_cla T7 ON T1.var_claid = T7.var_id
				WHERE T1.var_id = P1 AND IF(P3 = 0, TRUE, T3.dou_amt + T3.dou_disc + T3.dou_app > 0);
		END CASE;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_PY_GL`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_PY_GL`(P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_DATE DATETIME DEFAULT NULL;
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		DECLARE P_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_CASH VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_AP VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_DISC VARCHAR(100) DEFAULT "";
    DECLARE P_ACC_EXP VARCHAR(100) DEFAULT "";
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_VEN VARCHAR(100) DEFAULT "";
		DECLARE P_CLA VARCHAR(250) DEFAULT "";
		DECLARE P_CLA_D VARCHAR(250) DEFAULT "";
		DECLARE P_DESC VARCHAR(2000) DEFAULT "";
		DECLARE P_DESC_D VARCHAR(2000) DEFAULT "";
		DECLARE P_DISC DOUBLE DEFAULT 0.0;
		DECLARE P_AMT DOUBLE DEFAULT 0.0;
		DECLARE P_APP DOUBLE DEFAULT 0.0;
		DECLARE P_TCASH DOUBLE DEFAULT 0.0;
		DECLARE P_TAPP DOUBLE DEFAULT 0.0;
		DECLARE P_RATE DOUBLE DEFAULT 1.0;
		DECLARE P_DIFF DOUBLE DEFAULT 0.0;
		# DECIMAL
		BEGIN DECLARE C1 CURSOR FOR SELECT int_amt FROM ame_sys_decimal T WHERE T.var_id = P3; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF;CLOSE C1;END;
		IF P2 = "AP-PY" THEN
				DELETE FROM ame_sys_tem_py;
				BEGIN
						DECLARE C2 CURSOR FOR SELECT T1.dat_date, T1.var_type, T1.var_accid, T1.var_desc, T1.var_claid, T1.var_venid, T1.var_cur, T1.dou_rate FROM ame_ap_py T1 WHERE T1.var_id = P1;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_DATE, P_TYPE, P_ACC_CASH, P_DESC, P_CLA, P_VEN, P_CUR, P_RATE;
										IF P_CUR = P3 THEN SET P_RATE = 1.0; END IF;
										SET P_ACC_AP = AME_FUN_ACC('AP', P_VEN, 'NA');
										SET P_ACC_DISC = AME_FUN_ACC('DISC-AP-PY', 'DEFAULT', 'DEFAULT');
										SET @LINE = 0;
										BEGIN
												DECLARE D3 INTEGER DEFAULT 0;
												DEClARE C3 CURSOR FOR SELECT T1.var_docid, T1.var_claid, T1.var_desc, AME_FUN_ROUND(COALESCE(T1.dou_disc, 0), P_DEC_AMT), AME_FUN_ROUND(COALESCE(T1.dou_amt, 0), P_DEC_AMT), AME_FUN_ROUND(COALESCE(T1.dou_app, 0), P_DEC_AMT) 
												FROM ame_ap_py_det T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
												DECLARE CONTINUE HANDLER FOR NOT FOUND SET D3 = 1;
												OPEN C3;
														L3: LOOP FETCH C3 INTO P_ACC_EXP, P_CLA_D, P_DESC_D, P_DISC, P_AMT, P_APP;
																IF D3 = 1 THEN LEAVE L3; END IF;
																SET P_TCASH = AME_FUN_ROUND(P_TCASH + P_AMT, P_DEC_AMT);
																CASE P_TYPE
																WHEN "Apply to Purchase Invoice" THEN
																		BEGIN
																				DECLARE P_DOC_RATE DOUBLE DEFAULT 1.0;
																				DECLARE C2 CURSOR FOR SELECT T1.dou_rate FROM ame_ap_in T1 WHERE T1.var_id = P_ACC_EXP;
																				OPEN C2;
																						IF FOUND_ROWS() > 0 THEN 
																								FETCH C2 INTO  P_DOC_RATE; 
																								IF P_CUR = P3 THEN SET P_DOC_RATE = 1.0; END IF;
																						END IF;
																				CLOSE C2;
																				SET @AP = AME_FUN_ROUND(P_AMT + P_DISC + P_APP, P_DEC_AMT);
																				IF @AP <> 0 THEN
																						SET @LINE = @LINE + 1;
																						INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_AP, P_CLA_D, P_DESC_D, P_DATE, AME_FUN_ROUND(@AP * P_DOC_RATE, P_DEC_AMT), 0, @LINE);
																				END IF;
																				IF P_DISC <> 0 THEN	
																						SET @LINE = @LINE + 1;
																						INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_DISC, P_CLA_D, P_DESC_D, P_DATE, 0, AME_FUN_ROUND(P_DISC * P_RATE, P_DEC_AMT), @LINE);
																				END IF;
																				BEGIN
																						DECLARE D4 INTEGER DEFAULT 0;
																						DECLARE P_APP_TYPE VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_ACC VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_ACC_NAME VARCHAR(250) DEFAULT "";
																						DECLARE P_APP_CLA VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_CLA_NAME VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_DESC VARCHAR(2000) DEFAULT "";
																						DECLARE P_APP_AMT DOUBLE DEFAULT 0.0;
																						DEClARE C4 CURSOR FOR SELECT T1.var_doctype, T1.var_docid, T3.var_name, T1.var_claid, IF(COALESCE(T1.var_desc, '') = "", T1.var_doctype, CONCAT(T1.var_doctype, ": ",T1.var_desc)), AME_FUN_ROUND(T1.dou_app, P_DEC_AMT) FROM ame_ap_py_app AS T1 LEFT JOIN ame_mt_acc T3 ON T1.var_docid = T3.var_id WHERE T1.var_id = P1 AND var_num = P_ACC_EXP ORDER BY T1.int_line;
																						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D4 = 1;
																						OPEN C4;
																								L4: LOOP FETCH C4 INTO P_APP_TYPE, P_APP_ACC, P_APP_ACC_NAME, P_APP_CLA, P_APP_DESC, P_APP_AMT;
																										IF D4 = 1 THEN LEAVE L4; END IF;
																										CASE P_APP_TYPE
																										WHEN "Prepayment" THEN
																												BEGIN	
																														DECLARE P_APP_ACC_D VARCHAR(100) DEFAULT "";
																														DECLARE C5 CURSOR FOR SELECT T1.var_docid FROM ame_ap_py_det T1 WHERE T1.var_id = P_APP_ACC LIMIT 1;
																														OPEN C5;
																																IF FOUND_ROWS() > 0 THEN FETCH C5 INTO P_APP_ACC_D;
																																		IF P_APP_AMT <> 0 THEN
																																				SET @LINE = @LINE + 1;
																																				INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_APP_ACC_D, P_APP_CLA, P_APP_DESC, P_DATE, 0, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), @LINE);
																																		END IF;
																																END IF;
																														CLOSE C5;
																												END;
																										WHEN "Purchase Return" THEN
																												IF P_APP_AMT <> 0 THEN
																														SET @LINE = @LINE + 1;
																														INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_AP, P_APP_CLA, P_APP_DESC, P_DATE, 0, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), @LINE);
																												END IF;
																										WHEN "Debit Note" THEN
																												IF P_APP_AMT <> 0 THEN
																														SET @LINE = @LINE + 1;
																														INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_AP, P_APP_CLA, P_APP_DESC, P_DATE, 0, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), @LINE);
																												END IF;
																										ELSE
																												IF P_APP_AMT > 0 THEN
																														SET @LINE = @LINE + 1;
																														INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_APP_ACC, P_APP_CLA, P_APP_DESC, P_DATE, 0, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), @LINE);
																												ELSEIF P_APP_AMT < 0 THEN
																														SET @LINE = @LINE + 1;
																														INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_APP_ACC, P_APP_CLA, P_APP_DESC, P_DATE, -AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), 0,  @LINE);
																												END IF;
																										END CASE;
																								END LOOP L4;
																						CLOSE C4;
																				END;
																		END;	
																WHEN "Prepayment" THEN
																		IF P_AMT > 0 THEN
																				SET @LINE = @LINE + 1;
																				INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_EXP, P_CLA_D, P_DESC_D, P_DATE, 0, AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT), @LINE);
																		ELSEIF P_AMT < 0 THEN
																				SET @LINE = @LINE + 1;
																				INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_EXP, P_CLA_D, P_DESC_D, P_DATE, -AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT), 0, @LINE);
																		END IF;
																		/*
																		BEGIN
																				DECLARE D4 INTEGER DEFAULT 0;
																				DECLARE P_APP_TYPE VARCHAR(100) DEFAULT "";
																				DECLARE P_APP_ACC VARCHAR(100) DEFAULT "";
																				DECLARE P_APP_CLA VARCHAR(100) DEFAULT "";    
																				DECLARE P_APP_DESC VARCHAR(2000) DEFAULT "";
																				DECLARE P_APP_AMT DOUBLE DEFAULT 0.0;
																				DEClARE C4 CURSOR FOR SELECT T1.var_doctype, T1.var_docid, T1.var_claid, T1.var_desc, AME_FUN_ROUND(T1.dou_app, P_DEC_AMT) FROM ame_ap_py_app AS T1 WHERE T1.var_id = P1 AND var_num = P1 ORDER BY T1.int_line;
																				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D4 = 1;
																				OPEN C4;
																						L4: LOOP FETCH C4 INTO P_APP_TYPE, P_APP_ACC, P_APP_CLA, P_APP_DESC, P_APP_AMT;
																								IF D4 = 1 THEN LEAVE L4; END IF;
																								CASE P_APP_TYPE
																								WHEN "Prepayment" THEN
																										SET @Donothing = NULL;
																								WHEN "Purchase Return" THEN
																										SET @Donothing = NULL;
																								WHEN "Debit Note" THEN
																										SET @Donothing = NULL;
																								ELSE
																										SET P_TAPP = AME_FUN_ROUND(P_TAPP + P_APP_AMT, P_DEC_AMT);
																										IF P_APP_AMT > 0 THEN
																												SET @LINE = @LINE + 1;
																												INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_APP_ACC, P_APP_CLA, P_APP_DESC, P_DATE, 0, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), @LINE);
																										ELSEIF P_APP_AMT < 0 THEN
																												SET @LINE = @LINE + 1;
																												INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_APP_ACC, P_APP_CLA, P_APP_DESC, P_DATE, -AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), 0,  @LINE);
																										END IF;
																								END CASE;
																						END LOOP L4;
																				CLOSE C4;
																		END;
																		*/
																ELSE
																		IF P_AMT > 0 THEN
																				SET @LINE = @LINE + 1;
																				INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_EXP, P_CLA_D, P_DESC_D, P_DATE, AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT), 0, @LINE);
																		ELSEIF P_AMT < 0 THEN
																				SET @LINE = @LINE + 1;
																				INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_EXP, P_CLA_D, P_DESC_D, P_DATE, 0, -AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT), @LINE);
																		END IF;
																END CASE;
														END LOOP L3;
												CLOSE C3;
										END;
								END IF;
						CLOSE C2;
				END;
				SET @P_TOTAL_CASH = AME_FUN_ROUND(P_TCASH - P_TAPP, P_DEC_AMT);
				IF P_TYPE = "Prepayment" THEN
						IF @P_TOTAL_CASH <> 0 THEN
								SET @LINE = @LINE + 1;
								IF @P_TOTAL_CASH > 0 THEN
										INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_CASH, P_CLA, P_DESC, P_DATE, AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT), 0, @LINE);
								ELSE
										INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_CASH, P_CLA, P_DESC, P_DATE, 0, -AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT), @LINE);
								END IF;
						END IF;
				ELSE
						IF @P_TOTAL_CASH <> 0 THEN
								SET @LINE = @LINE + 1;
								IF @P_TOTAL_CASH > 0 THEN
										INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_CASH, P_CLA, P_DESC, P_DATE, 0, AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT), @LINE);
								ELSE
										INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_CASH, P_CLA, P_DESC, P_DATE, -AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT), 0, @LINE);
								END IF;
						END IF;
				END IF;	
				IF P_CUR <> P3 THEN
						SELECT AME_FUN_ROUND(SUM(dou_dr - dou_cr), P_DEC_AMT) INTO P_DIFF FROM ame_sys_tem_py;
						SET P_DIFF = -P_DIFF;
						IF P_DIFF > 0 THEN
								SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
								SET @LINE = @LINE + 1;
								INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_AP, P_CLA, "Gain/Loss on Exchange Rate", P_DATE, P_DIFF, 0, @LINE);
						ELSEIF P_DIFF < 0 THEN
								SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
								SET @LINE = @LINE + 1;
								INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_AP, P_CLA, "Gain/Loss on Exchange Rate", P_DATE, 0, -P_DIFF, @LINE);
						END IF;
				END IF;	
				SELECT T1.int_line AS LINE, T1.var_id AS P_ACC, T2.var_name AS P_ACC_NAME, T1.var_desc AS P_DESC, T1.var_claid AS P_CLA, T3.var_name AS P_CLA_NAME, dou_dr AS P_DR, dou_cr AS P_CR, T1.dat_date AS P_DATE FROM ame_sys_tem_py T1 LEFT JOIN ame_mt_acc T2 ON T1.var_id = T2.var_id LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id ORDER BY int_line;
		ELSE
				DELETE FROM ame_sys_tem_py;
				BEGIN
						DECLARE C2 CURSOR FOR SELECT T1.dat_date, T1.var_type, T1.var_accid, T1.var_desc, T1.var_claid, T1.var_cusid, T1.var_cur, T1.dou_rate FROM ame_ar_py T1 WHERE T1.var_id = P1;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_DATE, P_TYPE, P_ACC_CASH, P_DESC, P_CLA, P_VEN, P_CUR, P_RATE;
										IF P_CUR = P3 THEN SET P_RATE = 1.0; END IF;
										SET P_ACC_AP = AME_FUN_ACC('AR', P_VEN, 'NA');
										SET P_ACC_DISC = AME_FUN_ACC('DISC-AR-PY', 'DEFAULT', 'DEFAULT');
										SET @LINE = 0;
										BEGIN
												DECLARE D3 INTEGER DEFAULT 0;
												DEClARE C3 CURSOR FOR SELECT T1.var_docid, T1.var_claid, T1.var_desc, AME_FUN_ROUND(COALESCE(T1.dou_disc, 0), P_DEC_AMT), AME_FUN_ROUND(COALESCE(T1.dou_amt, 0), P_DEC_AMT), AME_FUN_ROUND(COALESCE(T1.dou_app, 0), P_DEC_AMT) FROM ame_ar_py_det T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
												DECLARE CONTINUE HANDLER FOR NOT FOUND SET D3 = 1;
												OPEN C3;
														L3: LOOP FETCH C3 INTO P_ACC_EXP, P_CLA_D, P_DESC_D, P_DISC, P_AMT, P_APP;
																IF D3 = 1 THEN LEAVE L3; END IF;
																SET P_TCASH = AME_FUN_ROUND(P_TCASH + P_AMT, P_DEC_AMT);
																CASE P_TYPE
																WHEN "Apply to Sale Invoice" THEN
																		BEGIN
																				DECLARE P_DOC_RATE DOUBLE DEFAULT 1.0;
																				DECLARE C2 CURSOR FOR SELECT T1.dou_rate FROM ame_ar_in T1 WHERE T1.var_id = P_ACC_EXP;
																				OPEN C2;
																						IF FOUND_ROWS() > 0 THEN 
																								FETCH C2 INTO  P_DOC_RATE; 
																								IF P_CUR = P3 THEN SET P_DOC_RATE = 1.0; END IF;
																						END IF;
																				CLOSE C2;
																				SET @AP = AME_FUN_ROUND(P_AMT + P_DISC + P_APP, P_DEC_AMT);
																				IF @AP <> 0 THEN
																						SET @LINE = @LINE + 1;
																						INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_AP, P_CLA_D, P_DESC_D, P_DATE, 0, AME_FUN_ROUND(@AP * P_DOC_RATE, P_DEC_AMT), @LINE);
																				END IF;
																				IF P_DISC <> 0 THEN	
																						SET @LINE = @LINE + 1;
																						INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_DISC, P_CLA_D, P_DESC_D, P_DATE, AME_FUN_ROUND(P_DISC * P_RATE, P_DEC_AMT), 0, @LINE);
																				END IF;
																				BEGIN
																						DECLARE D4 INTEGER DEFAULT 0;
																						DECLARE P_APP_TYPE VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_ACC VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_ACC_NAME VARCHAR(250) DEFAULT "";
																						DECLARE P_APP_CLA VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_CLA_NAME VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_DESC VARCHAR(2000) DEFAULT "";
																						DECLARE P_APP_AMT DOUBLE DEFAULT 0.0;
																						DEClARE C4 CURSOR FOR SELECT T1.var_doctype, T1.var_docid, T3.var_name, T1.var_claid, IF(COALESCE(T1.var_desc, '') = "", T1.var_doctype, CONCAT(T1.var_doctype, ": ", T1.var_desc)), AME_FUN_ROUND(T1.dou_app, P_DEC_AMT) FROM ame_ar_py_app AS T1 LEFT JOIN ame_mt_acc T3 ON T1.var_docid = T3.var_id WHERE T1.var_id = P1 AND var_num = P_ACC_EXP ORDER BY T1.int_line;
																						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D4 = 1;
																						OPEN C4;
																								L4: LOOP FETCH C4 INTO P_APP_TYPE, P_APP_ACC, P_APP_ACC_NAME, P_APP_CLA, P_APP_DESC, P_APP_AMT;
																										IF D4 = 1 THEN LEAVE L4; END IF;
																										CASE P_APP_TYPE
																										WHEN "Prepayment" THEN
																												BEGIN	
																														DECLARE P_APP_ACC_D VARCHAR(100) DEFAULT "";
																														DECLARE C5 CURSOR FOR SELECT T1.var_docid FROM ame_ar_py_det T1 WHERE T1.var_id = P_APP_ACC LIMIT 1;
																														OPEN C5;
																																IF FOUND_ROWS() > 0 THEN FETCH C5 INTO P_APP_ACC_D;
																																		IF P_APP_AMT <> 0 THEN
																																				SET @LINE = @LINE + 1;
																																				INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_APP_ACC_D, P_APP_CLA, P_APP_DESC, P_DATE, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), 0, @LINE);
																																		END IF;
																																END IF;
																														CLOSE C5;
																												END;
																										WHEN "Sales Return" THEN
																												IF P_APP_AMT <> 0 THEN
																														SET @LINE = @LINE + 1;
																														INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_AP, P_APP_CLA, P_APP_DESC, P_DATE, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), 0, @LINE);
																												END IF;
																										WHEN "Credit Note" THEN
																												IF P_APP_AMT <> 0 THEN
																														SET @LINE = @LINE + 1;
																														INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_AP, P_APP_CLA, P_APP_DESC, P_DATE, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), 0, @LINE);
																												END IF;
																										ELSE
																												IF P_APP_AMT > 0 THEN
																														SET @LINE = @LINE + 1;
																														INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_APP_ACC, P_APP_CLA, P_APP_DESC, P_DATE, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), 0, @LINE);
																												ELSEIF P_APP_AMT < 0 THEN
																														SET @LINE = @LINE + 1;
																														INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_APP_ACC, P_APP_CLA, P_APP_DESC, P_DATE, 0, -AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT),  @LINE);
																												END IF;
																										END CASE;
																								END LOOP L4;
																						CLOSE C4;
																				END;
																		END;	
																WHEN "Prepayment" THEN
																		IF P_AMT > 0 THEN
																				SET @LINE = @LINE + 1;
																				INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_EXP, P_CLA_D, P_DESC_D, P_DATE, AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT), 0, @LINE);
																		ELSEIF P_AMT < 0 THEN
																				SET @LINE = @LINE + 1;
																				INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_EXP, P_CLA_D, P_DESC_D, P_DATE, 0, -AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT), @LINE);
																		END IF;
																		/*
																		BEGIN
																				DECLARE D4 INTEGER DEFAULT 0;
																				DECLARE P_APP_TYPE VARCHAR(100) DEFAULT "";
																				DECLARE P_APP_ACC VARCHAR(100) DEFAULT "";
																				DECLARE P_APP_CLA VARCHAR(100) DEFAULT "";    
																				DECLARE P_APP_DESC VARCHAR(2000) DEFAULT "";
																				DECLARE P_APP_AMT DOUBLE DEFAULT 0.0;
																				DEClARE C4 CURSOR FOR SELECT T1.var_doctype, T1.var_docid, T1.var_claid, T1.var_desc, AME_FUN_ROUND(T1.dou_app, P_DEC_AMT) FROM ame_ar_py_app AS T1 WHERE T1.var_id = P1 AND var_num = P1 ORDER BY T1.int_line;
																				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D4 = 1;
																				OPEN C4;
																						L4: LOOP FETCH C4 INTO P_APP_TYPE, P_APP_ACC, P_APP_CLA, P_APP_DESC, P_APP_AMT;
																								IF D4 = 1 THEN LEAVE L4; END IF;
																								CASE P_APP_TYPE
																								WHEN "Prepayment" THEN
																										SET @Donothing = NULL;
																								WHEN "Sales Return" THEN
																										SET @Donothing = NULL;
																								WHEN "Credit Note" THEN
																										SET @Donothing = NULL;
																								ELSE
																										SET P_TAPP = AME_FUN_ROUND(P_TAPP + P_APP_AMT, P_DEC_AMT);
																										IF P_APP_AMT > 0 THEN
																												SET @LINE = @LINE + 1;
																												INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_APP_ACC, P_APP_CLA, P_APP_DESC, P_DATE, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT), 0, @LINE);
																										ELSEIF P_APP_AMT < 0 THEN
																												SET @LINE = @LINE + 1;
																												INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_APP_ACC, P_APP_CLA, P_APP_DESC, P_DATE, 0, -AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT),  @LINE);
																										END IF;
																								END CASE;
																						END LOOP L4;
																				CLOSE C4;
																		END;
																		*/
																ELSE
																		IF P_AMT > 0 THEN
																				SET @LINE = @LINE + 1;
																				INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_EXP, P_CLA_D, P_DESC_D, P_DATE, 0, AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT), @LINE);
																		ELSEIF P_AMT < 0 THEN
																				SET @LINE = @LINE + 1;
																				INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_EXP, P_CLA_D, P_DESC_D, P_DATE, -AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT), 0, @LINE);
																		END IF;
																END CASE;
														END LOOP L3;
												CLOSE C3;
										END;
								END IF;
						CLOSE C2;
				END;
				SET @P_TOTAL_CASH = AME_FUN_ROUND(P_TCASH - P_TAPP, P_DEC_AMT);
				IF P_TYPE = "Prepayment" THEN
						IF @P_TOTAL_CASH <> 0 THEN
								SET @LINE = @LINE + 1;
								IF @P_TOTAL_CASH > 0 THEN
										INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_CASH, P_CLA, P_DESC, P_DATE, 0, AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT), @LINE);
								ELSE
										INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_CASH, P_CLA, P_DESC, P_DATE, -AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT), 0, @LINE);
								END IF;
						END IF;
				ELSE
						IF @P_TOTAL_CASH <> 0 THEN
								SET @LINE = @LINE + 1;
								IF @P_TOTAL_CASH > 0 THEN
										INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_CASH, P_CLA, P_DESC, P_DATE, AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT), 0, @LINE);
								ELSE
										INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_CASH, P_CLA, P_DESC, P_DATE, 0, -AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT), @LINE);
								END IF;
						END IF;
				END IF;
				IF P_CUR <> P3 THEN
						SELECT -AME_FUN_ROUND(SUM(dou_dr - dou_cr), P_DEC_AMT) INTO P_DIFF FROM ame_sys_tem_py;
						IF P_DIFF > 0 THEN
								SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
								SET @LINE = @LINE + 1;
								INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_AP, P_CLA, "Gain/Loss on Exchange Rate", P_DATE, P_DIFF, 0, @LINE);
						ELSEIF P_DIFF < 0 THEN
								SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
								SET @LINE = @LINE + 1;
								INSERT INTO ame_sys_tem_py(var_id, var_claid, var_desc, dat_date, dou_dr, dou_cr, int_line) VALUES(P_ACC_AP, P_CLA, "Gain/Loss on Exchange Rate", P_DATE, 0, -P_DIFF, @LINE);
						END IF;
				END IF;	
				SELECT T1.int_line AS LINE, T1.var_id AS P_ACC, T2.var_name AS P_ACC_NAME, T1.var_desc AS P_DESC, T1.var_claid AS P_CLA, T3.var_name AS P_CLA_NAME, dou_dr AS P_DR, dou_cr AS P_CR, T1.dat_date AS P_DATE FROM ame_sys_tem_py T1 LEFT JOIN ame_mt_acc T2 ON T1.var_id = T2.var_id LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id ORDER BY int_line;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_PY_SD`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_PY_SD`(P1 varchar(100), P3 integer)
    DETERMINISTIC
BEGIN
		# DECIMAL
		IF P3 = 0 THEN
				SELECT T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, T1.var_cusid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, T1.var_desc AS P_DESC, IF(COALESCE(T2.var_tel1, '') = '', T2.var_tel2, IF(COALESCE(T2.var_tel2, '') = '', T2.var_tel1, CONCAT(T2.var_tel1, ' , ',T2.var_tel2))) AS P_TEL, T2.var_addr AS P_ADDR, T3.dat_date AS P_DATE_PMT, T1.dou_net AS P_INV_NET,
				T3.var_period AS P_SD_PERIOD, T3.var_method AS P_SD_METHOD, T3.var_type AS P_SD_TYPE, T3.var_desc AS P_SD_DESC, T3.dou_sch AS P_SD_PMT, T3.dou_rate_per AS P_SD_RATE_INT, T3.dou_rate_penalty AS P_SD_RATE_PEN, T3.int_term AS P_SD_TERM, 
				0.0 AS P_BEG, T4.P_PMT, T4.P_INTEREST, T4.P_PRINCIPAL, 0.0 AS P_END, T4.P_PENALTY, 0 AS `P_NUM_DAY`
				FROM ame_ar_in T1 
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id 
				LEFT JOIN ame_ar_py_sd T3 ON T1.var_id = T3.var_id
				LEFT JOIN (SELECT T1.var_id, SUM(T1.dou_net) AS P_PMT, SUM(T1.dou_rate_dol) AS P_INTEREST, SUM(T1.dou_penalty) AS P_PENALTY, SUM(T1.dou_amt) AS P_PRINCIPAL FROM ame_ar_py_sd_det T1 WHERE T1.var_id = P1) AS T4 ON T1.var_id = T4.var_id
				WHERE T1.var_id = P1 ORDER BY T3.int_id;
		ELSE
				SELECT T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, T1.var_cusid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, T1.var_desc AS P_DESC, IF(COALESCE(T2.var_tel1, '') = '', T2.var_tel2, IF(COALESCE(T2.var_tel2, '') = '', T2.var_tel1, CONCAT(T2.var_tel1, ' , ',T2.var_tel2))) AS P_TEL, T2.var_addr AS P_ADDR, T3.dat_date AS P_DATE_PMT, T1.dou_net AS P_INV_NET,
				'' AS P_SD_PERIOD, '' AS P_SD_METHOD, '' AS P_SD_TYPE, '' AS P_SD_DESC, 0.0 AS P_SD_PMT, 0.0 AS P_SD_RATE_INT, 0.0 AS P_SD_RATE_PEN, 0 AS P_SD_TERM, 
				T3.dou_sch AS P_BEG, T3.dou_net AS P_PMT, T3.dou_rate_dol AS P_INTEREST, T3.dou_amt AS P_PRINCIPAL, T3.dou_balance AS P_END, T3.dou_penalty AS P_PENALTY, T3.int_day AS `P_NUM_DAY`
				FROM ame_ar_in T1 
				LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id 
				LEFT JOIN ame_ar_py_sd_det T3 ON T1.var_id = T3.var_id
				WHERE T1.var_id = P1 ORDER BY T3.int_id, T3.int_line;
		END IF;
		
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_RF`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_RF`(P1 varchar(100), P3 integer, P5 varchar(100), P6 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		DECLARE VC_TYPE VARCHAR(100) DEFAULT "";
		# DECIMAL
		BEGIN DECLARE C1 CURSOR FOR SELECT int_amt FROM ame_sys_decimal T WHERE T.var_id = P3; 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_mt_cur T1 WHERE T1.var_id = P6 AND T1.tin_default = 1; 
		CASE P5
		WHEN "AP-RF" THEN
				IF P_ROW = 0 THEN
						SELECT T1.var_doctype AS P_TYPE, T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, 
						T1.var_perid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 
						T1.var_desc AS P_DESC, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T1.var_docid AS P_DOC, 
						CASE WHEN T1.var_doctype = "AP-PY" THEN T3.var_ref WHEN T1.var_doctype = "AP-DN" THEN T4.var_ref WHEN T1.var_doctype = "AP-RET" THEN T5.var_ref END AS P_DOC_REF, 
						CASE WHEN T1.var_doctype = "AP-PY" THEN DATE(T3.dat_date) WHEN T1.var_doctype = "AP-DN" THEN DATE(T4.dat_date) WHEN T1.var_doctype = "AP-RET" THEN DATE(T5.dat_date) END AS P_DOC_DATE, 
						T1.dou_amt AS P_AMT, T1.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T1.var_accid AS P_ACC_CASH, T6.var_name AS P_ACC_CASH_NAME, T1.var_preaccid AS P_ACC_PRE, T7.var_name AS P_ACC_PRE_NAME
						FROM ame_refund T1 
						LEFT JOIN ame_mt_ven T2 ON T1.var_perid = T2.var_id 
						LEFT JOIN ame_ap_py T3 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AP-PY")
						LEFT JOIN ame_ap_dn T4 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AP-DN")
						LEFT JOIN ame_ap_ret T5 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AP-RET")
						LEFT JOIN ame_mt_acc T6 ON T1.var_accid = T6.var_id
						LEFT JOIN ame_mt_acc T7 ON T1.var_preaccid = T7.var_id
						LEFT JOIN ame_mt_acc T8 ON T1.var_claid = T8.var_id
						WHERE T1.var_id = P1 AND T1.int_line = 0;
				ELSE
						SELECT T1.var_doctype AS P_TYPE, T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, 
						T1.var_perid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 
						T1.var_desc AS P_DESC, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T1.var_docid AS P_DOC, 
						CASE WHEN T1.var_doctype = "AP-PY" THEN T3.var_ref WHEN T1.var_doctype = "AP-DN" THEN T4.var_ref WHEN T1.var_doctype = "AP-RET" THEN T5.var_ref END AS P_DOC_REF, 
						CASE WHEN T1.var_doctype = "AP-PY" THEN DATE(T3.dat_date) WHEN T1.var_doctype = "AP-DN" THEN DATE(T4.dat_date) WHEN T1.var_doctype = "AP-RET" THEN DATE(T5.dat_date) END AS P_DOC_DATE, 
						T1.dou_funamt AS P_AMT, T1.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T1.var_accid AS P_ACC_CASH, T6.var_name AS P_ACC_CASH_NAME, T1.var_preaccid AS P_ACC_PRE, T7.var_name AS P_ACC_PRE_NAME
						FROM ame_refund T1 
						LEFT JOIN ame_mt_ven T2 ON T1.var_perid = T2.var_id 
						LEFT JOIN ame_ap_py T3 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AP-PY")
						LEFT JOIN ame_ap_dn T4 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AP-DN")
						LEFT JOIN ame_ap_ret T5 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AP-RET")
						LEFT JOIN ame_mt_acc T6 ON T1.var_accid = T6.var_id
						LEFT JOIN ame_mt_acc T7 ON T1.var_preaccid = T7.var_id
						LEFT JOIN ame_mt_acc T8 ON T1.var_claid = T8.var_id
						WHERE T1.var_id = P1 AND T1.int_line = 0;
				END IF;			
		WHEN "AR-RF" THEN
				IF P_ROW = 0 THEN
						SELECT T1.var_doctype AS P_TYPE, T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, 
						T1.var_perid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 
						T1.var_desc AS P_DESC, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T1.var_docid AS P_DOC, 
						CASE WHEN T1.var_doctype = "AR-PY" THEN T3.var_ref WHEN T1.var_doctype = "AR-CN" THEN T4.var_ref WHEN T1.var_doctype = "AR-RET" THEN T5.var_ref END AS P_DOC_REF, 
						CASE WHEN T1.var_doctype = "AR-PY" THEN DATE(T3.dat_date) WHEN T1.var_doctype = "AR-CN" THEN DATE(T4.dat_date) WHEN T1.var_doctype = "AR-RET" THEN DATE(T5.dat_date) END AS P_DOC_DATE, 
						T1.dou_amt AS P_AMT, T1.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T1.var_accid AS P_ACC_CASH, T6.var_name AS P_ACC_CASH_NAME, T1.var_preaccid AS P_ACC_PRE, T7.var_name AS P_ACC_PRE_NAME
						FROM ame_refund T1 
						LEFT JOIN ame_mt_cus T2 ON T1.var_perid = T2.var_id 
						LEFT JOIN ame_ar_py T3 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AR-PY")
						LEFT JOIN ame_ar_cn T4 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AR-CN")
						LEFT JOIN ame_ar_ret T5 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AR-RET")
						LEFT JOIN ame_mt_acc T6 ON T1.var_accid = T6.var_id
						LEFT JOIN ame_mt_acc T7 ON T1.var_preaccid = T7.var_id
						LEFT JOIN ame_mt_acc T8 ON T1.var_claid = T8.var_id
						WHERE T1.var_id = P1 AND T1.int_line = 1;
				ELSE
						SELECT T1.var_doctype AS P_TYPE, T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, 
						T1.var_perid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 
						T1.var_desc AS P_DESC, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T1.var_docid AS P_DOC, 
						CASE WHEN T1.var_doctype = "AR-PY" THEN T3.var_ref WHEN T1.var_doctype = "AR-CN" THEN T4.var_ref WHEN T1.var_doctype = "AR-RET" THEN T5.var_ref END AS P_DOC_REF, 
						CASE WHEN T1.var_doctype = "AR-PY" THEN DATE(T3.dat_date) WHEN T1.var_doctype = "AR-CN" THEN DATE(T4.dat_date) WHEN T1.var_doctype = "AR-RET" THEN DATE(T5.dat_date) END AS P_DOC_DATE, 
						T1.dou_funamt AS P_AMT, T1.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T1.var_accid AS P_ACC_CASH, T6.var_name AS P_ACC_CASH_NAME, T1.var_preaccid AS P_ACC_PRE, T7.var_name AS P_ACC_PRE_NAME
						FROM ame_refund T1 
						LEFT JOIN ame_mt_cus T2 ON T1.var_perid = T2.var_id 
						LEFT JOIN ame_ar_py T3 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AR-PY")
						LEFT JOIN ame_ar_cn T4 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AR-CN")
						LEFT JOIN ame_ar_ret T5 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AR-RET")
						LEFT JOIN ame_mt_acc T6 ON T1.var_accid = T6.var_id
						LEFT JOIN ame_mt_acc T7 ON T1.var_preaccid = T7.var_id
						LEFT JOIN ame_mt_acc T8 ON T1.var_claid = T8.var_id
						WHERE T1.var_id = P1 AND T1.int_line = 1;
				END IF;	
		ELSE
				SELECT T1.var_doctype AS P_TYPE, T1.var_id AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, 
				T1.var_perid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 
				T1.var_desc AS P_DESC, T1.var_pmttype AS P_PMT_METHOD, T1.var_pmtref AS P_PMT_REF, T1.var_docid AS P_DOC, 
				CASE WHEN T1.var_doctype = "AR-PY" THEN T3.var_ref WHEN T1.var_doctype = "AR-CN" THEN T4.var_ref WHEN T1.var_doctype = "AR-RET" THEN T5.var_ref END AS P_DOC_REF, 
				CASE WHEN T1.var_doctype = "AR-PY" THEN DATE(T3.dat_date) WHEN T1.var_doctype = "AR-CN" THEN DATE(T4.dat_date) WHEN T1.var_doctype = "AR-RET" THEN DATE(T5.dat_date) END AS P_DOC_DATE, 
				T1.dou_amt AS P_AMT, T1.var_claid AS P_CLA, T8.var_name AS P_CLA_NAME, T1.var_accid AS P_ACC_CASH, T6.var_name AS P_ACC_CASH_NAME, T1.var_preaccid AS P_ACC_PRE, T7.var_name AS P_ACC_PRE_NAME
				FROM ame_refund T1 
				LEFT JOIN ame_mt_cus T2 ON T1.var_perid = T2.var_id 
				LEFT JOIN ame_ar_py T3 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AR-PY")
				LEFT JOIN ame_ar_cn T4 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AR-CN")
				LEFT JOIN ame_ar_ret T5 ON (T1.var_docid = T3.var_id AND T1.var_doctype = "AR-RET")
				LEFT JOIN ame_mt_acc T6 ON T1.var_accid = T6.var_id
				LEFT JOIN ame_mt_acc T7 ON T1.var_preaccid = T7.var_id
				LEFT JOIN ame_mt_acc T8 ON T1.var_claid = T8.var_id
				WHERE T1.var_id = P1 AND T1.int_line = 1;
		END CASE;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_SYS_AC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_SYS_AC`()
    DETERMINISTIC
BEGIN
		SET @AC1 = "AC1";
		SET @AC2 = "AC2";
		SET @AC3 = "AC3";
		SET @AC4 = "AC4";
		SET @AC5 = "AC5";
		SELECT COALESCE(ac.var_header,'') INTO @AC1 FROM ame_sys_add_cost ac WHERE ac.var_id = 'AC1';
		SELECT COALESCE(ac.var_header,'') INTO @AC2 FROM ame_sys_add_cost ac WHERE ac.var_id = 'AC2';
		SELECT COALESCE(ac.var_header,'') INTO @AC3 FROM ame_sys_add_cost ac WHERE ac.var_id = 'AC3';
		SELECT COALESCE(ac.var_header,'') INTO @AC4 FROM ame_sys_add_cost ac WHERE ac.var_id = 'AC4';
		SELECT COALESCE(ac.var_header,'') INTO @AC5 FROM ame_sys_add_cost ac WHERE ac.var_id = 'AC5';
		SELECT @AC1 AS 'AC1',@AC2 AS 'AC2',@AC3 AS 'AC3',@AC4 AS 'AC4',@AC5 AS 'AC5';
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_SYS_CLA`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_SYS_CLA`()
    DETERMINISTIC
BEGIN
		SET @P = "";
		SET @C1 = "C1";
		SET @C2 = "C2";
		SET @C3 = "C3";
		SET @C4 = "C4";
		SET @C5 = "C5";
		SET @C6 = "C6";
		SET @C7 = "C7";
		SET @C8 = "C8";
		SET @C9 = "C9";
		SET @C10 = "Unclassified";
		SELECT GROUP_CONCAT(T1.var_id ORDER BY T1.var_id ASC SEPARATOR ';') INTO @P FROM ame_mt_cla T1;
		IF AME_FUN_SPLIT_STRING(@P,';',1) <> '' THEN SET @C1 = AME_FUN_SPLIT_STRING(@P,';',1); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',2) <> '' THEN SET @C2 = AME_FUN_SPLIT_STRING(@P,';',2); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',3) <> '' THEN SET @C3 = AME_FUN_SPLIT_STRING(@P,';',3); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',4) <> '' THEN SET @C4 = AME_FUN_SPLIT_STRING(@P,';',4); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',5) <> '' THEN SET @C5 = AME_FUN_SPLIT_STRING(@P,';',5); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',6) <> '' THEN SET @C6 = AME_FUN_SPLIT_STRING(@P,';',6); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',7) <> '' THEN SET @C7 = AME_FUN_SPLIT_STRING(@P,';',7); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',8) <> '' THEN SET @C8 = AME_FUN_SPLIT_STRING(@P,';',8); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',9) <> '' THEN SET @C9 = AME_FUN_SPLIT_STRING(@P,';',9); END IF;
		SELECT @C1 AS 'C1', @C2 AS 'C2', @C3 AS 'C3', @C4 AS 'C4', @C5 AS 'C5', @C6 AS 'C6', @C7 AS 'C7', @C8 AS 'C8', @C9 AS 'C9', @C10 AS 'C10';
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_SYS_DECIMAL`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_SYS_DECIMAL`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		SELECT T1.*, T2.var_symbol FROM ame_sys_decimal T1 LEFT JOIN ame_mt_cur T2 ON T1.var_id = T2.var_id WHERE T1.var_id = P1;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_SYS_HEADER`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_SYS_HEADER`()
    DETERMINISTIC
BEGIN
		SELECT T1.*, (SELECT var_name FROM ame_sys_config WHERE var_id = 'IMG_PATH' LIMIT 1) AS Path FROM ame_mt_com T1;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_SYS_PC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_SYS_PC`()
    DETERMINISTIC
BEGIN
		SET @P = "";
		SET @PC1 = "PC 1";
		SET @PC2 = "PC 2";
		SET @PC3 = "PC 3";
		SET @PC4 = "PC 4";
		SET @PC5 = "PC 5";
		SET @PC6 = "PC 6";
		SET @PC7 = "PC 7";
		SET @PC8 = "PC 8";
		SET @PC9 = "PC 9";
		SET @PC10 = "PC 10";
		SELECT GROUP_CONCAT(var_id ORDER BY var_id ASC SEPARATOR ';') INTO @P FROM ame_mt_pri_cod;
		IF AME_FUN_SPLIT_STRING(@P,';',1) <> '' THEN SET @PC1 = AME_FUN_SPLIT_STRING(@P,';',1); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',2) <> '' THEN SET @PC2 = AME_FUN_SPLIT_STRING(@P,';',2); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',3) <> '' THEN SET @PC3 = AME_FUN_SPLIT_STRING(@P,';',3); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',4) <> '' THEN SET @PC4 = AME_FUN_SPLIT_STRING(@P,';',4); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',5) <> '' THEN SET @PC5 = AME_FUN_SPLIT_STRING(@P,';',5); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',6) <> '' THEN SET @PC6 = AME_FUN_SPLIT_STRING(@P,';',6); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',7) <> '' THEN SET @PC7 = AME_FUN_SPLIT_STRING(@P,';',7); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',8) <> '' THEN SET @PC8 = AME_FUN_SPLIT_STRING(@P,';',8); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',9) <> '' THEN SET @PC9 = AME_FUN_SPLIT_STRING(@P,';',9); END IF;
		IF AME_FUN_SPLIT_STRING(@P,';',10) <> '' THEN SET @PC10 = AME_FUN_SPLIT_STRING(@P,';',10); END IF;
		SELECT @PC1 AS 'PC1',@PC2 AS 'PC2',@PC3 AS 'PC3',@PC4 AS 'PC4',@PC5 AS 'PC5',@PC6 AS 'PC6',@PC7 AS 'PC7',@PC8 AS 'PC8',@PC9 AS 'PC9',@PC10 AS 'PC10';
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_SYS_SIGN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_SYS_SIGN`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		SET group_concat_max_len = 10000000;
		SELECT GROUP_CONCAT(CONCAT("<CENTER><FONT FACE = 'Khmer OS System'>", COALESCE(T1.var_desc1, ''), IF(COALESCE(T1.var_desc1, '') = '', '', '</BR>'), COALESCE(T1.var_desc2, ''), "</FONT> </CENTER> </BR> </BR> </BR> </BR> </BR>") SEPARATOR ';') AS H1, GROUP_CONCAT(CONCAT(IF(COALESCE(T1.var_empid, '') = '', '', CONCAT('Name : ', COALESCE(T2.var_name, ''))), IF(COALESCE(T1.var_empid, '') <> '' AND COALESCE(T1.var_posid, '') <> '', '</BR>', ''), IF(COALESCE(T1.var_posid, '') = '', '', CONCAT('Position : ', COALESCE(T3.var_name, '')))) SEPARATOR ';') AS H2 FROM ame_sys_sign T1 LEFT JOIN ame_mt_emp T2 ON T1.var_empid = T2.var_id LEFT JOIN ame_mt_pos T3 ON T1.var_posid = T3.var_id WHERE T1.tin_display = 1 AND T1.var_id = P1 ORDER BY T1.int_sortor;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_ROW_NUM`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_ROW_NUM`(P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		SET @P_INDEX = 0;
		SET @R = -1;
		IF P1 = "ame_gl_je" THEN
				SET @SQL = CONCAT("SELECT COALESCE(T.R, 0) INTO @P_INDEX FROM (SELECT @R := @R + 1 AS R, int_jid FROM ", P1, " ORDER BY int_jid) T WHERE T.int_jid = ", P2);
		ELSEIF P1 = "ame_refund" THEN
				SET @SQL = CONCAT("SELECT COALESCE(T.R, 0) INTO @P_INDEX FROM (SELECT @R := @R + 1 AS R, var_id FROM ", P1, " WHERE int_line = ", P3," ORDER BY var_id) T WHERE T.var_id = '", P2, "'");
		ELSE
				SET @SQL = CONCAT("SELECT COALESCE(T.R, 0) INTO @P_INDEX FROM (SELECT @R := @R + 1 AS R, var_id FROM ", P1, " ORDER BY var_id) T WHERE T.var_id = '", P2, "'");
		END IF;
		PREPARE STM FROM @SQL;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
		SELECT COALESCE(@P_INDEX, 0);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_SYS_FORM`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_SYS_FORM`(IN P_UID varchar(255))
    DETERMINISTIC
BEGIN
	SELECT 
	COALESCE(FrmSysName,'') AS FrmSysName,
	COALESCE(FrmName,'') AS FrmName,
	COALESCE(frmtag,'') AS FrmTag,
	COALESCE(MnuName,'') AS MnuName,
	COALESCE(SortKey,0) AS SortKey,
	COALESCE(IsForm,0) AS IsForm,
	COALESCE(IconName,'') AS IconName,
	COALESCE(menuseparator,'') AS MenuSeparator,
	COALESCE(`status`,'') AS `Status`
	FROM ame_sys_form
	ORDER BY SortKey;

	SELECT 
	COALESCE(`Name`,'') AS `Name`,
	COALESCE(Text,'') AS Text,
	COALESCE(SortKey,0) AS SortKey,
	COALESCE(`Status`,'') AS `Status`
	FROM ame_sys_menu
	ORDER BY SortKey;

	SELECT 
	COALESCE(`Name`,'') AS `Name`,
	COALESCE(FrmSysName,'') AS FrmSysName,
	COALESCE(FrmName,'') AS FrmName,
	COALESCE(`Status`,'') AS 'Status',
	COALESCE(FrmTag,'') AS FrmTag,
	COALESCE(SortKey,0) AS SortKey,
	COALESCE(`FrmStatus`,'') AS `FrmStatus`
	FROM ame_sys_menudetail
	ORDER BY SortKey;

	SELECT var_name,var_text,int_sortkey,var_uid,var_tag FROM ame_sys_menu_fev WHERE var_uid = P_UID ORDER BY int_sortkey;

	SELECT COALESCE(`name`,'') AS `Name`,
				COALESCE(text,'') AS Text,
				COALESCE(SortKey,0) AS SortKey,
				COALESCE(`status`,'') AS `Status` 
				FROM ame_sys_menu_process ORDER BY `name`,sortkey;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_SYS_PREFIX_CUSTOM`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_SYS_PREFIX_CUSTOM`()
    DETERMINISTIC
BEGIN
		# CONCAT(var_id, LPAD(int_lastid, int_length, '0'))
		SELECT var_modid AS 'Module Id', var_type AS 'Type', var_id AS 'Number Prefix', var_desc AS 'Remark', int_length AS 'Length', int_lastid AS 'New Id', var_id AS 'Format' FROM ame_sys_prefix_custom ORDER BY var_type, var_id;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_SYS_TYPE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_SYS_TYPE`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		CASE
		WHEN P1 = "AP_RF" THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id IN('AP_RF') ORDER BY int_sortor;
		WHEN P1 = "AR_RF" THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id IN('AR_RF') ORDER BY int_sortor;
		WHEN P1 = "MT_VEN" THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id IN('POST_ST', 'DATE_RANGE', 'IC_TYPE', 'METHOD') ORDER BY int_sortor;
		WHEN P1 = "AP_IN" THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id IN('AP_IN_TT', 'TERM') ORDER BY int_sortor;
		WHEN P1 = "AR_IN_POS_TT" THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id = 'AR_IN_POS_TT' ORDER BY int_sortor;
		WHEN P1 = "AP_PY" THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id IN('AP_PY_TT', 'METHOD') ORDER BY int_sortor;
		WHEN P1 = "AR_IN" THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id IN('AR_IN_TT', 'TERM') ORDER BY int_sortor;
		WHEN P1 = "AR_PY" THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id IN('AR_PY_TT', 'METHOD') ORDER BY int_sortor;
		WHEN P1 = "POST" THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id IN('POST_TT', 'POST_ST', 'DATE_RANGE') ORDER BY int_sortor;
		WHEN P1 = "M001D005" THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id IN('DATE_RANGE', 'AP_IN_TT', 'METHOD') ORDER BY int_sortor;
		WHEN P1 IN("M001D001", "M001D002", "M001D003", "M001D004", "M001D006") THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id IN('DATE_RANGE', 'AP_IN_TT', 'POST_ST') ORDER BY int_sortor;
		WHEN P1 IN("M002D001", "M002D002", "M002D003", "M002D004", "M002D006") THEN
				SELECT var_id, var_name FROM ame_sys_type WHERE var_id IN('DATE_RANGE', 'AP_IN_TT', 'POST_ST') ORDER BY int_sortor;
		ELSE
		SET @Donothing = NULL;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_SYS_USED`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_SYS_USED`(P1 varchar(100), P2 varchar(2000))
    DETERMINISTIC
BEGIN
CASE P1
WHEN 'MT_VEN' THEN
SELECT T1.var_venid FROM ame_ap_in T1 WHERE T1.var_venid = P2 AND T1.var_status <> 'Deleted'
UNION ALL
SELECT T1.var_venid FROM ame_ap_py T1 WHERE T1.var_venid = P2 AND T1.var_status <> 'Deleted';
WHEN 'MT_CUS' THEN
SELECT T1.var_cusid FROM ame_ar_in T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted'
UNION ALL
SELECT T1.var_cusid FROM ame_ar_py T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted';
WHEN 'MT_CUS_MSG' THEN
SET @ID = AME_FUN_SPLIT_STRING(P2, ";", 1);
SET @CUS = AME_FUN_SPLIT_STRING(P2, ";", 2);
SELECT T1.var_id FROM ame_mt_cus T1 WHERE T1.int_msg = @ID AND T1.var_id <> @CUS;
WHEN 'MT_ITE' THEN
SELECT T2.var_itemid FROM ame_ap_in T1 RIGHT JOIN ame_ap_in_det T2 ON (T1.var_id = T2.var_id) WHERE T1.var_status <> 'Deleted' AND T2.var_itemid = P2
UNION ALL
SELECT T2.var_itemid FROM ame_ar_in T1 RIGHT JOIN ame_ar_in_det T2 ON (T1.var_id = T2.var_id) WHERE T1.var_status <> 'Deleted' AND T2.var_itemid = P2;
ELSE
SET @Donothing = NULL;
END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_UPDATE_AR_IN_BATCH`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_UPDATE_AR_IN_BATCH`(P1 varchar(100), P2 integer, P3 varchar(100), P4 varchar(100), P5 integer, P6 double, P7 double,  P8 double, P9 double, P10 double, P11 double, P12 double)
    DETERMINISTIC
BEGIN
		DECLARE P_DEC_QTY INTEGER DEFAULT 0;
		DECLARE P_DEC_COST INTEGER DEFAULT 0;
		# DECIMAL
		BEGIN
		DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS T.int_qty, T.int_price FROM ame_sys_decimal T WHERE T.var_id = 'AR';
		OPEN C1;
		IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_QTY, P_DEC_COST; END IF;
		CLOSE C1;
		END;
		INSERT INTO ame_ar_in_det_batch(var_id, int_line, var_locid, var_itemid, int_batchno, dou_qty, dou_uc, dou_ac1, dou_ac2, dou_ac3, dou_ac4, dou_ac5)	VALUES
		(P1, P2, P3, P4, P5, ROUND(P6, P_DEC_QTY), ROUND(P7, P_DEC_COST), ROUND(P8, P_DEC_COST), ROUND(P9, P_DEC_COST), ROUND(P10, P_DEC_COST), ROUND(P11, P_DEC_COST), ROUND(P12, P_DEC_COST));
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_UPDATE_CUR_DEC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_UPDATE_CUR_DEC`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE C1 CURSOR FOR SELECT T1.var_id FROM ame_sys_decimal T1 WHERE T1.var_id = P1;
		OPEN C1;
				IF FOUND_ROWS() = 0 THEN
						INSERT INTO ame_sys_decimal(var_id, int_qty, int_cost, int_price, int_amt, int_per, int_rate, int_line) (SELECT P1, T1.int_qty, T1.int_cost, T1.int_price, T1.int_amt, T1.int_per, T1.int_rate, T1.int_line FROM ame_sys_decimal T1 WHERE T1.var_id = (SELECT T2.var_id FROM ame_mt_cur T2 WHERE T2.tin_default = 1));
				END IF;
		CLOSE C1;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_UPDATE_CUR_RATE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_UPDATE_CUR_RATE`(P1 varchar(100), P2 double)
    DETERMINISTIC
BEGIN
		DECLARE P_RATE DOUBLE DEFAULT 1;
		DECLARE C1 CURSOR FOR SELECT T1.dou_rate FROM ame_mt_cur_rate T1 WHERE T1.var_id = P1;
		OPEN C1;
				IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_RATE;
						IF P2 <> P_RATE THEN 		
								INSERT INTO ame_mt_cur_rate_his(var_id, dou_rate) VALUES(P1, P2);
								UPDATE ame_mt_cur_rate SET dou_rate = P2 WHERE var_id = P1;
						END IF;
				ELSE
						INSERT INTO ame_mt_cur_rate(var_id, dou_rate) VALUES(P1, P2);
						INSERT INTO ame_mt_cur_rate_his(var_id, dou_rate) VALUES(P1, P2);
				END IF;
		CLOSE C1;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_UPDATE_GL`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_UPDATE_GL`(P1 varchar(100), P2 varchar(100), P3 varchar(100), P4  date, P5 double, P6 integer, P7 varchar(100), P8 double, P9 integer)
    DETERMINISTIC
BEGIN
		# P1 = ACCOUNT NO : P2 = CLASS : P3 = DR , CR : P4 = DATE : P5 = AMOUNT
		DECLARE P_COUNT INTEGER DEFAULT 0;
		SELECT COUNT(T.var_accid) INTO P_COUNT FROM ame_gl T WHERE T.var_accid = P1 AND T.var_claid = P2 AND T.var_cur = P7 AND T.dat_date = P4;
		IF P_COUNT > 0 THEN
				IF P3 = "Dr" Then
						UPDATE ame_gl T SET T.dou_dr = AME_FUN_ROUND(COALESCE(T.dou_dr, 0) + P5, P6), T.dou_fundr = AME_FUN_ROUND(COALESCE(T.dou_fundr, 0) + P8, P9) WHERE T.var_accid = P1 AND T.var_claid = P2 AND T.var_cur = P7 AND T.dat_date = P4;
				ELSE
						UPDATE ame_gl T SET T.dou_cr = AME_FUN_ROUND(COALESCE(T.dou_cr, 0) + P5, P6), T.dou_funcr = AME_FUN_ROUND(COALESCE(T.dou_funcr, 0) + P8, P9) WHERE T.var_accid = P1 AND T.var_claid = P2 AND T.var_cur = P7 AND T.dat_date = P4;
				END IF;
		ELSE
				IF P3 = "Dr" Then
						INSERT INTO ame_gl(var_accid, var_claid, var_cur, dat_date, dou_dr, dou_cr, dou_fundr, dou_funcr) VALUES(P1, P2, P7, P4, AME_FUN_ROUND(P5, P6), 0, AME_FUN_ROUND(P8, P9), 0);
				ELSE
						INSERT INTO ame_gl(var_accid, var_claid, var_cur, dat_date, dou_dr, dou_cr, dou_fundr, dou_funcr) VALUES(P1, P2, P7, P4, 0, AME_FUN_ROUND(P5, P6), 0, AME_FUN_ROUND(P8, P9));
				END IF;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_UPDATE_GL_JE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_UPDATE_GL_JE`(P1 integer, P2 varchar(100), P3 varchar(100), P4 varchar(250), P5 varchar(2000), P6 varchar(100), P7 datetime, P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 double, P12 double, P13 varchar(100), P14 double, P15 integer, P16 integer)
    DETERMINISTIC
BEGIN
		DECLARE	P_AMT_FOR DOUBLE DEFAULT 0.0;
		DECLARE	P_AMT_FUN DOUBLE DEFAULT 0.0;
		SELECT COALESCE(SUM(T1.dou_amt), 0), COALESCE(SUM(T1.dou_funamt), 0) INTO P_AMT_FOR, P_AMT_FUN FROM ame_gl_je_det T1 WHERE T1.int_jid = P1 AND T1.var_drcr = 'Dr';
		INSERT INTO ame_gl_je(int_jid, var_doctype, var_docid, var_ref, var_desc, var_status, var_create, dat_date, dat_create, var_accid, var_modid, var_acctype, dou_tamt, dou_tnet, var_cur, dou_rate, dou_funamt, dou_funnet) VALUES
		(P1, P2, P3, P4, P5, 'Open', P6, P7, NOW(), P8, P9, P10, AME_FUN_ROUND(P11, P15), AME_FUN_ROUND(P_AMT_FOR, P15), P13, P14, AME_FUN_ROUND(P12, P16), AME_FUN_ROUND(P_AMT_FUN, P16));
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_UPDATE_GL_JE_DET`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_UPDATE_GL_JE_DET`(P1 int(11), P2 int(11), P3 varchar(100), P4 varchar(100), P5 varchar(20), P6 varchar(100), P7 varchar(100), P8 varchar(250), P9 varchar(100),  P10 varchar(100),  P11 varchar(100), P12 varchar(100), P13 varchar(2000), P14 varchar(2), P15 varchar(100), P16 varchar(100), P17 varchar(250), P18 varchar(100), P19 varchar(2000), P20 datetime, P21 datetime, P22 datetime, P23 double, P24 int(11), P25 varchar(100), P26 varchar(20), P27 varchar(100), P28 varchar(100), P29 varchar(250), P30 varchar(2000), P31 datetime, P32 datetime, P33 varchar(100), P34 varchar(100), P35 double, P36 double)
    DETERMINISTIC
BEGIN
		INSERT INTO ame_gl_je_det(int_jid, int_line, var_accid, var_claid, var_modid, var_doctype, var_docid, var_docref, var_empid, var_venid, var_cusid, var_itemid, var_desc, var_drcr, var_docterm, var_pmttype, var_pmtref, var_pricecode, var_shipaddr, dat_date, dat_docdate, dat_docdue, dou_amt, var_appterm, var_appmodid, var_apptype, var_appid, var_appref, var_appdesc, dat_appdate, dat_appdue, var_acctype, var_cur, dou_rate, dou_funamt)
		VALUES (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21, P22, AME_FUN_ROUND(P23, P24), P25, P26, P27, P28, P29, P30, P31, P32, P33, P34, P35, P36);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_UPDATE_PMT_STATUS`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_UPDATE_PMT_STATUS`(P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		# NOTE: P1 = TYPE, P2 = ENTRY NO, P3 = CURRENCY
		DECLARE P_DEC_AMT_FOR INTEGER DEFAULT 2;
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT int_amt FROM ame_sys_decimal T WHERE T.var_id = (SELECT T2.var_id FROM ame_mt_cur T2 WHERE T2.tin_default = 1);OPEN C1;IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FUN; END IF;CLOSE C1;END;
		BEGIN	DECLARE C1 CURSOR FOR SELECT T.int_qty, T.int_amt FROM ame_sys_decimal T WHERE T.var_id = P3; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_QTY, P_DEC_AMT_FOR; END IF;CLOSE C1; END;
		CASE P1
		WHEN "AP-IN" THEN
				# UPDATE PO
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DEClARE C1 CURSOR FOR SELECT T1.var_poid FROM ame_ap_in_det AS T1 WHERE T1.var_id = P2 AND COALESCE(T1.var_poid, '') <> '' GROUP BY T1.var_poid;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;	
								L1: LOOP FETCH C1 INTO P_ID;
										IF D1 = 1 THEN LEAVE L1; END IF;
										CALL AME_PRO_UPDATE_PMT_STATUS_1("AP-PO", P_ID, P3);
								END LOOP L1;
						CLOSE C1;			
				END;
				# UPDATE AR-IN
				CALL AME_PRO_UPDATE_PMT_STATUS_1("AP-RET", P2, P3);
				# UPDATE VENDOR
				BEGIN
						DECLARE P_STATUS INTEGER DEFAULT 0;
						DECLARE P_DOC VARCHAR(100) DEFAULT "";
						DECLARE P_CUR VARCHAR(100) DEFAULT "";
						DECLARE P_VEN VARCHAR(100) DEFAULT "";
						DECLARE P_RATE DOUBLE DEFAULT 0.0;
						DECLARE C1 CURSOR FOR SELECT T1.var_id, T3.var_cur, T3.dou_rate, T3.var_venid FROM ame_ap_py_det T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id LEFT JOIN ame_ap_in T3 ON T1.var_docid = T3.var_id WHERE T1.var_docid = P2 AND COALESCE(T2.tin_paid, 0) = 1;
						OPEN C1; 
								IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DOC, P_CUR, P_RATE, P_VEN; 
										SELECT COALESCE(COUNT(var_id), 0) INTO P_STATUS FROM ame_ap_in T1 WHERE T1.var_id = P2 AND T1.var_status = 'Deleted';
										IF P_STATUS = 0 THEN
												UPDATE ame_ap_py T1 SET T1.var_venid = P_VEN, T1.var_cur = P_CUR, T1.dou_rate = P_RATE WHERE var_id = P_DOC; 
										ELSE
												UPDATE ame_ap_py T1 SET T1.var_status = 'Deleted' WHERE var_id = P_DOC; 
										END IF;
								END IF;
						CLOSE C1;
				END; 
		WHEN "AP-RET" THEN
				# UPDATE PO
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DEClARE C1 CURSOR FOR SELECT T1.var_poid FROM ame_ap_ret_det AS T1 WHERE T1.var_id = P2 AND COALESCE(T1.var_poid, '') <> '' GROUP BY T1.var_poid;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;	
								L1: LOOP FETCH C1 INTO P_ID;
										IF D1 = 1 THEN LEAVE L1; END IF;
										CALL AME_PRO_UPDATE_PMT_STATUS_1("AP-RET", P_ID, P3);
								END LOOP L1;
						CLOSE C1;			
				END;
		WHEN "AR-SO" THEN
				# UPDATE QUO
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DEClARE C1 CURSOR FOR SELECT T1.var_poid FROM ame_ar_so_det AS T1 WHERE T1.var_id = P2 AND COALESCE(T1.var_poid, '') <> '' GROUP BY T1.var_poid;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;	
								L1: LOOP FETCH C1 INTO P_ID;
										IF D1 = 1 THEN LEAVE L1; END IF;
										CALL AME_PRO_UPDATE_PMT_STATUS_1("AR-QUO", P_ID, P3);
								END LOOP L1;
						CLOSE C1;			
				END;
		WHEN "AR-IN" THEN
				# UPDATE AR-QUO / AR-SO
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_TYPE VARCHAR(100) DEFAULT "";
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DEClARE C1 CURSOR FOR SELECT T1.var_potype, T1.var_poid FROM ame_ar_in_det AS T1 WHERE T1.var_id = P2 AND COALESCE(T1.var_poid, '') <> '' GROUP BY T1.var_potype, T1.var_poid;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;	
								L1: LOOP FETCH C1 INTO P_TYPE, P_ID;
										IF D1 = 1 THEN LEAVE L1; END IF;
										IF P_TYPE = "AR-QUO" THEN
												CALL AME_PRO_UPDATE_PMT_STATUS_1("AR-QUO", P_ID, P3);
										ELSE
												CALL AME_PRO_UPDATE_PMT_STATUS_1("AR-SO", P_ID, P3);
										END IF;					
								END LOOP L1;
						CLOSE C1;			
				END;
				# UPDATE AR-IN
				CALL AME_PRO_UPDATE_PMT_STATUS_1("AR-RET", P2, P3);
				# UPDATE AR-SM
				CALL AME_PRO_UPDATE_PMT_STATUS_1("AR-SM-IN", P2, P3);
				# UPDATE CUSTOMER
				BEGIN
						DECLARE P_STATUS INTEGER DEFAULT 0;
						DECLARE P_DOC VARCHAR(100) DEFAULT "";
						DECLARE P_CUR VARCHAR(100) DEFAULT "";
						DECLARE P_VEN VARCHAR(100) DEFAULT "";
						DECLARE P_RATE DOUBLE DEFAULT 0.0;
						DECLARE C1 CURSOR FOR SELECT T1.var_id, T3.var_cur, T3.dou_rate, T3.var_cusid FROM ame_ar_py_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id LEFT JOIN ame_ar_in T3 ON T1.var_docid = T3.var_id WHERE T1.var_docid = P2 AND COALESCE(T2.tin_paid, 0) = 1;
						OPEN C1; 
								IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DOC, P_CUR, P_RATE, P_VEN; 
										SELECT COALESCE(COUNT(var_id), 0) INTO P_STATUS FROM ame_ar_in T1 WHERE T1.var_id = P2 AND T1.var_status = 'Deleted';
										IF P_STATUS = 0 THEN
												UPDATE ame_ar_py T1 SET T1.var_cusid = P_VEN, T1.var_cur = P_CUR, T1.dou_rate = P_RATE WHERE var_id = P_DOC; 
										ELSE
												UPDATE ame_ar_py T1 SET T1.var_status = 'Deleted' WHERE var_id = P_DOC; 
										END IF;		
								END IF;
						CLOSE C1;
				END; 
		WHEN "AR-RET" THEN
				# UPDATE RETURN
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DEClARE C1 CURSOR FOR SELECT T1.var_poid FROM ame_ar_ret_det AS T1 WHERE T1.var_id = P2 AND COALESCE(T1.var_poid, '') <> '' GROUP BY T1.var_poid;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;	
								L1: LOOP FETCH C1 INTO P_ID;
										IF D1 = 1 THEN LEAVE L1; END IF;
										CALL AME_PRO_UPDATE_PMT_STATUS_1("AR-RET", P_ID, P3);
								END LOOP L1;
						CLOSE C1;			
				END;
		WHEN "AR-SM-IN" THEN
				# UPDATE SHIPMENT
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DEClARE C1 CURSOR FOR SELECT T1.var_poid FROM ame_ar_sm_det AS T1 WHERE T1.var_id = P2 AND COALESCE(T1.var_poid, '') <> '' GROUP BY T1.var_poid;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;	
								L1: LOOP FETCH C1 INTO P_ID;
										IF D1 = 1 THEN LEAVE L1; END IF;
										CALL AME_PRO_UPDATE_PMT_STATUS_1(P1, P_ID, P3);
								END LOOP L1;
						CLOSE C1;			
				END;
		WHEN "AR-SM-RET" THEN
				# UPDATE SHIPMENT
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DEClARE C1 CURSOR FOR SELECT T1.var_poid FROM ame_ar_sm_det AS T1 WHERE T1.var_id = P2 AND COALESCE(T1.var_poid, '') <> '' GROUP BY T1.var_poid;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;	
								L1: LOOP FETCH C1 INTO P_ID;
										IF D1 = 1 THEN LEAVE L1; END IF;
										CALL AME_PRO_UPDATE_PMT_STATUS_1(P1, P_ID, P3);
								END LOOP L1;
						CLOSE C1;			
				END;
		WHEN "AR-SM-CN" THEN
				# UPDATE SHIPMENT
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DEClARE C1 CURSOR FOR SELECT T1.var_poid FROM ame_ar_sm_det AS T1 WHERE T1.var_id = P2 AND COALESCE(T1.var_poid, '') <> '' GROUP BY T1.var_poid;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;	
								L1: LOOP FETCH C1 INTO P_ID;
										IF D1 = 1 THEN LEAVE L1; END IF;
										CALL AME_PRO_UPDATE_PMT_STATUS_1(P1, P_ID, P3);
								END LOOP L1;
						CLOSE C1;			
				END;
		WHEN "AP-RF" THEN
				# UPDATE REFUND
				BEGIN
						DECLARE P_DOC VARCHAR(100) DEFAULT "";
						DECLARE P_DOC_TYPE VARCHAR(100) DEFAULT "";
						DECLARE P_AMT DOUBLE DEFAULT 0.0;
						DEClARE C3 CURSOR FOR SELECT T1.var_doctype, T1.var_docid, T1.dou_amt FROM ame_refund AS T1 WHERE T1.var_id = P2 AND T1.int_line = 0;
						OPEN C3;
								IF FOUND_ROWS() > 0 THEN FETCH C3 INTO P_DOC_TYPE, P_DOC, P_AMT;
										CASE P_DOC_TYPE
										WHEN "AP-RET" THEN
												BEGIN
														DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_RATE DOUBLE DEFAULT 0.0;
														DECLARE C2 CURSOR FOR SELECT T1.dou_rate, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE((SELECT SUM(T1.dou_amt) FROM ame_refund T1 WHERE T1.var_doctype = P_DOC_TYPE AND T1.var_docid = P_DOC AND T1.var_status = "Posted"), 0) + COALESCE((SELECT SUM(T1.dou_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 T1.var_docid = P_DOC AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ap_ret T1 WHERE T1.var_id = P_DOC;
														OPEN C2;
																IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_DOC_RATE, P_DOC_NET, P_DOC_PAID;
																		IF P_DOC_NET = P_DOC_PAID THEN
																				UPDATE ame_ap_ret SET var_pmtstatus = "Fully Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSEIF P_DOC_NET > P_DOC_PAID AND P_DOC_PAID > 0 THEN
																				UPDATE ame_ap_ret SET var_pmtstatus = "Partially Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSE
																				UPDATE ame_ap_ret SET var_pmtstatus = "Unpaid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		END IF;
																END IF;
														CLOSE C2;
												END;
										WHEN "AP-DN" THEN
												BEGIN
														DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_RATE DOUBLE DEFAULT 0.0;
														DECLARE C2 CURSOR FOR SELECT T1.dou_rate, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE((SELECT SUM(T1.dou_amt) FROM ame_refund T1 WHERE T1.var_doctype = P_DOC_TYPE AND T1.var_docid = P_DOC AND T1.var_status = "Posted"), 0) + COALESCE((SELECT SUM(T1.dou_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 T1.var_docid = P_DOC AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ap_dn T1 WHERE T1.var_id = P_DOC;
														OPEN C2;
																IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_DOC_RATE, P_DOC_NET, P_DOC_PAID;
																		IF P_DOC_NET = P_DOC_PAID THEN
																				UPDATE ame_ap_dn SET var_pmtstatus = "Fully Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSEIF P_DOC_NET > P_DOC_PAID AND P_DOC_PAID > 0 THEN
																				UPDATE ame_ap_dn SET var_pmtstatus = "Partially Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSE
																				UPDATE ame_ap_dn SET var_pmtstatus = "Unpaid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		END IF;
																END IF;
														CLOSE C2;
												END;
										WHEN "AP-PY" THEN
												BEGIN
														DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_RATE DOUBLE DEFAULT 0.0;
														DECLARE C2 CURSOR FOR SELECT T1.dou_rate, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE((SELECT SUM(T1.dou_amt) FROM ame_refund T1 WHERE T1.var_doctype = P_DOC_TYPE AND T1.var_docid = P_DOC AND T1.var_status = "Posted"), 0) + COALESCE((SELECT SUM(T1.dou_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 T1.var_docid = P_DOC AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ap_py T1 WHERE T1.var_id = P_DOC;
														OPEN C2;
																IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_DOC_RATE, P_DOC_NET, P_DOC_PAID;
																		IF P_DOC_NET = P_DOC_PAID THEN
																				UPDATE ame_ap_py SET var_pmtstatus = "Fully Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSEIF P_DOC_NET > P_DOC_PAID AND P_DOC_PAID > 0 THEN
																				UPDATE ame_ap_py SET var_pmtstatus = "Partially Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSE
																				UPDATE ame_ap_py SET var_pmtstatus = "Unpaid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		END IF;
																END IF;
														CLOSE C2;
												END;
										ELSE
												SET @Donothing = NULL;
										END CASE;
								END IF;
						CLOSE C3;
				END;
		WHEN "AR-RF" THEN
				# UPDATE REFUND
				BEGIN
						DECLARE P_DOC VARCHAR(100) DEFAULT "";
						DECLARE P_DOC_TYPE VARCHAR(100) DEFAULT "";
						DECLARE P_AMT DOUBLE DEFAULT 0.0;
						DEClARE C3 CURSOR FOR SELECT T1.var_doctype, T1.var_docid, T1.dou_amt FROM ame_refund AS T1 WHERE T1.var_id = P2 AND T1.int_line = 1;
						OPEN C3;
								IF FOUND_ROWS() > 0 THEN FETCH C3 INTO P_DOC_TYPE, P_DOC, P_AMT;
										CASE P_DOC_TYPE
										WHEN "AR-RET" THEN
												BEGIN
														DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_RATE DOUBLE DEFAULT 0.0;
														DECLARE C2 CURSOR FOR SELECT T1.dou_rate, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE((SELECT SUM(T1.dou_amt) FROM ame_refund T1 WHERE T1.var_doctype = P_DOC_TYPE AND T1.var_docid = P_DOC AND T1.var_status = "Posted"), 0) + COALESCE((SELECT SUM(T1.dou_app) FROM ame_ar_py_app T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = "Sales Return" AND T1.var_docid = P_DOC AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ar_ret T1 WHERE T1.var_id = P_DOC;
														OPEN C2;
																IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_DOC_RATE, P_DOC_NET, P_DOC_PAID;
																		IF P_DOC_NET = P_DOC_PAID THEN
																				UPDATE ame_ar_ret SET var_pmtstatus = "Fully Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSEIF P_DOC_NET > P_DOC_PAID AND P_DOC_PAID > 0 THEN
																				UPDATE ame_ar_ret SET var_pmtstatus = "Partially Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSE
																				UPDATE ame_ar_ret SET var_pmtstatus = "Unpaid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		END IF;
																END IF;
														CLOSE C2;
												END;
										WHEN "AR-CN" THEN
												BEGIN
														DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_RATE DOUBLE DEFAULT 0.0;
														DECLARE C2 CURSOR FOR SELECT T1.dou_rate, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE((SELECT SUM(T1.dou_amt) FROM ame_refund T1 WHERE T1.var_doctype = P_DOC_TYPE AND T1.var_docid = P_DOC AND T1.var_status = "Posted"), 0) + COALESCE((SELECT SUM(T1.dou_app) FROM ame_ar_py_app T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = "Credit Note" AND T1.var_docid = P_DOC AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ar_cn T1 WHERE T1.var_id = P_DOC;
														OPEN C2;
																IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_DOC_RATE, P_DOC_NET, P_DOC_PAID;
																		IF P_DOC_NET = P_DOC_PAID THEN
																				UPDATE ame_ar_cn SET var_pmtstatus = "Fully Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSEIF P_DOC_NET > P_DOC_PAID AND P_DOC_PAID > 0 THEN
																				UPDATE ame_ar_cn SET var_pmtstatus = "Partially Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSE
																				UPDATE ame_ar_cn SET var_pmtstatus = "Unpaid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		END IF;
																END IF;
														CLOSE C2;
												END;
										WHEN "AR-PY" THEN
												BEGIN
														DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
														DECLARE P_DOC_RATE DOUBLE DEFAULT 0.0;
														DECLARE C2 CURSOR FOR SELECT T1.dou_rate, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE((SELECT SUM(T1.dou_amt) FROM ame_refund T1 WHERE T1.var_doctype = P_DOC_TYPE AND T1.var_docid = P_DOC AND T1.var_status = "Posted"), 0) + COALESCE((SELECT SUM(T1.dou_app) FROM ame_ar_py_app T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = "Prepayment" AND T1.var_docid = P_DOC AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ar_py T1 WHERE T1.var_id = P_DOC;
														OPEN C2;
																IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_DOC_RATE, P_DOC_NET, P_DOC_PAID;
																		IF P_DOC_NET = P_DOC_PAID THEN
																				UPDATE ame_ar_py SET var_pmtstatus = "Fully Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSEIF P_DOC_NET > P_DOC_PAID AND P_DOC_PAID > 0 THEN
																				UPDATE ame_ar_py SET var_pmtstatus = "Partially Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		ELSE
																				UPDATE ame_ar_py SET var_pmtstatus = "Unpaid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																		END IF;
																END IF;
														CLOSE C2;
												END;
										ELSE
												SET @Donothing = NULL;
										END CASE;
								END IF;
						CLOSE C3;
				END;
		ELSE
				SET @Donothing = NULL;
		END CASE;
		
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_UPDATE_PMT_STATUS_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_UPDATE_PMT_STATUS_1`(P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		# NOTE: P1 = MODULE, P2 = ENTRY NO, P3 = CURRENCY
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT int_amt FROM ame_sys_decimal T WHERE T.var_id = (SELECT T2.var_id FROM ame_mt_cur T2 WHERE T2.tin_default = 1);OPEN C1;IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FUN; END IF;CLOSE C1;END;
		BEGIN	DECLARE C1 CURSOR FOR SELECT T.int_qty, T.int_amt FROM ame_sys_decimal T WHERE T.var_id = P3; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_QTY, P_DEC_AMT; END IF;CLOSE C1; END;
		CASE P1
		WHEN "AP-PO" THEN	
				# UPDATE PO
				UPDATE ame_ap_po_det T1 SET T1.dou_orderqty = 0, T1.dou_orderfoc = 0 WHERE T1.var_id = P2;
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_LIN INTEGER DEFAULT 0;
						DECLARE P_DOC VARCHAR(100) DEFAULT "";
						DECLARE P_LOC VARCHAR(100) DEFAULT "";
						DECLARE P_ITE VARCHAR(100) DEFAULT "";
						DECLARE P_UOM VARCHAR(100) DEFAULT "";
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_FOC DOUBLE DEFAULT 0.0;
						DEClARE C1 CURSOR FOR SELECT T1.var_poid, T1.int_poline, T1.var_locid, T1.var_itemid, T1.var_uomid, AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0), P_DEC_QTY), AME_FUN_ROUND(COALESCE(SUM(T1.dou_foc), 0), P_DEC_QTY) FROM ame_ap_in_det T1 LEFT JOIN ame_ap_in T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T2.var_status IN('Open', 'Posted') GROUP BY T1.var_locid, T1.var_itemid, T1.var_uomid, T1.int_poline;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;		
								L: LOOP FETCH C1 INTO P_DOC, P_LIN, P_LOC, P_ITE, P_UOM, P_QTY, P_FOC;
										IF D1 = 1 THEN LEAVE L; END IF;
										UPDATE ame_ap_po_det T1 SET T1.dou_orderqty = P_QTY, T1.dou_orderfoc = P_FOC WHERE T1.var_id = P_DOC AND T1.int_poline = P_LIN AND T1.var_locid = P_LOC AND T1.var_itemid = P_ITE AND T1.var_uomid = P_UOM;
								END LOOP L;
						CLOSE C1;			
				END;
				BEGIN
						DECLARE D2 INTEGER DEFAULT 0;
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_QTY_ORD DOUBLE DEFAULT 0.0;
						DECLARE P_STA_CLEAR_UN VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_PARTIAL VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_FULL VARCHAR(100) DEFAULT "";
						DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(T1.dou_orderqty + T1.dou_orderfoc, P_DEC_QTY), AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P_DEC_QTY) FROM ame_ap_po_det T1 WHERE T1.var_id = P2;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN
										L2 : LOOP FETCH C2 INTO P_QTY_ORD, P_QTY;
												IF D2 = 1 THEN LEAVE L2; END IF;
												IF P_QTY_ORD = 0 AND P_QTY > 0 THEN
														SET P_STA_CLEAR_UN = "Not";
												ELSEIF P_QTY_ORD < P_QTY THEN
														SET P_STA_CLEAR_PARTIAL = 'Partial'; LEAVE L2;
												ELSEIF P_QTY_ORD >= P_QTY THEN
														SET P_STA_CLEAR_FULL = 'Full';
												END IF;
										END LOOP L2;	
										IF P_STA_CLEAR_PARTIAL <> "" OR (P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN <> "") THEN 
												UPDATE ame_ap_po T1 SET T1.var_status = 'Open', T1.var_pmtstatus = 'Partially Received' WHERE T1.var_id = P2; 
										ELSEIF P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN = "" THEN
												UPDATE ame_ap_po T1 SET T1.var_status = 'Closed', T1.var_pmtstatus = 'Fully Received' WHERE T1.var_id = P2; 
										ELSE
												UPDATE ame_ap_po T1 SET T1.var_status = 'Open', T1.var_pmtstatus = 'Not Received' WHERE T1.var_id = P2;
										END IF;		
								ELSE
										UPDATE ame_ap_po T1 SET T1.var_status = 'Open', T1.var_pmtstatus = 'Not Received' WHERE T1.var_id = P2;
								END IF;
						CLOSE C2;
				END;
		WHEN "AP-IN" THEN
				# UPDATE PAID
				BEGIN
						DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
						DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
						DECLARE P_RATE DOUBLE DEFAULT 0.0;
						DECLARE C2 CURSOR FOR SELECT T1.dou_rate, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT), AME_FUN_ROUND(COALESCE((SELECT SUM(T1.dou_amt + T1.dou_app + T1.dou_disc) FROM ame_ap_py_det T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P2 AND T2.var_status = "Posted"), 0), P_DEC_AMT) FROM ame_ap_in T1 WHERE T1.var_id = P2;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN
										FETCH C2 INTO P_RATE, P_DOC_NET, P_DOC_PAID;
										IF P_DOC_NET = P_DOC_PAID THEN
												UPDATE ame_ap_in SET var_pmtstatus = "Fully Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_RATE, P_DEC_AMT_FUN) WHERE var_id = P2;
										ELSEIF P_DOC_NET > P_DOC_PAID AND P_DOC_PAID > 0 THEN
												UPDATE ame_ap_in SET var_pmtstatus = "Partially Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_RATE, P_DEC_AMT_FUN) WHERE var_id = P2;
										ELSE
												UPDATE ame_ap_in SET var_pmtstatus = "Unpaid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_RATE, P_DEC_AMT_FUN) WHERE var_id = P2;
										END IF;
								END IF;
						CLOSE C2;
				END;			
		WHEN "AP-RET" THEN
				# UPDATE RETURN
				UPDATE ame_ap_in_det T1 SET T1.dou_orderqty = 0, T1.dou_orderfoc = 0 WHERE T1.var_id = P2;
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_LIN INTEGER DEFAULT 0;
						DECLARE P_DOC VARCHAR(100) DEFAULT "";
						DECLARE P_LOC VARCHAR(100) DEFAULT "";
						DECLARE P_ITE VARCHAR(100) DEFAULT "";
						DECLARE P_UOM VARCHAR(100) DEFAULT "";
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_FOC DOUBLE DEFAULT 0.0;
						DEClARE C1 CURSOR FOR SELECT T1.var_poid, T1.int_poline, T1.var_locid, T1.var_itemid, T1.var_uomid, AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0), P_DEC_QTY), AME_FUN_ROUND(COALESCE(SUM(T1.dou_foc), 0), P_DEC_QTY) FROM ame_ap_ret_det T1 LEFT JOIN ame_ap_ret T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T2.var_status IN('Open', 'Posted') GROUP BY T1.var_locid, T1.var_itemid, T1.var_uomid, T1.int_poline;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;		
								L: LOOP FETCH C1 INTO P_DOC, P_LIN, P_LOC, P_ITE, P_UOM, P_QTY, P_FOC;
										IF D1 = 1 THEN LEAVE L; END IF;
										UPDATE ame_ap_in_det T1 SET T1.dou_orderqty = P_QTY, T1.dou_orderfoc = P_FOC WHERE T1.var_id = P_DOC AND T1.int_poline = P_LIN AND T1.var_locid = P_LOC AND T1.var_itemid = P_ITE AND T1.var_uomid = P_UOM;
								END LOOP L;
						CLOSE C1;			
				END;
				BEGIN
						DECLARE D2 INTEGER DEFAULT 0;
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_QTY_ORD DOUBLE DEFAULT 0.0;
						DECLARE P_STA_CLEAR_UN VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_PARTIAL VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_FULL VARCHAR(100) DEFAULT "";
						DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(T1.dou_orderqty + T1.dou_orderfoc, P_DEC_QTY), AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P_DEC_QTY) FROM ame_ap_in_det T1 WHERE T1.var_id = P2;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN
										L2 : LOOP FETCH C2 INTO P_QTY_ORD, P_QTY;
												IF D2 = 1 THEN LEAVE L2; END IF;
												IF P_QTY_ORD = 0 AND P_QTY > 0 THEN
														SET P_STA_CLEAR_UN = "Not";
												ELSEIF P_QTY_ORD < P_QTY THEN
														SET P_STA_CLEAR_PARTIAL = 'Partial'; LEAVE L2;
												ELSEIF P_QTY_ORD >= P_QTY THEN
														SET P_STA_CLEAR_FULL = 'Full';
												END IF;
										END LOOP L2;	
										IF P_STA_CLEAR_PARTIAL <> "" OR (P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN <> "") THEN 
												UPDATE ame_ap_in T1 SET T1.var_retstatus = 'Open', T1.var_retpmtstatus = 'Partially Returned' WHERE T1.var_id = P2; 
										ELSEIF P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN = "" THEN
												UPDATE ame_ap_in T1 SET T1.var_retstatus = 'Closed', T1.var_retpmtstatus = 'Fully Returned' WHERE T1.var_id = P2; 
										ELSE
												UPDATE ame_ap_in T1 SET T1.var_retstatus = 'Open', T1.var_retpmtstatus = 'Not Returned' WHERE T1.var_id = P2;
										END IF;		
								ELSE
										UPDATE ame_ap_in T1 SET T1.var_status = 'Open', T1.var_pmtstatus = 'Not Returned' WHERE T1.var_id = P2;
								END IF;
						CLOSE C2;
				END;
		WHEN "AR-QUO" THEN
				# UPDATE SO
				UPDATE ame_ar_quo_det T1 SET T1.dou_orderqty = 0, T1.dou_orderfoc = 0 WHERE T1.var_id = P2;
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_LIN INTEGER DEFAULT 0;
						DECLARE P_DOC VARCHAR(100) DEFAULT "";
						DECLARE P_LOC VARCHAR(100) DEFAULT "";
						DECLARE P_ITE VARCHAR(100) DEFAULT "";
						DECLARE P_UOM VARCHAR(100) DEFAULT "";
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_FOC DOUBLE DEFAULT 0.0;
						DEClARE C1 CURSOR FOR SELECT T1.var_poid, T1.int_poline, T1.var_locid, T1.var_itemid, T1.var_uomid, AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0), P_DEC_QTY), AME_FUN_ROUND(COALESCE(SUM(T1.dou_foc), 0), P_DEC_QTY) FROM ((SELECT T1.var_poid, T1.int_poline, T1.var_locid, T1.var_itemid, T1.var_uomid, AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0), P_DEC_QTY) AS dou_qty, AME_FUN_ROUND(COALESCE(SUM(T1.dou_foc), 0), P_DEC_QTY) AS dou_foc FROM ame_ar_so_det T1 LEFT JOIN ame_ar_so T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T2.var_status IN('Open', 'Posted') GROUP BY T1.var_locid, T1.var_itemid, T1.var_uomid, T1.int_poline)
											 UNION ALL (SELECT T1.var_poid, T1.int_poline, T1.var_locid, T1.var_itemid, T1.var_uomid, AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0), P_DEC_QTY) AS dou_qty, AME_FUN_ROUND(COALESCE(SUM(T1.dou_foc), 0), P_DEC_QTY) AS dou_foc FROM ame_ar_in_det T1 LEFT JOIN ame_ar_in T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T1.var_potype = "AR-QUO" AND T2.var_status IN('Open', 'Posted') GROUP BY T1.var_locid, T1.var_itemid, T1.var_uomid, T1.int_poline)) AS T1 GROUP BY T1.var_locid, T1.var_itemid, T1.var_uomid, T1.int_poline;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;		
								L: LOOP FETCH C1 INTO P_DOC, P_LIN, P_LOC, P_ITE, P_UOM, P_QTY, P_FOC;
										IF D1 = 1 THEN LEAVE L; END IF;
										UPDATE ame_ar_quo_det T1 SET T1.dou_orderqty = P_QTY, T1.dou_orderfoc = P_FOC WHERE T1.var_id = P_DOC AND T1.int_poline = P_LIN AND T1.var_locid = P_LOC AND T1.var_itemid = P_ITE AND T1.var_uomid = P_UOM;
								END LOOP L;
						CLOSE C1;			
				END;
				BEGIN
						DECLARE D2 INTEGER DEFAULT 0;
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_QTY_ORD DOUBLE DEFAULT 0.0;
						DECLARE P_STA_CLEAR_UN VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_PARTIAL VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_FULL VARCHAR(100) DEFAULT "";
						DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(T1.dou_orderqty + T1.dou_orderfoc, P_DEC_QTY), AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P_DEC_QTY) FROM ame_ar_quo_det T1 WHERE T1.var_id = P2;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN
										L2 : LOOP FETCH C2 INTO P_QTY_ORD, P_QTY;
												IF D2 = 1 THEN LEAVE L2; END IF;
												IF P_QTY_ORD = 0 AND P_QTY > 0 THEN
														SET P_STA_CLEAR_UN = "Not";
												ELSEIF P_QTY_ORD < P_QTY THEN
														SET P_STA_CLEAR_PARTIAL = 'Partial'; LEAVE L2;
												ELSEIF P_QTY_ORD >= P_QTY THEN
														SET P_STA_CLEAR_FULL = 'Full';
												END IF;
										END LOOP L2;	
										IF P_STA_CLEAR_PARTIAL <> "" OR (P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN <> "") THEN 
												UPDATE ame_ar_quo T1 SET T1.var_status = 'Open', T1.var_pmtstatus = 'Partially Received' WHERE T1.var_id = P2; 
										ELSEIF P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN = "" THEN
												UPDATE ame_ar_quo T1 SET T1.var_status = 'Closed', T1.var_pmtstatus = 'Fully Received' WHERE T1.var_id = P2; 
										ELSE
												UPDATE ame_ar_quo T1 SET T1.var_status = 'Open', T1.var_pmtstatus = 'Not Received' WHERE T1.var_id = P2;
										END IF;		
								ELSE
										UPDATE ame_ar_quo T1 SET T1.var_status = 'Open', T1.var_pmtstatus = 'Not Received' WHERE T1.var_id = P2;
								END IF;
						CLOSE C2;
				END;
		WHEN "AR-SO" THEN
				# UPDATE SO
				UPDATE ame_ar_so_det T1 SET T1.dou_orderqty = 0, T1.dou_orderfoc = 0 WHERE T1.var_id = P2;
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_LIN INTEGER DEFAULT 0;
						DECLARE P_DOC VARCHAR(100) DEFAULT "";
						DECLARE P_LOC VARCHAR(100) DEFAULT "";
						DECLARE P_ITE VARCHAR(100) DEFAULT "";
						DECLARE P_UOM VARCHAR(100) DEFAULT "";
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_FOC DOUBLE DEFAULT 0.0;
						DEClARE C1 CURSOR FOR SELECT T1.var_poid, T1.int_poline, T1.var_locid, T1.var_itemid, T1.var_uomid, AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0), P_DEC_QTY), AME_FUN_ROUND(COALESCE(SUM(T1.dou_foc), 0), P_DEC_QTY) FROM ame_ar_in_det T1 LEFT JOIN ame_ar_in T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T1.var_potype = "AR-SO" AND T2.var_status IN('Open', 'Posted') GROUP BY T1.var_locid, T1.var_itemid, T1.var_uomid, T1.int_poline;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;	
								L: LOOP FETCH C1 INTO P_DOC, P_LIN, P_LOC, P_ITE, P_UOM, P_QTY, P_FOC;
										IF D1 = 1 THEN LEAVE L; END IF;
										UPDATE ame_ar_so_det T1 SET T1.dou_orderqty = P_QTY, T1.dou_orderfoc = P_FOC WHERE T1.var_id = P_DOC AND T1.int_poline = P_LIN AND T1.var_locid = P_LOC AND T1.var_itemid = P_ITE AND T1.var_uomid = P_UOM;
								END LOOP L;
						CLOSE C1;			
				END;
				BEGIN
						DECLARE D2 INTEGER DEFAULT 0;
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_QTY_ORD DOUBLE DEFAULT 0.0;
						DECLARE P_STA_CLEAR_UN VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_PARTIAL VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_FULL VARCHAR(100) DEFAULT "";
						DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(T1.dou_orderqty + T1.dou_orderfoc, P_DEC_QTY), AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P_DEC_QTY) FROM ame_ar_so_det T1 WHERE T1.var_id = P2;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN
										L2 : LOOP FETCH C2 INTO P_QTY_ORD, P_QTY;
												IF D2 = 1 THEN LEAVE L2; END IF;
												IF P_QTY_ORD = 0 AND P_QTY > 0 THEN
														SET P_STA_CLEAR_UN = "Not";
												ELSEIF P_QTY > 0 AND P_QTY_ORD > 0 AND P_QTY_ORD < P_QTY THEN
														SET P_STA_CLEAR_PARTIAL = 'Partial'; LEAVE L2;
												ELSEIF P_QTY > 0  AND P_QTY_ORD >= P_QTY THEN
														SET P_STA_CLEAR_FULL = 'Full';
												END IF;
										END LOOP L2;	
										IF P_STA_CLEAR_PARTIAL <> "" OR (P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN <> "") THEN 
												UPDATE ame_ar_so T1 SET T1.var_status = 'Open', T1.var_pmtstatus = 'Partially Received' WHERE T1.var_id = P2; 
										ELSEIF P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN = "" THEN
												UPDATE ame_ar_so T1 SET T1.var_status = 'Closed', T1.var_pmtstatus = 'Fully Received' WHERE T1.var_id = P2; 
										ELSE
												UPDATE ame_ar_so T1 SET T1.var_status = 'Open', T1.var_pmtstatus = 'Not Received' WHERE T1.var_id = P2;
										END IF;		
								ELSE
										UPDATE ame_ar_so T1 SET T1.var_status = 'Open', T1.var_pmtstatus = 'Not Received' WHERE T1.var_id = P2;
								END IF;
						CLOSE C2;
				END;
		WHEN "AR-IN" THEN
				# UPDATE PAID
				BEGIN
						DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
						DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
						DECLARE P_RATE DOUBLE DEFAULT 0.0;
						DECLARE C2 CURSOR FOR SELECT T1.dou_rate, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT), AME_FUN_ROUND(COALESCE((SELECT SUM(T1.dou_amt + T1.dou_app + T1.dou_disc) FROM ame_ar_py_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P2 AND T2.var_status = "Posted"), 0), P_DEC_AMT) FROM ame_ar_in T1 WHERE T1.var_id = P2;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN
										FETCH C2 INTO P_RATE, P_DOC_NET, P_DOC_PAID;
										IF P_DOC_NET = P_DOC_PAID THEN
												UPDATE ame_ar_in SET var_pmtstatus = "Fully Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_RATE, P_DEC_AMT_FUN) WHERE var_id = P2;
										ELSEIF P_DOC_NET > P_DOC_PAID AND P_DOC_PAID > 0 THEN
												UPDATE ame_ar_in SET var_pmtstatus = "Partially Paid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_RATE, P_DEC_AMT_FUN) WHERE var_id = P2;
										ELSE
												UPDATE ame_ar_in SET var_pmtstatus = "Unpaid", dou_paid = P_DOC_PAID, dou_funpaid = AME_FUN_ROUND(P_DOC_PAID * P_RATE, P_DEC_AMT_FUN) WHERE var_id = P2;
										END IF;
								END IF;
						CLOSE C2;
				END;			
		WHEN "AR-RET" THEN
				# UPDATE RETURN
				UPDATE ame_ar_in_det T1 SET T1.dou_orderqty = 0, T1.dou_orderfoc = 0 WHERE T1.var_id = P2;	
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_LIN INTEGER DEFAULT 0;
						DECLARE P_DOC VARCHAR(100) DEFAULT "";
						DECLARE P_LOC VARCHAR(100) DEFAULT "";
						DECLARE P_ITE VARCHAR(100) DEFAULT "";
						DECLARE P_UOM VARCHAR(100) DEFAULT "";
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_FOC DOUBLE DEFAULT 0.0;
						DEClARE C1 CURSOR FOR SELECT T1.var_poid, T1.int_poline, T1.var_locid, T1.var_itemid, T1.var_uomid, AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0), P_DEC_QTY), AME_FUN_ROUND(COALESCE(SUM(T1.dou_foc), 0), P_DEC_QTY) FROM ame_ar_ret_det T1 LEFT JOIN ame_ar_ret T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T2.var_status IN('Open', 'Posted') GROUP BY T1.var_locid, T1.var_itemid, T1.var_uomid, T1.int_poline;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;		
								L: LOOP FETCH C1 INTO P_DOC, P_LIN, P_LOC, P_ITE, P_UOM, P_QTY, P_FOC;
										IF D1 = 1 THEN LEAVE L; END IF;
										UPDATE ame_ar_in_det T1 SET T1.dou_orderqty = P_QTY, T1.dou_orderfoc = P_FOC WHERE T1.var_id = P_DOC AND T1.int_poline = P_LIN AND T1.var_locid = P_LOC AND T1.var_itemid = P_ITE AND T1.var_uomid = P_UOM;
								END LOOP L;
						CLOSE C1;			
				END;
				BEGIN
						DECLARE D2 INTEGER DEFAULT 0;
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_QTY_ORD DOUBLE DEFAULT 0.0;
						DECLARE P_STA_CLEAR_UN VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_PARTIAL VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_FULL VARCHAR(100) DEFAULT "";
						DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(T1.dou_orderqty + T1.dou_orderfoc, P_DEC_QTY), AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P_DEC_QTY) FROM ame_ar_in_det T1 WHERE T1.var_id = P2;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN
										L2 : LOOP FETCH C2 INTO P_QTY_ORD, P_QTY;
												IF D2 = 1 THEN LEAVE L2; END IF;
												IF P_QTY_ORD = 0 AND P_QTY > 0 THEN
														SET P_STA_CLEAR_UN = "Not";
												ELSEIF P_QTY_ORD < P_QTY THEN
														SET P_STA_CLEAR_PARTIAL = 'Partial'; LEAVE L2;
												ELSEIF P_QTY_ORD >= P_QTY THEN
														SET P_STA_CLEAR_FULL = 'Full';
												END IF;
										END LOOP L2;	
										IF P_STA_CLEAR_PARTIAL <> "" OR (P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN <> "") THEN 
												UPDATE ame_ar_in T1 SET T1.var_retstatus = 'Open', T1.var_retpmtstatus = 'Partially Returned' WHERE T1.var_id = P2; 
										ELSEIF P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN = "" THEN
												UPDATE ame_ar_in T1 SET T1.var_retstatus = 'Closed', T1.var_retpmtstatus = 'Fully Returned' WHERE T1.var_id = P2; 
										ELSE
												UPDATE ame_ar_in T1 SET T1.var_retstatus = 'Open', T1.var_retpmtstatus = 'Not Returned' WHERE T1.var_id = P2;
										END IF;		
								ELSE
										UPDATE ame_ar_in T1 SET T1.var_status = 'Open', T1.var_retpmtstatus = 'Not Returned' WHERE T1.var_id = P2;
								END IF;
						CLOSE C2;
				END;
		WHEN "AR-SM-IN" THEN
				# UPDATE RETURN
				UPDATE ame_ar_in_det T1 SET T1.dou_shipqty = 0, T1.dou_shipfoc = 0 WHERE T1.var_id = P2;	
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_LIN INTEGER DEFAULT 0;
						DECLARE P_DOC VARCHAR(100) DEFAULT "";
						DECLARE P_LOC VARCHAR(100) DEFAULT "";
						DECLARE P_ITE VARCHAR(100) DEFAULT "";
						DECLARE P_UOM VARCHAR(100) DEFAULT "";
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_FOC DOUBLE DEFAULT 0.0;
						DEClARE C1 CURSOR FOR SELECT T1.var_poid, T1.int_poline, T1.var_locid, T1.var_itemid, T1.var_uomid, AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0), P_DEC_QTY), AME_FUN_ROUND(COALESCE(SUM(T1.dou_foc), 0), P_DEC_QTY) FROM ame_ar_sm_det T1 LEFT JOIN ame_ar_sm T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T2.var_status = 'Open' AND T2.var_type = 'AR-IN' GROUP BY T1.var_locid, T1.var_itemid, T1.var_uomid, T1.int_poline;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;		
								L: LOOP FETCH C1 INTO P_DOC, P_LIN, P_LOC, P_ITE, P_UOM, P_QTY, P_FOC;
										IF D1 = 1 THEN LEAVE L; END IF;
										UPDATE ame_ar_in_det T1 SET T1.dou_shipqty = P_QTY, T1.dou_shipfoc = P_FOC WHERE T1.var_id = P_DOC AND T1.int_poline = P_LIN AND T1.var_locid = P_LOC AND T1.var_itemid = P_ITE AND T1.var_uomid = P_UOM;
								END LOOP L;
						CLOSE C1;			
				END;
				BEGIN
						DECLARE D2 INTEGER DEFAULT 0;
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_QTY_ORD DOUBLE DEFAULT 0.0;
						DECLARE P_STA_CLEAR_UN VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_PARTIAL VARCHAR(100) DEFAULT "";
						DECLARE P_STA_CLEAR_FULL VARCHAR(100) DEFAULT "";
						DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(T1.dou_shipqty + T1.dou_shipfoc, P_DEC_QTY), AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P_DEC_QTY) FROM ame_ar_in_det T1 WHERE T1.var_id = P2;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN
										L2 : LOOP FETCH C2 INTO P_QTY_ORD, P_QTY;
												IF D2 = 1 THEN LEAVE L2; END IF;
												IF P_QTY_ORD = 0 AND P_QTY > 0 THEN
														SET P_STA_CLEAR_UN = "Not";
												ELSEIF P_QTY_ORD < P_QTY THEN
														SET P_STA_CLEAR_PARTIAL = 'Partial'; LEAVE L2;
												ELSEIF P_QTY_ORD >= P_QTY THEN
														SET P_STA_CLEAR_FULL = 'Full';
												END IF;
										END LOOP L2;	
										IF P_STA_CLEAR_PARTIAL <> "" OR (P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN <> "") THEN 
												UPDATE ame_ar_in T1 SET T1.var_shipstatus = 'Open', T1.var_shippmtstatus = 'Partially Shipped' WHERE T1.var_id = P2; 
										ELSEIF P_STA_CLEAR_FULL <> "" AND P_STA_CLEAR_UN = "" THEN
												UPDATE ame_ar_in T1 SET T1.var_shipstatus = 'Closed', T1.var_shippmtstatus = 'Fully Shipped' WHERE T1.var_id = P2; 
										ELSE
												UPDATE ame_ar_in T1 SET T1.var_shipstatus = 'Open', T1.var_shippmtstatus = 'Unshipped' WHERE T1.var_id = P2;
										END IF;		
								ELSE
										UPDATE ame_ar_in T1 SET T1.var_shipstatus = 'Open', T1.var_shippmtstatus = 'Unshipped' WHERE T1.var_id = P2;
								END IF;
						CLOSE C2;
				END;
		WHEN "AR-SM-RET" THEN
				# UPDATE RETURN
				UPDATE ame_ar_ret_det T1 SET T1.dou_shipqty = 0, T1.dou_shipfoc = 0 WHERE T1.var_id = P2;	
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_LIN INTEGER DEFAULT 0;
						DECLARE P_DOC VARCHAR(100) DEFAULT "";
						DECLARE P_LOC VARCHAR(100) DEFAULT "";
						DECLARE P_ITE VARCHAR(100) DEFAULT "";
						DECLARE P_UOM VARCHAR(100) DEFAULT "";
						DECLARE P_QTY DOUBLE DEFAULT 0.0;
						DECLARE P_FOC DOUBLE DEFAULT 0.0;
						DEClARE C1 CURSOR FOR SELECT T1.var_poid, T1.int_poline, T1.var_locid, T1.var_itemid, T1.var_uomid, AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0), P_DEC_QTY), AME_FUN_ROUND(COALESCE(SUM(T1.dou_foc), 0), P_DEC_QTY) FROM ame_ar_sm_det T1 LEFT JOIN ame_ar_sm T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T2.var_status = 'Open' AND T2.var_type = 'AR-RET' GROUP BY T1.var_locid, T1.var_itemid, T1.var_uomid, T1.int_poline;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;		
								L: LOOP FETCH C1 INTO P_DOC, P_LIN, P_LOC, P_ITE, P_UOM, P_QTY, P_FOC;
										IF D1 = 1 THEN LEAVE L; END IF;
