CREATE OR replace FUNCTION VALOR_FECP_NFE(
   P_CDFILIAL       IN ITEMPRODUTO.CDFILIAL%TYPE,
   P_NRLANCTONF     IN ITEMPRODUTO.NRLANCTONF%TYPE,
   P_CDIMPOSTONFE   IN IMPOSTO.CDIMPOSTO%TYPE,
   P_IDVALOR        IN ITEMPROIMPOS.IDVALORINFIT%TYPE,
   P_CDFISCAL       IN ITEMPROIMPOS.CDFISCALIT%TYPE DEFAULT NULL) RETURN NUMBER
AS
   V_VRADICALIQFECP   ITEMPROIMPOS.VRADICALIQFECP%TYPE;
   V_VRADICFECP       ITEMPROIMPOS.VRADICFECP%TYPE;
BEGIN
   SELECT NVL(MAX(IT.VRADICALIQFECP),0),
          NVL(SUM(ROUND(IT.VRADICFECP,2)),0)
     INTO V_VRADICALIQFECP,
          V_VRADICFECP
     FROM ITEMPROIMPOS IT, IMPOSTO IP
    WHERE CDFILIAL     = P_CDFILIAL
      AND NRLANCTONF   = P_NRLANCTONF
      AND CDIMPOSTONFE = P_CDIMPOSTONFE
      AND NRITPROIMPOS = '1'
      AND IT.CDIMPOSTO = IP.CDIMPOSTO
      AND CDFISCALIT = NVL(P_CDFISCAL, CDFISCALIT);
   IF P_IDVALOR = 'A' THEN
      RETURN(V_VRADICALIQFECP);
   ELSIF P_IDVALOR = 'I' THEN
      RETURN(V_VRADICFECP);
   END IF;
END;