										BEGIN
												DECLARE D5 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 C5 CURSOR FOR SELECT T1.var_doctype, T1.var_docid, T3.var_name, T1.var_claid, T1.var_desc, AME_FUN_ROUND(T1.dou_app, P_DEC_AMT_FOR) 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 = P1 ORDER BY T1.int_line;
												DECLARE CONTINUE HANDLER FOR NOT FOUND SET D5 = 1;
												OPEN C5;
														L5: LOOP FETCH C5 INTO P_APP_TYPE, P_APP_ACC, P_APP_ACC_NAME, P_APP_CLA, P_APP_DESC, P_APP_AMT;
																IF D5 = 1 THEN LEAVE L5; 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_FOR);
																		IF P_APP_AMT > 0 THEN
																				SET P_LINE_JE = P_LINE_JE + 1;
																				CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_APP_ACC, P_APP_CLA, "AR-PY", P_APP_TYPE, P1, P_REF, "", P_VEN, "", "", P_APP_DESC, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, P_APP_AMT, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "GL", P_CUR, P_RATE, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT_FUN));
																		ELSEIF P_APP_AMT < 0 THEN
																				SET P_LINE_JE = P_LINE_JE + 1;
																				CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_APP_ACC, P_APP_CLA, "AR-PY", P_APP_TYPE, P1, P_REF, "", P_VEN, "", "", P_APP_DESC, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, P_APP_AMT, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "GL", P_CUR, P_RATE, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT_FUN));
																		END IF;
																END CASE;
														END LOOP L5;
												CLOSE C5;
										END;
										*/
										IF P_AMT > 0 THEN
												SET P_LINE_JE = P_LINE_JE + 1;
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_DOC, P_CLA_DET, "AR-PY", P_TYPE, P_DOC, P_REF, "", P_VEN, "", "", P_DESC_DET, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, P_AMT, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "GL", P_CUR, P_RATE, AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT_FUN));
										ELSEIF P_AMT < 0 THEN
												SET P_LINE_JE = P_LINE_JE + 1;
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_DOC, P_CLA_DET, "AR-PY", P_TYPE, P_DOC, P_REF, "", P_VEN, "", "", P_DESC_DET, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, P_AMT, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "GL", P_CUR, P_RATE, AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT_FUN));
										END IF;
								ELSE
										IF P_AMT > 0 THEN
												SET P_LINE_JE = P_LINE_JE + 1;
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_DOC, P_CLA_DET, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -P_AMT, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "GL", P_CUR, P_RATE, -AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT_FUN));
										ELSEIF P_AMT < 0 THEN
												SET P_LINE_JE = P_LINE_JE + 1;
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_DOC, P_CLA_DET, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -P_AMT, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "GL", P_CUR, P_RATE, -AME_FUN_ROUND(P_AMT * P_RATE, P_DEC_AMT_FUN));
										END IF;
								END CASE;
						END LOOP L1;
				CLOSE C1;
				SET @P_TOTAL_CASH = AME_FUN_ROUND(P_TCASH - P_TAPP, P_DEC_AMT_FOR);
				IF P_TYPE = "Prepayment" THEN
						IF @P_TOTAL_CASH <> 0 THEN
								SET P_LINE_JE = P_LINE_JE + 1;
								IF @P_TOTAL_CASH > 0 THEN
										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_CASH, P_CLA, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -AME_FUN_ROUND(@P_TOTAL_CASH, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "CASH", P_CUR, P_RATE, -AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT_FUN));
								ELSE
										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_CASH, P_CLA, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -AME_FUN_ROUND(@P_TOTAL_CASH, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "CASH", P_CUR, P_RATE, -AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT_FUN));
								END IF;
						END IF;
				ELSE
						IF @P_TOTAL_CASH <> 0 THEN
								SET P_LINE_JE = P_LINE_JE + 1;
								IF @P_TOTAL_CASH > 0 THEN
										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_CASH, P_CLA, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, AME_FUN_ROUND(@P_TOTAL_CASH, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "CASH", P_CUR, P_RATE, AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT_FUN));
								ELSE
										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_CASH, P_CLA, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, AME_FUN_ROUND(@P_TOTAL_CASH, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "CASH", P_CUR, P_RATE, AME_FUN_ROUND(@P_TOTAL_CASH * P_RATE, P_DEC_AMT_FUN));
								END IF;
						END IF;
				END IF;
				
				SET @P_DIFF = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF(P_JID), P_DEC_AMT_FOR);
				IF @P_DIFF > 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_AP, P_CLA, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
				ELSEIF @P_DIFF < 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_AP, P_CLA, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
				END IF;
				CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, P_ACC_CASH, "AR-PY", "CASH", @P_TOTAL_CASH, @P_TOTAL_CASH * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
				UPDATE ame_ar_py SET var_status = 'Posted', int_jid = P_JID, var_post = P2, dat_post = NOW() WHERE var_id = P1;
		END;
		IF P_TYPE = "Prepayment" THEN
				BEGIN
						DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
						DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
						DECLARE C2 CURSOR FOR SELECT 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 = "AR-PY" AND T1.var_docid = P1 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 = P1 AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ar_py T1 WHERE T1.var_id = P1;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN FETCH C2 INTO 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_RATE, P_DEC_AMT_FUN) WHERE var_id = P1;
										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_RATE, P_DEC_AMT_FUN) WHERE var_id = P1;
										ELSE
												UPDATE ame_ar_py 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 = P1;
										END IF;
								END IF;
						CLOSE C2;
				END;
		END IF;
		# AUTO POST GL
		IF AME_FUN_POST_ONE_TIME() = 1 THEN IF AME_FUN_VALID_GL_JE(P1) = 0 THEN CALL AME_PRO_POST_GL_JE_1(P_JID, P2); END IF; END IF;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_AR_PY_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AR_PY_2`(P1 varchar(100), P2 varchar(100), P3 tinyint(1))
    DETERMINISTIC
BEGIN
		DECLARE P_JID INTEGER DEFAULT -1;
		DECLARE P_JID_POST INTEGER DEFAULT -1;
		DECLARE P_NUM INTEGER DEFAULT -1;
		DECLARE P_DATE DATETIME;
		DECLARE P_VEN VARCHAR(100) DEFAULT "";
		DECLARE P_DOC_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_DR_AMT DOUBLE DEFAULT 0.0;
		DECLARE P_CR_AMT DOUBLE DEFAULT 0.0;
		DECLARE P_DR_AMT_FUN DOUBLE DEFAULT 0.0;
		DECLARE P_CR_AMT_FUN DOUBLE DEFAULT 0.0;
		DECLARE P_DEC_AMT_FOR INTEGER DEFAULT 0;
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 0;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.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 C2 CURSOR FOR SELECT T1.var_doctype, COALESCE(T1.var_cur, ''), T1.dou_fundr, T1.dou_funcr, T1.int_line, T1.dat_date, T1.var_cusid, T1.dou_dr, T1.dou_cr, T1.int_jid FROM ame_sl_ar_his T1 WHERE T1.var_modid = 'AR-PY' AND T1.var_docid = P1;
				OPEN C2;
						IF FOUND_ROWS() > 0 THEN
								FETCH C2 INTO P_DOC_TYPE, P_CUR, P_DR_AMT_FUN, P_CR_AMT_FUN, P_NUM, P_DATE, P_VEN, P_DR_AMT, P_CR_AMT, P_JID;
								IF P_DOC_TYPE <> "Apply to GL Account" THEN
										# DECIMAL FOREIGN CURRENCY
										BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
										CALL AME_PRO_UPDATE_SL_AR(P_VEN, "Cr", P_DATE, -P_CR_AMT, P_DEC_AMT_FOR, P_CUR, -P_CR_AMT_FUN, P_DEC_AMT_FUN);
										CALL AME_PRO_UPDATE_SL_AR(P_VEN, "Dr", P_DATE, -P_DR_AMT, P_DEC_AMT_FOR, P_CUR, -P_DR_AMT_FUN, P_DEC_AMT_FUN);
								END IF;
								DELETE FROM ame_sl_ar_his WHERE var_modid = 'AR-PY' AND var_docid = P1;
						END IF;
				CLOSE C2;
		END;
		# DELETE OLD POST GL
		SELECT COUNT(T1.int_jid) INTO P_JID_POST FROM ame_gl_je T1 WHERE T1.int_jid = P_JID AND T1.var_status = 'Posted';
		IF P_JID_POST > 0 THEN CALL AME_PRO_POST_GL_JE_2(P_JID, P2, 0);END IF;
		DELETE FROM ame_gl_je WHERE int_jid = P_JID;DELETE FROM ame_gl_je_det WHERE int_jid = P_JID;
		IF P3 = 1 THEN
				CALL AME_PRO_POST_AR_PY_1(P1, P2, P_JID, P_NUM);
		ELSE
				UPDATE ame_ar_py SET var_status = 'Deleted', int_jid = NULL, var_modify = P2, dat_modify = NOW() WHERE var_id = P1;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_AR_RET_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AR_RET_1`(P1 varchar(100), P2 varchar(100), P3 int(11), P4 int(11), P5 int(11), P6 int(11), P7 int(11))
    DETERMINISTIC
