CREATE OR REPLACE FUNCTION DADOSFISC_ITEMPROIMPOS_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_NRITPROIMPOS IN ITEMPROIMPOS.NRITPROIMPOS%TYPE DEFAULT '1') RETURN NUMBER
AS
   V_CDFISCOPERIT     ITEMPROIMPOS.CDFISCOPERIT%TYPE;
   V_CDSITUTRIBIT     ITEMPROIMPOS.CDSITUTRIBIT%TYPE;
   V_CDSITUTRIBITNFE  ITEMPROIMPOS.CDSITUTRIBITNFE%TYPE;
   V_CDFISCALIT       ITEMPROIMPOS.CDFISCALIT%TYPE;
BEGIN
   SELECT MAX(I.CDFISCOPERIT),
          MAX(I.CDSITUTRIBIT),
          MAX(I.CDSITUTRIBITNFE),
          MAX(I.CDFISCALIT)
     INTO V_CDFISCOPERIT,
          V_CDSITUTRIBIT,
          V_CDSITUTRIBITNFE,
          V_CDFISCALIT
     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;
   IF P_IDVALOR = 'P' THEN
      RETURN(V_CDFISCOPERIT);
   ELSIF P_IDVALOR = 'T' THEN
      RETURN(V_CDSITUTRIBIT);
   ELSIF P_IDVALOR = 'E' THEN
      RETURN(V_CDSITUTRIBITNFE);
   ELSIF P_IDVALOR = 'C' THEN
      RETURN(V_CDFISCALIT);
   END IF;
END;
/ 