create or replace FUNCTION VALOR_IMPOSTO_ITEM_REDUZ
   (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_VRICMS       ITEMPROIMPOS.VRIMPOSTOIT%TYPE;
   V_CDEMPRESA    VARCHAR2(02);
   V_CDFILIMOVI   VARCHAR2(04);
   V_IDEXCICMSEMPR VARCHAR2(01);
   V_IDEXCICMSFILI VARCHAR2(01);
BEGIN
    BEGIN
      SELECT CDEMPRESA, CDFILIEMIT
        INTO V_CDEMPRESA, V_CDFILIMOVI
        FROM NOTAFISCAL
       WHERE CDFILIAL = P_CDFILIAL
         AND NRLANCTONF = P_NRLANCTONF;
    EXCEPTION
       WHEN NO_DATA_FOUND THEN
          V_CDEMPRESA  := NULL;
          V_CDFILIMOVI := NULL;
    END;

    BEGIN
       SELECT NVL(E.IDEXCICMSBCPICO,'N'), NVL(F.IDEXCICMSBCPICO,'N')
         INTO V_IDEXCICMSEMPR, V_IDEXCICMSFILI
         FROM EMPRESA E, FILIAL F
        WHERE E.CDEMPRESA = V_CDEMPRESA
          AND F.CDFILIAL =  V_CDFILIMOVI;
    EXCEPTION
       WHEN NO_DATA_FOUND THEN
          V_IDEXCICMSEMPR := 'N';
          V_IDEXCICMSFILI := 'N';
    END;

   SELECT NVL(SUM(I.VRIMPOSTOIT),0)
     INTO V_VRICMS
     FROM ITEMPROIMPOS I, IMPOSTO IM
    WHERE I.CDFILIAL      = P_CDFILIAL     AND
          I.NRLANCTONF    = P_NRLANCTONF   AND
          I.NRSEQITPROD   = P_NRSEQITPROD  AND
          I.CDIMPOSTO     = IM.CDIMPOSTO AND
          IM.CDIMPOSTONFE = '00'  AND
          I.NRITPROIMPOS    = P_NRITPROIMPOS AND
          I.CDFISCALIT      = NVL(P_CDFISCAL, CDFISCALIT);

       IF (NVL(V_IDEXCICMSFILI,'N') = 'S') OR (NVL(V_IDEXCICMSEMPR,'N') = 'S') THEN
         SELECT NVL(SUM(I.VRBASECALCIT-NVL(V_VRICMS,0)),0),
                 NVL(SUM(I.VRALIQITEM),0),
                 NVL(SUM(I.VRPERREDBCIT),0),
                 NVL(SUM(I.VRCONTITIMP),0)
           INTO V_VRBASECALCIT,
                V_VRALIQITEM,
                V_VRPERREDBCIT,
                V_VRCONTITIMPO
           FROM ITEMPROIMPOS I, IMPOSTO IM
          WHERE I.CDFILIAL      = P_CDFILIAL     AND
                I.NRLANCTONF    = P_NRLANCTONF   AND
                I.NRSEQITPROD   = P_NRSEQITPROD  AND
                IM.CDIMPOSTONFE = P_CDIMPOSTO    AND
                I.CDIMPOSTO     = IM.CDIMPOSTO   AND   
                I.NRITPROIMPOS  = P_NRITPROIMPOS AND
                I.CDFISCALIT = NVL(P_CDFISCAL, I.CDFISCALIT);
           V_VRIMPOSTOIT := ROUND((V_VRBASECALCIT*V_VRALIQITEM)/100,2);
      ELSE
          SELECT NVL(SUM(I.VRBASECALCIT),0),
                 NVL(SUM(I.VRALIQITEM),0),
                 NVL(SUM(I.VRIMPOSTOIT),0),
                 NVL(SUM(I.VRPERREDBCIT),0),
                 NVL(SUM(VRCONTITIMP),0)
          INTO V_VRBASECALCIT,
               V_VRALIQITEM,
               V_VRIMPOSTOIT,
               V_VRPERREDBCIT,
               V_VRCONTITIMPO
          FROM ITEMPROIMPOS I, IMPOSTO IM
          WHERE I.CDFILIAL     = P_CDFILIAL        AND
                I.NRLANCTONF   = P_NRLANCTONF      AND
                I.NRSEQITPROD  = P_NRSEQITPROD     AND
                IM.CDIMPOSTONFE = P_CDIMPOSTO      AND
                I.CDIMPOSTO     = IM.CDIMPOSTO     AND
                I.NRITPROIMPOS  = P_NRITPROIMPOS   AND
                I.CDFISCALIT = NVL(P_CDFISCAL, I.CDFISCALIT);

      END IF;
   IF P_IDVALOR = 'A' THEN
      RETURN(NVL(V_VRALIQITEM,0));
   ELSIF P_IDVALOR = 'B' THEN
      RETURN(NVL(V_VRBASECALCIT,0));
   ELSIF P_IDVALOR = 'I' THEN
      RETURN(NVL(V_VRIMPOSTOIT,0));
   ELSIF P_IDVALOR = 'P' THEN
      RETURN(NVL(V_VRPERREDBCIT,0));
   ELSIF P_IDVALOR = 'C' THEN
      RETURN(NVL(V_VRCONTITIMPO,0));
   END IF;
END;
/ 
