CREATE OR REPLACE PROCEDURE HRIS_GL_ENTRY_PROC(
P_FINANCE_DATA_ID NUMBER
)
AS
V_GL_ENTRY_ID NUMBER;
V_EMPLOYEE_ID NUMBER;
V_EMPLOYEE_CODE NUMBER;
V_BRANCH_ID NUMBER;
V_CREATED_BY NUMBER;
V_CREATED_DATE DATE;
V_REQUESTED_AMOUNT NUMBER;
V_DEPARTMENT_ID NUMBER;
V_LOCATION_ID NUMBER;
V_MODULE_CODE VARCHAR(10);
V_REQUEST_ID NUMBER;
V_MASTER_ID NUMBER;
V_DR_ACC_CODE VARCHAR(20) DEFAULT NULL;
V_CR_ACC_CODE VARCHAR(20) DEFAULT NULL;
V_VOUCHER_NO VARCHAR(10) DEFAULT NULL;
BEGIN

SELECT IFNULL(MAX(GL_ENTRY_ID)+1, 1) INTO V_GL_ENTRY_ID FROM HRIS_GL_ENTRY;
SELECT MODULE_CODE, REQUEST_ID, MASTER_ID
INTO V_MODULE_CODE, V_REQUEST_ID, V_MASTER_ID 
FROM HRIS_FINANCE_DATA WHERE FINANCE_DATA_ID = P_FINANCE_DATA_ID;

--FOR LOAN
IF V_MODULE_CODE = 'LN' THEN 
SELECT EMPLOYEE_ID, APPROVED_BY, V_CREATED_DATE, REQUESTED_AMOUNT INTO 
V_EMPLOYEE_ID, V_CREATED_BY, V_CREATED_DATE, V_REQUESTED_AMOUNT
FROM HRIS_EMPLOYEE_LOAN_REQUEST WHERE LOAN_REQUEST_ID = V_REQUEST_ID;
SELECT DR_ACC_CODE, CR_ACC_CODE INTO V_DR_ACC_CODE, V_CR_ACC_CODE
FROM HRIS_LOAN_MASTER_SETUP WHERE LOAN_ID = (SELECT LOAN_ID FROM HRIS_EMPLOYEE_LOAN_REQUEST
WHERE LOAN_REQUEST_ID = V_REQUEST_ID);
--FOR TRAVEL
ELSEIF V_MODULE_CODE = 'TV' THEN
SELECT EMPLOYEE_ID, APPROVED_BY, APPROVED_DATE, REQUESTED_AMOUNT INTO 
V_EMPLOYEE_ID, V_CREATED_BY, V_CREATED_DATE, V_REQUESTED_AMOUNT
FROM HRIS_EMPLOYEE_TRAVEL_REQUEST WHERE TRAVEL_ID = V_REQUEST_ID;
END IF;

SELECT EMPLOYEE_CODE, BRANCH_ID, DEPARTMENT_ID, LOCATION_ID INTO 
V_EMPLOYEE_CODE, V_BRANCH_ID, V_DEPARTMENT_ID, V_LOCATION_ID
FROM HRIS_EMPLOYEES WHERE EMPLOYEE_ID = V_EMPLOYEE_ID;

--GENERATE VOUCHER NO
V_VOUCHER_NO = V_MODULE_CODE || IFNULL('/'||V_MASTER_ID, '') || IFNULL('/'||V_BRANCH_ID, '') 
				|| IFNULL('/'||V_LOCATION_ID, '') || IFNULL('/'||P_FINANCE_DATA_ID, '');

INSERT INTO HRIS_GL_ENTRY(
GL_ENTRY_ID,
FINANCE_DATA_ID,
EMPLOYEE_ID,
EMPLOYEE_CODE,
VOUCHER_NO,
CREATED_DATE,
CREATED_BY,
AMOUNT,
DR_ACC_CODE,
CR_ACC_CODE,
BRANCH_ID,
CC1,
CC2,
CC3,
CC4,
SYNC,
SYNCED_DATE
) VALUES(
V_GL_ENTRY_ID,
P_FINANCE_DATA_ID,
V_EMPLOYEE_ID,
V_EMPLOYEE_CODE,
V_VOUCHER_NO,
CURRENT_DATE,
V_CREATED_BY,
V_REQUESTED_AMOUNT,
V_DR_ACC_CODE,
V_CR_ACC_CODE,
V_BRANCH_ID,
V_LOCATION_ID,
V_DEPARTMENT_ID,
'NA',
'NA',
'N',
null
);
END;
