CREATE OR REPLACE PROCEDURE LIMPEZA_NOTAFISCAL(
   P_NRLISTA       IN VARCHAR2,
   P_DTFINAL       IN DATE,
   P_DSSISTLIMPEZ  IN VARCHAR2,
   P_NMOPERLIMPEZ  IN VARCHAR2,
   P_IDSISTEMA     IN VARCHAR2)

AS
   V_VALOR    VARCHAR2(10);
   V_STATUS   VARCHAR2(01);
   V_CDFILIAL VARCHAR2(04);
   V_PASSO             NUMBER := 0;
   V_ERROR             VARCHAR2(2000);
   V_NRORDEM           VARCHAR2(03);
   V_DSIP              VARCHAR2(50);
   V_DSUSUARIO         VARCHAR2(50);
   V_DSRDM             VARCHAR2(50);
   V_DSMETODO          VARCHAR2(250);
   V_DSOPERADOR        VARCHAR2(12);
   V_EXCLUNOTAFISCAL   NUMBER := 0;
   V_COUNT_COMMIT      NUMBER := 1;
   V_NUM_TO_COMMIT     NUMBER := 250; -- NUMERO UTILIZADO PARA REGULARIZAR A QUANTIDADE DE DMLS PARA O COMMIT.

   V_DTHRINILIMPEZA  DATE;
   V_DTHRFINLIMPEZA  DATE;
   V_DTINICIAL       DATE;

   V_IDSTATUSLIMPEZA   VARCHAR2(01);

   CURSOR C_LISTA IS (SELECT CDFILIAL
                      FROM TRABLIMPFILIAL
                      WHERE IDLIMPEZA = P_IDSISTEMA);

   PROCEDURE VerifTimeOut(DTLIMITE IN DATE)
   IS
   BEGIN
      IF SYSDATE > DTLIMITE THEN
         COMMIT;
         RAISE_APPLICATION_ERROR(-20001,'TIMEOUT - EXCECAO PROPOSITAL TEKNISA');
      END IF;
   END;
