create or replace PROCEDURE CONTABILIZAR_BATCH(
   P_TIPOMOVI IN VARCHAR2)
IS
   V_DTINICIAL DATE;
   V_DTFINAL DATE;
   V_SEQUENCIAL VARCHAR2(50);
   V_SEQUENCIAL1 VARCHAR2(50);
   V_COUNT INTEGER;
   V_TOTALGERAL FLOAT;
   V_CREDITO FLOAT;
   V_DEBITO FLOAT;
   V_IDLIBAUTLOTE VARCHAR2(01);
   V_COUNTNR INTEGER;
BEGIN
   NOVO_CODIGO('LISTA',V_SEQUENCIAL,1);
   V_SEQUENCIAL := SUBSTR(V_SEQUENCIAL,11,10);
   FOR C_EMPRESA IN(
      SELECT CDEMPRESA
        FROM EMPRESA) LOOP
         IF P_TIPOMOVI = '00' THEN
            BEGIN
               SELECT DTINIINTCONTDFE, DTFININTCONTDFE
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
            FOR C_INSERE_LISTA IN (
               SELECT NF.CDFILIAL||NF.NRLANCTONF AS CHAVE
                 FROM NOTAFISCAL NF, TIPOOPERAC T
                WHERE (NF.CDEMPRESA = C_EMPRESA.CDEMPRESA)
                  AND (NF.IDENTRSAIDOP = 'E')
                  AND (NF.IDSTATUS IN ('A','E'))
                  AND (NVL(NF.IDSISTEMA,'DFE') IN ('DFE', 'EST'))
                  AND (NF.IDSTATUSCTBNF <> 'C')
                  AND (T.IDCONTABOPER = 'S')
                  AND (NF.DTENTRSAID BETWEEN V_DTINICIAL AND V_DTFINAL)
                  AND (T.IDENTRSAIDOP = NF.IDENTRSAIDOP)
                  AND (T.CDTIPOOPER = NF.CDTIPOOPER)
                  AND (NOT EXISTS (SELECT 1
                                     FROM LANCCONT
                                    WHERE CDEMPRESA = NF.CDEMPRESA
                                      AND NRLANCCONT = NF.NRLANCTBDOC))) LOOP
                INSERT INTO LISTA
                   (NRLISTA,CDLISTA)
                VALUES
                   (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
            END LOOP;
            CONTABILIZAR(V_SEQUENCIAL,'00');
         ELSIF P_TIPOMOVI = '01' THEN
            BEGIN
               SELECT DTINIINTCONTDFS, DTFININTCONTDFS
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
            FOR C_INSERE_LISTA IN (
                SELECT NF.CDFILIAL||NF.NRLANCTONF AS CHAVE
                  FROM NOTAFISCAL NF, TIPOOPERAC T
                 WHERE (NF.CDEMPRESA = C_EMPRESA.CDEMPRESA)
                   AND (NF.IDENTRSAIDOP = 'S')
                   AND (NF.IDSTATUS IN ('A','E'))
                   AND (NVL(NF.IDSISTEMA,'DFS') NOT IN ('ITG', 'REC'))
                   AND (NF.IDSTATUSCTBNF <> 'C')
                   AND (NF.DTENTRSAID BETWEEN V_DTINICIAL AND V_DTFINAL)
                   AND (NF.NRLANCTBDOC IS NULL)
                   AND (NF.IDENTRSAIDOP = T.IDENTRSAIDOP)
                   AND (NF.CDTIPOOPER = T.CDTIPOOPER)
                   AND (T.IDCONTABOPER = 'S')
                   AND (NOT EXISTS (SELECT 1
                                      FROM LANCCONT
                                     WHERE CDEMPRESA = NF.CDEMPRESA
                                       AND NRLANCCONT = NF.NRLANCTBDOC)))LOOP
                 INSERT INTO LISTA
                    (NRLISTA,CDLISTA)
                 VALUES
                    (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
            END LOOP;
            CONTABILIZAR(V_SEQUENCIAL,'01');
         ELSIF P_TIPOMOVI = '02' THEN
            BEGIN
               SELECT DTINIINTCONTDFE, DTFININTCONTDFE
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
               FOR C_INSERE_LISTA IN(
                  SELECT A.CDFILIMOVI||A.CDTIPORETI||A.MESANO AS CHAVE
                    FROM (SELECT IT.CDFILIMOVI AS CDFILIMOVI, IT.CDTIPORETI AS CDTIPORETI, A.MESANO
                            FROM ITLANCTOEST IT, ITREQUESTO IR,
                                 LOCALESTOQUE LE,  PARAMAN PM, FILIAL F,
                                 RETIRADAESTO R, SETOR S, LANCTOESTOQ L,
                                 (SELECT DISTINCT TO_CHAR(DTDATA, 'MM/YYYY') MESANO
                                             FROM DATA
                                            WHERE DTDATA BETWEEN V_DTINICIAL AND V_DTFINAL) A
                           WHERE (IT.IDTIPOMOVI   = '3')
                             AND (F.CDEMPRESA     = C_EMPRESA.CDEMPRESA)
                             AND (IT.CDFILIMOVI   = LE.CDFILIAL)
                             AND (IT.CDFILIMOVI   = LE.CDFILIAL)
                             AND (PM.CDFILIAL     = IT.CDFILIMOVI)
                             AND (PM.CDTIPORETI   <> IT.CDTIPORETI)
                             AND (IT.CDALMOXARIFE = LE.CDALMOXARIFE)
                             AND (IT.CDLOCALESTOQ = LE.CDLOCALESTOQ)
                             AND (L.CDFILIAL      = R.CDFILIALLAN)
                             AND (L.NRLANCESTQ    = R.NRLANCESTQ)
                             AND (R.CDSETOR       = S.CDSETOR(+))
                             AND (R.CDCENTCUST    = S.CDCENTCUST(+))
                             AND (IT.CDFILIAL     = L.CDFILIAL)
                             AND (IT.NRLANCESTQ   = L.NRLANCESTQ)
                             AND ((R.CDCENTCUST   IS NOT NULL) OR (R.CDCENTCUST <> ''))
                             AND (NVL(IT.IDEVOLESTOQ,'S') = 'S')
                             AND (IT.CDFILIAL     = IR.CDFILILANC(+))
                             AND (IT.NRLANCESTQ   = IR.NRLANCESTQ(+))
                             AND (IT.NRSEQUITEM   = IR.NRSEQUITEM(+))
                             AND (IT.CDPRODUTO    = IR.CDPRODUTO(+))
                             AND (TO_CHAR(IT.DTLANCMOVI, 'MM/YYYY') = A.MESANO)
                             AND (F.CDFILIAL      = LE.CDFILIAL)
                             AND (IT.CDEMPITCTB   IS NULL)
                             AND (IT.CDLOTEITCTB  IS NULL)
                           GROUP BY
                                 IT.CDFILIMOVI, IT.CDTIPORETI, A.MESANO
                           UNION ALL
                          SELECT IT.CDFILIMOVI AS CDFILIMOVI, IT.CDTIPORETI AS CDTIPORETI, A.MESANO
                            FROM ITLANCTOEST IT,
                                 LOCALESTOQUE LE, TIPORETI TR,
                                 RETIRADA RE, LANCTOESTOQ L, FILIAL F,
                                 (SELECT DISTINCT TO_CHAR(DTDATA, 'MM/YYYY') MESANO
                                             FROM DATA
                                            WHERE DTDATA BETWEEN V_DTINICIAL AND V_DTFINAL) A
                           WHERE (IT.IDTIPOMOVI    = '3')
                             AND (F.CDEMPRESA      = C_EMPRESA.CDEMPRESA)
                             AND (IT.CDFILIMOVI    = LE.CDFILIAL)
                             AND (IT.CDALMOXARIFE  = LE.CDALMOXARIFE)
                             AND (IT.CDLOCALESTOQ  = LE.CDLOCALESTOQ)
                             AND (RE.CDFILANCESTQ  = F.CDFILIAL)
                             AND (L.CDFILIAL       = RE.CDFILANCESTQ)
                             AND (L.NRLANCESTQ     = RE.NRLANCESTQ)
                             AND (IT.CDFILIAL      = L.CDFILIAL)
                             AND (IT.NRLANCESTQ    = L.NRLANCESTQ)
                             AND (NVL(IT.IDEVOLESTOQ, 'S') = 'S')
                             AND (TO_CHAR(IT.DTLANCMOVI, 'MM/YYYY') = A.MESANO)
                             AND (IT.CDEMPITCTB   IS NULL)
                             AND (IT.CDLOTEITCTB  IS NULL)
                           GROUP BY
                                 IT.CDFILIMOVI, IT.CDTIPORETI, A.MESANO
                           ORDER BY 1, 2) A
                      GROUP BY A.CDFILIMOVI||A.CDTIPORETI||A.MESANO
                      ORDER BY 1
               )LOOP
               INSERT INTO LISTA
                 (NRLISTA,CDLISTA)
               VALUES
                 (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
               
               END LOOP;
            CONTABILIZAR(V_SEQUENCIAL,'02');
         ELSIF P_TIPOMOVI = '03' THEN
            BEGIN
               SELECT DTINIINTCONTDFE, DTFININTCONTDFE
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
            FOR C_INSERE_LISTA IN(
            SELECT A.CDFILIAL||A.MESANO AS CHAVE
                  FROM (SELECT APRCUSTOP.CDFILIAL AS CDFILIAL, M.MESANO AS MESANO
                          FROM APRCUSTOP, ORDPRODUCAO, (SELECT DISTINCT TO_CHAR(DTDATA, 'MM/YYYY') MESANO
                                                                   FROM DATA
                                                                  WHERE DTDATA BETWEEN V_DTINICIAL AND V_DTFINAL) M
                         WHERE (APRCUSTOP.CDFUNCUSTO <> '099')
                           AND (ORDPRODUCAO.IDSITORDPROD  = '3')
                           AND (APRCUSTOP.CDFILIAL        = ORDPRODUCAO.CDFILIAL)
                           AND (APRCUSTOP.NRORDPROD       = ORDPRODUCAO.NRORDPROD)
                           AND (APRCUSTOP.NRSUBORDPROD    = ORDPRODUCAO.NRSUBORDPROD)
                           AND (APRCUSTOP.CDEMPRCTBOP IS NULL)
                           AND (APRCUSTOP.CDLOTECTBOP IS NULL)
                           AND (TO_CHAR(TRUNC(ORDPRODUCAO.DTHRFIMREAOP),'MM/YYYY') = M.MESANO)) A
                 GROUP BY A.CDFILIAL||A.MESANO)LOOP
                 INSERT INTO LISTA
                    (NRLISTA,CDLISTA)
                 VALUES
                    (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
            END LOOP;
            CONTABILIZAR(V_SEQUENCIAL, '03');
         ELSIF P_TIPOMOVI = '04' THEN
            BEGIN
               SELECT DTINIINTCONTDFE, DTFININTCONTDFE
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
            FOR C_INSERE_LISTA IN(
                SELECT LE.CDFILIAL||LE.NRLANCESTQ AS CHAVE
                  FROM LANCTOESTOQ LE, FILIAL FL, PARAMFILIAL PF, PARAMGERAL PG
                 WHERE (LE.IDTPLANCTO = '4')
                   AND (LE.DTLANCESTQ BETWEEN V_DTINICIAL AND V_DTFINAL)
                   AND (LE.IDSTATUSCTBEST <> 'C')
                   AND (LE.CDFILIMOVI = FL.CDFILIAL)
                   AND (FL.CDEMPRESA = C_EMPRESA.CDEMPRESA)
                   AND (PF.CDFILIAL = LE.CDFILIMOVI)
                   AND (DECODE(PG.IDUTILDTPROC,'N',SYSDATE + 1,NVL(PF.DTPROCESSA,SYSDATE + 1)) > LE.DTLANCESTQ)
                   AND (NOT EXISTS (SELECT NRLANCESTQ
                                      FROM ITLANCTOEST IT
                                     WHERE LE.CDFILIAL = IT.CDFILIAL
                                       AND LE.NRLANCESTQ = IT.NRLANCESTQ
                                       AND IT.CDLOTEITCTB IS NOT NULL))) LOOP
                 INSERT INTO LISTA
                    (NRLISTA,CDLISTA)
                 VALUES
                    (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
            END LOOP;
            CONTABILIZAR(V_SEQUENCIAL, '04');
         ELSIF P_TIPOMOVI = '05' THEN
            BEGIN
               SELECT DTINIINTCONTDFE, DTFININTCONTDFE
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
            FOR C_INSERE_LISTA IN(
                SELECT DISTINCT IT.CDFILIMOVI||TO_CHAR(IT.DTLANCMOVI, 'MM/YYYY')||IT.IDTIPOMOVI AS CHAVE
                  FROM ITLANCTOEST IT, 
                       (SELECT DISTINCT TO_CHAR(DTDATA, 'MM/YYYY') AS DTLANCMOVI
                          FROM DATA
                         WHERE DTDATA BETWEEN V_DTINICIAL AND V_DTFINAL) DT
                 WHERE ((IT.IDTIPOMOVI = '0') OR (IT.IDTIPOMOVI = '1'))
                   AND (TO_CHAR(IT.DTLANCMOVI, 'MM/YYYY') = DT.DTLANCMOVI)
                   AND (IT.CDLOTEITCTB IS NULL))LOOP
                 INSERT INTO LISTA
                    (NRLISTA,CDLISTA)
                 VALUES
                    (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
            END LOOP;
            CONTABILIZAR(V_SEQUENCIAL, '05');
         ELSIF P_TIPOMOVI = '06' THEN
            BEGIN
               SELECT DTINICCTBL, DTFINACCTBL
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
            FOR C_INSERE_LISTA IN(
               SELECT NOTAFISCAL.CDFILIAL||NOTAFISCAL.NRLANCTONF AS CHAVE
                 FROM NOTAFISCAL
                WHERE (NOTAFISCAL.IDSISTEMA = 'PAG')
                  AND (NOTAFISCAL.CDEMPRESA = C_EMPRESA.CDEMPRESA)
                  AND (NOTAFISCAL.IDSTATUSCTBNF <> 'C')
                  AND (NOTAFISCAL.DTENTRSAID BETWEEN V_DTINICIAL AND V_DTFINAL)
                  AND (NOT EXISTS (SELECT 1
                                     FROM LANCCONT
                                    WHERE CDEMPRESA = NOTAFISCAL.CDEMPRESA
                                      AND NRLANCCONT = NOTAFISCAL.NRLANCTBDOC))) LOOP
                INSERT INTO LISTA
                   (NRLISTA,CDLISTA)
                VALUES
                   (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
            END LOOP;
            CONTABILIZAR(V_SEQUENCIAL, '06');
         ELSIF P_TIPOMOVI = '07' THEN
            BEGIN
               SELECT DTINICCTBL, DTFINACCTBL
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
            FOR C_INSERE_LISTA IN(
                SELECT NOTAFISCAL.CDFILIAL||NOTAFISCAL.NRLANCTONF AS CHAVE
                  FROM NOTAFISCAL
                 WHERE (NOTAFISCAL.IDSISTEMA = 'REC')
                   AND (NOTAFISCAL.CDEMPRESA = C_EMPRESA.CDEMPRESA)
                   AND (NOTAFISCAL.IDSTATUSCTBNF <> 'C')
                   AND (NOTAFISCAL.DTENTRSAID BETWEEN V_DTINICIAL AND V_DTFINAL)
                   AND (NOT EXISTS (SELECT 1
                                     FROM LANCCONT
                                    WHERE CDEMPRESA = NOTAFISCAL.CDEMPRESA
                                      AND NRLANCCONT = NOTAFISCAL.NRLANCTBDOC))) LOOP
                INSERT INTO LISTA
                   (NRLISTA,CDLISTA)
                VALUES
                   (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
            END LOOP;
            CONTABILIZAR(V_SEQUENCIAL, '07');
         ELSIF P_TIPOMOVI = '08' THEN
            BEGIN
               SELECT DTINICCTBL, DTFINACCTBL
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
            FOR C_INSERE_LISTA IN(
                SELECT MOVCREDBANC.CDREDUCONT||MOVCREDBANC.NRMOVBANC AS CHAVE
                  FROM MOVCREDBANC, MOVBANCARIO, FILIAL FL
                 WHERE MOVCREDBANC.CDFILIAL = FL.CDFILIAL
                   AND MOVCREDBANC.CDREDUCONT = MOVBANCARIO.CDREDUCONT
                   AND MOVCREDBANC.NRMOVBANC = MOVBANCARIO.NRMOVBANC
                   AND MOVCREDBANC.DTMOVICRED BETWEEN V_DTINICIAL AND V_DTFINAL
                   AND MOVBANCARIO.IDSTATUSCTBBANC <> 'C'
                   AND FL.CDEMPRESA = C_EMPRESA.CDEMPRESA
                   AND (NOT EXISTS (SELECT 1
                                     FROM LANCCONT
                                    WHERE CDEMPRESA = MOVCREDBANC.CDECTBMCRED
                                      AND NRLANCCONT = MOVCREDBANC.NRLCTBMCRED))
                UNION ALL
                SELECT MOVDEBBANC.CDREDUCONT||MOVDEBBANC.NRMOVBANC AS CHAVE
                  FROM MOVDEBBANC, MOVBANCARIO, FILIAL FL
                 WHERE MOVDEBBANC.CDFILIAL = FL.CDFILIAL
                   AND MOVDEBBANC.CDREDUCONT = MOVBANCARIO.CDREDUCONT
                   AND MOVDEBBANC.NRMOVBANC = MOVBANCARIO.NRMOVBANC
                   AND MOVDEBBANC.DTMOVIDEBI BETWEEN V_DTINICIAL AND V_DTFINAL
                   AND MOVBANCARIO.IDSTATUSCTBBANC <> 'C'
                   AND FL.CDEMPRESA = C_EMPRESA.CDEMPRESA
                   AND (NOT EXISTS (SELECT 1
                                     FROM LANCCONT
                                    WHERE CDEMPRESA = MOVDEBBANC.CDECTBMDEB
                                      AND NRLANCCONT = MOVDEBBANC.NRLCTBMDEB))) LOOP
                INSERT INTO LISTA
                   (NRLISTA,CDLISTA)
                VALUES
                   (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
            END LOOP;
            CONTABILIZAR(V_SEQUENCIAL, '08');
         ELSIF P_TIPOMOVI = '09' THEN
            BEGIN
               SELECT DTINICCTBL, DTFINACCTBL
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
            FOR C_INSERE_LISTA IN(
                SELECT TRANSFBANC.CDEMPRESA||TRANSFBANC.NRTRANBANC AS CHAVE
                  FROM TRANSFBANC
                 WHERE (TRANSFBANC.CDEMPRESA = C_EMPRESA.CDEMPRESA)
                   AND (TRANSFBANC.IDSTATUSCTBL <> 'C')
                   AND (TRANSFBANC.DTTRANBANC BETWEEN V_DTINICIAL AND V_DTFINAL)
                   AND (NOT EXISTS (SELECT 1
                                     FROM LANCCONT
                                    WHERE CDEMPRESA = TRANSFBANC.CDEMPRESA
                                      AND NRLANCCONT = TRANSFBANC.NRLCTBTRAN))) LOOP
                INSERT INTO LISTA
                   (NRLISTA,CDLISTA)
                VALUES
                   (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
            END LOOP;
            CONTABILIZAR(V_SEQUENCIAL, '09');
         ELSIF P_TIPOMOVI = '10' THEN
            BEGIN
               SELECT DTINICCTBL, DTFINACCTBL
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
            FOR C_INSERE_LISTA IN(
                SELECT BAIXAPAGA.CDFILIAL||BAIXAPAGA.NRBAIXAPAG AS CHAVE
                  FROM BAIXAPAGA
                 WHERE (BAIXAPAGA.CDEMPRESA    = C_EMPRESA.CDEMPRESA)
                   AND (BAIXAPAGA.IDSTATUSBPAG = 'E')
                   AND (BAIXAPAGA.DTBAIXAPAG BETWEEN V_DTINICIAL AND V_DTFINAL)
                   AND (BAIXAPAGA.IDSTATUSCTBPAG <> 'C')
                   AND (NOT EXISTS (SELECT 1
                                     FROM LANCCONT
                                    WHERE CDEMPRESA = BAIXAPAGA.CDEMPRESA
                                      AND NRLANCCONT = BAIXAPAGA.NRLCTBBPAG))) LOOP
                INSERT INTO LISTA
                   (NRLISTA,CDLISTA)
                VALUES
                   (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
            END LOOP;
            CONTABILIZAR(V_SEQUENCIAL, '10');
         ELSIF P_TIPOMOVI = '11' THEN
            BEGIN
               SELECT DTINICCTBL, DTFINACCTBL
                 INTO V_DTINICIAL, V_DTFINAL
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                 V_DTINICIAL := NULL;
                 V_DTFINAL := NULL;
            END;
            FOR C_INSERE_LISTA IN(
                SELECT BAIXARECEBIDA.CDFILIAL||BAIXARECEBIDA.NRBAIXAREC AS CHAVE
                  FROM BAIXARECEBIDA
                 WHERE (BAIXARECEBIDA.CDEMPRESA = C_EMPRESA.CDEMPRESA)
                   AND (BAIXARECEBIDA.IDSTATUSBREC = 'E')
                   AND (BAIXARECEBIDA.DTBAIXAREC BETWEEN V_DTINICIAL AND V_DTFINAL)
                   AND (BAIXARECEBIDA.IDSTATUSCTBREC <> 'C')
                   AND (NOT EXISTS (SELECT 1
                                     FROM LANCCONT
                                    WHERE CDEMPRESA = BAIXARECEBIDA.CDEMPRESA
                                      AND NRLANCCONT = BAIXARECEBIDA.NRLCTBBREC))) LOOP
                INSERT INTO LISTA
                   (NRLISTA,CDLISTA)
                VALUES
                   (V_SEQUENCIAL, C_INSERE_LISTA.CHAVE);
            END LOOP;
            CONTABILIZAR(V_SEQUENCIAL, '11');
         END IF;
         FOR C_LOTE IN(
           SELECT CDLOTE, DTINICLOTE
             FROM CAPALOTE
            WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA
              AND IDTPMOVLOTE IS NOT NULL
              AND IDLOTE <> 'L') LOOP
              BEGIN
                 SELECT IDLIBAUTLOTE
                   INTO V_IDLIBAUTLOTE
                   FROM AGENDACONTAB
                  WHERE IDAGENDMOVCONT = P_TIPOMOVI;
              EXCEPTION
                 WHEN NO_DATA_FOUND THEN
                  V_IDLIBAUTLOTE := NULL;
              END;
              IF V_IDLIBAUTLOTE = 'S' THEN
                  BEGIN
                   SELECT SUM(CREDITO) AS CREDITO,
                          SUM(DEBITO) AS DEBITO,
                          SUM(TOTAL) AS TOTALGERAL
                     INTO V_CREDITO, V_DEBITO, V_TOTALGERAL
                     FROM (
                           (SELECT CDLOTE,'D' AS IDCDV,0 AS CREDITO, SUM(VRLANCLOTE) AS DEBITO, 0 AS TOTAL
                              FROM LANCLOTE
                             WHERE (CDEMPRESA = C_EMPRESA.CDEMPRESA )
                               AND (CDLOTE    = C_LOTE.CDLOTE )
                               AND (VRLANCLOTE < 0)
                             GROUP BY CDLOTE)
                             UNION
                           (SELECT CDLOTE,'C' AS IDCDV,SUM(VRLANCLOTE) AS CREDITO, 0 AS DEBITO, 0 AS TOTAL
                              FROM LANCLOTE
                             WHERE (CDEMPRESA = C_EMPRESA.CDEMPRESA)
                               AND (CDLOTE    = C_LOTE.CDLOTE )
                               AND (VRLANCLOTE >= 0)
                             GROUP BY CDLOTE)
                             UNION
                           (SELECT CDLOTE,'T' AS IDCDV,0 AS CREDITO, 0 AS DEBITO, SUM(VRLANCLOTE) AS TOTAL
                              FROM LANCLOTE
                             WHERE (CDEMPRESA = C_EMPRESA.CDEMPRESA)
                               AND (CDLOTE    = C_LOTE.CDLOTE)
                             GROUP BY CDLOTE)) SOMA;
                  EXCEPTION
                   WHEN NO_DATA_FOUND THEN
                    V_CREDITO := 0;
                    V_DEBITO := 0;
                    V_TOTALGERAL := 0;
                  END;
                  IF V_TOTALGERAL = 0 THEN
                     UPDATE CAPALOTE
                        SET VRTOTCRDIGI = V_CREDITO,
                            VRTOTDBDIGI = V_DEBITO
                      WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA
                        AND CDLOTE = C_LOTE.CDLOTE;
                      BEGIN
                       SELECT COUNT(*)
                         INTO V_COUNT
                         FROM ERROLANCLOTE
                        WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA
                          AND CDLOTE = C_LOTE.CDLOTE;
                      EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                         V_COUNT := NULL;
                      END;
                      IF V_COUNT = 0 THEN
                         BEGIN
                            SELECT COUNT(DISTINCT NRLANCLOTE)
                              INTO V_COUNTNR
                              FROM LANCLOTE
                             WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA
                               AND CDLOTE = C_LOTE.CDLOTE;
                         EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                             V_COUNTNR := NULL;
                         END;
                         IF V_COUNTNR <> 0 THEN
                            NOVO_CODIGO('LANCCONTABIL' || C_EMPRESA.CDEMPRESA || TO_CHAR(C_LOTE.DTINICLOTE,'YYYYMM'),V_SEQUENCIAL1,V_COUNTNR);
                            V_SEQUENCIAL1 := TO_CHAR(C_LOTE.DTINICLOTE,'YYYYMM') || SUBSTR(V_SEQUENCIAL1,15,6);
                            LIBERA_LOTE(C_EMPRESA.CDEMPRESA,C_LOTE.CDLOTE,V_SEQUENCIAL1,'');
                         END IF;
                      ELSE
                         UPDATE CAPALOTE
                            SET QTERROLOTE = V_COUNT, IDLOTE = 'C'
                          WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA
                            AND CDLOTE = C_LOTE.CDLOTE;
                      END IF;
                      UPDATE CAPALOTE
                         SET QTERROLOTE = 0, IDLOTE = 'C'
                       WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA
                         AND CDLOTE = C_LOTE.CDLOTE;
                  ELSE
                     UPDATE CAPALOTE
                         SET QTERROLOTE = 1, IDLOTE = 'C'
                       WHERE CDEMPRESA = C_EMPRESA.CDEMPRESA
                         AND CDLOTE = C_LOTE.CDLOTE;
                  END IF;
              END IF;
         END LOOP;
   END LOOP;
END;
/