BEGIN
		DECLARE P_BAT_NUM_NEW INTEGER DEFAULT 0;
		DECLARE P_COUNT_PAID INTEGER DEFAULT 0;
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_LINE_JE INTEGER DEFAULT 0;
		DECLARE P_NUM_IC INTEGER DEFAULT 0;
		DECLARE P_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_CUS VARCHAR(100) DEFAULT "";
		DECLARE P_EMP VARCHAR(100) DEFAULT "";
		DECLARE P_CLA VARCHAR(100) DEFAULT "";
		DECLARE P_REF VARCHAR(100) DEFAULT "";
		DECLARE P_DESC VARCHAR(2000) DEFAULT "";
		DECLARE P_ACC_NUM VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_AR VARCHAR(100) DEFAULT "";
		DECLARE P_TERM VARCHAR(100) DEFAULT "";
		DECLARE P_PRICE_CODE VARCHAR(100) DEFAULT "";
		DECLARE P_SHIP_ADDR VARCHAR(2000) DEFAULT "";
		DECLARE P_BAL DOUBLE DEFAULT 0.0;
		DECLARE P_DISC DOUBLE DEFAULT 0.0;
		DECLARE P_VAT DOUBLE DEFAULT 0.0;
		DECLARE P_WHT DOUBLE DEFAULT 0.0;
		DECLARE P_DATE DATETIME;
		DECLARE P_DATE_DUE DATETIME;

		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_RATE DOUBLE DEFAULT 1.0;
		DECLARE P_FUN_BAL DOUBLE DEFAULT 0.0;
		DECLARE P_FUN_VAT DOUBLE DEFAULT 0.0;
		DECLARE P_FUN_WHT DOUBLE DEFAULT 0.0;
		DECLARE P_FUN_DISC DOUBLE DEFAULT 0.0;

		DECLARE P_DEC_QTY INTEGER DEFAULT 0;
		DECLARE P_DEC_COST_FOR INTEGER DEFAULT 6;
		DECLARE P_DEC_PRICE_FOR INTEGER DEFAULT 6;
		DECLARE P_DEC_AMT_FOR INTEGER DEFAULT 2;
		DECLARE P_DEC_COST_FUN INTEGER DEFAULT 6;
		DECLARE P_DEC_PRICE_FUN INTEGER DEFAULT 6;
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.int_qty, T.int_cost, T.int_price, T.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_QTY, P_DEC_COST_FUN, P_DEC_PRICE_FUN, P_DEC_AMT_FUN; END IF;CLOSE C1;END;
		
		IF P7 = -1 THEN SET P_BAT_NUM_NEW = AME_FUN_NEW_BATCH_NUM(); ELSE SET P_BAT_NUM_NEW = P7; END IF;
		IF P6 = -1 THEN SET P_JID = AME_FUN_NEW_JE_NUM(); ELSE SET P_JID = P6; END IF;
		IF P5 = -1 THEN SET P_NUM_IC = AME_FUN_NEW_SL_IC_HIS_NUM(); ELSE SET P_NUM_IC = P5; END IF;
		BEGIN
				SELECT T1.dat_date, T1.dat_due, T1.var_term,T1.var_type, T1.var_ref, T1.var_claid, T1.var_cusid, T1.var_empid, T1.var_desc, T1.var_pricecode, T1.var_shipaddr, T1.dou_disdol, T1.dou_taxdol, T1.dou_taxdol1, T1.dou_net, T1.var_cur, T1.dou_rate, T1.dou_funnet, T1.dou_fundisdol, T1.dou_funtaxdol, T1.dou_funtaxdol1
				INTO P_DATE, P_DATE_DUE, P_TERM, P_TYPE, P_REF, P_CLA, P_CUS, P_EMP, P_DESC, P_PRICE_CODE, P_SHIP_ADDR, P_DISC, P_VAT, P_WHT, P_BAL, P_CUR, P_RATE, P_FUN_BAL, P_FUN_DISC, P_FUN_VAT, P_FUN_WHT FROM ame_ar_ret T1 WHERE T1.var_id = P1;
				# DECIMAL FOREIGN CURRENCY
				BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
				# AR
				IF P_BAL <> 0 THEN
						SET P_ACC_AR = AME_FUN_ACC("AR", P_CUS, "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_AR, P_CLA, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, "", IF(COALESCE(P_DESC, "") = "", "Receivable", CONCAT("[Receivable] ", P_DESC)), "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, -P_BAL, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AR", P_CUR, P_RATE, -P_FUN_BAL);
				END IF;
				IF P_DISC <> 0 THEN
						SET P_ACC_NUM = AME_FUN_ACC("DISC-AR-IN", "DEFAULT", "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, "", "Discount", "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, -P_DISC, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "DISC", P_CUR, P_RATE, -P_FUN_DISC);
				END IF;
				# SCT
				IF P_WHT <> 0 THEN
						SET P_ACC_NUM = AME_FUN_ACC("TAX-SCT", "DEFAULT", "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, "", "SCT", "Dr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, P_WHT, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "SCT", P_CUR, P_RATE, P_FUN_WHT);
				END IF;
				# VAT
				IF P_VAT <> 0 THEN
						SET P_ACC_NUM = AME_FUN_ACC("TAX-VAT-OUT", "DEFAULT", "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, "", "VAT", "Dr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, P_VAT, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "VAT", P_CUR, P_RATE, P_FUN_VAT);
				END IF;
				# SUB LEDGER AR
				CALL AME_PRO_UPDATE_SL_AR(P_CUS, "Cr", P_DATE, P_BAL, P_DEC_AMT_FOR, P_CUR, P_FUN_BAL, P_DEC_AMT_FUN);
				CALL AME_PRO_UPDATE_SL_AR_HIS("AR-RET", P_TYPE, P1, P_CUS, P_EMP, P_CLA, "", P_TERM, "", "", P_REF, P_DESC, P_DATE, P_DATE_DUE, P_BAL, P_DEC_AMT_FOR, 0, "Cr", P4, P_JID, P_ACC_AR, P_CUR, P_RATE, P_FUN_BAL);
		END;
		BEGIN
				DECLARE D2 INTEGER DEFAULT 0;
				DECLARE P_COUNT_PAID INTEGER DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DECLARE P_LINE INTEGER DEFAULT 0;
				DECLARE P_LINE_FRM INTEGER DEFAULT 0;
				DECLARE P_ACC_IC VARCHAR(100) DEFAULT "";
				DECLARE P_ACC_REV VARCHAR(100) DEFAULT "";
				DECLARE P_ACC_COGS VARCHAR(100) DEFAULT "";
				DECLARE P_ACC_DISC VARCHAR(100) DEFAULT "";
				DECLARE P_CLA_DET VARCHAR(100) DEFAULT "";
				DECLARE P_DESC_DET VARCHAR(2000) DEFAULT "";
				DECLARE P_LOC VARCHAR(100) DEFAULT "";
				DECLARE P_ITEM VARCHAR(100) DEFAULT "";
				DECLARE P_UOM VARCHAR(100) DEFAULT "";
				DECLARE P_QTY DOUBLE DEFAULT 0.0;
				DECLARE P_PRICE DOUBLE DEFAULT 0.0;
				DECLARE P_REVENUE DOUBLE DEFAULT 0.0;
				DECLARE P_FOC DOUBLE DEFAULT 0.0;
				DEClARE C2 CURSOR FOR SELECT T1.int_line, T1.var_itemid, T1.var_accid, T1.var_locid, T1.var_claid, T1.var_uomid, IF(COALESCE(T1.var_desc, '') = '', T1.var_itemid, CONCAT('[', T1.var_itemid,'] ', T1.var_desc)), AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P_DEC_QTY), T1.dou_price, T1.dou_foc FROM ame_ar_ret_det AS T1 WHERE var_id = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
				SET @T_FOC = 0;
				OPEN C2;
						L2: LOOP FETCH C2 INTO P_LINE_FRM, P_ITEM, P_ACC_REV, P_LOC, P_CLA_DET, P_UOM, P_DESC_DET, P_QTY, P_PRICE, P_FOC;
								IF D2 = 1 THEN LEAVE L2; END IF;
								# REVENUE
								SET P_REVENUE = AME_FUN_ROUND(P_QTY * P_PRICE, P_DEC_AMT_FOR);
								IF P_REVENUE <> 0 THEN
										IF P_TYPE = "General Item Sale" THEN
												SET P_ACC_NUM = AME_FUN_ACC("REVENUE", P_ITEM, "NA");
										ELSE
												SET P_ACC_NUM = P_ACC_REV;
										END IF;
										SET P_LINE_JE = P_LINE_JE + 1;
										IF P_REVENUE >= 0 THEN
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA_DET, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, IF(COALESCE(P_DESC_DET, "") = "", "Revenue", CONCAT("[Revenue] ", P_DESC_DET)), "Dr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, P_REVENUE, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "REVENUE", P_CUR, P_RATE, AME_FUN_ROUND(P_REVENUE * P_RATE, P_DEC_AMT_FUN));
										ELSE
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA_DET, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, IF(COALESCE(P_DESC_DET, "") = "", "Revenue", CONCAT("[Revenue] ", P_DESC_DET)), "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, P_REVENUE, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "REVENUE", P_CUR, P_RATE, AME_FUN_ROUND(P_REVENUE * P_RATE, P_DEC_AMT_FUN));
										END IF;		
								END IF;
								SET @CON = AME_FUN_ITEM_CON(P_ITEM, P_UOM);
								SET @T_FOC = AME_FUN_ROUND(@T_FOC + AME_FUN_ROUND(P_FOC * P_PRICE, P_DEC_AMT_FOR), P_DEC_AMT_FOR);
								CASE AME_FUN_ITEM_TYPE(P_ITEM)
								WHEN "Inventory Part" THEN
										SET @P_BATCH = AME_FUN_BATCH_EXIST(P_LOC, P_ITEM);
										IF @P_BATCH = 0 THEN
												SET P_LINE = P_LINE + 1;
												CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_QTY * @CON, 0, 0, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
												CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_DATE, P_QTY * @CON, 0, 0, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
												CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_DATE, P_QTY * @CON, 0, 0, 0, 0, 0, 0, P_NUM_IC, P_LINE, "AR-RET", "Sales Increase Stock", P1, "", P_CUS, P_EMP, P_CLA_DET, P_REF, P_DESC, P_DESC_DET, P_CUR, P_RATE, AME_FUN_ROUND(P_FOC * @CON, P_DEC_QTY), P_JID);
										ELSE
												BEGIN
														DECLARE D3 INTEGER DEFAULT 0.0;
														DECLARE P_QTY_PUR DOUBLE DEFAULT 0.0;
														DECLARE P_QTY_BAL DOUBLE DEFAULT 0.0;
														DECLARE P_QTY_SOLD DOUBLE DEFAULT 0.0;
														DECLARE P_QTY_BATCH DOUBLE DEFAULT 0.0;
														DECLARE P_COST DOUBLE DEFAULT 0.0;
														DECLARE P_AC1 DOUBLE DEFAULT 0.0;
														DECLARE P_AC2 DOUBLE DEFAULT 0.0;
														DECLARE P_AC3 DOUBLE DEFAULT 0.0;
														DECLARE P_AC4 DOUBLE DEFAULT 0.0;
														DECLARE P_AC5 DOUBLE DEFAULT 0.0;
														DECLARE P_TCOST DOUBLE DEFAULT 0.0;
														DECLARE P_TAC1 DOUBLE DEFAULT 0.0;
														DECLARE P_TAC2 DOUBLE DEFAULT 0.0;
														DECLARE P_TAC3 DOUBLE DEFAULT 0.0;
														DECLARE P_TAC4 DOUBLE DEFAULT 0.0;
														DECLARE P_TAC5 DOUBLE DEFAULT 0.0;
														DEClARE C3 CURSOR FOR SELECT T.int_batchno, T.dou_drqty, T.dou_qty, 
														AME_FUN_ROUND(T.dou_druc - IF(T.dou_qty = 0, 0, T.dou_cruc), P_DEC_COST_FUN), AME_FUN_ROUND(T.dou_drac1 - IF(T.dou_qty = 0, 0, T.dou_crac1), P_DEC_COST_FUN), AME_FUN_ROUND(T.dou_drac2 - IF(T.dou_qty = 0, 0, T.dou_crac2), P_DEC_COST_FUN), AME_FUN_ROUND(T.dou_drac3 - IF(T.dou_qty = 0, 0, T.dou_crac3), P_DEC_COST_FUN), AME_FUN_ROUND(T.dou_drac4 - IF(T.dou_qty = 0, 0, T.dou_crac4), P_DEC_COST_FUN), AME_FUN_ROUND(T.dou_drac5 - IF(T.dou_qty = 0, 0, T.dou_crac5), P_DEC_COST_FUN)
														FROM ame_sl_ic_batch AS T WHERE T.int_batchno = @P_BATCH AND T.var_itemid = P_ITEM AND T.var_locid = P_LOC ORDER BY T.`int_batchno`;
														DECLARE CONTINUE HANDLER FOR NOT FOUND SET D3 = 1;
														SET P_QTY_BATCH = AME_FUN_ROUND(P_QTY * @CON, P_DEC_QTY);
														OPEN C3;
																L3:LOOP FETCH C3 INTO P_BAT_NUM, P_QTY_PUR, P_QTY_BAL, P_COST, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
																		IF D3 = 1 THEN LEAVE L3; END IF;
																		SET P_QTY_SOLD = P_QTY_BATCH;
																		SET P_QTY_BATCH = 0;
																		# COSTING
																		SET @UC = AME_FUN_ROUND(P_QTY_SOLD * (P_COST / IF(P_QTY_BAL = 0, P_QTY_PUR, P_QTY_BAL)), P_DEC_COST_FUN);
																		SET @AC1 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC1 / IF(P_QTY_BAL = 0, P_QTY_PUR, P_QTY_BAL)), P_DEC_COST_FUN);
																		SET @AC2 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC2 / IF(P_QTY_BAL = 0, P_QTY_PUR, P_QTY_BAL)), P_DEC_COST_FUN);
																		SET @AC3 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC3 / IF(P_QTY_BAL = 0, P_QTY_PUR, P_QTY_BAL)), P_DEC_COST_FUN);
																		SET @AC4 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC4 / IF(P_QTY_BAL = 0, P_QTY_PUR, P_QTY_BAL)), P_DEC_COST_FUN);
																		SET @AC5 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC5 / IF(P_QTY_BAL = 0, P_QTY_PUR, P_QTY_BAL)), P_DEC_COST_FUN);
																		
																		SET P_TCOST = AME_FUN_ROUND(P_TCOST + @UC, P_DEC_COST_FUN);
																		SET P_TAC1 = AME_FUN_ROUND(P_TAC1 + @AC1, P_DEC_COST_FUN);
																		SET P_TAC2 = AME_FUN_ROUND(P_TAC2 + @AC2, P_DEC_COST_FUN);
																		SET P_TAC3 = AME_FUN_ROUND(P_TAC3 + @AC3, P_DEC_COST_FUN);
																		SET P_TAC4 = AME_FUN_ROUND(P_TAC4 + @AC4, P_DEC_COST_FUN);
																		SET P_TAC5 = AME_FUN_ROUND(P_TAC5 + @AC5, P_DEC_COST_FUN);

																		SET P_LINE = P_LINE + 1;
																		CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
																		CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_DATE, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
																		CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_DATE, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, P_NUM_IC, P_LINE, "AR-RET", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), P_CUS, P_EMP, P_CLA_DET, P_REF, P_DESC, P_DESC_DET, P_CUR, P_RATE, AME_FUN_ROUND(P_FOC * @CON, P_DEC_QTY), P_JID);
																		IF P_QTY_BATCH = 0 THEN LEAVE L3; END IF;
																END LOOP L3;
														CLOSE C3;
														SET P_TCOST = AME_FUN_ROUND(P_TCOST, P_DEC_AMT_FUN);
														SET P_TAC1 = AME_FUN_ROUND(P_TAC1, P_DEC_AMT_FUN);
														SET P_TAC2 = AME_FUN_ROUND(P_TAC2, P_DEC_AMT_FUN);
														SET P_TAC3 = AME_FUN_ROUND(P_TAC3, P_DEC_AMT_FUN);
														SET P_TAC4 = AME_FUN_ROUND(P_TAC4, P_DEC_AMT_FUN);
														SET P_TAC5 = AME_FUN_ROUND(P_TAC5, P_DEC_AMT_FUN);
														# ASSET
														SET @TC = AME_FUN_ROUND(P_TCOST + P_TAC1 + P_TAC2 + P_TAC3 + P_TAC4 + P_TAC5, P_DEC_AMT_FUN);
														SET @FOR_UC = AME_FUN_ROUND(P_TCOST / P_RATE, P_DEC_AMT_FOR);
														SET @FOR_AC1 = AME_FUN_ROUND(P_TAC1 / P_RATE, P_DEC_AMT_FOR);
														SET @FOR_AC2 = AME_FUN_ROUND(P_TAC2 / P_RATE, P_DEC_AMT_FOR);
														SET @FOR_AC3 = AME_FUN_ROUND(P_TAC3 / P_RATE, P_DEC_AMT_FOR);
														SET @FOR_AC4 = AME_FUN_ROUND(P_TAC4 / P_RATE, P_DEC_AMT_FOR);
														SET @FOR_AC5 = AME_FUN_ROUND(P_TAC5 / P_RATE, P_DEC_AMT_FOR);
														SET @FOR_TUC = AME_FUN_ROUND(@FOR_UC + @FOR_AC1 + @FOR_AC2 + @FOR_AC3 + @FOR_AC4 + @FOR_AC5, P_DEC_AMT_FOR);
														IF @TC <> 0 THEN
																SET P_ACC_NUM = AME_FUN_ACC("IC", P_ITEM, "NA");
																SET P_LINE_JE = P_LINE_JE + 1;
																CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA_DET, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[Inventory] ", P_ITEM), "Dr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, @FOR_TUC, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, @TC);
														END IF;
														# COGS
														IF P_TCOST <> 0 THEN
																SET P_ACC_NUM = AME_FUN_ACC("COGS", P_ITEM, "NA");
																SET P_LINE_JE = P_LINE_JE + 1;
																CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA_DET, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[COGS] ", P_ITEM), "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, -@FOR_UC, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "COGS", P_CUR, P_RATE, -P_TCOST);
														END IF;
														# COGS AC1
														IF P_TAC1 <> 0 THEN
																SET P_ACC_NUM = AME_FUN_ACC("COGS-AC1", P_ITEM, "AC1");
																SET P_LINE_JE = P_LINE_JE + 1;
																CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA_DET, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[", AME_FUN_ADDI_COST("AC1"), "] ", P_ITEM), "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, -@FOR_AC1, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AC1", P_CUR, P_RATE, -P_TAC1);
														END IF;
														# COGS AC2
														IF P_TAC2 <> 0 THEN
																SET P_ACC_NUM = AME_FUN_ACC("COGS-AC2", P_ITEM, "AC2");
																SET P_LINE_JE = P_LINE_JE + 1;
																CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA_DET, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[", AME_FUN_ADDI_COST("AC2"), "] ", P_ITEM), "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, -@FOR_AC2, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AC2", P_CUR, P_RATE, -P_TAC2);
														END IF;
														# COGS AC3
														IF P_TAC3 <> 0 THEN
																SET P_ACC_NUM = AME_FUN_ACC("COGS-AC3", P_ITEM, "AC3");
																SET P_LINE_JE = P_LINE_JE + 1;
																CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA_DET, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[", AME_FUN_ADDI_COST("AC3"), "] ", P_ITEM), "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, -@FOR_AC3, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AC3", P_CUR, P_RATE, -P_TAC3);
														END IF;
														# COGS AC4
														IF P_TAC4 <> 0 THEN
																SET P_ACC_NUM = AME_FUN_ACC("COGS-AC4", P_ITEM, "AC4");
																SET P_LINE_JE = P_LINE_JE + 1;
																CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA_DET, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[", AME_FUN_ADDI_COST("AC4"), "] ", P_ITEM), "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, -@FOR_AC4, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AC4", P_CUR, P_RATE, -P_TAC4);
														END IF;
														# COGS AC5
														IF P_TAC5 <> 0 THEN
																SET P_ACC_NUM = AME_FUN_ACC("COGS-AC5", P_ITEM, "AC5");
																SET P_LINE_JE = P_LINE_JE + 1;
																CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA_DET, "AR-RET", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[", AME_FUN_ADDI_COST("AC5"), "] ", P_ITEM), "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, -@FOR_AC5, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AC5", P_CUR, P_RATE, -P_TAC5);
														END IF;
														UPDATE ame_ar_ret_det T1 SET T1.dou_uc = P_TCOST, T1.dou_ac1 = P_TAC1, T1.dou_ac2 = P_TAC2, T1.dou_ac3 = P_TAC3, T1.dou_ac4 = P_TAC4, T1.dou_ac5 = P_TAC5 WHERE T1.var_id = P1 AND T1.int_line = P_LINE_FRM;
												END;
										END IF;
								WHEN "Inventory Assembly" THEN
										SET @Donothing = NULL;
								ELSE
										SET @Donothing = NULL;
								END CASE;
						END LOOP L2;
				CLOSE C2;
				# ACC. FOC
				IF @T_FOC <> 0 THEN
						SET P_ACC_NUM = AME_FUN_ACC("FOC-AR-IN", "DEFAULT", "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA, "AR-RET", P_TYPE, P1, P_REF, P_EMP, P_CUS, "", "", "FOC", "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, -@T_FOC, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "FOC", P_CUR, P_RATE, -@T_FOC * P_RATE);
				END IF;
				# ACC. DIFFERENCE EXCHANGE RATE
				SET @P_DIFF = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF(P_JID), P_DEC_AMT_FUN);
				SET @P_DIFF_FOR = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF_FOR(P_JID), P_DEC_AMT_FOR);
				IF @P_DIFF > 0 OR @P_DIFF_FOR > 0 THEN
						SET P_ACC_NUM = AME_FUN_ACC("GAINLOSS-ROU", "DEFAULT", "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA, "AR-RET", P_TYPE, P1, P_REF, P_EMP, P_CUS, "", "", "Gain/Loss on Rounding Difference", "Dr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, @P_DIFF_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
				ELSEIF @P_DIFF < 0 OR @P_DIFF_FOR < 0 THEN
						SET P_ACC_NUM = AME_FUN_ACC("GAINLOSS-ROU", "DEFAULT", "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA, "AR-RET", P_TYPE, P1, P_REF, P_EMP, P_CUS, "", "", "Gain/Loss on Rounding Difference", "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, @P_DIFF_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
				END IF;
				CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, P_ACC_AR, "AR-RET", "VAT", P_VAT, P_VAT * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
				UPDATE ame_ar_ret SET var_status = 'Posted', int_jid = P_JID, var_post = P2, dat_post = NOW() WHERE var_id = P1;
		END;
		# RECEIPT
		BEGIN
				DECLARE P_ID VARCHAR(100) DEFAULT "";
				DECLARE C1 CURSOR FOR SELECT T1.var_id 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 = P1 AND COALESCE(T2.tin_paid, 0) = 2 LIMIT 1;
				OPEN C1;
						IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID;
								IF P3 = -1 THEN
										CALL AME_PRO_POST_AR_PY_1(P_ID, P2, -1, -1);
								ELSE
										CALL AME_PRO_POST_AR_PY_2(P_ID, P2, P3);
								END IF;
						ELSE
								BEGIN
										DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
										DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
										DECLARE C2 CURSOR FOR SELECT 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_type = "AR-RET" AND T1.var_docid = P1 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 = P1 AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ar_ret T1 WHERE T1.var_id = P1;
										OPEN C2;
												IF FOUND_ROWS() > 0 THEN FETCH C2 INTO 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_RATE, P_DEC_AMT_FUN) WHERE var_id = P1;
														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_RATE, P_DEC_AMT_FUN) WHERE var_id = P1;
														ELSE
																UPDATE ame_ar_ret 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 = P1;
														END IF;
												END IF;
										CLOSE C2;
								END;
						END IF;
				CLOSE C1;
		END;
		# AUTO POST GL
		IF AME_FUN_POST_ONE_TIME() = 1 THEN IF AME_FUN_VALID_GL_JE(P1) = 0 THEN CALL AME_PRO_POST_GL_JE_1(P_JID, P2); END IF; END IF;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_AR_RET_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AR_RET_2`(P1 varchar(100), P2 varchar(100), P3 tinyint(1))
    DETERMINISTIC
BEGIN
		DECLARE P_BAT_NUM_NEW INTEGER DEFAULT -1;
		DECLARE P_JID INTEGER DEFAULT -1;
		DECLARE P_JID_POST INTEGER DEFAULT -1;
		DECLARE P_NUM_AR INTEGER DEFAULT -1;
		DECLARE P_NUM_IC INTEGER DEFAULT -1;
		DECLARE P_DATE DATETIME;
		DECLARE P_CUS VARCHAR(100) DEFAULT "";

		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_BAL DOUBLE DEFAULT 0.0;
		DECLARE P_BAL_FUN DOUBLE DEFAULT 0.0;
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST_FOR INTEGER DEFAULT 6;
		DECLARE P_DEC_AMT_FOR INTEGER DEFAULT 2;
		DECLARE P_DEC_COST_FUN INTEGER DEFAULT 6;
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.int_qty, T.int_cost, T.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_QTY, P_DEC_COST_FUN, P_DEC_AMT_FUN; END IF;CLOSE C1;END;
		# SUB LEDGER AR
		BEGIN
				DECLARE C2 CURSOR FOR SELECT T1.var_cur, T1.dou_funcr, T1.int_line, T1.dat_date, T1.var_cusid, T1.dou_cr, T1.int_jid FROM ame_sl_ar_his T1 WHERE T1.var_modid = 'AR-RET' AND T1.var_docid = P1;
				OPEN C2;
						IF FOUND_ROWS() > 0 THEN
								FETCH C2 INTO P_CUR, P_BAL_FUN, P_NUM_AR, P_DATE, P_CUS, P_BAL, P_JID;
								 # DECIMAL FOREIGN CURRENCY
								BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_cost, T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_COST_FOR, P_DEC_AMT_FOR; END IF;CLOSE C1;END;
								CALL AME_PRO_UPDATE_SL_AR(P_CUS, "Cr", P_DATE, -P_BAL, P_DEC_AMT_FOR, P_CUR, -P_BAL_FUN, P_DEC_AMT_FUN);
								DELETE FROM ame_sl_ar_his WHERE var_modid = 'AR-RET' AND var_docid = P1;
						END IF;
				CLOSE C2;
		END;
		# SUB LEDGER IC
		BEGIN
				DECLARE D1 INTEGER DEFAULT 0;
				DECLARE P_ITEM VARCHAR(100) DEFAULT '';
				DECLARE P_LOC VARCHAR(100) DEFAULT '';
				DECLARE P_UOM VARCHAR(100) DEFAULT '';
				DECLARE P_QTY DOUBLE DEFAULT 0;
				DECLARE P_UC DOUBLE DEFAULT 0;
				DECLARE P_AC1 DOUBLE DEFAULT 0;
				DECLARE P_AC2 DOUBLE DEFAULT 0;
				DECLARE P_AC3 DOUBLE DEFAULT 0;
				DECLARE P_AC4 DOUBLE DEFAULT 0;
				DECLARE P_AC5 DOUBLE DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DEClARE C1 CURSOR FOR SELECT T1.int_id, T1.var_locid, T1.var_itemid, T1.int_batchno, T1.dou_qty, 
				T1.dou_uc, T1.dou_ac1, T1.dou_ac2, T1.dou_ac3, T1.dou_ac4, T1.dou_ac5
				FROM ame_sl_ic_his AS T1 WHERE T1.var_modid = 'AR-RET' AND T1.var_docid = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				OPEN C1;
						L1: LOOP FETCH C1 INTO  P_NUM_IC, P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
								IF D1 = 1 THEN LEAVE L1; END IF;
								CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
								CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
								SET P_BAT_NUM_NEW = P_BAT_NUM;
						END LOOP L1;
				CLOSE C1;
				DELETE FROM ame_sl_ic_his WHERE var_modid = 'AR-RET' AND var_docid = P1;
		END;
		# DELETE OLD POST GL
		SELECT COUNT(T1.int_jid) INTO P_JID_POST FROM ame_gl_je T1 WHERE T1.int_jid = P_JID AND T1.var_status = 'Posted';
		IF P_JID_POST > 0 THEN CALL AME_PRO_POST_GL_JE_2(P_JID, P2, 0);END IF;
		DELETE FROM ame_gl_je WHERE int_jid = P_JID;DELETE FROM ame_gl_je_det WHERE int_jid = P_JID;
		IF P3 = 1 THEN
				CALL AME_PRO_POST_AR_RET_1(P1, P2, P3, P_NUM_AR, P_NUM_IC, P_JID, P_BAT_NUM_NEW);
		ELSE
				UPDATE ame_ar_ret SET var_status = 'Deleted', int_jid = NULL, var_modify = P2, dat_modify = NOW() WHERE var_id = P1;
				BEGIN
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DECLARE C1 CURSOR FOR SELECT T1.var_id 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 = P1 AND COALESCE(T2.tin_paid, 0) = 2 LIMIT 1;
						OPEN C1;
								IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID;
										IF P3 = 0 THEN CALL AME_PRO_POST_AR_PY_2(P_ID, P2, 0); END IF;
								END IF;
						CLOSE C1;
				END;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_GL_JE_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_GL_JE_1`(P1 int(11), P2 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_DEC_AMT_FOR INTEGER DEFAULT 2;
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.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 C2 CURSOR FOR SELECT T1.var_cur FROM ame_gl_je T1 WHERE T1.int_jid = P1;
				OPEN C2;
						IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_CUR; 
								BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
						END IF;
				CLOSE C2;	
		END;
		BEGIN
				DECLARE D1 INTEGER DEFAULT 0;
				DECLARE P_ACC VARCHAR(100) DEFAULT "";
				DECLARE P_CLA VARCHAR(100) DEFAULT "";
				DECLARE P_DR_CR VARCHAR(2) DEFAULT "";
				DECLARE P_DATE DATE;
				DECLARE P_AMT_FOR DOUBLE DEFAULT 0.0;
				DECLARE P_AMT_FUN DOUBLE DEFAULT 0.0;
				DEClARE C1 CURSOR FOR SELECT DATE(T1.dat_date), T1.var_accid, COALESCE(T1.var_claid, ''), T1.var_drcr, T1.dou_amt, T1.dou_funamt FROM ame_gl_je_det AS T1 WHERE T1.int_jid = P1 ORDER BY T1.`int_line`;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				OPEN C1;
						L: LOOP FETCH C1 INTO P_DATE, P_ACC, P_CLA, P_DR_CR, P_AMT_FOR, P_AMT_FUN;
								IF D1 = 1 THEN LEAVE L; END IF;
								CALL AME_PRO_UPDATE_GL(P_ACC, P_CLA, P_DR_CR, P_DATE, P_AMT_FOR, P_DEC_AMT_FOR, P_CUR, P_AMT_FUN, P_DEC_AMT_FUN);
						END LOOP L;
				CLOSE C1;
				INSERT INTO ame_gl_his (SELECT T1.* FROM ame_gl_je_det T1 WHERE T1.int_jid = P1);
				UPDATE ame_gl_je T1 SET var_status = 'Posted', T1.var_post = P2, T1.dat_post = NOW() WHERE T1.int_jid = P1;
		END;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_GL_JE_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_GL_JE_2`(P1 int(11), P2 varchar(100), P3 tinyint(1))
    DETERMINISTIC
BEGIN
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_DEC_AMT_FOR INTEGER DEFAULT 2;
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.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 C2 CURSOR FOR SELECT T1.var_cur FROM ame_gl_je T1 WHERE T1.int_jid = P1;
				OPEN C2;
						IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_CUR; 
								BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
						END IF;
				CLOSE C2;	
		END;
		BEGIN
				DECLARE D1 INTEGER DEFAULT 0;
				DECLARE P_ACC VARCHAR(100) DEFAULT "";
				DECLARE P_CLA VARCHAR(100) DEFAULT "";
				DECLARE P_DR_CR VARCHAR(2) DEFAULT "";
				DECLARE P_DATE DATE;
				DECLARE P_AMT_FOR DOUBLE DEFAULT 0.0;
				DECLARE P_AMT_FUN DOUBLE DEFAULT 0.0;
				DEClARE C1 CURSOR FOR SELECT DATE(T1.dat_date), T1.var_accid, COALESCE(T1.var_claid, ''), T1.var_drcr, -T1.dou_amt, -T1.dou_funamt FROM ame_gl_his AS T1 WHERE T1.int_jid = P1 ORDER BY T1.`int_line`;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				OPEN C1;
						L: LOOP FETCH C1 INTO P_DATE, P_ACC, P_CLA, P_DR_CR, P_AMT_FOR, P_AMT_FUN;
								IF D1 = 1 THEN LEAVE L; END IF;
								CALL AME_PRO_UPDATE_GL(P_ACC, P_CLA, P_DR_CR, P_DATE, P_AMT_FOR, P_DEC_AMT_FOR, P_CUR, P_AMT_FUN, P_DEC_AMT_FUN);
						END LOOP L;
				CLOSE C1;
		END;
		DELETE FROM ame_gl_his WHERE int_jid = P1;
		CASE P3
		WHEN 1 THEN
				CALL AME_PRO_POST_GL_JE_1(P1, P2);
		ELSE
				UPDATE ame_gl_je T1 SET var_status = 'Deleted', T1.var_void = P2, T1.dat_void = NOW() WHERE T1.int_jid = P1;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_IC_IA_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_IC_IA_1`(P1 varchar(100), P2 varchar(100), P3 integer, P4 integer, P5 integer, P6 integer)
    DETERMINISTIC
BEGIN
		DECLARE P_BAT_NUM_NEW INTEGER DEFAULT 0;
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_LINE_FRM INTEGER DEFAULT 0;
		DECLARE P_LINE_JE INTEGER DEFAULT 0;
		DECLARE P_NUM_IC INTEGER DEFAULT 0;
		DECLARE P_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_REF VARCHAR(250) DEFAULT "";
		DECLARE P_DESC VARCHAR(2000) DEFAULT "";
		DECLARE P_DATE DATETIME;
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_CLA VARCHAR(100) DEFAULT "";
		
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST INTEGER DEFAULT 6;
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.var_id, T.int_qty, T.int_cost, T.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_CUR, P_DEC_QTY, P_DEC_COST, P_DEC_AMT; END IF;CLOSE C1;END;
		IF P6 = -1 THEN SET P_BAT_NUM_NEW = AME_FUN_NEW_BATCH_NUM(); ELSE SET P_BAT_NUM_NEW = P6; END IF;
		IF P5 = -1 THEN SET P_JID = AME_FUN_NEW_JE_NUM(); ELSE SET P_JID = P5; END IF;
		IF P4 = -1 THEN SET P_NUM_IC = AME_FUN_NEW_SL_IC_HIS_NUM(); ELSE SET P_NUM_IC = P4; END IF;
		BEGIN
				SELECT T1.dat_date, T1.var_type, T1.var_ref, T1.var_desc
				INTO P_DATE, P_TYPE, P_REF, P_DESC FROM ame_ic_ia T1 WHERE T1.var_id = P1;
		END;
		BEGIN
				DECLARE D2 INTEGER DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DECLARE P_BAT_NUM_FRM INTEGER DEFAULT 0;
				DECLARE P_LINE INTEGER DEFAULT 0;
				DECLARE P_DESC_DET VARCHAR(2000) DEFAULT "";
				DECLARE P_LOC VARCHAR(100) DEFAULT "";
				DECLARE P_ITEM VARCHAR(100) DEFAULT "";
				DECLARE P_UOM VARCHAR(100) DEFAULT "";
				DECLARE P_QTY DOUBLE DEFAULT 0.0;
				DECLARE P_PRICE DOUBLE DEFAULT 0.0;
				DECLARE P_TO_LOC VARCHAR(100) DEFAULT "";
				DECLARE P_TO_ITE VARCHAR(100) DEFAULT "";
				DECLARE P_TO_UOM VARCHAR(100) DEFAULT "";
				DEClARE C2 CURSOR FOR SELECT T1.int_line, T1.int_batchno, T1.var_claid, T1.var_itemid, T1.var_locid, T1.var_uomid, T1.var_desc, T1.dou_qty, T1.dou_price FROM ame_ic_ia_det AS T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
				OPEN C2;
						L2: LOOP FETCH C2 INTO P_LINE_FRM, P_BAT_NUM_FRM, P_CLA, P_ITEM, P_LOC, P_UOM, P_DESC_DET, P_QTY, P_PRICE;
								IF D2 = 1 THEN LEAVE L2; END IF;
								SET @CON = AME_FUN_ITEM_CON(P_ITEM, P_UOM);
								SET @CON_TO = AME_FUN_ITEM_CON(P_TO_ITE, P_TO_UOM);
								CASE AME_FUN_ITEM_TYPE(P_ITEM)
								WHEN "Inventory Part" THEN
										IF P_BAT_NUM_FRM = 0 THEN
												SET P_LINE = P_LINE + 1;
												CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_QTY * @CON, P_QTY * P_PRICE, 0, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
												CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_DATE, P_QTY * @CON, P_QTY * P_PRICE, 0, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
												CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_DATE, P_QTY * @CON, P_QTY * P_PRICE, 0, 0, 0, 0, 0, P_NUM_IC, P_LINE, "IC-IA", "Inventory Adjustment Both Increase", P1, "", "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, P_JID);
												# ACC. INVENTORY
												SET @TUC = AME_FUN_ROUND(P_QTY  * P_PRICE, P_DEC_AMT);
												IF @TUC <> 0 THEN
														SET @P_ACC_SET = AME_FUN_ACC("IC", P_ITEM, "NA");
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, @TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, @TUC);
														SET @P_ACC_SET = AME_FUN_ACC("IC-IA", P_ITEM, "NA");
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, -@TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -@TUC);
														UPDATE ame_ic_ia_det T1 SET T1.dou_uc = @TUC WHERE T1.var_id = P1 AND T1.int_line = P_LINE_FRM;
												END IF;
										ELSE
												BEGIN
														DECLARE D3 INTEGER DEFAULT 0.0;
														DECLARE P_QTY_BAL DOUBLE DEFAULT 0.0;
														DECLARE P_QTY_SOLD DOUBLE DEFAULT 0.0;
														DECLARE P_QTY_BATCH DOUBLE DEFAULT 0.0;
														DECLARE P_UC DOUBLE DEFAULT 0.0;
														DECLARE P_AC1 DOUBLE DEFAULT 0.0;
														DECLARE P_AC2 DOUBLE DEFAULT 0.0;
														DECLARE P_AC3 DOUBLE DEFAULT 0.0;
														DECLARE P_AC4 DOUBLE DEFAULT 0.0;
														DECLARE P_AC5 DOUBLE DEFAULT 0.0;
														DECLARE P_TUC DOUBLE DEFAULT 0.0;
														DECLARE P_TAC1 DOUBLE DEFAULT 0.0;
														DECLARE P_TAC2 DOUBLE DEFAULT 0.0;
														DECLARE P_TAC3 DOUBLE DEFAULT 0.0;
														DECLARE P_TAC4 DOUBLE DEFAULT 0.0;
														DECLARE P_TAC5 DOUBLE DEFAULT 0.0;
														DEClARE C3 CURSOR FOR SELECT T.int_batchno, T.dou_qty, 
														AME_FUN_ROUND(T.dou_druc - T.dou_cruc, P_DEC_COST), AME_FUN_ROUND(T.dou_drac1 - T.dou_crac1, P_DEC_COST), AME_FUN_ROUND(T.dou_drac2 - T.dou_crac2, P_DEC_COST), AME_FUN_ROUND(T.dou_drac3 - T.dou_crac3, P_DEC_COST), AME_FUN_ROUND(T.dou_drac4 - T.dou_crac4, P_DEC_COST), AME_FUN_ROUND(T.dou_drac5 - T.dou_crac5, P_DEC_COST)
														FROM ame_sl_ic_batch AS T WHERE T.var_itemid = P_ITEM AND T.var_locid = P_LOC AND T.int_batchno = P_BAT_NUM_FRM AND T.dou_qty > 0 ORDER BY T.`int_batchno`;
														DECLARE CONTINUE HANDLER FOR NOT FOUND SET D3 = 1;
														SET P_QTY_BATCH = AME_FUN_ROUND(P_QTY * @CON, P_DEC_QTY);
														OPEN C3;
																L3:LOOP FETCH C3 INTO P_BAT_NUM, P_QTY_BAL, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
																		IF D3 = 1 THEN LEAVE L3; END IF;
																		IF P_QTY = 0 THEN
																				IF P_PRICE <> 0 THEN			
																						IF P_PRICE > 0 THEN
																								SET @TUC = AME_FUN_ROUND(P_QTY_BAL * P_PRICE, P_DEC_COST);
																								SET P_LINE = P_LINE + 1;
																								CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, @TUC, 0, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																								CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, @TUC, 0, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																								CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, @TUC, 0, 0, 0, 0, 0, P_NUM_IC, P_LINE, "IC-IA", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, P_JID);
																								IF @TUC <> 0 THEN
																										SET @P_ACC_SET = AME_FUN_ACC("IC-IA", P_ITEM, "NA");
																										SET P_LINE_JE = P_LINE_JE + 1;
																										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, @TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, @TUC);
																										SET @P_ACC_SET = AME_FUN_ACC("IC", P_ITEM, "NA");
																										SET P_LINE_JE = P_LINE_JE + 1;
																										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, -@TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -@TUC);
																										UPDATE ame_ic_ia_det T1 SET T1.dou_uc = @TUC WHERE T1.var_id = P1 AND T1.int_line = P_LINE_FRM;
																								END IF;
																						ELSE
																								SET @TUC = -AME_FUN_ROUND(P_QTY_BAL * P_PRICE, P_DEC_COST);
																								SET P_LINE = P_LINE + 1;
																								CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, @TUC, 0, 0, 0, 0, 0, "Cr", P_DEC_QTY, P_DEC_COST);
																								CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, @TUC, 0, 0, 0, 0, 0, "Cr", P_DEC_QTY, P_DEC_COST);
																								CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -0, -@TUC, -0, -0, -0, -0, -0, P_NUM_IC, P_LINE, "IC-IA", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, -0, P_JID);
																								IF @TUC <> 0 THEN
																										SET @P_ACC_SET = AME_FUN_ACC("IC", P_ITEM, "NA");
																										SET P_LINE_JE = P_LINE_JE + 1;
																										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, @TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, @TUC);
																										SET @P_ACC_SET = AME_FUN_ACC("IC-IA", P_ITEM, "NA");
																										SET P_LINE_JE = P_LINE_JE + 1;
																										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, -@TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -@TUC);
																										UPDATE ame_ic_ia_det T1 SET T1.dou_uc = @TUC WHERE T1.var_id = P1 AND T1.int_line = P_LINE_FRM;
																								END IF;
																						END IF;	
																				END IF;			
																		ELSE
																				SET @TTUC = 0;
																				SET @UC = AME_FUN_ROUND(P_QTY_BATCH * (P_UC / P_QTY_BAL), P_DEC_COST);
																				SET @AC1 = AME_FUN_ROUND(P_QTY_BATCH * (P_AC1 / P_QTY_BAL), P_DEC_COST);
																				SET @AC2 = AME_FUN_ROUND(P_QTY_BATCH * (P_AC2 / P_QTY_BAL), P_DEC_COST);
																				SET @AC3 = AME_FUN_ROUND(P_QTY_BATCH * (P_AC3 / P_QTY_BAL), P_DEC_COST);
																				SET @AC4 = AME_FUN_ROUND(P_QTY_BATCH * (P_AC4 / P_QTY_BAL), P_DEC_COST);
																				SET @AC5 = AME_FUN_ROUND(P_QTY_BATCH * (P_AC5 / P_QTY_BAL), P_DEC_COST);
																				IF P_QTY > 0 THEN
																						SET P_LINE = P_LINE + 1;
																						CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY_BATCH, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Dr", P_DEC_QTY, P_DEC_COST);
																						CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY_BATCH, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Dr", P_DEC_QTY, P_DEC_COST);
																						CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY_BATCH, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, P_NUM_IC, P_LINE, "IC-IA", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, P_JID);
																						SET @TUC = AME_FUN_ROUND(@UC + @AC1 + @AC2 + @AC3 + @AC4 + @AC5, P_DEC_AMT);
																						IF @TUC <> 0 THEN
																								SET @P_ACC_SET = AME_FUN_ACC("IC", P_ITEM, "NA");
																								SET P_LINE_JE = P_LINE_JE + 1;
																								CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, @TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, @TUC);
																								SET @P_ACC_SET = AME_FUN_ACC("IC-IA", P_ITEM, "NA");
																								SET P_LINE_JE = P_LINE_JE + 1;
																								CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, -@TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -@TUC);
																								SET @TTUC = @TTUC + @UC;
																						END IF;
																				ELSE
																						SET P_LINE = P_LINE + 1;
																						CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, -P_QTY_BATCH, -@UC, -@AC1, -@AC2, -@AC3, -@AC4, -@AC5, "Cr", P_DEC_QTY, P_DEC_COST);
																						CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY_BATCH, -@UC, -@AC1, -@AC2, -@AC3, -@AC4, -@AC5, "Cr", P_DEC_QTY, P_DEC_COST);
																						CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY_BATCH, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, P_NUM_IC, P_LINE, "IC-IA", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, -0, P_JID);
																						SET @TUC = -AME_FUN_ROUND(@UC + @AC1 + @AC2 + @AC3 + @AC4 + @AC5, P_DEC_AMT);
																						IF @TUC <> 0 THEN
																								SET @P_ACC_SET = AME_FUN_ACC("IC-IA", P_ITEM, "NA");
																								SET P_LINE_JE = P_LINE_JE + 1;
																								CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, @TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, @TUC);
																								SET @P_ACC_SET = AME_FUN_ACC("IC", P_ITEM, "NA");
																								SET P_LINE_JE = P_LINE_JE + 1;
																								CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, -@TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -@TUC);
																								SET @TTUC = @TTUC + @UC;
																						END IF;
																				END IF;
																				IF P_PRICE <> 0 THEN			
																						IF P_PRICE > 0 THEN
																								SET @TUC = AME_FUN_ROUND((P_QTY_BAL + P_QTY_BATCH) * P_PRICE, P_DEC_COST);
																								SET P_LINE = P_LINE + 1;
																								CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, @TUC, 0, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																								CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, @TUC, 0, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																								CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, @TUC, 0, 0, 0, 0, 0, P_NUM_IC, P_LINE, "IC-IA", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, P_JID);
																								IF @TUC <> 0 THEN
																										SET @P_ACC_SET = AME_FUN_ACC("IC-IA", P_ITEM, "NA");
																										SET P_LINE_JE = P_LINE_JE + 1;
																										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, @TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, @TUC);
																										SET @P_ACC_SET = AME_FUN_ACC("IC", P_ITEM, "NA");
																										SET P_LINE_JE = P_LINE_JE + 1;
																										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, -@TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -@TUC);
																										SET @TTUC = @TTUC + @TUC;
																								END IF;
																						ELSE
																								SET @TUC = -AME_FUN_ROUND((P_QTY_BAL + P_QTY_BATCH) * P_PRICE, P_DEC_COST);
																								SET P_LINE = P_LINE + 1;
																								CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, @TUC, 0, 0, 0, 0, 0, "Cr", P_DEC_QTY, P_DEC_COST);
																								CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, @TUC, 0, 0, 0, 0, 0, "Cr", P_DEC_QTY, P_DEC_COST);
																								CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -0, -@TUC, -0, -0, -0, -0, -0, P_NUM_IC, P_LINE, "IC-IA", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, -0, P_JID);
																								IF @TUC <> 0 THEN
																										SET @P_ACC_SET = AME_FUN_ACC("IC", P_ITEM, "NA");
																										SET P_LINE_JE = P_LINE_JE + 1;
																										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, @TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, @TUC);
																										SET @P_ACC_SET = AME_FUN_ACC("IC-IA", P_ITEM, "NA");
																										SET P_LINE_JE = P_LINE_JE + 1;
																										CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_SET, P_CLA, "IC-IA", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, -@TUC, P_DEC_AMT, "", "IC-IA", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -@TUC);
																										SET @TTUC = @TTUC + @TUC;
																								END IF;
																						END IF;	
																				END IF;
																				UPDATE ame_ic_ia_det T1 SET T1.dou_uc = @TTUC, T1.dou_ac1 = @AC1, T1.dou_ac2 = @AC2, T1.dou_ac3 = @AC3, T1.dou_ac4 = @AC4, T1.dou_ac5 = @AC5 WHERE T1.var_id = P1 AND T1.int_line = P_LINE_FRM;
																		END IF;	
																END LOOP L3;
														CLOSE C3;	
												END;	
										END IF;	
								WHEN "Inventory Assembly" THEN
										SET @Donothing = NULL;
								ELSE
										SET @Donothing = NULL;
								END CASE;
						END LOOP L2;
				CLOSE C2;
				CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, "", "IC-IA", "", 0, 0, P_CUR, 1, P_DEC_AMT, P_DEC_AMT);
				UPDATE ame_ic_ia T1 SET T1.var_status = 'Posted', T1.var_post = P2, T1.dat_post = NOW(), T1.int_jid = P_JID WHERE T1.var_id = P1;
		END;
		# AUTO POST GL
		IF AME_FUN_POST_ONE_TIME() = 1 THEN IF AME_FUN_VALID_GL_JE(P1) = 0 THEN CALL AME_PRO_POST_GL_JE_1(P_JID, P2); END IF; END IF;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_IC_IA_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_IC_IA_2`(P1 varchar(100), P2 varchar(100), P3 tinyint(1))
    DETERMINISTIC
BEGIN
		DECLARE P_JID INTEGER DEFAULT -1;
		DECLARE P_NUM_IC INTEGER DEFAULT -1;
		DECLARE P_JID_POST INTEGER DEFAULT -1;
		DECLARE P_BAT_NUM_NEW INTEGER DEFAULT -1;
		DECLARE P_DATE DATETIME;
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST INTEGER DEFAULT 6;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.var_id, T.int_qty, T.int_cost 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_CUR, P_DEC_QTY, P_DEC_COST; END IF;CLOSE C1;END;
		# SUB LEDGER IC
		BEGIN
				DECLARE D1 INTEGER DEFAULT 0;
				DECLARE P_TYPE VARCHAR(100) DEFAULT '';
				DECLARE P_ITEM VARCHAR(100) DEFAULT '';
				DECLARE P_LOC VARCHAR(100) DEFAULT '';
				DECLARE P_UOM VARCHAR(100) DEFAULT '';
				DECLARE P_QTY DOUBLE DEFAULT 0;
				DECLARE P_UC DOUBLE DEFAULT 0;
				DECLARE P_AC1 DOUBLE DEFAULT 0;
				DECLARE P_AC2 DOUBLE DEFAULT 0;
				DECLARE P_AC3 DOUBLE DEFAULT 0;
				DECLARE P_AC4 DOUBLE DEFAULT 0;
				DECLARE P_AC5 DOUBLE DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DEClARE C1 CURSOR FOR SELECT T1.int_jid, T1.var_doctype, T1.dat_date, T1.int_id, T1.var_locid, T1.var_itemid, T1.int_batchno, T1.dou_qty, 
				T1.dou_uc, T1.dou_ac1, T1.dou_ac2, T1.dou_ac3, T1.dou_ac4, T1.dou_ac5
				FROM ame_sl_ic_his AS T1 WHERE T1.var_modid = 'IC-IA' AND T1.var_docid = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				OPEN C1;
						L1: LOOP FETCH C1 INTO P_JID, P_TYPE, P_DATE, P_NUM_IC, P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
								IF D1 = 1 THEN LEAVE L1; END IF;
								IF P_TYPE = "Inventory Adjustment Both Increase" THEN
										CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
										CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
										SET P_BAT_NUM_NEW = P_BAT_NUM;
								ELSE
										IF P_QTY = 0 THEN
												SET @TUC = AME_FUN_ROUND(P_UC + P_AC1 + P_AC2 + P_AC3 + P_AC4 + P_AC5, P_DEC_COST);
												IF @TUC > 0 THEN
														CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
														CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
												ELSEIF @TUC < 0 THEN
														CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);
														CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);			
												END IF;	
										ELSE
												IF P_QTY > 0 THEN
														CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
														CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
												ELSEIF P_QTY < 0 THEN
														CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);
														CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);			
												END IF;	
										END IF;	
								END IF;		
						END LOOP L1;
				CLOSE C1;
				DELETE FROM ame_sl_ic_his WHERE var_modid = 'IC-IA' AND var_docid = P1;
		END;
		# DELETE OLD POST GL
		SELECT COUNT(T1.int_jid) INTO P_JID_POST FROM ame_gl_je T1 WHERE T1.int_jid = P_JID AND T1.var_status = 'Posted';
		IF P_JID_POST > 0 THEN CALL AME_PRO_POST_GL_JE_2(P_JID, P2, 0);END IF;
		DELETE FROM ame_gl_je WHERE int_jid = P_JID;DELETE FROM ame_gl_je_det WHERE int_jid = P_JID;
		IF P3 = 1 THEN
				CALL AME_PRO_POST_IC_IA_1(P1, P2, P3, P_NUM_IC, P_JID, P_BAT_NUM_NEW);
		ELSE
				UPDATE ame_ic_ia SET var_status = 'Deleted', var_void = P2, dat_void = NOW() WHERE var_id = P1;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_IC_ICUOM_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_IC_ICUOM_1`(P1 varchar(100), P2 varchar(100), P3 integer, P4 integer, P5 integer, P6 integer)
    DETERMINISTIC
