			UPDATE vb_itemdetail SET vit_truckingNoInChina = itemCode, vit_someqty = availQty WHERE vit_itemId = itemId;

			UPDATE ame_mt_ite SET var_itemcode = itemCode WHERE var_id = itemId;
			SET @result = "The item code and related data updated successfully!";
		END IF;
	CLOSE C1;
	RETURN @result;
END |
DELIMITER ;

-- 
-- Dumping procedures
-- 

DROP PROCEDURE IF EXISTS `ameAddAuthorizationWatcherIn`;
DELIMITER |
CREATE PROCEDURE `ameAddAuthorizationWatcherIn`(IN AuthID varchar(255),IN ProcessID varchar(255),IN ProcessName varchar(255))
    DETERMINISTIC
BEGIN
DECLARE doneWatcher INT DEFAULT FALSE;
DECLARE AuthEmpWatcherID VARCHAR(255);
DECLARE cur2 CURSOR FOR SELECT DISTINCT(Emp_ID) FROM hrm_authorisation_watcher WHERE Auth_ID = AuthID AND Auth_Process = ProcessName;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET doneWatcher = TRUE;
OPEN cur2;
read_loop1: LOOP
FETCH cur2 INTO AuthEmpWatcherID;
IF doneWatcher THEN
LEAVE read_loop1;
END IF;
IF !ISNULL(AuthEmpWatcherID) THEN
INSERT INTO hrm_authorisation_approved(Auth_ProcessID,Auth_ProcessName,Auth_ID,Auth_EmpID,Auth_Status,Auth_Date) VALUES (ProcessID, ProcessName, AuthID, AuthEmpWatcherID, 'watcher', NOW());
END IF;
END LOOP;
CLOSE cur2;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameAddAuthorizatoinWatcherGro`;
DELIMITER |
CREATE PROCEDURE `ameAddAuthorizatoinWatcherGro`(IN AuthID varchar(255),IN ProcessID varchar(255),IN ProcessName varchar(255))
    DETERMINISTIC
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE AuthEmpID VARCHAR(255);
DECLARE AuthGroupID VARCHAR(255);
DECLARE SNow DATE;
DECLARE tz VARCHAR(20) DEFAULT('');
DECLARE cur1 CURSOR FOR SELECT a.Auth_GroupID,AuthGroup_EmpID FROM hrm_authorisation_detail a LEFT JOIN hrm_authorisation_group_detail g ON(g.AuthGroup_ID = a.Auth_GroupID) WHERE a.Auth_ID = AuthID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
SELECT Setting_Value INTO tz FROM hrm_setting WHERE Setting_Type = 'TimeZone' AND Setting_Name = 'TimeZone';
SET SNow = convert_tz(now(), @@session.time_zone, tz);
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO AuthGroupID,AuthEmpID;
IF done THEN
LEAVE read_loop;
END IF;
IF !ISNULL(AuthEmpID) THEN
INSERT INTO hrm_authorisation_approved(Auth_ProcessID,Auth_ProcessName,Auth_ID,Auth_GroupID,Auth_EmpID,Auth_Status,Auth_Date) VALUES (ProcessID, ProcessName, AuthID, AuthGroupID, AuthEmpID, 'requested', SNow);
END IF;
END LOOP;
CLOSE cur1;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationGroupTemp_Budget`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationGroupTemp_Budget`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT FrmID FROM crm_frm_budget WHERE LOWER(ApprovalStatus) NOT IN("approved", "rejected") AND BRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT FrmID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN crm_frm_budget ON(LOWER(ApprovalStatus) NOT IN("approved", "rejected") AND BRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT FrmID FROM crm_frm_budget WHERE LOWER(ApprovalStatus) NOT IN("approved", "rejected") AND BRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT FrmID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN crm_frm_budget ON(LOWER(ApprovalStatus) NOT IN("approved", "rejected") AND BRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Budget Requisition');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Budget Requisition');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT FrmID FROM crm_frm_budget WHERE LOWER(ApprovalStatus) NOT IN("approved", "rejected") AND BRAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationGroupTemp_Customer`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationGroupTemp_Customer`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT CustID FROM crm_customer WHERE LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT CustID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN crm_customer ON(LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT CustID FROM crm_customer WHERE LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT CustID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN crm_customer ON(LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Customer');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Customer');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT CustID FROM crm_customer WHERE LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationGroupTemp_Expense`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationGroupTemp_Expense`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT EntryNo FROM tblexpenserequest WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND ERAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT EntryNo,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblexpenserequest ON(LOWER(`Status`) NOT IN("approved", "rejected") AND ERAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM tblexpenserequest WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND ERAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT EntryNo,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblexpenserequest ON(LOWER(`Status`) NOT IN("approved", "rejected") AND ERAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Expense Request');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Expense Request');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT EntryNo FROM tblexpenserequest WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND ERAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationGroupTemp_General`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationGroupTemp_General`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT FrmID FROM crm_frm_general WHERE LOWER(ApprovalStatus) NOT IN("approved", "rejected") AND GRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT FrmID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN crm_frm_general ON(LOWER(ApprovalStatus) NOT IN("approved", "rejected") AND GRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT FrmID FROM crm_frm_general WHERE LOWER(ApprovalStatus) NOT IN("approved", "rejected") AND GRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT FrmID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN crm_frm_general ON(LOWER(ApprovalStatus) NOT IN("approved", "rejected") AND GRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'General Requisition');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'General Requisition');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT FrmID FROM crm_frm_general WHERE LOWER(ApprovalStatus) NOT IN("approved", "rejected") AND GRAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationGroupTemp_LogisticExpense`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationGroupTemp_LogisticExpense`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT EntryNo FROM logistic_expense_request WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND LEAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT EntryNo,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN logistic_expense_request ON(LOWER(`Status`) NOT IN("approved", "rejected") AND LEAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM logistic_expense_request WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND LEAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT EntryNo,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN logistic_expense_request ON(LOWER(`Status`) NOT IN("approved", "rejected") AND LEAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Logistic Expense');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Logistic Expense');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT EntryNo FROM logistic_expense_request WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND LEAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationGroupTemp_LogisticMaterial`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationGroupTemp_LogisticMaterial`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT EntryNo FROM logistic_material WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND LMAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT EntryNo,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN logistic_material ON(LOWER(`Status`) NOT IN("approved", "rejected") AND LMAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM logistic_material WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND LMAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT EntryNo,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN logistic_material ON(LOWER(`Status`) NOT IN("approved", "rejected") AND LMAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Logistic Material');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Logistic Material');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT EntryNo FROM logistic_material WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND LMAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationGroupTemp_Material`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationGroupTemp_Material`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT EntryNo FROM tblmaterialrequest WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND MRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT EntryNo,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblmaterialrequest ON(LOWER(`Status`) NOT IN("approved", "rejected") AND MRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM tblmaterialrequest WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND MRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT EntryNo,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblmaterialrequest ON(LOWER(`Status`) NOT IN("approved", "rejected") AND MRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Material Request');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Material Request');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT EntryNo FROM tblmaterialrequest WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND MRAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationGroupTemp_OverDueDate`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationGroupTemp_OverDueDate`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT IssueID FROM tblcustomeroverdue WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND ODAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT IssueID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblcustomeroverdue ON(LOWER(`Status`) NOT IN("approved", "rejected") AND ODAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT IssueID FROM tblcustomeroverdue WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND ODAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT IssueID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblcustomeroverdue ON(LOWER(`Status`) NOT IN("approved", "rejected") AND ODAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Over Due Date');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Over Due Date');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT IssueID FROM tblcustomeroverdue WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND ODAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationGroupTemp_Quote`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationGroupTemp_Quote`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT SalID FROM tblsbquote WHERE LOWER(ApproveStatus) NOT IN("approved", "rejected") AND QAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT SalID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblsbquote ON(LOWER(ApproveStatus) NOT IN("approved", "rejected") AND QAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT SalID FROM tblsbquote WHERE LOWER(ApproveStatus) NOT IN("approved", "rejected") AND QAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT SalID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblsbquote ON(LOWER(ApproveStatus) NOT IN("approved", "rejected") AND QAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Quote');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Quote');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT SalID FROM tblsbquote WHERE LOWER(ApproveStatus) NOT IN("approved", "rejected") AND QAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationGroupTemp_SaleOrder`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationGroupTemp_SaleOrder`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT SalID FROM tblsaleorder WHERE LOWER(ApproveStatus) NOT IN("approved", "rejected") AND SAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT SalID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblsaleorder ON(LOWER(ApproveStatus) NOT IN("approved", "rejected") AND SAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT SalID FROM tblsaleorder WHERE LOWER(ApproveStatus) NOT IN("approved", "rejected") AND SAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT SalID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblsaleorder ON(LOWER(ApproveStatus) NOT IN("approved", "rejected") AND SAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Sale Order');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Sale Order');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT SalID FROM tblsaleorder WHERE LOWER(ApproveStatus) NOT IN("approved", "rejected") AND SAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationGroupTemp_TemporaryCredit`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationGroupTemp_TemporaryCredit`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT IssueID FROM tblcustomerissue WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND TCAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT IssueID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblcustomerissue ON(LOWER(`Status`) NOT IN("approved", "rejected") AND TCAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT IssueID FROM tblcustomerissue WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND TCAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT IssueID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblcustomerissue ON(LOWER(`Status`) NOT IN("approved", "rejected") AND TCAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Temporary Credit');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Temporary Credit');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT IssueID FROM tblcustomerissue WHERE LOWER(`Status`) NOT IN("approved", "rejected") AND TCAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationTemp_Budget`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationTemp_Budget`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_temp WHERE Auth_TranID  IN(SELECT FrmID FROM crm_frm_budget WHERE LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND BRAuthID = AuthID);
INSERT INTO hrm_authorisation_temp(Auth_TranID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount) (SELECT FrmID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount FROM hrm_authorisation INNER JOIN crm_frm_budget ON(LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND BRAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_detail_temp WHERE Auth_TranID IN(SELECT FrmID FROM crm_frm_budget WHERE LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND BRAuthID = AuthID);
INSERT INTO hrm_authorisation_detail_temp(Auth_TranID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount) (SELECT FrmID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount FROM hrm_authorisation_detail INNER JOIN crm_frm_budget ON(LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND BRAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT FrmID FROM crm_frm_budget WHERE LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND BRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT FrmID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN crm_frm_budget ON(LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND BRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT FrmID FROM crm_frm_budget WHERE LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND BRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT FrmID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN crm_frm_budget ON(LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND BRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Budget Requisition');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Budget Requisition');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT FrmID FROM crm_frm_budget WHERE LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND BRAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationTemp_Customer`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationTemp_Customer`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_temp 	WHERE Auth_TranID  IN(SELECT CustID FROM crm_customer WHERE LOWER(CrmApproval) 	NOT IN(1, 2) AND CAuthID = AuthID);
INSERT INTO hrm_authorisation_temp(Auth_TranID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount)
(SELECT CustID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount FROM hrm_authorisation INNER JOIN crm_customer ON(LOWER(CrmApproval)
NOT IN(1,2) AND CAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_detail_temp WHERE Auth_TranID IN(SELECT CustID FROM crm_customer WHERE LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID);
INSERT INTO hrm_authorisation_detail_temp(Auth_TranID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount)
(SELECT CustID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount FROM hrm_authorisation_detail INNER JOIN crm_customer ON(LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT CustID FROM crm_customer WHERE LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT CustID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN crm_customer ON(LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT CustID FROM crm_customer WHERE LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT CustID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN crm_customer ON(LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Customer');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Customer');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT CustID FROM crm_customer WHERE LOWER(CrmApproval) NOT IN(1, 2) AND CAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationTemp_Expense`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationTemp_Expense`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_temp WHERE Auth_TranID  IN(SELECT EntryNo FROM tblexpenserequest WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND ERAuthID = AuthID);
INSERT INTO hrm_authorisation_temp(Auth_TranID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount) (SELECT EntryNo,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount FROM hrm_authorisation INNER JOIN tblexpenserequest ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND ERAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM tblexpenserequest WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND ERAuthID = AuthID);
INSERT INTO hrm_authorisation_detail_temp(Auth_TranID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount) (SELECT EntryNo,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount FROM hrm_authorisation_detail INNER JOIN tblexpenserequest ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND ERAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT EntryNo FROM tblexpenserequest WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND ERAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT EntryNo,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblexpenserequest ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND ERAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM tblexpenserequest WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND ERAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT EntryNo,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblexpenserequest ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND ERAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Expense Request');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Expense Request');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT EntryNo FROM tblexpenserequest WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND ERAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationTemp_General`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationTemp_General`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_temp WHERE Auth_TranID  IN(SELECT FrmID FROM crm_frm_general WHERE LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND GRAuthID = AuthID);
INSERT INTO hrm_authorisation_temp(Auth_TranID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount) (SELECT FrmID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount FROM hrm_authorisation INNER JOIN crm_frm_general ON(LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND GRAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_detail_temp WHERE Auth_TranID IN(SELECT FrmID FROM crm_frm_general WHERE LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND GRAuthID = AuthID);
INSERT INTO hrm_authorisation_detail_temp(Auth_TranID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount) (SELECT FrmID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount FROM hrm_authorisation_detail INNER JOIN crm_frm_general ON(LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND GRAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT FrmID FROM crm_frm_general WHERE LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND GRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT FrmID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN crm_frm_general ON(LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND GRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT FrmID FROM crm_frm_general WHERE LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND GRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT FrmID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN crm_frm_general ON(LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND GRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'General Requisition');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'General Requisition');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT FrmID FROM crm_frm_general WHERE LOWER(ApprovalStatus) NOT IN('approved', 'rejected') AND GRAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationTemp_LogisticExpense`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationTemp_LogisticExpense`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_temp WHERE Auth_TranID  IN(SELECT EntryNo FROM logistic_expense_request WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND LEAuthID = AuthID);
INSERT INTO hrm_authorisation_temp(Auth_TranID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount) (SELECT EntryNo,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount FROM hrm_authorisation INNER JOIN logistic_expense_request ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND LEAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM logistic_expense_request WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND LEAuthID = AuthID);
INSERT INTO hrm_authorisation_detail_temp(Auth_TranID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount) (SELECT EntryNo,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount FROM hrm_authorisation_detail INNER JOIN logistic_expense_request ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND LEAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT EntryNo FROM logistic_expense_request WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND LEAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT EntryNo,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN logistic_expense_request ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND LEAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM logistic_expense_request WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND LEAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT EntryNo,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN logistic_expense_request ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND LEAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Logistic Expense');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Logistic Expense');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT EntryNo FROM logistic_expense_request WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND LEAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationTemp_LogisticMaterial`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationTemp_LogisticMaterial`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_temp WHERE Auth_TranID  IN(SELECT EntryNo FROM logistic_material WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND LMAuthID = AuthID);
INSERT INTO hrm_authorisation_temp(Auth_TranID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount) (SELECT EntryNo,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount FROM hrm_authorisation INNER JOIN logistic_material ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND LMAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM logistic_material WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND LMAuthID = AuthID);
INSERT INTO hrm_authorisation_detail_temp(Auth_TranID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount) (SELECT EntryNo,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount FROM hrm_authorisation_detail INNER JOIN logistic_material ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND LMAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT EntryNo FROM logistic_material WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND LMAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT EntryNo,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN logistic_material ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND LMAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM logistic_material WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND LMAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT EntryNo,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN logistic_material ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND LMAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Logistic Material');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Logistic Material');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT EntryNo FROM logistic_material WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND LMAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationTemp_Material`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationTemp_Material`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_temp WHERE Auth_TranID  IN(SELECT EntryNo FROM tblmaterialrequest WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND MRAuthID = AuthID);
INSERT INTO hrm_authorisation_temp(Auth_TranID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount) (SELECT EntryNo,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount FROM hrm_authorisation INNER JOIN tblmaterialrequest ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND MRAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM tblmaterialrequest WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND MRAuthID = AuthID);
INSERT INTO hrm_authorisation_detail_temp(Auth_TranID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount) (SELECT EntryNo,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount FROM hrm_authorisation_detail INNER JOIN tblmaterialrequest ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND MRAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT EntryNo FROM tblmaterialrequest WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND MRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT EntryNo,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblmaterialrequest ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND MRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT EntryNo FROM tblmaterialrequest WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND MRAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT EntryNo,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblmaterialrequest ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND MRAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Material Request');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Material Request');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT EntryNo FROM tblmaterialrequest WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND MRAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationTemp_OverDueDate`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationTemp_OverDueDate`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_temp WHERE Auth_TranID  IN(SELECT IssueID FROM tblcustomeroverdue WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND ODAuthID = AuthID);
INSERT INTO hrm_authorisation_temp(Auth_TranID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount) (SELECT IssueID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount FROM hrm_authorisation INNER JOIN tblcustomeroverdue ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND ODAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_detail_temp WHERE Auth_TranID IN(SELECT IssueID FROM tblcustomeroverdue WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND ODAuthID = AuthID);
INSERT INTO hrm_authorisation_detail_temp(Auth_TranID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount) (SELECT IssueID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount FROM hrm_authorisation_detail INNER JOIN tblcustomeroverdue ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND ODAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT IssueID FROM tblcustomeroverdue WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND ODAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT IssueID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblcustomeroverdue ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND ODAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT IssueID FROM tblcustomeroverdue WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND ODAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT IssueID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblcustomeroverdue ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND ODAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Over Due Date');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Over Due Date');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT IssueID FROM tblcustomeroverdue WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND ODAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationTemp_Quote`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationTemp_Quote`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_temp WHERE Auth_TranID  IN(SELECT SalID FROM tblsbquote WHERE LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND QAuthID = AuthID);
INSERT INTO hrm_authorisation_temp(Auth_TranID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount) (SELECT SalID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount FROM hrm_authorisation INNER JOIN tblsbquote ON(LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND QAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_detail_temp WHERE Auth_TranID IN(SELECT SalID FROM tblsbquote WHERE LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND QAuthID = AuthID);
INSERT INTO hrm_authorisation_detail_temp(Auth_TranID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount) (SELECT SalID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount FROM hrm_authorisation_detail INNER JOIN tblsbquote ON(LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND QAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT SalID FROM tblsbquote WHERE LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND QAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT SalID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblsbquote ON(LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND QAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT SalID FROM tblsbquote WHERE LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND QAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT SalID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblsbquote ON(LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND QAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Quote');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Quote');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT SalID FROM tblsbquote WHERE LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND QAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationTemp_SaleOrder`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationTemp_SaleOrder`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_temp WHERE Auth_TranID  IN(SELECT SalID FROM tblsaleorder WHERE LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND SAuthID = AuthID);
INSERT INTO hrm_authorisation_temp(Auth_TranID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount) (SELECT SalID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount FROM hrm_authorisation INNER JOIN tblsaleorder ON(LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND SAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_detail_temp WHERE Auth_TranID IN(SELECT SalID FROM tblsaleorder WHERE LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND SAuthID = AuthID);
INSERT INTO hrm_authorisation_detail_temp(Auth_TranID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount) (SELECT dayoff_id,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount FROM hrm_authorisation_detail INNER JOIN hrm_timesheet_dayoff ON(LOWER(dayoff_status) NOT IN('approved', 'rejected') AND dayoff_authid = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT SalID FROM tblsaleorder WHERE LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND SAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT SalID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblsaleorder ON(LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND SAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT SalID FROM tblsaleorder WHERE LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND SAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT SalID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblsaleorder ON(LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND SAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Sale Order');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Sale Order');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT SalID FROM tblsaleorder WHERE LOWER(ApproveStatus) NOT IN('approved', 'rejected') AND SAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameCheckAuthorisationTemp_TemporaryCredit`;
DELIMITER |
CREATE PROCEDURE `ameCheckAuthorisationTemp_TemporaryCredit`(IN SUserID varchar(255), IN AuthID varchar(255), IN AuthType varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM hrm_authorisation_temp WHERE Auth_TranID  IN(SELECT IssueID FROM tblcustomerissue WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND TCAuthID = AuthID);
INSERT INTO hrm_authorisation_temp(Auth_TranID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount) (SELECT IssueID,Auth_ID,Auth_Name,Auth_Type,Auth_AndOr,Auth_Amount FROM hrm_authorisation INNER JOIN tblcustomerissue ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND TCAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_detail_temp WHERE Auth_TranID IN(SELECT IssueID FROM tblcustomerissue WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND TCAuthID = AuthID);
INSERT INTO hrm_authorisation_detail_temp(Auth_TranID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount) (SELECT IssueID,Auth_ID,Auth_EmpID,Auth_GroupID,Auth_GroupAndOr,Auth_GroupAmount FROM hrm_authorisation_detail INNER JOIN tblcustomerissue ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND TCAuthID = AuthID) WHERE Auth_ID = AuthID);
DELETE FROM hrm_authorisation_group_temp WHERE Auth_TranID IN(SELECT IssueID FROM tblcustomerissue WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND TCAuthID = AuthID);
INSERT INTO hrm_authorisation_group_temp(Auth_TranID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description) (SELECT IssueID,AuthGroup_ID,AuthGroup_Name,AuthGroup_Description FROM hrm_authorisation_group INNER JOIN tblcustomerissue ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND TCAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
DELETE FROM hrm_authorisation_group_detail_temp WHERE Auth_TranID IN(SELECT IssueID FROM tblcustomerissue WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND TCAuthID = AuthID);
INSERT INTO hrm_authorisation_group_detail_temp(Auth_TranID,AuthGroup_ID,AuthGroup_EmpID) (SELECT IssueID,AuthGroup_ID,AuthGroup_EmpID FROM hrm_authorisation_group_detail INNER JOIN tblcustomerissue ON(LOWER(`Status`) NOT IN('approved', 'rejected') AND TCAuthID = AuthID) WHERE AuthGroup_ID IN(SELECT Auth_GroupID FROM hrm_authorisation_detail WHERE Auth_ID = AuthID));
IF LOWER(AuthType) = 'individual' THEN
CALL spHRMAddIAuthorisationApprovedTemp(SUserID, AuthID, 'Temporary Credit');
ELSE
CALL spHRMAddGAuthorisationApprovedTemp(SUserID, AuthID, 'Temporary Credit');
END IF;
DELETE FROM hrm_authorisation_approved WHERE Auth_ProcessID IN(SELECT IssueID FROM tblcustomerissue WHERE LOWER(`Status`) NOT IN('approved', 'rejected') AND TCAuthID = AuthID);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameDeleteCollaboration`;
DELIMITER |
CREATE PROCEDURE `ameDeleteCollaboration`(IN `id` varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM int_collaboration WHERE CB_ID = `id`;
DELETE FROM int_collaboration_details WHERE PostID = `id`;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameDeleteCollaborationDetail`;
DELIMITER |
CREATE PROCEDURE `ameDeleteCollaborationDetail`(IN `id` varchar(255))
    DETERMINISTIC
BEGIN
DELETE FROM int_collaboration_details WHERE CommentID = `id`;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetAuthorisationApproveEmpID`;
DELIMITER |
CREATE PROCEDURE `ameGetAuthorisationApproveEmpID`(IN UserID varchar(255), IN SStatus varchar(255), IN SProcessName varchar(255), IN fDate varchar(255), IN tDate varchar(255))
    DETERMINISTIC
BEGIN
DECLARE SID VARCHAR(255) DEFAULT('');
SELECT EmpID INTO SID FROM tbluseremployee WHERE UID = UserID;
SELECT DISTINCT(al.Auth_ProcessID), al.Auth_ProcessName, al.Auth_ID, al.Auth_GroupID, al.Auth_EmpID,
al.Auth_Status, fnHRMCheckAuthorisationGroupApproved(al.Auth_ProcessID,al.Auth_ProcessName, al.Auth_ID, al.Auth_GroupID) as "Auth_Edit", al.Auth_Date, al.EmpID, COALESCE(al.EmpName, '') AS 'EmpName',
COALESCE(al.EmpEmail, '') AS 'EmpEmail', COALESCE(al.Emp_CompanyEmail, '') AS 'Emp_CompanyEmail',
COALESCE(al.Position, '') AS 'Position', COALESCE(al.PositionKhmer, '') AS 'PositionKhmer', COALESCE(al.Position_Level,"") AS 'Position_Level', COALESCE(al.UName, '') AS 'UName',
COALESCE(al.Auth_ProcessEmpID, '') AS 'Auth_ProcessEmpID', CONCAT('',COALESCE(e.EmpName, '')) AS 'Auth_ProcessEmpName',
fnHRMCheckAuthorisationApproved(al.Auth_ProcessID, al.Auth_ProcessName, al.Auth_ID) AS 'Auth_AmountApproved',
fnHRMCheckAuthorisationApprove(al.Auth_ProcessID, al.Auth_ProcessName, al.Auth_ID) AS 'Auth_AmountApprove',
auth.Auth_ID,auth.Auth_Name,auth.Auth_Type,auth.Auth_AndOr,
COALESCE((SELECT IF(LOWER(ad.Auth_GroupAndOr) = 'and', ad.Auth_GroupAmount, 1)
FROM hrm_authorisation_detail_temp ad
WHERE ad.Auth_TranID = al.Auth_ProcessID AND ad.Auth_ID = al.Auth_ID AND ad.Auth_GroupID = al.Auth_GroupID LIMIT 0,1),"") AS "Auth_Amount",
Auth_TranStatus,
ameAuthDescApproval(al.Auth_ProcessName,al.Auth_ProcessID) AS "Auth_Desc",
ameAuthGetCustomerName(al.Auth_ProcessName,al.Auth_ProcessID) AS "Auth_Customer"
FROM(
SELECT Auth_ProcessID,Auth_ProcessName,Auth_ID,Auth_GroupID,Auth_EmpID,Auth_Status, 1 AS 'Auth_Edit', Auth_Date,
IF(Auth_ProcessName='Customer', uemp.EmpID,
IF(Auth_ProcessName='Sale Order', sa.EmpID,
IF(Auth_ProcessName='Quote', qu.EmpID,
IF(Auth_ProcessName="Budget Requisition", br.EmpID,
IF(Auth_ProcessName="General Requisition", gr.EmpID,
IF(Auth_ProcessName='Logistic Material Request', lm.RequestedBy,
IF(Auth_ProcessName='Temporary Credit', ciemp.EmpID,
IF(Auth_ProcessName='Over Due Date', odd.EmpID,
IF(Auth_ProcessName='Expense Request', er.RequestedBy,
IF(Auth_ProcessName='Material Request', mr.RequestedBy,
IF(Auth_ProcessName='Logistic Expense Request', le.RequestedBy,''))))))))))) AS 'Auth_ProcessEmpID',
LOWER(IF(Auth_ProcessName='Customer', IF(cust.CrmApproval = 0, "requested", IF(cust.CrmApproval = 1, "approved", "rejected")),
IF(Auth_ProcessName='Sale Order', sa.ApproveStatus,
IF(Auth_ProcessName='Quote', qu.ApproveStatus,
IF(Auth_ProcessName="Budget Requisition", br.ApprovalStatus,
IF(Auth_ProcessName="General Requisition", gr.ApprovalStatus,
IF(Auth_ProcessName='Logistic Material Request', lm.`Status`,
IF(Auth_ProcessName='Temporary Credit', ci.`Status`,
IF(Auth_ProcessName='Over Due Date', co.`Status`,
IF(Auth_ProcessName='Expense Request', er.`Status`,
IF(Auth_ProcessName='Material Request', mr.`Status`,
IF(Auth_ProcessName='Logistic Expense Request', le.`Status`,'')))))))))))) AS 'Auth_TranStatus'
,e.EmpID,e.EmpName,e.EmpEmail,e.Emp_CompanyEmail,e.EmpPosition, COALESCE(e.EmpPositionKh, '') AS 'Position', COALESCE(e.Emp_PositionLevel, '') AS 'PositionKhmer', COALESCE(e.Emp_PositionLevel) AS 'Position_Level',u.UName
FROM hrm_authorisation_approved ap
LEFT JOIN tblmaterialrequest mr ON ap.Auth_ProcessID = mr.EntryNo
LEFT JOIN tblexpenserequest er ON ap.Auth_ProcessID = er.EntryNo
LEFT JOIN crm_customer cust ON ap.Auth_ProcessID = cust.CustID
LEFT JOIN logistic_material lm ON lm.EntryNo = ap.Auth_ProcessID
LEFT JOIN tblcustomerissue ci ON ci.IssueID = ap.Auth_ProcessID
LEFT JOIN logistic_expense_request le ON le.EntryNo = ap.Auth_ProcessID
LEFT JOIN tbluseremployee uemp ON uemp.UID = cust.CBy
LEFT JOIN tblsaleorder sa ON ap.Auth_ProcessID = sa.SalID
LEFT JOIN tblsbquote qu ON ap.Auth_ProcessID = qu.SalID
LEFT JOIN crm_frm_general gr ON ap.Auth_ProcessID = gr.FrmID
LEFT JOIN crm_frm_budget br ON ap.Auth_ProcessID = br.FrmID
LEFT JOIN tblcustomeroverdue co ON ap.Auth_ProcessID = co.IssueID
LEFT JOIN tbluseremployee odd ON (odd.UID = co.CreateBy)
LEFT JOIN tbluseremployee ciemp ON (ciemp.UID = ci.CreateBy)
INNER JOIN tblemployee e ON ap.Auth_EmpID = e.EmpID
LEFT JOIN tbluseremployee ue ON(ue.EmpID = e.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
/*AND ap.Auth_Status = "requested"
AND (cust.CrmApproval = 0 OR LOWER(lm.`Status`) = "requested" OR LOWER(le.`Status`) = "requested" OR LOWER(ci.`Status`) = "requested"
OR LOWER(mr.`Status`) = "requested" OR LOWER(er.`Status`) = "requested" OR LOWER(sa.ApproveStatus) = "requested"  OR LOWER(qu.ApproveStatus) = "requested" OR LOWER(co.`Status`) = "requested")*/
WHERE ap.Auth_EmpID = SID
AND IF(fDate <> "" && tDate <> "", ap.Auth_Date BETWEEN fDate AND tDate, true)
) al
INNER JOIN hrm_authorisation_module m ON(LOWER(m.AM_ID) = LOWER(al.Auth_ProcessName) AND LOWER(m.AM_APP) = 'ame')
INNER JOIN tblemployee e ON e.EmpID = al.Auth_ProcessEmpID
LEFT JOIN tbluseremployee ue ON (ue.EmpID = e.EmpID)
LEFT JOIN tbluser u ON (u.UID = ue.UID)
LEFT JOIN hrm_authorisation auth ON (auth.Auth_ID = al.Auth_ID)
WHERE if(LOWER(SStatus) = "all" && LOWER(SProcessName) = "all",
TRUE,
if(LOWER(SStatus) = "all" && LOWER(SProcessName) <> "all",
LOWER(Auth_ProcessName) = LOWER(SProcessName),
if(LOWER(SStatus) <> "all" && LOWER(SProcessName) <> "all",
LOWER(Auth_TranStatus) = LOWER(SStatus) AND LOWER(Auth_ProcessName) = LOWER(SProcessName),
if(LOWER(SStatus) <> "all" && LOWER(SProcessName) = "all",
LOWER(Auth_TranStatus) = LOWER(SStatus),
TRUE))))
ORDER BY IF(SProcessName = 'all', Auth_ProcessName, TRUE), FIELD(LOWER(Auth_TranStatus), 'requested', 'approved', 'rejected', ''),al.Auth_Date DESC;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetAuthorisationProcess`;
DELIMITER |
CREATE PROCEDURE `ameGetAuthorisationProcess`()
    DETERMINISTIC
BEGIN
SELECT AM_ID FROM hrm_authorisation_module WHERE LOWER(AM_APP) = 'ame';
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetAuthorizationRequested`;
DELIMITER |
CREATE PROCEDURE `ameGetAuthorizationRequested`(IN UserID varchar(255), IN SStatus varchar(255), IN SProcessName varchar(255), IN fDate varchar(255), IN tDate varchar(255))
    DETERMINISTIC
BEGIN
DECLARE SID VARCHAR(255) DEFAULT('');
SELECT EmpID INTO SID FROM tbluseremployee WHERE UID = UserID;
SELECT al.Auth_ProcessID, al.Auth_ProcessName, al.Auth_ID, al.Auth_GroupID, al.Auth_EmpID,
al.Auth_Status, al.Auth_Edit, al.Auth_Date, al.EmpID, COALESCE(al.EmpName, '') AS 'EmpName',
COALESCE(al.EmpEmail, '') AS 'EmpEmail', COALESCE(al.Emp_CompanyEmail, '') AS 'Emp_CompanyEmail',
COALESCE(e.EmpPosition, '') AS 'Position', COALESCE(e.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(e.Emp_PositionLevel) AS 'Position_Level', COALESCE(al.UName, '') AS 'UName',
COALESCE(al.Auth_ProcessEmpID, '') AS 'Auth_ProcessEmpID', CONCAT('',COALESCE(e.EmpName, '')) AS 'Auth_ProcessEmpName',
fnHRMCheckAuthorisationApproved(al.Auth_ProcessID, al.Auth_ProcessName, al.Auth_ID) AS 'Auth_AmountApproved',
fnHRMCheckAuthorisationApprove(al.Auth_ProcessID, al.Auth_ProcessName, al.Auth_ID) AS 'Auth_AmountApprove',
auth.Auth_ID,auth.Auth_Name,auth.Auth_Type,auth.Auth_AndOr,auth.Auth_Amount
FROM(
SELECT Auth_ProcessID,Auth_ProcessName,Auth_ID,Auth_GroupID,Auth_EmpID,Auth_Status, 1 AS 'Auth_Edit', Auth_Date,
IF(Auth_ProcessName='Customer', uemp.EmpID,
IF(Auth_ProcessName='Sale Order', sa.EmpID,
IF(Auth_ProcessName='Quote', qu.EmpID,
IF(Auth_ProcessName='Logistic Material Request', lm.RequestedBy,
IF(Auth_ProcessName='Temporary Credit', ciemp.EmpID,
IF(Auth_ProcessName='Over Due Date', odd.EmpID,
IF(Auth_ProcessName='Expense Request', er.RequestedBy,
IF(Auth_ProcessName='Material Request', mr.RequestedBy,
IF(Auth_ProcessName='Logistic Expense Request', le.RequestedBy,''))))))))) AS 'Auth_ProcessEmpID'
,e.EmpID,e.EmpName,e.EmpEmail,e.Emp_CompanyEmail,COALESCE(e.EmpPosition, '') AS 'Position', COALESCE(e.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(e.Emp_PositionLevel) AS 'Position_Level',u.UName
FROM hrm_authorisation_approved ap
LEFT JOIN tblmaterialrequest mr ON ap.Auth_ProcessID = mr.EntryNo
LEFT JOIN tblexpenserequest er ON ap.Auth_ProcessID = er.EntryNo
LEFT JOIN crm_customer cust ON ap.Auth_ProcessID = cust.CustID
LEFT JOIN logistic_material lm ON lm.EntryNo = ap.Auth_ProcessID
LEFT JOIN tblcustomerissue ci ON ci.IssueID = ap.Auth_ProcessID
LEFT JOIN logistic_expense_request le ON le.EntryNo = ap.Auth_ProcessID
LEFT JOIN tbluseremployee uemp ON uemp.UID = cust.CBy
LEFT JOIN tblsaleorder sa ON ap.Auth_ProcessID = sa.SalID
LEFT JOIN tblsbquote qu ON ap.Auth_ProcessID = qu.SalID
LEFT JOIN tblcustomeroverdue co ON ap.Auth_ProcessID = co.IssueID
LEFT JOIN tbluseremployee odd ON (odd.UID = co.CreateBy)
LEFT JOIN tbluseremployee ciemp ON (ciemp.UID = ci.CreateBy)
INNER JOIN tblemployee e ON ap.Auth_EmpID = e.EmpID
LEFT JOIN tbluseremployee ue ON(ue.EmpID = e.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE ap.Auth_Status = "requested"
AND (cust.CrmApproval = 0 OR LOWER(lm.`Status`) = "requested" OR LOWER(le.`Status`) = "requested" OR LOWER(ci.`Status`) = "requested"
OR LOWER(mr.`Status`) = "requested" OR LOWER(er.`Status`) = "requested" OR LOWER(sa.ApproveStatus) = "requested"  OR LOWER(qu.ApproveStatus) = "requested" OR LOWER(co.`Status`) = "requested")
AND IF(fDate <> "" && tDate <> "", ap.Auth_Date BETWEEN fDate AND tDate, true)
UNION
SELECT Auth_ProcessID,Auth_ProcessName,Auth_ID,Auth_GroupID,Auth_EmpID,Auth_Status,
(IF(ap.Auth_ProcessName='Customer' AND cust.CrmApproval='0',1,
IF(Auth_ProcessName='Temporary Credit' AND LOWER(ci.`Status`) = "requested",1,
IF(Auth_ProcessName='Over Due Date' AND LOWER(co.`Status`) = "requested",1,
IF(Auth_ProcessName="Material Request" AND LOWER(mr.`Status`) = "requested",1,
IF(Auth_ProcessName="Expense Request" AND LOWER(er.`Status`) = "requested",1,
IF(Auth_ProcessName="Sale Order" AND LOWER(sa.ApproveStatus) = "requested",1,
IF(Auth_ProcessName="Quote" AND LOWER(qu.ApproveStatus) = "requested",1,
IF(Auth_ProcessName='Logistic Material Request' AND LOWER(lm.`Status`) = "requested",1,
IF(Auth_ProcessName='Logistic Expense Request' AND LOWER(le.`Status`) = "requested",1,0)))))))))) AS 'Auth_Edit', Auth_Date,
IF(Auth_ProcessName='Customer', uemp.EmpID,
IF(Auth_ProcessName='Sale Order', sa.EmpID,
IF(Auth_ProcessName='Quote', qu.EmpID,
IF(Auth_ProcessName='Over Due Date', odd.EmpID,
IF(Auth_ProcessName='Temporary Credit', ciemp.EmpID,
IF(Auth_ProcessName='Logistic Material Request', lm.RequestedBy,
IF(Auth_ProcessName='Expense Request', er.RequestedBy,
IF(Auth_ProcessName='Material Request', mr.RequestedBy,
IF(Auth_ProcessName='Logistic Expense Request', le.RequestedBy, ''))))))))) AS 'Auth_ProcessEmpID',
e.EmpID,e.EmpName,e.EmpEmail,e.Emp_CompanyEmail,COALESCE(e.EmpPosition, '') AS 'Position', COALESCE(e.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(e.Emp_PositionLevel) AS 'Position_Level',u.UName
FROM hrm_authorisation_approved ap
LEFT JOIN tblmaterialrequest mr ON ap.Auth_ProcessID = mr.EntryNo
LEFT JOIN tblexpenserequest er ON ap.Auth_ProcessID = er.EntryNo
LEFT JOIN crm_customer cust ON ap.Auth_ProcessID = cust.CustID
LEFT JOIN logistic_expense_request le ON (le.EntryNo = ap.Auth_ProcessID)
LEFT JOIN tblcustomerissue ci ON(ci.IssueID = ap.Auth_ProcessID)
LEFT JOIN tbluseremployee uemp ON (uemp.UID = cust.CBy)
LEFT JOIN logistic_material lm ON (lm.EntryNo = ap.Auth_ProcessID)
LEFT JOIN tblsaleorder sa ON ap.Auth_ProcessID = sa.SalID
INNER JOIN tblemployee e ON ap.Auth_EmpID = e.EmpID
LEFT JOIN tblcustomeroverdue co ON ap.Auth_ProcessID = co.IssueID
LEFT JOIN tbluseremployee odd ON (odd.UID = co.CreateBy)
LEFT JOIN tblsbquote qu ON ap.Auth_ProcessID = qu.SalID
LEFT JOIN tbluseremployee ciemp ON (ciemp.UID = ci.CreateBy)
LEFT JOIN tbluseremployee ue ON(ue.EmpID = e.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE ap.Auth_Status <> "requested" 	AND IF(fDate <> "" && tDate <> "", ap.Auth_Date BETWEEN fDate AND tDate, true)
) al
INNER JOIN hrm_authorisation_module m ON(LOWER(m.AM_ID) = LOWER(al.Auth_ProcessName) AND LOWER(m.AM_APP) = 'ame')
INNER JOIN tblemployee e ON e.EmpID = al.Auth_ProcessEmpID
LEFT JOIN tbluseremployee ue ON(ue.EmpID = e.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
LEFT JOIN hrm_authorisation auth ON (auth.Auth_ID = al.Auth_ID)
WHERE if(LOWER(SStatus) = "all" && LOWER(SProcessName) = "all",
TRUE,
if(LOWER(SStatus) = "all" && LOWER(SProcessName) <> "all",
LOWER(al.Auth_ProcessName) = LOWER(SProcessName),
if(LOWER(SStatus) <> "all" && LOWER(SProcessName) <> "all",
LOWER(al.Auth_Status) = LOWER(SStatus) AND LOWER(al.Auth_ProcessName) = LOWER(SProcessName),
if(LOWER(SStatus) <> "all" && LOWER(SProcessName) = "all",
LOWER(al.Auth_Status) = LOWER(SStatus),
TRUE))))
AND al.Auth_ProcessEmpID = SID
ORDER BY Auth_ProcessName,
al.Auth_Edit DESC, FIELD(al.Auth_Status, 'requested', 'approved', 'rejected', '');
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetBudgetID`;
DELIMITER |
CREATE PROCEDURE `ameGetBudgetID`(userid varchar(255),ID varchar(255))
    DETERMINISTIC
BEGIN
DECLARE CheckU VARCHAR(255) DEFAULT("");
DECLARE SYear INT DEFAULT(0);
DECLARE SEmpID VARCHAR(255) DEFAULT("");
DECLARE SAuthID VARCHAR(255) DEFAULT("");
SELECT UType INTO CheckU FROM tbluser WHERE UID = userid;
SELECT Auth_ID INTO SAuthID FROM hrm_authorisation_approved WHERE Auth_ProcessID = ID LIMIT 1;
IF CheckU = "ESS" THEN
SELECT FrmID,FrmDate,EmpID,(SELECT ee.EmpName FROM tblemployee ee WHERE ee.EmpID = fg.EmpID) as "EmpName",Department,Position,Phone,COALESCE(Objective,"") as "Objective",COALESCE(Reason,"") as "Reason",COALESCE(ApprovalDate,"") as "ApprovalDate",ApprovalStatus FROM crm_frm_budget fg WHERE FrmID = ID ;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Budget Requisition', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Budget Requisition" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Budget Requisition" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
ELSE
SELECT FrmID,FrmDate,EmpID,(SELECT ee.EmpName FROM tblemployee ee WHERE ee.EmpID = fg.EmpID) as "EmpName",Department,Position,Phone,COALESCE(Objective,"") as "Objective",COALESCE(Reason,"") as "Reason",COALESCE(ApprovalDate,"") as "ApprovalDate",ApprovalStatus FROM crm_frm_budget fg WHERE FrmID = ID ;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Budget Requisition', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Budget Requisition" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Budget Requisition" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetCollaboration`;
DELIMITER |
CREATE PROCEDURE `ameGetCollaboration`(IN `rto` varchar(255), IN `rid` varchar(255))
    DETERMINISTIC
BEGIN
SELECT CB_Des, CB_UID, CB_CreateDate, CB_RToID, CB_RToType, CB_Own, CB_ID
FROM int_collaboration
WHERE CB_RToType = `rto` AND 	CB_RToID = `rid` ORDER BY CB_ID DESC;
SELECT CB_ID;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetCreditLimit`;
DELIMITER |
CREATE PROCEDURE `ameGetCreditLimit`(IN `id` varchar(255), IN `inv` varchar(255))
    DETERMINISTIC
BEGIN
	CALL crm_customer_creditLimit_1(id,inv);
  CALL crm_customer_creditLimit_2(id,inv);
	CALL crm_customer_creditLimit_3(id,inv);
	CALL crm_customer_creditLimit_4(id,inv);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetCustomerID`;
DELIMITER |
CREATE PROCEDURE `ameGetCustomerID`(userid varchar(255),ID varchar(255))
    DETERMINISTIC
BEGIN
DECLARE CheckU VARCHAR(255) DEFAULT("");
DECLARE SYear INT DEFAULT(0);
DECLARE SEmpID VARCHAR(255) DEFAULT("");
DECLARE SAuthID VARCHAR(255) DEFAULT("");
SELECT UType INTO CheckU FROM tbluser WHERE UID = userid;
SELECT Auth_ID INTO SAuthID FROM hrm_authorisation_approved WHERE Auth_ProcessID = ID LIMIT 1;
IF CheckU = "ESS" THEN
SELECT CustID,CustName,CustContactName,CustTel1,CustTel2,CustFax,CustEmail,CustWebsite,CustAddress,CustGroupID,PriceCode,Feedback,LocationID,Facebook,Line,Viber,WhatApp,AID,CrmLeadSource,CrmApproval,CrmImageName,CreateFrom,ImagePath,(SELECT ci.i_name FROM crm_industry ci WHERE ci.i_id = c.CrmIndustID) "CrmIndustID",(SELECT ca.at_name FROM crm_account_type ca WHERE ca.at_id = c.CrmAccountTypeID) "CrmAccountTypeID",LGT_Logo,CustContactName,CustNameKhmer,
COALESCE((SELECT CONCAT(var_nameen," (",var_namekh,")",IF(tin_province = 1," | ក្រុង","")) FROM crm_gazetteer WHERE var_id = c.CO_City),"") "City",
COALESCE((SELECT CONCAT(var_nameen," (",var_namekh,")",IF(tin_province = 1," | ក្រុង","")) FROM crm_gazetteer WHERE var_id = c.CO_District),"") "District",
COALESCE((SELECT CONCAT(var_nameen," (",var_namekh,")") FROM crm_gazetteer WHERE var_id = c.CO_Commune),"") "Commune",
COALESCE((SELECT CONCAT(var_nameen," (",var_namekh,")") FROM crm_gazetteer WHERE var_id = c.CO_Village),"") "Village" FROM crm_customer c WHERE CustID = ID ;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Customer', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Customer" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Customer" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
ELSE
SELECT CustID,CustName,CustContactName,CustTel1,CustTel2,CustFax,CustEmail,CustWebsite,CustAddress,CustGroupID,PriceCode,Feedback,LocationID,Facebook,Line,Viber,WhatApp,AID,CrmLeadSource,CrmApproval,CrmImageName,CreateFrom,ImagePath,(SELECT ci.i_name FROM crm_industry ci WHERE ci.i_id = c.CrmIndustID) "CrmIndustID",(SELECT ca.at_name FROM crm_account_type ca WHERE ca.at_id = c.CrmAccountTypeID) "CrmAccountTypeID",LGT_Logo,CustContactName,CustNameKhmer,
COALESCE((SELECT CONCAT(var_nameen," (",var_namekh,")",IF(tin_province = 1," | ក្រុង","")) FROM crm_gazetteer WHERE var_id = c.CO_City),"") "City",
COALESCE((SELECT CONCAT(var_nameen," (",var_namekh,")",IF(tin_province = 1," | ក្រុង","")) FROM crm_gazetteer WHERE var_id = c.CO_District),"") "District",
COALESCE((SELECT CONCAT(var_nameen," (",var_namekh,")") FROM crm_gazetteer WHERE var_id = c.CO_Commune),"") "Commune",
COALESCE((SELECT CONCAT(var_nameen," (",var_namekh,")") FROM crm_gazetteer WHERE var_id = c.CO_Village),"") "Village" FROM crm_customer c WHERE CustID = ID ;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Customer', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Customer" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Customer" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetCustomerIssueID`;
DELIMITER |
CREATE PROCEDURE `ameGetCustomerIssueID`(userid varchar(255),ID varchar(255))
    DETERMINISTIC
BEGIN
DECLARE CheckU VARCHAR(255) DEFAULT("");
DECLARE SYear INT DEFAULT(0);
DECLARE SEmpID VARCHAR(255) DEFAULT("");
DECLARE SAuthID VARCHAR(255) DEFAULT("");
SELECT UType INTO CheckU FROM tbluser WHERE UID = userid;
SELECT Auth_ID INTO SAuthID FROM hrm_authorisation_approved WHERE Auth_ProcessID = ID LIMIT 1;
IF CheckU = "ESS" THEN
SELECT i.IssueID,i.CustID "CustID", (SELECT c.CustName FROM tblcustomer c WHERE c.CustID = i.CustID) "CustName",COALESCE(i.InstallmentDate,"") "InstallmentDate",i.StartDate,i.EndDate,i.IssueAmt,i.Remark,i.Status,i.CreateBy,COALESCE(i.Relate,"") "Relate",COALESCE(i.RelateID,"") "RelateID" FROM tblcustomerissue i WHERE i.IssueID  = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Temporary Credit', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Temporary Credit" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Temporary Credit" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
ELSE
SELECT i.IssueID,i.CustID "CustID", (SELECT c.CustName FROM tblcustomer c WHERE c.CustID = i.CustID) "CustName",COALESCE(i.InstallmentDate,"") "InstallmentDate",i.StartDate,i.EndDate,i.IssueAmt,i.Remark,i.Status,i.CreateBy,COALESCE(i.Relate,"") "Relate",COALESCE(i.RelateID,"") "RelateID" FROM tblcustomerissue i WHERE i.IssueID  = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Temporary Credit', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Temporary Credit" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Temporary Credit" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetExpenseRequestID`;
DELIMITER |
CREATE PROCEDURE `ameGetExpenseRequestID`(userid varchar(255),ID varchar(255))
    DETERMINISTIC
BEGIN
DECLARE CheckU VARCHAR(255) DEFAULT("");
DECLARE SYear INT DEFAULT(0);
DECLARE SEmpID VARCHAR(255) DEFAULT("");
DECLARE SAuthID VARCHAR(255) DEFAULT("");
SELECT UType INTO CheckU FROM tbluser WHERE UID = userid;
SELECT Auth_ID INTO SAuthID FROM hrm_authorisation_approved WHERE Auth_ProcessID = ID LIMIT 1;
IF CheckU = "ESS" THEN
SELECT EntryNo,Reference,EntryDate,RequiredDate,`Status`,RequestedBy,ClassID,Remark,TotalDis,TotalSTax,TotalVTax,NetTotalAmt FROM tblexpenserequest WHERE EntryNo = ID;
SELECT LineNo,ItemID, (SELECT ii.ItemName FROM tblitem ii WHERE ii.ItemID = ed.ItemID) "ItemName", LocationID, UOMID, Qty, UnitCost, TotalAmt, Factor, ClassID , DisDol, DisPer, VTaxDol, NetTotalAmt , des, CONCAT("[",VendorID,"] ",(SELECT vv.VendName FROM tblvendor vv WHERE ed.VendorID = vv.VendID)) "VendorID" FROM tblexpenserequestdetail ed WHERE EntryNo = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Expense Request', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Expense Request" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Expense Request" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
ELSE
SELECT EntryNo,Reference,EntryDate,RequiredDate,`Status`,RequestedBy,ClassID,Remark,TotalDis,TotalSTax,TotalVTax,NetTotalAmt FROM tblexpenserequest WHERE EntryNo = ID ;
SELECT LineNo,ItemID, (SELECT ii.ItemName FROM tblitem ii WHERE ii.ItemID = ed.ItemID) "ItemName", LocationID, UOMID, Qty, UnitCost, TotalAmt, Factor, ClassID , DisDol, DisPer, VTaxDol, NetTotalAmt,  des, CONCAT("[",VendorID,"] ",(SELECT vv.VendName FROM tblvendor vv WHERE ed.VendorID = vv.VendID)) "VendorID" FROM tblexpenserequestdetail ed WHERE EntryNo = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Expense Request', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Expense Request" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Expense Request" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetGeneralID`;
DELIMITER |
CREATE PROCEDURE `ameGetGeneralID`(userid varchar(255),ID varchar(255))
    DETERMINISTIC
BEGIN
DECLARE CheckU VARCHAR(255) DEFAULT("");
DECLARE SYear INT DEFAULT(0);
DECLARE SEmpID VARCHAR(255) DEFAULT("");
DECLARE SAuthID VARCHAR(255) DEFAULT("");
SELECT UType INTO CheckU FROM tbluser WHERE UID = userid;
SELECT Auth_ID INTO SAuthID FROM hrm_authorisation_approved WHERE Auth_ProcessID = ID LIMIT 1;
IF CheckU = "ESS" THEN
SELECT FrmID,FrmDate,EmpID,(SELECT ee.EmpName FROM tblemployee ee WHERE ee.EmpID = fg.EmpID) as "EmpName",Department,Position,Phone,COALESCE(Objective,"") as "Objective",COALESCE(Reason,"") as "Reason",COALESCE(ApprovalDate,"") as "ApprovalDate",ApprovalStatus,COALESCE(ReqType,"") as "ReqType",COALESCE(ReqDescription,"") as "ReqDescription" FROM crm_frm_general fg WHERE FrmID = ID ;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'General Requisition', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "General Requisition" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "General Requisition" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
ELSE
SELECT FrmID,FrmDate,EmpID,(SELECT ee.EmpName FROM tblemployee ee WHERE ee.EmpID = fg.EmpID) as "EmpName",Department,Position,Phone,COALESCE(Objective,"") as "Objective",COALESCE(Reason,"") as "Reason",COALESCE(ApprovalDate,"") as "ApprovalDate",ApprovalStatus,COALESCE(ReqType,"") as "ReqType",COALESCE(ReqDescription,"") as "ReqDescription" FROM crm_frm_general fg WHERE FrmID = ID ;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'General Requisition', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "General Requisition" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "General Requisition" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetMaterialRequestID`;
DELIMITER |
CREATE PROCEDURE `ameGetMaterialRequestID`(userid varchar(255),ID varchar(255))
    DETERMINISTIC
BEGIN
DECLARE CheckU VARCHAR(255) DEFAULT("");
DECLARE SYear INT DEFAULT(0);
DECLARE SEmpID VARCHAR(255) DEFAULT("");
DECLARE SAuthID VARCHAR(255) DEFAULT("");
SELECT UType INTO CheckU FROM tbluser WHERE UID = userid;
SELECT Auth_ID INTO SAuthID FROM hrm_authorisation_approved WHERE Auth_ProcessID = ID LIMIT 1;
IF CheckU = "ESS" THEN
SELECT EntryNo,Reference,EntryDate,RequiredDate,`Status`,COALESCE((SELECT CONCAT("[",ee.EmpID,"] ",ee.EmpName) FROM tblemployee ee WHERE ee.EmpID = mat.RequestedBy),"") "RequestedBy",Remark,COALESCE((SELECT CONCAT("[",c.CustID,"] ",c.CustName) FROM tblcustomer c WHERE c.CustID = mat.CustID),"") "CustID" FROM tblmaterialrequest mat WHERE EntryNo = ID ;
SELECT dd.LineNo,dd.ItemID, (SELECT ii.ItemName FROM tblitem ii WHERE ii.ItemID = dd.ItemID) "ItemName" , dd.FromLocationID, dd.ToLocationID,dd.UOMID,dd.Qty FROM tblmaterialrequestdetail dd WHERE dd.EntryNo = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Material Request', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Material Request" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Material Request" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
ELSE
SELECT EntryNo,Reference,EntryDate,RequiredDate,`Status`,COALESCE((SELECT CONCAT("[",ee.EmpID,"] ",ee.EmpName) FROM tblemployee ee WHERE ee.EmpID = mat.RequestedBy),"") "RequestedBy",Remark,COALESCE((SELECT CONCAT("[",c.CustID,"] ",c.CustName) FROM tblcustomer c WHERE c.CustID = mat.CustID),"") "CustID" FROM tblmaterialrequest mat WHERE EntryNo = ID ;
SELECT dd.LineNo,dd.ItemID, (SELECT ii.ItemName FROM tblitem ii WHERE ii.ItemID = dd.ItemID) "ItemName" , dd.FromLocationID, dd.ToLocationID,dd.UOMID,dd.Qty FROM tblmaterialrequestdetail dd WHERE dd.EntryNo = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Material Request', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Material Request" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Material Request" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetOverDueDateID`;
DELIMITER |
CREATE PROCEDURE `ameGetOverDueDateID`(userid varchar(255),ID varchar(255))
    DETERMINISTIC
BEGIN
DECLARE CheckU VARCHAR(255) DEFAULT("");
DECLARE SYear INT DEFAULT(0);
DECLARE SEmpID VARCHAR(255) DEFAULT("");
DECLARE SAuthID VARCHAR(255) DEFAULT("");
SELECT UType INTO CheckU FROM tbluser WHERE UID = userid;
SELECT Auth_ID INTO SAuthID FROM hrm_authorisation_approved WHERE Auth_ProcessID = ID LIMIT 1;
IF CheckU = "ESS" THEN
SELECT IssueID, i.CustID 'CustID', COALESCE((SELECT c.CustName FROM tblcustomer c WHERE c.CustID = i.CustID),'') "CustName",InstallmentDate,StartDate,EndDate,Remark,Status,CreateBy,COALESCE(i.Relate,"") "Relate",COALESCE(i.RelateID,"") "RelateID" FROM tblcustomeroverdue i WHERE IssueID = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Over Due Date', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Over Due Date" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Over Due Date" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
ELSE
SELECT IssueID,i.CustID 'CustID', COALESCE((SELECT c.CustName FROM tblcustomer c WHERE c.CustID = i.CustID),'') "CustName",InstallmentDate,StartDate,EndDate,Remark,Status,CreateBy,COALESCE(i.Relate,"") "Relate",COALESCE(i.RelateID,"") "RelateID" FROM tblcustomeroverdue i WHERE IssueID = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Over Due Date', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Over Due Date" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Over Due Date" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetQuoteID`;
DELIMITER |
CREATE PROCEDURE `ameGetQuoteID`(userid varchar(255),ID varchar(255))
    DETERMINISTIC
BEGIN
DECLARE CheckU VARCHAR(255) DEFAULT("");
DECLARE SYear INT DEFAULT(0);
DECLARE SEmpID VARCHAR(255) DEFAULT("");
DECLARE SAuthID VARCHAR(255) DEFAULT("");
SELECT UType INTO CheckU FROM tbluser WHERE UID = userid;
SELECT Auth_ID INTO SAuthID FROM hrm_authorisation_approved WHERE Auth_ProcessID = ID LIMIT 1;
IF CheckU = "ESS" THEN
SELECT SalID, SalType, SalReference, SalDate, PeriodM, PeriodY, CONCAT("[",CustID,"] ",(SELECT dd.CustName FROM tblcustomer dd WHERE dd.CustID = qu.CustID)) "CustID" , (SELECT cc.TermCreditLimit FROM tblcustomer cc WHERE cc.CustID = qu.CustID) "CreditLimit" ,  PriceCode, PostStatus, PmtStatus, JID, COALESCE(PmtSchID,"") as "PmtSchID", CONCAT("[",EmpID,"] ", (SELECT ep.EmpName FROM tblemployee ep  WHERE ep.EmpID = qu.EmpID)) "EmpID" ,StartDate, ExpireDate, TotalAmt, DisInvDol, DisInvPer, TotalDis, TotalSTax, TotalVTax, NetTotalAmt,
PmtToDate, Cash, CashCard, COALESCE(PmtDisDate,"") "PmtDisDate", Remark, COALESCE(Message,"") "Message", COALESCE(ClientID,"") "ClientID", COALESCE(LocationID,"") "LocationID", DueDate, ClassID, ShipTo,
uidcreate, uidmodify, datecreate, datemodify, ApproveStatus FROM tblsbquote qu WHERE SalID = ID ;
SELECT sa.SalID,(SELECT i.ItemName From tblitem i WHERE i.ItemID = sa.ItemID) "ItemName", sa.LineNo,sa.ItemID, sa.UomID, sa.LocationID,sa.SalQty, sa.UnitPrice,sa.TotalAmt,sa.COGSUnit, sa.COGSAC2,sa.COGSAC3,sa.COGSAC4,sa.COGSAC5,sa.COGSTotal,sa.PostStatus,sa.JID,sa.NetTotalAmt,sa.DisDol,sa.DisPer,sa.STaxDol,sa.STaxPer,sa.VTaxDol,sa.VTaxPer,sa.ItemStatus,sa.ReportPrice,sa.AccID,sa.Des,sa.Factor, sa.ClientID, sa.RKSPrice FROM tblsbquotedetails sa WHERE SalID  = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Quote', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Quote" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Quote" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
ELSE
SELECT SalID, SalType, SalReference, SalDate, PeriodM, PeriodY, CONCAT("[",CustID,"] ",(SELECT dd.CustName FROM tblcustomer dd WHERE dd.CustID = qu.CustID)) "CustID" , (SELECT cc.TermCreditLimit FROM tblcustomer cc WHERE cc.CustID = qu.CustID) "CreditLimit" ,  PriceCode, PostStatus, PmtStatus, JID, COALESCE(PmtSchID,"") as "PmtSchID", CONCAT("[",EmpID,"] ", (SELECT ep.EmpName FROM tblemployee ep  WHERE ep.EmpID = qu.EmpID)) "EmpID",StartDate, ExpireDate, TotalAmt, DisInvDol, DisInvPer, TotalDis, TotalSTax, TotalVTax, NetTotalAmt,
PmtToDate, Cash, CashCard, COALESCE(PmtDisDate,"") "PmtDisDate", Remark, COALESCE(Message,"") "Message", COALESCE(ClientID,"") "ClientID", COALESCE(LocationID,"") "LocationID", DueDate, ClassID, ShipTo,
uidcreate, uidmodify, datecreate, datemodify, ApproveStatus FROM tblsbquote qu WHERE SalID = ID ;
SELECT sa.SalID,(SELECT i.ItemName From tblitem i WHERE i.ItemID = sa.ItemID) "ItemName" , sa.LineNo,sa.ItemID, sa.UomID, sa.LocationID,sa.SalQty, sa.UnitPrice,sa.TotalAmt,sa.COGSUnit, sa.COGSAC2,sa.COGSAC3,sa.COGSAC4,sa.COGSAC5,sa.COGSTotal,sa.PostStatus,sa.JID,sa.NetTotalAmt,sa.DisDol,sa.DisPer,sa.STaxDol,sa.STaxPer,sa.VTaxDol,sa.VTaxPer,sa.ItemStatus,sa.ReportPrice,sa.AccID,sa.Des,sa.Factor, sa.ClientID, sa.RKSPrice FROM tblsbquotedetails sa WHERE SalID  = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Quote', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Quote" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Quote" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetSaleOrderID`;
DELIMITER |
CREATE PROCEDURE `ameGetSaleOrderID`(userid varchar(255),ID varchar(255))
    DETERMINISTIC
BEGIN
DECLARE CheckU VARCHAR(255) DEFAULT("");
DECLARE SYear INT DEFAULT(0);
DECLARE SEmpID VARCHAR(255) DEFAULT("");
DECLARE SAuthID VARCHAR(255) DEFAULT("");
SELECT UType INTO CheckU FROM tbluser WHERE UID = userid;
SELECT Auth_ID INTO SAuthID FROM hrm_authorisation_approved WHERE Auth_ProcessID = ID LIMIT 1;
IF CheckU = "ESS" THEN
SELECT SalID, SalType, SalReference, SalDate, PeriodM, PeriodY, CONCAT("[",CustID,"] ",(SELECT dd.CustName FROM tblcustomer dd WHERE dd.CustID = ss.CustID)) "CustID" ,(SELECT cc.TermCreditLimit FROM tblcustomer cc WHERE cc.CustID = ss.CustID) "CreditLimit", PriceCode, PostStatus, PmtStatus, JID, COALESCE(PmtSchID,"") as "PmtSchID", CONCAT("[",EmpID,"] ", (SELECT ep.EmpName FROM tblemployee ep  WHERE ep.EmpID = ss.EmpID)) "EmpID", TotalAmt, DisInvDol, DisInvPer, TotalDis, TotalSTax, TotalVTax, NetTotalAmt,
PmtToDate, Cash, CashCard, CreditCard, OtherSetAmt, ReceiveAR, COALESCE(PmtDisDate,"") "PmtDisDate", Remark, COALESCE(Message,"") "Message", COALESCE(ClientID,"") "ClientID", COALESCE(LocationID,"") "LocationID", DueDate, ClassID, ShipTo,
IsRead, uidcreate, uidmodify, datecreate, datemodify, ApproveStatus FROM tblsaleorder ss WHERE SalID = ID ;
SELECT sa.SalID,(SELECT i.ItemName From tblitem i WHERE i.ItemID = sa.ItemID) "ItemName", sa.LineNo,sa.ItemID, sa.UomID, sa.LocationID,sa.SalQty, sa.UnitPrice,sa.TotalAmt,sa.COGSUnit, sa.COGSAC2,sa.COGSAC3,sa.COGSAC4,sa.COGSAC5,sa.COGSTotal,sa.PostStatus,sa.JID,sa.NetTotalAmt,sa.DisDol,sa.DisPer,sa.STaxDol,sa.STaxPer,sa.VTaxDol,sa.VTaxPer,sa.ItemStatus,sa.ReportPrice,sa.AccID,sa.Des,sa.Factor, sa.ClientID, sa.RKSPrice FROM tblsaleorderdetails sa WHERE SalID  = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Sale Order', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Sale Order" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID  AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Sale Order" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
ELSE
SELECT SalID, SalType, SalReference, SalDate, PeriodM, PeriodY, CONCAT("[",CustID,"] ",(SELECT dd.CustName FROM tblcustomer dd WHERE dd.CustID = ss.CustID)) "CustID" ,(SELECT cc.TermCreditLimit FROM tblcustomer cc WHERE cc.CustID = ss.CustID) "CreditLimit", PriceCode, PostStatus, PmtStatus, JID, COALESCE(PmtSchID,"") as "PmtSchID", CONCAT("[",EmpID,"] ", (SELECT ep.EmpName FROM tblemployee ep  WHERE ep.EmpID = ss.EmpID)) "EmpID", TotalAmt, DisInvDol, DisInvPer, TotalDis, TotalSTax, TotalVTax, NetTotalAmt,
PmtToDate, Cash, CashCard, CreditCard, OtherSetAmt, ReceiveAR, COALESCE(PmtDisDate,"") "PmtDisDate", Remark, COALESCE(Message,"") "Message", COALESCE(ClientID,"") "ClientID", COALESCE(LocationID,"") "LocationID", DueDate, ClassID, ShipTo,
IsRead, uidcreate, uidmodify, datecreate, datemodify, ApproveStatus FROM tblsaleorder ss WHERE SalID = ID ;
SELECT sa.SalID,(SELECT i.ItemName From tblitem i WHERE i.ItemID = sa.ItemID) "ItemName", sa.LineNo,sa.ItemID, sa.UomID, sa.LocationID,sa.SalQty, sa.UnitPrice,sa.TotalAmt,sa.COGSUnit, sa.COGSAC2,sa.COGSAC3,sa.COGSAC4,sa.COGSAC5,sa.COGSTotal,sa.PostStatus,sa.JID,sa.NetTotalAmt,sa.DisDol,sa.DisPer,sa.STaxDol,sa.STaxPer,sa.VTaxDol,sa.VTaxPer,sa.ItemStatus,sa.ReportPrice,sa.AccID,sa.Des,sa.Factor, sa.ClientID, sa.RKSPrice FROM tblsaleorderdetails sa WHERE SalID  = ID;
SELECT a.Auth_ID,a.Auth_Name,a.Auth_Type,a.Auth_AndOr,a.Auth_Amount, fnHRMCheckAuthorisationApproved(ID, 'Sale Order', SAuthID) AS 'Auth_Approved' FROM hrm_authorisation_temp a WHERE a.Auth_ID = SAuthID AND a.Auth_TranID = ID;
SELECT a.Auth_ID,a.Auth_EmpID,a.Auth_GroupID,a.Auth_GroupAndOr,a.Auth_GroupAmount,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_detail_temp a
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Sale Order" AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID)
LEFT JOIN tblemployee b ON(a.Auth_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND a.Auth_ID = SAuthID AND IF(a.Auth_GroupID <> '', TRUE, LOWER(app.Auth_Status) IN ("requested","rejected","approved"));
SELECT a.AuthGroup_ID,a.AuthGroup_Name,a.AuthGroup_Description,d.Auth_GroupID,COALESCE(d.Auth_GroupAndOr,"") as "Auth_GroupAndOr",COALESCE(d.Auth_GroupAmount,"") as "AuthGroup_Amount",(SELECT COUNT(ds.AuthGroup_ID) FROM hrm_authorisation_group_detail_temp ds WHERE ds.AuthGroup_ID = a.AuthGroup_ID AND ds.Auth_TranID = ID) as 'Count'
,(SELECT COUNT(ds.Auth_GroupID) FROM hrm_authorisation_approved ds WHERE ds.Auth_ProcessID = ID AND ds.Auth_GroupID = a.AuthGroup_ID AND ds.Auth_ID = SAuthID AND ds.Auth_Status = "approved") AS 'AuthGroup_Approved'
FROM hrm_authorisation_group_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(d.Auth_GroupID = a.AuthGroup_ID AND d.Auth_ID = SAuthID AND a.Auth_TranID = ID)
WHERE a.Auth_TranID = ID
ORDER BY a.AuthGroup_ID DESC;
SELECT a.AuthGroup_ID,a.AuthGroup_EmpID,
b.EmpID,b.EmpName,b.EmpEmail,b.Emp_CompanyEmail,COALESCE(b.EmpPosition, '') AS 'Position', COALESCE(b.EmpPositionKh, '') AS 'PositionKhmer', COALESCE(b.Emp_PositionLevel) AS 'Position_Level',u.UName,app.Auth_Status
FROM hrm_authorisation_group_detail_temp a
INNER JOIN hrm_authorisation_detail_temp d ON(a.AuthGroup_ID = d.Auth_GroupID AND d.Auth_ID = SAuthID AND a.Auth_TranID = d.Auth_TranID)
LEFT JOIN hrm_authorisation_approved app ON(app.Auth_ID = SAuthID AND app.Auth_GroupID = a.AuthGroup_ID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = "Sale Order" AND app.Auth_EmpID = a.AuthGroup_EmpID)
LEFT JOIN tblemployee b ON(a.AuthGroup_EmpID = b.EmpID)
LEFT JOIN (SELECT UID,EmpID FROM tbluseremployee GROUP BY EmpID) ue ON(ue.EmpID = b.EmpID)
LEFT JOIN tbluser u ON(u.UID = ue.UID)
WHERE a.Auth_TranID = ID AND d.Auth_ID = SAuthID
ORDER BY a.AuthGroup_EmpID, a.AuthGroup_ID;
END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetTranID`;
DELIMITER |
CREATE PROCEDURE `ameGetTranID`(IN processname varchar(255))
    DETERMINISTIC
BEGIN
	IF processname = "General Requisition" THEN
		SELECT FrmID FROM crm_frm_general;

	ELSEIF processname = "Expense Request" THEN
		SELECT EntryNo FROM tblexpenserequest;

	ELSEIF processname = "Over Due Date" THEN
		SELECT IssueID FROM tblcustomeroverdue;

	ELSEIF processname = "Temporary Credit" THEN
		SELECT IssueID FROM tblcustomerissue;

	ELSEIF processname = "Logistic Expense Request" THEN
		SELECT EntryNo FROM logistic_expense_request;

	ELSEIF processname = "Logistic Material Request" THEN
		SELECT EntryNo FROM logistic_material;

	ELSEIF processname = "Customer" THEN
		SELECT CustID FROM crm_customer;

	ELSEIF processname = "Quote" THEN
		SELECT SalID FROM tblsbquote;

	ELSEIF processname = "Sale Order" THEN
		SELECT SalID FROM tblsaleorder;

	ELSEIF processname = "Budget Requisition" THEN
		SELECT FrmID FROM crm_frm_budget;

	ELSEIF processname = "Material Request" THEN
		SELECT EntryNo FROM tblmaterialrequest;

	END IF;

END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameGetTranNameApproval`;
DELIMITER |
CREATE PROCEDURE `ameGetTranNameApproval`(IN `ProName` varchar(255),IN `TranID` varchar(255))
    DETERMINISTIC
BEGIN
	DECLARE countCustApp  INT;
	DECLARE countCustPad  INT;

	IF ProName = "Customer" THEN
				SELECT COUNT(CustID) INTO countCustApp FROM crm_customer WHERE CustID = TranID;
				IF countCustApp <> 0 THEN
					SELECT CustName as "authTranName" FROM crm_customer WHERE CustID = TranID;
				ELSE
					SELECT CustName AS "authTranName" FROM tblcustomer WHERE CustID = TranID;		
				END IF;	
	-- ELSE IF ProName = "Logistic Material" THEN
		-- SELECT EntryNo FROM logistic_material WHERE EntryNo =  TranID;
	-- ELSE IF ProName = "Logistic Expense" THEN
		-- SELECT EntryNo FROM logistic_expense_request WHERE EntryNo =  TranID;

	END IF;

END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameInsertCollaboration`;
DELIMITER |
CREATE PROCEDURE `ameInsertCollaboration`(IN `desc` varchar(255),IN `id` varchar(255), IN `date` varchar(255), IN `rto` varchar(255), IN `rtotype` varchar(255),IN `cbo` varchar(255))
    DETERMINISTIC
BEGIN
INSERT INTO int_collaboration (CB_Des, CB_UID, CB_CreateDate, CB_RToID, CB_RToType,CB_Own)
VALUES (`desc`,`id`, `date`, `rto`, `rtotype`, `cbo`);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameInsertCollaborationDetail`;
DELIMITER |
CREATE PROCEDURE `ameInsertCollaborationDetail`(IN `pid` varchar(255), IN `cmd` varchar(255), IN `un` varchar(255))
    DETERMINISTIC
BEGIN
INSERT INTO int_collaboration_details (PostID,`Comment`,CreateDate,Username) VALUES (`pid`,`cmd`,NOW(),`un`);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameUpdateCollaboration`;
DELIMITER |
CREATE PROCEDURE `ameUpdateCollaboration`(IN `desc` varchar(255),IN `id` varchar(255))
    DETERMINISTIC
BEGIN

	UPDATE int_collaboration SET CB_Des = `desc` WHERE id = `id`;

END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `ameUpdateRequestApproval`;
DELIMITER |
CREATE PROCEDURE `ameUpdateRequestApproval`(IN processid varchar(255),IN processname varchar(255))
    DETERMINISTIC
BEGIN
DECLARE authId VARCHAR(255) DEFAULT "";
DECLARE authType VARCHAR(255) DEFAULT "";
DECLARE authCount INT(11) DEFAULT 0;
DECLARE empid VARCHAR(255) DEFAULT "";
SELECT COUNT(Auth_ID) INTO authCount FROM hrm_authorisation_approved WHERE Auth_ProcessID = processid AND Auth_ProcessName = processname ;
IF authCount = 0 THEN
IF processname = "General Requisition" THEN
SELECT crm_frm_general.EmpID INTO empid FROM crm_frm_general WHERE crm_frm_general.FrmID = processid;
ELSEIF processname = "Expense Request" THEN
SELECT tblexpenserequest.RequestedBy INTO empid FROM tblexpenserequest WHERE tblexpenserequest.EntryNo = processid;
ELSEIF processname = "Over Due Date" THEN
SELECT (SELECT tbluseremployee.EmpID FROM tbluseremployee WHERE tbluseremployee.UID = tblcustomeroverdue.CreateBy LIMIT 1) INTO empid  FROM tblcustomeroverdue  WHERE tblcustomeroverdue.IssueID = processid;
ELSEIF processname = "Temporary Credit" THEN
SELECT (SELECT tbluseremployee.EmpID FROM tbluseremployee WHERE tbluseremployee.UID = tblcustomerissue.CreateBy LIMIT 1) INTO empid FROM tblcustomerissue  WHERE tblcustomerissue.IssueID = processid;
ELSEIF processname = "Logistic Expense Request" THEN
SELECT logistic_expense_request.RequestedBy INTO empid FROM logistic_expense_request WHERE logistic_expense_request.EntryNo = processid;
ELSEIF processname = "Logistic Material Request" THEN
SELECT logistic_material.RequestedBy INTO empid FROM logistic_material WHERE logistic_material.EntryNo = processid;
ELSEIF processname = "Customer" THEN
SELECT (SELECT tbluseremployee.EmpID FROM tbluseremployee WHERE tbluseremployee.UID = crm_customer.CBy  LIMIT 1) INTO empid FROM crm_customer WHERE crm_customer.CustID = processid;
ELSEIF processname = "Quote" THEN
SELECT tblsbquote.EmpID INTO empid FROM tblsbquote WHERE tblsbquote.SalID = processid;
ELSEIF processname = "Sale Order" THEN
SELECT tblsaleorder.EmpID  INTO empid FROM tblsaleorder WHERE tblsaleorder.SalID = processid;
ELSEIF processname = "Budget Requisition" THEN
SELECT crm_frm_budget.EmpID  INTO empid FROM crm_frm_budget WHERE crm_frm_budget.FrmID = processid;
ELSEIF processname = "Material Request" THEN
SELECT tblmaterialrequest.RequestedBy INTO empid FROM tblmaterialrequest WHERE tblmaterialrequest.EntryNo = processid;
END IF;
SELECT ae.Auth_ID 'Auth_ID', (SELECT Auth_Type FROM hrm_authorisation WHERE Auth_ID = ae.Auth_ID) 'Auth_Type' INTO authId, authType
FROM hrm_authorisation_employee ae
WHERE ae.Emp_ID = empid AND ae.Auth_Process = processname;
IF processname = "General Requisition" THEN
UPDATE crm_frm_general SET crm_frm_general.GRAuthID = authId WHERE crm_frm_general.FrmID = processid;
ELSEIF processname = "Expense Request" THEN
UPDATE tblexpenserequest SET tblexpenserequest.ERAuthID = authId WHERE tblexpenserequest.EntryNo = processid;
ELSEIF processname = "Over Due Date" THEN
UPDATE tblcustomeroverdue SET tblcustomeroverdue.ODAuthID = authId WHERE tblcustomeroverdue.IssueID = processid;
ELSEIF processname = "Temporary Credit" THEN
UPDATE tblcustomerissue SET tblcustomerissue.TCAuthID = authId WHERE tblcustomerissue.IssueID = processid;
ELSEIF processname = "Logistic Expense Request" THEN
UPDATE logistic_expense_request SET logistic_expense_request.LEAuthID = authId WHERE logistic_expense_request.EntryNo = processid;
ELSEIF processname = "Logistic Material Request" THEN
UPDATE logistic_material SET logistic_material.LMAuthID = authId WHERE logistic_material.EntryNo = processid;
ELSEIF processname = "Customer" THEN
update crm_customer SET crm_customer.CAuthID = authId WHERE crm_customer.CustID = processid;
ELSEIF processname = "Quote" THEN
update tblsbquote SET tblsbquote.QAuthID = authId WHERE tblsbquote.SalID = processid;
ELSEIF processname = "Sale Order" THEN
update tblsaleorder SET tblsaleorder.SAuthID = authId WHERE tblsaleorder.SalID = processid;
ELSEIF processname = "Budget Requisition" THEN
UPDATE crm_frm_budget SET crm_frm_budget.BRAuthID = authId WHERE crm_frm_budget.FrmID = processid;
ELSEIF processname = "Material Request" THEN
UPDATE tblmaterialrequest SET tblmaterialrequest.MRAuthID = authId WHERE tblmaterialrequest.EntryNo = processid;
END IF;
IF authType = "Individual" THEN
CALL spHRMAddAuthorisationTemp(authId,processid);
CALL spHRMAddIAuthorisationApproved(authId, processid, processname);
ELSE
CALL spHRMAddAuthorisationTemp(authId,processid);
CALL spHRMAddGAuthorisationApproved(authId, processid, processname);
END IF;
END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_LOAD_DEF_ACC`;
DELIMITER |
CREATE PROCEDURE `AME_LOAD_DEF_ACC`(P1 varchar(100))
    DETERMINISTIC
BEGIN		
		SELECT COALESCE(T1.var_accid, '') AS `P_ID`, COALESCE(T2.var_name, '') AS `P_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";
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_DELETE_ALL_TABLE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DELETE_ALL_TABLE`()
    DETERMINISTIC
BEGIN
		DELETE FROM ame_mt_ite;
		DELETE FROM ame_mt_ite_uom;
		DELETE FROM ame_mt_ite_pri;
		DELETE FROM ame_mt_ite_pri_his;
		ALTER TABLE ame_mt_ite_pri_his AUTO_INCREMENT = 1;

		DELETE FROM ame_mt_ite_ass;
		DELETE FROM ame_mt_ite_gro;
		DELETE FROM ame_mt_cla;
		DELETE FROM ame_mt_uom;
		DELETE FROM ame_mt_pri_cod;
		DELETE FROM ame_mt_loc;
		DELETE FROM ame_mt_pos;
		DELETE FROM ame_mt_emp;
		DELETE FROM ame_mt_emp_gro;
		DELETE FROM ame_mt_ven;
		DELETE FROM ame_mt_ven_gro;
		DELETE FROM ame_mt_cus;
		DELETE FROM ame_mt_cus_gro;
		DELETE FROM ame_user_log;
		DELETE FROM ame_mt_pmt_meth;
		DELETE FROM ame_mt_pmt_term;
		DELETE FROM ame_mt_ship_addr;

		DELETE FROM ame_user WHERE var_id <> 'Admin';
		UPDATE ame_user SET var_empid = '' WHERE var_id = 'Admin';
		DELETE FROM ame_user_app;
		DELETE FROM ame_user_pro_rem;
		DELETE FROM ame_user_pro_def_pre;
		DELETE FROM ame_user_role;

		DELETE FROM ame_sys_role WHERE tin_sys <> 1;
		DELETE FROM ame_user_role;
		DELETE FROM ame_user_per_frm;
		DELETE FROM ame_user_per_oth ;
		DELETE FROM ame_user_per_rep;

		DELETE FROM ame_ap_po;
		DELETE FROM ame_ap_po_det;
		DELETE FROM ame_ap_in;
		DELETE FROM ame_ap_in_det;
		DELETE FROM ame_ap_ret;
		DELETE FROM ame_ap_ret_det;
		DELETE FROM ame_ap_dn;
		DELETE FROM ame_ap_dn_det;
		DELETE FROM ame_ap_py;
		DELETE FROM ame_ap_py_app;
		DELETE FROM ame_ap_py_det;
		DELETE FROM ame_ar_quo;
		DELETE FROM ame_ar_quo_det;
		DELETE FROM ame_ar_so;
		DELETE FROM ame_ar_so_det;
		DELETE FROM ame_ar_in;
		DELETE FROM ame_ar_in_det;
		DELETE FROM ame_ar_ret;
		DELETE FROM ame_ar_ret_det;
		DELETE FROM ame_ar_cn;
		DELETE FROM ame_ar_cn_det;
		DELETE FROM ame_ar_sm;
		DELETE FROM ame_ar_sm_det;
		DELETE FROM ame_ar_py;
		DELETE FROM ame_ar_py_app;
		DELETE FROM ame_ar_py_det;

		DELETE FROM ame_refund;

		DELETE FROM ame_ic_tf;
		DELETE FROM ame_ic_tf_det;
		DELETE FROM ame_ic_iu;
		DELETE FROM ame_ic_iu_det;
		DELETE FROM ame_ic_icuom;
		DELETE FROM ame_ic_icuom_det;
		DELETE FROM ame_ic_ia;
		DELETE FROM ame_ic_ia_det;
		DELETE FROM ame_ic_ip;
		DELETE FROM ame_ic_ip_det;

		DELETE FROM ame_sl_ap;
		DELETE FROM ame_sl_ap_his;
		DELETE FROM ame_sl_ar;
		DELETE FROM ame_sl_ar_his;
		DELETE FROM ame_sl_ic;
		DELETE FROM ame_sl_ic_batch;
		DELETE FROM ame_sl_ic_his;

		DELETE FROM ame_gl;
		DELETE FROM ame_gl_his;
		DELETE FROM ame_gl_je;
		DELETE FROM ame_gl_je_det;

		UPDATE ame_sys_prefix SET int_lastid = 1;
		UPDATE ame_sys_prefix_custom SET int_lastid = 1;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_DIALOG_AP_DN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AP_DN`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_dn T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_venid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ap_dn T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id");
				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_ap_dn 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.var_id 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.var_id 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.var_id 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_ap_dn 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_DIALOG_AP_IN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AP_IN`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_venid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT_FUN,")) AS 'Total Amount' FROM ame_ap_in T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id");
				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_ap_in 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.var_id 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.var_id 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.var_id 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_ap_in 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_DIALOG_AP_IN_INV`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AP_IN_INV`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS 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;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("T.var_type = 'Purchase Inventory' AND T.var_status = 'Posted' AND (T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_type = 'Purchase Inventory' AND T.var_status = 'Posted' AND T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_type = 'Purchase Inventory' AND T.var_status = 'Posted' AND T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_type = 'Purchase Inventory' AND T.var_status = 'Posted' AND T.var_id 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 WHERE T.var_type = 'Purchase Inventory' AND T.var_status = 'Posted' ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_venid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_net AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ap_in T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id");
				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_ap_in T WHERE T.var_type = 'Purchase Inventory' AND T.var_status = 'Posted' 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," WHERE T.var_type = 'Purchase Inventory' AND T.var_status = 'Posted' ORDER BY T.var_id 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.var_id 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.var_id 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_ap_in 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_DIALOG_AP_PO`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AP_PO`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%' OR T.var_status LIKE '%", P1,"%' OR T.var_pmtstatus LIKE '%", P1,"%' OR T.var_claid LIKE '%", P1,"%' OR T.var_venid LIKE '%", P1,"%' OR T1.var_name LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "STATUS" THEN SET @SQL_CON = CONCAT("T.var_status LIKE '%", P1,"%'", P_BET);
		WHEN "RECEIPT STATUS" THEN SET @SQL_CON = CONCAT("T.var_pmtstatus LIKE '%", P1,"%'", P_BET);
		WHEN "CLASS ID" THEN SET @SQL_CON = CONCAT("T.var_claid LIKE '%", P1,"%'", P_BET);
		WHEN "VENDOR ID" THEN SET @SQL_CON = CONCAT("T.var_venid LIKE '%", P1,"%'", P_BET);
		WHEN "VENDOR NAME" THEN SET @SQL_CON = CONCAT("T1.var_name LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_po T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_venid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Receipt Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ", P_DEC_AMT_FUN,")) AS 'Total Amount' FROM ame_ap_po T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id");
				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_ap_po 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.var_id 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.var_id 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.var_id 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_ap_po T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id"," 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_DIALOG_AP_PY`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AP_PY`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_ref LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%",p1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_py T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_venid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Post Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ", P_DEC_AMT_FUN, ")) AS 'Total Amount' FROM ame_ap_py T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id");
				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_ap_py 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.var_id 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.var_id 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.var_id 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_ap_py 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_DIALOG_AP_RET`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AP_RET`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ret T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_venid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT_FUN,")) AS 'Total Amount' FROM ame_ap_ret T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id");
				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_ap_ret 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.var_id 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.var_id 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.var_id 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_ap_ret 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_DIALOG_AP_RET_DOC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AP_RET_DOC`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS 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;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("T.var_type = '", P2, "' AND T.var_venid = '", P3, "' AND T.var_status = 'Posted' AND (T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_type = '", P2, "' AND T.var_venid = '", P3, "' AND T.var_status = 'Posted' AND T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_type = '", P2, "' AND T.var_venid = '", P3, "' AND T.var_status = 'Posted' AND T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_type = '", P2, "' AND T.var_venid = '", P3, "' AND T.var_status = 'Posted' AND T.var_id 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 WHERE T.var_type = P2 AND T.var_venid = P3 AND T.var_status = 'Posted' ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_venid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_net AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ap_in T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id");
				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_ap_in T WHERE T.var_type = P2 AND T.var_venid = P3 AND T.var_status = 'Posted' 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," WHERE T.var_type = '", P2, "' AND T.var_venid = '", P3, "' AND T.var_status = 'Posted' ORDER BY T.var_id 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.var_id 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.var_id 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_ap_in 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_DIALOG_AP_RF`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AP_RF`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("T.int_line = 0 AND (T.var_id LIKE '%",p1,"%' OR T.var_ref LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.int_line = 0 AND T.var_ref LIKE '%",p1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.int_line = 0 AND T.var_desc LIKE '%",p1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.int_line = 0 AND T.var_id 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_refund T WHERE T.int_line = 0 ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_doctype AS 'Type', T.var_docid AS 'Document Id', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_perid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Post Status', T.var_desc AS 'Remark', CAST(T.dou_funamt AS DECIMAL(16, ", P_DEC_AMT, ")) AS 'Total Amount' FROM ame_refund T LEFT JOIN ame_mt_ven T1 ON T.var_perid = T1.var_id");
				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_refund T WHERE T.int_line = 0 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," WHERE T.int_line = 0 ORDER BY T.var_id 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.var_id 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.var_id 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_refund 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_DIALOG_AR_CN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AR_CN`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ar_cn T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_net AS DECIMAL(16, ",P_DEC_AMT_FUN,")) AS 'Total Amount' FROM ame_ar_cn T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id");
				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_ar_cn 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.var_id 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.var_id 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.var_id 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_ar_cn 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_DIALOG_AR_IN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AR_IN`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ar_in T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT_FUN,")) AS 'Total Amount' FROM ame_ar_in T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id");
				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_ar_in 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.var_id 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.var_id 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.var_id 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_ar_in 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_DIALOG_AR_PY`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AR_PY`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_ref LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%",p1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ar_py T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Trans. Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_status AS 'Post Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ", P_DEC_AMT, ")) AS 'Total Amount' FROM ame_ar_py T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id");
				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_ar_py 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.var_id 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.var_id 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.var_id 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_ar_py 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_DIALOG_AR_QUO`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AR_QUO`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ar_quo T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ", P_DEC_AMT_FUN,")) AS 'Total Amount' FROM ame_ar_quo T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id");
				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_ar_quo 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.var_id 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.var_id 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.var_id 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_ar_quo 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_DIALOG_AR_RET`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AR_RET`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ar_ret T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT_FUN,")) AS 'Total Amount' FROM ame_ar_ret T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id");
				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_ar_ret 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.var_id 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.var_id 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.var_id 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_ar_ret 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_DIALOG_AR_RF`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AR_RF`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("T.int_line = 1 AND (T.var_id LIKE '%",p1,"%' OR T.var_ref LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.int_line = 1 AND T.var_ref LIKE '%",p1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.int_line = 1 AND T.var_desc LIKE '%",p1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.int_line = 1 AND T.var_id 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_refund T WHERE T.int_line = 1 ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_doctype AS 'Type', T.var_docid AS 'Document Id', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_perid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_status AS 'Post Status', T.var_desc AS 'Remark', CAST(T.dou_funamt AS DECIMAL(16, ", P_DEC_AMT, ")) AS 'Total Amount' FROM ame_refund T LEFT JOIN ame_mt_ven T1 ON T.var_perid = T1.var_id");
				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_refund T WHERE T.int_line = 1 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," WHERE T.int_line = 1 ORDER BY T.var_id 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.var_id 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.var_id 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_refund 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_DIALOG_AR_SM`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AR_SM`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ar_sm T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_desc AS 'Remark', T.var_status AS 'Status' FROM ame_ar_sm T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id");
				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_ar_sm 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.var_id 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.var_id 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.var_id 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_ar_sm 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_DIALOG_AR_SO`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_AR_SO`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ar_so T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ", P_DEC_AMT_FUN,")) AS 'Total Amount' FROM ame_ar_so T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id");
				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_ar_so 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.var_id 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.var_id 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.var_id 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_ar_so 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_DIALOG_GL_JE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_GL_JE`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    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 "";
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.int_jid LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.int_jid 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.int_jid FROM ame_gl_je T ORDER BY T.int_jid) T WHERE T.int_jid = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT COALESCE(T.int_jid, '') AS 'Entry No', DATE(T.dat_date) AS 'Date', T.var_modid AS 'Module Id', T.var_doctype AS `Document Type`, T.var_docid AS `Document Id`, COALESCE(T.var_ref, '') AS 'Reference', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ", P_DEC_AMT, ")) AS 'Total Amount' FROM ame_gl_je 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.int_jid FROM ame_gl_je T ORDER BY T.int_jid) T WHERE T.int_jid = 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_jid LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.int_jid 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 ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.int_jid 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.int_jid) / 100 INTO @PAGE FROM ame_gl_je T"," WHERE ", var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_IC_IA`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_IC_IA`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ic_ia T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_desc AS 'Remark', T.var_status AS 'Status' FROM ame_ic_ia 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_ic_ia 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.var_id 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.var_id 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.var_id 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_ic_ia 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_DIALOG_IC_ICUOM`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_IC_ICUOM`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ic_icuom T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_desc AS 'Remark', T.var_status AS 'Status' FROM ame_ic_icuom 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_ic_icuom 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.var_id 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.var_id 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.var_id 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_ic_icuom 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_DIALOG_IC_IP`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_IC_IP`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ic_ip T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_desc AS 'Remark', T.var_status AS 'Status' FROM ame_ic_ip 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_ic_ip 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.var_id 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.var_id 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.var_id 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_ic_ip 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_DIALOG_IC_IU`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_IC_IU`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ic_iu T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_desc AS 'Remark', T.var_status AS 'Status' FROM ame_ic_iu 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_ic_iu 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.var_id 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.var_id 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.var_id 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_ic_iu 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_DIALOG_IC_TF`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_IC_TF`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT_FUN INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT_FUN; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_ref LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%') ", P_BET);
		WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P1,"%'", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_ic_tf T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_desc AS 'Remark', T.var_status AS 'Status' FROM ame_ic_tf 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_ic_tf 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.var_id 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.var_id 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.var_id 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_ic_tf 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_DIALOG_MT_ACC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_ACC`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), P1 varchar(200), P2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF P2 = "TRUE" THEN
		SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
		SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",P1,"%' OR T.var_name LIKE '%",P1,"%' OR T.var_desc LIKE '%",P1,"%' OR T.int_typeid LIKE '%",P1,"%' OR T1.var_name LIKE '%",P1,"%')");
		WHEN "ACCOUNT NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",P1,"%'");
		WHEN "ACCOUNT TYPE ID" THEN SET @SQL_CON = CONCAT("T.int_typeid LIKE '%",P1,"%'");
		WHEN "ACCOUNT TYPE NAME" THEN SET @SQL_CON = CONCAT("T1.var_name LIKE '%",P1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",P1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",P1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
		SET @R = -1;
		SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_acc WHERE IF(P2 = 'TRUE', tin_inactive = 0,TRUE) ORDER BY var_id) T WHERE T.var_id  = P1;
		ELSE
		SET @SQL_SEL = "SELECT T.var_id AS 'Account Id', COALESCE(T.var_name, '') AS 'Account Name', COALESCE(T.int_typeid, '') AS 'Account Type Id', T1.var_name AS `Account Type Name`, T.var_desc AS 'Remark', T.tin_inactive AS 'Inactive' FROM ame_mt_acc T LEFT JOIN ame_mt_acc_type T1 ON T.int_typeid = T1.var_id";
		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, var_id FROM ame_mt_acc WHERE IF(P2 = 'TRUE', tin_inactive = 0,TRUE) ORDER BY 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," WHERE ",var_inact," ORDER BY T.var_id LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
		SELECT 0 AS RowIndex, 1 AS Page;
		SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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 ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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_mt_acc T LEFT JOIN ame_mt_acc_type T1 ON T.int_typeid = T1.var_id"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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_DIALOG_MT_ACC_TYPE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_ACC_TYPE`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    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;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
		SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
		SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%')");
		WHEN "ACCOUNT TYPE NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
		SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_acc_type WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE) ORDER BY var_id) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id = p1;
		ELSE
		SET @SQL_SEL = "SELECT COALESCE(T.var_id, '') AS 'Account Type Id', COALESCE(T.var_name, '') AS 'Account Type Name', COALESCE(T.var_group1, '') AS 'Type' FROM ame_mt_acc_type T";
		CASE tin_count
		WHEN 1 THEN # LOAD FORM
		SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_acc_type WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE) ORDER BY var_id) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," ORDER BY T.var_id LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
		SELECT 0 AS RowIndex, 1 AS Page;
		SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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 ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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_mt_acc_type T"," WHERE ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id");
		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 ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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_DIALOG_MT_CLA`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_CLA`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
				SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
				SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "CLASS NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_cla WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
		ELSE
				SET @SQL_SEL = "SELECT T.var_id AS 'Class Id', COALESCE(T.var_name, '') AS 'Class Name', T.var_desc AS 'Remark', T.tin_inactive AS 'Inactive'  FROM ame_mt_cla T";
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_cla WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_mt_cla T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_CUR`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_CUR`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
		SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
		SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "CURRENCY NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
		SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_cur WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
		ELSE
		SET @SQL_SEL = "SELECT T.var_id AS 'Currency Id', COALESCE(T.var_name, '') AS 'Currency Name', COALESCE(T.var_symbol, '') AS 'Symbol', T.var_desc AS 'Remark', T.tin_inactive AS 'Inactive'  FROM ame_mt_cur T";
		CASE tin_count
		WHEN 1 THEN # LOAD FORM
		SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_cur WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
		SELECT 0 AS RowIndex, 1 AS Page;
		SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_mt_cur T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_CUS`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_CUS`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
		SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
		SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "CUSTOMER NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		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_mt_cus T WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE) ORDER BY T.var_id) T WHERE T.var_id  = p1;
		ELSE
		SET @SQL_SEL = "SELECT T.var_id AS 'Customer Id', COALESCE(T.var_name, '') AS 'Customer Name', T.var_desc AS 'Remark', COALESCE(var_pricecode, '') AS 'Price Code', T.var_cur AS `Currency`, COALESCE(var_term, '') AS 'Payment Term', COALESCE(var_pmttype, '') AS 'Payment Method', T.tin_inactive AS 'Inactive'  FROM ame_mt_cus 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_mt_cus T WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE) 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," WHERE ",var_inact," ORDER BY T.var_id LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
		SELECT 0 AS RowIndex, 1 AS Page;
		SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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 ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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_mt_cus T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_CUS_GRO`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_CUS_GRO`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
		SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
		SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "GROUP NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
		SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_cus_gro WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
		ELSE
		SET @SQL_SEL = "SELECT T.var_id AS 'Group Id', COALESCE(T.var_name, '') AS 'Group Name', T.var_desc AS 'Remark', COALESCE(T.var_accid, '') AS `Receivable Acc. ID`, T.tin_inactive AS 'Inactive' FROM ame_mt_cus_gro T";
		CASE tin_count
		WHEN 1 THEN # LOAD FORM
		SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_cus_gro WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
		SELECT 0 AS RowIndex, 1 AS Page;
		SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_mt_cus_gro T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_CUS_MSG`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_CUS_MSG`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    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;
