CREATE OR REPLACE
PROCEDURE LIMPEZA_DADOEST(
   P_NRLISTA       IN VARCHAR2,
   P_DTFINAL       IN DATE,
   P_DSSISTLIMPEZ  IN VARCHAR2,
   P_NMOPERLIMPEZ  IN VARCHAR2,
   P_IDSISTEMA     IN VARCHAR2)
AS
   V_ERR0R             VARCHAR2(2000);
   V_PASSO             NUMBER := 0;
   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_NRLANCESTQ        VARCHAR2(20);

   V_COUNT_COMMIT      NUMBER := 1;
   V_NUM_TO_COMMIT     NUMBER := 250; -- NUMERO UTILIZADO PARA REGULARIZAR A QUANTIDADE DE DMLS PARA O COMMIT.

   V_CDFILIAL          VARCHAR2(4);
   V_CDFILIPROC        VARCHAR2(04);

   V_NRSEQUITEM        NUMBER := 0;

   V_EXCLURETIRADAPCP  NUMBER := 0;
   V_EXCLURETIRADAESTO NUMBER := 0;
   V_EXCLUAJURELACRET  NUMBER := 0;
   V_EXCLUROMANEIO     NUMBER := 0;
   V_EXCLUPOSESTDIA    FLOAT;
   V_EXCLUPOSESTCONS   FLOAT;
   V_EXCLUNFABASTEROTA NUMBER := 0;
   V_EXCLUPREVPDVLESTO NUMBER := 0;
   V_EXCLUREQUESTO     NUMBER := 0;
   V_EXCLULANCTOESTOQ  NUMBER := 0;
   V_EXCLUCUSTOCMPPROD NUMBER := 0;
   V_EXCLUINVEROTA     NUMBER := 0;
   V_EXCLUIDM_INFLACAO NUMBER := 0;

   V_COUNT_IMPLANTACAO NUMBER;

   V_CDOPERADOR        VARCHAR2(12);

   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_CDOPERADOR := NULL;
      SELECT MAX(CDOPERADOR)
        INTO V_CDOPERADOR
        FROM OPERADOR
       WHERE CDOPERADOR = '000000000999';

      IF V_CDOPERADOR IS NULL THEN
         SELECT MAX(CDOPERADOR)
           INTO V_CDOPERADOR
           FROM OPERADOR;
      END IF;

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

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

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

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

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

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

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

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

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

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

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

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

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

      SELECT MAX(CDFILIAL)
      INTO V_CDFILIPROC
      FROM PARAMGERAL;

      V_NRORDEM := '0';

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

      COMMIT;

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

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

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

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

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

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

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

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

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_ATE_DELIVERY   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 HIST_INVEROTA_EST   DISABLE';
      EXCEPTION
         WHEN OTHERS THEN NULL;
      END;

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

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

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

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

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

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'RETIRADAPCP');
      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 = 'RETIRADAESTO';

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'RETIRADAESTO');
      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 = 'REQUESTO';

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'REQUESTO');
      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 = 'AJURELACRET';

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'AJURELACRET');
      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 = 'NFABASTEROTA';

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'NFABASTEROTA');
      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 = 'PREVPDVLESTO';

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'PREVPDVLESTO');
      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 = 'CUSTOCMPPROD';

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'CUSTOCMPPROD');
      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 = 'LANCTOESTOQ';

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'LANCTOESTOQ');
      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 = 'ROMANEIO';

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'ROMANEIO');
      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 = 'POSESTDIA';

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'POSESTDIA');
      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 = 'POSESTCONS';

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'POSESTCONS');
      END IF;
      
      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'RETIRADAPCP';

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

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'INVEROTA');
      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 = 'DM_INFLACAO';

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

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

      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO := 0;

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

         FOR C_RETIRADAPCP IN
            (SELECT DISTINCT C.CDFILIALLPCP, C.NRLANCESTQ
             FROM RETIRADAPCP C, LANCTOESTOQ L
             WHERE C.CDFILIALLPCP = L.CDFILIAL        AND
                   C.NRLANCESTQ   = L.NRLANCESTQ      AND
                   L.CDFILIMOVI   = V_CDFILIAL AND
                   L.DTLANCESTQ  BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
             DELETE ITEMRETPCP
             WHERE CDFILIALITLPCP = C_RETIRADAPCP.CDFILIALLPCP
               AND NRLANCESTQ = C_RETIRADAPCP.NRLANCESTQ;

             FOR C_RETIRADAPCPPCP IN
                (SELECT ROWID AS LINHA
                   FROM RETIRADAPCP
                  WHERE CDFILIALLPCP = C_RETIRADAPCP.CDFILIALLPCP
                    AND NRLANCESTQ = C_RETIRADAPCP.NRLANCESTQ)
             LOOP
                DELETE RETIRADAPCP
                WHERE ROWID = C_RETIRADAPCPPCP.LINHA;
                V_EXCLURETIRADAPCP := SQL%ROWCOUNT + V_EXCLURETIRADAPCP;

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

                  /*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;
                /*Verifica se a procedure estourou o tempo limite*/
                VERIFTIMEOUT(V_DTHRFINLIMPEZA);
             END LOOP;
         END LOOP;
      END LOOP;

      V_PASSO := 1;

      CLOSE C_LISTA;

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

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'RETIRADAESTO';

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_RETIRADAESTO IN (SELECT C.ROWID AS LINHA
                                FROM RETIRADAESTO C, LANCTOESTOQ L
                                WHERE C.CDFILIALLAN  = L.CDFILIAL        AND
                                      C.NRLANCESTQ   = L.NRLANCESTQ      AND
                                      L.CDFILIMOVI   = V_CDFILIAL        AND
                                      L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL AND
                                      L.IDTPLANCTO = '3') LOOP

            DELETE RETIRADAESTO
            WHERE ROWID = C_RETIRADAESTO.LINHA;
            V_EXCLURETIRADAESTO := SQL%ROWCOUNT + V_EXCLURETIRADAESTO;

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

              /*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;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;

      V_PASSO := 2;

      CLOSE C_LISTA;

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

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'REQUESTO';

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO := 3;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_ITREQUESTO IN (SELECT DISTINCT CDFILILANC, C.NRLANCESTQ, C.CDFILIAL, C.NRREQUESTO
                               FROM ITREQUESTO C, LANCTOESTOQ L
                               WHERE C.CDFILILANC   = L.CDFILIAL        AND
                                     C.NRLANCESTQ   = L.NRLANCESTQ      AND
                                     L.CDFILIMOVI   = V_CDFILIAL AND
                                     L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            DELETE CTRLMOVIESTQ
            WHERE CDFILIAL = C_ITREQUESTO.CDFILIAL
              AND NRREQUESTO = C_ITREQUESTO.NRREQUESTO;

            DELETE RETIRADAESTO
            WHERE CDFILIAL = C_ITREQUESTO.CDFILIAL
              AND NRREQUESTO = C_ITREQUESTO.NRREQUESTO;
            V_EXCLURETIRADAESTO := SQL%ROWCOUNT + V_EXCLURETIRADAESTO;

            UPDATE ITLANCTOEST
               SET NRREQUESTO = NULL
             WHERE CDFILIAL   = C_ITREQUESTO.CDFILIAL
               AND NRREQUESTO = C_ITREQUESTO.NRREQUESTO;

            DELETE REQUESTO
            WHERE CDFILIAL = C_ITREQUESTO.CDFILIAL
             AND NRREQUESTO = C_ITREQUESTO.NRREQUESTO;

            V_EXCLUREQUESTO := SQL%ROWCOUNT + V_EXCLUREQUESTO;

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

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

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

      CLOSE C_LISTA;

      V_PASSO := 4;

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

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_PRODACABADO IN (SELECT C.ROWID AS LINHA
                               FROM PRODACABADO C, LANCTOESTOQ L
                               WHERE C.CDFILIAL     = L.CDFILIAL        AND
                                     C.NRLANCESTQ   = L.NRLANCESTQ      AND
                                     L.CDFILIMOVI   = V_CDFILIAL AND
                                     L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            UPDATE PRODACABADO I
            SET I.NRSEQUITEM   = NULL,
                I.NRLANCESTQ   = NULL
            WHERE ROWID = C_PRODACABADO.LINHA;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               V_COUNT_COMMIT:= 1;
               COMMIT;
            ELSE
              V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO := 5;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_DISTRIREFEICAO IN (SELECT C.ROWID AS LINHA
                                    FROM DISTRIREFEICAO C, LANCTOESTOQ L
                                   WHERE C.CDFILIAL     = L.CDFILIAL     AND
                                         C.NRLANCESTQSAID = L.NRLANCESTQ   AND
                                         L.CDFILIMOVI   = V_CDFILIAL       AND
                                         L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            UPDATE DISTRIREFEICAO I
            SET I.NRLANCESTQSAID = NULL
            WHERE ROWID = C_DISTRIREFEICAO.LINHA;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               V_COUNT_COMMIT:= 1;
               COMMIT;
            ELSE
              V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;

         V_COUNT_COMMIT:= 1;
         COMMIT;
         VERIFTIMEOUT(V_DTHRFINLIMPEZA);

         FOR C_DISTRIREFEICAO IN (SELECT C.ROWID AS LINHA
                                    FROM DISTRIREFEICAO C, LANCTOESTOQ L
                                   WHERE C.CDFILIAL     = L.CDFILIAL     AND
                                         C.NRLANCESTQENTR = L.NRLANCESTQ   AND
                                         L.CDFILIMOVI   = V_CDFILIAL       AND
                                         L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            UPDATE DISTRIREFEICAO I
            SET I.NRLANCESTQENTR = NULL
            WHERE ROWID = C_DISTRIREFEICAO.LINHA;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               V_COUNT_COMMIT:= 1;
               COMMIT;
            ELSE
              V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_DELIVERY IN (SELECT C.ROWID AS LINHA
                              FROM DELIVERY C, LANCTOESTOQ L
                             WHERE C.CDFILIAL      = L.CDFILIAL     AND
                                   C.NRLANCFECHDEL = L.NRLANCESTQ   AND
                                   L.CDFILIMOVI   = V_CDFILIAL       AND
                                   L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            UPDATE DELIVERY I
            SET I.NRLANCFECHDEL = NULL
            WHERE ROWID = C_DELIVERY.LINHA;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               V_COUNT_COMMIT:= 1;
               COMMIT;
            ELSE
              V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_PRODDISTPROD IN (SELECT C.ROWID AS LINHA
                               FROM PRODDISTPROD C, LANCTOESTOQ L
                               WHERE C.CDFILANCTO     = L.CDFILIAL     AND
                                     C.NRLANCESTQSAID = L.NRLANCESTQ   AND
                                     L.CDFILIMOVI   = V_CDFILIAL       AND
                                     L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            UPDATE PRODDISTPROD I
            SET I.CDFILANCTO     = NULL,
                I.NRLANCESTQSAID = NULL
            WHERE ROWID = C_PRODDISTPROD.LINHA;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               V_COUNT_COMMIT:= 1;
               COMMIT;
            ELSE
              V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

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

         FOR C_PRODDISTPROD IN (SELECT C.ROWID AS LINHA
                               FROM PRODDISTPROD C, LANCTOESTOQ L
                               WHERE C.CDFILANCTO     = L.CDFILIAL     AND
                                     C.NRLANCESTQENTR = L.NRLANCESTQ   AND
                                     L.CDFILIMOVI   = V_CDFILIAL       AND
                                     L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            UPDATE PRODDISTPROD I
            SET I.CDFILANCTO     = NULL,
                I.NRLANCESTQENTR = NULL
            WHERE ROWID = C_PRODDISTPROD.LINHA;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               V_COUNT_COMMIT:= 1;
               COMMIT;
            ELSE
              V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_ITVENDACAN IN (SELECT C.ROWID AS LINHA
                               FROM ITVENDACAN C, LANCTOESTOQ L
                               WHERE C.CDFILIALEST = L.CDFILIAL     AND
                                     C.NRLANCESTQ  = L.NRLANCESTQ   AND
                                     L.CDFILIMOVI   = V_CDFILIAL    AND
                                     L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL AND
                                     L.IDTPLANCTO = '3') LOOP

            UPDATE ITVENDACAN I
            SET I.CDFILIALEST = NULL,
                I.NRLANCESTQ  = NULL
            WHERE ROWID = C_ITVENDACAN.LINHA;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               V_COUNT_COMMIT:= 1;
               COMMIT;
            ELSE
              V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_ITEMVENDA IN (SELECT C.ROWID AS LINHA
                               FROM ITEMVENDA C, LANCTOESTOQ L
                               WHERE C.CDFILIALEST = L.CDFILIAL     AND
                                     C.NRLANCESTQ  = L.NRLANCESTQ   AND
                                     L.CDFILIMOVI   = V_CDFILIAL    AND
                                     L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL  AND
                                     L.IDTPLANCTO = '3') LOOP

            UPDATE ITEMVENDA I
            SET I.CDFILIALEST = NULL,
                I.NRLANCESTQ  = NULL
            WHERE ROWID = C_ITEMVENDA.LINHA;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               V_COUNT_COMMIT:= 1;
               COMMIT;
            ELSE
              V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_CONSUMOREAL IN (SELECT C.ROWID AS LINHA
                               FROM CONSUMOREAL C, LANCTOESTOQ L
                               WHERE C.CDFILIAL   = L.CDFILIAL     AND
                                     C.NRLANCESTQ = L.NRLANCESTQ   AND
                                     L.CDFILIMOVI = V_CDFILIAL     AND
                                     L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            UPDATE CONSUMOREAL I
            SET I.CDFILIAL   = NULL,
                I.NRLANCESTQ = NULL
            WHERE ROWID = C_CONSUMOREAL.LINHA;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               V_COUNT_COMMIT:= 1;
               COMMIT;
            ELSE
              V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_COMPITVENDA IN (SELECT I.ROWID AS LINHA
                             FROM COMPITVENDA I, VENDA V
                             WHERE I.CDFILIAL = V_CDFILIAL AND
                                   V.DTVENDA BETWEEN V_DTINICIAL AND P_DTFINAL  AND
                                   I.CDFILIAL   = V.CDFILIAL      AND
                                   I.CDCAIXA    = V.CDCAIXA       AND
                                   I.NRSEQVENDA = V.NRSEQVENDA    AND
                                   I.NRLANCESTQ IS NOT NULL) LOOP

            UPDATE COMPITVENDA I
            SET I.CDFILIALEST = NULL,
                I.NRLANCESTQ  = NULL
            WHERE  ROWID = C_COMPITVENDA.LINHA;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               V_COUNT_COMMIT:= 1;
               COMMIT;
            ELSE
              V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_PRODDISTPROD IN (SELECT C.ROWID AS LINHA
                               FROM PRODDISTPROD C, LANCTOESTOQ L
                               WHERE C.CDFILANCTO   = L.CDFILIAL     AND
                                     C.NRLANCESTQENTR = L.NRLANCESTQ   AND
                                     L.CDFILIMOVI = V_CDFILIAL     AND
                                     L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            UPDATE PRODDISTPROD I
            SET I.CDFILANCTO   = NULL,
                I.NRLANCESTQENTR = NULL
            WHERE ROWID = C_PRODDISTPROD.LINHA;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               V_COUNT_COMMIT:= 1;
               COMMIT;
            ELSE
              V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
            END IF;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'AJURELACRET';

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_AJURELACRET IN (SELECT A.ROWID AS LINHA
                               FROM AJURELACRET A, LANCTOESTOQ L
                               WHERE A.CDFILIAL    = L.CDFILIAL        AND
                                     A.NRLANCESTQ  = L.NRLANCESTQ      AND
                                     L.CDFILIMOVI  = V_CDFILIAL AND
                                     L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL
                               UNION
                               SELECT A.ROWID AS LINHA
                               FROM AJURELACRET A, LANCTOESTOQ L
                               WHERE A.CDFILIALRET = L.CDFILIAL        AND
                                     A.NRLANCESTRET = L.NRLANCESTQ      AND
                                     L.CDFILIMOVI  = V_CDFILIAL AND
                                     L.DTLANCESTQ  BETWEEN V_DTINICIAL AND P_DTFINAL

                               ) LOOP

            DELETE AJURELACRET
            WHERE ROWID = C_AJURELACRET.LINHA;
            V_EXCLUAJURELACRET := SQL%ROWCOUNT + V_EXCLUAJURELACRET;

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

              /*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;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;

      CLOSE C_LISTA;

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

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'NFABASTEROTA';

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO := 6;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_NFABASTEROTA IN (SELECT C.ROWID AS LINHA
                                FROM NFABASTEROTA C, LANCTOESTOQ L
                                WHERE C.CDFILIAL   = L.CDFILIAL        AND
                                      C.NRLANCESTQ = L.NRLANCESTQ      AND
                                      L.CDFILIMOVI = V_CDFILIAL AND
                                      L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            DELETE NFABASTEROTA
            WHERE ROWID = C_NFABASTEROTA.LINHA;
            V_EXCLUNFABASTEROTA := SQL%ROWCOUNT + V_EXCLUNFABASTEROTA;

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

              /*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;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;

      CLOSE C_LISTA;

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

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'PREVPDVLESTO';

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_PREVPDVLESTO IN (SELECT C.ROWID AS LINHA
                                FROM PREVPDVLESTO C, LANCTOESTOQ L
                                WHERE C.CDFILANCESTQ = L.CDFILIAL        AND
                                      C.NRLANCESTQ   = L.NRLANCESTQ      AND
                                      L.CDFILIMOVI   = V_CDFILIAL AND
                                      L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            DELETE PREVPDVLESTO
            WHERE ROWID = C_PREVPDVLESTO.LINHA;
            V_EXCLUPREVPDVLESTO := SQL%ROWCOUNT + V_EXCLUPREVPDVLESTO;

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

              /*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;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;

      CLOSE C_LISTA;

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

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'CUSTOCMPPROD';

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_CUSTOCMPPROD IN (SELECT C.ROWID AS LINHA
                                FROM CUSTOCMPPROD C, LANCTOESTOQ L
                                WHERE C.CDFILLANCEST = L.CDFILIAL        AND
                                      C.NRLANCESTQ   = L.NRLANCESTQ      AND
                                      L.CDFILIMOVI   = V_CDFILIAL AND
                                      L.DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            DELETE CUSTOCMPPROD
            WHERE ROWID = C_CUSTOCMPPROD.LINHA;
            V_EXCLUCUSTOCMPPROD := SQL%ROWCOUNT + V_EXCLUCUSTOCMPPROD;

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

              /*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;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;

      CLOSE C_LISTA;

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

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'LANCTOESTOQ';

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_LANCTOESTOQ IN (SELECT ROWID AS LINHA, CDFILIAL, NRLANCESTQ
                               FROM LANCTOESTOQ
                               WHERE CDFILIMOVI  = V_CDFILIAL
                                 AND DTLANCESTQ BETWEEN V_DTINICIAL AND P_DTFINAL
                                 AND UPPER(DSLANCESTQ) NOT LIKE UPPER('%Limpeza de Dados%')
                                 AND IDTPLANCTO = '0') LOOP

            FOR C_DEL IN
               (SELECT ROWID
                  FROM RELALESTLANCLOT
                 WHERE CDFILIAL = C_LANCTOESTOQ.CDFILIAL
                   AND NRLANCESTQ = C_LANCTOESTOQ.NRLANCESTQ) LOOP

               DELETE FROM RELALESTLANCLOT
                WHERE ROWID = C_DEL.ROWID;
            END LOOP;

            FOR C_DEL IN
               (SELECT ROWID
                  FROM CTRLMOVIESTQ
                 WHERE CDFILIALENTR = C_LANCTOESTOQ.CDFILIAL
                   AND NRLANESTENTR = C_LANCTOESTOQ.NRLANCESTQ) LOOP

               DELETE FROM CTRLMOVIESTQ
                WHERE ROWID = C_DEL.ROWID;
            END LOOP;

            FOR C_DEL IN
               (SELECT ROWID
                  FROM CTRLMOVIESTQ
                 WHERE CDFILIALSAID = C_LANCTOESTOQ.CDFILIAL
                   AND NRLANESTSAID = C_LANCTOESTOQ.NRLANCESTQ) LOOP

               DELETE FROM CTRLMOVIESTQ
                WHERE ROWID = C_DEL.ROWID;
            END LOOP;

            FOR C_DEL IN
               (SELECT ROWID
                  FROM RETIRADA
                 WHERE CDFILANCESTQ = C_LANCTOESTOQ.CDFILIAL
                   AND NRLANCESTQ   = C_LANCTOESTOQ.NRLANCESTQ) LOOP

               DELETE FROM RETIRADA
                WHERE ROWID = C_DEL.ROWID;
            END LOOP;

            DELETE LANCTOESTOQ
            WHERE ROWID = C_LANCTOESTOQ.LINHA;
            V_EXCLULANCTOESTOQ := SQL%ROWCOUNT + V_EXCLULANCTOESTOQ;

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

              /*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;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;

      CLOSE C_LISTA;

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

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'ROMANEIO';

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO := 7;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_ROMANEIO IN (SELECT CDFILIAL, NRROMANEIO, ROWID AS LINHA
                            FROM ROMANEIO
                            WHERE CDFILIEMITRO = V_CDFILIAL AND
                                  DTROMANEIO BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP
            DELETE ROMANEIONF
             WHERE CDFILIAL = C_ROMANEIO.CDFILIAL
               AND NRROMANEIO = C_ROMANEIO.NRROMANEIO;

            DELETE ROMANEIO
            WHERE ROWID = C_ROMANEIO.LINHA;
            V_EXCLUROMANEIO := SQL%ROWCOUNT + V_EXCLUROMANEIO;

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

              /*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;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;

      CLOSE C_LISTA;

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

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'POSESTDIA';

      V_COUNT_COMMIT := 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      -- Gera Implantacao de Estoque para Data da Limpeza
      V_PASSO := 8;

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

         V_COUNT_IMPLANTACAO := 0;
         SELECT COUNT(1)
           INTO V_COUNT_IMPLANTACAO
           FROM LANCTOESTOQ
          WHERE CDFILIAL = V_CDFILIPROC
            AND CDFILIMOVI = V_CDFILIAL
            AND DTLANCESTQ = P_DTFINAL
            AND UPPER(DSLANCESTQ) LIKE UPPER('%Limpeza de Dados%')
            and IDTPLANCTO = '0';

         IF (NOT (NVL(V_COUNT_IMPLANTACAO, 0) > 0)) THEN
            V_NRSEQUITEM := 1;
            FOR C_IMPLANTACAO IN (SELECT CDALMOXARIFE, CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE,
                                         CDPRODUTO, ROUND(QTESTOQDIA,5) AS QTESTOQDIA, ROUND(VRESTOQDIA,5) AS VRESTOQDIA,
                                         ROUND(VRESTOQBRUT,5) AS VRESTOQBRUT
                                  FROM POSESTDIA P
                                  WHERE CDFILIAL   = V_CDFILIAL AND
                                        QTESTOQDIA <> 0                 AND
                                        DTPOSIESTQ = (SELECT MAX(DTPOSIESTQ)
                                                      FROM POSESTDIA
                                                      WHERE CDFILIAL     = P.CDFILIAL     AND
                                                            CDALMOXARIFE = P.CDALMOXARIFE AND
                                                            CDLOCALESTOQ = P.CDLOCALESTOQ AND
                                                            NRLOTEESTQ   = P.NRLOTEESTQ   AND
                                                            NRSUBLOTE    = P.NRSUBLOTE    AND
                                                            CDPRODUTO    = P.CDPRODUTO    AND
                                                            DTPOSIESTQ   <= TO_DATE(P_DTFINAL + 1)))
            LOOP
               IF V_NRSEQUITEM = 1 THEN
                  V_NRLANCESTQ := '';
                  NOVO_CODIGO('LANCTOEST' || V_CDFILIPROC, V_NRLANCESTQ);
                  INSERT INTO LANCTOESTOQ
                  (CDFILIAL,NRLANCESTQ,CDOPERADOR,IDTPLANCTO,DTLANCESTQ,CDFILIMOVI, DSLANCESTQ)
                  VALUES
                  (V_CDFILIPROC, SUBSTR(V_NRLANCESTQ,11,10), V_CDOPERADOR, '0', P_DTFINAL, V_CDFILIAL, 'Implantacao por Limpeza de Dados');
               END IF;
               INSERT INTO ITLANCTOEST
                  (CDFILIAL, NRLANCESTQ, NRSEQUITEM, CDPRODUTO,
                   QTTOTLANCTO, VRUNILANCTO, VRTOTLANCTO, IDTIPOMOVI,
                   DTLANCMOVI, IDORDEMCLC, CDPRODMOVI, QTLANCTOEST,
                   VRLANCTOBRUT, VRLANCTOEST, CDFILIMOVI, IDEVOLESTOQ,
                   CDALMOXARIFE, CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE)
               VALUES
                  (V_CDFILIPROC, SUBSTR(V_NRLANCESTQ,11,10), LPAD(TO_CHAR(V_NRSEQUITEM), 4, '0'), C_IMPLANTACAO.CDPRODUTO,
                   C_IMPLANTACAO.QTESTOQDIA, DECODE(C_IMPLANTACAO.QTESTOQDIA, 0, C_IMPLANTACAO.VRESTOQDIA, ROUND((C_IMPLANTACAO.VRESTOQDIA/C_IMPLANTACAO.QTESTOQDIA),5)), C_IMPLANTACAO.VRESTOQDIA, '0',
                   P_DTFINAL, '0', C_IMPLANTACAO.CDPRODUTO, C_IMPLANTACAO.QTESTOQDIA,
                   C_IMPLANTACAO.VRESTOQBRUT, C_IMPLANTACAO.VRESTOQDIA, V_CDFILIAL, 'S',
                   C_IMPLANTACAO.CDALMOXARIFE, C_IMPLANTACAO.CDLOCALESTOQ, C_IMPLANTACAO.NRLOTEESTQ, C_IMPLANTACAO.NRSUBLOTE);
               V_NRSEQUITEM := V_NRSEQUITEM + 1;

               IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
                 V_COUNT_COMMIT := 1;
                 COMMIT;
               ELSE
                 V_COUNT_COMMIT := V_COUNT_COMMIT + 1;
               END IF;
            END LOOP;
            ATUALIZA_ESTOQUE(V_CDFILIAL, P_DTFINAL, SYSDATE, V_CDFILIPROC, SUBSTR(V_NRLANCESTQ,11,10));
         END IF;
         COMMIT;
      END LOOP;
      CLOSE C_LISTA;

      V_COUNT_COMMIT := 1;
       /*Verifica se a procedure estourou o tempo limite*/
       VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO := 9;

      V_EXCLUPOSESTDIA := 0;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_POSESTDIA IN (SELECT ROWID
                             FROM POSESTDIA
                             WHERE CDFILIAL    = V_CDFILIAL AND
                                   DTPOSIESTQ BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            DELETE POSESTDIA
            WHERE POSESTDIA.ROWID = C_POSESTDIA.ROWID;

            V_EXCLUPOSESTDIA := SQL%ROWCOUNT + V_EXCLUPOSESTDIA;

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

              /*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;
            /*Verifica se a procedure estourou o tempo limite*/
            VERIFTIMEOUT(V_DTHRFINLIMPEZA);
         END LOOP;
      END LOOP;

      CLOSE C_LISTA;

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

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'POSESTCONS';

      V_COUNT_COMMIT:= 1;
      COMMIT;
       /*Verifica se a procedure estourou o tempo limite*/
       VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO := 10;

      V_EXCLUPOSESTCONS := 0;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_POSESTCONS IN (SELECT ROWID
                              FROM POSESTCONS
                              WHERE CDFILIAL    = V_CDFILIAL AND
                                    DTPOSESTCON BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            DELETE POSESTCONS
             WHERE POSESTCONS.ROWID = C_POSESTCONS.ROWID;

            V_EXCLUPOSESTCONS := SQL%ROWCOUNT + V_EXCLUPOSESTCONS;

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

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

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

      CLOSE C_LISTA;

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

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'INVEROTA';

      V_COUNT_COMMIT:= 1;
      COMMIT;
       /*Verifica se a procedure estourou o tempo limite*/
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO := 11;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_INVEROTA IN (SELECT ROWID
                              FROM INVEROTA
                             WHERE CDFILIAL    = V_CDFILIAL
                               AND DTINVEROTA BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            DELETE INVEROTA
             WHERE INVEROTA.ROWID = C_INVEROTA.ROWID;

            V_EXCLUINVEROTA := SQL%ROWCOUNT + V_EXCLUINVEROTA;

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

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

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

      CLOSE C_LISTA;

      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_INVEROTALOTE IN (SELECT ROWID
                              FROM INVEROTALOTE
                             WHERE CDFILIAL   = V_CDFILIAL
                               AND DTINVELOTE BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

            DELETE INVEROTALOTE
             WHERE INVEROTALOTE.ROWID = C_INVEROTALOTE.ROWID;

            V_EXCLUINVEROTA := SQL%ROWCOUNT + V_EXCLUINVEROTA;

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

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

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

      CLOSE C_LISTA;

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

      /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'DM_INFLACAO';

      V_COUNT_COMMIT:= 1;
      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO := 12;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_DM_INFLACAO IN (SELECT ROWID
                                 FROM DM_INFLACAO
                                WHERE CDFILIAL = V_CDFILIAL
                                  AND CDANOBI  BETWEEN '1990' AND TO_CHAR(P_DTFINAL, 'YYYY')
                                  AND CDMESBI  BETWEEN '01'   AND TO_CHAR(P_DTFINAL, 'MM')) LOOP

            DELETE DM_INFLACAO
             WHERE DM_INFLACAO.ROWID = C_DM_INFLACAO.ROWID;

            V_EXCLUIDM_INFLACAO := SQL%ROWCOUNT + V_EXCLUIDM_INFLACAO;

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

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

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

      CLOSE C_LISTA;

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

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

      V_COUNT_COMMIT:= 1;

      COMMIT;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      COMMIT;
   END IF;
EXCEPTION
WHEN OTHERS THEN
   V_ERR0R := SQLERRM(SQLCODE);

   IF INSTR(V_ERR0R, '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_EXCLUPOSESTDIA > 0) THEN
      INSERT INTO LOGLIMPEZA
        (DSSISTLIMPEZ, NMTABLIMP, DTLIMPDADOS, NMOPERLIMPEZ, DTFINALIMPEZ, DSLIMPEZA, DSRDMUSULOGL, DSENDUSULOGL, DSNMUSUOLOGL, DSMETUSULOGL, QTDLINHAEXCL)
      VALUES
        (P_DSSISTLIMPEZ, 'POSESTDIA', SYSDATE, P_NMOPERLIMPEZ, P_DTFINAL, 'POSESTDIA', V_DSRDM, V_DSIP, V_DSUSUARIO, V_DSMETODO, V_EXCLUPOSESTDIA);
   END IF;

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

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

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

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

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

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

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

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

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

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

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

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

   COMMIT;

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

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

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

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

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

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

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

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

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

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

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