CREATE OR REPLACE PROCEDURE CALC_CRED_PRESUMIDO(
   P_NRSEQUMAPAPU    IN MAPAAPURAC.NRSEQUMAPAPU%TYPE,
   P_CDEMPRESA       IN MAPAAPURAC.CDEMPRESA%TYPE,
   P_CDIMPOSTO       IN MAPAAPURAC.CDIMPOSTO%TYPE,
   P_INSCRICAO       IN MAPAAPURAC.CDINSCESTA%TYPE,
   P_CDAJUSTEITMAPA  IN ITMAPARECUP.CDAJUSTEITMAPA%TYPE,
   P_CDITPROCEDENCIA IN ITMAPARECUP.CDITPROCEDENCIA%TYPE,
   P_CDAJUSTEITSEF   IN ITMAPARECUP.CDAJUSTEITSEF%TYPE)
AS
   V_CDAJUSTEITMAPA    VARCHAR2(10);
   V_CDITPROCEDENCIA   VARCHAR2(10);
   V_CDAJUSTEITSEF     VARCHAR2(03);
   CURSOR C_CODAJUSTECREDP (P_NRSEQUMAPAPU   IN MAPAAPURAC.NRSEQUMAPAPU%TYPE) IS
      (SELECT CDCLASFISC, CDCFOPPADRAO, CDAJUSTEITMAPA, CDAJUSTEITSEF, CDITPROCEDENCIA
         FROM ITMAPARECUP
        WHERE IDCREDPRESUMIDO = 'S'
          AND NRSEQUMAPAPU    = P_NRSEQUMAPAPU
          AND IDTIPOITMAPR    = 'OC');
   TYPE CODAJUSTECREDP IS TABLE OF C_CODAJUSTECREDP%ROWTYPE;
   V_CODAJUSTECREDP    CODAJUSTECREDP;
   V_VRALIQTOTALSAIDA  NUMBER;
   V_DSOUTCREDTOTSAI   VARCHAR2(500);
   V_CDTIPOPERCREDP    VARCHAR2(500);
   V_VRALIQIMPOSREC    NUMBER;
   V_DSOUTCREDIMPREC   VARCHAR2(500);
   V_VRTOTALSAIDA      NUMBER;
   V_VRNORMDEBI        NUMBER;
   V_VRCREDPRES        NUMBER;
   V_STDESCRICAO       VARCHAR2(500);
   V_NRSEQITMAPR       NUMBER;
   V_VRTOTAIMPOS       NUMBER;
   V_VRDEBINORMAL      NUMBER;
   V_VRTOTSAIDA        NUMBER;
   V_IDSOMAFECP        VARCHAR2(01);
   V_CADCFOP           VARCHAR2(01);
   V_CADNCM            VARCHAR2(01);
   V_CHAVEAJUSTE       VARCHAR2(50);
   V_IDUTSUBAPURCP     VARCHAR2(01);
   V_CDAJESTORNOCP     VARCHAR2(20);
   V_DSESTORNOCP       VARCHAR2(250);
   V_CDAJCREDPRES      VARCHAR2(20);
   V_CDCLASFISCCP      VARCHAR2(10);
   V_CDCFOPPADRAOCP    VARCHAR2(10);
   V_DSOUTROSCREDCP    VARCHAR2(250);
   V_VRALIQUOTACP      FLOAT;
   V_NRSEQDETITMAPA    NUMBER;
   V_NOTAFISCAL        VARCHAR2(14);
