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_PASSO             NUMBER := 0;
   V_AUX               NUMBER := 0;
   V_COMMIT            NUMBER;
   V_QTCOMMIT          NUMBER;

   V_CDFILIAL          VARCHAR2(4);

   V_DTHRFINLIMPEZA  DATE;

   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_COMMIT := 0;
   V_QTCOMMIT := 5000;

   WEBTUNNING(V_DSIP, V_DSUSUARIO, V_DSRDM, V_DSMETODO, V_DSOPERADOR);

   V_NRORDEM := '0';

   DELETE FROM TRABPROCLIMPEZA
   WHERE IDLIMPEZA = P_IDSISTEMA;

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

   --PEGA A DATA INICIAL E FINAL DA EXECUCAO
   SELECT TO_DATE(TO_CHAR(SYSDATE,'DDMMYYYY') || HRFINLIMPEZA||'00','DDMMYYYYHH24MISS')
   INTO V_DTHRFINLIMPEZA
   FROM TRABLIMPEZA
   WHERE IDLIMPEZA = P_IDSISTEMA;

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

   COMMIT;

   /* TABELA SOLICITA  */
   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;

   INSERT INTO TRABPROCLIMPEZA
     (IDLIMPEZA, NMTABELALIMP, IDSITUPROCLIMP)
   VALUES
      (P_IDSISTEMA, 'TURCAIXA', 'A');

   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.DTMOVTURCAIX < P_DTFINAL
                            AND TURCAIXA.CDFILIAL = V_CDFILIAL)
      LOOP
         V_PASSO := 3;
         FOR C_VENDA IN
            (SELECT CDFILIAL, CDCAIXA, NRSEQVENDA
               FROM VENDA
              WHERE CDFILIAL = C_TURCAIXA.CDFILIAL
                AND CDCAIXA = 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;
               COMMITA;
               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;
               COMMITA;
               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;
               COMMITA;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            END LOOP;

            V_PASSO := 20;
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            FOR C_DEL IN
               (SELECT ROWID
                  FROM VENDA
                 WHERE CDFILIAL   = C_VENDA.CDFILIAL
                   AND CDCAIXA    = C_VENDA.CDCAIXA
                   AND NRSEQVENDA = C_VENDA.NRSEQVENDA) LOOP
               DELETE FROM VENDA
                WHERE ROWID = C_DEL.ROWID;
               V_EXCLUVENDA := SQL%ROWCOUNT + V_EXCLUVENDA;
               COMMITA;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            END LOOP;
         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;
            COMMITA;
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;

         VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         DELETE
           FROM FECHACAIXA
          WHERE CDFILIAL = C_TURCAIXA.CDFILIAL
            AND CDCAIXA = C_TURCAIXA.CDCAIXA
            AND DTABERCAIX = C_TURCAIXA.DTABERCAIX;

         VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         DELETE
           FROM TURCAIXA
          WHERE CDFILIAL = C_TURCAIXA.CDFILIAL
            AND CDCAIXA = C_TURCAIXA.CDCAIXA
            AND DTABERCAIX = C_TURCAIXA.DTABERCAIX;
         VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         COMMITA;
      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';
   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);
   V_PASSO := 28;
   /*Apagando a lista utilizada para a limpeza*/
   DELETE FROM LISTA
   WHERE NRLISTA =P_NRLISTA;
   V_PASSO := 29;
   COMMIT;
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;
   COMMIT;
   RAISE_APPLICATION_ERROR (-20101,  'PASSO : ' || V_PASSO || '   -   ' || V_ERROR);
END;
/