CREATE OR REPLACE PROCEDURE HRIS_DAILY_SYSTEM_CHECK(
    P_DATE DATE:=NULL)
AS
  V_DATE      DATE;
  V_MONTH_ID  NUMBER(7,0);
  V_FROM_DATE DATE;
  V_TO_DATE   DATE;
  V_YEAR      NUMBER;
  V_MONTH_NO  NUMBER;
  V_FISCAL_YEAR_ID HRIS_MONTH_CODE.FISCAL_YEAR_ID%TYPE;
  V_FISCAL_YEAR_MONTH_NO HRIS_MONTH_CODE.FISCAL_YEAR_MONTH_NO%TYPE;
  IS_START_OF_MONTH         CHAR(1 BYTE);
  IS_TWO_DAY_BEFORE_MTH_END CHAR(1 BYTE);
  IS_END_OF_MONTH           CHAR(1 BYTE);
  V_DEDUCTION_RATE FLOAT:=0.5;
BEGIN
  --SET DATE
  IF P_DATE IS NULL THEN
    V_DATE  :=TRUNC(SYSDATE);
  ELSE
    V_DATE :=TRUNC(P_DATE);
  END IF;
  --
  SELECT MONTH_ID,
    FROM_DATE,
    TO_DATE,
    YEAR,
    MONTH_NO,
    FISCAL_YEAR_ID,
    FISCAL_YEAR_MONTH_NO
  INTO V_MONTH_ID,
    V_FROM_DATE,
    V_TO_DATE,
    V_YEAR,
    V_MONTH_NO,
    V_FISCAL_YEAR_ID,
    V_FISCAL_YEAR_MONTH_NO
  FROM HRIS_MONTH_CODE
  WHERE V_DATE BETWEEN TRUNC(FROM_DATE) AND TRUNC(TO_DATE);
  --
  SELECT (
    CASE
      WHEN V_DATE = TRUNC(V_FROM_DATE)
      THEN 'Y'
      ELSE 'N'
    END),
    (
    CASE
      WHEN TRUNC(V_TO_DATE-2) = V_DATE
      THEN 'Y'
      ELSE 'N'
    END),
    (
    CASE
      WHEN V_DATE = TRUNC(V_TO_DATE)
      THEN 'Y'
      ELSE 'N'
    END)
  INTO IS_START_OF_MONTH,
    IS_TWO_DAY_BEFORE_MTH_END,
    IS_END_OF_MONTH
  FROM DUAL;
  --SERVICE STATUS UPDATE
  FOR service IN
  (SELECT * FROM HRIS_JOB_HISTORY WHERE EVENT_DATE =V_DATE AND STATUS = 'E'
  )
  LOOP
    HRIS_UPDATE_EMPLOYEE_SERVICE(service.JOB_HISTORY_ID);
  END LOOP;
  --END OF SERVICE STATUS UPDATE
  --  NOTIFY PENALTY
  IF IS_TWO_DAY_BEFORE_MTH_END ='Y' THEN
    FOR penalty IN
    (SELECT DISTINCT A.EMPLOYEE_ID
    FROM HRIS_ATTENDANCE_DETAIL A
    WHERE A.OVERALL_STATUS IN ('LA','BA')
    AND (A.ATTENDANCE_DT BETWEEN V_FROM_DATE AND V_TO_DATE )
    )
    LOOP
      HRIS_SYSTEM_NOTIFICATION(penalty.EMPLOYEE_ID,SYSDATE,'Late Penalty','Please verify your attendance.','{"route":"penalty","action":"self","id":"'||V_MONTH_ID||'"}');
    END LOOP;
  END IF;
  IF IS_END_OF_MONTH ='Y' THEN
    NULL;
    --HRIS_LATE_LEAVE_DEDUCTION
  END IF;
  --  END OF NOTIFY PENALTY
END;
