CREATE OR REPLACE FUNCTION VALOR_IMPOSTO_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) RETURN FLOAT
AS
   V_VRIMPITMAF FLOAT;
BEGIN

   SELECT SUM(DECODE(IP.IDTRATIMPO, 'A', VRIMPITMAF, (VRIMPITMAF * -1)))
   INTO V_VRIMPITMAF
   FROM IMPITMAF I, IMPOSTO IP
   WHERE (CDFILDEST   = P_CDFILDEST)   AND
         (CDFILLANC   = P_CDFILLANC)   AND
         (NRAUTOFORN  = P_NRAUTOFORN)  AND
         (DTPROGENTAF = P_DTPROGENTAF) AND
         (IDTRATIMPO IN ('A', 'D'))    AND
         (I.CDIMPOSTO = IP.CDIMPOSTO)  AND
         NVL(IP.IDACRPRODENTRNF, 'N') = 'N';

   RETURN(V_VRIMPITMAF);
END;
/

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