SET @SQL_EXC = "";
SET @SQL_CON = "";
IF p2 = "TRUE" THEN
SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
ELSE
SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
END IF;
CASE UPPER(var_column)
WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.int_id LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
WHEN "MESSAGE" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
ELSE SET @SQL_CON = CONCAT("T.int_id LIKE '%",p1,"%'");
END CASE;
CASE int_offset
WHEN -1 THEN
SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, int_id FROM ame_mt_cus_msg WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_id  = p1;
ELSE
SET @SQL_SEL = "SELECT COALESCE(T.int_id, '') AS 'Message Id', T.var_desc AS 'Message Name', T.tin_inactive AS 'Inactive'  FROM ame_mt_cus_msg T";
CASE tin_count
WHEN 1 THEN # LOAD FORM
SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, int_id FROM ame_mt_cus_msg WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
WHEN 2 THEN # SEARCH
SELECT 0 AS RowIndex, 1 AS Page;
SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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.int_id) / 100 INTO @PAGE FROM ame_mt_cus_msg T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_EMP`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_EMP`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), P1 varchar(200), P2 varchar(200), P3 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF P2 = "TRUE" THEN
				SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
				SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		IF P3 = "TRUE" THEN
				SET var_inact = CONCAT(var_inact, " AND T.tin_rep = 1 ");
				SET var_inact_opterator = CONCAT(var_inact_opterator, " T.tin_rep = 1 AND ");
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",P1,"%' OR T.var_name LIKE '%",P1,"%' OR T.var_desc LIKE '%",P1,"%')");
		WHEN "EMPLOYEE NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",P1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",P1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",P1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, T.var_id FROM ame_mt_emp T WHERE IF(P2 = 'TRUE', T.tin_inactive = 0, TRUE) AND IF(P3 = 'TRUE', T.tin_rep = 1, TRUE) ORDER BY T.var_id) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = P1;
		ELSE
				SET @SQL_SEL = "SELECT T.var_id AS 'Employee Id', COALESCE(T.var_name, '') AS 'Employee Name', T.var_desc AS 'Remark', T.var_cur AS 'Currency', COALESCE(T1.var_id, '') AS `User ID`, T.var_posid AS 'Position ID', T.tin_inactive AS 'Inactive'  FROM ame_mt_emp T LEFT JOIN ame_user T1 ON T.var_id = T1.var_empid";
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, T.var_id FROM ame_mt_emp T WHERE IF(P2 = 'TRUE', T.tin_inactive = 0,TRUE) AND IF(P3 = 'TRUE', T.tin_rep = 1, TRUE) ORDER BY T.var_id) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," ORDER BY T.var_id LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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 ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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_mt_emp T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_EMP_GRO`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_EMP_GRO`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
				SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
				SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "GROUP NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_emp_gro WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
		ELSE
				SET @SQL_SEL = "SELECT T.var_id AS 'Group Id', COALESCE(T.var_name, '') AS 'Group Name', T.var_desc AS 'Remark', T.tin_inactive AS 'Inactive'  FROM ame_mt_emp_gro T";
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_emp_gro WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_mt_emp_gro T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_HOL`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_HOL`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
				SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
				SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_name LIKE '%",p1,"%')");
		WHEN "HOLIDAY NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, int_id FROM ame_mt_hol WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_id  = p1;
		ELSE
				SET @SQL_SEL = "SELECT COALESCE(T.int_id, '') AS `Holiday Code`, COALESCE(T.var_name, '') AS `Holiday Name`, DATE(T.dat_date) AS 'Date', T.tin_inactive AS 'Inactive'  FROM ame_mt_hol T";
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, int_id FROM ame_mt_hol WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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.int_id) / 100 INTO @PAGE FROM ame_mt_hol T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_ITE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_ITE`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), P1 varchar(200), P2 varchar(200), P3 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF P2 = "TRUE" THEN
				SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
				SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		IF P3 = "TRUE" THEN
				SET var_inact = CONCAT(var_inact, " AND T.tin_no_sal = 0 ");
				SET var_inact_opterator = CONCAT(var_inact_opterator, " T.tin_no_sal = 0 AND ");
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_barcode LIKE '%", P1,"%' OR T.var_itemcode LIKE '%", P1,"%' OR T.var_name LIKE '%", P1,"%' OR T.var_desc LIKE '%", P1,"%')");
		WHEN "ITEM NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%", P1,"%'");
		WHEN "BARCODE" THEN SET @SQL_CON = CONCAT("T.var_barcode LIKE '%", P1,"%'");
		WHEN "ITEM CODE" THEN SET @SQL_CON = CONCAT("T.var_itemcode LIKE '%", P1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SET @R = -1;
				SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_ite T WHERE IF(P2 = 'TRUE', T.tin_inactive = 0, TRUE) AND IF(P3 = 'TRUE', T.tin_no_sal = 0, TRUE) ORDER BY T.var_id) T WHERE T.var_id  = P1;
		ELSE
				SET @SQL_SEL = "SELECT T.var_id AS 'Item Id', COALESCE(T.var_name, '') AS 'Item Name', COALESCE(T.var_barcode, '') AS 'Barcode', COALESCE(T.var_itemcode, '') AS 'Item Code', T.var_type AS 'Item Type', T.var_groupid AS 'Group Id', T.var_uomid AS 'Uom Id', T.var_desc AS 'Remark' FROM ame_mt_ite 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_mt_ite T WHERE IF(P2 = 'TRUE', tin_inactive = 0, TRUE) AND IF(P3 = 'TRUE', T.tin_no_sal = 0, TRUE) 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," WHERE ", var_inact," ORDER BY T.var_id LIMIT 100 OFFSET ", (int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ", var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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 ", var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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_mt_ite T"," WHERE ", var_inact_opterator," (",@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 ", var_inact_opterator," (", @SQL_CON,") ORDER BY T.var_id 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_DIALOG_MT_ITE_GRO`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_ITE_GRO`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
		SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
		SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "CLASS NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
		SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_ite_gro WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
		ELSE
		SET @SQL_SEL = "SELECT T.var_id AS 'Item Group Id', COALESCE(T.var_name, '') AS 'Item Group Name', T.var_desc AS 'Remark',
										COALESCE(T.var_accid_ic, '') AS `Inventory`, COALESCE(T.var_accid_pl, '') AS `Revenue`, COALESCE(T.var_accid_pl_ret, '') AS `Return Revenue`,
										COALESCE(T.var_accid_cogs, '') AS `COGS`, COALESCE(T.var_accid_ac1, '') AS `AC1`, COALESCE(T.var_accid_ac2, '') AS `AC2`,
										COALESCE(T.var_accid_ac3, '') AS `AC3`, COALESCE(T.var_accid_ac4, '') AS `AC4`, COALESCE(T.var_accid_ac5, '') AS `AC5`,
										COALESCE(T.var_accid_ia, '') AS `Inventory Adjustment`, COALESCE(T.var_accid_iu, '') AS `Internal Usage`,
										T.tin_inactive AS 'Inactive'  FROM ame_mt_ite_gro T";
		CASE tin_count
		WHEN 1 THEN # LOAD FORM
		SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_ite_gro WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
		SELECT 0 AS RowIndex, 1 AS Page;
		SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_mt_ite_gro T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_ITE_UOM`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_ITE_UOM`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), p1 varchar(100), p2 varchar(100))
    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;
SET @SQL_EXC = "";
SET @SQL_CON = "";
CASE UPPER(var_column)
WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_uomid LIKE '%",p1,"%' OR T1.var_name LIKE '%",p1,"%')");
WHEN "UOM NAME" THEN SET @SQL_CON = CONCAT("T1.var_name LIKE '%",p1,"%'");
ELSE SET @SQL_CON = CONCAT("T.var_uomid LIKE '%",p1,"%'");
END CASE;
CASE int_offset
WHEN -1 THEN
SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_uomid FROM ame_mt_ite_uom WHERE var_id = p2) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_uomid  = p1;
ELSE
SET @SQL_SEL = "SELECT T.var_uomid 'Uom Id', COALESCE(T1.var_name,'') 'Uom Name', COALESCE(T.dou_conv, 1) AS 'Conversion' FROM ame_mt_ite_uom T LEFT JOIN ame_mt_uom T1 ON T.var_id = T1.var_id";
CASE tin_count
WHEN 1 THEN # LOAD FORM
SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_uomid FROM ame_mt_ite_uom WHERE var_id = p2) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_uomid  = 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," WHERE T.var_id = '",p2,"' LIMIT 100 OFFSET ",(int_page * 100));
WHEN 2 THEN # SEARCH
SELECT 0 AS RowIndex, 1 AS Page;
SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE T.var_id = '",p2,"' AND (",@SQL_CON,") 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 T.var_id = '",p2,"' AND (",@SQL_CON,") 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_mt_ite_uom T LEFT JOIN ame_mt_uom T1 ON T.var_id = T1.var_id"," WHERE T.var_id = '",p2,"' AND (",@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 T.var_id = '",p2,"' AND (",@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_DIALOG_MT_LOC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_LOC`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
				SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
				SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_addr LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "LOCATION NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		WHEN "ADDRESS" THEN SET @SQL_CON = CONCAT("T.var_addr LIKE '%",p1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_loc WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
		ELSE
				SET @SQL_SEL = "SELECT T.var_id AS 'Location Id', COALESCE(T.var_name, '') AS 'Location Name', T.var_addr AS 'Address', T.var_desc AS 'Remark', T.tin_inactive AS 'Inactive'  FROM ame_mt_loc T";
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_loc WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_mt_loc T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_MSG`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_MSG`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    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;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
		SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
		SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.int_id LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "MESSAGE" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.int_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
		SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, int_id FROM ame_mt_msg WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_id  = p1;
		ELSE
		SET @SQL_SEL = "SELECT COALESCE(T.int_id, '') AS 'Message Id', T.var_desc AS 'Message Name', T.tin_inactive AS 'Inactive'  FROM ame_mt_msg T";
		CASE tin_count
		WHEN 1 THEN # LOAD FORM
		SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, int_id FROM ame_mt_msg WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
		SELECT 0 AS RowIndex, 1 AS Page;
		SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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.int_id) / 100 INTO @PAGE FROM ame_mt_msg T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_OPT`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_OPT`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200),p3 varchar(200))
    DETERMINISTIC
