CREATE OR REPLACE FUNCTION VALOR_IMPOSTO_NF
   (P_CDFILIAL    IN ITEMPRODUTO.CDFILIAL%TYPE,
    P_NRLANCTONF  IN ITEMPRODUTO.NRLANCTONF%TYPE,
    P_CDIMPOSTO   IN IMPOSTO.CDIMPOSTO%TYPE,
    P_IDVALOR     IN ITEMPROIMPOS.IDVALORINFIT%TYPE,
    P_CDFISCAL    IN ITEMPROIMPOS.CDFISCALIT%TYPE DEFAULT NULL) 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;
BEGIN
   SELECT NVL(SUM(VRBASECALCIT),0),
          NVL(MAX(VRALIQITEM),0),
          NVL(SUM(VRIMPOSTOIT),0),
          NVL(MAX(VRPERREDBCIT),0),
          NVL(MAX(VRCONTITIMP),0)
   INTO V_VRBASECALCIT,
        V_VRALIQITEM,
        V_VRIMPOSTOIT,
        V_VRPERREDBCIT,
        V_VRCONTITIMPO
   FROM ITEMPROIMPOS
   WHERE (CDFILIAL     = P_CDFILIAL)    AND
         (NRLANCTONF   = P_NRLANCTONF)  AND
         (CDIMPOSTO    BETWEEN P_CDIMPOSTO AND P_CDIMPOSTO) AND
         (NRITPROIMPOS = '1')           AND
         (CDFISCALIT = NVL(P_CDFISCAL, 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);
   END IF;
END;

/

-- VERSAO --
BEGIN ATUVEROBJ('VALOR_IMPOSTO_NF','5.05.025'); END;
/

