IF EXISTS (SELECT * FROM SYSOBJECTS O, SYSUSERS U
            WHERE O.NAME = 'CONTABILIZA_PAG_CONTA' AND
                  TYPE = 'P' 	                     AND
	               U.UID = O.UID                    AND
	               U.NAME = USER)
   DROP PROCEDURE CONTABILIZA_PAG_CONTA
GO
                                                                                                                
CREATE PROCEDURE CONTABILIZA_PAG_CONTA(
   @P_NRLISTA  VARCHAR(10))
AS
   DECLARE @V_GERLOTE                     VARCHAR(01);
   DECLARE @V_IDRETORNA                   VARCHAR(01);
   DECLARE @V_IDCONTCCUS                  VARCHAR(01);
   DECLARE @V_CDHISTPADR                  VARCHAR(04);
   DECLARE @V_CDHISTPADRA                 VARCHAR(04);
   DECLARE @V_CDCENTCUST                  VARCHAR(04);
   DECLARE @V_CDFILIAL                    VARCHAR(04);
   DECLARE @V_NRLANCLOTE                  VARCHAR(06);
   DECLARE @V_CDLOTE                      VARCHAR(20);
   DECLARE @V_STCONTCTBL                  VARCHAR(20);
   DECLARE @V_STCONTCTBLP                 VARCHAR(20);
   DECLARE @V_STCONTADIFE                 VARCHAR(20);
   DECLARE @V_DSHISTPADR                  VARCHAR(1000);
   DECLARE @V_COUNT                       NUMERIC;
   DECLARE @V_INPARTIDA                   INTEGER;
   DECLARE @V_VRRATEIO                    DOUBLE PRECISION;
   DECLARE @V_VALOR                       DOUBLE PRECISION;
   DECLARE @V_VALORAJUST                  DOUBLE PRECISION;
   DECLARE @V_DBFATOR                     DOUBLE PRECISION;
   DECLARE @V_DOVRTOTALRAT                DOUBLE PRECISION;
                                                                                                                
   /* VARIAVEIS DO CURSOR C_PAG_CONTA */
   DECLARE @C_PAG_CONTA_CDTPCTPAGAR       VARCHAR(06);
   DECLARE @C_PAG_CONTA_CDSERIEENTR       VARCHAR(05);
   DECLARE @C_PAG_CONTA_VRNOTAFISC        DOUBLE PRECISION;
   DECLARE @C_PAG_CONTA_VRNOTAFISCAJUST   DOUBLE PRECISION;
   DECLARE @C_PAG_CONTA_CDFILIORIG        VARCHAR(30);
   DECLARE @C_PAG_CONTA_CDTIPOOPER        VARCHAR(02);
   DECLARE @C_PAG_CONTA_NRINSJURFORN      VARCHAR(20);
   DECLARE @C_PAG_CONTA_CDFORNECED        VARCHAR(21);
   DECLARE @C_PAG_CONTA_NRNOTAFISC        VARCHAR(20);
   DECLARE @C_PAG_CONTA_NRNOTAFISCAJUST   VARCHAR(26);
   DECLARE @C_PAG_CONTA_DTEMISSAO         DATETIME;
   DECLARE @C_PAG_CONTA_DTENTRSAID        DATETIME;
   DECLARE @C_PAG_CONTA_IDSTATUS          VARCHAR(01);
   DECLARE @C_PAG_CONTA_NRLANCTBDOC       VARCHAR(12);
   DECLARE @C_PAG_CONTA_CDFILIAL          VARCHAR(04);
   DECLARE @C_PAG_CONTA_CDEMPRESA         VARCHAR(02);
   DECLARE @C_PAG_CONTA_NRLANCTONF        VARCHAR(10);
   DECLARE @C_PAG_CONTA_CDPADRCONTABIL    VARCHAR(05);
                                                                                                                
   /* VARIAVEIS DO CURSOR C_PARTIDA */
   DECLARE @C_PARTIDA_IDTPPARTCONTAB      VARCHAR(01);
   DECLARE @C_PARTIDA_CDCONTCTBL          VARCHAR(20);
   DECLARE @C_PARTIDA_IDTPCONTAPART       VARCHAR(02);
   DECLARE @C_PARTIDA_IDCREDDEBCONTAB     VARCHAR(01);
   DECLARE @C_PARTIDA_IDTPVALORPART       VARCHAR(02);
                                                                                                                
   /* VARIAVEIS DO CURSOR C_RATTPCTP */
   DECLARE @C_RATTPCTP_CDCENTCUST         VARCHAR(04);
   DECLARE @C_RATTPCTP_CDFIRATTPCTP       VARCHAR(04);
   DECLARE @C_RATTPCTP_CDFILIAL           VARCHAR(04);
   DECLARE @C_RATTPCTP_VRPERATTPCTP       DOUBLE PRECISION;
                                                                                                                
   /* VARIAVEIS DO CURSOR @C_AJUSPAG */
   DECLARE @C_AJUSPAG_CDFILIAL            VARCHAR(04);
   DECLARE @C_AJUSPAG_NRLANCTONF          VARCHAR(10);
   DECLARE @C_AJUSPAG_NRSEQVENPAG         VARCHAR(02);
   DECLARE @C_AJUSPAG_NRPARCELAPAG        VARCHAR(02);
   DECLARE @C_AJUSPAG_DTAJUSTEPAG         DATETIME;
   DECLARE @C_AJUSPAG_CDAJUCTPAG          VARCHAR(02);
   DECLARE @C_AJUSPAG_IDAJUCTPAG          VARCHAR(01);
   DECLARE @C_AJUSPAG_DSAJUCTPAG          VARCHAR(40);
   DECLARE @C_AJUSPAG_DSAJUSTEPAG         VARCHAR(20);
   DECLARE @C_AJUSPAG_VRAJUSTEPAG         DOUBLE PRECISION;
   DECLARE @C_AJUSPAG_VRAJUSTEPAGAJUST    DOUBLE PRECISION;
   DECLARE @C_AJUSPAG_CDECTBAJPAG         VARCHAR(02);
   DECLARE @C_AJUSPAG_NRLCTBAJPAG         VARCHAR(12);
   DECLARE @C_AJUSPAG_CDCLASSFINA         VARCHAR(06);
   DECLARE @C_AJUSPAG_CDEMMFAJCNTP        VARCHAR(02);
   DECLARE @C_AJUSPAG_CDFIMFAJCNTP        VARCHAR(04);
   DECLARE @C_AJUSPAG_DTMOVFLUXO          DATETIME;
   DECLARE @C_AJUSPAG_IDMOVFLUXO          VARCHAR(01);
                                                                                                                
   /* VARIAVEIS DO CURSOR @C_PARTIDAAJUS */
   DECLARE @C_PARTIDAAJUS_IDTPPARTCONTAB  VARCHAR(01);
   DECLARE @C_PARTIDAAJUS_CDCONTCTBL      VARCHAR(20);
   DECLARE @C_PARTIDAAJUS_IDTPCONTAPART   VARCHAR(02);
   DECLARE @C_PARTIDAAJUS_IDCREDDEBCONTAB VARCHAR(01);
   DECLARE @C_PARTIDAAJUS_IDTPVALORPART   VARCHAR(02);
                                                                                                                
   /* VARIAVEIS DO CURSOR C_TPAJUSPAG */
   DECLARE @C_TPAJUSPAG_CDAJUCTPAG        VARCHAR(02);
   DECLARE @C_TPAJUSPAG_CDHISTPADR        VARCHAR(04);
   DECLARE @C_TPAJUSPAG_CDCONTCTBL        VARCHAR(20);
   DECLARE @C_TPAJUSPAG_DSAJUCTPAG        VARCHAR(40);
                                                                                                                
   /* VARIAVEIS DO CURSOR C_RATTPAJP */
   DECLARE @C_RATTPAJP_CDCENTCUST         VARCHAR(04);
   DECLARE @C_RATTPAJP_CDFIRATTPAJP       VARCHAR(04);
   DECLARE @C_RATTPAJP_CDFILIAL           VARCHAR(04);
   DECLARE @C_RATTPAJP_VRPERATTPAJP       DOUBLE PRECISION;
