BEGIN
   DECLARE
      V_COUNT NUMBER;
   BEGIN
      V_COUNT := 0;
      BEGIN
         SELECT COUNT(*)
           INTO V_COUNT
           FROM USER_SCHEDULER_JOBS
          WHERE JOB_NAME = 'JOB_INUTILIZACAO_NFE';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_COUNT := 0;
      END;
      
      IF V_COUNT = 0 THEN
         DBMS_SCHEDULER.CREATE_JOB (
           JOB_NAME        => 'JOB_INUTILIZACAO_NFE',
           JOB_TYPE        => 'PLSQL_BLOCK',
           JOB_ACTION      => 'BEGIN INUTILIZACAO_NFE(NULL, NULL, NULL, NULL, NULL, NULL, NULL, ''S''); COMMIT; END;',
           START_DATE      => SYSTIMESTAMP,
           REPEAT_INTERVAL => 'TRUNC(SYSDATE + 1)',
           JOB_CLASS       => 'DEFAULT_JOB_CLASS',
           ENABLED         => TRUE,
           AUTO_DROP       => FALSE,
           COMMENTS        => 'Job criado para inutilizar a nota fiscal eletronica.');
      ELSE
         DBMS_SCHEDULER.DROP_JOB(JOB_NAME => 'JOB_INUTILIZACAO_NFE');
         
         DBMS_SCHEDULER.CREATE_JOB (
           JOB_NAME        => 'JOB_INUTILIZACAO_NFE',
           JOB_TYPE        => 'PLSQL_BLOCK',
           JOB_ACTION      => 'BEGIN INUTILIZACAO_NFE(NULL, NULL, NULL, NULL, NULL, NULL, NULL, ''S''); COMMIT; END;',
           START_DATE      => SYSTIMESTAMP,
           REPEAT_INTERVAL => 'TRUNC(SYSDATE + 1)',
           JOB_CLASS       => 'DEFAULT_JOB_CLASS',
           ENABLED         => TRUE,
           AUTO_DROP       => FALSE,
           COMMENTS        => 'Job criado para inutilizar a nota fiscal eletronica.');
      END IF;
   END;
END;
/