CREATE OR REPLACE FUNCTION get_kiskerar(date, bigint)
  RETURNS double precision AS
'declare v_egysegar float8; v_naplo_id integer; 
begin 
   select max(n.kod) into v_naplo_id 
     from tetel_egysegar_naplo n, egysegarak e 
    where n.egysegar_kod = e.kod 
      and e.alapertelmezett = 1 
      and n.datum<=$1+1 
      and n.tetel_kod = $2; 

   select n.egysegar_brutto into v_egysegar 
     from tetel_egysegar_naplo n 
    where n.kod = v_naplo_id; 

  return v_egysegar; 

end; '
  LANGUAGE 'plpgsql' VOLATILE;