BEGIN
	DECLARE var_inact VARCHAR(100) DEFAULT '';
	DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
	DECLARE var_list VARCHAR(100) DEFAULT '';
	DECLARE var_list_opterator VARCHAR(100) DEFAULT '';
	DECLARE int_index INTEGER DEFAULT 0;
	DECLARE int_page INTEGER DEFAULT 0;
	SET @SQL_EXC = "";
	SET @SQL_CON = "";
	IF p2 = "TRUE" THEN
		SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
	ELSE
		SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
	END IF;
	IF p3 = "TRUE" THEN
		SET var_list = " T.var_type = 'LIST' ";SET var_list_opterator = " T.var_type = 'LIST' AND ";
	ELSE
		SET var_list = " TRUE ";SET var_list_opterator = " TRUE AND ";
	END IF;
	CASE UPPER(var_column)
	WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.int_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%' OR T.var_type LIKE '%",p1,"%') ");
	WHEN "FIELD ID" THEN SET @SQL_CON = CONCAT("T.int_id LIKE '%",p1,"%'");
	WHEN "FIELD NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
	WHEN "DESCRIPTION" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
	WHEN "TYPE" THEN SET @SQL_CON = CONCAT("T.var_type LIKE '%",p1,"%'");
	ELSE SET @SQL_CON = CONCAT("T.int_id LIKE '%",p1,"%'");
	END CASE;
	CASE int_offset
	WHEN -1 THEN
		SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, int_id FROM ame_mt_of WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE) AND IF(p3 = 'TRUE',var_type = 'LIST',TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_id  = p1;
	ELSE
		SET @SQL_SEL = "SELECT T.int_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";
		CASE tin_count
		WHEN 1 THEN # LOAD FORM
			SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, int_id FROM ame_mt_of WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE) AND IF(p3 = 'TRUE',var_type = 'LIST',TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_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," WHERE ",var_inact_opterator,var_list," LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
			SELECT 0 AS RowIndex, 1 AS Page;
			SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator,var_list_opterator," (",@SQL_CON,") 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 ",var_inact_opterator,var_list_opterator," (",@SQL_CON,") 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.int_id) / 100 INTO @PAGE FROM ame_mt_of T"," WHERE ",var_inact_opterator,var_list_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_OPT_VAL`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_OPT_VAL`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    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;
	SET @SQL_EXC = "";
	SET @SQL_CON = "";
	SET var_inact = CONCAT(" T.int_id = '",P2,"' ");SET var_inact_opterator = CONCAT(" T.int_id = '",P2,"' AND ");
	CASE UPPER(var_column)
	WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_value LIKE '%",p1,"%') ");
	WHEN "VALUE" THEN SET @SQL_CON = CONCAT("(T.var_value LIKE '%",p1,"%') ");
	ELSE SET @SQL_CON = CONCAT("(T.var_value LIKE '%",p1,"%') ");
	END CASE;
	CASE int_offset
	WHEN -1 THEN
		SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, int_id,int_line FROM ame_mt_of_list WHERE int_id = P2) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_line  = p1;
	ELSE
		SET @SQL_SEL = "SELECT COALESCE(T.int_line,0) AS 'Line No', COALESCE(T.var_value,'') AS 'Value' FROM ame_mt_of_list T";
		CASE tin_count
		WHEN 1 THEN # LOAD FORM
			SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, int_id, int_line FROM ame_mt_of_list WHERE int_id = P2) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_line  = 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
			SELECT 0 AS RowIndex, 1 AS Page;
			SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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.int_id) / 100 INTO @PAGE FROM ame_mt_of_list T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_PMT_METH`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_PMT_METH`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
				SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
				SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "DESCRIPTION" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_pmt_meth WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
		ELSE
				SET @SQL_SEL = "SELECT T.var_id AS 'Payment Method Id', COALESCE(T.var_name, '') AS 'Description', T.var_desc AS 'Remark', T.tin_inactive AS 'Inactive'  FROM ame_mt_pmt_meth T";
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_pmt_meth WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_mt_pmt_meth T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_PMT_TERM`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_PMT_TERM`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
				SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
				SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%')");
		WHEN "DESCRIPTION" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_pmt_term WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
		ELSE
				SET @SQL_SEL = "SELECT T.var_id AS 'Payment Term Id', COALESCE(T.var_name, '') AS 'Description', T.var_type AS 'Type', T.int_daydue AS 'Net Due Days', T.dou_disc AS 'Discount %', T.int_daydisc AS 'Discount Days', T.int_dayminpay AS 'Min Days to Pay', T.tin_inactive AS 'Inactive'  FROM ame_mt_pmt_term T";
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_pmt_term WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_mt_pmt_term T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_POS`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_POS`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
				SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
				SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_addr LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "LOCATION NAME 1" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		WHEN "LOCATION NAME 2" THEN SET @SQL_CON = CONCAT("T.var_addr LIKE '%",p1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_pos WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
		ELSE
				SET @SQL_SEL = "SELECT T.var_id AS 'Position Id', COALESCE(T.var_name, '') AS 'Position Name 1', T.var_addr AS 'Position Name 2', T.var_desc AS 'Remark', T.tin_inactive AS 'Inactive'  FROM ame_mt_pos T";
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_pos WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_mt_pos T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_PRI_COD`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_PRI_COD`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    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;
SET @SQL_EXC = "";
SET @SQL_CON = "";
IF p2 = "TRUE" THEN
SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
ELSE
SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
END IF;
CASE UPPER(var_column)
WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
WHEN "CLASS NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
END CASE;
CASE int_offset
WHEN -1 THEN
SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_pri_cod WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
ELSE
SET @SQL_SEL = "SELECT T.var_id AS 'Price Code', COALESCE(T.var_name, '') AS 'Description', T.var_desc AS 'Remark', T.tin_reportprice AS 'Comparative Price', T.tin_inactive AS 'Inactive'  FROM ame_mt_pri_cod T";
CASE tin_count
WHEN 1 THEN # LOAD FORM
SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_pri_cod WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
WHEN 2 THEN # SEARCH
SELECT 0 AS RowIndex, 1 AS Page;
SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_mt_pri_cod T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_SHIP_ADDR`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_SHIP_ADDR`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), P1 varchar(200), P2 varchar(200), P3 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P1,"%' OR T.var_name LIKE '%", P1,"%')");
		WHEN "SHIP TO ADDRESS NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%", P1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
		SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, T.var_id FROM ame_mt_ship_addr T WHERE T.var_type = P2 AND T.var_num = P3) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = P1;
		ELSE
		SET @SQL_SEL = "SELECT T.var_id AS 'Ship Address Id', COALESCE(T.var_name, '') AS 'Ship Address Name', COALESCE(T.var_addr, '') AS 'Address', T.tin_default AS 'Default Ship Address'  FROM ame_mt_ship_addr T";
		CASE tin_count
		WHEN 1 THEN # LOAD FORM
				SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, T.var_id FROM ame_mt_ship_addr T WHERE T.var_type = P2 AND T.var_num = P3) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE T.var_type = '", P2,"' AND T.var_num = '", P3,"' LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
				SELECT 0 AS RowIndex, 1 AS Page;
				SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE T.var_type = '", P2,"' AND T.var_num = '", P3,"' AND (",@SQL_CON,") 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 T.var_type = '", P2,"' AND T.var_num = '", P3,"' AND (",@SQL_CON,") 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_mt_ship_addr T"," WHERE T.var_type = '", P2,"' AND T.var_num = '", P3,"' AND(",@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 T.var_type = '", P2,"' AND T.var_num = '", P3,"' AND (",@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_DIALOG_MT_TEM_ODD`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_TEM_ODD`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", p1,"%' OR T.var_desc LIKE '%", p1,"%') ", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_mt_tem_odd T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', DATE(T.dat_from) AS 'Start Date', DATE(T.dat_to) AS 'End Date', T.var_cusid AS 'Customer Id', IF(T.var_type = 'Customer', T1.var_name, T2.var_name)  AS 'Customer Name', T.var_status AS 'Status', T.var_desc AS 'Remark' FROM ame_mt_tem_odd T LEFT JOIN ame_mt_cus T1 ON (T.var_type = 'Customer' AND T.var_cusid = T1.var_id)  LEFT JOIN ame_mt_ven T2 ON (T.var_type = 'Vendor' AND T.var_cusid = T1.var_id)");
				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_mt_tem_odd 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.var_id 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.var_id 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.var_id 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_mt_tem_odd 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_DIALOG_MT_TEM_TC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_TEM_TC`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT; END IF;CLOSE C1;END;
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", p1,"%' OR T.var_desc LIKE '%", p1,"%') ", P_BET);
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'", P_BET);
		ELSE SET @SQL_CON = CONCAT("T.var_id 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_mt_tem_tc T ORDER BY T.var_id) T WHERE T.var_id = P1;
		ELSE
				SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', DATE(T.dat_from) AS 'Start Date', DATE(T.dat_to) AS 'End Date', T.var_cusid AS 'Customer Id', IF(T.var_type = 'Customer', T1.var_name, T2.var_name)  AS 'Customer Name', T.var_status AS 'Status', T.var_desc AS 'Remark', CAST(T.dou_funamt AS DECIMAL(16, ", P_DEC_AMT, ")) AS 'Total Amount' FROM ame_mt_tem_tc T LEFT JOIN ame_mt_cus T1 ON (T.var_type = 'Customer' AND T.var_cusid = T1.var_id)  LEFT JOIN ame_mt_ven T2 ON (T.var_type = 'Vendor' AND T.var_cusid = T1.var_id)");
				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_mt_tem_tc 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.var_id 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.var_id 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.var_id 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_mt_tem_tc 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_DIALOG_MT_UOM`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_UOM`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    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;
SET @SQL_EXC = "";
SET @SQL_CON = "";
IF p2 = "TRUE" THEN
SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
ELSE
SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
END IF;
CASE UPPER(var_column)
WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
WHEN "UOM NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
END CASE;
CASE int_offset
WHEN -1 THEN
SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM AME_MT_UOM WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
ELSE
SET @SQL_SEL = "SELECT T.var_id AS 'Uom Id', COALESCE(T.var_name, '') AS 'Uom Name', T.var_desc AS 'Remark', T.tin_inactive AS 'Inactive'  FROM AME_MT_UOM T";
CASE tin_count
WHEN 1 THEN # LOAD FORM
SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM AME_MT_UOM WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
WHEN 2 THEN # SEARCH
SELECT 0 AS RowIndex, 1 AS Page;
SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_MT_UOM T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_VEN`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_VEN`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),P1 varchar(200), P2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
		SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
		SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "VENDOR NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		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_mt_ven T WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE) ORDER BY T.var_id) T WHERE T.var_id  = p1;
		ELSE
		SET @SQL_SEL = "SELECT T.var_id AS 'Vendor Id', COALESCE(T.var_name, '') AS 'Vendor Name', T.var_desc AS 'Remark', COALESCE(T.var_cur, '') AS `Currency`, COALESCE(T.var_term, '') AS 'Payment Term', COALESCE(T.var_pmttype, '') AS 'Payment Method', T.tin_inactive AS 'Inactive'  FROM AME_MT_VEN 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_mt_ven T WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE) 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," WHERE ",var_inact," ORDER BY T.var_id LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
		SELECT 0 AS RowIndex, 1 AS Page;
		SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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 ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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_MT_VEN T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_MT_VEN_GRO`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_MT_VEN_GRO`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),p1 varchar(200),p2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE var_inact VARCHAR(100) DEFAULT '';
		DECLARE var_inact_opterator VARCHAR(100) DEFAULT '';
		DECLARE int_index INTEGER DEFAULT -1;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
		SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
		SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%')");
		WHEN "GROUP NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
		SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_ven_gro WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id  = p1;
		ELSE
		SET @SQL_SEL = "SELECT T.var_id AS 'Group Id', COALESCE(T.var_name, '') AS 'Group Name', T.var_desc AS 'Remark', COALESCE(T.var_accid, '') AS `Payable Acc. ID`, T.tin_inactive AS 'Inactive'  FROM ame_mt_ven_gro T";
		CASE tin_count
		WHEN 1 THEN # LOAD FORM
		SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_mt_ven_gro WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE)) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," LIMIT 100 OFFSET ",(int_page * 100));
		WHEN 2 THEN # SEARCH
		SELECT 0 AS RowIndex, 1 AS Page;
		SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") 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 ",var_inact_opterator," (",@SQL_CON,") 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_mt_ven_gro T"," WHERE ",var_inact_opterator," (",@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 ",var_inact_opterator," (",@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_DIALOG_ORDER`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_ORDER`(var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100), P2 varchar(100), P3 varchar(100), P4 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_BET VARCHAR(250) DEFAULT "";
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE var_range
		WHEN "RANGE" THEN
				SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
		WHEN "TODAY" THEN
				SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
		WHEN "THIS PERIOD" THEN
				SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
		WHEN "THIS YEAR" THEN
				SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
		ELSE
				SET P_BET = CONCAT(" AND TRUE");
		END CASE;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT; END IF;CLOSE C1;END;
		CASE P1
		WHEN "AP-PO" THEN			
				SET P_BET = CONCAT(" AND T.var_venid = '" , P2, "' AND IF('", P3, "' = 'All', TRUE, T.var_status = '", P3, "')", P_BET);
				CASE UPPER(var_column)
				WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P4,"%' OR T.var_ref LIKE '%", P4,"%' OR T.var_desc LIKE '%", P4,"%') ", P_BET);
				WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P4,"%'", P_BET);
				WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P4,"%'", P_BET);
				ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P4,"%'", P_BET);
				END CASE;
				SET @SQL_EXC = CONCAT("SELECT FALSE AS '', T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T1.var_name AS 'Vendor Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Receive Status', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ap_po T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id WHERE ", @SQL_CON);
		WHEN "AP-IN" THEN
				SET P_BET = CONCAT(" AND T.var_status = 'Posted' AND T.var_venid = '" , P2, "' AND IF('", P3, "' = 'All', TRUE, T.var_retstatus = '", P3, "')", P_BET);
				CASE UPPER(var_column)
				WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P4,"%' OR T.var_ref LIKE '%", P4,"%' OR T.var_desc LIKE '%", P4,"%') ", P_BET);
				WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P4,"%'", P_BET);
				WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P4,"%'", P_BET);
				ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P4,"%'", P_BET);
				END CASE;
				SET @SQL_EXC = CONCAT("SELECT FALSE AS '', T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', COALESCE(T.var_po, '') AS 'PO Number', T1.var_name AS 'Vendor Name', T.var_retstatus AS 'Status', T.var_retpmtstatus AS 'Return Status', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ap_in T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id WHERE T.var_type = 'Purchase Inventory' AND T.var_status = 'Posted' AND ", @SQL_CON);
		WHEN "AP-IN-AC" THEN
				SET P_BET = CONCAT(" AND T.var_venid = '" , P2, "' ", P_BET);
				CASE UPPER(var_column)
				WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P4,"%' OR T.var_ref LIKE '%", P4,"%' OR T.var_desc LIKE '%", P4,"%') ", P_BET);
				WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P4,"%'", P_BET);
				WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P4,"%'", P_BET);
				ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P4,"%'", P_BET);
				END CASE;
				SET @SQL_EXC = CONCAT("SELECT FALSE AS '', T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', COALESCE(T.var_po, '') AS 'PO Number', T1.var_name AS 'Vendor Name', T.var_status AS 'Status', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ap_in T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id WHERE T.var_type = 'Purchase Inventory' AND T.var_status = 'Posted' AND ", @SQL_CON);
		WHEN "AR-SO" THEN
				SET P_BET = CONCAT(" AND T.var_cusid = '" , P2, "' AND IF('", P3, "' = 'All', TRUE, T.var_status = '", P3, "')", P_BET);
				CASE UPPER(var_column)
				WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P4,"%' OR T.var_ref LIKE '%", P4,"%' OR T.var_desc LIKE '%", P4,"%') ", P_BET);
				WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P4,"%'", P_BET);
				WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P4,"%'", P_BET);
				ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P4,"%'", P_BET);
				END CASE;
				SET @SQL_EXC = CONCAT("SELECT FALSE AS '', T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T1.var_name AS 'Customer Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Receive Status', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ar_quo T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id WHERE ", @SQL_CON);
		WHEN "AR-IN" THEN
				SET P_BET = CONCAT(" AND T.var_cusid = '" , P2, "' AND IF('", P3, "' = 'All', TRUE, T.var_status = '", P3, "')", P_BET);
				CASE UPPER(var_column)
				WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P4,"%' OR T.var_ref LIKE '%", P4,"%' OR T.var_desc LIKE '%", P4,"%') ", P_BET);
				WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P4,"%'", P_BET);
				WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P4,"%'", P_BET);
				ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P4,"%'", P_BET);
				END CASE;
				SET @SQL_EXC = CONCAT("SELECT FALSE AS '', T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T1.var_name AS 'Customer Name', T.var_status AS 'Status', T.var_pmtstatus AS 'Receive Status', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount', COALESCE(T.var_po, '') AS 'PO Number' FROM ame_ar_so T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id WHERE ", @SQL_CON);
		WHEN "AR-RET" THEN
				SET P_BET = CONCAT(" AND T.var_cusid = '" , P2, "' AND IF('", P3, "' = 'All', TRUE, T.var_retstatus = '", P3, "')", P_BET);
				CASE UPPER(var_column)
				WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P4,"%' OR T.var_ref LIKE '%", P4,"%' OR T.var_desc LIKE '%", P4,"%') ", P_BET);
				WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P4,"%'", P_BET);
				WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P4,"%'", P_BET);
				ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P4,"%'", P_BET);
				END CASE;
				SET @SQL_EXC = CONCAT("SELECT FALSE AS '', T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T1.var_name AS 'Customer Name', T.var_retstatus AS 'Status', T.var_retpmtstatus AS 'Return Status', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount', COALESCE(T.var_po, '') AS 'PO Number', COALESCE(T.var_so, '') AS 'SO Number' FROM ame_ar_in T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id WHERE T.var_type = 'General Item Sale' AND T.var_status = 'Posted' AND ", @SQL_CON);
		WHEN "AR-SM" THEN
				SET P_BET = CONCAT(" AND IF('", P2, "' = 'All' OR '", P2, "' = '', TRUE, T.var_cusid = '", P2, "') AND IF('", P3, "' = 'All', TRUE, T.var_shippmtstatus = '", P3, "')", P_BET);
				CASE UPPER(var_column)
				WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P4,"%' OR T.var_ref LIKE '%", P4,"%' OR T.var_desc LIKE '%", P4,"%') ", P_BET);
				WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P4,"%'", P_BET);
				WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P4,"%'", P_BET);
				ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P4,"%'", P_BET);
				END CASE;
				SET @SQL_EXC = CONCAT("(SELECT 'AR-IN' AS 'Module Id', T.var_id AS 'Entry No', T.var_ref AS 'Reference', T.dat_date AS 'Date', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_shippmtstatus AS 'Shipment Status', IF(P_PROGRESS >= 1, 'YES', 'NO') AS `Shipment in Progress`, CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ar_in T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id LEFT JOIN (SELECT T2.var_poid, COUNT(tin_progress) AS `P_PROGRESS` FROM ame_ar_sm T1 LEFT JOIN ame_ar_sm_det T2 ON T1.var_id = T2.var_id WHERE T1.var_type = 'AR-IN' AND T1.tin_progress = 1 GROUP BY T2.var_poid) AS T2 ON T.var_id = T2.var_poid WHERE T.var_type = 'General Item Sale' AND T.var_status = 'Posted' AND ", @SQL_CON, ") ",
															"UNION ALL (SELECT 'AR-RET' AS 'Module Id', T.var_id AS 'Entry No', T.var_ref AS 'Reference', T.dat_date AS 'Date', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_shippmtstatus AS 'Shipment Status', IF(P_PROGRESS >= 1, 'YES', 'NO') AS `Shipment in Progress`, CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ar_ret T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id LEFT JOIN (SELECT T2.var_poid, COUNT(tin_progress) AS `P_PROGRESS` FROM ame_ar_sm T1 LEFT JOIN ame_ar_sm_det T2 ON T1.var_id = T2.var_id WHERE T1.var_type = 'AR-RET' AND T1.tin_progress = 1 GROUP BY T2.var_poid) AS T2 ON T.var_id = T2.var_poid WHERE T.var_type = 'General Item Sale' AND T.var_status = 'Posted' AND ", @SQL_CON, ") ", 
															"UNION ALL (SELECT 'AR-CN' AS 'Module Id', T.var_id AS 'Entry No', T.var_ref AS 'Reference', T.dat_date AS 'Date', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_shippmtstatus AS 'Shipment Status', IF(P_PROGRESS >= 1, 'YES', 'NO') AS `Shipment in Progress`, CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ar_cn T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id LEFT JOIN (SELECT T2.var_poid, COUNT(tin_progress) AS `P_PROGRESS` FROM ame_ar_sm T1 LEFT JOIN ame_ar_sm_det T2 ON T1.var_id = T2.var_id WHERE T1.var_type = 'AR-CN' AND T1.tin_progress = 1 GROUP BY T2.var_poid) AS T2 ON T.var_id = T2.var_poid WHERE T.var_type = 'General Item Sale' AND T.var_status = 'Posted' AND ", @SQL_CON, ") ");
		WHEN "AR-SM-IN" THEN
				SET P_BET = CONCAT(" AND T.var_cusid = '" , P2, "' AND IF('", P3, "' = 'All', TRUE, T.var_shipstatus = '", P3, "')", P_BET);
				CASE UPPER(var_column)
				WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P4,"%' OR T.var_ref LIKE '%", P4,"%' OR T.var_desc LIKE '%", P4,"%') ", P_BET);
				WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P4,"%'", P_BET);
				WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P4,"%'", P_BET);
				ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P4,"%'", P_BET);
				END CASE;
				SET @SQL_EXC = CONCAT("SELECT FALSE AS '', T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T1.var_name AS 'Customer Name', T.var_shipstatus AS 'Status', T.var_shippmtstatus AS 'Shipment Status', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ar_in T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id WHERE T.var_type = 'General Item Sale' AND T.var_status = 'Posted' AND ", @SQL_CON);
		WHEN "AR-SM-RET" THEN
				SET P_BET = CONCAT(" AND T.var_cusid = '" , P2, "' AND IF('", P3, "' = 'All', TRUE, T.var_shipstatus = '", P3, "')", P_BET);
				CASE UPPER(var_column)
				WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P4,"%' OR T.var_ref LIKE '%", P4,"%' OR T.var_desc LIKE '%", P4,"%') ", P_BET);
				WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P4,"%'", P_BET);
				WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P4,"%'", P_BET);
				ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P4,"%'", P_BET);
				END CASE;
				SET @SQL_EXC = CONCAT("SELECT FALSE AS '', T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T1.var_name AS 'Customer Name', T.var_shipstatus AS 'Status', T.var_shippmtstatus AS 'Shipment Status', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ar_ret T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id WHERE T.var_type = 'General Item Sale' AND T.var_status = 'Posted' AND ", @SQL_CON);
		WHEN "AR-SM-CN" THEN
				SET P_BET = CONCAT(" AND T.var_cusid = '" , P2, "' AND IF('", P3, "' = 'All', TRUE, T.var_shipstatus = '", P3, "')", P_BET);
				CASE UPPER(var_column)
				WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%", P4,"%' OR T.var_ref LIKE '%", P4,"%' OR T.var_desc LIKE '%", P4,"%') ", P_BET);
				WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%", P4,"%'", P_BET);
				WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%", P4,"%'", P_BET);
				ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%", P4,"%'", P_BET);
				END CASE;
				SET @SQL_EXC = CONCAT("SELECT FALSE AS '', T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T1.var_name AS 'Customer Name', T.var_shipstatus AS 'Status', T.var_shippmtstatus AS 'Shipment Status', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ar_cn T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id WHERE T.var_type = 'General Item Sale' AND T.var_status = 'Posted' AND ", @SQL_CON);
		ELSE
				SET @Donothing = NULL;
		END CASE;
		PREPARE STM FROM @SQL_EXC;
		EXECUTE STM;
		DEALLOCATE PREPARE STM;	
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_DIALOG_REFUND`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_REFUND`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100))
    DETERMINISTIC
BEGIN
DECLARE int_index INTEGER DEFAULT 0;
DECLARE int_page INTEGER DEFAULT 0;
DECLARE P_BET VARCHAR(250) DEFAULT "";
DECLARE P_DEC_AMT INTEGER DEFAULT 2;
BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT; END IF;CLOSE C1;END;
CASE var_range
WHEN "RANGE" THEN
SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
WHEN "TODAY" THEN
SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
WHEN "THIS PERIOD" THEN
SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
WHEN "THIS YEAR" THEN
SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
ELSE
SET P_BET = CONCAT(" AND TRUE");
END CASE;
SET @SQL_EXC = "";
SET @SQL_CON = "";
CASE UPPER(var_column)
WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_ref LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%') ", P_BET);
WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%",p1,"%'", P_BET);
WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'", P_BET);
ELSE SET @SQL_CON = CONCAT("T.var_id 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_refund T ORDER BY T.var_id) T WHERE T.var_id = P1;
ELSE
SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_doctype AS 'Trans. Type', T.var_docid AS 'Document Id', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_perid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Post Status', T.var_desc AS 'Remark', CAST(T.dou_funamt AS DECIMAL(16, ", P_DEC_AMT, ")) AS 'Total Amount' FROM ame_refund T LEFT JOIN ame_mt_ven T1 ON T.var_perid = T1.var_id");
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_refund 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.var_id 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.var_id 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.var_id 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_refund 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_DIALOG_REFUND_DOC`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_REFUND_DOC`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), var_range varchar(100), dat_df date, dat_dt date, P1 varchar(100), P2 varchar(100), P3 varchar(100))
    DETERMINISTIC
BEGIN
		DECLARE P_DEC_AMT INTEGER DEFAULT 2;
		BEGIN DECLARE C1 CURSOR FOR SELECT 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_AMT; END IF;CLOSE C1;END;
		CASE P2
		WHEN "AP-RET" THEN
				BEGIN
						DECLARE int_index INTEGER DEFAULT 0;
						DECLARE int_page INTEGER DEFAULT 0;
						DECLARE P_BET VARCHAR(250) DEFAULT "";
						CASE var_range
						WHEN "RANGE" THEN
								SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
						WHEN "TODAY" THEN
								SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
						WHEN "THIS PERIOD" THEN
								SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
						WHEN "THIS YEAR" THEN
								SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
						ELSE
								SET P_BET = CONCAT(" AND TRUE");
						END CASE;
						SET @SQL_EXC = "";
						SET @SQL_CON = "";
						CASE UPPER(var_column)
						WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_ref LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%') ", P_BET);
						WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%",p1,"%'", P_BET);
						WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'", P_BET);
						ELSE SET @SQL_CON = CONCAT("T.var_id 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_ret T WHERE T.var_status = 'Posted' AND T.var_venid = P3 ORDER BY T.var_id) T WHERE T.var_id = P1;
						ELSE
								SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_venid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Post Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ap_ret T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id");
								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_ap_ret T WHERE T.var_status = 'Posted' AND T.var_venid = P3 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," WHERE T.var_status = 'Posted' AND T.var_venid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_venid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_venid = '", P3,"' ORDER BY T.var_id 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_ap_ret T"," WHERE (",@SQL_CON,") AND T.var_status = 'Posted' AND T.var_venid = '", P3,"'");
													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,") AND T.var_status = 'Posted' AND T.var_venid = '", P3,"' 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;
		WHEN "AP-DN" THEN
				BEGIN
						DECLARE int_index INTEGER DEFAULT 0;
						DECLARE int_page INTEGER DEFAULT 0;
						DECLARE P_BET VARCHAR(250) DEFAULT "";
						CASE var_range
						WHEN "RANGE" THEN
								SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
						WHEN "TODAY" THEN
								SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
						WHEN "THIS PERIOD" THEN
								SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
						WHEN "THIS YEAR" THEN
								SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
						ELSE
								SET P_BET = CONCAT(" AND TRUE");
						END CASE;
						SET @SQL_EXC = "";
						SET @SQL_CON = "";
						CASE UPPER(var_column)
						WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_ref LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%') ", P_BET);
						WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%",p1,"%'", P_BET);
						WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'", P_BET);
						ELSE SET @SQL_CON = CONCAT("T.var_id 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_dn T WHERE T.var_status = 'Posted' AND T.var_venid = P3 ORDER BY T.var_id) T WHERE T.var_id = P1;
						ELSE
								SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_venid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Post Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ap_dn T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id");
								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_ap_dn T WHERE T.var_status = 'Posted' AND T.var_venid = P3 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," WHERE T.var_status = 'Posted' AND T.var_venid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_venid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_venid = '", P3,"' ORDER BY T.var_id 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_ap_dn T"," WHERE (",@SQL_CON,") AND T.var_status = 'Posted' AND T.var_venid = '", P3,"'");
													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,") AND T.var_status = 'Posted' AND T.var_venid = '", P3,"' 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;
		WHEN "AP-PY" THEN
				BEGIN
						DECLARE int_index INTEGER DEFAULT 0;
						DECLARE int_page INTEGER DEFAULT 0;
						DECLARE P_BET VARCHAR(250) DEFAULT "";
						CASE var_range
						WHEN "RANGE" THEN
								SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
						WHEN "TODAY" THEN
								SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
						WHEN "THIS PERIOD" THEN
								SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
						WHEN "THIS YEAR" THEN
								SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
						ELSE
								SET P_BET = CONCAT(" AND TRUE");
						END CASE;
						SET @SQL_EXC = "";
						SET @SQL_CON = "";
						CASE UPPER(var_column)
						WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_ref LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%') ", P_BET);
						WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%",p1,"%'", P_BET);
						WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'", P_BET);
						ELSE SET @SQL_CON = CONCAT("T.var_id 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_py T WHERE T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_venid = P3 ORDER BY T.var_id) T WHERE T.var_id = P1;
						ELSE
								SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_venid AS 'Vendor Id', T1.var_name AS 'Vendor Name', T.var_status AS 'Post Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ap_py T LEFT JOIN ame_mt_ven T1 ON T.var_venid = T1.var_id");
								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_ap_py T WHERE T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_venid = P3 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," WHERE T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_venid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_venid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_venid = '", P3,"' ORDER BY T.var_id 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_ap_py T"," WHERE (",@SQL_CON,") AND T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_venid = '", P3,"'");
													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,") AND T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_venid = '", P3,"' 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;
		WHEN "AR-RET" THEN
				BEGIN
						DECLARE int_index INTEGER DEFAULT 0;
						DECLARE int_page INTEGER DEFAULT 0;
						DECLARE P_BET VARCHAR(250) DEFAULT "";
						CASE var_range
						WHEN "RANGE" THEN
								SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
						WHEN "TODAY" THEN
								SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
						WHEN "THIS PERIOD" THEN
								SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
						WHEN "THIS YEAR" THEN
								SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
						ELSE
								SET P_BET = CONCAT(" AND TRUE");
						END CASE;
						SET @SQL_EXC = "";
						SET @SQL_CON = "";
						CASE UPPER(var_column)
						WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_ref LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%') ", P_BET);
						WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%",p1,"%'", P_BET);
						WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'", P_BET);
						ELSE SET @SQL_CON = CONCAT("T.var_id 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_ar_ret T WHERE T.var_status = 'Posted' AND T.var_cusid = P3 ORDER BY T.var_id) T WHERE T.var_id = P1;
						ELSE
								SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_status AS 'Post Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ar_ret T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id");
								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_ar_ret T WHERE T.var_status = 'Posted' AND T.var_cusid = P3 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," WHERE T.var_status = 'Posted' AND T.var_cusid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_cusid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_cusid = '", P3,"' ORDER BY T.var_id 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_ar_ret T"," WHERE (",@SQL_CON,") AND T.var_status = 'Posted' AND T.var_cusid = '", P3,"'");
													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,") AND T.var_status = 'Posted' AND T.var_cusid = '", P3,"' 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;
		WHEN "AR-CN" THEN
				BEGIN
						DECLARE int_index INTEGER DEFAULT 0;
						DECLARE int_page INTEGER DEFAULT 0;
						DECLARE P_BET VARCHAR(250) DEFAULT "";
						CASE var_range
						WHEN "RANGE" THEN
								SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
						WHEN "TODAY" THEN
								SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
						WHEN "THIS PERIOD" THEN
								SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
						WHEN "THIS YEAR" THEN
								SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
						ELSE
								SET P_BET = CONCAT(" AND TRUE");
						END CASE;
						SET @SQL_EXC = "";
						SET @SQL_CON = "";
						CASE UPPER(var_column)
						WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_ref LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%') ", P_BET);
						WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%",p1,"%'", P_BET);
						WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'", P_BET);
						ELSE SET @SQL_CON = CONCAT("T.var_id 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_ar_cn T WHERE T.var_status = 'Posted' AND T.var_cusid = P3 ORDER BY T.var_id) T WHERE T.var_id = P1;
						ELSE
								SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_status AS 'Post Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ar_cn T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id");
								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_ar_cn T WHERE T.var_status = 'Posted' AND T.var_cusid = P3 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," WHERE T.var_status = 'Posted' AND T.var_cusid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_cusid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_cusid = '", P3,"' ORDER BY T.var_id 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_ar_cn T"," WHERE (",@SQL_CON,") AND T.var_status = 'Posted' AND T.var_cusid = '", P3,"'");
													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,") AND T.var_status = 'Posted' AND T.var_cusid = '", P3,"' 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;
		WHEN "AR-PY" THEN
				BEGIN
						DECLARE int_index INTEGER DEFAULT 0;
						DECLARE int_page INTEGER DEFAULT 0;
						DECLARE P_BET VARCHAR(250) DEFAULT "";
						CASE var_range
						WHEN "RANGE" THEN
								SET P_BET = CONCAT(" AND (DATE(T.dat_date) BETWEEN '", dat_df,"' AND '", dat_dt,"')");
						WHEN "TODAY" THEN
								SET P_BET = CONCAT(" AND DATE(T.dat_date) = DATE('", NOW(), "')");
						WHEN "THIS PERIOD" THEN
								SET P_BET = CONCAT(" AND MONTH(T.dat_date) = MONTH('", NOW(), "')");
						WHEN "THIS YEAR" THEN
								SET P_BET = CONCAT(" AND YEAR(T.dat_date) = YEAR('", NOW(), "')");
						ELSE
								SET P_BET = CONCAT(" AND TRUE");
						END CASE;
						SET @SQL_EXC = "";
						SET @SQL_CON = "";
						CASE UPPER(var_column)
						WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_ref LIKE '%",p1,"%' OR T.var_desc LIKE '%",p1,"%') ", P_BET);
						WHEN "REFERENCE" THEN SET @SQL_CON = CONCAT("T.var_ref LIKE '%",p1,"%'", P_BET);
						WHEN "REMARK" THEN SET @SQL_CON = CONCAT("T.var_desc LIKE '%",p1,"%'", P_BET);
						ELSE SET @SQL_CON = CONCAT("T.var_id 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_ar_py T WHERE T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_cusid = P3 ORDER BY T.var_id) T WHERE T.var_id = P1;
						ELSE
								SET @SQL_SEL = CONCAT("SELECT T.var_id AS 'Entry No', T.var_type AS 'Type', DATE(T.dat_date) AS 'Date', COALESCE(T.var_ref, '') AS 'Reference', T.var_cusid AS 'Customer Id', T1.var_name AS 'Customer Name', T.var_status AS 'Post Status', T.var_pmtstatus AS 'Payment Status', T.var_desc AS 'Remark', CAST(T.dou_funnet AS DECIMAL(16, ",P_DEC_AMT,")) AS 'Total Amount' FROM ame_ar_py T LEFT JOIN ame_mt_cus T1 ON T.var_cusid = T1.var_id");
								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_ar_py T WHERE T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_cusid = P3 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," WHERE T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_cusid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_cusid = '", P3,"' ORDER BY T.var_id 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,") AND T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_cusid = '", P3,"' ORDER BY T.var_id 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_ar_py T"," WHERE (",@SQL_CON,") AND T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_cusid = '", P3,"'");
													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,") AND T.var_status = 'Posted' AND T.var_type = 'Prepayment' AND T.var_cusid = '", P3,"' 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;
		END CASE;
		# P4 = TYPE, P5 = PERSON ID
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_DIALOG_SL_IC_BATCH`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_SL_IC_BATCH`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), P1 varchar(200), P2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.int_batchno LIKE '%", P1,"%' OR T.var_locid LIKE '%",p1,"%')");
		WHEN "LOCATION ID" THEN SET @SQL_CON = CONCAT("T.var_locid LIKE '%", P1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.int_batchno LIKE '%", P1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SELECT COALESCE(T.R, 0) FROM (SELECT int_batchno, @R := @R + 1 AS R FROM ame_sl_ic_batch WHERE var_itemid = P2 AND dou_qty > 0 ORDER BY int_batchno) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_batchno = P1;
		ELSE
				SET @SQL_SEL = "SELECT COALESCE(T.int_batchno, '') AS 'Batch No', COALESCE(T.var_locid, '') AS 'Location Id', COALESCE(T.dou_drqty, 0) AS `Quantity In`, COALESCE(T.dou_druc, 0) AS `Cost In`, COALESCE(T.dou_crqty, 0) AS `Quantity Out`, COALESCE(T.dou_cruc, 0) AS `Cost Out`, COALESCE(T.dou_qty, 0) AS `Quantity Balance`   FROM ame_sl_ic_batch T";
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT int_batchno, @R := @R + 1 AS R FROM ame_sl_ic_batch WHERE var_itemid = P2 AND dou_qty > 0 ORDER BY int_batchno) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_batchno  = 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," WHERE T.var_itemid = '", P2,"' AND T.dou_qty > 0 ORDER BY T.int_batchno LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE T.var_itemid = '", P2,"' AND T.dou_qty > 0 AND (",@SQL_CON,") ORDER BY T.int_batchno 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 T.var_itemid = '", P2,"' AND T.dou_qty > 0 AND (",@SQL_CON,") ORDER BY T.int_batchno 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.int_batchno) / 100 INTO @PAGE FROM ame_sl_ic_batch T"," WHERE T.var_itemid = '", P2,"' AND T.dou_qty > 0 AND (",@SQL_CON,") ORDER BY T.int_batchno");
								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 T.var_itemid = '", P2,"' AND (",@SQL_CON,") ORDER BY T.int_batchno 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_DIALOG_SL_IC_BATCH_MT_ITE_PRI`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_SL_IC_BATCH_MT_ITE_PRI`(int_offset int(11),tin_count tinyint(1),var_column varchar(200), P1 varchar(200), P2 varchar(200))
    DETERMINISTIC
BEGIN
		DECLARE int_index INTEGER DEFAULT 0;
		DECLARE int_page INTEGER DEFAULT 0;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.int_batchno LIKE '%",P1,"%' OR T.var_locid LIKE '%",p1,"%')");
		WHEN "LOCATION ID" THEN SET @SQL_CON = CONCAT("T.var_locid LIKE '%", P1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.int_batchno LIKE '%", P1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SELECT COALESCE(T.R, 0) FROM (SELECT int_batchno, var_locid, @R := @R + 1 AS R FROM (SELECT DISTINCTROW int_batchno, var_locid FROM ame_sl_ic_batch WHERE dou_qty > 0 ORDER BY int_batchno, var_locid) AS T) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_batchno = P1 AND T.var_locid = P2;
		ELSE
				SET @SQL_SEL = "SELECT COALESCE(T.int_batchno, '') AS 'Batch No', COALESCE(T.var_locid, '') AS 'Location Id' FROM ame_sl_ic_batch T";
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT int_batchno, var_locid, @R := @R + 1 AS R FROM (SELECT DISTINCTROW int_batchno, var_locid FROM ame_sl_ic_batch WHERE dou_qty > 0 ORDER BY int_batchno, var_locid) AS T) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.int_batchno = P1 AND T.var_locid = P2;
						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," WHERE T.dou_qty > 0 GROUP BY T.int_batchno, T.var_locid ORDER BY T.int_batchno, T.var_locid LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE T.dou_qty > 0 AND (",@SQL_CON,") GROUP BY T.int_batchno, T.var_locid ORDER BY T.int_batchno, T.var_locid 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 T.dou_qty > 0 AND (",@SQL_CON,") GROUP BY T.int_batchno, T.var_locid ORDER BY T.int_batchno, T.var_locid 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.int_batchno) / 100 INTO @PAGE FROM ame_sl_ic_batch T WHERE T.dou_qty > 0 AND (",@SQL_CON,") GROUP BY T.int_batchno, T.var_locid  ORDER BY T.int_batchno, T.var_locid");
								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,") GROUP BY T.int_batchno, T.var_locid ORDER BY T.int_batchno, T.var_locid 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_DIALOG_SYS_ADD_COST`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_DIALOG_SYS_ADD_COST`(int_offset int(11),tin_count tinyint(1),var_column varchar(200),P1 varchar(200),P2 varchar(200))
    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;
		SET @SQL_EXC = "";
		SET @SQL_CON = "";
		IF p2 = "TRUE" THEN
				SET var_inact = " T.tin_inactive = 0 ";SET var_inact_opterator = " T.tin_inactive = 0 AND ";
		ELSE
				SET var_inact = " TRUE ";SET var_inact_opterator = " TRUE AND ";
		END IF;
		CASE UPPER(var_column)
		WHEN "ALL" THEN SET @SQL_CON = CONCAT("(T.var_id LIKE '%",p1,"%' OR T.var_name LIKE '%",p1,"%')");
		WHEN "ADDITIONAL COST NAME" THEN SET @SQL_CON = CONCAT("T.var_name LIKE '%",p1,"%'");
		ELSE SET @SQL_CON = CONCAT("T.var_id LIKE '%",p1,"%'");
		END CASE;
		CASE int_offset
		WHEN -1 THEN
				SELECT COALESCE(T.R, 0) FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_sys_add_cost WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE) ORDER BY var_id) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R WHERE T.var_id = p1;
		ELSE
				SET @SQL_SEL = "SELECT COALESCE(T.var_id, '') AS 'Additional Cost Id', COALESCE(T.var_name, '') AS 'Additional Cost Name', T.var_desc AS `Description`, T.var_header AS `Column Header` FROM ame_sys_add_cost T";
				CASE tin_count
				WHEN 1 THEN # LOAD FORM
						SELECT COALESCE(T.R, 0) INTO int_index FROM (SELECT @R := @R + 1 AS R, var_id FROM ame_sys_add_cost WHERE IF(p2 = 'TRUE', tin_inactive = 0,TRUE) ORDER BY var_id) T LEFT JOIN (SELECT @R := -1 AS R) T1 ON T.R = T1.R 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," WHERE ",var_inact," ORDER BY T.var_id LIMIT 100 OFFSET ",(int_page * 100));
				WHEN 2 THEN # SEARCH
						SELECT 0 AS RowIndex, 1 AS Page;
						SET @SQL_EXC = CONCAT(@SQL_SEL," WHERE ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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 ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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_sys_add_cost T"," WHERE ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id");
								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 ",var_inact_opterator," (",@SQL_CON,") ORDER BY T.var_id 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_EXC_FL_USER_DEL`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_EXC_FL_USER_DEL`(P1 varchar(100), P2 varchar(100))
    DETERMINISTIC
BEGIN
		IF P1 = "USER" THEN
				DELETE FROM ame_user WHERE var_id = P2;
				DELETE FROM ame_user_app WHERE var_id = P2;
				DELETE FROM ame_user_pro_rem WHERE var_id = P2;
				DELETE FROM ame_user_pro_def_pre WHERE var_id = P2;
				DELETE FROM ame_user_role WHERE var_id = P2;
		ELSE
				DELETE FROM ame_sys_role WHERE int_id = P2;
				DELETE FROM ame_user_role WHERE int_id = P2;
				DELETE FROM ame_user_per_frm WHERE int_id = P2;
				DELETE FROM ame_user_per_oth WHERE int_id = P2;
				DELETE FROM ame_user_per_rep WHERE int_id = P2;
		END IF;		
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_EXECUTE`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_EXECUTE`(Str LONGTEXT)
    DETERMINISTIC
BEGIN
	IF Str != "" THEN
		SET @SQL1 = Str;
		PREPARE St FROM @SQL1;
		EXECUTE St;
		DEALLOCATE PREPARE St;
	END IF;
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_EXE_CONFIG`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_EXE_CONFIG`(P1 varchar(250), P2 varchar(250), P3 varchar(250), P4 varchar(250), P5 varchar(250), P6 varchar(250), P7 varchar(250), P8 varchar(250), P9 varchar(250), P10 varchar(250), P11 varchar(250))
    DETERMINISTIC
BEGIN
		UPDATE ame_sys_config SET var_name = P1 WHERE var_id = 'IMG_PATH';
		UPDATE ame_sys_config SET var_name = P2 WHERE var_id = 'TAX_BF_DISC';	
		UPDATE ame_sys_config SET var_name = P3 WHERE var_id = 'POST_ONE_TIME';
		UPDATE ame_sys_config SET var_name = P4 WHERE var_id = 'STOCK_NEGATIVE';
		UPDATE ame_sys_config SET var_name = P5 WHERE var_id = 'RECEIVE';
		UPDATE ame_sys_config SET var_name = P6 WHERE var_id = 'ISSUE';
		UPDATE ame_sys_config SET var_name = P7 WHERE var_id = 'CLASS';
		UPDATE ame_sys_config SET var_name = P8 WHERE var_id = 'MULTI_CURRENCY';
		UPDATE ame_sys_config SET var_name = P9 WHERE var_id = 'EXC_SATURDAY';
		UPDATE ame_sys_config SET var_name = P10 WHERE var_id = 'EXC_SUNDAY';
		UPDATE ame_sys_config SET var_name = P11 WHERE var_id = 'EXC_HOLIDAY';
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_EXE_DUP_USER`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_EXE_DUP_USER`(P1 varchar(100), P2 varchar(100))
    DETERMINISTIC
BEGIN
		INSERT INTO ame_user_role(var_id, int_id) (SELECT P2, T1.int_id FROM ame_user_role T1 WHERE T1.var_id = P1);
		INSERT INTO ame_user_pro_rem(var_id, var_modid, int_day, tin_pro1) (SELECT P2, T1.var_modid, T1.int_day, T1.tin_pro1 FROM ame_user_pro_rem T1 WHERE T1.var_id = P1);
		INSERT INTO ame_user_pro_def_pre(var_id, var_modid, var_type) (SELECT P2, T1.var_modid, T1.var_type FROM ame_user_pro_def_pre T1 WHERE T1.var_id = P1);
END |
DELIMITER ;

DROP PROCEDURE IF EXISTS `AME_PRO_IMP_EXP_1`;
DELIMITER |
CREATE PROCEDURE `AME_PRO_IMP_EXP_1`(P1 varchar(100))
    DETERMINISTIC
BEGIN
		CASE P1
		WHEN "MT_HOL" THEN
				SELECT DISTINCT int_id FROM ame_mt_hol;
		WHEN "MT_CUR" THEN
				SELECT DISTINCT var_id FROM ame_mt_cur;
		WHEN "MT_CLA" THEN
				SELECT DISTINCT var_id FROM ame_mt_cla;
		WHEN "MT_LOC" THEN
				SELECT DISTINCT var_id FROM ame_mt_loc;
		WHEN "MT_PRI_COD" THEN
				SELECT DISTINCT var_id FROM ame_mt_pri_cod;
		WHEN "MT_OPT" THEN
				SELECT DISTINCT var_id FROM ame_mt_of;
				SELECT DISTINCT COALESCE(var_name, '') AS var_name FROM ame_mt_of;
		WHEN "MT_UOM" THEN
				SELECT DISTINCT var_id FROM ame_mt_uom;
		WHEN "MT_VEN_GRO" THEN
				SELECT DISTINCT var_id FROM ame_mt_ven_gro;
				SELECT DISTINCT T1.var_id FROM ame_mt_acc T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
		WHEN "MT_CUS_GRO" THEN
				SELECT DISTINCT var_id FROM ame_mt_cus_gro;
				SELECT DISTINCT T1.var_id FROM ame_mt_acc T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
		WHEN "MT_EMP_GRO" THEN
				SELECT DISTINCT var_id FROM ame_mt_emp_gro;						
		WHEN "MT_ITE_GRO" THEN
				SELECT DISTINCT var_id FROM ame_mt_ite_gro;
				SELECT DISTINCT T1.var_id FROM ame_mt_acc T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id, CONCAT("Additional COGS: ", T1.var_name) AS `var_name` FROM ame_sys_add_cost T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
		WHEN "MT_POS" THEN
				SELECT DISTINCT var_id FROM ame_mt_pos;
		WHEN "MT_ACC" THEN
				SELECT DISTINCT T1.var_id FROM ame_mt_acc T1;
				SELECT DISTINCT T1.var_id FROM ame_mt_acc_type T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
		WHEN "MT_PMT_METH" THEN
				SELECT DISTINCT T1.var_id FROM ame_mt_pmt_meth T1;
		WHEN "MT_PMT_TERM" THEN
				SELECT DISTINCT T1.var_id FROM ame_mt_pmt_term T1;
		WHEN "MT_VEN" THEN
				SELECT DISTINCT T1.var_id, T1.var_create, T1.dat_create FROM ame_mt_ven T1;
				SELECT DISTINCT T1.var_type, T1.var_groupid, T1.var_subgroupid FROM ame_mt_acc_set T1 WHERE T1.var_type = "AP" AND T1.var_groupid <> 'DEFAULT' AND T1.var_subgroupid <> 'DEFAULT';
				SELECT DISTINCT T1.var_id FROM ame_mt_cur T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_ven_gro T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_acc T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				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_pmt_meth T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id, T1.int_length, T1.int_lastid FROM ame_sys_prefix T1 WHERE T1.var_modid = 'MT-VEN';
		WHEN "MT_CUS" THEN
				SELECT DISTINCT T1.var_id, T1.var_create, T1.dat_create FROM ame_mt_cus T1;
				SELECT DISTINCT T1.var_type, T1.var_groupid, T1.var_subgroupid FROM ame_mt_acc_set T1 WHERE T1.var_type = "AR" AND T1.var_groupid <> 'DEFAULT' AND T1.var_subgroupid <> 'DEFAULT';
				SELECT DISTINCT T1.var_id FROM ame_mt_cur T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_cus_gro T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_acc T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				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_pmt_meth 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;
				SELECT DISTINCT T1.var_id, T1.int_length, T1.int_lastid FROM ame_sys_prefix T1 WHERE T1.var_modid = 'MT-CUS';
		WHEN "MT_EMP" THEN
				SELECT DISTINCT T1.var_id, T1.var_create, T1.dat_create FROM ame_mt_emp T1;
				SELECT DISTINCT T1.var_id FROM ame_mt_cur T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_emp_gro T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_pos T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id, T1.int_length, T1.int_lastid FROM ame_sys_prefix T1 WHERE T1.var_modid = 'MT-EMP';
		WHEN "MT_ITE_IMP" THEN
				SELECT DISTINCT T1.var_id, COALESCE(T1.var_itemcode, '') AS `var_itemcode`, COALESCE(T1.var_barcode, '') AS `var_barcode`, T1.var_create, T1.dat_create FROM ame_mt_ite T1;
				SELECT DISTINCT T1.var_id, T1.tin_pur, T1.tin_sal 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);
				SELECT DISTINCT T1.var_type, T1.var_groupid, T1.var_subgroupid 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';
				SELECT DISTINCT T1.var_id FROM ame_mt_uom T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_ite_gro T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_acc T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_ven T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id, CONCAT("Additional COGS: ", T1.var_name) AS `var_name` FROM ame_sys_add_cost T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id, T1.int_length, T1.int_lastid FROM ame_sys_prefix T1 WHERE T1.var_modid = 'MT-ITE';
		WHEN "MT_ITE_IMP_ITE_UOM" THEN
				SELECT DISTINCT T1.var_id, T1.var_uomid FROM ame_mt_ite_uom T1;
				SELECT DISTINCT T1.var_id, T1.var_uomid FROM ame_mt_ite T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_uom T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
		WHEN "MT_ITE_IMP_ITE_ASS" THEN
				SELECT DISTINCT T1.var_id, var_itemid, var_uomid_ass, var_uomid FROM ame_mt_ite_ass T1;
				SELECT DISTINCT T1.var_id, T1.var_type FROM ame_mt_ite T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id, T1.var_uomid FROM ame_mt_ite_uom T1;
		WHEN "MT_ITE_IMP_ITE_PRI" THEN
				SELECT DISTINCT T1.var_id, var_pricecode, var_uomid, var_cur FROM ame_mt_ite_pri T1;
				SELECT DISTINCT T1.var_id, T1.var_type FROM ame_mt_ite T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id, T1.var_uomid FROM ame_mt_ite_uom T1;
				SELECT DISTINCT T1.var_id FROM ame_mt_pri_cod T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				SELECT DISTINCT T1.var_id FROM ame_mt_cur T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
		WHEN "AP_PO" THEN
				SELECT DISTINCT T1.var_id, T1.var_status FROM ame_ap_po T1;
				SELECT DISTINCT T1.var_id, T1.var_cur FROM ame_mt_ven T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				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_acc 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;
				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;
		WHEN "AP_IN" THEN
				SELECT DISTINCT T1.var_id, T1.var_status FROM ame_ap_in T1;
				SELECT DISTINCT T1.var_id, T1.var_cur FROM ame_mt_ven T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				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_acc 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;
				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;
		WHEN "AP_DN" THEN
				SELECT DISTINCT T1.var_id, T1.var_status FROM ame_ap_dn T1;
				SELECT DISTINCT T1.var_id, T1.var_cur FROM ame_mt_ven T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				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_acc 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;
				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;
		WHEN "AR_QUO" THEN
				SELECT DISTINCT T1.var_id, T1.var_status FROM ame_ar_quo T1;
				SELECT DISTINCT T1.var_id, T1.var_cur FROM ame_mt_cus T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				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_acc 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;
				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 "AR_SO" THEN
				SELECT DISTINCT T1.var_id, T1.var_status FROM ame_ar_so T1;
				SELECT DISTINCT T1.var_id, T1.var_cur FROM ame_mt_cus T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				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_acc 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;
				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 "AR_IN" THEN
				SELECT DISTINCT T1.var_id, T1.var_status FROM ame_ar_in T1;
				SELECT DISTINCT T1.var_id, T1.var_cur FROM ame_mt_cus T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				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_acc 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;
				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 "AR_CN" THEN
				SELECT DISTINCT T1.var_id, T1.var_status FROM ame_ar_cn T1;
				SELECT DISTINCT T1.var_id, T1.var_cur FROM ame_mt_cus T1 WHERE COALESCE(T1.tin_inactive, 0) = 0;
				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_acc 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;
