CREATE OR REPLACE FUNCTION VALOR_FECP_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_VRADICALIQFECP   ITEMPROIMPOS.VRADICALIQFECP%TYPE;
   V_VRADICFECP       ITEMPROIMPOS.VRADICFECP%TYPE;
BEGIN
   SELECT NVL(MAX(I.VRADICALIQFECP),0),
          NVL(SUM(I.VRADICFECP),0)
   INTO   V_VRADICALIQFECP,
          V_VRADICFECP
     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_VRADICALIQFECP);
   ELSIF P_IDVALOR = 'I' THEN
      RETURN(V_VRADICFECP);
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('VALOR_FECP_ITEM_NFE','6.19.000'); END;
/