CREATE OR REPLACE PROCEDURE LIMPEZA_LANCCONT(
   P_NRLISTA           IN VARCHAR2,
   P_DTLANC            IN LANCCONT.DTLANCCONT%TYPE,
   P_DSSISTLIMPEZ      IN VARCHAR2,
   P_NMOPERLIMPEZ      IN VARCHAR2,
   P_IDSISTEMA         IN VARCHAR2)
IS
   V_DTHRINILIMPEZA    DATE;
   V_DTHRFINLIMPEZA    DATE;
   V_NRORDEM           VARCHAR2(03);

   V_COUNT_COMMIT      NUMBER := 1;
   V_NUM_TO_COMMIT     NUMBER := 250;

   V_EXCLUILANCCONT    NUMBER := 0;
   V_EXCLUISALDEMPR    NUMBER := 0;
   V_EXCLUILOGLANCONT  NUMBER := 0;
   V_EXCLUISALDCOME    NUMBER := 0;
   V_EXCLUISALDCCUS    NUMBER := 0;

   V_DSIP              VARCHAR2(50);
   V_DSUSUARIO         VARCHAR2(50);
   V_DSRDM             VARCHAR2(50);
   V_DSMETODO          VARCHAR2(250);
   V_DSOPERADOR        VARCHAR2(12);
   V_ERR0R             VARCHAR2(2000);
   V_PASSO             NUMBER := 0;
   V_CDEMPRESA         VARCHAR2(02);
   V_DTINICIAL         DATE;
   V_IDSTATUSLIMPEZA   VARCHAR2(01);

   CURSOR C_LISTA IS (SELECT DISTINCT F.CDEMPRESA
                        FROM TRABLIMPFILIAL T, FILIAL F
                       WHERE T.IDLIMPEZA = P_IDSISTEMA
                         AND F.CDFILIAL = T.CDFILIAL);

   PROCEDURE VERIFTIMEOUT(DTLIMITE IN DATE) IS
   BEGIN
      IF SYSDATE > DTLIMITE THEN
         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_EXCLUILANCCONT
           FROM TRABPROCLIMPEZA
          WHERE IDLIMPEZA = P_IDSISTEMA
            AND NMTABELALIMP = 'LANCCONT';

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

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

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

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

      V_NRORDEM := '0';

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

      COMMIT;

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

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

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

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

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

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

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

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

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

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

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

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'LOGLANCONT');
      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 = 'SALDCOME'; 
      
      IF NOT(SQL%ROWCOUNT > 0) THEN
         INSERT INTO TABLIMPEZA
         (IDLIMPEZA, NMTABELALIMP, NRORDEMLIMPEZA)
         VALUES
         (P_IDSISTEMA, 'SALDCOME', V_NRORDEM);  
      END IF;

      IF V_IDSTATUSLIMPEZA <> 'P' THEN
         INSERT INTO TRABPROCLIMPEZA
           (IDLIMPEZA, NMTABELALIMP)
         VALUES
            (P_IDSISTEMA, 'SALDCOME');
      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 = 'SALDCCUS'; 
      
      IF NOT(SQL%ROWCOUNT > 0) THEN
         INSERT INTO TABLIMPEZA
         (IDLIMPEZA, NMTABELALIMP, NRORDEMLIMPEZA)
         VALUES
         (P_IDSISTEMA, 'SALDCCUS', V_NRORDEM);
      END IF;        

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

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

         V_COUNT_COMMIT := 0;
         FOR C_LANCCONT IN (SELECT ROWID AS LINHA, CDEMPRESA, NRLANCCONT, NRSEQLANC
                              FROM LANCCONT
                             WHERE CDEMPRESA   = V_CDEMPRESA
                               AND DTLANCCONT BETWEEN V_DTINICIAL AND P_DTLANC) LOOP

            UPDATE AJCTPAGAR I
               SET I.NRLANCCONT = NULL,
                   I.NRSEQLANC  = NULL
             WHERE CDEMPRESA  = C_LANCCONT.CDEMPRESA
               AND NRLANCCONT = C_LANCCONT.NRLANCCONT
               AND NRSEQLANC  = C_LANCCONT.NRSEQLANC;

            UPDATE NOTAFISCAL I
               SET I.NRLANCCONT = NULL,
                   I.NRSEQLANC  = NULL
             WHERE CDEMPRESA  = C_LANCCONT.CDEMPRESA
               AND NRLANCCONT = C_LANCCONT.NRLANCCONT
               AND NRSEQLANC  = C_LANCCONT.NRSEQLANC;

            DELETE CONTCTBLMAPA
             WHERE CDEMPRESA  = C_LANCCONT.CDEMPRESA
               AND NRLANCCONT = C_LANCCONT.NRLANCCONT
               AND NRSEQLANC  = C_LANCCONT.NRSEQLANC;

            DELETE CREDBENFEITAPUR
             WHERE CDEMPRESA  = C_LANCCONT.CDEMPRESA
               AND NRLANCCONT = C_LANCCONT.NRLANCCONT
               AND NRSEQLANC  = C_LANCCONT.NRSEQLANC;

            DELETE LANCCONT
             WHERE ROWID = C_LANCCONT.LINHA;
          
            V_EXCLUILANCCONT := SQL%ROWCOUNT + V_EXCLUILANCCONT;
            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               UPDATE TRABPROCLIMPEZA
                  SET QTREGEXCLIMPEZA = V_EXCLUILANCCONT
                WHERE IDLIMPEZA       = P_IDSISTEMA
                  AND NMTABELALIMP    = 'LANCCONT';

               COMMIT;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            ELSE
               V_COUNT_COMMIT := V_COUNT_COMMIT + 1;
            END IF;
         END LOOP;
      
         V_PASSO := 2;
         V_COUNT_COMMIT := 0;
         FOR C_SALDEMPR IN (SELECT ROWID AS LINHA
                              FROM SALDEMPR
                             WHERE CDEMPRESA  = V_CDEMPRESA
                               AND DTSALDEMPR BETWEEN V_DTINICIAL AND P_DTLANC) LOOP
                         
            DELETE SALDEMPR
             WHERE ROWID = C_SALDEMPR.LINHA;
          
            V_EXCLUISALDEMPR := SQL%ROWCOUNT + V_EXCLUISALDEMPR;
            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               UPDATE TRABPROCLIMPEZA
                  SET QTREGEXCLIMPEZA = V_EXCLUISALDEMPR
                WHERE IDLIMPEZA       = P_IDSISTEMA
                  AND NMTABELALIMP    = 'SALDEMPR';
             
               COMMIT;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            ELSE
               V_COUNT_COMMIT := V_COUNT_COMMIT + 1;
            END IF;
         END LOOP;

         V_PASSO := 3;
         V_COUNT_COMMIT := 0;
         FOR C_LOGLANCONT IN (SELECT ROWID AS LINHA
                                FROM LOGLANCONT
                               WHERE CDEMPRLOG = V_CDEMPRESA
                                 AND DTLANCLOG BETWEEN V_DTINICIAL AND P_DTLANC) LOOP
                         
            DELETE LOGLANCONT
             WHERE ROWID = C_LOGLANCONT.LINHA;
          
            V_EXCLUILOGLANCONT := SQL%ROWCOUNT + V_EXCLUILOGLANCONT;
            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               UPDATE TRABPROCLIMPEZA
                  SET QTREGEXCLIMPEZA = V_EXCLUILOGLANCONT
                WHERE IDLIMPEZA       = P_IDSISTEMA
                  AND NMTABELALIMP    = 'LOGLANCONT';
             
               COMMIT;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            ELSE
               V_COUNT_COMMIT := V_COUNT_COMMIT + 1;
            END IF;
         END LOOP;    

         V_PASSO := 4;
         V_COUNT_COMMIT := 0;
         FOR C_SALDCOME IN (SELECT ROWID AS LINHA
                              FROM SALDCOME
                             WHERE CDEMPRESA  = V_CDEMPRESA
                               AND DTSALDCOME BETWEEN V_DTINICIAL AND P_DTLANC) LOOP
                         
            DELETE SALDCOME
             WHERE ROWID = C_SALDCOME.LINHA;
          
            V_EXCLUISALDCOME := SQL%ROWCOUNT + V_EXCLUISALDCOME;
            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               UPDATE TRABPROCLIMPEZA
                  SET QTREGEXCLIMPEZA = V_EXCLUISALDCOME
                WHERE IDLIMPEZA       = P_IDSISTEMA
                  AND NMTABELALIMP    = 'SALDCOME';
             
               COMMIT;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            ELSE
               V_COUNT_COMMIT := V_COUNT_COMMIT + 1;
            END IF;
         END LOOP;   

         V_PASSO := 5;
         V_COUNT_COMMIT := 0;
         FOR C_SALDCCUS IN (SELECT ROWID AS LINHA
                              FROM SALDCCUS
                             WHERE CDEMPRESA  = V_CDEMPRESA
                               AND DTSALDCCUS BETWEEN V_DTINICIAL AND P_DTLANC) LOOP
                         
            DELETE SALDCCUS
             WHERE ROWID = C_SALDCCUS.LINHA;
          
            V_EXCLUISALDCCUS := SQL%ROWCOUNT + V_EXCLUISALDCCUS;
            IF V_COUNT_COMMIT >= V_NUM_TO_COMMIT THEN
               UPDATE TRABPROCLIMPEZA
                  SET QTREGEXCLIMPEZA = V_EXCLUISALDCCUS
                WHERE IDLIMPEZA       = P_IDSISTEMA
                  AND NMTABELALIMP    = 'SALDCCUS';
             
               COMMIT;
               VERIFTIMEOUT(V_DTHRFINLIMPEZA);
            ELSE
               V_COUNT_COMMIT := V_COUNT_COMMIT + 1;
            END IF;
         END LOOP;    
      END LOOP; /*C_LISTA*/
   
      V_PASSO := 6;

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

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

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

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

      UPDATE TRABPROCLIMPEZA
         SET IDSITUPROCLIMP  = 'C',
             QTREGEXCLIMPEZA = V_EXCLUISALDCCUS,
             DTHRFINLIMPEZA  = SYSDATE
       WHERE IDLIMPEZA    = P_IDSISTEMA
         AND NMTABELALIMP = 'SALDCCUS';
      
      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, 'LANCCONT', SYSDATE, P_NMOPERLIMPEZ, P_DTLANC, 'LANCCONT', V_DSRDM, V_DSIP, V_DSUSUARIO, V_DSMETODO, V_EXCLUILANCCONT);

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

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

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

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

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

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

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

      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
      DELETE FROM LISTA
       WHERE NRLISTA =P_NRLISTA;

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

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

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

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

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

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

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

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

   COMMIT;

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

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

   BEGIN
      EXECUTE IMMEDIATE 'ALTER TRIGGER LOG_CTB_LANCCONT   ENABLE';
   EXCEPTION
      WHEN OTHERS THEN NULL;
   END;
   
   RAISE_APPLICATION_ERROR (-20101, 'PASSO : ' || V_PASSO || '   -   ' || V_ERR0R);
END;
/