CREATE OR REPLACE PROCEDURE CTB_REC_BAIXA(
   P_CDEMPRESA     IN EMPRESA.CDEMPRESA%TYPE,
   P_CDFILIAL      IN FILIAL.CDFILIAL%TYPE,
   P_FILIPROC      IN FILIAL.CDFILIAL%TYPE,
   P_NRBAIXA       IN BAIXARECEBIDA.NRBAIXAREC%TYPE,
   P_NRLANCCTB    OUT VARCHAR2)
AS
   V_NRLANCTEMP  VARCHAR2(20);
   V_NRLANCTEMPERRO  VARCHAR2(20);
   V_DTBAIXA     DATE;
   V_CDREDUCONT  CONTCORR.CDREDUCONT%TYPE;
   V_CDTPBAIXA   BAIXARECEBIDA.CDTPBAIXAREC%TYPE;
   V_VRBAIXAREC  BAIXARECEBIDA.VRBAIXAREC%TYPE;
   V_CDCONTCTBL  CONTCTBL.CDCONTCTBL%TYPE;
   V_DOCUMENTO   BAIXARECEBIDA.NRBAIXAREC%TYPE;
   V_IDCTBLBXREC TIPOBAIXAREC.IDCTBLBXREC%TYPE;
   V_CDCONTCTBLPROD CONTCTBL.CDCONTCTBL%TYPE;

   /*HISTORICO PADRAO */
   V_CDHISTPADR  HISTPADR.CDHISTPADR%TYPE;
   V_DSHISTPADR  LANCCONT.DSHISTLANC%TYPE;


   V_IDCOME      VARCHAR2(01);
   V_IDCCUS      VARCHAR2(01);
   V_CODCOMEAUX  VARCHAR2(21);
   V_SEQPARTIDA  INTEGER;


   /* VARIAVEL UTILIZADA PARA VERIFICAR SE A EMPRESA CONTROLA RATEIO */
   V_CONTRAT     VARCHAR2(01);

   V_CDCENTCUST  CENTCUST.CDCENTCUST%TYPE;
   V_RATSEQ      INTEGER;
   V_COUNT       INTEGER;

   V_BOVENDAFOS  BOOLEAN;


   V_ENTROULOOP  VARCHAR2(01);
   V_TOTALRATEIO LANCCONT.VRLANCAMENT%TYPE;
   V_CDFILIAL    FILIAL.CDFILIAL%TYPE;

   V_SEQUENCIAL VARCHAR2(05);

   V_VRAJUSTE   LANCCONT.VRLANCAMENT%TYPE;
   V_TOTAJUSTE  LANCCONT.VRLANCAMENT%TYPE;
   V_TOTRATAJ   LANCCONT.VRLANCAMENT%TYPE;

   V_STATUS     VARCHAR2(01);
   V_FATOR      INTEGER;
   V_CDHISTINT  HISTPADR.CDHISTPADR%TYPE;
   V_DSHISTINT  LANCCONT.DSHISTLANC%TYPE;

   /* BUSCA VALOR DO AJUSTE QUE NAO CONTABILIZA */
   V_VRTOTALAJUSNCTB LANCCONT.VRLANCAMENT%TYPE;
   /* CALCULA O VALRO DO AJUSTE DO BOLETO RATEADO NA CONTA*/
   V_VRAJUSNCTB      LANCCONT.VRLANCAMENT%TYPE;

   /* SOMA O VALRO DO AJUSTE DO BOLETO RATEADO NA CONTA*/
   V_VRSOMAAJUSNCTB  LANCCONT.VRLANCAMENT%TYPE;

   V_CDFILIALBOL BOLETRECEBER.CDFILIAL%TYPE;
   V_NRLANBOLREC BOLETRECEBER.NRLANBOLREC%TYPE;

   /* CONTROLE NUMERO DE CONTAS NO BOLETOS*/
   V_NRCONTABOL NUMBER;

   V_VRPARTIDA LANCCONT.VRLANCAMENT%TYPE;

   V_CONTAJUST INTEGER;

   V_INPARTIDAS INTEGER;
   V_TOTCREDITO NUMBER;
   V_TOTDEBITO NUMBER;
   V_DIFERENCA NUMBER;
   V_NRSEQUENCIAL LANCCTBTEMP.NRSEQCTBTEMP%TYPE;
   V_CONTATRANSFIL VARCHAR2(20);
   V_IDCONTATRANSFIL VARCHAR2(01);
   V_INTGPLATAO BOOLEAN;
   V_COUNTPLAT     INTEGER;
   V_CDREDUCONTAUX  CONTCORR.CDREDUCONT%TYPE;
   V_VRSOMAAJUSPT  LANCCONT.VRLANCAMENT%TYPE;
   V_COUNTBOLASS INTEGER;
   V_CDFILIALBPART   VARCHAR2(04);
   V_NRLANCTONFBPART VARCHAR2(10);
   V_NRSEQVENRECBPART VARCHAR2(02);
   V_NRPARCELARECBPART VARCHAR2(02);
   V_CDHISTPADRBOL  HISTPADR.CDHISTPADR%TYPE;

   PROCEDURE BUSCA_TPCOMERCIAL(P_CDCONTCTBL IN VARCHAR2,
                               P_IDCOME     OUT VARCHAR2,
                               P_IDCCUS     OUT VARCHAR2)
   AS
      V_IDCODCOME VARCHAR2(01);
      V_IDCCUS VARCHAR2(21);
   BEGIN
      BEGIN
         SELECT NVL(IDTIPOCOME, 'N') IDTIPOCOME, IDCONTCCUS
           INTO V_IDCODCOME, V_IDCCUS
           FROM CONTCTBL
          WHERE CDCONTCTBL = P_CDCONTCTBL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDCODCOME  := 'N';
            V_IDCCUS      := 'L';
      END;
      P_IDCOME := V_IDCODCOME;
      P_IDCCUS := V_IDCCUS;
   END;

   FUNCTION BUSCA_CCUSTO(P_CDFILIAL IN VARCHAR, P_CDEMPRESA IN VARCHAR) RETURN VARCHAR
   IS
      V_CDCENTCUST VARCHAR2(04);
   BEGIN
      BEGIN
         SELECT CDCENTCUST
           INTO V_CDCENTCUST
           FROM FILIAL
          WHERE CDFILIAL = P_CDFILIAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDCENTCUST := NULL;
      END;

      IF V_CDCENTCUST IS NULL THEN
         BEGIN
            SELECT CDCENTCUST
              INTO V_CDCENTCUST
              FROM EMPRESA
             WHERE CDEMPRESA = P_CDEMPRESA;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CDCENTCUST := NULL;
         END;
      END IF;
      RETURN(V_CDCENTCUST);
   END;
