CREATE OR REPLACE FUNCTION GERA_ALIQU_ST(
   P_CDFILIAL      IN FILIAL.CDFILIAL%TYPE,
   P_NRLANCTONF    IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_VRALIQNF      IN IMPOSTONF.VRALIQNF%TYPE,
   P_CDIMPOSTRIB   IN IMPOSTO.CDIMPOSTRIB%TYPE,
   P_CDIMPOSTO     IN IMPOSTO.CDIMPOSTO%TYPE) RETURN NUMBER

AS
   V_NRSEQITPROD    VARCHAR2(04);
   V_VRALIQITEM     NUMBER;
BEGIN

   SELECT MAX(T.NRSEQITPROD)
   INTO V_NRSEQITPROD
   FROM ITEMPROIMPOS I, ITEMPRODUTO T
   WHERE I.CDFILIAL    = P_CDFILIAL
     AND I.NRLANCTONF  = P_NRLANCTONF
     AND I.VRALIQITEM  = P_VRALIQNF
     AND I.CDIMPOSTO   = P_CDIMPOSTRIB
     AND I.CDFILIAL    = T.CDFILIAL
     AND I.NRLANCTONF  = T.NRLANCTONF
     AND I.NRSEQITPROD = T.NRSEQITPROD;


   SELECT SUM(VRALIQITEM) AS VRALIQITEM
   INTO V_VRALIQITEM
   FROM ITEMPROIMPOS
   WHERE CDFILIAL    = P_CDFILIAL
     AND NRLANCTONF  = P_NRLANCTONF
     AND NRSEQITPROD = V_NRSEQITPROD
     AND CDIMPOSTO   = P_CDIMPOSTO;


   RETURN(V_VRALIQITEM);
END;
/