CREATE OR REPLACE PROCEDURE CTB_AJUSTE_APURACAO(
   P_CDIMPOSTO    IN IMPOSTONFE.CDIMPOSTONFE%TYPE,
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA   IN INSCRESTAD.CDINSCESTA%TYPE,
   P_NRSEQUMAPAPU IN MAPAAPURAC.NRSEQUMAPAPU%TYPE,
   P_CDLOTE       IN CAPALOTE.CDLOTE%TYPE,
   P_CDHISTPADR   IN HISTPADR.CDHISTPADR%TYPE)
AS
   V_IDGEROULOTE       VARCHAR2(01);
   V_DATACONTABIL      DATE;
   V_CDCONTCTBLCRED    VARCHAR2(20);
   V_CDCONTCTBLDEB     VARCHAR2(20);
   V_CDCONTCTBLREC     VARCHAR2(20);
   V_IDCONTABILIZA     VARCHAR2(01);
   V_CDPADRAORATEIO    VARCHAR2(05);
   V_VRENTRADA         NUMBER;
   V_VRSAIDA           NUMBER;
   V_IDTIPORATEIO      VARCHAR2(05);
   V_TOTAL             NUMBER;
   V_TOTFILIAL         NUMBER;
   V_DSHISTPADR        VARCHAR2(1000);
   V_DSESTORNOAJ       VARCHAR2(1000);
   V_NRLANCTEMP        VARCHAR2(20);
   V_CDCONTCREDITO     VARCHAR2(20);
   V_CDCONTDEBITO      VARCHAR2(20);
   V_VRPARTIDA         NUMBER;
   V_CDCENTCUST        VARCHAR2(04);
   V_IDCONTCCUS        VARCHAR2(01);
   V_CDLOTE            VARCHAR2(20);
   V_NRLANCLOTE        VARCHAR2(6);
   V_IDCODCOMECONTEST  VARCHAR2(1);
   V_COUNT             NUMBER;
   V_CODCOMEEST        VARCHAR2(30);
   V_IDCODCOMECONTICM  VARCHAR2(1);
   V_NRNOTAFISC        VARCHAR2(50);
   V_CODCOMEICM        VARCHAR2(30);