BEGIN
		DECLARE P_BAT_NUM_NEW INTEGER DEFAULT 0;
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_LINE_FRM INTEGER DEFAULT 0;
		DECLARE P_LINE_JE INTEGER DEFAULT 0;
		DECLARE P_NUM_IC INTEGER DEFAULT 0;
		DECLARE P_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_REF VARCHAR(250) DEFAULT "";
		DECLARE P_DESC VARCHAR(2000) DEFAULT "";
		DECLARE P_DATE DATETIME;
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_CLA VARCHAR(100) DEFAULT "";
		
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST INTEGER DEFAULT 6;
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.var_id, T.int_qty, T.int_cost, T.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_CUR, P_DEC_QTY, P_DEC_COST, P_DEC_AMT; END IF;CLOSE C1;END;
		IF P6 = -1 THEN SET P_BAT_NUM_NEW = AME_FUN_NEW_BATCH_NUM(); ELSE SET P_BAT_NUM_NEW = P6; END IF;
		IF P5 = -1 THEN SET P_JID = AME_FUN_NEW_JE_NUM(); ELSE SET P_JID = P5; END IF;
		IF P4 = -1 THEN SET P_NUM_IC = AME_FUN_NEW_SL_IC_HIS_NUM(); ELSE SET P_NUM_IC = P4; END IF;
		BEGIN
				SELECT T1.dat_date, T1.var_type, T1.var_ref, T1.var_desc
				INTO P_DATE, P_TYPE, P_REF, P_DESC FROM ame_ic_icuom T1 WHERE T1.var_id = P1;
		END;
		BEGIN
				DECLARE D2 INTEGER DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DECLARE P_LINE INTEGER DEFAULT 0;
				DECLARE P_DESC_DET VARCHAR(2000) DEFAULT "";
				DECLARE P_LOC VARCHAR(100) DEFAULT "";
				DECLARE P_ITEM VARCHAR(100) DEFAULT "";
				DECLARE P_UOM VARCHAR(100) DEFAULT "";
				DECLARE P_QTY DOUBLE DEFAULT 0.0;
				DECLARE P_FACTOR DOUBLE DEFAULT 0.0;
				DECLARE P_TO_LOC VARCHAR(100) DEFAULT "";
				DECLARE P_TO_ITE VARCHAR(100) DEFAULT "";
				DECLARE P_TO_UOM VARCHAR(100) DEFAULT "";
				DEClARE C2 CURSOR FOR SELECT T1.int_line, T1.var_claid, T1.var_itemid, T1.var_locid, T1.var_uomid, T1.var_desc, T1.dou_qty, T1.var_toitemid, T1.var_tolocid, T1.var_uomid, IF(T1.var_convtype = "Small To Big", 1 / T1.dou_conv, T1.dou_conv) FROM ame_ic_icuom_det AS T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
				OPEN C2;
						L2: LOOP FETCH C2 INTO P_LINE_FRM, P_CLA, P_ITEM, P_LOC, P_UOM, P_DESC_DET, P_QTY, P_TO_ITE, P_TO_LOC, P_TO_UOM, P_FACTOR;
								IF D2 = 1 THEN LEAVE L2; END IF;
								SET @CON = AME_FUN_ITEM_CON(P_ITEM, P_UOM);
								SET @CON_TO = AME_FUN_ITEM_CON(P_TO_ITE, P_TO_UOM);
								CASE AME_FUN_ITEM_TYPE(P_ITEM)
								WHEN "Inventory Part" THEN
										BEGIN
												DECLARE D3 INTEGER DEFAULT 0.0;
												DECLARE P_QTY_BAL DOUBLE DEFAULT 0.0;
												DECLARE P_QTY_SOLD DOUBLE DEFAULT 0.0;
												DECLARE P_QTY_BATCH DOUBLE DEFAULT 0.0;
												DECLARE P_UC DOUBLE DEFAULT 0.0;
												DECLARE P_AC1 DOUBLE DEFAULT 0.0;
												DECLARE P_AC2 DOUBLE DEFAULT 0.0;
												DECLARE P_AC3 DOUBLE DEFAULT 0.0;
												DECLARE P_AC4 DOUBLE DEFAULT 0.0;
												DECLARE P_AC5 DOUBLE DEFAULT 0.0;
												DECLARE P_TUC DOUBLE DEFAULT 0.0;
												DECLARE P_TAC1 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC2 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC3 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC4 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC5 DOUBLE DEFAULT 0.0;
												DEClARE C3 CURSOR FOR SELECT T.int_batchno, T.dou_qty, 
												AME_FUN_ROUND(T.dou_druc - T.dou_cruc, P_DEC_COST), AME_FUN_ROUND(T.dou_drac1 - T.dou_crac1, P_DEC_COST), AME_FUN_ROUND(T.dou_drac2 - T.dou_crac2, P_DEC_COST), AME_FUN_ROUND(T.dou_drac3 - T.dou_crac3, P_DEC_COST), AME_FUN_ROUND(T.dou_drac4 - T.dou_crac4, P_DEC_COST), AME_FUN_ROUND(T.dou_drac5 - T.dou_crac5, P_DEC_COST)
												FROM ame_sl_ic_batch AS T WHERE T.var_itemid = P_ITEM AND T.var_locid = P_LOC AND T.dou_qty > 0 ORDER BY T.`int_batchno`;
												DECLARE CONTINUE HANDLER FOR NOT FOUND SET D3 = 1;
												SET P_QTY_BATCH = AME_FUN_ROUND(P_QTY * @CON, P_DEC_QTY);
												OPEN C3;
														L3:LOOP FETCH C3 INTO P_BAT_NUM, P_QTY_BAL, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
																IF D3 = 1 THEN LEAVE L3; END IF;
																IF P_QTY_BATCH >= P_QTY_BAL Then
																		SET P_QTY_SOLD = P_QTY_BAL;
																		SET P_QTY_BATCH = AME_FUN_ROUND(P_QTY_BATCH - P_QTY_BAL, P_DEC_QTY);
																ELSE
																		SET P_QTY_SOLD = P_QTY_BATCH;
																		SET P_QTY_BATCH = 0;
																END IF;
																# COSTING
																SET @UC = AME_FUN_ROUND(P_QTY_SOLD * (P_UC / P_QTY_BAL), P_DEC_COST);
																SET @AC1 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC1 / P_QTY_BAL), P_DEC_COST);
																SET @AC2 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC2 / P_QTY_BAL), P_DEC_COST);
																SET @AC3 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC3 / P_QTY_BAL), P_DEC_COST);
																SET @AC4 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC4 / P_QTY_BAL), P_DEC_COST);
																SET @AC5 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC5 / P_QTY_BAL), P_DEC_COST);

																SET P_TUC = AME_FUN_ROUND(P_TUC + @UC, P_DEC_COST);
																SET P_TAC1 = AME_FUN_ROUND(P_TAC1 + @AC1, P_DEC_COST);
																SET P_TAC2 = AME_FUN_ROUND(P_TAC2 + @AC2, P_DEC_COST);
																SET P_TAC3 = AME_FUN_ROUND(P_TAC3 + @AC3, P_DEC_COST);
																SET P_TAC4 = AME_FUN_ROUND(P_TAC4 + @AC4, P_DEC_COST);
																SET P_TAC5 = AME_FUN_ROUND(P_TAC5 + @AC5, P_DEC_COST);
															
																SET P_LINE = P_LINE + 1;
																CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Cr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Cr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY_SOLD, -@UC, -@AC1, -@AC2, -@AC3, -@AC4, -@AC5, P_NUM_IC, P_LINE, "IC-ICUOM", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, -0, P_JID);
																SET P_LINE = P_LINE + 1;
																CALL AME_PRO_UPDATE_SL_IC_BAT(P_TO_LOC, P_TO_ITE, P_BAT_NUM_NEW, P_QTY_SOLD * P_FACTOR * @CON_TO, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC(P_TO_LOC, P_TO_ITE, P_BAT_NUM_NEW, P_DATE, P_QTY_SOLD * P_FACTOR * @CON_TO, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC_HIS(P_TO_LOC, P_TO_ITE, P_BAT_NUM_NEW, P_DATE, P_QTY_SOLD * P_FACTOR * @CON_TO, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, P_NUM_IC, P_LINE, "IC-ICUOM", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, P_JID);
																IF P_QTY_BATCH = 0 THEN LEAVE L3; END IF;
														END LOOP L3;
												CLOSE C3;	
												SET @TUC = AME_FUN_ROUND(P_TUC + P_TAC1 + P_TAC2 + P_AC3 + P_AC4 + P_AC5, P_DEC_AMT);
												IF @TUC <> 0 THEN		
														SET @P_ACC_IC = AME_FUN_ACC("IC", P_TO_ITE, "NA");
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_IC, P_CLA, "IC-ICUOM", P_TYPE, P1, P_REF, "", "", "", P_TO_ITE, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, @TUC, P_DEC_AMT, "", "IC-ICUOM", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, @TUC);
														SET @P_ACC_IC = AME_FUN_ACC("IC", P_TO_ITE, "NA");
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_IC, P_CLA, "IC-ICUOM", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, -@TUC, P_DEC_AMT, "", "IC-ICUOM", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -@TUC);
												END IF;
												UPDATE ame_ic_icuom_det T1 SET T1.dou_uc = P_TUC, T1.dou_ac1 = P_TAC1, T1.dou_ac2 = P_TAC2, T1.dou_ac3 = P_TAC3, T1.dou_ac4 = P_TAC4, T1.dou_ac5 = P_TAC5 WHERE T1.var_id = P1 AND T1.int_line = P_LINE_FRM;
										END;		
								WHEN "Inventory Assembly" THEN
										SET @Donothing = NULL;
								ELSE
										SET @Donothing = NULL;
								END CASE;
						END LOOP L2;
				CLOSE C2;
				CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, "", "IC-ICUOM", "", 0, 0, P_CUR, 1, P_DEC_AMT, P_DEC_AMT);
				UPDATE ame_ic_icuom T1 SET T1.var_status = 'Posted', T1.var_post = P2, T1.dat_post = NOW(), T1.int_jid = P_JID WHERE T1.var_id = P1;
		END;
		# AUTO POST GL
		IF AME_FUN_POST_ONE_TIME() = 1 THEN IF AME_FUN_VALID_GL_JE(P1) = 0 THEN CALL AME_PRO_POST_GL_JE_1(P_JID, P2); END IF; END IF;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_IC_ICUOM_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_IC_ICUOM_2`(P1 varchar(100), P2 varchar(100), P3 tinyint(1))
    DETERMINISTIC
BEGIN
		DECLARE P_JID INTEGER DEFAULT -1;
		DECLARE P_NUM_IC INTEGER DEFAULT -1;
		DECLARE P_JID_POST INTEGER DEFAULT -1;
		DECLARE P_BAT_NUM_NEW INTEGER DEFAULT -1;
		DECLARE P_DATE DATETIME;
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST INTEGER DEFAULT 6;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.var_id, T.int_qty, T.int_cost 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_CUR, P_DEC_QTY, P_DEC_COST; END IF;CLOSE C1;END;
		# SUB LEDGER IC
		BEGIN
				DECLARE D1 INTEGER DEFAULT 0;
				DECLARE P_TYPE VARCHAR(100) DEFAULT '';
				DECLARE P_ITEM VARCHAR(100) DEFAULT '';
				DECLARE P_LOC VARCHAR(100) DEFAULT '';
				DECLARE P_UOM VARCHAR(100) DEFAULT '';
				DECLARE P_QTY DOUBLE DEFAULT 0;
				DECLARE P_UC DOUBLE DEFAULT 0;
				DECLARE P_AC1 DOUBLE DEFAULT 0;
				DECLARE P_AC2 DOUBLE DEFAULT 0;
				DECLARE P_AC3 DOUBLE DEFAULT 0;
				DECLARE P_AC4 DOUBLE DEFAULT 0;
				DECLARE P_AC5 DOUBLE DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DEClARE C1 CURSOR FOR SELECT T1.int_jid, T1.var_doctype, T1.dat_date, T1.int_id, T1.var_locid, T1.var_itemid, T1.int_batchno, T1.dou_qty, 
				T1.dou_uc, T1.dou_ac1, T1.dou_ac2, T1.dou_ac3, T1.dou_ac4, T1.dou_ac5
				FROM ame_sl_ic_his AS T1 WHERE T1.var_modid = 'IC-ICUOM' AND T1.var_docid = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				OPEN C1;
						L1: LOOP FETCH C1 INTO P_JID, P_TYPE, P_DATE, P_NUM_IC, P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
								IF D1 = 1 THEN LEAVE L1; END IF;	
								IF P_QTY >= 0 THEN
										CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
										CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
										SET P_BAT_NUM_NEW = P_BAT_NUM;
								ELSEIF P_QTY < 0 THEN
										CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);
										CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);			
								END IF;	
						END LOOP L1;
				CLOSE C1;
				DELETE FROM ame_sl_ic_his WHERE var_modid = 'IC-ICUOM' AND var_docid = P1;
		END;
		# DELETE OLD POST GL
		SELECT COUNT(T1.int_jid) INTO P_JID_POST FROM ame_gl_je T1 WHERE T1.int_jid = P_JID AND T1.var_status = 'Posted';
		IF P_JID_POST > 0 THEN CALL AME_PRO_POST_GL_JE_2(P_JID, P2, 0);END IF;
		DELETE FROM ame_gl_je WHERE int_jid = P_JID;DELETE FROM ame_gl_je_det WHERE int_jid = P_JID;
		IF P3 = 1 THEN
				CALL AME_PRO_POST_IC_ICUOM_1(P1, P2, P3, P_NUM_IC, P_JID, P_BAT_NUM_NEW);
		ELSE
				UPDATE ame_ic_icuom SET var_status = 'Deleted', var_void = P2, dat_void = NOW() WHERE var_id = P1;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_IC_IP_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_IC_IP_1`(P1 varchar(100), P2 varchar(100), P3 integer, P4 integer, P5 integer, P6 integer)
    DETERMINISTIC
