create or replace PROCEDURE LIMPA_TABELAS_PLA(
   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 := 250; -- NUMERO UTILIZADO PARA REGULARIZAR A QUANTIDADE DE DMLS PARA O COMMIT.

   V_EXCLURETIRADA        NUMBER := 0;
   V_EXCLUCNFCAMES        NUMBER := 0;
   V_EXCLUCARDADIA        NUMBER := 0;
   V_EXCLUPREVRETI        NUMBER := 0;
   V_EXCLULOGPREVRETI     NUMBER := 0;
   V_EXCLUPREVCONS        NUMBER := 0;
   V_EXCLULOGPREVCONS     NUMBER := 0;
   V_EXCLULOGSUBCONSSER   NUMBER := 0;
   V_EXCLSERVTRAN         NUMBER := 0;
   V_EXCLULOGSERVTRAN     NUMBER := 0;
   
   V_EXCLUCONFEMISCARD    NUMBER := 0;
   V_EXCLUCARDIAPME       NUMBER := 0;
   V_EXCLUCOMPCARDPME     NUMBER := 0;
   V_EXCLUCONFCARDPME     NUMBER := 0;
   V_EXCLUCARDAPIOPME     NUMBER := 0;

   V_CDFILIAL          VARCHAR2(4);

   V_DTHRINILIMPEZA    DATE;
   V_DTHRFINLIMPEZA    DATE;
   V_DTINICIAL         DATE;

   V_IDSTATUSLIMPEZA   VARCHAR2(01);

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

   PROCEDURE VerifTimeOut(DTLIMITE IN DATE)
   IS
   BEGIN
      IF SYSDATE > DTLIMITE THEN
         COMMIT;
         RAISE_APPLICATION_ERROR(-20001,'TIMEOUT - EXCECAO PROPOSITAL TEKNISA');
      END IF;
   END;

BEGIN
   V_DTINICIAL := TO_DATE('01/01/1990');

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      V_NRORDEM := '0';

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      COMMIT;

      /* TABELA RETIRADA  */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

       IF V_IDSTATUSLIMPEZA <> 'P' THEN
          INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP)
          VALUES
             (P_IDSISTEMA, 'RETIRADA');
       END IF;
       
      /* TABELA CARDADIA  */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

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

      /* TABELA PREVRETI  */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

       IF V_IDSTATUSLIMPEZA <> 'P' THEN
          INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP)
          VALUES
             (P_IDSISTEMA, 'PREVRETI');
       END IF;
       
      /* TABELA LOGPREVRETI  */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'LOGPREVRETI');
      END IF;
      
       /* TABELA PREVCONS  */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'PREVCONS');
      END IF;
      
      /* TABELA LOGPREVCONS  */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP)
         VALUES
             (P_IDSISTEMA, 'LOGPREVCONS');
      END IF;
      
       /* TABELA LOGSUBCONSSER  */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP)
         VALUES
             (P_IDSISTEMA, 'LOGSUBCONSSER');
      END IF;
      
       /* TABELA SERVTRAN  */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP)
         VALUES
             (P_IDSISTEMA, 'SERVTRAN');
      END IF;
      
       /* TABELA LOGSERVTRAN  */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP)
         VALUES
             (P_IDSISTEMA, 'LOGSERVTRAN');
      END IF;
      /*TABELA CARDAPIOPME */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'CARDAPIOPME');
      END IF;
      
      /*TABELA CONFCARDPME */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'CONFCARDPME');
      END IF;
      
      /*TABELA CONFEMISCARDPME */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'CONFEMISCARDPME');
      END IF;
      
      /*TABELA CARDIAPME */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'CARDIAPME');
      END IF;
      
      /*TABELA COMPCARDPME */
      V_NRORDEM := LPAD(TO_CHAR(TO_NUMBER(V_NRORDEM)+1), 3, '0');

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
            (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'COMPCARDPME');
      END IF;
      
      V_PASSO:= 1;

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

      /* PRECISA DO COMMIT PARA PODER  ATUALIZAR A TELA */
      V_COUNT_COMMIT := 1;
      COMMIT;

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

         FOR C_RETIRADA IN
           (SELECT ROWID AS LINHA, RETIRADA.CDFILIAL, RETIRADA.NRCARDAPIO, RETIRADA.NRRETIRADA
            FROM RETIRADA
            WHERE CDFILIAL    = V_CDFILIAL
              AND DTRETIRADA BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            DELETE FROM RETIRADA
             WHERE ROWID = C_RETIRADA.LINHA;
            V_EXCLURETIRADA := SQL%ROWCOUNT + V_EXCLURETIRADA;

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

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

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

      /* PRECISA DO COMMIT PARA PODER  ATUALIZAR A TELA */
      V_COUNT_COMMIT:= 1;
      COMMIT;
      /*Verifica se a procedure estourou o tempo limite*/
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO:= 2;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         FOR C_SERVICO IN (SELECT CDSERVFILI
                             FROM SERVICO
                            WHERE CDFILIAL = V_CDFILIAL)
         LOOP
            FOR C_CARDADIA IN
               (SELECT CARDADIA.CDFILIAL, CARDADIA.CDSERVFILI, ROWID AS LINHA
                  FROM CARDADIA
                 WHERE CARDADIA.CDFILIAL    = V_CDFILIAL
                   AND CARDADIA.CDSERVFILI  = C_SERVICO.CDSERVFILI
                   AND CARDADIA.DTCARDAPIO BETWEEN V_DTINICIAL AND P_DTFINAL
                   AND NOT EXISTS (SELECT CDFILIAL
                                     FROM RETIRADA
                                    WHERE RETIRADA.CDFILIAL = CARDADIA.CDFILIAL
                                      AND RETIRADA.NRCARDAPIO = CARDADIA.NRCARDAPIO)
                  )

            LOOP
               DELETE FROM CARDADIA
               WHERE ROWID = C_CARDADIA.LINHA;
               V_EXCLUCARDADIA := SQL%ROWCOUNT + V_EXCLUCARDADIA;

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

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

            FOR C_CNFCAMES IN
               (SELECT ROWID AS LINHA
                  FROM CNFCAMES
                 WHERE CDFILIAL   = V_CDFILIAL
                   AND CDSERVFILI = C_SERVICO.CDSERVFILI
                   AND DTREFECNFMES BETWEEN V_DTINICIAL AND P_DTFINAL)
            LOOP
               DELETE FROM CNFCAMES
               WHERE ROWID = C_CNFCAMES.LINHA;
               V_EXCLUCNFCAMES := SQL%ROWCOUNT + V_EXCLUCNFCAMES;

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

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

               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            END LOOP;
         END LOOP;
      END LOOP;

      CLOSE C_LISTA;

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

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

      /* PRECISA DO COMMIT PARA PODER  ATUALIZAR A TELA */
      V_COUNT_COMMIT:= 1;
      COMMIT;
      /*Verifica se a procedure estourou o tempo limite*/
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO:= 3;

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

         FOR C_SERVICO IN (SELECT CDSERVFILI
                             FROM SERVICO
                            WHERE CDFILIAL = V_CDFILIAL)
         LOOP
            FOR C_PREVRETI IN
               (SELECT ROWID AS LINHA
                FROM PREVRETI
                WHERE PREVRETI.CDFILIAL   = V_CDFILIAL
                  AND PREVRETI.CDSERVFILI = C_SERVICO.CDSERVFILI
                  AND PREVRETI.DTPREVRETI BETWEEN V_DTINICIAL AND P_DTFINAL)
            LOOP
               DELETE FROM PREVRETI
               WHERE ROWID = C_PREVRETI.LINHA;
               V_EXCLUPREVRETI := SQL%ROWCOUNT + V_EXCLUPREVRETI;

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

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

      CLOSE C_LISTA;

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

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

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

      V_PASSO:= 4;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         /* Deleta Log da Requisicao */
         FOR C_LOGPREVRETI IN
            (SELECT ROWID AS LINHA
             FROM LOGPREVRETI
             WHERE CDFILIAL = V_CDFILIAL
               AND DTHRCALCPR BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            DELETE FROM LOGPREVRETI
            WHERE ROWID = C_LOGPREVRETI.LINHA;
            V_EXCLULOGPREVRETI := SQL%ROWCOUNT + V_EXCLULOGPREVRETI;

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

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

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

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

      V_PASSO:= 5;

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

         FOR C_SERVICO IN (SELECT CDSERVFILI
                             FROM SERVICO
                            WHERE CDFILIAL = V_CDFILIAL)
         LOOP
            FOR C_PREVCONS IN
               (SELECT ROWID AS LINHA
                FROM PREVCONS
                WHERE PREVCONS.CDFILIAL   = V_CDFILIAL
                  AND PREVCONS.CDSERVFILI = C_SERVICO.CDSERVFILI
                  AND PREVCONS.DTPREVCONS BETWEEN V_DTINICIAL AND P_DTFINAL)
            LOOP
               DELETE FROM PREVCONS
               WHERE ROWID = C_PREVCONS.LINHA;
               V_EXCLUPREVCONS := SQL%ROWCOUNT + V_EXCLUPREVCONS;

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

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

      CLOSE C_LISTA;

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

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

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

      V_PASSO:= 6;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         /* Deleta Log da Previsao de Consumo */
         FOR C_LOGPREVCONS IN
            (SELECT
                ROWID AS LINHA
             FROM LOGPREVCONS
             WHERE CDFILIAL = V_CDFILIAL
               AND DTHRCALCPC BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            DELETE FROM LOGPREVCONS
            WHERE ROWID = C_LOGPREVCONS.LINHA;
            V_EXCLULOGPREVCONS := SQL%ROWCOUNT + V_EXCLULOGPREVCONS;
            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               UPDATE TRABPROCLIMPEZA
                  SET QTREGEXCLIMPEZA = V_EXCLULOGPREVCONS
                WHERE IDLIMPEZA = P_IDSISTEMA
                  AND NMTABELALIMP = 'LOGPREVCONS';

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

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

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

      V_PASSO:= 9;

      OPEN C_LISTA;
      LOOP
         FETCH C_LISTA INTO V_CDFILIAL;
         EXIT WHEN C_LISTA%NOTFOUND;
         /* Deleta Log de Substituicao de Cons. Servico */
         FOR C_LOGSUBCONSSER IN
           (SELECT ROWID AS LINHA
            FROM LOGSUBCONSSER
            WHERE DTOPERSUB BETWEEN V_DTINICIAL AND P_DTFINAL
            )
         LOOP
            DELETE FROM LOGSUBCONSSER
            WHERE ROWID = C_LOGSUBCONSSER.LINHA;
            V_EXCLULOGSUBCONSSER := SQL%ROWCOUNT + V_EXCLULOGSUBCONSSER;
            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               UPDATE TRABPROCLIMPEZA
                  SET QTREGEXCLIMPEZA = V_EXCLULOGSUBCONSSER
                WHERE IDLIMPEZA = P_IDSISTEMA
                  AND NMTABELALIMP = 'LOGSUBCONSSER';

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

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

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

      V_PASSO:= 10;

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

         FOR C_SERVICO IN (SELECT CDSERVFILI
                             FROM SERVICO
                            WHERE CDFILIAL = V_CDFILIAL)
         LOOP
            FOR C_SERVTRAN IN
               (SELECT ROWID AS LINHA
                  FROM SERVTRAN
                 WHERE SERVTRAN.CDFILIAL   = V_CDFILIAL
                   AND SERVTRAN.CDSERVFILI = C_SERVICO.CDSERVFILI
                   AND SERVTRAN.DTSERVTRAN BETWEEN V_DTINICIAL AND P_DTFINAL)
            LOOP
               DELETE FROM SERVTRAN
               WHERE ROWID = C_SERVTRAN.LINHA;
               V_EXCLSERVTRAN := SQL%ROWCOUNT + V_EXCLSERVTRAN;
               IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
                  UPDATE TRABPROCLIMPEZA
                     SET QTREGEXCLIMPEZA = V_EXCLSERVTRAN
                   WHERE IDLIMPEZA = P_IDSISTEMA
                     AND NMTABELALIMP = 'SERVTRAN';

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

      CLOSE C_LISTA;

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

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

      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_LOGSERVTRAN IN
            (SELECT ROWID AS LINHA
             FROM LOGSERVTRAN
             WHERE LOGSERVTRAN.CDFILIAL = V_CDFILIAL
               AND LOGSERVTRAN.DTHRCALCST BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            DELETE FROM LOGSERVTRAN
            WHERE ROWID = C_LOGSERVTRAN.LINHA;
            V_EXCLULOGSERVTRAN := SQL%ROWCOUNT + V_EXCLULOGSERVTRAN;
            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               UPDATE TRABPROCLIMPEZA
                  SET QTREGEXCLIMPEZA = V_EXCLULOGSERVTRAN
                WHERE IDLIMPEZA = P_IDSISTEMA
                  AND NMTABELALIMP = 'LOGSERVTRAN';

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

      V_COUNT_COMMIT:= 1;
      COMMIT;

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

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

      V_PASSO:= 14;

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

         FOR C_CARDAPIOPME IN
            (SELECT ROWID AS LINHA, CDFILCENTPME, NRCARDPME
               FROM CARDAPIOPME
              WHERE CARDAPIOPME.CDFILCENTPME = V_CDFILIAL
                AND CARDAPIOPME.DTFINCARDPME BETWEEN V_DTINICIAL AND P_DTFINAL)LOOP

            -->>INICIO DA DELECAO DOS DADOS DA TABELA CONFCARDPME
             /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
             UPDATE TRABPROCLIMPEZA
                SET IDSITUPROCLIMP = 'A'
              WHERE IDLIMPEZA = P_IDSISTEMA
                AND NMTABELALIMP = 'CONFCARDPME';

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

            FOR C_CONFCARDPME IN(SELECT ROWID AS LINHA
                                   FROM CONFCARDPME
                                  WHERE CDFILCENTPME = C_CARDAPIOPME.CDFILCENTPME
                                    AND NRCARDPME    = C_CARDAPIOPME.NRCARDPME)LOOP

               DELETE FROM CONFCARDPME WHERE ROWID = C_CONFCARDPME.LINHA;

               V_EXCLUCONFCARDPME := SQL%ROWCOUNT + V_EXCLUCONFCARDPME;

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

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

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

            V_COUNT_COMMIT:= 1;
            COMMIT;
            -->>FIM DELECAO DOS DADOS DA TABELA CONFCARDPME

            -->>INICIO DA DELECAO DOS DADOS DA TABELA CONFEMISCARDPME
            /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
             UPDATE TRABPROCLIMPEZA
                SET IDSITUPROCLIMP = 'A'
              WHERE IDLIMPEZA = P_IDSISTEMA
                AND NMTABELALIMP = 'CONFEMISCARDPME';

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

            FOR C_CONFEMISCARDPME IN (SELECT ROWID AS LINHA
                                        FROM CONFEMISCARDPME
                                       WHERE CDFILCENTPME = C_CARDAPIOPME.CDFILCENTPME
                                         AND NRCARDPME  = C_CARDAPIOPME.NRCARDPME )LOOP

            DELETE FROM CONFEMISCARDPME WHERE ROWID = C_CONFEMISCARDPME.LINHA;

            V_EXCLUCONFEMISCARD := SQL%ROWCOUNT + V_EXCLUCONFEMISCARD;

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

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

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

            V_COUNT_COMMIT:= 1;
            COMMIT;
            -->>FIM DELECAO DOS DADOS DA TABELA CONFEMISCARDPME

            -->>INICIO DA DELECAO DOS DADOS DA TABELA CARDIAPME
            FOR C_CARDIAPME IN (SELECT ROWID AS LINHA, CDFILCENTPME, NRCARDPME, NRCARDIAPME
                                        FROM CARDIAPME
                                       WHERE CDFILCENTPME = C_CARDAPIOPME.CDFILCENTPME
                                         AND NRCARDPME  = C_CARDAPIOPME.NRCARDPME )LOOP

                   -->>INICIO DA DELECAO DOS DADOS DA TABELA COMPCARDPME
                    /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA ANDAMENTO */
                    UPDATE TRABPROCLIMPEZA
                       SET IDSITUPROCLIMP = 'A'
                     WHERE IDLIMPEZA = P_IDSISTEMA
                       AND NMTABELALIMP = 'COMPCARDPME';

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

                   FOR C_COMPCARDPME IN (SELECT ROWID AS LINHA
                                           FROM COMPCARDPME
                                          WHERE CDFILCENTPME = C_CARDIAPME.CDFILCENTPME
                                            AND NRCARDPME    = C_CARDIAPME.NRCARDPME
                                            AND NRCARDIAPME  = C_CARDIAPME.NRCARDIAPME)LOOP

                       DELETE FROM COMPCARDPME WHERE ROWID = C_COMPCARDPME.LINHA;

                       V_EXCLUCOMPCARDPME := SQL%ROWCOUNT + V_EXCLUCOMPCARDPME;

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

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

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

                   V_COUNT_COMMIT:= 1;
                   COMMIT;
                   -->>FIM DELECAO DOS DADOS DA TABELA COMPCARDPME

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

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

            DELETE FROM CARDIAPME WHERE ROWID = C_CARDIAPME.LINHA;

            V_EXCLUCARDIAPME := SQL%ROWCOUNT + V_EXCLUCARDIAPME;

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

                  /*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;
              /* COLOCA O STATUS DA TABELA DE LIMPEZA PARA CONCLUIDO */
             UPDATE TRABPROCLIMPEZA
                SET IDSITUPROCLIMP = 'C',
                    QTREGEXCLIMPEZA = V_EXCLUCARDIAPME,
                    DTHRFINLIMPEZA = SYSDATE
              WHERE IDLIMPEZA = P_IDSISTEMA
                AND NMTABELALIMP = 'CARDIAPME';

             V_COUNT_COMMIT:= 1;
             COMMIT;
            -->>FIM DELECAO DOS DADOS DA TABELA CARDIAPME

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

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

        DELETE FROM CARDAPIOPME WHERE ROWID = C_CARDAPIOPME.LINHA;

        V_EXCLUCARDAPIOPME := SQL%ROWCOUNT + V_EXCLUCARDAPIOPME;

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

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

      V_COUNT_COMMIT:= 1;
      COMMIT;

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

      COMMIT;

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

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

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

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

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

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

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

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

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

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

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

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_PLA_RETIRADA 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_EXCLURETIRADA > 0) THEN
      INSERT INTO LOGLIMPEZA (DSSISTLIMPEZ, NMTABLIMP, DTLIMPDADOS, NMOPERLIMPEZ, DTFINALIMPEZ, DSLIMPEZA, DSRDMUSULOGL, DSENDUSULOGL, DSNMUSUOLOGL, DSMETUSULOGL, QTDLINHAEXCL)
      VALUES (P_DSSISTLIMPEZ, 'RETIRADA', SYSDATE, P_NMOPERLIMPEZ, P_DTFINAL, 'RETIRADA', V_DSRDM, V_DSIP, V_DSUSUARIO, V_DSMETODO, V_EXCLURETIRADA);
   END IF;

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

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

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

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

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

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

   COMMIT;

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

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

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

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

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

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

END;
/