CREATE OR REPLACE PROCEDURE LIMPEZA_VENDAS(
   P_NRLISTA       IN VARCHAR2,
   P_DTFINAL       IN DATE,
   P_DSSISTLIMPEZ  IN VARCHAR2,
   P_NMOPERLIMPEZ  IN VARCHAR2,
   P_IDSISTEMA     IN VARCHAR2)
AS
   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_EXCLUTURCAIXA     NUMBER := 0;
   V_EXCLUMOVCAIXA     NUMBER := 0;
   V_EXCLUMOVICLIE     NUMBER := 0;
   V_EXCLUVENDA        NUMBER := 0;
   V_EXCLUITEMVENDA    NUMBER := 0;
   V_EXCLUIARQCAT52    NUMBER := 0;
   V_EXLANCROPFILIAL   NUMBER := 0;

   V_PASSO             NUMBER := 0;
   V_AUX               NUMBER := 0;
   V_COMMIT            NUMBER;
   V_QTCOMMIT          NUMBER;

   V_CDFILIAL          VARCHAR2(4);

   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;

   PROCEDURE COMMITA
   IS
   BEGIN
      V_COMMIT := V_COMMIT + 1;
      IF V_COMMIT = V_QTCOMMIT THEN
         V_COMMIT := 0;
         COMMIT;
      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

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

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

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

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

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

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

      V_COMMIT := 0;
      V_QTCOMMIT := 250;

      V_NRORDEM := '0';

      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_EXCLUTURCAIXA
           FROM TRABPROCLIMPEZA
          WHERE IDLIMPEZA = P_IDSISTEMA
            AND NMTABELALIMP = 'TURCAIXA';

         SELECT MAX(QTREGEXCLIMPEZA)
           INTO V_EXCLUMOVCAIXA
           FROM TRABPROCLIMPEZA
          WHERE IDLIMPEZA = P_IDSISTEMA
            AND NMTABELALIMP = 'MOVCAIXA';

         SELECT MAX(QTREGEXCLIMPEZA)
           INTO V_EXCLUMOVICLIE
           FROM TRABPROCLIMPEZA
          WHERE IDLIMPEZA = P_IDSISTEMA
            AND NMTABELALIMP = 'MOVICLIE';

         SELECT MAX(QTREGEXCLIMPEZA)
           INTO V_EXCLUVENDA
           FROM TRABPROCLIMPEZA
          WHERE IDLIMPEZA = P_IDSISTEMA
            AND NMTABELALIMP = 'VENDA';

         SELECT MAX(QTREGEXCLIMPEZA)
           INTO V_EXCLUITEMVENDA
           FROM TRABPROCLIMPEZA
          WHERE IDLIMPEZA = P_IDSISTEMA
            AND NMTABELALIMP = 'ITEMVENDA';

         SELECT MAX(QTREGEXCLIMPEZA)
           INTO V_EXCLUIARQCAT52
           FROM TRABPROCLIMPEZA
          WHERE IDLIMPEZA = P_IDSISTEMA
            AND NMTABELALIMP = 'ARQCAT52';

         SELECT MAX(QTREGEXCLIMPEZA)
           INTO V_EXLANCROPFILIAL
           FROM TRABPROCLIMPEZA
          WHERE IDLIMPEZA = P_IDSISTEMA
            AND NMTABELALIMP = 'LANCROPFILIAL';
      END IF;

      UPDATE TRABLIMPEZA
         SET IDSTATUSLIMPEZA = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA;

      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 = 'TURCAIXA';

      IF NOT(SQL%ROWCOUNT > 0) THEN
         INSERT INTO TABLIMPEZA
         (IDLIMPEZA, NMTABELALIMP, NRORDEMLIMPEZA)
         VALUES
         (P_IDSISTEMA, 'TURCAIXA', V_NRORDEM);
      END IF;

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP, IDSITUPROCLIMP)
         VALUES
            (P_IDSISTEMA, 'TURCAIXA', 'A');
      END IF;
      
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

      UPDATE TABLIMPEZA
         SET NRORDEMLIMPEZA = V_NRORDEM
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'MOVCAIXA';

      IF NOT(SQL%ROWCOUNT > 0) THEN
         INSERT INTO TABLIMPEZA
         (IDLIMPEZA, NMTABELALIMP, NRORDEMLIMPEZA)
         VALUES
         (P_IDSISTEMA, 'MOVCAIXA', V_NRORDEM);
      END IF;

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP, IDSITUPROCLIMP)
         VALUES
            (P_IDSISTEMA, 'MOVCAIXA', 'A');
      END IF;
      
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

      UPDATE TABLIMPEZA
         SET NRORDEMLIMPEZA = V_NRORDEM
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'MOVICLIE';

      IF NOT(SQL%ROWCOUNT > 0) THEN
         INSERT INTO TABLIMPEZA
         (IDLIMPEZA, NMTABELALIMP, NRORDEMLIMPEZA)
         VALUES
         (P_IDSISTEMA, 'MOVICLIE', V_NRORDEM);
      END IF;

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP, IDSITUPROCLIMP)
         VALUES
            (P_IDSISTEMA, 'MOVICLIE', 'A');
      END IF;
      
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

      UPDATE TABLIMPEZA
         SET NRORDEMLIMPEZA = V_NRORDEM
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'VENDA';

      IF NOT(SQL%ROWCOUNT > 0) THEN
         INSERT INTO TABLIMPEZA
         (IDLIMPEZA, NMTABELALIMP, NRORDEMLIMPEZA)
         VALUES
         (P_IDSISTEMA, 'VENDA', V_NRORDEM);
      END IF;

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP, IDSITUPROCLIMP)
         VALUES
            (P_IDSISTEMA, 'VENDA', 'A');
      END IF;

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

      UPDATE TABLIMPEZA
         SET NRORDEMLIMPEZA = V_NRORDEM
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'ITEMVENDA';

      IF NOT(SQL%ROWCOUNT > 0) THEN
         INSERT INTO TABLIMPEZA
         (IDLIMPEZA, NMTABELALIMP, NRORDEMLIMPEZA)
         VALUES
         (P_IDSISTEMA, 'ITEMVENDA', V_NRORDEM);
      END IF;

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP, IDSITUPROCLIMP)
         VALUES
            (P_IDSISTEMA, 'ITEMVENDA', 'A');
      END IF;

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

      UPDATE TABLIMPEZA
         SET NRORDEMLIMPEZA = V_NRORDEM
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'ARQCAT52';

      IF NOT(SQL%ROWCOUNT > 0) THEN
         INSERT INTO TABLIMPEZA
         (IDLIMPEZA, NMTABELALIMP, NRORDEMLIMPEZA)
         VALUES
         (P_IDSISTEMA, 'ARQCAT52', V_NRORDEM);
      END IF;

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP, IDSITUPROCLIMP)
         VALUES
            (P_IDSISTEMA, 'ARQCAT52', 'A');
      END IF;

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

      UPDATE TABLIMPEZA
         SET NRORDEMLIMPEZA = V_NRORDEM
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'LANCROPFILIAL';

      IF NOT(SQL%ROWCOUNT > 0) THEN
         INSERT INTO TABLIMPEZA
         (IDLIMPEZA, NMTABELALIMP, NRORDEMLIMPEZA)
         VALUES
         (P_IDSISTEMA, 'LANCROPFILIAL', V_NRORDEM);
      END IF;

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP, IDSITUPROCLIMP)
         VALUES
            (P_IDSISTEMA, 'LANCROPFILIAL', 'A');
      END IF;

      COMMIT;
      V_PASSO := 1;
      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         V_PASSO := 2;
         FOR C_TURCAIXA IN (SELECT CDFILIAL, CDCAIXA, DTABERCAIX, ROWID AS LINHA
                              FROM TURCAIXA
                             WHERE TURCAIXA.CDFILIAL = V_CDFILIAL
                               AND TURCAIXA.DTMOVTURCAIX BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            V_PASSO := 3;
            FOR C_VENDA IN
               (SELECT CDFILIAL, CDCAIXA, NRSEQVENDA, ROWID
                  FROM VENDA
                 WHERE CDFILIALTUR = C_TURCAIXA.CDFILIAL
                   AND CDCAIXATUR  = C_TURCAIXA.CDCAIXA
                   AND DTABERTUR   = C_TURCAIXA.DTABERCAIX) LOOP
               V_PASSO := 4;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM OBSITEMVENDAEST
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA)LOOP
                  DELETE
                    FROM OBSITEMVENDAEST
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 5;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM OBSITEMVENDA
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE
                    FROM OBSITEMVENDA
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 6;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                    FROM ITEMVENDAEST
                   WHERE CDFILIAL   = C_VENDA.CDFILIAL
                     AND CDCAIXA    = C_VENDA.CDCAIXA
                     AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM ITEMVENDAEST
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 7;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ITVENDACAN
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM ITVENDACAN
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 8;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ITVENDADES
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM ITVENDADES
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 9;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ITVENDAIMPOS
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM ITVENDAIMPOS
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 10;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM USOCUPOMDESCFOS
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM USOCUPOMDESCFOS
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 11;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM MOVBENEFICIOCONS
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM MOVBENEFICIOCONS
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 12;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ITFIDELIDADE
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM ITFIDELIDADE
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 13;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM EXTRATOCONS
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM EXTRATOCONS
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 14;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM MOVICLIE
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM MOVICLIE
                   WHERE ROWID = C_DEL.ROWID;
                  V_EXCLUMOVICLIE := SQL%ROWCOUNT + V_EXCLUMOVICLIE;

                  IF V_COMMIT >= V_QTCOMMIT THEN
                     UPDATE TRABPROCLIMPEZA
                       SET QTREGEXCLIMPEZA = V_EXCLUMOVICLIE
                     WHERE IDLIMPEZA = P_IDSISTEMA
                       AND NMTABELALIMP = 'MOVCLIE';

                     /*CONTROLE DE COMMIT HA CADA 100 REGISTRO DELETEDO  E PARA ATUALIZAR A TELA DE LIMPEZA*/
                     V_COMMIT := 0;
                     COMMIT;
                  ELSE
                     V_COMMIT:= V_COMMIT + 1;
                  END IF;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 15;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ITEMVENDA
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM ITEMVENDA
                   WHERE ROWID = C_DEL.ROWID;
                  V_EXCLUITEMVENDA := SQL%ROWCOUNT + V_EXCLUITEMVENDA;

                  IF V_COMMIT >= V_QTCOMMIT THEN
                     UPDATE TRABPROCLIMPEZA
                       SET QTREGEXCLIMPEZA = V_EXCLUITEMVENDA
                     WHERE IDLIMPEZA = P_IDSISTEMA
                       AND NMTABELALIMP = 'ITEMVENDA';

                     /*CONTROLE DE COMMIT HA CADA 100 REGISTRO DELETEDO  E PARA ATUALIZAR A TELA DE LIMPEZA*/
                     V_COMMIT := 0;
                     COMMIT;
                  ELSE
                     V_COMMIT := V_COMMIT + 1;
                  END IF;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 16;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ARQXMLNFCE
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM ARQXMLNFCE
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;


               V_PASSO := 17;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ARQXMLNFCESAT
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM ARQXMLNFCESAT
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 18;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM ITHRCOMANDA
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM ITHRCOMANDA
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 19;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM MOVCAIXA
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM MOVCAIXA
                   WHERE ROWID = C_DEL.ROWID;
                  V_EXCLUMOVCAIXA := V_EXCLUMOVCAIXA + SQL%ROWCOUNT;

                  IF V_COMMIT >= V_QTCOMMIT THEN
                     UPDATE TRABPROCLIMPEZA
                       SET QTREGEXCLIMPEZA = V_EXCLUMOVCAIXA
                     WHERE IDLIMPEZA = P_IDSISTEMA
                       AND NMTABELALIMP = 'MOVCAIXA';

                     /*CONTROLE DE COMMIT HA CADA 100 REGISTRO DELETEDO  E PARA ATUALIZAR A TELA DE LIMPEZA*/
                     V_COMMIT := 0;
                     COMMIT;
                  ELSE
                     V_COMMIT := V_COMMIT + 1;
                  END IF;

                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               FOR C_DEL IN
                  (SELECT ROWID
                     FROM MOVPONTOCONS
                    WHERE CDFILIAL   = C_VENDA.CDFILIAL
                      AND CDCAIXA    = C_VENDA.CDCAIXA
                      AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
                  DELETE FROM MOVPONTOCONS
                   WHERE ROWID = C_DEL.ROWID;
                  COMMITA;
                  VERIFTIMEOUT(V_DTHRFINLIMPEZA);
               END LOOP;

               V_PASSO := 20;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);

               DELETE FROM VENDA
                WHERE ROWID = C_VENDA.ROWID;
               V_EXCLUVENDA := SQL%ROWCOUNT + V_EXCLUVENDA;

               IF V_COMMIT >= V_QTCOMMIT THEN
                  UPDATE TRABPROCLIMPEZA
                    SET QTREGEXCLIMPEZA = V_EXCLUVENDA
                  WHERE IDLIMPEZA = P_IDSISTEMA
                    AND NMTABELALIMP = 'VENDA';

                  /*CONTROLE DE COMMIT HA CADA 100 REGISTRO DELETEDO  E PARA ATUALIZAR A TELA DE LIMPEZA*/
                  V_COMMIT := 0;
                  COMMIT;
               ELSE
                  V_COMMIT:= V_COMMIT + 1;
               END IF;

               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            END LOOP;

            FOR C_DEL IN
               (SELECT ROWID
                  FROM EXTRATOCONS
                 WHERE CDFILIAL   = C_TURCAIXA.CDFILIAL
                   AND CDCAIXA    = C_TURCAIXA.CDCAIXA
                   AND DTABERCAIX = C_TURCAIXA.DTABERCAIX) LOOP
               DELETE FROM EXTRATOCONS
                WHERE ROWID = C_DEL.ROWID;
               COMMITA;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            END LOOP;

            FOR C_DEL IN
               (SELECT ROWID
                  FROM MOVCAIXA
                 WHERE CDFILIAL   = C_TURCAIXA.CDFILIAL
                   AND CDCAIXA    = C_TURCAIXA.CDCAIXA
                   AND DTABERCAIX = C_TURCAIXA.DTABERCAIX) LOOP
               DELETE FROM MOVCAIXA
                WHERE ROWID = C_DEL.ROWID;
               V_EXCLUMOVCAIXA := V_EXCLUMOVCAIXA + SQL%ROWCOUNT;

               IF V_COMMIT >= V_QTCOMMIT THEN
                  UPDATE TRABPROCLIMPEZA
                    SET QTREGEXCLIMPEZA = V_EXCLUMOVCAIXA
                  WHERE IDLIMPEZA = P_IDSISTEMA
                    AND NMTABELALIMP = 'MOVCAIXA';

                  /*CONTROLE DE COMMIT HA CADA 100 REGISTRO DELETEDO  E PARA ATUALIZAR A TELA DE LIMPEZA*/
                  V_COMMIT := 0;
                  COMMIT;
               ELSE
                  V_COMMIT:= V_COMMIT + 1;
               END IF;

               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            END LOOP;

            VERIFTIMEOUT(V_DTHRFINLIMPEZA);  
            FOR C_DEL IN
               (SELECT ROWID
                  FROM FECHACAIXA
                 WHERE CDFILIAL   = C_TURCAIXA.CDFILIAL
                   AND CDCAIXA    = C_TURCAIXA.CDCAIXA
                   AND DTABERCAIX = C_TURCAIXA.DTABERCAIX)
            LOOP
               DELETE FROM FECHACAIXA
                WHERE ROWID = C_DEL.ROWID;

               COMMITA;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            END LOOP;

            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            DELETE
              FROM TURCAIXA
             WHERE ROWID = C_TURCAIXA.LINHA;
            V_EXCLUTURCAIXA := V_EXCLUTURCAIXA + SQL%ROWCOUNT;

            IF V_COMMIT >= V_QTCOMMIT THEN
               UPDATE TRABPROCLIMPEZA
                 SET QTREGEXCLIMPEZA = V_EXCLUTURCAIXA
               WHERE IDLIMPEZA = P_IDSISTEMA
                 AND NMTABELALIMP = 'TURCAIXA';

               /*CONTROLE DE COMMIT HA CADA 100 REGISTRO DELETEDO  E PARA ATUALIZAR A TELA DE LIMPEZA*/
               V_COMMIT := 0;
               COMMIT;
            ELSE
               V_COMMIT:= V_COMMIT + 1;
            END IF;

            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;

         FOR C_ARQCAT52 IN (SELECT ROWID AS LINHA
                               FROM ARQCAT52
                              WHERE CDFILIAL = V_CDFILIAL
                                AND DTMOVCAT52 BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            DELETE FROM ARQCAT52
             WHERE ROWID = C_ARQCAT52.LINHA;

            V_EXCLUIARQCAT52 := SQL%ROWCOUNT + V_EXCLUIARQCAT52;

            IF V_COMMIT >= V_QTCOMMIT THEN
               UPDATE TRABPROCLIMPEZA
                 SET QTREGEXCLIMPEZA = V_EXCLUIARQCAT52
               WHERE IDLIMPEZA = P_IDSISTEMA
                 AND NMTABELALIMP = 'ARQCAT52';

               /*CONTROLE DE COMMIT HA CADA 100 REGISTRO DELETEDO  E PARA ATUALIZAR A TELA DE LIMPEZA*/
               V_COMMIT := 0;
               COMMIT;
            ELSE
               V_COMMIT := V_COMMIT + 1;
            END IF;

            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;

         FOR C_LANCROPFILIAL IN (SELECT ROWID AS LINHA
                                   FROM LANCROPFILIAL
                                  WHERE CDFILIAL = V_CDFILIAL
                                    AND DTLANCROP BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            DELETE FROM LANCROPFILIAL
             WHERE ROWID = C_LANCROPFILIAL.LINHA;

            V_EXLANCROPFILIAL := SQL%ROWCOUNT + V_EXLANCROPFILIAL;

            IF V_COMMIT >= V_QTCOMMIT THEN
               UPDATE TRABPROCLIMPEZA
                 SET QTREGEXCLIMPEZA = V_EXLANCROPFILIAL
               WHERE IDLIMPEZA = P_IDSISTEMA
                 AND NMTABELALIMP = 'LANCROPFILIAL';

               /*CONTROLE DE COMMIT HA CADA 100 REGISTRO DELETEDO  E PARA ATUALIZAR A TELA DE LIMPEZA*/
               V_COMMIT := 0;
               COMMIT;
            ELSE
               V_COMMIT := V_COMMIT + 1;
            END IF;

            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;

      END LOOP;
      CLOSE C_LISTA;

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA CONCLUIDO */
      V_PASSO := 21;
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'C',
             QTREGEXCLIMPEZA = V_EXCLUTURCAIXA,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA AND
             NMTABELALIMP = 'TURCAIXA';

      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'C',
             QTREGEXCLIMPEZA = V_EXCLUMOVCAIXA,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA AND
             NMTABELALIMP = 'MOVCAIXA';

      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'C',
             QTREGEXCLIMPEZA = V_EXCLUMOVICLIE,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA AND
             NMTABELALIMP = 'MOVICLIE';

       UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'C',
             QTREGEXCLIMPEZA = V_EXCLUVENDA,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA AND
             NMTABELALIMP = 'VENDA';

       UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'C',
             QTREGEXCLIMPEZA = V_EXCLUITEMVENDA,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA AND
             NMTABELALIMP = 'ITEMVENDA';

       UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'C',
             QTREGEXCLIMPEZA = V_EXCLUIARQCAT52,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA AND
             NMTABELALIMP = 'ARQCAT52';

       UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'C',
             QTREGEXCLIMPEZA = V_EXLANCROPFILIAL,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA AND
             NMTABELALIMP = 'LANCROPFILIAL';
      COMMIT;

      /*deleta a tabela temporaria da limpeza */
      V_PASSO := 22;
      UPDATE TRABLIMPEZA
         SET IDSTATUSLIMPEZA = 'C'
       WHERE IDLIMPEZA = P_IDSISTEMA;

      V_PASSO := 23;
      INSERT INTO LOGLIMPEZA (DSSISTLIMPEZ, NMTABLIMP, DTLIMPDADOS, NMOPERLIMPEZ, DTFINALIMPEZ, DSLIMPEZA, DSRDMUSULOGL, DSENDUSULOGL, DSNMUSUOLOGL, DSMETUSULOGL, QTDLINHAEXCL)
      VALUES (P_DSSISTLIMPEZ, 'TURCAIXA', SYSDATE, P_NMOPERLIMPEZ, P_DTFINAL, 'TURCAIXA', V_DSRDM, V_DSIP, V_DSUSUARIO, V_DSMETODO, V_EXCLUTURCAIXA);

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

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

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

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

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

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

      /*VERIFICA SE A PROCEDURE ESTOUROU O TEMPO LIMITE*/
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      BEGIN
         EXECUTE IMMEDIATE 'TRUNCATE TABLE LOGOPERAVND';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      BEGIN
         EXECUTE IMMEDIATE 'TRUNCATE TABLE LOGOPERFOS';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

      V_PASSO := 28;
      /*Apagando a lista utilizada para a limpeza*/
      DELETE FROM LISTA
      WHERE NRLISTA =P_NRLISTA;
      V_PASSO := 29;
      COMMIT;
   END IF;

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

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

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

   BEGIN
      EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_VND_ITEMVENDA   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_VND_TURCAIXA   ENABLE';
   EXCEPTION
      WHEN OTHERS THEN NULL;
   END;
EXCEPTION
  WHEN OTHERS THEN
   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;
   ELSE
       UPDATE TRABLIMPEZA
       SET IDSTATUSLIMPEZA = 'P'
       WHERE IDLIMPEZA = P_IDSISTEMA;
   END IF;
   COMMIT;

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

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

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

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

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

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

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

   COMMIT;

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

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

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

   BEGIN
      EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_VND_ITEMVENDA   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_VND_TURCAIXA   ENABLE';
   EXCEPTION
      WHEN OTHERS THEN NULL;
   END;

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