BEGIN
		DECLARE P_BAT_NUM_NEW INTEGER DEFAULT 0;
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_LINE_FRM INTEGER DEFAULT 0;
		DECLARE P_LINE_JE INTEGER DEFAULT 0;
		DECLARE P_NUM_IC INTEGER DEFAULT 0;
		DECLARE P_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_REF VARCHAR(250) DEFAULT "";
		DECLARE P_DESC VARCHAR(2000) DEFAULT "";
		DECLARE P_DATE DATETIME;
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_CLA VARCHAR(100) DEFAULT "";
		DECLARE P_TTUC DOUBLE DEFAULT 0;
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST INTEGER DEFAULT 6;
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		DECLARE P_DEC_PER INTEGER DEFAULT 5;
		
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.var_id, T.int_qty, T.int_cost, T.int_amt, T.int_per 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_CUR, P_DEC_QTY, P_DEC_COST, P_DEC_AMT, P_DEC_PER; END IF;CLOSE C1;END;
		IF P6 = -1 THEN SET P_BAT_NUM_NEW = AME_FUN_NEW_BATCH_NUM(); ELSE SET P_BAT_NUM_NEW = P6; END IF;
		IF P5 = -1 THEN SET P_JID = AME_FUN_NEW_JE_NUM(); ELSE SET P_JID = P5; END IF;
		IF P4 = -1 THEN SET P_NUM_IC = AME_FUN_NEW_SL_IC_HIS_NUM(); ELSE SET P_NUM_IC = P4; END IF;
		BEGIN
				SELECT T1.dat_date, T1.var_type, T1.var_ref, T1.var_desc
				INTO P_DATE, P_TYPE, P_REF, P_DESC FROM ame_ic_ip T1 WHERE T1.var_id = P1;
		END;
		BEGIN
				DECLARE D2 INTEGER DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DECLARE P_LINE INTEGER DEFAULT 0;
				DECLARE P_DESC_DET VARCHAR(2000) DEFAULT "";
				DECLARE P_TYPE_DET VARCHAR(100) DEFAULT "";
				DECLARE P_LOC VARCHAR(100) DEFAULT "";
				DECLARE P_ITEM VARCHAR(100) DEFAULT "";
				DECLARE P_UOM VARCHAR(100) DEFAULT "";
				DECLARE P_QTY DOUBLE DEFAULT 0.0;
				DECLARE P_PRICE DOUBLE DEFAULT 0.0;
				DECLARE P_PRO INTEGER DEFAULT 0;
				DEClARE C2 CURSOR FOR SELECT T1.int_line, T1.var_type, T1.var_claid, T1.var_itemid, T1.var_locid, T1.var_uomid, T1.var_desc, T1.dou_qty, IF(COALESCE(T1.tin_pro, 0) = 0, T1.dou_price, AME_FUN_ROUND(T1.dou_price / 100, P_DEC_PER)), COALESCE(T1.tin_pro, 0) FROM ame_ic_ip_det AS T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
				OPEN C2;
						L2: LOOP FETCH C2 INTO P_LINE_FRM, P_TYPE_DET, P_CLA, P_ITEM, P_LOC, P_UOM, P_DESC_DET, P_QTY, P_PRICE, P_PRO;
								IF D2 = 1 THEN LEAVE L2; END IF;
								IF P_PRO = 0 THEN
										IF P_TYPE_DET = "Account" THEN
												IF P_PRICE > 0 THEN
														SET P_TTUC = AME_FUN_ROUND(P_TTUC + P_PRICE, P_DEC_AMT);
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ITEM, P_CLA, "IC-IP", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, -P_PRICE, P_DEC_AMT, "", "IC-IP", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -AME_FUN_ROUND(P_PRICE, P_DEC_AMT));
												ELSEIF P_PRICE < 0 THEN
														SET P_TTUC = AME_FUN_ROUND(P_TTUC - P_PRICE, P_DEC_AMT);
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ITEM, P_CLA, "IC-IP", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, -P_PRICE, P_DEC_AMT, "", "IC-IP", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -AME_FUN_ROUND(P_PRICE, P_DEC_AMT));
												END IF;
										ELSE	
												SET @CON = AME_FUN_ITEM_CON(P_ITEM, P_UOM);
												CASE AME_FUN_ITEM_TYPE(P_ITEM)
												WHEN "Inventory Part" THEN
														BEGIN
																DECLARE D3 INTEGER DEFAULT 0.0;
																DECLARE P_QTY_BAL DOUBLE DEFAULT 0.0;
																DECLARE P_QTY_SOLD DOUBLE DEFAULT 0.0;
																DECLARE P_QTY_BATCH DOUBLE DEFAULT 0.0;
																DECLARE P_UC DOUBLE DEFAULT 0.0;
																DECLARE P_AC1 DOUBLE DEFAULT 0.0;
																DECLARE P_AC2 DOUBLE DEFAULT 0.0;
																DECLARE P_AC3 DOUBLE DEFAULT 0.0;
																DECLARE P_AC4 DOUBLE DEFAULT 0.0;
																DECLARE P_AC5 DOUBLE DEFAULT 0.0;
																DECLARE P_TUC DOUBLE DEFAULT 0.0;
																DECLARE P_TAC1 DOUBLE DEFAULT 0.0;
																DECLARE P_TAC2 DOUBLE DEFAULT 0.0;
																DECLARE P_TAC3 DOUBLE DEFAULT 0.0;
																DECLARE P_TAC4 DOUBLE DEFAULT 0.0;
																DECLARE P_TAC5 DOUBLE DEFAULT 0.0;
																DEClARE C3 CURSOR FOR SELECT T.int_batchno, T.dou_qty, 
																AME_FUN_ROUND(T.dou_druc - T.dou_cruc, P_DEC_COST), AME_FUN_ROUND(T.dou_drac1 - T.dou_crac1, P_DEC_COST), AME_FUN_ROUND(T.dou_drac2 - T.dou_crac2, P_DEC_COST), AME_FUN_ROUND(T.dou_drac3 - T.dou_crac3, P_DEC_COST), AME_FUN_ROUND(T.dou_drac4 - T.dou_crac4, P_DEC_COST), AME_FUN_ROUND(T.dou_drac5 - T.dou_crac5, P_DEC_COST)
																FROM ame_sl_ic_batch AS T WHERE T.var_itemid = P_ITEM AND T.var_locid = P_LOC AND T.dou_qty > 0 ORDER BY T.`int_batchno`;
																DECLARE CONTINUE HANDLER FOR NOT FOUND SET D3 = 1;
																SET P_QTY_BATCH = AME_FUN_ROUND(P_QTY * @CON, P_DEC_QTY);
																OPEN C3;
																		L3:LOOP FETCH C3 INTO P_BAT_NUM, P_QTY_BAL, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
																				IF D3 = 1 THEN LEAVE L3; END IF;
																				IF P_QTY_BATCH >= P_QTY_BAL Then
																						SET P_QTY_SOLD = P_QTY_BAL;
																						SET P_QTY_BATCH = AME_FUN_ROUND(P_QTY_BATCH - P_QTY_BAL, P_DEC_QTY);
																				ELSE
																						SET P_QTY_SOLD = P_QTY_BATCH;
																						SET P_QTY_BATCH = 0;
																				END IF;
																				# COSTING
																				SET @UC = AME_FUN_ROUND(P_QTY_SOLD * (P_UC / P_QTY_BAL), P_DEC_COST);
																				SET @AC1 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC1 / P_QTY_BAL), P_DEC_COST);
																				SET @AC2 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC2 / P_QTY_BAL), P_DEC_COST);
																				SET @AC3 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC3 / P_QTY_BAL), P_DEC_COST);
																				SET @AC4 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC4 / P_QTY_BAL), P_DEC_COST);
																				SET @AC5 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC5 / P_QTY_BAL), P_DEC_COST);

																				SET P_TUC = AME_FUN_ROUND(P_TUC + @UC, P_DEC_COST);
																				SET P_TAC1 = AME_FUN_ROUND(P_TAC1 + @AC1, P_DEC_COST);
																				SET P_TAC2 = AME_FUN_ROUND(P_TAC2 + @AC2, P_DEC_COST);
																				SET P_TAC3 = AME_FUN_ROUND(P_TAC3 + @AC3, P_DEC_COST);
																				SET P_TAC4 = AME_FUN_ROUND(P_TAC4 + @AC4, P_DEC_COST);
																				SET P_TAC5 = AME_FUN_ROUND(P_TAC5 + @AC5, P_DEC_COST);
																			
																				SET P_LINE = P_LINE + 1;
																				CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Cr", P_DEC_QTY, P_DEC_COST);
																				CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Cr", P_DEC_QTY, P_DEC_COST);
																				CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY_SOLD, -@UC, -@AC1, -@AC2, -@AC3, -@AC4, -@AC5, P_NUM_IC, P_LINE, "IC-IP", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, -0, P_JID);
																				IF P_QTY_BATCH = 0 THEN LEAVE L3; END IF;
																		END LOOP L3;
																CLOSE C3;	
																SET @TUC = AME_FUN_ROUND(P_TUC + P_TAC1 + P_TAC2 + P_AC3 + P_AC4 + P_AC5, P_DEC_COST);
																SET P_TTUC = AME_FUN_ROUND(P_TTUC + @TUC, P_DEC_COST);
																IF AME_FUN_ROUND(@TUC, P_DEC_AMT) <> 0 THEN		
																		SET @P_ACC_IC = AME_FUN_ACC("IC", P_ITEM, "NA");
																		SET P_LINE_JE = P_LINE_JE + 1;
																		CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_IC, P_CLA, "IC-IP", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, -@TUC, P_DEC_AMT, "", "IC-IP", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -AME_FUN_ROUND(@TUC, P_DEC_AMT));
																END IF;
																UPDATE ame_ic_ip_det T1 SET T1.dou_uc = P_TUC, T1.dou_ac1 = P_TAC1, T1.dou_ac2 = P_TAC2, T1.dou_ac3 = P_TAC3, T1.dou_ac4 = P_TAC4, T1.dou_ac5 = P_TAC5 WHERE T1.var_id = P1 AND T1.int_line = P_LINE_FRM;
														END;
												WHEN "Inventory Assembly" THEN
														SET @Donothing = NULL;
												ELSE
														SET @Donothing = NULL;
												END CASE;
										END IF;
								ELSE
										IF P_TYPE_DET = "Account" THEN
												SET P_PRICE = AME_FUN_ROUND(P_TTUC * P_PRICE, P_DEC_AMT);
												IF P_PRICE > 0 THEN
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ITEM, P_CLA, "IC-IP", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, P_PRICE, P_DEC_AMT, "", "IC-IP", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, P_PRICE);
												ELSEIF P_PRICE < 0 THEN
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ITEM, P_CLA, "IC-IP", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, P_PRICE, P_DEC_AMT, "", "IC-IP", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, P_PRICE);
												END IF;
										ELSE	
												SET @CON = AME_FUN_ITEM_CON(P_ITEM, P_UOM);
												CASE AME_FUN_ITEM_TYPE(P_ITEM)
												WHEN "Inventory Part" THEN
														SET P_LINE = P_LINE + 1;
														CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_QTY * @CON, P_TTUC * P_PRICE, 0, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
														CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_DATE, P_QTY * @CON, P_TTUC * P_PRICE, 0, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
														CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM_NEW, P_DATE, P_QTY * @CON, P_TTUC * P_PRICE, 0, 0, 0, 0, 0, P_NUM_IC, P_LINE, "IC-IP", P_TYPE, P1, "", "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, P_JID);			
														
														SET @TUC = AME_FUN_ROUND(P_TTUC * P_PRICE, P_DEC_AMT);
														IF @TUC <> 0 THEN		
																SET @P_ACC_IC = AME_FUN_ACC("IC", P_ITEM, "NA");
																SET P_LINE_JE = P_LINE_JE + 1;
																CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_IC, P_CLA, "IC-IP", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, @TUC, P_DEC_AMT, "", "IC-IP", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, @TUC);
														END IF;
														UPDATE ame_ic_ip_det T1 SET T1.dou_uc = @TUC, T1.dou_ac1 = 0, T1.dou_ac2 = 0, T1.dou_ac3 = 0, T1.dou_ac4 = 0, T1.dou_ac5 = 0 WHERE T1.var_id = P1 AND T1.int_line = P_LINE_FRM;
												WHEN "Inventory Assembly" THEN
														SET @Donothing = NULL;
												ELSE
														SET @Donothing = NULL;
												END CASE;
										END IF;
								END IF;
						END LOOP L2;
				CLOSE C2;
				CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, "", "IC-IP", "", 0, 0, P_CUR, 1, P_DEC_AMT, P_DEC_AMT);
				UPDATE ame_ic_ip T1 SET T1.var_status = 'Posted', T1.var_post = P2, T1.dat_post = NOW(), T1.int_jid = P_JID WHERE T1.var_id = P1;
		END;
		# AUTO POST GL
		IF AME_FUN_POST_ONE_TIME() = 1 THEN IF AME_FUN_VALID_GL_JE(P1) = 0 THEN CALL AME_PRO_POST_GL_JE_1(P_JID, P2); END IF; END IF;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_IC_IP_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_IC_IP_2`(P1 varchar(100), P2 varchar(100), P3 tinyint(1))
    DETERMINISTIC
BEGIN
		DECLARE P_JID INTEGER DEFAULT -1;
		DECLARE P_NUM_IC INTEGER DEFAULT -1;
		DECLARE P_JID_POST INTEGER DEFAULT -1;
		DECLARE P_BAT_NUM_NEW INTEGER DEFAULT -1;
		DECLARE P_DATE DATETIME;
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST INTEGER DEFAULT 6;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.var_id, T.int_qty, T.int_cost 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_CUR, P_DEC_QTY, P_DEC_COST; END IF;CLOSE C1;END;
		# SUB LEDGER IC
		BEGIN
				DECLARE D1 INTEGER DEFAULT 0;
				DECLARE P_TYPE VARCHAR(100) DEFAULT '';
				DECLARE P_ITEM VARCHAR(100) DEFAULT '';
				DECLARE P_LOC VARCHAR(100) DEFAULT '';
				DECLARE P_UOM VARCHAR(100) DEFAULT '';
				DECLARE P_QTY DOUBLE DEFAULT 0;
				DECLARE P_UC DOUBLE DEFAULT 0;
				DECLARE P_AC1 DOUBLE DEFAULT 0;
				DECLARE P_AC2 DOUBLE DEFAULT 0;
				DECLARE P_AC3 DOUBLE DEFAULT 0;
				DECLARE P_AC4 DOUBLE DEFAULT 0;
				DECLARE P_AC5 DOUBLE DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DEClARE C1 CURSOR FOR SELECT T1.int_jid, T1.var_doctype, T1.dat_date, T1.int_id, T1.var_locid, T1.var_itemid, T1.int_batchno, T1.dou_qty, 
				T1.dou_uc, T1.dou_ac1, T1.dou_ac2, T1.dou_ac3, T1.dou_ac4, T1.dou_ac5
				FROM ame_sl_ic_his AS T1 WHERE T1.var_modid = 'IC-IP' AND T1.var_docid = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				OPEN C1;
						L1: LOOP FETCH C1 INTO P_JID, P_TYPE, P_DATE, P_NUM_IC, P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
								IF D1 = 1 THEN LEAVE L1; END IF;
										IF P_QTY > 0 THEN
												CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
												CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
												SET P_BAT_NUM_NEW = P_BAT_NUM;
										ELSEIF P_QTY < 0 THEN
												CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);
												CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);			
										END IF;	
						END LOOP L1;
				CLOSE C1;
				DELETE FROM ame_sl_ic_his WHERE var_modid = 'IC-IP' AND var_docid = P1;
		END;
		# DELETE OLD POST GL
		SELECT COUNT(T1.int_jid) INTO P_JID_POST FROM ame_gl_je T1 WHERE T1.int_jid = P_JID AND T1.var_status = 'Posted';
		IF P_JID_POST > 0 THEN CALL AME_PRO_POST_GL_JE_2(P_JID, P2, 0);END IF;
		DELETE FROM ame_gl_je WHERE int_jid = P_JID;DELETE FROM ame_gl_je_det WHERE int_jid = P_JID;
		IF P3 = 1 THEN
				CALL AME_PRO_POST_IC_IP_1(P1, P2, P3, P_NUM_IC, P_JID, P_BAT_NUM_NEW);
		ELSE
				UPDATE ame_ic_ip SET var_status = 'Deleted', var_void = P2, dat_void = NOW() WHERE var_id = P1;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_IC_IU_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_IC_IU_1`(P1 varchar(100), P2 varchar(100), P3 integer, P4 integer, P5 integer)
    DETERMINISTIC
