create or replace PROCEDURE LIMPEZA_DADOGER(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_COUNT_COMMIT       NUMBER := 1;
V_NUM_TO_COMMIT      NUMBER := 10; -- NUMERO UTILIZADO PARA REGULARIZAR A QUANTIDADE DE DMLS PARA O COMMIT.
V_CDFILIAL           VARCHAR2(4);
V_EXCLULOGOPERA      NUMBER := 0;
V_EXCLULOGACESSO     NUMBER := 0;
V_EXCLULOGATIVFILIAL NUMBER := 0;
V_EXCLUDELETECACHE   NUMBER := 0;
V_EXCLUPRODFILI      NUMBER := 0;
V_EXCLUDM_INFLACAO   NUMBER := 0;

V_CDOPERADOR         VARCHAR2(12);
V_DTHRFINLIMPEZA     DATE;
V_DTINICIAL          DATE;
V_DTINIANO           VARCHAR2(04);
V_DTFINANO           VARCHAR2(04);
V_DTINIMES           VARCHAR2(02);
V_DTFINMES           VARCHAR2(02);


   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');

   V_DTINIANO  := '1990';
   V_DTFINANO  := TO_CHAR(P_DTFINAL, 'YYYY');
   V_DTINIMES  := '01';
   V_DTFINMES  := TO_CHAR(P_DTFINAL, 'MM');

   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;

   DELETE FROM TRABPROCLIMPEZA
   WHERE IDLIMPEZA = P_IDSISTEMA;

   V_NRORDEM := '0';

   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;
   
   BEGIN
      EXECUTE IMMEDIATE 'TRUNCATE TABLE LISTA';
   EXCEPTION
      WHEN OTHERS THEN NULL;
   END;

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

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

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

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

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

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

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

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

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

   BEGIN
      EXECUTE IMMEDIATE 'TRUNCATE TABLE TMP_REGDADOSGIASP';
   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 = 'LOGOPERA';

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

   INSERT INTO TRABPROCLIMPEZA
      (IDLIMPEZA, NMTABELALIMP)
   VALUES
      (P_IDSISTEMA, 'LOGOPERA');

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

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

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

   INSERT INTO TRABPROCLIMPEZA
      (IDLIMPEZA, NMTABELALIMP)
   VALUES
      (P_IDSISTEMA, 'LOGACESSO');

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

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

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

   INSERT INTO TRABPROCLIMPEZA
      (IDLIMPEZA, NMTABELALIMP)
   VALUES
      (P_IDSISTEMA, 'LOGATIVFILIAL');

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

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

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

   INSERT INTO TRABPROCLIMPEZA
      (IDLIMPEZA, NMTABELALIMP)
   VALUES
      (P_IDSISTEMA, 'DELETECACHE');
      
   
   V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

   INSERT INTO TRABPROCLIMPEZA
      (IDLIMPEZA, NMTABELALIMP)
   VALUES
      (P_IDSISTEMA, 'PRODFILI');


   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;

   INSERT INTO TRABPROCLIMPEZA
      (IDLIMPEZA, NMTABELALIMP)
   VALUES
      (P_IDSISTEMA, 'DM_INFLACAO');

   BEGIN
      EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_EST_PRODFILI DISABLE';
   EXCEPTION
      WHEN OTHERS THEN NULL;
   END;
   
   BEGIN
      EXECUTE IMMEDIATE 'ALTER TRIGGER DTULTATU_PRODFILI DISABLE';
   EXCEPTION
      WHEN OTHERS THEN NULL;
   END;
  
  /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
   UPDATE TRABPROCLIMPEZA
      SET IDSITUPROCLIMP = 'A'
    WHERE IDLIMPEZA = P_IDSISTEMA 
      AND NMTABELALIMP = 'LOGOPERA';

   COMMIT;
   VERIFTIMEOUT(V_DTHRFINLIMPEZA);

   V_PASSO := 0;

      FOR C_LOGOPERA IN(SELECT ROWID LINHA 
                          FROM LOGOPERA
                         WHERE TO_DATE(DTOPERACAO) BETWEEN V_DTINICIAL AND P_DTFINAL)LOOP
                         
          DELETE LOGOPERA
           WHERE ROWID = C_LOGOPERA.LINHA;

          V_EXCLULOGOPERA := SQL%ROWCOUNT + V_EXCLULOGOPERA;
          
          IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
                
                UPDATE TRABPROCLIMPEZA
                  SET QTREGEXCLIMPEZA = V_EXCLULOGOPERA
                WHERE IDLIMPEZA = P_IDSISTEMA
                  AND NMTABELALIMP = 'LOGOPERA';

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

   V_PASSO := 1;

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

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

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

      FOR C_LOGACESSO IN (SELECT ROWID AS LINHA 
                            FROM LOGACESSO
                           WHERE TO_DATE(DTHRACESSO) BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

         DELETE LOGACESSO
          WHERE ROWID = C_LOGACESSO.LINHA;
         
         V_EXCLULOGACESSO := SQL%ROWCOUNT + V_EXCLULOGACESSO;

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

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

   V_PASSO := 2;

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

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

   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_LOGATIVFILIAL IN (SELECT ROWID AS LINHA 
                                FROM LOGATIVFILIAL
                               WHERE CDFILIAL   = V_CDFILIAL 
                                 AND TO_DATE(DTLOGATIVFILIAL)  BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

         DELETE LOGATIVFILIAL
         WHERE ROWID = C_LOGATIVFILIAL.LINHA;
           
         V_EXCLULOGATIVFILIAL := SQL%ROWCOUNT + V_EXCLULOGATIVFILIAL;

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

           /*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_EXCLULOGATIVFILIAL,
          DTHRFINLIMPEZA = SYSDATE
    WHERE IDLIMPEZA = P_IDSISTEMA
      AND NMTABELALIMP = 'LOGATIVFILIAL';

   V_COUNT_COMMIT:= 1;
   COMMIT;
   VERIFTIMEOUT(V_DTHRFINLIMPEZA);
   
   V_PASSO := 5;
   
   /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
   UPDATE TRABPROCLIMPEZA
      SET IDSITUPROCLIMP = 'A'
    WHERE IDLIMPEZA = P_IDSISTEMA
      AND NMTABELALIMP = 'DELETECACHE';

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

   V_PASSO := 6;
      
      FOR C_DELETECACHE IN (SELECT ROWID AS LINHA 
                                FROM DELETECACHE
                               WHERE TO_DATE(DTULTATUCACH) BETWEEN V_DTINICIAL AND P_DTFINAL) LOOP

         DELETE DELETECACHE
         WHERE ROWID = C_DELETECACHE.LINHA;
           
         V_EXCLUDELETECACHE := SQL%ROWCOUNT + V_EXCLUDELETECACHE;

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

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

   V_PASSO := 7;

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

   COMMIT;
   VERIFTIMEOUT(V_DTHRFINLIMPEZA);

   V_PASSO := 8;

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

      FOR C_PRODFILI IN(SELECT P.ROWID LINHA
                          FROM PRODFILI P, FILIAL F
                         WHERE P.CDFILIAL = V_CDFILIAL
                           AND F.CDFILIAL = P.CDFILIAL
                           AND F.IDCNTRRESC = 'S')
      LOOP
         DELETE PRODFILI
          WHERE ROWID = C_PRODFILI.LINHA;

         V_EXCLUPRODFILI := SQL%ROWCOUNT + V_EXCLUPRODFILI;

         IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN

               UPDATE TRABPROCLIMPEZA
                 SET QTREGEXCLIMPEZA = V_EXCLUPRODFILI
               WHERE IDLIMPEZA = P_IDSISTEMA
                 AND NMTABELALIMP = 'PRODFILI';

              /*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_EXCLUPRODFILI,
          DTHRFINLIMPEZA = SYSDATE
    WHERE IDLIMPEZA = P_IDSISTEMA
      AND NMTABELALIMP = 'PRODFILI';

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

   COMMIT;
   VERIFTIMEOUT(V_DTHRFINLIMPEZA);

   V_PASSO := 9;

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

      FOR C_DM_INFLACAO IN (SELECT ROWID AS LINHA
                               FROM DM_INFLACAO
                              WHERE CDFILIAL = V_CDFILIAL
                                AND CDANOBI BETWEEN V_DTINIANO AND V_DTFINANO
                                AND CDMESBI BETWEEN V_DTINIMES AND V_DTFINMES) LOOP

         DELETE DM_INFLACAO
          WHERE ROWID = C_DM_INFLACAO.LINHA;

         V_EXCLUDM_INFLACAO := SQL%ROWCOUNT + V_EXCLUDM_INFLACAO;

         IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN

            UPDATE TRABPROCLIMPEZA
              SET QTREGEXCLIMPEZA = V_EXCLUDM_INFLACAO
            WHERE IDLIMPEZA = 'GER'
              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_EXCLUDM_INFLACAO,
          DTHRFINLIMPEZA = SYSDATE
    WHERE IDLIMPEZA = P_IDSISTEMA
      AND NMTABELALIMP = 'DM_INFLACAO';
--fab
      
    UPDATE TRABLIMPEZA
      SET IDSTATUSLIMPEZA = 'C', IDLIMPEZACONCERRO = 'N'
    WHERE IDLIMPEZA = P_IDSISTEMA;

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

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

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

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

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

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

   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_EXCLUDM_INFLACAO);

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

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

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

   COMMIT;
EXCEPTION
WHEN OTHERS THEN

   BEGIN
      EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_EST_PRODFILI ENABLE';
   EXCEPTION
      WHEN OTHERS THEN NULL;
   END;
   
   BEGIN
      EXECUTE IMMEDIATE 'ALTER TRIGGER DTULTATU_PRODFILI ENABLE';
   EXCEPTION
      WHEN OTHERS THEN NULL;
   END;
   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_EXCLULOGOPERA > 0) THEN
      INSERT INTO LOGLIMPEZA
        (DSSISTLIMPEZ, NMTABLIMP, DTLIMPDADOS, NMOPERLIMPEZ, DTFINALIMPEZ, DSLIMPEZA, DSRDMUSULOGL, DSENDUSULOGL, DSNMUSUOLOGL, DSMETUSULOGL, QTDLINHAEXCL)
      VALUES
        (P_DSSISTLIMPEZ, 'LOGOPERA', SYSDATE, P_NMOPERLIMPEZ, P_DTFINAL, 'LOGOPERA', V_DSRDM, V_DSIP, V_DSUSUARIO, V_DSMETODO, V_EXCLULOGOPERA);
   END IF;

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

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

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

   COMMIT;

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