CREATE OR REPLACE FUNCTION TOT_MUNI_FILI(
   P_CDFILIAL  IN NOTAFISCAL.CDFILIAL%TYPE,
   P_NRLANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_IDMUNIFILI IN NOTAFISCAL.IDSTATUS%TYPE) RETURN NUMBER
AS
   V_TOTAL    NUMBER;
BEGIN

   IF P_IDMUNIFILI = 'F' THEN

      SELECT SUM(I.VRIMPOSTONF)
      INTO V_TOTAL
      FROM(SELECT SUM(I.VRIMPOSTONF) AS VRIMPOSTONF
           FROM IMPOSTONF I
           WHERE I.CDFILIAL   = P_CDFILIAL     AND
                 I.NRLANCTONF = P_NRLANCTONF   AND
                 I.CDIMPOSTO  = '31'
           UNION ALL
           SELECT SUM(I.VRIMPOSTONF) AS VRIMPOSTONF
           FROM IMPOSTONF I
           WHERE I.CDFILIAL   = P_CDFILIAL     AND
                 I.NRLANCTONF = P_NRLANCTONF   AND
                 I.CDIMPOSTO  = '31') I;

   ELSIF P_IDMUNIFILI = 'M' THEN
      SELECT SUM(I.VRIMPOSTONF)
      INTO V_TOTAL
      FROM(SELECT SUM(I.VRIMPOSTONF) AS VRIMPOSTONF
           FROM IMPOSTONF I, IMPOSTO IM
           WHERE I.CDFILIAL     = P_CDFILIAL     AND
                 I.NRLANCTONF   = P_NRLANCTONF   AND
                 I.CDIMPOSTO    = IM.CDIMPOSTO   AND
                 IM.CDIMPOSTONFE = '30'
           UNION ALL
           SELECT SUM(I.VRIMPOSTONF) AS VRIMPOSTONF
           FROM IMPOSTONF I, IMPOSTO IM
           WHERE I.CDFILIAL      = P_CDFILIAL     AND
                 I.NRLANCTONF    = P_NRLANCTONF   AND
                 IM.CDIMPOSTO    = I.CDIMPOSTO    AND
                 IM.CDIMPOSTONFE = '30') I;
   END IF;
   RETURN(V_TOTAL);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('TOT_MUNI_FILI','5.01.026'); END;
/

