CREATE OR REPLACE FUNCTION VALOR_IMPOSTO_ITEM_NFE
  (P_CDFILIAL     IN ITEMPRODUTO.CDFILIAL%TYPE,
   P_NRLANCTONF   IN ITEMPRODUTO.NRLANCTONF%TYPE,
   P_NRSEQITPROD  IN ITEMPRODUTO.NRSEQITPROD%TYPE,
   P_CDIMPOSTO    IN IMPOSTO.CDIMPOSTO%TYPE,
   P_IDVALOR      IN ITEMPROIMPOS.IDVALORINFIT%TYPE,
   P_CDFISCAL     IN ITEMPROIMPOS.CDFISCALIT%TYPE DEFAULT NULL,
   P_NRITPROIMPOS IN ITEMPROIMPOS.NRITPROIMPOS%TYPE DEFAULT '1') RETURN NUMBER
AS
   V_VRBASECALCIT ITEMPROIMPOS.VRBASECALCIT%TYPE;
   V_VRALIQITEM   ITEMPROIMPOS.VRALIQITEM%TYPE;
   V_VRIMPOSTOIT  ITEMPROIMPOS.VRIMPOSTOIT%TYPE;
   V_VRPERREDBCIT ITEMPROIMPOS.VRPERREDBCIT%TYPE;
   V_VRCONTITIMPO ITEMPROIMPOS.VRCONTITIMP%TYPE;
   V_VRIMPOSTOXML ITEMPROIMPOS.VRIMPOSTOIT%TYPE;
BEGIN
   SELECT NVL(SUM(ROUND(I.VRBASECALCIT,2)),0),
          NVL(MAX(I.VRALIQITEM),0),
          NVL(SUM(ROUND(I.VRIMPOSTOIT,2)),0),
          NVL(MAX(I.VRPERREDBCIT),0),
          NVL(SUM(ROUND(I.VRCONTITIMP,2)),0),
          NVL(SUM(CASE WHEN I.VRIMPOSTOXML IS NULL 
                       THEN ROUND(DECODE(I.CDFISCALIT,'1',I.VRIMPOSTOIT,0),2)
                       ELSE ROUND(I.VRIMPOSTOXML,2) 
                   END),0)
     INTO V_VRBASECALCIT,
          V_VRALIQITEM,
          V_VRIMPOSTOIT,
          V_VRPERREDBCIT,
          V_VRCONTITIMPO,
          V_VRIMPOSTOXML
     FROM ITEMPROIMPOS I, IMPOSTO P
    WHERE I.CDFILIAL     = P_CDFILIAL     AND
          I.NRLANCTONF   = P_NRLANCTONF   AND
          I.NRSEQITPROD  = P_NRSEQITPROD  AND
          I.CDIMPOSTO    = P.CDIMPOSTO    AND
          P.CDIMPOSTONFE = P_CDIMPOSTO    AND
          I.NRITPROIMPOS = P_NRITPROIMPOS AND
          I.CDFISCALIT   = NVL(P_CDFISCAL, I.CDFISCALIT);
   IF P_IDVALOR = 'A' THEN
      RETURN(V_VRALIQITEM);
   ELSIF P_IDVALOR = 'B' THEN
      RETURN(V_VRBASECALCIT);
   ELSIF P_IDVALOR = 'I' THEN
      RETURN(V_VRIMPOSTOIT);
   ELSIF P_IDVALOR = 'P' THEN
      RETURN(V_VRPERREDBCIT);
   ELSIF P_IDVALOR = 'C' THEN
      RETURN(V_VRCONTITIMPO);
   ELSIF P_IDVALOR = 'X' THEN
      RETURN(V_VRIMPOSTOXML);
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('VALOR_IMPOSTO_ITEM_NFE','6.49.011'); END;
/