BEGIN
   /*BUSCA A DATA FINAL DO MAPA COMO DATA DE REFERENCIA PARA A CONTABILIDADE*/
   BEGIN
      SELECT M.DTFIMPERIODO
        INTO V_DATACONTABIL
        FROM MAPAAPURAC M
       WHERE M.NRSEQUMAPAPU = P_NRSEQUMAPAPU;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_DATACONTABIL := '';
   END;

   V_IDGEROULOTE   := 'N';
   V_IDCONTABILIZA := 'N';

   FOR C_AJUSTE IN (SELECT A.NRSEQAJUSTE,
                           CASE A.IDINDICAJUSTE||A.IDTPAJUSTE
                              WHEN 'C0'  THEN 'RC'
                              WHEN 'C1'  THEN 'AC'
                              WHEN 'DB0' THEN 'RDI'
                              WHEN 'DB1' THEN 'ADI'
                              WHEN 'DI0' THEN 'ARB'
                              WHEN 'DI1' THEN 'ADB'
                           END AS IDTPAJUSTE,
                           NVL(A.CDAJUSTE,A.CDAJUSTEBC) AS CDAJUSTE,
                           A.DSAJUSTE, A.VRAJUSTE
                      FROM AJUSTEAPURCONTR A
                     WHERE A.NRSEQUMAPAPU  = P_NRSEQUMAPAPU
                       AND A.IDFORMAPUR    = 'C'
                       AND A.IDINDICAJUSTE IS NOT NULL
                     UNION ALL
                    SELECT I.NRSEQITMAPR, I.IDTIPOITMAPR AS IDTPAJUSTE,
                           NVL(I.CDAJUSTEITMAPA,I.CDAJUSTEITSEF) AS CDAJUSTE,
                           I.DSITEMMAPR, I.VRITEMMAPR
                      FROM ITMAPARECUP I
                     WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                       AND I.IDTIPOITMAPR IN ('DA','DD','DE','EC','ED','OC','OD')
                     UNION ALL
                    SELECT I.NRSEQITDETAPU, I.IDTIPODETAPU AS IDTPAJUSTE,
                           I.IDORIGDEDUC AS CDAJUSTE,
                           I.DSITEMDETAPU, I.VRITEMDETAPU
                      FROM ITDETAPUCONTR I
                     WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                       AND I.IDTIPODETAPU = 'DD') LOOP

      /*Verifica as parametrizacoes por codigo de ajuste*/
      BEGIN
         SELECT P.CDCONTAJCRED, P.CDCONTAJDEB, P.CDCONTRECOL, P.IDCONTCTBL, P.CDCONTRATEIO
           INTO V_CDCONTCTBLCRED, V_CDCONTCTBLDEB, V_CDCONTCTBLREC, V_IDCONTABILIZA, V_CDPADRAORATEIO
           FROM PARAMCODAJUS P
          WHERE P.CODAJUSTE    = C_AJUSTE.CDAJUSTE
            AND P.CDIMPOSTONFE = P_CDIMPOSTO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDCONTCTBLCRED := '';
            V_CDCONTCTBLDEB  := '';
            V_CDCONTCTBLREC  := '';
            V_IDCONTABILIZA  := 'N';
            V_CDPADRAORATEIO := '';
      END;

      /*Verifica as parametrizacoes por tipo de ajuste*/
      IF V_CDCONTCTBLREC IS NULL THEN
         BEGIN
            SELECT P.CDCONTAJCRED, P.CDCONTAJDEB, P.CDCONTRECOL, P.IDCONTCTBL, P.CDCONTRATEIO
              INTO V_CDCONTCTBLCRED, V_CDCONTCTBLDEB, V_CDCONTCTBLREC, V_IDCONTABILIZA, V_CDPADRAORATEIO
              FROM PARAMAJUSTE P
             WHERE P.IDTPAJUSTE   = C_AJUSTE.IDTPAJUSTE
               AND P.CDIMPOSTONFE = P_CDIMPOSTO;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CDCONTCTBLCRED := '';
               V_CDCONTCTBLDEB  := '';
               V_CDCONTCTBLREC  := '';
               V_IDCONTABILIZA  := 'N';
               V_CDPADRAORATEIO := '';
         END;

         /*Verifica as parametrizacoes por imposto*/
         IF V_CDCONTCTBLREC IS NULL THEN
            BEGIN
               SELECT I.CDCONTAJCRED, I.CDCONTAJDEB, I.CDCONTRECOL, I.IDCONTCTBL, I.CDCONTRATEIO
                 INTO V_CDCONTCTBLCRED, V_CDCONTCTBLDEB, V_CDCONTCTBLREC, V_IDCONTABILIZA, V_CDPADRAORATEIO
                 FROM IMPOSTONFE I
                WHERE I.CDIMPOSTONFE = P_CDIMPOSTO;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDCONTCTBLCRED := '';
                  V_CDCONTCTBLDEB  := '';
                  V_CDCONTCTBLREC  := '';
                  V_IDCONTABILIZA  := 'N';
                  V_CDPADRAORATEIO := '';
            END;
         END IF;
      END IF;

      IF V_IDCONTABILIZA = 'S' THEN
         BEGIN
            SELECT SUM(DECODE(NF.IDENTRSAIDOP,'E',II.VRCONTITIMP,0)) AS VRENTRADA,
                   SUM(DECODE(NF.IDENTRSAIDOP,'S',II.VRCONTITIMP,0)) AS VRSAIDA,
                   RA.IDRATEIOPADRAO
              INTO V_VRENTRADA, V_VRSAIDA, V_IDTIPORATEIO
              FROM ITEMPROIMPOS II, ITEMPRODUTO IT, IMPOSTO IM, NOTAFISCAL NF, RATEIOPADRAOFIS RA,
                   (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                      FROM IMPOSTONF I
                     WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU) IP,
                   (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.CDTIPOOPER
                      FROM RATFISTPOPERAC R
                     WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                     UNION ALL
                    SELECT V_CDPADRAORATEIO, 'E', 'T' FROM DUAL
                     WHERE NOT EXISTS (SELECT * FROM RATFISTPOPERAC R
                                        WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                          AND R.IDENTRSAIDOP   = 'E')
                     UNION ALL
                    SELECT V_CDPADRAORATEIO, 'S', 'T' FROM DUAL
                     WHERE NOT EXISTS (SELECT * FROM RATFISTPOPERAC R
                                        WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                          AND R.IDENTRSAIDOP   = 'S'))RT,
                   (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.CDCFOPPADRAO
                      FROM RATFISCFOPPADRAO R
                     WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                     UNION ALL
                    SELECT V_CDPADRAORATEIO, 'E', 'T' FROM DUAL
                     WHERE NOT EXISTS (SELECT * FROM RATFISCFOPPADRAO R
                                        WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                          AND R.IDENTRSAIDOP   = 'E')
                     UNION ALL
                    SELECT V_CDPADRAORATEIO, 'S', 'T' FROM DUAL
                     WHERE NOT EXISTS (SELECT * FROM RATFISCFOPPADRAO R
                                        WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                          AND R.IDENTRSAIDOP   = 'S')) RC,
                   (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.CDPRODUTO
                      FROM RATFISPRODUTO R
                     WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                     UNION ALL
                    SELECT V_CDPADRAORATEIO, 'E', 'T' FROM DUAL
                     WHERE NOT EXISTS (SELECT * FROM RATFISCFOPPADRAO R
                                        WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                          AND R.IDENTRSAIDOP   = 'E')
                     UNION ALL
                    SELECT V_CDPADRAORATEIO, 'S', 'T' FROM DUAL
                     WHERE NOT EXISTS (SELECT * FROM RATFISCFOPPADRAO R
                                        WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                          AND R.IDENTRSAIDOP   = 'S'))RP
             WHERE IP.CDFILIAL       = II.CDFILIAL
               AND IP.NRLANCTONF     = II.NRLANCTONF
               AND II.CDIMPOSTO      = IM.CDIMPOSTO
               AND IM.CDIMPOSTONFE   = P_CDIMPOSTO
               AND NVL(II.NRITPROIMPOS,'1') <> '3'
               AND II.CDFILIAL       = NF.CDFILIAL
               AND II.NRLANCTONF     = NF.NRLANCTONF
               AND RA.CDRATEIOPADRAO = V_CDPADRAORATEIO
               AND RA.CDRATEIOPADRAO = RT.CDRATEIOPADRAO
               AND NF.IDENTRSAIDOP   = RT.IDENTRSAIDOP
               AND (NF.CDTIPOOPER = RT.CDTIPOOPER OR RT.CDTIPOOPER = 'T')
               AND RA.CDRATEIOPADRAO = RC.CDRATEIOPADRAO
               AND NF.IDENTRSAIDOP   = RC.IDENTRSAIDOP
               AND (II.CDFISCOPERIT = RC.CDCFOPPADRAO OR RC.CDCFOPPADRAO = 'T')
               AND II.CDFILIAL       = IT.CDFILIAL
               AND II.NRLANCTONF     = IT.NRLANCTONF
               AND RA.CDRATEIOPADRAO = RP.CDRATEIOPADRAO
               AND NF.IDENTRSAIDOP   = RP.IDENTRSAIDOP
               AND (IT.CDPRODUTO = RP.CDPRODUTO OR RP.CDPRODUTO = 'T')
             GROUP BY RA.IDRATEIOPADRAO;
         EXCEPTION
             WHEN NO_DATA_FOUND THEN
                V_VRENTRADA    := 0;
                V_VRSAIDA      := 0;
                V_IDTIPORATEIO := '';
         END;

         CASE V_IDTIPORATEIO
           WHEN 'ENT' THEN V_TOTAL := V_VRENTRADA;
           WHEN 'SAI' THEN V_TOTAL := V_VRSAIDA;
           WHEN 'SME' THEN V_TOTAL := V_VRSAIDA - V_VRENTRADA;
          ELSE V_TOTAL := 0;  
         END CASE;

         IF V_TOTAL < 0 THEN V_TOTAL := 0; END IF;

         FOR C_FILIAL IN (SELECT SUM(DECODE(NF.IDENTRSAIDOP,'E',II.VRCONTITIMP,0)) AS VRENTRADA,
                                 SUM(DECODE(NF.IDENTRSAIDOP,'S',II.VRCONTITIMP,0)) AS VRSAIDA,
                                 DECODE(NF.IDENTRSAIDOP,'E',NF.CDFILIRECE,NF.CDFILIEMIT) AS CDFILIAL,
                                 RA.IDRATEIOPADRAO
                            FROM ITEMPROIMPOS II, ITEMPRODUTO IT, IMPOSTO IM, NOTAFISCAL NF, RATEIOPADRAOFIS RA,
                                 (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                    FROM IMPOSTONF I
                                   WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU) IP,
                                 (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.CDTIPOOPER
                                    FROM RATFISTPOPERAC R
                                   WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                   UNION ALL
                                  SELECT V_CDPADRAORATEIO, 'E', 'T' FROM DUAL
                                   WHERE NOT EXISTS (SELECT * FROM RATFISTPOPERAC R
                                                      WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                        AND R.IDENTRSAIDOP   = 'E')
                                   UNION ALL
                                  SELECT V_CDPADRAORATEIO, 'S', 'T' FROM DUAL
                                   WHERE NOT EXISTS (SELECT * FROM RATFISTPOPERAC R
                                                      WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                        AND R.IDENTRSAIDOP   = 'S'))RT,
                                 (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.CDCFOPPADRAO
                                    FROM RATFISCFOPPADRAO R
                                   WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                   UNION ALL
                                  SELECT V_CDPADRAORATEIO, 'E', 'T' FROM DUAL
                                   WHERE NOT EXISTS (SELECT * FROM RATFISCFOPPADRAO R
                                                      WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                        AND R.IDENTRSAIDOP   = 'E')
                                   UNION ALL
                                  SELECT V_CDPADRAORATEIO, 'S', 'T' FROM DUAL
                                   WHERE NOT EXISTS (SELECT * FROM RATFISCFOPPADRAO R
                                                      WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                        AND R.IDENTRSAIDOP   = 'S')) RC,
                                 (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.CDPRODUTO
                                    FROM RATFISPRODUTO R
                                   WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                   UNION ALL
                                  SELECT V_CDPADRAORATEIO, 'E', 'T' FROM DUAL
                                   WHERE NOT EXISTS (SELECT * FROM RATFISCFOPPADRAO R
                                                      WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                        AND R.IDENTRSAIDOP   = 'E')
                                   UNION ALL
                                  SELECT V_CDPADRAORATEIO, 'S', 'T' FROM DUAL
                                   WHERE NOT EXISTS (SELECT * FROM RATFISCFOPPADRAO R
                                                      WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                        AND R.IDENTRSAIDOP   = 'S'))RP
                           WHERE IP.CDFILIAL       = II.CDFILIAL
                             AND IP.NRLANCTONF     = II.NRLANCTONF
                             AND II.CDIMPOSTO      = IM.CDIMPOSTO
                             AND IM.CDIMPOSTONFE   = P_CDIMPOSTO
                             AND NVL(II.NRITPROIMPOS,'1') <> '3'
                             AND II.CDFILIAL       = NF.CDFILIAL
                             AND II.NRLANCTONF     = NF.NRLANCTONF
                             AND RA.CDRATEIOPADRAO = V_CDPADRAORATEIO
                             AND RA.CDRATEIOPADRAO = RT.CDRATEIOPADRAO
                             AND NF.IDENTRSAIDOP   = RT.IDENTRSAIDOP
                             AND (NF.CDTIPOOPER = RT.CDTIPOOPER OR RT.CDTIPOOPER = 'T')
                             AND RA.CDRATEIOPADRAO = RC.CDRATEIOPADRAO
                             AND NF.IDENTRSAIDOP   = RC.IDENTRSAIDOP
                             AND (II.CDFISCOPERIT = RC.CDCFOPPADRAO OR RC.CDCFOPPADRAO = 'T')
                             AND II.CDFILIAL       = IT.CDFILIAL
                             AND II.NRLANCTONF     = IT.NRLANCTONF
                             AND RA.CDRATEIOPADRAO = RP.CDRATEIOPADRAO
                             AND NF.IDENTRSAIDOP   = RP.IDENTRSAIDOP
                             AND (IT.CDPRODUTO = RP.CDPRODUTO OR RP.CDPRODUTO = 'T')
                          GROUP BY DECODE(NF.IDENTRSAIDOP,'E',NF.CDFILIRECE,NF.CDFILIEMIT), RA.IDRATEIOPADRAO) LOOP

            CASE V_IDTIPORATEIO
              WHEN 'ENT' THEN V_TOTFILIAL := C_FILIAL.VRENTRADA;
              WHEN 'SAI' THEN V_TOTFILIAL := C_FILIAL.VRSAIDA;
              WHEN 'SME' THEN V_TOTFILIAL := C_FILIAL.VRSAIDA - C_FILIAL.VRENTRADA;
            END CASE;

            IF V_TOTFILIAL < 0 THEN V_TOTFILIAL := 0; END IF;
            V_NRNOTAFISC := '00AJ'||C_FILIAL.CDFILIAL;

            /*MONTA O HISTORICO DO LANCAMENTO*/
            --MONTA_HISTORICO(C_FILIAL.CDFILIAL, P_CDINSCESTA ,P_CDHISTPADR,'',V_DATACONTABIL,'13', '', V_DSHISTPADR);
            V_DSESTORNOAJ := C_AJUSTE.DSAJUSTE;
            /*GERA LOTE CONTABIL*/
            GERA_LOTECONTABIL(P_CDEMPRESA,'01',C_FILIAL.CDFILIAL,'',V_DATACONTABIL,'','',V_CDLOTE,V_NRLANCLOTE,P_CDLOTE);

            IF V_TOTFILIAL > 0 AND V_TOTAL > 0 THEN
               /*AJUSTES DE DEBITO*/
               IF C_AJUSTE.IDTPAJUSTE IN ('RC', 'ADI', 'ADB', 'DA', 'DE', 'EC', 'OD') THEN
                  V_CDCONTCREDITO := V_CDCONTCTBLREC; --CREDITA NA CONTA A RECOLHER
                  V_CDCONTDEBITO  := V_CDCONTCTBLDEB; --DEBITA NO AJUSTE DE DEBITO
               /*AJUSTES DE CREDITO - 'AC', 'RDI', 'ARB', 'DD', 'ED', 'OC'*/
               ELSE
                  V_CDCONTCREDITO := V_CDCONTCTBLCRED; --CREDITA NO AJUSTE DE CREDITO
                  V_CDCONTDEBITO  := V_CDCONTCTBLREC; --DEBITA NA CONTA A RECOLHER
               END IF;

               V_VRPARTIDA := ROUND(C_AJUSTE.VRAJUSTE * V_TOTFILIAL / V_TOTAL,2);

               /*INSERE A TABELA PARA EXIBIR O RELATORIO*/
               INSERT INTO CTB_TEMP_CREDPRES
                  (CDFILIAL, VRTOTENTRADAS, VRTOTSAIDAS, VRSAIDCONTAB, CDCONTCTBLDEB, CDCONTCTBLCRED)
               VALUES
                  (C_FILIAL.CDFILIAL, NVL(C_FILIAL.VRENTRADA,0),  NVL(C_FILIAL.VRSAIDA,0), NVL(V_VRPARTIDA,0), V_CDCONTDEBITO, V_CDCONTCREDITO);

               /*PARTIDA DE DEBITO*/
               NOVA_PARTIDA(P_CDEMPRESA, V_CDLOTE, V_DATACONTABIL, V_NRLANCLOTE, V_CDCONTDEBITO, P_CDHISTPADR,
                            NVL(V_DSHISTPADR,V_DSESTORNOAJ), NVL(V_VRPARTIDA,0)*-1, V_IDCODCOMECONTEST, V_CODCOMEEST, '', V_NRNOTAFISC, 'N');

               /*VERIFICA O CENTRO DE CUSTO PARAMETRIZADO*/
               BEGIN
                  SELECT NVL(F.CDCENTCUST, E.CDCENTCUST) AS CDCENTCUST
                    INTO V_CDCENTCUST
                    FROM FILIAL F, EMPRESA E
                   WHERE F.CDFILIAL  = C_FILIAL.CDFILIAL
                     AND E.CDEMPRESA = P_CDEMPRESA;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_CDCENTCUST := NULL;
               END;

               /*INSERE RATEIO DA PARTIDA*/
               IF V_CDCENTCUST IS NOT NULL THEN
                  RATEIO_PARTIDA(P_CDEMPRESA, V_CDLOTE, V_DATACONTABIL, V_NRLANCLOTE, C_FILIAL.CDFILIAL,
                                 V_CDCENTCUST, NVL(V_VRPARTIDA,0)*-1);
               END IF;

               /*PARTIDA DE CREDITO*/
               NOVA_PARTIDA(P_CDEMPRESA, V_CDLOTE, V_DATACONTABIL, V_NRLANCLOTE, V_CDCONTCREDITO, P_CDHISTPADR,
                            NVL(V_DSHISTPADR,V_DSESTORNOAJ), NVL(V_VRPARTIDA,0), V_IDCODCOMECONTICM, V_CODCOMEICM, '', V_NRNOTAFISC, 'N');

               /*VERIFICA O CENTRO DE CUSTO CADASTRADO*/
               BEGIN
                  SELECT NVL(F.CDCENTCUST, E.CDCENTCUST) AS CDCENTCUST
                    INTO V_CDCENTCUST
                    FROM FILIAL F, EMPRESA E
                   WHERE F.CDFILIAL  = C_FILIAL.CDFILIAL
                     AND E.CDEMPRESA = P_CDEMPRESA;
               EXCEPTION
                    WHEN NO_DATA_FOUND THEN
                     V_CDCENTCUST := NULL;
               END;

               /*VERIFICA SE A CONTA CONTABIL E OFICIAL/GERENCIAL E GERENCIAL*/
               BEGIN
                SELECT IDCONTCCUS
                  INTO V_IDCONTCCUS
                  FROM CONTCTBL
                 WHERE CDCONTCTBL = V_CDCONTCREDITO;
               EXCEPTION
                 WHEN NO_DATA_FOUND THEN
                   V_IDCONTCCUS := NULL;
               END;

               IF V_CDCENTCUST IS NOT NULL THEN
                  IF V_IDCONTCCUS <> 'O' THEN
                   RATEIO_PARTIDA(P_CDEMPRESA, V_CDLOTE, V_DATACONTABIL, V_NRLANCLOTE, C_FILIAL.CDFILIAL,
                                  V_CDCENTCUST, NVL(V_VRPARTIDA,0));
                  END IF;
               END IF;
            END IF;
         END LOOP;
      END IF;
   END LOOP;

   /*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;
END;
/