====== pears.iqacInvoiceOneTimesheet ======
Generated schema reference. Regenerate this page from the SQL unload; keep hand-maintained business notes in the narrative namespace.
===== Original SQL =====
create procedure "pears"."iqacInvoiceOneTimesheet"(
/* Application Maintained Function / Procedure - DO NOT EDIT*/
in "ptsid" char(20),in "pDocDate" date,in "pcoid" char(20),in "pacccode" char(12),in "ptheirref" char(50),in "pperiod" integer,
in "pnetcharge" double,in "pinvcount" integer,in "puncommittedcount" integer,in "pdocid" char(8),out "oResult" char(250),out "ocoid" char(20),out "oacccode" char(12),out "otheirref" char(50),
out "operiod" integer,out "onetcharge" double,out "oinvcount" integer,out "ouncommittedcount" integer,
out "odocid" char(8) default null )
begin
declare "res" char(250);
declare "invaddres" integer;
-- invoice finish checks
declare "currentcompanyid" char(20);
declare "currentaccountcode" char(12);
declare "currenttheirref" char(50);
declare "currentperiod" integer;
declare "netcharge" double;
-- company fields
declare "erni" integer;
declare "vate" integer;
declare "fni" double;
declare "fwtr" double;
-- ts line items
declare "vrate" double;
declare "nrechargeni" double;
declare "nfixedni" double;
declare "nfixedwtr" double;
declare "nominal2" char(20);
declare "vcode2" char(3);
declare "descrip2" long varchar;
-- ts items
declare "taxtype" integer;
declare "holallow" integer;
call "iqacStartNewInvoice"("ptsid","pcoid","pacccode","ptheirref","pperiod","pnetcharge","res","ocoid","oacccode","otheirref","operiod","onetcharge");
if "left"("res",1) = '1' then
-- if new inv :- inc invcount, set uncommitted = 0, start new invoice,
if "puncommittedcount" > 0 then -- here be bugs
call "iqacDocumentFinish"("pdocid","ptsid","pDocDate","pacccode","invaddres")
else set "invaddres" = 0
end if;
set "ouncommittedcount" = 1;
set "oinvcount" = "pinvcount"+"invaddres";
call "iqacDocumentStart"("otheirref","oacccode","ptsID","pDocDate","Res","odocid")
else
set "ouncommittedcount" = "puncommittedcount"+1;
set "oinvcount" = "pinvcount";
set "odocid" = "pdocid"
end if;
select "isnull"("ernioninvoice",0),"isnull"("vatexempt",0),"isnull"("fixedni",0.0),"isnull"("fixedwtr",0.0) into "erni","vate","fni","fwtr" from "companyaccount" where "companyid" = "ocoid";
select "isnull"("temptimesheet"."taxmethod","pay_employee"."taxmethod"),"isnull"(cast("holidaypaystatus" as varchar(1)),
"tempholidaycalc"("temptimesheet"."personid","truncnum"("period"/100,0),"remainder"("period",100),-1)) into "taxtype","holallow" from "temptimesheet" key join "person" key join "pay_employee" where "temptimesheetid" = "ptsid";
-- loop over lines
for "timesheetlines" as "tslcursor" no scroll cursor for
select "l"."linenumber",
"l"."unitscharged",
"l"."chargerate",
"round"(("l"."unitscharged"*"l"."chargerate"),2) as "charge",
("l"."unitspaid"*"l"."payrate") as "pay",
"nominalfortimesheetline"("l"."temptimesheetlineid") as "nominal",
"descriptionfortimesheetline"("l"."temptimesheetlineid") as "descrip",
"vatcodefortimesheetline"("l"."temptimesheetlineid") as "vcode",
"l"."temptimesheetlineid",
"b"."unit",
"isnull"("b"."niexempt",0) as "niexempt",
"isnull"("b"."IncludeInHolidayPay",0) as "IncludeInHolidayPay"
from "temptimesheetline" as "l" key join "temppayband" as "b"
where "l"."temptimesheetid" = "ptsid"
and "charge" <> 0.0
order by "l"."linenumber" asc for read only
do -- invoice a line (pdocid)
set "vcode2" = "vcode";
if "isnull"("vcode2",'') = '' then
if "vate" = 1 then
set "vcode2" = 'X'
else
set "vcode2" = 'S'
end if end if;
select "vatrate" into "vrate" from "iqacvatrate" where "vatcode" = "vcode2";
call "iqacCreateNominal"("nominal",'P','Temp timesheet revenue '+"nominal",'Sales','NoAccount',null,null,"res");
call "iqacPostJournalLine"("odocid","nominal","descrip","vcode2","temptimesheetlineid",'',"chargerate","vrate","unitscharged","pdocdate","unit",'T','InvoiceGoods');
-- erni recharge per ts line
if("erni" = 1) and("taxtype" = 1) then
select "round"("accordernitimesheet"("ptsid","temptimesheetlineid"),2) into "nrechargeni";
if "nrechargeni" <> 0.0 then
select "ValuesForInvoiceNIRechargeLine"("ptsid","temptimesheetlineid",'N') into "nominal2";
select "ValuesForInvoiceNIRechargeLine"("ptsid","temptimesheetlineid",'V') into "vcode2";
select "ValuesForInvoiceNIRechargeLine"("ptsid","temptimesheetlineid",'D') into "descrip2";
if "isnull"("vcode2",'') = '' then
if "vate" = 1 then
set "vcode2" = 'X'
else
set "vcode2" = 'S'
end if end if;
select "vatrate" into "vrate" from "iqacvatrate" where "vatcode" = "vcode2";
call "iqacCreateNominal"("nominal2",'P','Temp timesheet revenue '+"nominal2",'Sales','NoAccount',null,null,"res");
call "iqacpostjournalline"("odocid","nominal2","descrip2","vcode2","temptimesheetlineid",'',"nrechargeni","vrate",1,"pdocdate",'','I','InvoiceGoods')
end if end if;
-- fixed ni
if("erni" <> 1) and("fni" <> 0.0) and("niexempt" = 0) and("pay" <> 0.0) and("Taxtype" = 1) then
set "nfixedni" = "round"("pay"*"fni"/100,2);
select "ValuesForInvoiceFixedNILine"("ptsid","temptimesheetlineid",'N') into "nominal2";
select "ValuesForInvoiceFixedNILine"("ptsid","temptimesheetlineid",'V') into "vcode2";
select "ValuesForInvoiceFixedNILine"("ptsid","temptimesheetlineid",'D') into "descrip2";
if "isnull"("vcode2",'') = '' then
if "vate" = 1 then
set "vcode2" = 'X'
else
set "vcode2" = 'S'
end if end if;
select "vatrate" into "vrate" from "iqacvatrate" where "vatcode" = "vcode2";
call "iqacCreateNominal"("nominal2",'P','Temp timesheet revenue '+"nominal2",'Sales','NoAccount',null,null,"res");
call "iqacpostjournalline"("odocid","nominal2","descrip2","vcode2","temptimesheetlineid",'',"nfixedni","vrate",1,"pdocdate",'','F','InvoiceGoods')
end if;
-- fixed wtr
if("fwtr" <> 0.0) and("IncludeInHolidayPay" = 1) and("pay" <> 0.0) and("Taxtype" = 1) and("holallow" = 1) then
set "nfixedwtr" = "round"("pay"*"fwtr"/100,2);
select "ValuesForInvoiceFixedWTRLine"("ptsid","temptimesheetlineid",'N') into "nominal2";
select "ValuesForInvoiceFixedWTRLine"("ptsid","temptimesheetlineid",'V') into "vcode2";
select "ValuesForInvoiceFixedWTRLine"("ptsid","temptimesheetlineid",'D') into "descrip2";
if "isnull"("vcode2",'') = '' then
if "vate" = 1 then
set "vcode2" = 'X'
else
set "vcode2" = 'S'
end if end if;
select "vatrate" into "vrate" from "iqacvatrate" where "vatcode" = "vcode2";
call "iqacCreateNominal"("nominal2",'P','Temp timesheet revenue '+"nominal2",'Sales','NoAccount',null,null,"res");
call "iqacpostjournalline"("odocid","nominal2","descrip2","vcode2","ptsid",'',"nfixedwtr","vrate",1,"pdocdate",'','W','InvoiceGoods')
end if end for;
-- ni recharge by timesheet
if("erni" = 1) and("Taxtype" = 1) then
select "round"("accordernitimesheet"("ptsid",''),2) into "nrechargeni";
if "nrechargeni" <> 0.0 then
select "ValuesForInvoiceNIRechargeLine"("ptsid",'','N') into "nominal2";
select "ValuesForInvoiceNIRechargeLine"("ptsid",'','V') into "vcode2";
select "ValuesForInvoiceNIRechargeLine"("ptsid",'','D') into "descrip2";
if "isnull"("vcode2",'') = '' then
if "vate" = 1 then
set "vcode2" = 'X'
else
set "vcode2" = 'S'
end if end if;
select "vatrate" into "vrate" from "iqacvatrate" where "vatcode" = "vcode2";
call "iqacCreateNominal"("nominal2",'P','Temp timesheet revenue '+"nominal2",'Sales','NoAccount',null,null,"res");
call "iqacpostjournalline"("odocid","nominal2","descrip2","vcode2","temptimesheetlineid",'',"nrechargeni","vrate",1,"pdocdate",'','N','InvoiceGoods')
end if end if;
update "temptimesheet" set "billed" = 1 where "temptimesheetid" = "ptsid"
exception
when others then
rollback work
end
go
COMMENT TO PRESERVE FORMAT ON PROCEDURE "pears"."iqacInvoiceOneTimesheet" IS
{create procedure iqacInvoiceOneTimesheet
/* Application Maintained Function / Procedure - DO NOT EDIT*/
(in ptsid char(20),in pDocDate date,in pcoid char(20),in pacccode char(12),in ptheirref char(50),in pperiod integer,
in pnetcharge double,in pinvcount integer,in puncommittedcount integer,in pdocid char(8),out oResult char(250),out ocoid char(20),out oacccode char(12),out otheirref char(50),
out operiod integer,out onetcharge double,out oinvcount integer,out ouncommittedcount integer,out odocid char(8) default
null)
begin
declare res char(250);
declare invaddres integer;
-- invoice finish checks
declare currentcompanyid char(20);
declare currentaccountcode char(12);
declare currenttheirref char(50);
declare currentperiod integer;
declare netcharge double;
-- company fields
declare erni integer;
declare vate integer;
declare fni double;
declare fwtr double;
-- ts line items
declare vrate double;
declare nrechargeni double;
declare nfixedni double;
declare nfixedwtr double;
declare nominal2 char(20);
declare vcode2 char(3);
declare descrip2 long varchar;
-- ts items
declare taxtype integer;
declare holallow integer;
call iqacStartNewInvoice(ptsid,pcoid,pacccode,ptheirref,pperiod,pnetcharge,res,ocoid,oacccode,otheirref,operiod,onetcharge);
if "left"(res,1) = '1' then
-- if new inv :- inc invcount, set uncommitted = 0, start new invoice,
if puncommittedcount > 0 then -- here be bugs
call iqacDocumentFinish(pdocid,ptsid,pDocDate,pacccode,invaddres)
else set invaddres=0
end if;
set ouncommittedcount=1;
set oinvcount=pinvcount+invaddres;
call iqacDocumentStart(otheirref,oacccode,ptsID,pDocDate,Res,odocid)
else
set ouncommittedcount=puncommittedcount+1;
set oinvcount=pinvcount;
set odocid=pdocid
end if;
select isnull(ernioninvoice,0),isnull(vatexempt,0),isnull(fixedni,0.0),isnull(fixedwtr,0.0) into erni,vate,fni,fwtr from companyaccount where companyid = ocoid;
select isnull(temptimesheet.taxmethod,pay_employee.taxmethod),isnull(cast(holidaypaystatus as varchar(1)),
tempholidaycalc(temptimesheet.personid,truncnum(period/100,0),remainder(period,100),-1)) into taxtype,holallow from temptimesheet key join person key join pay_employee where temptimesheetid = ptsid;
-- loop over lines
for timesheetlines as tslcursor no scroll cursor for
select l.linenumber,
l.unitscharged,
l.chargerate,
round((l.unitscharged*l.chargerate),2) as charge,
(l.unitspaid*l.payrate) as pay,
nominalfortimesheetline(l.temptimesheetlineid) as nominal,
descriptionfortimesheetline(l.temptimesheetlineid) as descrip,
vatcodefortimesheetline(l.temptimesheetlineid) as vcode,
l.temptimesheetlineid,
b.unit,
isnull(b.niexempt,0) as niexempt,
isnull(b.IncludeInHolidayPay,0) as IncludeInHolidayPay from
temptimesheetline as l key join temppayband as b where
l.temptimesheetid = ptsid and
charge <> 0.0 order by
l.linenumber asc for read only do
-- invoice a line (pdocid)
set vcode2=vcode;
if isnull(vcode2,'') = '' then
if vate = 1 then
set vcode2='X'
else
set vcode2='S'
end if
end if;
select vatrate into vrate from iqacvatrate where vatcode = vcode2;
call iqacCreateNominal(nominal,'P','Temp timesheet revenue '+nominal,'Sales','NoAccount',null,null,res);
call iqacPostJournalLine(odocid,nominal,descrip,vcode2,temptimesheetlineid,'',chargerate,vrate,unitscharged,pdocdate,unit,'T','InvoiceGoods');
-- erni recharge per ts line
if(erni = 1) and(taxtype = 1) then
select round(accordernitimesheet(ptsid,temptimesheetlineid),2) into nrechargeni;
if nrechargeni <> 0.0 then
select ValuesForInvoiceNIRechargeLine(ptsid,temptimesheetlineid,'N') into nominal2;
select ValuesForInvoiceNIRechargeLine(ptsid,temptimesheetlineid,'V') into vcode2;
select ValuesForInvoiceNIRechargeLine(ptsid,temptimesheetlineid,'D') into descrip2;
if isnull(vcode2,'') = '' then
if vate = 1 then
set vcode2='X'
else
set vcode2='S'
end if
end if;
select vatrate into vrate from iqacvatrate where vatcode = vcode2;
call iqacCreateNominal(nominal2,'P','Temp timesheet revenue '+nominal2,'Sales','NoAccount',null,null,res);
call iqacpostjournalline(odocid,nominal2,descrip2,vcode2,temptimesheetlineid,'',nrechargeni,vrate,1,pdocdate,'','I','InvoiceGoods')
end if
end if;
-- fixed ni
if(erni <> 1) and(fni <> 0.0) and(niexempt = 0) and(pay <> 0.0) and(Taxtype = 1) then
set nfixedni=round(pay*fni/100,2);
select ValuesForInvoiceFixedNILine(ptsid,temptimesheetlineid,'N') into nominal2;
select ValuesForInvoiceFixedNILine(ptsid,temptimesheetlineid,'V') into vcode2;
select ValuesForInvoiceFixedNILine(ptsid,temptimesheetlineid,'D') into descrip2;
if isnull(vcode2,'') = '' then
if vate = 1 then
set vcode2='X'
else
set vcode2='S'
end if
end if;
select vatrate into vrate from iqacvatrate where vatcode = vcode2;
call iqacCreateNominal(nominal2,'P','Temp timesheet revenue '+nominal2,'Sales','NoAccount',null,null,res);
call iqacpostjournalline(odocid,nominal2,descrip2,vcode2,temptimesheetlineid,'',nfixedni,vrate,1,pdocdate,'','F','InvoiceGoods')
end if;
-- fixed wtr
if(fwtr <> 0.0) and(IncludeInHolidayPay = 1) and(pay <> 0.0) and(Taxtype = 1) and(holallow = 1) then
set nfixedwtr=round(pay*fwtr/100,2);
select ValuesForInvoiceFixedWTRLine(ptsid,temptimesheetlineid,'N') into nominal2;
select ValuesForInvoiceFixedWTRLine(ptsid,temptimesheetlineid,'V') into vcode2;
select ValuesForInvoiceFixedWTRLine(ptsid,temptimesheetlineid,'D') into descrip2;
if isnull(vcode2,'') = '' then
if vate = 1 then
set vcode2='X'
else
set vcode2='S'
end if
end if;
select vatrate into vrate from iqacvatrate where vatcode = vcode2;
call iqacCreateNominal(nominal2,'P','Temp timesheet revenue '+nominal2,'Sales','NoAccount',null,null,res);
call iqacpostjournalline(odocid,nominal2,descrip2,vcode2,ptsid,'',nfixedwtr,vrate,1,pdocdate,'','W','InvoiceGoods')
end if end for;
-- ni recharge by timesheet
if(erni = 1) and(Taxtype = 1) then
select round(accordernitimesheet(ptsid,''),2) into nrechargeni;
if nrechargeni <> 0.0 then
select ValuesForInvoiceNIRechargeLine(ptsid,'','N') into nominal2;
select ValuesForInvoiceNIRechargeLine(ptsid,'','V') into vcode2;
select ValuesForInvoiceNIRechargeLine(ptsid,'','D') into descrip2;
if isnull(vcode2,'') = '' then
if vate = 1 then
set vcode2='X'
else
set vcode2='S'
end if
end if;
select vatrate into vrate from iqacvatrate where vatcode = vcode2;
call iqacCreateNominal(nominal2,'P','Temp timesheet revenue '+nominal2,'Sales','NoAccount',null,null,res);
call iqacpostjournalline(odocid,nominal2,descrip2,vcode2,temptimesheetlineid,'',nrechargeni,vrate,1,pdocdate,'','N','InvoiceGoods')
end if
end if;
update temptimesheet set billed = 1 where temptimesheetid = ptsid
exception
when others then
rollback work
end
}