BEGIN
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_LINE_FRM INTEGER DEFAULT 0;
		DECLARE P_LINE_JE INTEGER DEFAULT 0;
		DECLARE P_NUM_IC INTEGER DEFAULT 0;
		DECLARE P_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_REF VARCHAR(250) DEFAULT "";
		DECLARE P_DESC VARCHAR(2000) DEFAULT "";
		DECLARE P_DATE DATETIME;
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_CLA VARCHAR(100) DEFAULT "";
		
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST INTEGER DEFAULT 6;
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.var_id, T.int_qty, T.int_cost, T.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_CUR, P_DEC_QTY, P_DEC_COST, P_DEC_AMT; END IF;CLOSE C1;END;
		IF P5 = -1 THEN SET P_JID = AME_FUN_NEW_JE_NUM(); ELSE SET P_JID = P5; END IF;
		IF P4 = -1 THEN SET P_NUM_IC = AME_FUN_NEW_SL_IC_HIS_NUM(); ELSE SET P_NUM_IC = P4; END IF;
		BEGIN
				SELECT T1.dat_date, T1.var_type, T1.var_ref, T1.var_desc
				INTO P_DATE, P_TYPE, P_REF, P_DESC FROM ame_ic_iu T1 WHERE T1.var_id = P1;
		END;
		BEGIN
				DECLARE D2 INTEGER DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DECLARE P_LINE INTEGER DEFAULT 0;
				DECLARE P_DESC_DET VARCHAR(2000) DEFAULT "";
				DECLARE P_LOC VARCHAR(100) DEFAULT "";
				DECLARE P_TYPE_USE VARCHAR(100) DEFAULT "";
				DECLARE P_ITEM VARCHAR(100) DEFAULT "";
				DECLARE P_UOM VARCHAR(100) DEFAULT "";
				DECLARE P_ACC VARCHAR(100) DEFAULT "";
				DECLARE P_QTY DOUBLE DEFAULT 0.0;
				DECLARE P_APP_LOC VARCHAR(100) DEFAULT "";
				DECLARE P_APP_ITE VARCHAR(100) DEFAULT "";
				DECLARE P_APP_AC VARCHAR(100) DEFAULT "";
				DECLARE P_APP_BAT_NUM INTEGER DEFAULT 0;
				DEClARE C2 CURSOR FOR SELECT T1.var_accid, T1.int_line, T1.var_claid, T1.var_itemid, T1.var_locid, T1.var_type, T1.var_uomid, T1.var_desc, T1.dou_qty, T1.var_appacid, T1.var_applocid, T1.var_appitemid, T1.int_appbatchno FROM ame_ic_iu_det AS T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
				OPEN C2;
						L2: LOOP FETCH C2 INTO P_ACC, P_LINE_FRM, P_CLA, P_ITEM, P_LOC, P_TYPE_USE, P_UOM, P_DESC_DET, P_QTY, P_APP_AC, P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM;
								IF D2 = 1 THEN LEAVE L2; END IF;
								SET @CON = AME_FUN_ITEM_CON(P_ITEM, P_UOM);
								CASE AME_FUN_ITEM_TYPE(P_ITEM)
								WHEN "Inventory Part" THEN
										BEGIN
												DECLARE D3 INTEGER DEFAULT 0.0;
												DECLARE P_QTY_BAL DOUBLE DEFAULT 0.0;
												DECLARE P_QTY_SOLD DOUBLE DEFAULT 0.0;
												DECLARE P_QTY_BATCH DOUBLE DEFAULT 0.0;
												DECLARE P_UC DOUBLE DEFAULT 0.0;
												DECLARE P_AC1 DOUBLE DEFAULT 0.0;
												DECLARE P_AC2 DOUBLE DEFAULT 0.0;
												DECLARE P_AC3 DOUBLE DEFAULT 0.0;
												DECLARE P_AC4 DOUBLE DEFAULT 0.0;
												DECLARE P_AC5 DOUBLE DEFAULT 0.0;
												DECLARE P_TUC DOUBLE DEFAULT 0.0;
												DECLARE P_TAC1 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC2 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC3 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC4 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC5 DOUBLE DEFAULT 0.0;
												DEClARE C3 CURSOR FOR SELECT T.int_batchno, T.dou_qty, 
												AME_FUN_ROUND(T.dou_druc - T.dou_cruc, P_DEC_COST), AME_FUN_ROUND(T.dou_drac1 - T.dou_crac1, P_DEC_COST), AME_FUN_ROUND(T.dou_drac2 - T.dou_crac2, P_DEC_COST), AME_FUN_ROUND(T.dou_drac3 - T.dou_crac3, P_DEC_COST), AME_FUN_ROUND(T.dou_drac4 - T.dou_crac4, P_DEC_COST), AME_FUN_ROUND(T.dou_drac5 - T.dou_crac5, P_DEC_COST)
												FROM ame_sl_ic_batch AS T WHERE T.var_itemid = P_ITEM AND T.var_locid = P_LOC AND T.dou_qty > 0 ORDER BY T.`int_batchno`;
												DECLARE CONTINUE HANDLER FOR NOT FOUND SET D3 = 1;
												SET P_QTY_BATCH = AME_FUN_ROUND(P_QTY * @CON, P_DEC_QTY);
												OPEN C3;
														L3:LOOP FETCH C3 INTO P_BAT_NUM, P_QTY_BAL, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
																IF D3 = 1 THEN LEAVE L3; END IF;
																IF P_QTY_BATCH >= P_QTY_BAL Then
																		SET P_QTY_SOLD = P_QTY_BAL;
																		SET P_QTY_BATCH = AME_FUN_ROUND(P_QTY_BATCH - P_QTY_BAL, P_DEC_QTY);
																ELSE
																		SET P_QTY_SOLD = P_QTY_BATCH;
																		SET P_QTY_BATCH = 0;
																END IF;
																# COSTING
																SET @UC = AME_FUN_ROUND(P_QTY_SOLD * (P_UC / P_QTY_BAL), P_DEC_COST);
																SET @AC1 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC1 / P_QTY_BAL), P_DEC_COST);
																SET @AC2 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC2 / P_QTY_BAL), P_DEC_COST);
																SET @AC3 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC3 / P_QTY_BAL), P_DEC_COST);
																SET @AC4 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC4 / P_QTY_BAL), P_DEC_COST);
																SET @AC5 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC5 / P_QTY_BAL), P_DEC_COST);

																SET P_TUC = AME_FUN_ROUND(P_TUC + @UC, P_DEC_COST);
																SET P_TAC1 = AME_FUN_ROUND(P_TAC1 + @AC1, P_DEC_COST);
																SET P_TAC2 = AME_FUN_ROUND(P_TAC2 + @AC2, P_DEC_COST);
																SET P_TAC3 = AME_FUN_ROUND(P_TAC3 + @AC3, P_DEC_COST);
																SET P_TAC4 = AME_FUN_ROUND(P_TAC4 + @AC4, P_DEC_COST);
																SET P_TAC5 = AME_FUN_ROUND(P_TAC5 + @AC5, P_DEC_COST);
															
																SET P_LINE = P_LINE + 1;
																CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Cr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Cr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY_SOLD, -@UC, -@AC1, -@AC2, -@AC3, -@AC4, -@AC5, P_NUM_IC, P_LINE, "IC-IU", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, -0, P_JID);
																IF P_QTY_BATCH = 0 THEN LEAVE L3; END IF;
														END LOOP L3;
												CLOSE C3;	
												SET @TUC = AME_FUN_ROUND(P_TUC + P_TAC1 + P_TAC2 + P_AC3 + P_AC4 + P_AC5, P_DEC_COST);
												IF P_TYPE_USE = "Apply to Item Costing" THEN	
														SET P_LINE = P_LINE + 1;
														CASE P_APP_AC
														WHEN "AC1" THEN	
																CALL AME_PRO_UPDATE_SL_IC_BAT(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, 0, 0, @TUC, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, P_DATE, 0, 0, @TUC, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC_HIS(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, P_DATE, 0, 0, @TUC, 0, 0, 0, 0, P_NUM_IC, P_LINE, "IC-IU", P_TYPE_USE, P1, AME_FUN_VEN_BATCH(P_APP_BAT_NUM, P_APP_LOC, P_APP_ITE), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, P_JID);
														WHEN "AC2" THEN
																CALL AME_PRO_UPDATE_SL_IC_BAT(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, 0, 0, 0, @TUC, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, P_DATE, 0, 0, 0, @TUC, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC_HIS(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, P_DATE, 0, 0, 0, @TUC, 0, 0, 0, P_NUM_IC, P_LINE, "IC-IU", P_TYPE_USE, P1, AME_FUN_VEN_BATCH(P_APP_BAT_NUM, P_APP_LOC, P_APP_ITE), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, P_JID);
														WHEN "AC3" THEN
																CALL AME_PRO_UPDATE_SL_IC_BAT(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, 0, 0, 0, 0, @TUC, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, P_DATE, 0, 0, 0, 0, @TUC, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC_HIS(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, P_DATE, 0, 0, 0, 0, @TUC, 0, 0, P_NUM_IC, P_LINE, "IC-IU", P_TYPE_USE, P1, AME_FUN_VEN_BATCH(P_APP_BAT_NUM, P_APP_LOC, P_APP_ITE), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, P_JID);
														WHEN "AC4" THEN
																CALL AME_PRO_UPDATE_SL_IC_BAT(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, 0, 0, 0, 0, 0, @TUC, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, P_DATE, 0, 0, 0, 0, 0, @TUC, 0, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC_HIS(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, P_DATE, 0, 0, 0, 0, 0, @TUC, 0, P_NUM_IC, P_LINE, "IC-IU", P_TYPE_USE, P1, AME_FUN_VEN_BATCH(P_APP_BAT_NUM, P_APP_LOC, P_APP_ITE), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, P_JID);
														WHEN "AC5" THEN
																CALL AME_PRO_UPDATE_SL_IC_BAT(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, 0, 0, 0, 0, 0, 0, @TUC, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, P_DATE, 0, 0, 0, 0, 0, 0, @TUC, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC_HIS(P_APP_LOC, P_APP_ITE, P_APP_BAT_NUM, P_DATE, 0, 0, 0, 0, 0, 0, @TUC, P_NUM_IC, P_LINE, "IC-IU", P_TYPE_USE, P1, AME_FUN_VEN_BATCH(P_APP_BAT_NUM, P_APP_LOC, P_APP_ITE), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, P_JID);
														ELSE
																SET @Donothing = NULL;
														END CASE;
												END IF;
												IF AME_FUN_ROUND(@TUC, P_DEC_AMT) <> 0 THEN		
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC, P_CLA, "IC-IU", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Dr", "", "", "", "", "", P_DATE, NULL, NULL, @TUC, P_DEC_AMT, "", "IC-IU", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, AME_FUN_ROUND(@TUC, P_DEC_AMT));
														SET @P_ACC_IC = AME_FUN_ACC("IC", P_ITEM, "NA");
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, @P_ACC_IC, P_CLA, "IC-IU", P_TYPE, P1, P_REF, "", "", "", P_ITEM, P_DESC_DET, "Cr", "", "", "", "", "", P_DATE, NULL, NULL, -@TUC, P_DEC_AMT, "", "IC-IU", "", "", "", "", NULL, NULL, "IC", P_CUR, 1, -AME_FUN_ROUND(@TUC, P_DEC_AMT));
												END IF;
												UPDATE ame_ic_iu_det T1 SET T1.dou_uc = P_TUC, T1.dou_ac1 = P_TAC1, T1.dou_ac2 = P_TAC2, T1.dou_ac3 = P_TAC3, T1.dou_ac4 = P_TAC4, T1.dou_ac5 = P_TAC5 WHERE T1.var_id = P1 AND T1.int_line = P_LINE_FRM;
										END;		
								WHEN "Inventory Assembly" THEN
										SET @Donothing = NULL;
								ELSE
										SET @Donothing = NULL;
								END CASE;
						END LOOP L2;
				CLOSE C2;
				CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, "", "IC-IU", "", 0, 0, P_CUR, 1, P_DEC_AMT, P_DEC_AMT);
				UPDATE ame_ic_iu T1 SET T1.var_status = 'Posted', T1.var_post = P2, T1.dat_post = NOW(), T1.int_jid = P_JID WHERE T1.var_id = P1;
		END;
		# AUTO POST GL
		IF AME_FUN_POST_ONE_TIME() = 1 THEN IF AME_FUN_VALID_GL_JE(P1) = 0 THEN CALL AME_PRO_POST_GL_JE_1(P_JID, P2); END IF; END IF;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_IC_IU_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_IC_IU_2`(P1 varchar(100), P2 varchar(100), P3 tinyint(1))
    DETERMINISTIC
BEGIN
		DECLARE P_JID INTEGER DEFAULT -1;
		DECLARE P_NUM_IC INTEGER DEFAULT -1;
		DECLARE P_JID_POST INTEGER DEFAULT -1;
		DECLARE P_DATE DATETIME;
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST INTEGER DEFAULT 6;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.var_id, T.int_qty, T.int_cost 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_CUR, P_DEC_QTY, P_DEC_COST; END IF;CLOSE C1;END;
		# SUB LEDGER IC
		BEGIN
				DECLARE D1 INTEGER DEFAULT 0;
				DECLARE P_TYPE VARCHAR(100) DEFAULT '';
				DECLARE P_ITEM VARCHAR(100) DEFAULT '';
				DECLARE P_LOC VARCHAR(100) DEFAULT '';
				DECLARE P_UOM VARCHAR(100) DEFAULT '';
				DECLARE P_QTY DOUBLE DEFAULT 0;
				DECLARE P_UC DOUBLE DEFAULT 0;
				DECLARE P_AC1 DOUBLE DEFAULT 0;
				DECLARE P_AC2 DOUBLE DEFAULT 0;
				DECLARE P_AC3 DOUBLE DEFAULT 0;
				DECLARE P_AC4 DOUBLE DEFAULT 0;
				DECLARE P_AC5 DOUBLE DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DEClARE C1 CURSOR FOR SELECT T1.int_jid, T1.var_doctype, T1.dat_date, T1.int_id, T1.var_locid, T1.var_itemid, T1.int_batchno, T1.dou_qty, 
				T1.dou_uc, T1.dou_ac1, T1.dou_ac2, T1.dou_ac3, T1.dou_ac4, T1.dou_ac5
				FROM ame_sl_ic_his AS T1 WHERE T1.var_modid = 'IC-IU' AND T1.var_docid = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				OPEN C1;
						L1: LOOP FETCH C1 INTO P_JID, P_TYPE, P_DATE, P_NUM_IC, P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
								IF D1 = 1 THEN LEAVE L1; END IF;	
								IF P_TYPE = "Apply to Item Costing" THEN
										CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, 0, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
										CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);			
								ELSE
										CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);
										CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);			
								END IF;	
						END LOOP L1;
				CLOSE C1;
				DELETE FROM ame_sl_ic_his WHERE var_modid = 'IC-IU' AND var_docid = P1;
		END;
		# DELETE OLD POST GL
		SELECT COUNT(T1.int_jid) INTO P_JID_POST FROM ame_gl_je T1 WHERE T1.int_jid = P_JID AND T1.var_status = 'Posted';
		IF P_JID_POST > 0 THEN CALL AME_PRO_POST_GL_JE_2(P_JID, P2, 0);END IF;
		DELETE FROM ame_gl_je WHERE int_jid = P_JID;DELETE FROM ame_gl_je_det WHERE int_jid = P_JID;
		IF P3 = 1 THEN
				CALL AME_PRO_POST_IC_IU_1(P1, P2, P3, P_NUM_IC, P_JID);
		ELSE
				UPDATE ame_ic_iu SET var_status = 'Deleted', var_void = P2, dat_void = NOW() WHERE var_id = P1;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_IC_TF_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_IC_TF_1`(P1 varchar(100), P2 varchar(100), P3 int(11), P4 int(11), P5 int(11))
    DETERMINISTIC
BEGIN
		DECLARE P_BAT_NUM_NEW INTEGER DEFAULT 0;
		DECLARE P_LINE_JE INTEGER DEFAULT 0;
		DECLARE P_NUM_IC INTEGER DEFAULT 0;
		DECLARE P_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_REF VARCHAR(250) DEFAULT "";
		DECLARE P_DESC VARCHAR(2000) DEFAULT "";
		DECLARE P_DATE DATETIME;
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_CLA VARCHAR(100) DEFAULT "";
		
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST INTEGER DEFAULT 6;
		
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.var_id, T.int_qty, T.int_cost 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_CUR, P_DEC_QTY, P_DEC_COST; END IF;CLOSE C1;END;
		
		IF P5 = -1 THEN SET P_BAT_NUM_NEW = AME_FUN_NEW_BATCH_NUM(); ELSE SET P_BAT_NUM_NEW = P5; END IF;
		IF P4 = -1 THEN SET P_NUM_IC = AME_FUN_NEW_SL_IC_HIS_NUM(); ELSE SET P_NUM_IC = P4; END IF;
		BEGIN
				SELECT T1.dat_date, T1.var_type, T1.var_ref, T1.var_desc
				INTO P_DATE, P_TYPE, P_REF, P_DESC FROM ame_ic_tf T1 WHERE T1.var_id = P1;
		END;
		BEGIN
				DECLARE D2 INTEGER DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DECLARE P_LINE INTEGER DEFAULT 0;
				DECLARE P_LINE_FRM INTEGER DEFAULT 0;
				DECLARE P_DESC_DET VARCHAR(2000) DEFAULT "";
				DECLARE P_LOC VARCHAR(100) DEFAULT "";
				DECLARE P_LOC_TO VARCHAR(100) DEFAULT "";
				DECLARE P_ITEM VARCHAR(100) DEFAULT "";
				DECLARE P_UOM VARCHAR(100) DEFAULT "";
				DECLARE P_QTY DOUBLE DEFAULT 0.0;
				DEClARE C2 CURSOR FOR SELECT T1.int_line, T1.var_claid, T1.var_itemid, T1.var_locid, T1.var_tolocid, T1.var_uomid, T1.var_desc, T1.dou_qty FROM ame_ic_tf_det AS T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D2 = 1;
				OPEN C2;
						L2: LOOP FETCH C2 INTO P_LINE_FRM, P_CLA, P_ITEM, P_LOC, P_LOC_TO, P_UOM, P_DESC_DET, P_QTY;
								IF D2 = 1 THEN LEAVE L2; END IF;
								SET @CON = AME_FUN_ITEM_CON(P_ITEM, P_UOM);
								CASE AME_FUN_ITEM_TYPE(P_ITEM)
								WHEN "Inventory Part" THEN
										BEGIN
												DECLARE D3 INTEGER DEFAULT 0.0;
												DECLARE P_QTY_BAL DOUBLE DEFAULT 0.0;
												DECLARE P_QTY_SOLD DOUBLE DEFAULT 0.0;
												DECLARE P_QTY_BATCH DOUBLE DEFAULT 0.0;
												DECLARE P_UC DOUBLE DEFAULT 0.0;
												DECLARE P_AC1 DOUBLE DEFAULT 0.0;
												DECLARE P_AC2 DOUBLE DEFAULT 0.0;
												DECLARE P_AC3 DOUBLE DEFAULT 0.0;
												DECLARE P_AC4 DOUBLE DEFAULT 0.0;
												DECLARE P_AC5 DOUBLE DEFAULT 0.0;
												DECLARE P_TUC DOUBLE DEFAULT 0.0;
												DECLARE P_TAC1 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC2 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC3 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC4 DOUBLE DEFAULT 0.0;
												DECLARE P_TAC5 DOUBLE DEFAULT 0.0;
												DEClARE C3 CURSOR FOR SELECT T.int_batchno, AME_FUN_ROUND(T.dou_qty, P_DEC_QTY), 
												AME_FUN_ROUND(T.dou_druc - T.dou_cruc, P_DEC_COST), AME_FUN_ROUND(T.dou_drac1 - T.dou_crac1, P_DEC_COST), AME_FUN_ROUND(T.dou_drac2 - T.dou_crac2, P_DEC_COST), AME_FUN_ROUND(T.dou_drac3 - T.dou_crac3, P_DEC_COST), AME_FUN_ROUND(T.dou_drac4 - T.dou_crac4, P_DEC_COST), AME_FUN_ROUND(T.dou_drac5 - T.dou_crac5, P_DEC_COST)
												FROM ame_sl_ic_batch AS T WHERE T.var_itemid = P_ITEM AND T.var_locid = P_LOC AND T.dou_qty > 0 ORDER BY T.`int_batchno`;
												DECLARE CONTINUE HANDLER FOR NOT FOUND SET D3 = 1;
												SET P_QTY_BATCH = AME_FUN_ROUND(P_QTY * @CON, P_DEC_QTY);
												OPEN C3;
														L3:LOOP FETCH C3 INTO P_BAT_NUM, P_QTY_BAL, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
																IF D3 = 1 THEN LEAVE L3; END IF;
																IF P_QTY_BATCH >= P_QTY_BAL Then
																		SET P_QTY_SOLD = P_QTY_BAL;
																		SET P_QTY_BATCH = AME_FUN_ROUND(P_QTY_BATCH - P_QTY_BAL, P_DEC_QTY);
																ELSE
																		SET P_QTY_SOLD = P_QTY_BATCH;
																		SET P_QTY_BATCH = 0;
																END IF;
																# COSTING
																SET @UC = AME_FUN_ROUND(P_QTY_SOLD * (P_UC / P_QTY_BAL), P_DEC_COST);
																SET @AC1 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC1 / P_QTY_BAL), P_DEC_COST);
																SET @AC2 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC2 / P_QTY_BAL), P_DEC_COST);
																SET @AC3 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC3 / P_QTY_BAL), P_DEC_COST);
																SET @AC4 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC4 / P_QTY_BAL), P_DEC_COST);
																SET @AC5 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC5 / P_QTY_BAL), P_DEC_COST);		
																
																SET P_TUC = AME_FUN_ROUND(P_TUC + @UC, P_DEC_COST);
																SET P_TAC1 = AME_FUN_ROUND(P_TAC1 + @AC1, P_DEC_COST);
																SET P_TAC2 = AME_FUN_ROUND(P_TAC2 + @AC2, P_DEC_COST);
																SET P_TAC3 = AME_FUN_ROUND(P_TAC3 + @AC3, P_DEC_COST);
																SET P_TAC4 = AME_FUN_ROUND(P_TAC4 + @AC4, P_DEC_COST);
																SET P_TAC5 = AME_FUN_ROUND(P_TAC5 + @AC5, P_DEC_COST);

																SET P_LINE = P_LINE + 1;
																CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Cr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Cr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY_SOLD, -@UC, -@AC1, -@AC2, -@AC3, -@AC4, -@AC5, P_NUM_IC, P_LINE, "IC-TF", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, -0, 0);
																# TO LOCATION
																SET P_LINE = P_LINE + 1;
																CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC_TO, P_ITEM, P_BAT_NUM_NEW, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC(P_LOC_TO, P_ITEM, P_BAT_NUM_NEW, P_DATE, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Dr", P_DEC_QTY, P_DEC_COST);
																CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC_TO, P_ITEM, P_BAT_NUM_NEW, P_DATE, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, P_NUM_IC, P_LINE, "IC-TF", P_TYPE, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), "", "", P_CLA, P_REF, P_DESC, P_DESC_DET, P_CUR, 1, 0, 0);
																IF P_QTY_BATCH = 0 THEN LEAVE L3; END IF;
														END LOOP L3;
												CLOSE C3;		
												UPDATE ame_ic_tf_det T1 SET T1.dou_uc = P_TUC, T1.dou_ac1 = P_TAC1, T1.dou_ac2 = P_TAC2, T1.dou_ac3 = P_TAC3, T1.dou_ac4 = P_TAC4, T1.dou_ac5 = P_TAC5 WHERE T1.var_id = P1 AND T1.int_line = P_LINE_FRM;
										END;
								WHEN "Inventory Assembly" THEN
										SET @Donothing = NULL;
								ELSE
										SET @Donothing = NULL;
								END CASE;
						END LOOP L2;
				CLOSE C2;
				UPDATE ame_ic_tf SET var_status = 'Posted', var_post = P2, dat_post = NOW() WHERE var_id = P1;
		END;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_IC_TF_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_IC_TF_2`(P1 varchar(100), P2 varchar(100), P3 tinyint(1))
    DETERMINISTIC
BEGIN
		DECLARE P_BAT_NUM_NEW INTEGER DEFAULT -1;
		DECLARE P_NUM_IC INTEGER DEFAULT -1;		
		DECLARE P_DATE DATETIME;
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST INTEGER DEFAULT 6;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.var_id, T.int_qty, T.int_cost 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_CUR, P_DEC_QTY, P_DEC_COST; END IF;CLOSE C1;END;
		# SUB LEDGER IC
		BEGIN
				DECLARE D1 INTEGER DEFAULT 0;
				DECLARE P_ITEM VARCHAR(100) DEFAULT '';
				DECLARE P_LOC VARCHAR(100) DEFAULT '';
				DECLARE P_UOM VARCHAR(100) DEFAULT '';
				DECLARE P_QTY DOUBLE DEFAULT 0;
				DECLARE P_UC DOUBLE DEFAULT 0;
				DECLARE P_AC1 DOUBLE DEFAULT 0;
				DECLARE P_AC2 DOUBLE DEFAULT 0;
				DECLARE P_AC3 DOUBLE DEFAULT 0;
				DECLARE P_AC4 DOUBLE DEFAULT 0;
				DECLARE P_AC5 DOUBLE DEFAULT 0;
				DECLARE P_BAT_NUM INTEGER DEFAULT 0;
				DEClARE C1 CURSOR FOR SELECT T1.dat_date, T1.int_id, T1.var_locid, T1.var_itemid, T1.int_batchno, T1.dou_qty, 
				T1.dou_uc, T1.dou_ac1, T1.dou_ac2, T1.dou_ac3, T1.dou_ac4, T1.dou_ac5
				FROM ame_sl_ic_his AS T1 WHERE T1.var_modid = 'IC-TF' AND T1.var_docid = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				OPEN C1;
						L1: LOOP FETCH C1 INTO  P_DATE, P_NUM_IC, P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
								IF D1 = 1 THEN LEAVE L1; END IF;
								IF P_QTY > 0 THEN
										CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
										CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, -P_QTY, -P_UC, -P_AC1, -P_AC2, -P_AC3, -P_AC4, -P_AC5, "Dr", P_DEC_QTY, P_DEC_COST);
										SET P_BAT_NUM_NEW = P_BAT_NUM;
								ELSEIF P_QTY < 0 THEN
										CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);
										CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, P_QTY, P_UC, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST);
								END IF;	
						END LOOP L1;
				CLOSE C1;
				DELETE FROM ame_sl_ic_his WHERE var_modid = 'IC-TF' AND var_docid = P1;
		END;
		IF P3 = 1 THEN
				CALL AME_PRO_POST_IC_TF_1(P1, P2, P3, P_NUM_IC, P_BAT_NUM_NEW);
		ELSE
				UPDATE ame_ic_tf SET var_status = 'Deleted', var_void = P2, dat_void = NOW() WHERE var_id = P1;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_REFUND_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_REFUND_1`(P1 varchar(100), P2 varchar(100), P3 int(11), P4 int(11), P5 tinyint(1))
    DETERMINISTIC
