CREATE OR REPLACE FUNCTION adamlive.
get_exrate(p_company character varying,
p_currency character varying, p_sellingrate character varying, p_asondate date)
RETURNS double precision
LANGUAGE plpgsql
AS $function$
DECLARE
l_result numeric(15,6);
bcurr varchar(15);
maxdate date;
BEGIN
select basecurrency into bcurr from company where companyname=p_company;
select max(docdate) into maxdate from mg_exrates where currency=p_currency and
docdate<=p_asondate and active='T';
if p_currency=bcurr then
l_result := 1;
end if;
if p_currency<>bcurr then
select case when p_sellingrate='F' then bexrate else sexrate end into
l_result
from mg_exrates
where docdate =maxdate
and currency=p_currency and active='T';
end if;
return l_result;
END;
$function$
;