CREATE OR REPLACE FUNCTION VALOR_IMPOSTO_ITEM_NFE_CARGA(
   P_CDFILIAL       IN ITEMPRODUTO.CDFILIAL%TYPE,
   P_NRLANCTONF     IN ITEMPRODUTO.NRLANCTONF%TYPE,
   P_NRSEQITPROD    IN ITEMPROIMPOS.NRSEQITPROD%TYPE,
   P_IDVALOR        IN ITEMPROIMPOS.IDVALORINFIT%TYPE,
   P_IDTIPO         IN ITEMPROIMPOS.IDVALORINFIT%TYPE DEFAULT NULL,
   P_CDFISCAL       IN ITEMPROIMPOS.CDFISCALIT%TYPE   DEFAULT NULL,
   P_NRITPROIMPOS   IN ITEMPROIMPOS.NRITPROIMPOS%TYPE DEFAULT '1') RETURN NUMBER
AS
   V_VRCONTABILNF_I IMPOSTONF.VRCONTABILNF%TYPE;
   V_VRBASECALCNF_I IMPOSTONF.VRBASECALCNF%TYPE;
   V_VRALIQNF_I     IMPOSTONF.VRALIQNF%TYPE;
   V_VRIMPOSTONF_I  IMPOSTONF.VRIMPOSTONF%TYPE;
   V_VRCONTABILNF_C IMPOSTONF.VRCONTABILNF%TYPE;
   V_VRBASECALCNF_C IMPOSTONF.VRBASECALCNF%TYPE;
   V_VRALIQNF_C     IMPOSTONF.VRALIQNF%TYPE;
   V_VRIMPOSTONF_C  IMPOSTONF.VRIMPOSTONF%TYPE;
   V_VRCONTABILNF   IMPOSTONF.VRCONTABILNF%TYPE;
   V_VRBASECALCNF   IMPOSTONF.VRBASECALCNF%TYPE;
   V_VRALIQNF       IMPOSTONF.VRALIQNF%TYPE;
   V_VRIMPOSTONF    IMPOSTONF.VRIMPOSTONF%TYPE;
   V_TEMCARGA       VARCHAR2(1);
BEGIN

  IF (P_IDTIPO = 'I' OR P_IDTIPO IS NULL) THEN

     SELECT MAX('S')
       INTO V_TEMCARGA
       FROM ITEMPROIMPOS I, IMPOSTO IM
      WHERE I.CDFILIAL      = P_CDFILIAL
        AND I.NRLANCTONF    = P_NRLANCTONF
        AND I.NRSEQITPROD   = P_NRSEQITPROD
        AND I.CDIMPOSTO     = IM.CDIMPOSTO
        AND IM.CDIMPOSTONFE = '03';
      
     IF V_TEMCARGA <> 'S' THEN   
        SELECT SUM(ROUND(VRCONTITIMP,2)) AS VRCONTABILNF,
               SUM(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRBASECALCIT,0),2)) AS VRBASECALCNF,
               MAX(DECODE(IT.CDFISCALIT,'1',IT.VRALIQITEM,0))AS VRALIQNF,
               SUM(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRIMPOSTOIT, 0),2))AS VRIMPOSTONF
          INTO V_VRCONTABILNF_I, V_VRBASECALCNF_I, V_VRALIQNF_I, V_VRIMPOSTONF_I
          FROM ITEMPROIMPOS IT, IMPOSTO IP
         WHERE IT.CDFILIAL     = P_CDFILIAL
           AND IT.NRLANCTONF   = P_NRLANCTONF
           AND IT.NRSEQITPROD  = P_NRSEQITPROD
           AND IT.CDIMPOSTO    = IP.CDIMPOSTO
           AND IP.CDIMPOSTONFE = '00'
           AND IT.CDFISCALIT   = NVL(P_CDFISCAL, IT.CDFISCALIT)
           AND IT.NRITPROIMPOS = P_NRITPROIMPOS;
     END IF;    
  END IF;
  IF (P_IDTIPO = 'C' OR P_IDTIPO IS NULL) THEN
     SELECT SUM(ROUND(VRCONTITIMP,2)) AS VRCONTABILNF,
            SUM(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRBASECALCIT,0),2)) AS VRBASECALCNF,
            MAX(DECODE(IT.CDFISCALIT,'1',IT.VRALIQITEM,0)) AS VRALIQNF,
            SUM(ROUND(DECODE(IT.CDFISCALIT,'1',IT.VRIMPOSTOIT, 0),2)) AS VRIMPOSTONF
       INTO V_VRCONTABILNF_C, V_VRBASECALCNF_C, V_VRALIQNF_C, V_VRIMPOSTONF_C
       FROM ITEMPROIMPOS IT, IMPOSTO IP
      WHERE IT.CDFILIAL     = P_CDFILIAL
        AND IT.NRLANCTONF   = P_NRLANCTONF
        AND IT.NRSEQITPROD  = P_NRSEQITPROD
        AND IT.CDIMPOSTO    = IP.CDIMPOSTO
        AND IT.CDFISCALIT   = NVL(P_CDFISCAL, IT.CDFISCALIT)
        AND IP.CDIMPOSTONFE = '03'
        AND IT.NRITPROIMPOS = P_NRITPROIMPOS;
   END IF;

   V_VRCONTABILNF  := NVL(V_VRCONTABILNF_I,0) + NVL(V_VRCONTABILNF_C,0);
   V_VRBASECALCNF  := NVL(V_VRBASECALCNF_I,0) + NVL(V_VRBASECALCNF_C,0);
   IF NVL(V_VRALIQNF_C,0) > NVL(V_VRALIQNF_I,0) THEN
      V_VRALIQNF := NVL(V_VRALIQNF_C,0);
   ELSE
      V_VRALIQNF := NVL(V_VRALIQNF_I,0);
   END IF;
   V_VRIMPOSTONF   := NVL(V_VRIMPOSTONF_I,0) + NVL(V_VRIMPOSTONF_C,0);

   IF P_IDVALOR = 'A' THEN
      RETURN V_VRALIQNF;
   ELSIF P_IDVALOR = 'B' THEN
      RETURN V_VRBASECALCNF;
   ELSIF P_IDVALOR = 'I' THEN
      RETURN V_VRIMPOSTONF;
   ELSIF P_IDVALOR = 'C' THEN
      RETURN V_VRCONTABILNF;
   END IF;
END;
/