BEGIN
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_DATE DATETIME;
		DECLARE P_ACC_AP VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_CASH VARCHAR(100) DEFAULT "";
		DECLARE P_VEN VARCHAR(100) DEFAULT "";
		DECLARE P_CLA VARCHAR(100) DEFAULT "";
		DECLARE P_CLA_DET VARCHAR(100) DEFAULT "";
		DECLARE P_REF VARCHAR(100) DEFAULT "";
		DECLARE P_DESC VARCHAR(2000) DEFAULT "";
		DECLARE P_DESC_DET VARCHAR(2000) DEFAULT "";
		DECLARE P_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_DOC VARCHAR(100) DEFAULT "";
		DECLARE P_DOC_REF VARCHAR(250) DEFAULT "";
		DECLARE P_DOC_DATE DATETIME;
		DECLARE P_PMT_METHOD VARCHAR(100) DEFAULT "";
		DECLARE P_PMT_REF VARCHAR(250) DEFAULT "";
		DECLARE P_BAL DOUBLE DEFAULT 0.0;
		
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_RATE DOUBLE DEFAULT 1.0;
		DECLARE P_FUN_BAL DOUBLE DEFAULT 0.0;
		DECLARE P_FUN_CASH DOUBLE DEFAULT 0.0;
		DECLARE P_DEC_AMT_FOR INTEGER DEFAULT 0;
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 0;

		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.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;
		# JOURNAL NUMBER
		IF P3 = -1 THEN SET P_JID = AME_FUN_NEW_JE_NUM(); ELSE SET P_JID = P3; END IF;
		BEGIN 
				DECLARE C2 CURSOR FOR SELECT T1.dat_date, T1.var_doctype, T1.var_ref, T1.var_claid, T1.var_perid, T1.var_accid, T1.var_preaccid, T1.var_docid, T1.var_desc, T1.var_pmttype, T1.var_pmtref, T1.dou_amt, T1.var_cur, T1.dou_rate, T1.dou_funamt, T1.dou_funar FROM ame_refund T1 WHERE T1.var_id = P1 AND T1.int_line = P5;
				OPEN C2;
						IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_DATE, P_TYPE, P_REF, P_CLA, P_VEN, P_ACC_CASH, P_ACC_AP, P_DOC, P_DESC, P_PMT_METHOD, P_PMT_REF, P_BAL, P_CUR, P_RATE, P_FUN_CASH, P_FUN_BAL; 
								CASE P_TYPE	
								WHEN "AP-RET" THEN
										# DECIMAL FOREIGN CURRENCY
										BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
										
										CALL AME_PRO_UPDATE_SL_AP(P_VEN, "Cr", P_DATE, P_BAL, P_DEC_AMT_FOR, P_CUR, P_FUN_BAL, P_DEC_AMT_FUN);
										CALL AME_PRO_UPDATE_SL_AP_HIS("AP-RF", P_TYPE, P1, P_VEN, P_CLA, P_ACC_CASH, "", P_PMT_METHOD, P_PMT_REF, P_REF, P_DESC, P_DATE, NULL, P_BAL, P_DEC_AMT_FOR, 0, "Cr", P4, P_JID, P_ACC_AP, P_CUR, P_RATE, P_FUN_BAL);
										IF AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR) <> 0 THEN
												BEGIN
														DECLARE P_APP_NET DOUBLE DEFAULT 0.0;
														DECLARE P_APP_PAID DOUBLE DEFAULT 0.0;
														DECLARE C3 CURSOR FOR SELECT T1.var_ref, T1.dat_date, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE(T1.dou_paid, 0), P_DEC_AMT_FOR) FROM ame_ap_ret T1 WHERE T1.var_id = P_DOC LIMIT 1;
														OPEN C3;
																IF FOUND_ROWS() > 0 THEN FETCH C3 INTO P_DOC_REF, P_DOC_DATE, P_APP_NET, P_APP_PAID;END IF;																				
														CLOSE C3;
												END;
												# CASH
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 1, P_ACC_CASH, P_CLA, "AP-RET", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AP-RET", "Purchase Return", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "CASH", P_CUR, P_RATE, P_FUN_CASH);
												# PREPAYMENT / AP
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 2, P_ACC_AP, P_CLA, "AP-RET", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AP-RET", "Purchase Return", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "AP", P_CUR, P_RATE, -P_FUN_BAL);
												SET @P_DIFF = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF(P_JID), P_DEC_AMT_FOR);
												IF @P_DIFF > 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AP-RET", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AP-RET", "Purchase Return", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												ELSEIF @P_DIFF < 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AP-RET", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AP-RET", "Purchase Return", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												END IF;
												CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, P_ACC_CASH, "AP-RF", "CASH", P_BAL, P_BAL * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
												UPDATE ame_refund SET var_status = 'Posted', int_jid = P_JID, var_post = P2, dat_post = NOW() WHERE var_id = P1 AND int_line = 0;
										END IF;
								WHEN "AP-DN" THEN
										# DECIMAL FOREIGN CURRENCY
										BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
										CALL AME_PRO_UPDATE_SL_AP(P_VEN, "Cr", P_DATE, P_BAL, P_DEC_AMT_FOR, P_CUR, P_FUN_BAL, P_DEC_AMT_FUN);
										CALL AME_PRO_UPDATE_SL_AP_HIS("AP-RF", P_TYPE, P1, P_VEN, P_CLA, P_ACC_CASH, "", P_PMT_METHOD, P_PMT_REF, P_REF, P_DESC, P_DATE, NULL, P_BAL, P_DEC_AMT_FOR, 0, "Cr", P4, P_JID, P_ACC_AP, P_CUR, P_RATE, P_FUN_BAL);
										IF AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR) <> 0 THEN
												BEGIN
														DECLARE P_APP_NET DOUBLE DEFAULT 0.0;
														DECLARE P_APP_PAID DOUBLE DEFAULT 0.0;
														DECLARE C3 CURSOR FOR SELECT T1.var_ref, T1.dat_date, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE(T1.dou_paid, 0), P_DEC_AMT_FOR) FROM ame_ap_dn T1 WHERE T1.var_id = P_DOC LIMIT 1;
														OPEN C3;
																IF FOUND_ROWS() > 0 THEN FETCH C3 INTO P_DOC_REF, P_DOC_DATE, P_APP_NET, P_APP_PAID;END IF;																				
														CLOSE C3;
												END;
												# CASH
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 1, P_ACC_CASH, P_CLA, "AP-DN", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AP-DN", "Debit Note", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "CASH", P_CUR, P_RATE, P_FUN_CASH);
												# PREPAYMENT / AP
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 2, P_ACC_AP, P_CLA, "AP-DN", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AP-DN", "Debit Note", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "AP", P_CUR, P_RATE, -P_FUN_BAL);
												SET @P_DIFF = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF(P_JID), P_DEC_AMT_FOR);
												IF @P_DIFF > 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AP-DN", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AP-DN", "Debit Note", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												ELSEIF @P_DIFF < 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AP-DN", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AP-DN", "Debit Note", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												END IF;
												CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, P_ACC_CASH, "AP-RF", "CASH", P_BAL, P_BAL * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
												UPDATE ame_refund SET var_status = 'Posted', int_jid = P_JID, var_post = P2, dat_post = NOW() WHERE var_id = P1 AND int_line = 0;
										END IF;
								WHEN "AP-PY" THEN
										# DECIMAL FOREIGN CURRENCY
										BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
										CALL AME_PRO_UPDATE_SL_AP(P_VEN, "Cr", P_DATE, P_BAL, P_DEC_AMT_FOR, P_CUR, P_FUN_BAL, P_DEC_AMT_FUN);
										CALL AME_PRO_UPDATE_SL_AP_HIS("AP-RF", P_TYPE, P1, P_VEN, P_CLA, P_ACC_CASH, "", P_PMT_METHOD, P_PMT_REF, P_REF, P_DESC, P_DATE, NULL, P_BAL, P_DEC_AMT_FOR, 0, "Cr", P4, P_JID, P_ACC_AP, P_CUR, P_RATE, P_FUN_BAL);
										IF AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR) <> 0 THEN
												BEGIN
			
														DECLARE C3 CURSOR FOR SELECT T1.var_ref, T1.dat_date FROM ame_ap_py T1 WHERE T1.var_id = P_DOC LIMIT 1;
														OPEN C3;
																IF FOUND_ROWS() > 0 THEN FETCH C3 INTO P_DOC_REF, P_DOC_DATE;END IF;																				
														CLOSE C3;
												END;
												# CASH
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 1, P_ACC_CASH, P_CLA, "AP-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AP-PY", "Prepayment", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "CASH", P_CUR, P_RATE, P_FUN_CASH);
												# PREPAYMENT / AP
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 2, P_ACC_AP, P_CLA, "AP-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AP-PY", "Prepayment", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "PREPAYMENT", P_CUR, P_RATE, -P_FUN_BAL);
												SET @P_DIFF = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF(P_JID), P_DEC_AMT_FOR);
												IF @P_DIFF > 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AP-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AP-PY", "Prepayment", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												ELSEIF @P_DIFF < 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AP-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AP-PY", "Prepayment", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												END IF;
												CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, P_ACC_CASH, "AP-RF", "CASH", P_BAL, P_BAL * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
												UPDATE ame_refund SET var_status = 'Posted', int_jid = P_JID, var_post = P2, dat_post = NOW() WHERE var_id = P1 AND int_line = 0;
										END IF;
								WHEN "AR-RET" THEN
										# DECIMAL FOREIGN CURRENCY
										BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
										CALL AME_PRO_UPDATE_SL_AR(P_VEN, "Dr", P_DATE, P_BAL, P_DEC_AMT_FOR, P_CUR, P_FUN_BAL, P_DEC_AMT_FUN);
										CALL AME_PRO_UPDATE_SL_AR_HIS("AR-RF", P_TYPE, P1, P_VEN, "", P_CLA, P_ACC_CASH, "", P_PMT_METHOD, P_PMT_REF, P_REF, P_DESC, P_DATE, NULL, P_BAL, P_DEC_AMT_FOR, 0, "Dr", P4, P_JID, P_ACC_AP, P_CUR, P_RATE, P_FUN_BAL);
										IF AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR) <> 0 THEN
												BEGIN
														DECLARE P_APP_NET DOUBLE DEFAULT 0.0;
														DECLARE P_APP_PAID DOUBLE DEFAULT 0.0;
														DECLARE C3 CURSOR FOR SELECT T1.var_ref, T1.dat_date, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE(T1.dou_paid, 0), P_DEC_AMT_FOR) FROM ame_ar_ret T1 WHERE T1.var_id = P_DOC LIMIT 1;
														OPEN C3;
																IF FOUND_ROWS() > 0 THEN FETCH C3 INTO P_DOC_REF, P_DOC_DATE, P_APP_NET, P_APP_PAID;END IF;																				
														CLOSE C3;
												END;
												# PREPAYMENT / AP
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 1, P_ACC_AP, P_CLA, "AR-RET", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AR-RET", "Sales Return", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "AR", P_CUR, P_RATE, P_FUN_BAL);
												# CASH
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 2, P_ACC_CASH, P_CLA, "AR-RET", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AR-RET", "Sales Return", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "CASH", P_CUR, P_RATE, -P_FUN_CASH);
												SET @P_DIFF = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF(P_JID), P_DEC_AMT_FOR);
												IF @P_DIFF > 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AR-RET", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AR-RET", "Credit Note", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												ELSEIF @P_DIFF < 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AR-RET", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AR-RET", "Credit Note", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												END IF;
												CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, P_ACC_CASH, "AR-RF", "CASH", P_BAL, P_BAL * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
												UPDATE ame_refund SET var_status = 'Posted', int_jid = P_JID, var_post = P2, dat_post = NOW() WHERE var_id = P1 AND int_line = 1;
										END IF;
								WHEN "AR-CN" THEN
										# DECIMAL FOREIGN CURRENCY
										BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
										CALL AME_PRO_UPDATE_SL_AR(P_VEN, "Dr", P_DATE, P_BAL, P_DEC_AMT_FOR, P_CUR, P_FUN_BAL, P_DEC_AMT_FUN);
										CALL AME_PRO_UPDATE_SL_AR_HIS("AR-RF", P_TYPE, P1, P_VEN, "", P_CLA, P_ACC_CASH, "", P_PMT_METHOD, P_PMT_REF, P_REF, P_DESC, P_DATE, NULL, P_BAL, P_DEC_AMT_FOR, 0, "Dr", P4, P_JID, P_ACC_AP, P_CUR, P_RATE, P_FUN_BAL);
										IF AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR) <> 0 THEN
												BEGIN
														DECLARE P_APP_NET DOUBLE DEFAULT 0.0;
														DECLARE P_APP_PAID DOUBLE DEFAULT 0.0;
														DECLARE C3 CURSOR FOR SELECT T1.var_ref, T1.dat_date, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE(T1.dou_paid, 0), P_DEC_AMT_FOR) FROM ame_ar_cn T1 WHERE T1.var_id = P_DOC LIMIT 1;
														OPEN C3;
																IF FOUND_ROWS() > 0 THEN FETCH C3 INTO P_DOC_REF, P_DOC_DATE, P_APP_NET, P_APP_PAID;END IF;																				
														CLOSE C3;
												END;
												# PREPAYMENT / AP
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 1, P_ACC_AP, P_CLA, "AR-CN", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AR-CN", "Credit Note", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "AR", P_CUR, P_RATE, P_FUN_BAL);
												# CASH
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 2, P_ACC_CASH, P_CLA, "AR-CN", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AR-CN", "Credit Note", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "CASH", P_CUR, P_RATE, -P_FUN_CASH);
												SET @P_DIFF = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF(P_JID), P_DEC_AMT_FOR);
												IF @P_DIFF > 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AR-CN", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AR-CN", "Credit Note", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												ELSEIF @P_DIFF < 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AR-CN", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AR-CN", "Credit Note", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												END IF;
												CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, P_ACC_CASH, "AR-RF", "CASH", P_BAL, P_BAL * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
												UPDATE ame_refund SET var_status = 'Posted', int_jid = P_JID, var_post = P2, dat_post = NOW() WHERE var_id = P1 AND int_line = 1;
										END IF;
								WHEN "AR-PY" THEN
										# DECIMAL FOREIGN CURRENCY
										BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
										CALL AME_PRO_UPDATE_SL_AR(P_VEN, "Dr", P_DATE, P_BAL, P_DEC_AMT_FOR, P_CUR, P_FUN_BAL, P_DEC_AMT_FUN);
										CALL AME_PRO_UPDATE_SL_AR_HIS("AR-RF", P_TYPE, P1, P_VEN, "", P_CLA, P_ACC_CASH, "", P_PMT_METHOD, P_PMT_REF, P_REF, P_DESC, P_DATE, NULL, P_BAL, P_DEC_AMT_FOR, 0, "Dr", P4, P_JID, P_ACC_AP, P_CUR, P_RATE, P_FUN_BAL);
										IF AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR) <> 0 THEN
												BEGIN
														DECLARE P_APP_NET DOUBLE DEFAULT 0.0;
														DECLARE P_APP_PAID DOUBLE DEFAULT 0.0;
														DECLARE C3 CURSOR FOR SELECT T1.var_ref, T1.dat_date, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE(T1.dou_paid, 0), P_DEC_AMT_FOR) FROM ame_ar_py T1 WHERE T1.var_id = P_DOC LIMIT 1;
														OPEN C3;
																IF FOUND_ROWS() > 0 THEN FETCH C3 INTO P_DOC_REF, P_DOC_DATE, P_APP_NET, P_APP_PAID;END IF;																				
														CLOSE C3;
												END;
												# PREPAYMENT / AP
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 1, P_ACC_AP, P_CLA, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AR-PY", "Prepayment", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "PREPAYMENT", P_CUR, P_RATE, P_FUN_BAL);
												# CASH
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 2, P_ACC_CASH, P_CLA, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -AME_FUN_ROUND(P_BAL, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "AR-PY", "Prepayment", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "CASH", P_CUR, P_RATE, -P_FUN_CASH);
												SET @P_DIFF = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF(P_JID), P_DEC_AMT_FOR);
												IF @P_DIFF > 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AR-PY", "Prepayment", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												ELSEIF @P_DIFF < 0 THEN
														SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-EXC", "DEFAULT", "DEFAULT");
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, 3, P_ACC_AP, P_CLA, "AR-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Exchange Rate", "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, 0, P_DEC_AMT_FOR, "", "AR-PY", "Prepayment", P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
												END IF;
												CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE, P1, P_REF, P_DESC, P2, P_DATE, P_ACC_CASH, "AR-RF", "CASH", P_BAL, P_BAL * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
												UPDATE ame_refund SET var_status = 'Posted', int_jid = P_JID, var_post = P2, dat_post = NOW() WHERE var_id = P1 AND int_line = 1;
										END IF;
								ELSE
										SET @Donothing = NULL;
								END CASE;				
						END IF;
				CLOSE C2;
		END;
		IF P5 = 0 THEN
				CALL AME_PRO_UPDATE_PMT_STATUS('AP-RF', P1, P_CUR);
		ELSE
				CALL AME_PRO_UPDATE_PMT_STATUS('AR-RF', P1, P_CUR);
		END IF;
		# AUTO POST GL
		IF AME_FUN_POST_ONE_TIME() = 1 THEN IF AME_FUN_VALID_GL_JE(P1) = 0 THEN CALL AME_PRO_POST_GL_JE_1(P_JID, P2); END IF; END IF;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_REFUND_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_REFUND_2`(P1 varchar(100), P2 varchar(100), P3 tinyint(1), P4 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_JID_POST INTEGER DEFAULT 0;
		DECLARE P_NUM INTEGER DEFAULT 0;
		DECLARE P_DATE DATETIME;
		DECLARE P_VEN VARCHAR(100) DEFAULT "";
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_DR_AMT DOUBLE DEFAULT 0.0;
		DECLARE P_CR_AMT DOUBLE DEFAULT 0.0;
		DECLARE P_DR_AMT_FUN DOUBLE DEFAULT 0.0;
		DECLARE P_CR_AMT_FUN DOUBLE DEFAULT 0.0;
		DECLARE P_DEC_AMT_FOR INTEGER DEFAULT 0;
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 0;
		CASE P4
		WHEN "AP-RF" THEN
				# DECIMAL FUNCTIONAL CURRENCY
				BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.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 C2 CURSOR FOR SELECT COALESCE(T1.var_cur, ''), T1.dou_fundr, T1.dou_funcr, T1.int_line, T1.dat_date, T1.var_venid, T1.dou_dr, T1.dou_cr, T1.int_jid FROM ame_sl_ap_his T1 WHERE T1.var_modid = P4 AND T1.var_docid = P1;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN
										FETCH C2 INTO P_CUR, P_DR_AMT_FUN, P_CR_AMT_FUN, P_NUM, P_DATE, P_VEN, P_DR_AMT, P_CR_AMT, P_JID;

										# DECIMAL FOREIGN CURRENCY
										BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
										CALL AME_PRO_UPDATE_SL_AP(P_VEN, "Cr", P_DATE, -P_CR_AMT, P_DEC_AMT_FOR, P_CUR, -P_CR_AMT_FUN, P_DEC_AMT_FUN);
										DELETE FROM ame_sl_ap_his WHERE var_modid = P4 AND var_docid = P1;
								END IF;
						CLOSE C2;
				END;
		WHEN "AR-RF" THEN
				# DECIMAL FUNCTIONAL CURRENCY
				BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.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 C2 CURSOR FOR SELECT COALESCE(T1.var_cur, ''), T1.dou_fundr, T1.dou_funcr, T1.int_line, T1.dat_date, T1.var_cusid, T1.dou_dr, T1.dou_cr, T1.int_jid FROM ame_sl_ar_his T1 WHERE T1.var_modid = P4 AND T1.var_docid = P1;
						OPEN C2;
								IF FOUND_ROWS() > 0 THEN
										FETCH C2 INTO P_CUR, P_DR_AMT_FUN, P_CR_AMT_FUN, P_NUM, P_DATE, P_VEN, P_DR_AMT, P_CR_AMT, P_JID;

										# DECIMAL FOREIGN CURRENCY
										BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT_FOR; END IF;CLOSE C1;END;
										CALL AME_PRO_UPDATE_SL_AR(P_VEN, "Dr", P_DATE, -P_DR_AMT, P_DEC_AMT_FOR, P_CUR, -P_DR_AMT_FUN, P_DEC_AMT_FUN);
										DELETE FROM ame_sl_ar_his WHERE var_modid = P4 AND var_docid = P1;
								END IF;
						CLOSE C2;
				END;
		ELSE
				SET @Donothing = NULL;
		END CASE;
		# DELETE OLD POST GL
		SELECT COUNT(T1.int_jid) INTO P_JID_POST FROM ame_gl_je T1 WHERE T1.int_jid = P_JID AND T1.var_status = 'Posted';
		IF P_JID_POST > 0 THEN CALL AME_PRO_POST_GL_JE_2(P_JID, P2, 0);END IF;
		DELETE FROM ame_gl_je WHERE int_jid = P_JID;DELETE FROM ame_gl_je_det WHERE int_jid = P_JID;
		IF P3 = 1 THEN
				CASE P4
				WHEN "AP-RF" THEN
						CALL AME_PRO_POST_REFUND_1(P1, P2, P_JID, P_NUM, 0);
				WHEN "AR-RF" THEN
						CALL AME_PRO_POST_REFUND_1(P1, P2, P_JID, P_NUM, 1);
				ELSE
						SET @Donothing = NULL;
				END CASE;	
		ELSE
				CASE P4
				WHEN "AP-RF" THEN
						UPDATE ame_refund SET var_status = 'Deleted', int_jid = NULL, var_modify = P2, dat_modify = NOW() WHERE var_id = P1 AND int_line = 0;
				WHEN "AR-RF" THEN
						UPDATE ame_refund SET var_status = 'Deleted', int_jid = NULL, var_modify = P2, dat_modify = NOW() WHERE var_id = P1 AND int_line = 1;
				ELSE
						SET @Donothing = NULL;
				END CASE;		
				CALL AME_PRO_UPDATE_PMT_STATUS(P4, P1, P_CUR);
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_IN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_IN`(P1 varchar(100), P4 integer, P5 integer, P7 integer, P8 integer, P11 integer, P12 varchar(100), P13 varchar(100), P14 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_PAID DOUBLE DEFAULT 0.0;
		DECLARE P_PRE_OUT DOUBLE DEFAULT 0.0;
		CASE P13
		WHEN "AP-PO" THEN
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(T1.dat_due) AS P_DUE, T1.var_term AS P_TERM, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_EMP_NAME_OTHER, T1.var_venid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 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, T1.var_desc AS P_DESC, IF(P5 = 0, T4.var_name_other, T3.var_desc) AS P_DESC_D, IF(T1.var_type = 'Apply to GL Account', T3.var_accid, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END) AS `P_ITE`, IF(T1.var_type = 'Apply to GL Account', T5.var_name, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name)) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_PRICE, T3.dou_sub AS P_AMT, T3.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T3.dou_net AS P_NET, 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, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ap_po T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id LEFT JOIN (ame_ap_po_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_accid = 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(P11 = 1, T3.dou_sub <> 0, TRUE);
		WHEN "AP-IN" THEN
				IF P7 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_net), 0) - COALESCE(T2.dou_paid, 0) FROM ame_ap_in T1
								LEFT JOIN (SELECT T1.var_venid, SUM(T2.dou_amt + T2.dou_app + T2.dou_disc) AS `dou_paid` FROM ame_ap_py T1 LEFT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id WHERE T1.var_venid = P12 AND T2.var_docid <> P1 AND T1.var_type = 'Apply to Purchase Invoice' AND T1.var_status IN('Open', 'Posted')) T2 ON T1.var_venid = T2.var_venid
								WHERE T1.var_venid = P12 AND T1.var_id <> P1;
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PRE_OUT; END IF; CLOSE C1;
						END;
				END IF;
				IF P8 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T2.dou_amt + T2.dou_disc + T2.dou_app), 0) AS `dou_paid` FROM ame_ap_py T1 LEFT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id WHERE T2.var_docid = P1 AND T1.var_status IN('Open', 'Posted');
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PAID; END IF; CLOSE C1;
						END;
				END IF;
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(T1.dat_due) AS P_DUE, T1.var_term AS P_TERM, T8.var_name AS P_EMP, '' AS P_EMP_NAME, '' AS P_EMP_NAME_OTHER, T1.var_venid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 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, T1.var_desc AS P_DESC, IF(P5 = 0, T4.var_name_other, T3.var_desc) AS P_DESC_D, IF(T1.var_type = 'Apply to GL Account', T3.var_accid, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END) AS `P_ITE`, IF(T1.var_type = 'Apply to GL Account', T5.var_name, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name)) AS P_ITE_NAME,IF(T3.var_type = "Purchase Inventory", T3.var_uomid, T4.var_uomid) AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_PRICE, T3.dou_sub AS P_AMT, T3.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T3.dou_net AS P_NET, 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, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ap_in T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id LEFT JOIN (ame_ap_in_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_accid = 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 LEFT JOIN ame_sys_add_cost T8 ON T3.var_type = T8.var_id
				WHERE T1.var_id = P1 AND IF(P11 = 1, T3.dou_sub <> 0, TRUE);
		WHEN "AP-RET" THEN
				IF P7 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_net), 0) - COALESCE(T2.dou_paid, 0) FROM ame_ap_ret T1
								LEFT JOIN (SELECT T1.var_venid, SUM(T2.dou_amt + T2.dou_app + T2.dou_disc) AS `dou_paid` FROM ame_ap_py T1 LEFT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id WHERE T1.var_venid = P12 AND T2.var_docid <> P1 AND T1.var_type = 'Apply to Purchase Invoice' AND T1.var_status IN('Open', 'Posted')) T2 ON T1.var_venid = T2.var_venid
								WHERE T1.var_venid = P12 AND T1.var_id <> P1;
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PRE_OUT; END IF; CLOSE C1;
						END;
				END IF;
				IF P8 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_tnet), 0) AS `dou_paid` FROM ame_ap_py T1 LEFT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id WHERE T2.var_docid = P1 AND T1.var_status IN('Open', 'Posted');
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PAID; END IF; CLOSE C1;
						END;
				END IF;
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(T1.dat_due) AS P_DUE, T1.var_term AS P_TERM, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_EMP_NAME_OTHER, T1.var_venid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 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, T1.var_desc AS P_DESC, IF(P5 = 0, T4.var_name_other, T3.var_desc) AS P_DESC_D, IF(T1.var_type = 'Apply to GL Account', T3.var_accid, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END) AS `P_ITE`, IF(T1.var_type = 'Apply to GL Account', T5.var_name, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name)) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_PRICE, T3.dou_sub AS P_AMT, T3.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T3.dou_net AS P_NET, 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, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ap_ret T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id LEFT JOIN (ame_ap_ret_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_accid = 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(P11 = 1, T3.dou_sub <> 0, TRUE);
		WHEN "AP-DN" THEN
				IF P7 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_net), 0) - COALESCE(T2.dou_paid, 0) FROM ame_ap_dn T1
								LEFT JOIN (SELECT T1.var_venid, SUM(T2.dou_amt + T2.dou_app + T2.dou_disc) AS `dou_paid` FROM ame_ap_py T1 LEFT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id WHERE T1.var_venid = P12 AND T2.var_docid <> P1 AND T1.var_type = 'Apply to Purchase Invoice' AND T1.var_status IN('Open', 'Posted')) T2 ON T1.var_venid = T2.var_venid
								WHERE T1.var_venid = P12 AND T1.var_id <> P1;
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PRE_OUT; END IF; CLOSE C1;
						END;
				END IF;
				IF P8 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_tnet), 0) AS `dou_paid` FROM ame_ap_py T1 LEFT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id WHERE T2.var_docid = P1 AND T1.var_status IN('Open', 'Posted');
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PAID; END IF; CLOSE C1;
						END;
				END IF;
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(T1.dat_due) AS P_DUE, T1.var_term AS P_TERM, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_EMP_NAME_OTHER, T1.var_venid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 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, T1.var_desc AS P_DESC, IF(P5 = 0, T4.var_name_other, T3.var_desc) AS P_DESC_D, IF(T1.var_type = 'Apply to GL Account', T3.var_accid, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END) AS `P_ITE`, IF(T1.var_type = 'Apply to GL Account', T5.var_name, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name)) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_PRICE, T3.dou_sub AS P_AMT, T3.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T3.dou_net AS P_NET, 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, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ap_dn T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id LEFT JOIN (ame_ap_dn_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_accid = 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(P11 = 1, T3.dou_sub <> 0, TRUE);
		WHEN "AR-IN" THEN
				IF P7 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_net), 0) - COALESCE(T2.dou_paid, 0) FROM ame_ar_in T1
								LEFT JOIN (SELECT T1.var_cusid, SUM(T2.dou_amt + T2.dou_app + T2.dou_disc) AS `dou_paid` FROM ame_ar_py T1 LEFT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id WHERE T1.var_cusid = P12 AND T2.var_docid <> P1 AND T1.var_type = 'Apply to Sale Invoice' AND T1.var_status IN('Open', 'Posted')) T2 ON T1.var_cusid = T2.var_cusid
								WHERE T1.var_cusid = P12 AND T1.var_id <> P1;
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PRE_OUT; END IF; CLOSE C1;
						END;
				END IF;
				IF P8 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T2.dou_amt + T2.dou_disc + T2.dou_app), 0) AS `dou_paid` FROM ame_ar_py T1 LEFT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id WHERE T2.var_docid = P1 AND T1.var_status IN('Open', 'Posted');
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PAID; END IF; CLOSE C1;
						END;
				END IF;
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(T1.dat_due) AS P_DUE, T1.var_term AS P_TERM, T1.var_empid AS P_EMP, T8.var_name AS P_EMP_NAME, COALESCE(T8.var_name_other, '') AS P_EMP_NAME_OTHER, T1.var_cusid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 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, T1.var_desc AS P_DESC, IF(P5 = 0, T4.var_name_other, T3.var_desc) AS P_DESC_D, IF(T1.var_type = 'Apply to GL Account', T3.var_accid, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END) AS `P_ITE`, IF(T1.var_type = 'Apply to GL Account', T5.var_name, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name)) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_PRICE, T3.dou_sub AS P_AMT, T3.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T3.dou_net AS P_NET, 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, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ar_in T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id LEFT JOIN (ame_ar_in_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_accid = 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(P11 = 1, T3.dou_sub <> 0, TRUE);
		WHEN "AR-RET" THEN
				IF P7 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_net), 0) - COALESCE(T2.dou_paid, 0) FROM ame_ar_ret T1
								LEFT JOIN (SELECT T1.var_cusid, SUM(T2.dou_amt + T2.dou_app + T2.dou_disc) AS `dou_paid` FROM ame_ar_py T1 LEFT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id WHERE T1.var_cusid = P12 AND T2.var_docid <> P1 AND T1.var_type = 'Apply to Sale Invoice' AND T1.var_status IN('Open', 'Posted')) T2 ON T1.var_cusid = T2.var_cusid
								WHERE T1.var_cusid = P12 AND T1.var_id <> P1;
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PRE_OUT; END IF; CLOSE C1;
						END;
				END IF;
				IF P8 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T2.dou_amt + dou_disc + dou_app), 0) AS `dou_paid` FROM ame_ar_py T1 LEFT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id WHERE T2.var_docid = P1 AND T1.var_status IN('Open', 'Posted');
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PAID; END IF; CLOSE C1;
						END;
				END IF;
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(T1.dat_due) AS P_DUE, T1.var_term AS P_TERM, T1.var_empid AS P_EMP, T8.var_name AS P_EMP_NAME, COALESCE(T8.var_name_other, '') AS P_EMP_NAME_OTHER, T1.var_cusid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 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, T1.var_desc AS P_DESC, IF(P5 = 0, T4.var_name_other, T3.var_desc) AS P_DESC_D, IF(T1.var_type = 'Apply to GL Account', T3.var_accid, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END) AS `P_ITE`, IF(T1.var_type = 'Apply to GL Account', T5.var_name, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name)) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_PRICE, T3.dou_sub AS P_AMT, T3.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T3.dou_net AS P_NET, 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, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ar_ret T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id LEFT JOIN (ame_ar_ret_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_accid = 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(P11 = 1, T3.dou_sub <> 0, TRUE);
		WHEN "AR-CN" THEN
				IF P7 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_net), 0) - COALESCE(T2.dou_paid, 0) FROM ame_ar_cn T1
								LEFT JOIN (SELECT T1.var_cusid, SUM(T2.dou_amt + T2.dou_app + T2.dou_disc) AS `dou_paid` FROM ame_ar_py T1 LEFT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id WHERE T1.var_cusid = P12 AND T2.var_docid <> P1 AND T1.var_type = 'Apply to Sale Invoice' AND T1.var_status IN('Open', 'Posted')) T2 ON T1.var_cusid = T2.var_cusid
								WHERE T1.var_cusid = P12 AND T1.var_id <> P1;
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PRE_OUT; END IF; CLOSE C1;
						END;
				END IF;
				IF P8 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T2.dou_amt + dou_disc + dou_app), 0) AS `dou_paid` FROM ame_ar_py T1 LEFT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id WHERE T2.var_docid = P1 AND T1.var_status IN('Open', 'Posted');
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PAID; END IF; CLOSE C1;
						END;
				END IF;
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(T1.dat_due) AS P_DUE, T1.var_term AS P_TERM, T1.var_empid AS P_EMP, T8.var_name AS P_EMP_NAME, COALESCE(T8.var_name_other, '') AS P_EMP_NAME_OTHER, T1.var_cusid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 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, T1.var_desc AS P_DESC, IF(P5 = 0, T4.var_name_other, T3.var_desc) AS P_DESC_D, IF(T1.var_type = 'Apply to GL Account', T3.var_accid, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END) AS `P_ITE`, IF(T1.var_type = 'Apply to GL Account', T5.var_name, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name)) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_PRICE, T3.dou_sub AS P_AMT, T3.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T3.dou_net AS P_NET, 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, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ar_cn T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id LEFT JOIN (ame_ar_cn_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_accid = 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(P11 = 1, T3.dou_sub <> 0, TRUE);
		WHEN "AR-QUO" THEN
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(T1.dat_due) AS P_DUE, T1.var_term AS P_TERM, T1.var_empid AS P_EMP, T8.var_name AS P_EMP_NAME, COALESCE(T8.var_name_other, '') AS P_EMP_NAME_OTHER, T1.var_cusid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 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, T1.var_desc AS P_DESC, IF(P5 = 0, T4.var_name_other, T3.var_desc) AS P_DESC_D, IF(T1.var_type = 'Apply to GL Account', T3.var_accid, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END) AS `P_ITE`, IF(T1.var_type = 'Apply to GL Account', T5.var_name, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name)) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_PRICE, T3.dou_sub AS P_AMT, T3.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T3.dou_net AS P_NET, 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, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ar_quo T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id LEFT JOIN (ame_ar_quo_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_accid = 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(P11 = 1, T3.dou_sub <> 0, TRUE);
		WHEN "AR-SO" THEN
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(T1.dat_due) AS P_DUE, T1.var_term AS P_TERM, T1.var_empid AS P_EMP, T8.var_name AS P_EMP_NAME, COALESCE(T8.var_name_other, '') AS P_EMP_NAME_OTHER, T1.var_cusid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 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, T1.var_desc AS P_DESC, IF(P5 = 0, T4.var_name_other, T3.var_desc) AS P_DESC_D, IF(T1.var_type = 'Apply to GL Account', T3.var_accid, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END) AS `P_ITE`, IF(T1.var_type = 'Apply to GL Account', T5.var_name, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name)) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_PRICE, T3.dou_sub AS P_AMT, T3.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T3.dou_net AS P_NET, 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, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ar_so T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id LEFT JOIN ame_mt_emp T8 ON T1.var_empid = T8.var_id LEFT JOIN (ame_ar_so_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_accid = 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(P11 = 1, T3.dou_sub <> 0, TRUE);
		WHEN "AR-SM" THEN
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(NOW()) AS P_DUE, "" AS P_TERM, "" AS P_EMP, "" AS P_EMP_NAME, "" AS P_EMP_NAME_OTHER, T1.var_cusid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 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, T1.var_desc AS P_DESC, T3.var_desc AS P_DESC_D, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END AS `P_ITE`, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, 0.0 AS P_PRICE, 0.0 AS P_AMT, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, "" AS P_CLA_M, "" AS P_CLA_M_NAME, "" AS P_CLA_D, "" AS P_CLA_D_NAME, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ar_sm T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id LEFT JOIN (ame_ar_sm_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id) ON T1.var_id = T3.var_id
				WHERE T1.var_id = P1 AND IF(P11 = 1, (T3.dou_qty + T3.dou_foc) <> 0, TRUE);
		WHEN "IC-TF" THEN
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(NOW()) AS P_DUE, "" AS P_TERM, "" AS P_EMP, "" AS P_EMP_NAME, "" AS P_EMP_NAME_OTHER, T3.var_locid AS P_VEN, T3.var_locid AS P_VEN_NAME, T3.var_tolocid AS P_VEN_NAME_OTHER, "" AS P_TEL, "" AS P_ADDR, T1.var_desc AS P_DESC, T3.var_desc AS P_DESC_D, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END AS `P_ITE`, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, 0.0 AS P_PRICE, 0.0 AS P_AMT, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, "" AS P_CLA_M, "" AS P_CLA_M_NAME, "" AS P_CLA_D, "" AS P_CLA_D_NAME, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ic_tf T1 LEFT JOIN (ame_ic_tf_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id) ON T1.var_id = T3.var_id
				WHERE T1.var_id = P1 AND IF(P11 = 1, T3.dou_qty <> 0, TRUE);
		WHEN "IC-IU" THEN
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(NOW()) AS P_DUE, "" AS P_TERM, "" AS P_EMP, "" AS P_EMP_NAME, "" AS P_EMP_NAME_OTHER, T3.var_locid AS P_VEN, T3.var_locid AS P_VEN_NAME, "" AS P_VEN_NAME_OTHER, "" AS P_TEL, "" AS P_ADDR, T1.var_desc AS P_DESC, T3.var_desc AS P_DESC_D, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END AS `P_ITE`, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, 0.0 AS P_PRICE, 0.0 AS P_AMT, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, "" AS P_CLA_M, "" AS P_CLA_M_NAME, "" AS P_CLA_D, "" AS P_CLA_D_NAME, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ic_iu T1 LEFT JOIN (ame_ic_iu_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id) ON T1.var_id = T3.var_id
				WHERE T1.var_id = P1 AND IF(P11 = 1, T3.dou_qty <> 0, TRUE);
		WHEN "IC-ICUOM" THEN
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(NOW()) AS P_DUE, "" AS P_TERM, "" AS P_EMP, "" AS P_EMP_NAME, "" AS P_EMP_NAME_OTHER, T3.var_locid AS P_VEN, T3.var_locid AS P_VEN_NAME, "" AS P_VEN_NAME_OTHER, "" AS P_TEL, "" AS P_ADDR, T1.var_desc AS P_DESC, T3.var_desc AS P_DESC_D, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END AS `P_ITE`, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, 0.0 AS P_PRICE, 0.0 AS P_AMT, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, "" AS P_CLA_M, "" AS P_CLA_M_NAME, "" AS P_CLA_D, "" AS P_CLA_D_NAME, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ic_icuom T1 LEFT JOIN (ame_ic_icuom_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id) ON T1.var_id = T3.var_id
				WHERE T1.var_id = P1 AND IF(P11 = 1, T3.dou_qty <> 0, TRUE);
		WHEN "IC-IA" THEN
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(NOW()) AS P_DUE, "" AS P_TERM, "" AS P_EMP, "" AS P_EMP_NAME, "" AS P_EMP_NAME_OTHER, T3.var_locid AS P_VEN, T3.var_locid AS P_VEN_NAME, "" AS P_VEN_NAME_OTHER, "" AS P_TEL, "" AS P_ADDR, T1.var_desc AS P_DESC, T3.var_desc AS P_DESC_D, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END AS `P_ITE`, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_PRICE, 0.0 AS P_AMT, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, "" AS P_CLA_M, "" AS P_CLA_M_NAME, "" AS P_CLA_D, "" AS P_CLA_D_NAME, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ic_ia T1 LEFT JOIN (ame_ic_ia_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id) ON T1.var_id = T3.var_id
				WHERE T1.var_id = P1 AND IF(P11 = 1, T3.dou_qty <> 0, TRUE);
		WHEN "IC-IP" THEN
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(NOW()) AS P_DUE, "" AS P_TERM, "" AS P_EMP, "" AS P_EMP_NAME, "" AS P_EMP_NAME_OTHER, T3.var_locid AS P_VEN, T3.var_locid AS P_VEN_NAME, "" AS P_VEN_NAME_OTHER, "" AS P_TEL, "" AS P_ADDR, T1.var_desc AS P_DESC, T3.var_desc AS P_DESC_D, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END AS `P_ITE`, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name) AS P_ITE_NAME, T3.var_uomid AS P_UOM, IF (T3.tin_pro = 0, -T3.dou_qty, T3.dou_qty) AS P_QTY, T3.dou_price AS P_PRICE, COALESCE(T3.tin_pro, 0) AS P_AMT, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, "" AS P_CLA_M, "" AS P_CLA_M_NAME, "" AS P_CLA_D, "" AS P_CLA_D_NAME, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ic_ip T1 LEFT JOIN (ame_ic_ip_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id) ON T1.var_id = T3.var_id
				WHERE T1.var_id = P1 AND IF(P11 = 1, T3.dou_qty <> 0, TRUE);
		ELSE
				IF P7 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_net), 0) - COALESCE(T2.dou_paid, 0) FROM ame_ap_in T1
								LEFT JOIN (SELECT T1.var_venid, SUM(T2.dou_amt + T2.dou_app + T2.dou_disc) AS `dou_paid` FROM ame_ap_py T1 LEFT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id WHERE T1.var_venid = P12 AND T2.var_docid <> P1 AND T1.var_type = 'Apply to Purchase Invoice' AND T1.var_status IN('Open', 'Posted')) T2 ON T1.var_venid = T2.var_venid
								WHERE T1.var_venid = P12 AND T1.var_id <> P1;
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PRE_OUT; END IF; CLOSE C1;
						END;
				END IF;
				IF P8 = 1 THEN
						BEGIN
								DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_net), 0) AS `dou_paid` FROM ame_ap_py T1 LEFT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id WHERE T2.var_docid = P1 AND T1.var_status IN('Open', 'Posted');
								OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_PAID; END IF; CLOSE C1;
						END;
				END IF;
				SELECT T1.var_type AS P_TYPE, T1.var_id AS P_NUM, COALESCE(T1.var_ref, '') AS P_REF, DATE(T1.dat_date) AS P_DATE, DATE(T1.dat_due) AS P_DUE, T1.var_term AS P_TERM, '' AS P_EMP, '' AS P_EMP_NAME, '' AS P_EMP_NAME_OTHER, T1.var_venid AS P_VEN, T2.var_name AS P_VEN_NAME, COALESCE(T2.var_name_other, '') AS P_VEN_NAME_OTHER, 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, T1.var_desc AS P_DESC, IF(P5 = 0, T4.var_name_other, T3.var_desc) AS P_DESC_D, IF(T1.var_type = 'Apply to GL Account', T3.var_accid, CASE WHEN P4 = 0 THEN T3.var_itemid WHEN P4 = 1 THEN T4.var_itemcode ELSE T4.var_barcode END) AS `P_ITE`, IF(T1.var_type = 'Apply to GL Account', T5.var_name, IF(P5 = 2, CONCAT(COALESCE(T4.var_name, ''), '\n', COALESCE(T3.var_desc, '')), T4.var_name)) AS P_ITE_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_PRICE, T3.dou_sub AS P_AMT, T3.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T3.dou_net AS P_NET, 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, P_PAID, P_PRE_OUT, "" AS P_SHIP_NAME, "" AS P_SHIP_ADDR
				FROM ame_ap_in T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id LEFT JOIN (ame_ap_in_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_accid = 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(P11 = 1, T3.dou_sub <> 0, TRUE);
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_JE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_JE`(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 PLACES
		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; 	
		IF P_ROW = 0 THEN
				SELECT T1.var_doctype AS P_TYPE, T1.var_docid AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, LPAD(COALESCE(T1.int_jid, ''), 5, "0")  AS P_VEN_NAME, "" AS P_VEN_NAME_OTHER, T1.var_desc AS P_DESC, T2.var_desc AS P_DESC_D, T2.var_accid AS P_DOC, COALESCE(T3.var_name, '') AS P_DOC_REF, IF(T2.dou_amt > 0, T2.dou_amt, 0) AS P_AMT, IF(T2.dou_amt < 0, -T2.dou_amt, 0) AS P_AMT_CR, T1.var_modid AS P_MOD, "" AS P_CLA_M_NAME, T2.var_claid AS P_CLA_D, T4.var_name AS P_CLA_D_NAME
				FROM ame_gl_je T1 LEFT JOIN (ame_gl_je_det T2 LEFT JOIN ame_mt_acc T3 ON T2.var_accid = T3.var_id LEFT JOIN ame_mt_cla T4 ON T2.var_docid = T4.var_id) ON T1.int_jid = T2.int_jid
				WHERE T1.int_jid = P1 AND IF(P3 = 0, TRUE, T2.dou_amt <> 0)  ORDER BY T2.int_line;
		ELSE
				SELECT T1.var_doctype AS P_TYPE, T1.var_docid AS P_NUM, T1.var_ref AS P_REF, DATE(T1.dat_date) AS P_DATE, LPAD(COALESCE(T1.int_jid, ''), 5, "0")  AS P_VEN_NAME, "" AS P_VEN_NAME_OTHER, T1.var_desc AS P_DESC, T2.var_desc AS P_DESC_D, T2.var_accid AS P_DOC, COALESCE(T3.var_name, '') AS P_DOC_REF, IF(T2.dou_funamt > 0, T2.dou_funamt, 0) AS P_AMT, IF(T2.dou_funamt < 0, -T2.dou_funamt, 0) AS P_AMT_CR, T1.var_modid AS P_MOD, "" AS P_CLA_M_NAME, T2.var_claid AS P_CLA_D, T4.var_name AS P_CLA_D_NAME
				FROM ame_gl_je T1 LEFT JOIN (ame_gl_je_det T2 LEFT JOIN ame_mt_acc T3 ON T2.var_accid = T3.var_id LEFT JOIN ame_mt_cla T4 ON T2.var_docid = T4.var_id) ON T1.int_jid = T2.int_jid
				WHERE T1.int_jid = P1 AND IF(P3 = 0, TRUE, T2.dou_funamt <> 0) ORDER BY T2.int_line;
		END IF;		
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D001DET_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D001DET_1`(P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(2000), P26 tinyint(1), P27 tinyint(1), P28 tinyint(1), P29 varchar(100))
    DETERMINISTIC
BEGIN
DECLARE P_ROW INTEGER DEFAULT 0;
SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_user_per_oth T1 WHERE T1.var_id = P29 AND T1.var_modid = 'HCI' AND COALESCE(T1.tin_per1, 0) = 0;
SET @ORDERBY = "";
SET @SORT = IF(P24 = "A-Z", " ASC ", " DESC ");
CASE P23
WHEN "Item Code" THEN SET @ORDERBY = " P_ITE_CODE ";
WHEN "Item Id" THEN SET @ORDERBY = " P_ITE ";
WHEN "Item Name" THEN SET @ORDERBY = " P_ITE_NAME ";
WHEN "Description" THEN SET @ORDERBY = " P_DESC ";
WHEN "Location" THEN SET @ORDERBY = " P_LOC ";
WHEN "Class Id" THEN SET @ORDERBY = " P_CLA_NAME ";
WHEN "Sub Total" THEN SET @ORDERBY = " P_SUB ";
WHEN "Discount" THEN SET @ORDERBY = " P_DISC ";
WHEN "VAT" THEN SET @ORDERBY = " P_TAX ";
WHEN "WHT" THEN SET @ORDERBY = " P_TAX ";
WHEN "Grand Total" THEN SET @ORDERBY = " P_NET ";
ELSE SET @ORDERBY = " P_ITE ";
END CASE;
SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT);
SET @SQL_EXC = CONCAT("SELECT T1.* FROM (SELECT 1, 'AP-IN' AS P_TYPE, T1.var_type AS P_TYPE_TRANS, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, IF(", P_ROW, " > 0, 0, T1.dou_sub) AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_TAX, T1.dou_net AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_modify AS P_USER
, T5.int_line AS P_LINE, IF(COALESCE(T5.var_itemid, '') = '', T5.var_accid, T5.var_itemid) AS 'P_ITE', IF(COALESCE(T5.var_itemid, '') = '', T7.var_name, T6.var_name) AS P_ITE_NAME, T5.var_uomid AS P_UOM, T5.dou_qty AS P_QTY, T5.dou_price AS P_UC, T5.dou_sub AS P_SUB_1, T5.dou_disdol AS P_DISC_1, T5.dou_taxdol AS P_TAX_1, T5.dou_net AS P_NET_1
FROM ame_ap_in T1
LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc) AS PMT_DISC, SUM(T1.dou_amt) AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_det T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id LEFT JOIN ame_mt_acc T7 ON T5.var_accid = T7.var_id) ON T1.var_id = T5.var_id
WHERE
(CASE
WHEN '", P1, "' = 'ALL' THEN TRUE
WHEN '", P1, "' = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
AND
IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
AND
IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
AND
IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
AND
IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
AND
IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
AND
IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
AND
IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
AND
IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
AND
IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
AND
IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
AND
IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
ORDER BY T1.var_id, T5.int_line) AS T1", @ORDERBY);
PREPARE STM FROM @SQL_EXC;
EXECUTE STM;
DEALLOCATE PREPARE STM;
END |
DELIMITER ;

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

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D001_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D001_M1`(D1 varchar(100),D2 varchar(100),D3 varchar(100),D4 varchar(100),D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(100), P26 varchar(100), P27 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_user_role T1 LEFT JOIN ame_user_per_oth T2 ON T1.int_id = T2.int_id WHERE T1.var_id = D1 AND T2.var_modid = 'HCI' AND COALESCE(T2.tin_per1, 0) = 1;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_VEN ";
		WHEN "Type" THEN SET @ORDERBY = " T1.P_TYPE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
		WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
		WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Location" THEN SET @ORDERBY = " T1.P_LOC ";
		WHEN "Class Id" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
		WHEN "Term" THEN SET @ORDERBY = " T1.P_TERM ";
		WHEN "Due Date" THEN SET @ORDERBY = " T1.P_DATE_DUE ";
		WHEN "Status" THEN SET @ORDERBY = " T1.P_STATUS ";
		WHEN "Payment Status" THEN SET @ORDERBY = " T1.P_STATUS_PMT ";
		WHEN "User" THEN SET @ORDERBY = " T1.P_USER ";
		WHEN "Sub Total" THEN SET @ORDERBY = " T1.P_SUB ";
		WHEN "Discount" THEN SET @ORDERBY = " T1.P_DISC ";
		WHEN "VAT" THEN SET @ORDERBY = " T1.P_VAT ";
		WHEN "Grand Total" THEN SET @ORDERBY = " T1.P_NET ";
		WHEN "Applied PMT" THEN SET @ORDERBY = " T1.PMT_APP ";
		WHEN "Discount PMT" THEN SET @ORDERBY = " T1.PMT_DISC ";
		WHEN "Cash PMT" THEN SET @ORDERBY = " T1.PMT_AMT ";
		ELSE SET @ORDERBY = " T1.P_ID ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY T1.P_SORTKEY, ", @ORDERBY, @SORT, ", T1.P_ID ", @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.* FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE,  '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_DESC, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE LIMIT 0)", 		
				# PURCHASE ORDER
				IF(P23 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 AS P_SORTKEY, 'AP-PO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB, T1.dou_fundisdol AS P_DISC, T1.dou_funtaxdol AS P_VAT, T1.dou_funtaxdol1 AS P_WHT, T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_po T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_po_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) 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', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PURCHASE
				IF(P24 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB, T1.dou_fundisdol AS P_DISC, T1.dou_funtaxdol AS P_VAT, T1.dou_funtaxdol1 AS P_WHT, T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_fundisc) AS PMT_DISC, SUM(T1.dou_funamt) AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_det T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PURCHASE RETURN
				IF(P25 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 AS P_SORTKEY, 'AP-RET' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_funsub AS P_SUB, -T1.dou_fundisdol AS P_DISC, -T1.dou_funtaxdol AS P_VAT, -T1.dou_funtaxdol1 AS P_WHT, -T1.dou_funnet AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Purchase Return' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AP-RET' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# DEBIT NOTE
				IF(P26 = "0", "", CONCAT(
				"UNION ALL (SELECT 4 AS P_SORTKEY, 'AP-DN' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_funsub AS P_SUB, -T1.dou_fundisdol AS P_DISC, -T1.dou_funtaxdol AS P_VAT, -T1.dou_funtaxdol1 AS P_WHT, -T1.dou_funnet AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_dn T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Debit Note' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AP-DN' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PAYMENT
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, '' AS P_LOC, -T1.dou_funnet AS P_SUB, 0 AS P_DISC, 0 AS P_VAT, 0 AS P_WHT, -dou_funnet AS P_NET, '' AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_py T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AP-PY' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				GROUP BY T1.var_id)")),
				") AS T1", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT * FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE,  '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_DESC, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE LIMIT 0)", 		
				# PURCHASE ORDER
				IF(P23 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 AS P_SORTKEY, 'AP-PO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_po T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_po_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) 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', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PURCHASE
				IF(P24 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc) AS PMT_DISC, SUM(T1.dou_amt) AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_det T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PURCHASE RETURN
				IF(P25 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 AS P_SORTKEY, 'AP-RET' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_sub AS P_SUB, -T1.dou_disdol AS P_DISC, -T1.dou_taxdol AS P_VAT, -T1.dou_taxdol1 AS P_WHT, -T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Purchase Return' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AP-RET' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# DEBIT NOTE
				IF(P26 = "0", "", CONCAT(
				"UNION ALL (SELECT 4 AS P_SORTKEY, 'AP-DN' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_sub AS P_SUB, -T1.dou_disdol AS P_DISC, -T1.dou_taxdol AS P_VAT, -T1.dou_taxdol1 AS P_WHT, -T1.dou_net AS P_NET, T1.var_term AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_dn T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Debit Note' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AP-DN' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PAYMENT
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, '' AS P_LOC, -T1.dou_net AS P_SUB, 0 AS P_DISC, 0 AS P_VAT, 0 AS P_WHT, -dou_net AS P_NET, '' AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_py T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status = 'Posted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status = 'Posted' AND T1.var_doctype = 'AP-PY' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				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_M001D002DET_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D002DET_2`(P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(2000), P26 tinyint(1), P27 tinyint(1), P28 tinyint(1))
    DETERMINISTIC
