CREATE OR REPLACE PROCEDURE DASHBOARD_PROCEDURES (
    P_DTFINAL IN DATE DEFAULT SYSDATE
)
AS
   V_IDUTILIZAFIN VARCHAR2(01);
BEGIN
    DASHBOARD_FIS_COUNT;

    BEGIN
       SELECT MAX(ID)
         INTO V_IDUTILIZAFIN
         FROM (SELECT 'S' AS ID
                 FROM GERAL_REC
                UNION ALL
               SELECT 'S' AS ID
                 FROM GERAL_PAG);
    EXCEPTION
       WHEN NO_DATA_FOUND THEN
          V_IDUTILIZAFIN := 'N';
    END;

    IF NVL(V_IDUTILIZAFIN,'N') = 'S' THEN

       DASHBOARD_FIN_SALDO_CONTAS(P_DTFINAL);

       DASHBOARD_FIN_TITULOS_REC;

       DASHBOARD_FIN_TITULOS_PAG;

       DASHBOARD_FIN_DIVISAO_RECEITAS;

       DASHBOARD_FIN_DIVISAO_DESPESAS;

       DASHBOARD_FIN_CLI_INAD;

       DASHBOARD_FIN_COUNT;

       DASHBOARD_FIN_FLUXO_CAIXA;

    END IF;
END;
/
BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
   job_name           =>  'JOB_DASHBOARD_ERP',
   job_type           =>  'PLSQL_BLOCK',
   job_action         =>  'BEGIN DASHBOARD_PROCEDURES(); END;',
   start_date         =>  SYSDATE,
   repeat_interval    =>  'FREQ=HOURLY; BYHOUR=0,4,8,12,16,20; BYMINUTE=0; BYSECOND=0;',
   end_date           =>   NULL,
   auto_drop          =>   FALSE,
   job_class          =>  'DEFAULT_JOB_CLASS',
   ENABLED            =>   TRUE,
   comments           =>  'JOB PARA DASHBOARD DO ERP');
END;
/

BEGIN
   dbms_scheduler.run_job('JOB_DASHBOARD_ERP');
END;
/