CREATE OR REPLACE FUNCTION VALOR_IMPOSTO_ITEM_AF
   (P_CDFILDEST   IN AUTOFORNE.CDFILDEST%TYPE,
    P_CDFILLANC   IN AUTOFORNE.CDFILLANC%TYPE,
    P_NRAUTOFORN  IN AUTOFORNE.NRAUTOFORN%TYPE,
    P_DTPROGENTAF IN ITEMAUTF.DTPROGENTAF%TYPE,
    P_CDPRODUTO   IN ITEMAUTF.CDPRODUTO%TYPE,
    P_CDMARCA     IN ITEMAUTF.CDMARCA%TYPE) RETURN FLOAT
AS
   V_VRIMPITMAF FLOAT;
BEGIN

   SELECT SUM(DECODE(IP.IDTRATIMPO, 'A', VRIMPITMAF, (VRIMPITMAF * -1))* ((IT.QTPROGENTAF - NVL(IT.QTEFETENTRAF,0)) / IT.QTPROGENTAF))
   INTO V_VRIMPITMAF
   FROM IMPITMAF I, IMPOSTO IP, ITEMAUTF IT
   WHERE (I.CDFILDEST   = P_CDFILDEST)    AND
         (I.CDFILLANC   = P_CDFILLANC)    AND
         (I.NRAUTOFORN  = P_NRAUTOFORN)   AND
         (I.DTPROGENTAF = P_DTPROGENTAF)  AND
         (I.CDPRODUTO   = NVL(P_CDPRODUTO,I.CDPRODUTO))   AND
         (I.CDMARCA     = NVL(P_CDMARCA,I.CDMARCA))     AND
         (IDTRATIMPO IN ('A', 'D'))       AND
         (I.CDFILDEST   = IT.CDFILDEST)   AND
         (I.CDFILLANC   = IT.CDFILLANC)   AND
         (I.NRAUTOFORN  = IT.NRAUTOFORN)  AND
         (I.DTPROGENTAF = IT.DTPROGENTAF) AND
         (I.CDPRODUTO   = IT.CDPRODUTO)   AND
         (I.CDMARCA     = IT.CDMARCA)     AND
         (IT.QTPROGENTAF <> 0)            AND
         (I.CDIMPOSTO = IP.CDIMPOSTO)     AND
         NVL(IP.IDACRPRODENTRNF, 'N') = 'N';

   RETURN(V_VRIMPITMAF);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('VALOR_IMPOSTO_ITEM_AF','5.05.022'); END;
/

