CREATE OR REPLACE
PROCEDURE LIMPEZA_CMPSUP(
   P_NRLISTA       IN VARCHAR2,
   P_DTFINAL       IN DATE,
   P_DSSISTLIMPEZ  IN VARCHAR2,
   P_NMOPERLIMPEZ  IN VARCHAR2,
   P_IDSISTEMA     IN VARCHAR2)
AS
   V_ERROR             VARCHAR2(2000);
   V_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_EXCLUCOLETAPR     NUMBER := 0;
   V_EXCLUCUSTOSUP     NUMBER := 0;
   V_EXCLUPREVLISTA    NUMBER := 0;
   V_EXCLUITMAPURPREC  NUMBER := 0;
   V_EXCLUSOLICITA     NUMBER := 0;
   V_EXCLUNFRELACAF    NUMBER := 0;
   V_EXCLUNFRELACAFSAI NUMBER := 0;
   V_EXCLUITEMSAIDOL   NUMBER := 0;
   V_EXCLUAUTOFORNE    NUMBER := 0;
   V_EXCLUCALECOMP     NUMBER := 0;
   V_EXCLUFERICOMP     NUMBER := 0;
   V_EXCLUAPURPRECO    NUMBER := 0;
   V_EXCLUPRODCOMPRA   NUMBER := 0;
   V_EXCLUITEXCLISPREC NUMBER := 0;

   V_EXCLUPEDCOLETA    NUMBER := 0;

   V_DTHRINILIMPEZA    DATE;
   V_DTHRFINLIMPEZA    DATE;

   V_CDFILIAL          VARCHAR2(4);
   V_CDFILCENTCMP      VARCHAR2(4);
   V_CDREGIAO          VARCHAR2(3);
   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

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

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

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

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

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

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

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

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

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

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

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

      COMMIT;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      COMMIT;
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      V_PASSO:= 1;

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

         /* DELETA AS TABELAS SOLICITA E MOVOPSOL PRODCOMPRA*/
         FOR C_SOLICITA IN (SELECT ROWID, SOLICITA.CDFILSOLI, SOLICITA.CDFILLANCA, SOLICITA.NRSOLICMP
                              FROM SOLICITA
                             WHERE SOLICITA.CDFILSOLI  = V_CDFILIAL
                               AND SOLICITA.CDFILLANCA <> 'X'
                               AND SOLICITA.DTSOLCMP BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            V_PASSO:= 0;

            UPDATE PEDIDO
               SET CDFILLANCA = NULL,
                   CDFILSOLI = NULL,
                   NRSOLICMP = NULL
             WHERE CDFILLANCA = C_SOLICITA.CDFILLANCA
               AND CDFILSOLI = C_SOLICITA.CDFILSOLI
               AND NRSOLICMP = C_SOLICITA.NRSOLICMP;

            FOR C_DEL IN
               (SELECT ROWID
                  FROM MOVOPSOL
                 WHERE CDFILSOLMOV = C_SOLICITA.CDFILSOLI
                   AND CDFILLANMOV = C_SOLICITA.CDFILLANCA
                   AND NRSOLIMOV   = C_SOLICITA.NRSOLICMP) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM PRODCOMPRA
                 WHERE CDFILSOLI = C_SOLICITA.CDFILSOLI
                   AND CDFILLAN  = C_SOLICITA.CDFILLANCA
                   AND NRSOLICMP = C_SOLICITA.NRSOLICMP) LOOP

               DELETE FROM PRODCOMPRA
                WHERE ROWID = C_DEL.ROWID;

               V_EXCLUPRODCOMPRA := SQL%ROWCOUNT + V_EXCLUPRODCOMPRA;
            END LOOP;

            DELETE SOLICITA
            WHERE ROWID = C_SOLICITA.ROWID;
            V_EXCLUSOLICITA := SQL%ROWCOUNT + V_EXCLUSOLICITA;

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

              /*CONTROLE DE COMMIT A CADA 1000 REGISTROS DELETADOS 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_EXCLUSOLICITA,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'SOLICITA';

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

       /* PRECISA DO COMMIT PARA PODER ATUALIZAR A TELA */
      V_COUNT_COMMIT:= 1;
      COMMIT;
      /*VERIFICA SE A PROCEDURE ESTOUROU O TEMPO LIMITE*/
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

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

      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP = 'A'
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'PEDCOLETA';

      /* 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 := 5;

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

         /* DELETA AS TABELAS COLETAPR, ITMPREVLIST, ITEMCOLE*/
         SELECT NVL(MAX(CDFILCENTCMP), V_CDFILIAL), MAX(CDREGIAO)
           INTO V_CDFILCENTCMP, V_CDREGIAO
           FROM FILIAL
          WHERE FILIAL.CDFILIAL = V_CDFILIAL;

         FOR C_DADOS IN
            (SELECT ROWID, CDFILIAL, NRCOLEPRECO
               FROM COLETAPR
              WHERE CDFILIAL = V_CDFILCENTCMP
                AND DTFINVALCOL BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            FOR C_COLETAPR IN
               (SELECT DISTINCT CDFILIAL, NRCOLEPRECO, CDFORNECED, CDREGIAO, NRPEDIDOCOL
                  FROM ITEMCOLE
                 WHERE CDFILIAL    = C_DADOS.CDFILIAL
                   AND NRCOLEPRECO = C_DADOS.NRCOLEPRECO)
            LOOP
               UPDATE PARTFILI SET CDFILCOTA   = NULL,
                                   NRCOLEPRECO = NULL
                WHERE PARTFILI.CDFILCOTA   = C_COLETAPR.CDFILIAL
                  AND PARTFILI.NRCOLEPRECO = C_COLETAPR.NRCOLEPRECO;

               UPDATE PARAMPRODSUP SET CDFILCOLETA = NULL,
                                       NRCOLEPRECO = NULL
                WHERE PARAMPRODSUP.CDFILCOLETA = C_COLETAPR.CDFILIAL
                  AND PARAMPRODSUP.NRCOLEPRECO = C_COLETAPR.NRCOLEPRECO;

               DELETE FROM COLETAPR
                WHERE ROWID = C_DADOS.ROWID;

               V_EXCLUCOLETAPR := SQL%ROWCOUNT + V_EXCLUCOLETAPR;

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

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

               FOR C_PEDCOLETA IN (SELECT CDREGIAO, CDFORNECED, CDFILIAL, NRPEDCOLETA, NRPEDIDOCOL, ROWID
                                     FROM PEDCOLETA
                                    WHERE CDFILIAL    = C_COLETAPR.CDFILIAL
                                      AND NRPEDIDOCOL = C_COLETAPR.NRPEDIDOCOL)
               LOOP
                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM HISTNEGOCIAPCOL
                       WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                         AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM LOGACESPEDCOL
                       WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                         AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM LOGNGERAPEDCOL
                       WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                         AND NRPEDCOLETA = C_PEDCOLETA.NRPEDCOLETA
                         AND CDFORNECED = C_PEDCOLETA.CDFORNECED
                         AND CDREGIAO = C_PEDCOLETA.CDREGIAO) LOOP

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

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM LOGNGERAPEDWEB
                       WHERE CDFILNGERPEDWEB = C_PEDCOLETA.CDFILIAL
                         AND NRPEDNGERPEDWEB = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM MOVOPITEMPEDCOL
                       WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                         AND NRPEDCOLETA = C_PEDCOLETA.NRPEDCOLETA
                         AND CDFORNECED = C_PEDCOLETA.CDFORNECED
                         AND CDREGIAO = C_PEDCOLETA.CDREGIAO) LOOP

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

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM IMPPEDCOLETA
                       WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                         AND NRPEDCOLETA = C_PEDCOLETA.NRPEDCOLETA
                         AND CDFORNECED = C_PEDCOLETA.CDFORNECED
                         AND CDREGIAO = C_PEDCOLETA.CDREGIAO) LOOP

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

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM FORNEPEDCOL
                       WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                         AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM REGIAOPEDCOL
                       WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                         AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM COMENTPEDCOL
                       WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                         AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM ACEITEPEDCOL
                       WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                         AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM ITEMPEDCOL
                       WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                         AND NRPEDCOLETA = C_PEDCOLETA.NRPEDCOLETA
                         AND CDFORNECED = C_PEDCOLETA.CDFORNECED
                         AND CDREGIAO = C_PEDCOLETA.CDREGIAO) LOOP

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

                  FOR C_DEL IN
                     (SELECT ROWID
                        FROM PRMARCPEDCOL
                       WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                         AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

                  DELETE FROM PEDCOLETA
                   WHERE ROWID = C_PEDCOLETA.ROWID;

                   V_EXCLUPEDCOLETA := SQL%ROWCOUNT + V_EXCLUPEDCOLETA;

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

                     /*CONTROLE DE COMMIT A CADA 1000 REGISTROS DELETADOS 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;

         FOR C_PEDCOLETA IN (SELECT CDREGIAO, CDFORNECED, CDFILIAL, NRPEDCOLETA, NRPEDIDOCOL, ROWID
                              FROM PEDCOLETA
                             WHERE (CDFILIAL     = V_CDFILCENTCMP)
                               AND (CDREGIAO     = V_CDREGIAO)
                               AND (DTFINPERCOT BETWEEN V_DTINICIAL AND P_DTFINAL))
         LOOP
            FOR C_DEL IN
               (SELECT ROWID
                  FROM HISTNEGOCIAPCOL
                 WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                   AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM LOGACESPEDCOL
                 WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                   AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM LOGNGERAPEDCOL
                 WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                   AND NRPEDCOLETA = C_PEDCOLETA.NRPEDCOLETA
                   AND CDFORNECED = C_PEDCOLETA.CDFORNECED
                   AND CDREGIAO = C_PEDCOLETA.CDREGIAO) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM LOGNGERAPEDWEB
                 WHERE CDFILNGERPEDWEB = C_PEDCOLETA.CDFILIAL
                   AND NRPEDNGERPEDWEB = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM MOVOPITEMPEDCOL
                 WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                   AND NRPEDCOLETA = C_PEDCOLETA.NRPEDCOLETA
                   AND CDFORNECED = C_PEDCOLETA.CDFORNECED
                   AND CDREGIAO = C_PEDCOLETA.CDREGIAO) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM IMPPEDCOLETA
                 WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                   AND NRPEDCOLETA = C_PEDCOLETA.NRPEDCOLETA
                   AND CDFORNECED = C_PEDCOLETA.CDFORNECED
                   AND CDREGIAO = C_PEDCOLETA.CDREGIAO) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM FORNEPEDCOL
                 WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                   AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM REGIAOPEDCOL
                 WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                   AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM COMENTPEDCOL
                 WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                   AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM ACEITEPEDCOL
                 WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                   AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM ITEMPEDCOL
                 WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                   AND NRPEDCOLETA = C_PEDCOLETA.NRPEDCOLETA
                   AND CDFORNECED = C_PEDCOLETA.CDFORNECED
                   AND CDREGIAO = C_PEDCOLETA.CDREGIAO) LOOP

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

            FOR C_DEL IN
               (SELECT ROWID
                  FROM PRMARCPEDCOL
                 WHERE CDFILIAL = C_PEDCOLETA.CDFILIAL
                   AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL) LOOP

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

            /*POR GARANTIA, JA QUE UMA COTACAO PODE TER ALTERACAO DE VALIDADE MESMO QUE TENHA SIDO GERADA POR UM PEDIDO.*/
            UPDATE ITEMCOLE
               SET NRPEDIDOCOL = NULL
             WHERE CDFILIAL    = C_PEDCOLETA.CDFILIAL
               AND CDFORNECED  = C_PEDCOLETA.CDFORNECED
               AND CDREGIAO    = C_PEDCOLETA.CDREGIAO
               AND NRPEDIDOCOL = C_PEDCOLETA.NRPEDIDOCOL;

            DELETE FROM PEDCOLETA
             WHERE ROWID = C_PEDCOLETA.ROWID;

            V_EXCLUPEDCOLETA := SQL%ROWCOUNT + V_EXCLUPEDCOLETA;

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

              /*CONTROLE DE COMMIT A CADA 1000 REGISTROS DELETADOS 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;

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

         SELECT NVL(MAX(CDFILCENTCMP), V_CDFILIAL)
           INTO V_CDFILCENTCMP
           FROM FILIAL
          WHERE FILIAL.CDFILIAL = V_CDFILIAL;

         FOR C_PEDCOLGERAL IN
            (SELECT ROWID, CDFILIAL, NRPEDIDOCOL
               FROM PEDCOLGERAL
              WHERE CDFILIAL     = V_CDFILCENTCMP
                AND DTFINPERGERAL BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            FOR C_FORNEPEDCOL IN (SELECT ROWID
                                    FROM FORNEPEDCOL
                                   WHERE CDFILIAL    = C_PEDCOLGERAL.CDFILIAL
                                     AND NRPEDIDOCOL = C_PEDCOLGERAL.NRPEDIDOCOL)
            LOOP
               DELETE FROM FORNEPEDCOL
                WHERE ROWID = C_FORNEPEDCOL.ROWID;
            END LOOP;

            FOR C_ITEMCOLE IN (SELECT ROWID
                                    FROM ITEMCOLE
                                   WHERE CDFILIAL    = C_PEDCOLGERAL.CDFILIAL
                                     AND NRPEDIDOCOL = C_PEDCOLGERAL.NRPEDIDOCOL)
            LOOP
               DELETE FROM ITEMCOLE
                WHERE ROWID = C_ITEMCOLE.ROWID;
            END LOOP;

            FOR C_PRMARCPEDCOL IN (SELECT ROWID
                                    FROM PRMARCPEDCOL
                                   WHERE CDFILIAL    = C_PEDCOLGERAL.CDFILIAL
                                     AND NRPEDIDOCOL = C_PEDCOLGERAL.NRPEDIDOCOL)
            LOOP
               DELETE FROM PRMARCPEDCOL
                WHERE ROWID = C_PRMARCPEDCOL.ROWID;
            END LOOP;

            FOR C_REGIAOPEDCOL IN (SELECT ROWID
                                    FROM REGIAOPEDCOL
                                   WHERE CDFILIAL    = C_PEDCOLGERAL.CDFILIAL
                                     AND NRPEDIDOCOL = C_PEDCOLGERAL.NRPEDIDOCOL)
            LOOP
               DELETE FROM REGIAOPEDCOL
                WHERE ROWID = C_REGIAOPEDCOL.ROWID;
            END LOOP;

            FOR C_ACEITEPEDCOL IN (SELECT ROWID
                                    FROM ACEITEPEDCOL
                                   WHERE CDFILIAL    = C_PEDCOLGERAL.CDFILIAL
                                     AND NRPEDIDOCOL = C_PEDCOLGERAL.NRPEDIDOCOL)
            LOOP
               DELETE FROM ACEITEPEDCOL
                WHERE ROWID = C_ACEITEPEDCOL.ROWID;
            END LOOP;

            FOR C_COMENTPEDCOL IN (SELECT ROWID
                                    FROM COMENTPEDCOL
                                   WHERE CDFILIAL    = C_PEDCOLGERAL.CDFILIAL
                                     AND NRPEDIDOCOL = C_PEDCOLGERAL.NRPEDIDOCOL)
            LOOP
               DELETE FROM COMENTPEDCOL
                WHERE ROWID = C_COMENTPEDCOL.ROWID;
            END LOOP;

            FOR C_LOGACESPEDCOL IN (SELECT ROWID
                                     FROM LOGACESPEDCOL
                                    WHERE CDFILIAL    = C_PEDCOLGERAL.CDFILIAL
                                      AND NRPEDIDOCOL = C_PEDCOLGERAL.NRPEDIDOCOL)
            LOOP
               DELETE FROM LOGACESPEDCOL
                WHERE ROWID = C_LOGACESPEDCOL.ROWID;
            END LOOP;

            FOR C_PEDCOLETA IN (SELECT ROWID
                                  FROM PEDCOLETA
                                 WHERE CDFILIAL    = C_PEDCOLGERAL.CDFILIAL
                                   AND NRPEDIDOCOL = C_PEDCOLGERAL.NRPEDIDOCOL)
            LOOP
               DELETE FROM PEDCOLETA
                WHERE ROWID = C_PEDCOLETA.ROWID;

               V_EXCLUPEDCOLETA := SQL%ROWCOUNT + V_EXCLUPEDCOLETA;

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

                 /*CONTROLE DE COMMIT A CADA 1000 REGISTROS DELETADOS E PARA ATUALIZAR A TELA DE LIMPEZA*/
                 V_COUNT_COMMIT:= 1;
                 COMMIT;
               ELSE
                 V_COUNT_COMMIT:= V_COUNT_COMMIT + 1;
               END IF;
            END LOOP;

            DELETE FROM PEDCOLGERAL
             WHERE ROWID = C_PEDCOLGERAL.ROWID;
         END LOOP;
      END LOOP;
      CLOSE C_LISTA;

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

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

      COMMIT;

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

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

      /* DELETA A TABELA PREVLISTA*/
      FOR C_PREVLISTA IN
         (SELECT ROWID AS LINHA, CDFILIAL, NRSEQPRELIST
          FROM PREVLISTA
          WHERE NOT EXISTS(SELECT '1'
                           FROM ITEMCOLE
                           WHERE ITEMCOLE.NRSEQPRELIST = PREVLISTA.NRSEQPRELIST
                             AND ITEMCOLE.CDFILIAL     = PREVLISTA.CDFILIAL)
          )
      LOOP
         FOR C_DEL IN
            (SELECT ROWID
               FROM ITEXCLISPREC
              WHERE CDFILIAL = C_PREVLISTA.CDFILIAL
           AND NRSEQPRELIST = C_PREVLISTA.NRSEQPRELIST) LOOP

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

         V_EXCLUITEXCLISPREC := SQL%ROWCOUNT + V_EXCLUITEXCLISPREC;

         DELETE PREVLISTA
         WHERE ROWID = C_PREVLISTA.LINHA;

         V_EXCLUPREVLISTA := SQL%ROWCOUNT + V_EXCLUPREVLISTA;

         IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
            UPDATE TRABPROCLIMPEZA
               SET QTREGEXCLIMPEZA = V_EXCLUPREVLISTA
             WHERE IDLIMPEZA = P_IDSISTEMA
               AND NMTABELALIMP = 'PREVLISTA';
           COMMIT;
           V_COUNT_COMMIT:= 1;
         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_EXCLUPREVLISTA,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'PREVLISTA';

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

      /* PRECISA DO COMMIT PARA PODER  ATUALIZAR A TELA */
      V_COUNT_COMMIT:= 1;
      COMMIT;
      /*VERIFICA SE A PROCEDURE ESTOUROU O TEMPO LIMITE*/
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

      /* PRECISA DO COMMIT PARA PODER  ATUALIZAR A TELA */
      COMMIT;
      /*VERIFICA SE A PROCEDURE ESTOUROU O TEMPO LIMITE*/
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);
      V_COUNT_COMMIT:= 1;

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

      V_COUNT_COMMIT:= 1;
      COMMIT;
      /*VERIFICA SE A PROCEDURE ESTOUROU O TEMPO LIMITE*/
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);

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

         /* DELETA AS TABELAS NFRELACAF, NFRELACAFSAI, ITEMSAIDOL, AUTOFORNE*/
         FOR C_AUTOFORNE IN (SELECT ROWID, AUTOFORNE.CDFILDEST, AUTOFORNE.CDFILLANC, AUTOFORNE.NRAUTOFORN
                             FROM AUTOFORNE
                             WHERE (CDFILDEST   =  V_CDFILIAL)
                               AND (CDFILLANC   <> 'X')
                               AND (NRAUTOFORN  <> 'X')
                               AND (DTAUTOFORN BETWEEN V_DTINICIAL AND P_DTFINAL))
         LOOP
             UPDATE ITEMPRODUTO SET ITEMPRODUTO.CDFILDEST   = NULL,
                                    ITEMPRODUTO.NRAUTOFORN  = NULL,
                                    ITEMPRODUTO.CDMARCA     = NULL,
                                    ITEMPRODUTO.DTPROGENTAF = NULL
             WHERE ITEMPRODUTO.CDFILDEST  = C_AUTOFORNE.CDFILDEST
               AND ITEMPRODUTO.CDFILIAL   = C_AUTOFORNE.CDFILLANC
               AND ITEMPRODUTO.NRAUTOFORN = C_AUTOFORNE.NRAUTOFORN;

             V_PASSO:= 8;


             FOR C_DEL IN
                (SELECT ROWID
                   FROM NFRELACAF
                  WHERE NFRELACAF.CDFILDEST  = C_AUTOFORNE.CDFILDEST
                    AND NFRELACAF.CDFILLANC  = C_AUTOFORNE.CDFILLANC
                    AND NFRELACAF.NRAUTOFORN = C_AUTOFORNE.NRAUTOFORN) LOOP

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

             V_EXCLUNFRELACAF:= V_EXCLUNFRELACAF + SQL%ROWCOUNT;

             V_PASSO:= 9;

             FOR C_DEL IN
                (SELECT ROWID
                   FROM NFRELACAFSAI
                  WHERE NFRELACAFSAI.CDFILDEST  = C_AUTOFORNE.CDFILDEST
                    AND NFRELACAFSAI.CDFILLANC  = C_AUTOFORNE.CDFILLANC
                    AND NFRELACAFSAI.NRAUTOFORN = C_AUTOFORNE.NRAUTOFORN) LOOP

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

             V_EXCLUNFRELACAFSAI:= V_EXCLUNFRELACAFSAI + SQL%ROWCOUNT;

             V_PASSO:= 10;

             FOR C_DEL IN
                (SELECT ROWID
                   FROM ITEMSAIDOL
                  WHERE ITEMSAIDOL.CDFILDEST  = C_AUTOFORNE.CDFILDEST
                    AND ITEMSAIDOL.CDFILLANC  = C_AUTOFORNE.CDFILLANC
                    AND ITEMSAIDOL.NRAUTOFORN = C_AUTOFORNE.NRAUTOFORN) LOOP

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

             V_EXCLUITEMSAIDOL:= V_EXCLUITEMSAIDOL + SQL%ROWCOUNT;

             V_PASSO:= 11;

             UPDATE AUTOFORNE
             SET CDFILDESTCON = NULL,
                 CDFILLANCCON = NULL,
                 NRAUTOFORCON = NULL
             WHERE CDFILDESTCON = C_AUTOFORNE.CDFILDEST
               AND CDFILLANCCON = C_AUTOFORNE.CDFILLANC
               AND NRAUTOFORCON = C_AUTOFORNE.NRAUTOFORN;

             V_PASSO:= 12;

             FOR C_DEL IN
                (SELECT ROWID
                   FROM ITEMAUTF
                  WHERE CDFILDESTOL = C_AUTOFORNE.CDFILDEST
                    AND CDFILLANCOL = C_AUTOFORNE.CDFILLANC
                    AND NRAUTOFORNOL = C_AUTOFORNE.NRAUTOFORN) LOOP

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

             DELETE AUTOFORNE
             WHERE ROWID = C_AUTOFORNE.ROWID;

             V_EXCLUAUTOFORNE := V_EXCLUAUTOFORNE + SQL%ROWCOUNT;

             IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
                UPDATE TRABPROCLIMPEZA
                   SET QTREGEXCLIMPEZA = V_EXCLUAUTOFORNE
                 WHERE IDLIMPEZA = P_IDSISTEMA
                   AND NMTABELALIMP = 'AUTOFORNE';
               COMMIT;
               V_COUNT_COMMIT:= 1;
             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_EXCLUAUTOFORNE,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'AUTOFORNE';

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

      V_COUNT_COMMIT:= 1;
      COMMIT;
      /*VERIFICA SE A PROCEDURE ESTOUROU O TEMPO LIMITE*/
      VERIFTIMEOUT(V_DTHRFINLIMPEZA);
      V_PASSO:= 13;

      /* DELETA A TABELA CALECOMP */

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

         FOR C_CALECOMP IN
            (SELECT ROWID AS LINHA
             FROM CALECOMP
             WHERE CDFILIAL   =  V_CDFILIAL
               AND CDPERIENTR <> 'X'
               AND DTENTREGA  BETWEEN V_DTINICIAL AND P_DTFINAL
             )
         LOOP
            DELETE CALECOMP
            WHERE ROWID = C_CALECOMP.LINHA;

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

              COMMIT;
              V_COUNT_COMMIT:= 1;
            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_EXCLUCALECOMP,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'CALECOMP';

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

      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;

         /* DELETA A TABELA FERICOMP */
         FOR C_FERICOMP IN
            (SELECT ROWID AS LINHA
             FROM FERICOMP
             WHERE CDFILIAL   =  V_CDFILIAL
               AND DTFERICOMP BETWEEN V_DTINICIAL AND P_DTFINAL
             )
         LOOP
            DELETE FERICOMP
            WHERE ROWID = C_FERICOMP.LINHA;

            V_EXCLUFERICOMP:= V_EXCLUFERICOMP + SQL%ROWCOUNT;
            
            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               UPDATE TRABPROCLIMPEZA
                 SET QTREGEXCLIMPEZA = V_EXCLUFERICOMP
               WHERE IDLIMPEZA = P_IDSISTEMA
                 AND NMTABELALIMP = 'FERICOMP';
              COMMIT;
              V_COUNT_COMMIT:= 1;
            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:= 15;

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

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

      V_COUNT_COMMIT:= 1;

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

      /* DELETA A TABELA APURPRECO*/

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

         SELECT NVL(MAX(CDFILCENTCMP), V_CDFILIAL), MAX(CDREGIAO)
         INTO V_CDFILCENTCMP, V_CDREGIAO
         FROM FILIAL
         WHERE FILIAL.CDFILIAL = V_CDFILIAL;

         FOR C_APURPRECO IN
            (SELECT ROWID AS LINHA, NRAPURPREC
             FROM APURPRECO
             WHERE CDFILCENTCMP =  V_CDFILCENTCMP
               AND NRAPURPREC   <> 'X'
               AND DTFINAPUPREC BETWEEN V_DTINICIAL AND P_DTFINAL)
         LOOP
            UPDATE APURPRECO
            SET NRAPURORIG = NULL
            WHERE CDFILCENTCMP = V_CDFILCENTCMP
              AND NRAPURORIG = C_APURPRECO.NRAPURPREC;

            UPDATE APURPRECO
            SET NRAPURCONS = NULL
            WHERE CDFILCENTCMP = V_CDFILCENTCMP
              AND NRAPURCONS = C_APURPRECO.NRAPURPREC;

            DELETE FROM APURPRECO
            WHERE ROWID = C_APURPRECO.LINHA;

            V_EXCLUAPURPRECO:= V_EXCLUAPURPRECO + SQL%ROWCOUNT;

            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               UPDATE TRABPROCLIMPEZA
                 SET QTREGEXCLIMPEZA = V_EXCLUAPURPRECO
               WHERE IDLIMPEZA = P_IDSISTEMA
                 AND NMTABELALIMP = 'APURPRECO';
              COMMIT;
              V_COUNT_COMMIT:= 1;
            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_EXCLUAPURPRECO,
             DTHRFINLIMPEZA = SYSDATE
       WHERE IDLIMPEZA = P_IDSISTEMA
         AND NMTABELALIMP = 'APURPRECO';

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

       V_COUNT_COMMIT:= 1;

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

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

          /* DELETA A TABELA ITMAPURPREC*/
          FOR C_ITMAPURPREC IN
             (SELECT ROWID AS LINHA
                FROM ITMAPURPREC
               WHERE CDFILIAP = V_CDFILIAL
                 AND DTENTRAPUT BETWEEN V_DTINICIAL AND P_DTFINAL)
          LOOP
             DELETE FROM ITMAPURPREC
             WHERE ROWID = C_ITMAPURPREC.LINHA;
             
             V_EXCLUITMAPURPREC:= V_EXCLUITMAPURPREC + SQL%ROWCOUNT;
             
             IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
                UPDATE TRABPROCLIMPEZA
                  SET QTREGEXCLIMPEZA = V_EXCLUITMAPURPREC
                WHERE IDLIMPEZA = P_IDSISTEMA
                  AND NMTABELALIMP = 'ITMAPURPREC';
               COMMIT;
               V_COUNT_COMMIT:= 1;
             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:= 16;

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

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

       V_COUNT_COMMIT:= 1;

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

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

           /* DELETA A TABELA CUSTOSUP*/
          FOR C_CS IN (SELECT ROWID AS LINHA --CDFILIAL, CDPRODUTO, DTCUSTOSUP
                       FROM CUSTOSUP
                       WHERE CDFILIAL  = V_CDFILIAL
                         AND CDPRODUTO IN (SELECT CDPRODUTO
                                            FROM PRODUTO
                                           WHERE CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO) FROM PRODUTO))
                         AND DTCUSTOSUP BETWEEN V_DTINICIAL AND P_DTFINAL
                       --GROUP BY CDFILIAL,DTCUSTOSUP
                       )
          LOOP
             V_PASSO:= 17;
