====== pears.ValuesForAWRInvoiceFixedNIWTRLine ======
Generated schema reference. Regenerate this page from the SQL unload; keep hand-maintained business notes in the narrative namespace.
===== Original SQL =====
create function "pears"."ValuesForAWRInvoiceFixedNIWTRLine"( in "tsid" char(20),in "tslineid" char(20),in "RecordType" char(1) )
returns double
begin
-- RecordType W - WTR N - NI
declare "vid" char(20);
declare "cid" char(20);
declare "rv" double;
set "rv" = 0;
select "companyid","vacancyid" into "cid","vid" from "employment" key join "placement" key join "temptimesheet" where "temptimesheetid" = "tsid";
case "RecordType"
when 'W' then //'Fixed WTR Rate. check vacancy, then company then non-awr'
select "AWRFixedWTR" into "rv" from "vacancy" where "vacancyid" = "vid";
if "isnull"("rv",0) <> 0 then return "rv"
end if;
select "AWRFixedWTR" into "rv" from "companyaccount" where "companyid" = "cid";
if "isnull"("rv",0) <> 0 then return "rv"
end if;
select "FixedWTR" into "rv" from "companyaccount" where "companyid" = "cid"
when 'N' then //'Fixed NI Rate.'
select "AWRFixedNI" into "rv" from "vacancy" where "vacancyid" = "vid";
if "isnull"("rv",0) <> 0 then return "rv"
end if;
select "AWRFixedNI" into "rv" from "companyaccount" where "companyid" = "cid";
if "isnull"("rv",0) <> 0 then return "rv"
end if;
select "FixedNI" into "rv" from "companyaccount" where "companyid" = "cid"
end case;
return("rv")
end