BEGIN
   /*Obs.: As alteracoes na base de calculo e debito normal no mapa de apuracao foram retiradas
     devido a legislacao que os tipos de operacao que nao pertencem ao credito
     presumido devem ser considerados normalmente na apuracao, com o valor total*/
   V_VRCREDPRES := 0;

   BEGIN
      SELECT IDSOMAFECP
        INTO V_IDSOMAFECP
        FROM AJUSTEFECP
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_INSCRICAO
         AND CDIMPOSTO    = P_CDIMPOSTO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDSOMAFECP := 'S';
   END;
   V_IDSOMAFECP := NVL(V_IDSOMAFECP,'S');

   /******** Busca a parametrizacao para a geracao do credito presumido da inscricao estadual do mapa ********/
   BEGIN
      SELECT VRALIQTOTALSAIDA, DSOUTCREDTOTSAI, CDTIPOPERCREDP,
             VRALIQIMPOSREC,   DSOUTCREDIMPREC, IDUTSUBAPURCP,
             CDAJESTORNOCP,    DSESTORNOCP,     CDAJCREDPRES
        INTO V_VRALIQTOTALSAIDA, V_DSOUTCREDTOTSAI, V_CDTIPOPERCREDP,
             V_VRALIQIMPOSREC,   V_DSOUTCREDIMPREC, V_IDUTSUBAPURCP,
             V_CDAJESTORNOCP,    V_DSESTORNOCP,     V_CDAJCREDPRES
        FROM INSCRESTAD
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_INSCRICAO
         AND IDAPURLUCPRE = 'S';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRALIQTOTALSAIDA := 0;
         V_DSOUTCREDTOTSAI  := NULL;
         V_CDTIPOPERCREDP   := NULL;
         V_VRALIQIMPOSREC   := 0;
         V_DSOUTCREDIMPREC  := NULL;
         V_IDUTSUBAPURCP    := 'N';
         V_CDAJESTORNOCP    := NULL;
         V_DSESTORNOCP      := NULL;
         V_CDAJCREDPRES     := NULL;
   END;

   /******** Verifica se utiliza parametrizacao por NCM ou CFOP, se utilizar apenas uma delas, sera analisada exclusivamente,
     mas se o cadastro for realizado nas duas abas os parametros serao validados em conjunto, so considerando itens que atendem
     ao cadastro tanto por NCM quanto por CFOP ********/
   V_CADCFOP := 'N';
   BEGIN
      SELECT MAX('S')
        INTO V_CADCFOP
        FROM CREDPRESCFOP
       WHERE CDEMPRESA  = P_CDEMPRESA
         AND CDINSCESTA = P_INSCRICAO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CADCFOP := 'N';
   END;

   V_CADNCM := 'N';
   BEGIN
      SELECT MAX('S')
        INTO V_CADNCM
        FROM CREDPRESNCM
       WHERE CDEMPRESA  = P_CDEMPRESA
         AND CDINSCESTA = P_INSCRICAO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CADNCM := 'N';
   END;

   /******** Deleta o item de outros creditos e estorno de debitos gerados pela rotina de credito presumido,
             evitando erro na reapuracao de mapas ********/
   DELETE FROM ITMAPARECUP
    WHERE NRSEQUMAPAPU    = P_NRSEQUMAPAPU
      AND IDTIPOITMAPR    IN ('OC','ED')
      AND IDCREDPRESUMIDO = 'S';

   /******** Busca os items de credito presumido que ja estavam na ITMAPARECUP, de acordo com a parametrizacao,
             para salvar os dados digitados pelo usuario, em caso de reapuracao de mapas ********/
   OPEN  C_CODAJUSTECREDP (P_NRSEQUMAPAPU);
   FETCH C_CODAJUSTECREDP BULK COLLECT INTO V_CODAJUSTECREDP;
   CLOSE C_CODAJUSTECREDP;

   /******** Credito Presumido sobre o Valor Total das Saidas ********/
   IF V_VRALIQTOTALSAIDA > 0 THEN
      BEGIN
         SELECT SUM(VRTOTALSAIDA), SUM(VRNORMDEBI)
           INTO V_VRTOTALSAIDA, V_VRNORMDEBI
           FROM (SELECT NVL(SUM(NVL(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRBASECALCIT,0),2),0.0) +
                        NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '00', 'C','','2'),0.0)),0.0) AS VRTOTALSAIDA,
                        NVL(SUM(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRIMPOSTOIT + DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICFECP,0),0),0),2)),0.0) AS VRNORMDEBI
                   FROM ITEMPROIMPOS IT, IMPOSTO IM, ITEMPRODUTO IP, PRODUTO PR,
                        (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF, N.CDEMPRESA, N.CDINSCESTA
                           FROM IMPOSTONF I, NOTAFISCAL N
                          WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                            AND I.CDFILIAL     = N.CDFILIAL
                            AND I.NRLANCTONF   = N.NRLANCTONF
                            AND I.CDFISCALNF   = '1'
                            AND I.IDIMPOSTONF  <> '3'
                            AND N.IDENTRSAIDOP = 'S'
                            AND ((INSTR(V_CDTIPOPERCREDP, '_' ||N.IDENTRSAIDOP||N.CDTIPOOPER||'_' ) > 0) OR (V_CDTIPOPERCREDP IS NULL))
                            AND N.IDSISTEMA    <> 'FIS') II
                  WHERE II.CDFILIAL     = IT.CDFILIAL
                    AND II.NRLANCTONF   = IT.NRLANCTONF
                    AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                    AND IM.CDIMPOSTONFE = '00'
                    AND IT.CDFISCALIT   = '1'
                    AND IT.NRITPROIMPOS <> '3'
                    AND IT.CDFILIAL     = IP.CDFILIAL
                    AND IT.NRLANCTONF   = IP.NRLANCTONF
                    AND IT.NRSEQITPROD  = IP.NRSEQITPROD
                    AND IP.CDPRODUTO    = PR.CDPRODUTO
                    AND IT.CDFISCOPERIT NOT IN (SELECT CDCFOPPADRAO
                                                  FROM CREDPRESCFOP
                                                 WHERE CDEMPRESA  = II.CDEMPRESA
                                                   AND CDINSCESTA = II.CDINSCESTA)
                    AND PR.CDCLASFISC   NOT IN (SELECT CDCLASFISC
                                                  FROM CREDPRESNCM
                                                 WHERE CDEMPRESA  = II.CDEMPRESA
                                                   AND CDINSCESTA = II.CDINSCESTA)
                  UNION ALL
                 SELECT NVL(SUM(NVL(ROUND(IP.VRCONTABIL,2),0.0)),0.0) AS VRTOTALSAIDA,
                        NVL(SUM(ROUND(IP.VRIMPOSTO,2)),0.0) AS VRNORMDEBI
                   FROM IMPPRODVENDAFIS IP, PRODUTO PR,
                        (SELECT DISTINCT I.CDFILIAL, N.DTEMISSAO, N.DTENTRSAID,
                                I.NRLANCTONF, N.CDFILIEMIT, N.CDEMPRESA, N.CDINSCESTA
                           FROM IMPOSTONF I, NOTAFISCAL N
                          WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                            AND I.CDFILIAL     = N.CDFILIAL
                            AND I.NRLANCTONF   = N.NRLANCTONF
                            AND I.CDFISCALNF   = '1'
                            AND I.IDIMPOSTONF  <> '3'
                            AND N.IDENTRSAIDOP = 'S'
                            AND ((INSTR(V_CDTIPOPERCREDP, '_' ||N.IDENTRSAIDOP||N.CDTIPOOPER||'_' ) > 0) OR (V_CDTIPOPERCREDP IS NULL))
                            AND N.IDSISTEMA    = 'FIS') II
                  WHERE IP.CDFILIAL      = II.CDFILIEMIT
                    AND IP.CDFILIALNFFIS = II.CDFILIAL
                    AND IP.NRLANCTONFFIS = II.NRLANCTONF
                    AND ((IP.CDFISCALIT  = '1') OR (IP.CDFISCALIT = '2' AND IP.NRITPROIMPOS = '2'))
                    AND IP.CDIMPOSTO     = '00'
                    AND IP.CDPRODUTO     = PR.CDPRODUTO
                    AND IP.CDFISCOPERIT  NOT IN (SELECT CDCFOPPADRAO
                                                   FROM CREDPRESCFOP
                                                  WHERE CDEMPRESA  = II.CDEMPRESA
                                                    AND CDINSCESTA = II.CDINSCESTA)
                    AND PR.CDCLASFISC    NOT IN (SELECT CDCLASFISC
                                                   FROM CREDPRESNCM
                                                  WHERE CDEMPRESA  = II.CDEMPRESA
                                                    AND CDINSCESTA = II.CDINSCESTA));
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_VRTOTALSAIDA := 0;
            V_VRNORMDEBI   := 0;
      END;

      V_VRCREDPRES  := NVL(V_VRTOTALSAIDA,0) * 0.01 * NVL(V_VRALIQTOTALSAIDA,0);
      V_VRCREDPRES  := ROUND(NVL(V_VRNORMDEBI,0) - NVL(V_VRCREDPRES,0),2);
      V_STDESCRICAO := V_DSOUTCREDTOTSAI;

      /******** Recupera os codigos de ajuste inseridos pelo usuario para o registro de outros creditos gerado
                pela rotina de credito presumido, parametrizado pela aliquota geral (Re-Apuracao de Mapas) ********/
      FOR I IN 1 .. V_CODAJUSTECREDP.COUNT LOOP
         IF (V_CODAJUSTECREDP(I).CDCFOPPADRAO IS NULL) AND (V_CODAJUSTECREDP(I).CDCLASFISC IS NULL) THEN
            V_CDAJUSTEITMAPA  := V_CODAJUSTECREDP(I).CDAJUSTEITMAPA;
            V_CDITPROCEDENCIA := V_CODAJUSTECREDP(I).CDITPROCEDENCIA;
            V_CDAJUSTEITSEF   := V_CODAJUSTECREDP(I).CDAJUSTEITSEF;
            EXIT;
         END IF;
      END LOOP;

   /******** Credito Presumido sobre o Imposto a Recolher ********/
   ELSIF V_VRALIQIMPOSREC > 0 THEN
      V_VRTOTAIMPOS := 0;
      BEGIN
         SELECT VRTOTAIMPOS
           INTO V_VRTOTAIMPOS
           FROM MAPAAPURAC
          WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_VRTOTAIMPOS := 0;
      END;

      V_VRCREDPRES  := ROUND(NVL(V_VRTOTAIMPOS,0) * 0.01 * NVL(V_VRALIQIMPOSREC,0),2);
      V_STDESCRICAO := NVL(V_DSOUTCREDIMPREC,0);

      /******** Recupera os codigos de ajuste inseridos pelo usuario para o registro de outros creditos gerado
                pela rotina de credito presumido, parametrizado sobre o valor total do imposto (Re-Apuracao de Mapas) ********/
      V_CDAJUSTEITMAPA  := P_CDAJUSTEITMAPA;
      V_CDITPROCEDENCIA := P_CDITPROCEDENCIA;
      V_CDAJUSTEITSEF   := P_CDAJUSTEITSEF;

   END IF;

   V_CDAJUSTEITMAPA := NVL(V_CDAJUSTEITMAPA, V_CDAJCREDPRES);

   /******** Verifica se deve ser gerado o item de outros creditos, refente ao Credito Presumido ********/
   IF V_VRCREDPRES > 0 THEN

      /******** Busca o maior sequencial de Outros Creditos ********/
      BEGIN
         SELECT NVL(MAX(NRSEQITMAPR),0) AS NRSEQITMAPR
           INTO V_NRSEQITMAPR
           FROM ITMAPARECUP
          WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
            AND IDTIPOITMAPR = 'OC';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_NRSEQITMAPR := NULL;
      END;

      V_NRSEQITMAPR := NVL(V_NRSEQITMAPR,0)+1;

      /******** Insere o item de outros creditos com o valor do Credito Presumido calculado ********/
      INSERT INTO ITMAPARECUP
         (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDITEMOCOR, DSITEMOCOR,
          DSFLEGALOCOR, NRLANCTONF, CDFILIAL, CDAJUSTEITMAPA, CDAJUSTEITSEF, CDITPROCEDENCIA,
          IDCREDPRESUMIDO, IDDIFALMANUAL, IDCARGATRIBUTARIA)
      VALUES
         (P_NRSEQUMAPAPU, 'OC', LPAD(V_NRSEQITMAPR,10,'0'), V_STDESCRICAO, V_VRCREDPRES, '', '',
          '', '', '', V_CDAJUSTEITMAPA, V_CDAJUSTEITSEF, V_CDITPROCEDENCIA,
          'S', 'S', 'N');
   END IF;

   IF NVL(V_VRALIQIMPOSREC,0) = 0 THEN
      /******** Credito Presumido sobre o Valor Total das Saidas Por CFOP ********/
      IF (V_CADCFOP = 'S') AND (V_CADNCM = 'N') THEN
         FOR C_CPCFOP IN (SELECT CDCFOPPADRAO, DSOUTROSCRED, VRALIQUOTA
                            FROM CREDPRESCFOP
                           WHERE CDEMPRESA  = P_CDEMPRESA
                             AND CDINSCESTA = P_INSCRICAO
                           ORDER BY CDCFOPPADRAO) LOOP

            BEGIN
               SELECT SUM(VRTOTALSAIDA), SUM(VRNORMDEBI)
                 INTO V_VRTOTALSAIDA, V_VRNORMDEBI
                 FROM (SELECT NVL(SUM(NVL(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRBASECALCIT,0),2),0.0) +
                              NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '00', 'C','','2'),0.0)),0.0) AS VRTOTALSAIDA,
                              NVL(SUM(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRIMPOSTOIT + DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICFECP,0),0),0),2)),0.0) AS VRNORMDEBI
                         FROM ITEMPROIMPOS IT, IMPOSTO IM, ITEMPRODUTO IP, PRODUTO PR,
                              (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF, N.CDEMPRESA, N.CDINSCESTA
                                 FROM IMPOSTONF I, NOTAFISCAL N
                                WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                                  AND I.CDFILIAL     = N.CDFILIAL
                                  AND I.NRLANCTONF   = N.NRLANCTONF
                                  AND I.CDFISCALNF   = '1'
                                  AND I.IDIMPOSTONF  <> '3'
                                  AND N.IDENTRSAIDOP = 'S'
                                  AND ((INSTR(V_CDTIPOPERCREDP, '_' ||N.IDENTRSAIDOP||N.CDTIPOOPER||'_' ) > 0) OR (V_CDTIPOPERCREDP IS NULL))
                                  AND N.IDSISTEMA    <> 'FIS') II
                        WHERE II.CDFILIAL     = IT.CDFILIAL
                          AND II.NRLANCTONF   = IT.NRLANCTONF
                          AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                          AND IM.CDIMPOSTONFE = '00'
                          AND IT.CDFISCALIT   = '1'
                          AND IT.NRITPROIMPOS <> '3'
                          AND IT.CDFISCOPERIT = C_CPCFOP.CDCFOPPADRAO
                          AND IT.CDFILIAL     = IP.CDFILIAL
                          AND IT.NRLANCTONF   = IP.NRLANCTONF
                          AND IT.NRSEQITPROD  = IP.NRSEQITPROD
                          AND IP.CDPRODUTO    = PR.CDPRODUTO
                          AND PR.CDCLASFISC NOT IN (SELECT CDCLASFISC
                                                      FROM CREDPRESNCM
                                                     WHERE CDEMPRESA  = II.CDEMPRESA
                                                       AND CDINSCESTA = II.CDINSCESTA)
                        UNION ALL
                       SELECT NVL(SUM(NVL(ROUND(IP.VRCONTABIL,2),0.0)),0.0) AS VRTOTALSAIDA,
                              NVL(SUM(ROUND(IP.VRIMPOSTO,2)),0.0) AS VRNORMDEBI
                         FROM IMPPRODVENDAFIS IP, PRODUTO PR,
                              (SELECT DISTINCT I.CDFILIAL, N.DTEMISSAO, N.DTENTRSAID,
                                      I.NRLANCTONF, N.CDFILIEMIT, N.CDEMPRESA, N.CDINSCESTA
                                 FROM IMPOSTONF I, NOTAFISCAL N
                                WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                                  AND I.CDFILIAL     = N.CDFILIAL
                                  AND I.NRLANCTONF   = N.NRLANCTONF
                                  AND I.CDFISCALNF   = '1'
                                  AND I.IDIMPOSTONF  <> '3'
                                  AND N.IDENTRSAIDOP = 'S'
                                  AND ((INSTR(V_CDTIPOPERCREDP, '_' ||N.IDENTRSAIDOP||N.CDTIPOOPER||'_' ) > 0) OR (V_CDTIPOPERCREDP IS NULL))
                                  AND N.IDSISTEMA    = 'FIS') II
                        WHERE IP.CDFILIAL      = II.CDFILIEMIT
                          AND IP.CDFILIALNFFIS = II.CDFILIAL
                          AND IP.NRLANCTONFFIS = II.NRLANCTONF
                          AND ((IP.CDFISCALIT = '1') OR (IP.CDFISCALIT = '2' AND IP.NRITPROIMPOS = '2'))
                          AND IP.CDIMPOSTO    = '00'
                          AND IP.CDPRODUTO    = PR.CDPRODUTO
                          AND IP.CDFISCOPERIT = C_CPCFOP.CDCFOPPADRAO
                          AND PR.CDCLASFISC   NOT IN (SELECT CDCLASFISC
                                                        FROM CREDPRESNCM
                                                       WHERE CDEMPRESA  = II.CDEMPRESA
                                                         AND CDINSCESTA = II.CDINSCESTA));
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_VRTOTALSAIDA := 0;
                  V_VRNORMDEBI   := 0;
            END;

            V_VRCREDPRES  := NVL(V_VRTOTALSAIDA,0) * 0.01 * NVL(C_CPCFOP.VRALIQUOTA,0);
            V_VRCREDPRES  := ROUND(NVL(V_VRNORMDEBI,0) - NVL(V_VRCREDPRES,0),2);
            V_STDESCRICAO := C_CPCFOP.DSOUTROSCRED;

            /******** Verifica se deve ser gerado o item de outros creditos, refente ao Credito Presumido ********/
            IF V_VRCREDPRES > 0 THEN

               /******** Busca o maior sequencial de Outros Creditos ********/
               BEGIN
                  SELECT NVL(MAX(NRSEQITMAPR),0) AS NRSEQITMAPR
                    INTO V_NRSEQITMAPR
                    FROM ITMAPARECUP
                   WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
                     AND IDTIPOITMAPR = 'OC';
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_NRSEQITMAPR := NULL;
               END;

               V_NRSEQITMAPR := NVL(V_NRSEQITMAPR,0)+1;

               /******** Recupera os codigos de ajuste inseridos pelo usuario para o registro de outros creditos gerado
                   pela rotina de credito presumido, parametrizado pela aliquota por CFOP (Re-Apuracao de Mapas) ********/
               FOR I IN 1 .. V_CODAJUSTECREDP.COUNT LOOP
                  IF V_CODAJUSTECREDP(I).CDCFOPPADRAO = C_CPCFOP.CDCFOPPADRAO THEN
                     V_CDAJUSTEITMAPA  := V_CODAJUSTECREDP(I).CDAJUSTEITMAPA;
                     V_CDITPROCEDENCIA := V_CODAJUSTECREDP(I).CDITPROCEDENCIA;
                     V_CDAJUSTEITSEF   := V_CODAJUSTECREDP(I).CDAJUSTEITSEF;
                     EXIT;
                  END IF;
               END LOOP;

               /******** Insere o item de outros creditos com o valor do Credito Presumido calculado ********/
               INSERT INTO ITMAPARECUP
                  (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDITEMOCOR, DSITEMOCOR,
                   DSFLEGALOCOR, NRLANCTONF, CDFILIAL, CDAJUSTEITMAPA, CDAJUSTEITSEF, CDITPROCEDENCIA,
                   IDCREDPRESUMIDO, IDDIFALMANUAL, IDCARGATRIBUTARIA, CDCFOPPADRAO)
               VALUES
                  (P_NRSEQUMAPAPU, 'OC', LPAD(V_NRSEQITMAPR,10,'0'), V_STDESCRICAO, V_VRCREDPRES, '', '',
                   '', '', '', V_CDAJUSTEITMAPA, V_CDAJUSTEITSEF, V_CDITPROCEDENCIA,
                   'S', 'S', 'N', C_CPCFOP.CDCFOPPADRAO);
            END IF;
         END LOOP;
      END IF;

      /******** Credito Presumido sobre o Valor Total das Saidas Por NCM ********/
      IF (V_CADCFOP = 'N') AND (V_CADNCM = 'S') THEN
         FOR C_CPNCM IN (SELECT CDCLASFISC, DSOUTROSCRED, VRALIQUOTA
                           FROM CREDPRESNCM
                          WHERE CDEMPRESA  = P_CDEMPRESA
                            AND CDINSCESTA = P_INSCRICAO
                          ORDER BY CDCLASFISC) LOOP

            BEGIN
               SELECT SUM(VRTOTALSAIDA), SUM(VRNORMDEBI)
                 INTO V_VRTOTALSAIDA, V_VRNORMDEBI
                 FROM (SELECT NVL(SUM(NVL(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRBASECALCIT,0),2),0.0) +
                              NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '00', 'C','','2'),0.0)),0.0) AS VRTOTALSAIDA,
                              NVL(SUM(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRIMPOSTOIT + DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICFECP,0),0),0),2)),0.0) AS VRNORMDEBI
                         FROM ITEMPROIMPOS IT, IMPOSTO IM, ITEMPRODUTO IP, PRODUTO PR,
                              (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF, N.CDEMPRESA, N.CDINSCESTA
                                 FROM IMPOSTONF I, NOTAFISCAL N
                                WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                                  AND I.CDFILIAL     = N.CDFILIAL
                                  AND I.NRLANCTONF   = N.NRLANCTONF
                                  AND I.CDFISCALNF   = '1'
                                  AND I.IDIMPOSTONF  <> '3'
                                  AND N.IDENTRSAIDOP = 'S'
                                  AND ((INSTR(V_CDTIPOPERCREDP, '_' ||N.IDENTRSAIDOP||N.CDTIPOOPER||'_' ) > 0) OR (V_CDTIPOPERCREDP IS NULL))
                                  AND N.IDSISTEMA    <> 'FIS') II
                        WHERE II.CDFILIAL     = IT.CDFILIAL
                          AND II.NRLANCTONF   = IT.NRLANCTONF
                          AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                          AND IM.CDIMPOSTONFE = '00'
                          AND IT.CDFISCALIT   = '1'
                          AND IT.NRITPROIMPOS <> '3'
                          AND IT.CDFILIAL     = IP.CDFILIAL
                          AND IT.NRLANCTONF   = IP.NRLANCTONF
                          AND IT.NRSEQITPROD  = IP.NRSEQITPROD
                          AND IP.CDPRODUTO    = PR.CDPRODUTO
                          AND PR.CDCLASFISC   = C_CPNCM.CDCLASFISC
                        UNION ALL
                       SELECT NVL(SUM(NVL(ROUND(IP.VRCONTABIL,2),0.0)),0.0) AS VRTOTALSAIDA,
                              NVL(SUM(ROUND(IP.VRIMPOSTO,2)),0.0) AS VRNORMDEBI
                         FROM IMPPRODVENDAFIS IP, PRODUTO PR,
                              (SELECT DISTINCT I.CDFILIAL, N.DTEMISSAO, N.DTENTRSAID,
                                      I.NRLANCTONF, N.CDFILIEMIT, N.CDEMPRESA, N.CDINSCESTA
                                 FROM IMPOSTONF I, NOTAFISCAL N
                                WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                                  AND I.CDFILIAL     = N.CDFILIAL
                                  AND I.NRLANCTONF   = N.NRLANCTONF
                                  AND I.CDFISCALNF   = '1'
                                  AND I.IDIMPOSTONF  <> '3'
                                  AND N.IDENTRSAIDOP = 'S'
                                  AND ((INSTR(V_CDTIPOPERCREDP, '_' ||N.IDENTRSAIDOP||N.CDTIPOOPER||'_' ) > 0) OR (V_CDTIPOPERCREDP IS NULL))
                                  AND N.IDSISTEMA    = 'FIS') II
                        WHERE IP.CDFILIAL      = II.CDFILIEMIT
                          AND IP.CDFILIALNFFIS = II.CDFILIAL
                          AND IP.NRLANCTONFFIS = II.NRLANCTONF
                          AND ((IP.CDFISCALIT = '1') OR (IP.CDFISCALIT = '2' AND IP.NRITPROIMPOS = '2'))
                          AND IP.CDIMPOSTO    = '00'
                          AND IP.CDPRODUTO    = PR.CDPRODUTO
                          AND PR.CDCLASFISC   = C_CPNCM.CDCLASFISC);
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_VRTOTALSAIDA := 0;
                  V_VRNORMDEBI   := 0;
            END;

            V_VRCREDPRES  := NVL(V_VRTOTALSAIDA,0) * 0.01 * NVL(C_CPNCM.VRALIQUOTA,0);
            V_VRCREDPRES  := ROUND(NVL(V_VRNORMDEBI,0) - NVL(V_VRCREDPRES,0),2);
            V_STDESCRICAO := C_CPNCM.DSOUTROSCRED;

            /******** Verifica se deve ser gerado o item de outros creditos, refente ao Credito Presumido ********/
            IF V_VRCREDPRES > 0 THEN

               /******** Busca o maior sequencial de Outros Creditos ********/
               BEGIN
                  SELECT NVL(MAX(NRSEQITMAPR),0) AS NRSEQITMAPR
                    INTO V_NRSEQITMAPR
                    FROM ITMAPARECUP
                   WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
                     AND IDTIPOITMAPR = 'OC';
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_NRSEQITMAPR := NULL;
               END;

               V_NRSEQITMAPR := NVL(V_NRSEQITMAPR,0)+1;

               /******** Recupera os codigos de ajuste inseridos pelo usuario para o registro de outros creditos gerado
                   pela rotina de credito presumido, parametrizado pela aliquota por CFOP (Re-Apuracao de Mapas) ********/
               FOR I IN 1 .. V_CODAJUSTECREDP.COUNT LOOP
                  IF V_CODAJUSTECREDP(I).CDCLASFISC = C_CPNCM.CDCLASFISC THEN
                     V_CDAJUSTEITMAPA  := V_CODAJUSTECREDP(I).CDAJUSTEITMAPA;
                     V_CDITPROCEDENCIA := V_CODAJUSTECREDP(I).CDITPROCEDENCIA;
                     V_CDAJUSTEITSEF   := V_CODAJUSTECREDP(I).CDAJUSTEITSEF;
                     EXIT;
                  END IF;
               END LOOP;

               /******** Insere o item de outros creditos com o valor do Credito Presumido calculado ********/
               INSERT INTO ITMAPARECUP
                  (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDITEMOCOR, DSITEMOCOR,
                   DSFLEGALOCOR, NRLANCTONF, CDFILIAL, CDAJUSTEITMAPA, CDAJUSTEITSEF, CDITPROCEDENCIA,
                   IDCREDPRESUMIDO, IDDIFALMANUAL, IDCARGATRIBUTARIA, CDCLASFISC)
               VALUES
                  (P_NRSEQUMAPAPU, 'OC', LPAD(V_NRSEQITMAPR,10,'0'), V_STDESCRICAO, V_VRCREDPRES, '', '',
                   '', '', '', V_CDAJUSTEITMAPA, V_CDAJUSTEITSEF, V_CDITPROCEDENCIA,
                   'S', 'S', 'N', C_CPNCM.CDCLASFISC);
            END IF;
         END LOOP;
      END IF;

      /******** Credito Presumido sobre o Valor Total das Saidas Por NCM e CFOP, simultaneamente ********/
      IF (V_CADCFOP = 'S') AND (V_CADNCM = 'S') THEN
         V_CHAVEAJUSTE := NULL;
         V_NOTAFISCAL  := NULL;
         /******** Busca o maior sequencial de Estorno de Debito ********/
         BEGIN
            SELECT NVL(MAX(NRSEQITMAPR),0) AS NRSEQITMAPR
              INTO V_NRSEQITMAPR
              FROM ITMAPARECUP
             WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
               AND IDTIPOITMAPR = 'ED';
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_NRSEQITMAPR := NULL;
         END;

         FOR C_CREDPRES IN (SELECT SUM(VRTOTALSAIDA) AS VRTOTALSAIDA, SUM(VRNORMDEBI) AS VRNORMDEBI, CDCLASFISC, CDCFOPPADRAO, MAX(DSOUTROSCRED) AS DSOUTROSCRED,
                                   VRALIQUOTA, CDFILIAL, NRLANCTONF, NRSEQITPROD
                              FROM (SELECT NVL(SUM(NVL(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRBASECALCIT,0),2),0.0) +
                                           NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, '00', 'C','','2'),0.0)),0.0) AS VRTOTALSAIDA,
                                           NVL(SUM(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRIMPOSTOIT + DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICFECP,0),0),0),2)),0.0) AS VRNORMDEBI,
                                           CN.CDCLASFISC, CC.CDCFOPPADRAO, MAX(CC.DSOUTROSCRED) AS DSOUTROSCRED, CC.VRALIQUOTA, II.CDFILIAL, II.NRLANCTONF, IP.NRSEQITPROD
                                      FROM ITEMPROIMPOS IT, IMPOSTO IM, ITEMPRODUTO IP, PRODUTO PR, CREDPRESNCM CN, CREDPRESCFOP CC,
                                           (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF, N.CDEMPRESA, N.CDINSCESTA
                                              FROM IMPOSTONF I, NOTAFISCAL N
                                             WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                                               AND I.CDFILIAL     = N.CDFILIAL
                                               AND I.NRLANCTONF   = N.NRLANCTONF
                                               AND I.CDFISCALNF   = '1'
                                               AND I.IDIMPOSTONF  <> '3'
                                               AND N.IDENTRSAIDOP = 'S'
                                               AND ((INSTR(V_CDTIPOPERCREDP, '_' ||N.IDENTRSAIDOP||N.CDTIPOOPER||'_' ) > 0) OR (V_CDTIPOPERCREDP IS NULL))
                                               AND N.IDSISTEMA    <> 'FIS') II
                                     WHERE II.CDFILIAL     = IT.CDFILIAL
                                       AND II.NRLANCTONF   = IT.NRLANCTONF
                                       AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                                       AND IM.CDIMPOSTONFE = '00'
                                       AND IT.CDFISCALIT   = '1'
                                       AND IT.NRITPROIMPOS <> '3'
                                       AND IT.CDFISCOPERIT = CC.CDCFOPPADRAO
                                       AND CC.CDEMPRESA    = P_CDEMPRESA
                                       AND CC.CDINSCESTA   = P_INSCRICAO
                                       AND IT.CDFILIAL     = IP.CDFILIAL
                                       AND IT.NRLANCTONF   = IP.NRLANCTONF
                                       AND IT.NRSEQITPROD  = IP.NRSEQITPROD
                                       AND IP.CDPRODUTO    = PR.CDPRODUTO
                                       AND PR.CDCLASFISC   = CN.CDCLASFISC
                                       AND CN.CDEMPRESA    = II.CDEMPRESA
                                       AND CN.CDINSCESTA   = II.CDINSCESTA
                                       AND CN.VRALIQUOTA   = CC.VRALIQUOTA
                                     GROUP BY CN.CDCLASFISC, CC.CDCFOPPADRAO, CC.VRALIQUOTA, II.CDFILIAL, II.NRLANCTONF, IP.NRSEQITPROD
                                     UNION ALL
                                    SELECT NVL(SUM(NVL(ROUND(IP.VRCONTABIL,2),0.0)),0.0) AS VRTOTALSAIDA,
                                           NVL(SUM(ROUND(IP.VRIMPOSTO,2)),0.0) AS VRNORMDEBI, CN.CDCLASFISC,
                                           CC.CDCFOPPADRAO, MAX(CC.DSOUTROSCRED) AS DSOUTROSCRED, CC.VRALIQUOTA,
                                           II.CDFILIAL, II.NRLANCTONF, '' AS NRSEQITPROD
                                      FROM IMPPRODVENDAFIS IP, PRODUTO PR, CREDPRESNCM CN, CREDPRESCFOP CC,
                                           (SELECT DISTINCT I.CDFILIAL, N.DTEMISSAO, N.DTENTRSAID,
                                                   I.NRLANCTONF, N.CDFILIEMIT, N.CDEMPRESA, N.CDINSCESTA
                                              FROM IMPOSTONF I, NOTAFISCAL N
                                             WHERE I.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                                               AND I.CDFILIAL     = N.CDFILIAL
                                               AND I.NRLANCTONF   = N.NRLANCTONF
                                               AND I.CDFISCALNF   = '1'
                                               AND I.IDIMPOSTONF  <> '3'
                                               AND N.IDENTRSAIDOP = 'S'
                                               AND ((INSTR(V_CDTIPOPERCREDP, '_' ||N.IDENTRSAIDOP||N.CDTIPOOPER||'_' ) > 0) OR (V_CDTIPOPERCREDP IS NULL))
                                               AND N.IDSISTEMA    = 'FIS') II
                                     WHERE IP.CDFILIAL      = II.CDFILIEMIT
                                       AND IP.CDFILIALNFFIS = II.CDFILIAL
                                       AND IP.NRLANCTONFFIS = II.NRLANCTONF
                                       AND ((IP.CDFISCALIT = '1') OR (IP.CDFISCALIT = '2' AND IP.NRITPROIMPOS = '2'))
                                       AND IP.CDIMPOSTO    = '00'
                                       AND IP.CDPRODUTO    = PR.CDPRODUTO
                                       AND IP.CDFISCOPERIT = CC.CDCFOPPADRAO
                                       AND CC.CDEMPRESA    = P_CDEMPRESA
                                       AND CC.CDINSCESTA   = P_INSCRICAO
                                       AND PR.CDCLASFISC   = CN.CDCLASFISC
                                       AND CN.CDEMPRESA    = II.CDEMPRESA
                                       AND CN.CDINSCESTA   = II.CDINSCESTA
                                       AND CN.VRALIQUOTA   = CC.VRALIQUOTA
                                     GROUP BY CN.CDCLASFISC, CC.CDCFOPPADRAO, CC.VRALIQUOTA, II.CDFILIAL, II.NRLANCTONF)
                             GROUP BY CDCLASFISC, CDCFOPPADRAO, VRALIQUOTA, CDFILIAL, NRLANCTONF, NRSEQITPROD
                             ORDER BY CDCLASFISC, CDCFOPPADRAO, VRALIQUOTA, CDFILIAL, NRLANCTONF) LOOP

            /******** Gera o estorno nota a nota do imposto da NF quando o mesmo tiver que ser considerado na sub-apuracao do ICMS ********/
            IF V_IDUTSUBAPURCP = 'S' THEN
               IF (V_NOTAFISCAL IS NULL) OR (V_NOTAFISCAL <> C_CREDPRES.CDFILIAL||C_CREDPRES.NRLANCTONF) THEN
                  V_NRSEQITMAPR := LPAD(TO_NUMBER(NVL(V_NRSEQITMAPR,0)) + 1,10,'0');
                  /******** Insere o item de estorno de debito com o valor de imposto da NF ********/
                  INSERT INTO ITMAPARECUP
                     (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDITEMOCOR, DSITEMOCOR,
                      DSFLEGALOCOR, CDFILIAL, NRLANCTONF, CDAJUSTEITMAPA, CDAJUSTEITSEF, CDITPROCEDENCIA,
                      IDCREDPRESUMIDO, IDDIFALMANUAL, IDCARGATRIBUTARIA, CDCLASFISC, CDCFOPPADRAO, IDCOMPOEC197, IDESTSUBAPUR)
                  VALUES
                     (P_NRSEQUMAPAPU, 'ED', V_NRSEQITMAPR, V_DSESTORNOCP, C_CREDPRES.VRNORMDEBI, '', '',
                      '', C_CREDPRES.CDFILIAL, C_CREDPRES.NRLANCTONF, V_CDAJESTORNOCP, '', '',
                      'S', 'S', 'N', C_CREDPRES.CDCLASFISC, C_CREDPRES.CDCFOPPADRAO, 'I', 'S');
                  V_NOTAFISCAL := C_CREDPRES.CDFILIAL||C_CREDPRES.NRLANCTONF;
               ELSE
                  UPDATE ITMAPARECUP
                     SET VRITEMMAPR = NVL(VRITEMMAPR,0) + C_CREDPRES.VRNORMDEBI
                   WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
                     AND IDTIPOITMAPR = 'ED'
                     AND NRSEQITMAPR  = V_NRSEQITMAPR;
               END IF;

               NOVO_CODIGO('DETITMAPARECUP',V_NRSEQDETITMAPA);
               INSERT INTO DETITMAPARECUP
                 (NRSEQDETITMAPA, NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, CDFILIAL, NRLANCTONF,
                  NRSEQITPROD, NRITPROIMPOS, CDIMPOSTO, VRIMPOSTOIT)
               VALUES
                 (V_NRSEQDETITMAPA, P_NRSEQUMAPAPU, 'ED', V_NRSEQITMAPR, C_CREDPRES.CDFILIAL,C_CREDPRES.NRLANCTONF,
                  C_CREDPRES.NRSEQITPROD, '1', '00', C_CREDPRES.VRNORMDEBI);
            END IF;

            IF (V_CHAVEAJUSTE IS NULL) THEN
               V_CHAVEAJUSTE    := C_CREDPRES.CDCLASFISC||C_CREDPRES.CDCFOPPADRAO||C_CREDPRES.VRALIQUOTA;
               V_CDCLASFISCCP   := C_CREDPRES.CDCLASFISC;
               V_CDCFOPPADRAOCP := C_CREDPRES.CDCFOPPADRAO;
               V_DSOUTROSCREDCP := C_CREDPRES.DSOUTROSCRED;
               V_VRALIQUOTACP   := C_CREDPRES.VRALIQUOTA;
               V_VRTOTALSAIDA   := 0;
               V_VRNORMDEBI     := 0;
            END IF;

            IF (V_CHAVEAJUSTE <> C_CREDPRES.CDCLASFISC||C_CREDPRES.CDCFOPPADRAO||C_CREDPRES.VRALIQUOTA) THEN
               V_VRCREDPRES  := NVL(V_VRTOTALSAIDA,0) * 0.01 * NVL(V_VRALIQUOTACP,0);
               V_VRCREDPRES  := ROUND(NVL(V_VRNORMDEBI,0) - NVL(V_VRCREDPRES,0),2);
               V_STDESCRICAO := V_DSOUTROSCREDCP;

               /******** Verifica se deve ser gerado o item de outros creditos, refente ao Credito Presumido ********/
               IF V_VRCREDPRES > 0 THEN

                  /******** Busca o maior sequencial de Outros Creditos ********/
                  BEGIN
                     SELECT NVL(MAX(NRSEQITMAPR),0) AS NRSEQITMAPR
                       INTO V_NRSEQITMAPR
                       FROM ITMAPARECUP
                      WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
                        AND IDTIPOITMAPR = 'OC';
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_NRSEQITMAPR := NULL;
                  END;

                  V_NRSEQITMAPR := NVL(V_NRSEQITMAPR,0)+1;

                  /******** Insere o item de outros creditos com o valor do Credito Presumido calculado ********/
                  INSERT INTO ITMAPARECUP
                     (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDITEMOCOR, DSITEMOCOR,
                      DSFLEGALOCOR, NRLANCTONF, CDFILIAL, CDAJUSTEITMAPA, CDAJUSTEITSEF, CDITPROCEDENCIA,
                      IDCREDPRESUMIDO, IDDIFALMANUAL, IDCARGATRIBUTARIA, CDCLASFISC, CDCFOPPADRAO, IDAJSUBAPURCP)
                  VALUES
                     (P_NRSEQUMAPAPU, 'OC', LPAD(V_NRSEQITMAPR,10,'0'), V_STDESCRICAO, V_VRCREDPRES, '', '',
                      '', '', '', V_CDAJUSTEITMAPA, V_CDAJUSTEITSEF, V_CDITPROCEDENCIA,
                      'S', 'S', 'N', V_CDCLASFISCCP, V_CDCFOPPADRAOCP, V_IDUTSUBAPURCP);
               END IF;
               V_CHAVEAJUSTE    := C_CREDPRES.CDCLASFISC||C_CREDPRES.CDCFOPPADRAO||C_CREDPRES.VRALIQUOTA;
               V_CDCLASFISCCP   := C_CREDPRES.CDCLASFISC;
               V_CDCFOPPADRAOCP := C_CREDPRES.CDCFOPPADRAO;
               V_DSOUTROSCREDCP := C_CREDPRES.DSOUTROSCRED;
               V_VRALIQUOTACP   := C_CREDPRES.VRALIQUOTA;
               V_VRTOTALSAIDA   := C_CREDPRES.VRTOTALSAIDA;
               V_VRNORMDEBI     := C_CREDPRES.VRNORMDEBI;
            ELSE
                V_VRTOTALSAIDA := NVL(V_VRTOTALSAIDA,0) + C_CREDPRES.VRTOTALSAIDA;
                V_VRNORMDEBI   := NVL(V_VRNORMDEBI,0)   + C_CREDPRES.VRNORMDEBI;
            END IF;
         END LOOP;
         /*Insere o ajuste de credito presumido para a ultima NF do loop*/
         V_VRCREDPRES  := NVL(V_VRTOTALSAIDA,0) * 0.01 * NVL(V_VRALIQUOTACP,0);
         V_VRCREDPRES  := ROUND(NVL(V_VRNORMDEBI,0) - NVL(V_VRCREDPRES,0),2);
         V_STDESCRICAO := V_DSOUTROSCREDCP;

         /******** Verifica se deve ser gerado o item de outros creditos, refente ao Credito Presumido ********/
         IF V_VRCREDPRES > 0 THEN

            /******** Busca o maior sequencial de Outros Creditos ********/
            BEGIN
               SELECT NVL(MAX(NRSEQITMAPR),0) AS NRSEQITMAPR
                 INTO V_NRSEQITMAPR
                 FROM ITMAPARECUP
                WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
                  AND IDTIPOITMAPR = 'OC';
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_NRSEQITMAPR := NULL;
            END;

            V_NRSEQITMAPR := NVL(V_NRSEQITMAPR,0)+1;

            /******** Insere o item de outros creditos com o valor do Credito Presumido calculado ********/
            INSERT INTO ITMAPARECUP
               (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDITEMOCOR, DSITEMOCOR,
                DSFLEGALOCOR, NRLANCTONF, CDFILIAL, CDAJUSTEITMAPA, CDAJUSTEITSEF, CDITPROCEDENCIA,
                IDCREDPRESUMIDO, IDDIFALMANUAL, IDCARGATRIBUTARIA, CDCLASFISC, CDCFOPPADRAO, IDAJSUBAPURCP)
            VALUES
               (P_NRSEQUMAPAPU, 'OC', LPAD(V_NRSEQITMAPR,10,'0'), V_STDESCRICAO, V_VRCREDPRES, '', '',
                '', '', '', V_CDAJUSTEITMAPA, V_CDAJUSTEITSEF, V_CDITPROCEDENCIA,
                'S', 'S', 'N', V_CDCLASFISCCP, V_CDCFOPPADRAOCP, V_IDUTSUBAPURCP);
         END IF;
      END IF;
   END IF;
END;
/