CREATE OR REPLACE PROCEDURE CTB_BAIXA_PAG(
   P_CDEMPRESA     IN EMPRESA.CDEMPRESA%TYPE,
   P_CDFILIAL      IN FILIAL.CDFILIAL%TYPE,
   P_NRBAIXA       IN BAIXAPAGA.NRBAIXAPAG%TYPE,
   P_NRLANCCTB    OUT LANCCONT.NRLANCCONT%TYPE)
IS
   V_NRLANCTEMP  VARCHAR2(20);
   V_DTBAIXA     DATE;
   V_CDREDUCONT  CONTCORR.CDREDUCONT%TYPE;
   V_CDTPBAIXA   BAIXAPAGA.CDTPBAIXAPAG%TYPE;
   V_VRBAIXAPAG  BAIXAPAGA.VRBAIXAPAG%TYPE;
   V_CDCONTCTBL  CONTCTBL.CDCONTCTBL%TYPE;
   V_DOCUMENTO   BAIXAPAGA.NRBAIXAPAG%TYPE;
   V_CONTABILIZA VARCHAR2(01);
   V_IDBAIXAUTILP VARCHAR2(01);
   V_NMTPBAIXAPAG TIPOBAIXAPAG.NMTPBAIXAPAG%TYPE;
   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;
   V_VRCONTA     LANCCONT.VRLANCAMENT%TYPE;
   V_BOCTBAJUSTE BOOLEAN;
   V_CONTRAT     VARCHAR2(01);
   V_CDCENTCUST  CENTCUST.CDCENTCUST%TYPE;
   V_CDCENTCUST_OLD CENTCUST.CDCENTCUST%TYPE;
   V_RATSEQ      INTEGER;
   V_COUNT       INTEGER;
   V_COUNTBOLASS INTEGER;
   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;
   V_CDHISTLANC HISTPADR.CDHISTPADR%TYPE;
   V_DSHISTLANC LANCCONT.DSHISTLANC%TYPE;
   V_VRTOTALAJUSNCTB LANCCONT.VRLANCAMENT%TYPE;
   V_VRAJUSNCTB      LANCCONT.VRLANCAMENT%TYPE;
   V_VRSOMAAJUSNCTB  LANCCONT.VRLANCAMENT%TYPE;
   V_CDFILIALBOL BOLETPAGAR.CDFILIAL%TYPE;
   V_NRLANBOLPAG BOLETPAGAR.NRLANBOLPAG%TYPE;
   V_NRCONTABOL NUMBER;
   V_VRPARTIDA LANCCONT.VRLANCAMENT%TYPE;
   V_CONTAJUST INTEGER;
   V_TOTCREDITO LANCCONT.VRLANCAMENT%TYPE;
   V_TOTDEBITO LANCCONT.VRLANCAMENT%TYPE;
   V_NRSEQUENCIAL VARCHAR2(05);
   V_INPARTIDAS INT;
   V_DIFERENCA NUMBER;
   V_NRCHEQUE VARCHAR2(06);
   V_NRSEQUENCIALTEMP VARCHAR2(05);
   V_VRRATEIO NUMBER;
   V_RESTORAT NUMBER;
   V_ENTROUAJUSTE VARCHAR2(01);
   V_PARTIDABOLETO VARCHAR2(01);
   V_IDCTBBXPLAN VARCHAR2(01);
   V_CDCONTCTBLCHQ VARCHAR2(20);
   V_DSBAIXAPAG   BAIXAPAGA.DSBAIXAPAG%TYPE;
   V_CONTATRANSFIL VARCHAR2(20);
   V_IDCONTATRANSFIL VARCHAR2(01);
   V_CDFILIALBPART   VARCHAR2(04);
   V_NRLANCTONFBPART VARCHAR2(10);
   V_NRSEQVENPAGBPART VARCHAR2(02);
   V_NRPARCELAPAGBPART 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 
   BEGIN 
      SELECT DTBAIXAPAG, CDREDUCONT, BX.CDTPBAIXAPAG, VRBAIXAPAG, NRBAIXAPAG, 
             IDSTATUSBPAG, IDCTBLBXPAG, T.IDBAIXAUTILP, T.NMTPBAIXAPAG, BX.NRCHEQUE, 
             BX.NRLCTBBPAG, NVL(T.IDCTBBXPLAN,'N') IDCTBBXPLAN, T.CDCONTCTBLCHTR,
             DSBAIXAPAG
        INTO V_DTBAIXA, V_CDREDUCONT, V_CDTPBAIXA, V_VRBAIXAPAG, V_DOCUMENTO, 
             V_STATUS, V_CONTABILIZA, V_IDBAIXAUTILP, V_NMTPBAIXAPAG, V_NRCHEQUE, 
             V_NRLANCTEMP, V_IDCTBBXPLAN, V_CDCONTCTBLCHQ, V_DSBAIXAPAG
        FROM BAIXAPAGA BX, TIPOBAIXAPAG T 
       WHERE BX.CDFILIAL = P_CDFILIAL 
         AND BX.CDEMPRESA = P_CDEMPRESA 
         AND BX.NRBAIXAPAG = P_NRBAIXA 
         AND T.CDTPBAIXAPAG = BX.CDTPBAIXAPAG; 
   EXCEPTION 
      WHEN NO_DATA_FOUND THEN 
         V_DTBAIXA    := NULL; 
         V_CDREDUCONT := NULL; 
         V_CDTPBAIXA  := NULL; 
         V_VRBAIXAPAG := NULL; 
         V_DOCUMENTO  := NULL; 
         V_STATUS     := NULL; 
         V_IDBAIXAUTILP := NULL; 
         V_CONTABILIZA := NULL; 
         V_NMTPBAIXAPAG := NULL; 
         V_IDCTBBXPLAN := NULL; 
         V_CDCONTCTBLCHQ := NULL; 
   END; 
 
   BEGIN  
      SELECT COUNT(*)  
        INTO V_COUNT  
        FROM PARAMCTBL  
       WHERE V_DTBAIXA >= DTINTCTBPAG;  
   EXCEPTION  
      WHEN NO_DATA_FOUND THEN  
         V_COUNT := 0;  
   END;
   /*BUSCA O CENTRO DE CUSTO DO LANCAMENTO EM CASO DE RECALCULO O MESMO SERA RECUPERADO CASO NAO EXISTA CENTRO DE CUSTO PARAMETRIZADO PARA A FILIAL/EMPRESA*/
   BEGIN
     SELECT MAX(CDCENTCUST)
       INTO V_CDCENTCUST_OLD
       FROM LANCCCUSTO  
      WHERE NRLANCCONT = V_NRLANCTEMP  
        AND CDEMPRESA  = P_CDEMPRESA;  
    
   
   EXCEPTION
   WHEN NO_DATA_FOUND THEN
      V_CDCENTCUST_OLD := NULL;
   END;
   IF V_NRLANCTEMP IS NOT NULL THEN  
      DELETE  
        FROM LANCCCUSTO  
       WHERE NRLANCCONT = V_NRLANCTEMP  
         AND CDEMPRESA  = P_CDEMPRESA;  
  
      DELETE 
        FROM LANCCONT  
       WHERE NRLANCCONT = V_NRLANCTEMP  
         AND CDEMPRESA  = P_CDEMPRESA;  
  
      DELETE 
        FROM LANCCTBTEMP  
       WHERE NRLANCCTBTEMP = V_NRLANCTEMP  
         AND CDEMPRESA = P_CDEMPRESA;  
  
      DELETE  
        FROM RATECTBTEMP  
       WHERE NRLANCCTBTEMP = V_NRLANCTEMP  
         AND CDEMPRESA = P_CDEMPRESA;  
   END IF;  
  V_NRLANCTEMP := NULL;
   IF V_COUNT > 0 THEN  
      BEGIN  
         SELECT COUNT(*)  
           INTO V_CONTAJUST  
           FROM BOLETPAGAR BO, AJBOLPAG AJ, TIPOAJPAGAR TP  
          WHERE BO.CDFILIBAIPAG = P_CDFILIAL 
            AND BO.NRBAIXAPAG = P_NRBAIXA  
            AND BO.NRLANBOLPAG = AJ.NRLANBOLPAG  
            AND BO.CDFILIAL = AJ.CDFILIAL  
            AND AJ.CDAJUCTPAG = TP.CDAJUCTPAG  
            AND TP.IDCTBAJCTPAG = 'S';  
      EXCEPTION  
         WHEN NO_DATA_FOUND THEN  
            V_CONTAJUST := 0;  
      END; 
      IF ((V_STATUS <> 'P') AND (V_CONTABILIZA <> 'N')) THEN  
         IF V_NRLANCTEMP IS NULL THEN  
            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_VRBAIXAPAG <> 0 OR V_CONTAJUST <> 0) THEN  
            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 = 'BAIXAP';  
            EXCEPTION  
               WHEN NO_DATA_FOUND THEN  
                  V_CDHISTINT := NULL;  
                  V_DSHISTINT := NULL;  
            END;  
            BEGIN  
               SELECT T.CDHISTPADR, H.DSHISTPADR 
                 INTO V_CDHISTPADR, V_DSHISTPADR  
                 FROM TIPOBAIXAPAG T, HISTPADR H  
                WHERE T.CDTPBAIXAPAG = 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_VRBAIXAPAG <> 0 THEN  
               BEGIN  
                  SELECT CC.CDCONTCTBL  
                    INTO V_CDCONTCTBL  
                    FROM CONTCORR CC  
                   WHERE CC.CDREDUCONT = V_CDREDUCONT;  
               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;  
               IF V_IDBAIXAUTILP IN ('A', 'M') THEN 
                  V_DSHISTLANC := 'Cheque: ' || NVL(V_NRCHEQUE, '[#CHQ]');  
               ELSE  
                  IF V_DSBAIXAPAG IS NULL THEN
                    V_DSHISTLANC := 'PGTO ' ||  SUBSTR(V_NMTPBAIXAPAG, 1, 30);
                  ELSE
                    V_DSHISTLANC := 'PGTO ' ||  SUBSTR(V_DSBAIXAPAG, 1, 200);
                  END IF;
               END IF;

               /* CAMPO USADO PARA DETERMINAR SE A PARTIDA DE BANCO SERA ACUMULADA OU NAO */
               V_PARTIDABOLETO := 'N';
               BEGIN
                  SELECT IDPARTIDABOLPAG
                    INTO V_PARTIDABOLETO
                    FROM TIPOBAIXAPAG
                   WHERE CDTPBAIXAPAG = V_CDTPBAIXA;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_PARTIDABOLETO := 'N';
               END;
               IF ((V_PARTIDABOLETO = 'N') OR (V_IDCTBBXPLAN = 'S')) THEN
                  V_SEQPARTIDA := V_SEQPARTIDA + 1;
                  V_TOTCREDITO := V_TOTCREDITO + ROUND(V_VRBAIXAPAG, 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_DSHISTLANC, V_DTBAIXA, ROUND(V_VRBAIXAPAG, 2), V_IDCOME, V_CDREDUCONT, 'PAG'); 
                  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_CDREDUCONT; 
                        EXCEPTION 
                           WHEN NO_DATA_FOUND THEN 
                              V_CDFILIAL := NULL; 
                        END; 
                        
                        IF V_CDFILIAL IS NULL THEN
                           V_CDCENTCUST := BUSCA_CCUSTO(P_CDFILIAL, P_CDEMPRESA);
                           IF V_CDCENTCUST  IS NULL THEN
                              V_CDCENTCUST  := V_CDCENTCUST_OLD;
                           END IF;
                           V_CDFILIAL := P_CDFILIAL; 
                        ELSE 
                           IF V_CDCENTCUST IS NULL THEN
                            V_CDCENTCUST := BUSCA_CCUSTO(V_CDFILIAL, P_CDEMPRESA);
                              IF V_CDCENTCUST  IS NULL THEN
                                 V_CDCENTCUST  := V_CDCENTCUST_OLD;
                              END IF;
                           END IF;
                        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_VRBAIXAPAG, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0')); 
                     END IF; 
                  END IF; 
               ELSE 
                  FOR C_BOLBAIXA IN
                     (SELECT BO.VRFINALBPAG, BO.CDFILIAL, BO.NRLANBOLPAG
                        FROM BOLETPAGAR BO
                       WHERE BO.CDFILIBAIPAG = P_CDFILIAL
                         AND BO.NRBAIXAPAG = P_NRBAIXA
                         AND BO.VRFINALBPAG <> 0) LOOP
                       V_SEQPARTIDA := V_SEQPARTIDA + 1;
                       V_TOTCREDITO := V_TOTCREDITO + ROUND(C_BOLBAIXA.VRFINALBPAG, 2);
                       V_NRSEQUENCIAL := LPAD(TO_CHAR(V_SEQPARTIDA),5,'0');

                          SELECT  COUNT(*), MAX(CDHISTPADR), MAX(CDFILIAL) , MAX(NRLANCTONF), MAX(NRSEQVENPAG) , MAX(NRPARCELAPAG)
                            INTO V_COUNTBOLASS, V_CDHISTPADRBOL, V_CDFILIALBPART, V_NRLANCTONFBPART, V_NRSEQVENPAGBPART, V_NRPARCELAPAGBPART
                            FROM CONTAPAGAR C, TIPOCTPAGAR TP
                           WHERE C.CDFILIBPAG = C_BOLBAIXA.CDFILIAL
                             AND C.NRLANBOLPAG = C_BOLBAIXA.NRLANBOLPAG
                             AND C.CDTPCTPAGAR = TP.CDTPCTPAGAR;

                     IF V_COUNTBOLASS = 1 THEN
                        V_DSHISTPADR := CTB_HIST_PAG(V_CDHISTPADR, V_CDFILIALBPART, V_NRLANCTONFBPART, V_NRSEQVENPAGBPART , V_NRPARCELAPAGBPART, '');
                     ELSE
                        V_CDHISTPADRBOL := NULL;
                        V_DSHISTPADR := NULL; 
                     END IF;
                     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_CDHISTPADR,V_CDHISTPADR),
                         NVL(V_DSHISTPADR, V_DSHISTLANC), V_DTBAIXA, ROUND(C_BOLBAIXA.VRFINALBPAG, 2), V_IDCOME, V_CDREDUCONT, 'PAG'); 
                     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_CDREDUCONT; 
                           EXCEPTION 
                              WHEN NO_DATA_FOUND THEN 
                                 V_CDFILIAL := NULL; 
                           END; 
                           IF V_CDFILIAL IS NULL THEN
                              V_CDCENTCUST := BUSCA_CCUSTO(P_CDFILIAL, P_CDEMPRESA);
                              IF V_CDCENTCUST  IS NULL THEN
                                 V_CDCENTCUST  := V_CDCENTCUST_OLD;
                              END IF;
                              V_CDFILIAL := P_CDFILIAL;
                           ELSE
                              V_CDCENTCUST := BUSCA_CCUSTO(V_CDFILIAL, P_CDEMPRESA);
                              IF V_CDCENTCUST  IS NULL THEN
                                 V_CDCENTCUST  := V_CDCENTCUST_OLD;
                              END IF;
                           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(C_BOLBAIXA.VRFINALBPAG, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0')); 
                        END IF; 
                     END IF; 
                  END LOOP;
               END IF;
            END IF;
            IF (V_IDCTBBXPLAN = 'N') THEN
               V_CDFILIALBOL := 'XXXX';
               V_NRLANBOLPAG := 'XXXXXXXXXX';

               FOR CONTAS IN
                  (SELECT CT.CDFILIAL, CT.NRLANCTONF, CT.NRSEQVENPAG, CT.NRPARCELAPAG,
                          B.CDFILIAL AS CDFILBOL, B.NRLANBOLPAG, B.CDFORNECED, CT.CDTPCTPAGAR, TP.CDCONTCTBL,
                          TP.CDHISTPADR, H.DSHISTPADR, NF.CDFILIRECE, NF.NRNOTAFISC,
                          CT.VRATUAPAG,  IDCTBTPCNTBPAG, B.VRBOLPAG, COUNT(*) OVER(PARTITION BY B.CDFILIAL, B.NRLANBOLPAG) AS QTCONTAS,
                          TP.IDTPCTPAGAR, CT.CDEMPRESA, CT.VRORIGPAG, TP.IDCTBTIPOOPERBX,
                          T.CDCONTFORCLI
                     FROM BOLETPAGAR B, CONTAPAGAR CT, NOTAFISCAL NF, TIPOCTPAGAR TP,
                          HISTPADR H, TIPOOPERAC T
                    WHERE B.CDFILIBAIPAG = P_CDFILIAL
                      AND B.NRBAIXAPAG = P_NRBAIXA
                      AND B.CDFILIAL = CT.CDFILIBPAG
                      AND B.NRLANBOLPAG = CT.NRLANBOLPAG
                      AND CT.CDFILIAL = NF.CDFILIAL
                      AND CT.NRLANCTONF = NF.NRLANCTONF
                      AND CT.CDTPCTPAGAR = TP.CDTPCTPAGAR
                      AND TP.CDHISTPADR = H.CDHISTPADR(+)
                      AND NF.IDENTRSAIDOP = T.IDENTRSAIDOP
                      AND NF.CDTIPOOPER = T.CDTIPOOPER
                    ORDER BY B.CDFILIAL, B.NRLANBOLPAG)LOOP
                  IF CONTAS.VRATUAPAG <> 0 THEN
                  /*BLOCO PARA GERAR CONTABILIZACAO NA CONTA TRANSITORIA  CASO O PARAMETRO ESTEJA MARCADO*/
                     BEGIN
                        SELECT CDFILIAL
                          INTO V_CDFILIAL
                          FROM CONTCORR
                         WHERE CDREDUCONT = V_CDREDUCONT;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_CDFILIAL := NULL;
                     END;
                     BEGIN
                        SELECT CDCONTTRANSIT
                          INTO V_CONTATRANSFIL
                          FROM FILIAL
                         WHERE CDFILIAL = CONTAS.CDFILIAL;
                     EXCEPTION
                          WHEN NO_DATA_FOUND THEN
                             V_CONTATRANSFIL   := NULL;
                     END;
                     BEGIN
                        SELECT IDCONTTRANSIT
                          INTO V_IDCONTATRANSFIL
                          FROM FILIAL
                         WHERE CDFILIAL = V_CDFILIAL;
                     EXCEPTION
                          WHEN NO_DATA_FOUND THEN
                             V_IDCONTATRANSFIL := 'N';
                     END;
                     V_CDHISTLANC := V_CDHISTPADR;
                     IF NVL(V_IDCONTATRANSFIL,'N') = '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.CDFORNECED;
                        END IF;
                        V_DSHISTPADR := CTB_HIST_PAG(CONTAS.CDHISTPADR, CONTAS.CDFILIAL, CONTAS.NRLANCTONF, CONTAS.NRSEQVENPAG, CONTAS.NRPARCELAPAG, '');
                        /*ROTINA PARA RATEAR OS VALORES DOS AJUSTES QUE NAO CONTABILIZAM */
                        IF (V_CDFILIALBOL <> CONTAS.CDFILBOL) OR
                           (V_NRLANBOLPAG <> CONTAS.NRLANBOLPAG) THEN
                           BEGIN
                              SELECT SUM(DECODE(TA.IDAJUCTPAG, 'D', AJ.VRAJBOLPAG * -1, AJ.VRAJBOLPAG)) AS VRAJBOLPAG
                                INTO V_VRTOTALAJUSNCTB
                                FROM AJBOLPAG AJ, TIPOAJPAGAR TA
                               WHERE AJ.CDFILIAL = CONTAS.CDFILBOL
                                 AND AJ.NRLANBOLPAG = CONTAS.NRLANBOLPAG
                                 AND AJ.CDAJUCTPAG = TA.CDAJUCTPAG
                                 AND TA.IDCTBAJCTPAG = '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_NRLANBOLPAG := CONTAS.NRLANBOLPAG;
                        ELSE
                           V_NRCONTABOL := V_NRCONTABOL +1;
                        END IF;

                        IF V_VRTOTALAJUSNCTB <> 0 THEN
                           V_VRAJUSNCTB := (V_VRTOTALAJUSNCTB * 100) / CONTAS.VRBOLPAG;
                           V_VRAJUSNCTB := ROUND((V_VRAJUSNCTB * CONTAS.VRATUAPAG) / 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.VRATUAPAG + 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, 'PAG');
                           /*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
                                INTO V_CONTATRANSFIL
                                FROM FILIAL
                               WHERE CDFILIAL = V_CDFILIAL;
                           EXCEPTION
                                WHEN NO_DATA_FOUND THEN
                                   V_CONTATRANSFIL   := NULL;
                           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, 'PAG');
                        END IF;
                        /*RATEIO PARTIDA DE credito*/
                        V_RATSEQ := 0;
                        IF V_CONTRAT = 'S' THEN
                           IF V_IDCCUS <> 'O' THEN
                              V_TOTALRATEIO := 0;
                              IF CONTAS.IDCTBTPCNTBPAG <> 'S' THEN
                                 V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIRECE, 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.CDFILIRECE, 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, CDFIRATCTPAG, VRRATCCCTPAG
                                       FROM RATCTPAGAR
                                      WHERE CDFILIAL = CONTAS.CDFILIAL
                                        AND NRLANCTONF = CONTAS.NRLANCTONF
                                        AND NRSEQVENPAG = CONTAS.NRSEQVENPAG
                                        AND NRPARCELAPAG = CONTAS.NRPARCELAPAG
                                        AND ROUND(VRRATCCCTPAG,2) > 0) 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.CDFIRATCTPAG, C_RATEIO.CDCENTCUST,
                                        ROUND(C_RATEIO.VRRATCCCTPAG, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                    V_TOTALRATEIO := V_TOTALRATEIO +  ROUND(C_RATEIO.VRRATCCCTPAG, 2);
                                    V_CDFILIAL   := C_RATEIO.CDFIRATCTPAG;
                                    V_CDCENTCUST := C_RATEIO.CDCENTCUST;
                                 END LOOP;

                                 IF V_ENTROULOOP = 'N' THEN
                                    V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIRECE, 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.CDFILIRECE, V_CDCENTCUST,
                                        ROUND(V_VRPARTIDA, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                    V_TOTALRATEIO := V_TOTALRATEIO + ROUND(CONTAS.VRATUAPAG, 2);
                                    V_CDFILIAL := CONTAS.CDFILIRECE;
                                    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.IDCTBTPCNTBPAG <> 'S' THEN
                                 V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIRECE, 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.CDFILIRECE, 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, CDFIRATCTPAG, VRRATCCCTPAG
                                       FROM RATCTPAGAR
                                      WHERE CDFILIAL = CONTAS.CDFILIAL
                                        AND NRLANCTONF = CONTAS.NRLANCTONF
                                        AND NRSEQVENPAG = CONTAS.NRSEQVENPAG
                                        AND NRPARCELAPAG = CONTAS.NRPARCELAPAG
                                        AND ROUND(VRRATCCCTPAG,2)> 0 ) 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.CDFIRATCTPAG, C_RATEIO.CDCENTCUST,
                                        ROUND(C_RATEIO.VRRATCCCTPAG*-1, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                    V_TOTALRATEIO := V_TOTALRATEIO +  ROUND(C_RATEIO.VRRATCCCTPAG*-1, 2);
                                    V_CDFILIAL   := C_RATEIO.CDFIRATCTPAG;
                                    V_CDCENTCUST := C_RATEIO.CDCENTCUST;
                                 END LOOP;

                                 IF V_ENTROULOOP = 'N' THEN
                                    V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIRECE, 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.CDFILIRECE, V_CDCENTCUST,
                                        ROUND(V_VRPARTIDA*-1, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0'));
                                    V_TOTALRATEIO := V_TOTALRATEIO + ROUND(CONTAS.VRATUAPAG*-1, 2);
                                    V_CDFILIAL := CONTAS.CDFILIRECE;
                                    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;
                     IF CONTAS.IDTPCTPAGAR = '2' THEN
                        V_CDCONTCTBL := CONTAS.CDCONTCTBL;
                        V_CDHISTLANC := CONTAS.CDHISTPADR;
                        IF V_CDHISTLANC IS NULL THEN
                           BEGIN
                              SELECT H.CDHISTPADR, HP.DSHISTPADR
                                INTO V_CDHISTLANC, V_DSHISTLANC
                                FROM HISTINTEGRA H ,HISTPADR HP
                               WHERE H.CDHISTPADR = HP.CDHISTPADR
                                 AND CDOPERACAO = 'BXPADI';
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN
                                 V_CDHISTLANC := V_CDHISTPADR;
                                 V_DSHISTLANC := V_DSHISTPADR;
                           END;
                        END IF;
                        V_VRCONTA := CONTAS.VRATUAPAG * -1;
                        V_BOCTBAJUSTE := FALSE;
                     ELSE

                        IF CONTAS.IDCTBTPCNTBPAG = 'N' THEN

                           V_CDCONTCTBL := NULL;
                           IF CONTAS.IDCTBTIPOOPERBX = 'S' THEN
                              V_CDCONTCTBL := CONTAS.CDCONTFORCLI;

                           END IF;

                           IF V_CDCONTCTBL IS NULL THEN
                              V_CDCONTCTBL := CTB_CCFORNECEDOR(CONTAS.CDFORNECED, CONTAS.CDFILIRECE, CONTAS.CDEMPRESA);
                           END IF;
                           V_VRCONTA := CONTAS.VRATUAPAG * -1;
                           V_BOCTBAJUSTE := FALSE;
                        ELSE
                           IF CONTAS.CDHISTPADR IS NOT NULL THEN
                              V_CDHISTLANC := CONTAS.CDHISTPADR;
                           END IF;
                           IF CONTAS.IDCTBTIPOOPERBX = 'S' THEN
                              V_CDCONTCTBL := CONTAS.CDCONTFORCLI;
                               IF V_CDCONTCTBL IS NULL THEN
                                   V_CDCONTCTBL := CTB_CCFORNECEDOR(CONTAS.CDFORNECED, CONTAS.CDFILIRECE, CONTAS.CDEMPRESA);
                               END IF;
                            ELSE
                            V_CDCONTCTBL := CONTAS.CDCONTCTBL;
                           END IF;

                           V_VRCONTA := CONTAS.VRORIGPAG * -1;
                           V_BOCTBAJUSTE := TRUE;
                        END IF;
                     END IF;
                     IF (V_CDFILIALBOL <> CONTAS.CDFILBOL) OR
                        (V_NRLANBOLPAG <> CONTAS.NRLANBOLPAG) THEN 
                        BEGIN 
                           SELECT SUM(DECODE(TA.IDAJUCTPAG, 'D', AJ.VRAJBOLPAG * -1, AJ.VRAJBOLPAG)) AS VRAJBOLPAG 
                             INTO V_VRTOTALAJUSNCTB 
                             FROM AJBOLPAG AJ, TIPOAJPAGAR TA 
                            WHERE AJ.CDFILIAL = CONTAS.CDFILBOL 
                              AND AJ.NRLANBOLPAG = CONTAS.NRLANBOLPAG 
                              AND AJ.CDAJUCTPAG = TA.CDAJUCTPAG 
                              AND TA.IDCTBAJCTPAG = '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_NRLANBOLPAG := CONTAS.NRLANBOLPAG; 
                     ELSE 
                        V_NRCONTABOL := V_NRCONTABOL +1; 
                     END IF; 
                     IF V_VRTOTALAJUSNCTB <> 0 THEN 
                        V_VRAJUSNCTB := (V_VRTOTALAJUSNCTB * 100) / CONTAS.VRBOLPAG; 
                        V_VRAJUSNCTB := ROUND((V_VRAJUSNCTB * CONTAS.VRATUAPAG) / 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(V_VRCONTA + (V_VRAJUSNCTB * -1), 2); 
                     IF NVL(V_VRPARTIDA, 0) <> 0 THEN 
                        BUSCA_TPCOMERCIAL(V_CDCONTCTBL, V_IDCOME, V_IDCCUS); 
                        IF V_IDCOME <> 'F' THEN 
                           V_IDCOME  := 'N'; 
                           V_CODCOMEAUX := NULL; 
                        ELSE 
                           V_CODCOMEAUX  := CONTAS.CDFORNECED; 
                        END IF; 
                        V_SEQPARTIDA := V_SEQPARTIDA + 1; 
                        V_DSHISTLANC := CTB_HIST_PAG(V_CDHISTLANC, CONTAS.CDFILIAL, CONTAS.NRLANCTONF, CONTAS.NRSEQVENPAG, CONTAS.NRPARCELAPAG, '');
                        V_TOTDEBITO :=  V_TOTDEBITO + 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_CDHISTLANC, 
                            V_DSHISTLANC, V_DTBAIXA, ROUND(V_VRPARTIDA, 2), V_IDCOME, V_CODCOMEAUX, 'PAG'); 
                        V_RATSEQ := 0; 
                        IF V_CONTRAT = 'S' THEN 
                           IF V_IDCCUS <> 'O' THEN 
                              V_TOTALRATEIO := 0; 
                              IF CONTAS.IDCTBTPCNTBPAG <> 'S' THEN
                                 V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIRECE, P_CDEMPRESA);
                                 IF V_CDCENTCUST  IS NULL THEN
                                    V_CDCENTCUST  := V_CDCENTCUST_OLD;
                                 END IF;
                                 V_CDFILIAL := CONTAS.CDFILIRECE; 
                                 INSERT INTO RATECTBTEMP 
                                    (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP) 
                                 VALUES 
                                    (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), CONTAS.CDFILIRECE, 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'; 
                                 V_RESTORAT := V_VRPARTIDA; 
                                 FOR C_RATEIO IN 
                                    (SELECT RP.CDFILIAL, RP.NRLANCTONF, RP.NRSEQVENPAG, RP.NRPARCELAPAG, 
                                            RP.CDCENTCUST, RP.CDFIRATCTPAG, RP.VRRATCCCTPAG, 
                                            Round(((RP.VRRATCCCTPAG * 100) / CP.VRATUAPAG), 2) PERCENTUAL 
                                       FROM RATCTPAGAR RP, CONTAPAGAR CP, TIPOCTPAGAR TP 
                                      WHERE RP.CDFILIAL = CONTAS.CDFILIAL 
                                        AND RP.NRLANCTONF = CONTAS.NRLANCTONF 
                                        AND RP.NRSEQVENPAG = CONTAS.NRSEQVENPAG 
                                        AND RP.NRPARCELAPAG = CONTAS.NRPARCELAPAG 
                                        AND RP.CDFILIAL = CP.CDFILIAL 
                                        AND RP.NRLANCTONF = CP.NRLANCTONF 
                                        AND RP.NRSEQVENPAG = CP.NRSEQVENPAG 
                                        AND RP.NRPARCELAPAG = CP.NRPARCELAPAG 
                                        AND TP.CDTPCTPAGAR = CP.CDTPCTPAGAR
                                        AND ROUND(RP.VRRATCCCTPAG,2) > 0 
                                      ORDER BY RP.CDFILIAL, RP.NRLANCTONF, RP.NRSEQVENPAG, RP.NRPARCELAPAG, CDFIRATCTPAG) LOOP 
                                    V_VRRATEIO := ROUND((V_VRPARTIDA * C_RATEIO.PERCENTUAL)/100, 2); 
                                    V_RESTORAT := ROUND((V_RESTORAT - V_VRRATEIO), 2); 
                                    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.CDFIRATCTPAG, C_RATEIO.CDCENTCUST, 
                                        ROUND(V_VRRATEIO, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0')); 
                                    V_TOTALRATEIO := V_TOTALRATEIO + V_VRRATEIO; 
                                    V_CDFILIAL   := C_RATEIO.CDFIRATCTPAG; 
                                    V_CDCENTCUST := C_RATEIO.CDCENTCUST; 
                                    V_NRSEQUENCIAL :=  LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'); 
                                 END LOOP; 
                                 IF ((V_ENTROULOOP = 'S') AND (V_RESTORAT <> 0)) THEN 
                                    UPDATE RATECTBTEMP 
                                       SET VRRATECTBTEMP = VRRATECTBTEMP + V_RESTORAT 
                                     WHERE CDEMPRESA = P_CDEMPRESA 
                                       AND NRLANCCTBTEMP = V_NRLANCTEMP 
                                       AND NRSEQCTBTEMP = V_NRSEQUENCIAL 
                                       AND CDFILIAL = V_CDFILIAL 
                                       AND CDCENTCUST = V_CDCENTCUST; 
                                    V_TOTALRATEIO := V_TOTALRATEIO + V_RESTORAT; 
                                 END IF; 
 
                                 IF V_ENTROULOOP = 'N' THEN 

                                    V_CDCENTCUST := BUSCA_CCUSTO(CONTAS.CDFILIRECE, P_CDEMPRESA);
                                    IF V_CDCENTCUST  IS NULL THEN
                                       V_CDCENTCUST  := V_CDCENTCUST_OLD;
                                    END IF;
                                    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.CDFILIRECE, V_CDCENTCUST, 
                                        ROUND(V_VRPARTIDA, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0')); 
                                    V_TOTALRATEIO := V_TOTALRATEIO + ROUND(V_VRPARTIDA, 2); 
                                    V_CDFILIAL := CONTAS.CDFILIRECE; 
                                    V_CDCENTCUST := V_CDCENTCUST; 
                                 END IF; 
                                 IF V_TOTALRATEIO <> V_VRPARTIDA THEN 
                                    UPDATE RATECTBTEMP 
                                       SET VRRATECTBTEMP = VRRATECTBTEMP + (V_TOTALRATEIO - V_VRPARTIDA) 
                                     WHERE CDEMPRESA = P_CDEMPRESA 
                                       AND NRLANCCTBTEMP = V_NRLANCTEMP 
                                       AND NRSEQCTBTEMP = LPAD(TO_CHAR(V_SEQPARTIDA),5,'0') 
                                       AND CDFILIAL = V_CDFILIAL 
                                       AND CDCENTCUST = V_CDCENTCUST; 
                                 END IF; 
                              END IF; 
                           END IF; 
                        END IF; 
                     END IF; 
                     IF V_BOCTBAJUSTE = TRUE THEN 
                        FOR C_AJUSTECONTA IN 
                           (SELECT AJ.CDAJUCTPAG, AJ.VRAJUSTEPAG, TA.DSAJUCTPAG, TA.IDAJUCTPAG, 
                                   TA.CDCONTCTBL, TA.CDHISTPADR, H.DSHISTPADR 
                             FROM AJCTPAGAR AJ, TIPOAJPAGAR TA, HISTPADR H 
                            WHERE AJ.CDAJUCTPAG = TA.CDAJUCTPAG 
                              AND TA.CDHISTPADR = H.CDHISTPADR 
                              AND AJ.CDFILIAL = CONTAS.CDFILIAL 
                              AND AJ.NRLANCTONF = CONTAS.NRLANCTONF 
                              AND AJ.NRSEQVENPAG = CONTAS.NRSEQVENPAG 
                              AND AJ.NRPARCELAPAG = CONTAS.NRPARCELAPAG) LOOP 
 
                           IF C_AJUSTECONTA.IDAJUCTPAG = 'A' THEN 
                              V_FATOR := -1; 
                           ELSE 
                              V_FATOR := 1; 
                           END IF; 
 
                           V_SEQPARTIDA := V_SEQPARTIDA + 1; 
                           V_VRPARTIDA := ROUND(C_AJUSTECONTA.VRAJUSTEPAG, 2) * V_FATOR; 
                           IF NVL(V_VRPARTIDA, 0) <> 0 THEN 
 
                              IF C_AJUSTECONTA.CDHISTPADR IS NULL THEN 
                                 V_DSHISTLANC := CTB_HIST_PAG(V_CDHISTLANC, CONTAS.CDFILIAL, CONTAS.NRLANCTONF, CONTAS.NRSEQVENPAG, CONTAS.NRPARCELAPAG, C_AJUSTECONTA.CDAJUCTPAG);
                              ELSE 
                                 V_DSHISTLANC := CTB_HIST_PAG(C_AJUSTECONTA.CDHISTPADR, CONTAS.CDFILIAL, CONTAS.NRLANCTONF, CONTAS.NRSEQVENPAG, CONTAS.NRPARCELAPAG, C_AJUSTECONTA.CDAJUCTPAG); 
                              END IF; 
 
                              /*V_DSHISTLANC := CTB_HIST_PAG(V_CDHISTLANC, CONTAS.CDFILIAL, CONTAS.NRLANCTONF, CONTAS.NRSEQVENPAG, CONTAS.NRPARCELAPAG, '');*/
 
                              IF V_VRPARTIDA < 0 THEN 
                                 V_TOTDEBITO :=  V_TOTDEBITO + ROUND(V_VRPARTIDA, 2); 
                              ELSE 
                                 V_TOTCREDITO := V_TOTCREDITO + ROUND(V_VRPARTIDA, 2); 
                              END IF; 
 
                              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'), C_AJUSTECONTA.CDCONTCTBL, V_CDHISTLANC, 
                                  V_DSHISTLANC, V_DTBAIXA, ROUND(V_VRPARTIDA, 2), V_IDCOME, V_CODCOMEAUX, 'PAG'); 
                              V_RATSEQ := 0; 
                              BUSCA_TPCOMERCIAL(C_AJUSTECONTA.CDCONTCTBL, V_IDCOME, V_IDCCUS); 
                              IF V_CONTRAT = 'S' THEN 
                                 IF V_IDCCUS <> 'O' THEN 
                                    V_CDFILIAL := CONTAS.CDFILIRECE; 
                                    INSERT INTO RATECTBTEMP 
                                       (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP) 
                                    VALUES 
                                       (P_CDEMPRESA, V_NRLANCTEMP, LPAD(TO_CHAR(V_SEQPARTIDA),5,'0'), CONTAS.CDFILIRECE, V_CDCENTCUST, 
                                        ROUND(V_VRPARTIDA, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0')); 
                                 END IF; 
                              END IF; 
                           END IF; 
                        END LOOP; 
                     END IF; 
                  END IF; 
               END LOOP; 
            ELSE 
               /* ROTINA PARA COMPENSAR O CHEQUE */  
               V_SEQPARTIDA := V_SEQPARTIDA + 1; 
               V_TOTDEBITO := V_TOTDEBITO + ROUND(V_VRBAIXAPAG, 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_CDCONTCTBLCHQ, V_CDHISTPADR, 
                   V_DSHISTLANC, V_DTBAIXA, -ROUND(V_VRBAIXAPAG, 2), V_IDCOME, V_CDREDUCONT, 'PAG'); 
               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_CDREDUCONT; 
                     EXCEPTION 
                        WHEN NO_DATA_FOUND THEN 
                           V_CDFILIAL := NULL; 
                     END; 
                     IF V_CDFILIAL IS NULL THEN

                        V_CDCENTCUST := BUSCA_CCUSTO(P_CDFILIAL, P_CDEMPRESA);
                        IF V_CDCENTCUST  IS NULL THEN
                           V_CDCENTCUST  := V_CDCENTCUST_OLD;
                        END IF;

                        V_CDFILIAL := P_CDFILIAL;
                     ELSE
                        V_CDCENTCUST := BUSCA_CCUSTO(V_CDFILIAL, P_CDEMPRESA);
                        IF V_CDCENTCUST  IS NULL THEN
                           V_CDCENTCUST  := V_CDCENTCUST_OLD;
                        END IF;
                     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_VRBAIXAPAG, 2), V_DTBAIXA, LPAD(TO_CHAR(V_RATSEQ),5,'0')); 
                  END IF; 
               END IF; 
            END IF; 
         END IF; 
 
         IF V_IDCTBBXPLAN = 'N' THEN 
            V_INPARTIDAS := 0; 
            V_ENTROUAJUSTE := 'N'; 
            FOR AJUSTES IN 
               (SELECT BO.NRLANBOLPAG, BO.CDFILIBAIPAG, NF.CDFILIRECE, AJ.CDAJUCTPAG, 
                       BX.CDREDUCONT, BO.CDFORNECED, AJ.VRAJBOLPAG, IDAJUCTPAG, BO.VRBOLPAG, 
                       CT.NRLANCTONF, CT.CDFILIAL, HP.CDHISTPADR, CT.NRSEQVENPAG, CT.NRPARCELAPAG, 
                       CT.VRATUAPAG, TP.CDCONTCTBL,  COUNT(*) OVER(PARTITION BY BX.CDFILIAL, BX.NRBAIXAPAG) RECORDCOUNT 
                  FROM BAIXAPAGA BX, BOLETPAGAR BO, AJBOLPAG AJ, CONTAPAGAR CT, 
                       NOTAFISCAL NF, TIPOAJPAGAR TP, HISTPADR HP 
                 WHERE BX.CDFILIAL = P_CDFILIAL 
                   AND BX.NRBAIXAPAG = P_NRBAIXA 
                   AND BX.CDFILIAL = BO.CDFILIBAIPAG 
                   AND BX.NRBAIXAPAG = BO.NRBAIXAPAG 
                   AND BO.CDFILIAL = AJ.CDFILIAL 
                   AND BO.NRLANBOLPAG = AJ.NRLANBOLPAG 
                   AND BO.CDFILIAL = CT.CDFILIBPAG 
                   AND BO.NRLANBOLPAG = CT.NRLANBOLPAG 
                   AND CT.CDFILIAL = NF.CDFILIAL 
                   AND CT.NRLANCTONF = NF.NRLANCTONF 
                   AND AJ.CDAJUCTPAG = TP.CDAJUCTPAG 
                   AND TP.IDCTBAJCTPAG = 'S' 
                   AND HP.CDHISTPADR = TP.CDHISTPADR(+) 
                 GROUP BY BO.NRLANBOLPAG, BO.CDFILIBAIPAG, NF.CDFILIRECE, AJ.CDAJUCTPAG, 
                          BX.CDREDUCONT, BO.CDFORNECED, AJ.VRAJBOLPAG, IDAJUCTPAG, BO.VRBOLPAG, 
                          CT.NRLANCTONF, CT.CDFILIAL, HP.CDHISTPADR, CT.NRSEQVENPAG, CT.NRPARCELAPAG, 
                          CT.VRATUAPAG, TP.CDCONTCTBL, BX.CDFILIAL, BX.NRBAIXAPAG)LOOP 
               V_INPARTIDAS := V_INPARTIDAS + 1; 
               IF AJUSTES.IDAJUCTPAG = 'A' THEN 
                  V_FATOR := -1; 
               ELSE 
                  V_FATOR := 1; 
               END IF; 
 
               IF AJUSTES.CDHISTPADR IS NULL THEN 
                  V_DSHISTLANC := CTB_HIST_PAG(V_CDHISTLANC, AJUSTES.CDFILIAL, AJUSTES.NRLANCTONF, AJUSTES.NRSEQVENPAG, AJUSTES.NRPARCELAPAG, AJUSTES.CDAJUCTPAG); 
               ELSE
                  V_DSHISTLANC := CTB_HIST_PAG(AJUSTES.CDHISTPADR, AJUSTES.CDFILIAL, AJUSTES.NRLANCTONF, AJUSTES.NRSEQVENPAG, AJUSTES.NRPARCELAPAG, AJUSTES.CDAJUCTPAG);
               END IF; 

               V_CDCENTCUST := BUSCA_CCUSTO(AJUSTES.CDFILIRECE, P_CDEMPRESA);
               IF V_CDCENTCUST  IS NULL THEN
                  V_CDCENTCUST  := V_CDCENTCUST_OLD;
               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 = 'F' THEN 
                  V_CODCOMEAUX := AJUSTES.CDFORNECED; 
               ELSE 
                  V_IDCOME := 'N'; 
                  V_CODCOMEAUX := NULL; 
               END IF; 
               IF V_SEQPARTIDA IS NULL THEN 
                  V_SEQPARTIDA := 0; 
               END IF; 
               V_VRAJUSTE := ROUND((AJUSTES.VRATUAPAG  * 100) / AJUSTES.VRBOLPAG, 2); 
               V_VRAJUSTE := ROUND((AJUSTES.VRAJBOLPAG * V_VRAJUSTE) / 100, 2); 
               IF ROUND(NVL(V_VRAJUSTE, 0), 2) <> 0 THEN 
                 V_SEQPARTIDA := V_SEQPARTIDA + 1; 
                 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_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_DSHISTLANC,  V_DTBAIXA, ROUND(V_VRAJUSTE * V_FATOR, 2), V_IDCOME, V_CODCOMEAUX, 'PAG'); 
                 V_TOTAJUSTE := V_VRAJUSTE * V_FATOR; 
                 IF V_CONTRAT = 'S' THEN 
                    IF V_IDCCUS <> 'O' THEN 
                       V_CDCENTCUST := BUSCA_CCUSTO(AJUSTES.CDFILIRECE, P_CDEMPRESA);
                       IF V_CDCENTCUST  IS NULL THEN
                          V_CDCENTCUST  := V_CDCENTCUST_OLD;
                       END IF;
                       V_CDFILIAL := AJUSTES.CDFILIRECE;
                       V_TOTRATAJ := 0;
                       V_RATSEQ := 0; 
                       BEGIN 
                          SELECT COUNT(*) 
                            INTO V_COUNT 
                            FROM RATTPAJP 
                           WHERE CDAJUCTPAG = AJUSTES.CDAJUCTPAG; 
                       EXCEPTION 
                          WHEN NO_DATA_FOUND THEN 
                             V_COUNT := 0; 
                       END; 
                       IF V_COUNT <> 0 THEN 
                          FOR RATAJ IN( 
                             SELECT CDCENTCUST, CDFIRATTPAJP, VRPERATTPAJP 
                               FROM RATTPAJP 
                              WHERE CDAJUCTPAG = AJUSTES.CDAJUCTPAG)LOOP 
                             V_VRAJUSTE := ROUND((V_TOTAJUSTE * RATAJ.VRPERATTPAJP) / 100, 2); 
                             V_RATSEQ := V_RATSEQ + 1; 
                             IF RATAJ.CDFIRATTPAJP = '----' THEN 
                                V_CDFILIAL := AJUSTES.CDFILIRECE; 
                             ELSE 
                                V_CDFILIAL := RATAJ.CDFIRATTPAJP; 
                             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; 

                          V_CDCENTCUST := BUSCA_CCUSTO(AJUSTES.CDFILIRECE, P_CDEMPRESA);
                          IF V_CDCENTCUST  IS NULL THEN
                             V_CDCENTCUST  := V_CDCENTCUST_OLD;
                          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.CDFILIRECE, 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.CDFILIRECE; 
                          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 
                  V_ENTROUAJUSTE := 'S'; 
                  IF ABS(V_TOTCREDITO) <> ABS(V_TOTDEBITO) THEN 
                     V_DIFERENCA := ABS(V_TOTCREDITO) + V_TOTDEBITO; 
                     V_NRSEQUENCIALTEMP := NULL; 
                     BEGIN 
                        SELECT MAX(NRSEQCTBTEMP) 
                          INTO V_NRSEQUENCIALTEMP 
                          FROM LANCCTBTEMP 
                         WHERE NRLANCCTBTEMP = V_NRLANCTEMP 
                           AND CDEMPRESA = P_CDEMPRESA 
                           AND ABS(VRLANCCTBTEMP) > ABS(V_DIFERENCA); 
                     EXCEPTION 
                        WHEN NO_DATA_FOUND THEN 
                           V_NRSEQUENCIALTEMP := NULL; 
                     END; 
                     IF V_NRSEQUENCIALTEMP = V_NRSEQUENCIAL THEN 
                        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; 
                     ELSE 
                        UPDATE LANCCTBTEMP 
                           SET VRLANCCTBTEMP = VRLANCCTBTEMP - V_DIFERENCA 
                         WHERE NRLANCCTBTEMP = V_NRLANCTEMP 
                           AND NRSEQCTBTEMP = V_NRSEQUENCIALTEMP 
                           AND CDEMPRESA = P_CDEMPRESA; 
                        IF V_CONTRAT = 'S' THEN 
                           IF V_IDCCUS <> 'O' THEN 
                              BEGIN 
                                 SELECT CDFILIAL, CDCENTCUST 
                                   INTO V_CDFILIAL, V_CDCENTCUST 
                                   FROM RATECTBTEMP 
                                  WHERE CDEMPRESA = P_CDEMPRESA 
                                    AND NRLANCCTBTEMP = V_NRLANCTEMP 
                                    AND NRSEQCTBTEMP = V_NRSEQUENCIALTEMP 
                                    AND ROWNUM = 1; 
                              EXCEPTION 
                                 WHEN NO_DATA_FOUND THEN 
                                    V_CDFILIAL := NULL; 
                                    V_CDCENTCUST := NULL; 
                              END; 
                              UPDATE RATECTBTEMP 
                                  SET VRRATECTBTEMP = VRRATECTBTEMP - V_DIFERENCA 
                                WHERE CDEMPRESA = P_CDEMPRESA 
                                  AND NRLANCCTBTEMP = V_NRLANCTEMP 
                                  AND NRSEQCTBTEMP = V_NRSEQUENCIALTEMP 
                                  AND CDFILIAL = V_CDFILIAL 
                                  AND CDCENTCUST = V_CDCENTCUST; 
                           END IF; 
                        END IF; 
                     END IF; 
                  END IF; 
               END IF; 
            END LOOP; 
         END IF; 
      END IF; 
 
      IF ((ABS(V_TOTCREDITO) <> ABS(V_TOTDEBITO)) AND (V_ENTROUAJUSTE = 'N')) THEN 
         V_DIFERENCA := ABS(V_TOTCREDITO) + V_TOTDEBITO; 
         V_NRSEQUENCIALTEMP := NULL; 
         BEGIN 
            SELECT MAX(NRSEQCTBTEMP) 
              INTO V_NRSEQUENCIALTEMP 
              FROM LANCCTBTEMP 
             WHERE NRLANCCTBTEMP = V_NRLANCTEMP 
               AND CDEMPRESA = P_CDEMPRESA 
               AND ABS(VRLANCCTBTEMP) > ABS(V_DIFERENCA); 
         EXCEPTION 
            WHEN NO_DATA_FOUND THEN 
               V_NRSEQUENCIALTEMP := NULL; 
         END; 
         IF V_NRSEQUENCIALTEMP = V_NRSEQUENCIAL THEN 
            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; 
         ELSE 
            UPDATE LANCCTBTEMP 
               SET VRLANCCTBTEMP = VRLANCCTBTEMP - V_DIFERENCA 
             WHERE NRLANCCTBTEMP = V_NRLANCTEMP 
               AND NRSEQCTBTEMP = V_NRSEQUENCIALTEMP 
               AND CDEMPRESA = P_CDEMPRESA; 
            IF V_CONTRAT = 'S' THEN 
               IF V_IDCCUS <> 'O' THEN 
                  BEGIN 
                     SELECT CDFILIAL, CDCENTCUST 
                       INTO V_CDFILIAL, V_CDCENTCUST 
                       FROM RATECTBTEMP 
                      WHERE CDEMPRESA = P_CDEMPRESA 
                        AND NRLANCCTBTEMP = V_NRLANCTEMP 
                        AND NRSEQCTBTEMP = V_NRSEQUENCIALTEMP 
                        AND ROWNUM = 1; 
                  EXCEPTION 
                     WHEN NO_DATA_FOUND THEN 
                        V_CDFILIAL := NULL; 
                        V_CDCENTCUST := NULL; 
                  END; 
                  UPDATE RATECTBTEMP 
                      SET VRRATECTBTEMP = VRRATECTBTEMP - V_DIFERENCA 
                    WHERE CDEMPRESA = P_CDEMPRESA 
                      AND NRLANCCTBTEMP = V_NRLANCTEMP
                      AND NRSEQCTBTEMP = V_NRSEQUENCIALTEMP 
                      AND CDFILIAL = V_CDFILIAL 
                      AND CDCENTCUST = V_CDCENTCUST; 
               END IF; 
            END IF; 
         END IF; 
      END IF; 
 
      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 BAIXAPAGA  
            SET NRLCTBBPAG = V_NRLANCTEMP  
          WHERE NRBAIXAPAG = P_NRBAIXA  
            AND CDFILIAL   = P_CDFILIAL;  
      END IF;  
      GRAVA_LANC_CTB(V_NRLANCTEMP, P_CDEMPRESA);  
      P_NRLANCCTB := V_NRLANCTEMP;  
   END IF; 
END;
/