BEGIN
    V_INTGPLATAO := FALSE;
   /* FILTRA A BAIXA A SER CONTABILIZADA */
   BEGIN
      SELECT BR.DTBAIXAREC, BR.CDREDUCONT, BR.CDTPBAIXAREC, BR.VRBAIXAREC, BR.NRBAIXAREC,
             BR.IDSTATUSBREC, TB.IDCTBLBXREC, BR.NRLCTBBREC
        INTO V_DTBAIXA, V_CDREDUCONT, V_CDTPBAIXA, V_VRBAIXAREC, V_DOCUMENTO,
             V_STATUS, V_IDCTBLBXREC, V_NRLANCTEMP
        FROM BAIXARECEBIDA BR, TIPOBAIXAREC TB
       WHERE BR.CDFILIAL = P_CDFILIAL
         AND BR.CDEMPRESA = P_CDEMPRESA
         AND BR.NRBAIXAREC = P_NRBAIXA
         AND BR.CDTPBAIXAREC = TB.CDTPBAIXAREC
         AND TB.IDCTBLBXREC = 'S';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_DTBAIXA     := NULL;
         V_CDREDUCONT  := NULL;
         V_CDTPBAIXA   := NULL;
         V_VRBAIXAREC  := NULL;
         V_DOCUMENTO   := NULL;
         V_STATUS      := NULL;
         V_IDCTBLBXREC := NULL;
         V_NRLANCTEMP  := NULL;
   END;

   /* CASO O VARIAVEL DE NUMERO DE LANCAMENTO CONTABIL NAO SEJA NULO, E PORQUE O LANCAMENTO JA EXISTIA.
      TENDO ASSIM QUE APAGAR O LANCAMENTO PARA REFAZE-LO NA ROTINA
                                       */
   V_CDREDUCONTAUX := V_CDREDUCONT;
   IF V_NRLANCTEMP IS NOT NULL THEN
      /* DELETA OS RATEIOS DO LANCAMENTO CONTABIL */
      DELETE
        FROM LANCCCUSTO
       WHERE NRLANCCONT = V_NRLANCTEMP
         AND CDEMPRESA  = P_CDEMPRESA;

      /* DELETA AS PARTIDAS DO LANCAMENTO CONTABIL */
      DELETE
        FROM LANCCONT
       WHERE NRLANCCONT = V_NRLANCTEMP
         AND CDEMPRESA  = P_CDEMPRESA;

      DELETE
        FROM RATECTBTEMP
       WHERE NRLANCCTBTEMP = V_NRLANCTEMP
         AND CDEMPRESA = P_CDEMPRESA;

      DELETE
        FROM LANCCTBTEMP
       WHERE NRLANCCTBTEMP = V_NRLANCTEMP
         AND CDEMPRESA = P_CDEMPRESA;
   END IF;

   BEGIN
      SELECT COUNT(*)
        INTO V_COUNT
        FROM PARAMCTBL
       WHERE V_DTBAIXA >= DTINTCTBREC;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_COUNT := 0;
   END;
  /* BEGIN
      SELECT COUNT(*)
        INTO V_COUNTPLAT
        FROM EMPRESA
       WHERE NRORG = 1062;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_COUNT := 0;
   END; */
   IF V_COUNTPLAT > 0 THEN
      V_INTGPLATAO := TRUE;

   END IF;
   IF V_COUNT > 0 THEN
      BEGIN
         SELECT COUNT(*)
           INTO V_CONTAJUST
           FROM BOLETRECEBER BO, AJBOLREC AJ, TIPOAJRECEBER TP
          WHERE BO.CDFILIBAIREC = P_FILIPROC
            AND BO.NRBAIXAREC = P_NRBAIXA
            AND BO.NRLANBOLREC = AJ.NRLANBOLREC
            AND BO.CDFILIAL = AJ.CDFILIAL
            AND AJ.CDAJUCTREC = TP.CDAJUCTREC
            AND TP.IDCTBAJCTREC = 'S';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CONTAJUST := 0;
      END;
      BEGIN
       SELECT SUM((AJ.VRAJBOLREC*DECODE(TP.IDAJUCTREC,'D',1,-1)))
           INTO V_VRSOMAAJUSPT
           FROM BOLETRECEBER BO, AJBOLREC AJ, TIPOAJRECEBER TP
          WHERE BO.CDFILIBAIREC = P_FILIPROC
            AND BO.NRBAIXAREC = P_NRBAIXA
            AND BO.NRLANBOLREC = AJ.NRLANBOLREC
            AND BO.CDFILIAL = AJ.CDFILIAL
            AND AJ.CDAJUCTREC = TP.CDAJUCTREC
            AND TP.IDCTBAJCTREC = 'S';
      EXCEPTION
          WHEN NO_DATA_FOUND THEN
           V_VRSOMAAJUSPT :=  0;
      END;

      IF V_STATUS <> 'P' AND V_IDCTBLBXREC = 'S' THEN
         /* CASO A VARIAVEL QUE RECEBE O NUMERO DO LANCAMENTO CONTABIL SEJA NULA, E PORQUE O LANCAMENTO CONTABIL E NOVO. */
         IF V_NRLANCTEMP IS NULL THEN
            /* PEGA UM NOVO NUMERO DE LANCAMENTO CONTABIL, CASO O LANCAMENTO NO REC SEJA NOVO */
            NOVO_CODIGO_NT('LANCCONTABIL' || P_CDEMPRESA || TO_CHAR(V_DTBAIXA,'YYYYMM'), V_NRLANCTEMP);
            V_NRLANCTEMP := TO_CHAR(V_DTBAIXA,'YYYYMM')||SUBSTR(V_NRLANCTEMP, 15, 6);
         END IF;

         V_TOTCREDITO := 0;
         V_TOTDEBITO := 0;
         IF (V_VRBAIXAREC <> 0 OR V_CONTAJUST <> 0) THEN
            /* PARAMETRO E BUSCADO PARA VER SE IRA OU NAO GERAR OS RATEIOS */
            BEGIN
               SELECT IDCONTGERE
                 INTO V_CONTRAT
                 FROM CONTOPECTBL
                WHERE CDEMPRESA = P_CDEMPRESA;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CONTRAT := NULL;
            END;

            BEGIN
               SELECT H.CDHISTPADR, HP.DSHISTPADR
                 INTO V_CDHISTINT, V_DSHISTINT
                 FROM HISTINTEGRA H, HISTPADR HP
                WHERE H.CDHISTPADR = HP.CDHISTPADR
                  AND CDOPERACAO = 'BAIXAR';
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDHISTINT := NULL;
                  V_DSHISTINT := NULL;
            END;

            /* SELECIONA O HISTORICO PADRAO RELACIONADO A BAIXA */
            BEGIN
               SELECT T.CDHISTPADR, H.DSHISTPADR
                 INTO V_CDHISTPADR, V_DSHISTPADR
                 FROM TIPOBAIXAREC T, HISTPADR H
                WHERE T.CDTPBAIXAREC = V_CDTPBAIXA
                  AND T.CDHISTPADR   = H.CDHISTPADR;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDHISTPADR := NULL;
                  V_DSHISTPADR := NULL;
            END;

            IF V_CDHISTPADR IS NULL THEN
               V_CDHISTPADR := V_CDHISTINT;
               V_DSHISTPADR := V_DSHISTINT;
            END IF;

            V_SEQPARTIDA := 0;
            IF V_VRBAIXAREC <> 0 THEN
               IF V_INTGPLATAO THEN
                  /*BUSCA A CONTA DE RECEITA SE O SISTEMA REALIZAR INTEGRACAO COM O PLATAO*/
                  BEGIN
                    SELECT MAX(CDCONTPROD)
                      INTO V_CDCONTCTBLPROD
                      FROM TIPOOPERAC
                     WHERE CDTIPOOPER = '13';
                  EXCEPTION WHEN NO_DATA_FOUND THEN
                     V_CDCONTCTBLPROD := NULL;
                  END;

                  BUSCA_TPCOMERCIAL(V_CDCONTCTBLPROD, V_IDCOME, V_IDCCUS);
                  V_CDREDUCONTAUX := V_CDREDUCONT;
                  IF V_IDCOME <> 'B' THEN
                     V_IDCOME  := 'N';
                     V_CDREDUCONT := NULL;
                  END IF;
                  V_SEQPARTIDA := V_SEQPARTIDA + 1;
                  V_NRSEQUENCIAL := LPAD(TO_CHAR(V_SEQPARTIDA),5,'0');

                  INSERT INTO LANCCTBTEMP
                     (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                      DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
                  VALUES
                     (P_CDEMPRESA, V_NRLANCTEMP, V_DOCUMENTO, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDCONTCTBLPROD, V_CDHISTPADR,
                      'Recebimento', V_DTBAIXA, ROUND((V_VRBAIXAREC+NVL(V_VRSOMAAJUSPT,0))*-1 , 2), V_IDCOME, V_CDREDUCONT, 'REC');

                  V_RATSEQ := 1;
                  IF V_CONTRAT = 'S' THEN
                     IF V_IDCCUS <> 'O' THEN
                        V_CDFILIAL := NULL;
                        BEGIN
                           SELECT CDFILIAL
                             INTO V_CDFILIAL
                             FROM CONTCORR
                            WHERE CDREDUCONT = V_CDREDUCONTAUX;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_CDFILIAL := NULL;
                        END;
                        IF V_CDFILIAL IS NULL THEN
                           V_CDFILIAL := P_FILIPROC;
                           V_CDCENTCUST := BUSCA_CCUSTO(P_FILIPROC, P_CDEMPRESA);
                        ELSE
                           V_CDCENTCUST := BUSCA_CCUSTO(V_CDFILIAL, P_CDEMPRESA);
                        END IF;
                        INSERT INTO RATECTBTEMP
                           (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                        VALUES
                           (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDFILIAL, V_CDCENTCUST,
                            ROUND((V_VRBAIXAREC+NVL(V_VRSOMAAJUSPT,0))*-1 , 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                     END IF;
                  END IF;
                   /* FILTRA TODOS AS CONTAS A RECEBER RELACIONADAS AO BOLETO DA BAIXA*/
                  FOR CONTAS IN
                     (SELECT CT.CDFILIAL, CT.NRLANCTONF, CT.NRSEQVENREC, CT.NRPARCELAREC,
                             B.CDFILIAL AS CDFILBOL, B.NRLANBOLREC, B.CDCLIENTE, CT.CDTPCTRECEB, TP.CDCONTCTBL,
                             TP.CDHISTPADR, H.DSHISTPADR, NF.CDFILIEMIT, NF.NRNOTAFISC,
                             CT.VRATUAREC,  IDCTBTPCNTBREC, B.VRBOLREC, COUNT(*) OVER(PARTITION BY B.CDFILIAL, B.NRLANBOLREC) AS QTCONTAS,
                             TP.IDCTBTIPOOPERBX, T.CDCONTFORCLI
                        FROM BOLETRECEBER B, CONTARECEBER CT, NOTAFISCAL NF, TIPOCTRECEBER TP,
                             HISTPADR H, TIPOOPERAC T
                       WHERE B.CDFILIBAIREC = P_CDFILIAL
                         AND B.NRBAIXAREC = P_NRBAIXA
                         AND B.CDFILIAL = CT.CDFILIBOLREC
                         AND B.NRLANBOLREC = CT.NRLANBOLREC
                         AND CT.CDFILIAL = NF.CDFILIAL
                         AND CT.NRLANCTONF = NF.NRLANCTONF
                         AND CT.CDTPCTRECEB = TP.CDTPCTRECEB
                         AND TP.CDHISTPADR = H.CDHISTPADR(+)
                         AND NF.IDENTRSAIDOP = T.IDENTRSAIDOP
                         AND NF.CDTIPOOPER = T.CDTIPOOPER
                       ORDER BY B.CDFILIAL, B.NRLANBOLREC)LOOP
                     IF CONTAS.VRATUAREC <> 0 THEN
                        BEGIN
                           SELECT COUNT(*)
                             INTO V_COUNT
                             FROM MOVCAIXA
                            WHERE CDFILIALNF = CONTAS.CDFILIAL
                              AND NRLANCTONF = CONTAS.NRLANCTONF;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_COUNT := 0;
                        END;

                        IF V_COUNT > 0 THEN
                           V_BOVENDAFOS := TRUE;
                        END IF;

                        IF ((CONTAS.IDCTBTPCNTBREC = 'S') OR (V_BOVENDAFOS) OR (V_INTGPLATAO)) THEN
                           V_CDCONTCTBL := CONTAS.CDCONTCTBL;
                           V_CDHISTPADR := CONTAS.CDHISTPADR;
                           IF V_CDHISTPADR IS NULL THEN
                              V_DSHISTPADR := V_DSHISTINT;
                              V_CDHISTPADR := V_CDHISTINT;
                           END IF;
                        ELSE
                           V_CDCONTCTBL := NULL;
                           IF CONTAS.IDCTBTIPOOPERBX = 'S' THEN
                              V_CDCONTCTBL := CONTAS.CDCONTFORCLI;
                           END IF;
                           IF V_CDCONTCTBL IS NULL THEN
                              V_CDCONTCTBL := CTB_CCCLIENTE(CONTAS.CDCLIENTE, CONTAS.CDFILIEMIT, P_CDEMPRESA);
                           END IF;
                        END IF;

                        V_DSHISTPADR := CTB_HIST_REC(V_CDHISTPADR, CONTAS.CDFILIAL, CONTAS.NRLANCTONF, CONTAS.NRSEQVENREC, CONTAS.NRPARCELAREC, '');

                        V_SEQPARTIDA := V_SEQPARTIDA + 1;

                        BUSCA_TPCOMERCIAL(V_CDCONTCTBL, V_IDCOME, V_IDCCUS);
                        IF V_IDCOME <> 'C' THEN
                           V_IDCOME  := 'N';
                           V_CODCOMEAUX := NULL;
                        ELSE
                           V_CODCOMEAUX  := CONTAS.CDCLIENTE;
                        END IF;

                        /*ROTINA PARA RATEAR OS VALORES DOS AJUSTES QUE NAO CONTABILIZAM */
                        IF (V_CDFILIALBOL <> CONTAS.CDFILBOL) OR
                           (V_NRLANBOLREC <> CONTAS.NRLANBOLREC) THEN
                           BEGIN
                              SELECT SUM(DECODE(TA.IDAJUCTREC, 'D', AJ.VRAJBOLREC * -1, AJ.VRAJBOLREC)) AS VRAJBOLPAG
                                INTO V_VRTOTALAJUSNCTB
                                FROM AJBOLREC AJ, TIPOAJRECEBER TA
                               WHERE AJ.CDFILIAL = CONTAS.CDFILBOL
                                 AND AJ.NRLANBOLREC = CONTAS.NRLANBOLREC
                                 AND AJ.CDAJUCTREC = TA.CDAJUCTREC
                                 AND TA.IDCTBAJCTREC = 'N';
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN
                                 V_VRTOTALAJUSNCTB := 0;
                           END;
                           IF V_VRTOTALAJUSNCTB IS NULL THEN
                              V_VRTOTALAJUSNCTB := 0;
                           END IF;
                           V_NRCONTABOL := 1;
                           V_VRSOMAAJUSNCTB := 0;
                           V_VRAJUSNCTB := 0;
                           V_CDFILIALBOL := CONTAS.CDFILBOL;
                           V_NRLANBOLREC := CONTAS.NRLANBOLREC;
                        ELSE
                           V_NRCONTABOL := V_NRCONTABOL +1;
                        END IF;

                        IF V_VRTOTALAJUSNCTB <> 0 THEN
                           V_VRAJUSNCTB := (V_VRTOTALAJUSNCTB * 100) / CONTAS.VRBOLREC;
                           V_VRAJUSNCTB := ROUND((V_VRAJUSNCTB * CONTAS.VRATUAREC) / 100, 2);

                           V_VRSOMAAJUSNCTB := V_VRSOMAAJUSNCTB + V_VRAJUSNCTB;

                           IF V_NRCONTABOL = CONTAS.QTCONTAS THEN
                              IF V_VRTOTALAJUSNCTB <> V_VRSOMAAJUSNCTB THEN
                                 V_VRAJUSNCTB := V_VRAJUSNCTB + (V_VRTOTALAJUSNCTB - V_VRSOMAAJUSNCTB);
                              END IF;
                           END IF;

                        END IF;

                        IF V_VRAJUSNCTB IS NULL THEN
                           V_VRAJUSNCTB := 0;
                        END IF;

                        V_VRPARTIDA := ROUND(CONTAS.VRATUAREC + V_VRAJUSNCTB, 2);
                        IF NVL(V_VRPARTIDA, 0) <> 0 THEN
                         --  V_TOTCREDITO :=  V_TOTCREDITO + ROUND(V_VRPARTIDA, 2);
                           V_NRSEQUENCIAL := LPAD(TO_CHAR(V_SEQPARTIDA),5,'0');
                           INSERT INTO LANCCTBTEMP
                              (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                               DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
                           VALUES
                              (P_CDEMPRESA, V_NRLANCTEMP, V_DOCUMENTO, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDCONTCTBL, V_CDHISTPADR,
                               V_DSHISTPADR, V_DTBAIXA, ROUND(V_VRPARTIDA, 2), V_IDCOME, V_CODCOMEAUX, 'REC');

                           V_RATSEQ := 0;
                           IF V_CONTRAT = 'S' THEN
                              IF V_IDCCUS <> 'O' THEN
                                 V_TOTALRATEIO := 0;
                                 IF CONTAS.IDCTBTPCNTBREC <> 'S' THEN
                                    V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIEMIT, P_CDEMPRESA);
                                    INSERT INTO RATECTBTEMP
                                       (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                    VALUES
                                       (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), CONTAS.CDFILIEMIT, V_CDCENTCUST,
                                        ROUND(V_VRPARTIDA, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                    V_TOTALRATEIO := V_TOTALRATEIO + V_VRPARTIDA;
                                 ELSE
                                    V_ENTROULOOP := 'N';
                                    FOR C_RATEIO IN
                                       (SELECT CDCENTCUST, CDFIRATCTREC, VRRATCCCTREC
                                          FROM RATCTRECEBER
                                         WHERE CDFILIAL = CONTAS.CDFILIAL
                                           AND NRLANCTONF = CONTAS.NRLANCTONF
                                           AND NRSEQVENREC = CONTAS.NRSEQVENREC
                                           AND NRPARCELAREC = CONTAS.NRPARCELAREC) LOOP

                                       V_ENTROULOOP := 'S';
                                       V_RATSEQ := V_RATSEQ + 1;
                                       INSERT INTO RATECTBTEMP
                                          (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                       VALUES
                                          (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), C_RATEIO.CDFIRATCTREC, C_RATEIO.CDCENTCUST,
                                           ROUND(C_RATEIO.VRRATCCCTREC, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                       V_TOTALRATEIO := V_TOTALRATEIO +  ROUND(C_RATEIO.VRRATCCCTREC, 2);
                                       V_CDFILIAL   := C_RATEIO.CDFIRATCTREC;
                                       V_CDCENTCUST := C_RATEIO.CDCENTCUST;
                                    END LOOP;

                                    IF V_ENTROULOOP = 'N' THEN
                                       V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIEMIT, P_CDEMPRESA);
                                       V_RATSEQ := V_RATSEQ + 1;
                                       INSERT INTO RATECTBTEMP
                                          (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                       VALUES
                                          (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), CONTAS.CDFILIEMIT, V_CDCENTCUST,
                                           ROUND(V_VRPARTIDA, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                       V_TOTALRATEIO := V_TOTALRATEIO + ROUND(CONTAS.VRATUAREC, 2);
                                       V_CDFILIAL := CONTAS.CDFILIEMIT;
                                       V_CDCENTCUST := V_CDCENTCUST;
                                    END IF;

                                    IF V_TOTALRATEIO <> V_VRPARTIDA THEN
                                       V_VRPARTIDA := V_TOTALRATEIO - V_VRPARTIDA;
                                       V_RATSEQ := V_RATSEQ + 1;
                                       INSERT INTO RATECTBTEMP
                                          (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                       VALUES
                                          (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDFILIAL, V_CDCENTCUST,
                                           V_TOTALRATEIO, V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                    END IF;
                                 END IF;
                              END IF;
                           END IF;
                        END IF;
                     END IF;
                  END LOOP;
               END IF;
               BEGIN
                  SELECT CC.CDCONTCTBL
                    INTO V_CDCONTCTBL
                    FROM CONTCORR CC
                   WHERE CC.CDREDUCONT = V_CDREDUCONTAUX;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_CDCONTCTBL := NULL;
               END;

               BUSCA_TPCOMERCIAL(V_CDCONTCTBL, V_IDCOME, V_IDCCUS);

               IF V_IDCOME <> 'B' THEN
                  V_IDCOME  := 'N';
                  V_CDREDUCONT := NULL;
               END IF;

               /*HISTORICO*/
                SELECT  COUNT(*), MAX(CDHISTPADR), MAX(C.CDFILIAL) , MAX(NRLANCTONF), MAX(NRSEQVENREC) , MAX(NRPARCELAREC)
                  INTO V_COUNTBOLASS, V_CDHISTPADRBOL, V_CDFILIALBPART, V_NRLANCTONFBPART, V_NRSEQVENRECBPART, V_NRPARCELARECBPART
                  FROM CONTARECEBER C, TIPOCTRECEBER TP, BOLETRECEBER BO
                 WHERE C.CDFILIBOLREC = BO.CDFILIAL
                   AND C.NRLANBOLREC = BO.NRLANBOLREC
                   AND BO.CDFILIBAIREC = P_CDFILIAL
                   AND BO.NRBAIXAREC  = P_NRBAIXA
                   AND C.CDTPCTRECEB  = TP.CDTPCTRECEB;
               IF V_COUNTBOLASS = 1 THEN
                  V_DSHISTPADR := CTB_HIST_REC(V_CDHISTPADRBOL, V_CDFILIALBPART, V_NRLANCTONFBPART, V_NRSEQVENRECBPART, V_NRPARCELARECBPART, '');
               ELSE
                  V_DSHISTPADR := 'Recebimento';
                  V_CDHISTPADRBOL := NULL;
               END IF;
               V_SEQPARTIDA := V_SEQPARTIDA + 1;
               V_TOTDEBITO := V_TOTDEBITO + ROUND(V_VRBAIXAREC * -1, 2);
               V_NRSEQUENCIAL := LPAD(TO_CHAR(V_SEQPARTIDA),5,'0');
               INSERT INTO LANCCTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                   DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
               VALUES
                  (P_CDEMPRESA, V_NRLANCTEMP, V_DOCUMENTO, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDCONTCTBL, NVL(V_CDHISTPADRBOL,V_CDHISTPADR),
                   NVL(V_DSHISTPADR,'Recebimento'), V_DTBAIXA, ROUND(V_VRBAIXAREC * -1, 2), V_IDCOME, V_CDREDUCONT, 'REC');

               V_RATSEQ := 1;
               IF V_CONTRAT = 'S' THEN
                  IF V_IDCCUS <> 'O' THEN
                     V_CDFILIAL := NULL;
                     BEGIN
                        SELECT CDFILIAL
                          INTO V_CDFILIAL
                          FROM CONTCORR
                         WHERE CDREDUCONT = V_CDREDUCONTAUX;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_CDFILIAL := NULL;
                     END;
                     IF V_CDFILIAL IS NULL THEN
                        V_CDFILIAL := P_FILIPROC;
                        V_CDCENTCUST := BUSCA_CCUSTO(P_FILIPROC, P_CDEMPRESA);
                     ELSE
                        V_CDCENTCUST := BUSCA_CCUSTO(V_CDFILIAL, P_CDEMPRESA);
                     END IF;
                     INSERT INTO RATECTBTEMP
                        (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                     VALUES
                        (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDFILIAL, V_CDCENTCUST,
                         ROUND(V_VRBAIXAREC * -1, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));

                     V_TOTALRATEIO := V_TOTALRATEIO + V_VRPARTIDA;

                  END IF;
               END IF;
            END IF;

            V_BOVENDAFOS := FALSE;

            V_CDFILIALBOL := 'XXXX';
            V_NRLANBOLREC := 'XXXXXXXXXX';

           /* FILTRA TODOS AS CONTAS A RECEBER RELACIONADAS AO BOLETO DA BAIXA*/
            FOR CONTAS IN
               (SELECT CT.CDFILIAL, CT.NRLANCTONF, CT.NRSEQVENREC, CT.NRPARCELAREC,
                       B.CDFILIAL AS CDFILBOL, B.NRLANBOLREC, B.CDCLIENTE, CT.CDTPCTRECEB, TP.CDCONTCTBL,
                       TP.CDHISTPADR, H.DSHISTPADR, NF.CDFILIEMIT, NF.NRNOTAFISC,
                       CT.VRATUAREC,  IDCTBTPCNTBREC, B.VRBOLREC, COUNT(*) OVER(PARTITION BY B.CDFILIAL, B.NRLANBOLREC) AS QTCONTAS,
                       TP.IDCTBTIPOOPERBX, T.CDCONTFORCLI
                  FROM BOLETRECEBER B, CONTARECEBER CT, NOTAFISCAL NF, TIPOCTRECEBER TP,
                       HISTPADR H, TIPOOPERAC T
                 WHERE B.CDFILIBAIREC = P_CDFILIAL
                   AND B.NRBAIXAREC = P_NRBAIXA
                   AND B.CDFILIAL = CT.CDFILIBOLREC
                   AND B.NRLANBOLREC = CT.NRLANBOLREC
                   AND CT.CDFILIAL = NF.CDFILIAL
                   AND CT.NRLANCTONF = NF.NRLANCTONF
                   AND CT.CDTPCTRECEB = TP.CDTPCTRECEB
                   AND TP.CDHISTPADR = H.CDHISTPADR(+)
                   AND NF.IDENTRSAIDOP = T.IDENTRSAIDOP
                   AND NF.CDTIPOOPER = T.CDTIPOOPER
                 ORDER BY B.CDFILIAL, B.NRLANBOLREC)LOOP
               IF CONTAS.VRATUAREC <> 0 THEN
                  /*BLOCO PARA GERAR CONTABILIZACAO NA CONTA TRANSITORIA  CASO O PARAMETRO ESTEJA MARCADO*/
                  BEGIN
                     SELECT CDCONTTRANSIT, IDCONTTRANSIT
                       INTO V_CONTATRANSFIL, V_IDCONTATRANSFIL
                       FROM FILIAL
                      WHERE CDFILIAL = CONTAS.CDFILIAL;
                  EXCEPTION
                       WHEN NO_DATA_FOUND THEN
                          V_CONTATRANSFIL   := NULL;
                          V_IDCONTATRANSFIL := 'N';
                  END;
                  IF V_IDCONTATRANSFIL = 'S' THEN

                     V_CDCONTCTBL := V_CONTATRANSFIL;
                     BUSCA_TPCOMERCIAL(V_CDCONTCTBL, V_IDCOME, V_IDCCUS);
                     IF V_IDCOME <> 'C' THEN
                        V_IDCOME  := 'N';
                        V_CODCOMEAUX := NULL;
                     ELSE
                        V_CODCOMEAUX  := CONTAS.CDCLIENTE;
                     END IF;
                     V_DSHISTPADR := CTB_HIST_REC(CONTAS.CDHISTPADR, CONTAS.CDFILIAL, CONTAS.NRLANCTONF, CONTAS.NRSEQVENREC, CONTAS.NRPARCELAREC, '');
                     /*ROTINA PARA RATEAR OS VALORES DOS AJUSTES QUE NAO CONTABILIZAM */
                     IF (V_CDFILIALBOL <> CONTAS.CDFILBOL) OR
                        (V_NRLANBOLREC <> CONTAS.NRLANBOLREC) THEN
                        BEGIN
                           SELECT SUM(DECODE(TA.IDAJUCTREC, 'D', AJ.VRAJBOLREC * -1, AJ.VRAJBOLREC)) AS VRAJBOLPAG
                             INTO V_VRTOTALAJUSNCTB
                             FROM AJBOLREC AJ, TIPOAJRECEBER TA
                            WHERE AJ.CDFILIAL = CONTAS.CDFILBOL
                              AND AJ.NRLANBOLREC = CONTAS.NRLANBOLREC
                              AND AJ.CDAJUCTREC = TA.CDAJUCTREC
                              AND TA.IDCTBAJCTREC = 'N';
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_VRTOTALAJUSNCTB := 0;
                        END;
                        IF V_VRTOTALAJUSNCTB IS NULL THEN
                           V_VRTOTALAJUSNCTB := 0;
                        END IF;
                        V_NRCONTABOL := 1;
                        V_VRSOMAAJUSNCTB := 0;
                        V_VRAJUSNCTB := 0;
                        V_CDFILIALBOL := CONTAS.CDFILBOL;
                        V_NRLANBOLREC := CONTAS.NRLANBOLREC;
                     ELSE
                        V_NRCONTABOL := V_NRCONTABOL +1;
                     END IF;

                     IF V_VRTOTALAJUSNCTB <> 0 THEN
                        V_VRAJUSNCTB := (V_VRTOTALAJUSNCTB * 100) / CONTAS.VRBOLREC;
                        V_VRAJUSNCTB := ROUND((V_VRAJUSNCTB * CONTAS.VRATUAREC) / 100, 2);

                        V_VRSOMAAJUSNCTB := V_VRSOMAAJUSNCTB + V_VRAJUSNCTB;

                        IF V_NRCONTABOL = CONTAS.QTCONTAS THEN
                           IF V_VRTOTALAJUSNCTB <> V_VRSOMAAJUSNCTB THEN
                              V_VRAJUSNCTB := V_VRAJUSNCTB + (V_VRTOTALAJUSNCTB - V_VRSOMAAJUSNCTB);
                           END IF;
                        END IF;

                     END IF;

                     IF V_VRAJUSNCTB IS NULL THEN
                        V_VRAJUSNCTB := 0;
                     END IF;

                     V_VRPARTIDA := ROUND(CONTAS.VRATUAREC + V_VRAJUSNCTB, 2);
                     IF NVL(V_VRPARTIDA, 0) <> 0 THEN
                        V_TOTCREDITO :=  V_TOTCREDITO + ROUND(V_VRPARTIDA, 2);
                        V_SEQPARTIDA := V_SEQPARTIDA + 1;
                        V_NRSEQUENCIAL := LPAD(TO_CHAR(V_SEQPARTIDA),5,'0');
                        INSERT INTO LANCCTBTEMP
                           (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                            DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
                        VALUES
                           (P_CDEMPRESA, V_NRLANCTEMP, V_DOCUMENTO, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDCONTCTBL, V_CDHISTPADR,
                            V_DSHISTPADR, V_DTBAIXA, ROUND(V_VRPARTIDA, 2), V_IDCOME, V_CODCOMEAUX, 'REC');
                        /*PARTIDA DE DEBITO*/
                        V_TOTDEBITO := V_TOTDEBITO + ROUND(V_VRPARTIDA * -1, 2);
                        V_SEQPARTIDA := V_SEQPARTIDA + 1;
                        V_NRSEQUENCIAL := LPAD(TO_CHAR(V_SEQPARTIDA),5,'0');
                        BEGIN
                           SELECT CDCONTTRANSIT, IDCONTTRANSIT
                             INTO V_CONTATRANSFIL, V_IDCONTATRANSFIL
                             FROM FILIAL
                            WHERE CDFILIAL = CONTAS.CDFILIEMIT;
                        EXCEPTION
                             WHEN NO_DATA_FOUND THEN
                                V_CONTATRANSFIL   := NULL;
                                V_IDCONTATRANSFIL := 'N';
                        END;
                        V_CDCONTCTBL := V_CONTATRANSFIL;
                        INSERT INTO LANCCTBTEMP
                           (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                            DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
                        VALUES
                           (P_CDEMPRESA, V_NRLANCTEMP, V_DOCUMENTO, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDCONTCTBL, V_CDHISTPADR,
                            V_DSHISTPADR, V_DTBAIXA, ROUND(V_VRPARTIDA*-1, 2), V_IDCOME, V_CODCOMEAUX, 'REC');
                     END IF;

                     /*RATEIO PARTIDA DE CREDITO*/
                     V_RATSEQ := 0;
                     IF V_CONTRAT = 'S' THEN
                        IF V_IDCCUS <> 'O' THEN
                           V_TOTALRATEIO := 0;
                           IF CONTAS.IDCTBTPCNTBREC <> 'S' THEN
                              V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIEMIT, P_CDEMPRESA);
                              INSERT INTO RATECTBTEMP
                                 (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                              VALUES
                                 (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), CONTAS.CDFILIEMIT, V_CDCENTCUST,
                                  ROUND(V_VRPARTIDA, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                              V_TOTALRATEIO := V_TOTALRATEIO + V_VRPARTIDA;
                           ELSE
                              V_ENTROULOOP := 'N';
                              FOR C_RATEIO IN
                                 (SELECT CDCENTCUST, CDFIRATCTREC, VRRATCCCTREC
                                    FROM RATCTRECEBER
                                   WHERE CDFILIAL = CONTAS.CDFILIAL
                                     AND NRLANCTONF = CONTAS.NRLANCTONF
                                     AND NRSEQVENREC = CONTAS.NRSEQVENREC
                                     AND NRPARCELAREC = CONTAS.NRPARCELAREC) LOOP

                                 V_ENTROULOOP := 'S';
                                 V_RATSEQ := V_RATSEQ + 1;
                                 INSERT INTO RATECTBTEMP
                                    (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                 VALUES
                                    (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), C_RATEIO.CDFIRATCTREC, C_RATEIO.CDCENTCUST,
                                     ROUND(C_RATEIO.VRRATCCCTREC, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                 V_TOTALRATEIO := V_TOTALRATEIO +  ROUND(C_RATEIO.VRRATCCCTREC, 2);
                                 V_CDFILIAL   := C_RATEIO.CDFIRATCTREC;
                                 V_CDCENTCUST := C_RATEIO.CDCENTCUST;
                              END LOOP;

                              IF V_ENTROULOOP = 'N' THEN
                                 V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIEMIT, P_CDEMPRESA);
                                 V_RATSEQ := V_RATSEQ + 1;
                                 INSERT INTO RATECTBTEMP
                                    (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                 VALUES
                                    (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), CONTAS.CDFILIEMIT, V_CDCENTCUST,
                                     ROUND(V_VRPARTIDA, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                 V_TOTALRATEIO := V_TOTALRATEIO + ROUND(CONTAS.VRATUAREC, 2);
                                 V_CDFILIAL := CONTAS.CDFILIEMIT;
                                 V_CDCENTCUST := V_CDCENTCUST;
                              END IF;

                              IF V_TOTALRATEIO <> V_VRPARTIDA THEN
                                 V_VRPARTIDA := V_TOTALRATEIO - V_VRPARTIDA;
                                 V_RATSEQ := V_RATSEQ + 1;
                                 INSERT INTO RATECTBTEMP
                                    (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                       VALUES
                                    (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDFILIAL, V_CDCENTCUST,
                                     V_TOTALRATEIO, V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                              END IF;
                           END IF;
                        END IF;
                     END IF;

                     /*RATEIO PARTIDA DE DEBITO*/
                     IF V_CONTRAT = 'S' THEN
                        IF V_IDCCUS <> 'O' THEN
                           V_TOTALRATEIO := 0;
                           IF CONTAS.IDCTBTPCNTBREC <> 'S' THEN
                              V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIEMIT, P_CDEMPRESA);
                              INSERT INTO RATECTBTEMP
                                 (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                              VALUES
                                 (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), CONTAS.CDFILIEMIT, V_CDCENTCUST,
                                  ROUND(V_VRPARTIDA*-1, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                              V_TOTALRATEIO := V_TOTALRATEIO + V_VRPARTIDA*-1;
                           ELSE
                              V_ENTROULOOP := 'N';
                              FOR C_RATEIO IN
                                 (SELECT CDCENTCUST, CDFIRATCTREC, VRRATCCCTREC
                                    FROM RATCTRECEBER
                                   WHERE CDFILIAL = CONTAS.CDFILIAL
                                     AND NRLANCTONF = CONTAS.NRLANCTONF
                                     AND NRSEQVENREC = CONTAS.NRSEQVENREC
                                     AND NRPARCELAREC = CONTAS.NRPARCELAREC) LOOP

                                 V_ENTROULOOP := 'S';
                                 V_RATSEQ := V_RATSEQ + 1;
                                 INSERT INTO RATECTBTEMP
                                    (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                 VALUES
                                    (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), C_RATEIO.CDFIRATCTREC, C_RATEIO.CDCENTCUST,
                                     ROUND(C_RATEIO.VRRATCCCTREC*-1, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                 V_TOTALRATEIO := V_TOTALRATEIO +  ROUND(C_RATEIO.VRRATCCCTREC*-1, 2);
                                 V_CDFILIAL   := C_RATEIO.CDFIRATCTREC;
                                 V_CDCENTCUST := C_RATEIO.CDCENTCUST;
                              END LOOP;

                              IF V_ENTROULOOP = 'N' THEN
                                 V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIEMIT, P_CDEMPRESA);
                                 V_RATSEQ := V_RATSEQ + 1;
                                 INSERT INTO RATECTBTEMP
                                    (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                 VALUES
                                    (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), CONTAS.CDFILIEMIT, V_CDCENTCUST,
                                     ROUND(V_VRPARTIDA*-1, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                 V_TOTALRATEIO := V_TOTALRATEIO + ROUND(CONTAS.VRATUAREC*-1, 2);
                                 V_CDFILIAL := CONTAS.CDFILIEMIT;
                                 V_CDCENTCUST := V_CDCENTCUST;
                              END IF;

                              IF V_TOTALRATEIO <> (V_VRPARTIDA) THEN
                                 V_VRPARTIDA := V_TOTALRATEIO - V_VRPARTIDA;
                                 V_RATSEQ := V_RATSEQ + 1;
                                 INSERT INTO RATECTBTEMP
                                    (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                       VALUES
                                    (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDFILIAL, V_CDCENTCUST,
                                     V_TOTALRATEIO*-1, V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                              END IF;
                           END IF;
                        END IF;
                     END IF;

                  END IF;
                  BEGIN
                     SELECT COUNT(*)
                       INTO V_COUNT
                       FROM MOVCAIXA
                      WHERE CDFILIALNF = CONTAS.CDFILIAL
                        AND NRLANCTONF = CONTAS.NRLANCTONF;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_COUNT := 0;
                  END;

                  IF V_COUNT > 0 THEN
                     V_BOVENDAFOS := TRUE;
                  END IF;

                  IF ((CONTAS.IDCTBTPCNTBREC = 'S') OR (V_BOVENDAFOS)) THEN
                     V_CDCONTCTBL := CONTAS.CDCONTCTBL;
                     V_CDHISTPADR := CONTAS.CDHISTPADR;
                     IF V_CDHISTPADR IS NULL THEN
                        V_DSHISTPADR := V_DSHISTINT;
                        V_CDHISTPADR := V_CDHISTINT;
                     END IF;
                  ELSE
                     V_CDCONTCTBL := NULL;
                     IF CONTAS.IDCTBTIPOOPERBX = 'S' THEN
                        V_CDCONTCTBL := CONTAS.CDCONTFORCLI;
                     END IF;
                     IF V_CDCONTCTBL IS NULL THEN
                        V_CDCONTCTBL := CTB_CCCLIENTE(CONTAS.CDCLIENTE, CONTAS.CDFILIEMIT, P_CDEMPRESA);
                     END IF;
                  END IF;

                  V_DSHISTPADR := CTB_HIST_REC(V_CDHISTPADR, CONTAS.CDFILIAL, CONTAS.NRLANCTONF, CONTAS.NRSEQVENREC, CONTAS.NRPARCELAREC, '');

                  V_SEQPARTIDA := V_SEQPARTIDA + 1;

                  BUSCA_TPCOMERCIAL(V_CDCONTCTBL, V_IDCOME, V_IDCCUS);
                  IF V_IDCOME <> 'C' THEN
                     V_IDCOME  := 'N';
                     V_CODCOMEAUX := NULL;
                  ELSE
                     V_CODCOMEAUX  := CONTAS.CDCLIENTE;
                  END IF;

                  /*ROTINA PARA RATEAR OS VALORES DOS AJUSTES QUE NAO CONTABILIZAM */
                  IF (V_CDFILIALBOL <> CONTAS.CDFILBOL) OR
                     (V_NRLANBOLREC <> CONTAS.NRLANBOLREC) THEN
                     BEGIN
                        SELECT SUM(DECODE(TA.IDAJUCTREC, 'D', AJ.VRAJBOLREC * -1, AJ.VRAJBOLREC)) AS VRAJBOLPAG
                          INTO V_VRTOTALAJUSNCTB
                          FROM AJBOLREC AJ, TIPOAJRECEBER TA
                         WHERE AJ.CDFILIAL = CONTAS.CDFILBOL
                           AND AJ.NRLANBOLREC = CONTAS.NRLANBOLREC
                           AND AJ.CDAJUCTREC = TA.CDAJUCTREC
                           AND TA.IDCTBAJCTREC = 'N';
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_VRTOTALAJUSNCTB := 0;
                     END;
                     IF V_VRTOTALAJUSNCTB IS NULL THEN
                        V_VRTOTALAJUSNCTB := 0;
                     END IF;
                     V_NRCONTABOL := 1;
                     V_VRSOMAAJUSNCTB := 0;
                     V_VRAJUSNCTB := 0;
                     V_CDFILIALBOL := CONTAS.CDFILBOL;
                     V_NRLANBOLREC := CONTAS.NRLANBOLREC;
                  ELSE
                     V_NRCONTABOL := V_NRCONTABOL +1;
                  END IF;

                  IF V_VRTOTALAJUSNCTB <> 0 THEN
                     V_VRAJUSNCTB := (V_VRTOTALAJUSNCTB * 100) / CONTAS.VRBOLREC;
                     V_VRAJUSNCTB := ROUND((V_VRAJUSNCTB * CONTAS.VRATUAREC) / 100, 2);

                     V_VRSOMAAJUSNCTB := V_VRSOMAAJUSNCTB + V_VRAJUSNCTB;

                     IF V_NRCONTABOL = CONTAS.QTCONTAS THEN
                        IF V_VRTOTALAJUSNCTB <> V_VRSOMAAJUSNCTB THEN
                           V_VRAJUSNCTB := V_VRAJUSNCTB + (V_VRTOTALAJUSNCTB - V_VRSOMAAJUSNCTB);
                        END IF;
                     END IF;

                  END IF;

                  IF V_VRAJUSNCTB IS NULL THEN
                     V_VRAJUSNCTB := 0;
                  END IF;

                  V_VRPARTIDA := ROUND(CONTAS.VRATUAREC + V_VRAJUSNCTB, 2);
                  IF NVL(V_VRPARTIDA, 0) <> 0 THEN
                     V_TOTCREDITO :=  V_TOTCREDITO + ROUND(V_VRPARTIDA, 2);
                     V_NRSEQUENCIAL := LPAD(TO_CHAR(V_SEQPARTIDA),5,'0');
                     INSERT INTO LANCCTBTEMP
                        (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                         DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
                     VALUES
                        (P_CDEMPRESA, V_NRLANCTEMP, V_DOCUMENTO, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDCONTCTBL, V_CDHISTPADR,
                         V_DSHISTPADR, V_DTBAIXA, ROUND(V_VRPARTIDA, 2), V_IDCOME, V_CODCOMEAUX, 'REC');

                     V_RATSEQ := 0;
                     IF V_CONTRAT = 'S' THEN
                        IF V_IDCCUS <> 'O' THEN
                           V_TOTALRATEIO := 0;
                           IF CONTAS.IDCTBTPCNTBREC <> 'S' THEN
                              V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIEMIT, P_CDEMPRESA);
                              INSERT INTO RATECTBTEMP
                                 (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                              VALUES
                                 (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), CONTAS.CDFILIEMIT, V_CDCENTCUST,
                                  ROUND(V_VRPARTIDA, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                              V_TOTALRATEIO := V_TOTALRATEIO + V_VRPARTIDA;
                           ELSE
                              V_ENTROULOOP := 'N';
                              FOR C_RATEIO IN
                                 (SELECT CDCENTCUST, CDFIRATCTREC, VRRATCCCTREC
                                    FROM RATCTRECEBER
                                   WHERE CDFILIAL = CONTAS.CDFILIAL
                                     AND NRLANCTONF = CONTAS.NRLANCTONF
                                     AND NRSEQVENREC = CONTAS.NRSEQVENREC
                                     AND NRPARCELAREC = CONTAS.NRPARCELAREC) LOOP

                                 V_ENTROULOOP := 'S';
                                 V_RATSEQ := V_RATSEQ + 1;
                                 INSERT INTO RATECTBTEMP
                                    (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                 VALUES
                                    (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), C_RATEIO.CDFIRATCTREC, C_RATEIO.CDCENTCUST,
                                     ROUND(C_RATEIO.VRRATCCCTREC, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                 V_TOTALRATEIO := V_TOTALRATEIO +  ROUND(C_RATEIO.VRRATCCCTREC, 2);
                                 V_CDFILIAL   := C_RATEIO.CDFIRATCTREC;
                                 V_CDCENTCUST := C_RATEIO.CDCENTCUST;
                              END LOOP;

                              IF V_ENTROULOOP = 'N' THEN
                                 V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIEMIT, P_CDEMPRESA);
                                 V_RATSEQ := V_RATSEQ + 1;
                                 INSERT INTO RATECTBTEMP
                                    (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                 VALUES
                                    (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), CONTAS.CDFILIEMIT, V_CDCENTCUST,
                                     ROUND(V_VRPARTIDA, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                 V_TOTALRATEIO := V_TOTALRATEIO + ROUND(CONTAS.VRATUAREC, 2);
                                 V_CDFILIAL := CONTAS.CDFILIEMIT;
                                 V_CDCENTCUST := V_CDCENTCUST;
                              END IF;

                              IF V_TOTALRATEIO <> V_VRPARTIDA THEN
                                 V_VRPARTIDA := V_TOTALRATEIO - V_VRPARTIDA;
                                 V_RATSEQ := V_RATSEQ + 1;
                                 INSERT INTO RATECTBTEMP
                                    (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                                 VALUES
                                    (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDFILIAL, V_CDCENTCUST,
                                     V_TOTALRATEIO, V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                              END IF;
                           END IF;
                        END IF;
                     END IF;
                  END IF;
               END IF;
            END LOOP;
         END IF;

         V_INPARTIDAS := 0;
         FOR AJUSTES IN
            (SELECT BO.NRLANBOLREC, BO.CDFILIBAIREC, NF.CDFILIEMIT, AJ.CDAJUCTREC,
                    BX.CDREDUCONT, BO.CDCLIENTE, AJ.VRAJBOLREC, IDAJUCTREC, BO.VRBOLREC,
                    CT.NRLANCTONF, CT.CDFILIAL, HP.CDHISTPADR, CT.NRSEQVENREC, CT.NRPARCELAREC,
                    CT.VRATUAREC, TP.CDCONTCTBL, COUNT(*) OVER (PARTITION BY BX.CDFILIAL, BX.NRBAIXAREC) RECORDCOUNT
               FROM BAIXARECEBIDA BX, BOLETRECEBER BO, AJBOLREC AJ, CONTARECEBER CT,
                    NOTAFISCAL NF, TIPOAJRECEBER TP, HISTPADR HP
              WHERE BX.CDFILIAL = P_CDFILIAL
                AND BX.NRBAIXAREC = P_NRBAIXA
                AND BX.CDFILIAL = BO.CDFILIBAIREC
                AND BX.NRBAIXAREC = BO.NRBAIXAREC
                AND BO.CDFILIAL = AJ.CDFILIAL
                AND BO.NRLANBOLREC = AJ.NRLANBOLREC
                AND BO.CDFILIAL = CT.CDFILIBOLREC
                AND BO.NRLANBOLREC = CT.NRLANBOLREC
                AND CT.CDFILIAL = NF.CDFILIAL
                AND CT.NRLANCTONF = NF.NRLANCTONF
                AND AJ.CDAJUCTREC = TP.CDAJUCTREC
                AND TP.IDCTBAJCTREC = 'S'
                AND HP.CDHISTPADR = TP.CDHISTPADR(+))LOOP
            V_INPARTIDAS := V_INPARTIDAS + 1;
            IF AJUSTES.IDAJUCTREC = 'D' THEN
               V_FATOR := -1;
            ELSE
               V_FATOR := 1;
            END IF;

            V_DSHISTPADR := CTB_HIST_REC(AJUSTES.CDHISTPADR, AJUSTES.CDFILIAL, AJUSTES.NRLANCTONF, AJUSTES.NRSEQVENREC, AJUSTES.NRPARCELAREC, AJUSTES.CDAJUCTREC);

            BEGIN
               SELECT CDCENTCUSTCLI
                 INTO V_CDCENTCUST
                 FROM CLIENTE
                WHERE CDCLIENTE =  AJUSTES.CDCLIENTE;
            EXCEPTION
                 WHEN NO_DATA_FOUND THEN
                 V_CDCENTCUST := NULL;
            END;
            IF V_CDCENTCUST IS NULL THEN
             V_CDCENTCUST := BUSCA_CCUSTO(AJUSTES.CDFILIEMIT, P_CDEMPRESA);
            END IF;
            V_CDCONTCTBL := AJUSTES.CDCONTCTBL;
            BUSCA_TPCOMERCIAL(V_CDCONTCTBL, V_IDCOME, V_IDCCUS);
            IF V_IDCOME = 'B' THEN
               V_CODCOMEAUX := AJUSTES.CDREDUCONT;
            ELSIF V_IDCOME = 'C' THEN
               V_CODCOMEAUX := AJUSTES.CDCLIENTE;
            ELSE
               V_IDCOME := 'N';
               V_CODCOMEAUX := NULL;
            END IF;

            IF V_SEQPARTIDA IS NULL THEN
               V_SEQPARTIDA := 0;
            END IF;



            V_VRAJUSTE := ROUND((NVL(AJUSTES.VRATUAREC, 0)  * 100) / AJUSTES.VRBOLREC, 2);
            V_VRAJUSTE := ROUND((AJUSTES.VRAJBOLREC * NVL(V_VRAJUSTE, 0)) / 100, 2);

            IF NVL(V_VRAJUSTE, 0) <> 0 THEN
               IF ROUND(V_VRAJUSTE * V_FATOR, 2) > 0 THEN
                  V_TOTCREDITO := V_TOTCREDITO + ROUND(V_VRAJUSTE * V_FATOR, 2);
               ELSE
                  V_TOTDEBITO := V_TOTDEBITO + ROUND(V_VRAJUSTE * V_FATOR, 2);
               END IF;

               V_SEQPARTIDA := V_SEQPARTIDA + 1;
               V_NRSEQUENCIAL := LPAD(TO_CHAR(V_SEQPARTIDA),5,'0');
               INSERT INTO LANCCTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
                   DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
               VALUES
                  (P_CDEMPRESA, V_NRLANCTEMP, V_DOCUMENTO, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), V_CDCONTCTBL, V_CDHISTPADR,
                  V_DSHISTPADR,  V_DTBAIXA, ROUND(V_VRAJUSTE * V_FATOR, 2), V_IDCOME, V_CODCOMEAUX, 'REC');

               V_TOTAJUSTE := V_VRAJUSTE * V_FATOR;

               IF V_CONTRAT = 'S' THEN
                  IF V_IDCCUS <> 'O' THEN
                     V_TOTRATAJ := 0;
                     V_RATSEQ := 0;
                     BEGIN
                        SELECT COUNT(*)
                          INTO V_COUNT
                          FROM RATTPAJR
                         WHERE CDAJUCTREC = AJUSTES.CDAJUCTREC;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_COUNT := 0;
                     END;
                     IF V_COUNT <> 0 THEN
                        FOR RATAJ IN(
                           SELECT CDCENTCUST, CDFIRATTPAJR, VRPERATTPAJR
                             FROM RATTPAJR
                            WHERE CDAJUCTREC = AJUSTES.CDAJUCTREC)LOOP

                           V_VRAJUSTE := ROUND((V_TOTAJUSTE * RATAJ.VRPERATTPAJR) / 100, 2);

                           V_RATSEQ := V_RATSEQ + 1;
                           IF RATAJ.CDFIRATTPAJR = '----' THEN
                              V_CDFILIAL := AJUSTES.CDFILIEMIT;
                           ELSE
                              V_CDFILIAL := RATAJ.CDFIRATTPAJR;
                           END IF;
                           INSERT INTO RATECTBTEMP
                              (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP,
                               CDFILIAL, CDCENTCUST,
                               VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                           VALUES
                              (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'),
                               V_CDFILIAL, RATAJ.CDCENTCUST,
                               ROUND(V_VRAJUSTE, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                           V_TOTRATAJ := V_TOTRATAJ + V_VRAJUSTE;
                           V_CDCENTCUST := RATAJ.CDCENTCUST;

                        END LOOP;
                     ELSE
                        V_RATSEQ := V_RATSEQ + 1;
                        IF V_CDCENTCUST IS NULL THEN
                           V_CDCENTCUST := BUSCA_CCUSTO(AJUSTES.CDFILIEMIT, P_CDEMPRESA);
                        END IF;
                        INSERT INTO RATECTBTEMP
                           (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
                        VALUES
                           (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), AJUSTES.CDFILIEMIT, V_CDCENTCUST,
                            ROUND(V_VRAJUSTE * V_FATOR, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));

                        V_TOTRATAJ := V_TOTRATAJ + (V_VRAJUSTE * V_FATOR);
                        V_CDFILIAL := AJUSTES.CDFILIEMIT;
                        V_CDCENTCUST := V_CDCENTCUST;
                     END IF;

                     IF V_TOTRATAJ <> V_TOTAJUSTE THEN
                        IF V_TOTAJUSTE < V_TOTRATAJ THEN
                           V_TOTRATAJ := V_TOTRATAJ  - V_TOTAJUSTE;
                        ELSE
                           V_TOTRATAJ := V_TOTAJUSTE - V_TOTRATAJ;
                        END IF;

                        UPDATE RATECTBTEMP
                           SET VRRATECTBTEMP = VRRATECTBTEMP + V_TOTRATAJ
                         WHERE NRSEQCTBTEMP = V_SEQPARTIDA
                           AND CDFILIAL = V_CDFILIAL
                           AND CDCENTCUST = V_CDCENTCUST
                           AND NRLANCCTBTEMP = V_NRLANCTEMP
                           AND CDEMPRESA = P_CDEMPRESA;
                     END IF;
                  END IF;
               END IF;
            END IF;
            IF V_INPARTIDAS = AJUSTES.RECORDCOUNT THEN
               IF ABS(V_TOTCREDITO) <> ABS(V_TOTDEBITO) THEN
                  V_DIFERENCA := ABS(V_TOTCREDITO) + V_TOTDEBITO;
                  UPDATE LANCCTBTEMP
                     SET VRLANCCTBTEMP = VRLANCCTBTEMP - V_DIFERENCA
                   WHERE NRLANCCTBTEMP = V_NRLANCTEMP
                     AND NRSEQCTBTEMP = V_NRSEQUENCIAL
                     AND CDEMPRESA = P_CDEMPRESA;
                   IF V_CONTRAT = 'S' THEN
                      IF V_IDCCUS <> 'O' THEN
                         UPDATE RATECTBTEMP
                            SET VRRATECTBTEMP = VRRATECTBTEMP - V_DIFERENCA
                          WHERE CDEMPRESA = P_CDEMPRESA
                            AND NRLANCCTBTEMP = V_NRLANCTEMP
                            AND NRSEQCTBTEMP = V_NRSEQUENCIAL
                            AND CDFILIAL = V_CDFILIAL
                            AND CDCENTCUST = V_CDCENTCUST;
                      END IF;
                   END IF;
               END IF;
            END IF;
         END LOOP;
      END IF;

      /*VERIFICACAO DE ERROS NAS PARTIDAS E NO LANCAMENTO CONTABIL*/
      VERIFICA_ERRO_CTB(V_NRLANCTEMP, P_CDEMPRESA);
      BEGIN
         SELECT COUNT(*)
           INTO V_COUNT
           FROM ERROCTBTEMP
          WHERE NRLANCCTBTEMP = V_NRLANCTEMP
            AND CDEMPRESA = P_CDEMPRESA;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_COUNT := 0;
      END;

      IF V_COUNT = 0 THEN
         UPDATE BAIXARECEBIDA
            SET NRLCTBBREC = V_NRLANCTEMP
          WHERE NRBAIXAREC = P_NRBAIXA
            AND CDFILIAL   = P_CDFILIAL;
      ELSE
         V_NRLANCTEMPERRO := 'N'||V_NRLANCTEMP;
      END IF;

      GRAVA_LANC_CTB(V_NRLANCTEMP, P_CDEMPRESA);
      P_NRLANCCTB := NVL(V_NRLANCTEMPERRO,V_NRLANCTEMP);
   END IF;
END;
/