IF EXISTS (SELECT * FROM SYSOBJECTS O, SYSUSERS U
            WHERE O.NAME = 'CONTABILIZA_REC_CONTA' AND
                  TYPE = 'P' 	                     AND
	               U.UID = O.UID                    AND
	               U.NAME = USER)
   DROP PROCEDURE CONTABILIZA_REC_CONTA
GO
                                                                                                                  
CREATE PROCEDURE CONTABILIZA_REC_CONTA(
   @P_NRLISTA        VARCHAR(10))
AS
   DECLARE @V_NRLANCLOTE      VARCHAR(06);
   DECLARE @V_CDLOTE          VARCHAR(20);
   DECLARE @V_GERLOTE         VARCHAR(01);
   DECLARE @V_IDRETORNA       VARCHAR(01);
   DECLARE @V_IDCONTCCUS      VARCHAR(01);
   DECLARE @V_DSHISTPADR      VARCHAR(1000);
   DECLARE @V_STCONTCTBL      VARCHAR(20);
   DECLARE @V_STCONTCTBLP     VARCHAR(20);
   DECLARE @V_STCONTADIFE     VARCHAR(20);
   DECLARE @V_CDHISTPADR      VARCHAR(04);
   DECLARE @V_CDHISTPADRA     VARCHAR(04);
   DECLARE @V_DOVRTOTALRAT    DOUBLE PRECISION;
   DECLARE @V_CDCENTCUST      VARCHAR(04);
   DECLARE @V_CDFILIAL        VARCHAR(04);
   DECLARE @V_COUNT           NUMERIC;
   DECLARE @V_INPARTIDA       INTEGER;
   DECLARE @V_VRRATEIO        DOUBLE PRECISION;
   DECLARE @V_VRRATEIOAJUST   DOUBLE PRECISION;
   DECLARE @V_DBFATOR         DOUBLE PRECISION;
   DECLARE @V_VALOR           DOUBLE PRECISION;
   DECLARE @V_VALORAJUST      DOUBLE PRECISION;
                                                                                                                  
   /* VARIAVEIS DO CURSOR C_REC_CONTA */
   DECLARE @C_REC_CONTA_CDTPCTRECEB       VARCHAR(06);
   DECLARE @C_REC_CONTA_CDSERIE           VARCHAR(05);
   DECLARE @C_REC_CONTA_CDINSCESTA        VARCHAR(16);
   DECLARE @C_REC_CONTA_CDINSCMUNI        VARCHAR(16);
   DECLARE @C_REC_CONTA_VRNOTAFISC        DOUBLE PRECISION;
   DECLARE @C_REC_CONTA_VRNOTAFISCAJUST   DOUBLE PRECISION;
   DECLARE @C_REC_CONTA_CDFILIEMIT        VARCHAR(04);
   DECLARE @C_REC_CONTA_CDTIPOOPER        VARCHAR(02);
   DECLARE @C_REC_CONTA_IDTPINSJUR        VARCHAR(01);
   DECLARE @C_REC_CONTA_NRINSJURCLIE      VARCHAR(20);
   DECLARE @C_REC_CONTA_CDCLIENTE         VARCHAR(21);
   DECLARE @C_REC_CONTA_NRNOTAFISC        VARCHAR(20);
   DECLARE @C_REC_CONTA_NRNOTAFISCAJUST   VARCHAR(26);
   DECLARE @C_REC_CONTA_DTEMISSAO         DATETIME;
   DECLARE @C_REC_CONTA_DTENTRSAID        DATETIME;
   DECLARE @C_REC_CONTA_IDSTATUS          VARCHAR(01);
   DECLARE @C_REC_CONTA_NRLANCTBDOC       VARCHAR(12);
   DECLARE @C_REC_CONTA_NRLICENCAUSO      VARCHAR(10);
   DECLARE @C_REC_CONTA_CDVENDEDOR        VARCHAR(04);
   DECLARE @C_REC_CONTA_CDFILIAL          VARCHAR(04);
   DECLARE @C_REC_CONTA_NRLANCTONF        VARCHAR(10);
   DECLARE @C_REC_CONTA_CDEMPRESA         VARCHAR(02);
   DECLARE @C_REC_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_RATTPCTR */
   DECLARE @C_RATTPCTR_CDCENTCUST        VARCHAR(04);
   DECLARE @C_RATTPCTR_CDFIRATTPCTR      VARCHAR(04);
   DECLARE @C_RATTPCTR_CDFILIAL          VARCHAR(04);
   DECLARE @C_RATTPCTR_VRPERATTPCTR      DOUBLE PRECISION;
                                                                                                                  
   /* VARIAVEIS DO CURSOR C_AJUSREC */
   DECLARE @C_AJUSREC_CDFILIAL          VARCHAR(04);
   DECLARE @C_AJUSREC_NRLANCTONF        VARCHAR(10);
   DECLARE @C_AJUSREC_NRSEQVENREC       VARCHAR(02);
   DECLARE @C_AJUSREC_NRPARCELAREC      VARCHAR(02);
   DECLARE @C_AJUSREC_DTAJUSTEREC       DATETIME;
   DECLARE @C_AJUSREC_CDAJUCTREC        VARCHAR(02);
   DECLARE @C_AJUSREC_IDAJUCTREC        VARCHAR(01);
   DECLARE @C_AJUSREC_DSAJUCTREC        VARCHAR(40);
   DECLARE @C_AJUSREC_DSAJUSTEREC       VARCHAR(20);
   DECLARE @C_AJUSREC_VRAJUSTEREC       DOUBLE PRECISION;
   DECLARE @C_AJUSREC_VRAJUSTERECAJUST  DOUBLE PRECISION;
   DECLARE @C_AJUSREC_CDECTBAJREC       VARCHAR(02);
   DECLARE @C_AJUSREC_NRLCTBAJREC       VARCHAR(12);
   DECLARE @C_AJUSREC_CDCLASSFINA       VARCHAR(06);
   DECLARE @C_AJUSREC_CDEMMFAJCNTR      VARCHAR(02);
   DECLARE @C_AJUSREC_CDFIMFAJCNTR      VARCHAR(04);
   DECLARE @C_AJUSREC_DTMOVFLUXO        DATETIME;
   DECLARE @C_AJUSREC_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_TPAJUSREC */
   DECLARE @C_TPAJUSREC_CDAJUCTREC       VARCHAR(02);
   DECLARE @C_TPAJUSREC_CDHISTPADR       VARCHAR(04);
   DECLARE @C_TPAJUSREC_CDCONTCTBL       VARCHAR(20);
   DECLARE @C_TPAJUSREC_DSAJUCTREC       VARCHAR(40);
   DECLARE @C_TPAJUSREC_IDAJUCTREC       VARCHAR(01);
   DECLARE @C_TPAJUSREC_DSHISTPADR       VARCHAR(60);
                                                                                                                  
   /* VARIAVEIS DO CURSOR C_RATTPAJR */
   DECLARE @C_RATTPAJR_CDCENTCUST       VARCHAR(04);
   DECLARE @C_RATTPAJR_CDFIRATTPAJR     VARCHAR(04);
   DECLARE @C_RATTPAJR_CDFILIAL         VARCHAR(04);
   DECLARE @C_RATTPAJR_VRPERATTPAJR     DOUBLE PRECISION;
