IF EXISTS (SELECT * FROM SYSOBJECTS O, SYSUSERS U
           WHERE O.NAME = 'CONTABILIZAR_BATCH' AND
                 TYPE = 'P' 	         AND
	              U.UID = O.UID      AND
	              U.NAME = USER)
DROP PROCEDURE CONTABILIZAR_BATCH
GO
                                                              
CREATE PROCEDURE CONTABILIZAR_BATCH(
   @P_TIPOMOVI  VARCHAR)
AS
BEGIN
   DECLARE @V_DTINICIAL     DATETIME;
   DECLARE @V_DTFINAL       DATETIME;
   DECLARE @V_SEQUENCIAL    VARCHAR(50);
   DECLARE @V_SEQUENCIAL1   VARCHAR(50);
   DECLARE @V_COUNT         INTEGER;
   DECLARE @V_TOTALGERAL    FLOAT;
   DECLARE @V_CREDITO       FLOAT;
   DECLARE @V_DEBITO        FLOAT;
   DECLARE @V_IDLIBAUTLOTE  VARCHAR(01);
   DECLARE @V_COUNTNR       INTEGER;
                                                              
   /* VARIAVEIS DO CURSOR C_EMPRESA */
   DECLARE @C_EMPRESA_CDEMPRESA        VARCHAR(02);
   DECLARE @C_EMPRESA_CDEMPRESAAJUST   VARCHAR;
                                                              
   /* VARIAVEIS DO CURSOR C_INSERE_LISTA */
   DECLARE @C_INSERE_LISTA_CHAVE       VARCHAR;

   /* VARIAVEIS DO CURSOR C_LOTE */
   DECLARE @C_LOTE_CDLOTE              VARCHAR(04);
   DECLARE @C_LOTE_DTINICLOTE          DATETIME;
                                                              
                                                              
   EXEC NOVO_CODIGO 'LISTA', @V_SEQUENCIAL, 1;
   SET @V_SEQUENCIAL = SUBSTRING(@V_SEQUENCIAL,11,10);
                                                              
   DECLARE C_EMPRESA CURSOR FOR 
      SELECT CDEMPRESA
        FROM EMPRESA;
                                                              
   OPEN C_EMPRESA ;
                                                              
   FETCH NEXT FROM C_EMPRESA INTO @C_EMPRESA_CDEMPRESA;
                                                              
   WHILE (@@FETCH_STATUS = 0)   BEGIN
                                                              
         IF @P_TIPOMOVI = '00' BEGIN
            SELECT @V_DTINICIAL = DTINIINTCONTDFE, 
                   @V_DTFINAL   = DTFININTCONTDFE
              FROM CONTOPECTBL
             WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                              
            DECLARE C_INSERE_LISTA CURSOR FOR 
               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 (ISNULL(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));
                                                              
            OPEN C_INSERE_LISTA ;
                                                              
            FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                              
            WHILE (@@FETCH_STATUS = 0) BEGIN
                                                              
                INSERT INTO LISTA
                   (NRLISTA,CDLISTA)
                VALUES
                   (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                           
                FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
            END;
            CLOSE C_INSERE_LISTA;
            DEALLOCATE C_INSERE_LISTA;
                                                                      
            EXEC CONTABILIZAR @V_SEQUENCIAL, '00';
                                                                      
         END
         ELSE IF @P_TIPOMOVI = '01' BEGIN
                 SELECT @V_DTINICIAL = DTINIINTCONTDFS, 
                        @V_DTFINAL = DTFININTCONTDFS
                   FROM CONTOPECTBL
                  WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                                    
                 DECLARE C_INSERE_LISTA CURSOR FOR
                    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 (ISNULL(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));
                                                              
                 OPEN C_INSERE_LISTA;
                                                              
                 FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                              
                 WHILE (@@FETCH_STATUS = 0) BEGIN
                                                              
                    INSERT INTO LISTA
                       (NRLISTA,CDLISTA)
                    VALUES
                       (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                                                
                    FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                 END;
                 CLOSE C_INSERE_LISTA;
                 DEALLOCATE C_INSERE_LISTA;
                                                                  
                 EXEC CONTABILIZAR @V_SEQUENCIAL, '01';
                                                                
              END
              ELSE IF @P_TIPOMOVI = '02' BEGIN
                      SELECT @V_DTINICIAL = DTINIINTCONTDFE, 
                             @V_DTFINAL   = DTFININTCONTDFE
                        FROM CONTOPECTBL
                       WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                                              
                      DECLARE C_INSERE_LISTA CURSOR FOR
                         SELECT L.CDFILIAL + L.NRLANCESTQ AS CHAVE 
                           FROM LANCTOESTOQ L, FILIAL FL, PARAMFILIAL PF, PARAMGERAL PG
                          WHERE (L.IDTPLANCTO     = '3')
                            AND (L.CDFILIMOVI     = FL.CDFILIAL)
                            AND (FL.CDEMPRESA     = @C_EMPRESA_CDEMPRESA)
                            AND (L.DTLANCESTQ     BETWEEN @V_DTINICIAL AND @V_DTFINAL)
                            AND (L.IDSTATUSCTBEST <> 'C')
                            AND (PF.CDFILIAL      = L.CDFILIMOVI)
                            AND ((CASE PG.IDUTILDTPROC WHEN 'N' THEN GETDATE() + 1 ELSE ISNULL(PF.DTPROCESSA,GETDATE() + 1)END) > L.DTLANCESTQ)
                            AND (NOT EXISTS (SELECT NRLANCESTQ
                                               FROM ITLANCTOEST IT
                                              WHERE L.CDFILIAL = IT.CDFILIAL
                                                AND L.NRLANCESTQ = IT.NRLANCESTQ
                                                AND IT.CDLOTEITCTB IS NOT NULL));
                                                              
                         OPEN C_INSERE_LISTA;
                                                              
                         FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                              
                         WHILE (@@FETCH_STATUS = 0) BEGIN
                                                              
                           INSERT INTO LISTA
                              (NRLISTA,CDLISTA)
                           VALUES
                              (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                                                
                           FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                         END;
                         CLOSE C_INSERE_LISTA;
                         DEALLOCATE C_INSERE_LISTA;
                                                                  
                         EXEC CONTABILIZAR @V_SEQUENCIAL,'02';
                                                                        
                   END
                   ELSE IF @P_TIPOMOVI = '03' BEGIN
                           SELECT @V_DTINICIAL = DTINIINTCONTDFE, 
                                  @V_DTFINAL = DTFININTCONTDFE
                             FROM CONTOPECTBL
                            WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                                       
                           DECLARE C_INSERE_LISTA CURSOR FOR
                              SELECT APRCUSTOP.CDFILIAL + APRCUSTOP.NRORDPROD + APRCUSTOP.NRSUBORDPROD + APRCUSTOP.CDETAPAPROD + APRCUSTOP.NRAPRCUSTOP AS CHAVE
                                FROM APRCUSTOP, ORDPRODUCAO, FILIAL FL, PARAMFILIAL PF, PARAMGERAL PG
                               WHERE (APRCUSTOP.CDFUNCUSTO <> '099')
                                 AND (ORDPRODUCAO.IDSITORDPROD  = '3')
                                 AND (APRCUSTOP.CDFILIAL        = ORDPRODUCAO.CDFILIAL)
                                 AND (APRCUSTOP.NRORDPROD       = ORDPRODUCAO.NRORDPROD)
                                 AND (APRCUSTOP.NRSUBORDPROD    = ORDPRODUCAO.NRSUBORDPROD)
                                 AND (CONVERT(VARCHAR, ORDPRODUCAO.DTHRFIMREAOP, 103) BETWEEN @V_DTINICIAL AND @V_DTFINAL)
                                 AND (APRCUSTOP.CDFILIAL        = FL.CDFILIAL)
                                 AND (APRCUSTOP.CDFILIAL        = PF.CDFILIAL)
                                 AND (ORDPRODUCAO.IDGERACTBOP   <> 'N')
                                 AND (FL.CDEMPRESA = @C_EMPRESA_CDEMPRESA)
                                 AND (APRCUSTOP.IDSTATUSCTBOP <> 'C')
                                 AND ((CASE PG.IDUTILDTPROC WHEN 'N' THEN GETDATE() + 1 ELSE ISNULL(PF.DTPROCESSA, GETDATE() + 1)END) > ORDPRODUCAO.DTHRFIMREAOP)
                                                              
                           OPEN C_INSERE_LISTA;
                                                              
                           FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                              
                           WHILE (@@FETCH_STATUS = 0) BEGIN
                                                              
                              INSERT INTO LISTA
                                 (NRLISTA,CDLISTA)
                              VALUES
                                 (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                                                
                              FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                           END;
                           CLOSE C_INSERE_LISTA;
                           DEALLOCATE C_INSERE_LISTA;
                                                                  
                           EXEC CONTABILIZAR @V_SEQUENCIAL, '03';
                                                                    
                        END   
                        ELSE IF @P_TIPOMOVI = '04' BEGIN
                                SELECT @V_DTINICIAL = DTINIINTCONTDFE, @V_DTFINAL = DTFININTCONTDFE
                                  FROM CONTOPECTBL
                                 WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                                  
                                DECLARE C_INSERE_LISTA CURSOR FOR
                                    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 ((CASE PG.IDUTILDTPROC WHEN 'N' THEN GETDATE() + 1 ELSE ISNULL(PF.DTPROCESSA,GETDATE() + 1)END) > 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));
                                                              
                                OPEN C_INSERE_LISTA;
                                                              
                                FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                              
                                WHILE (@@FETCH_STATUS = 0) BEGIN
                                                              
                                   INSERT INTO LISTA
                                      (NRLISTA,CDLISTA)
                                   VALUES
                                      (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                                                
                                   FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                END;
                                CLOSE C_INSERE_LISTA;
                                DEALLOCATE C_INSERE_LISTA;
                                                                  
                                EXEC CONTABILIZAR @V_SEQUENCIAL, '04';
                                                                   
                             END                       
                             ELSE IF @P_TIPOMOVI = '05' BEGIN
                                     SELECT @V_DTINICIAL = DTINIINTCONTDFE, 
                                            @V_DTFINAL = DTFININTCONTDFE
                                       FROM CONTOPECTBL
                                      WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                                  
                                     DECLARE C_INSERE_LISTA CURSOR FOR
                                         SELECT LE.CDFILIAL + LE.NRLANCESTQ AS CHAVE
                                           FROM LANCTOESTOQ LE, FILIAL FL, PARAMFILIAL PF, PARAMGERAL PG
                                          WHERE LE.IDTPLANCTO IN('0','1')
                                            AND (LE.DTLANCESTQ BETWEEN @V_DTINICIAL AND @V_DTFINAL)
                                            AND (LE.CDFILIMOVI = FL.CDFILIAL)
                                            AND (LE.IDSTATUSCTBEST <> 'C')
                                            AND (FL.CDEMPRESA = @C_EMPRESA_CDEMPRESA)
                                            AND (PF.CDFILIAL = LE.CDFILIMOVI)
                                            AND ((CASE PG.IDUTILDTPROC WHEN 'N' THEN GETDATE() + 1 ELSE ISNULL(PF.DTPROCESSA,GETDATE() + 1)END) > 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));
                                                              
                                     OPEN C_INSERE_LISTA;
                                                              
                                     FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                              
                                     WHILE (@@FETCH_STATUS = 0) BEGIN
                                                              
                                         INSERT INTO LISTA
                                            (NRLISTA,CDLISTA)
                                         VALUES
                                            (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                                                
                                         FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                     END;
                                     CLOSE C_INSERE_LISTA;
                                     DEALLOCATE C_INSERE_LISTA;
                                                                  
                                     EXEC CONTABILIZAR @V_SEQUENCIAL, '05';
                                                                           
                                  END
                                  ELSE IF @P_TIPOMOVI = '06' BEGIN
                                          SELECT @V_DTINICIAL = DTINICCTBL, @V_DTFINAL = DTFINACCTBL
                                            FROM CONTOPECTBL
                                           WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                                                 
                                          DECLARE C_INSERE_LISTA CURSOR FOR
                                               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));
                                                              
                                          OPEN C_INSERE_LISTA;
                                                              
                                          FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                              
                                          WHILE (@@FETCH_STATUS = 0) BEGIN
                                                              
                                             INSERT INTO LISTA
                                                (NRLISTA,CDLISTA)
                                             VALUES
                                                (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                                                
                                             FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                          END;
                                          CLOSE C_INSERE_LISTA;
                                          DEALLOCATE C_INSERE_LISTA;
                                                                  
                                          EXEC CONTABILIZAR @V_SEQUENCIAL, '06';
                                                                  
                                       END
                                       ELSE IF @P_TIPOMOVI = '07' BEGIN
                                               SELECT @V_DTINICIAL = DTINICCTBL, 
                                                      @V_DTFINAL = DTFINACCTBL
                                                 FROM CONTOPECTBL
                                                WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                                                                                           
                                               DECLARE C_INSERE_LISTA CURSOR FOR 
                                                   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));
                                                              
                                               OPEN C_INSERE_LISTA;
                                                                                                                               
                                               FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                                                                                               
                                               WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                               
                                                 INSERT INTO LISTA
                                                    (NRLISTA,CDLISTA)
                                                 VALUES
                                                    (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                                                                                                               
                                                 FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                               END;
                                               CLOSE C_INSERE_LISTA;
                                               DEALLOCATE C_INSERE_LISTA;
                                                                                                                               
                                               EXEC CONTABILIZAR @V_SEQUENCIAL, '07';
                                                                                                                               
                                           END                                                    
                                           ELSE IF @P_TIPOMOVI = '08' BEGIN
                                                   SELECT @V_DTINICIAL = DTINICCTBL, 
                                                          @V_DTFINAL = DTFINACCTBL
                                                     FROM CONTOPECTBL
                                                    WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                                                                                                 
                                                   DECLARE C_INSERE_LISTA CURSOR FOR
                                                       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));
                                                                                                                               
                                                   OPEN C_INSERE_LISTA;
                                                                                                                               
                                                   FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                                                                                               
                                                   WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                               
                                                       INSERT INTO LISTA
                                                          (NRLISTA,CDLISTA)
                                                       VALUES
                                                          (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                                                                                                               
                                                       FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                    END;
                                                    CLOSE C_INSERE_LISTA;
                                                    DEALLOCATE C_INSERE_LISTA;
                                                                                                                               
                                                    EXEC CONTABILIZAR @V_SEQUENCIAL, '08';
                                                                                                                                  
                                                END
                                                ELSE IF @P_TIPOMOVI = '09' BEGIN
                                                        SELECT @V_DTINICIAL = DTINICCTBL, @V_DTFINAL = DTFINACCTBL
                                                          FROM CONTOPECTBL
                                                         WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                                                                                           
                                                        DECLARE C_INSERE_LISTA CURSOR FOR 
                                                            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));
                                                                                                                               
                                                        OPEN C_INSERE_LISTA;
                                                                                                                               
                                                        FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                                                                                               
                                                        WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                               
                                                            INSERT INTO LISTA
                                                               (NRLISTA,CDLISTA)
                                                            VALUES
                                                               (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                                                                                                               
                                                            FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                         END;
                                                         CLOSE C_INSERE_LISTA;
                                                         DEALLOCATE C_INSERE_LISTA;
                                                                                                                               
                                                         EXEC CONTABILIZAR @V_SEQUENCIAL, '09';
                                                                                                                               
                                                     END
                                                     ELSE IF @P_TIPOMOVI = '10' BEGIN
                                                             SELECT @V_DTINICIAL = DTINICCTBL, @V_DTFINAL = DTFINACCTBL
                                                               FROM CONTOPECTBL
                                                              WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                                                                                               
                                                             DECLARE C_INSERE_LISTA CURSOR FOR
                                                                 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));
                                                                                                                               
                                                             OPEN C_INSERE_LISTA;
                                                                                                                               
                                                             FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                                                                                               
                                                             WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                               
                                                                 INSERT INTO LISTA
                                                                    (NRLISTA,CDLISTA)
                                                                 VALUES
                                                                    (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                                                                                                               
                                                                 FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                             END;
                                                             CLOSE C_INSERE_LISTA;
                                                             DEALLOCATE C_INSERE_LISTA;
                                                                                                                               
                                                             EXEC CONTABILIZAR @V_SEQUENCIAL, '10';
                                                                                                                               
                                                          END
                                                          ELSE IF @P_TIPOMOVI = '11' BEGIN
                                                                  SELECT @V_DTINICIAL = DTINICCTBL, @V_DTFINAL = DTFINACCTBL
                                                                    FROM CONTOPECTBL
                                                                   WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA;
                                                                                                                            
                                                                  DECLARE C_INSERE_LISTA CURSOR FOR 
                                                                      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));
                                                                                                                                                       
                                                                  OPEN C_INSERE_LISTA;
                                                                                                                                                       
                                                                  FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                                                                                                                       
                                                                  WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                                                       
                                                                      INSERT INTO LISTA
                                                                          (NRLISTA,CDLISTA)
                                                                       VALUES
                                                                          (@V_SEQUENCIAL, @C_INSERE_LISTA_CHAVE);
                                                                                                                                                       
                                                                       FETCH NEXT FROM C_INSERE_LISTA INTO @C_INSERE_LISTA_CHAVE;
                                                                  END;
                                                                  CLOSE C_INSERE_LISTA;
                                                                  DEALLOCATE C_INSERE_LISTA;
                                                                                                                                                       
                                                                  EXEC CONTABILIZAR @V_SEQUENCIAL, '11';
                                                                                                                                        
                                                               END;
         DECLARE C_LOTE CURSOR FOR
            SELECT CDLOTE, DTINICLOTE
              FROM CAPALOTE
             WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA
               AND IDTPMOVLOTE IS NOT NULL
               AND IDLOTE <> 'L';
                                                                                                                                  
         OPEN C_LOTE;
                                                                                                                                 
         FETCH NEXT FROM C_LOTE INTO @C_LOTE_CDLOTE, @C_LOTE_DTINICLOTE;
                                                                                                                                  
         WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                                  
              SELECT @V_IDLIBAUTLOTE = IDLIBAUTLOTE
                FROM AGENDACONTAB
               WHERE IDAGENDMOVCONT = @P_TIPOMOVI;
                                                                                                                                  
              IF @V_IDLIBAUTLOTE = 'S' BEGIN
                                                                                                                                  
                 IF NOT EXISTS (                                                                                                                 
                     SELECT SUM(CREDITO) AS CREDITO, SUM(DEBITO)  AS DEBITO, SUM(TOTAL)   AS 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)
                 BEGIN
                    SET @V_CREDITO    = 0;
                    SET @V_DEBITO     = 0;
                    SET @V_TOTALGERAL = 0; 
                 END
                 ELSE BEGIN
                     SELECT @V_CREDITO    = SUM(CREDITO),
                            @V_DEBITO     = SUM(DEBITO),
                            @V_TOTALGERAL = SUM(TOTAL)
                       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;
                 END;                                                                                                
                                                                                                                                  
                 IF @V_TOTALGERAL = 0 BEGIN
                     UPDATE CAPALOTE
                        SET VRTOTCRDIGI = @V_CREDITO,
                            VRTOTDBDIGI = @V_DEBITO
                      WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA
                        AND CDLOTE = @C_LOTE_CDLOTE;
                                                                                                                                  
                       SELECT @V_COUNT = COUNT(*)
                         FROM ERROLANCLOTE
                        WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA
                          AND CDLOTE = @C_LOTE_CDLOTE;
                                                                                                                                  
                      IF @V_COUNT = 0 BEGIN
                            SELECT @V_COUNTNR = COUNT(DISTINCT NRLANCLOTE)
                              FROM LANCLOTE
                             WHERE CDEMPRESA = @C_EMPRESA_CDEMPRESA
                               AND CDLOTE = @C_LOTE_CDLOTE;
                                                                                                                                  
                         IF @V_COUNTNR <> 0 BEGIN
                            SET  @C_EMPRESA_CDEMPRESAAJUST = 'LANCCONTABIL' + @C_EMPRESA_CDEMPRESA + SUBSTRING(CONVERT(VARCHAR, @C_LOTE_DTINICLOTE, 112), 1, 6);
                            EXEC NOVO_CODIGO @C_EMPRESA_CDEMPRESAAJUST, @V_SEQUENCIAL1, @V_COUNTNR;
                                                                                                                                   
                            SET @V_SEQUENCIAL = SUBSTRING(CONVERT(VARCHAR, @C_LOTE_DTINICLOTE, 112), 1, 6) + SUBSTRING(@V_SEQUENCIAL1, 15, 6);
                            EXEC LIBERA_LOTE @C_EMPRESA_CDEMPRESA, @C_LOTE_CDLOTE, @V_SEQUENCIAL, '';
                         END;
                      END
                      ELSE BEGIN
                         UPDATE CAPALOTE
                            SET QTERROLOTE = @V_COUNT, IDLOTE = 'C'
                          WHERE CDEMPRESA  = @C_EMPRESA_CDEMPRESA
                            AND CDLOTE     = @C_LOTE_CDLOTE;
                      END;
                                                                                                                                 
                   UPDATE CAPALOTE
                      SET QTERROLOTE = 0, IDLOTE = 'C'
                    WHERE CDEMPRESA  = @C_EMPRESA_CDEMPRESA
                      AND CDLOTE     = @C_LOTE_CDLOTE;
                                                                                                 
                 END                                                                                                                 
                 ELSE BEGIN
                   UPDATE CAPALOTE
                      SET QTERROLOTE = 1, IDLOTE = 'C'
                    WHERE CDEMPRESA  = @C_EMPRESA_CDEMPRESA
                      AND CDLOTE     = @C_LOTE_CDLOTE;
                 END;                                
                                                                                                                                  
                 FETCH NEXT FROM C_LOTE INTO @C_INSERE_LISTA_CHAVE;
              END;
         END;
         CLOSE C_LOTE;
         DEALLOCATE C_LOTE;
                                                                                                                                     
         FETCH NEXT FROM C_EMPRESA INTO @C_EMPRESA_CDEMPRESA;
   END;
   CLOSE C_EMPRESA;
   DEALLOCATE C_EMPRESA;
END                                                                                                                                 
GO