				SELECT DISTINCT T1.* FROM ame_sys_decimal T1 WHERE T1.var_id = (SELECT T2.var_id FROM ame_mt_cur T2 WHERE T2.tin_default = 1);
				SELECT DISTINCT T1.var_id FROM ame_mt_pmt_term T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_emp T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_pri_cod T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
		WHEN "IC_TF" THEN
				SELECT DISTINCT T1.var_id, T1.var_status FROM ame_ic_tf T1;
				SELECT DISTINCT T1.var_id FROM ame_mt_cla T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_loc T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_ite T1 WHERE COALESCE(T1.tin_inactive, 0) = 0 AND T1.var_type IN('Inventory Part', 'Non-Inventory Part', 'Fixed Asset');
				SELECT DISTINCT T1.var_id, T1.var_uomid FROM ame_mt_ite_uom T1;
				SELECT DISTINCT T1.* FROM ame_sys_decimal T1 WHERE T1.var_id = (SELECT T2.var_id FROM ame_mt_cur T2 WHERE T2.tin_default = 1);
		ELSE
				SET @Donothing = NULL;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_IMP_EXP_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_IMP_EXP_2`(P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_H TEXT;
		CASE P1
		WHEN "MT_HOL" THEN
				SELECT DISTINCT COALESCE(int_id, '') AS 'Holiday Code', var_name AS 'Holiday Name', DATE_FORMAT(T1.dat_date, "%d-%b-%Y") AS `Date (dd-MMM-YYYY)`, COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_hol T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.int_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.int_id <= P3)
				ORDER BY T1.int_id;
		WHEN "MT_CUR" THEN
				SELECT DISTINCT var_id AS 'Currency Code', var_name AS 'Currency Name', var_symbol AS 'Symbol', var_desc AS 'Remark', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_cur T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_CLA" THEN
				SELECT DISTINCT var_id AS 'Class Code', var_name AS 'Class Name', var_desc AS 'Remark', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_cla T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_LOC" THEN
				SELECT DISTINCT var_id AS 'Location Code', var_name AS 'Location Name', var_addr AS 'Address', var_desc AS 'Remark', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_loc T1
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_PRI_COD" THEN
				SELECT DISTINCT var_id AS 'Price Code', var_name AS 'Description', var_desc AS 'Remark', COALESCE(tin_reportprice, 0) AS 'Comparative Price', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_pri_cod T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_UOM" THEN
				SELECT DISTINCT var_id AS 'UOM Code', var_name AS 'UOM Name', var_desc AS 'Remark', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_uom T1
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_ITE_GRO" THEN
				SELECT GROUP_CONCAT(T1.var_header) INTO P_H FROM ame_sys_add_cost T1 ORDER BY T1.int_line;
				SET @SQL_EXC = CONCAT("SELECT DISTINCT var_id AS 'Item Group Code', var_name AS 'Item Group Name', var_desc AS 'Remark', var_accid_ic AS 'Inventory (Asset)', var_accid_pl AS 'Sale',
				var_accid_pl_ret AS 'Sale Return', var_accid_cogs AS 'Cost of Goods Sold (COGS)', var_accid_ac1 AS `Additional COGS: ", AME_FUN_SPLIT_STRING(P_H, ',', 1),"`, var_accid_ac2 AS `Additional COGS: ", AME_FUN_SPLIT_STRING(P_H, ',', 2),"`, var_accid_ac3 AS `Additional COGS: ", AME_FUN_SPLIT_STRING(P_H, ',', 3),"`, var_accid_ac4 AS `Additional COGS: ", AME_FUN_SPLIT_STRING(P_H, ',', 4),"`, var_accid_ac5 AS `Additional COGS: ", AME_FUN_SPLIT_STRING(P_H, ',', 5),"`,
				var_accid_ia AS 'Inventory Adjustment Expense', var_accid_iu AS 'Internal Usage Expense',	COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_ite_gro T1 
				WHERE IF('", P2, "' = 'ALL', TRUE, T1.var_id >= '", P2, "') AND IF('", P3, "' = 'ALL', TRUE, T1.var_id <= '", P3, "')
				ORDER BY T1.var_id");
				PREPARE STM FROM @SQL_EXC;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;
		WHEN "MT_VEN_GRO" THEN
				SELECT DISTINCT var_id AS 'Vendor Group Code', var_name AS 'Vendor Group Name', var_desc AS 'Remark', var_accid AS 'Payable Account', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_ven_gro T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_CUS_GRO" THEN
				SELECT DISTINCT var_id AS 'Customer Group Code', var_name AS 'Customer Group Name', var_desc AS 'Remark', var_accid AS 'Receivable Account', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_cus_gro T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_POS" THEN
				SELECT DISTINCT var_id AS 'Position Code', var_name AS 'Position Name', var_addr AS 'Position Name (Second Language)', var_desc AS 'Remark', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_pos T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_EMP_GRO" THEN
				SELECT DISTINCT var_id AS 'Employee Group Code', var_name AS 'Employee Group Name', var_desc AS 'Remark', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_emp_gro T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_PMT_TERM" THEN
				SELECT DISTINCT T1.var_id AS 'Payment Term Code', T1.var_name AS 'Description', T1.var_type AS 'Type', T1.int_daydue AS 'Net Due Days', T1.dou_disc AS 'Discount %', T1.int_daydisc AS 'Discount Days', T1.int_dayminpay AS 'Min Days to Pay', COALESCE(T1.tin_inactive, 0) AS 'Inactive'
				FROM ame_mt_pmt_term T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_PMT_METH" THEN
				SELECT DISTINCT T1.var_id AS 'Payment Method Code', T1.var_name AS 'Description', T1.var_desc AS 'Remark', COALESCE(T1.tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_pmt_meth T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_ACC" THEN
				SELECT DISTINCT T1.var_id AS 'Account ID', T1.var_name AS 'Account Name', COALESCE(T1.int_typeid, '') AS 'Account Type ID', T1.var_desc AS 'Remark', COALESCE(T1.tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_acc T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
				SELECT DISTINCT COALESCE(T1.var_id, '') AS 'Account Type ID', T1.var_name AS 'Account Type Name' FROM ame_mt_acc_type T1 ORDER BY T1.var_id;
		WHEN "MT_CUS" THEN
				SELECT DISTINCT T1.var_id AS 'Customer ID', T1.var_name AS 'Customer Name', T1.var_name_other AS 'Customer Name (Second Language)', T1.var_cur AS 'Currency Code', T1.var_accid AS 'Receivable Account', T1.var_pricecode AS 'Price Code',
				T1.var_groupid AS 'Customer Group Code', T1.var_term AS 'Payment Term Code', T1.var_pmttype AS 'Payment Method Code', T1.dou_limit AS 'Credit Limit', T1.var_name_con AS 'Contact Name', T1.var_tel1 AS 'Telephone 1', T1.var_tel2 AS 'Telephone 2', 
				T1.var_email AS 'Email', T1.var_fax AS 'Fax', T1.var_web AS 'Website', T1.var_desc AS 'Note', T1.var_tax AS 'Tax Number', T1.var_img AS 'Profile Picture', 
				T1.var_addr AS 'Invoice to Address', T1.var_addr1 AS 'House No', T1.var_addr2 AS 'Street', T1.var_addr3 AS 'Village', T1.var_addr4 AS 'Commune', T1.var_addr5 AS 'District / City', T1.var_addr6 AS 'Province / Capital', T1.var_addr7 AS 'Country / Region', 
				COALESCE(T1.tin_so, 0) AS 'Quotation / Sales Order picks up price from latest order', COALESCE(T1.tin_si, 0) AS 'Sales Invoicing picks up price from latest invoice', 
				COALESCE(tin_overcredit, 0) AS 'Allow issuing invoice over credit limit', COALESCE(tin_overduedate, 0) AS 'Allow issuing invoice over due date', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_cus T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_VEN" THEN
				SELECT DISTINCT T1.var_id AS 'Vendor ID', T1.var_name AS 'Vendor Name', T1.var_name_other AS 'Vendor Name (Second Language)', T1.var_cur AS 'Currency Code', T1.var_accid AS 'Payable Account',
				T1.var_groupid AS 'Vendor Group Code', T1.var_term AS 'Payment Term Code', T1.var_pmttype AS 'Payment Method Code', T1.dou_limit AS 'Credit Limit', T1.var_name_con AS 'Contact Name', T1.var_tel1 AS 'Telephone 1', T1.var_tel2 AS 'Telephone 2', 
				T1.var_email AS 'Email', T1.var_fax AS 'Fax', T1.var_web AS 'Website', T1.var_desc AS 'Note', T1.var_tax AS 'Tax Number', T1.var_img AS 'Profile Picture', 
				T1.var_addr AS 'Invoice to Address', T1.var_addr1 AS 'House No', T1.var_addr2 AS 'Street', T1.var_addr3 AS 'Village', T1.var_addr4 AS 'Commune', T1.var_addr5 AS 'District / City', T1.var_addr6 AS 'Province / Capital', T1.var_addr7 AS 'Country / Region', 
				COALESCE(T1.tin_po, 0) AS 'Purchase Order picks up price from latest order', COALESCE(T1.tin_pi, 0) AS 'Purchase / Receive Inventory picks up price from latest invoice', 
				COALESCE(tin_overcredit, 0) AS 'Allow issuing invoice over credit limit', COALESCE(tin_overduedate, 0) AS 'Allow issuing invoice over due date', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_ven T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_EMP" THEN
				SELECT DISTINCT T1.var_id AS 'Employee ID', T1.var_name AS 'Employee Name', T1.var_name_other AS 'Employee Name (Second Language)', T1.var_cur AS 'Currency Code', T1.var_posid AS 'Position Code',
				T1.var_groupid AS 'Employee Group Code', T1.var_name_con AS 'Contact Name', T1.var_tel1 AS 'Telephone 1', T1.var_tel2 AS 'Telephone 2', 
				T1.var_email AS 'Email', T1.var_fax AS 'Fax', T1.var_web AS 'Website', T1.var_desc AS 'Note', T1.var_cardid AS 'Identity Card', T1.var_img AS 'Profile Picture', 
				T1.var_addr AS 'Invoice to Address', T1.var_addr1 AS 'House No', T1.var_addr2 AS 'Street', T1.var_addr3 AS 'Village', T1.var_addr4 AS 'Commune', T1.var_addr5 AS 'District / City', T1.var_addr6 AS 'Province / Capital', T1.var_addr7 AS 'Country / Region', 
				COALESCE(T1.tin_rep, 0) AS 'Employee is sales representative', COALESCE(tin_inactive, 0) AS 'Inactive' 
				FROM ame_mt_emp T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
		WHEN "MT_ITE_IMP" THEN
				SELECT GROUP_CONCAT(T1.var_header) INTO P_H FROM ame_sys_add_cost T1 ORDER BY T1.int_line;
				SET @SQL_EXC = CONCAT("SELECT T1.var_id AS 'Item ID', T1.var_itemcode AS 'Item Code', T1.var_barcode AS 'Barcode', T1.var_type AS 'Item Type', T1.var_name AS 'Item Name', T1.var_name_other AS 'Item Name (Second Language)', 
				T1.var_uomid AS 'UOM Code', T1.var_groupid AS 'Item Group Code', T1.dou_order_pt AS 'Reorder Point', T1.dou_order_qty AS 'Reorder Quantity', T1.var_img AS 'Image', 
				T1.var_desc_pur AS 'Purchase Description', T1.var_desc_sal AS 'Sales Description', T1.var_desc AS 'Additional Information',
				COALESCE(T1.tin_no_disc, 0) AS 'No Sales Discount', COALESCE(T1.tin_no_sal, 0) AS 'No Sellable', COALESCE(T1.tin_vat, 0) AS 'VAT', COALESCE(T1.tin_wht, 0) AS 'WHT', COALESCE(T1.tin_sct, 0) AS 'SCT', COALESCE(tin_inactive, 0) AS 'Inactive', T1.var_venid AS 'Preffered Vendor ID', 
				T2.`Inventory` AS `Inventory (Asset)`, T2.`Revenue` AS `Sale`, T2.`Return Revenue` AS `Sale Return`, T2.`COGS` AS `Cost of Goods Sold (COGS)`, T2.`AC1` AS `Additional COGS: ", AME_FUN_SPLIT_STRING(P_H, ',', 1),"`, T2.`AC2` AS `Additional COGS: ", AME_FUN_SPLIT_STRING(P_H, ',', 2),"`, T2.`AC3` AS `Additional COGS: ", AME_FUN_SPLIT_STRING(P_H, ',', 3),"`,	T2.`AC4` AS `Additional COGS: ", AME_FUN_SPLIT_STRING(P_H, ',', 4),"`, T2.`AC5` AS `Additional COGS: ", AME_FUN_SPLIT_STRING(P_H, ',', 5),"`, T2.`Internal Usage` AS `Internal Usage Expense`, T2.`Inventory Adjustment` AS `Inventory Adjustment Expense`
				FROM ame_mt_ite T1 LEFT JOIN (SELECT 
				T1.var_groupid,
				SUM(T1.`Inventory`) AS `Inventory`, 
				SUM(T1.`Revenue`) AS `Revenue`, 
				SUM(T1.`Return Revenue`) AS `Return Revenue`,
				SUM(T1.`COGS`) AS `COGS`,
				SUM(T1.`AC1`) AS `AC1`,
				SUM(T1.`AC2`) AS `AC2`,
				SUM(T1.`AC3`) AS `AC3`,
				SUM(T1.`AC4`) AS `AC4`,
				SUM(T1.`AC5`) AS `AC5`,
				SUM(T1.`Inventory Adjustment`) AS `Inventory Adjustment`,
				SUM(T1.`Internal Usage`) AS `Internal Usage`
				FROM
				(SELECT 
				var_groupid,
				IF(CONCAT(var_type,var_subgroupid) = 'ICNA',var_accid, NULL) AS 'Inventory',
				IF(CONCAT(var_type,var_subgroupid) = 'REVENUENA',var_accid,NULL) AS 'Revenue',
				IF(CONCAT(var_type,var_subgroupid) = 'REVENUE_RETURNNA',var_accid, NULL) AS 'Return Revenue',
				IF(CONCAT(var_type,var_subgroupid) = 'COGSNA',var_accid, NULL) AS 'COGS',
				IF(CONCAT(var_type,var_subgroupid) = 'COGSAC1',var_accid,NULL ) AS 'AC1',
				IF(CONCAT(var_type,var_subgroupid) = 'COGSAC2',var_accid, NULL) AS 'AC2',
				IF(CONCAT(var_type,var_subgroupid) = 'COGSAC3',var_accid, NULL) AS 'AC3',
				IF(CONCAT(var_type,var_subgroupid) = 'COGSAC4',var_accid, NULL) AS 'AC4',
				IF(CONCAT(var_type,var_subgroupid) = 'COGSAC5',var_accid, NULL) AS 'AC5',
				IF(CONCAT(var_type,var_subgroupid) = 'IC-IANA',var_accid, NULL) AS 'Inventory Adjustment',
				IF(CONCAT(var_type,var_subgroupid) = 'IC-IUNA',var_accid, NULL) AS 'Internal Usage'
				FROM ame_mt_acc_set T1 
				WHERE T1.var_type IN('IC', 'IC-IA', 'IC-IU', 'COGS', 'REVENUE', 'REVENUE_RETURN') AND T1.var_groupid <> 'DEFAULT' AND T1.var_subgroupid <> 'DEFAULT') T1
				GROUP BY T1.var_groupid) AS T2 ON T1.var_id = T2.var_groupid 
				WHERE IF('", P2, "' = 'ALL', TRUE, T1.var_id >= '", P2, "') AND IF('", P3, "' = 'ALL', TRUE, T1.var_id <= '", P3, "')
				GROUP BY T1.var_id
				ORDER BY T1.var_id");
				PREPARE STM FROM @SQL_EXC;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;

				SELECT T1.var_id AS `Item ID`, T2.var_name AS `Item Name`, T1.var_uomid AS `UOM Code`, T1.dou_conv AS `Conversion`, COALESCE(T1.tin_pur, 0) AS `Purchases`, COALESCE(T1.tin_sal, 0) AS `Sales` 
				FROM ame_mt_ite_uom T1 INNER JOIN ame_mt_ite T2 ON (T1.var_id = T2.var_id AND T1.var_uomid <> T2.var_uomid) 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id, T1.int_line;

				SELECT T1.var_id AS `Assembly Item ID`, T2.var_name AS `Assembly Item Name`, T1.var_uomid_ass AS `Assembly UOM Code`, T1.var_itemid AS `Component Item ID`, T3.var_name AS `Component Item Name`, T1.var_uomid AS `Component UOM Code`, T1.dou_qty AS `Quantity` 
				FROM ame_mt_ite_ass T1 
				LEFT JOIN ame_mt_ite T2 ON T1.var_id = T2.var_id 
				LEFT JOIN ame_mt_ite T3 ON T1.var_itemid = T3.var_id 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id, T1.int_line;
				
				SET @SQL1 = "";
				SELECT COALESCE(GROUP_CONCAT(DISTINCT CONCAT("CASE T1.var_id WHEN """ , T1.var_id,""" THEN T2.dou_price ELSE 0 END AS `", T1.var_id,"`") ORDER BY T1.var_id), "")
				INTO @SQL1 FROM ame_mt_pri_cod T1 LEFT JOIN ame_mt_ite_pri T2 ON T1.var_id = T2.var_pricecode;
								
				SET @SQL2 = "";
				SELECT COALESCE(GROUP_CONCAT(DISTINCT CONCAT("COALESCE(SUM(`" , T1.var_id,"`), 0) AS `", T1.var_id,"`") ORDER BY T1.`var_id`), "")
				INTO @SQL2 FROM ame_mt_pri_cod T1 LEFT JOIN ame_mt_ite_pri T2 ON T1.var_id = T2.var_pricecode;
								
				SET @SQL3 = "";
				SET @SQL3 = CONCAT("SELECT T1.var_id AS `Item ID`, T1.var_name AS `Item Name`, IF(ISNULL(T2.var_uomid), T1.var_uomid, T2.var_uomid) AS `UOM Code`, T2.var_cur AS `Currency Code`,", @SQL2, " FROM ame_mt_ite T1 INNER JOIN (SELECT T2.var_pricecode, T2.var_cur, T2.var_id, T2.var_uomid, T2.int_line, ", @SQL1, " FROM ame_mt_pri_cod T1 LEFT JOIN ame_mt_ite_pri T2 ON T1.var_id = T2.var_pricecode) AS T2 ON T1.var_id = T2.var_id 
				WHERE IF('", P2, "' = 'ALL', TRUE, T1.var_id >= '", P2, "') AND IF('", P3, "' = 'ALL', TRUE, T1.var_id <= '", P3, "')
				GROUP BY T1.var_id, T2.var_uomid, T2.var_cur 		
				ORDER BY T1.var_id, T2.int_line");
				PREPARE STM FROM @SQL3;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;
		WHEN "AP_PO" THEN
				SELECT T1.var_id AS `Entry No`, DATE_FORMAT(T1.dat_date, "%d-%b-%Y") AS `Date (dd-MMM-YYYY)`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_venid AS `Vendor ID`, T1.var_claid AS `Class ID`, T1.var_term AS `Term`, DATE_FORMAT(T1.dat_due, "%d-%b-%Y") AS `Due Date (dd-MMM-YYYY)`, T1.var_shipid AS `Ship To Address ID`, T1.var_shipaddr AS `Ship To Address`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_taxper AS `VAT %`, T1.dou_taxper1 AS `WHT %`, T1.var_cur AS `Currency ID`, T1.dou_rate AS `Exchange Rate` 
				FROM ame_ap_po T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
				SELECT T1.var_id AS `Entry No`, T1.var_desc AS `Description`, T1.var_itemid AS `Item ID`, T1.var_locid AS `Location ID`, T1.var_claid AS `Class ID`, T1.var_uomid AS `Uom ID`, T1.dou_qty AS `Quantity`, T1.dou_foc AS `FOC`, T1.dou_price AS `Cost`, T1.dou_sub AS `Sub Total`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_net AS `Grand Total`, COALESCE(T1.tin_vat, 0) AS `VAT (0 OR 1)`, COALESCE(T1.tin_wht, 0) AS `WHT (0 OR 1)`, T1.int_poline AS `PO Line No` 
				FROM ame_ap_po_det T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id, T1.int_line;
		WHEN "AP_IN" THEN
				SELECT T1.var_id AS `Entry No`, DATE_FORMAT(T1.dat_date, "%d-%b-%Y") AS `Date (dd-MMM-YYYY)`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_venid AS `Vendor ID`, T1.var_claid AS `Class ID`, T1.var_term AS `Term`, DATE_FORMAT(T1.dat_due, "%d-%b-%Y") AS `Due Date (dd-MMM-YYYY)`, T1.var_po AS `PO Number`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_taxper AS `VAT %`, T1.dou_taxper1 AS `WHT %`, T1.var_cur AS `Currency ID`, T1.dou_rate AS `Exchange Rate` 
				FROM ame_ap_in T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
				SELECT T1.var_id AS `Entry No`, T1.var_desc AS `Description`, T1.var_accid AS `Account ID`, T1.var_itemid AS `Item ID`, T1.var_locid AS `Location ID`, T1.var_claid AS `Class ID`, T1.var_uomid AS `Uom ID`, T1.dou_qty AS `Quantity`, T1.dou_foc AS `FOC`, T1.dou_price AS `Cost`, T1.dou_sub AS `Sub Total`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_net AS `Grand Total`, COALESCE(T1.tin_vat, 0) AS `VAT (0 OR 1)`, COALESCE(T1.tin_wht, 0) AS `WHT (0 OR 1)`, T1.var_poid AS `PO Number`, T1.int_poline AS `PO Line No` 
				FROM ame_ap_in_det T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id, T1.int_line;
		WHEN "AP_DN" THEN
				SELECT T1.var_id AS `Entry No`, DATE_FORMAT(T1.dat_date, "%d-%b-%Y") AS `Date (dd-MMM-YYYY)`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_venid AS `Vendor ID`, T1.var_claid AS `Class ID`, T1.var_term AS `Term`, DATE_FORMAT(T1.dat_due, "%d-%b-%Y") AS `Due Date (dd-MMM-YYYY)`, T1.var_po AS `PO Number`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_taxper AS `VAT %`, T1.dou_taxper1 AS `WHT %`, T1.var_cur AS `Currency ID`, T1.dou_rate AS `Exchange Rate` 
				FROM ame_ap_dn T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
				SELECT T1.var_id AS `Entry No`, T1.var_desc AS `Description`, T1.var_itemid AS `Item ID`, T1.var_locid AS `Location ID`, T1.var_claid AS `Class ID`, T1.var_uomid AS `Uom ID`, T1.dou_qty AS `Quantity`, T1.dou_foc AS `FOC`, T1.dou_price AS `Cost`, T1.dou_sub AS `Sub Total`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_net AS `Grand Total`, COALESCE(T1.tin_vat, 0) AS `VAT (0 OR 1)`, COALESCE(T1.tin_wht, 0) AS `WHT (0 OR 1)` 
				FROM ame_ap_dn_det T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id, T1.int_line;
		WHEN "AR_QUO" THEN
				SELECT T1.var_id AS `Entry No`, DATE_FORMAT(T1.dat_date, "%d-%b-%Y") AS `Date (dd-MMM-YYYY)`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_cusid AS `Customer ID`, T1.var_empid AS `Employee ID`, T1.var_pricecode AS `Price Code`, T1.var_claid AS `Class ID`, T1.var_term AS `Term`, DATE_FORMAT(T1.dat_due, "%d-%b-%Y") AS `Due Date (dd-MMM-YYYY)`, DATE_FORMAT(T1.dat_from, "%d-%b-%Y") AS `Valid From (dd-MMM-YYYY)`, DATE_FORMAT(T1.dat_to, "%d-%b-%Y") AS `Valid To (dd-MMM-YYYY)`, T1.var_shipid AS `Ship To Address ID`, T1.var_shipaddr AS `Ship To Address`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_taxper AS `VAT %`, T1.dou_taxper1 AS `SCT %`, T1.var_cur AS `Currency ID`, T1.dou_rate AS `Exchange Rate`
				FROM ame_ar_quo T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
				SELECT T1.var_id AS `Entry No`, T1.var_desc AS `Description`, T1.var_accid AS `Account ID`, T1.var_itemid AS `Item ID`, T1.var_locid AS `Location ID`, T1.var_claid AS `Class ID`, T1.var_uomid AS `Uom ID`, T1.dou_qty AS `Quantity`, T1.dou_foc AS `FOC`, T1.dou_price AS `Price`, T1.dou_pricecom AS `Comparative Price`, T1.dou_sub AS `Sub Total`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_net AS `Grand Total`, COALESCE(T1.tin_vat, 0) AS `VAT (0 OR 1)`, COALESCE(T1.tin_wht, 0) AS `SCT (0 OR 1)` 
				FROM ame_ar_quo_det T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id, T1.int_line;
		WHEN "AR_SO" THEN
				SELECT T1.var_id AS `Entry No`, DATE_FORMAT(T1.dat_date, "%d-%b-%Y") AS `Date (dd-MMM-YYYY)`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_cusid AS `Customer ID`, T1.var_empid AS `Employee ID`, T1.var_pricecode AS `Price Code`, T1.var_claid AS `Class ID`, T1.var_term AS `Term`, DATE_FORMAT(T1.dat_due, "%d-%b-%Y") AS `Due Date (dd-MMM-YYYY)`, T1.var_po AS `PO Number`, T1.var_so AS `Quotation / SO Number`, T1.var_shipid AS `Ship To Address ID`, T1.var_shipaddr AS `Ship To Address`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_taxper AS `VAT %`, T1.dou_taxper1 AS `SCT %`, T1.var_cur AS `Currency ID`, T1.dou_rate AS `Exchange Rate` 
				FROM ame_ar_so T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
				SELECT T1.var_id AS `Entry No`, T1.var_desc AS `Description`, T1.var_accid AS `Account ID`, T1.var_itemid AS `Item ID`, T1.var_locid AS `Location ID`, T1.var_claid AS `Class ID`, T1.var_uomid AS `Uom ID`, T1.dou_qty AS `Quantity`, T1.dou_foc AS `FOC`, T1.dou_price AS `Price`, T1.dou_pricecom AS `Comparative Price`, T1.dou_sub AS `Sub Total`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_net AS `Grand Total`, COALESCE(T1.tin_vat, 0) AS `VAT (0 OR 1)`, COALESCE(T1.tin_wht, 0) AS `SCT (0 OR 1)` 
				FROM ame_ar_so_det T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id, T1.int_line;
		WHEN "AR_IN" THEN
				SELECT T1.var_id AS `Entry No`, DATE_FORMAT(T1.dat_date, "%d-%b-%Y") AS `Date (dd-MMM-YYYY)`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_cusid AS `Customer ID`, T1.var_empid AS `Employee ID`, T1.var_pricecode AS `Price Code`, T1.var_claid AS `Class ID`, T1.var_term AS `Term`, DATE_FORMAT(T1.dat_due, "%d-%b-%Y") AS `Due Date (dd-MMM-YYYY)`, T1.var_po AS `PO Number`, T1.var_so AS `Quotation / SO Number`, T1.var_shipid AS `Ship To Address ID`, T1.var_shipaddr AS `Ship To Address`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_taxper AS `VAT %`, T1.dou_taxper1 AS `SCT %`, T1.var_cur AS `Currency ID`, T1.dou_rate AS `Exchange Rate`, T1.var_prefix AS `Prefix ID` 
				FROM ame_ar_in T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
				SELECT T1.var_id AS `Entry No`, T1.var_desc AS `Description`, T1.var_accid AS `Account ID`, T1.var_itemid AS `Item ID`, T1.var_locid AS `Location ID`, T1.var_claid AS `Class ID`, T1.var_uomid AS `Uom ID`, T1.dou_qty AS `Quantity`, T1.dou_foc AS `FOC`, T1.dou_price AS `Price`, T1.dou_pricecom AS `Comparative Price`, T1.dou_sub AS `Sub Total`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_net AS `Grand Total`, COALESCE(T1.tin_vat, 0) AS `VAT (0 OR 1)`, COALESCE(T1.tin_wht, 0) AS `SCT (0 OR 1)` 
				FROM ame_ar_in_det T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id, T1.int_line;
		WHEN "AR_CN" THEN
				SELECT T1.var_id AS `Entry No`, DATE_FORMAT(T1.dat_date, "%d-%b-%Y") AS `Date (dd-MMM-YYYY)`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_cusid AS `Customer ID`, T1.var_empid AS `Employee ID`, T1.var_pricecode AS `Price Code`, T1.var_claid AS `Class ID`, T1.var_term AS `Term`, DATE_FORMAT(T1.dat_due, "%d-%b-%Y") AS `Due Date (dd-MMM-YYYY)`, T1.var_po AS `PO Number`, T1.var_so AS `Quotation / SO Number`, T1.var_shipid AS `Ship To Address ID`, T1.var_shipaddr AS `Ship To Address`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_taxper AS `VAT %`, T1.dou_taxper1 AS `SCT %`, T1.var_cur AS `Currency ID`, T1.dou_rate AS `Exchange Rate`
				FROM ame_ar_cn T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
				SELECT T1.var_id AS `Entry No`, T1.var_desc AS `Description`, T1.var_accid AS `Account ID`, T1.var_itemid AS `Item ID`, T1.var_locid AS `Location ID`, T1.var_claid AS `Class ID`, T1.var_uomid AS `Uom ID`, T1.dou_qty AS `Quantity`, T1.dou_foc AS `FOC`, T1.dou_price AS `Price`, T1.dou_pricecom AS `Comparative Price`, T1.dou_sub AS `Sub Total`, T1.dou_disper AS `Discount %`, T1.dou_disdol AS `Discount $`, T1.dou_net AS `Grand Total`, COALESCE(T1.tin_vat, 0) AS `VAT (0 OR 1)`, COALESCE(T1.tin_wht, 0) AS `SCT (0 OR 1)` 
				FROM ame_ar_cn_det T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id, T1.int_line;
		WHEN "IC_TF" THEN
				SELECT T1.var_id AS `Entry No`, DATE_FORMAT(T1.dat_date, "%d-%b-%Y") AS `Date (dd-MMM-YYYY)`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_claid AS `Class ID`
				FROM ame_ic_tf T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id;
				SELECT T1.var_id AS `Entry No`, T1.var_desc AS `Description`, T1.var_itemid AS `Item ID`, T1.var_locid AS `From Location ID`, T1.var_tolocid AS `To Location ID`, T1.var_claid AS `Class ID`, T1.var_uomid AS `Uom ID`, T1.dou_qty AS `Quantity` 
				FROM ame_ic_tf_det T1 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T1.var_id <= P3)
				ORDER BY T1.var_id, T1.int_line;
		WHEN "MT_OPT" THEN
				SELECT 
				T.var_id AS 'Field ID', 
				COALESCE(T.var_name, '') AS 'Field Name', 
				COALESCE(T.var_type,'') AS 'Type',
				COALESCE(T.var_desc,'') AS 'Description',
				COALESCE(T.int_sort,0) AS 'Sort Order', 
				T.tin_item AS 'Item',
				T.tin_emp AS 'Employee',
				T.tin_cus AS 'Customer',
				T.tin_ven AS 'Vendor',
				T.tin_req AS 'Required', 
				T.tin_inactive AS 'Inactive'  
				FROM ame_mt_of T
				WHERE IF(P2 = 'ALL', TRUE, T.var_id >= P2) AND IF(P3 = 'ALL', TRUE, T.var_id <= P3)
				ORDER BY T.var_id;
		ELSE
				SET @NULL = NULL;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_ACC_DEF`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_ACC_DEF`(P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE VC_ACC VARCHAR(100) DEFAULT "";
		DECLARE VC_ACC_NAME VARCHAR(250) DEFAULT "";
		DECLARE C1 CURSOR FOR SELECT T1.var_accid, COALESCE(T2.var_name, '') AS var_name FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_type = P1 AND T1.var_groupid = P2 AND T1.var_subgroupid = P3;
		OPEN C1;
				IF FOUND_ROWS() > 0 THEN 
						FETCH C1 INTO VC_ACC, VC_ACC_NAME; 		
				ELSE
						BEGIN
								DECLARE C2 CURSOR FOR SELECT T1.var_accid, COALESCE(T2.var_name, '') AS var_name FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_type = P1 AND T1.var_groupid = 'DEFAULT' AND T1.var_subgroupid = 'DEFAULT';
								OPEN C2;
										IF FOUND_ROWS() > 0 THEN FETCH C2 INTO VC_ACC, VC_ACC_NAME; END IF;	
								CLOSE C2;
						END;
				END IF;
		CLOSE C1;
		SELECT VC_ACC, VC_ACC_NAME;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_ADAPTER`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_ADAPTER`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		CASE P1
		WHEN "FL_USER_PER" THEN
				SELECT int_id, var_modid, tin_per1, tin_per2, tin_per3, tin_per4, tin_per5, tin_per6, tin_per7, tin_per8 FROM ame_user_per_frm LIMIT 0;
				SELECT int_id, var_modid, tin_per1 FROM ame_user_per_rep LIMIT 0;
				SELECT int_id, var_modid, tin_per1 FROM ame_user_per_oth LIMIT 0;
		WHEN "FL_USER_PRO" THEN
				SELECT var_id, var_modid, int_day, tin_pro1 FROM ame_user_pro_rem LIMIT 0;
				SELECT var_id, var_modid, var_type FROM ame_user_pro_def_pre LIMIT 0;
		WHEN "MT_GEN_SET" THEN
				SELECT var_type, var_groupid, var_subgroupid,var_accid FROM ame_mt_acc_set LIMIT 0;
				SELECT var_id, var_name, var_desc, var_header FROM ame_sys_add_cost LIMIT 0;
				SELECT int_id, var_name, tin_inactive FROM ame_sys_pro_hie LIMIT 0;
				#SELECT int_id, int_line, var_datatype,var_value,var_desc,tin_ven,tin_cus,tin_emp,tin_item,tin_req FROM ame_sys_of LIMIT 0;
		WHEN "MT_DEC" THEN
				SELECT var_id, int_qty, int_cost, int_price, int_amt, int_per, int_rate FROM ame_sys_decimal LIMIT 0;
		WHEN "MT_LOCK" THEN
				SELECT var_id, int_year, int_month, dat_start, dat_end, tin_lock FROM ame_sys_lock LIMIT 0;
		WHEN "MT_PREFIX" THEN
				SELECT var_modid, var_desc, var_id, int_length, int_lastid FROM ame_sys_prefix LIMIT 0;
				SELECT var_modid, var_desc, var_id, int_length, int_lastid, var_type FROM ame_sys_prefix_custom LIMIT 0;
		WHEN "MT_ITE" THEN
				SELECT var_type, var_groupid, var_subgroupid, var_accid FROM ame_mt_acc_set LIMIT 0;
				SELECT var_id, var_uomid, int_line, dou_conv, tin_pur, tin_sal, tin_def FROM ame_mt_ite_uom LIMIT 0;
				SELECT var_id, var_itemid, var_uomid_ass, var_uomid, int_line, dou_qty FROM ame_mt_ite_ass LIMIT 0;
				SELECT var_id, var_pricecode, var_uomid, var_cur, int_line, dou_price FROM ame_mt_ite_pri LIMIT 0;
				SELECT var_id, var_pricecode, var_uomid, var_cur, dat_date, dou_price FROM ame_mt_ite_pri_his LIMIT 0;
		WHEN "MT_ITE_IMP" THEN
				SELECT T1.var_id, T1.var_itemcode, T1.var_barcode, T1.var_type, T1.var_name, T1.var_name_other, T1.var_uomid, T1.var_groupid, T1.dou_order_pt, T1.dou_order_qty, T1.var_img, 
				T1.var_desc_pur, T1.var_desc_sal, T1.var_desc, T1.tin_no_disc, T1.tin_no_sal, T1.tin_vat, T1.tin_wht, T1.tin_sct, tin_inactive, T1.var_venid, T1.var_create, T1.var_modify, T1.dat_create, T1.dat_modify FROM ame_mt_ite T1 LIMIT 0;
				SELECT var_id, var_uomid, int_line, dou_conv, tin_pur, tin_sal, tin_def FROM ame_mt_ite_uom LIMIT 0;
				SELECT var_type, var_groupid, var_subgroupid, var_accid FROM ame_mt_acc_set LIMIT 0;
				SELECT var_id, var_uomid, int_line, dou_conv, tin_pur, tin_sal, tin_def FROM ame_mt_ite_uom LIMIT 0;
				SELECT var_id, var_itemid, var_uomid_ass, var_uomid, int_line, dou_qty FROM ame_mt_ite_ass LIMIT 0;
				SELECT var_id, var_pricecode, var_uomid, var_cur, int_line, dou_price FROM ame_mt_ite_pri LIMIT 0;
				SELECT var_id, var_pricecode, var_uomid, var_cur, dat_date, dou_price FROM ame_mt_ite_pri_his LIMIT 0;
		WHEN "MT_ITE_PRI" THEN
				SELECT var_id, var_pricecode, var_uomid, var_cur, int_line, dou_price FROM ame_mt_ite_pri LIMIT 0;
				SELECT var_id, var_pricecode, var_uomid, var_cur, dat_date, dou_price FROM ame_mt_ite_pri_his LIMIT 0;
		WHEN "MT_VEN" THEN
				SELECT DISTINCT T1.var_id, T1.var_name, T1.var_name_other, T1.var_cur, T1.var_accid,
				T1.var_groupid, T1.var_term, T1.var_pmttype, T1.dou_limit, T1.var_name_con, T1.var_tel1, T1.var_tel2, 
				T1.var_email, T1.var_fax, T1.var_web, T1.var_desc, T1.var_tax, T1.var_img, 
				T1.var_addr, T1.var_addr1, T1.var_addr2, T1.var_addr3, T1.var_addr4, T1.var_addr5, T1.var_addr6, T1.var_addr7, 
				T1.tin_po, T1.tin_pi, T1.tin_overcredit, T1.tin_overduedate, T1.tin_inactive, T1.var_create, T1.var_modify, T1.dat_create, T1.dat_modify FROM ame_mt_ven T1 LIMIT 0;
				SELECT var_type, var_groupid, var_subgroupid, var_accid FROM ame_mt_acc_set LIMIT 0;
		WHEN "MT_CUS" THEN
				SELECT DISTINCT T1.var_id, T1.var_name, T1.var_name_other, T1.var_cur, T1.var_accid, T1.var_pricecode,
				T1.var_groupid, T1.var_term, T1.var_pmttype, T1.dou_limit, T1.var_name_con, T1.var_tel1, T1.var_tel2, 
				T1.var_email, T1.var_fax, T1.var_web, T1.var_desc, T1.var_tax, T1.var_img, 
				T1.var_addr, T1.var_addr1, T1.var_addr2, T1.var_addr3, T1.var_addr4, T1.var_addr5, T1.var_addr6, T1.var_addr7, 
				T1.tin_so, T1.tin_si, T1.tin_overcredit, T1.tin_overduedate, T1.tin_inactive, T1.var_create, T1.var_modify, T1.dat_create, T1.dat_modify FROM ame_mt_cus T1 LIMIT 0;
				SELECT var_type, var_groupid, var_subgroupid, var_accid FROM ame_mt_acc_set LIMIT 0;
		WHEN "MT_EMP" THEN
				SELECT DISTINCT T1.var_id, T1.var_name, T1.var_name_other, T1.var_cur, T1.var_posid,
				T1.var_groupid, T1.var_name_con, T1.var_tel1, T1.var_tel2, 
				T1.var_email, T1.var_fax, T1.var_web, T1.var_desc, T1.var_cardid, T1.var_img, 
				T1.var_addr, T1.var_addr1, T1.var_addr2, T1.var_addr3, T1.var_addr4, T1.var_addr5, T1.var_addr6, T1.var_addr7, 
				T1.tin_rep, T1.tin_inactive, T1.var_create, T1.var_modify, T1.dat_create, T1.dat_modify FROM ame_mt_emp T1 LIMIT 0;
		WHEN "MT_COM" THEN
				SELECT var_id, int_line, var_accid, var_name, var_desc FROM ame_mt_com_acc LIMIT 0;
		WHEN "MT_ACC" THEN
				SELECT var_id, var_name, int_typeid, var_desc, tin_inactive FROM ame_mt_acc LIMIT 0;
		WHEN "MT_CUR" THEN
				SELECT var_id, var_name, var_symbol, var_desc, tin_inactive FROM ame_mt_cur LIMIT 0;
		WHEN "MT_CLA" THEN
				SELECT var_id, var_name, var_desc, tin_inactive FROM ame_mt_cla LIMIT 0;
		WHEN "MT_UOM" THEN
				SELECT var_id, var_name, var_desc, tin_inactive FROM ame_mt_uom LIMIT 0;
		WHEN "MT_LOC" THEN
				SELECT var_id, var_name, var_addr, var_desc, tin_inactive FROM ame_mt_loc LIMIT 0;
		WHEN "MT_POS" THEN
				SELECT var_id, var_name, var_addr, var_desc, tin_inactive FROM ame_mt_pos LIMIT 0;
		WHEN "MT_PRI_COD" THEN
				SELECT var_id, var_name, var_desc, tin_reportprice, tin_inactive FROM ame_mt_pri_cod LIMIT 0;
		WHEN "MT_CUS_GRO" THEN
				SELECT var_id, var_name, var_desc, var_accid, tin_inactive FROM ame_mt_cus_gro LIMIT 0;
		WHEN "MT_SHIP_ADDR" THEN
				SELECT T1.var_num, T1.var_type, T1.var_id, T1.var_name, T1.var_addr, T1.var_addr1, T1.var_addr2, T1.var_addr3, T1.var_addr4, T1.var_addr5, T1.var_addr6, T1.var_addr7, T1.tin_default FROM ame_mt_ship_addr T1 LIMIT 0;
		WHEN "MT_VEN_GRO" THEN
				SELECT var_id, var_name, var_desc, var_accid, tin_inactive FROM ame_mt_ven_gro LIMIT 0;
		WHEN "MT_EMP_GRO" THEN
				SELECT var_id, var_name, var_desc, tin_inactive FROM ame_mt_emp_gro LIMIT 0;
		WHEN "MT_ITE_GRO" THEN
				SELECT var_id, var_name, var_desc, var_accid_ic, var_accid_pl, var_accid_pl_ret, var_accid_cogs, var_accid_ac1, var_accid_ac2, var_accid_ac3, var_accid_ac4, var_accid_ac5, var_accid_ia, var_accid_iu, tin_inactive FROM ame_mt_ite_gro LIMIT 0;
		WHEN "MT_PMT_TERM" THEN
				SELECT T1.var_id, T1.var_name, T1.var_type, T1.int_daydue, T1.dou_disc, T1.int_daydisc, T1.int_dayminpay, T1.tin_inactive FROM ame_mt_pmt_term T1 LIMIT 0;
		WHEN "MT_PMT_METH" THEN
				SELECT var_id, var_name, var_desc, tin_inactive FROM ame_mt_pmt_meth LIMIT 0;
		WHEN "MT_HOL" THEN
				SELECT int_id, var_name, dat_date, tin_inactive FROM ame_mt_hol LIMIT 0;
		WHEN "AP_PO_PI" THEN
				SELECT var_id, var_type, int_line, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, dou_orderqty, dou_orderfoc, int_poline, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ap_po_det LIMIT 0;
		WHEN "AP_PO_AC" THEN
				SELECT var_id, int_line, var_itemid, var_desc, var_type, var_claid, int_batchno, var_locid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, tin_vat, tin_wht FROM ame_ap_po_det LIMIT 0;
		WHEN "AP_PO_GL" THEN
				SELECT var_id, var_type, int_line, var_accid, var_claid, var_desc, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, tin_vat, tin_wht FROM ame_ap_po_det LIMIT 0;
		WHEN "AP_IN_PI" THEN
				SELECT var_id, var_type, int_line, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, int_poline, var_poid, dou_orderqty, dou_orderfoc, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ap_in_det LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty, int_batchno FROM ame_sys_tem_ap_in LIMIT 0;
		WHEN "AP_IN_AC" THEN
				SELECT var_id, int_line, var_itemid, var_desc, var_type, var_claid, int_batchno, var_locid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, tin_vat, tin_wht, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet, int_poline, var_poid FROM ame_ap_in_det LIMIT 0;
		WHEN "AP_IN_AC_COST" THEN
				SELECT var_id, int_line, var_itemid, var_locid, var_claid, dou_ac1, dou_ac2, dou_ac3, dou_ac4, dou_ac5, dou_funac1, dou_funac2, dou_funac3, dou_funac4, dou_funac5, int_poline FROM ame_ap_in_ac LIMIT 0;
		WHEN "AP_IN_GL" THEN
				SELECT var_id, var_type, int_line, var_accid, var_claid, var_desc, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, tin_vat, tin_wht, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ap_in_det LIMIT 0;
		WHEN "AP_DN_PI" THEN
				SELECT var_id, var_type, int_line, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ap_dn_det LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM ame_sys_tem_ap_dn LIMIT 0;
		WHEN "AP_RET_PI" THEN
				SELECT var_id, var_type, int_line, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, int_batchno, int_poline, var_poid, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ap_ret_det LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM ame_sys_tem_ap_ret LIMIT 0;
		WHEN "AP_PY_PI" THEN
				SELECT var_id, int_line, var_docid, var_claid, var_desc, dou_disc, dou_amt, dou_app, dou_fundisc, dou_funamt, dou_funapp FROM ame_ap_py_det LIMIT 0;
				SELECT var_id, int_line, var_num, var_doctype, var_docid, var_claid, var_desc, dou_app, dou_funapp FROM ame_ap_py_app LIMIT 0;
		WHEN "AP_PY_AD" THEN
				SELECT var_id, int_line, var_num, var_doctype, var_docid, var_claid, var_desc, dou_app FROM ame_ap_py_app LIMIT 0;
		WHEN "AR_QUO_GS" THEN
				SELECT var_id, int_line, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, dou_orderqty, dou_orderfoc, int_poline, dou_pricecom, dou_funprice, dou_funpricecom, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_quo_det LIMIT 0;
		WHEN "AR_SO_GS" THEN
				SELECT var_id, int_line, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, dou_orderqty, dou_orderfoc, int_poline, var_poid, dou_pricecom, dou_funprice, dou_funpricecom, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_so_det LIMIT 0;
		WHEN "AR_IN_GS" THEN
				SELECT var_id, int_line, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, dou_orderqty, dou_orderfoc, int_poline, var_poid, var_potype, dou_pricecom, dou_funprice, dou_funpricecom, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_in_det LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM ame_sys_tem_ar_in LIMIT 0;
		WHEN "AR_IN_GL" THEN
				SELECT var_id, int_line, var_accid, var_claid, var_desc, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, tin_vat, tin_wht, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_in_det LIMIT 0;	
		WHEN "AR_RET_GS" THEN
				SELECT var_id, dou_pricecom, int_line, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, int_batchno, int_poline, var_poid, dou_funprice, dou_funpricecom, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_ret_det LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM ame_sys_tem_ar_ret LIMIT 0;
		WHEN "AR_CN_GS" THEN
				SELECT var_id, int_line, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, dou_orderqty, dou_orderfoc, int_poline, var_poid, var_potype, dou_pricecom, dou_funprice, dou_funpricecom, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_in_det LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM ame_sys_tem_ar_cn LIMIT 0;
		WHEN "AR_CN_GL" THEN
				SELECT var_id, int_line, var_accid, var_claid, var_desc, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, tin_vat, tin_wht, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_cn_det LIMIT 0;	
		WHEN "AR_SM_GS" THEN
				SELECT var_id, int_line, var_itemid, var_locid, var_uomid, var_desc, var_poid, dou_qty, dou_foc, int_poline FROM ame_ar_sm_det LIMIT 0;
		WHEN "AR_PY_PI" THEN
				SELECT var_id, int_line, var_docid, var_claid, var_desc, dou_disc, dou_amt, dou_app, dou_fundisc, dou_funamt, dou_funapp FROM ame_ar_py_det LIMIT 0;
				SELECT var_id, int_line, var_num, var_doctype, var_docid, var_claid, var_desc, dou_app, dou_funapp FROM ame_ar_py_app LIMIT 0;
		WHEN "AR_PY_SD" THEN
				SELECT var_id, int_id, dat_date, var_cusid, var_period, var_method, var_type, var_desc, dou_sch, dou_rate_per, dou_rate_penalty, int_term FROM ame_ar_py_sd LIMIT 0;
				SELECT var_id, int_id, int_line, dat_date, dou_sch, dou_amt, dou_rate_dol, dou_net, dou_balance, dou_penalty, int_day FROM ame_ar_py_sd_det LIMIT 0;
		WHEN "IC_TF_IT" THEN
				SELECT var_id, int_line, var_itemid, var_locid, var_claid, var_uomid, var_desc, var_tolocid, dou_qty, int_poline FROM ame_ic_tf_det LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_tolocid, var_uomid, dou_qty FROM ame_sys_tem_ic LIMIT 0;
		WHEN "IC_IU_IU" THEN
				SELECT T1.var_id, T1.int_line, T1.var_itemid, T1.var_desc, T1.var_type, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.var_accid, T1.var_appitemid, T1.var_appacid, T1.int_appbatchno, T1.var_applocid, T1.int_poline FROM ame_ic_iu_det T1 LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM ame_sys_tem_ic LIMIT 0;
		WHEN "IC_ICUOM_ICUOM" THEN
				SELECT T1.var_id, T1.int_line, T1.var_itemid, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.var_toitemid, T1.var_tolocid, T1.var_touomid, T1.var_convtype, T1.dou_conv, T1.int_poline FROM ame_ic_icuom_det T1 LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM ame_sys_tem_ic LIMIT 0;
		WHEN "IC_IA_IA" THEN
				SELECT T1.var_id, T1.int_line, T1.var_itemid, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.int_batchno, T1.dou_qty, T1.dou_price, T1.int_poline FROM ame_ic_ia_det T1 LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM ame_sys_tem_ic_ia LIMIT 0;
		WHEN "IC_IP_IP" THEN
				SELECT T1.var_id, T1.int_line, T1.var_type, T1.var_itemid, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.dou_price, T1.tin_pro, T1.int_poline FROM ame_ic_ip_det T1 LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM ame_sys_tem_ic_ip LIMIT 0;
		WHEN "GL_JE" THEN
				SELECT int_jid, int_line, var_accid, dat_date, var_claid, var_desc, var_drcr, dou_amt, dou_funamt FROM ame_gl_je_det LIMIT 0;
		WHEN "SYS_FEV" THEN
				SELECT var_name,var_text,int_sortkey,var_uid,var_tag FROM ame_sys_menu_fev LIMIT 0;
		WHEN "AP_PO" THEN
				SELECT var_id, var_type, var_venid, var_claid, var_term, var_ref, var_desc, var_shipid, var_shipaddr, var_create, var_modify, dat_create, dat_modify, dat_date, dat_due, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, var_cur, dou_rate, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ap_po LIMIT 0;
				SELECT var_id, var_type, int_line, var_accid, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, int_poline, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ap_po_det LIMIT 0;
		WHEN "AP_IN" THEN
				SELECT var_id, var_type, var_venid, var_claid, var_term, var_ref, var_desc, var_po, var_create, var_modify, dat_create, dat_modify, dat_date, dat_due, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, var_cur, dou_rate, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ap_in LIMIT 0;
				SELECT var_id, var_type, int_line, var_accid, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, var_poid, int_poline, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ap_in_det LIMIT 0;
		WHEN "AP_DN" THEN
				SELECT var_id, var_type, var_venid, var_claid, var_term, var_ref, var_desc, var_po, var_create, var_modify, dat_create, dat_modify, dat_date, dat_due, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, var_cur, dou_rate, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ap_dn LIMIT 0;
				SELECT var_id, var_type, int_line, var_accid, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, int_poline, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ap_dn_det LIMIT 0;
		WHEN "AR_QUO" THEN
				SELECT var_id, var_type, var_cusid, var_empid, var_pricecode, var_claid, var_term, var_ref, var_desc, var_shipid, var_shipaddr, var_create, var_modify, dat_create, dat_modify, dat_date, dat_due, dat_from, dat_to, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, var_cur, dou_rate, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_quo LIMIT 0;
				SELECT var_id, int_line, var_accid, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_pricecom, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, int_poline, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_quo_det LIMIT 0;
		WHEN "AR_SO" THEN
				SELECT var_id, var_type, var_cusid, var_empid, var_pricecode, var_claid, var_term, var_ref, var_desc, var_po, var_so, var_shipid, var_shipaddr, var_create, var_modify, dat_create, dat_modify, dat_date, dat_due, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, var_cur, dou_rate, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_so LIMIT 0;
				SELECT var_id, int_line, var_accid, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_pricecom, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, int_poline, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_so_det LIMIT 0;
		WHEN "AR_IN" THEN
				SELECT var_id, var_type, var_cusid, var_empid, var_pricecode, var_claid, var_term, var_ref, var_desc, var_po, var_so, var_shipid, var_shipaddr, var_prefix, var_create, var_modify, dat_create, dat_modify, dat_date, dat_due, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, var_cur, dou_rate, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_in LIMIT 0;
				SELECT var_id, int_line, var_accid, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_pricecom, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, int_poline, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_in_det LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM ame_sys_tem_ar_in LIMIT 0;
		WHEN "AR_CN" THEN
				SELECT var_id, var_type, var_cusid, var_empid, var_pricecode, var_claid, var_term, var_ref, var_desc, var_po, var_so, var_shipid, var_shipaddr, var_create, var_modify, dat_create, dat_modify, dat_date, dat_due, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, var_cur, dou_rate, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_cn LIMIT 0;
				SELECT var_id, int_line, var_accid, var_itemid, var_desc, var_locid, var_claid, var_uomid, dou_qty, dou_price, dou_pricecom, dou_sub, dou_disper, dou_disdol, dou_taxper, dou_taxdol, dou_taxper1, dou_taxdol1, dou_net, dou_foc, tin_vat, tin_wht, int_poline, dou_funprice, dou_funsub, dou_fundisdol, dou_funtaxdol, dou_funtaxdol1, dou_funnet FROM ame_ar_cn_det LIMIT 0;
		WHEN "IC_TF" THEN
				SELECT var_id, var_type, var_claid, var_ref, var_desc, var_create, var_modify, dat_create, dat_modify, dat_date, var_cur, dou_rate FROM ame_ic_tf LIMIT 0;
				SELECT var_id, int_line, var_itemid, var_desc, var_locid, var_tolocid, var_claid, var_uomid, dou_qty, int_poline FROM ame_ic_tf_det LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM ame_sys_tem_ic LIMIT 0;
		WHEN "MT_OPT" THEN
				SELECT int_id,var_name,var_type,var_desc,int_sort,tin_item,tin_emp,tin_cus,tin_ven,tin_req,tin_inactive FROM ame_mt_of LIMIT 0;
		WHEN "MT_OPT_VAL" THEN
				SELECT int_id,int_line,var_value FROM ame_mt_of_list LIMIT 0;
		WHEN "MT_OPT_VAL_SAVE" THEN
				SELECT int_id,var_modid,var_num,var_value FROM ame_mt_of_value LIMIT 0;
		WHEN "POS_PMT_MET" THEN
				SELECT var_id,var_name,var_cur,int_sort,var_desc,tin_inactive FROM pos_pay_met LIMIT 0;
				SELECT var_type,var_groupid,var_subgroupid,var_accid FROM ame_mt_acc_set LIMIT 0;
		WHEN "AR_IN_POS" THEN
				SELECT 
				T.var_id,
				T.var_type,
				T.var_cusid,
				T.var_empid,
				T.var_pricecode,
				T.var_status,
				T.var_ref,
				T.var_desc,
				T.var_cur,
				T.var_prefix,
				T.var_create,
				T.var_modify,
				T.var_post,
				T.var_void,
				T.dat_date,
				T.dat_create,
				T.dat_modify,
				T.dat_post,
				T.dat_void,
				T.int_jid,
				T.dou_sub,
				T.dou_disper,
				T.dou_disdol,
				T.dou_taxper,
				T.dou_taxdol,
				T.dou_taxper1,
				T.dou_taxdol1,
				T.dou_net,
				T.dou_paid,
				T.dou_change,
				T.dou_rate,
				T.dou_funsub,
				T.dou_fundisdol,
				T.dou_funtaxdol,
				T.dou_funtaxdol1,
				T.dou_funnet,
				T.dou_funpaid,
				T.dou_funchange,
				T.tin_taxbfdisc,
				T.var_salmanid,
				T.var_macid,
				T.var_locid,
				T.var_stationid,
				T.dat_from,
				T.dat_to,
				T.var_shiftid,
				T.var_cardid,
				T.int_cus_count,
				T.dou_rate_khr
				FROM pos_ar_in T LIMIT 0;
				SELECT 
				T.var_id,
				T.int_line,
				T.var_itemid,
				T.var_uomid,
				T.var_desc,
				T.dou_qty,
				T.dou_foc,
				T.var_locid,
				T.dou_price,
				T.dou_sub,
				T.dou_disper,
				T.dou_disdol,
				T.dou_taxper,
				T.dou_taxdol,
				T.dou_taxper1,
				T.dou_taxdol1,
				T.dou_svcper,
				T.dou_svcdol,
				T.dou_net,
				T.dou_funprice,
				T.dou_funsub,
				T.dou_fundisdol,
				T.dou_funtaxdol,
				T.dou_funtaxdol1,
				T.dou_funsvcdol,
				T.dou_funnet,
				T.tin_vat,
				T.tin_wht,
				T.tin_svc
				FROM pos_ar_in_det T LIMIT 0;		
				SELECT 
				T.var_salid,
				T.var_setid,
				T.var_cur,
				T.dou_rate,
				T.dou_amt,
				T.dou_total,
				T.dou_funtotal
				FROM pos_ar_in_pay_met T LIMIT 0;
				SELECT int_line, var_itemid, var_locid, var_uomid, dou_qty FROM pos_sys_tem_ar_in LIMIT 0;
		ELSE
				SET @NULL = NULL;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_APP_DOC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_APP_DOC`(P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		#NOTE: P1 = TYPE , P2 = VENDOR , P3 = PAYMENT NO
		DECLARE P_DEC_AMT INTEGER DEFAULT 0;
		# DECIMAL
		CASE 
		WHEN P1 IN ("AP-RET", "AP-DN", "AP-PY")  THEN
				BEGIN DECLARE C1 CURSOR FOR SELECT T.int_amt FROM ame_sys_decimal T WHERE T.var_id = 'AP';OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF; CLOSE C1;END;
				SET @P_DEC = P_DEC_AMT;
				SELECT 'Purchase', T.`Entry No`, T.Reference, T.Date, T.`Original Amt`, COALESCE(T.Paid, 0) AS 'Paid Amt', ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), @P_DEC) AS 'Amount Due', ROUND(T.`Original Amt` - COALESCE(T.Paid, 0) - T.APP, @P_DEC) AS Balance, T.Description, T.APP AS 'Applied Payment', FALSE FROM
				(SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', DATE(T1.dat_date) AS 'Date', T1.var_desc AS 'Description', T1.dou_net AS 'Original Amt', ROUND(COALESCE(T5.Paid, 0), @P_DEC) AS 'Paid', COALESCE(T6.APP, 0) AS APP FROM ame_ap_in T1
				LEFT JOIN (SELECT T3.var_docid, ROUND(SUM(T3.dou_disc + T3.dou_amt + T3.dou_app), @P_DEC) AS Paid FROM ame_ap_py_det T3 LEFT JOIN ame_ap_py T4 ON T3.var_id = T4.var_id WHERE T3.var_id <> P3 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T5 ON T1.var_id = T5.var_docid
				LEFT JOIN (SELECT T3.var_docid, T3.dou_app AS APP FROM ame_ap_py_det T3 LEFT JOIN ame_ap_py T4 ON T3.var_id = T4.var_id WHERE T3.var_id = P3 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T6 ON T1.var_id = T6.var_docid
				WHERE T1.var_venid = P2 AND T1.var_status = 'Posted' AND ROUND(COALESCE(T1.dou_net, 0) - COALESCE(T1.dou_paid, 0) + COALESCE(T6.APP, 0), @P_DEC) > 0 GROUP BY T1.var_id)
				AS T ORDER BY T.`Entry No`;
		WHEN P1 IN ("AR-RET", "AR-CN", "AR-PY")  THEN
				BEGIN DECLARE C1 CURSOR FOR SELECT T.int_amt FROM ame_sys_decimal T WHERE T.var_id = 'AR';OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF; CLOSE C1;END;
				SET @P_DEC = P_DEC_AMT;
				SELECT 'Sales', T.`Entry No`, T.Reference, T.Date, T.`Original Amt`, COALESCE(T.Paid, 0) AS 'Paid Amt', ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), @P_DEC) AS 'Amount Due', ROUND(T.`Original Amt` - COALESCE(T.Paid, 0) - T.APP, @P_DEC) AS Balance, T.Description, T.APP AS 'Applied Payment', FALSE FROM
				(SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', DATE(T1.dat_date) AS 'Date', T1.var_desc AS 'Description', T1.dou_net AS 'Original Amt', ROUND(COALESCE(T5.Paid, 0), @P_DEC) AS 'Paid', COALESCE(T6.APP, 0) AS APP FROM ame_ar_in T1
				LEFT JOIN (SELECT T3.var_docid, ROUND(SUM(T3.dou_disc + T3.dou_amt + T3.dou_app), @P_DEC) AS Paid FROM ame_ar_py_det T3 LEFT JOIN ame_ar_py T4 ON T3.var_id = T4.var_id WHERE T3.var_id <> P3 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T5 ON T1.var_id = T5.var_docid
				LEFT JOIN (SELECT T3.var_docid, T3.dou_app AS APP FROM ame_ar_py_det T3 LEFT JOIN ame_ar_py T4 ON T3.var_id = T4.var_id WHERE T3.var_id = P3 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T6 ON T1.var_id = T6.var_docid
				WHERE T1.var_cusid = P2 AND T1.var_status = 'Posted' AND ROUND(COALESCE(T1.dou_net, 0) - COALESCE(T1.dou_paid, 0) + COALESCE(T6.APP, 0), @P_DEC) > 0 GROUP BY T1.var_id)
				AS T ORDER BY T.`Entry No`;
		ELSE
				SET @Donothing = NULL;
		END CASE;
		
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_AP_IN_AC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_AP_IN_AC`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_H VARCHAR(2000) DEFAULT "";
		SELECT GROUP_CONCAT(T1.var_header) INTO P_H FROM ame_sys_add_cost T1 ORDER BY T1.int_line;
		SET @SQL_EXC = CONCAT("SELECT T1.var_id AS `Entry No`, T1.int_poline AS `Key Number`, T1.var_itemid AS `Item ID`, T2.var_name AS `Item Name`, T1.var_locid AS `Location ID`, T1.var_claid AS `Class ID`, T1.dou_qty AS `Quantity`, T1.dou_net AS `Purchase Cost`, 
		COALESCE(T3.dou_ac1, 0) AS `", AME_FUN_SPLIT_STRING(P_H, ',', 1),"`, 
		COALESCE(T3.dou_ac2, 0) AS `", AME_FUN_SPLIT_STRING(P_H, ',', 2),"`, 
		COALESCE(T3.dou_ac3, 0) AS `", AME_FUN_SPLIT_STRING(P_H, ',', 3),"`, 
		COALESCE(T3.dou_ac4, 0) AS `", AME_FUN_SPLIT_STRING(P_H, ',', 4),"`, 
		COALESCE(T3.dou_ac5, 0) AS `", AME_FUN_SPLIT_STRING(P_H, ',', 5),"`
		FROM ame_ap_in_det T1 
		LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
		LEFT JOIN ame_ap_in_ac T3 ON T1.var_id = T3.var_id AND T1.var_itemid = T3.var_itemid AND T1.var_locid = T3.var_locid AND T1.int_poline = T3.int_poline
		WHERE T1.var_id = '", P1, "' AND T2.var_type IN('Inventory Part', 'Fixed Asset') ORDER BY T1.int_line");
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
		SELECT T1.var_id AS 'Code', T1.var_name AS 'Type of Additional COGS', T1.var_desc AS 'Description', T1.var_header AS 'Header' FROM ame_sys_add_cost T1 ORDER BY T1.int_line;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_AR_IN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_AR_IN`(P1 varchar(100), P2 varchar(100))
    DETERMINISTIC
BEGIN
		CASE P2
		WHEN "General Item Sale" THEN
				SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.dou_price, T1.dou_sub, T1.dou_disper, T1.dou_disdol, T1.dou_taxper, T1.dou_taxdol, dou_net, dou_foc, dou_pricecom FROM ame_ar_in_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P1;
		WHEN "Apply to GL Account" THEN
				SELECT T1.var_accid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_price FROM ame_ar_in_det T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_id = P1;
		ELSE
				SET @Donothing = NULL;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_BATCH`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_BATCH`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_BATCH VARCHAR(100) DEFAULT "";
		DECLARE P_LOC VARCHAR(100) DEFAULT "";
		DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS T1.int_batchno, T1.var_locid FROM ame_sl_ic_batch T1 WHERE T1.var_itemid = P1 AND T1.dou_qty > 0 ORDER BY T1.int_batchno LIMIT 1;
		OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_BATCH, P_LOC; END IF;CLOSE C1;
		SELECT COALESCE(P_BATCH, ''), COALESCE(P_LOC, '');
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_CUR_RATE_DEC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_CUR_RATE_DEC`(P1 varchar(100), P2 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE DEF_CUR VARCHAR(100) DEFAULT "USD";
		CASE P1
		WHEN "DEC_FUN" THEN
				SELECT T1.int_qty, T1.int_cost, T1.int_price, T1.int_amt, T1.int_per, T1.int_rate FROM ame_sys_decimal T1 WHERE T1.var_id = (SELECT T2.var_id FROM ame_mt_cur T2 WHERE T2.tin_default = 1);
				SELECT T1.var_id FROM ame_mt_cur T1 WHERE T1.tin_default = 1;
		WHEN "DEC_FOR" THEN
				SELECT COALESCE(T1.dou_rate, 1) AS `dou_rate` FROM ame_mt_cur_rate T1 WHERE T1.var_id = P2;
				SELECT T1.int_qty, T1.int_cost, T1.int_price, T1.int_amt, T1.int_per, T1.int_rate FROM ame_sys_decimal T1 WHERE T1.var_id = P2;
		WHEN "DEC_DEF" THEN	
				BEGIN
						DECLARE C1 CURSOR FOR SELECT T1.var_id FROM ame_mt_cur T1 WHERE T1.tin_default = 1;
						OPEN C1;
								IF FOUND_ROWS() > 0 THEN FETCH C1 INTO DEF_CUR; END IF;
								SELECT T1.int_qty AS QTY, T1.int_cost AS UC, T1.int_price AS UP, T1.int_amt AS AMT, T1.int_per AS PER, T1.int_rate AS RATE FROM ame_sys_decimal T1 WHERE T1.var_id = DEF_CUR;
						CLOSE C1;
				END;	
		ELSE
				SET @Donothing = NULL;
		END CASE;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_DEF_UOM`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_DEF_UOM`(P1 varchar(100), P2 varchar(100), P3 varchar(100), P4 varchar(100), P5 varchar(100), P6 varchar(100))
    DETERMINISTIC
BEGIN
		#NOTE: P1 = MODULE , P2 = VENDOR , P3 = ITEM , P4 = UOM , P5 = PRICE CODE , P6  = CURRENCY
		DECLARE DEF_UOM VARCHAR(100) DEFAULT "";
		DECLARE DEF_DESC VARCHAR(100) DEFAULT "";
		DECLARE DEF_VAT TINYINT(1) DEFAULT 0;
		DECLARE DEF_WHT TINYINT(1) DEFAULT 0;
		DECLARE DEF_SCT TINYINT(1) DEFAULT 0;
		DECLARE DEF_COM_PRICE DOUBLE DEFAULT 0.0;
		DECLARE P_ROW INTEGER DEFAULT 0;
		CASE P1
		WHEN "AP-PO" THEN
				SELECT IF(ISNULL(T2.var_uomid), T1.var_uomid, T2.var_uomid), T1.var_desc_pur, T1.tin_sct, T1.tin_vat, T1.tin_wht INTO DEF_UOM, DEF_DESC, DEF_SCT, DEF_VAT, DEF_WHT
				FROM ame_mt_ite T1 
				LEFT JOIN ame_mt_ite_uom T2 ON (T1.var_id = T2.var_id AND COALESCE(T2.tin_pur, 0) = 1) WHERE T1.var_id = P3;
				IF P4 = "" THEN SET P4 = DEF_UOM; END IF;
				SELECT DEF_UOM, DEF_DESC, DEF_SCT, DEF_VAT, DEF_WHT;
				SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_mt_ven T1 WHERE T1.var_id = P2 AND COALESCE(T1.tin_po, 0) = 1;
				IF P_ROW = 0 THEN
						SELECT 0;
				ELSE
						SELECT COALESCE(T2.dou_price, 0) AS `dou_price` FROM ame_ap_po T1 
						LEFT JOIN ame_ap_po_det T2 ON T1.var_id = T2.var_id 
						LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id 
						WHERE T1.var_venid = P2 AND T2.var_itemid = P3 AND T2.var_uomid = P4 AND COALESCE(T3.tin_po, 0) = 1 AND T1.var_status IN('Open', 'Closed') 
						AND T1.dat_date = (SELECT MAX(T1.dat_date) FROM ame_ap_po T1 
						LEFT JOIN ame_ap_po_det T2 ON T1.var_id = T2.var_id 
						LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id 
						WHERE T1.var_venid = P2 AND T2.var_itemid = P3 AND T2.var_uomid = P4 AND COALESCE(T3.tin_po, 0) = 1 AND T1.var_status IN('Open', 'Closed'))
						ORDER BY T2.int_line LIMIT 1;
				END IF;
		WHEN "AP-IN" THEN
				SELECT IF(ISNULL(T2.var_uomid), T1.var_uomid, T2.var_uomid), T1.var_desc_pur, T1.tin_sct, T1.tin_vat, T1.tin_wht INTO DEF_UOM, DEF_DESC, DEF_SCT, DEF_VAT, DEF_WHT
				FROM ame_mt_ite T1 
				LEFT JOIN ame_mt_ite_uom T2 ON (T1.var_id = T2.var_id AND COALESCE(T2.tin_pur, 0) = 1) WHERE T1.var_id = P3;
				IF P4 = "" THEN SET P4 = DEF_UOM; END IF;
				SELECT DEF_UOM, DEF_DESC, DEF_SCT, DEF_VAT, DEF_WHT;						
				SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_mt_ven T1 WHERE T1.var_id = P2 AND COALESCE(T1.tin_pi, 0) = 1;
				IF P_ROW = 0 THEN
						SELECT 0;
				ELSE
						SELECT COALESCE(T2.dou_price, 0) AS `dou_price` FROM ame_ap_in T1 
						LEFT JOIN ame_ap_in_det T2 ON T1.var_id = T2.var_id 
						LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id 
						WHERE T1.var_venid = P2 AND T2.var_itemid = P3 AND T2.var_uomid = P4 AND COALESCE(T3.tin_pi, 0) = 1 AND T1.var_status IN('Open', 'Posted') 
						AND T1.dat_date = (SELECT MAX(T1.dat_date) FROM ame_ap_in T1 
						LEFT JOIN ame_ap_in_det T2 ON T1.var_id = T2.var_id 
						LEFT JOIN ame_mt_ven T3 ON T1.var_venid = T3.var_id 
						WHERE T1.var_venid = P2 AND T2.var_itemid = P3 AND T2.var_uomid = P4 AND COALESCE(T3.tin_pi, 0) = 1 AND T1.var_status IN('Open', 'Posted'))
						ORDER BY T2.int_line LIMIT 1;
				END IF;	
		WHEN "AR-SO" THEN
				SELECT IF(ISNULL(T2.var_uomid), T1.var_uomid, T2.var_uomid), T1.var_desc_pur, T1.tin_sct, T1.tin_vat, T1.tin_wht INTO DEF_UOM, DEF_DESC, DEF_SCT, DEF_VAT, DEF_WHT
				FROM ame_mt_ite T1 
				LEFT JOIN ame_mt_ite_uom T2 ON (T1.var_id = T2.var_id AND COALESCE(T2.tin_sal, 0) = 1) WHERE T1.var_id = P3;
				IF P4 = "" THEN SET P4 = DEF_UOM; END IF;
				# Comparative Price
				BEGIN
						DECLARE C1 CURSOR FOR SELECT COALESCE(T1.dou_price, 0) FROM ame_mt_ite_pri T1 LEFT JOIN ame_mt_pri_cod T2 ON T1.var_pricecode = T2.var_id WHERE T1.var_id = P3 AND T1.var_uomid = P4 AND T2.tin_reportprice = 1 AND T1.var_cur = P6;
						OPEN C1;
								IF FOUND_ROWS() > 0 THEN FETCH C1 INTO DEF_COM_PRICE; END IF;
						CLOSE C1;
				END;
				SELECT DEF_UOM, DEF_DESC, DEF_SCT, DEF_VAT, DEF_WHT, DEF_COM_PRICE;
				SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_mt_cus T1 WHERE T1.var_id = P2 AND COALESCE(T1.tin_so, 0) = 1;
				IF P_ROW = 0 THEN
						SELECT COALESCE(T1.dou_price, 0) FROM ame_mt_ite_pri T1 WHERE T1.var_id = P3 AND T1.var_uomid = P4 AND T1.var_pricecode = P5 AND T1.var_cur = P6;
				ELSE
						SELECT COALESCE(T2.dou_price, 0) AS `dou_price` FROM ame_ar_so T1 
						LEFT JOIN ame_ar_so_det T2 ON T1.var_id = T2.var_id 
						LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id 
						WHERE T1.var_cusid = P2 AND T2.var_itemid = P3 AND T2.var_uomid = P4 AND COALESCE(T3.tin_so, 0) = 1 AND T1.var_status IN('Open', 'Posted') 
						AND T1.dat_date = (SELECT MAX(T1.dat_date) FROM ame_ar_so T1 
						LEFT JOIN ame_ar_so_det T2 ON T1.var_id = T2.var_id 
						LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id 
						WHERE T1.var_cusid = P2 AND T2.var_itemid = P3 AND T2.var_uomid = P4 AND COALESCE(T3.tin_so, 0) = 1 AND T1.var_status IN('Open', 'Posted'))
						ORDER BY T2.int_line LIMIT 1;
				END IF;
		WHEN "AR-IN" THEN
				SELECT IF(ISNULL(T2.var_uomid), T1.var_uomid, T2.var_uomid), T1.var_desc_pur, T1.tin_sct, T1.tin_vat, T1.tin_wht INTO DEF_UOM, DEF_DESC, DEF_SCT, DEF_VAT, DEF_WHT
				FROM ame_mt_ite T1 
				LEFT JOIN ame_mt_ite_uom T2 ON (T1.var_id = T2.var_id AND COALESCE(T2.tin_sal, 0) = 1) WHERE T1.var_id = P3;
				IF P4 = "" THEN SET P4 = DEF_UOM; END IF;
				# Comparative Price
				BEGIN
						DECLARE C1 CURSOR FOR SELECT COALESCE(T1.dou_price, 0) FROM ame_mt_ite_pri T1 LEFT JOIN ame_mt_pri_cod T2 ON T1.var_pricecode = T2.var_id WHERE T1.var_id = P3 AND T1.var_uomid = P4 AND T2.tin_reportprice = 1 AND T1.var_cur = P6;
						OPEN C1;
								IF FOUND_ROWS() > 0 THEN FETCH C1 INTO DEF_COM_PRICE; END IF;
						CLOSE C1;
				END;
				SELECT DEF_UOM, DEF_DESC, DEF_SCT, DEF_VAT, DEF_WHT, DEF_COM_PRICE;
				SELECT COUNT(T1.var_id) INTO P_ROW FROM ame_mt_cus T1 WHERE T1.var_id = P2 AND COALESCE(T1.tin_si, 0) = 1;
				IF P_ROW = 0 THEN
						SELECT COALESCE(T1.dou_price) FROM ame_mt_ite_pri T1 WHERE T1.var_id = P3 AND T1.var_uomid = P4 AND T1.var_pricecode = P5 AND T1.var_cur = P6;
				ELSE
						SELECT COALESCE(T2.dou_price, 0) AS `dou_price` FROM ame_ar_in T1 
						LEFT JOIN ame_ar_in_det T2 ON T1.var_id = T2.var_id 
						LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id 
						WHERE T1.var_cusid = P2 AND T2.var_itemid = P3 AND T2.var_uomid = P4 AND COALESCE(T3.tin_si, 0) = 1 AND T1.var_status IN('Open', 'Posted') 
						AND T1.dat_date = (SELECT MAX(T1.dat_date) FROM ame_ar_in T1 
						LEFT JOIN ame_ar_in_det T2 ON T1.var_id = T2.var_id 
						LEFT JOIN ame_mt_cus T3 ON T1.var_cusid = T3.var_id 
						WHERE T1.var_cusid = P2 AND T2.var_itemid = P3 AND T2.var_uomid = P4 AND COALESCE(T3.tin_si, 0) = 1 AND T1.var_status IN('Open', 'Posted'))
						ORDER BY T2.int_line LIMIT 1;
				END IF;
		WHEN "IC-TF" THEN
				SELECT IF(ISNULL(T2.var_uomid), T1.var_uomid, T2.var_uomid) INTO DEF_UOM
				FROM ame_mt_ite T1 
				LEFT JOIN ame_mt_ite_uom T2 ON (T1.var_id = T2.var_id AND COALESCE(T2.tin_pur, 0) = 1) WHERE T1.var_id = P3;
				IF P4 = "" THEN SET P4 = DEF_UOM; END IF;
				SELECT DEF_UOM;
				SELECT 0;
		WHEN "IC-IU" THEN
				SELECT IF(ISNULL(T2.var_uomid), T1.var_uomid, T2.var_uomid) INTO DEF_UOM
				FROM ame_mt_ite T1 
				LEFT JOIN ame_mt_ite_uom T2 ON (T1.var_id = T2.var_id AND COALESCE(T2.tin_pur, 0) = 1) WHERE T1.var_id = P3;
				IF P4 = "" THEN SET P4 = DEF_UOM; END IF;
				SELECT DEF_UOM;
				SELECT 0;
		WHEN "IC-ICUOM" THEN
				SELECT IF(ISNULL(T2.var_uomid), T1.var_uomid, T2.var_uomid) INTO DEF_UOM
				FROM ame_mt_ite T1 
				LEFT JOIN ame_mt_ite_uom T2 ON (T1.var_id = T2.var_id AND COALESCE(T2.tin_pur, 0) = 1) WHERE T1.var_id = P3;
				IF P4 = "" THEN SET P4 = DEF_UOM; END IF;
				SELECT DEF_UOM;
				SELECT 0;
		WHEN "IC-IA" THEN
				SELECT IF(ISNULL(T2.var_uomid), T1.var_uomid, T2.var_uomid) INTO DEF_UOM
				FROM ame_mt_ite T1 
				LEFT JOIN ame_mt_ite_uom T2 ON (T1.var_id = T2.var_id AND COALESCE(T2.tin_pur, 0) = 1) WHERE T1.var_id = P3;
				IF P4 = "" THEN SET P4 = DEF_UOM; END IF;
				SELECT DEF_UOM;
				SELECT 0;
		WHEN "IC-IP" THEN
				SELECT IF(ISNULL(T2.var_uomid), T1.var_uomid, T2.var_uomid) INTO DEF_UOM
				FROM ame_mt_ite T1 
				LEFT JOIN ame_mt_ite_uom T2 ON (T1.var_id = T2.var_id AND COALESCE(T2.tin_pur, 0) = 1) WHERE T1.var_id = P3;
				IF P4 = "" THEN SET P4 = DEF_UOM; END IF;
				SELECT DEF_UOM;
				SELECT 0;
		ELSE
				SET @Donothing = NULL;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_DETAIL`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_DETAIL`(P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		CASE P1
		WHEN "AP-PO" THEN
				CASE P2
				WHEN "Purchase Inventory" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.dou_price, dou_sub, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.dou_foc, T1.tin_vat, T1.tin_wht, T1.dou_orderqty, T1.dou_orderfoc, T1.int_poline FROM ame_ap_po_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P3;
				WHEN "Additional Costing" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_type, T3.var_name, T1.var_claid, T1.int_batchno, T1.var_locid, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht FROM ame_ap_po_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id LEFT JOIN ame_sys_add_cost T3 ON T1.var_type = T3.var_id WHERE T1.var_id = P3;
				WHEN "Apply to GL Account" THEN
						SELECT T1.var_accid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht FROM ame_ap_po_det T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_id = P3;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "AP-IN" THEN
				CASE P2
				WHEN "Purchase Inventory" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.dou_price, dou_sub, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.dou_foc, T1.tin_vat, T1.tin_wht, T1.int_poline, T1.var_poid, T1.dou_orderqty, T1.dou_orderfoc FROM ame_ap_in_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P3;
				WHEN "Additional Costing" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_type, T3.var_name, T1.var_claid, T1.int_batchno, T1.var_locid, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht, T1.int_poline, T1.var_poid FROM ame_ap_in_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id LEFT JOIN ame_sys_add_cost T3 ON T1.var_type = T3.var_id WHERE T1.var_id = P3;
				WHEN "Apply to GL Account" THEN
						SELECT T1.var_accid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht FROM ame_ap_in_det T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_id = P3;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "AP-RET" THEN
				CASE P2
				WHEN "Purchase Inventory" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.dou_price, dou_sub, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.dou_foc, T1.tin_vat, T1.tin_wht FROM ame_ap_ret_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P3;
				WHEN "Additional Costing" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_type, T3.var_name, T1.var_claid, T1.int_batchno, T1.var_locid, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht FROM ame_ap_ret_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id LEFT JOIN ame_sys_add_cost T3 ON T1.var_type = T3.var_id WHERE T1.var_id = P3;
				WHEN "Apply to GL Account" THEN
						SELECT T1.var_accid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht FROM ame_ap_ret_det T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_id = P3;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "AP-DN" THEN
				CASE P2
				WHEN "Purchase Inventory" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.dou_price, dou_sub, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.dou_foc, T1.tin_vat, T1.tin_wht FROM ame_ap_dn_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P3;
				WHEN "Additional Costing" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_type, T3.var_name, T1.var_claid, T1.int_batchno, T1.var_locid, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht FROM ame_ap_dn_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id LEFT JOIN ame_sys_add_cost T3 ON T1.var_type = T3.var_id WHERE T1.var_id = P3;
				WHEN "Apply to GL Account" THEN
						SELECT T1.var_accid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht FROM ame_ap_dn_det T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_id = P3;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "AR-QUO" THEN
				CASE P2
				WHEN "General Item Sale" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.dou_price, dou_sub, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.dou_foc, T1.tin_vat, T1.tin_wht, T1.dou_orderqty, T1.dou_orderfoc, T1.int_poline, T1.dou_pricecom FROM ame_ar_quo_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P3;
				END CASE;
		WHEN "AR-SO" THEN
				CASE P2
				WHEN "General Item Sale" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.dou_price, dou_sub, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.dou_foc, T1.tin_vat, T1.tin_wht, T1.dou_orderqty, T1.dou_orderfoc, T1.int_poline, T1.dou_pricecom, T1.var_poid FROM ame_ar_so_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P3;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "AR-IN" THEN
				CASE P2
				WHEN "General Item Sale" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.dou_price, dou_sub, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.dou_foc, T1.tin_vat, T1.tin_wht, T1.dou_orderqty, T1.dou_orderfoc, T1.int_poline, T1.dou_pricecom, T1.var_poid, T1.var_potype FROM ame_ar_in_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P3;
				ELSE
						SELECT T1.var_accid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht FROM ame_ar_in_det T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_id = P3;
				END CASE;
		WHEN "AR-CN" THEN
				CASE P2
				WHEN "General Item Sale" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.dou_price, dou_sub, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.dou_foc, T1.tin_vat, T1.tin_wht, T1.dou_orderqty, T1.dou_orderfoc, T1.int_poline, T1.dou_pricecom, T1.var_poid, T1.var_potype FROM ame_ar_cn_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P3;
				ELSE
						SELECT T1.var_accid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht FROM ame_ar_cn_det T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_id = P3;
				END CASE;
		WHEN "AR-PY-SD" THEN
				SELECT T1.int_id, T1.dat_date, T1.var_cusid, T1.var_period, T1.var_desc, T1.dou_sch, T1.dou_rate_per, T1.int_term, T1.var_method, T1.var_type, T1.dou_rate_penalty FROM ame_ar_py_sd T1 WHERE T1.var_id = P3 ORDER BY T1.int_id;
				SELECT T1.int_id, T1.int_line, T1.dat_date, T1.dou_sch, T1.dou_amt, T1.dou_rate_dol, T1.dou_net, T1.dou_balance, T1.dou_penalty, T1.int_day FROM ame_ar_py_sd_det T1 WHERE T1.var_id = P3 ORDER BY T1.int_id, T1.int_line;
				SELECT var_id, var_name FROM ame_sys_config WHERE var_id IN('EXC_SATURDAY', 'EXC_SUNDAY', 'EXC_HOLIDAY') AND var_name = 'TRUE';
				SELECT DISTINCT DATE_FORMAT(dat_date, "%d-%b-%Y") AS `dat_date` FROM ame_mt_hol WHERE tin_inactive = 0;
		WHEN "GL-JE" THEN
				SELECT T1.var_accid, T2.var_name, T1.dat_date, T1.var_claid, T1.var_desc, IF(T1.var_drcr = "Dr", T1.dou_amt, 0), IF(T1.var_drcr = "Cr", -T1.dou_amt, 0), T1.int_line, T1.var_drcr, IF(T1.var_drcr = "Dr", T1.dou_funamt, 0), IF(T1.var_drcr = "Cr", -T1.dou_funamt, 0)  FROM ame_gl_je_det T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id LEFT JOIN ame_mt_cla T3 ON T1.var_claid = T3.var_id WHERE T1.int_jid = P3 ORDER BY T1.int_line;
		WHEN "IC-TF" THEN
				CASE P2
				WHEN "Inventory Transfer" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_tolocid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.int_poline FROM ame_ic_tf_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P3 ORDER BY T1.int_line;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "IC-IU" THEN
				CASE P2
				WHEN "Internal Usage" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_type, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.var_accid, T3.var_name, T1.var_appitemid, T5.var_name, T1.var_appacid, T4.var_name, T1.int_appbatchno, T1.var_applocid, T1.int_poline FROM ame_ic_iu_det T1 
						LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
						LEFT JOIN ame_mt_acc T3 ON T1.var_accid = T3.var_id 
						LEFT JOIN ame_sys_add_cost T4 ON T1.var_appacid = T4.var_id 
						LEFT JOIN ame_mt_ite T5 ON T1.var_appitemid = T5.var_id 
						WHERE T1.var_id = P3 ORDER BY T1.int_line;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "IC-ICUOM" THEN
				CASE P2
				WHEN "Inventory Convert UOM" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, T1.var_toitemid, T3.var_name, T1.var_tolocid, T1.var_touomid, T1.var_convtype, T1.dou_conv, T1.int_poline FROM ame_ic_icuom_det T1 
						LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
						LEFT JOIN ame_mt_ite T3 ON T1.var_toitemid = T3.var_id 
						WHERE T1.var_id = P3 ORDER BY T1.int_line;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "IC-IA" THEN
				CASE P2
				WHEN "Inventory Adjustment" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, IF(T1.int_batchno = 0, '', T1.int_batchno), T1.dou_qty, T1.dou_price, T1.int_poline FROM ame_ic_ia_det T1 
						LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
						WHERE T1.var_id = P3 ORDER BY T1.int_line;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "IC-IP" THEN
				CASE P2
				WHEN "Inventory Production" THEN
						SELECT T1.var_type, IF(T1.var_type = "Item", T1.var_itemid, ""), T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, T1.dou_qty, IF(T1.var_type = "Account", T1.var_itemid, ""), T3.var_name, IF(T1.tin_pro = 0, T1.dou_price, 0), IF(T1.tin_pro = 1, T1.dou_price, 0), T1.int_poline, T1.tin_pro FROM ame_ic_ip_det T1 
						LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id AND T1.var_type = "Item")
						LEFT JOIN ame_mt_acc T3 ON(T1.var_itemid = T2.var_id AND T1.var_type = "Account")
						WHERE T1.var_id = P3 ORDER BY T1.int_line;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "POS-AR-IN" THEN
				SELECT 
				COALESCE(T1.var_itemid,'') AS var_itemid,
				COALESCE(T2.var_name,'') AS var_itemname,
				COALESCE(T1.var_desc,'') AS var_desc,
				COALESCE(T1.var_uomid,'') AS var_uomid,
				COALESCE(T1.dou_qty,0) AS dou_qty,
				COALESCE(T1.dou_price,0) AS dou_price,
				COALESCE(T1.dou_sub,0) AS dou_sub,
				COALESCE(T1.dou_disper,0) AS dou_disper,
				COALESCE(T1.dou_disdol,0) AS dou_disdol,
				COALESCE(T1.dou_net,0) AS dou_net,
				COALESCE(T1.dou_foc,0) AS dou_foc,
				T1.tin_vat AS tin_vat,
				T1.tin_wht AS tin_wht,
				T1.tin_svc AS tin_svc
				FROM pos_ar_in_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P3;
				SELECT 
				T.var_salid,
				T.var_setid,
				T.var_cur,
				T.dou_rate,
				T.dou_amt,
				T.dou_total,
				T.dou_funtotal
				FROM pos_ar_in_pay_met T WHERE T.var_salid = P3;
		ELSE
				SET @Donothing = NULL;
		END CASE;
		
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_DIALOG`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_DIALOG`(P1 varchar(500))
    DETERMINISTIC
BEGIN
		DECLARE var_1 VARCHAR(250) DEFAULT '';
		DECLARE var_2 VARCHAR(250) DEFAULT '';
		SELECT var_name,var_min_max_date INTO var_1, var_2 FROM ame_sys_dialog WHERE var_procedure = P1;
		SELECT var_1, var_2;
		IF var_2 <> "" THEN
				SET @P1 = AME_FUN_SPLIT_STRING(var_2,';',1); # TABLE
				SET @P2 = AME_FUN_SPLIT_STRING(var_2,';',2); # FIELD
				SET @SQL = CONCAT("SELECT DATE(COALESCE(MIN(", @P2,"),NOW())) AS 'MIN',DATE(COALESCE(MAX(", @P2,"),NOW())) AS 'MAX' FROM ",@P1);
				PREPARE St FROM @SQL;
				EXECUTE St;
				DEALLOCATE PREPARE St;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_FL_USER_LOG`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_FL_USER_LOG`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),P1 varchar(100), P2 date, P3 date)
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", P2,"' AND '", P3,"')");
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("(T.var_id LIKE '%",P1,"%' OR T.var_modid LIKE '%",P1,"%' OR T.var_actiontype LIKE '%",P1,"%')", P_BET);
		END CASE;

		CASE int_offset
		WHEN -1 THEN
				SET @R = -1;
				SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, T.var_id FROM ame_ap_in T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.dat_date AS 'Log Date Time', var_uid AS 'User Id', var_actiontype AS 'Action Type', T.var_modid AS 'Module Id', T.var_id AS 'Num' FROM ame_user_log T");
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SET @R = -1;
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, T.var_id FROM ame_user_log T ORDER BY T.var_id) T WHERE T.var_id = P1;
						WHILE int_index >= 100 DO
						SET int_page = int_page + 1;
						SET int_index = int_index - 100;
						END WHILE;
						SELECT int_index AS RowIndex, int_page + 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," ORDER BY T.int_line LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE (",@SQL_CON,") ORDER BY T.int_line LIMIT 100 OFFSET ",int_offset);
				WHEN 3 THEN # PAGINATION (FIRST, PREVIOUS, NEXT)
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE (",@SQL_CON,") ORDER BY T.int_line LIMIT 100 OFFSET ",IF(int_offset <= 0,0,(int_offset * 100) - 100));
				WHEN 4 THEN  # PAGINATION (LAST)
						BEGIN
								SET @PAGE = 0;
								SET @SQL_EXC = CONCAT("SELECT COUNT(T.var_id) / 100 INTO @PAGE FROM ame_user_log T"," WHERE (",@SQL_CON,")");
								PREPARE STM FROM @SQL_EXC;
								EXECUTE STM;
								DEALLOCATE PREPARE STM;
								IF @PAGE <= 1 THEN
										SET @PAGE = 1;
								ELSE
										IF CEIL(@PAGE) = @PAGE THEN
												SET @PAGE = TRUNCATE(@PAGE,0);
										ELSE
												SET @PAGE = TRUNCATE(@PAGE + 1,0);
										END IF;
								END IF;
								SELECT 0 AS RowIndex, @PAGE AS Page;
						END;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE (",@SQL_CON,") LIMIT 100 OFFSET ",IF(@PAGE <= 0,0,TRUNCATE(@PAGE * 100,0) - 100));
				ELSE
						SET @DONOTHING = NULL;
				END CASE;
				PREPARE STM FROM @SQL_EXC;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_FL_USER_PER`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_FL_USER_PER`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		SELECT T1.var_modid, T1.int_line AS `Key`, T1.var_name_menu AS `Menu`, T1.var_name AS `Form Name`,
		COALESCE(T2.tin_per1, 0) AS `View`,
		COALESCE(T2.tin_per2, 0) AS `Add`,
		COALESCE(T2.tin_per3, 0) AS `Edit`,
		COALESCE(T2.tin_per4, 0) AS `Delete`,
		COALESCE(T2.tin_per5, 0) AS `Post`,
		COALESCE(T2.tin_per6, 0) AS `Repost`,
		COALESCE(T2.tin_per7, 0) AS `Delete Posting`,
		COALESCE(T2.tin_per8, 0) AS `Print` FROM (ame_sys_per_frm T1 INNER JOIN ame_sys_form T3 ON T1.var_modid = T3.frmsysname) LEFT JOIN ame_user_per_frm T2 ON (T1.var_modid = T2.var_modid AND T2.int_id = P1) ORDER BY T1.int_line;
		SELECT T1.var_modid, T1.int_line AS `Key`, T1.var_name_menu AS `Menu`, T1.var_name AS `Report Name`,
		COALESCE(T2.tin_per1, 0) AS `View` FROM ame_sys_per_rep T1 LEFT JOIN ame_user_per_rep T2 ON (T1.var_modid = T2.var_modid AND T2.int_id = P1) ORDER BY T1.int_group, T1.int_line;
		SELECT T1.var_modid, T1.var_name AS `Other Permission`, COALESCE(T2.tin_per1, 0) AS `Check` FROM ame_sys_per_oth T1 LEFT JOIN ame_user_per_oth T2 ON (T1.var_modid = T2.var_modid AND T2.int_id = P1);
		SELECT T1.var_modid,
		COALESCE(T1.tin_per1, 0) AS `View`,
		COALESCE(T1.tin_per2, 0) AS `Add`,
		COALESCE(T1.tin_per3, 0) AS `Edit`,
		COALESCE(T1.tin_per4, 0) AS `Delete`,
		COALESCE(T1.tin_per5, 0) AS `Post`,
		COALESCE(T1.tin_per6, 0) AS `Repost`,
		COALESCE(T1.tin_per7, 0) AS `Delete Posting`,
		COALESCE(T1.tin_per8, 0) AS `Print` FROM ame_sys_per_frm T1 ORDER BY T1.int_line;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_FL_USER_PRO`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_FL_USER_PRO`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		SELECT T1.var_modid, T1.var_name, COALESCE(T2.tin_pro1, 0) AS `tin_per1`, COALESCE(T2.int_day, 0) AS `int_day` FROM ame_sys_rem T1 LEFT JOIN ame_user_pro_rem T2 ON (T1.var_modid = T2.var_modid AND T2.var_id = P1) ORDER BY T1.int_line;
		SELECT T1.var_type, T1.var_desc, COALESCE(T2.var_modid, '') AS `var_modid` 
		FROM ame_sys_prefix_custom T1 
		LEFT JOIN ame_user_pro_def_pre T2 ON (T1.var_type = T2.var_type AND T2.var_id = P1)
		WHERE T1.var_modid = 'DEFAULT';
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_IC_IA_LIS`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_IC_IA_LIS`(P1 varchar(100), P2 varchar(100), P3 tinyint(1), P4 tinyint(1), P5 date)
BEGIN
		IF P3 = 1 THEN
				SELECT T1.var_id AS `Item ID`, T1.var_name AS `Item Name`,IF(P4 = 1, "All", T2.var_locid) AS `Location Code`, T1.var_uomid AS `UOM Code`, COALESCE(T2.`Quantity on Hand`, 0) AS `Quantity on Hand`, NULL AS `Physical Count`, 1 AS `Confirm Adjust` FROM ame_mt_ite T1 
				LEFT JOIN (SELECT T1.var_itemid, T1.var_locid, SUM(T1.dou_drqty - T1.dou_crqty) AS `Quantity on Hand` FROM ame_sl_ic T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.dat_date <= P5 AND IF(P4 = 1, TRUE, T1.var_locid = P2) AND IF(P1 = "All" OR P1 = "", TRUE, T2.var_groupid = P1) GROUP BY T1.var_itemid, T1.var_locid) AS T2 ON T1.var_id = T2.var_itemid 
				WHERE T1.var_type = "Inventory Part" AND COALESCE(T1.tin_inactive, 0) = 0  AND IF(P1 = "All" OR P1 = "", TRUE, T1.var_groupid = P1) ORDER BY T1.var_id;
		ELSE
				SELECT T1.var_id AS `Item ID`, T1.var_name AS `Item Name`, IF(P4 = 1, "All", T2.var_locid) AS `Location Code`, T1.var_uomid AS `UOM Code`, COALESCE(SUM(T2.dou_drqty - T2.dou_crqty), 0) AS `Quantity on Hand`, NULL AS `Physical Count`, 1 AS `Confirm Adjust` FROM ame_mt_ite T1 
				RIGHT JOIN ame_sl_ic T2 ON T1.var_id = T2.var_itemid 
				WHERE T2.dat_date <= P5 AND IF(P4 = 1, TRUE, T2.var_locid = P2) AND IF(P1 = "All" OR P1 = "", TRUE, T1.var_groupid = P1) AND T1.var_type = "Inventory Part" AND COALESCE(T1.tin_inactive, 0) = 0 GROUP BY T2.var_itemid, T2.var_locid;
		END IF;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_COM`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_COM`()
    DETERMINISTIC
BEGIN
		DECLARE P_PATH VARCHAR(2000) DEFAULT "";
		SELECT COALESCE(var_name, '') INTO P_PATH FROM ame_sys_config WHERE var_id = 'IMG_PATH';
		SELECT var_id, var_name1, var_name2, var_addr1, var_addr2, var_tel1, var_tel2, var_fax, var_email, var_website, var_repheader, var_taxid, CONCAT(P_PATH, "\\", COALESCE(var_logo1, '')) AS `var_logo1`,  CONCAT(P_PATH, "\\", COALESCE(var_logo2, '')) AS `var_logo2` FROM ame_mt_com;
		SELECT var_id, int_line, var_accid AS `Account Number`, var_name AS `Account Name`,  var_desc AS `Remark` FROM ame_mt_com_acc;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_CUS_TRAN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_CUS_TRAN`(P1 varchar(100), P2 varchar(20), P3 varchar(20), P4 date, P5 date, P6 tinyint(1), P7 tinyint(1), P8 tinyint(1), P9 tinyint(1), P10 tinyint(1), P11 tinyint(1))
    DETERMINISTIC
BEGIN
		SET @SQL_EXC = "(SELECT 'Module', 'Type', 'Entry No', 'Referene', NOW() AS 'Date', 'Remark', 'Status', 0.0 AS 'Grand Total' LIMIT 0)";
		IF P6 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AR-QUO' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ar_quo T1 WHERE T1.var_cusid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P7 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AR-SO' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ar_so T1 WHERE T1.var_cusid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P8 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AR-IN' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ar_in T1 WHERE T1.var_cusid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P9 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AR-RET' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ar_ret T1 WHERE T1.var_cusid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P10 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AR-CN' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ar_cn T1 WHERE T1.var_cusid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P11 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AR-PY' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ar_py T1 WHERE T1.var_cusid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_DEC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_DEC`()
    DETERMINISTIC
BEGIN
		SELECT T1.var_id AS `Currency`, T1.int_qty AS `Quantity`, T1.int_cost AS `Cost`, T1.int_price AS `Price`, T1.int_amt AS `Amount`, T1.int_per AS `Percentage`, T1.int_rate AS `Rate` FROM ame_sys_decimal T1  WHERE T1.var_id = (SELECT T2.var_id FROM ame_mt_cur T2 WHERE T2.tin_default = 1) ORDER BY T1.int_line;
		SELECT T1.var_id AS `Currency`, T1.int_qty AS `Quantity`, T1.int_cost AS `Cost`, T1.int_price AS `Price`, T1.int_amt AS `Amount`, T1.int_per AS `Percentage`, T1.int_rate AS `Rate` FROM ame_sys_decimal T1 WHERE T1.var_id <> (SELECT T2.var_id FROM ame_mt_cur T2 WHERE T2.tin_default = 1) ORDER BY T1.int_line;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_EMP_TRAN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_EMP_TRAN`(P1 varchar(100), P2 varchar(20), P3 varchar(20), P4 date, P5 date, P6 tinyint(1), P7 tinyint(1), P8 tinyint(1), P9 tinyint(1), P10 tinyint(1))
    DETERMINISTIC
BEGIN
		SET @SQL_EXC = "(SELECT 'Module', 'Type', 'Entry No', 'Referene', NOW() AS 'Date', 'Remark', 'Status', 0.0 AS 'Grand Total' LIMIT 0)";
		IF P6 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AR-QUO' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ar_quo T1 WHERE T1.var_empid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P7 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AR-SO' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ar_so T1 WHERE T1.var_empid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P8 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AR-IN' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ar_in T1 WHERE T1.var_empid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P9 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AR-RET' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ar_ret T1 WHERE T1.var_empid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P10 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AR-CN' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ar_cn T1 WHERE T1.var_empid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_GEN_SET`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_GEN_SET`()
    DETERMINISTIC
BEGIN
		SELECT T1.var_name AS 'Name', COALESCE(T2.var_accid, '') AS 'Account Id', COALESCE(T2.var_name, '') AS 'Account Name', T1.var_id AS 'Type' FROM ame_sys_def_acc T1 LEFT JOIN (SELECT T1.var_type, T1.var_accid, T2.var_name FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_groupid = "DEFAULT" AND T1.var_subgroupid = "DEFAULT") T2 ON T1.var_id = T2.var_type ORDER BY T1.int_line;
		SELECT T1.var_id AS 'Code', T1.var_name AS 'Type of Additional COGS', T1.var_desc AS 'Description', T1.var_header AS 'Header' FROM ame_sys_add_cost T1 ORDER BY T1.int_line;
		SELECT T1.var_id, COALESCE(T1.var_name, '') AS var_name FROM ame_sys_config T1 ORDER BY T1.int_line;
		SELECT T1.int_id AS `Code`, COALESCE(T1.var_name, '') AS `Name`, T1.tin_inactive AS `Inactive` FROM ame_sys_pro_hie T1 ORDER BY T1.int_line;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_GEN_SET_DEL`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_GEN_SET_DEL`()
    DETERMINISTIC
BEGIN
		DELETE FROM ame_mt_acc_set WHERE var_type IN(SELECT var_id FROM ame_sys_def_acc) AND var_groupid = 'DEFAULT' AND var_subgroupid = 'DEFAULT';
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_ITE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_ITE`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		#NOTE: P1 = PAYMENT NO , P2 = TYPE
		DECLARE P_DEC_AMT INTEGER DEFAULT 0;
		DECLARE P_DEC_QTY INTEGER DEFAULT 0;
		DECLARE P_DEC_COST INTEGER DEFAULT 0;
		DECLARE P_H VARCHAR(2000) DEFAULT "";
		DECLARE H1 VARCHAR(2000) DEFAULT "";
		# DECIMAL
		BEGIN DECLARE C1 CURSOR FOR SELECT T.int_qty, 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, P_DEC_AMT; END IF;CLOSE C1; END;
		SELECT T1.var_id FROM ame_mt_ite T1 WHERE T1.var_id = P1;
		SELECT T1.var_type, T1.var_subgroupid, T1.var_accid FROM ame_mt_acc_set T1 WHERE T1.var_groupid = P1 AND T1.var_type IN('IC', 'REVENUE', 'REVENUE_RETURN', 'COGS', 'IC-IA', 'IC-IU');
		SELECT var_uomid, dou_conv, tin_pur, tin_sal FROM ame_mt_ite_uom T1 WHERE T1.var_id = P1 AND T1.var_uomid NOT IN(SELECT T2.var_uomid FROM ame_mt_ite T2 WHERE T2.var_id = P1) ORDER BY T1.int_line;
		SELECT T1.var_itemid, T2.var_name, T1.var_uomid_ass, T1.var_uomid, dou_qty FROM ame_mt_ite_ass T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_id = P1 ORDER BY T1.int_line;
		SELECT var_pricecode, var_uomid, var_cur, dou_price FROM ame_mt_ite_pri T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
		SELECT GROUP_CONCAT(T1.var_header) INTO P_H FROM ame_sys_add_cost T1 ORDER BY T1.int_line;
		SET @P_BAL := 0.0;
		SET @SQL_EXC = CONCAT("SELECT T1.var_locid AS 'Location Code', T1.int_batchno AS 'Batch No',
		AME_FUN_ROUND(T1.dou_drqty, ", P_DEC_QTY, ") AS 'Quantity In', AME_FUN_ROUND(T1.dou_crqty, ", P_DEC_QTY, ") AS 'Quantity Out', AME_FUN_ROUND(T1.dou_qty, ", P_DEC_QTY, ") AS 'Qty on Hand', AME_FUN_ROUND(@P_BAL := @P_BAL + T1.dou_qty, ", P_DEC_QTY, ") AS Balance,
		AME_FUN_ROUND(IF(T1.dou_qty = 0, T1.dou_druc / T1.dou_drqty, (T1.dou_druc - T1.dou_cruc) / dou_qty), ", P_DEC_COST, ") AS Cost,
		AME_FUN_ROUND(IF(T1.dou_qty = 0, T1.dou_drac1 / T1.dou_drqty, (T1.dou_drac1 - T1.dou_crac1) / dou_qty), ", P_DEC_COST, ") AS `", AME_FUN_SPLIT_STRING(P_H, ',', 1),"`,
		AME_FUN_ROUND(IF(T1.dou_qty = 0, T1.dou_drac2 / T1.dou_drqty, (T1.dou_drac2 - T1.dou_crac2) / dou_qty), ", P_DEC_COST, ") AS `", AME_FUN_SPLIT_STRING(P_H, ',', 2),"`,
		AME_FUN_ROUND(IF(T1.dou_qty = 0, T1.dou_drac3 / T1.dou_drqty, (T1.dou_drac3 - T1.dou_crac3) / dou_qty), ", P_DEC_COST, ") AS `", AME_FUN_SPLIT_STRING(P_H, ',', 3),"`,
		AME_FUN_ROUND(IF(T1.dou_qty = 0, T1.dou_drac4 / T1.dou_drqty, (T1.dou_drac4 - T1.dou_crac4) / dou_qty), ", P_DEC_COST, ") AS `", AME_FUN_SPLIT_STRING(P_H, ',', 4),"`,
		AME_FUN_ROUND(IF(T1.dou_qty = 0, T1.dou_drac5 / T1.dou_drqty, (T1.dou_drac5 - T1.dou_crac5) / dou_qty), ", P_DEC_COST, ") AS `", AME_FUN_SPLIT_STRING(P_H, ',', 5),"`,
		AME_FUN_ROUND(IF(T1.dou_qty = 0, (T1.dou_druc + T1.dou_drac1 + T1.dou_drac2 + T1.dou_drac3 + T1.dou_drac4 + T1.dou_drac5) / T1.dou_drqty, ((T1.dou_druc + T1.dou_drac1 + T1.dou_drac2 + T1.dou_drac3 + T1.dou_drac4 + T1.dou_drac5) - (T1.dou_cruc + T1.dou_crac1 + T1.dou_crac2 + T1.dou_crac3 + T1.dou_crac4 + T1.dou_crac5)) / dou_qty), ", P_DEC_COST, ") AS 'Total Cost' FROM ame_sl_ic_batch T1 WHERE T1.var_itemid = '", P1, "' ORDER BY T1.var_locid, T1.int_batchno");
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_ITE_COGS`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_ITE_COGS`(P1 varchar(100), P2 varchar(100), P3 varchar(100), P4 text)
    DETERMINISTIC
BEGIN
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		DECLARE P_DEC_COST INTEGER DEFAULT 5;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_DEC_QTY, P_DEC_COST; END IF;CLOSE C1;END;
		SET @P_DEC_COST = P_DEC_COST;
		CASE P1
		WHEN "Average Cost" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.var_itemid, 
				AME_FUN_ROUND(SUM((T1.dou_druc + T1.dou_drac1 + T1.dou_drac2 + T1.dou_drac3 + T1.dou_drac4 + T1.dou_drac5) - (T1.dou_cruc + T1.dou_crac1 + T1.dou_crac2 + T1.dou_crac3 + T1.dou_crac4 + T1.dou_crac5)) / SUM(T1.dou_qty), ", @P_DEC_COST, ") AS `Current Price`
				FROM ame_sl_ic_batch T1 WHERE T1.dou_qty > 0 AND IF('", P3,"' = 'All' OR '", P3,"' = '', TRUE, T1.var_locid = '", P3,"') AND T1.var_itemid IN('", P4,"') GROUP BY T1.var_itemid");
		WHEN "First Batch" THEN
				SET @LINE = 0;
				SET @ITEM = "";
				SET @SQL_EXC = CONCAT("SELECT T1.var_itemid, T1.`Current Price` FROM (SELECT IF(@ITEM <> T1.var_itemid, 0, @LINE := @LINE + 1) AS `P_LINE`, @ITEM := T1.var_itemid AS `P_ITEM`, T1.var_itemid, 
				AME_FUN_ROUND(((T1.dou_druc + T1.dou_drac1 + T1.dou_drac2 + T1.dou_drac3 + T1.dou_drac4 + T1.dou_drac5) - (T1.dou_cruc + T1.dou_crac1 + T1.dou_crac2 + T1.dou_crac3 + T1.dou_crac4 + T1.dou_crac5)) / T1.dou_qty, ", @P_DEC_COST, ") AS `Current Price`
				FROM ame_sl_ic_batch T1 WHERE T1.dou_qty > 0 AND T1.var_itemid IN('", P4,"') ORDER BY T1.var_itemid ASC, T1.int_batchno ASC) AS T1 WHERE T1.P_LINE = 0");
		WHEN "Last Purchase Cost" THEN
				SET @LINE = 0;
				SET @ITEM = "";
				SET @SQL_EXC = CONCAT("SELECT T1.var_itemid, T1.`Current Price` FROM (SELECT IF(@ITEM <> T1.var_itemid, 0, @LINE := @LINE + 1) AS `P_LINE`, @ITEM := T1.var_itemid AS `P_ITEM`, T1.var_itemid, 
				AME_FUN_ROUND(((T1.dou_druc + T1.dou_drac1 + T1.dou_drac2 + T1.dou_drac3 + T1.dou_drac4 + T1.dou_drac5) - (T1.dou_cruc + T1.dou_crac1 + T1.dou_crac2 + T1.dou_crac3 + T1.dou_crac4 + T1.dou_crac5)) / T1.dou_qty, ", @P_DEC_COST, ") AS `Current Price`
				FROM ame_sl_ic_batch T1 WHERE T1.dou_qty > 0 AND T1.var_itemid IN('", P4,"') ORDER BY T1.var_itemid ASC, T1.int_batchno DESC) AS T1 WHERE T1.P_LINE = 0");
		WHEN "Specific Batch" THEN
				SET @SQL_EXC = CONCAT("SELECT T1.var_itemid, 
				AME_FUN_ROUND(((T1.dou_druc + T1.dou_drac1 + T1.dou_drac2 + T1.dou_drac3 + T1.dou_drac4 + T1.dou_drac5) - (T1.dou_cruc + T1.dou_crac1 + T1.dou_crac2 + T1.dou_crac3 + T1.dou_crac4 + T1.dou_crac5)) / T1.dou_qty, ", @P_DEC_COST, ") AS `Current Price`
				FROM ame_sl_ic_batch T1 WHERE T1.dou_qty > 0 AND T1.int_batchno = ", P2," AND T1.var_locid = '", P3,"' AND T1.var_itemid IN('", P4,"')");
		END CASE;
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_ITE_MOVE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_ITE_MOVE`(P1 varchar(100), P2 varchar(20), P3 varchar(20), P4 date, P5 date, P6 varchar(250))
    DETERMINISTIC
BEGIN
		DECLARE P_DEC_AMT INTEGER DEFAULT 0;
		DECLARE P_DEC_QTY INTEGER DEFAULT 0;
		DECLARE P_DEC_PRICE INTEGER DEFAULT 0;
		DECLARE P_H VARCHAR(2000) DEFAULT "";
		BEGIN DECLARE C1 CURSOR FOR SELECT T.int_qty, 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_PRICE, P_DEC_AMT; END IF;CLOSE C1; END;
		SELECT GROUP_CONCAT(T1.var_header) INTO P_H FROM ame_sys_add_cost T1 ORDER BY T1.int_line;
		SET @P_BAL := 0.0;
		SET @SQL_EXC = CONCAT("SELECT T1.var_modid AS 'Module', T1.var_doctype AS 'Type', T1.var_docid AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc_det AS Description, T1.int_batchno AS `Batch No`, T1.var_locid AS 'Location Code', AME_FUN_ROUND(T1.dou_qty, ", P_DEC_QTY,") AS Quantity, AME_FUN_ROUND(@P_BAL := @P_BAL + T1.dou_qty, ", P_DEC_QTY, ") AS Balance, AME_FUN_ROUND(T1.dou_uc, ", P_DEC_PRICE,") AS 'Item Cost', AME_FUN_ROUND(T1.dou_ac1, ", P_DEC_PRICE,") AS `", AME_FUN_SPLIT_STRING(P_H, ',', 1),"`, AME_FUN_ROUND(T1.dou_ac2, ", P_DEC_PRICE,") AS `", AME_FUN_SPLIT_STRING(P_H, ',', 2),"`, AME_FUN_ROUND(T1.dou_ac3, ", P_DEC_PRICE,") AS `", AME_FUN_SPLIT_STRING(P_H, ',', 3),"`, AME_FUN_ROUND(T1.dou_ac4, ", P_DEC_PRICE,") AS `", AME_FUN_SPLIT_STRING(P_H, ',', 4),"`, AME_FUN_ROUND(T1.dou_ac5, ", P_DEC_PRICE,") AS `", AME_FUN_SPLIT_STRING(P_H, ',', 5),"` FROM ame_sl_ic_his T1 WHERE T1.var_itemid = '", P1,"' AND
		IF('", P2, "' = 'All' OR '", P2, "' = '', TRUE, T1.var_locid = '", P2, "') AND T1.var_modid IN('", P6 , "') AND
		(CASE
		WHEN '", P3, "' = 'ALL' THEN TRUE
		WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
		WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
		WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
		ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.int_id, T1.int_line");
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_ITE_PRI`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_ITE_PRI`(P1 varchar(100), P2 varchar(100), P3 varchar(100), P4 varchar(100), P5 varchar(100), P6 varchar(100), P7 varchar(100))
    DETERMINISTIC
BEGIN
		IF P6 = "Average Cost" THEN
				SELECT 0  AS '',  T1.var_id AS `Item ID`, T1.var_name AS `Item Name`, T1.tin_inactive AS 'Inactive', T2.var_uomid AS `Uom ID`, COALESCE(T3.`Current Cost`, 0) AS `Current Cost`, COALESCE(T2.dou_price, 0) AS `Current Price`, 0.0 AS `New Price`
				FROM ame_mt_ite T1 
				INNER JOIN (SELECT T1.var_id, T1.var_uomid, T1.int_line, COALESCE(T2.dou_price, 0) AS `dou_price` FROM ame_mt_ite_uom T1 LEFT JOIN (SELECT T1.var_id, T1.var_uomid, T1.dou_price FROM ame_mt_ite_pri T1 WHERE T1.var_pricecode = P2 AND T1.var_cur = P3) AS T2 ON T1.var_id = T2.var_id AND T1.var_uomid = T2.var_uomid WHERE IF(P4 = "0", TRUE, IF(P4 = "1", T1.tin_def = 1, T1.tin_def <> 1))) AS T2 ON T1.var_id = T2.var_id
				LEFT JOIN (SELECT T1.var_itemid, SUM((T1.dou_druc + T1.dou_drac1 + T1.dou_drac2 + T1.dou_drac3 + T1.dou_drac4 + T1.dou_drac5) - (T1.dou_cruc + T1.dou_crac1 + T1.dou_crac2 + T1.dou_crac3 + T1.dou_crac4 + T1.dou_crac5)) / SUM(T1.dou_qty) AS `Current Cost`FROM ame_sl_ic_batch T1 WHERE T1.dou_qty > 0 AND IF(P7 = 'All', TRUE, T1.var_locid = P7) GROUP BY T1.var_itemid) AS T3 ON T1.var_id = T3.var_itemid
				WHERE T1.var_type = P1 AND IF(P5 = "1", TRUE, T1.tin_inactive = 0)
				ORDER BY T1.var_id, T2.int_line;
		ELSE
				SET @LINE = 0;
				SET @ITEM = "";
				SELECT 0  AS '',  T1.var_id AS `Item ID`, T1.var_name AS `Item Name`, T1.tin_inactive AS 'Inactive', T2.var_uomid AS `Uom ID`, COALESCE(T3.`Current Cost`, 0) AS `Current Cost`, COALESCE(T2.dou_price, 0) AS `Current Price`, 0.0 AS `New Price`
				FROM ame_mt_ite T1 
				INNER JOIN (SELECT T1.var_id, T1.var_uomid, T1.int_line, COALESCE(T2.dou_price, 0) AS `dou_price` FROM ame_mt_ite_uom T1 LEFT JOIN (SELECT T1.var_id, T1.var_uomid, T1.dou_price FROM ame_mt_ite_pri T1 WHERE T1.var_pricecode = P2 AND T1.var_cur = P3) AS T2 ON T1.var_id = T2.var_id AND T1.var_uomid = T2.var_uomid WHERE IF(P4 = "0", TRUE, IF(P4 = "1", T1.tin_def = 1, T1.tin_def <> 1))) AS T2 ON T1.var_id = T2.var_id
				LEFT JOIN (SELECT T1.var_itemid, T1.`Current Cost` FROM (SELECT IF(@ITEM <> T1.var_itemid, 0, @LINE := @LINE + 1) AS `P_LINE`, @ITEM := T1.var_itemid AS `P_ITEM`, T1.var_itemid, 
				((T1.dou_druc + T1.dou_drac1 + T1.dou_drac2 + T1.dou_drac3 + T1.dou_drac4 + T1.dou_drac5) - (T1.dou_cruc + T1.dou_crac1 + T1.dou_crac2 + T1.dou_crac3 + T1.dou_crac4 + T1.dou_crac5)) / T1.dou_qty AS `Current Cost`
				FROM ame_sl_ic_batch T1 WHERE T1.dou_qty > 0 ORDER BY T1.var_itemid ASC, T1.int_batchno DESC) AS T1 WHERE T1.P_LINE = 0) AS T3 ON T1.var_id = T3.var_itemid
				WHERE T1.var_type = P1 AND IF(P5 = "1", TRUE, T1.tin_inactive = 0)
				ORDER BY T1.var_id, T2.int_line;
		END IF;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_ITE_PRI_HIS`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_ITE_PRI_HIS`(P1 text)
    DETERMINISTIC
BEGIN
		SET @SQL_EXC = CONCAT("SELECT DISTINCT T1.var_id, T1.var_pricecode, T1.var_uomid, T1.var_cur, COALESCE(T1.dou_price, 0) AS `dou_price` FROM ame_mt_ite_pri_his T1 
		WHERE T1.var_id IN('", P1, "') AND T1.int_line IN(SELECT MAX(T2.int_line) FROM ame_mt_ite_pri_his T2 WHERE T2.var_id IN('", P1, "') GROUP BY T2.var_id, T2.var_pricecode, T2.var_uomid, T2.var_cur)");
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_ITE_TRAN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_ITE_TRAN`(P1 varchar(100), P2 varchar(20), P3 varchar(20), P4 date, P5 date, P6 tinyint(1), P7 tinyint(1), P8 tinyint(1), P9 tinyint(1), P10 tinyint(1), P11 tinyint(1), P12 tinyint(1), P13 tinyint(1), P14 tinyint(1), P15 tinyint(1), P16 tinyint(1), P17 tinyint(1), P18 tinyint(1), P19 tinyint(1))
    DETERMINISTIC
BEGIN
		SET @SQL_EXC = "SELECT T1.`Module`, T1.`Type`, T1.`Entry No`, T1.`Reference`, T1.`Date`, T1.`Description`,
										T1.`Location Id`, T1.`Uom Id`, T1.`Quantity`, T1.`FOC`, T1.`Price`, T1.`Sub Total`, T1.`Disc.`, T1.`Grand Total` FROM ((SELECT 0 AS `SORTKEY`, 'Module', 'Type', 'Entry No', 'Reference', NOW() AS 'Date', 'Status', 'Description', 'Location Id', 'Uom Id', 0.0 AS 'Quantity', 0.0 AS `FOC`, 0.0 AS 'Price', 0.0 AS 'Sub Total', 0.0 AS 'Disc.', 0.0 AS 'Grand Total' LIMIT 0)";
		IF P6 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 1 AS `SORTKEY`, 'AP-PO' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, T2.dou_foc AS `FOC`, T2.dou_price AS `Price`, T2.dou_sub AS `Sub Total`, T2.dou_disdol AS `Disc.`, T2.dou_net AS `Grand Total` FROM ame_ap_po T1 LEFT JOIN ame_ap_po_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P7 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 2 AS `SORTKEY`, 'AP-IN' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, T2.dou_foc AS `FOC`, T2.dou_price AS `Price`, T2.dou_sub AS `Sub Total`, T2.dou_disdol AS `Disc.`, T2.dou_net AS `Grand Total` FROM ame_ap_in T1 LEFT JOIN ame_ap_in_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P8 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 3 AS `SORTKEY`, 'AP-RET' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, T2.dou_foc AS `FOC`, T2.dou_price AS `Price`, T2.dou_sub AS `Sub Total`, T2.dou_disdol AS `Disc.`, T2.dou_net AS `Grand Total` FROM ame_ap_ret T1 LEFT JOIN ame_ap_ret_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P9 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 4 AS `SORTKEY`, 'AP-DN' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, T2.dou_foc AS `FOC`, T2.dou_price AS `Price`, T2.dou_sub AS `Sub Total`, T2.dou_disdol AS `Disc.`, T2.dou_net AS `Grand Total` FROM ame_ap_dn T1 LEFT JOIN ame_ap_dn_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P10 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 5 AS `SORTKEY`, 'AR-QUO' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, T2.dou_foc AS `FOC`, T2.dou_price AS `Price`, T2.dou_sub AS `Sub Total`, T2.dou_disdol AS `Disc.`, T2.dou_net AS `Grand Total` FROM ame_ar_quo T1 LEFT JOIN ame_ar_quo_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P11 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 6 AS `SORTKEY`, 'AR-SO' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, T2.dou_foc AS `FOC`, T2.dou_price AS `Price`, T2.dou_sub AS `Sub Total`, T2.dou_disdol AS `Disc.`, T2.dou_net AS `Grand Total` FROM ame_ar_so T1 LEFT JOIN ame_ar_so_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P12 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 7 AS `SORTKEY`, 'AR-IN' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, T2.dou_foc AS `FOC`, T2.dou_price AS `Price`, T2.dou_sub AS `Sub Total`, T2.dou_disdol AS `Disc.`, T2.dou_net AS `Grand Total` FROM ame_ar_in T1 LEFT JOIN ame_ar_in_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P13 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 7 AS `SORTKEY`, 'AR-RET' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, T2.dou_foc AS `FOC`, T2.dou_price AS `Price`, T2.dou_sub AS `Sub Total`, T2.dou_disdol AS `Disc.`, T2.dou_net AS `Grand Total` FROM ame_ar_ret T1 LEFT JOIN ame_ar_ret_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P14 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 8 AS `SORTKEY`, 'AR-CN' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, T2.dou_foc AS `FOC`, T2.dou_price AS `Price`, T2.dou_sub AS `Sub Total`, T2.dou_disdol AS `Disc.`, T2.dou_net AS `Grand Total` FROM ame_ar_cn T1 LEFT JOIN ame_ar_cn_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P15 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 9 AS `SORTKEY`, 'IC-TF' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, 0.0 AS `FOC`, 0.0 AS `Price`, 0.0 AS `Sub Total`, 0.0 AS `Disc.`, 0.0 AS `Grand Total` FROM ame_ic_tf T1 LEFT JOIN ame_ic_tf_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P16 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 10 AS `SORTKEY`, 'IC-IU' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, 0.0 AS `FOC`, 0.0 AS `Price`, 0.0 AS `Sub Total`, 0.0 AS `Disc.`, 0.0 AS `Grand Total` FROM ame_ic_iu T1 LEFT JOIN ame_ic_iu_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P17 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 11 AS `SORTKEY`, 'IC-ICUOM' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, 0.0 AS `FOC`, 0.0 AS `Price`, 0.0 AS `Sub Total`, 0.0 AS `Disc.`, 0.0 AS `Grand Total` FROM ame_ic_icuom T1 LEFT JOIN ame_ic_icuom_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P18 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 12 AS `SORTKEY`, 'IC-IP' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, 0.0 AS `FOC`, 0.0 AS `Price`, 0.0 AS `Sub Total`, 0.0 AS `Disc.`, 0.0 AS `Grand Total` FROM ame_ic_ip T1 LEFT JOIN ame_ic_ip_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		IF P19 = 1 THEN SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 13 AS `SORTKEY`, 'IC-IA' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.dat_date AS Date, var_status AS Status, T2.var_desc AS Description, T2.var_locid AS `Location Id`, T2.var_uomid AS `Uom Id`, T2.dou_qty AS `Quantity`, 0.0 AS `FOC`, T2.dou_price AS `Price`, T2.dou_qty * T2.dou_price AS `Sub Total`, 0.0 AS `Disc.`, 0.0 AS `Grand Total` FROM ame_ic_ia T1 LEFT JOIN ame_ic_ia_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END))");
		END IF;
		SET @SQL_EXC = CONCAT(@SQL_EXC, ") AS T1 ORDER BY T1.SORTKEY, T1.`Entry No`");
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_OF`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_OF`(P1 varchar(100))
    DETERMINISTIC
BEGIN

	CASE P1
		WHEN "MT_ITE" THEN
				SELECT 
				COALESCE(o.int_id,0) AS int_id,
				COALESCE(o.var_name,'') AS var_name,
				COALESCE(o.var_type,'') AS var_type,
				COALESCE(o.tin_req,0) AS tin_req
				FROM ame_mt_of o WHERE COALESCE(tin_inactive,0) = 0 AND tin_item = 1
				ORDER BY o.int_sort,o.int_id;
		WHEN "MT_CUS" THEN
				SELECT 
				COALESCE(o.int_id,0) AS int_id,
				COALESCE(o.var_name,'') AS var_name,
				COALESCE(o.var_type,'') AS var_type,
				COALESCE(o.tin_req,0) AS tin_req
				FROM ame_mt_of o WHERE COALESCE(tin_inactive,0) = 0 AND tin_cus = 1
				ORDER BY o.int_sort,o.int_id;
		WHEN "MT_EMP" THEN
				SELECT 
				COALESCE(o.int_id,0) AS int_id,
				COALESCE(o.var_name,'') AS var_name,
				COALESCE(o.var_type,'') AS var_type,
				COALESCE(o.tin_req,0) AS tin_req
				FROM ame_mt_of o WHERE COALESCE(tin_inactive,0) = 0 AND tin_emp = 1
				ORDER BY o.int_sort,o.int_id;
		WHEN "MT_VEN" THEN
				SELECT 
				COALESCE(o.int_id,0) AS int_id,
				COALESCE(o.var_name,'') AS var_name,
				COALESCE(o.var_type,'') AS var_type,
				COALESCE(o.tin_req,0) AS tin_req
				FROM ame_mt_of o WHERE COALESCE(tin_inactive,0) = 0 AND tin_ven = 1
				ORDER BY o.int_sort,o.int_id;
		ELSE
				SET @NULL = NULL;
	END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_OF_VAL`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_OF_VAL`(P1 varchar(100),P2 varchar(100))
    DETERMINISTIC
BEGIN
	SELECT 
	T1.int_id,
	COALESCE(T1.var_value,'') AS var_value,
	COALESCE(T2.var_type,'') AS var_type
	FROM ame_mt_of_value T1 INNER JOIN ame_mt_of T2 ON T1.int_id = T2.int_id 
	WHERE T1.var_modid = P1 AND T1.var_num = P2 
	ORDER BY T2.int_sort,T2.int_id;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_PEOPLE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_PEOPLE`(P1 varchar(100), P2 varchar(100))
    DETERMINISTIC
BEGIN
		CASE P1
		WHEN "MT-EMP" THEN
				SELECT T1.var_id FROM ame_mt_emp T1 WHERE T1.var_id = P2;
				SELECT SUM(T1.dou_cr - T1.dou_dr) AS P_BAL FROM ame_sl_ap T1 WHERE T1.var_venid = P2;
				SELECT T1.var_id, T1.var_name, T1.var_addr, T1.var_addr1, T1.var_addr2, T1.var_addr3, T1.var_addr4, T1.var_addr5, T1.var_addr6, T1.var_addr7, T1.tin_default FROM ame_mt_ship_addr T1 WHERE T1.var_num = P2 AND T1.var_type = 'MT-EMP' ORDER BY T1.var_name;
				SELECT COALESCE(SUM(T1.dou_amt), 0) FROM ame_mt_tem_tc T1 WHERE T1.var_type = "Customer" AND  T1.var_status = "Approved" AND T1.var_cusid = P2 AND DATE(T1.dat_to) >= DATE(NOW());
				SELECT COALESCE(SUM(T1.dou_net), 0) - COALESCE(T2.PAID, 0) FROM ame_ar_in T1 LEFT JOIN (SELECT T1.var_cusid, SUM(T1.dou_amt + T1.dou_disc + T1.dou_app) AS `PAID` FROM ame_ar_py T1 WHERE T1.var_cusid = P2 AND T1.var_type = 'Apply to Sale Invoice' AND T1.var_status <> 'Deleted') AS T2 ON T1.var_cusid = T2.var_cusid WHERE T1.var_cusid = P2 AND  T1.var_status <> 'Deleted';
				IF EXISTS(SELECT T1.var_id FROM ame_ar_quo T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_so T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_cn T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSE
						SELECT 0;
				END IF;
		WHEN "MT-VEN" THEN
				SELECT T1.var_id FROM ame_mt_ven T1 WHERE T1.var_id = P2;
				SELECT SUM(T1.dou_cr - T1.dou_dr) AS P_BAL FROM ame_sl_ap T1 WHERE T1.var_venid = P2;
				SELECT T1.var_id, T1.var_name, T1.var_addr, T1.var_addr1, T1.var_addr2, T1.var_addr3, T1.var_addr4, T1.var_addr5, T1.var_addr6, T1.var_addr7, T1.tin_default FROM ame_mt_ship_addr T1 WHERE T1.var_num = P2 AND T1.var_type = 'MT-VEN' ORDER BY T1.var_name;
				SELECT COALESCE(SUM(T1.dou_amt), 0) FROM ame_mt_tem_tc T1 WHERE T1.var_type = "Vendor" AND T1.var_status = "Approved" AND T1.var_cusid = P2 AND DATE(T1.dat_to) >= DATE(NOW());
				SELECT COALESCE(SUM(T1.dou_net), 0) - COALESCE(T2.PAID, 0) FROM ame_ap_in T1 LEFT JOIN (SELECT T1.var_venid, SUM(T1.dou_amt + T1.dou_disc + T1.dou_app) AS `PAID` FROM ame_ap_py T1 WHERE T1.var_venid = P2 AND T1.var_type = 'Apply to Purchase Invoice' AND T1.var_status <> 'Deleted') AS T2 ON T1.var_venid = T2.var_venid WHERE T1.var_venid = P2 AND  T1.var_status <> 'Deleted';
				IF EXISTS(SELECT T1.var_id FROM ame_ap_po T1 WHERE T1.var_venid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_in T1 WHERE T1.var_venid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_dn T1 WHERE T1.var_venid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_py T1 WHERE T1.var_venid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSE
						SELECT 0;
				END IF;
		WHEN "MT-CUS" THEN
				SELECT T1.var_id FROM ame_mt_cus T1 WHERE T1.var_id = P2;
				SELECT SUM(T1.dou_dr - T1.dou_cr) AS P_BAL FROM ame_sl_ar T1 WHERE T1.var_cusid = P2;
				SELECT T1.var_id, T1.var_name, T1.var_addr, T1.var_addr1, T1.var_addr2, T1.var_addr3, T1.var_addr4, T1.var_addr5, T1.var_addr6, T1.var_addr7, T1.tin_default FROM ame_mt_ship_addr T1 WHERE T1.var_num = P2 AND T1.var_type = 'MT-CUS' ORDER BY T1.var_name;
				SELECT COALESCE(SUM(T1.dou_amt), 0) FROM ame_mt_tem_tc T1 WHERE T1.var_type = "Customer" AND  T1.var_status = "Approved" AND T1.var_cusid = P2 AND DATE(T1.dat_to) >= DATE(NOW());
				SELECT COALESCE(SUM(T1.dou_net), 0) - COALESCE(T2.PAID, 0) FROM ame_ar_in T1 LEFT JOIN (SELECT T1.var_cusid, SUM(T1.dou_amt + T1.dou_disc + T1.dou_app) AS `PAID` FROM ame_ar_py T1 WHERE T1.var_cusid = P2 AND T1.var_type = 'Apply to Sale Invoice' AND T1.var_status <> 'Deleted') AS T2 ON T1.var_cusid = T2.var_cusid WHERE T1.var_cusid = P2 AND  T1.var_status <> 'Deleted';
				IF EXISTS(SELECT T1.var_id FROM ame_ar_quo T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_so T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_cn T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_py T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						SELECT 1;
				ELSE
						SELECT 0;
				END IF;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_PREFIX`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_PREFIX`()
    DETERMINISTIC
BEGIN
		#NOTE: P1 = PAYMENT NO , P2 = TYPE
		SELECT T1.var_modid AS 'Prefix ID', T1.var_desc AS 'Description', T1.var_id AS 'Prefix Letter', T1.int_length AS 'Number Length', T1.int_lastid AS 'Next Number', CONCAT(T1.var_id, LPAD(T1.int_lastid, T1.int_length, '0')) AS 'Number Format' FROM ame_sys_prefix T1 INNER JOIN ame_sys_form T2 ON T1.var_modid = REPLACE(T2.frmsysname, "_", "-") ORDER BY T1.int_line;
		SELECT T1.var_modid AS 'Prefix ID', T1.var_desc AS 'Description', T1.var_id AS 'Prefix Letter', T1.int_length AS 'Number Length', T1.int_lastid AS 'Next Number', CONCAT(T1.var_id, LPAD(T1.int_lastid, T1.int_length, '0')) AS 'Number Format', T1.var_type AS 'Prefix Type' FROM ame_sys_prefix_custom T1 ORDER BY T1.var_modid;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_USER`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_USER`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		SELECT T1.var_appid FROM ame_user_app T1 WHERE T1.var_id = P1;
		SELECT T1.var_empid, COALESCE(T2.var_name, '') AS `var_name` FROM ame_user T1 LEFT JOIN ame_mt_emp T2 ON T1.var_empid = T2.var_id WHERE T1.var_id = P1;
		SELECT COALESCE(T1.var_name, '') AS `var_name` FROM ame_sys_role T1 WHERE T1.int_id NOT IN(SELECT T2.int_id FROM ame_user_role T2 WHERE T2.var_id = P1) ORDER BY T1.var_name;
		SELECT COALESCE(T2.var_name, '') AS `var_name` FROM ame_user_role T1 LEFT JOIN ame_sys_role T2 ON T1.int_id = T2.int_id WHERE T1.var_id = P1 ORDER BY T2.var_name;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_MT_VEN_TRAN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_MT_VEN_TRAN`(P1 varchar(100), P2 varchar(20), P3 varchar(20), P4 date, P5 date, P6 tinyint(1), P7 tinyint(1), P8 tinyint(1), P9 tinyint(1), P10 tinyint(1))
    DETERMINISTIC
BEGIN
		SET @SQL_EXC = "(SELECT 'Module', 'Type', 'Entry No', 'Referene', NOW() AS 'Date', 'Remark', 'Status', 0.0 AS 'Grand Total' LIMIT 0)";
		IF P6 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AP-PO' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ap_po T1 WHERE T1.var_venid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P7 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AP-IN' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ap_in T1 WHERE T1.var_venid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P8 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AP-RET' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ap_ret T1 WHERE T1.var_venid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P9 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AP-DN' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ap_dn T1 WHERE T1.var_venid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		IF P10 = 1 THEN 
				SET @SQL_EXC = CONCAT(@SQL_EXC, " UNION ALL (SELECT 'AP-PY' AS 'Module', T1.var_type AS 'Type', T1.var_id AS 'Entry No', T1.var_ref AS 'Referene', T1.dat_date AS Date, T1.var_desc AS Remark, var_status AS Status, T1.dou_net AS 'Grand Total' FROM ame_ap_py T1 WHERE T1.var_venid = '", P1,"' AND
				IF('", P2, "' = 'All', TRUE, T1.var_status = '", P2, "') AND
				(CASE
				WHEN '", P3, "' = 'ALL' THEN TRUE
				WHEN '", P3, "' = 'THIS TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				WHEN '", P3, "' = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH('", P4, "')
				WHEN '", P3, "' = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR('", P4, "')
				ELSE DATE(T1.dat_date) BETWEEN DATE('", P4, "') AND DATE('", P5, "') END) ORDER BY T1.var_id)");
		END IF;
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_ORDER`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_ORDER`(P1 varchar(100), P2 varchar(2000), P3 varchar(100), P4 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		BEGIN	DECLARE C1 CURSOR FOR SELECT T.int_qty, T.int_amt FROM ame_sys_decimal T WHERE T.var_id = P4; OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_QTY, P_DEC_AMT; END IF;CLOSE C1; END;
		SET @SQL_EXC = "";
		CASE P1
		WHEN "AP-PO" THEN	
				SET @SQL_EXC = CONCAT("SELECT T1.var_venid, COALESCE(T2.var_name, '') FROM ame_ap_po T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id WHERE T1.var_id IN(", P2,") LIMIT 1");	
				PREPARE STM FROM @SQL_EXC;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;	
				SET @SQL_EXC = CONCAT("SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, AME_FUN_ROUND(COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0), ", P_DEC_QTY,") AS `dou_qty`, T1.dou_price, AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price, ", P_DEC_AMT,"), T1.dou_disper, AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price * T1.dou_disper / 100, ", P_DEC_AMT,"), AME_FUN_ROUND(AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price, ", P_DEC_AMT,") - AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price * T1.dou_disper / 100, ", P_DEC_AMT,"), ", P_DEC_AMT, "), AME_FUN_ROUND(COALESCE(T1.dou_foc, 0) - COALESCE(T1.dou_orderfoc, 0) + COALESCE(T3.dou_foc, 0), ", P_DEC_QTY, ") AS `dou_foc`, T1.tin_vat, T1.tin_wht, T1.int_poline, T1.var_id FROM ame_ap_po_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
				LEFT JOIN ame_ap_in_det T3 ON (T1.var_id = T3.var_poid AND T1.int_poline = T3.int_poline AND T1.var_locid = T3.var_locid AND T1.var_itemid = T3.var_itemid AND T1.var_uomid = T3.var_uomid AND T3.var_id = '", P3, "') WHERE T1.var_id IN(", P2,") ORDER BY T1.var_id, T1.int_line");
		WHEN "AP-IN" THEN	
				SET @SQL_EXC = CONCAT("SELECT T1.var_venid, COALESCE(T2.var_name, '') FROM ame_ap_in T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id WHERE T1.var_id IN(", P2,") LIMIT 1");	
				PREPARE STM FROM @SQL_EXC;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;	
				SET @SQL_EXC = CONCAT("SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, IF(ISNULL(T3.dou_qty), 0, T3.dou_qty) AS `dou_qty`, IF(ISNULL(T3.dou_price), T1.dou_price, T3.dou_price) AS `dou_price`, IF(ISNULL(T3.dou_sub), 0, T3.dou_sub) AS `dou_sub`, IF(ISNULL(T3.dou_disper), T1.dou_disper, T3.dou_disper) AS `dou_disper`, IF(ISNULL(T3.dou_disdol), 0, T3.dou_disdol) AS `dou_disdol`, IF(ISNULL(T3.dou_net), 0, T3.dou_net) AS `dou_net`, IF(ISNULL(T3.dou_foc), 0, T3.dou_foc) AS `dou_foc`, IF(ISNULL(T3.tin_vat), T1.tin_vat, T3.tin_vat) AS `tin_vat`, IF(ISNULL(T3.tin_wht), T1.tin_wht, T3.tin_wht) AS `tin_wht`, T1.dou_qty AS `dou_purqty`, AME_FUN_ROUND(COALESCE(T1.dou_orderqty, 0) - COALESCE(T3.dou_qty, 0), ", P_DEC_QTY,") AS `dou_orderqty`, T1.dou_foc AS `dou_purfoc`, AME_FUN_ROUND(COALESCE(T1.dou_orderfoc, 0) - COALESCE(T3.dou_foc, 0), ", P_DEC_QTY,") AS `dou_orderfoc`, T1.int_batchno, T1.int_poline, T1.var_id FROM ame_ap_in_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
				LEFT JOIN ame_ap_ret_det T3 ON (T1.var_id = T3.var_poid AND T1.int_poline = T3.int_poline AND T1.var_locid = T3.var_locid AND T1.var_itemid = T3.var_itemid AND T1.var_uomid = T3.var_uomid AND T3.var_id = '", P3, "') WHERE T1.var_id IN(", P2,") ORDER BY T1.var_id, T1.int_line");	
		WHEN "AR-SO" THEN	
				SET @SQL_EXC = CONCAT("SELECT T1.var_cusid, COALESCE(T2.var_name, '') FROM ame_ar_quo T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id WHERE T1.var_id IN(", P2,") LIMIT 1");	
				PREPARE STM FROM @SQL_EXC;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;	
				SET @SQL_EXC = CONCAT("SELECT 
				T1.var_itemid, 
				T2.var_name, 
				T1.var_desc, 
				T1.var_locid, 
				T1.var_claid, 
				T1.var_uomid, 
				AME_FUN_ROUND(COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0), ", P_DEC_QTY,") AS `dou_qty`, 
				T1.dou_price, 
				AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price, ", P_DEC_AMT,"), 
				T1.dou_disper, 
				AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price * T1.dou_disper / 100, ", P_DEC_AMT,"), 
				AME_FUN_ROUND(AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price, ", P_DEC_AMT,") - AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price * T1.dou_disper / 100, ", P_DEC_AMT,"), ", P_DEC_AMT, "), 
				AME_FUN_ROUND(COALESCE(T1.dou_foc, 0) - COALESCE(T1.dou_orderfoc, 0) + COALESCE(T3.dou_foc, 0), ", P_DEC_QTY, ") AS `dou_foc`, 
				T1.tin_vat, 
				T1.tin_wht, 
				0, 
				0,
				T1.int_poline, 
				T1.dou_pricecom, 
				T1.var_id FROM ame_ar_quo_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
				LEFT JOIN ame_ar_so_det T3 ON (T1.var_id = T3.var_poid AND T1.int_poline = T3.int_poline AND T1.var_locid = T3.var_locid AND T1.var_itemid = T3.var_itemid AND T1.var_uomid = T3.var_uomid AND T3.var_id = '", P3, "') WHERE T1.var_id IN(", P2,") ORDER BY T1.var_id, T1.int_line");
		WHEN "AR-IN-QUO" THEN	
				SET @SQL_EXC = CONCAT("SELECT T1.var_cusid, COALESCE(T2.var_name, '') FROM ame_ar_quo T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id WHERE T1.var_id IN(", P2,") LIMIT 1");	
				PREPARE STM FROM @SQL_EXC;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;	
				SET @SQL_EXC = CONCAT("SELECT 
				T1.var_itemid, 
				T2.var_name, 
				T1.var_desc, 
				T1.var_locid, 
				T1.var_claid, 
				T1.var_uomid, 
				AME_FUN_ROUND(COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0), ", P_DEC_QTY,") AS `dou_qty`, 
				T1.dou_price, 
				AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price, ", P_DEC_AMT,"), 
				T1.dou_disper, 
				AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price * T1.dou_disper / 100, ", P_DEC_AMT,"), 
				AME_FUN_ROUND(AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price, ", P_DEC_AMT,") - AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price * T1.dou_disper / 100, ", P_DEC_AMT,"), ", P_DEC_AMT, "), 
				AME_FUN_ROUND(COALESCE(T1.dou_foc, 0) - COALESCE(T1.dou_orderfoc, 0) + COALESCE(T3.dou_foc, 0), ", P_DEC_QTY, ") AS `dou_foc`, 
				T1.tin_vat, 
				T1.tin_wht, 
				0, 
				0,
				T1.int_poline, 
				T1.dou_pricecom, 
				T1.var_id, 'AR-QUO' AS `NumType` FROM ame_ar_quo_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
				LEFT JOIN ame_ar_in_det T3 ON (T1.var_id = T3.var_poid AND T1.int_poline = T3.int_poline AND T1.var_locid = T3.var_locid AND T1.var_itemid = T3.var_itemid AND T1.var_uomid = T3.var_uomid AND T3.var_id = '", P3, "' AND T3.var_potype = 'AR-QUO') WHERE T1.var_id IN(", P2,") ORDER BY T1.var_id, T1.int_line");
		WHEN "AR-IN-SO" THEN	
				SET @SQL_EXC = CONCAT("SELECT T1.var_cusid, COALESCE(T2.var_name, '') FROM ame_ar_quo T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id WHERE T1.var_id IN(", P2,") LIMIT 1");	
				PREPARE STM FROM @SQL_EXC;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;	
				SET @SQL_EXC = CONCAT("SELECT 
				T1.var_itemid, 
				T2.var_name, 
				T1.var_desc, 
				T1.var_locid, 
				T1.var_claid, 
				T1.var_uomid, 
				AME_FUN_ROUND(COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0), ", P_DEC_QTY,") AS `dou_qty`, 
				T1.dou_price, 
				AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price, ", P_DEC_AMT,"), 
				T1.dou_disper, 
				AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price * T1.dou_disper / 100, ", P_DEC_AMT,"), 
				AME_FUN_ROUND(AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price, ", P_DEC_AMT,") - AME_FUN_ROUND((COALESCE(T1.dou_qty, 0) - COALESCE(T1.dou_orderqty, 0) + COALESCE(T3.dou_qty, 0)) * T1.dou_price * T1.dou_disper / 100, ", P_DEC_AMT,"), ", P_DEC_AMT, "), 
				AME_FUN_ROUND(COALESCE(T1.dou_foc, 0) - COALESCE(T1.dou_orderfoc, 0) + COALESCE(T3.dou_foc, 0), ", P_DEC_QTY, ") AS `dou_foc`, 
				T1.tin_vat, 
				T1.tin_wht, 
				0, 
				0,
				T1.int_poline, 
				T1.dou_pricecom, 
				T1.var_id, 'AR-SO' AS `NumType` FROM ame_ar_so_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
				LEFT JOIN ame_ar_in_det T3 ON (T1.var_id = T3.var_poid AND T1.int_poline = T3.int_poline AND T1.var_locid = T3.var_locid AND T1.var_itemid = T3.var_itemid AND T1.var_uomid = T3.var_uomid AND T3.var_id = '", P3, "' AND T3.var_potype = 'AR-SO') WHERE T1.var_id IN(", P2,") ORDER BY T1.var_id, T1.int_line");
		WHEN "AR-RET" THEN	
				SET @SQL_EXC = CONCAT("SELECT T1.var_cusid, COALESCE(T2.var_name, '') FROM ame_ar_in T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id WHERE T1.var_id IN(", P2,") LIMIT 1");	
				PREPARE STM FROM @SQL_EXC;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;	
				SET @SQL_EXC = CONCAT("SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_locid, T1.var_claid, T1.var_uomid, IF(ISNULL(T3.dou_qty), 0, T3.dou_qty) AS `dou_qty`, IF(ISNULL(T3.dou_price), T1.dou_price, T3.dou_price) AS `dou_price`, IF(ISNULL(T3.dou_sub), 0, T3.dou_sub) AS `dou_sub`, IF(ISNULL(T3.dou_disper), T1.dou_disper, T3.dou_disper) AS `dou_disper`, IF(ISNULL(T3.dou_disdol), 0, T3.dou_disdol) AS `dou_disdol`, IF(ISNULL(T3.dou_net), 0, T3.dou_net) AS `dou_net`, IF(ISNULL(T3.dou_foc), 0, T3.dou_foc) AS `dou_foc`, IF(ISNULL(T3.tin_vat), T1.tin_vat, T3.tin_vat) AS `tin_vat`, IF(ISNULL(T3.tin_wht), T1.tin_wht, T3.tin_wht) AS `tin_wht`, T1.dou_qty AS `dou_purqty`, AME_FUN_ROUND(COALESCE(T1.dou_orderqty, 0) - COALESCE(T3.dou_qty, 0), ", P_DEC_QTY,") AS `dou_orderqty`, T1.dou_foc AS `dou_purfoc`, AME_FUN_ROUND(COALESCE(T1.dou_orderfoc, 0) - COALESCE(T3.dou_foc, 0), ", P_DEC_QTY,") AS `dou_orderfoc`, T1.int_batchno, T1.int_poline, T1.var_id, IF(ISNULL(T3.dou_pricecom), T1.dou_pricecom, T3.dou_pricecom) AS `dou_pricecom` FROM ame_ar_in_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
				LEFT JOIN ame_ar_ret_det T3 ON (T1.var_id = T3.var_poid AND T1.int_poline = T3.int_poline AND T1.var_locid = T3.var_locid AND T1.var_itemid = T3.var_itemid AND T1.var_uomid = T3.var_uomid AND T3.var_id = '", P3, "') WHERE T1.var_id IN(", P2,") ORDER BY T1.var_id, T1.int_line");	
		WHEN "AP-DN-GS" THEN	
				SET @SQL_EXC = CONCAT("SELECT T1.var_venid, COALESCE(T2.var_name, '') FROM ame_ap_in T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id WHERE T1.var_id IN(", P2,") LIMIT 1");	
				PREPARE STM FROM @SQL_EXC;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;	
				SET @SQL_EXC = CONCAT("SELECT 
				T1.var_itemid, 
				T2.var_name, 
				T1.var_desc, 
				T1.var_locid, 
				T1.var_claid, 
				T1.var_uomid, 
				COALESCE(T1.dou_qty, 0) AS `dou_qty`, 
				T1.dou_price, 
				T1.dou_sub, 
				T1.dou_disper, 
				T1.dou_disdol, 
				T1.dou_net, 
				T1.dou_foc, 
				T1.tin_vat, 
				T1.tin_wht, 
				T1.int_poline, 
				T1.var_id FROM ame_ap_in_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
				WHERE T1.var_id IN(", P2,") ORDER BY T1.var_id, T1.int_line");
		WHEN "AR-CN-GS" THEN	
				SET @SQL_EXC = CONCAT("SELECT T1.var_cusid, COALESCE(T2.var_name, '') FROM ame_ar_in T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id WHERE T1.var_id IN(", P2,") LIMIT 1");	
				PREPARE STM FROM @SQL_EXC;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;	
				SET @SQL_EXC = CONCAT("SELECT 
				T1.var_itemid, 
				T2.var_name, 
				T1.var_desc, 
				T1.var_locid, 
				T1.var_claid, 
				T1.var_uomid, 
				COALESCE(T1.dou_qty, 0) AS `dou_qty`, 
				T1.dou_price, 
				T1.dou_sub, 
				T1.dou_disper, 
				T1.dou_disdol, 
				T1.dou_net, 
				T1.dou_foc, 
				T1.tin_vat, 
				T1.tin_wht, 
				0, 
				0,
				T1.int_poline, 
				T1.dou_pricecom, 
				T1.var_id, '' AS `NumType` FROM ame_ar_in_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
				WHERE T1.var_id IN(", P2,") ORDER BY T1.var_id, T1.int_line");
		WHEN "AR-SM-IN" THEN		
				SET @SQL_EXC = CONCAT("SELECT T1.var_itemid, T2.var_name, IF(ISNULL(T3.var_desc), T1.var_desc, T3.var_desc), T1.var_locid, T1.var_uomid, T1.dou_qty AS `dou_purqty`, T1.dou_foc AS `dou_purfoc`, AME_FUN_ROUND(COALESCE(T1.dou_shipqty, 0) - COALESCE(T3.dou_qty, 0), ", P_DEC_QTY,") AS `dou_orderqty`, AME_FUN_ROUND(COALESCE(T1.dou_shipfoc, 0) - COALESCE(T3.dou_foc, 0), ", P_DEC_QTY,") AS `dou_orderfoc`, IF(ISNULL(T3.dou_qty), 0, T3.dou_qty) AS `dou_qty`, IF(ISNULL(T3.dou_foc), 0, T3.dou_foc) AS `dou_foc`, 0, T1.int_poline, T1.var_id FROM ame_ar_in_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
				LEFT JOIN ame_ar_sm_det T3 ON (T1.var_id = T3.var_poid AND T1.int_poline = T3.int_poline AND T1.var_locid = T3.var_locid AND T1.var_itemid = T3.var_itemid AND T1.var_uomid = T3.var_uomid AND T3.var_id = '", P3, "') WHERE T1.var_id IN(", P2,") ORDER BY T1.var_id, T1.int_line");	
		WHEN "AR-SM-RET" THEN		
				SET @SQL_EXC = CONCAT("SELECT T1.var_itemid, T2.var_name, IF(ISNULL(T3.var_desc), T1.var_desc, T3.var_desc), T1.var_locid, T1.var_uomid, T1.dou_qty AS `dou_purqty`, T1.dou_foc AS `dou_purfoc`, AME_FUN_ROUND(COALESCE(T1.dou_shipqty, 0) - COALESCE(T3.dou_qty, 0), ", P_DEC_QTY,") AS `dou_orderqty`, AME_FUN_ROUND(COALESCE(T1.dou_shipfoc, 0) - COALESCE(T3.dou_foc, 0), ", P_DEC_QTY,") AS `dou_orderfoc`, IF(ISNULL(T3.dou_qty), 0, T3.dou_qty) AS `dou_qty`, IF(ISNULL(T3.dou_foc), 0, T3.dou_foc) AS `dou_foc`, 0, T1.int_poline, T1.var_id FROM ame_ar_ret_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
				LEFT JOIN ame_ar_sm_det T3 ON (T1.var_id = T3.var_poid AND T1.int_poline = T3.int_poline AND T1.var_locid = T3.var_locid AND T1.var_itemid = T3.var_itemid AND T1.var_uomid = T3.var_uomid AND T3.var_id = '", P3, "') WHERE T1.var_id IN(", P2,") ORDER BY T1.var_id, T1.int_line");	
		WHEN "AR-SM-CN" THEN		
				SET @SQL_EXC = CONCAT("SELECT T1.var_itemid, T2.var_name, IF(ISNULL(T3.var_desc), T1.var_desc, T3.var_desc), T1.var_locid, T1.var_uomid, T1.dou_qty AS `dou_purqty`, T1.dou_foc AS `dou_purfoc`, AME_FUN_ROUND(COALESCE(T1.dou_shipqty, 0) - COALESCE(T3.dou_qty, 0), ", P_DEC_QTY,") AS `dou_orderqty`, AME_FUN_ROUND(COALESCE(T1.dou_shipfoc, 0) - COALESCE(T3.dou_foc, 0), ", P_DEC_QTY,") AS `dou_orderfoc`, IF(ISNULL(T3.dou_qty), 0, T3.dou_qty) AS `dou_qty`, IF(ISNULL(T3.dou_foc), 0, T3.dou_foc) AS `dou_foc`, 0, T1.int_poline, T1.var_id FROM ame_ar_cn_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id 
				LEFT JOIN ame_ar_sm_det T3 ON (T1.var_id = T3.var_poid AND T1.int_poline = T3.int_poline AND T1.var_locid = T3.var_locid AND T1.var_itemid = T3.var_itemid AND T1.var_uomid = T3.var_uomid AND T3.var_id = '", P3, "') WHERE T1.var_id IN(", P2,") ORDER BY T1.var_id, T1.int_line");	
		ELSE
				SET @Donothing = NULL;
		END CASE;
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_POST`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_POST`(P1 varchar(100), P2 varchar(100), P3 varchar(100), P4 varchar(100), P5 varchar(100), P6 date, P7 date)
    DETERMINISTIC
BEGIN
		CASE P1
		WHEN 'AP-IN' THEN	
				SELECT 0 AS `CHECK`, T1.var_id AS `Entry No`, DATE(T1.dat_date) AS `Date`, T1.var_ref AS `Reference`, T2.var_name AS `Vendor Name`, T1.var_desc AS `Remark`, T1.var_status AS `Status`, CAST(T1.dou_net AS DECIMAL(16, 2))  AS `Total Amount` 
				FROM ame_ap_in T1 
				LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id 
				WHERE IF(P2 = 'ALL', TRUE, T1.var_status = P2) 
				AND 
				( CASE
					WHEN P3 = 'Entry No' THEN T1.var_id LIKE CONCAT('%', P4,'%')
				  WHEN P3 = 'Reference' THEN T1.var_ref LIKE CONCAT('%', P4,'%')
					WHEN P3 = 'Vendor Code' THEN T1.var_venid LIKE CONCAT('%', P4,'%')
				  WHEN P3 = 'Vendor Name' THEN T2.var_name LIKE CONCAT('%', P4,'%')
					WHEN P3 = 'Remark' THEN T1.var_desc LIKE CONCAT('%', P4,'%')
					WHEN P3 = 'Total Amount' THEN T1.dou_net LIKE CONCAT('%', P4,'%')
				  ELSE T1.var_id LIKE CONCAT('%', P4,'%') OR T1.var_ref LIKE CONCAT('%', P4,'%') OR T1.var_venid LIKE CONCAT('%', P4,'%') OR T2.var_name LIKE CONCAT('%', P4,'%') OR T1.var_desc LIKE CONCAT('%', P4,'%') OR T1.dou_net LIKE CONCAT('%', P4,'%') END
				)
				AND 
				( CASE
					WHEN P5 = 'ALL' THEN TRUE
					WHEN P5 = 'TODAY' THEN DATE(T1.dat_date) = DATE(NOW())
				  WHEN P5 = 'THIS PERIOD' THEN MONTH(T1.dat_date) = MONTH(P6)
				  WHEN P5 = 'THIS YEAR' THEN YEAR(T1.dat_date) = YEAR(P6)
				  ELSE DATE(T1.dat_date) BETWEEN DATE(P6) AND DATE(P7) END
				)	
				ORDER BY T1.var_id;
		WHEN 'AP-RET' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T2.var_name AS 'Vendor Name', T1.var_desc AS 'Remark', T1.var_status AS 'Status', T1.dou_net AS `Total Amount` FROM ame_ap_ret T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'AP-DN' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T2.var_name AS 'Vendor Name', T1.var_desc AS 'Remark', T1.var_status AS 'Status', T1.dou_net AS `Total Amount` FROM ame_ap_dn T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'AP-PY' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T2.var_name AS 'Vendor Name', T1.var_desc AS 'Remark', T1.var_status AS 'Status', T1.dou_net AS `Total Amount` FROM ame_ap_py T1 LEFT JOIN ame_mt_ven T2 ON T1.var_venid = T2.var_id WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'AP-RF' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', COALESCE(T2.var_name, '') AS 'Vendor Name', T1.var_doctype AS 'Module ID', T1.var_docid AS 'Document ID', T1.var_desc AS 'Remark', T1.var_status AS 'Status', T1.dou_amt AS `Total Amount`
				FROM ame_refund T1 LEFT JOIN ame_mt_ven T2 ON (T1.var_perid = T2.var_id)
				WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) AND T1.int_line = 0 ORDER BY T1.var_id;
		WHEN 'AR-IN' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T2.var_name AS 'Customer Name', T1.var_desc AS 'Remark', T1.var_status AS 'Status', T1.dou_net AS `Total Amount` FROM ame_ar_in T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'POS-AR-IN' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T2.var_name AS 'Customer Name', T1.var_desc AS 'Remark', T1.var_status AS 'Status', T1.dou_net AS `Total Amount` FROM pos_ar_in T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;		
		WHEN 'AR-RET' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T2.var_name AS 'Customer Name', T1.var_desc AS 'Remark', T1.var_status AS 'Status', T1.dou_net AS `Total Amount` FROM ame_ar_ret T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'AR-CN' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T2.var_name AS 'Customer Name', T1.var_desc AS 'Remark', T1.var_status AS 'Status', T1.dou_net AS `Total Amount` FROM ame_ar_cn T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'AR-PY' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T2.var_name AS 'Customer Name', T1.var_desc AS 'Remark', T1.var_status AS 'Status', T1.dou_net AS `Total Amount` FROM ame_ar_py T1 LEFT JOIN ame_mt_cus T2 ON T1.var_cusid = T2.var_id WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'AR-RF' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', COALESCE(T2.var_name, '') AS 'Customer Name', T1.var_doctype AS 'Module ID', T1.var_docid AS 'Document ID', T1.var_desc AS 'Remark', var_status AS 'Status', T1.dou_amt AS `Total Amount`
				FROM ame_refund T1 LEFT JOIN ame_mt_cus T2 ON (T1.var_perid = T2.var_id)
				WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) AND T1.int_line = 1 ORDER BY T1.var_id;
		WHEN 'IC-TF' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T1.var_desc AS 'Remark', var_status AS 'Status', 0.0 AS `Total Amount` FROM ame_ic_tf T1  WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'IC-IU' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T1.var_desc AS 'Remark', var_status AS 'Status', 0.0 AS `Total Amount` FROM ame_ic_iu T1  WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'IC-ICUOM' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T1.var_desc AS 'Remark', var_status AS 'Status', 0.0 AS `Total Amount` FROM ame_ic_icuom T1  WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'IC-IP' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T1.var_desc AS 'Remark', var_status AS 'Status', 0.0 AS `Total Amount` FROM ame_ic_ip T1  WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'IC-IA' THEN
				SELECT 0 AS 'CHECK', T1.var_id AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T1.var_desc AS 'Remark', var_status AS 'Status', 0.0 AS `Total Amount` FROM ame_ic_ia T1  WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.var_id;
		WHEN 'GL-JE' THEN
				SELECT 0 AS 'CHECK', COALESCE(T1.int_jid, '') AS 'Entry No', DATE(T1.dat_date) AS 'Date', T1.var_ref AS 'Reference', T1.var_modid AS 'Module ID', T1.var_doctype AS 'Type', T1.var_docid AS 'JV No', T1.var_desc AS 'Remark', T1.var_status AS 'Status', T1.dou_tamt AS `Total Amount` FROM ame_gl_je T1  WHERE IF(P2 = "ALL", TRUE, T1.var_status = P2) ORDER BY T1.int_jid;
		ELSE
		SET @Donothing = NULL;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_POST_SEARCH`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_POST_SEARCH`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		CASE 
		WHEN P1 IN('AP-IN', 'AP-RET', 'AP-DN', 'AP-PY', 'AP-RF') THEN
				SELECT 'All', 'Entry No', 'Reference', 'Vendor Code', 'Vendor Name', 'Remark';
		WHEN P1 IN('AR-IN', 'AR-RET', 'AR-CN', 'AR-PY', 'AR-RF','POS-AR-IN') THEN
				SELECT 'All', 'Entry No', 'Reference', 'Customer Code', 'Customer Name', 'Remark';
		WHEN P1 IN('IC-TF', 'IC-IU', 'IC-ICUOM', 'IC-IP', 'IC-IA') THEN
				SELECT 'All', 'Entry No', 'Reference', 'Remark';
		WHEN P1 IN('GL-JE') THEN
				SELECT 'All', 'Entry No', 'Reference', 'Module ID', 'Type', 'JV No', 'Remark';
		ELSE
				SET @Donothing = NULL;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_PY`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_PY`(P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		#NOTE: P1 = PAYMENT NO , P2 = TYPE
		IF P3 = "AP-PY" THEN
				CASE P2
				WHEN 'Apply to Purchase Invoice' THEN
						SELECT T1.var_docid AS 'Entry No', T2.var_ref AS 'Reference', DATE(T2.dat_date) AS 'Date', T2.dou_net AS 'Original Amt', COALESCE(T4.Paid, 0) AS Paid , T2.dou_net - COALESCE(T4.Paid, 0) AS 'Amount Due', T2.dou_net - COALESCE(T4.Paid, 0) - T1.dou_disc - T1.dou_amt - T1.dou_app AS Balance, T1.var_desc AS 'Description', T1.dou_disc AS DISC, T1.dou_amt AS AMT, FALSE, T1.dou_app AS APP FROM ame_ap_py_det T1
						LEFT JOIN (ame_ap_in T2) ON T1.var_docid = T2.var_id
						LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc + T1.dou_amt + T1.dou_app) AS Paid FROM ame_ap_py_det T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_id <> P1 AND T2.var_status IN('Open', 'Posted') GROUP BY T1.var_docid) T4 ON T1.var_docid = T4.var_docid
						WHERE T1.var_id = P1 GROUP BY T1.var_docid ORDER BY T1.int_line;
						SELECT T1.var_id, T1.int_line, T1.var_num, T1.var_doctype, T1.var_docid, T1.var_claid, T1.var_desc, T1.dou_app, T1.dou_funapp FROM ame_ap_py_app T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
						SELECT COALESCE(T1.tin_paid, 0) FROM ame_ap_py T1 WHERE T1.var_id = P1 AND COALESCE(T1.tin_paid, 0) <> 0;
				WHEN 'Prepayment' THEN
						SELECT T1.var_docid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_amt T FROM ame_ap_py_det T1 INNER JOIN ame_mt_acc T2 ON T1.var_docid = T2.var_id WHERE T1.var_id = P1 ORDER BY T1.int_line;
						SELECT T1.var_id, T1.int_line, T1.var_num, T1.var_doctype, T1.var_docid, T1.var_claid, T1.var_desc, T1.dou_app, T1.dou_app FROM ame_ap_py_app T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
				ELSE
						SELECT T1.var_docid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_amt T FROM ame_ap_py_det T1 INNER JOIN ame_mt_acc T2 ON T1.var_docid = T2.var_id WHERE T1.var_id = P1 ORDER BY T1.int_line;
				END CASE;
		ELSEIF P3 = "AR-PY" THEN
				CASE P2
				WHEN 'Apply to Sale Invoice' THEN
						SELECT T1.var_docid AS 'Entry No', T2.var_ref AS 'Reference', DATE(T2.dat_date) AS 'Date', T2.dou_net AS 'Original Amt', COALESCE(T4.Paid, 0) AS Paid , T2.dou_net - COALESCE(T4.Paid, 0) AS 'Amount Due', T2.dou_net - COALESCE(T4.Paid, 0) - T1.dou_disc - T1.dou_amt - T1.dou_app AS Balance, T1.var_desc AS 'Description', T1.dou_disc AS DISC, T1.dou_amt AS AMT, FALSE, T1.dou_app AS APP FROM ame_ar_py_det T1
						LEFT JOIN (ame_ar_in T2) ON T1.var_docid = T2.var_id
						LEFT JOIN (SELECT T1.var_docid, SUM(T1.dou_disc + T1.dou_amt + T1.dou_app) AS Paid FROM ame_ar_py_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_id <> P1 AND T2.var_status IN('Open', 'Posted') GROUP BY T1.var_docid) T4 ON T1.var_docid = T4.var_docid
						WHERE T1.var_id = P1 GROUP BY T1.var_docid ORDER BY T1.int_line;
						SELECT T1.var_id, T1.int_line, T1.var_num, T1.var_doctype, T1.var_docid, T1.var_claid, T1.var_desc, T1.dou_app, T1.dou_funapp FROM ame_ar_py_app T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
						SELECT COALESCE(T1.tin_paid, 0) FROM ame_ar_py T1 WHERE T1.var_id = P1 AND COALESCE(T1.tin_paid, 0) <> 0;
				WHEN 'Prepayment' THEN
						SELECT T1.var_docid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_amt T FROM ame_ar_py_det T1 INNER JOIN ame_mt_acc T2 ON T1.var_docid = T2.var_id WHERE T1.var_id = P1 ORDER BY T1.int_line;
						SELECT T1.var_id, T1.int_line, T1.var_num, T1.var_doctype, T1.var_docid, T1.var_claid, T1.var_desc, T1.dou_app, T1.dou_app FROM ame_ar_py_app T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
				ELSE
						SELECT T1.var_docid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_amt T FROM ame_ar_py_det T1 INNER JOIN ame_mt_acc T2 ON T1.var_docid = T2.var_id WHERE T1.var_id = P1 ORDER BY T1.int_line;
				END CASE;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_PY_APP_PRESS`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_PY_APP_PRESS`(P1 varchar(100), P2 varchar(100), P3 varchar(100), P4 varchar(100), P5 integer)
    DETERMINISTIC
BEGIN
		#NOTE: P1 = VENDOR , P2 = PAYMENT NO
		CASE P4
		WHEN "AP-PY" THEN
				(SELECT 'Purchase Return' AS 'Type', T.`Entry No`, T.Reference, T.Date, T.`Original Amt`, COALESCE(T.Paid, 0) AS 'Applied Amount', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Amount Due', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Balance', T.Description, T.APP AS 'Applied Payment', FALSE FROM
				(SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', DATE(T1.dat_date) AS 'Date', T6.var_desc AS 'Description', T1.dou_net AS 'Original Amt', COALESCE(T5.Paid, 0) + COALESCE(T7.Refund, 0) AS 'Paid', COALESCE(T6.APP, 0) AS APP FROM ame_ap_ret T1
				LEFT JOIN (SELECT T3.var_docid, AME_FUN_ROUND(SUM(T3.dou_app), P5) AS 'Paid' FROM ame_ap_py_app T3 LEFT JOIN ame_ap_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Purchase Return' AND T3.var_id <> P2 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T5 ON (T1.var_id = T5.var_docid)
				LEFT JOIN (SELECT T3.var_docid, T3.var_desc, T3.dou_app AS APP FROM ame_ap_py_app T3 LEFT JOIN ame_ap_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Purchase Return' AND T3.var_id = P2 AND T3.var_num = P3 AND T4.var_status IN('Open', 'Posted')) T6 ON (T1.var_id = T6.var_docid)
				LEFT JOIN (SELECT T7.var_docid, AME_FUN_ROUND(SUM(T7.dou_amt), P5) AS 'Refund' FROM ame_refund T7 WHERE T7.var_doctype = 'AP-RET' AND T7.var_status IN('Open', 'Posted') GROUP BY T7.var_docid) T7 ON (T1.var_id = T7.var_docid)
				WHERE T1.var_venid = P1 AND T1.var_status = 'Posted' AND AME_FUN_ROUND(COALESCE(T1.dou_net, 0) - COALESCE(T1.dou_paid, 0) + COALESCE(T6.APP, 0), P5) > 0 GROUP BY T1.var_id)
				AS T ORDER BY T.`Entry No`)
				UNION ALL
				(SELECT 'Debit Note' AS 'Type', T.`Entry No`, T.Reference, T.Date, T.`Original Amt`, COALESCE(T.Paid, 0) AS 'Applied Amount', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Amount Due', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Balance', T.Description, T.APP AS 'Applied Payment', FALSE FROM
				(SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', DATE(T1.dat_date) AS 'Date', T6.var_desc AS 'Description', T1.dou_net AS 'Original Amt', COALESCE(T5.Paid, 0) + COALESCE(T7.Refund, 0) AS 'Paid', COALESCE(T6.APP, 0) AS APP FROM ame_ap_dn T1
				LEFT JOIN (SELECT T3.var_docid, AME_FUN_ROUND(SUM(T3.dou_app), P5) AS 'Paid' FROM ame_ap_py_app T3 LEFT JOIN ame_ap_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Debit Note' AND T3.var_id <> P2 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T5 ON (T1.var_id = T5.var_docid)
				LEFT JOIN (SELECT T3.var_docid, T3.var_desc, T3.dou_app AS APP FROM ame_ap_py_app T3 LEFT JOIN ame_ap_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Debit Note' AND T3.var_id = P2 AND T3.var_num = P3 AND T4.var_status IN('Open', 'Posted')) T6 ON (T1.var_id = T6.var_docid)
				LEFT JOIN (SELECT T7.var_docid, AME_FUN_ROUND(SUM(T7.dou_amt), P5) AS 'Refund' FROM ame_refund T7 WHERE T7.var_doctype = 'AP-DN' AND T7.var_status IN('Open', 'Posted') GROUP BY T7.var_docid) T7 ON (T1.var_id = T7.var_docid)
				WHERE T1.var_venid = P1 AND T1.var_status = 'Posted' AND AME_FUN_ROUND(COALESCE(T1.dou_net, 0) - COALESCE(T1.dou_paid, 0) + COALESCE(T6.APP, 0), P5) > 0 GROUP BY T1.var_id)
				AS T ORDER BY T.`Entry No`)
				UNION ALL
				(SELECT 'Prepayment' AS 'Type', T.`Entry No`, T.Reference, T.Date, T.`Original Amt`, COALESCE(T.Paid, 0) AS 'Applied Amount', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Amount Due', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Balance', T.Description, T.APP AS 'Applied Payment', FALSE FROM
				(SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', DATE(T1.dat_date) AS 'Date', T6.var_desc AS 'Description', T1.dou_net AS 'Original Amt', COALESCE(T5.Paid, 0) + COALESCE(T7.Refund, 0) AS 'Paid', COALESCE(T6.APP, 0) AS APP FROM ame_ap_py T1
				LEFT JOIN (SELECT T3.var_docid, AME_FUN_ROUND(SUM(T3.dou_app), P5) AS 'Paid' FROM ame_ap_py_app T3 LEFT JOIN ame_ap_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Prepayment' AND T3.var_id <> P2 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T5 ON (T1.var_id = T5.var_docid)
				LEFT JOIN (SELECT T3.var_docid, T3.var_desc, T3.dou_app AS APP FROM ame_ap_py_app T3 LEFT JOIN ame_ap_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Prepayment' AND T3.var_id = P2 AND T3.var_num = P3 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T6 ON (T1.var_id = T6.var_docid)
				LEFT JOIN (SELECT T7.var_docid, AME_FUN_ROUND(SUM(T7.dou_amt), P5) AS 'Refund' FROM ame_refund T7 WHERE T7.var_doctype = 'AP-PY' AND T7.var_status IN('Open', 'Posted') GROUP BY T7.var_docid) T7 ON (T1.var_id = T7.var_docid)
				WHERE T1.var_venid = P1 AND T1.var_type = 'Prepayment' AND T1.var_status = 'Posted' AND AME_FUN_ROUND(COALESCE(T1.dou_net, 0) - COALESCE(T1.dou_paid, 0) + COALESCE(T6.APP, 0), P5) > 0 GROUP BY T1.var_id)
				AS T ORDER BY T.`Entry No`);
		WHEN "AR-PY" THEN
				(SELECT 'Sales Return' AS 'Type', T.`Entry No`, T.Reference, T.Date, T.`Original Amt`, COALESCE(T.Paid, 0) AS 'Applied Amount', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Amount Due', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Balance', T.Description, T.APP AS 'Applied Payment', FALSE FROM
				(SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', DATE(T1.dat_date) AS 'Date', T6.var_desc AS 'Description', T1.dou_net AS 'Original Amt', COALESCE(T5.Paid, 0) + COALESCE(T7.Refund, 0) AS 'Paid', COALESCE(T6.APP, 0) AS APP FROM ame_ar_ret T1
				LEFT JOIN (SELECT T3.var_docid, AME_FUN_ROUND(SUM(T3.dou_app), P5) AS 'Paid' FROM ame_ar_py_app T3 LEFT JOIN ame_ar_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Sales Return' AND T3.var_id <> P2 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T5 ON (T1.var_id = T5.var_docid)
				LEFT JOIN (SELECT T3.var_docid, T3.var_desc, T3.dou_app AS APP FROM ame_ar_py_app T3 LEFT JOIN ame_ar_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Sales Return' AND T3.var_id = P2 AND T3.var_num = P3 AND T4.var_status IN('Open', 'Posted')) T6 ON (T1.var_id = T6.var_docid)
				LEFT JOIN (SELECT T7.var_docid, AME_FUN_ROUND(SUM(T7.dou_amt), P5) AS 'Refund' FROM ame_refund T7 WHERE T7.var_doctype = 'AR-RET' AND T7.var_status IN('Open', 'Posted') GROUP BY T7.var_docid) T7 ON (T1.var_id = T7.var_docid)
				WHERE T1.var_cusid = P1 AND T1.var_status = 'Posted' AND AME_FUN_ROUND(COALESCE(T1.dou_net, 0) - COALESCE(T1.dou_paid, 0) + COALESCE(T6.APP, 0), P5) > 0 GROUP BY T1.var_id)
				AS T ORDER BY T.`Entry No`)
				UNION ALL
				(SELECT 'Credit Note' AS 'Type', T.`Entry No`, T.Reference, T.Date, T.`Original Amt`, COALESCE(T.Paid, 0) AS 'Applied Amount', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Amount Due', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Balance', T.Description, T.APP AS 'Applied Payment', FALSE FROM
				(SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', DATE(T1.dat_date) AS 'Date', T6.var_desc AS 'Description', T1.dou_net AS 'Original Amt', COALESCE(T5.Paid, 0) + COALESCE(T7.Refund, 0) AS 'Paid', COALESCE(T6.APP, 0) AS APP FROM ame_ar_cn T1
				LEFT JOIN (SELECT T3.var_docid, AME_FUN_ROUND(SUM(T3.dou_app), P5) AS 'Paid' FROM ame_ar_py_app T3 LEFT JOIN ame_ar_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Credit Note' AND T3.var_id <> P2 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T5 ON (T1.var_id = T5.var_docid)
				LEFT JOIN (SELECT T3.var_docid, T3.var_desc, T3.dou_app AS APP FROM ame_ar_py_app T3 LEFT JOIN ame_ar_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Credit Note' AND T3.var_id = P2 AND T3.var_num = P3 AND T4.var_status IN('Open', 'Posted')) T6 ON (T1.var_id = T6.var_docid)
				LEFT JOIN (SELECT T7.var_docid, AME_FUN_ROUND(SUM(T7.dou_amt), P5) AS 'Refund' FROM ame_refund T7 WHERE T7.var_doctype = 'AR-CN' AND T7.var_status IN('Open', 'Posted') GROUP BY T7.var_docid) T7 ON (T1.var_id = T7.var_docid)
				WHERE T1.var_cusid = P1 AND T1.var_status = 'Posted' AND AME_FUN_ROUND(COALESCE(T1.dou_net, 0) - COALESCE(T1.dou_paid, 0) + COALESCE(T6.APP, 0), P5) > 0 GROUP BY T1.var_id)
				AS T ORDER BY T.`Entry No`)
				UNION ALL
				(SELECT 'Prepayment' AS 'Type', T.`Entry No`, T.Reference, T.Date, T.`Original Amt`, COALESCE(T.Paid, 0) AS 'Applied Amount', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Amount Due', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P5) AS 'Balance', T.Description, T.APP AS 'Applied Payment', FALSE FROM
				(SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', DATE(T1.dat_date) AS 'Date', T6.var_desc AS 'Description', T1.dou_net AS 'Original Amt', COALESCE(T5.Paid, 0) + COALESCE(T7.Refund, 0) AS 'Paid', COALESCE(T6.APP, 0) AS APP FROM ame_ar_py T1
				LEFT JOIN (SELECT T3.var_docid, AME_FUN_ROUND(SUM(T3.dou_app), P5) AS 'Paid' FROM ame_ar_py_app T3 LEFT JOIN ame_ar_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Prepayment' AND T3.var_id <> P2 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T5 ON (T1.var_id = T5.var_docid)
				LEFT JOIN (SELECT T3.var_docid, T3.var_desc, T3.dou_app AS APP FROM ame_ar_py_app T3 LEFT JOIN ame_ar_py T4 ON T3.var_id = T4.var_id WHERE T3.var_doctype = 'Prepayment' AND T3.var_id = P2 AND T3.var_num = P3 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T6 ON (T1.var_id = T6.var_docid)
				LEFT JOIN (SELECT T7.var_docid, AME_FUN_ROUND(SUM(T7.dou_amt), P5) AS 'Refund' FROM ame_refund T7 WHERE T7.var_doctype = 'AR-PY' AND T7.var_status IN('Open', 'Posted') GROUP BY T7.var_docid) T7 ON (T1.var_id = T7.var_docid)
				WHERE T1.var_cusid = P1 AND T1.var_type = 'Prepayment' AND T1.var_status = 'Posted' AND AME_FUN_ROUND(COALESCE(T1.dou_net, 0) - COALESCE(T1.dou_paid, 0) + COALESCE(T6.APP, 0), P5) > 0 GROUP BY T1.var_id)
				AS T ORDER BY T.`Entry No`);
		ELSE
				SET @Donothing = NULL;
		END CASE;
		
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_PY_HIS`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_PY_HIS`(P1 varchar(100), P2 varchar(100))
    DETERMINISTIC
BEGIN
		#NOTE: P1 = TYPE, P2 = ENTRY NO
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		# DECIMAL
		CASE P1
		WHEN "AP-IN" THEN
				SELECT T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T2.var_desc AS `Description`, T1.var_status AS `Status`, CASE WHEN T1.tin_paid = 1 THEN 'Paid at Purchase' WHEN T1.tin_paid = 2 THEN 'Applied at Return' WHEN T1.tin_paid = 3 THEN 'Applied at Debit Note' WHEN T1.tin_paid = 4 THEN 'Applied at Prepayment' ELSE 'Payment' END AS 'Applied Status', T2.dou_app AS 'Applied Payment', T2.dou_disc AS `Discount`, T2.dou_amt AS 'Payment from Cash Acc.' FROM
				ame_ap_py T1 LEFT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id
				WHERE T2.var_docid = P2 ORDER BY T1.var_id;
		WHEN "AP-RET" THEN
				SELECT * FROM ((SELECT 'AP-PY' AS 'Module Id', T2.var_doctype AS 'Doc. Type', T2.var_docid AS 'Doc. Id', T2.var_num AS 'Purchase', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T2.var_desc AS `Description`, IF(T1.tin_paid = 2, 'Applied at Return', 'Payment') AS `Applied Status`, T1.var_status AS `Status`, T2.dou_app AS 'Applied Payment' FROM
				ame_ap_py T1 LEFT JOIN ame_ap_py_app T2 ON T1.var_id = T2.var_id
				WHERE T2.var_doctype = 'Purchase Return' AND T2.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)
				UNION ALL 
				(SELECT 'AP-RF' AS 'Module Id', T1.var_doctype AS 'Doc. Type', T1.var_docid AS 'Doc. Id', '' AS 'Purchase', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T1.var_desc AS `Description`, 'Refund' AS `Applied Status`, T1.var_status AS `Status`, T1.dou_amt AS 'Applied Payment' FROM
				ame_refund T1
				WHERE T1.var_doctype = 'AP-RET' AND T1.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)) AS T ORDER BY T.`Module Id`, T.`Entry No`;
		WHEN "AP-DN" THEN
				SELECT * FROM ((SELECT 'AP-PY' AS 'Module Id', T2.var_doctype AS 'Doc. Type', T2.var_docid AS 'Doc. Id', T2.var_num AS 'Purchase', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T2.var_desc AS `Description`, IF(T1.tin_paid = 3, 'Applied at Debit Note', 'Payment') AS `Applied Status`, T1.var_status AS `Status`, T2.dou_app AS 'Applied Payment' FROM
				ame_ap_py T1 LEFT JOIN ame_ap_py_app T2 ON T1.var_id = T2.var_id
				WHERE T2.var_doctype = 'Debit Note' AND T2.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)
				UNION ALL 
				(SELECT 'AP-RF' AS 'Module Id', T1.var_doctype AS 'Doc. Type', T1.var_docid AS 'Doc. Id', '' AS 'Purchase', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T1.var_desc AS `Description`, 'Refund' AS 'Applied at Debit Note', T1.var_status AS `Status`, T1.dou_amt AS 'Applied Payment' FROM
				ame_refund T1
				WHERE T1.var_doctype = 'AP-DN' AND T1.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)) AS T ORDER BY T.`Module Id`, T.`Entry No`;
		WHEN "AP-PY" THEN
				SELECT * FROM ((SELECT 'AP-PY' AS 'Module Id', T2.var_doctype AS 'Doc. Type', T2.var_docid AS 'Doc. Id', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T2.var_desc AS `Description`, T1.var_status AS `Status`, T2.dou_app AS 'Applied Payment' FROM
				ame_ap_py T1 LEFT JOIN ame_ap_py_app T2 ON T1.var_id = T2.var_id
				WHERE T2.var_doctype = 'Prepayment' AND T2.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)
				UNION ALL 
				(SELECT 'AP-RF' AS 'Module Id', T1.var_doctype AS 'Doc. Type', T1.var_docid AS 'Doc. Id', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T1.var_desc AS `Description`, T1.var_status AS `Status`, T1.dou_amt AS 'Applied Payment' FROM
				ame_refund T1
				WHERE T1.var_doctype = 'AP-PY' AND T1.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)) AS T ORDER BY T.`Module Id`, T.`Entry No`;
		WHEN "AR-IN" THEN
				SELECT T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T2.var_desc AS `Description`, T1.var_status AS `Status`, CASE WHEN T1.tin_paid = 1 THEN 'Paid at Sales' WHEN T1.tin_paid = 2 THEN 'Applied at Return' WHEN T1.tin_paid = 3 THEN 'Applied at Credit Note' WHEN T1.tin_paid = 4 THEN 'Applied at Prepayment' ELSE 'Payment' END AS 'Applied Status', T2.dou_app AS 'Applied Payment', T2.dou_disc AS `Discount`, T2.dou_amt AS 'Receive to Cash Acc.' FROM
				ame_ar_py T1 LEFT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id
				WHERE T2.var_docid = P2 ORDER BY T1.var_id;
		WHEN "AR-RET" THEN
				SELECT * FROM ((SELECT 'AR-PY' AS 'Module Id', T2.var_doctype AS 'Doc. Type', T2.var_docid AS 'Doc. Id', T2.var_num AS 'Sales', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T2.var_desc AS `Description`, IF(T1.tin_paid = 3, 'Applied at Return', 'Payment') AS `Applied Status`, T1.var_status AS `Status`, T2.dou_app AS 'Applied Payment' FROM
				ame_ar_py T1 LEFT JOIN ame_ar_py_app T2 ON T1.var_id = T2.var_id
				WHERE T2.var_doctype = 'Sales Return' AND T2.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)
				UNION ALL 
				(SELECT 'AR-RF' AS 'Module Id', T1.var_doctype AS 'Doc. Type', T1.var_docid AS 'Doc. Id', '' AS 'Sales', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T1.var_desc AS `Description`, 'Refund' AS 'Applied at Return', T1.var_status AS `Status`, T1.dou_amt AS 'Applied Payment' FROM
				ame_refund T1
				WHERE T1.var_doctype = 'AR-RET' AND T1.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)) AS T ORDER BY T.`Module Id`, T.`Entry No`;
		WHEN "AR-CN" THEN
				SELECT * FROM ((SELECT 'AR-PY' AS 'Module Id', T2.var_doctype AS 'Doc. Type', T2.var_docid AS 'Doc. Id', T2.var_num AS 'Sales', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T2.var_desc AS `Description`, IF(T1.tin_paid = 3, 'Applied at Credit Note', 'Payment') AS `Applied Status`, T1.var_status AS `Status`, T2.dou_app AS 'Applied Payment' FROM
				ame_ar_py T1 LEFT JOIN ame_ar_py_app T2 ON T1.var_id = T2.var_id
				WHERE T2.var_doctype = 'Credit Note' AND T2.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)
				UNION ALL 
				(SELECT 'AR-RF' AS 'Module Id', T1.var_doctype AS 'Doc. Type', T1.var_docid AS 'Doc. Id', '' AS 'Sales', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T1.var_desc AS `Description`, 'Refund' AS 'Applied at Credit Note', T1.var_status AS `Status`, T1.dou_amt AS 'Applied Payment' FROM
				ame_refund T1
				WHERE T1.var_doctype = 'AR-CN' AND T1.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)) AS T ORDER BY T.`Module Id`, T.`Entry No`;
		WHEN "AR-PY" THEN
				SELECT * FROM ((SELECT 'AR-PY' AS 'Module Id', T2.var_doctype AS 'Doc. Type', T2.var_docid AS 'Doc. Id', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T2.var_desc AS `Description`, T1.var_status AS `Status`, T2.dou_app AS 'Applied Payment' FROM
				ame_ar_py T1 LEFT JOIN ame_ar_py_app T2 ON T1.var_id = T2.var_id
				WHERE T2.var_doctype = 'Prepayment' AND T2.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)
				UNION ALL 
				(SELECT 'AR-RF' AS 'Module Id', T1.var_doctype AS 'Doc. Type', T1.var_docid AS 'Doc. Id', T1.var_id AS `Entry No`, T1.var_ref AS `Reference`, DATE(T1.dat_date) AS `Date`, T1.var_pmttype AS `Payment Method`, T1.var_pmtref AS `Payment Ref.`, T1.var_desc AS `Description`, T1.var_status AS `Status`, T1.dou_amt AS 'Applied Payment' FROM
				ame_refund T1
				WHERE T1.var_doctype = 'AR-PY' AND T1.var_docid = P2 AND T1.var_status IN('Open', 'Posted') ORDER BY T1.var_id)) AS T ORDER BY T.`Module Id`, T.`Entry No`;
		WHEN "MT-VEN" THEN
				SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.var_term AS 'Payment Term', DATE(T1.dat_date) AS 'Date', DATE(T1.dat_due) AS 'Due Date', T1.dou_net AS 'Original Amount',
				COALESCE(T2.PAID, 0) AS 'Payment to Date', COALESCE(T1.dou_net, 0) - COALESCE(T2.PAID, 0) AS 'Outstanding',
				IF(DATEDIFF(DATE(T1.dat_due), DATE(NOW())) < 0, "-", DATEDIFF(DATE(T1.dat_due), DATE(NOW()))) AS 'Days Left',
				IF(DATEDIFF(DATE(NOW()), DATE(T1.dat_due)) < 0, "-", DATEDIFF(DATE(NOW()),DATE(T1.dat_due))) AS 'Over Due'
				FROM ame_ap_in T1 LEFT JOIN (SELECT T2.var_docid, SUM(T1.dou_amt + T1.dou_disc + T1.dou_app) AS PAID FROM ame_ap_py T1 LEFT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id WHERE T1.var_venid = P2 AND T1.var_status IN('Open', 'Posted') GROUP BY T2.var_docid) AS T2 ON T1.var_id = T2.var_docid
				WHERE T1.var_status IN('Open', 'Posted') AND T1.var_venid = P2 ORDER BY T1.var_id;
		WHEN "MT-CUS" THEN
				SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', T1.var_term AS 'Payment Term', DATE(T1.dat_date) AS 'Date', DATE(T1.dat_due) AS 'Due Date', T1.dou_net AS 'Original Amount',
				COALESCE(T2.PAID, 0) AS 'Payment to Date', COALESCE(T1.dou_net, 0) - COALESCE(T2.PAID, 0) AS 'Outstanding',
				IF(DATEDIFF(DATE(T1.dat_due), DATE(NOW())) < 0, "-", DATEDIFF(DATE(T1.dat_due), DATE(NOW()))) AS 'Days Left',
				IF(DATEDIFF(DATE(NOW()), DATE(T1.dat_due)) < 0, "-", DATEDIFF(DATE(NOW()),DATE(T1.dat_due))) AS 'Over Due'
				FROM ame_ar_in T1 LEFT JOIN (SELECT T2.var_docid, SUM(T1.dou_amt + T1.dou_disc + T1.dou_app) AS PAID FROM ame_ar_py T1 LEFT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id WHERE T1.var_cusid = P2 AND T1.var_status IN('Open', 'Posted') GROUP BY T2.var_docid) AS T2 ON T1.var_id = T2.var_docid
				WHERE T1.var_status IN('Open', 'Posted') AND T1.var_cusid = P2 ORDER BY T1.var_id;
		ELSE
				SET @Donothing = NULL;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_PY_PRESS`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_PY_PRESS`(P1 varchar(100), P2 varchar(100), P3 varchar(100), P4 integer)
    DETERMINISTIC
BEGIN
		#NOTE: P1 = VENDOR , P2 = PAYMENT NO
		CASE P3
		WHEN "AP-PY" THEN		
				SELECT T.`Entry No`, T.Reference, T.Date, T.`Original Amt`, COALESCE(T.Paid, 0) AS 'Paid Amt', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P4) AS 'Amt Due', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P4) AS Balance, T.Description, T.DISC AS 'Discount', T.CASH AS 'Payment from Cash Acc.', FALSE, T.APP AS 'Applied Payment' FROM
				(SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', DATE(T1.dat_date) AS 'Date', T1.var_desc AS 'Description', T1.dou_net AS 'Original Amt', AME_FUN_ROUND(COALESCE(T5.Paid, 0), P4) AS 'Paid', COALESCE(T6.DISC, 0) AS DISC, COALESCE(T6.CASH, 0) AS CASH, COALESCE(T6.APP, 0) AS APP FROM ame_ap_in T1
				LEFT JOIN (SELECT T3.var_docid, AME_FUN_ROUND(SUM(T3.dou_disc + T3.dou_amt + T3.dou_app), P4) AS Paid FROM ame_ap_py_det T3 LEFT JOIN ame_ap_py T4 ON T3.var_id = T4.var_id WHERE T3.var_id <> P2 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T5 ON T1.var_id = T5.var_docid
				LEFT JOIN (SELECT T3.var_docid, T3.dou_disc AS DISC, T3.dou_amt AS CASH, T3.dou_app AS APP FROM ame_ap_py_det T3 LEFT JOIN ame_ap_py T4 ON T3.var_id = T4.var_id WHERE T3.var_id = P2 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T6 ON T1.var_id = T6.var_docid
				WHERE T1.var_venid = P1 AND T1.var_status = 'Posted' AND AME_FUN_ROUND(COALESCE(T1.dou_net, 0) - COALESCE(T1.dou_paid, 0) + (COALESCE(T6.DISC, 0) + COALESCE(T6.CASH, 0) + COALESCE(T6.APP, 0)), P4) > 0 GROUP BY T1.var_id)
				AS T ORDER BY T.`Entry No`;
				SELECT T1.var_id, T1.int_line, T1.var_num, T1.var_doctype, T1.var_docid, T1.var_claid, T1.var_desc, T1.dou_app FROM ame_ap_py_app T1 WHERE T1.var_id = P2 ORDER BY int_line;
		WHEN "AR-PY" THEN
				SELECT T.`Entry No`, T.Reference, T.Date, T.`Original Amt`, COALESCE(T.Paid, 0) AS 'Paid Amt', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P4) AS 'Amt Due', AME_FUN_ROUND(T.`Original Amt` - COALESCE(T.Paid, 0), P4) AS Balance, T.Description, T.DISC AS 'Discount', T.CASH AS 'Payment from Cash Acc.', FALSE, T.APP AS 'Applied Payment' FROM
				(SELECT T1.var_id AS 'Entry No', T1.var_ref AS 'Reference', DATE(T1.dat_date) AS 'Date', T1.var_desc AS 'Description', T1.dou_net AS 'Original Amt', AME_FUN_ROUND(COALESCE(T5.Paid, 0), P4) AS 'Paid', COALESCE(T6.DISC, 0) AS DISC, COALESCE(T6.CASH, 0) AS CASH, COALESCE(T6.APP, 0) AS APP FROM ame_ar_in T1
				LEFT JOIN (SELECT T3.var_docid, AME_FUN_ROUND(SUM(T3.dou_disc + T3.dou_amt + T3.dou_app), P4) AS Paid FROM ame_ar_py_det T3 LEFT JOIN ame_ar_py T4 ON T3.var_id = T4.var_id WHERE T3.var_id <> P2 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T5 ON T1.var_id = T5.var_docid
				LEFT JOIN (SELECT T3.var_docid, T3.dou_disc AS DISC, T3.dou_amt AS CASH, T3.dou_app AS APP FROM ame_ar_py_det T3 LEFT JOIN ame_ar_py T4 ON T3.var_id = T4.var_id WHERE T3.var_id = P2 AND T4.var_status IN('Open', 'Posted') GROUP BY T3.var_docid) T6 ON T1.var_id = T6.var_docid
				WHERE T1.var_cusid = P1 AND T1.var_status = 'Posted' AND AME_FUN_ROUND(COALESCE(T1.dou_net, 0) - COALESCE(T1.dou_paid, 0) + (COALESCE(T6.DISC, 0) + COALESCE(T6.CASH, 0) + COALESCE(T6.APP, 0)), P4) > 0 GROUP BY T1.var_id)
				AS T ORDER BY T.`Entry No`;
				SELECT T1.var_id, T1.int_line, T1.var_num, T1.var_doctype, T1.var_docid, T1.var_claid, T1.var_desc, T1.dou_app FROM ame_ar_py_app T1 WHERE T1.var_id = P2 ORDER BY int_line;
		ELSE
				SET @Donothing = NULL;
		END CASE; 
		
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_QOH`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_QOH`(P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		# DECIMAL FUNCTIONAL CURRENCY
		DECLARE P_DEC_QTY INTEGER DEFAULT 0;
		BEGIN DECLARE C1 CURSOR FOR SELECT T.int_qty 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; END IF;CLOSE C1;END;
		SET @LOC = "";
		SET @LOC_ALL = "";
		IF P3 = "" THEN
				SET @LOC = CONCAT("Location : ", P1);
				SET @LOC_ALL = CONCAT("All Locations ");
		ELSE
				SET @LOC = CONCAT("Location : ", P1, " (", P3, ")");
				SET @LOC_ALL = CONCAT("All Locations (", P3, ")");
		END IF;
		(SELECT  @LOC AS `CODE`, AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0) * AME_FUN_ITEM_CON(P2, P3), P_DEC_QTY) AS `QOH` FROM ame_sl_ic_batch T1 WHERE T1.var_locid = P1 AND T1.var_itemid = P2 AND T1.dou_qty > 0)
		UNION ALL
		(SELECT @LOC_ALL AS `CODE`, AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0) * AME_FUN_ITEM_CON(P2, P3), P_DEC_QTY) AS `QOH` FROM ame_sl_ic_batch T1 WHERE T1.var_itemid = P2 AND T1.dou_qty > 0);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_REFUND_DEF`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_REFUND_DEF`(P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		#NOTE: P1 = TYPE, P2 = VENDOR / CUSTOMER, P3 = NUM
		DECLARE P_AMT1 DOUBLE DEFAULT 0.0;
		DECLARE P_AMT2 DOUBLE DEFAULT 0.0;
		DECLARE P_AMT3 DOUBLE DEFAULT 0.0;
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		CASE P1
		WHEN "AP-IN" THEN
				SELECT COALESCE(T1.var_pmttype, '') AS `PMT_TYPE` FROM ame_mt_ven T1 WHERE T1.var_id = P2; 
				SELECT COALESCE(T1.var_accid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_type = 'CASH-PMT' AND T1.var_groupid = 'DEFAULT' AND T1.var_subgroupid = 'DEFAULT';
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_amt + T1.dou_disc + T1.dou_app), 0) FROM ame_ap_py_det T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P3 AND T2.var_status IN('Open', 'Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_AMT1; END IF;CLOSE C1;
				END;
				SELECT COALESCE(T1.dou_net, 0) - P_AMT1 AS `AMT` FROM ame_ap_in T1 WHERE T1.var_id = P3;
		WHEN "AP-RET" THEN
				SET @P_ACC_AP = AME_FUN_ACC("AP", P2, "NA");
				SELECT COALESCE(T1.var_pmttype, '') AS `PMT_TYPE` FROM ame_mt_ven T1 WHERE T1.var_id = P2; 
				SELECT COALESCE(T1.var_accid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_type = 'CASH-PMT' AND T1.var_groupid = 'DEFAULT' AND T1.var_subgroupid = 'DEFAULT';
				SELECT COALESCE(@P_ACC_AP, '') AS `ID`, COALESCE(T1.var_name, '') AS `NAME` FROM ame_mt_acc T1 WHERE T1.var_id = @P_ACC_AP LIMIT 1;
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_app), 0) FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P3 AND T1.var_doctype = 'Purchase Return' AND T2.var_status IN('Open', 'Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_AMT1; END IF;CLOSE C1;
				END;
				BEGIN 
						DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_amt), 0) FROM ame_refund T1 WHERE T1.var_docid = P3 AND T1.var_doctype = 'AP-RET' AND T1.var_status IN('Open', 'Posted');
						OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT2; END IF;CLOSE C2;
				END;
				SELECT COALESCE(T1.dou_net, 0) - (P_AMT1 + P_AMT2) AS `AMT` FROM ame_ap_ret T1 WHERE T1.var_id = P3;
		WHEN "AP-DN" THEN
				SET @P_ACC_AP = AME_FUN_ACC("AP", P2, "NA");
				SELECT COALESCE(T1.var_pmttype, '') AS `PMT_TYPE` FROM ame_mt_ven T1 WHERE T1.var_id = P2; 
				SELECT COALESCE(T1.var_accid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_type = 'CASH-PMT' AND T1.var_groupid = 'DEFAULT' AND T1.var_subgroupid = 'DEFAULT';
				SELECT COALESCE(@P_ACC_AP, '') AS `ID`, COALESCE(T1.var_name, '') AS `NAME` FROM ame_mt_acc T1 WHERE T1.var_id = @P_ACC_AP LIMIT 1;
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_app), 0) FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P3 AND T1.var_doctype = 'Debit Note' AND T2.var_status IN('Open', 'Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_AMT1; END IF;CLOSE C1;
				END;
				BEGIN 
						DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_amt), 0) FROM ame_refund T1 WHERE T1.var_docid = P3 AND T1.var_doctype = 'AP-DN' AND T1.var_status IN('Open', 'Posted');
						OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT2; END IF;CLOSE C2;
				END;
				SELECT COALESCE(T1.dou_net, 0) - (P_AMT1 + P_AMT2) AS `AMT` FROM ame_ap_dn T1 WHERE T1.var_id = P3;
		WHEN "AP-PY" THEN
				SELECT COALESCE(T1.var_pmttype, '') AS `PMT_TYPE` FROM ame_mt_ven T1 WHERE T1.var_id = P2; 
				SELECT COALESCE(T1.var_accid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_type = 'CASH-PMT' AND T1.var_groupid = 'DEFAULT' AND T1.var_subgroupid = 'DEFAULT';
				SELECT COALESCE(T1.var_accid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_ap_py T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_id = P3 LIMIT 1;
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_app), 0) FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P3 AND T1.var_doctype = 'Prepayment' AND T2.var_status IN('Open', 'Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_AMT1; END IF;CLOSE C1;
				END;
				BEGIN 
						DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_amt), 0) FROM ame_refund T1 WHERE T1.var_docid = P3 AND T1.var_doctype = 'AP-PY' AND T1.var_status IN('Open', 'Posted');
						OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT2; END IF;CLOSE C2;
				END;
				SELECT COALESCE(T1.dou_net, 0) - (P_AMT1 + P_AMT2) AS `AMT` FROM ame_ap_py T1 WHERE T1.var_id = P3;
		WHEN "AR-IN" THEN
				SELECT COALESCE(T1.var_pmttype, '') AS `PMT_TYPE` FROM ame_mt_cus T1 WHERE T1.var_id = P2; 
				SELECT COALESCE(T1.var_accid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_type = 'CASH-RCP' AND T1.var_groupid = 'DEFAULT' AND T1.var_subgroupid = 'DEFAULT';
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_amt + T1.dou_disc + T1.dou_app), 0) FROM ame_ar_py_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P3 AND T2.var_status IN('Open', 'Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_AMT1; END IF;CLOSE C1;
				END;
				SELECT COALESCE(T1.dou_net, 0) - P_AMT1 AS `AMT` FROM ame_ar_in T1 WHERE T1.var_id = P3;
		WHEN "AR-PY-SD" THEN
				# DECIMAL CURRENCY
				BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = (SELECT T2.var_cur FROM ame_ar_in T2 WHERE T2.var_id = P3);OPEN C1;IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_AMT; END IF;CLOSE C1;END;
				SELECT COALESCE(T1.var_pmttype, '') AS `PMT_TYPE` FROM ame_mt_cus T1 WHERE T1.var_id = P2; 
				SELECT COALESCE(T1.var_accid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME`, COALESCE(T1.var_type, '') AS `TYPE` FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id 
				WHERE T1.var_type IN('CASH-RCP', 'CASH-RCP-INTEREST', 'CASH-RCP-PENALTY') AND T1.var_groupid = 'DEFAULT' AND T1.var_subgroupid = 'DEFAULT';
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT AME_FUN_ROUND(COALESCE(SUM(T1.dou_app + T1.dou_amt + T1.dou_disc), 0), P_DEC_AMT) FROM ame_ar_py_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P3 AND T2.var_status IN('Open', 'Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_AMT1; END IF;CLOSE C1;
				END;
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT -AME_FUN_ROUND(COALESCE(SUM(IF(T1.var_desc = 'Interest', T1.dou_app, 0)), 0), P_DEC_AMT), -AME_FUN_ROUND(COALESCE(SUM(IF(T1.var_desc = 'Penalty', T1.dou_app, 0)), 0), P_DEC_AMT) FROM ame_ar_py_app T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Apply to GL Account' AND T1.var_num = P3 AND T2.var_status IN('Open', 'Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_AMT2, P_AMT3; END IF;CLOSE C1;
				END;
				BEGIN
						SET @P_PRINC = P_AMT1;
						SET @P_INTER = P_AMT2;
						SET @P_PENAL = P_AMT3;
						SELECT T1.P_DATE, T1.P_PRINCIPAL, T1.P_INTEREST, T1.P_PENALTY, T1.P_PAID_PRI, T1.P_PAID_INT, T1.P_PAID_PEN, T1.P_NET, T1.`P_PAID`
						FROM (SELECT DATE(T1.dat_date) AS `P_DATE`, T1.dou_amt AS `P_PRINCIPAL`, T1.dou_rate_dol AS `P_INTEREST`, T1.dou_penalty AS `P_PENALTY`, AME_FUN_ROUND(T1.dou_net + T1.dou_penalty, P_DEC_AMT) AS `P_NET`, 
						AME_FUN_ROUND(@P_PRINC + @P_INTER + @P_PENAL, P_DEC_AMT) AS `P_PAID`,
						@P_PRINC AS `P_PAID_PRI`, IF(@P_PRINC >= COALESCE(T1.dou_amt, 0), @P_PRINC := AME_FUN_ROUND(@P_PRINC - COALESCE(T1.dou_amt, 0), P_DEC_AMT), @P_PRINC := AME_FUN_ROUND(COALESCE(T1.dou_amt, 0) - @P_PRINC, P_DEC_AMT)) AS `P_RUN_PRI`,
						@P_INTER AS `P_PAID_INT`, IF(@P_INTER >= COALESCE(T1.dou_rate_dol, 0), @P_INTER := AME_FUN_ROUND(@P_INTER - COALESCE(T1.dou_rate_dol, 0), P_DEC_AMT), @P_INTER := AME_FUN_ROUND(COALESCE(T1.dou_rate_dol, 0) - @P_INTER, P_DEC_AMT)) AS `P_RUN_INT`,
						@P_PENAL AS `P_PAID_PEN`, IF(@P_PENAL >= COALESCE(T1.dou_penalty, 0), @P_PENAL := AME_FUN_ROUND(@P_PENAL - COALESCE(T1.dou_penalty, 0), P_DEC_AMT), @P_PENAL := AME_FUN_ROUND(COALESCE(T1.dou_penalty, 0) - @P_PENAL, P_DEC_AMT)) AS `P_RUN_PEN`
						FROM ame_ar_py_sd_det T1 
						WHERE T1.var_id = P3
						ORDER BY T1.int_id, T1.int_line) AS T1 WHERE T1.`P_NET` > T1.`P_PAID` LIMIT 2;
				END;
		WHEN "AR-RET" THEN
				SELECT COALESCE(T1.var_pmttype, '') AS `PMT_TYPE` FROM ame_mt_cus T1 WHERE T1.var_id = P2; 
				SELECT COALESCE(T1.var_accid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_type = 'CASH-RCP' AND T1.var_groupid = 'DEFAULT' AND T1.var_subgroupid = 'DEFAULT';
				SELECT COALESCE(T1.var_docid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_ar_py_det T1 LEFT JOIN ame_mt_acc T2 ON T1.var_docid = T2.var_id WHERE T1.var_id = P3 LIMIT 1;
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_app), 0) FROM ame_ar_py_app T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P3 AND T1.var_doctype = 'Sales Return' AND T2.var_status IN('Open', 'Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_AMT1; END IF;CLOSE C1;
				END;
				BEGIN 
						DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_amt), 0) FROM ame_refund T1 WHERE T1.var_docid = P3 AND T1.var_doctype = 'AR-RET' AND T1.var_status IN('Open', 'Posted');
						OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT2; END IF;CLOSE C2;
				END;
				SELECT COALESCE(T1.dou_net, 0) - (P_AMT1 + P_AMT2) AS `AMT` FROM ame_ar_ret T1 WHERE T1.var_id = P3;
		WHEN "AR-CN" THEN
				SELECT COALESCE(T1.var_pmttype, '') AS `PMT_TYPE` FROM ame_mt_cus T1 WHERE T1.var_id = P2; 
				SELECT COALESCE(T1.var_accid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_type = 'CASH-RCP' AND T1.var_groupid = 'DEFAULT' AND T1.var_subgroupid = 'DEFAULT';
				SELECT COALESCE(T1.var_docid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_ar_py_det T1 LEFT JOIN ame_mt_acc T2 ON T1.var_docid = T2.var_id WHERE T1.var_id = P3 LIMIT 1;
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_app), 0) FROM ame_ar_py_app T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P3 AND T1.var_doctype = 'Credit Note' AND T2.var_status IN('Open', 'Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_AMT1; END IF;CLOSE C1;
				END;
				BEGIN 
						DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_amt), 0) FROM ame_refund T1 WHERE T1.var_docid = P3 AND T1.var_doctype = 'AR-CN' AND T1.var_status IN('Open', 'Posted');
						OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT2; END IF;CLOSE C2;
				END;
				SELECT COALESCE(T1.dou_net, 0) - (P_AMT1 + P_AMT2) AS `AMT` FROM ame_ar_cn T1 WHERE T1.var_id = P3;
		WHEN "AR-PY" THEN
				SELECT COALESCE(T1.var_pmttype, '') AS `PMT_TYPE` FROM ame_mt_cus T1 WHERE T1.var_id = P2; 
				SELECT COALESCE(T1.var_accid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_mt_acc_set T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_type = 'CASH-RCP' AND T1.var_groupid = 'DEFAULT' AND T1.var_subgroupid = 'DEFAULT';
				SELECT COALESCE(T1.var_accid, '') AS `ID`, COALESCE(T2.var_name, '') AS `NAME` FROM ame_ar_py T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_id = P3 LIMIT 1;
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT COALESCE(SUM(T1.dou_app), 0) FROM ame_ar_py_app T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P3 AND T1.var_doctype = 'Prepayment' AND T2.var_status IN('Open', 'Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_AMT1; END IF;CLOSE C1;
				END;
				BEGIN 
						DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_amt), 0) FROM ame_refund T1 WHERE T1.var_docid = P3 AND T1.var_doctype = 'AR-PY' AND T1.var_status IN('Open', 'Posted');
						OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT2; END IF;CLOSE C2;
				END;
				SELECT COALESCE(T1.dou_net, 0) - (P_AMT1 + P_AMT2) AS `AMT` FROM ame_ar_py T1 WHERE T1.var_id = P3;
		ELSE
				SET @Donothing = NULL;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_REMINDER`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_REMINDER`(P1 varchar(100), P2 tinyint(1), P3 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_DAY INTEGER DEFAULT 0;
		DECLARE P_LEAVE INTEGER DEFAULT 0;
		CASE P2
		WHEN 1 THEN
				SELECT T1.var_modid, T1.var_name FROM ame_sys_rem T1 INNER JOIN ame_user_pro_rem T2 ON T1.var_modid = T2.var_modid 
				WHERE T2.var_id = P1 AND COALESCE(T2.tin_pro1, 0) = 1 ORDER BY T1.int_line;
		WHEN 2 THEN
				SET @SQL_EXE = "(SELECT '' AS `var_modid`, '' AS `Reminder` LIMIT 0)";
				BEGIN
						DECLARE C1 CURSOR FOR SELECT T1.var_modid, COALESCE(T1.int_day, 0) FROM ame_user_pro_rem T1 WHERE T1.var_id = P1 AND COALESCE(T1.tin_pro1, 0) = 1;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET P_LEAVE = 1;
						OPEN C1;
								L: LOOP FETCH C1 INTO P_TYPE, P_DAY;
										IF P_LEAVE = 1 THEN LEAVE L; END IF;
										CASE P_TYPE
										WHEN "SYS1" THEN
												SET @SQL_EXE = CONCAT(@SQL_EXE, " UNION ALL (SELECT 'SYS1' AS `var_modid`, CONVERT(COUNT(T1.var_id), CHAR(100)) AS `Reminder` FROM ame_ap_in T1 WHERE DATEDIFF(DATE(T1.dat_due), DATE(CURRENT_DATE)) >= 0 AND DATEDIFF(DATE(T1.dat_due), DATE(CURRENT_DATE)) <= ", P_DAY, " AND T1.var_status = 'Posted' AND T1.var_pmtstatus <> 'Fully Paid') ");
										WHEN "SYS2" THEN
												SET @SQL_EXE = CONCAT(@SQL_EXE, " UNION ALL (SELECT 'SYS2' AS `var_modid`, CONVERT(COUNT(T1.var_id), CHAR(100)) AS `Reminder` FROM ame_ar_in T1 WHERE DATEDIFF(DATE(T1.dat_due), DATE(CURRENT_DATE)) >= 0 AND DATEDIFF(DATE(T1.dat_due), DATE(CURRENT_DATE)) <= ", P_DAY, " AND T1.var_status = 'Posted' AND T1.var_pmtstatus <> 'Fully Paid') ");
										WHEN "SYS3" THEN
												SET @SQL_EXE = CONCAT(@SQL_EXE, " UNION ALL (SELECT 'SYS3' AS `var_modid`, CONVERT(COUNT(T1.var_id), CHAR(100)) AS `Reminder` FROM ame_ap_in T1 WHERE DATEDIFF(DATE(CURRENT_DATE), DATE(T1.dat_due)) >= ", P_DAY, " AND DATEDIFF(DATE(CURRENT_DATE), DATE(T1.dat_due)) > 0 AND T1.var_status = 'Posted' AND T1.var_pmtstatus <> 'Fully Paid') ");
										WHEN "SYS4" THEN
												SET @SQL_EXE = CONCAT(@SQL_EXE, " UNION ALL (SELECT 'SYS4' AS `var_modid`, CONVERT(COUNT(T1.var_id), CHAR(100)) AS `Reminder` FROM ame_ar_in T1 WHERE DATEDIFF(DATE(CURRENT_DATE), DATE(T1.dat_due)) >= ", P_DAY, " AND DATEDIFF(DATE(CURRENT_DATE), DATE(T1.dat_due)) > 0 AND T1.var_status = 'Posted' AND T1.var_pmtstatus <> 'Fully Paid') ");
										WHEN "SYS5" THEN
												SET @SQL_EXE = CONCAT(@SQL_EXE, " UNION ALL (SELECT 'SYS5' AS `var_modid`, CONVERT(COUNT(T1.var_id),CHAR(100)) AS `Reminder` FROM ame_ap_po T1 WHERE T1.var_status = 'Open' AND T1.var_pmtstatus = 'Not Received') ");
										WHEN "SYS6" THEN
												SET @SQL_EXE = CONCAT(@SQL_EXE, " UNION ALL (SELECT 'SYS6' AS `var_modid`, CONVERT(COUNT(T1.var_id),CHAR(100)) AS `Reminder` FROM ame_ar_quo T1 WHERE T1.var_status = 'Open' AND T1.var_pmtstatus = 'Not Received') ");
										WHEN "SYS7" THEN
												SET @SQL_EXE = CONCAT(@SQL_EXE, " UNION ALL (SELECT 'SYS7' AS `var_modid`, CONVERT(COUNT(T1.var_id),CHAR(100)) AS `Reminder` FROM ame_ar_so T1 WHERE T1.var_status = 'Open' AND T1.var_pmtstatus = 'Not Received') ");
										WHEN "SYS8" THEN
												SET @SQL_EXE = CONCAT(@SQL_EXE, " UNION ALL (SELECT 'SYS8' AS `var_modid`, CONVERT(COUNT(T1.var_id),CHAR(100)) AS `Reminder` FROM ame_ar_in T1 WHERE T1.var_status = 'Posted' AND T1.var_shippmtstatus <> 'Fully Shipped') ");
										WHEN "SYS9" THEN
												SET @SQL_EXE = CONCAT(@SQL_EXE, " UNION ALL (SELECT 'SYS9' AS `var_modid`, CONVERT(COUNT(T1.var_id),CHAR(100)) AS `Reminder` FROM ame_mt_ite T1 LEFT JOIN (SELECT T2.var_itemid, COALESCE(SUM(T2.dou_qty), 0) AS `dou_qty` FROM ame_sl_ic_batch T2 GROUP BY T2.var_itemid) AS T2 ON T1.var_id = T2.var_itemid 
																													WHERE COALESCE(T1.tin_inactive, 0) = 0 AND T1.var_type IN('Inventory Part', 'Fixed Asset') AND COALESCE(T1.dou_order_pt, 0) > 0 AND COALESCE(T1.dou_order_pt, 0) >= COALESCE(T2.dou_qty, 0)) ");
										ELSE
												SET @Donothing = NULL;
										END CASE;	
								END LOOP L;
						CLOSE C1;
				END;
				PREPARE STM FROM @SQL_EXE;
				EXECUTE STM;
				DEALLOCATE PREPARE STM;	
		ELSE
				BEGIN
						DECLARE C1 CURSOR FOR SELECT COALESCE(T1.int_day, 0) FROM ame_user_pro_rem T1 WHERE T1.var_id = P1 AND COALESCE(T1.tin_pro1, 0) = 1 AND T1.var_modid = P3 LIMIT 1;
						OPEN C1;
								IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DAY; END IF;
										CASE P3
										WHEN "SYS1" THEN
												SELECT 'AP-IN' AS `Module ID`, T1.var_id AS `Entry No`, DATE(T1.dat_date) AS `Date`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_venid AS `Vendor Code`, T2.var_name AS `Vendor Name`, T1.dou_net AS `Total Amount` FROM ame_ap_in T1 LEFT JOIN ame_mt_ven  AS T2 ON T1.var_venid = T2.var_id WHERE DATEDIFF(DATE(T1.dat_due), DATE(CURRENT_DATE)) >= 0 AND DATEDIFF(DATE(T1.dat_due), DATE(CURRENT_DATE)) <= P_DAY AND T1.var_status = 'Posted' AND T1.var_pmtstatus <> 'Fully Paid' ORDER BY T1.var_id DESC;
										WHEN "SYS2" THEN
												SELECT 'AR-IN' AS `Module ID`, T1.var_id AS `Entry No`, DATE(T1.dat_date) AS `Date`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_cusid AS `Customer Code`, T2.var_name AS `Customer Name`, T1.dou_net AS `Total Amount` FROM ame_ar_in T1 LEFT JOIN ame_mt_cus  AS T2 ON T1.var_cusid = T2.var_id WHERE DATEDIFF(DATE(T1.dat_due), DATE(CURRENT_DATE)) >= 0 AND DATEDIFF(DATE(T1.dat_due), DATE(CURRENT_DATE)) <= P_DAY AND T1.var_status = 'Posted' AND T1.var_pmtstatus <> 'Fully Paid' ORDER BY T1.var_id DESC;
										WHEN "SYS3" THEN
												SELECT 'AP-IN' AS `Module ID`, T1.var_id AS `Entry No`, DATE(T1.dat_date) AS `Date`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_venid AS `Vendor Code`, T2.var_name AS `Vendor Name`, T1.dou_net AS `Total Amount` FROM ame_ap_in T1 LEFT JOIN ame_mt_ven  AS T2 ON T1.var_venid = T2.var_id WHERE DATEDIFF(DATE(CURRENT_DATE), DATE(T1.dat_due)) >= P_DAY AND DATEDIFF(DATE(CURRENT_DATE), DATE(T1.dat_due)) > 0 AND T1.var_status = 'Posted' AND T1.var_pmtstatus <> 'Fully Paid' ORDER BY T1.var_id DESC;
										WHEN "SYS4" THEN
												SELECT 'AR-IN' AS `Module ID`, T1.var_id AS `Entry No`, DATE(T1.dat_date) AS `Date`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_cusid AS `Customer Code`, T2.var_name AS `Customer Name`, T1.dou_net AS `Total Amount` FROM ame_ar_in T1 LEFT JOIN ame_mt_cus  AS T2 ON T1.var_cusid = T2.var_id WHERE DATEDIFF(DATE(CURRENT_DATE), DATE(T1.dat_due)) >= P_DAY AND DATEDIFF(DATE(CURRENT_DATE), DATE(T1.dat_due)) > 0 AND T1.var_status = 'Posted' AND T1.var_pmtstatus <> 'Fully Paid' ORDER BY T1.var_id DESC;
										WHEN "SYS5" THEN
												SELECT 'AP-PO' AS `Module ID`, T1.var_id AS `Entry No`, DATE(T1.dat_date) AS `Date`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_venid AS `Vendor Code`, T2.var_name AS `Vendor Name`, T1.var_pmtstatus AS `Receipt Status`, T1.dou_net AS `Total Amount` FROM ame_ap_po T1 LEFT JOIN ame_mt_ven  AS T2 ON T1.var_venid = T2.var_id WHERE T1.var_status = 'Open' AND T1.var_pmtstatus IN('Not Received', 'Partially Received') ORDER BY T1.var_id DESC;
										WHEN "SYS6" THEN
												SELECT 'AR-QUO' AS `Module ID`, T1.var_id AS `Entry No`, DATE(T1.dat_date) AS `Date`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_cusid AS `Customer Code`, T2.var_name AS `Customer Name`, T1.var_pmtstatus AS `Receipt Status`, T1.dou_net AS `Total Amount` FROM ame_ar_quo T1 LEFT JOIN ame_mt_cus  AS T2 ON T1.var_cusid = T2.var_id WHERE T1.var_status = 'Open' AND T1.var_pmtstatus IN('Not Received', 'Partially Received') ORDER BY T1.var_id DESC;
										WHEN "SYS7" THEN
												SELECT 'AR-SO' AS `Module ID`, T1.var_id AS `Entry No`, DATE(T1.dat_date) AS `Date`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_cusid AS `Customer Code`, T2.var_name AS `Customer Name`, T1.var_pmtstatus AS `Receipt Status`, T1.dou_net AS `Total Amount` FROM ame_ar_so T1 LEFT JOIN ame_mt_cus  AS T2 ON T1.var_cusid = T2.var_id WHERE T1.var_status = 'Open' AND T1.var_pmtstatus IN('Not Received', 'Partially Received') ORDER BY T1.var_id DESC;
										WHEN "SYS8" THEN
												SELECT 'AR-IN' AS `Module ID`, T1.var_id AS `Entry No`, DATE(T1.dat_date) AS `Date`, T1.var_type AS `Type`, T1.var_ref AS `Reference`, T1.var_desc AS `Remark`, T1.var_cusid AS `Customer Code`, T2.var_name AS `Customer Name`, T1.var_shippmtstatus AS `Shipment Status`, T1.dou_net AS `Total Amount` FROM ame_ar_in T1 LEFT JOIN ame_mt_cus  AS T2 ON T1.var_cusid = T2.var_id WHERE T1.var_status = 'Posted' AND T1.var_shippmtstatus <> 'Fully Shipped' ORDER BY T1.var_id DESC;
										WHEN "SYS9" THEN
												SELECT 'MT-ITE' AS `Module ID`, T1.var_id AS `Item ID`, T1.var_name AS `Item Name`, T1.dou_order_pt AS `Reorder Point`, COALESCE(T2.dou_qty, 0) AS `Quantity on Hand` FROM ame_mt_ite T1 LEFT JOIN (SELECT T2.var_itemid, COALESCE(SUM(T2.dou_qty), 0) AS `dou_qty` FROM ame_sl_ic_batch T2 GROUP BY T2.var_itemid) AS T2 ON T1.var_id = T2.var_itemid 
																													WHERE COALESCE(T1.tin_inactive, 0) = 0 AND T1.var_type IN('Inventory Part', 'Fixed Asset') AND COALESCE(T1.dou_order_pt, 0) > 0 AND COALESCE(T1.dou_order_pt, 0) >= COALESCE(T2.dou_qty, 0);
										ELSE
												SET @Donothing = NULL;
										END CASE;	
						CLOSE C1;
				END;	
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_RET_PRESS_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_RET_PRESS_1`(P1 varchar(100), P2 varchar(100), P3 varchar(100), P4 varchar(100))
    DETERMINISTIC
BEGIN
		CASE P1
		WHEN "AP-RET" THEN
				CASE P2
				WHEN "Purchase Inventory" THEN
						SELECT DISTINCTROW T1.var_itemid, T5.var_name, IF(ISNULL(T4.var_desc), T1.var_desc, T4.var_desc) AS `var_desc`, T1.var_locid, T1.var_claid, T1.var_uomid, T4.dou_qty, IF(ISNULL(T4.dou_price), T1.dou_price, T4.dou_price) AS `dou_price`, T4.dou_sub, IF(ISNULL(T4.dou_disper), T1.dou_disper, T4.dou_disper) AS `dou_disper`, T4.dou_disdol, T4.dou_net, T4.dou_foc, IF(ISNULL(T4.tin_vat), T1.tin_vat, T4.tin_vat) AS `tin_vat`, IF(ISNULL(T4.tin_wht), T1.tin_wht, T4.tin_wht) AS `tin_wht`, T1.dou_qty AS `Pur. Qty`, COALESCE(T2.dou_qty, 0) AS `Pur To Date`, T1.dou_foc AS `FOC`, COALESCE(T2.dou_foc, 0) AS `FOC To Date`, T1.int_batchno, T1.int_line 
						FROM ame_ap_in_det T1
						LEFT JOIN (SELECT T1.var_docid, T2.var_locid, T2.var_itemid, T2.var_uomid, SUM(T2.dou_qty) AS `dou_qty`, SUM(T2.dou_foc) AS `dou_foc` FROM ame_ap_ret T1 LEFT JOIN ame_ap_ret_det T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P3 AND T1.var_id <> P4 GROUP BY T1.var_docid, T2.var_locid, T2.var_itemid, T2.var_uomid) T2 ON (T1.var_id = T2.var_docid AND T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid AND T1.var_uomid = T2.var_uomid)
						LEFT JOIN (ame_ap_ret T3 LEFT JOIN ame_ap_ret_det T4 ON T3.var_id = T4.var_id) ON (T4.var_poid = T1.var_id AND T4.var_locid = T1.var_locid AND T4.var_itemid = T1.var_itemid AND T4.var_uomid = T1.var_uomid AND T4.int_line = T1.int_line AND T3.var_id = P4)
						LEFT JOIN ame_mt_ite T5 ON T1.var_itemid = T5.var_id WHERE T1.var_id = P3;
				WHEN "Additional Costing" THEN
						SELECT T1.var_itemid, T2.var_name, T1.var_desc, T1.var_type, T3.var_name, T1.var_claid, T1.int_batchno, T1.var_locid, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht FROM ame_ap_ret_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id LEFT JOIN ame_sys_add_cost T3 ON T1.var_type = T3.var_id WHERE T1.var_id = P3;
				WHEN "Apply to GL Account" THEN
						SELECT T1.var_accid, T2.var_name, T1.var_claid, T1.var_desc, T1.dou_price, T1.dou_disper, T1.dou_disdol, T1.dou_net, T1.tin_vat, T1.tin_wht FROM ame_ap_ret_det T1 LEFT JOIN ame_mt_acc T2 ON T1.var_accid = T2.var_id WHERE T1.var_id = P3;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		ELSE
				SET @Donothing = NULL;
		END CASE;
		
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_SM_HIS`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_SM_HIS`(P1 varchar(100), P2 varchar(100), P3 integer)
    DETERMINISTIC
BEGIN
		#NOTE: P1 = TYPE, P2 = ENTRY NO, P3 = DECIMAL PLACES
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		# DECIMAL
		CASE P1
		WHEN "AR-IN" THEN
				SELECT T1.var_itemid AS `Item Id`, COALESCE(T2.var_name, '') AS `Item Name`, T1.var_locid AS `Location Id`, T1.var_uomid AS `Uom Id`, AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P3) AS `Quantity Sold`, AME_FUN_ROUND(T1.dou_shipqty + T1.dou_shipfoc, P3) AS `Quantity Shipped`, AME_FUN_ROUND((T1.dou_qty + T1.dou_foc) - (T1.dou_shipqty + T1.dou_shipfoc), P3) AS `Quantity Balance`, CASE WHEN AME_FUN_ROUND((T1.dou_qty + T1.dou_foc) - (T1.dou_shipqty + T1.dou_shipfoc), P3) = 0 THEN 'Fully Shipped' WHEN AME_FUN_ROUND((T1.dou_qty + T1.dou_foc), P3) > AME_FUN_ROUND((T1.dou_shipqty + T1.dou_shipfoc), P3) AND AME_FUN_ROUND((T1.dou_shipqty + T1.dou_shipfoc), P3) > 0 THEN 'Partially Shipped' ELSE 'Unshipped' END AS `Shipment Status` 
				FROM ame_ar_in_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id
				WHERE T1.var_id = P2 ORDER BY T1.int_line;

				SELECT T0.var_id AS `Entry No`, T0.var_ref AS `Reference`, T0.dat_date AS `Date`, T1.var_itemid AS `Item Id`, COALESCE(T2.var_name, '') AS `Item Name`, T1.var_locid AS `Location Id`, T1.var_uomid AS `Uom Id`, AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P3) AS `Quantity Ship`
				FROM ame_ar_sm T0 LEFT JOIN (ame_ar_sm_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id) ON T0.var_id = T1.var_id
				WHERE T0.var_status = 'Open' AND T0.var_type = P1 AND T1.var_poid = P2 AND AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P3) > 0 ORDER BY T0.var_id, T1.int_line;
		WHEN "AR-RET" THEN
				SELECT T1.var_itemid AS `Item Id`, COALESCE(T2.var_name, '') AS `Item Name`, T1.var_locid AS `Location Id`, T1.var_uomid AS `Uom Id`, AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P3) AS `Quantity Sold`, AME_FUN_ROUND(T1.dou_shipqty + T1.dou_shipfoc, P3) AS `Quantity Shipped`, AME_FUN_ROUND((T1.dou_qty + T1.dou_foc) - (T1.dou_shipqty + T1.dou_shipfoc), P3) AS `Quantity Balance`, CASE WHEN AME_FUN_ROUND((T1.dou_qty + T1.dou_foc) - (T1.dou_shipqty + T1.dou_shipfoc), P3) = 0 THEN 'Fully Shipped' WHEN AME_FUN_ROUND((T1.dou_qty + T1.dou_foc), P3) > AME_FUN_ROUND((T1.dou_shipqty + T1.dou_shipfoc), P3) AND AME_FUN_ROUND((T1.dou_shipqty + T1.dou_shipfoc), P3) > 0 THEN 'Partially Shipped' ELSE 'Unshipped' END AS `Shipment Status` 
				FROM ame_ar_ret_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id
				WHERE T1.var_id = P2 ORDER BY T1.int_line;

				SELECT T0.var_id AS `Entry No`, T0.var_ref AS `Reference`, T0.dat_date AS `Date`, T1.var_itemid AS `Item Id`, COALESCE(T2.var_name, '') AS `Item Name`, T1.var_locid AS `Location Id`, T1.var_uomid AS `Uom Id`, AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P3) AS `Quantity Ship`
				FROM ame_ar_sm T0 LEFT JOIN (ame_ar_sm_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id) ON T0.var_id = T1.var_id
				WHERE T0.var_status = 'Open' AND T0.var_type = P1 AND T1.var_poid = P2 AND AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P3) > 0 ORDER BY T0.var_id, T1.int_line;
		WHEN "AR-CN" THEN
				SELECT T1.var_itemid AS `Item Id`, COALESCE(T2.var_name, '') AS `Item Name`, T1.var_locid AS `Location Id`, T1.var_uomid AS `Uom Id`, AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P3) AS `Quantity Sold`, AME_FUN_ROUND(T1.dou_shipqty + T1.dou_shipfoc, P3) AS `Quantity Shipped`, AME_FUN_ROUND((T1.dou_qty + T1.dou_foc) - (T1.dou_shipqty + T1.dou_shipfoc), P3) AS `Quantity Balance`, CASE WHEN AME_FUN_ROUND((T1.dou_qty + T1.dou_foc) - (T1.dou_shipqty + T1.dou_shipfoc), P3) = 0 THEN 'Fully Shipped' WHEN AME_FUN_ROUND((T1.dou_qty + T1.dou_foc), P3) > AME_FUN_ROUND((T1.dou_shipqty + T1.dou_shipfoc), P3) AND AME_FUN_ROUND((T1.dou_shipqty + T1.dou_shipfoc), P3) > 0 THEN 'Partially Shipped' ELSE 'Unshipped' END AS `Shipment Status` 
				FROM ame_ar_cn_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id
				WHERE T1.var_id = P2 ORDER BY T1.int_line;

				SELECT T0.var_id AS `Entry No`, T0.var_ref AS `Reference`, T0.dat_date AS `Date`, T1.var_itemid AS `Item Id`, COALESCE(T2.var_name, '') AS `Item Name`, T1.var_locid AS `Location Id`, T1.var_uomid AS `Uom Id`, AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P3) AS `Quantity Ship`
				FROM ame_ar_sm T0 LEFT JOIN (ame_ar_sm_det T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id) ON T0.var_id = T1.var_id
				WHERE T0.var_status = 'Open' AND T0.var_type = P1 AND T1.var_poid = P2 AND AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P3) > 0 ORDER BY T0.var_id, T1.int_line;
		ELSE
				SET @Donothing = NULL;
		END CASE;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_LOAD_TREE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_LOAD_TREE`()
    DETERMINISTIC
BEGIN
		SELECT T1.var_name_menu, COALESCE(T1.var_status, '') AS `Status` FROM ame_sys_per_rep T1 WHERE T1.tin_show = 1 GROUP BY T1.int_group ORDER BY T1.int_group;
		SELECT T1.var_modid, T1.var_name_menu, T1.var_name, 
		COALESCE(T1.var_name_split1, '') AS `SD1`, COALESCE(T1.var_name_split2, '') AS `SD2`, COALESCE(T1.var_sort_split1, '') AS `SDSO1`, COALESCE(T1.var_sort_split2, '') AS `SDSO2`, 
		COALESCE(T2.CS1, '') AS `CS1`, COALESCE(T2.CS2, '') AS `CS2`, COALESCE(T1.var_status, '') AS `Status`  FROM ame_sys_per_rep T1
		LEFT JOIN (SELECT T2.var_modid, T2.var_name_split1 AS `CS1`, T2.var_name_split2 AS `CS2` FROM ame_sys_custom_com T1 LEFT JOIN ame_sys_custom_rep T2 ON T1.int_id = T2.int_id WHERE T1.tin_inactive = 0) AS T2 ON T1.var_modid = T2.var_modid 
		WHERE T1.tin_show = 1 ORDER BY T1.int_group, T1.int_line;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_AP_DN_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AP_DN_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 D1 INTEGER DEFAULT 0;
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_BAT_NUM INTEGER DEFAULT 0;
		DECLARE P_BAT_NUM_AC INTEGER DEFAULT 0;
		DECLARE P_LINE INTEGER DEFAULT 0;
		DECLARE P_LINE_JE INTEGER DEFAULT 0;
		DECLARE P_NUM_IC INTEGER DEFAULT 0;
		DECLARE P_DATE DATETIME;
		DECLARE P_DATE_DUE DATETIME;
		DECLARE P_ACC_AP VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_IC VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_EXP VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_DISC VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_VAT VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_WHT 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_PUR VARCHAR(100) DEFAULT "";
		DECLARE P_TYPE_AC VARCHAR(100) DEFAULT "";
		DECLARE P_LOC VARCHAR(100) DEFAULT "";
		DECLARE P_ITEM VARCHAR(100) DEFAULT "";
		DECLARE P_UOM VARCHAR(100) DEFAULT "";
		DECLARE P_TERM VARCHAR(100) DEFAULT "";
		DECLARE P_BAL DOUBLE DEFAULT 0.0;
		DECLARE P_DISC DOUBLE DEFAULT 0.0;
		DECLARE P_DISC_DET DOUBLE DEFAULT 0.0;
		DECLARE P_VAT DOUBLE DEFAULT 0.0;
		DECLARE P_WHT DOUBLE DEFAULT 0.0;
		DECLARE P_QTY DOUBLE DEFAULT 0.0;
		DECLARE P_FOC DOUBLE DEFAULT 0.0;
		DECLARE P_COST 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_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 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, int_cost, 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;
		IF P7 = -1 THEN SET P_JID = AME_FUN_NEW_JE_NUM(); ELSE SET P_JID = P7; 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
				DECLARE P_BAL DOUBLE DEFAULT 0;
				SELECT T1.dat_date, T1.dat_due, T1.var_term, T1.var_type, T1.var_ref, T1.var_claid, T1.var_venid, T1.var_desc, 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_PUR, P_REF, P_CLA, P_VEN, P_DESC, 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_ap_dn 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;
				# ACC. PAYABLE
				IF P_BAL <> 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("AP", P_VEN, "NA");
						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, "AP-DN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", IF(COALESCE(P_DESC, "") = "", "Payable", CONCAT("[Payable] ", P_DESC)), "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_BAL, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AP", P_CUR, P_RATE, P_FUN_BAL);
						
						# SUB LEDGER
						CALL AME_PRO_UPDATE_SL_AP(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_AP_HIS("AP-DN", P_TYPE_PUR, P1, P_VEN, P_CLA, "", P_TERM, "", "", P_REF, P_DESC, P_DATE, P_DATE_DUE, P_BAL, P_DEC_AMT_FOR, 0, "Dr", P4, P_JID, P_ACC_AP, P_CUR, P_RATE, P_FUN_BAL);
				END IF;
				# DISC
				IF P_DISC <> 0 THEN
						SET P_ACC_DISC = AME_FUN_ACC("DISC-AP-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_DISC, P_CLA, "AP-DN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "Discount", "Dr", P_TERM, "", "", "", "", 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;
				# VAT
				IF P_VAT <> 0 THEN
						SET P_ACC_VAT = AME_FUN_ACC("TAX-VAT-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_VAT, P_CLA, "AP-DN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "VAT", "Cr", P_TERM, "", "", "", "", 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;
				# WHT
				IF P_WHT <> 0 THEN
						SET P_ACC_WHT = AME_FUN_ACC("TAX-WHT", "DEFAULT", "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_WHT, P_CLA, "AP-DN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "WHT", "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_WHT, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "WHT", P_CUR, P_RATE, P_FUN_WHT);
				END IF;
		END;
		BEGIN
				DEClARE C1 CURSOR FOR SELECT 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)), T1.var_type, T1.int_batchno, AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P_DEC_QTY), T1.dou_foc, T1.dou_price, T1.dou_disdol FROM ame_ap_dn_det AS T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				SET @T_FOC = 0;
				OPEN C1;
						L1: LOOP FETCH C1 INTO P_ITEM, P_ACC_EXP, P_LOC, P_CLA_DET, P_UOM, P_DESC_DET, P_TYPE_AC, P_BAT_NUM_AC, P_QTY, P_FOC, P_COST, P_DISC_DET;
								IF D1 = 1 THEN LEAVE L1; END IF;
								SET @P_TC = 0;SET @P_TC_FUN = 0;
								CASE P_TYPE_PUR
								WHEN "Purchase Inventory" THEN
										SET @P_ITEM_TYPE = AME_FUN_ITEM_TYPE(P_ITEM);
										SET @CON = AME_FUN_ITEM_CON(P_ITEM, P_UOM);
										IF @P_ITEM_TYPE = "Inventory Part" OR @P_ITEM_TYPE = "Fixed Asset" THEN
												BEGIN
														DECLARE D3 INTEGER DEFAULT 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 C3 CURSOR FOR SELECT int_batchno, T.dou_qty, 
														AME_FUN_ROUND(dou_druc - dou_cruc, P_DEC_COST_FUN), AME_FUN_ROUND(dou_drac1 - dou_crac1, P_DEC_COST_FUN), AME_FUN_ROUND(dou_drac2 - dou_crac2, P_DEC_COST_FUN), AME_FUN_ROUND(dou_drac3 - dou_crac3, P_DEC_COST_FUN), AME_FUN_ROUND(dou_drac4 - dou_crac4, P_DEC_COST_FUN), AME_FUN_ROUND(dou_drac5 - dou_crac5, P_DEC_COST_FUN)
														FROM ame_sl_ic_batch AS T WHERE T.var_itemid = P_ITEM AND T.var_locid = P_LOC AND 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_FUN);
																		SET @AC1 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC1 / P_QTY_BAL), P_DEC_COST_FUN);
																		SET @AC2 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC2 / P_QTY_BAL), P_DEC_COST_FUN);
																		SET @AC3 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC3 / P_QTY_BAL), P_DEC_COST_FUN);
																		SET @AC4 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC4 / P_QTY_BAL), P_DEC_COST_FUN);
																		SET @AC5 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC5 / P_QTY_BAL), P_DEC_COST_FUN);
																		SET @P_TC = AME_FUN_ROUND(@P_TC + @UC + @AC1 + @AC2 + @AC3 + @AC4 + @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, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Cr", P_DEC_QTY, P_DEC_COST_FUN);
																		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_FUN);
																		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, "AP-DN", P_TYPE_PUR, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), P_VEN, "", 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);
																		# ACC. FOC
																		SET @T_FOC = AME_FUN_ROUND(@T_FOC + AME_FUN_ROUND(AME_FUN_ROUND(P_FOC * @CON, P_DEC_QTY) * AME_FUN_ROUND(AME_FUN_ROUND(P_UC / P_QTY_BAL, P_DEC_COST_FUN) + AME_FUN_ROUND(P_AC1 / P_QTY_BAL, P_DEC_COST_FUN) + AME_FUN_ROUND(P_AC2 / P_QTY_BAL, P_DEC_COST_FUN) + AME_FUN_ROUND(P_AC3 / P_QTY_BAL, P_DEC_COST_FUN) + AME_FUN_ROUND(P_AC4 / P_QTY_BAL, P_DEC_COST_FUN) + AME_FUN_ROUND(P_AC5 / P_QTY_BAL, P_DEC_COST_FUN), P_DEC_COST_FUN), P_DEC_COST_FUN), P_DEC_COST_FUN);
																		IF P_QTY_BATCH = 0 THEN LEAVE L3; END IF;
																END LOOP L3;
														CLOSE C3;
												END;
												# ACC. INVENTORY
												IF @P_TC <> 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_DET, "AP-DN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", P_ITEM, CONCAT("[Inventory] ", P_DESC_DET), "Cr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, -AME_FUN_ROUND(@P_TC / P_RATE, P_DEC_AMT_FOR), P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, -AME_FUN_ROUND(@P_TC, P_DEC_AMT_FUN));
												END IF;
										ELSEIF @P_ITEM_TYPE = "Non-Inventory Part" THEN
												# ACC. INVENTORY
												SET @P_TC = AME_FUN_ROUND(P_QTY  * P_COST, P_DEC_AMT_FOR);
												IF @P_TC <> 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_DET, "AP-DN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", P_ITEM, CONCAT("[Inventory] ", P_DESC_DET), "Cr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, -@P_TC, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, -AME_FUN_ROUND(@P_TC * P_RATE, P_DEC_AMT_FUN));
												END IF;
										END IF;		
								ELSE
										SET @Donothing = NULL;
								END CASE;
						END LOOP L1;
				CLOSE C1;
				# ACC. FOC
				IF @T_FOC <> 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("FOC-AP-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_AP, P_CLA, "AP-DN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "FOC", "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, @T_FOC / P_RATE, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "FOC", P_CUR, P_RATE, AME_FUN_ROUND(@T_FOC, P_DEC_AMT_FUN));
				END IF;
				# ACC. GAIN/LOSS RETURNED
				SET @P_DIFF_FOR = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF_FOR(P_JID), P_DEC_AMT_FOR);
				SET @P_DIFF = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF(P_JID), P_DEC_AMT_FOR);
				IF @P_DIFF > 0 OR @P_DIFF_FOR > 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-RET", "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, "AP-DN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Returned/Rounding Defference", "Dr", "", "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, @P_DIFF_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "RET-GAINLOSS", P_CUR, P_RATE, @P_DIFF);
				ELSEIF @P_DIFF < 0 OR @P_DIFF_FOR < 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-RET", "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, "AP-DN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Returned/Rounding Defference", "Cr", "", "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, @P_DIFF_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "RET-GAINLOSS", P_CUR, P_RATE, @P_DIFF);
				END IF;

				CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE_PUR, P1, P_REF, P_DESC, P2, P_DATE, P_ACC_VAT, "AP-DN", "VAT", P_VAT, P_VAT * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
				UPDATE ame_ap_dn SET var_status = 'Posted', int_jid = P_JID, var_post = P2, dat_post = NOW() WHERE var_id = P1;
		END;
		# PAYMENT
		BEGIN
				DECLARE P_ID VARCHAR(100) DEFAULT "";
				DECLARE C1 CURSOR FOR SELECT T1.var_id FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Debit Note' AND T1.var_docid = P1 AND COALESCE(T2.tin_paid, 0) = 3 LIMIT 1;
				OPEN C1;
						IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID;
								IF P3 = -1 THEN
										CALL AME_PRO_POST_AP_PY_1(P_ID, P2, -1, -1);
								ELSE
										CALL AME_PRO_POST_AP_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_doctype = "AP-DN" AND T1.var_docid = P1 AND T1.var_status = "Posted"), 0) + COALESCE((SELECT SUM(T1.dou_app) FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = "Debit Note" AND T1.var_docid = P1 AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ap_dn 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_ap_dn 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_ap_dn 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_ap_dn 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;	
# NOTE: P3 = P_JID, P4 = P_NUM_AP, P5 = P_NUM_IC, P6 = P_BAT_NUM
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_AP_DN_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AP_DN_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_AP INTEGER DEFAULT -1;
		DECLARE P_NUM_IC INTEGER DEFAULT -1;
		DECLARE P_BAT_NUM INTEGER DEFAULT -1;
		DECLARE P_DATE DATETIME;
		DECLARE P_VEN 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 AP
		BEGIN
				DECLARE C2 CURSOR FOR SELECT T1.var_cur, T1.dou_fundr, T1.int_line, T1.dat_date, T1.var_venid, T1.dou_dr, T1.int_jid FROM ame_sl_ap_his T1 WHERE T1.var_modid = 'AP-DN' AND T1.var_docid = P1;
				OPEN C2;
						IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_CUR, P_BAL_FUN, P_NUM_AP, P_DATE, P_VEN, 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_AP(P_VEN, "Dr", P_DATE, -P_BAL, P_DEC_AMT_FOR, P_CUR, -P_BAL_FUN, P_DEC_AMT_FUN);
								DELETE FROM ame_sl_ap_his WHERE var_modid = 'AP-DN' 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_TYPE VARCHAR(100) DEFAULT "";
				DECLARE P_QTY 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 C1 CURSOR FOR SELECT int_id, var_itemid, var_locid, var_doctype, int_batchno, 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 = 'AP-DN' 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_ITEM, P_LOC, P_TYPE, 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;
								CASE P_TYPE
								WHEN "Purchase Inventory" 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_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, "Cr", P_DEC_QTY, P_DEC_COST_FUN);
								ELSE
										SET @Donothing = NULL;
								END CASE;
						END LOOP L1;
				CLOSE C1;
				DELETE FROM ame_sl_ic_his WHERE var_modid = 'AP-DN' 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_AP_DN_1(P1, P2, P3, P_NUM_AP, P_NUM_IC, P_BAT_NUM, P_JID);
		ELSE
				UPDATE ame_ap_dn 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_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Debit Note' AND T1.var_docid = P1 AND COALESCE(T2.tin_paid, 0) = 3 LIMIT 1;
						OPEN C1;
								IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID;
										IF P3 = 0 THEN CALL AME_PRO_POST_AP_PY_2(P_ID, P2, 0); END IF;
								END IF;
						CLOSE C1;
				END;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_AP_IN_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AP_IN_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 D1 INTEGER DEFAULT 0;
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_BAT_NUM INTEGER DEFAULT 0;
		DECLARE P_BAT_NUM_AC INTEGER DEFAULT 0;
		DECLARE P_LINE INTEGER DEFAULT 0;
		DECLARE P_LINE_JE INTEGER DEFAULT 0;
		DECLARE P_NUM_IC INTEGER DEFAULT 0;
		DECLARE P_DATE DATETIME;
		DECLARE P_DATE_DUE DATETIME;
		DECLARE P_ACC_AP VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_IC VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_EXP VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_DISC VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_VAT VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_WHT 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_PUR VARCHAR(100) DEFAULT "";
		DECLARE P_TYPE_AC VARCHAR(100) DEFAULT "";
		DECLARE P_LOC VARCHAR(100) DEFAULT "";
		DECLARE P_ITEM VARCHAR(100) DEFAULT "";
		DECLARE P_UOM VARCHAR(100) DEFAULT "";
		DECLARE P_TERM VARCHAR(100) 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_QTY DOUBLE DEFAULT 0.0;
		DECLARE P_FOC DOUBLE DEFAULT 0.0;
		DECLARE P_COST DOUBLE DEFAULT 0.0;

		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_RATE DOUBLE DEFAULT 0.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 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, int_cost, 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;
		
		IF P7 = -1 THEN SET P_JID = AME_FUN_NEW_JE_NUM(); ELSE SET P_JID = P7; 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_venid, T1.var_desc, 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_PUR, P_REF, P_CLA, P_VEN, P_DESC, 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_ap_in 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;
				# SUB LEDGER
				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-IN", P_TYPE_PUR, P1, P_VEN, 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_AP, P_CUR, P_RATE, P_FUN_BAL);
				IF P_TYPE_PUR = "Purchase Inventory" THEN IF P6 = -1 THEN SET P_BAT_NUM = AME_FUN_NEW_BATCH_NUM(); ELSE SET P_BAT_NUM = P6; END IF; END IF;
		END;
		SET P_LINE_JE = 0;
		BEGIN			
				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_TAC DOUBLE DEFAULT 0.0;
				DECLARE P_FUN_AC1 DOUBLE DEFAULT 0.0;
				DECLARE P_FUN_AC2 DOUBLE DEFAULT 0.0;
				DECLARE P_FUN_AC3 DOUBLE DEFAULT 0.0;
				DECLARE P_FUN_AC4 DOUBLE DEFAULT 0.0;
				DECLARE P_FUN_AC5 DOUBLE DEFAULT 0.0;
				DECLARE P_FUN_TAC DOUBLE DEFAULT 0.0;
				DEClARE C1 CURSOR FOR SELECT 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)), T1.var_type, T1.int_batchno, AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P_DEC_QTY), T1.dou_foc, T1.dou_price,
				COALESCE(T2.dou_ac1, 0), COALESCE(T2.dou_ac2, 0), COALESCE(T2.dou_ac3, 0), COALESCE(T2.dou_ac4, 0), COALESCE(T2.dou_ac5, 0),
				COALESCE(T2.dou_funac1, 0), COALESCE(T2.dou_funac2, 0), COALESCE(T2.dou_funac3, 0), COALESCE(T2.dou_funac4, 0), COALESCE(T2.dou_funac5, 0)
				FROM ame_ap_in_det AS T1 LEFT JOIN ame_ap_in_ac T2 ON T1.var_id = T2.var_id AND T1.var_itemid = T2.var_itemid AND T1.var_locid = T2.var_locid AND T1.int_poline = T2.int_poline WHERE T1.var_id = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				SET @T_FOC = 0;
				OPEN C1;
						L1: LOOP FETCH C1 INTO P_ITEM, P_ACC_EXP, P_LOC, P_CLA_DET, P_UOM, P_DESC_DET, P_TYPE_AC, P_BAT_NUM_AC, P_QTY, P_FOC, P_COST, 
								P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, P_FUN_AC1, P_FUN_AC2, P_FUN_AC3, P_FUN_AC4, P_FUN_AC5;
								IF D1 = 1 THEN LEAVE L1; END IF;
								CASE P_TYPE_PUR
								WHEN "Purchase Inventory" THEN
										SET @P_ITEM_TYPE = AME_FUN_ITEM_TYPE(P_ITEM);
										SET @CON = AME_FUN_ITEM_CON(P_ITEM, P_UOM);
										IF @P_ITEM_TYPE = "Inventory Part" OR @P_ITEM_TYPE = "Fixed Asset" THEN
												SET P_LINE = P_LINE + 1;
												CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY * @CON, P_QTY * P_COST * P_RATE, P_AC1  * P_RATE, P_AC2 * P_RATE, P_AC3 * P_RATE, P_AC4 * P_RATE, P_AC5 * P_RATE, "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 * @CON, P_QTY * P_COST * P_RATE, P_AC1 * P_RATE, P_AC2 * P_RATE, P_AC3 * P_RATE, P_AC4 * P_RATE, P_AC5 * P_RATE, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
												CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, AME_FUN_ROUND(P_QTY * @CON, P_DEC_QTY), AME_FUN_ROUND(P_QTY  * P_COST * P_RATE, P_DEC_COST_FUN),  AME_FUN_ROUND(P_AC1 * P_RATE, P_DEC_COST_FUN),  AME_FUN_ROUND(P_AC2 * P_RATE, P_DEC_COST_FUN),  AME_FUN_ROUND(P_AC3 * P_RATE, P_DEC_COST_FUN),  AME_FUN_ROUND(P_AC4 * P_RATE, P_DEC_COST_FUN),  AME_FUN_ROUND(P_AC5 * P_RATE, P_DEC_COST_FUN), P_NUM_IC, P_LINE, "AP-IN", P_TYPE_PUR, P1, P_VEN, "", "", 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);
										END IF;
										# ACC. INVENTORY
										SET @P_TC = AME_FUN_ROUND(P_QTY  * P_COST, P_DEC_AMT_FOR);
										IF @P_TC <> 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_DET, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", P_ITEM, IF(COALESCE(P_DESC_DET, "") = "", "Inventory", CONCAT("[Inventory] ", P_DESC_DET)), "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, @P_TC, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, AME_FUN_ROUND(@P_TC * P_RATE, P_DEC_AMT_FUN));
										END IF;
										IF P_AC1 > 0 THEN
												SET P_ACC_IC = AME_FUN_ACC("IC", P_ITEM, "NA");
												SET P_LINE_JE = P_LINE_JE + 1;
												SET @P_AC_TYPE_NAME = AME_FUN_ADDI_COST("AC1");
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_IC, P_CLA_DET, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", P_ITEM, IF(COALESCE(P_DESC_DET, "") = "", @P_AC_TYPE_NAME, CONCAT("[", @P_AC_TYPE_NAME, "] ", P_DESC_DET)), "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_AC1, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, P_FUN_AC1);
												SET P_TAC = AME_FUN_ROUND(P_TAC + P_AC1, P_DEC_AMT_FOR);
												SET P_FUN_TAC = AME_FUN_ROUND(P_FUN_TAC + P_FUN_AC1, P_DEC_AMT_FUN);
										END IF;	
										IF P_AC2 > 0 THEN
												SET P_ACC_IC = AME_FUN_ACC("IC", P_ITEM, "NA");
												SET P_LINE_JE = P_LINE_JE + 1;
												SET @P_AC_TYPE_NAME = AME_FUN_ADDI_COST("AC1");
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_IC, P_CLA_DET, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", P_ITEM, IF(COALESCE(P_DESC_DET, "") = "", @P_AC_TYPE_NAME, CONCAT("[", @P_AC_TYPE_NAME, "] ", P_DESC_DET)), "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_AC2, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, P_FUN_AC2);
												SET P_TAC = AME_FUN_ROUND(P_TAC + P_AC2, P_DEC_AMT_FOR);
												SET P_FUN_TAC = AME_FUN_ROUND(P_FUN_TAC + P_FUN_AC2, P_DEC_AMT_FUN);
										END IF;	
										IF P_AC3 > 0 THEN
												SET P_ACC_IC = AME_FUN_ACC("IC", P_ITEM, "NA");
												SET P_LINE_JE = P_LINE_JE + 1;
												SET @P_AC_TYPE_NAME = AME_FUN_ADDI_COST("AC3");
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_IC, P_CLA_DET, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", P_ITEM, IF(COALESCE(P_DESC_DET, "") = "", @P_AC_TYPE_NAME, CONCAT("[", @P_AC_TYPE_NAME, "] ", P_DESC_DET)), "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_AC3, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, P_FUN_AC3);
												SET P_TAC = AME_FUN_ROUND(P_TAC + P_AC3, P_DEC_AMT_FOR);
												SET P_FUN_TAC = AME_FUN_ROUND(P_FUN_TAC + P_FUN_AC3, P_DEC_AMT_FUN);
										END IF;	
										IF P_AC4 > 0 THEN
												SET P_ACC_IC = AME_FUN_ACC("IC", P_ITEM, "NA");
												SET P_LINE_JE = P_LINE_JE + 1;
												SET @P_AC_TYPE_NAME = AME_FUN_ADDI_COST("AC4");
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_IC, P_CLA_DET, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", P_ITEM, IF(COALESCE(P_DESC_DET, "") = "", @P_AC_TYPE_NAME, CONCAT("[", @P_AC_TYPE_NAME, "] ", P_DESC_DET)), "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_AC4, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, P_FUN_AC4);
												SET P_TAC = AME_FUN_ROUND(P_TAC + P_AC4, P_DEC_AMT_FOR);
												SET P_FUN_TAC = AME_FUN_ROUND(P_FUN_TAC + P_FUN_AC4, P_DEC_AMT_FUN);
										END IF;
										IF P_AC5 > 0 THEN
												SET P_ACC_IC = AME_FUN_ACC("IC", P_ITEM, "NA");
												SET P_LINE_JE = P_LINE_JE + 1;
												SET @P_AC_TYPE_NAME = AME_FUN_ADDI_COST("AC5");
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_IC, P_CLA_DET, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", P_ITEM, IF(COALESCE(P_DESC_DET, "") = "", @P_AC_TYPE_NAME, CONCAT("[", @P_AC_TYPE_NAME, "] ", P_DESC_DET)), "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_AC5, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, P_FUN_AC5);
												SET P_TAC = AME_FUN_ROUND(P_TAC + P_AC5, P_DEC_AMT_FOR);
												SET P_FUN_TAC = AME_FUN_ROUND(P_FUN_TAC + P_FUN_AC5, P_DEC_AMT_FUN);
										END IF;
										# ACC. FOC
										SET @T_FOC = AME_FUN_ROUND(@T_FOC + AME_FUN_ROUND(P_FOC * P_COST, P_DEC_COST_FUN), P_DEC_COST_FUN);
								WHEN "Additional Costing" THEN
										SET P_BAT_NUM = P_BAT_NUM_AC;
										CASE P_TYPE_AC
										WHEN "AC1" THEN
												SET P_LINE = P_LINE + 1;
												CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, 0, P_COST * P_RATE, 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, P_DATE, 0, 0, P_COST * P_RATE, 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, P_DATE, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), 0, 0, 0, 0, P_NUM_IC, P_LINE, "AP-IN", P_TYPE_AC, P1, P_VEN, "", "", P_CLA_DET, P_REF, P_DESC, P_DESC_DET, P_CUR, P_RATE, 0, P_JID);
										WHEN "AC2" THEN
												SET P_LINE = P_LINE + 1;
												CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
												CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), 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, P_DATE, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), 0, 0, 0, P_NUM_IC, P_LINE, "AP-IN", P_TYPE_AC, P1, P_VEN, "", "", P_CLA_DET, P_REF, P_DESC, P_DESC_DET, P_CUR, P_RATE, 0, P_JID);
										WHEN "AC3" THEN
												SET P_LINE = P_LINE + 1;
												CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), 0, 0, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
												CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), 0, 0, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
												CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), 0, 0, P_NUM_IC, P_LINE, "AP-IN", P_TYPE_AC, P1, P_VEN, "", "", P_CLA_DET, P_REF, P_DESC, P_DESC_DET, P_CUR, P_RATE, 0, P_JID);
										WHEN "AC4" THEN
												SET P_LINE = P_LINE + 1;
												CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, 0, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), 0, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
												CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), 0, "Dr", P_DEC_QTY, P_DEC_COST_FUN);
												CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), 0, P_NUM_IC, P_LINE, "AP-IN", P_TYPE_AC, P1, P_VEN, "", "", P_CLA_DET, P_REF, P_DESC, P_DESC_DET, P_CUR, P_RATE, 0, P_JID);
										WHEN "AC5" THEN
												SET P_LINE = P_LINE + 1;
												CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, 0, 0, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), "Dr", P_DEC_QTY, P_DEC_COST_FUN);
												CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, 0, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), "Dr", P_DEC_QTY, P_DEC_COST_FUN);
												CALL AME_PRO_UPDATE_SL_IC_HIS(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, 0, 0, 0, 0, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_COST_FUN), P_NUM_IC, P_LINE, "AP-IN", P_TYPE_AC, P1, P_VEN, "", "", P_CLA_DET, P_REF, P_DESC, P_DESC_DET, P_CUR, P_RATE, 0, P_JID);
										ELSE
												SET @Donothing = NULL;
										END CASE;
										IF P_COST <> 0 THEN
												#SET P_ACC_EXP = AME_FUN_ACC(CONCAT("COGS-", P_TYPE_AC), P_ITEM, P_TYPE_AC);
												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_DET, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", P_ITEM, IF(COALESCE(P_DESC_DET, "") = "", "Inventory", CONCAT("[Inventory] ", P_DESC_DET)), "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_COST, P_DEC_AMT_FOR, "", "AP-IN", P_TYPE_AC, "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, AME_FUN_ROUND(P_COST * P_RATE, P_DEC_AMT_FUN));
										END IF;
								WHEN "Apply to GL Account" THEN
										IF P_COST > 0 THEN
												SET P_LINE_JE = P_LINE_JE + 1;
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_EXP, P_CLA_DET, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_COST, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "GL", P_CUR, P_RATE, P_COST * P_RATE);
										ELSEIF P_COST < 0 THEN
												SET P_LINE_JE = P_LINE_JE + 1;
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_EXP, P_CLA_DET, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Cr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_COST, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "GL", P_CUR, P_RATE, P_COST * P_RATE);
										END IF;
								ELSE
										SET @Donothing = NULL;
								END CASE;
						END LOOP L1;
				CLOSE C1;
				IF P_DISC <> 0 THEN
						SET P_ACC_DISC = AME_FUN_ACC("DISC-AP-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_DISC, P_CLA, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "Discount", "Cr", P_TERM, "", "", "", "", 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;
				# VAT
				IF P_VAT <> 0 THEN
						SET P_ACC_VAT = AME_FUN_ACC("TAX-VAT-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_VAT, P_CLA, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "VAT", "Dr", P_TERM, "", "", "", "", 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;
				# WHT
				IF P_WHT <> 0 THEN
						SET P_ACC_WHT = AME_FUN_ACC("TAX-WHT", "DEFAULT", "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_WHT, P_CLA, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "WHT", "Cr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, -P_WHT, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "WHT", P_CUR, P_RATE, -P_FUN_WHT);
				END IF;
				# ACC. FOC
				IF @T_FOC <> 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("FOC-AP-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_AP, P_CLA, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "FOC", "Cr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, -@T_FOC, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "FOC", P_CUR, P_RATE, -AME_FUN_ROUND(@T_FOC * P_RATE, P_DEC_AMT_FUN));
				END IF;
				# ACC. Clearance AC
				IF P_TAC > 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("CLEARANCE-AC", "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, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "Clearance AC", "Cr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, -P_TAC, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "Clearance AC", P_CUR, P_RATE, -P_FUN_TAC);
				END IF;
				# ACC. PAYABLE
				IF P_BAL <> 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("AP", P_VEN, "NA");
						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, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", IF(COALESCE(P_DESC, "") = "", "Payable", CONCAT("[Payable] ", P_DESC)), "Cr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, -P_BAL, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AP", P_CUR, P_RATE, -P_FUN_BAL);
				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_AP = 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_AP, P_CLA, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Rounding Difference", "Dr", P_TERM, "", "", "", "", 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_AP = 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_AP, P_CLA, "AP-IN", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Rounding Difference", "Cr", P_TERM, "", "", "", "", 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_PUR, P1, P_REF, P_DESC, P2, P_DATE, P_ACC_VAT, "AP-IN", "VAT", P_VAT, P_VAT * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
				UPDATE ame_ap_in SET var_status = 'Posted', int_jid = P_JID, var_post = P2, dat_post = NOW() WHERE var_id = P1;
				IF P_TYPE_PUR = "Purchase Inventory" THEN UPDATE ame_ap_in_det SET int_batchno = P_BAT_NUM WHERE var_id = P1; END IF;
		END;
		# PAYMENT
		BEGIN
				DECLARE P_ID VARCHAR(100) DEFAULT "";
				DECLARE C1 CURSOR FOR SELECT T1.var_id FROM ame_ap_py_det T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P1 AND COALESCE(T2.tin_paid, 0) = 1 LIMIT 1;
				OPEN C1;
						IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID;
								IF P3 = -1 THEN
										CALL AME_PRO_POST_AP_PY_1(P_ID, P2, -1, -1);
								ELSE
										CALL AME_PRO_POST_AP_PY_2(P_ID, P2, P3);
								END IF;
						ELSE
								# UPDATE PAID
								CALL AME_PRO_UPDATE_PMT_STATUS_1("AP-IN", P1, P_CUR);
						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;	
# NOTE: P4 = P_NUM_AP, P5 = P_NUM_IC, P6 = P_BAT_NUM, P7 = P_JID
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_AP_IN_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AP_IN_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_AP INTEGER DEFAULT -1;
		DECLARE P_NUM_IC INTEGER DEFAULT -1;
		DECLARE P_BAT_NUM INTEGER DEFAULT -1;
		DECLARE P_DATE DATETIME;
		DECLARE P_VEN 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_PRICE_FOR INTEGER DEFAULT 6;
		DECLARE P_DEC_AMT_FOR INTEGER DEFAULT 2;
		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_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_PRICE_FUN, P_DEC_AMT_FUN; END IF;CLOSE C1;END;
		# SUB LEDGER AP
		BEGIN
				DECLARE C2 CURSOR FOR SELECT T1.var_cur, T1.dou_funcr, T1.int_line, T1.dat_date, T1.var_venid, T1.dou_cr, T1.int_jid FROM ame_sl_ap_his T1 WHERE T1.var_modid = 'AP-IN' AND T1.var_docid = P1;
				OPEN C2;
						IF FOUND_ROWS() > 0 THEN
								FETCH C2 INTO P_CUR, P_BAL_FUN, P_NUM_AP, P_DATE, P_VEN, 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_PRICE_FOR, 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_BAL_FUN, P_DEC_AMT_FUN);
								DELETE FROM ame_sl_ap_his WHERE var_modid = 'AP-IN' 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_TYPE VARCHAR(100) DEFAULT "";
				DECLARE P_QTY 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 C1 CURSOR FOR SELECT T1.int_id, T1.var_itemid, T1.var_locid, T1.var_doctype, 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 = 'AP-IN' 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_ITEM, P_LOC, P_TYPE, 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;
						CASE P_TYPE
						WHEN "Purchase Inventory" 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_PRICE_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_PRICE_FUN);
						WHEN "AC1" THEN
								CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, 0, -P_AC1, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_PRICE_FUN);
								CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, -P_AC1, 0, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_PRICE_FUN);
						WHEN "AC2" THEN
								CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, 0, 0, -P_AC2, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_PRICE_FUN);
								CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, 0, -P_AC2, 0, 0, 0, "Dr", P_DEC_QTY, P_DEC_PRICE_FUN);
						WHEN "AC3" THEN
								CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, 0, 0, 0, -P_AC3, 0, 0, "Dr", P_DEC_QTY, P_DEC_PRICE_FUN);
								CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, 0, 0, -P_AC3, 0, 0, "Dr", P_DEC_QTY, P_DEC_PRICE_FUN);
						WHEN "AC4" THEN
								CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, 0, 0, 0, 0, -P_AC3, 0, "Dr", P_DEC_QTY, P_DEC_PRICE_FUN);
								CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, 0, 0, 0, -P_AC3, 0, "Dr", P_DEC_QTY, P_DEC_PRICE_FUN);
						WHEN "AC5" THEN
								CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, 0, 0, 0, 0, 0, 0, -P_AC3, "Dr", P_DEC_QTY, P_DEC_PRICE_FUN);
								CALL AME_PRO_UPDATE_SL_IC(P_LOC, P_ITEM, P_BAT_NUM, P_DATE, 0, 0, 0, 0, 0, 0, -P_AC3, "Dr", P_DEC_QTY, P_DEC_PRICE_FUN);
						ELSE
								SET @Donothing = NULL;
						END CASE;
						END LOOP L1;
				CLOSE C1;
				DELETE FROM ame_sl_ic_his WHERE var_modid = 'AP-IN' 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_AP_IN_1(P1, P2, P3, P_NUM_AP, P_NUM_IC, P_BAT_NUM, P_JID);
		ELSE
				UPDATE ame_ap_in SET var_status = 'Deleted', var_pmtstatus = 'Unpaid', 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_ap_py_det T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P1 AND COALESCE(T2.tin_paid, 0) = 1 LIMIT 1;
						OPEN C1;
								IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID;
										IF P3 = 0 THEN CALL AME_PRO_POST_AP_PY_2(P_ID, P2, 0); END IF;
								END IF;
						CLOSE C1;
				END;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_AP_PY_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AP_PY_1`(P1 varchar(100), P2 varchar(100), P3 int(11), P4 int(11))
    DETERMINISTIC
BEGIN
		DECLARE D1 INTEGER DEFAULT 0;
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_LINE INTEGER DEFAULT 0;
		DECLARE P_LINE_JE INTEGER DEFAULT 0;
		DECLARE P_DATE DATETIME;
		DECLARE P_ACC_AP VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_DISC 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_PMT_METHOD VARCHAR(100) DEFAULT "";
		DECLARE P_PMT_REF VARCHAR(250) DEFAULT "";
		DECLARE P_BAL DOUBLE DEFAULT 0.0;
		DECLARE P_DISC DOUBLE DEFAULT 0.0;
		DECLARE P_AMT 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_APP DOUBLE DEFAULT 0.0;
		DECLARE P_APP_PMT DOUBLE DEFAULT 0.0;
		DECLARE P_APP_PMT_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;
		
		# JOURNAL NUMBER
		IF P3 = -1 THEN SET P_JID = AME_FUN_NEW_JE_NUM(); ELSE SET P_JID = P3; END IF;
		SELECT T1.dat_date, T1.var_type, T1.var_ref, T1.var_claid, T1.var_venid, T1.var_accid, T1.var_desc, T1.var_pmttype, T1.var_pmtref, T1.dou_net, T1.var_cur, T1.dou_rate, T1.dou_funar
		INTO P_DATE, P_TYPE, P_REF, P_CLA, P_VEN, P_ACC_CASH, P_DESC, P_PMT_METHOD, P_PMT_REF, P_BAL, P_CUR, P_RATE, P_FUN_BAL FROM ame_ap_py 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;
		
		IF P_TYPE = "Prepayment" OR P_TYPE = "Apply to Purchase Invoice" THEN
				CALL AME_PRO_UPDATE_SL_AP(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_AP_HIS("AP-PY", 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);
				SELECT AME_FUN_ROUND(COALESCE(SUM(T1.dou_app), 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE(SUM(T1.dou_funapp), 0), P_DEC_AMT_FUN) INTO P_APP_PMT, P_APP_PMT_FUN FROM ame_ap_py_app T1 WHERE T1.var_id = P1 AND T1.var_doctype <> 'Apply to GL Account';
				IF P_APP_PMT <> 0 THEN
						CALL AME_PRO_UPDATE_SL_AP(P_VEN, "Cr", P_DATE, P_APP_PMT, P_DEC_AMT_FOR, P_CUR, P_APP_PMT_FUN, P_DEC_AMT_FUN);
						CALL AME_PRO_UPDATE_SL_AP_HIS("AP-PY", P_TYPE, P1, P_VEN, P_CLA, P_ACC_CASH, "", P_PMT_METHOD, P_PMT_REF, P_REF, P_DESC, P_DATE, NULL, P_APP_PMT, P_DEC_AMT_FOR, 1, "Cr", P4, P_JID, P_ACC_AP, P_CUR, P_RATE, P_APP_PMT_FUN);
				END IF;
		ELSE
				CALL AME_PRO_UPDATE_SL_AP_HIS("AP-PY", 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);
		END IF;

		BEGIN
				DECLARE P_TCASH DOUBLE DEFAULT 0.0;
				DECLARE P_TAPP DOUBLE DEFAULT 0.0;
				DEClARE C1 CURSOR FOR SELECT T1.var_docid, T1.var_claid, T1.var_desc, AME_FUN_ROUND(COALESCE(T1.dou_disc, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE(T1.dou_amt, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE(T1.dou_app, 0), P_DEC_AMT_FOR) FROM ame_ap_py_det AS T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				SET P_ACC_AP = AME_FUN_ACC("AP", P_VEN, "NA");
				SET P_ACC_DISC = AME_FUN_ACC("DISC-AP-PY", "DEFAULT", "DEFAULT");
				OPEN C1;
						L1: LOOP FETCH C1 INTO P_DOC, P_CLA_DET, P_DESC_DET, P_DISC, P_AMT, P_APP;
								IF D1 = 1 THEN LEAVE L1; END IF;
								SET P_TCASH = AME_FUN_ROUND(P_TCASH + P_AMT, P_DEC_AMT_FOR);
								CASE P_TYPE
								WHEN "Apply to Purchase Invoice" THEN
										SET @P_AP = AME_FUN_ROUND(P_DISC + P_AMT + P_APP, P_DEC_AMT_FOR);
										BEGIN
												DECLARE P_DOC_TYPE VARCHAR(100) DEFAULT "";
												DECLARE P_DOC_REF VARCHAR(250) DEFAULT "";
												DECLARE P_DOC_TERM VARCHAR(100) DEFAULT "";
												DECLARE P_DOC_DATE DATETIME DEFAULT NULL;
												DECLARE P_DOC_DUE DATETIME DEFAULT NULL;
												DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
												DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
												DECLARE P_DOC_RATE DOUBLE DEFAULT 0.0;
												DECLARE C2 CURSOR FOR SELECT T1.var_type, T1.var_ref, T1.var_term, T1.dat_date, T1.dat_due, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE((SELECT SUM(T1.dou_amt + T1.dou_app + T1.dou_disc) FROM ame_ap_py_det T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_id <> P1 AND T1.var_docid = P_DOC AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR), T1.dou_rate FROM ame_ap_in T1 WHERE T1.var_id = P_DOC;
												OPEN C2;
														IF FOUND_ROWS() > 0 THEN
																FETCH C2 INTO P_DOC_TYPE, P_DOC_REF, P_DOC_TERM, P_DOC_DATE, P_DOC_DUE, P_DOC_NET, P_DOC_PAID, P_DOC_RATE;
																SET @PAID = AME_FUN_ROUND(@P_AP + P_DOC_PAID, P_DEC_AMT_FOR);
																IF P_DOC_NET = @PAID THEN
																		UPDATE ame_ap_in SET var_pmtstatus = "Fully Paid", dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																ELSEIF P_DOC_NET > @PAID AND @PAID > 0 THEN
																		UPDATE ame_ap_in SET var_pmtstatus = "Partially Paid", dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																ELSE
																		UPDATE ame_ap_in SET var_pmtstatus = "Unpaid", dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_DOC_RATE, P_DEC_AMT_FUN) WHERE var_id = P_DOC;
																END IF;
														END IF;
												CLOSE C2;
												IF @P_AP <> 0 THEN
														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, "AP-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC_DET, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, @P_AP, P_DEC_AMT_FOR, P_DOC_TERM, "AP-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "AP", P_CUR, P_RATE, @P_AP * P_DOC_RATE);
												END IF;
												IF P_DISC <> 0 THEN
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_DISC, 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, -P_DISC, P_DEC_AMT_FOR, P_DOC_TERM, "AP-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "DISC", P_CUR, P_RATE, -P_DISC * P_RATE);
												END IF;
												BEGIN
														DECLARE D3 INTEGER DEFAULT 0;
														DECLARE P_APP_TYPE VARCHAR(100) DEFAULT "";
														DECLARE P_APP_ID VARCHAR(100) DEFAULT "";
														DECLARE P_APP_CLA VARCHAR(100) DEFAULT "";
														DECLARE P_APP_DESC VARCHAR(2000) DEFAULT "";
														DECLARE P_APP_AMT DOUBLE DEFAULT 0.0;
														DEClARE C3 CURSOR FOR SELECT T1.var_doctype, T1.var_docid, T1.var_claid, IF(COALESCE(T1.var_desc, '') = "", T1.var_doctype, CONCAT(T1.var_doctype, ": ",T1.var_desc)), AME_FUN_ROUND(T1.dou_app, P_DEC_AMT_FOR) FROM ame_ap_py_app AS T1 WHERE var_id = P1 AND var_num = P_DOC ORDER BY T1.int_line;
														DECLARE CONTINUE HANDLER FOR NOT FOUND SET D3 = 1;
														OPEN C3;
																L3: LOOP FETCH C3 INTO P_APP_TYPE, P_APP_ID, P_APP_CLA, P_APP_DESC, P_APP_AMT;
																		IF D3 = 1 THEN LEAVE L3; END IF;
																		CASE P_APP_TYPE
																		WHEN "Prepayment" THEN
																				BEGIN
																						DECLARE P_APP_REF VARCHAR(250) DEFAULT "";
																						DECLARE P_APP_ACC VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_DATE DATETIME DEFAULT NULL;
																						DECLARE P_APP_NET DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_PAID DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_RATE DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_AMT_TOTAL DOUBLE DEFAULT 0.0;
																						DECLARE C4 CURSOR FOR SELECT T1.dou_rate, T2.var_docid, T1.var_ref, T1.dat_date, 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_status = "Posted" AND T1.var_doctype = "AP-PY" AND T1.var_docid = P_APP_ID), 0) + COALESCE((SELECT SUM(T1.dou_app) FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_id <> P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ap_py T1 LEFT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id WHERE T1.var_id = P_APP_ID LIMIT 1;
																						OPEN C4;
																								IF FOUND_ROWS() > 0 THEN
																										FETCH C4 INTO P_APP_RATE, P_APP_ACC, P_APP_REF, P_APP_DATE, P_APP_NET, P_APP_PAID;
																										SELECT AME_FUN_ROUND(SUM(T1.dou_app), P_DEC_AMT_FOR) INTO P_APP_AMT_TOTAL FROM ame_ap_py_app AS T1 WHERE T1.var_id = P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID;	
																										SET @PAID = AME_FUN_ROUND(P_APP_AMT_TOTAL + P_APP_PAID, P_DEC_AMT_FOR);
																										IF P_APP_NET = @PAID THEN
																												UPDATE ame_ap_py SET var_pmtstatus = 'Fully Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSEIF P_APP_NET > @PAID AND @PAID > 0 THEN
																												UPDATE ame_ap_py SET var_pmtstatus = 'Partially Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSE
																												UPDATE ame_ap_py SET var_pmtstatus = 'Unpaid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										END IF;
																								END IF;
																								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_CLA_DET, "AP-PY", P_APP_TYPE, P_APP_ID, P_APP_REF, "", P_VEN, "", "", P_APP_DESC, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, P_APP_DATE, NULL, -P_APP_AMT, P_DEC_AMT_FOR, P_DOC_TERM, "AP-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "PREPAYMENT", P_CUR, P_RATE, -AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT_FUN));
																								END IF;
																						CLOSE C4;
																				END;
																		WHEN "Purchase Return" THEN
																				BEGIN
																						DECLARE P_APP_REF VARCHAR(250) DEFAULT "";
																						DECLARE P_APP_ACC VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_DATE DATETIME DEFAULT NULL;
																						DECLARE P_APP_NET DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_PAID DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_RATE DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_AMT_TOTAL DOUBLE DEFAULT 0.0;
																						DECLARE C4 CURSOR FOR SELECT T1.dou_rate, T1.var_id, T1.var_ref, T1.dat_date, 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_status = "Posted" AND T1.var_doctype = "AP-RET" AND T1.var_docid = P_APP_ID), 0) + COALESCE((SELECT SUM(T1.dou_app) FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_id <> P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ap_ret T1 WHERE T1.var_id = P_APP_ID;
																						OPEN C4;
																								IF FOUND_ROWS() > 0 THEN FETCH C4 INTO P_APP_RATE, P_APP_ACC, P_APP_REF, P_APP_DATE, P_APP_NET, P_APP_PAID;
																										SELECT AME_FUN_ROUND(SUM(T1.dou_app), P_DEC_AMT_FOR) INTO P_APP_AMT_TOTAL FROM ame_ap_py_app AS T1 WHERE T1.var_id = P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID;	
																										SET @PAID = AME_FUN_ROUND(P_APP_AMT_TOTAL + P_APP_PAID, P_DEC_AMT_FOR);
																										IF P_APP_NET = @PAID THEN
																												UPDATE ame_ap_ret SET var_pmtstatus = 'Fully Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSEIF P_APP_NET > @PAID AND @PAID > 0 THEN
																												UPDATE ame_ap_ret SET var_pmtstatus = 'Partially Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSE
																												UPDATE ame_ap_ret SET var_pmtstatus = 'Unpaid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										END IF;
																								END IF;
																								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_ACC_AP, P_CLA_DET, "AP-PY", P_APP_TYPE, P_APP_ID, P_APP_REF, "", P_VEN, "", "", P_APP_DESC, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, P_APP_DATE, NULL, -P_APP_AMT, P_DEC_AMT_FOR, P_DOC_TERM, "AP-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "AP", P_CUR, P_RATE, -AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT_FUN));
																								END IF;
																						CLOSE C4;
																				END;
																		WHEN "Debit Note" THEN
																				BEGIN
																						DECLARE P_APP_REF VARCHAR(250) DEFAULT "";
																						DECLARE P_APP_ACC VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_DATE DATETIME DEFAULT NULL;
																						DECLARE P_APP_NET DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_PAID DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_RATE DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_AMT_TOTAL DOUBLE DEFAULT 0.0;
																						DECLARE C4 CURSOR FOR SELECT T1.dou_rate, T1.var_id, T1.var_ref, T1.dat_date, 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_status = "Posted" AND T1.var_doctype = "AP-DN" AND T1.var_docid = P_APP_ID), 0) + COALESCE((SELECT SUM(T1.dou_app) FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_id <> P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ap_dn T1 WHERE T1.var_id = P_APP_ID;
																						OPEN C4;
																								IF FOUND_ROWS() > 0 THEN FETCH C4 INTO P_APP_RATE, P_APP_ACC, P_APP_REF, P_APP_DATE, P_APP_NET, P_APP_PAID;
																										SELECT AME_FUN_ROUND(SUM(T1.dou_app), P_DEC_AMT_FOR) INTO P_APP_AMT_TOTAL FROM ame_ap_py_app AS T1 WHERE T1.var_id = P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID;	
																										SET @PAID = AME_FUN_ROUND(P_APP_AMT_TOTAL + P_APP_PAID, P_DEC_AMT_FOR);
																										IF P_APP_NET = @PAID THEN
																												UPDATE ame_ap_dn SET var_pmtstatus = 'Fully Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSEIF P_APP_NET > @PAID AND @PAID > 0 THEN
																												UPDATE ame_ap_dn SET var_pmtstatus = 'Partially Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSE
																												UPDATE ame_ap_dn SET var_pmtstatus = 'Unpaid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										END IF;
																								END IF;
																								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_ACC_AP, P_CLA_DET, "AP-PY", P_APP_TYPE, P_APP_ID, P_APP_REF, "", P_VEN, "", "", P_APP_DESC, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, P_APP_DATE, NULL, -P_APP_AMT, P_DEC_AMT_FOR, P_DOC_TERM, "AP-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "AP", P_CUR, P_RATE, -AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT_FUN));
																								END IF;
																						CLOSE C4;
																				END;
																		ELSE
																				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_ID, P_APP_CLA, "AP-PY", P_APP_TYPE, P_APP_ID, 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, P_DOC_TERM, "AP-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "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_ID, P_APP_CLA, "AP-PY", P_APP_TYPE, P_APP_ID, 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, P_DOC_TERM, "AP-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "GL", P_CUR, P_RATE, -AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT_FUN));
																				END IF;
																		END CASE;
																END LOOP L3;
														CLOSE C3;
												END;
										END;	
								WHEN "Prepayment" THEN
										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, "AP-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));
										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, "AP-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));
										END IF;
										/*
										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_ap_py_app AS T1 LEFT JOIN ame_mt_acc T3 ON T1.var_docid = T3.var_id WHERE T1.var_id = P1 AND var_num = 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 "Purchase Return" THEN
																		SET @Donothing = NULL;
																WHEN "Debit Note" THEN
																		SET @Donothing = NULL;
																ELSE
																		SET P_TAPP = AME_FUN_ROUND(P_TAPP + P_APP_AMT, P_DEC_AMT_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, "AP-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));
																		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, "AP-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));
																		END IF;
																END CASE;
														END LOOP L5;
												CLOSE C5;
										END;
										*/
								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, "AP-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));
										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, "AP-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));
										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, "AP-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, @P_TOTAL_CASH, 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, "AP-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, @P_TOTAL_CASH, 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, "AP-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -@P_TOTAL_CASH, 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, "AP-PY", P_TYPE, P1, P_REF, "", P_VEN, "", "", P_DESC, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -@P_TOTAL_CASH, 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, "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, "", "", "", "", "", "", 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, "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, "", "", "", "", "", "", 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, "AP-PY", "CASH", @P_TOTAL_CASH, @P_TOTAL_CASH * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
				UPDATE ame_ap_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 = "AP-PY" AND T1.var_docid = P1 AND T1.var_status = "Posted"), 0) + COALESCE((SELECT SUM(T1.dou_app) FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = "Prepayment" AND T1.var_docid = P1 AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ap_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_ap_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_ap_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_ap_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_AP_PY_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AP_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_DOC_TYPE VARCHAR(100) DEFAULT "";
		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;

		# 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_venid, T1.dou_dr, T1.dou_cr, T1.int_jid FROM ame_sl_ap_his T1 WHERE T1.var_modid = 'AP-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_AP(P_VEN, "Dr", P_DATE, -P_DR_AMT, P_DEC_AMT_FOR, P_CUR, -P_DR_AMT_FUN, P_DEC_AMT_FUN);
										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);
								END IF;
								DELETE FROM ame_sl_ap_his WHERE var_modid = 'AP-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_AP_PY_1(P1, P2, P_JID, P_NUM);
		ELSE
				UPDATE ame_ap_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_AP_RET_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AP_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 D1 INTEGER DEFAULT 0;
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_BAT_NUM INTEGER DEFAULT 0;
		DECLARE P_BAT_NUM_AC INTEGER DEFAULT 0;
		DECLARE P_LINE INTEGER DEFAULT 0;
		DECLARE P_LINE_JE INTEGER DEFAULT 0;
		DECLARE P_NUM_IC INTEGER DEFAULT 0;
		DECLARE P_DATE DATETIME;
		DECLARE P_DATE_DUE DATETIME;
		DECLARE P_ACC_AP VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_IC VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_EXP VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_DISC VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_VAT VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_WHT 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_PUR VARCHAR(100) DEFAULT "";
		DECLARE P_TYPE_AC VARCHAR(100) DEFAULT "";
		DECLARE P_LOC VARCHAR(100) DEFAULT "";
		DECLARE P_ITEM VARCHAR(100) DEFAULT "";
		DECLARE P_UOM VARCHAR(100) DEFAULT "";
		DECLARE P_TERM VARCHAR(100) DEFAULT "";
		DECLARE P_BAL DOUBLE DEFAULT 0.0;
		DECLARE P_DISC DOUBLE DEFAULT 0.0;
		DECLARE P_DISC_DET DOUBLE DEFAULT 0.0;
		DECLARE P_VAT DOUBLE DEFAULT 0.0;
		DECLARE P_WHT DOUBLE DEFAULT 0.0;
		DECLARE P_QTY DOUBLE DEFAULT 0.0;
		DECLARE P_FOC DOUBLE DEFAULT 0.0;
		DECLARE P_COST DOUBLE DEFAULT 0.0;

		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_RATE DOUBLE DEFAULT 1.0;
		DECLARE P_FUN_DIFF DOUBLE DEFAULT 0.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 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, int_cost, 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;
		IF P7 = -1 THEN SET P_JID = AME_FUN_NEW_JE_NUM(); ELSE SET P_JID = P7; 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
				DECLARE P_BAL DOUBLE DEFAULT 0;
				SELECT T1.dat_date, T1.dat_due, T1.var_term, T1.var_type, T1.var_ref, T1.var_claid, T1.var_venid, T1.var_desc, 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_PUR, P_REF, P_CLA, P_VEN, P_DESC, 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_ap_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;
				# ACC. PAYABLE
				IF P_BAL <> 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("AP", P_VEN, "NA");
						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, "AP-RET", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", IF(COALESCE(P_DESC, "") = "", "Payable", CONCAT("[Payable] ", P_DESC)), "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_BAL, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AP", P_CUR, P_RATE, P_FUN_BAL);
						
						# SUB LEDGER
						CALL AME_PRO_UPDATE_SL_AP(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_AP_HIS("AP-RET", P_TYPE_PUR, P1, P_VEN, P_CLA, "", P_TERM, "", "", P_REF, P_DESC, P_DATE, P_DATE_DUE, P_BAL, P_DEC_AMT_FOR, 0, "Dr", P4, P_JID, P_ACC_AP, P_CUR, P_RATE, P_FUN_BAL);
				END IF;
				# DISC
				IF P_DISC <> 0 THEN
						SET P_ACC_DISC = AME_FUN_ACC("DISC-AP-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_DISC, P_CLA, "AP-RET", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "Discount", "Dr", P_TERM, "", "", "", "", 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;
				# VAT
				IF P_VAT <> 0 THEN
						SET P_ACC_VAT = AME_FUN_ACC("TAX-VAT-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_VAT, P_CLA, "AP-RET", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "VAT", "Cr", P_TERM, "", "", "", "", 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;
				# WHT
				IF P_WHT <> 0 THEN
						SET P_ACC_WHT = AME_FUN_ACC("TAX-WHT", "DEFAULT", "DEFAULT");
						SET P_LINE_JE = P_LINE_JE + 1;
						CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_WHT, P_CLA, "AP-RET", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "WHT", "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, P_WHT, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "WHT", P_CUR, P_RATE, P_FUN_WHT);
				END IF;
		END;
		BEGIN
				DEClARE C1 CURSOR FOR SELECT 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)), T1.var_type, T1.int_batchno, AME_FUN_ROUND(T1.dou_qty + T1.dou_foc, P_DEC_QTY), T1.dou_foc, T1.dou_price, T1.dou_disdol FROM ame_ap_ret_det AS T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				SET @T_FOC = 0;
				OPEN C1;
						L1: LOOP FETCH C1 INTO P_ITEM, P_ACC_EXP, P_LOC, P_CLA_DET, P_UOM, P_DESC_DET, P_TYPE_AC, P_BAT_NUM_AC, P_QTY, P_FOC, P_COST, P_DISC_DET;
								IF D1 = 1 THEN LEAVE L1; END IF;
								SET @P_TC = 0;SET @P_TC_FUN = 0;
								CASE P_TYPE_PUR
								WHEN "Purchase Inventory" THEN
										SET @P_ITEM_TYPE = AME_FUN_ITEM_TYPE(P_ITEM);
										SET @CON = AME_FUN_ITEM_CON(P_ITEM, P_UOM);
										IF @P_ITEM_TYPE = "Inventory Part" OR @P_ITEM_TYPE = "Fixed Asset" THEN
												BEGIN
														DECLARE D3 INTEGER DEFAULT 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 C3 CURSOR FOR SELECT int_batchno, T.dou_qty, 
														AME_FUN_ROUND(dou_druc - dou_cruc, P_DEC_COST_FUN), AME_FUN_ROUND(dou_drac1 - dou_crac1, P_DEC_COST_FUN), AME_FUN_ROUND(dou_drac2 - dou_crac2, P_DEC_COST_FUN), AME_FUN_ROUND(dou_drac3 - dou_crac3, P_DEC_COST_FUN), AME_FUN_ROUND(dou_drac4 - dou_crac4, P_DEC_COST_FUN), AME_FUN_ROUND(dou_drac5 - dou_crac5, P_DEC_COST_FUN)
														FROM ame_sl_ic_batch AS T WHERE T.int_batchno = P_BAT_NUM_AC AND T.var_itemid = P_ITEM AND T.var_locid = P_LOC AND 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_FUN);
																		SET @AC1 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC1 / P_QTY_BAL), P_DEC_COST_FUN);
																		SET @AC2 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC2 / P_QTY_BAL), P_DEC_COST_FUN);
																		SET @AC3 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC3 / P_QTY_BAL), P_DEC_COST_FUN);
																		SET @AC4 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC4 / P_QTY_BAL), P_DEC_COST_FUN);
																		SET @AC5 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC5 / P_QTY_BAL), P_DEC_COST_FUN);
																		SET @P_TC = AME_FUN_ROUND(@P_TC + @UC + @AC1 + @AC2 + @AC3 + @AC4 + @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, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Cr", P_DEC_QTY, P_DEC_COST_FUN);
																		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_FUN);
																		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, "AP-RET", P_TYPE_PUR, P1, AME_FUN_VEN_BATCH(P_BAT_NUM, P_LOC, P_ITEM), P_VEN, "", 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);
																		# ACC. FOC
																		SET @T_FOC = AME_FUN_ROUND(@T_FOC + AME_FUN_ROUND(AME_FUN_ROUND(P_FOC * @CON, P_DEC_QTY) * AME_FUN_ROUND(AME_FUN_ROUND(P_UC / P_QTY_BAL, P_DEC_COST_FUN) + AME_FUN_ROUND(P_AC1 / P_QTY_BAL, P_DEC_COST_FUN) + AME_FUN_ROUND(P_AC2 / P_QTY_BAL, P_DEC_COST_FUN) + AME_FUN_ROUND(P_AC3 / P_QTY_BAL, P_DEC_COST_FUN) + AME_FUN_ROUND(P_AC4 / P_QTY_BAL, P_DEC_COST_FUN) + AME_FUN_ROUND(P_AC5 / P_QTY_BAL, P_DEC_COST_FUN), P_DEC_COST_FUN), P_DEC_COST_FUN), P_DEC_COST_FUN);
																		IF P_QTY_BATCH = 0 THEN LEAVE L3; END IF;
																END LOOP L3;
														CLOSE C3;
												END;
										END IF;
										# ACC. INVENTORY
										IF @P_TC <> 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_DET, "AP-RET", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", P_ITEM, CONCAT("[Inventory] ", P_DESC_DET), "Cr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, -(@P_TC / P_RATE), P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, -AME_FUN_ROUND(@P_TC, P_DEC_AMT_FUN));
										END IF;
								ELSE
										SET @Donothing = NULL;
								END CASE;
						END LOOP L1;
				CLOSE C1;
				# ACC. FOC
				IF @T_FOC <> 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("FOC-AP-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_AP, P_CLA, "AP-RET", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "FOC", "Dr", P_TERM, "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, @T_FOC / P_RATE, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "FOC", P_CUR, P_RATE, AME_FUN_ROUND(@T_FOC, P_DEC_AMT_FUN));
				END IF;
				# ACC. GAIN/LOSS RETURNED
				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_AP = AME_FUN_ACC("GAINLOSS-RET", "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, "AP-RET", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Returned/Rounding Defference", "Dr", "", "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, @P_DIFF_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "RET-GAINLOSS", P_CUR, P_RATE, @P_DIFF);
				ELSEIF @P_DIFF < 0 OR @P_DIFF_FOR < 0 THEN
						SET P_ACC_AP = AME_FUN_ACC("GAINLOSS-RET", "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, "AP-RET", P_TYPE_PUR, P1, P_REF, "", P_VEN, "", "", "Gain/Loss on Returned/Rounding Defference", "Cr", "", "", "", "", "", P_DATE, P_DATE, P_DATE_DUE, @P_DIFF_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "RET-GAINLOSS", P_CUR, P_RATE, @P_DIFF);
				END IF;
				CALL AME_PRO_UPDATE_GL_JE(P_JID, P_TYPE_PUR, P1, P_REF, P_DESC, P2, P_DATE, P_ACC_VAT, "AP-RET", "VAT", P_VAT, P_VAT * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
				UPDATE ame_ap_ret SET var_status = 'Posted', int_jid = P_JID, var_post = P2, dat_post = NOW() WHERE var_id = P1;
		END;
		# PAYMENT
		BEGIN
				DECLARE P_ID VARCHAR(100) DEFAULT "";
				DECLARE C1 CURSOR FOR SELECT T1.var_id FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Purchase Return' AND T1.var_docid = 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_AP_PY_1(P_ID, P2, -1, -1);
								ELSE
										CALL AME_PRO_POST_AP_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 = "AP-RET" AND T1.var_docid = P1 AND T1.var_status = "Posted"), 0) + COALESCE((SELECT SUM(T1.dou_app) FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = "Purchase Return" AND T1.var_docid = P1 AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ap_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_ap_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_ap_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_ap_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;	
# NOTE: P4 = P_NUM_AP, P5 = P_NUM_IC, P6 = P_BAT_NUM, P7 = P_JID
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_AP_RET_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AP_RET_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_AP INTEGER DEFAULT -1;
		DECLARE P_NUM_IC INTEGER DEFAULT -1;
		DECLARE P_BAT_NUM INTEGER DEFAULT -1;
		DECLARE P_DATE DATETIME;
		DECLARE P_VEN 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 AP
		BEGIN
				DECLARE C2 CURSOR FOR SELECT T1.var_cur, T1.dou_fundr, T1.int_line, T1.dat_date, T1.var_venid, AME_FUN_ROUND(T1.dou_dr, P_DEC_AMT_FOR), T1.int_jid FROM ame_sl_ap_his T1 WHERE T1.var_modid = 'AP-RET' AND T1.var_docid = P1;
				OPEN C2;
						IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_CUR, P_BAL_FUN, P_NUM_AP, P_DATE, P_VEN, 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_AP(P_VEN, "Dr", P_DATE, -P_BAL, P_DEC_AMT_FOR, P_CUR, -P_BAL_FUN, P_DEC_AMT_FUN);
								DELETE FROM ame_sl_ap_his WHERE var_modid = 'AP-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_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_QTY 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 C1 CURSOR FOR SELECT int_id, var_itemid, var_locid, var_doctype, int_batchno, 
		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 = 'AP-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_ITEM, P_LOC, P_TYPE, P_BAT_NUM, P_QTY, P_COST, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5;
						IF D1 = 1 THEN LEAVE L1; END IF;
						CASE P_TYPE
						WHEN "Purchase Inventory" THEN
								CALL AME_PRO_UPDATE_SL_IC_BAT(P_LOC, P_ITEM, P_BAT_NUM, P_QTY, P_COST, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", 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_COST, P_AC1, P_AC2, P_AC3, P_AC4, P_AC5, "Cr", P_DEC_QTY, P_DEC_COST_FUN);
						ELSE
								SET @Donothing = NULL;
						END CASE;
				END LOOP L1;
		CLOSE C1;
		DELETE FROM ame_sl_ic_his WHERE var_modid = 'AP-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_AP_RET_1(P1, P2, P3, P_NUM_AP, P_NUM_IC, P_BAT_NUM, P_JID);
		ELSE
				UPDATE ame_ap_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_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = 'Purchase Return' AND T1.var_docid = 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_AP_PY_2(P_ID, P2, 0); END IF;
								END IF;
						CLOSE C1;
				END;
		END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_POST_AR_CN_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AR_CN_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 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_cn T1 WHERE T1.var_id = P1;
				# DECIMAL FOREIGN CURRENCY
				BEGIN DECLARE C1 CURSOR FOR SELECT T1.int_price, 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;
				# 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-CN", 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-CN", 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-CN", 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-CN", 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-CN", 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);
				IF P_TYPE = "General Item Sale" THEN IF P7 = -1 THEN SET P_BAT_NUM_NEW = AME_FUN_NEW_BATCH_NUM(); ELSE SET P_BAT_NUM_NEW = P7; END IF; END IF;
		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_cn_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-CN", 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));
										ELSEIF P_REVENUE < 0 THEN
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA_DET, "AR-CN", 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-CN", "Credit Note 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-CN", 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 = 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-CN", 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-CN", 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-CN", 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-CN", 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-CN", 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-CN", 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-CN", 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_cn_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-CN", 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-CN", 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-CN", 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-CN", "VAT", P_VAT, P_VAT * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
				UPDATE ame_ar_cn 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 = 'Credit Note' AND T1.var_docid = P1 AND COALESCE(T2.tin_paid, 0) = 3 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_doctype = "AR-CN" 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 = "Credit Note" AND T1.var_docid = P1 AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ar_cn 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_cn 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_cn 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_cn 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_CN_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AR_CN_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-CN' 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-CN' 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_TYPE 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_doctype, 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-CN' 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_TYPE, 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-CN' 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_CN_1(P1, P2, P3, P_NUM_AR, P_NUM_IC, P_JID, P_BAT_NUM_NEW);
		ELSE
				UPDATE ame_ar_cn 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 = 'Credit Note' AND T1.var_docid = P1 AND COALESCE(T2.tin_paid, 0) = 3 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_AR_IN_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AR_IN_1`(P1 varchar(100), P2 varchar(100), P3 int(11), P4 int(11), P5 int(11), P6 int(11))
    DETERMINISTIC
BEGIN
		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 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_in 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-IN", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, "", IF(COALESCE(P_DESC, "") = "", "Receivable", CONCAT("[Receivable] ", P_DESC)), "Dr", 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-IN", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, "", "Discount", "Dr", 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;
				# 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-IN", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, "", "VAT", "Cr", 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;
				# 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-IN", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, "", "SCT", "Cr", 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;	
				# SUB LEDGER AR
				CALL AME_PRO_UPDATE_SL_AR(P_CUS, "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-IN", 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, "Dr", 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(dou_qty + dou_foc, P_DEC_QTY), T1.dou_price, T1.dou_foc FROM ame_ar_in_det AS T1 WHERE T1.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-IN", 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));
										ELSE
												CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_NUM, P_CLA_DET, "AR-IN", 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));
										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
										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_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_qty, 
												AME_FUN_ROUND(T.dou_druc - T.dou_cruc, P_DEC_COST_FUN), AME_FUN_ROUND(T.dou_drac1 - T.dou_crac1, P_DEC_COST_FUN), AME_FUN_ROUND(T.dou_drac2 - T.dou_crac2, P_DEC_COST_FUN), AME_FUN_ROUND(T.dou_drac3 - T.dou_crac3, P_DEC_COST_FUN), AME_FUN_ROUND(T.dou_drac4 - T.dou_crac4, P_DEC_COST_FUN), AME_FUN_ROUND(T.dou_drac5 - T.dou_crac5, P_DEC_COST_FUN)
												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_COST, 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_COST / P_QTY_BAL), P_DEC_COST_FUN);
																SET @AC1 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC1 / P_QTY_BAL), P_DEC_COST_FUN);
																SET @AC2 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC2 / P_QTY_BAL), P_DEC_COST_FUN);
																SET @AC3 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC3 / P_QTY_BAL), P_DEC_COST_FUN);
																SET @AC4 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC4 / P_QTY_BAL), P_DEC_COST_FUN);
																SET @AC5 = AME_FUN_ROUND(P_QTY_SOLD * (P_AC5 / 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, P_QTY_SOLD, @UC, @AC1, @AC2, @AC3, @AC4, @AC5, "Cr", P_DEC_QTY, P_DEC_COST_FUN);
																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_FUN);
																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, "AR-IN", 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 @TC_FOR = 0;
												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);
												# COGS
												IF P_TCOST <> 0 THEN
														SET @UC_FOR = AME_FUN_ROUND(P_TCOST / P_RATE, P_DEC_AMT_FOR);
														SET @TC_FOR = AME_FUN_ROUND(@TC_FOR + @UC_FOR, P_DEC_AMT_FOR);
														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-IN", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[COGS] ", P_ITEM), "Dr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, @UC_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "COGS", P_CUR, P_RATE, P_TCOST);
												END IF;
												# COGS AC1
												IF P_TAC1 <> 0 THEN
														SET @UC_FOR = AME_FUN_ROUND(P_TAC1 / P_RATE, P_DEC_AMT_FOR);
														SET @TC_FOR = AME_FUN_ROUND(@TC_FOR + @UC_FOR, P_DEC_AMT_FOR);
														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-IN", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[", AME_FUN_ADDI_COST("AC1"), "] ", P_ITEM), "Dr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, @UC_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AC1", P_CUR, P_RATE, P_TAC1);
												END IF;
												# COGS AC2
												IF P_TAC2 <> 0 THEN
														SET @UC_FOR = AME_FUN_ROUND(P_TAC2 * P_RATE, P_DEC_AMT_FOR);
														SET @TC_FOR = AME_FUN_ROUND(@TC_FOR + @UC_FOR, P_DEC_AMT_FOR);
														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-IN", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[", AME_FUN_ADDI_COST("AC2"), "] ", P_ITEM), "Dr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, @UC_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AC2", P_CUR, P_RATE, P_TAC2);
												END IF;
												# COGS AC3
												IF P_TAC3 <> 0 THEN
														SET @UC_FOR = AME_FUN_ROUND(P_TAC3 * P_RATE, P_DEC_AMT_FOR);
														SET @TC_FOR = AME_FUN_ROUND(@TC_FOR + @UC_FOR, P_DEC_AMT_FOR);
														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-IN", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[", AME_FUN_ADDI_COST("AC3"), "] ", P_ITEM), "Dr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, @UC_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AC3", P_CUR, P_RATE, P_TAC3);
												END IF;
												# COGS AC4
												IF P_TAC4 <> 0 THEN
														SET @UC_FOR = AME_FUN_ROUND(P_TAC4 * P_RATE, P_DEC_AMT_FOR);
														SET @TC_FOR = AME_FUN_ROUND(@TC_FOR + @UC_FOR, P_DEC_AMT_FOR);
														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-IN", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[", AME_FUN_ADDI_COST("AC4"), "] ", P_ITEM), "Dr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, @UC_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AC4", P_CUR, P_RATE, P_TAC4);
												END IF;
												# COGS AC5
												IF P_TAC5 <> 0 THEN
														SET @UC_FOR = AME_FUN_ROUND(P_TAC5 * P_RATE, P_DEC_AMT_FOR);
														SET @TC_FOR = AME_FUN_ROUND(@TC_FOR + @UC_FOR, P_DEC_AMT_FOR);
														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-IN", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[", AME_FUN_ADDI_COST("AC5"), "] ", P_ITEM), "Dr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, @UC_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "AC5", P_CUR, P_RATE, P_TAC5);
												END IF;
												# ASSET
												SET @TC = AME_FUN_ROUND(P_TCOST + P_TAC1 + P_TAC2 + P_TAC3 + P_TAC4 + P_TAC5, P_DEC_AMT_FUN);
												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-IN", P_TYPE, P1, P_REF, P_EMP, "", P_CUS, P_ITEM, CONCAT("[Inventory] ", P_ITEM), "Cr", P_TERM, "", "", P_PRICE_CODE, P_SHIP_ADDR, P_DATE, P_DATE, P_DATE_DUE, -@TC_FOR, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "IC", P_CUR, P_RATE, -@TC);
												END IF;
												UPDATE ame_ar_in_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;
								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-IN", P_TYPE, P1, P_REF, P_EMP, P_CUS, "", "", "FOC", "Dr", 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_FOR = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF_FOR(P_JID), P_DEC_AMT_FOR);
				IF @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-IN", 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_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-IN", 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;
				SET @P_DIFF = -AME_FUN_ROUND(AME_FUN_TOTAL_JE_DIFF(P_JID), P_DEC_AMT_FUN);
				IF @P_DIFF > 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-IN", 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, 0, P_DEC_AMT_FOR, "", "", "", "", "", "", NULL, NULL, "EXC-RATE", P_CUR, P_RATE, @P_DIFF);
				ELSEIF @P_DIFF < 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-IN", 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, 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_AR, "AR-IN", "VAT", P_VAT, P_VAT * P_RATE, P_CUR, P_RATE, P_DEC_AMT_FOR, P_DEC_AMT_FUN);
				UPDATE ame_ar_in 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_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P1 AND COALESCE(T2.tin_paid, 0) = 1 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
								# UPDATE PAID
								CALL AME_PRO_UPDATE_PMT_STATUS_1("AR-IN", P1, P_CUR);
						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_IN_2`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AR_IN_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_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_fundr, T1.int_line, T1.dat_date, T1.var_cusid, T1.dou_dr, T1.int_jid FROM ame_sl_ar_his T1 WHERE T1.var_modid = 'AR-IN' 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, "Dr", 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-IN' 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-IN' 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, "Cr", 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, "Cr", P_DEC_QTY, P_DEC_COST_FUN);
						END LOOP L1;
				CLOSE C1;
				DELETE FROM ame_sl_ic_his WHERE var_modid = 'AR-IN' 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_IN_1(P1, P2, P3, P_NUM_AR, P_NUM_IC, P_JID);
		ELSE
				UPDATE ame_ar_in 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_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P1 AND COALESCE(T2.tin_paid, 0) = 1 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_AR_PY_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_POST_AR_PY_1`(P1 varchar(100), P2 varchar(100), P3 int(11), P4 int(11))
    DETERMINISTIC
BEGIN
		DECLARE D1 INTEGER DEFAULT 0;
		DECLARE P_JID INTEGER DEFAULT 0;
		DECLARE P_LINE_JE INTEGER DEFAULT 1;
		DECLARE P_DATE DATETIME;
		DECLARE P_ACC_AP VARCHAR(100) DEFAULT "";
		DECLARE P_ACC_DISC 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_PMT_METHOD VARCHAR(100) DEFAULT "";
		DECLARE P_PMT_REF VARCHAR(250) DEFAULT "";
		DECLARE P_BAL DOUBLE DEFAULT 0.0;
		DECLARE P_DISC DOUBLE DEFAULT 0.0;
		DECLARE P_AMT 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_APP DOUBLE DEFAULT 0.0;
		DECLARE P_APP_PMT DOUBLE DEFAULT 0.0;
		DECLARE P_APP_PMT_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;
		
		# JOURNAL NUMBER
		IF P3 = -1 THEN SET P_JID = AME_FUN_NEW_JE_NUM(); ELSE SET P_JID = P3; END IF;
		SELECT T1.dat_date, T1.var_type, T1.var_ref, T1.var_claid, T1.var_cusid, T1.var_accid, T1.var_desc, T1.var_pmttype, T1.var_pmtref, T1.dou_net, T1.var_cur, T1.dou_rate, T1.dou_funar
		INTO P_DATE, P_TYPE, P_REF, P_CLA, P_VEN, P_ACC_CASH, P_DESC, P_PMT_METHOD, P_PMT_REF, P_BAL, P_CUR, P_RATE, P_FUN_BAL FROM ame_ar_py 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;
		
		IF P_TYPE = "Prepayment" OR P_TYPE = "Apply to Sale Invoice" THEN
				CALL AME_PRO_UPDATE_SL_AR(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_AR_HIS("AR-PY", 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);
				SELECT AME_FUN_ROUND(COALESCE(SUM(T1.dou_app), 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE(SUM(T1.dou_funapp), 0), P_DEC_AMT_FUN) INTO P_APP_PMT, P_APP_PMT_FUN FROM ame_ar_py_app T1 WHERE T1.var_id = P1 AND T1.var_doctype <> 'Apply to GL Account';
				IF P_APP_PMT <> 0 THEN
						CALL AME_PRO_UPDATE_SL_AR(P_VEN, "Dr", P_DATE, P_APP_PMT, P_DEC_AMT_FOR, P_CUR, P_APP_PMT_FUN, P_DEC_AMT_FUN);
						CALL AME_PRO_UPDATE_SL_AR_HIS("AR-PY", 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, 1, "Dr", P4, P_JID, P_ACC_AP, P_CUR, P_RATE, P_APP_PMT_FUN);
				END IF;
		ELSE
				CALL AME_PRO_UPDATE_SL_AR_HIS("AR-PY", 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);
		END IF;

		BEGIN
				DECLARE P_TCASH DOUBLE DEFAULT 0.0;
				DECLARE P_TAPP DOUBLE DEFAULT 0.0;
				DEClARE C1 CURSOR FOR SELECT T1.var_docid, T1.var_claid, T1.var_desc, AME_FUN_ROUND(COALESCE(T1.dou_disc, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE(T1.dou_amt, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE(T1.dou_app, 0), P_DEC_AMT_FOR) FROM ame_ar_py_det AS T1 WHERE T1.var_id = P1 ORDER BY T1.int_line;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
				SET P_ACC_AP = AME_FUN_ACC("AR", P_VEN, "NA");
				SET P_ACC_DISC = AME_FUN_ACC("DISC-AR-PY", "DEFAULT", "DEFAULT");
				OPEN C1;
						L1: LOOP FETCH C1 INTO P_DOC, P_CLA_DET, P_DESC_DET, P_DISC, P_AMT, P_APP;
								IF D1 = 1 THEN LEAVE L1; END IF;
								SET P_TCASH = AME_FUN_ROUND(P_TCASH + P_AMT, P_DEC_AMT_FOR);
								CASE P_TYPE
								WHEN "Apply to Sale Invoice" THEN
										SET @P_AP = AME_FUN_ROUND(P_DISC + P_AMT + P_APP, P_DEC_AMT_FOR);
										BEGIN
												DECLARE P_DOC_TYPE VARCHAR(100) DEFAULT "";
												DECLARE P_DOC_REF VARCHAR(250) DEFAULT "";
												DECLARE P_DOC_TERM VARCHAR(100) DEFAULT "";
												DECLARE P_DOC_DATE DATETIME DEFAULT NULL;
												DECLARE P_DOC_DUE DATETIME DEFAULT NULL;
												DECLARE P_DOC_NET DOUBLE DEFAULT 0.0;
												DECLARE P_DOC_PAID DOUBLE DEFAULT 0.0;
												DECLARE P_DOC_RATE DOUBLE DEFAULT 0.0;
												DECLARE C2 CURSOR FOR SELECT T1.var_type, T1.var_ref, T1.var_term, T1.dat_date, T1.dat_due, AME_FUN_ROUND(COALESCE(T1.dou_net, 0), P_DEC_AMT_FOR), AME_FUN_ROUND(COALESCE((SELECT SUM(T1.dou_amt + T1.dou_app + T1.dou_disc) FROM ame_ar_py_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_id <> P1 AND T1.var_docid = P_DOC AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR), T1.dou_rate FROM ame_ar_in T1 WHERE T1.var_id = P_DOC;
												OPEN C2;
														IF FOUND_ROWS() > 0 THEN
																FETCH C2 INTO P_DOC_TYPE, P_DOC_REF, P_DOC_TERM, P_DOC_DATE, P_DOC_DUE, P_DOC_NET, P_DOC_PAID, P_DOC_RATE;
																SET @PAID = AME_FUN_ROUND(@P_AP + P_DOC_PAID, P_DEC_AMT_FOR);
																IF P_DOC_NET = @PAID THEN
																		UPDATE ame_ar_in SET var_pmtstatus = "Fully Paid", dou_paid = @PAID WHERE var_id = P_DOC;
																ELSEIF P_DOC_NET > @PAID AND @PAID > 0 THEN
																		UPDATE ame_ar_in SET var_pmtstatus = "Partially Paid", dou_paid = @PAID WHERE var_id = P_DOC;
																ELSE
																		UPDATE ame_ar_in SET var_pmtstatus = "Unpaid", dou_paid = @PAID WHERE var_id = P_DOC;
																END IF;
														END IF;
												CLOSE C2;
												IF @P_AP <> 0 THEN
														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, "", "", P_DESC_DET, "Cr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, NULL, NULL, -@P_AP, P_DEC_AMT_FOR, P_DOC_TERM, "AR-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "AR", P_CUR, P_RATE, -AME_FUN_ROUND(@P_AP * P_DOC_RATE, P_DEC_AMT_FUN));
												END IF;
												IF P_DISC <> 0 THEN
														SET P_LINE_JE = P_LINE_JE + 1;
														CALL AME_PRO_UPDATE_GL_JE_DET(P_JID, P_LINE_JE, P_ACC_DISC, 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, P_DISC, P_DEC_AMT_FOR, P_DOC_TERM, "AR-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "DISC", P_CUR, P_RATE, AME_FUN_ROUND(P_DISC * P_RATE, P_DEC_AMT_FUN));
												END IF;
												BEGIN
														DECLARE D3 INTEGER DEFAULT 0;
														DECLARE P_APP_TYPE VARCHAR(100) DEFAULT "";
														DECLARE P_APP_ID VARCHAR(100) DEFAULT "";
														DECLARE P_APP_CLA VARCHAR(100) DEFAULT "";
														DECLARE P_APP_DESC VARCHAR(2000) DEFAULT "";
														DECLARE P_APP_AMT DOUBLE DEFAULT 0.0;
														DEClARE C3 CURSOR FOR SELECT T1.var_doctype, T1.var_docid, T1.var_claid, IF(COALESCE(T1.var_desc, '') = "", T1.var_doctype, CONCAT(T1.var_doctype, ": ",T1.var_desc)), AME_FUN_ROUND(T1.dou_app, P_DEC_AMT_FOR) FROM ame_ar_py_app AS T1 WHERE var_id = P1 AND var_num = P_DOC ORDER BY T1.int_line;
														DECLARE CONTINUE HANDLER FOR NOT FOUND SET D3 = 1;
														OPEN C3;
																L3: LOOP FETCH C3 INTO P_APP_TYPE, P_APP_ID, P_APP_CLA, P_APP_DESC, P_APP_AMT;
																		IF D3 = 1 THEN LEAVE L3; END IF;
																		CASE P_APP_TYPE
																		WHEN "Prepayment" THEN
																				BEGIN
																						DECLARE P_APP_REF VARCHAR(250) DEFAULT "";
																						DECLARE P_APP_ACC VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_DATE DATETIME DEFAULT NULL;
																						DECLARE P_APP_NET DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_PAID DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_RATE DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_AMT_TOTAL DOUBLE DEFAULT 0.0;
																						DECLARE C4 CURSOR FOR SELECT T1.dou_rate, T2.var_docid, T1.var_ref, T1.dat_date, 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_status = "Posted" AND T1.var_doctype = "AR-PY" AND T1.var_docid = P_APP_ID), 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_id <> P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ar_py T1 LEFT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id WHERE T1.var_id = P_APP_ID LIMIT 1;
																						OPEN C4;
																								IF FOUND_ROWS() > 0 THEN FETCH C4 INTO P_APP_RATE, P_APP_ACC, P_APP_REF, P_APP_DATE, P_APP_NET, P_APP_PAID;
																										SELECT AME_FUN_ROUND(SUM(T1.dou_app), P_DEC_AMT_FOR) INTO P_APP_AMT_TOTAL FROM ame_ar_py_app AS T1 WHERE T1.var_id = P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID;	
																										SET @PAID = AME_FUN_ROUND(P_APP_AMT_TOTAL + P_APP_PAID, P_DEC_AMT_FOR);
																										IF P_APP_NET = @PAID THEN
																												UPDATE ame_ar_py SET var_pmtstatus = 'Fully Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSEIF P_APP_NET > @PAID AND @PAID > 0 THEN
																												UPDATE ame_ar_py SET var_pmtstatus = 'Partially Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSE
																												UPDATE ame_ar_py SET var_pmtstatus = 'Unpaid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										END IF;
																								END IF;
																								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_CLA_DET, "AR-PY", P_APP_TYPE, P_APP_ID, P_APP_REF, "", P_VEN, "", "", P_APP_DESC, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, P_APP_DATE, NULL, P_APP_AMT, P_DEC_AMT_FOR, P_DOC_TERM, "AR-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "PREPAYMENT", P_CUR, P_RATE, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT_FUN));
																								END IF;
																						CLOSE C4;
																				END;
																		WHEN "Sales Return" THEN
																				BEGIN
																						DECLARE P_APP_REF VARCHAR(250) DEFAULT "";
																						DECLARE P_APP_ACC VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_DATE DATETIME DEFAULT NULL;
																						DECLARE P_APP_NET DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_PAID DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_RATE DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_AMT_TOTAL DOUBLE DEFAULT 0.0;
																						DECLARE C4 CURSOR FOR SELECT T1.dou_rate, T1.var_id, T1.var_ref, T1.dat_date, 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_status = "Posted" AND T1.var_doctype = "AR-RET" AND T1.var_docid = P_APP_ID), 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_id <> P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ar_ret T1 WHERE T1.var_id = P_APP_ID LIMIT 1;
																						OPEN C4;
																								IF FOUND_ROWS() > 0 THEN FETCH C4 INTO P_APP_RATE, P_APP_ACC, P_APP_REF, P_APP_DATE, P_APP_NET, P_APP_PAID;
																										SELECT AME_FUN_ROUND(SUM(T1.dou_app), P_DEC_AMT_FOR) INTO P_APP_AMT_TOTAL FROM ame_ar_py_app AS T1 WHERE T1.var_id = P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID;	
																										SET @PAID = AME_FUN_ROUND(P_APP_AMT_TOTAL + P_APP_PAID, P_DEC_AMT_FOR);
																										IF P_APP_NET = @PAID THEN
																												UPDATE ame_ar_ret SET var_pmtstatus = 'Fully Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSEIF P_APP_NET > @PAID AND @PAID > 0 THEN
																												UPDATE ame_ar_ret SET var_pmtstatus = 'Partially Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSE
																												UPDATE ame_ar_ret SET var_pmtstatus = 'Unpaid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										END IF;
																								END IF;
																								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_ACC_AP, P_CLA_DET, "AR-PY", P_APP_TYPE, P_APP_ID, P_APP_REF, "", P_VEN, "", "", P_APP_DESC, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, P_APP_DATE, NULL, P_APP_AMT, P_DEC_AMT_FOR, P_DOC_TERM, "AR-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "AR", P_CUR, P_RATE, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT_FUN));
																								END IF;
																						CLOSE C4;
																				END;
																		WHEN "Credit Note" THEN
																				BEGIN
																						DECLARE P_APP_REF VARCHAR(250) DEFAULT "";
																						DECLARE P_APP_ACC VARCHAR(100) DEFAULT "";
																						DECLARE P_APP_DATE DATETIME DEFAULT NULL;
																						DECLARE P_APP_NET DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_PAID DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_RATE DOUBLE DEFAULT 0.0;
																						DECLARE P_APP_AMT_TOTAL DOUBLE DEFAULT 0.0;
																						DECLARE C4 CURSOR FOR SELECT T1.dou_rate, T1.var_id, T1.var_ref, T1.dat_date, 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_status = "Posted" AND T1.var_doctype = "AR-CN" AND T1.var_docid = P_APP_ID), 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_id <> P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID AND T2.var_status = "Posted"), 0), P_DEC_AMT_FOR) FROM ame_ar_cn T1 WHERE T1.var_id = P_APP_ID LIMIT 1;
																						OPEN C4;
																								IF FOUND_ROWS() > 0 THEN FETCH C4 INTO P_APP_RATE, P_APP_ACC, P_APP_REF, P_APP_DATE, P_APP_NET, P_APP_PAID;
																										SELECT AME_FUN_ROUND(SUM(T1.dou_app), P_DEC_AMT_FOR) INTO P_APP_AMT_TOTAL FROM ame_ar_py_app AS T1 WHERE T1.var_id = P1 AND T1.var_doctype = P_APP_TYPE AND T1.var_docid = P_APP_ID;	
																										SET @PAID = AME_FUN_ROUND(P_APP_AMT_TOTAL + P_APP_PAID, P_DEC_AMT_FOR);
																										IF P_APP_NET = @PAID THEN
																												UPDATE ame_ar_cn SET var_pmtstatus = 'Fully Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSEIF P_APP_NET > @PAID AND @PAID > 0 THEN
																												UPDATE ame_ar_cn SET var_pmtstatus = 'Partially Paid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										ELSE
																												UPDATE ame_ar_cn SET var_pmtstatus = 'Unpaid', dou_paid = @PAID, dou_funpaid = AME_FUN_ROUND(@PAID * P_APP_RATE, P_DEC_AMT_FUN) WHERE var_id = P_APP_ID;
																										END IF;
																								END IF;
																								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_ACC_AP, P_CLA_DET, "AR-PY", P_APP_TYPE, P_APP_ID, P_APP_REF, "", P_VEN, "", "", P_APP_DESC, "Dr", "", P_PMT_METHOD, P_PMT_REF, "", "", P_DATE, P_APP_DATE, NULL, P_APP_AMT, P_DEC_AMT_FOR, P_DOC_TERM, "AR-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "AR", P_CUR, P_RATE, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT_FUN));
																								END IF;
																						CLOSE C4;
																				END;
																		ELSE
																				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_ID, P_APP_CLA, "AR-PY", P_APP_TYPE, P_APP_ID, 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, P_DOC_TERM, "AR-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "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_ID, P_APP_CLA, "AR-PY", P_APP_TYPE, P_APP_ID, 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, P_DOC_TERM, "AR-IN", P_DOC_TYPE, P_DOC, P_DOC_REF, P_DESC_DET, P_DOC_DATE, P_DOC_DUE, "GL", P_CUR, P_RATE, AME_FUN_ROUND(P_APP_AMT * P_RATE, P_DEC_AMT_FUN));
																				END IF;
																		END CASE;
																END LOOP L3;
														CLOSE C3;
												END;
										END;	
								WHEN "Prepayment" THEN
										/*