BEGIN
   EXEC EXCLUI_LANCCONTABIL '06', @P_NRLISTA;
                                                                                                                
   DECLARE C_PAG_CONTA CURSOR FOR
       SELECT DISTINCT CP.CDTPCTPAGAR, NF.CDSERIEENTR, NF.VRNOTAFISC,
              NF.CDFILIRECE AS CDFILIORIG, NF.CDTIPOOPER, NF.NRINSJURNF AS NRINSJURFORN,
              NF.CDFORNECED, NF.NRNOTAFISC, NF.DTEMISSAO, NF.DTENTRSAID, NF.IDSTATUS,
              NF.NRLANCTBDOC,NF.CDFILIAL, NF.CDEMPRESA, NF.NRLANCTONF, T.CDPADRCONTABIL
         FROM NOTAFISCAL NF,CONTAPAGAR CP ,LISTA L, TIPOCTPAGAR T
        WHERE (NF.CDFILIAL     = SUBSTRING(L.CDLISTA, 01, 04))
          AND (NF.NRLANCTONF   = SUBSTRING(L.CDLISTA, 05, 10))
          AND (CP.CDFILIAL     = NF.CDFILIAL)
          AND (CP.NRLANCTONF   = NF.NRLANCTONF)
          AND (L.NRLISTA       = @P_NRLISTA)
          AND (CP.CDTPCTPAGAR  = T.CDTPCTPAGAR)
        ORDER BY NF.NRLANCTONF;
                                                                                                                
   OPEN C_PAG_CONTA;
   FETCH NEXT FROM C_PAG_CONTA INTO @C_PAG_CONTA_CDTPCTPAGAR, @C_PAG_CONTA_CDSERIEENTR,  @C_PAG_CONTA_VRNOTAFISC,
                                    @C_PAG_CONTA_CDFILIORIG,  @C_PAG_CONTA_CDTIPOOPER,   @C_PAG_CONTA_NRINSJURFORN,
                                    @C_PAG_CONTA_CDFORNECED,  @C_PAG_CONTA_NRNOTAFISC,   @C_PAG_CONTA_DTEMISSAO,
                                    @C_PAG_CONTA_DTENTRSAID,  @C_PAG_CONTA_IDSTATUS,     @C_PAG_CONTA_NRLANCTBDOC,
                                    @C_PAG_CONTA_CDFILIAL,    @C_PAG_CONTA_CDEMPRESA,    @C_PAG_CONTA_NRLANCTONF,
                                    @C_PAG_CONTA_CDPADRCONTABIL;
                                                                                                                
   WHILE (@@FETCH_STATUS = 0) BEGIN
        IF (@C_PAG_CONTA_CDPADRCONTABIL IS NOT NULL) BEGIN
           EXEC GERA_LOTECONTABIL @C_PAG_CONTA_CDEMPRESA, '06', '', @C_PAG_CONTA_CDFILIORIG, @C_PAG_CONTA_DTENTRSAID,
                                  @C_PAG_CONTA_CDTPCTPAGAR, '', @V_CDLOTE, @V_NRLANCLOTE;

           DECLARE C_PARTIDA CURSOR FOR
               SELECT P.IDTPPARTCONTAB, P.CDCONTCTBL, P.IDTPCONTAPART, P.IDCREDDEBCONTAB, P.IDTPVALORPART
                 FROM PARTIDACONTABIL P
                WHERE P.CDPADRCONTABIL = @C_PAG_CONTA_CDPADRCONTABIL
                  AND ((IDTPCONTAPART  = '00' AND IDTPVALORPART = '00') OR (IDTPCONTAPART = '14' AND IDTPVALORPART = '00'))
                  AND (IDTPMOVLOTEPACO = '06')
                ORDER BY P.NRORDPARTCONTAB; /*SELECIONA AS PARTIDAS DO PADRAO DE CONTABILIZACAO*/
                                                                                                                
           OPEN C_PARTIDA;
           FETCH NEXT FROM C_PARTIDA INTO @C_PARTIDA_IDTPPARTCONTAB,  @C_PARTIDA_CDCONTCTBL, @C_PARTIDA_IDTPCONTAPART,
                                          @C_PARTIDA_IDCREDDEBCONTAB, @C_PARTIDA_IDTPVALORPART;
                                                                                                                
           WHILE (@@FETCH_STATUS = 0) BEGIN
                SELECT @V_CDHISTPADR = T.CDHISTPADR, 
                       @V_DSHISTPADR = H.DSHISTPADR
                  FROM TIPOCTPAGAR T, HISTPADR H
                 WHERE T.CDTPCTPAGAR = @C_PAG_CONTA_CDTPCTPAGAR
                   AND T.CDHISTPADR  = H.CDHISTPADR;
                                                                                                                
                IF (@V_CDHISTPADR IS NULL) BEGIN
                     SELECT @V_CDHISTPADR = HI.CDHISTPADR, 
                            @V_DSHISTPADR = HP.DSHISTPADR
                       FROM HISTINTEGRA HI, HISTPADR HP
                      WHERE (HI.CDHISTPADR = HP.CDHISTPADR)
                        AND (HI.CDOPERACAO = 'CTAPAG');
                END;
                                                                                                                
                EXEC MONTA_HISTORICO @C_PAG_CONTA_CDFILIAL, @C_PAG_CONTA_NRLANCTONF, @V_CDHISTPADR, '', '', '06', '',
                                     @V_DSHISTPADR;
                                                                                                                
                IF (@C_PARTIDA_IDTPCONTAPART = '00') BEGIN
                    EXEC CONTCTBL_FORNECEDOR @C_PAG_CONTA_CDEMPRESA, '', 'E', @C_PAG_CONTA_CDFILIORIG, @C_PAG_CONTA_CDFORNECED,
                                             @V_STCONTCTBL, @V_STCONTCTBLP, @V_STCONTADIFE, @V_CDHISTPADRA;
                END 
                ELSE IF (@C_PARTIDA_IDTPCONTAPART = '14') BEGIN
                    SELECT @V_STCONTCTBL = CDCONTCTBL
                      FROM TIPOCTPAGAR
                     WHERE CDTPCTPAGAR = @C_PAG_CONTA_CDTPCTPAGAR;
                END;
                                                                                                                
                IF (@C_PARTIDA_IDTPVALORPART = '00') BEGIN
                    SET @V_VALOR = @C_PAG_CONTA_VRNOTAFISC;
                END;
                                                                                                                
                SELECT @V_CDCENTCUST = ISNULL(F.CDCENTCUST, E.CDCENTCUST)
                  FROM FILIAL F, EMPRESA E
                 WHERE F.CDFILIAL  = @C_PAG_CONTA_CDFILIORIG
                   AND E.CDEMPRESA = @C_PAG_CONTA_CDEMPRESA;
                                                                                                                
                SELECT @V_CDFILIAL = CDFILIAL
                  FROM RATTPCTP
                 WHERE (CDTPCTPAGAR = @C_PAG_CONTA_CDTPCTPAGAR);
                                                                                                               
                IF (@C_PARTIDA_IDCREDDEBCONTAB = 'D') BEGIN
                   SET @V_VALORAJUST = -@V_VALOR; 
                   SET @C_PAG_CONTA_NRNOTAFISCAJUST = @C_PAG_CONTA_NRNOTAFISC + '/' + @C_PAG_CONTA_CDSERIEENTR;
                                                                                                               
                   EXEC NOVA_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE, @V_STCONTCTBL,
                                     @V_CDHISTPADR, @V_DSHISTPADR, @V_VALORAJUST, 'F', @C_PAG_CONTA_CDFORNECED, '06',
                                     @C_PAG_CONTA_NRNOTAFISCAJUST, 'S';
                                                                                                               
                   SET @V_INPARTIDA    = 1;
                   SET @V_DOVRTOTALRAT = 0;
                                                                                                               
                   SELECT @V_COUNT = COUNT(*)
                     FROM RATTPCTP
                    WHERE (CDTPCTPAGAR = @C_PAG_CONTA_CDTPCTPAGAR);
                                                                                                               
                   IF (@V_CDFILIAL IS NOT NULL) BEGIN
                                                                                                               
                      DECLARE C_RATTPCTP CURSOR FOR
                         SELECT CDCENTCUST, CDFIRATTPCTP, CDFILIAL, VRPERATTPCTP
                           FROM RATTPCTP
                          WHERE (CDTPCTPAGAR = @C_PAG_CONTA_CDTPCTPAGAR);
                                                                                                               
                      OPEN C_RATTPCTP;
                      FETCH NEXT FROM C_RATTPCTP INTO @C_RATTPCTP_CDCENTCUST, @C_RATTPCTP_CDFIRATTPCTP, 
                                                      @C_RATTPCTP_CDFILIAL,   @C_RATTPCTP_VRPERATTPCTP;
                                                                                                               
                      WHILE (@@FETCH_STATUS = 0) BEGIN
                          SET @V_DOVRTOTALRAT = @V_DOVRTOTALRAT + ROUND(((@C_PAG_CONTA_VRNOTAFISC * @C_RATTPCTP_VRPERATTPCTP) / 100), 2);
                          SET @V_VRRATEIO = (ROUND(((@C_PAG_CONTA_VRNOTAFISC * @C_RATTPCTP_VRPERATTPCTP) / 100), 2) * -1);
                                                                                                               
                          IF (@V_INPARTIDA = @V_COUNT) BEGIN
                             IF (@C_PAG_CONTA_VRNOTAFISC <> @V_DOVRTOTALRAT) BEGIN
                                SET @V_VRRATEIO = @V_VRRATEIO + (@C_PAG_CONTA_VRNOTAFISC - @V_DOVRTOTALRAT);
                             END;
                          END;
                                                                                                               
                          IF (@C_RATTPCTP_CDFILIAL IS NOT NULL) BEGIN
                             EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                                 @C_RATTPCTP_CDFILIAL, @C_RATTPCTP_CDCENTCUST, @V_VRRATEIO;
                          END
                          ELSE BEGIN
                             IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                                EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                                    @C_PAG_CONTA_CDFILIORIG, @C_RATTPCTP_CDCENTCUST, @V_VRRATEIO;
                             END;
                          END;
                                                                                                               
                          SET @V_INPARTIDA = @V_INPARTIDA + 1 ;
                                                                                                               
                          FETCH NEXT FROM C_RATTPCTP INTO @C_RATTPCTP_CDCENTCUST, @C_RATTPCTP_CDFIRATTPCTP, 
                                                          @C_RATTPCTP_CDFILIAL,   @C_RATTPCTP_VRPERATTPCTP;
                      END;
                      CLOSE C_RATTPCTP;
                      DEALLOCATE C_RATTPCTP;
                   END
                   ELSE BEGIN
                      IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                         SET @V_VALORAJUST = ROUND((@V_VALOR * -1), 0);
                         EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                             @C_PAG_CONTA_CDFILIORIG, @V_CDCENTCUST, @V_VALORAJUST;
                      END;
                   END;
                END
                ELSE BEGIN
                   SET @C_PAG_CONTA_NRNOTAFISCAJUST = @C_PAG_CONTA_NRNOTAFISC + '/' + @C_PAG_CONTA_CDSERIEENTR;
                   EXEC NOVA_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE, 
                                     @V_STCONTCTBL, @V_CDHISTPADR, @V_DSHISTPADR, @V_VALOR, 'F', @C_PAG_CONTA_CDFORNECED,
                                     '06', @C_PAG_CONTA_NRNOTAFISCAJUST, 'S';
                                                                                                               
                   IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                      EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                          @C_PAG_CONTA_CDFILIORIG, @V_CDCENTCUST, @V_VALOR;
                   END;
                END;
                FETCH NEXT FROM C_PARTIDA INTO @C_PARTIDA_IDTPPARTCONTAB,  @C_PARTIDA_CDCONTCTBL, @C_PARTIDA_IDTPCONTAPART,
                                               @C_PARTIDA_IDCREDDEBCONTAB, @C_PARTIDA_IDTPVALORPART;
           END;
           CLOSE C_PARTIDA;
           DEALLOCATE C_PARTIDA;
                                                                                                               
           DECLARE C_AJUSPAG CURSOR FOR
              SELECT A.CDFILIAL, A.NRLANCTONF, A.NRSEQVENPAG, A.NRPARCELAPAG,
                     A.DTAJUSTEPAG, A.CDAJUCTPAG, T.IDAJUCTPAG, T.DSAJUCTPAG,
                     A.DSAJUSTEPAG, A.VRAJUSTEPAG, A.CDECTBAJPAG, A.NRLCTBAJPAG,
                     A.CDCLASSFINA, A.CDEMMFAJCNTP, A.CDFIMFAJCNTP, A.DTMOVFLUXO,
                     A.IDMOVFLUXO
                FROM TIPOAJPAGAR T, AJCTPAGAR A, LISTA L
               WHERE (A.CDFILIAL   = SUBSTRING(L.CDLISTA, 01, 04))
                 AND (A.NRLANCTONF = SUBSTRING(L.CDLISTA, 05, 10))
                 AND (A.CDAJUCTPAG = T.CDAJUCTPAG)
                 AND (L.NRLISTA    = @P_NRLISTA)
               ORDER BY A.CDFILIAL, A.NRLANCTONF, A.CDAJUCTPAG;
                                                                                                               
           OPEN C_AJUSPAG;
           FETCH NEXT FROM C_AJUSPAG INTO @C_AJUSPAG_CDFILIAL,     @C_AJUSPAG_NRLANCTONF,   @C_AJUSPAG_NRSEQVENPAG,
                                          @C_AJUSPAG_NRPARCELAPAG, @C_AJUSPAG_DTAJUSTEPAG,  @C_AJUSPAG_CDAJUCTPAG,
                                          @C_AJUSPAG_IDAJUCTPAG,   @C_AJUSPAG_DSAJUCTPAG,   @C_AJUSPAG_DSAJUSTEPAG,
                                          @C_AJUSPAG_VRAJUSTEPAG,  @C_AJUSPAG_CDECTBAJPAG,  @C_AJUSPAG_NRLCTBAJPAG,
                                          @C_AJUSPAG_CDCLASSFINA,  @C_AJUSPAG_CDEMMFAJCNTP, @C_AJUSPAG_CDFIMFAJCNTP,
                                          @C_AJUSPAG_DTMOVFLUXO,   @C_AJUSPAG_IDMOVFLUXO;
                                                                                                               
           WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                               
               DECLARE C_PARTIDAAJUS CURSOR FOR
                   SELECT P.IDTPPARTCONTAB, P.CDCONTCTBL, P.IDTPCONTAPART, P.IDCREDDEBCONTAB, P.IDTPVALORPART
                     FROM PARTIDACONTABIL P
                    WHERE P.CDPADRCONTABIL = @C_PAG_CONTA_CDPADRCONTABIL
                      AND (IDTPVALORPART   = '06')
                      AND (IDTPMOVLOTEPACO = '06')
                    ORDER BY P.NRORDPARTCONTAB;
                                                                                                               
               OPEN C_PARTIDAAJUS;
               FETCH NEXT FROM C_PARTIDAAJUS INTO @C_PARTIDAAJUS_IDTPPARTCONTAB,  @C_PARTIDAAJUS_CDCONTCTBL, @C_PARTIDAAJUS_IDTPCONTAPART,
                                                  @C_PARTIDAAJUS_IDCREDDEBCONTAB, @C_PARTIDAAJUS_IDTPVALORPART;
                                                                                                               
               WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                               
                    DECLARE C_TPAJUSPAG CURSOR FOR
                        SELECT T.CDAJUCTPAG, T.CDHISTPADR, T.CDCONTCTBL, T.DSAJUCTPAG,
                               T.IDAJUCTPAG, H.DSHISTPADR, IDCTBAJCTPAG
                          FROM TIPOAJPAGAR T, HISTPADR H
                         WHERE (T.CDAJUCTPAG = @C_AJUSPAG_CDAJUCTPAG)
                           AND (T.CDHISTPADR = H.CDHISTPADR)
                         ORDER BY T.CDAJUCTPAG;
                                                                                                                
                    OPEN C_TPAJUSPAG;
                    FETCH NEXT FROM C_TPAJUSPAG INTO @C_TPAJUSPAG_CDAJUCTPAG, @C_TPAJUSPAG_CDHISTPADR,
                                                     @C_TPAJUSPAG_CDCONTCTBL, @C_TPAJUSPAG_DSAJUCTPAG;  
                                                                                                                
                    WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                
                         EXEC MONTA_HISTORICO @C_AJUSPAG_CDFILIAL, @C_AJUSPAG_NRLANCTONF, @C_TPAJUSPAG_CDHISTPADR,'','',
                                              '06', @C_AJUSPAG_CDAJUCTPAG, @V_DSHISTPADR;
                                                                                                                
                         FETCH NEXT FROM C_TPAJUSPAG INTO @C_TPAJUSPAG_CDAJUCTPAG, @C_TPAJUSPAG_CDHISTPADR,
                                                          @C_TPAJUSPAG_CDCONTCTBL, @C_TPAJUSPAG_DSAJUCTPAG;
                    END;
                    CLOSE C_TPAJUSPAG;
                    DEALLOCATE C_TPAJUSPAG;
                                                                                                                
                    IF (@C_PARTIDAAJUS_IDTPCONTAPART = '00') BEGIN
                        EXEC CONTCTBL_FORNECEDOR @C_PAG_CONTA_CDEMPRESA, '', 'E', @C_PAG_CONTA_CDFILIORIG, @C_PAG_CONTA_CDFORNECED,
                                                 @V_STCONTCTBL, @V_STCONTCTBLP, @V_STCONTADIFE, @V_CDHISTPADRA;
                    END
                    ELSE IF (@C_PARTIDAAJUS_IDTPCONTAPART = '19') BEGIN
                        SELECT @V_STCONTCTBL = CDCONTCTBL
                          FROM TIPOAJPAGAR
                         WHERE CDAJUCTPAG = @C_AJUSPAG_CDAJUCTPAG;
                    END;
                                                                                                                
                    IF (@C_PARTIDAAJUS_IDTPVALORPART = '06') BEGIN
                        SET @V_VALOR = @C_AJUSPAG_VRAJUSTEPAG;
                    END;
                                                                                                                
                    IF (@C_PARTIDAAJUS_IDCREDDEBCONTAB = 'D') BEGIN
                        SET @V_VALORAJUST = (@V_VALOR * -1);
                        SET @C_PAG_CONTA_NRNOTAFISCAJUST = (@C_PAG_CONTA_NRNOTAFISC + '/' + @C_PAG_CONTA_CDSERIEENTR);
                                                                                                                
                        EXEC NOVA_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE, 
                                          @V_STCONTCTBL, @V_CDHISTPADR, @V_DSHISTPADR, @V_VALORAJUST, 'F', 
                                          @C_PAG_CONTA_CDFORNECED, '06', @C_PAG_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                
                        IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                           SET @V_VALORAJUST = ((@V_VALOR)*-1);
                           EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                               @C_PAG_CONTA_CDFILIORIG, @V_CDCENTCUST, @V_VALORAJUST;
                        END;
                    END
                    ELSE BEGIN
                       SET @C_PAG_CONTA_NRNOTAFISCAJUST = (@C_PAG_CONTA_NRNOTAFISC + '/' + @C_PAG_CONTA_CDSERIEENTR);
                       EXEC NOVA_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                         @V_STCONTCTBL, @V_CDHISTPADR, @V_DSHISTPADR, @V_VALOR, 'F', @C_PAG_CONTA_CDFORNECED,
                                         '06', @C_PAG_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                
                       SET @V_INPARTIDA = 1;
                                                                                                                
                       SELECT @V_CDFILIAL = CDFILIAL
                         FROM RATTPAJP
                        WHERE (CDAJUCTPAG = @C_AJUSPAG_CDAJUCTPAG);
                                                                                                                
                       SELECT @V_COUNT = COUNT(*)
                         FROM RATTPAJP
                        WHERE (CDAJUCTPAG = @C_AJUSPAG_CDAJUCTPAG);
                                                                                                                
                       SET @V_DOVRTOTALRAT = 0;
                                                                                                                
                       IF (@V_CDFILIAL IS NOT NULL) BEGIN
                          DECLARE C_RATTPAJP CURSOR FOR
                             SELECT CDCENTCUST, CDFIRATTPAJP, CDFILIAL, VRPERATTPAJP
                               FROM RATTPAJP
                              WHERE (CDAJUCTPAG = @C_AJUSPAG_CDAJUCTPAG);
                                                                                                                
                          OPEN C_RATTPAJP;
                          FETCH NEXT FROM C_RATTPAJP INTO @C_RATTPAJP_CDCENTCUST, @C_RATTPAJP_CDFIRATTPAJP,
                                                          @C_RATTPAJP_CDFILIAL, @C_RATTPAJP_VRPERATTPAJP;
                                                                                                                
                          WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                                                                                                                                
                             SET @V_DOVRTOTALRAT = 0;
                             SET @V_DOVRTOTALRAT = @V_DOVRTOTALRAT + ROUND(((@C_AJUSPAG_VRAJUSTEPAG * @C_RATTPAJP_VRPERATTPAJP) / 100), 2);
                             SET @V_VRRATEIO = (ROUND(((@C_AJUSPAG_VRAJUSTEPAG * @C_RATTPAJP_VRPERATTPAJP) / 100), 2));
                                                                                                                
                             IF (@C_RATTPAJP_CDFILIAL IS NOT NULL) BEGIN
                                EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                                    @C_RATTPAJP_CDFILIAL, @C_RATTPAJP_CDCENTCUST, @V_VRRATEIO;
                             END
                             ELSE BEGIN
                                EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                                    @C_PAG_CONTA_CDFILIORIG, @C_RATTPAJP_CDCENTCUST, @V_VRRATEIO;
                             END;
                                                                                                                
                             SET @V_INPARTIDA = @V_INPARTIDA + 1;
                                                                                                                
                             FETCH NEXT FROM C_RATTPAJP INTO @C_RATTPAJP_CDCENTCUST, @C_RATTPAJP_CDFIRATTPAJP,
                                                             @C_RATTPAJP_CDFILIAL, @C_RATTPAJP_VRPERATTPAJP; 
                          END;
                          CLOSE C_RATTPAJP;
                          DEALLOCATE C_RATTPAJP;
                       END
                       ELSE BEGIN
                         IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                            SET @C_AJUSPAG_VRAJUSTEPAGAJUST = (ROUND(@C_AJUSPAG_VRAJUSTEPAG, 0));
                            EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                                @C_PAG_CONTA_CDFILIORIG, @V_CDCENTCUST, @C_AJUSPAG_VRAJUSTEPAGAJUST;
                         END;                  
                       END;
                    END;
                    FETCH NEXT FROM C_PARTIDAAJUS INTO @C_PARTIDAAJUS_IDTPPARTCONTAB, @C_PARTIDAAJUS_CDCONTCTBL, 
                                                       @C_PARTIDAAJUS_IDTPCONTAPART,  @C_PARTIDAAJUS_IDCREDDEBCONTAB, 
                                                       @C_PARTIDAAJUS_IDTPVALORPART; 
               END;
               CLOSE C_PARTIDAAJUS;
               DEALLOCATE C_PARTIDAAJUS;
                                                                                                                
               FETCH NEXT FROM C_AJUSPAG INTO @C_AJUSPAG_CDFILIAL,     @C_AJUSPAG_NRLANCTONF,   @C_AJUSPAG_NRSEQVENPAG,
                                              @C_AJUSPAG_NRPARCELAPAG, @C_AJUSPAG_DTAJUSTEPAG,  @C_AJUSPAG_CDAJUCTPAG,
                                              @C_AJUSPAG_IDAJUCTPAG,   @C_AJUSPAG_DSAJUCTPAG,   @C_AJUSPAG_DSAJUSTEPAG,
                                              @C_AJUSPAG_VRAJUSTEPAG,  @C_AJUSPAG_CDECTBAJPAG,  @C_AJUSPAG_NRLCTBAJPAG,
                                              @C_AJUSPAG_CDCLASSFINA,  @C_AJUSPAG_CDEMMFAJCNTP, @C_AJUSPAG_CDFIMFAJCNTP,
                                              @C_AJUSPAG_DTMOVFLUXO,   @C_AJUSPAG_IDMOVFLUXO;
           END;
           CLOSE C_AJUSPAG;
           DEALLOCATE C_AJUSPAG;
        END
        ELSE BEGIN
           SELECT @V_CDHISTPADR = T.CDHISTPADR, 
                  @V_DSHISTPADR = H.DSHISTPADR
             FROM TIPOCTPAGAR T, HISTPADR H
            WHERE T.CDTPCTPAGAR = @C_PAG_CONTA_CDTPCTPAGAR
              AND T.CDHISTPADR = H.CDHISTPADR;
                                                                                                                
           IF (@V_CDHISTPADR IS NULL) BEGIN
                SELECT @V_CDHISTPADR = HI.CDHISTPADR, 
                       @V_DSHISTPADR = HP.DSHISTPADR
                  FROM HISTINTEGRA HI, HISTPADR HP
                 WHERE (HI.CDHISTPADR = HP.CDHISTPADR)
                   AND (HI.CDOPERACAO = 'CTAPAG');
           END;
                                                                                                                
           EXEC MONTA_HISTORICO @C_PAG_CONTA_CDFILIAL, @C_PAG_CONTA_NRLANCTONF, @V_CDHISTPADR, '', '', '06', '',
                                @V_DSHISTPADR;
                                                                                                                
           EXEC CONTCTBL_FORNECEDOR @C_PAG_CONTA_CDEMPRESA, '', 'E', @C_PAG_CONTA_CDFILIORIG, @C_PAG_CONTA_CDFORNECED,
                                    @V_STCONTCTBL, @V_STCONTCTBLP, @V_STCONTADIFE, @V_CDHISTPADRA;

           EXEC GERA_LOTECONTABIL @C_PAG_CONTA_CDEMPRESA, '06', '', @C_PAG_CONTA_CDFILIORIG, @C_PAG_CONTA_DTENTRSAID,
                                  @C_PAG_CONTA_CDTPCTPAGAR, '', @V_CDLOTE, @V_NRLANCLOTE;
                                                                                                                
           /* CREDITA FORNECEDOR */
           SET @C_PAG_CONTA_NRNOTAFISCAJUST = (@C_PAG_CONTA_NRNOTAFISC + '/' + @C_PAG_CONTA_CDSERIEENTR);
           EXEC NOVA_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE, @V_STCONTCTBL,
                             @V_CDHISTPADR, @V_DSHISTPADR, @C_PAG_CONTA_VRNOTAFISC, 'F', @C_PAG_CONTA_CDFORNECED, '06',
                             @C_PAG_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                
           SELECT @V_CDCENTCUST = ISNULL(F.CDCENTCUST, E.CDCENTCUST)
             FROM FILIAL F, EMPRESA E
            WHERE F.CDFILIAL  = @C_PAG_CONTA_CDFILIORIG
              AND E.CDEMPRESA = @C_PAG_CONTA_CDEMPRESA;
                                                                                                                
           IF (@V_CDCENTCUST IS NOT NULL) BEGIN
              EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                  @C_PAG_CONTA_CDFILIORIG, @V_CDCENTCUST, @C_PAG_CONTA_VRNOTAFISC;
           END;
                                                                                                                
           SELECT @V_STCONTCTBL = CDCONTCTBL
             FROM TIPOCTPAGAR
            WHERE CDTPCTPAGAR = @C_PAG_CONTA_CDTPCTPAGAR;
                                                                                                                
           SELECT @V_CDFILIAL = CDFILIAL
             FROM RATTPCTP
            WHERE (CDTPCTPAGAR = @C_PAG_CONTA_CDTPCTPAGAR);
                                                                                                                
           SET @C_PAG_CONTA_VRNOTAFISCAJUST = (@C_PAG_CONTA_VRNOTAFISC * -1);
           SET @C_PAG_CONTA_NRNOTAFISCAJUST = (@C_PAG_CONTA_NRNOTAFISC + '/' + @C_PAG_CONTA_CDSERIEENTR);
           EXEC NOVA_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE, @V_STCONTCTBL,
                             @V_CDHISTPADR, @V_DSHISTPADR, @C_PAG_CONTA_VRNOTAFISCAJUST, 'F', @C_PAG_CONTA_CDFORNECED,
                             '06', @C_PAG_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                
           SET @V_INPARTIDA    = 1;
           SET @V_DOVRTOTALRAT = 0;
                                                                                                                
           SELECT @V_COUNT = COUNT(*)
             FROM RATTPCTP
            WHERE (CDTPCTPAGAR = @C_PAG_CONTA_CDTPCTPAGAR);
                                                                                                                
           IF (@V_CDFILIAL IS NOT NULL) BEGIN
              DECLARE C_RATTPCTP CURSOR FOR
                 SELECT CDCENTCUST, CDFIRATTPCTP, CDFILIAL, VRPERATTPCTP
                   FROM RATTPCTP
                  WHERE (CDTPCTPAGAR = @C_PAG_CONTA_CDTPCTPAGAR);
                                                                                                                
              OPEN C_RATTPCTP;
              FETCH NEXT FROM C_RATTPCTP INTO @C_RATTPCTP_CDCENTCUST, @C_RATTPCTP_CDFIRATTPCTP, 
                                              @C_RATTPCTP_CDFILIAL,   @C_RATTPCTP_VRPERATTPCTP;               
                                                                                                                
              WHILE (@@FETCH_STATUS = 0) BEGIN
                  SET @V_DOVRTOTALRAT = @V_DOVRTOTALRAT + ROUND(((@C_PAG_CONTA_VRNOTAFISC * @C_RATTPCTP_VRPERATTPCTP) / 100), 2);
                  SET @V_VRRATEIO = (ROUND((@C_PAG_CONTA_VRNOTAFISC * @C_RATTPCTP_VRPERATTPCTP/100), 2) * -1);
                                                                                                                
                  IF (@V_INPARTIDA = @V_COUNT) BEGIN
                     IF (@C_PAG_CONTA_VRNOTAFISC <> @V_DOVRTOTALRAT) BEGIN
                        SET @V_VRRATEIO = @V_VRRATEIO + (@C_PAG_CONTA_VRNOTAFISC - @V_DOVRTOTALRAT);
                     END;
                  END;
                                                                                                                
                  IF (@C_RATTPCTP_CDFILIAL IS NOT NULL) BEGIN
                     EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                         @C_RATTPCTP_CDFILIAL, @C_RATTPCTP_CDCENTCUST, @V_VRRATEIO;
                  END
                  ELSE BEGIN
                     IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                        EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                            @C_PAG_CONTA_CDFILIORIG, @C_RATTPCTP_CDCENTCUST, @V_VRRATEIO;
                     END;
                  END;
                                                                                                                
                  SET @V_INPARTIDA = @V_INPARTIDA + 1;
                  FETCH NEXT FROM C_RATTPCTP INTO @C_RATTPCTP_CDCENTCUST, @C_RATTPCTP_CDFIRATTPCTP, 
                                                  @C_RATTPCTP_CDFILIAL,   @C_RATTPCTP_VRPERATTPCTP; 
              END;
              CLOSE C_RATTPCTP;
              DEALLOCATE C_RATTPCTP;
           END
           ELSE BEGIN
              IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                 SET @C_PAG_CONTA_VRNOTAFISCAJUST = ROUND((@C_PAG_CONTA_VRNOTAFISC * -1), 0);
                 EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE, 
                                     @C_PAG_CONTA_CDFILIORIG, @V_CDCENTCUST, @C_PAG_CONTA_VRNOTAFISCAJUST;
              END;
           END;
                                                                                                                
           DECLARE C_AJUSPAG CURSOR FOR 
              SELECT A.CDFILIAL, A.NRLANCTONF, A.NRSEQVENPAG, A.NRPARCELAPAG,
                     A.DTAJUSTEPAG, A.CDAJUCTPAG, T.IDAJUCTPAG, T.DSAJUCTPAG,
                     A.DSAJUSTEPAG, A.VRAJUSTEPAG, A.CDECTBAJPAG, A.NRLCTBAJPAG,
                     A.CDCLASSFINA, A.CDEMMFAJCNTP, A.CDFIMFAJCNTP, A.DTMOVFLUXO,
                     A.IDMOVFLUXO
                FROM TIPOAJPAGAR T, AJCTPAGAR A, LISTA L
               WHERE (A.CDFILIAL   = SUBSTRING(L.CDLISTA, 01, 04))
                 AND (A.NRLANCTONF = SUBSTRING(L.CDLISTA, 05, 10))
                 AND (A.CDAJUCTPAG = T.CDAJUCTPAG)
                 AND (L.NRLISTA    = @P_NRLISTA)
               ORDER BY A.CDFILIAL, A.NRLANCTONF, A.CDAJUCTPAG;
                                                                                                                
           OPEN C_AJUSPAG;
           FETCH NEXT FROM C_AJUSPAG INTO @C_AJUSPAG_CDFILIAL,     @C_AJUSPAG_NRLANCTONF,   @C_AJUSPAG_NRSEQVENPAG,
                                          @C_AJUSPAG_NRPARCELAPAG, @C_AJUSPAG_DTAJUSTEPAG,  @C_AJUSPAG_CDAJUCTPAG,
                                          @C_AJUSPAG_IDAJUCTPAG,   @C_AJUSPAG_DSAJUCTPAG,   @C_AJUSPAG_DSAJUSTEPAG,
                                          @C_AJUSPAG_VRAJUSTEPAG,  @C_AJUSPAG_CDECTBAJPAG,  @C_AJUSPAG_NRLCTBAJPAG,
                                          @C_AJUSPAG_CDCLASSFINA,  @C_AJUSPAG_CDEMMFAJCNTP, @C_AJUSPAG_CDFIMFAJCNTP,
                                          @C_AJUSPAG_DTMOVFLUXO,   @C_AJUSPAG_IDMOVFLUXO; 
                                                                                                                
           WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                
               DECLARE C_TPAJUSPAG CURSOR FOR
                  SELECT T.CDAJUCTPAG, T.CDHISTPADR, T.CDCONTCTBL, T.DSAJUCTPAG,
                         T.IDAJUCTPAG, H.DSHISTPADR, IDCTBAJCTPAG
                    FROM TIPOAJPAGAR T, HISTPADR H
                   WHERE (T.CDAJUCTPAG = @C_AJUSPAG_CDAJUCTPAG)
                     AND (T.CDHISTPADR = H.CDHISTPADR)
                   ORDER BY T.CDAJUCTPAG;
                                                                                                                
               OPEN C_TPAJUSPAG;
               FETCH NEXT FROM C_TPAJUSPAG INTO @C_TPAJUSPAG_CDAJUCTPAG, @C_TPAJUSPAG_CDHISTPADR,
                                                @C_TPAJUSPAG_CDCONTCTBL, @C_TPAJUSPAG_DSAJUCTPAG;  
                                                                                                                
               WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                
                   EXEC MONTA_HISTORICO @C_AJUSPAG_CDFILIAL, @C_AJUSPAG_NRLANCTONF, @C_TPAJUSPAG_CDHISTPADR, '', '', '06',
                                        @C_AJUSPAG_CDAJUCTPAG, @V_DSHISTPADR;
                                                                                                                
                   FETCH NEXT FROM C_TPAJUSPAG INTO @C_TPAJUSPAG_CDAJUCTPAG, @C_TPAJUSPAG_CDHISTPADR,
                                                    @C_TPAJUSPAG_CDCONTCTBL, @C_TPAJUSPAG_DSAJUCTPAG;  
               END;
               CLOSE C_TPAJUSPAG;
               DEALLOCATE C_TPAJUSPAG;
                                                                                                                
               EXEC CONTCTBL_FORNECEDOR @C_PAG_CONTA_CDEMPRESA, '', 'E', @C_PAG_CONTA_CDFILIORIG, @C_PAG_CONTA_CDFORNECED,
                                        @V_STCONTCTBL, @V_STCONTCTBLP, @V_STCONTADIFE, @V_CDHISTPADRA;
                                                                                                                
               IF (@C_AJUSPAG_IDAJUCTPAG = 'D') BEGIN
                  SET @V_DBFATOR = -1;
               END
               ELSE BEGIN
                  SET @V_DBFATOR = 1;
               END;
                                                                                                                
               /* DEBITA FORNECEDOR*/
               SET @C_AJUSPAG_VRAJUSTEPAGAJUST  = (@C_AJUSPAG_VRAJUSTEPAG * -1 * @V_DBFATOR);
               SET @C_PAG_CONTA_NRNOTAFISCAJUST = (@C_PAG_CONTA_NRNOTAFISC + '/' + @C_PAG_CONTA_CDSERIEENTR);
               EXEC NOVA_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE, @V_STCONTCTBL,
                                 @V_CDHISTPADR, @V_DSHISTPADR, @C_AJUSPAG_VRAJUSTEPAGAJUST, 'F', @C_PAG_CONTA_CDFORNECED,
                                 '06', @C_PAG_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                
               IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                  SET @C_AJUSPAG_VRAJUSTEPAGAJUST = (@C_AJUSPAG_VRAJUSTEPAG * -1 * @V_DBFATOR);
                  EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                      @C_PAG_CONTA_CDFILIORIG, @V_CDCENTCUST, @C_AJUSPAG_VRAJUSTEPAGAJUST;
               END;
                                                                                                                
               SELECT @V_STCONTCTBL = CDCONTCTBL
                 FROM TIPOAJPAGAR
                WHERE CDAJUCTPAG = @C_AJUSPAG_CDAJUCTPAG;
                                                                                                                
               /* CREDITA TIPO DE AJUSTE A PAGAR */
               SET @C_AJUSPAG_VRAJUSTEPAGAJUST  = (@C_AJUSPAG_VRAJUSTEPAG * @V_DBFATOR);
               SET @C_PAG_CONTA_NRNOTAFISCAJUST = (@C_PAG_CONTA_NRNOTAFISC + '/' + @C_PAG_CONTA_CDSERIEENTR);
               EXEC NOVA_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE, @V_STCONTCTBL,
                                 @V_CDHISTPADR, @V_DSHISTPADR, @C_AJUSPAG_VRAJUSTEPAGAJUST, 'F', @C_PAG_CONTA_CDFORNECED,
                                 '06', @C_PAG_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                
               SET @V_INPARTIDA = 1;
                                                                                                                
               SELECT @V_CDFILIAL = CDFILIAL
                 FROM RATTPAJP
                WHERE (CDAJUCTPAG = @C_AJUSPAG_CDAJUCTPAG);
                                                                                                                
               SELECT @V_COUNT = COUNT(*)
                 FROM RATTPAJP
                WHERE (CDAJUCTPAG = @C_AJUSPAG_CDAJUCTPAG);
                                                                                                                
               SET @V_DOVRTOTALRAT = 0;
                                                                                                                
               IF (@V_CDFILIAL IS NOT NULL) BEGIN
                  DECLARE C_RATTPAJP CURSOR FOR
                     SELECT CDCENTCUST, CDFIRATTPAJP, CDFILIAL, VRPERATTPAJP
                       FROM RATTPAJP
                      WHERE (CDAJUCTPAG = @C_AJUSPAG_CDAJUCTPAG);
                                                                                                                
                  OPEN C_RATTPAJP;
                  FETCH NEXT FROM C_RATTPAJP INTO @C_RATTPAJP_CDCENTCUST, @C_RATTPAJP_CDFIRATTPAJP,
                                                  @C_RATTPAJP_CDFILIAL, @C_RATTPAJP_VRPERATTPAJP;
                                                                                                                
                  WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                
                     SET @V_DOVRTOTALRAT = 0;
                     SET @V_DOVRTOTALRAT = @V_DOVRTOTALRAT + ROUND(((@C_AJUSPAG_VRAJUSTEPAG * @C_RATTPAJP_VRPERATTPAJP) / 100), 2);
                     SET @V_VRRATEIO = (ROUND((@C_AJUSPAG_VRAJUSTEPAG * @C_RATTPAJP_VRPERATTPAJP) / 100, 2) * @V_DBFATOR);
                                                                                                                
                     IF (@C_RATTPAJP_CDFILIAL IS NOT NULL) BEGIN
                        EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                            @C_RATTPAJP_CDFILIAL, @C_RATTPAJP_CDCENTCUST, @V_VRRATEIO;
                     END
                     ELSE BEGIN
                        IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                           EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                               @C_PAG_CONTA_CDFILIORIG, @C_RATTPAJP_CDCENTCUST, @V_VRRATEIO;
                        END;
                     END;
                                                                                                                
                     SET @V_INPARTIDA = @V_INPARTIDA + 1;
                                                                                                                
                     FETCH NEXT FROM C_RATTPAJP INTO @C_RATTPAJP_CDCENTCUST, @C_RATTPAJP_CDFIRATTPAJP,
                                                     @C_RATTPAJP_CDFILIAL, @C_RATTPAJP_VRPERATTPAJP;
                  END;
                  CLOSE C_RATTPAJP;
                  DEALLOCATE C_RATTPAJP;
               END
               ELSE BEGIN
                 IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                    SET @C_AJUSPAG_VRAJUSTEPAGAJUST = ROUND((@C_AJUSPAG_VRAJUSTEPAG * @V_DBFATOR), 0);
                    EXEC RATEIO_PARTIDA @C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @C_PAG_CONTA_DTENTRSAID, @V_NRLANCLOTE, 
                                        @C_PAG_CONTA_CDFILIORIG, @V_CDCENTCUST, @C_AJUSPAG_VRAJUSTEPAGAJUST;
                 END;
               END;
               FETCH NEXT FROM C_AJUSPAG INTO @C_AJUSPAG_CDFILIAL,     @C_AJUSPAG_NRLANCTONF,   @C_AJUSPAG_NRSEQVENPAG,
                                              @C_AJUSPAG_NRPARCELAPAG, @C_AJUSPAG_DTAJUSTEPAG,  @C_AJUSPAG_CDAJUCTPAG,
                                              @C_AJUSPAG_IDAJUCTPAG,   @C_AJUSPAG_DSAJUCTPAG,   @C_AJUSPAG_DSAJUSTEPAG,
                                              @C_AJUSPAG_VRAJUSTEPAG,  @C_AJUSPAG_CDECTBAJPAG,  @C_AJUSPAG_NRLCTBAJPAG,
                                              @C_AJUSPAG_CDCLASSFINA,  @C_AJUSPAG_CDEMMFAJCNTP, @C_AJUSPAG_CDFIMFAJCNTP,
                                              @C_AJUSPAG_DTMOVFLUXO,   @C_AJUSPAG_IDMOVFLUXO;
           END;
           CLOSE C_AJUSPAG;
           DEALLOCATE C_AJUSPAG;
        END;
                                                                                                                
        INSERT INTO RELACNFLANCLOTE
           (CDEMPRESA, CDLOTE, NRLANCLOTE, NRSEQLANLOTE,CDFILIAL, NRLANCTONF)
        VALUES
           (@C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @V_NRLANCLOTE, '00001', @C_PAG_CONTA_CDFILIAL, @C_PAG_CONTA_NRLANCTONF);
                                                                                                                
        IF (TEKNISA.CONFERE_PARTIDA(@C_PAG_CONTA_CDEMPRESA, @V_CDLOTE, @V_NRLANCLOTE,@C_PAG_CONTA_DTENTRSAID) = 'V') BEGIN
           UPDATE NOTAFISCAL 
              SET IDSTATUSCTBNF = 'C'
            WHERE CDFILIAL   = @C_PAG_CONTA_CDFILIAL
              AND NRLANCTONF = @C_PAG_CONTA_NRLANCTONF;
        END
        ELSE BEGIN
           UPDATE NOTAFISCAL 
              SET IDSTATUSCTBNF = 'E'
            WHERE CDFILIAL   = @C_PAG_CONTA_CDFILIAL
              AND NRLANCTONF = @C_PAG_CONTA_NRLANCTONF;
        END;
        FETCH NEXT FROM C_PAG_CONTA INTO @C_PAG_CONTA_CDTPCTPAGAR, @C_PAG_CONTA_CDSERIEENTR,  @C_PAG_CONTA_VRNOTAFISC,
                                         @C_PAG_CONTA_CDFILIORIG,  @C_PAG_CONTA_CDTIPOOPER,   @C_PAG_CONTA_NRINSJURFORN,
                                         @C_PAG_CONTA_CDFORNECED,  @C_PAG_CONTA_NRNOTAFISC,   @C_PAG_CONTA_DTEMISSAO,
                                         @C_PAG_CONTA_DTENTRSAID,  @C_PAG_CONTA_IDSTATUS,     @C_PAG_CONTA_NRLANCTBDOC,
                                         @C_PAG_CONTA_CDFILIAL,    @C_PAG_CONTA_CDEMPRESA,    @C_PAG_CONTA_NRLANCTONF,
                                         @C_PAG_CONTA_CDPADRCONTABIL;
   END;
   CLOSE C_PAG_CONTA;
   DEALLOCATE C_PAG_CONTA;
                                                                                                                
   DELETE FROM LISTA
    WHERE NRLISTA = @P_NRLISTA;
END;
GO