BEGIN
   V_DTINICIAL := TO_DATE('01/01/1990');
   
   --PEGA A DATA INICIAL E FINAL DA EXECUCAO
   SELECT TO_DATE(TO_CHAR(SYSDATE,'DDMMYYYY') || HRINILIMPEZA||'00','DDMMYYYYHH24MISS'),
          TO_DATE(TO_CHAR(SYSDATE,'DDMMYYYY') || HRFINLIMPEZA||'00','DDMMYYYYHH24MISS')
     INTO V_DTHRINILIMPEZA, V_DTHRFINLIMPEZA
     FROM TRABLIMPEZA
    WHERE IDLIMPEZA = P_IDSISTEMA;

   IF V_DTHRFINLIMPEZA <= SYSDATE THEN
      V_DTHRINILIMPEZA := V_DTHRINILIMPEZA + 1;
      V_DTHRFINLIMPEZA := V_DTHRFINLIMPEZA + 1;
   END IF;

   IF (SYSDATE >= V_DTHRINILIMPEZA) AND (SYSDATE <= V_DTHRFINLIMPEZA) THEN

      V_PASSO:= 1;
      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_NOTAFISCAL DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_ITEMPRODUTO DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_EST_ITLANCTOEST   DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      V_PASSO:= 2;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER NF_BAIXA_SAIDA DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 3;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER BLOQ_DEL_NFE DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 4;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER GRAVA_CANCELAMENTO_EXP DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 5;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_NOTADESTINADA DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;   
      V_PASSO:= 6;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER DTULTATU_BEMATIVO DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 7;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER GERA_HISTORICO_BEM DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 8;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_MOVCAIXA DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 9;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_DEVOLUCAOPAG DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 10;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER ATUALIZA_VRDEVOLUCAO DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 11;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER ATUALIZA_VRDEVOLUCAOREC DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 12;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_RELACMAPANF DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 13;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_ITMAPARECUP DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 14;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_EXP_MAPACOMEXP DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 15;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_EXP_MAPAPRXCOMEXP DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 16;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER GRAVA_ATUA_CONTCTBL_LANCCUSTO DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 17;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_CTB_LANCCONT DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 18;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_CTB_LANCCONT DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 19;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_REDUCAOZ DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 20;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER USR_EXCLUSAO_EXPORTAREDZ DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER NF_BAIXA_SAIDA DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      V_PASSO:= 21;

      WEBTUNNING(V_DSIP, V_DSUSUARIO, V_DSRDM, V_DSMETODO, V_DSOPERADOR);
      V_PASSO:= 22;

      V_NRORDEM := '0';
      V_PASSO:= 23;

      SELECT MAX(IDSTATUSLIMPEZA)
        INTO V_IDSTATUSLIMPEZA
        FROM TRABLIMPEZA
       WHERE IDLIMPEZA = P_IDSISTEMA;

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         DELETE FROM TRABPROCLIMPEZA
         WHERE IDLIMPEZA = P_IDSISTEMA;
      ELSE
         SELECT MAX(QTREGEXCLIMPEZA)
           INTO V_EXCLUNOTAFISCAL
           FROM TRABPROCLIMPEZA
          WHERE IDLIMPEZA = P_IDSISTEMA
            AND NMTABELALIMP = 'NOTAFISCAL';
      END IF;

      V_PASSO:= 24;

      UPDATE TRABLIMPEZA
      SET IDSTATUSLIMPEZA = 'A'
      WHERE IDLIMPEZA = P_IDSISTEMA;
      V_PASSO:= 25;

      COMMIT;

      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

      UPDATE TABLIMPEZA
         SET NRORDEMLIMPEZA = V_NRORDEM
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'NOTAFISCAL';
      V_PASSO:= 27;

      IF NOT(SQL%ROWCOUNT > 0) THEN
          INSERT INTO TABLIMPEZA (IDLIMPEZA, NMTABELALIMP, NRORDEMLIMPEZA)
          VALUES
          (P_IDSISTEMA, 'NOTAFISCAL', V_NRORDEM);
      END IF;
      V_PASSO:= 28;

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP, IDSITUPROCLIMP)
         VALUES
             (P_IDSISTEMA, 'NOTAFISCAL', 'A');
      END IF;
             
      V_PASSO:= 29;
   
      COMMIT;

      /*Verifica se a procedure estourou o tempo limite*/
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_STATUS := 'S';

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;

         FOR C_NOTAFISCAL IN (SELECT ROWID, NOTAFISCAL.CDFILIAL, NOTAFISCAL.NRLANCTONF, NOTAFISCAL.IDENTRSAIDOP, NOTAFISCAL.NRACESSONFE
                              FROM NOTAFISCAL
                              WHERE NOTAFISCAL.CDFILIRECE  = V_CDFILIAL AND
                                    NOTAFISCAL.IDENTRSAIDOP = 'E'       AND
                                    NOTAFISCAL.DTENTRSAID BETWEEN V_DTINICIAL AND P_DTFINAL
                              UNION ALL
                              SELECT ROWID, NOTAFISCAL.CDFILIAL, NOTAFISCAL.NRLANCTONF, NOTAFISCAL.IDENTRSAIDOP, NOTAFISCAL.NRACESSONFE
                              FROM NOTAFISCAL
                              WHERE NOTAFISCAL.CDFILIEMIT  = V_CDFILIAL AND
                                    NOTAFISCAL.IDENTRSAIDOP = 'S'       AND
                                    NOTAFISCAL.DTENTRSAID BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            V_PASSO:= 30;

            V_STATUS := 'S';

            IF V_STATUS = 'S' THEN
               SELECT MAX(L.CDFILIAL)
               INTO V_VALOR
               FROM LANCTOESTOQ L
               WHERE L.CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     L.NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;

               IF V_VALOR IS NOT NULL THEN
                  V_STATUS := 'N';
               END IF;
            END IF;
            V_PASSO:= 31;

            IF V_STATUS = 'S' THEN
               SELECT MAX(L.CDFILIAL)
               INTO V_VALOR
               FROM MAPARESUMECF L
               WHERE L.CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     L.NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;

               IF V_VALOR IS NOT NULL THEN
                  V_STATUS := 'N';
               END IF;
            END IF;
            V_PASSO:= 32;

            IF V_STATUS = 'S' THEN
               SELECT MAX(L.CDFILIAL)
               INTO V_VALOR
               FROM INVOICE L
               WHERE L.CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     L.NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;

               IF V_VALOR IS NOT NULL THEN
                  V_STATUS := 'N';
               END IF;

            END IF;
            V_PASSO:= 33;

            IF V_STATUS = 'S' THEN
               SELECT MAX(C.CDFILIAL)
               INTO V_VALOR
               FROM CONTARECEBER C, BOLETRECEBER B
               WHERE C.CDFILIAL      = C_NOTAFISCAL.CDFILIAL    AND
                     C.NRLANCTONF    = C_NOTAFISCAL.NRLANCTONF  AND
                     C.CDFILIBOLREC  = B.CDFILIAL               AND
                     C.NRLANBOLREC   = B.NRLANBOLREC            AND
                     NOT EXISTS (SELECT CDFILIAL
                                 FROM BAIXARECEBIDA I
                                 WHERE I.CDFILIAL   = B.CDFILIAL AND
                                       i.NRBAIXAREC = B.NRBAIXAREC);

               IF V_VALOR IS NOT NULL THEN
                  V_STATUS := 'N';
               END IF;
            END IF;
            V_PASSO:= 34;

            IF V_STATUS = 'S' THEN
               SELECT MAX(C.CDFILIAL)
               INTO V_VALOR
               FROM CONTAPAGAR C, BOLETPAGAR B
               WHERE C.CDFILIAL      = C_NOTAFISCAL.CDFILIAL    AND
                     C.NRLANCTONF    = C_NOTAFISCAL.NRLANCTONF  AND
                     C.CDFILIBPAG    = B.CDFILIAL               AND
                     C.NRLANBOLPAG   = B.NRLANBOLPAG            AND
                     NOT EXISTS (SELECT CDFILIAL
                                 FROM BAIXAPAGA I
                                 WHERE I.CDFILIAL   = B.CDFILIAL AND
                                       i.NRBAIXAPAG = B.NRBAIXAPAG);

               IF V_VALOR IS NOT NULL THEN
                  V_STATUS := 'N';
               END IF;

            END IF;
            V_PASSO:= 35;

            IF V_STATUS = 'S' THEN
               SELECT MAX(C.CDFILIAL)
               INTO V_VALOR
               FROM ITEMNOTAFISC C
               WHERE C.CDFILIAL      = C_NOTAFISCAL.CDFILIAL    AND
                     C.NRLANCTONF    = C_NOTAFISCAL.NRLANCTONF;

               IF V_VALOR IS NOT NULL THEN
                  V_STATUS := 'N';
               END IF;
            END IF;
            V_PASSO:= 36;

            IF V_STATUS = 'S' THEN
               SELECT MAX(C.CDFILIAL)
               INTO V_VALOR
               FROM ITEMNOTAFISC C
               WHERE C.CDFILIALIT = C_NOTAFISCAL.CDFILIAL    AND
                     C.NRLANCTONFIT = C_NOTAFISCAL.NRLANCTONF;

               IF V_VALOR IS NOT NULL THEN
                  V_STATUS := 'N';
               END IF;
            END IF;
            V_PASSO:= 37;

            IF C_NOTAFISCAL.IDENTRSAIDOP = 'E' THEN
               IF V_STATUS = 'S' THEN
                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM AJCTPAGAR
                       WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                             NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                     DELETE FROM AJCTPAGAR
                      WHERE ROWID = C_DEL.ROWID;

                     VERIFTIMEOUT(V_DTHRFINLIMPEZA);
                  END LOOP;

                  V_PASSO:= 38;

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM CONTAGERADA
                       WHERE CDFILIALPAG = C_NOTAFISCAL.CDFILIAL AND
                             NRLANCTOPAG = C_NOTAFISCAL.NRLANCTONF) LOOP

                     DELETE FROM CONTAGERADA
                      WHERE ROWID = C_DEL.ROWID;

                     VERIFTIMEOUT(V_DTHRFINLIMPEZA);
                  END LOOP;

                  V_PASSO:= 39;

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM ADIANTAPAGAR
                       WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                             NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                     DELETE FROM ADIANTAPAGAR
                      WHERE ROWID = C_DEL.ROWID;

                     VERIFTIMEOUT(V_DTHRFINLIMPEZA);
                  END LOOP;

                  V_PASSO:= 40;
               END IF;
            END IF;
            V_PASSO:= 41;

            IF C_NOTAFISCAL.IDENTRSAIDOP = 'S' THEN
               IF V_STATUS = 'S' THEN

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM AJCTRECEBER
                       WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                             NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                     DELETE FROM AJCTRECEBER
                      WHERE ROWID = C_DEL.ROWID;

                     VERIFTIMEOUT(V_DTHRFINLIMPEZA);
                  END LOOP;

                  V_PASSO:= 42;

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM PARCELAVENDA
                       WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                             NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                     DELETE FROM PARCELAVENDA
                      WHERE ROWID = C_DEL.ROWID;

                     VERIFTIMEOUT(V_DTHRFINLIMPEZA);
                  END LOOP;

                  V_PASSO:= 43;

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM ADIANTARECEB
                       WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                             NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                     DELETE FROM ADIANTARECEB
                      WHERE ROWID = C_DEL.ROWID;

                     VERIFTIMEOUT(V_DTHRFINLIMPEZA);
                  END LOOP;

                  V_PASSO:= 44;
               END IF;
            END IF;
            V_PASSO:= 45;

            IF V_STATUS = 'S' THEN
               SELECT MAX(I.CDFILIAL)
               INTO V_VALOR
               FROM ITMAPARECUP I
               WHERE I.CDFILIAL = C_NOTAFISCAL.CDFILIAL    AND
                     I.NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;

               IF V_VALOR IS NOT NULL THEN
                  V_STATUS := 'N';
               END IF;
            END IF;

            IF V_STATUS = 'S' THEN
               UPDATE MOVOCORR
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF   = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 46;

               UPDATE MAPAAPURAC
               SET CDFILIAL   = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 47;

               UPDATE PREVRETI
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 48;

               UPDATE ITEMAUTF
               SET NRLANCTONF = NULL
               WHERE CDFILLANC   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 49;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM AMOSTRAPROC
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM AMOSTRAPROC
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 50;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ITEMROMANEIO
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM ITEMROMANEIO
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 51;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ROMANEIONF
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM ROMANEIONF
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 52;

               UPDATE ITREQUESTO
               SET NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 53;

               UPDATE APURFATUFAS
               SET NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 54;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM EXPNFFRETE
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM EXPNFFRETE
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 55;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM NFRELACAFSAI
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM NFRELACAFSAI
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 56;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM NFRELACPEXOL
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM NFRELACPEXOL
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 57;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM NFRELACORC
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM NFRELACORC
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 58;

               UPDATE  FORMULARIONF
               SET CDFILIAL   = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 59;

               UPDATE OCORRERFATUR
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 60;

               UPDATE AUDITORIA
               SET CDFILIALNF  = NULL,
                   NRLANCTONF  = NULL,
                   NRSEQITPROD = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 61;

               UPDATE ITEMVENDA
               SET CDFILIALNF  = NULL,
                   NRLANCTONF  = NULL,
                   NRSEQITPROD = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 62;

               UPDATE ITVENDAVMN
               SET CDFILIALNFV = NULL,
                   NRLANCTONF  = NULL,
                   NRSEQITPROD = NULL
               WHERE CDFILIALNFV = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF  = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 63;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM CONTARECEBER
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM CONTARECEBER
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 64;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM CONTAPAGAR
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM CONTAPAGAR
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 65;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM SERVPLANO
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM SERVPLANO
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 66;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM NFRELACAF
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM NFRELACAF
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 67;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM LANCDESPFATU
                    WHERE CDFILILANC   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM LANCDESPFATU
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 68;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM NFABASTEROTA
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM NFABASTEROTA
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 69;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM PARCLANCATIV
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM PARCLANCATIV
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 70;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM CTRAPUITPROD
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM CTRAPUITPROD
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 71;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM SERVFATU
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM SERVFATU
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 72;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM NFPREVALIDFIS
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM NFPREVALIDFIS
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 73;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ERRONFPREVALID
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM ERRONFPREVALID
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 75;

               UPDATE ITDETAPUCONTR
               SET CDFILIAL   = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 77;

               UPDATE BEMATIVO
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 78;

               UPDATE MOVCAIXA
               SET CDFILIALNFFIS = NULL,
                   NRLANCTONFFIS = NULL
               WHERE CDFILIALNFFIS = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONFFIS = C_NOTAFISCAL.NRLANCTONF;

               UPDATE MOVCAIXA
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;

               V_PASSO:= 79;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM DEVOLUCAOPAG
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM DEVOLUCAOPAG
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 80;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM DEVOLUCAOREC
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM DEVOLUCAOREC
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 81;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM RELACMAPANF
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM RELACMAPANF
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 82;

               UPDATE ITMAPARECUP
               SET CDFILIAL = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 83;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM MAPACOMEXP
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM MAPACOMEXP
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 84;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM MAPAPRXCOMEXP
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM MAPAPRXCOMEXP
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 85;

               UPDATE LANCCONT
               SET CDFILIAL   = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 86;

               UPDATE REDUCAOZ
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 87;

               UPDATE MAPARESUMECF
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 88;

               UPDATE INVOICE
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 89;

               UPDATE COMPRATKN
               SET CDFILIAL = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 90;

               UPDATE ITCAPAOPERABEM
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 91;

               UPDATE OPERACAOBEM
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF  = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 92;

               UPDATE ITCAPAENTRABEM
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 93;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM NFRELACCDRS
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM NFRELACCDRS
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 94;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM RELACNFLANCLOTE
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM RELACNFLANCLOTE
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 95;

               UPDATE SOLICITASUPRI
               SET CDFIVERNFSOLSUP = NULL,
                   NRLANCTONF = NULL
               WHERE CDFIVERNFSOLSUP = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF      = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 96;

               UPDATE PRESTACONTA
               SET CDFIVERNFPRECON = NULL,
                   NRLANCTONF = NULL
               WHERE CDFIVERNFPRECON = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF      = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 97;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ITEMNOTATRANSF
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM ITEMNOTATRANSF
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 98;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM GNRENF
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM GNRENF
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 99;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM CARTACORNFE
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM CARTACORNFE
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 100;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM NFPRODRURAL
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM NFPRODRURAL
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 101;

               UPDATE MAPAAPURACSN
               SET CDFILIAL = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 102;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM NFABARELNFVENBO
                    WHERE CDFILIABA   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTOABA = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM NFABARELNFVENBO
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 103;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM RELACMAPAIMPONF
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM RELACMAPAIMPONF
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 104;

               UPDATE FCT_APURACAO
               SET CDFILIAL = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 105;

               UPDATE ITDETAPURAUTO
               SET CDFILIAL = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 106;

               UPDATE ITMAPARECUPNC
               SET CDFILIAL = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 107;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM LOGNFDUPCTB
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM LOGNFDUPCTB
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 108;

               UPDATE SERVPLANOM
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF  = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 109;

               UPDATE CTEDOCFISCAIS
               SET CDFILIAL = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 110;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM CTRLCOMPENSACAO
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM CTRLCOMPENSACAO
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ITEMPROIMPOSORIG
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM ITEMPROIMPOSORIG
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 111;

               UPDATE PEDIDOSERV
               SET CDFILIAL   = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 112;

               UPDATE RETVENDAADQ
               SET CDFILIAL   = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 114;

               UPDATE AJUSTMAPAAPURSG
               SET CDFILIAL = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 115;

               UPDATE CONSMOVCAIXA
               SET CDFILIALNF = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIALNF = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 116;

               UPDATE DOCARRECADPAG
               SET CDFILIAL   = NULL,
                   NRLANCTONF = NULL
               WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                     NRLANCTONF = C_NOTAFISCAL.NRLANCTONF;
               V_PASSO:= 117;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM CONTRIBDIFERIDA
                    WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM CONTRIBDIFERIDA
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 118;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM RELACVENDANF
                    WHERE CDFILIALNF  = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM RELACVENDANF
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM INFICMSST
                    WHERE CDFILIAL  = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM INFICMSST
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM CTRLSENHADEVOL
                    WHERE CDFILIAL  = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM CTRLSENHADEVOL
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM RESERITEMPROD
                    WHERE CDFILIAL  = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM RESERITEMPROD
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM GUIAPAGDOC
                    WHERE CDFILIAL  = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM GUIAPAGDOC
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM RELACONTAINERNF
                    WHERE CDFILIAL  = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM RELACONTAINERNF
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM COLETAVOL
                    WHERE CDFILIAL  = C_NOTAFISCAL.CDFILIAL AND
                          NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM COLETAVOL
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 120;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM INTG_TRABITPROIMPOS
                    WHERE NRACESSONFE = C_NOTAFISCAL.NRACESSONFE) LOOP

                  DELETE FROM INTG_TRABITPROIMPOS
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 121;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM INTG_ITEMENCARGO
                    WHERE NRACESSONFE = C_NOTAFISCAL.NRACESSONFE) LOOP

                  DELETE FROM INTG_ITEMENCARGO
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 122;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM INTG_CONTAPAGAR
                    WHERE NRACESSONFE = C_NOTAFISCAL.NRACESSONFE) LOOP

                  DELETE FROM INTG_CONTAPAGAR
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 123;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM INTG_ITEMPRODUTO
                    WHERE NRACESSONFE = C_NOTAFISCAL.NRACESSONFE) LOOP

                  DELETE FROM INTG_ITEMPRODUTO
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 124;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM INTG_INCONSISTENCIA
                    WHERE NRACESSONFE = C_NOTAFISCAL.NRACESSONFE) LOOP

                  DELETE FROM INTG_INCONSISTENCIA
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 125;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM INTG_ARQXMLNFE
                    WHERE NRACESSONFE = C_NOTAFISCAL.NRACESSONFE) LOOP

                  DELETE FROM INTG_ARQXMLNFE
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 126;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM NOTADESTINADA
                    WHERE CDFILIAL = C_NOTAFISCAL.CDFILIAL
                      AND NRLANCTONF = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM NOTADESTINADA
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 127;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM NOTABCMENSAL
                    WHERE CDFILIALIT   = C_NOTAFISCAL.CDFILIAL
                      AND NRLANCTONFIT = C_NOTAFISCAL.NRLANCTONF) LOOP

                  DELETE FROM NOTABCMENSAL
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 128;

               UPDATE NOTADESTINADA
                  SET NRACESSOINTG = NULL
                WHERE NRACESSOINTG = C_NOTAFISCAL.NRACESSONFE;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM HISTXMLNFE
                    WHERE NRACESSONFE = C_NOTAFISCAL.NRACESSONFE) LOOP

                  DELETE FROM HISTXMLNFE
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 129;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM INTG_ITEMAF
                    WHERE NRACESSONFE = C_NOTAFISCAL.NRACESSONFE) LOOP

                  DELETE FROM INTG_ITEMAF
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               FOR C_DEL IN
                  (SELECT ROWID
                     FROM INTG_NOTAFISCAL
                    WHERE NRACESSONFE = C_NOTAFISCAL.NRACESSONFE) LOOP

                  DELETE FROM INTG_NOTAFISCAL
                   WHERE ROWID = C_DEL.ROWID;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO:= 130;

               DELETE NOTAFISCAL
               WHERE ROWID = C_NOTAFISCAL.ROWID;
               V_PASSO:= 131;

               V_EXCLUNOTAFISCAL := V_EXCLUNOTAFISCAL + 1;
               V_PASSO:= 132;

               IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
                  UPDATE TRABPROCLIMPEZA
                    SET QTREGEXCLIMPEZA = V_EXCLUNOTAFISCAL
                  WHERE IDLIMPEZA = P_IDSISTEMA
                    AND NMTABELALIMP = 'NOTAFISCAL';

                 /*CONTROLE DE COMMIT HA CADA 1000 REGISTRO DELETEDO  E PARA ATUALIZAR A TELA DE LIMPEZA*/
                 V_COUNT_COMMIT:= 1;
                 COMMIT;
               ELSE
                 V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
               END IF;
               V_PASSO:= 133;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;
      V_PASSO:= 134;

      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'C',
             QTREGEXCLIMPEZA = V_EXCLUNOTAFISCAL,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA AND
             NMTABELALIMP = 'NOTAFISCAL';
      V_PASSO:= 135;

      UPDATE TRABLIMPEZA
      SET IDSTATUSLIMPEZA = 'C'
      WHERE IDLIMPEZA = P_IDSISTEMA;
      V_PASSO:= 136;

      INSERT INTO LOGLIMPEZA (DSSISTLIMPEZ, NMTABLIMP, DTLIMPDADOS, NMOPERLIMPEZ, DTFINALIMPEZ, DSLIMPEZA, DSRDMUSULOGL, DSENDUSULOGL, DSNMUSUOLOGL, DSMETUSULOGL, QTDLINHAEXCL)
      VALUES (P_DSSISTLIMPEZ, 'NOTAFISCAL', SYSDATE, P_NMOPERLIMPEZ, P_DTFINAL, 'NOTAFISCAL', V_DSRDM, V_DSIP, V_DSUSUARIO, V_DSMETODO, V_EXCLUNOTAFISCAL);

      COMMIT;

      V_PASSO:= 137;

      V_COUNT_COMMIT := '1';

      FOR C_DEL IN
         (SELECT ROWID
            FROM NSUNFE
           WHERE DTINSNRNSU BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

         DELETE FROM NSUNFE
          WHERE ROWID = C_DEL.ROWID;

         IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
            V_COUNT_COMMIT:= 1;
            COMMIT;
         ELSE
            V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
         END IF;

         VERIFTIMEOUT(V_DTHRFINLIMPEZA);
      END LOOP;
      COMMIT;

      V_PASSO:= 138;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_NOTAFISCAL ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_ITEMPRODUTO ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_EST_ITLANCTOEST ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      
      V_PASSO:= 139;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER NF_BAIXA_SAIDA ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 140;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER BLOQ_DEL_NFE ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 141;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER GRAVA_CANCELAMENTO_EXP ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 142;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_NOTADESTINADA ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;   
      V_PASSO:= 143;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER DTULTATU_BEMATIVO ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 144;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER GERA_HISTORICO_BEM ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 145;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_MOVCAIXA ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 146;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_DEVOLUCAOPAG ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 147;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER ATUALIZA_VRDEVOLUCAO ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 148;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER ATUALIZA_VRDEVOLUCAOREC ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 149;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_RELACMAPANF ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 150;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_ITMAPARECUP ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 151;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_EXP_MAPACOMEXP ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_EXP_MAPAPRXCOMEXP ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 152;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER GRAVA_ATUA_CONTCTBL_LANCCUSTO ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 153;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_CTB_LANCCONT ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 154;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_CTB_LANCCONT ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 155;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_REDUCAOZ ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;
      V_PASSO:= 156;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER USR_EXCLUSAO_EXPORTAREDZ ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER NF_BAIXA_SAIDA ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      V_PASSO:= 157;

      /*Apagando a lista utilizada para a limpeza*/
      DELETE FROM LISTA
      WHERE NRLISTA =P_NRLISTA;
      V_PASSO:= 158;

      COMMIT;
      V_PASSO:= 159;
   END IF;      
EXCEPTION
   WHEN OTHERS THEN BEGIN
      V_ERROR:= SQLERRM(SQLCODE);

      IF INSTR(V_ERROR, 'TIMEOUT - EXCECAO PROPOSITAL TEKNISA') = 0 THEN
         /*Apagando a lista utilizada para a limpeza*/
         DELETE FROM LISTA
         WHERE NRLISTA =P_NRLISTA;

         UPDATE TRABLIMPEZA
         SET IDSTATUSLIMPEZA = 'C', IDLIMPEZACONCERRO = 'S'
         WHERE IDLIMPEZA = P_IDSISTEMA;

         COMMIT;
      ELSE
         UPDATE TRABLIMPEZA
         SET IDSTATUSLIMPEZA = 'P'
         WHERE IDLIMPEZA = P_IDSISTEMA;
      END IF;

      IF (V_EXCLUNOTAFISCAL > 0) THEN
         INSERT INTO LOGLIMPEZA (DSSISTLIMPEZ, NMTABLIMP, DTLIMPDADOS, NMOPERLIMPEZ, DTFINALIMPEZ, DSLIMPEZA, DSRDMUSULOGL, DSENDUSULOGL, DSNMUSUOLOGL, DSMETUSULOGL, QTDLINHAEXCL)
         VALUES (P_DSSISTLIMPEZ, 'NOTAFISCAL', SYSDATE, P_NMOPERLIMPEZ, P_DTFINAL, 'NOTAFISCAL', V_DSRDM, V_DSIP, V_DSUSUARIO, V_DSMETODO, V_EXCLUNOTAFISCAL);
      END IF;

      COMMIT;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_NOTAFISCAL  ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_ITEMPRODUTO ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_EST_ITLANCTOEST ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER NF_BAIXA_SAIDA ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER BLOQ_DEL_NFE ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER GRAVA_CANCELAMENTO_EXP ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_NOTADESTINADA ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;   

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER DTULTATU_BEMATIVO ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER GERA_HISTORICO_BEM ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_MOVCAIXA ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_DEVOLUCAOPAG ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER ATUALIZA_VRDEVOLUCAO ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER ATUALIZA_VRDEVOLUCAOREC ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_RELACMAPANF ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_ITMAPARECUP ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_EXP_MAPACOMEXP ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_EXP_MAPAPRXCOMEXP ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER GRAVA_ATUA_CONTCTBL_LANCCUSTO ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_CTB_LANCCONT ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_CTB_LANCCONT ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_FIS_REDUCAOZ ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER USR_EXCLUSAO_EXPORTAREDZ ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER NF_BAIXA_SAIDA ENABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      RAISE_APPLICATION_ERROR (-20101, 'PASSO : ' || V_PASSO || '   -   ' || V_ERROR);
   END;
END;
/