BEGIN
   EXEC EXCLUI_LANCCONTABIL '07', @P_NRLISTA;
                                                                                                                  
   DECLARE C_REC_CONTA CURSOR FOR
      SELECT DISTINCT CP.CDTPCTRECEB, NF.CDSERIE, NF.CDINSCESTA, NF.CDINSCMUNI, NF.VRNOTAFISC,
             NF.CDFILIEMIT, NF.CDTIPOOPER, NF.IDTPIJURNF AS IDTPINSJUR, NF.NRINSJURNF AS NRINSJURCLIE,
             NF.CDCLIENTE, NF.NRNOTAFISC, NF.DTEMISSAO, NF.DTENTRSAID,  NF.IDSTATUS,
             NF.NRLANCTBDOC, NF.NRLICENCAUSO, NF.CDVENDEDOR, NF.CDFILIAL, NF.NRLANCTONF, NF.CDEMPRESA,
             TR.CDPADRCONTABIL
        FROM CONTARECEBER CP, NOTAFISCAL NF, LISTA L, TIPOCTRECEBER TR
       WHERE (NF.CDFILIAL      = SUBSTRING(L.CDLISTA,1,4))
         AND (NF.NRLANCTONF    = SUBSTRING(L.CDLISTA,5,10))
         AND (CP.CDFILIAL      = NF.CDFILIAL)
         AND (CP.NRLANCTONF    = NF.NRLANCTONF)
         AND (L.NRLISTA        = @P_NRLISTA)
         AND (CP.CDTPCTRECEB   = TR.CDTPCTRECEB);
                                                                                                                  
   OPEN C_REC_CONTA;
   FETCH NEXT FROM C_REC_CONTA INTO @C_REC_CONTA_CDTPCTRECEB,  @C_REC_CONTA_CDSERIE,    @C_REC_CONTA_CDINSCESTA,
                                    @C_REC_CONTA_CDINSCMUNI,   @C_REC_CONTA_VRNOTAFISC, @C_REC_CONTA_CDFILIEMIT,
                                    @C_REC_CONTA_CDTIPOOPER,   @C_REC_CONTA_IDTPINSJUR, @C_REC_CONTA_NRINSJURCLIE,
                                    @C_REC_CONTA_CDCLIENTE,    @C_REC_CONTA_NRNOTAFISC, @C_REC_CONTA_DTEMISSAO,
                                    @C_REC_CONTA_DTENTRSAID,   @C_REC_CONTA_IDSTATUS,   @C_REC_CONTA_NRLANCTBDOC,
                                    @C_REC_CONTA_NRLICENCAUSO, @C_REC_CONTA_CDVENDEDOR, @C_REC_CONTA_CDFILIAL,
                                    @C_REC_CONTA_NRLANCTONF,   @C_REC_CONTA_CDEMPRESA,  @C_REC_CONTA_CDPADRCONTABIL;
                                                                                                                  
   WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                  
         IF (@C_REC_CONTA_CDPADRCONTABIL IS NOT NULL) BEGIN
                                                                                                                  
            EXEC GERA_LOTECONTABIL @C_REC_CONTA_CDEMPRESA, '07', '', @C_REC_CONTA_CDFILIEMIT, @C_REC_CONTA_DTENTRSAID,
                                   @C_REC_CONTA_CDTPCTRECEB, '', @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_REC_CONTA_CDPADRCONTABIL
                   AND ((IDTPCONTAPART = '01' AND IDTPVALORPART = '00') OR (IDTPCONTAPART = '15' AND IDTPVALORPART = '00'))
                   AND (IDTPMOVLOTEPACO = '07')
                 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 TIPOCTRECEBER T, HISTPADR H
                 WHERE T.CDTPCTRECEB = @C_REC_CONTA_CDTPCTRECEB
                   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 = 'CTAREC');
                END;
                                                                                                                  
                EXEC MONTA_HISTORICO @C_REC_CONTA_CDFILIAL, @C_REC_CONTA_NRLANCTONF, @V_CDHISTPADR, '', '', '07', '',
                                     @V_DSHISTPADR;
                                                                                                                  
                IF (@C_PARTIDA_IDTPCONTAPART = '01') BEGIN
                   EXEC CONTCTBL_CLIENTE @C_REC_CONTA_CDEMPRESA, '', 'E', @C_REC_CONTA_CDFILIEMIT, @C_REC_CONTA_CDCLIENTE,
                                         @V_STCONTCTBL, @V_STCONTCTBLP, @V_STCONTADIFE, @V_CDHISTPADRA;
                END
                ELSE IF (@C_PARTIDA_IDTPCONTAPART = '15') BEGIN
                    SELECT @V_STCONTCTBL = CDCONTCTBL
                      FROM TIPOCTRECEBER
                     WHERE CDTPCTRECEB = @C_REC_CONTA_CDTPCTRECEB;
                END;
                                                                                                                  
                IF (@C_PARTIDA_IDTPVALORPART = '00') BEGIN
                   SET @V_VALOR = @C_REC_CONTA_VRNOTAFISC;
                END;
                                                                                                                  
                SELECT @V_CDCENTCUST = ISNULL(F.CDCENTCUST, E.CDCENTCUST)
                  FROM FILIAL F, EMPRESA E
                 WHERE F.CDFILIAL  = @C_REC_CONTA_CDFILIEMIT
                   AND E.CDEMPRESA = @C_REC_CONTA_CDEMPRESA;
                                                                                                                  
                SELECT @V_CDFILIAL = CDFILIAL
                  FROM RATTPCTR
                 WHERE (CDTPCTRECEB = @C_REC_CONTA_CDTPCTRECEB);
                                                                                                                   
                 IF (@C_PARTIDA_IDCREDDEBCONTAB = 'D') BEGIN
                    SET @V_VALORAJUST = -@V_VALOR;
                    SET @C_REC_CONTA_NRNOTAFISCAJUST = @C_REC_CONTA_NRNOTAFISC + '/' + @C_REC_CONTA_CDSERIE;
                    EXEC NOVA_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                      @V_STCONTCTBL, @V_CDHISTPADR, @V_DSHISTPADR, @V_VALORAJUST, 'C', 
                                      @C_REC_CONTA_CDCLIENTE, '07', @C_REC_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                   
                    SELECT @V_CDCENTCUST = ISNULL(F.CDCENTCUST, E.CDCENTCUST)
                      FROM FILIAL F, EMPRESA E
                     WHERE F.CDFILIAL  = @C_REC_CONTA_CDFILIEMIT
                       AND E.CDEMPRESA = @C_REC_CONTA_CDEMPRESA;
                                                                                                                   
                    IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                       SET @C_REC_CONTA_VRNOTAFISCAJUST = (@C_REC_CONTA_VRNOTAFISC * -1);
                       EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                           @C_REC_CONTA_CDFILIEMIT, @V_CDCENTCUST, @C_REC_CONTA_VRNOTAFISCAJUST;
                    END;
                 END
                 ELSE BEGIN
                    SELECT @V_CDFILIAL = CDFILIAL
                      FROM RATTPCTR
                     WHERE (CDTPCTRECEB = @C_REC_CONTA_CDTPCTRECEB);
                                                                                                                   
                    SET @C_REC_CONTA_NRNOTAFISCAJUST = (@C_REC_CONTA_NRNOTAFISC + '/' + @C_REC_CONTA_CDSERIE);
                    EXEC NOVA_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                      @V_STCONTCTBL, @V_CDHISTPADR, @V_DSHISTPADR, @V_VALOR, 'C', @C_REC_CONTA_CDCLIENTE,
                                      '07', @C_REC_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                   
                    SET @V_INPARTIDA    = 1;
                    SET @V_DOVRTOTALRAT = 0;
                                                                                                                   
                    SELECT @V_COUNT = COUNT(*)
                      FROM RATTPCTR
                     WHERE (CDTPCTRECEB = @C_REC_CONTA_CDTPCTRECEB);
                                                                                                                   
                    IF (@V_CDFILIAL IS NOT NULL) BEGIN
                                                                                                                   
                       DECLARE C_RATTPCTR CURSOR FOR
                          SELECT CDCENTCUST, CDFIRATTPCTR, CDFILIAL, VRPERATTPCTR
                            FROM RATTPCTR
                           WHERE (CDTPCTRECEB = @C_REC_CONTA_CDTPCTRECEB);
                                                                                                                   
                       OPEN C_RATTPCTR;
                       FETCH NEXT FROM C_RATTPCTR INTO @C_RATTPCTR_CDCENTCUST, @C_RATTPCTR_CDFIRATTPCTR, 
                                                       @C_RATTPCTR_CDFILIAL,   @C_RATTPCTR_VRPERATTPCTR;
                                                                                                                   
                       WHILE (@@FETCH_STATUS = 0) BEGIN
                           SET @V_DOVRTOTALRAT = @V_DOVRTOTALRAT + ROUND(((@C_REC_CONTA_VRNOTAFISC * @C_RATTPCTR_VRPERATTPCTR) / 100), 2);
                           SET @V_VRRATEIO = ROUND((@C_REC_CONTA_VRNOTAFISC * @C_RATTPCTR_VRPERATTPCTR / 100), 2);
                                                                                                                   
                           IF (@V_INPARTIDA = @V_COUNT) BEGIN
                              IF (@C_REC_CONTA_VRNOTAFISC <> @V_DOVRTOTALRAT) BEGIN
                                 SET @V_VRRATEIO = @V_VRRATEIO + (@C_REC_CONTA_VRNOTAFISC - @V_DOVRTOTALRAT);
                              END;
                           END;
                                                                                                                   
                           IF (@C_RATTPCTR_CDFILIAL IS NOT NULL) BEGIN
                              EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                                  @C_RATTPCTR_CDFILIAL, @C_RATTPCTR_CDCENTCUST, @V_VRRATEIO; 
                           END
                           ELSE BEGIN
                              EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                                  @C_REC_CONTA_CDFILIEMIT, @C_RATTPCTR_CDCENTCUST, @V_VRRATEIO;
                           END;
                                                                                                                   
                           SET @V_INPARTIDA = @V_INPARTIDA + 1;

                           FETCH NEXT FROM C_RATTPCTR INTO @C_RATTPCTR_CDCENTCUST, @C_RATTPCTR_CDFIRATTPCTR, 
                                                           @C_RATTPCTR_CDFILIAL,   @C_RATTPCTR_VRPERATTPCTR;
                       END;
                       CLOSE C_RATTPCTR;
                       DEALLOCATE C_RATTPCTR;
                    END
                    ELSE BEGIN
                       SET @C_REC_CONTA_VRNOTAFISCAJUST = ROUND(@C_REC_CONTA_VRNOTAFISC, 0);
                       EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                           @C_REC_CONTA_CDFILIEMIT, @V_CDCENTCUST, @C_REC_CONTA_VRNOTAFISCAJUST;
                    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_AJUSREC CURSOR FOR
               SELECT A.CDFILIAL, A.NRLANCTONF, A.NRSEQVENREC,       
                      A.NRPARCELAREC, A.DTAJUSTEREC, A.CDAJUCTREC,
                      T.IDAJUCTREC, T.DSAJUCTREC, A.DSAJUSTEREC,
                      A.VRAJUSTEREC, A.CDECTBAJREC, A.NRLCTBAJREC,
                      A.CDCLASSFINA, A.CDEMMFAJCNTR, A.CDFIMFAJCNTR,
                      A.DTMOVFLUXO, A.IDMOVFLUXO
                 FROM AJCTRECEBER A, TIPOAJRECEBER T, LISTA L
                WHERE (A.CDFILIAL     = SUBSTRING(L.CDLISTA, 01, 04))
                  AND (A.NRLANCTONF   = SUBSTRING(L.CDLISTA, 05, 10))
                  AND (A.CDAJUCTREC   = T.CDAJUCTREC)
                  AND (L.NRLISTA      = @P_NRLISTA)
                ORDER BY A.CDFILIAL, A.NRLANCTONF, A.CDAJUCTREC;
                                                                                                                   
            OPEN C_AJUSREC;
            FETCH NEXT FROM C_AJUSREC INTO @C_AJUSREC_CDFILIAL,     @C_AJUSREC_NRLANCTONF,   @C_AJUSREC_NRSEQVENREC,
                                           @C_AJUSREC_NRPARCELAREC, @C_AJUSREC_DTAJUSTEREC,  @C_AJUSREC_CDAJUCTREC,
                                           @C_AJUSREC_IDAJUCTREC,   @C_AJUSREC_DSAJUCTREC,   @C_AJUSREC_DSAJUSTEREC,
                                           @C_AJUSREC_VRAJUSTEREC,  @C_AJUSREC_CDECTBAJREC,  @C_AJUSREC_NRLCTBAJREC,
                                           @C_AJUSREC_CDCLASSFINA,  @C_AJUSREC_CDEMMFAJCNTR, @C_AJUSREC_CDFIMFAJCNTR,
                                           @C_AJUSREC_DTMOVFLUXO,   @C_AJUSREC_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_REC_CONTA_CDPADRCONTABIL
                     AND (IDTPVALORPART   = '07')
                     AND (IDTPMOVLOTEPACO = '07')
                   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_TPAJUSREC CURSOR FOR
                     SELECT T.CDAJUCTREC, T.CDHISTPADR, T.CDCONTCTBL, T.DSAJUCTREC,
                            T.IDAJUCTREC, H.DSHISTPADR
                       FROM TIPOAJRECEBER T, HISTPADR H
                      WHERE (T.CDAJUCTREC = @C_AJUSREC_CDAJUCTREC)
                        AND (T.CDHISTPADR = H.CDHISTPADR)
                      ORDER BY T.CDAJUCTREC;
                                                                                                                   
                  OPEN C_TPAJUSREC;
                  FETCH NEXT FROM C_TPAJUSREC INTO @C_TPAJUSREC_CDAJUCTREC, @C_TPAJUSREC_CDHISTPADR, @C_TPAJUSREC_CDCONTCTBL,
                                                   @C_TPAJUSREC_DSAJUCTREC, @C_TPAJUSREC_IDAJUCTREC, @C_TPAJUSREC_DSHISTPADR;
                                                                                                                   
                  WHILE (@@FETCH_STATUS = 0) BEGIN
                     EXEC MONTA_HISTORICO @C_AJUSREC_CDFILIAL, @C_AJUSREC_NRLANCTONF, @C_TPAJUSREC_CDHISTPADR, '',
                                          @C_AJUSREC_DTAJUSTEREC, '07', @C_AJUSREC_CDAJUCTREC, @V_DSHISTPADR;
                                                                                                                   
                     FETCH NEXT FROM C_TPAJUSREC INTO @C_TPAJUSREC_CDAJUCTREC, @C_TPAJUSREC_CDHISTPADR, @C_TPAJUSREC_CDCONTCTBL,
                                                      @C_TPAJUSREC_DSAJUCTREC, @C_TPAJUSREC_IDAJUCTREC, @C_TPAJUSREC_DSHISTPADR;
                  END;
                                                                                                                   
                 IF (@C_PARTIDAAJUS_IDTPCONTAPART = '01') BEGIN
                    EXEC CONTCTBL_CLIENTE @C_REC_CONTA_CDEMPRESA, '', 'E', @C_REC_CONTA_CDFILIEMIT,@C_REC_CONTA_CDCLIENTE,
                                          @V_STCONTCTBL, @V_STCONTCTBLP, @V_STCONTADIFE, @V_CDHISTPADRA;
                 END
                 ELSE IF (@C_PARTIDAAJUS_IDTPCONTAPART = '20') BEGIN
                    SELECT @V_STCONTCTBL = CDCONTCTBL
                      FROM TIPOAJRECEBER
                     WHERE CDAJUCTREC = @C_AJUSREC_CDAJUCTREC;
                 END;
                                                                                                                   
                 IF (@C_PARTIDAAJUS_IDTPVALORPART = '07') BEGIN
                    SET @V_VALOR = @C_AJUSREC_VRAJUSTEREC;
                 END;
                                                                                                                   
                 IF (@C_PARTIDAAJUS_IDCREDDEBCONTAB = 'C') BEGIN
                    SET @C_REC_CONTA_NRNOTAFISCAJUST = (@C_REC_CONTA_NRNOTAFISC + '/' + @C_REC_CONTA_CDSERIE);
                    EXEC NOVA_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                      @V_STCONTCTBL, @V_CDHISTPADR, @V_DSHISTPADR, @V_VALOR, 'C', @C_REC_CONTA_CDCLIENTE,
                                      '07', @C_REC_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                   
                    IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                       EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                           @C_REC_CONTA_CDFILIEMIT, @V_CDCENTCUST, @V_VALOR;
                    END;
                 END
                 ELSE BEGIN
                    SET @V_VALORAJUST = -@V_VALOR;
                    SET @C_REC_CONTA_NRNOTAFISCAJUST = @C_REC_CONTA_NRNOTAFISC + '/' + @C_REC_CONTA_CDSERIE;
                    EXEC NOVA_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE, 
                                      @V_STCONTCTBL, @V_CDHISTPADR, @V_DSHISTPADR, @V_VALORAJUST, 'C', 
                                      @C_REC_CONTA_CDCLIENTE, '07', @C_REC_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                   
                    SET @V_INPARTIDA = 1;
                                                                                                                   
                    SELECT @V_CDFILIAL = CDFILIAL
                      FROM RATTPAJR
                     WHERE (CDAJUCTREC = @C_AJUSREC_CDAJUCTREC);
                                                                                                                   
                    SELECT @V_COUNT = COUNT(*)
                      FROM RATTPAJR
                     WHERE (CDAJUCTREC = @C_AJUSREC_CDAJUCTREC);
                                                                                                                   
                    SET @V_DOVRTOTALRAT = 0;
                                                                                                                   
                    IF (@V_CDFILIAL IS NOT NULL) BEGIN
                       DECLARE C_RATTPAJR CURSOR FOR
                          SELECT CDCENTCUST, CDFIRATTPAJR, CDFILIAL, VRPERATTPAJR
                            FROM RATTPAJR
                           WHERE (CDAJUCTREC = @C_AJUSREC_CDAJUCTREC);
                                                                                                                   
                       OPEN C_RATTPAJR;
                       FETCH NEXT FROM C_RATTPAJR INTO @C_RATTPAJR_CDCENTCUST, @C_RATTPAJR_CDFIRATTPAJR,
                                                       @C_RATTPAJR_CDFILIAL,   @C_RATTPAJR_VRPERATTPAJR;
                                                                                                                   
                       WHILE (@@FETCH_STATUS = 0) BEGIN
                          SET @V_DOVRTOTALRAT = 0;
                          SET @V_DOVRTOTALRAT = @V_DOVRTOTALRAT + ROUND(((@C_AJUSREC_VRAJUSTEREC * @C_RATTPAJR_VRPERATTPAJR) / 100),2);
                          SET @V_VRRATEIO = ROUND(((@C_AJUSREC_VRAJUSTEREC * @C_RATTPAJR_VRPERATTPAJR) / 100), 2);
                                                                                                                   
                          IF (@C_RATTPAJR_CDFILIAL IS NOT NULL) BEGIN
                             EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                                 @C_RATTPAJR_CDFILIAL, @C_RATTPAJR_CDCENTCUST, @V_VRRATEIO;
                          END
                          ELSE BEGIN
                             EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                                 @C_REC_CONTA_CDFILIEMIT, @C_RATTPAJR_CDCENTCUST, @V_VRRATEIO;
                          END;
                                                                                                                   
                          SET @V_INPARTIDA = @V_INPARTIDA + 1;
                                                                                                                   
                          FETCH NEXT FROM C_RATTPAJR INTO @C_RATTPAJR_CDCENTCUST, @C_RATTPAJR_CDFIRATTPAJR,
                                                          @C_RATTPAJR_CDFILIAL,   @C_RATTPAJR_VRPERATTPAJR;
                       END;
                       CLOSE C_RATTPAJR;
                       DEALLOCATE C_RATTPAJR;
                    END 
                    ELSE BEGIN
                      SET @C_AJUSREC_VRAJUSTERECAJUST = ROUND(@C_AJUSREC_VRAJUSTEREC, 2);
                      EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                          @C_REC_CONTA_CDFILIEMIT, @V_CDCENTCUST, @C_AJUSREC_VRAJUSTERECAJUST;
                    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_AJUSREC INTO @C_AJUSREC_CDFILIAL,     @C_AJUSREC_NRLANCTONF,   @C_AJUSREC_NRSEQVENREC,
                                              @C_AJUSREC_NRPARCELAREC, @C_AJUSREC_DTAJUSTEREC,  @C_AJUSREC_CDAJUCTREC,
                                              @C_AJUSREC_IDAJUCTREC,   @C_AJUSREC_DSAJUCTREC,   @C_AJUSREC_DSAJUSTEREC,
                                              @C_AJUSREC_VRAJUSTEREC,  @C_AJUSREC_CDECTBAJREC,  @C_AJUSREC_NRLCTBAJREC,
                                              @C_AJUSREC_CDCLASSFINA,  @C_AJUSREC_CDEMMFAJCNTR, @C_AJUSREC_CDFIMFAJCNTR,
                                              @C_AJUSREC_DTMOVFLUXO,   @C_AJUSREC_IDMOVFLUXO;
            END;
            CLOSE C_AJUSREC;
            DEALLOCATE C_AJUSREC;
         END
         ELSE BEGIN
            SELECT @V_CDHISTPADR = T.CDHISTPADR, 
                   @V_DSHISTPADR = H.DSHISTPADR
              FROM TIPOCTRECEBER T, HISTPADR H
             WHERE T.CDTPCTRECEB = @C_REC_CONTA_CDTPCTRECEB 
               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 = 'CTAREC');
            END;
                                                                                                                   
            EXEC MONTA_HISTORICO @C_REC_CONTA_CDFILIAL, @C_REC_CONTA_NRLANCTONF, @V_CDHISTPADR, '', '', '07', '',
                                 @V_DSHISTPADR;
                                                                                                                   
            EXEC CONTCTBL_CLIENTE @C_REC_CONTA_CDEMPRESA, '', 'E', @C_REC_CONTA_CDFILIEMIT, @C_REC_CONTA_CDCLIENTE,
                                  @V_STCONTCTBL, @V_STCONTCTBLP, @V_STCONTADIFE, @V_CDHISTPADRA;
                                                                                                                   
            EXEC GERA_LOTECONTABIL @C_REC_CONTA_CDEMPRESA, '07', '', @C_REC_CONTA_CDFILIEMIT, @C_REC_CONTA_DTENTRSAID,
                                   @C_REC_CONTA_CDTPCTRECEB, '', @V_CDLOTE,@V_NRLANCLOTE;
                                                                                                                   
            /* DEBITA CLIENTE */
            SET @C_REC_CONTA_VRNOTAFISCAJUST = (@C_REC_CONTA_VRNOTAFISC * -1);
            SET @C_REC_CONTA_NRNOTAFISCAJUST = (@C_REC_CONTA_NRNOTAFISC + '/' + @C_REC_CONTA_CDSERIE);
            EXEC NOVA_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE,@C_REC_CONTA_DTENTRSAID,@V_NRLANCLOTE, @V_STCONTCTBL, 
                              @V_CDHISTPADR, @V_DSHISTPADR, @C_REC_CONTA_VRNOTAFISCAJUST, 'C', @C_REC_CONTA_CDCLIENTE,
                              '07', @C_REC_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                   
            SELECT @V_CDCENTCUST = ISNULL(F.CDCENTCUST, E.CDCENTCUST)
              FROM FILIAL F, EMPRESA E
             WHERE F.CDFILIAL  = @C_REC_CONTA_CDFILIEMIT
               AND E.CDEMPRESA = @C_REC_CONTA_CDEMPRESA;
                                                                                                                   
            IF (@V_CDCENTCUST IS NOT NULL) BEGIN
               SET @C_REC_CONTA_VRNOTAFISCAJUST = (@C_REC_CONTA_VRNOTAFISC * -1);
               EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                   @C_REC_CONTA_CDFILIEMIT, @V_CDCENTCUST, @C_REC_CONTA_VRNOTAFISCAJUST;
            END;
                                                                                                                   
            SELECT @V_STCONTCTBL = CDCONTCTBL
              FROM TIPOCTRECEBER
             WHERE CDTPCTRECEB = @C_REC_CONTA_CDTPCTRECEB;
                                                                                                                   
            SELECT @V_CDFILIAL = CDFILIAL
              FROM RATTPCTR
             WHERE (CDTPCTRECEB = @C_REC_CONTA_CDTPCTRECEB);
                                                                                                                   
            /* CREDITA tipo de conta a receber */
            SET @C_REC_CONTA_NRNOTAFISCAJUST = (@C_REC_CONTA_NRNOTAFISC + '/' + @C_REC_CONTA_CDSERIE);
            EXEC NOVA_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE, @V_STCONTCTBL,
                              @V_CDHISTPADR, @V_DSHISTPADR, @C_REC_CONTA_VRNOTAFISC, 'C', @C_REC_CONTA_CDCLIENTE, '07',
                              @C_REC_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                   
            SET @V_INPARTIDA    = 1;
            SET @V_DOVRTOTALRAT = 0;
                                                                                                                   
            SELECT @V_COUNT = COUNT(*)
              FROM RATTPCTR
             WHERE (CDTPCTRECEB = @C_REC_CONTA_CDTPCTRECEB);
                                                                                                                   
            IF (@V_CDFILIAL IS NOT NULL) BEGIN
                                                                                                                   
               DECLARE C_RATTPCTR CURSOR FOR
                  SELECT CDCENTCUST, CDFIRATTPCTR, CDFILIAL, VRPERATTPCTR
                    FROM RATTPCTR
                   WHERE (CDTPCTRECEB = @C_REC_CONTA_CDTPCTRECEB);
                                                                                                                   
               OPEN C_RATTPCTR;
               FETCH NEXT FROM C_RATTPCTR INTO @C_RATTPCTR_CDCENTCUST, @C_RATTPCTR_CDFIRATTPCTR, 
                                               @C_RATTPCTR_CDFILIAL,   @C_RATTPCTR_VRPERATTPCTR;
                                                                                                                   
               WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                   
                   SET @V_DOVRTOTALRAT = @V_DOVRTOTALRAT + ROUND(((@C_REC_CONTA_VRNOTAFISC * @C_RATTPCTR_VRPERATTPCTR) / 100), 2);
                   SET @V_VRRATEIO = ROUND(((@C_REC_CONTA_VRNOTAFISC * @C_RATTPCTR_VRPERATTPCTR) / 100), 2);
                                                                                                                   
                   IF (@V_INPARTIDA = @V_COUNT) BEGIN
                      IF (@C_REC_CONTA_VRNOTAFISC <> @V_DOVRTOTALRAT) BEGIN
                         SET @V_VRRATEIO = @V_VRRATEIO + (@C_REC_CONTA_VRNOTAFISC - @V_DOVRTOTALRAT);
                      END;
                   END;
                                                                                                                   
                   IF (@C_RATTPCTR_CDFILIAL IS NOT NULL) BEGIN
                      EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                          @C_RATTPCTR_CDFILIAL, @C_RATTPCTR_CDCENTCUST, @V_VRRATEIO;
                   END
                   ELSE BEGIN
                      EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                          @C_REC_CONTA_CDFILIEMIT, @C_RATTPCTR_CDCENTCUST, @V_VRRATEIO;
                   END;
                                                                                                                   
                   SET @V_INPARTIDA = (@V_INPARTIDA + 1);
                                                                                                                   
                   FETCH NEXT FROM C_RATTPCTR INTO @C_RATTPCTR_CDCENTCUST, @C_RATTPCTR_CDFIRATTPCTR, 
                                                   @C_RATTPCTR_CDFILIAL,   @C_RATTPCTR_VRPERATTPCTR;
               END; 
               CLOSE C_RATTPCTR;
               DEALLOCATE C_RATTPCTR;
            END
            ELSE BEGIN
               SET @C_REC_CONTA_VRNOTAFISCAJUST = ROUND(@C_REC_CONTA_VRNOTAFISC, 0);
               EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                   @C_REC_CONTA_CDFILIEMIT, @V_CDCENTCUST, @C_REC_CONTA_VRNOTAFISCAJUST;
            END;
                                                                                                                   
            DECLARE C_AJUSREC CURSOR FOR
               SELECT A.CDFILIAL, A.NRLANCTONF, A.NRSEQVENREC,
                      A.NRPARCELAREC, A.DTAJUSTEREC, A.CDAJUCTREC,
                      T.IDAJUCTREC, T.DSAJUCTREC, A.DSAJUSTEREC,
                      A.VRAJUSTEREC, A.CDECTBAJREC, A.NRLCTBAJREC,
                      A.CDCLASSFINA, A.CDEMMFAJCNTR, A.CDFIMFAJCNTR,
                      A.DTMOVFLUXO, A.IDMOVFLUXO
                 FROM TIPOAJRECEBER T, AJCTRECEBER A, LISTA L
                WHERE (A.CDFILIAL     = SUBSTRING(L.CDLISTA, 01, 04))
                  AND (A.NRLANCTONF   = SUBSTRING(L.CDLISTA, 05, 10))
                  AND (A.CDAJUCTREC   = T.CDAJUCTREC)
                ORDER BY A.CDFILIAL, A.NRLANCTONF, A.CDAJUCTREC;
                                                                                                                   
            OPEN C_AJUSREC;
            FETCH NEXT FROM C_AJUSREC INTO @C_AJUSREC_CDFILIAL,     @C_AJUSREC_NRLANCTONF,   @C_AJUSREC_NRSEQVENREC,
                                           @C_AJUSREC_NRPARCELAREC, @C_AJUSREC_DTAJUSTEREC,  @C_AJUSREC_CDAJUCTREC,
                                           @C_AJUSREC_IDAJUCTREC,   @C_AJUSREC_DSAJUCTREC,   @C_AJUSREC_DSAJUSTEREC,
                                           @C_AJUSREC_VRAJUSTEREC,  @C_AJUSREC_CDECTBAJREC,  @C_AJUSREC_NRLCTBAJREC,
                                           @C_AJUSREC_CDCLASSFINA,  @C_AJUSREC_CDEMMFAJCNTR, @C_AJUSREC_CDFIMFAJCNTR,
                                           @C_AJUSREC_DTMOVFLUXO,   @C_AJUSREC_IDMOVFLUXO;
                                                                                                                   
            WHILE (@@FETCH_STATUS = 0) BEGIN 
                                                                                                                   
                DECLARE C_TPAJUSREC CURSOR FOR
                   SELECT T.CDAJUCTREC, T.CDHISTPADR, T.CDCONTCTBL, T.DSAJUCTREC, T.IDAJUCTREC, H.DSHISTPADR
                     FROM TIPOAJRECEBER T, HISTPADR H
                    WHERE (T.CDAJUCTREC = @C_AJUSREC_CDAJUCTREC)
                      AND (T.CDHISTPADR = H.CDHISTPADR)
                    ORDER BY T.CDAJUCTREC;
                                                                                                                   
                OPEN C_TPAJUSREC;
                FETCH NEXT FROM C_TPAJUSREC INTO @C_TPAJUSREC_CDAJUCTREC, @C_TPAJUSREC_CDHISTPADR, @C_TPAJUSREC_CDCONTCTBL,
                                                 @C_TPAJUSREC_DSAJUCTREC, @C_TPAJUSREC_IDAJUCTREC, @C_TPAJUSREC_DSHISTPADR;
                                                                                                                   
                WHILE (@@FETCH_STATUS = 0) BEGIN
                    EXEC MONTA_HISTORICO @C_AJUSREC_CDFILIAL, @C_AJUSREC_NRLANCTONF, @C_TPAJUSREC_CDHISTPADR, '', 
                                         @C_AJUSREC_DTAJUSTEREC, '07', @C_AJUSREC_CDAJUCTREC, @V_DSHISTPADR;
                                                                                                                   
                    FETCH NEXT FROM C_TPAJUSREC INTO @C_TPAJUSREC_CDAJUCTREC, @C_TPAJUSREC_CDHISTPADR, @C_TPAJUSREC_CDCONTCTBL,
                                                     @C_TPAJUSREC_DSAJUCTREC, @C_TPAJUSREC_IDAJUCTREC, @C_TPAJUSREC_DSHISTPADR;                    
                END;
                CLOSE C_TPAJUSREC;
                DEALLOCATE C_TPAJUSREC;
                                                                                                                   
                EXEC CONTCTBL_CLIENTE @C_REC_CONTA_CDEMPRESA, '', 'E', @C_REC_CONTA_CDFILIEMIT, @C_REC_CONTA_CDCLIENTE,
                                      @V_STCONTCTBL, @V_STCONTCTBLP, @V_STCONTADIFE, @V_CDHISTPADRA;
                                                                                                                   
                IF (@C_AJUSREC_IDAJUCTREC = 'D') BEGIN
                   SET @V_DBFATOR = -1;
                END
                ELSE BEGIN
                   SET @V_DBFATOR = 1;
                END;
                                                                                                                   
                /* CREDITA CLIENTE*/
                SET @C_AJUSREC_VRAJUSTERECAJUST  = (@C_AJUSREC_VRAJUSTEREC * (@V_DBFATOR * -1));
                SET @C_REC_CONTA_NRNOTAFISCAJUST = (@C_REC_CONTA_NRNOTAFISC + '/' + @C_REC_CONTA_CDSERIE);
                                                                                                                   
                EXEC NOVA_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE, 
                                  @V_STCONTCTBL, @V_CDHISTPADR, @V_DSHISTPADR, @C_AJUSREC_VRAJUSTERECAJUST, 'C',
                                  @C_REC_CONTA_CDCLIENTE, '07', @C_REC_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                   
                IF (@V_CDCENTCUST IS NOT NULL) BEGIN
                   SET @C_AJUSREC_VRAJUSTERECAJUST = (@C_AJUSREC_VRAJUSTEREC * (@V_DBFATOR * -1));
                   EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                       @C_REC_CONTA_CDFILIEMIT, @V_CDCENTCUST, @C_AJUSREC_VRAJUSTERECAJUST;
                END;
                                                                                                                   
                SELECT @V_STCONTCTBL = CDCONTCTBL
                  FROM TIPOAJRECEBER
                 WHERE CDAJUCTREC = @C_AJUSREC_CDAJUCTREC;
                                                                                                                   
                /* DEBITA TIPO DE AJUSTE A RECEBER */
                SET @C_AJUSREC_VRAJUSTERECAJUST  = (@C_AJUSREC_VRAJUSTEREC * @V_DBFATOR);
                SET @C_REC_CONTA_NRNOTAFISCAJUST = (@C_REC_CONTA_NRNOTAFISC + '/' + @C_REC_CONTA_CDSERIE);
                EXEC NOVA_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE, @V_STCONTCTBL,
                                  @V_CDHISTPADR, @V_DSHISTPADR, @C_AJUSREC_VRAJUSTERECAJUST, 'C', @C_REC_CONTA_CDCLIENTE,
                                  '07', @C_REC_CONTA_NRNOTAFISCAJUST, 'N';
                                                                                                                   
                SET @V_INPARTIDA = 1;
                                                                                                                   
                SELECT @V_CDFILIAL = CDFILIAL
                  FROM RATTPAJR
                 WHERE (CDAJUCTREC = @C_AJUSREC_CDAJUCTREC);
                                                                                                                   
                SELECT @V_COUNT = COUNT(*)
                  FROM RATTPAJR
                 WHERE (CDAJUCTREC = @C_AJUSREC_CDAJUCTREC);
                                                                                                                   
                SET @V_DOVRTOTALRAT = 0;
                                                                                                                   
                IF (@V_CDFILIAL IS NOT NULL) BEGIN
                   DECLARE C_RATTPAJR CURSOR FOR
                      SELECT CDCENTCUST, CDFIRATTPAJR, CDFILIAL, VRPERATTPAJR
                        FROM RATTPAJR
                       WHERE (CDAJUCTREC = @C_AJUSREC_CDAJUCTREC);
                                                                                                                   
                   OPEN C_RATTPAJR;
                   FETCH NEXT FROM C_RATTPAJR INTO @C_RATTPAJR_CDCENTCUST, @C_RATTPAJR_CDFIRATTPAJR,
                                                   @C_RATTPAJR_CDFILIAL,   @C_RATTPAJR_VRPERATTPAJR;
                                                                                                                   
                   WHILE (@@FETCH_STATUS = 0) BEGIN
                                                                                                                   
                      SET @V_DOVRTOTALRAT = 0;
                      SET @V_DOVRTOTALRAT = @V_DOVRTOTALRAT + ROUND(((@C_AJUSREC_VRAJUSTEREC * @C_RATTPAJR_VRPERATTPAJR) / 100), 2);
                      SET @V_VRRATEIO = (ROUND(((@C_AJUSREC_VRAJUSTEREC * @C_RATTPAJR_VRPERATTPAJR) / 100), 2) * -1);
                                                                                                                   
                      IF (@C_RATTPAJR_CDFILIAL IS NOT NULL) BEGIN
                         SET @V_VRRATEIOAJUST = (@V_VRRATEIO * @V_DBFATOR);
                         EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                             @C_RATTPAJR_CDFILIAL, @C_RATTPAJR_CDCENTCUST, @V_VRRATEIOAJUST;
                      END
                      ELSE BEGIN
                         SET @V_VRRATEIOAJUST = (@V_VRRATEIO * @V_DBFATOR);
                         EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                             @C_REC_CONTA_CDFILIEMIT, @C_RATTPAJR_CDCENTCUST, @V_VRRATEIOAJUST;
                      END;
                                                                                                                   
                      SET @V_INPARTIDA = @V_INPARTIDA + 1;
                                                                                                                   
                      FETCH NEXT FROM C_RATTPAJR INTO @C_RATTPAJR_CDCENTCUST, @C_RATTPAJR_CDFIRATTPAJR,
                                                      @C_RATTPAJR_CDFILIAL,   @C_RATTPAJR_VRPERATTPAJR;
                   END;
                   CLOSE C_RATTPAJR;
                   DEALLOCATE C_RATTPAJR;
                END
                ELSE BEGIN
                   SET @C_AJUSREC_VRAJUSTERECAJUST = ROUND((@C_AJUSREC_VRAJUSTEREC * @V_DBFATOR), 2);
                   EXEC RATEIO_PARTIDA @C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @C_REC_CONTA_DTENTRSAID, @V_NRLANCLOTE,
                                       @C_REC_CONTA_CDFILIEMIT, @V_CDCENTCUST, @C_AJUSREC_VRAJUSTERECAJUST;
                END;
                                                                                                                   
                FETCH NEXT FROM C_AJUSREC INTO @C_AJUSREC_CDFILIAL,     @C_AJUSREC_NRLANCTONF,   @C_AJUSREC_NRSEQVENREC,
                                               @C_AJUSREC_NRPARCELAREC, @C_AJUSREC_DTAJUSTEREC,  @C_AJUSREC_CDAJUCTREC,
                                               @C_AJUSREC_IDAJUCTREC,   @C_AJUSREC_DSAJUCTREC,   @C_AJUSREC_DSAJUSTEREC,
                                               @C_AJUSREC_VRAJUSTEREC,  @C_AJUSREC_CDECTBAJREC,  @C_AJUSREC_NRLCTBAJREC,
                                               @C_AJUSREC_CDCLASSFINA,  @C_AJUSREC_CDEMMFAJCNTR, @C_AJUSREC_CDFIMFAJCNTR,
                                               @C_AJUSREC_DTMOVFLUXO,   @C_AJUSREC_IDMOVFLUXO;
            END;
            CLOSE C_AJUSREC;
            DEALLOCATE C_AJUSREC;
         END;
                                                                                                                   
         INSERT INTO RELACNFLANCLOTE
            (CDEMPRESA, CDLOTE, NRLANCLOTE, NRSEQLANLOTE,CDFILIAL, NRLANCTONF)
         VALUES
            (@C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @V_NRLANCLOTE, '00001', @C_REC_CONTA_CDFILIAL, @C_REC_CONTA_NRLANCTONF);
                                                                                                                   
         IF (TEKNISA.CONFERE_PARTIDA(@C_REC_CONTA_CDEMPRESA, @V_CDLOTE, @V_NRLANCLOTE,@C_REC_CONTA_DTENTRSAID) = 'V') BEGIN
            UPDATE NOTAFISCAL 
               SET IDSTATUSCTBNF = 'C'
             WHERE CDFILIAL   = @C_REC_CONTA_CDFILIAL
               AND NRLANCTONF = @C_REC_CONTA_NRLANCTONF;
         END
         ELSE BEGIN
            UPDATE NOTAFISCAL 
               SET IDSTATUSCTBNF = 'E'
             WHERE CDFILIAL   = @C_REC_CONTA_CDFILIAL
               AND NRLANCTONF = @C_REC_CONTA_NRLANCTONF;
         END;

         FETCH NEXT FROM C_REC_CONTA INTO @C_REC_CONTA_CDTPCTRECEB,  @C_REC_CONTA_CDSERIE,    @C_REC_CONTA_CDINSCESTA,
                                          @C_REC_CONTA_CDINSCMUNI,   @C_REC_CONTA_VRNOTAFISC, @C_REC_CONTA_CDFILIEMIT,
                                          @C_REC_CONTA_CDTIPOOPER,   @C_REC_CONTA_IDTPINSJUR, @C_REC_CONTA_NRINSJURCLIE,
                                          @C_REC_CONTA_CDCLIENTE,    @C_REC_CONTA_NRNOTAFISC, @C_REC_CONTA_DTEMISSAO,
                                          @C_REC_CONTA_DTENTRSAID,   @C_REC_CONTA_IDSTATUS,   @C_REC_CONTA_NRLANCTBDOC,
                                          @C_REC_CONTA_NRLICENCAUSO, @C_REC_CONTA_CDVENDEDOR, @C_REC_CONTA_CDFILIAL,
                                          @C_REC_CONTA_NRLANCTONF,   @C_REC_CONTA_CDEMPRESA,  @C_REC_CONTA_CDPADRCONTABIL;
   END;
   CLOSE C_REC_CONTA;
   DEALLOCATE C_REC_CONTA;

   DELETE FROM LISTA
    WHERE NRLISTA = @P_NRLISTA;
END;
GO