CREATE OR REPLACE PROCEDURE AGENDAJOBMOVIEST
IS
  V_COMANDO             VARCHAR2(5000);
  V_NRMESANTEMOVIEST    NUMBER;
  V_DTMOVIESTINI        DATE;
  V_DTMOVIESTFIN        DATE;
  V_HRAGEMOVIEST        VARCHAR2(5);
BEGIN
    BEGIN
        DBMS_SCHEDULER.DROP_JOB('JOB_PREGERAR_MOVIEST');
    EXCEPTION
    WHEN OTHERS THEN
        NULL;
    END;

    BEGIN
        SELECT NRMESANTEMOVIEST, DTMOVIESTINI, DTMOVIESTFIN, HRAGEMOVIEST
        INTO V_NRMESANTEMOVIEST, V_DTMOVIESTINI, V_DTMOVIESTFIN , V_HRAGEMOVIEST
        FROM PARAMESTOQUE;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_NRMESANTEMOVIEST  := NULL;
        V_DTMOVIESTINI      := NULL;
        V_DTMOVIESTFIN      := NULL;
        V_HRAGEMOVIEST      := NULL;
        GOTO PROCEDURE_END;
      WHEN OTHERS THEN
        GOTO PROCEDURE_END;
    END;

    V_HRAGEMOVIEST := NVL(V_HRAGEMOVIEST, '00:00');

    IF((V_NRMESANTEMOVIEST IS NULL) AND (V_DTMOVIESTINI IS NULL) AND (V_DTMOVIESTFIN IS NULL)) THEN
        GOTO PROCEDURE_END;
    END IF;

    V_COMANDO := 'BEGIN MOVIMENTACAO_ESTOQUE_ANALI(); END;';

    BEGIN
        DBMS_SCHEDULER.CREATE_JOB (
            JOB_NAME => 'JOB_PREGERAR_MOVIEST',
            JOB_TYPE => 'PLSQL_BLOCK',
            JOB_ACTION => V_COMANDO,
            START_DATE => TO_DATE(TO_CHAR(TRUNC(SYSDATE), 'DD/MM/YY')||' '||V_HRAGEMOVIEST, 'DD/MM/YY HH24:MI'),
            REPEAT_INTERVAL      => 'FREQ=DAILY',
            ENABLED => TRUE);
    EXCEPTION
      WHEN OTHERS THEN
            NULL;
    END;

    <<PROCEDURE_END>>
    COMMIT;
END;
/