--             UPDATE CUSTOSUP SET CDFILIALORIG = NULL,
--                                 CDPRODORIG   = NULL,
--                                 DTCUSTSUPORI = NULL
--             WHERE CDFILIALORIG = C_CS.CDFILIAL
--               AND CDPRODORIG   = C_CS.CDPRODUTO
--               AND DTCUSTSUPORI = C_CS.DTCUSTOSUP;

--             FOR C_CUSTOSUP IN
--                (SELECT ROWID AS LINHA
--                 FROM CUSTOSUP
--                 WHERE CDFILIAL   = C_CS.CDFILIAL
--                   AND DTCUSTOSUP = C_CS.DTCUSTOSUP
--                   AND EXISTS (SELECT 1
--                                 FROM CUSTOSUP C
--                                WHERE C.CDFILIAL   = C_CS.CDFILIAL
--                                  AND C.CDPRODUTO  = CUSTOSUP.CDPRODUTO
--                                 AND C.DTCUSTOSUP > C_CS.DTCUSTOSUP)
--                 )
--             LOOP
                DELETE CUSTOSUP
                WHERE ROWID = C_CS.LINHA;

                V_EXCLUCUSTOSUP := V_EXCLUCUSTOSUP + SQL%ROWCOUNT;
                
                IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
                   UPDATE TRABPROCLIMPEZA
                     SET QTREGEXCLIMPEZA = V_EXCLUCUSTOSUP
                   WHERE IDLIMPEZA = P_IDSISTEMA
                     AND NMTABELALIMP = 'CUSTOSUP';
                  COMMIT;
                  V_COUNT_COMMIT:= 1;
                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_EXCLUCUSTOSUP,
              DTHRFINLIMPEZA = SYSDATE
        WHERE IDLIMPEZA = P_IDSISTEMA AND
              NMTABELALIMP = 'CUSTOSUP';

       /*DELETA A TABELA TEMPORARIA DA LIMPEZA */
       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, 'SOLICITA', SYSDATE, P_NMOPERLIMPEZ, P_DTFINAL, 'SOLICITA', V_DSRDM, V_DSIP, V_DSUSUARIO, V_DSMETODO, V_EXCLUSOLICITA);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      COMMIT;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_SUP_ITMAPURPREC   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_ERROR:= SQLERRM(SQLCODE);

   IF INSTR(V_ERROR, 'TIMEOUT - EXCECAO PROPOSITAL TEKNISA') = 0 THEN
      /*APAGANDO A LISTA UTILIZADA PARA A LIMPEZA*/
      DELETE FROM LISTA
      WHERE NRLISTA =P_NRLISTA;

      UPDATE TRABLIMPEZA
      SET IDSTATUSLIMPEZA = 'C', IDLIMPEZACONCERRO = 'S'
      WHERE IDLIMPEZA = P_IDSISTEMA;

      COMMIT;
   ELSE
       UPDATE TRABLIMPEZA
       SET IDSTATUSLIMPEZA = 'P'
       WHERE IDLIMPEZA = P_IDSISTEMA;
   END IF;

   COMMIT;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

   COMMIT;

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

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

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

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

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

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

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

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

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

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

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

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

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

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