create or replace
PROCEDURE CTB_AJUSTE_APURACAO(
   P_CDIMPOSTO    IN MAPAAPURAC.CDIMPOSTO%TYPE,
   P_CDEMPRESA    IN MAPAAPURAC.CDEMPRESA%TYPE,
   P_CDINSCESTA   IN MAPAAPURAC.CDINSCESTA%TYPE,
   P_CDINSCMUNI   IN MAPAAPURAC.CDINSCMUNI%TYPE,
   P_NRSEQUMAPAPU IN MAPAAPURAC.NRSEQUMAPAPU%TYPE,
   P_IDTABELA     IN VARCHAR2,
   P_IDTPAJUSTE   IN VARCHAR2,
   P_NRSEQITEM    IN ITMAPARECUP.NRSEQITMAPR%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_VRBASEENTR        NUMBER;
   V_VRBASESAID        NUMBER;
   V_VRIMPENTRADA      NUMBER;
   V_VRIMPSAIDA        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);
   V_IDORIGVALOR       VARCHAR2(01);
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';

   /*BUSCA OS DADOS DO AJUSTE QUE ESTA SENDO CONTABILIZADO*/
   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, A.CDCONTRATEIO
                      FROM AJUSTEAPURCONTR A
                     WHERE A.NRSEQUMAPAPU  = P_NRSEQUMAPAPU
                       AND A.IDFORMAPUR    = 'C'
                       AND A.IDINDICAJUSTE IS NOT NULL
                       AND P_IDTABELA      = 'AC'
                       AND P_IDTPAJUSTE    = 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
                       AND P_NRSEQITEM     = A.NRSEQAJUSTE
                     UNION ALL
                    SELECT I.NRSEQITMAPR, I.IDTIPOITMAPR AS IDTPAJUSTE,
                           NVL(I.CDAJUSTEITMAPA,I.CDAJUSTEITSEF) AS CDAJUSTE,
                           I.DSITEMMAPR, I.VRITEMMAPR, I.CDCONTRATEIO
                      FROM ITMAPARECUP I
                     WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                       AND P_IDTABELA     = 'IM'
                       AND P_IDTPAJUSTE   = I.IDTIPOITMAPR
                       AND P_NRSEQITEM    = I.NRSEQITMAPR
                     UNION ALL
                    SELECT I.NRSEQITDETAPU, I.IDTIPODETAPU AS IDTPAJUSTE,
                           I.IDORIGDEDUC AS CDAJUSTE,
                           I.DSITEMDETAPU, I.VRITEMDETAPU, I.CDCONTRATEIO
                      FROM ITDETAPUCONTR I
                     WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                       AND P_IDTABELA     = 'IC'
                       AND P_IDTPAJUSTE   = I.IDTIPODETAPU
                       AND P_NRSEQITEM    = I.NRSEQITDETAPU) LOOP

      V_CDPADRAORATEIO := C_AJUSTE.CDCONTRATEIO;

      /*Verifica as parametrizacoes por codigo de ajuste*/
      BEGIN
         SELECT P.CDCONTAJCRED, P.CDCONTAJDEB, P.CDCONTRECOL
           INTO V_CDCONTCTBLCRED, V_CDCONTCTBLDEB, V_CDCONTCTBLREC
           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  := '';
      END;

      /*Verifica as parametrizacoes por tipo de ajuste*/
      IF V_CDCONTCTBLREC IS NULL THEN
         BEGIN
            SELECT P.CDCONTAJCRED, P.CDCONTAJDEB, P.CDCONTRECOL
              INTO V_CDCONTCTBLCRED, V_CDCONTCTBLDEB, V_CDCONTCTBLREC
              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  := '';
         END;

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

      IF C_AJUSTE.CDCONTRATEIO IS NOT NULL THEN
         BEGIN
            SELECT SUM(VRENTRADA) AS VRENTRADA, SUM(VRSAIDA) AS VRSAIDA,
                   SUM(VRBASEENTR) AS VRBASEENTR, SUM(VRBASESAID) AS VRBASESAID,
                   SUM(VRIMPENTRADA) AS VRIMPENTRADA, SUM(VRIMPSAIDA) AS VRIMPSAIDA,
                   IDRATEIOPADRAO, IDORIGVALOR
              INTO V_VRENTRADA, V_VRSAIDA, V_VRBASEENTR, V_VRBASESAID,
                   V_VRIMPENTRADA, V_VRIMPSAIDA, V_IDTIPORATEIO, V_IDORIGVALOR
              FROM (SELECT SUM(DECODE(NF.IDENTRSAIDOP,'E',II.VRCONTITIMP,0)) AS VRENTRADA,
                           SUM(DECODE(NF.IDENTRSAIDOP,'S',II.VRCONTITIMP,0)) AS VRSAIDA,
                           SUM(DECODE(NF.IDENTRSAIDOP,'E',DECODE(II.CDFISCALIT,'1',II.VRBASECALCIT,0),0)) AS VRBASEENTR,
                           SUM(DECODE(NF.IDENTRSAIDOP,'S',DECODE(II.CDFISCALIT,'1',II.VRBASECALCIT,0),0)) AS VRBASESAID,
                           SUM(DECODE(NF.IDENTRSAIDOP,'E',DECODE(II.CDFISCALIT,'1',II.VRIMPOSTOIT,0),0)) AS VRIMPENTRADA,
                           SUM(DECODE(NF.IDENTRSAIDOP,'S',DECODE(II.CDFISCALIT,'1',II.VRIMPOSTOIT,0),0)) AS VRIMPSAIDA,
                           RA.IDRATEIOPADRAO, RA.IDORIGVALOR
                      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 RATFISPRODUTO R
                                                WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                  AND R.IDENTRSAIDOP   = 'E')
                             UNION ALL
                            SELECT V_CDPADRAORATEIO, 'S', 'T' FROM DUAL
                             WHERE NOT EXISTS (SELECT * FROM RATFISPRODUTO R
                                                WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                  AND R.IDENTRSAIDOP   = 'S')) RP,
                           (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.IDTPPART, R.CDPARTICIPANTE
                              FROM RATFISPARTICIP R
                             WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                             UNION ALL
                            SELECT V_CDPADRAORATEIO, 'E', 'T', 'T' FROM DUAL
                             WHERE NOT EXISTS (SELECT * FROM RATFISPARTICIP R
                                                WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                  AND R.IDENTRSAIDOP   = 'E')
                             UNION ALL
                            SELECT V_CDPADRAORATEIO, 'S', 'T', 'T' FROM DUAL
                             WHERE NOT EXISTS (SELECT * FROM RATFISPARTICIP R
                                                WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                  AND R.IDENTRSAIDOP   = 'S')) RF
                     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 II.NRSEQITPROD    = IT.NRSEQITPROD
                       AND RA.CDRATEIOPADRAO = RP.CDRATEIOPADRAO
                       AND NF.IDENTRSAIDOP   = RP.IDENTRSAIDOP
                       AND (IT.CDPRODUTO = RP.CDPRODUTO OR RP.CDPRODUTO = 'T')
                       AND RA.CDRATEIOPADRAO = RF.CDRATEIOPADRAO
                       AND NF.IDENTRSAIDOP   = RF.IDENTRSAIDOP
                       AND ((NF.IDORIGDESTNF = RF.IDTPPART AND CASE WHEN NF.IDORIGDESTNF = 'F' THEN NF.CDFORNECED
                                                                    WHEN NF.IDORIGDESTNF = 'C' THEN NF.CDCLIENTE
                                                                    WHEN NF.IDORIGDESTNF = 'L' AND NF.IDENTRSAIDOP = 'E' THEN NF.CDFILIEMIT
                                                                    WHEN NF.IDORIGDESTNF = 'L' AND NF.IDENTRSAIDOP = 'S' THEN NF.CDFILIRECE
                                                               END = RF.CDPARTICIPANTE) OR (RF.IDTPPART = 'T' AND RF.CDPARTICIPANTE = 'T'))
                     GROUP BY RA.IDRATEIOPADRAO, RA.IDORIGVALOR
                     UNION ALL
                    SELECT 0 AS VRENTRADA,
                           SUM(TRUNC((IT.QTPRODVEND  * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))) - NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0),2)) AS VRSAIDA,
                           0 AS VRBASEENTR,
                           SUM(CASE WHEN IM.IDTPIMPOSFIS = 'T'
                                    THEN CASE WHEN II.VRPRBCREDUICMS > 0
                                              THEN II.VRBASECALCREDUZ
                                              ELSE TRUNC((IT.QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))) - NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0),2) END
                                    ELSE 0 END) AS VRBASESAID,
                           0 AS VRIMPENTRADA,
                           SUM(CASE WHEN IM.IDTPIMPOSFIS = 'T'
                                    THEN CASE WHEN II.VRPRBCREDUICMS > 0
                                              THEN NVL(II.VRIMPOPRODREDUZ,0)
                                              ELSE NVL(VRIMPOPRODIT,0) END
                                    ELSE 0 END) AS VRIMPSAIDA,
                           RA.IDRATEIOPADRAO, RA.IDORIGVALOR
                      FROM ITVENDAIMPOS II, ITEMVENDA IT, IMPOSTO IM, NOTAFISCAL NF, RATEIOPADRAOFIS RA,
                           (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF, R.CDFILIALVENDA, R.CDCAIXA, R.NRSEQVENDA
                              FROM IMPOSTONF I, RELACNFCEAPROV R, VENDA V
                             WHERE I.NRSEQUMAPAPU  = P_NRSEQUMAPAPU
                               AND I.CDFILIAL      = R.CDFILIALNOTA
                               AND I.NRLANCTONF    = R.NRLANCTONF
                               AND R.CDFILIALVENDA = V.CDFILIAL
                               AND R.CDCAIXA       = V.CDCAIXA
                               AND R.NRSEQVENDA    = V.NRSEQVENDA
                               AND V.IDSITUVENDA   = 'O') IP,
                           (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.CDTIPOOPER
                              FROM RATFISTPOPERAC R
                             WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                             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, '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, 'S', 'T' FROM DUAL
                             WHERE NOT EXISTS (SELECT * FROM RATFISPRODUTO R
                                                WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                  AND R.IDENTRSAIDOP   = 'S')) RP,
                           (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.IDTPPART, R.CDPARTICIPANTE
                              FROM RATFISPARTICIP R
                             WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                             UNION ALL
                            SELECT V_CDPADRAORATEIO, 'S', 'T', 'T' FROM DUAL
                             WHERE NOT EXISTS (SELECT * FROM RATFISPARTICIP R
                                                WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                  AND R.IDENTRSAIDOP   = 'S')) RF
                     WHERE IP.CDFILIALVENDA       = II.CDFILIAL
                       AND IP.CDCAIXA        = II.CDCAIXA
                       AND IP.NRSEQVENDA     = II.NRSEQVENDA
                       AND II.CDIMPOSTO      = IM.CDIMPOSTO
                       AND IP.CDFILIAL       = NF.CDFILIAL
                       AND IP.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.CDCFOPPROD    = RC.CDCFOPPADRAO OR RC.CDCFOPPADRAO = 'T')
                       AND II.CDFILIAL       = IT.CDFILIAL
                       AND II.CDCAIXA        = IT.CDCAIXA
                       AND II.NRSEQVENDA     = IT.NRSEQVENDA
                       AND II.NRSEQUITVEND   = IT.NRSEQUITVEND
                       AND RA.CDRATEIOPADRAO = RP.CDRATEIOPADRAO
                       AND NF.IDENTRSAIDOP   = RP.IDENTRSAIDOP
                       AND (IT.CDPRODUTO     = RP.CDPRODUTO OR RP.CDPRODUTO = 'T')
                       AND RA.CDRATEIOPADRAO = RF.CDRATEIOPADRAO
                       AND NF.IDENTRSAIDOP   = RF.IDENTRSAIDOP
                       AND ((NF.IDORIGDESTNF = RF.IDTPPART AND CASE WHEN NF.IDORIGDESTNF = 'F' THEN NF.CDFORNECED
                                                                    WHEN NF.IDORIGDESTNF = 'C' THEN NF.CDCLIENTE
                                                                    WHEN NF.IDORIGDESTNF = 'L' AND NF.IDENTRSAIDOP = 'S' THEN NF.CDFILIRECE
                                                               END = RF.CDPARTICIPANTE) OR (RF.IDTPPART = 'T' AND RF.CDPARTICIPANTE = 'T'))
                     GROUP BY RA.IDRATEIOPADRAO, RA.IDORIGVALOR)
              GROUP BY IDRATEIOPADRAO, IDORIGVALOR;
         EXCEPTION
             WHEN NO_DATA_FOUND THEN
                V_VRENTRADA    := 0;
                V_VRSAIDA      := 0;
                V_VRBASEENTR   := 0;
                V_VRBASESAID   := 0;
                V_VRIMPENTRADA := 0;
                V_VRIMPSAIDA   := 0;
                V_IDTIPORATEIO := '';
                V_IDORIGVALOR  := 'C';
         END;

         IF V_IDORIGVALOR = 'I' THEN /*Considera valor do imposto*/
            CASE V_IDTIPORATEIO
              WHEN 'ENT' THEN V_TOTAL := V_VRIMPENTRADA;
              WHEN 'SAI' THEN V_TOTAL := V_VRIMPSAIDA;
              WHEN 'SME' THEN V_TOTAL := V_VRIMPSAIDA - V_VRIMPENTRADA;
              ELSE V_TOTAL := 0;
            END CASE;
         ELSIF V_IDORIGVALOR = 'B' THEN /*Considera valor da base de calculo*/
            CASE V_IDTIPORATEIO
              WHEN 'ENT' THEN V_TOTAL := V_VRBASEENTR;
              WHEN 'SAI' THEN V_TOTAL := V_VRBASESAID;
              WHEN 'SME' THEN V_TOTAL := V_VRBASESAID - V_VRBASEENTR;
              ELSE V_TOTAL := 0;
            END CASE;
         ELSE /*Considera Valor Contabil*/
            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;
         END IF;

         IF V_TOTAL > 0 THEN
            --INCLUIR A VALIDACAO DE FORNECEDOR E OS DIFERENTES VALORES
            FOR C_FILIAL IN (SELECT SUM(VRENTRADA)    AS VRENTRADA,    SUM(VRSAIDA) AS VRSAIDA,
                                    SUM(VRBASEENTR)   AS VRBASEENTR,   SUM(VRBASESAID) AS VRBASESAID,
                                    SUM(VRIMPENTRADA) AS VRIMPENTRADA, SUM(VRIMPSAIDA) AS VRIMPSAIDA,
                                    CDFILIAL, IDRATEIOPADRAO, IDORIGVALOR
                               FROM (SELECT SUM(DECODE(NF.IDENTRSAIDOP,'E',II.VRCONTITIMP,0)) AS VRENTRADA,
                                            SUM(DECODE(NF.IDENTRSAIDOP,'S',II.VRCONTITIMP,0)) AS VRSAIDA,
                                            SUM(DECODE(NF.IDENTRSAIDOP,'E',DECODE(II.CDFISCALIT,'1',II.VRBASECALCIT,0),0)) AS VRBASEENTR,
                                            SUM(DECODE(NF.IDENTRSAIDOP,'S',DECODE(II.CDFISCALIT,'1',II.VRBASECALCIT,0),0)) AS VRBASESAID,
                                            SUM(DECODE(NF.IDENTRSAIDOP,'E',DECODE(II.CDFISCALIT,'1',II.VRIMPOSTOIT,0),0)) AS VRIMPENTRADA,
                                            SUM(DECODE(NF.IDENTRSAIDOP,'S',DECODE(II.CDFISCALIT,'1',II.VRIMPOSTOIT,0),0)) AS VRIMPSAIDA,
                                            DECODE(NF.IDENTRSAIDOP,'E',NF.CDFILIRECE,NF.CDFILIEMIT) AS CDFILIAL,
                                            RA.IDRATEIOPADRAO, RA.IDORIGVALOR
                                       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 RATFISPRODUTO R
                                                                 WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                                   AND R.IDENTRSAIDOP   = 'E')
                                              UNION ALL
                                             SELECT V_CDPADRAORATEIO, 'S', 'T' FROM DUAL
                                              WHERE NOT EXISTS (SELECT * FROM RATFISPRODUTO R
                                                                 WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                                   AND R.IDENTRSAIDOP   = 'S'))RP,
                                            (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.IDTPPART, R.CDPARTICIPANTE
                                               FROM RATFISPARTICIP R
                                              WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                              UNION ALL
                                             SELECT V_CDPADRAORATEIO, 'E', 'T', 'T' FROM DUAL
                                              WHERE NOT EXISTS (SELECT * FROM RATFISPARTICIP R
                                                                 WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                                   AND R.IDENTRSAIDOP   = 'E')
                                              UNION ALL
                                             SELECT V_CDPADRAORATEIO, 'S', 'T', 'T' FROM DUAL
                                              WHERE NOT EXISTS (SELECT * FROM RATFISPARTICIP R
                                                                 WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                                   AND R.IDENTRSAIDOP   = 'S')) RF
                                      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 II.NRSEQITPROD    = IT.NRSEQITPROD
                                        AND RA.CDRATEIOPADRAO = RP.CDRATEIOPADRAO
                                        AND NF.IDENTRSAIDOP   = RP.IDENTRSAIDOP
                                        AND (IT.CDPRODUTO = RP.CDPRODUTO OR RP.CDPRODUTO = 'T')
                                        AND RA.CDRATEIOPADRAO = RF.CDRATEIOPADRAO
                                        AND NF.IDENTRSAIDOP   = RF.IDENTRSAIDOP
                                        AND ((NF.IDORIGDESTNF = RF.IDTPPART AND CASE WHEN NF.IDORIGDESTNF = 'F' THEN NF.CDFORNECED
                                                                                     WHEN NF.IDORIGDESTNF = 'C' THEN NF.CDCLIENTE
                                                                                     WHEN NF.IDORIGDESTNF = 'L' AND NF.IDENTRSAIDOP = 'E' THEN NF.CDFILIEMIT
                                                                                     WHEN NF.IDORIGDESTNF = 'L' AND NF.IDENTRSAIDOP = 'S' THEN NF.CDFILIRECE
                                                                                END = RF.CDPARTICIPANTE) OR (RF.IDTPPART = 'T' AND RF.CDPARTICIPANTE = 'T'))
                                     GROUP BY DECODE(NF.IDENTRSAIDOP,'E',NF.CDFILIRECE,NF.CDFILIEMIT), RA.IDRATEIOPADRAO, RA.IDORIGVALOR
                                     UNION ALL
                                    SELECT 0 AS VRENTRADA,
                                           SUM(TRUNC((IT.QTPRODVEND  * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))) - NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0),2)) AS VRSAIDA,
                                           0 AS VRBASEENTR,
                                           SUM(CASE WHEN IM.IDTPIMPOSFIS = 'T'
                                                    THEN CASE WHEN II.VRPRBCREDUICMS > 0
                                                              THEN II.VRBASECALCREDUZ
                                                              ELSE TRUNC((IT.QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))) - NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0),2) END
                                                    ELSE 0 END) AS VRBASESAID,
                                           0 AS VRIMPENTRADA,
                                           SUM(CASE WHEN IM.IDTPIMPOSFIS = 'T'
                                                    THEN CASE WHEN II.VRPRBCREDUICMS > 0
                                                              THEN NVL(II.VRIMPOPRODREDUZ,0)
                                                              ELSE NVL(VRIMPOPRODIT,0) END
                                                    ELSE 0 END) AS VRIMPSAIDA,
                                           NF.CDFILIEMIT, RA.IDRATEIOPADRAO, RA.IDORIGVALOR
                                      FROM ITVENDAIMPOS II, ITEMVENDA IT, IMPOSTO IM, NOTAFISCAL NF, RATEIOPADRAOFIS RA,
                                          (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF, R.CDFILIALVENDA, R.CDCAIXA, R.NRSEQVENDA
                                             FROM IMPOSTONF I, RELACNFCEAPROV R, VENDA V
                                            WHERE I.NRSEQUMAPAPU  = P_NRSEQUMAPAPU
                                              AND I.CDFILIAL      = R.CDFILIALNOTA
                                              AND I.NRLANCTONF    = R.NRLANCTONF
                                              AND R.CDFILIALVENDA = V.CDFILIAL
                                              AND R.CDCAIXA       = V.CDCAIXA
                                              AND R.NRSEQVENDA    = V.NRSEQVENDA
                                              AND V.IDSITUVENDA   = 'O') IP,
                                          (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.CDTIPOOPER
                                             FROM RATFISTPOPERAC R
                                            WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                            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, '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, 'S', 'T' FROM DUAL
                                            WHERE NOT EXISTS (SELECT * FROM RATFISPRODUTO R
                                                               WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                                 AND R.IDENTRSAIDOP   = 'S')) RP,
                                          (SELECT R.CDRATEIOPADRAO, R.IDENTRSAIDOP, R.IDTPPART, R.CDPARTICIPANTE
                                             FROM RATFISPARTICIP R
                                            WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                            UNION ALL
                                           SELECT V_CDPADRAORATEIO, 'S', 'T', 'T' FROM DUAL
                                            WHERE NOT EXISTS (SELECT * FROM RATFISPARTICIP R
                                                               WHERE R.CDRATEIOPADRAO = V_CDPADRAORATEIO
                                                                 AND R.IDENTRSAIDOP   = 'S')) RF
                                    WHERE IP.CDFILIALVENDA       = II.CDFILIAL
                                      AND IP.CDCAIXA        = II.CDCAIXA
                                      AND IP.NRSEQVENDA     = II.NRSEQVENDA
                                      AND II.CDIMPOSTO      = IM.CDIMPOSTO
                                      AND IP.CDFILIAL       = NF.CDFILIAL
                                      AND IP.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.CDCFOPPROD    = RC.CDCFOPPADRAO OR RC.CDCFOPPADRAO = 'T')
                                      AND II.CDFILIAL       = IT.CDFILIAL
                                      AND II.CDCAIXA        = IT.CDCAIXA
                                      AND II.NRSEQVENDA     = IT.NRSEQVENDA
                                      AND II.NRSEQUITVEND   = IT.NRSEQUITVEND
                                      AND RA.CDRATEIOPADRAO = RP.CDRATEIOPADRAO
                                      AND NF.IDENTRSAIDOP   = RP.IDENTRSAIDOP
                                      AND (IT.CDPRODUTO     = RP.CDPRODUTO OR RP.CDPRODUTO = 'T')
                                      AND RA.CDRATEIOPADRAO = RF.CDRATEIOPADRAO
                                      AND NF.IDENTRSAIDOP   = RF.IDENTRSAIDOP
                                      AND ((NF.IDORIGDESTNF = RF.IDTPPART AND CASE WHEN NF.IDORIGDESTNF = 'F' THEN NF.CDFORNECED
                                                                                   WHEN NF.IDORIGDESTNF = 'C' THEN NF.CDCLIENTE
                                                                                   WHEN NF.IDORIGDESTNF = 'L' AND NF.IDENTRSAIDOP = 'S' THEN NF.CDFILIRECE
                                                                              END = RF.CDPARTICIPANTE) OR (RF.IDTPPART = 'T' AND RF.CDPARTICIPANTE = 'T'))
                                    GROUP BY NF.CDFILIEMIT, RA.IDRATEIOPADRAO, RA.IDORIGVALOR)
                                 GROUP BY CDFILIAL, IDRATEIOPADRAO, IDORIGVALOR) LOOP

               IF C_FILIAL.IDORIGVALOR = 'I' THEN /*Considera valor do imposto*/
                  CASE C_FILIAL.IDRATEIOPADRAO
                    WHEN 'ENT' THEN V_TOTFILIAL := C_FILIAL.VRIMPENTRADA;
                    WHEN 'SAI' THEN V_TOTFILIAL := C_FILIAL.VRIMPSAIDA;
                    WHEN 'SME' THEN V_TOTFILIAL := C_FILIAL.VRIMPSAIDA - C_FILIAL.VRIMPENTRADA;
                    ELSE V_TOTFILIAL := 0;
                  END CASE;
               ELSIF C_FILIAL.IDORIGVALOR = 'B' THEN /*Considera valor da base de calculo*/
                  CASE C_FILIAL.IDRATEIOPADRAO
                    WHEN 'ENT' THEN V_TOTFILIAL := C_FILIAL.VRBASEENTR;
                    WHEN 'SAI' THEN V_TOTFILIAL := C_FILIAL.VRBASESAID;
                    WHEN 'SME' THEN V_TOTFILIAL := C_FILIAL.VRBASESAID - C_FILIAL.VRBASEENTR;
                    ELSE V_TOTFILIAL := 0;
                  END CASE;
               ELSE /*Considera Valor Contabil*/
                  CASE C_FILIAL.IDRATEIOPADRAO
                    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;
                    ELSE V_TOTFILIAL := 0;
                  END CASE;
               END IF;

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

               /*MONTA O HISTARICO DO LANAAMENTO*/
               V_DSESTORNOAJ := SUBSTR(C_AJUSTE.DSAJUSTE,1,188)||' Filial:' || C_FILIAL.CDFILIAL;

               IF V_TOTFILIAL > 0 AND V_TOTAL > 0 THEN
                  /*GERA LOTE CONTABIL*/
                  GERA_LOTECONTABIL(P_CDEMPRESA,'01',C_FILIAL.CDFILIAL,'',V_DATACONTABIL,'','',V_CDLOTE,V_NRLANCLOTE,P_CDLOTE);

                  /*AJUSTES DE DABITO*/
                  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 CRADITO - '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 RELATARIO*/
                  INSERT INTO CTB_TEMP_CREDPRES
                     (CDFILIAL, VRTOTENTRADAS, VRTOTSAIDAS, VRCREDPRES, VRSAIDCONTAB, CDCONTCTBLDEB, CDCONTCTBLCRED,
                      DSHISTORICO, CDINSCESTA, CDINSCMUNI, NRSEQUMAPAPU, CDEMPRESA, CDLOTE)
                  VALUES
                     (C_FILIAL.CDFILIAL, NVL(C_FILIAL.VRENTRADA,0),  NVL(C_FILIAL.VRSAIDA,0), NVL(V_TOTFILIAL,0), NVL(V_VRPARTIDA,0), V_CDCONTDEBITO, V_CDCONTCREDITO,
                      NVL(V_DSHISTPADR,V_DSESTORNOAJ), P_CDINSCESTA, P_CDINSCMUNI, P_NRSEQUMAPAPU, P_CDEMPRESA, P_CDLOTE);

                  /*PARTIDA DE DABITO*/
                  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 CRADITO*/
                  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 CONTA?BIL A 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;

               V_IDGEROULOTE := 'S';
               END IF;
         END LOOP;
         END IF;
      END IF;
   END LOOP;


   IF V_IDGEROULOTE  = 'S' THEN
      UPDATE MAPAAPURAC
         SET CDLOTE       = P_CDLOTE
       WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU;
      COMMIT;

      CASE P_IDTABELA
         WHEN 'AC' THEN
            UPDATE AJUSTEAPURCONTR
               SET IDCONTABILIZADO = 'S'
             WHERE NRSEQUMAPAPU  = P_NRSEQUMAPAPU
               AND IDFORMAPUR    = 'C'
               AND P_IDTPAJUSTE  = CASE IDINDICAJUSTE||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
               AND NRSEQAJUSTE   = P_NRSEQITEM;
         WHEN 'IM' THEN
            UPDATE ITMAPARECUP
               SET IDCONTABILIZADO = 'S'
             WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
               AND IDTIPOITMAPR = P_IDTPAJUSTE
               AND NRSEQITMAPR  = P_NRSEQITEM;
         WHEN 'IC' THEN
            UPDATE ITDETAPUCONTR
               SET IDCONTABILIZADO = 'S'
             WHERE NRSEQUMAPAPU  = P_NRSEQUMAPAPU
               AND IDTIPODETAPU  = P_IDTPAJUSTE
               AND NRSEQITDETAPU = P_NRSEQITEM;
         END CASE;
   END IF;
END;
/