BEGIN
SET @ORDERBY = "";
SET @SORT = IF(P24 = "A-Z", " ASC ", " DESC ");
CASE P23
WHEN "Type" THEN SET @ORDERBY = " T1.P_TYPE ";
WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
WHEN "Transaction Type" THEN SET @ORDERBY = " T1.P_TYPE_TRAN ";
WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
WHEN "Quantity" THEN SET @ORDERBY = " T1.P_QTY ";
WHEN "Sub Total" THEN SET @ORDERBY = " T1.P_SUB ";
WHEN "Disc." THEN SET @ORDERBY = " T1.P_DISC ";
WHEN "Tax" THEN SET @ORDERBY = " T1.P_TAX ";
WHEN "Grand Total" THEN SET @ORDERBY = " T1.P_NET ";
ELSE SET @ORDERBY = " T1.P_ID ";
END CASE;
SET @ORDERBY = CONCAT(" ORDER BY ", @ORDERBY, @SORT, ", T1.P_ID ", @SORT);
SET @SQL_EXC = CONCAT("SELECT * FROM (SELECT 'AP-IN' AS P_TYPE, T1.var_type AS P_TYPE_TRAN, 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, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM, T1.var_venid AS P_VEN, T2.var_name AS P_VEN_NAME, IF(COALESCE(T3.var_itemid, '') = '', T3.var_accid, T3.var_itemid) AS P_ITE, IF(COALESCE(T3.var_itemid, '') = '', T5.var_name, T4.var_name) AS P_ITE_NAME, T3.var_desc AS P_DESC, T3.var_locid AS P_LOC, T3.var_claid AS P_CLA, T6.var_name AS P_CLA_NAME, T3.var_uomid AS P_UOM, T3.dou_qty AS P_QTY, T3.dou_price AS P_UC, T3.dou_sub AS P_SUB, T3.dou_disdol AS P_DISC, T3.dou_taxdol AS P_TAX, T3.dou_net AS P_NET
FROM ame_ap_in T1
LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id
LEFT JOIN (ame_ap_in_det T3 LEFT JOIN ame_mt_ite T4 ON T3.var_itemid = T4.var_id LEFT JOIN ame_mt_acc T5 ON T3.var_accid = T5.var_id LEFT JOIN ame_mt_cla T6 ON T3.var_claid = T6.var_id) ON T1.var_id = T3.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_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
AND
IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
AND
IF('", P15, "' = 'ALL', TRUE, T3.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T3.var_locid <= '", P16, "')
AND
IF('", P17, "' = 'ALL', TRUE, T4.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T4.var_groupid <= '", P18, "')
AND
IF('", P19, "' = 'ALL', TRUE, T3.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T3.var_itemid <= '", P20, "')
AND
IF('", P21, "' = 'ALL', TRUE, T3.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T3.var_accid <= '", P22, "')
) AS T1", @ORDERBY);
PREPARE STM FROM @SQL_EXC;
EXECUTE STM;
DEALLOCATE PREPARE STM;
END |
DELIMITER ;

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

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

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

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D002_M1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D002_M1`(D1 varchar(100),D2 varchar(100),D3 varchar(100),D4 varchar(100),D5 varchar(100), P1 varchar(100), P2 datetime, P3 datetime, P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100), P8 varchar(100), P9 varchar(100), P10 varchar(100), P11 varchar(100), P12 varchar(100), P13 varchar(100), P14 varchar(100), P15 varchar(100), P16 varchar(100), P17 varchar(100), P18 varchar(100), P19 varchar(100), P20 varchar(100), P21 varchar(100), P22 varchar(100), P23 varchar(100), P24 varchar(100), P25 varchar(100), P26 varchar(100), P27 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_user_role T1 LEFT JOIN ame_user_per_oth T2 ON T1.int_id = T2.int_id WHERE T1.var_id = D1 AND T2.var_modid = 'HCI' AND COALESCE(T2.tin_per1, 0) = 1;
		SET @ORDERBY = "";
		SET @SORT = IF(D5 = "A-Z", " ASC ", " DESC ");
		CASE D4
		WHEN "Date" THEN SET @ORDERBY = " T1.P_DATE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_VEN ";
		WHEN "Type" THEN SET @ORDERBY = " T1.P_TYPE ";
		WHEN "Reference" THEN SET @ORDERBY = " T1.P_REF ";
		WHEN "Vendor Name" THEN SET @ORDERBY = " T1.P_VEN_NAME ";
		WHEN "Remark" THEN SET @ORDERBY = " T1.P_DESC ";
		WHEN "Location" THEN SET @ORDERBY = " T1.P_LOC ";
		WHEN "Class Id" THEN SET @ORDERBY = " T1.P_CLA_NAME ";
		WHEN "Term" THEN SET @ORDERBY = " T1.P_TERM ";
		WHEN "Due Date" THEN SET @ORDERBY = " T1.P_DATE_DUE ";
		WHEN "Status" THEN SET @ORDERBY = " T1.P_STATUS ";
		WHEN "Payment Status" THEN SET @ORDERBY = " T1.P_STATUS_PMT ";
		WHEN "User" THEN SET @ORDERBY = " T1.P_USER ";
		WHEN "Sub Total" THEN SET @ORDERBY = " T1.P_SUB ";
		WHEN "Discount" THEN SET @ORDERBY = " T1.P_DISC ";
		WHEN "VAT" THEN SET @ORDERBY = " T1.P_VAT ";
		WHEN "Grand Total" THEN SET @ORDERBY = " T1.P_NET ";
		WHEN "Applied PMT" THEN SET @ORDERBY = " T1.PMT_APP ";
		WHEN "Discount PMT" THEN SET @ORDERBY = " T1.PMT_DISC ";
		WHEN "Cash PMT" THEN SET @ORDERBY = " T1.PMT_AMT ";
		ELSE SET @ORDERBY = " T1.P_ID ";
		END CASE;
		SET @ORDERBY = CONCAT(" ORDER BY T1.P_SORTKEY, ", @ORDERBY, @SORT, ", T1.P_ID ", @SORT);
		IF D2 = "0" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.P_SORTKEY, T1.P_MODULE,  T1.P_TYPE, T1.P_ID, T1.P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_DESC, T1.P_STATUS, T1.P_STATUS_PMT,
				T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, SUM(T1.P_SUB) AS P_SUB, SUM(T1.P_DISC) AS P_DISC, SUM(T1.P_VAT) AS P_VAT, SUM(T1.P_WHT) AS P_WHT, SUM(T1.P_NET) AS P_NET, T1.P_TERM,
				SUM(T1.PMT_DISC) AS PMT_DISC, SUM(T1.PMT_AMT) AS PMT_AMT, SUM(T1.PMT_APP) AS PMT_APP, T1.P_USER, T1.P_CUR, T1.P_RATE FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE,  '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_DESC, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE LIMIT 0)", 		
				# PURCHASE ORDER
				IF(P23 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 AS P_SORTKEY, 'AP-PO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB, T1.dou_fundisdol AS P_DISC, T1.dou_funtaxdol AS P_VAT, T1.dou_funtaxdol1 AS P_WHT, T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_po T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_po_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) 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', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PURCHASE
				IF(P24 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE, T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_funsub AS P_SUB, T1.dou_fundisdol AS P_DISC, T1.dou_funtaxdol AS P_VAT, T1.dou_funtaxdol1 AS P_WHT, T1.dou_funnet AS P_NET, T1.var_term AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_fundisc) AS PMT_DISC, SUM(T1.dou_funamt) AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_det T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status <> 'Deleted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PURCHASE RETURN
				IF(P25 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 AS P_SORTKEY, 'AP-RET' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_funsub AS P_SUB, -T1.dou_fundisdol AS P_DISC, -T1.dou_funtaxdol AS P_VAT, -T1.dou_funtaxdol1 AS P_WHT, -T1.dou_funnet AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status <> 'Deleted' AND T1.var_doctype = 'Purchase Return' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status <> 'Deleted' AND T1.var_doctype = 'AP-RET' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# DEBIT NOTE
				IF(P26 = "0", "", CONCAT(
				"UNION ALL (SELECT 4 AS P_SORTKEY, 'AP-DN' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_funsub AS P_SUB, -T1.dou_fundisdol AS P_DISC, -T1.dou_funtaxdol AS P_VAT, -T1.dou_funtaxdol1 AS P_WHT, -T1.dou_funnet AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_dn T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status <> 'Deleted' AND T1.var_doctype = 'Debit Note' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status <> 'Deleted' AND T1.var_doctype = 'AP-DN' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PAYMENT
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, '' AS P_LOC, -T1.dou_funnet AS P_SUB, 0 AS P_DISC, 0 AS P_VAT, 0 AS P_WHT, -dou_funnet AS P_NET, '' AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_py T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_funapp) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status <> 'Deleted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_funamt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status <> 'Deleted' AND T1.var_doctype = 'AP-PY' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				GROUP BY T1.var_id)")),
				") AS T1 GROUP BY T1.P_VEN ", @ORDERBY);
		ELSE
				SET @SQL_EXC = CONCAT("SELECT T1.P_SORTKEY, T1.P_MODULE,  T1.P_TYPE, T1.P_ID, T1.P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, T1.P_VEN, T1.P_VEN_NAME, T1.P_DESC, T1.P_STATUS, T1.P_STATUS_PMT,
				T1.P_CLA, T1.P_CLA_NAME, T1.P_LOC, SUM(T1.P_SUB) AS P_SUB, SUM(T1.P_DISC) AS P_DISC, SUM(T1.P_VAT) AS P_VAT, SUM(T1.P_WHT) AS P_WHT, SUM(T1.P_NET) AS P_NET, T1.P_TERM,
				SUM(T1.PMT_DISC) AS PMT_DISC, SUM(T1.PMT_AMT) AS PMT_AMT, SUM(T1.PMT_APP) AS PMT_APP, T1.P_USER, T1.P_CUR, T1.P_RATE FROM (
				(SELECT 1 AS P_SORTKEY, '' AS P_MODULE,  '' AS P_TYPE, '' AS P_ID, '' AS P_REF, DATE(NOW()) AS P_DATE, DATE(NOW()) AS P_DATE_DUE, '' AS P_VEN, '' AS P_VEN_NAME, '' AS P_DESC, '' AS P_STATUS, '' AS P_STATUS_PMT,
				'' AS P_CLA, '' AS P_CLA_NAME, '' AS P_LOC, 0.0 AS P_SUB, 0.0 AS P_DISC, 0.0 AS P_VAT, 0.0 AS P_WHT, 0.0 AS P_NET, '' AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, '' AS P_USER, '' AS P_CUR, 0.0 AS P_RATE LIMIT 0)", 		
				# PURCHASE ORDER
				IF(P23 = "0", "", CONCAT(
				"UNION ALL (SELECT 1 AS P_SORTKEY, 'AP-PO' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T1.dou_net AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
				0.0 AS PMT_DISC, 0.0 AS PMT_AMT, 0.0 AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_po T1
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_po_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) 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', 'Closed'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PURCHASE
				IF(P24 = "0", "", CONCAT(
				"UNION ALL (SELECT 2 AS P_SORTKEY, 'AP-IN' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, T1.dou_sub AS P_SUB, T1.dou_disdol AS P_DISC, T1.dou_taxdol AS P_VAT, T1.dou_taxdol1 AS P_WHT, T1.dou_net AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
				COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, COALESCE(T2.PMT_APP, 0) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_in T1
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc) AS PMT_DISC, SUM(T1.dou_amt) AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_det T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status <> 'Deleted' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_in_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PURCHASE RETURN
				IF(P25 = "0", "", CONCAT(
				"UNION ALL (SELECT 3 AS P_SORTKEY, 'AP-RET' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_sub AS P_SUB, -T1.dou_disdol AS P_DISC, -T1.dou_taxdol AS P_VAT, -T1.dou_taxdol1 AS P_WHT, -T1.dou_net AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_ret T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status <> 'Deleted' AND T1.var_doctype = 'Purchase Return' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_ret_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status <> 'Deleted' AND T1.var_doctype = 'AP-RET' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# DEBIT NOTE
				IF(P26 = "0", "", CONCAT(
				"UNION ALL (SELECT 4 AS P_SORTKEY, 'AP-DN' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, T1.dat_due AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, T5.var_locid AS P_LOC, -T1.dou_sub AS P_SUB, -T1.dou_disdol AS P_DISC, -T1.dou_taxdol AS P_VAT, -T1.dou_taxdol1 AS P_WHT, -T1.dou_net AS P_NET, IF(T1.var_term = 'CUSTOM', CONCAT(COALESCE(DATEDIFF(T1.dat_due, T1.dat_date), 0),' DAYS'), T1.var_term) AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_dn T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status <> 'Deleted' AND T1.var_doctype = 'Debit Note' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_dn_det T5 LEFT JOIN ame_mt_ite T6 ON T5.var_itemid = T6.var_id) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status <> 'Deleted' AND T1.var_doctype = 'AP-DN' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				IF('", P4, "' = 'ALL', TRUE, T1.var_type = '", P4, "')
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P6, "' = 'ALL', TRUE, T1.var_term = '", P6, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P15, "' = 'ALL', TRUE, T5.var_locid >= '", P15, "') AND IF('", P16, "' = 'ALL', TRUE, T5.var_locid <= '", P16, "')
				AND
				IF('", P17, "' = 'ALL', TRUE, T6.var_groupid >= '", P17, "') AND IF('", P18, "' = 'ALL', TRUE, T6.var_groupid <= '", P18, "')
				AND
				IF('", P19, "' = 'ALL', TRUE, T5.var_itemid >= '", P19, "') AND IF('", P20, "' = 'ALL', TRUE, T5.var_itemid <= '", P20, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_accid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_accid <= '", P22, "')
				GROUP BY T1.var_id)")),
				# PAYMENT
				IF(P27 = "0", "", CONCAT(
				"UNION ALL (SELECT 5 AS P_SORTKEY, 'AP-PY' AS P_MODULE,  T1.var_type AS P_TYPE, T1.var_id AS P_ID, T1.var_ref AS P_REF, T1.dat_date AS P_DATE, NULL AS P_DATE_DUE, T1.var_venid AS P_VEN, COALESCE(T3.var_name, '') AS P_VEN_NAME, T1.var_desc AS P_DESC, T1.var_status AS P_STATUS, T1.var_pmtstatus AS P_STATUS_PMT,
				T1.var_claid AS P_CLA, COALESCE(T4.var_name, '') AS P_CLA_NAME, '' AS P_LOC, -T1.dou_net AS P_SUB, 0 AS P_DISC, 0 AS P_VAT, 0 AS P_WHT, -dou_net AS P_NET, '' AS P_TERM,
				-COALESCE(T2.PMT_DISC, 0) AS PMT_DISC, -COALESCE(T2.PMT_AMT, 0) AS PMT_AMT, -(COALESCE(T2.PMT_APP, 0) + COALESCE(T7.PMT_APP, 0)) AS PMT_APP, T1.var_create AS P_USER, T1.var_cur AS P_CUR, T1.dou_rate AS P_RATE
				FROM ame_ap_py T1
				LEFT JOIN (SELECT T1.var_docid, 0 AS PMT_DISC, 0 AS PMT_AMT, SUM(T1.dou_app) AS PMT_APP FROM ame_ap_py_app T1 LEFT JOIN  ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T2.var_status <> 'Deleted' AND T1.var_doctype = 'Prepayment' GROUP BY T1.var_docid) AS T2 ON T1.var_id = T2.var_docid
				LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id
				LEFT JOIN ame_mt_cla T4 ON T1.var_claid = T4.var_id
				LEFT JOIN (ame_ap_py_det T5) ON T1.var_id = T5.var_id
				LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_amt) AS PMT_APP FROM ame_refund T1 WHERE T1.var_status <> 'Deleted' AND T1.var_doctype = 'AP-PY' GROUP BY T1.var_docid) AS T7 ON T1.var_id = T7.var_docid
				WHERE
				(CASE
				WHEN '", P1, "' = 'ALL' THEN TRUE
				WHEN '", P1, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P1, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P2, "')
				WHEN '", P1, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P2, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P2, "') AND DATE('", P3, "') END)
				AND
				T1.var_type = 'Prepayment'
				AND
				IF('", P5, "' = 'ALL', T1.var_status IN('Open', 'Posted'), T1.var_status = '", P5, "')
				AND
				IF('", P7, "' = 'ALL', TRUE, T1.var_id >= '", P7, "') AND IF('", P8, "' = 'ALL', TRUE, T1.var_id <= '", P8, "')
				AND
				IF('", P9, "' = 'ALL', TRUE, T3.var_groupid >= '", P9, "') AND IF('", P10, "' = 'ALL', TRUE, T3.var_groupid <= '", P10, "')
				AND
				IF('", P11, "' = 'ALL', TRUE, T1.var_venid >= '", P11, "') AND IF('", P12, "' = 'ALL', TRUE, T1.var_venid <= '", P12, "')
				AND
				IF('", P13, "' = 'ALL', TRUE, T1.var_claid >= '", P13, "') AND IF('", P14, "' = 'ALL', TRUE, T1.var_claid <= '", P14, "')
				AND
				IF('", P21, "' = 'ALL', TRUE, T5.var_docid >= '", P21, "') AND IF('", P22, "' = 'ALL', TRUE, T5.var_docid <= '", P22, "')
				GROUP BY T1.var_id)")),
				") AS T1 GROUP BY T1.P_VEN ", @ORDERBY);
		END IF;		
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_REP_RPT_M001D002_M3`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_REP_RPT_M001D002_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))
    DETERMINISTIC
BEGIN
		DECLARE P_ROW INTEGER DEFAULT 0;
		DECLARE P_WHERE_APP VARCHAR(2000) DEFAULT "";
		DECLARE P_WHERE_RF VARCHAR(2000) DEFAULT "";
		IF P25 = "1" THEN SET P_WHERE_APP = CONCAT(P_WHERE_APP, "'Purchase Return',"); SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AP-RET',"); END IF;
		IF P26 = "1" THEN SET P_WHERE_APP = CONCAT(P_WHERE_APP, "'Debit Note',"); SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AP-DN',"); END IF;
		IF P27 = "1" THEN SET P_WHERE_APP = CONCAT(P_WHERE_APP, "'Prepayment',"); SET P_WHERE_RF = CONCAT(P_WHERE_RF, "'AP-PY',"); END IF;
		IF P_WHERE_APP = "" THEN		
				SET P_WHERE_APP = " = TRUE ";
				SET P_WHERE_RF = " = TRUE ";
		ELSE
				SET P_WHERE_APP = SUBSTR(P_WHERE_APP FROM 1 FOR LENGTH(P_WHERE_APP) - 1);
				SET P_WHERE_APP = CONCAT(" IN(", P_WHERE_APP, ") ");

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