====== pears.IQXPopulateAccountStatisticsIQAc ======
Generated schema reference. Regenerate this page from the SQL unload; keep hand-maintained business notes in the narrative namespace.
===== Original SQL =====
create procedure "pears"."IQXPopulateAccountStatisticsIQAc"( in @FromDate date,in @ToDate date,in @TransferBatch integer,in @AccountFilter char(30),in @DivFilter char(30),in @DeptFilter char(30) )
begin
--
--
--
declare @AnalysisBatch integer;
declare @DoNothing smallint;
declare @AccountFilterLike char(40);
declare @DivFilterLike char(40);
declare @DeptFilterLike char(40);
declare @TimeSheetLines integer;
--
--
-- create temporary table (DO NOT ALTER THIS SECTION)
--
--
--
--
-- work out the like clause if any
--
--
if(@AccountFilter is null or "trim"(@AccountFilter) = '') then
set @AccountFilterLike = '%'
else
set @AccountFilterLike = "string"('[',"trim"(@AccountFilter),']%')
end if;
if(@DivFilter is null or "trim"(@DivFilter) = '') then
set @DivFilterLike = '%'
else
set @DivFilterLike = "string"('[',"trim"(@DivFilter),']%')
end if;
if(@DeptFilter is null or "trim"(@DeptFilter) = '') then
set @DeptFilterLike = '%'
else
set @DeptFilterLike = "string"('[',"trim"(@DeptFilter),']%')
end if;
--
--
-- Are there any Timesheet lines that cover the search criocumentdateteria, if not abort
--
--
if "Isnull"(@TransferBatch,0) > 0 then
select "count"()
into @TimeSheetLines
from "temptimesheetline" key join "temptimesheet" as "t" left outer join("placement" key join "employment" key join "company")
where "isnull"("t"."TransferBatch",0) > 0 and "company"."clientcode" like @AccountFilterLike
and "isnull"("t"."AnalysisBatch",0) = 0
and "isnull"("company"."divisionid",'') like @DivFilterLike
and "isnull"("placement"."departmentid",'') like @DeptFilterLike
else
select "count"()
into @TimeSheetLines
from "temptimesheetline" key join "temptimesheet" as "t" left outer join("placement" key join "employment" key join "company")
where "company"."clientcode" like @AccountFilterLike and "isnull"("t"."AnalysisBatch",0) = 0
and "date"("t"."WhenEntered") between @FromDate and @ToDate
and "isnull"("company"."divisionid",'') like @DivFilterLike
and "isnull"("placement"."departmentid",'') like @DeptFilterLike
end if;
if "isnull"(@TimeSheetLines,0) = 0 then return end if;
--
--
-- Get the next analysis batch number
--
--
begin
update "params" set "LastAnalysisBatch" = "isnull"("LastAnalysisBatch",0)+1;
select "LastAnalysisBatch" into @AnalysisBatch from "params";
commit work
end;
-- Should only be needed if debugging - table should not exist
begin
drop table "pears"."TEMPAccountsStatistics"
exception
when others then set @DoNothing = 1
end;
--
-- create temporary table (DO NOT ALTER THIS SECTION)
--
create local temporary table "pears"."TEMPAccountsStatistics"(
"AccountsStatisticsID" char(20) not null,
"AccountingDate" date null,
"Amount" numeric(12,2) null,
"Description" char(100) null,
"Type" char(20) null,
"NominalCodeSegment1" char(12) null,
"NominalCodeSegment2" char(12) null,
"NominalCodeSegment3" char(12) null,
"NominalCodeSegment4" char(12) null,
"NominalCodeSegment5" char(12) null,
"PayrollCompany" char(10) null,
"DivisionAnalysisCode" char(50) null,
"DepartmentAnalysisCode" char(50) null,
"TimesheetAnalysisCode" char(50) null,
"TimesheetConsultantAnalysisCode" char(50) null,
"PlacementConsultantAnalysisCode" char(50) null,
"vacancyConsultantAnalysisCode" char(50) null,
"ConsultantAnalysisCode" char(50) null,
"ShiftConsultantAnalysisCode" char(50) null,
"ClientAccountCode" char(20) null,
"CompanyID" char(20) null,
"PersonID" char(20) null,
"TempTimesheetID" char(20) null,
"TempTimesheetLineID" char(20) null,
"TempDeskID" char(20) null,
"TimeSheetStaffID" char(20) null,
"VacancyStaffID" char(20) null,
"PlacementStaffID" char(20) null,
"PayrollNumber" char(20) null,
"PersonName" char(50) null,
"CompanyName" char(50) null,
"TimesheetSerial" char(20) null,
"TimesheetLineNumber" smallint null,
"DocumentNumber" char(20) null,
"DocumentID" char(20) null,
"DocumentDate" date null,
"JournalLineNumber" smallint null,
"SalesOrCost" char(1) null,
"Currency" char(3) null,
"PlacementID" char(20) null,
"VacancyID" char(20) null,
"TempShiftID" char(20) null,
"TempShiftDate" date null,
"DocumentLineType" char(1) null,
"Quantity" double null,
"JournalClass" char(20) null,
"Unitdescription" char(50) null,
"PayBandPayrollFlag" char(10) null, // payrollflag
"IsExpenses" smallint null,
"TaxMethod" integer null,
"Paid" double null,
"HolPay" double null,
"HolLiab" double null,
"AWRHolRate" double null,
"Levy" double null,
"HolLiabNI" double null,
"ActualNI" double null,
"Pension" double null,
"Statutory" double null,
"Charged" double null,
"TempdeskAnalysiscode" char(20) null,
"GoodsAmount" numeric(12,2) null,
"PriceEach" double null,
"VatAmount" double null,
"VatCode" char(3) null,
"JournalNominalCode" char(12) null,
"PayrollIdentifier" char(1) null,
"Period" integer null,
"TimesheetSupplierCode" char(12) null,
"ShiftAnalysisCode" char(20) null,
"ShiftReferenceCode" char(20) null,
"ShiftSerialNumber" bigint null,
"TempShiftTypeID" char(20) null,
"AnalysisBatch" integer null,
"TransferBatch" integer null,
"EmploymentPosition" char(50) null,
"EssentialSkillGradeID" char(4) null,
"vacancytheirref" char(50) null,
"vacancyrefcode" char(20) null,
"CustomString1" char(40) null,
"CustomString2" char(40) null,
"CustomString3" char(40) null,
"CustomString4" char(40) null,
"CustomString5" char(40) null,
"CustomDouble1" double null,
"CustomDouble2" double null,
"CustomDouble3" double null,
"CustomDouble4" double null,
"CustomDouble5" double null,
primary key("AccountsStatisticsID"),
) on commit delete rows;
create index "a" on "pears"."TEMPAccountsStatistics"("TempTimesheetID");
create index "b" on "pears"."TEMPAccountsStatistics"("PersonID");
create index "c" on "pears"."TEMPAccountsStatistics"("PayrollNumber");
create index "d" on "pears"."TEMPAccountsStatistics"("TempTimesheetLineID");
create index "e" on "pears"."TEMPAccountsStatistics"("NominalCodeSegment1");
create index "f" on "pears"."TEMPAccountsStatistics"("NominalCodeSegment2");
create index "g" on "pears"."TEMPAccountsStatistics"("NominalCodeSegment3");
create index "h" on "pears"."TEMPAccountsStatistics"("NominalCodeSegment4");
create index "i" on "pears"."TEMPAccountsStatistics"("NominalCodeSegment5");
create index "j" on "pears"."TEMPAccountsStatistics"("PlacementID");
create index "l" on "pears"."TEMPAccountsStatistics"("TempTimesheetLineID");
create index "k" on "pears"."TEMPAccountsStatistics"("DocumentID");
begin atomic
--
--
-- Update the Temptimesheet lines with the new Analysis batch
-- if transfer batch > 0 then use this as a filter, ie records in transfer batch will mirror the transfer batch number
-- if transferbatch = 0, then pick up journal records in the date range @PeriodFrom to @periodto
--
if "Isnull"(@TransferBatch,0) > 0 then
update "Temptimesheet" as "t"
set "t"."AnalysisBatch" = @AnalysisBatch from
"temptimesheet" as "t" left outer join("placement" key join "employment" key join "company")
where "isnull"("t"."TransferBatch",0) = @transferbatch and "company"."clientcode" like @AccountFilterLike
and "isnull"("t"."AnalysisBatch",0) = 0
and "isnull"("company"."divisionid",'') like @DivFilterLike
and "isnull"("placement"."departmentid",'') like @DeptFilterLike
else
update "Temptimesheet" as "t"
set "t"."AnalysisBatch" = @AnalysisBatch from
"temptimesheet" as "t" left outer join("placement" key join "employment" key join "company")
where "company"."clientcode" like @AccountFilterLike and "isnull"("t"."AnalysisBatch",0) = 0
and "date"("t"."whenentered") between @FromDate and @ToDate
and "isnull"("company"."divisionid",'') like @DivFilterLike
and "isnull"("placement"."departmentid",'') like @DeptFilterLike
end if;
--
--
-- Populate temporary table with Data, fixed data first. Should not needs to change this section
--
--
--
--
-- timesheet line related details, dont change this section
--
--
insert into "TEMPAccountsStatistics"( "AccountsStatisticsID",
"TemptimesheetID","TimesheetSerial","TimesheetLineNumber","Personid","PayrollNumber","Placementid","Tempdeskid",
"TimesheetAnalysisCode","tempshiftid","paid","PayBandPayRollFlag","IsExpenses","charged","AnalysisBatch",
"TaxMethod","TempTimesheetLineID","Period","Quantity",
"Unitdescription","accountingdate","transferbatch" )
select "uniquekey"("number"()),
"tl"."TempTimesheetID",
"t"."SerialNumber",
"tl"."LineNumber",
"t"."personid",
"t"."payrollnumber",
"t"."PlacementID",
"t"."TempDeskID",
"t"."AnalysisCode",
"tl"."TempShiftID",
"isnull"("tl"."unitspaid"*"tl"."payrate",0) as "paid",
"PayrollFlag",
"ISNULL"("IsExpenses",0),
"isnull"("tl"."UnitsCharged"*"tl"."ChargeRate",0) as "charged",
"AnalysisBatch","TaxMethod","tl"."TempTimesheetLineID","Period",
"isnull"("unitspaid",0),
"b"."unit","whenentered","t"."transferbatch"
from "TempTimesheetLine" as "tl"
key join("TempTimesheet" as "t","temppayband" as "b") where "analysisbatch" = @AnalysisBatch;
--
--
update "TEMPAccountsStatistics" as "tas" set "journallinenumber" = "j"."linenumber","amount" = "j"."amount",
"goodsamount" = "j"."goodsamount","JournalNominalCode" = "j"."NominalCode",
"journalclass" = "j"."journalclass","priceeach" = "j"."priceeach","vatamount" = "j"."vatamount","vatcode" = "j"."vatcode",
"description" = "j"."description","documentid" = "j"."documentid" from
"TEMPAccountsStatistics" as "tas" join "iqacjournal" as "j" on "xref" = 'T' and "xrefid" = "tas"."temptimesheetlineid";
update "TEMPAccountsStatistics" as "tas" set "documentnumber" = "ourref","DocumentDate" = "d"."Documentdate" from
"TEMPAccountsStatistics" as "tas" join "iqacdocument" as "d" on "tas"."documentid" = "d"."documentid" and "d"."ledgerid" = 'SALES';
--
-- Person ID when no timesheet line
--
--
update "TEMPAccountsStatistics" as "tas"
set "vacancyid" = "v"."vacancyid","vacancytheirref" = "v"."theirref","vacancyrefcode" = "v"."refcode" from
"TEMPAccountsStatistics" as "tas" join "placement" as "p" on "tas"."placementID" = "p"."placementID" key join("vacancy" as "v","employment" as "e");
--
--
-- Employment Position, Placement Temp, Currency
--
--
update "TEMPAccountsStatistics" as "tas" set "EmploymentPosition" = "Employment"."Position",
"currency" = "isnull"("tas"."currency","p"."currency") from
"TEMPAccountsStatistics" as "tas" join "placement" as "p" on "tas"."placementID" = "p"."placementID"
,"placement" as "p" key join "employment";
--
-- Employment Position, Placement Temp, Currency
--
--
update "TEMPAccountsStatistics" as "tas"
set "Period" = "t"."Period","payrollidentifier" = "t"."payrollidentifier","TimesheetSupplierCode" = "t"."SupplierCode" from
"TEMPAccountsStatistics" as "tas" join "TempTimesheet" as "t" on "tas"."TempTimesheetID" = "t"."TempTimesheetID";
--
-- Shift Fields
--
--
update "TEMPAccountsStatistics" as "tas"
set "ShiftReferenceCode" = "t"."referencecode","ShiftSerialNumber" = "p"."shiftserialnumber","tempshiftdate" = "t"."shiftdate",
"Shiftanalysiscode" = "t"."AnalysisCode","EssentialSkillGradeID" = "t"."EssentialSkillGradeID",
"TempShifttypeid" = "t"."TempShifttypeid","shiftconsultantAnalysisCode" = (select "s"."analysiscode" from "staff" as "s" where "s"."staffid" = "t"."staffid") from
"TEMPAccountsStatistics" as "tas" join "TempShift" as "t" on "tas"."TempShiftID" = "t"."TempShiftID"
,"TempShift" as "t" key join "TempShiftplan" as "p";
--
--
-- Person names
--
--
update "TEMPAccountsStatistics" as "tas" set "personname" = "p"."name" from "TEMPAccountsStatistics" as "tas" join "person" as "p" on "tas"."personid" = "p"."personid";
--
--
-- Company Details for placement related invoices
--
--
update "TEMPAccountsStatistics" as "tas"
set "CompanyID" = "c"."CompanyID",
"CompanyName" = "c"."Name",
"ClientAccountCode" = "c"."clientcode" from
"TEMPAccountsStatistics" as "tas" join "placement" as "p" on "tas"."placementID" = "p"."placementID"
key join "vacancy" key join "employment" as "e" key join "company" as "c";
--
--
--
-- Placement Nominal Codes
--
--
update "TEMPAccountsStatistics" as "tas" set "NominalCodeSegment1" = (select first "nominalcode"
from(select top 1 "nominalcode" from "placementanalysis" as "pl1"
,(select "count"() as "plc" from "placementanalysis" as "pl" where "pl"."placementid" = "tas"."placementid") as "t2"
where "pl1"."placementid" = "tas"."placementid" and "plc" > 0 order by "whenauthorised" asc) as "t1" order by "nominalcode" desc) where "placementid" is not null;
update "TEMPAccountsStatistics" as "tas"
set "NominalCodeSegment2"
= (select first "nominalcode"
from(select top 2 "nominalcode" from "placementanalysis" as "pl1"
,(select "count"() as "plc" from "placementanalysis" as "pl" where "pl"."placementid" = "tas"."placementid") as "t2"
where "pl1"."placementid" = "tas"."placementid" and "plc" > 1 order by "whenauthorised" asc) as "t1" order by "nominalcode" desc) where "placementid" is not null and "NominalCodeSegment1" is not null;
update "TEMPAccountsStatistics" as "tas" set "NominalCodeSegment3" = (select first "nominalcode"
from(select top 3 "nominalcode" from "placementanalysis" as "pl1"
,(select "count"() as "plc" from "placementanalysis" as "pl" where "pl"."placementid" = "tas"."placementid") as "t2"
where "pl1"."placementid" = "tas"."placementid" and "plc" > 2 order by "whenauthorised" asc) as "t1" order by "nominalcode" desc) where "placementid" is not null and "NominalCodeSegment2" is not null;
update "TEMPAccountsStatistics" as "tas" set "NominalCodeSegment4" = (select first "nominalcode"
from(select top 4 "nominalcode" from "placementanalysis" as "pl1"
,(select "count"() as "plc" from "placementanalysis" as "pl" where "pl"."placementid" = "tas"."placementid") as "t2"
where "pl1"."placementid" = "tas"."placementid" and "plc" > 3 order by "whenauthorised" asc) as "t1" order by "nominalcode" desc) where "placementid" is not null and "NominalCodeSegment3" is not null;
update "TEMPAccountsStatistics" as "tas" set "NominalCodeSegment5" = (select first "nominalcode"
from(select top 5 "nominalcode" from "placementanalysis" as "pl1"
,(select "count"() as "plc" from "placementanalysis" as "pl" where "pl"."placementid" = "tas"."placementid") as "t2"
where "pl1"."placementid" = "tas"."placementid" and "plc" > 4 order by "whenauthorised" asc) as "t1" order by "nominalcode" desc) where "placementid" is not null and "NominalCodeSegment4" is not null;
--
--
-- StaffID
--
--
update "TEMPAccountsStatistics" as "tas" set "TimesheetStaffID" = (select "staffid" from "temptimesheet" as "t" where "t"."temptimesheetid" = "tas"."temptimesheetid"),
"PlacementStaffID" = (select "staffid" from "placement" as "p" where "p"."placementid" = "tas"."placementid"),
"vacancyStaffID" = (select "staffid" from "vacancy" as "v" where "v"."vacancyid" = "tas"."vacancyid");
--
--
-- Analysis Codes
--
--
update "TEMPAccountsStatistics" as "tas"
set "DivisionAnalysisCode" = "isnull"((select "Analysis" from "Division" key join "TempDesk" where "TempDesk"."TempDeskID" = "tas"."TempDeskID"),
(select "Analysis" from "Division" key join "Company" where "tas"."CompanyID" = "Company"."CompanyID")),
"DepartmentAnalysisCode" = (select "Analysis" from "Department" key join "Placement" where "Placement"."PlacementID" = "tas"."PlacementID"),
"TempdeskAnalysiscode" = "isnull"((select "Defanalysiscode" from "TempDesk" where "TempDesk"."TempDeskID" = "tas"."TempDeskID"),''),
"VacancyConsultantAnalysisCode" = (select "s"."analysiscode" from "staff" as "s" where "tas"."vacancystaffid" = "s"."staffid"),
"PlacementConsultantAnalysisCode" = (select "s"."analysiscode" from "staff" as "s" where "tas"."placementstaffid" = "s"."staffid"),
"TimeSheetConsultantAnalysisCode" = (select "s"."analysiscode" from "staff" as "s" where "tas"."timesheetstaffid" = "s"."staffid"),
"ConsultantAnalysisCode" = (select "s"."analysiscode" from "person" key join "staff" as "s" where "tas"."personid" = "person"."personid");
update "TEMPAccountsStatistics" as "tas" set "personname" = "p"."name" from "TEMPAccountsStatistics" as "tas" join "person" as "p" on "tas"."personid" = "p"."personid";
--
--
-- Now the extras which WILL different per site
--
--un
--
-- Pensions, Holiday, Levy
--
update "TEMPAccountsStatistics" as "tas"
set "HolPay" = if "IncludeInHolidayPay" = '1' then "paid" else 0 endif from
"TEMPAccountsStatistics" as "tas" join "temptimesheetline" as "l" on("l"."temptimesheetlineid" = "tas"."temptimesheetlineid"),"temptimesheetline" as "l" key join "temppayband" where "tas"."temptimesheetlineid"
= "l"."temptimesheetlineid";
update "TEMPAccountsStatistics" as "tas"
set "AWRHolRate"
= if "isnull"("AWRWasInvolved",0) = 0 then
"isnull"((select first("ExtraHols"/(52-"ExtraHols")) from "AWRJobMaster" as "a" where "a"."placementid" = "tas"."placementid"),0) else 0 endif from
"TEMPAccountsStatistics" as "tas" join "temptimesheet" as "t" on "tas"."temptimesheetid" = "t"."temptimesheetid";
-- update "TEMPAccountsStatistics" as "tas" set "HolLiab" = "PayRate"*"UnitsPaid" from
-- "temptimesheetline" key join "temppayband" as "b" where "b"."description" like '%HP';
-- update "TEMPAccountsStatistics" as "tas" set "Levy" = "isnull"((select "sum"("amount") from "accord"."IQXJournalHistory" where "trim"("cosegmenta") = "timesheetserial" and("wtcode" = '603950')),0);
-- update "TEMPAccountsStatistics" as "tas" set "HolLiabNI" = "iqmoneyround"("HolLiab"*.1);
-- update "TEMPAccountsStatistics" as "tas" set "ActualNI" = "isnull"((select "sum"("amount") from "accord"."IQXJournalHistory" where "trim"("cosegmenta") = "timesheetserial" and(("wtcode" = '603000') or("wtcode" = '603500'))),0);
-- update "TEMPAccountsStatistics" as "tas" set "Pension" = "isnull"((select "sum"("amount") from "accord"."iQXJournalHistory" where "trim"("cosegmenta") = "timesheetserial" and("wtcode" = '540620')),0);
update "TEMPAccountsStatistics" as "tas" set "Statutory" = 0;
update "TEMPAccountsStatistics" as "tas" set "Charged" = "Charged"+"Pension"
where exists(select * from "placement" as "p" key join "employment" key join "companyaccount" where "p"."placementid" = "tas"."placementid" and "ERNIoninvoice" = 1)
and exists(select * from "temptimesheetline" as "l" where "l"."temptimesheetlineid" = "tas"."temptimesheetlineid" and "l"."linenumber" = 1);
--
-- questions etc, WILL different per site
--
update "TEMPAccountsStatistics" as "tas" set "CustomDouble1" = (select("unitscharged"*"chargerate")-("unitspaid"*"payrate") from "temptimesheetline" as "t" where "tas"."temptimesheetlineid" = "t"."temptimesheetlineid");
update "TEMPAccountsStatistics" as "tas" set "CustomDouble2" = (("paid"/"charged")*100) where "charged" <> 0;
update "TEMPAccountsStatistics" as "tas" set "CustomDouble3" = "paid";
update "TEMPAccountsStatistics" as "tas" set "CustomDouble4" = "charged";
update "TEMPAccountsStatistics" as "tas" set "CustomDouble5" = 0;
update "TEMPAccountsStatistics" set "type" = '';
update "TEMPAccountsStatistics" as "tas" set "CustomString1" = (select "department"."name" from "placement" key join "department" where "placement"."placementid" = "tas"."placementid");
update "TEMPAccountsStatistics" as "tas" set "CustomString2" = (select "staff"."userid" from "staff" where "staff"."staffid" = "tas"."vacancystaffid");
update "TEMPAccountsStatistics" as "tas" set "CustomString3" = (select "keyname" from "person" where "person"."personid" = "tas"."personid");
update "TEMPAccountsStatistics" as "tas" set "CustomString4" = (select "surname" from "person" where "person"."personid" = "tas"."personid");
update "TEMPAccountsStatistics" as "tas" set "CustomString5" = (select "forenames" from "person" where "person"."personid" = "tas"."personid");
--
--
-- Finally Insert the temp table into the Main Accounts Stats table - Do not modify
--
--
--
--
-- delete from "TEMPAccountsStatistics";
--
--
-- Insert the Analysis Batch into the batch transfer table- Do not modify
insert into "IQXAccountsStatistics"
( "IQXAccountsStatisticsID","AccountingDate","Amount","Description","Type",
"NominalCodeSegment1","NominalCodeSegment2","NominalCodeSegment3","NominalCodeSegment4","NominalCodeSegment5","PayrollCompany",
"DivisionAnalysisCode","DepartmentAnalysisCode","TimesheetAnalysisCode","TimesheetConsultantAnalysisCode","VacancyConsultantAnalysisCode","PlacementConsultantAnalysisCode","ConsultantAnalysisCode",
"ClientAccountCode","CompanyID","PersonID",
"TempTimesheetID","TempTimesheetLineID","TempDeskID","VacancyStaffID","PlacementStaffid","TimeSheetStaffid","PayrollNumber","PersonName","CompanyName","TimesheetSerial",
"TimesheetLineNumber","DocumentNumber","DocumentID","DocumentDate","JournalLineNumber","SalesOrCost","Currency","PlacementID","VacancyID","TempShiftID","tempshiftdate","DocumentLineType",
"JournalClass","Quantity","Unitdescription","TaxMethod","PaID","HolPay","HolLiab","AWRHolRate","Levy",
"HolLiabNI","ActualNI","Pension","Statutory","Charged","TempdeskAnalysiscode","GoodsAmount","priceEach","VatAmount","VatCode","JournalNominalCode",
"PayrollIdentifier","Period","TimesheetSupplierCode","ShiftAnalysisCode","ShiftReferenceCode","ShiftSerialNumber","TempShiftTypeID",
"CustomString1","CustomString2","CustomString3","CustomString4","CustomString5","CustomDouble1","CustomDouble2","CustomDouble3",
"CustomDouble4","CustomDouble5","AnalysisBatch","EmploymentPosition","EssentialSkillGradeID","PayBandPayrollFlag","IsExpenses","VacancyTheirRef","VacancyRefcode","ShiftConsultantAnalysisCode",
"transferbatch" )
select "AccountsStatisticsID","AccountingDate","Amount","Description","Type",
"NominalCodeSegment1","NominalCodeSegment2","NominalCodeSegment3","NominalCodeSegment4","NominalCodeSegment5","PayrollCompany",
"DivisionAnalysisCode","DepartmentAnalysisCode","TimesheetAnalysisCode","TimesheetConsultantAnalysisCode","VacancyConsultantAnalysisCode","PlacementConsultantAnalysisCode","ConsultantAnalysisCode",
"ClientAccountCode","CompanyID","PersonID",
"TempTimesheetID","TempTimesheetLineID","TempDeskID","VacancyStaffID","PlacementStaffid","TimeSheetStaffid","PayrollNumber","PersonName","CompanyName","TimesheetSerial",
"TimesheetLineNumber","DocumentNumber","DocumentID","DocumentDate","JournalLineNumber","SalesOrCost","Currency","PlacementID","VacancyID","TempShiftID","tempshiftdate","DocumentLineType",
"JournalClass","Quantity","Unitdescription","TaxMethod","PaID","HolPay","HolLiab","AWRHolRate","Levy",
"HolLiabNI","ActualNI","Pension","Statutory","Charged","TempdeskAnalysiscode","GoodsAmount","priceEach","VatAmount","VatCode","JournalNominalCode",
"PayrollIdentifier","Period","TimesheetSupplierCode","ShiftAnalysisCode","ShiftReferenceCode","ShiftSerialNumber","TempShiftTypeID",
"CustomString1","CustomString2","CustomString3","CustomString4","CustomString5","CustomDouble1","CustomDouble2","CustomDouble3",
"CustomDouble4","CustomDouble5","AnalysisBatch","EmploymentPosition","EssentialSkillGradeID","PayBandPayrollFlag","IsExpenses","VacancyTheirRef","VacancyRefcode","ShiftConsultantAnalysisCode",
"transferbatch"
from "TEMPAccountsStatistics";
--
insert into "BatchTransfers"( "BatchType","BatchNumber","TransferTime","Note" ) values
( 'IQXAnalysis',@AnalysisBatch,current timestamp,"string"('Accounts Statistics Export by ',(select "userid" from "staff" where "staffid" = "userstaffid")) ) ;
-- drop the temporary table
drop table "pears"."TEMPAccountsStatistics"
-- return the transfer batch
-- return(@AnalysisBatch)
end // end of atomic statement
end
go
COMMENT TO PRESERVE FORMAT ON PROCEDURE "pears"."IQXPopulateAccountStatisticsIQAc" IS
{create procedure pears."IQXPopulateAccountStatisticsIQAc"(in @FromDate Date, in @ToDate Date, in @TransferBatch integer,in @AccountFilter char(30), in @DivFilter char(30), in @DeptFilter char(30) )
begin
--
--
--
declare @AnalysisBatch integer;
declare @DoNothing smallint;
declare @AccountFilterLike char(40);
declare @DivFilterLike char(40);
declare @DeptFilterLike char(40);
declare @TimeSheetLines integer;
--
--
-- create temporary table (DO NOT ALTER THIS SECTION)
--
--
--
--
-- work out the like clause if any
--
--
if(@AccountFilter is null or "trim"(@AccountFilter) = '') then
set @AccountFilterLike = '%'
else
set @AccountFilterLike = "string"('[',"trim"(@AccountFilter),']%')
end if;
if(@DivFilter is null or "trim"(@DivFilter) = '') then
set @DivFilterLike = '%'
else
set @DivFilterLike = "string"('[',"trim"(@DivFilter),']%')
end if;
if(@DeptFilter is null or "trim"(@DeptFilter) = '') then
set @DeptFilterLike = '%'
else
set @DeptFilterLike = "string"('[',"trim"(@DeptFilter),']%')
end if;
--
--
-- Are there any Timesheet lines that cover the search criocumentdateteria, if not abort
--
--
if "Isnull"(@TransferBatch,0) > 0 then
select "count"()
into @TimeSheetLines
from "temptimesheetline" key join "temptimesheet" as "t" left outer join("placement" key join "employment" key join "company")
where "isnull"("t"."TransferBatch",0) >0 and "company"."clientcode" like @AccountFilterLike
and "isnull"("t"."AnalysisBatch",0) = 0
and "isnull"("company"."divisionid",'') like @DivFilterLike
and "isnull"("placement"."departmentid",'') like @DeptFilterLike
else
select "count"()
into @TimeSheetLines
from "temptimesheetline" key join "temptimesheet" as "t" left outer join("placement" key join "employment" key join "company")
where "company"."clientcode" like @AccountFilterLike and "isnull"("t"."AnalysisBatch",0) = 0
and "date"("t"."WhenEntered") between @FromDate and @ToDate
and "isnull"("company"."divisionid",'') like @DivFilterLike
and "isnull"("placement"."departmentid",'') like @DeptFilterLike
end if;
if "isnull"(@TimeSheetLines,0) = 0 then return end if;
--
--
-- Get the next analysis batch number
--
--
begin
update "params" set "LastAnalysisBatch" = "isnull"("LastAnalysisBatch",0)+1;
select "LastAnalysisBatch" into @AnalysisBatch from "params";
commit work
end;
-- Should only be needed if debugging - table should not exist
begin
drop table "pears"."TEMPAccountsStatistics"
exception
when others then set @DoNothing = 1
end;
--
-- create temporary table (DO NOT ALTER THIS SECTION)
--
create local temporary table "pears"."TEMPAccountsStatistics"(
"AccountsStatisticsID" char(20) not null,
"AccountingDate" date null,
"Amount" numeric(12,2) null,
"Description" char(100) null,
"Type" char(20) null,
"NominalCodeSegment1" char(12) null,
"NominalCodeSegment2" char(12) null,
"NominalCodeSegment3" char(12) null,
"NominalCodeSegment4" char(12) null,
"NominalCodeSegment5" char(12) null,
"PayrollCompany" char(10) null,
"DivisionAnalysisCode" char(50) null,
"DepartmentAnalysisCode" char(50) null,
"TimesheetAnalysisCode" char(50) null,
"TimesheetConsultantAnalysisCode" char(50) null,
"PlacementConsultantAnalysisCode" char(50) null,
"vacancyConsultantAnalysisCode" char(50) null,
"ConsultantAnalysisCode" char(50) null,
"ShiftConsultantAnalysisCode" char(50) null,
"ClientAccountCode" char(20) null,
"CompanyID" char(20) null,
"PersonID" char(20) null,
"TempTimesheetID" char(20) null,
"TempTimesheetLineID" char(20) null,
"TempDeskID" char(20) null,
"TimeSheetStaffID" char(20) null,
"VacancyStaffID" char(20) null,
"PlacementStaffID" char(20) null,
"PayrollNumber" char(20) null,
"PersonName" char(50) null,
"CompanyName" char(50) null,
"TimesheetSerial" char(20) null,
"TimesheetLineNumber" smallint null,
"DocumentNumber" char(20) null,
"DocumentID" char(20) null,
"DocumentDate" date null,
"JournalLineNumber" smallint null,
"SalesOrCost" char(1) null,
"Currency" char(3) null,
"PlacementID" char(20) null,
"VacancyID" char(20) null,
"TempShiftID" char(20) null,
"TempShiftDate" date null,
"DocumentLineType" char(1) null,
"Quantity" double null,
"JournalClass" char(20) null,
"Unitdescription" char(50) null,
"PayBandPayrollFlag" char(10) null, // payrollflag
"IsExpenses" smallint null,
"TaxMethod" integer null,
"Paid" double null,
"HolPay" double null,
"HolLiab" double null,
"AWRHolRate" double null,
"Levy" double null,
"HolLiabNI" double null,
"ActualNI" double null,
"Pension" double null,
"Statutory" double null,
"Charged" double null,
"TempdeskAnalysiscode" char(20) null,
"GoodsAmount" numeric(12,2) null,
"PriceEach" double null,
"VatAmount" double null,
"VatCode" char(3) null,
"JournalNominalCode" char(12) null,
"PayrollIdentifier" char(1) null,
"Period" integer null,
"TimesheetSupplierCode" char(12) null,
"ShiftAnalysisCode" char(20) null,
"ShiftReferenceCode" char(20) null,
"ShiftSerialNumber" bigint null,
"TempShiftTypeID" char(20) null,
"AnalysisBatch" integer null,
"TransferBatch" integer null,
"EmploymentPosition" char(50) null,
"EssentialSkillGradeID" char(4) null,
"vacancytheirref" char(50) null,
"vacancyrefcode" char(20) null,
"CustomString1" char(40) null,
"CustomString2" char(40) null,
"CustomString3" char(40) null,
"CustomString4" char(40) null,
"CustomString5" char(40) null,
"CustomDouble1" double null,
"CustomDouble2" double null,
"CustomDouble3" double null,
"CustomDouble4" double null,
"CustomDouble5" double null,
primary key("AccountsStatisticsID"),
) on commit delete rows;
create index "a" on "pears"."TEMPAccountsStatistics"("TempTimesheetID");
create index "b" on "pears"."TEMPAccountsStatistics"("PersonID");
create index "c" on "pears"."TEMPAccountsStatistics"("PayrollNumber");
create index "d" on "pears"."TEMPAccountsStatistics"("TempTimesheetLineID");
create index "e" on "pears"."TEMPAccountsStatistics"("NominalCodeSegment1");
create index "f" on "pears"."TEMPAccountsStatistics"("NominalCodeSegment2");
create index "g" on "pears"."TEMPAccountsStatistics"("NominalCodeSegment3");
create index "h" on "pears"."TEMPAccountsStatistics"("NominalCodeSegment4");
create index "i" on "pears"."TEMPAccountsStatistics"("NominalCodeSegment5");
create index "j" on "pears"."TEMPAccountsStatistics"("PlacementID");
create index "l" on "pears"."TEMPAccountsStatistics"("TempTimesheetLineID");
create index "k" on "pears"."TEMPAccountsStatistics"("DocumentID");
begin atomic
--
--
-- Update the Temptimesheet lines with the new Analysis batch
-- if transfer batch > 0 then use this as a filter, ie records in transfer batch will mirror the transfer batch number
-- if transferbatch = 0, then pick up journal records in the date range @PeriodFrom to @periodto
--
if "Isnull"(@TransferBatch,0) > 0 then
update "Temptimesheet" as "t"
set "t"."AnalysisBatch" = @AnalysisBatch from
"temptimesheet" as "t" left outer join("placement" key join "employment" key join "company")
where "isnull"("t"."TransferBatch",0) = @transferbatch and "company"."clientcode" like @AccountFilterLike
and "isnull"("t"."AnalysisBatch",0) = 0
and "isnull"("company"."divisionid",'') like @DivFilterLike
and "isnull"("placement"."departmentid",'') like @DeptFilterLike
else
update "Temptimesheet" as "t"
set "t"."AnalysisBatch" = @AnalysisBatch from
"temptimesheet" as "t" left outer join("placement" key join "employment" key join "company")
where "company"."clientcode" like @AccountFilterLike and "isnull"("t"."AnalysisBatch",0) = 0
and "date"("t"."whenentered") between @FromDate and @ToDate
and "isnull"("company"."divisionid",'') like @DivFilterLike
and "isnull"("placement"."departmentid",'') like @DeptFilterLike
end if;
--
--
-- Populate temporary table with Data, fixed data first. Should not needs to change this section
--
--
--
--
-- timesheet line related details, dont change this section
--
--
insert into "TEMPAccountsStatistics"( "AccountsStatisticsID",
"TemptimesheetID","TimesheetSerial","TimesheetLineNumber","Personid","PayrollNumber","Placementid","Tempdeskid",
"TimesheetAnalysisCode","tempshiftid","paid","PayBandPayRollFlag","IsExpenses","charged","AnalysisBatch",
"TaxMethod","TempTimesheetLineID","Period","Quantity",
"Unitdescription","accountingdate","transferbatch" )
select "uniquekey"("number"()),
"tl"."TempTimesheetID",
"t"."SerialNumber",
"tl"."LineNumber",
"t"."personid",
"t"."payrollnumber",
"t"."PlacementID",
"t"."TempDeskID",
"t"."AnalysisCode",
"tl"."TempShiftID",
"isnull"("tl"."unitspaid"*"tl"."payrate",0) as "paid",
"PayrollFlag",
ISNULL("IsExpenses",0),
"isnull"("tl"."UnitsCharged"*"tl"."ChargeRate",0) as "charged",
"AnalysisBatch","TaxMethod","tl"."TempTimesheetLineID","Period",
isnull ("unitspaid",0),
"b"."unit","whenentered","t"."transferbatch"
from "TempTimesheetLine" as "tl"
key join("TempTimesheet" as "t","temppayband" as "b") where "analysisbatch" = @AnalysisBatch;
--
--
update "TEMPAccountsStatistics" as "tas" set "journallinenumber" = "j"."linenumber","amount" = "j"."amount",
"goodsamount" = "j"."goodsamount","JournalNominalCode" = "j"."NominalCode",
"journalclass" = "j"."journalclass","priceeach" = "j"."priceeach","vatamount" = "j"."vatamount","vatcode" = "j"."vatcode",
"description" = "j"."description", "documentid" = "j"."documentid" from
"TEMPAccountsStatistics" as "tas" join "iqacjournal" as "j" on "xref" = 'T' and "xrefid" = "tas"."temptimesheetlineid" ;
update "TEMPAccountsStatistics" as "tas" set "documentnumber" = "ourref","DocumentDate" = d.Documentdate from
"TEMPAccountsStatistics" as "tas" join "iqacdocument" as "d" on "tas"."documentid" = "d"."documentid" and "d"."ledgerid" = 'SALES';
--
-- Person ID when no timesheet line
--
--
update "TEMPAccountsStatistics" as "tas"
set "vacancyid" = "v"."vacancyid","vacancytheirref" = "v"."theirref","vacancyrefcode" = "v"."refcode" from
"TEMPAccountsStatistics" as "tas" join "placement" as "p" on "tas"."placementID" = "p"."placementID" key join("vacancy" as "v","employment" as "e");
--
--
-- Employment Position, Placement Temp, Currency
--
--
update "TEMPAccountsStatistics" as "tas" set "EmploymentPosition" = "Employment"."Position",
"currency" = "isnull"("tas"."currency","p"."currency") from
"TEMPAccountsStatistics" as "tas" join "placement" as "p" on "tas"."placementID" = "p"."placementID"
,"placement" as "p" key join "employment";
--
-- Employment Position, Placement Temp, Currency
--
--
update "TEMPAccountsStatistics" as "tas"
set "Period" = "t"."Period","payrollidentifier" = "t"."payrollidentifier","TimesheetSupplierCode" = "t"."SupplierCode" from
"TEMPAccountsStatistics" as "tas" join "TempTimesheet" as "t" on "tas"."TempTimesheetID" = "t"."TempTimesheetID";
--
-- Shift Fields
--
--
update "TEMPAccountsStatistics" as "tas"
set "ShiftReferenceCode" = "t"."referencecode","ShiftSerialNumber" = "p"."shiftserialnumber","tempshiftdate" = "t"."shiftdate",
"Shiftanalysiscode" = "t"."AnalysisCode","EssentialSkillGradeID" = "t"."EssentialSkillGradeID",
"TempShifttypeid" = "t"."TempShifttypeid","shiftconsultantAnalysisCode" = (select "s"."analysiscode" from "staff" as "s" where "s"."staffid" = "t"."staffid") from
"TEMPAccountsStatistics" as "tas" join "TempShift" as "t" on "tas"."TempShiftID" = "t"."TempShiftID"
,"TempShift" as "t" key join "TempShiftplan" as "p";
--
--
-- Person names
--
--
update "TEMPAccountsStatistics" as "tas" set "personname" = "p"."name" from "TEMPAccountsStatistics" as "tas" join "person" as "p" on "tas"."personid" = "p"."personid";
--
--
-- Company Details for placement related invoices
--
--
update "TEMPAccountsStatistics" as "tas"
set "CompanyID" = "c"."CompanyID",
"CompanyName" = "c"."Name",
"ClientAccountCode" = "c"."clientcode" from
"TEMPAccountsStatistics" as "tas" join "placement" as "p" on "tas"."placementID" = "p"."placementID"
key join "vacancy" key join "employment" as "e" key join "company" as "c";
--
--
--
-- Placement Nominal Codes
--
--
update "TEMPAccountsStatistics" as "tas" set "NominalCodeSegment1" = (select first "nominalcode"
from(select top 1 "nominalcode" from "placementanalysis" as "pl1"
,(select "count"() as "plc" from "placementanalysis" as "pl" where "pl"."placementid" = "tas"."placementid") as "t2"
where "pl1"."placementid" = "tas"."placementid" and "plc" > 0 order by "whenauthorised" asc) as "t1" order by "nominalcode" desc) where "placementid" is not null;
update "TEMPAccountsStatistics" as "tas"
set "NominalCodeSegment2"
= (select first "nominalcode"
from(select top 2 "nominalcode" from "placementanalysis" as "pl1"
,(select "count"() as "plc" from "placementanalysis" as "pl" where "pl"."placementid" = "tas"."placementid") as "t2"
where "pl1"."placementid" = "tas"."placementid" and "plc" > 1 order by "whenauthorised" asc) as "t1" order by "nominalcode" desc) where "placementid" is not null and "NominalCodeSegment1" is not null;
update "TEMPAccountsStatistics" as "tas" set "NominalCodeSegment3" = (select first "nominalcode"
from(select top 3 "nominalcode" from "placementanalysis" as "pl1"
,(select "count"() as "plc" from "placementanalysis" as "pl" where "pl"."placementid" = "tas"."placementid") as "t2"
where "pl1"."placementid" = "tas"."placementid" and "plc" > 2 order by "whenauthorised" asc) as "t1" order by "nominalcode" desc) where "placementid" is not null and "NominalCodeSegment2" is not null;
update "TEMPAccountsStatistics" as "tas" set "NominalCodeSegment4" = (select first "nominalcode"
from(select top 4 "nominalcode" from "placementanalysis" as "pl1"
,(select "count"() as "plc" from "placementanalysis" as "pl" where "pl"."placementid" = "tas"."placementid") as "t2"
where "pl1"."placementid" = "tas"."placementid" and "plc" > 3 order by "whenauthorised" asc) as "t1" order by "nominalcode" desc) where "placementid" is not null and "NominalCodeSegment3" is not null;
update "TEMPAccountsStatistics" as "tas" set "NominalCodeSegment5" = (select first "nominalcode"
from(select top 5 "nominalcode" from "placementanalysis" as "pl1"
,(select "count"() as "plc" from "placementanalysis" as "pl" where "pl"."placementid" = "tas"."placementid") as "t2"
where "pl1"."placementid" = "tas"."placementid" and "plc" > 4 order by "whenauthorised" asc) as "t1" order by "nominalcode" desc) where "placementid" is not null and "NominalCodeSegment4" is not null;
--
--
-- StaffID
--
--
update "TEMPAccountsStatistics" as "tas" set "TimesheetStaffID" = (select "staffid" from "temptimesheet" as "t" where "t"."temptimesheetid" = "tas"."temptimesheetid"),
"PlacementStaffID" = (select "staffid" from "placement" as "p" where "p"."placementid" = "tas"."placementid"),
"vacancyStaffID" = (select "staffid" from "vacancy" as "v" where "v"."vacancyid" = "tas"."vacancyid");
--
--
-- Analysis Codes
--
--
update "TEMPAccountsStatistics" as "tas"
set "DivisionAnalysisCode" = "isnull"((select "Analysis" from "Division" key join "TempDesk" where "TempDesk"."TempDeskID" = "tas"."TempDeskID"),
(select "Analysis" from "Division" key join "Company" where "tas"."CompanyID" = "Company"."CompanyID")),
"DepartmentAnalysisCode" = (select "Analysis" from "Department" key join "Placement" where "Placement"."PlacementID" = "tas"."PlacementID"),
"TempdeskAnalysiscode" = "isnull"((select "Defanalysiscode" from "TempDesk" where "TempDesk"."TempDeskID" = "tas"."TempDeskID"),''),
"VacancyConsultantAnalysisCode" = (select "s"."analysiscode" from "staff" as "s" where "tas"."vacancystaffid" = "s"."staffid"),
"PlacementConsultantAnalysisCode" = (select "s"."analysiscode" from "staff" as "s" where "tas"."placementstaffid" = "s"."staffid"),
"TimeSheetConsultantAnalysisCode" = (select "s"."analysiscode" from "staff" as "s" where "tas"."timesheetstaffid" = "s"."staffid"),
"ConsultantAnalysisCode" = (select "s"."analysiscode" from "person" key join "staff" as "s" where "tas"."personid" = "person"."personid");
update "TEMPAccountsStatistics" as "tas" set "personname" = "p"."name" from "TEMPAccountsStatistics" as "tas" join "person" as "p" on "tas"."personid" = "p"."personid";
--
--
-- Now the extras which WILL different per site
--
--un
--
-- Pensions, Holiday, Levy
--
update "TEMPAccountsStatistics" as "tas"
set "HolPay" = if "IncludeInHolidayPay" = '1' then "paid" else 0 endif from
"TEMPAccountsStatistics" as "tas" join "temptimesheetline" as "l" on("l"."temptimesheetlineid" = "tas"."temptimesheetlineid"),"temptimesheetline" as "l" key join "temppayband" where "tas"."temptimesheetlineid"
= "l"."temptimesheetlineid";
update "TEMPAccountsStatistics" as "tas"
set "AWRHolRate"
= if "isnull"("AWRWasInvolved",0) = 0 then
"isnull"((select first("ExtraHols"/(52-"ExtraHols")) from "AWRJobMaster" as "a" where "a"."placementid" = "tas"."placementid"),0) else 0 endif from
"TEMPAccountsStatistics" as "tas" join "temptimesheet" as "t" on "tas"."temptimesheetid" = "t"."temptimesheetid";
-- update "TEMPAccountsStatistics" as "tas" set "HolLiab" = "PayRate"*"UnitsPaid" from
-- "temptimesheetline" key join "temppayband" as "b" where "b"."description" like '%HP';
-- update "TEMPAccountsStatistics" as "tas" set "Levy" = "isnull"((select "sum"("amount") from "accord"."IQXJournalHistory" where "trim"("cosegmenta") = "timesheetserial" and("wtcode" = '603950')),0);
-- update "TEMPAccountsStatistics" as "tas" set "HolLiabNI" = "iqmoneyround"("HolLiab"*.1);
-- update "TEMPAccountsStatistics" as "tas" set "ActualNI" = "isnull"((select "sum"("amount") from "accord"."IQXJournalHistory" where "trim"("cosegmenta") = "timesheetserial" and(("wtcode" = '603000') or("wtcode" = '603500'))),0);
-- update "TEMPAccountsStatistics" as "tas" set "Pension" = "isnull"((select "sum"("amount") from "accord"."iQXJournalHistory" where "trim"("cosegmenta") = "timesheetserial" and("wtcode" = '540620')),0);
update "TEMPAccountsStatistics" as "tas" set "Statutory" = 0;
update "TEMPAccountsStatistics" as "tas" set "Charged" = "Charged"+"Pension"
where exists(select * from "placement" as "p" key join "employment" key join "companyaccount" where "p"."placementid" = "tas"."placementid" and "ERNIoninvoice" = 1)
and exists(select * from "temptimesheetline" as "l" where "l"."temptimesheetlineid" = "tas"."temptimesheetlineid" and "l"."linenumber" = 1);
--
-- questions etc, WILL different per site
--
update "TEMPAccountsStatistics" as "tas" set "CustomDouble1" = (select("unitscharged"*"chargerate")-("unitspaid"*"payrate") from "temptimesheetline" as "t" where "tas"."temptimesheetlineid" = "t"."temptimesheetlineid");
update "TEMPAccountsStatistics" as "tas" set "CustomDouble2" = (("paid"/"charged")*100) where "charged" <> 0;
update "TEMPAccountsStatistics" as "tas" set "CustomDouble3" = "paid";
update "TEMPAccountsStatistics" as "tas" set "CustomDouble4" = "charged";
update "TEMPAccountsStatistics" as "tas" set "CustomDouble5" = 0;
update "TEMPAccountsStatistics" set "type" = '';
update "TEMPAccountsStatistics" as "tas" set "CustomString1" = (select "department"."name" from "placement" key join "department" where "placement"."placementid" = "tas"."placementid");
update "TEMPAccountsStatistics" as "tas" set "CustomString2" = (select "staff"."userid" from "staff" where "staff"."staffid" = "tas"."vacancystaffid");
update "TEMPAccountsStatistics" as "tas" set "CustomString3" = (select "keyname" from "person" where "person"."personid" = "tas"."personid");
update "TEMPAccountsStatistics" as "tas" set "CustomString4" = (select "surname" from "person" where "person"."personid" = "tas"."personid");
update "TEMPAccountsStatistics" as "tas" set "CustomString5" = (select "forenames" from "person" where "person"."personid" = "tas"."personid");
--
--
-- Finally Insert the temp table into the Main Accounts Stats table - Do not modify
--
--
--
--
-- delete from "TEMPAccountsStatistics";
--
--
-- Insert the Analysis Batch into the batch transfer table- Do not modify
insert into "IQXAccountsStatistics"
( "IQXAccountsStatisticsID","AccountingDate","Amount","Description","Type",
"NominalCodeSegment1","NominalCodeSegment2","NominalCodeSegment3","NominalCodeSegment4","NominalCodeSegment5","PayrollCompany",
"DivisionAnalysisCode","DepartmentAnalysisCode","TimesheetAnalysisCode","TimesheetConsultantAnalysisCode","VacancyConsultantAnalysisCode","PlacementConsultantAnalysisCode","ConsultantAnalysisCode",
"ClientAccountCode","CompanyID","PersonID",
"TempTimesheetID","TempTimesheetLineID","TempDeskID","VacancyStaffID","PlacementStaffid","TimeSheetStaffid","PayrollNumber","PersonName","CompanyName","TimesheetSerial",
"TimesheetLineNumber","DocumentNumber","DocumentID","DocumentDate","JournalLineNumber","SalesOrCost","Currency","PlacementID","VacancyID","TempShiftID","tempshiftdate","DocumentLineType",
"JournalClass","Quantity","Unitdescription","TaxMethod","PaID","HolPay","HolLiab","AWRHolRate","Levy",
"HolLiabNI","ActualNI","Pension","Statutory","Charged","TempdeskAnalysiscode","GoodsAmount","priceEach","VatAmount","VatCode","JournalNominalCode",
"PayrollIdentifier","Period","TimesheetSupplierCode","ShiftAnalysisCode","ShiftReferenceCode","ShiftSerialNumber","TempShiftTypeID",
"CustomString1","CustomString2","CustomString3","CustomString4","CustomString5","CustomDouble1","CustomDouble2","CustomDouble3",
"CustomDouble4","CustomDouble5","AnalysisBatch","EmploymentPosition","EssentialSkillGradeID","PayBandPayrollFlag","IsExpenses","VacancyTheirRef","VacancyRefcode","ShiftConsultantAnalysisCode",
transferbatch)
select "AccountsStatisticsID","AccountingDate","Amount","Description","Type",
"NominalCodeSegment1","NominalCodeSegment2","NominalCodeSegment3","NominalCodeSegment4","NominalCodeSegment5","PayrollCompany",
"DivisionAnalysisCode","DepartmentAnalysisCode","TimesheetAnalysisCode","TimesheetConsultantAnalysisCode","VacancyConsultantAnalysisCode","PlacementConsultantAnalysisCode","ConsultantAnalysisCode",
"ClientAccountCode","CompanyID","PersonID",
"TempTimesheetID","TempTimesheetLineID","TempDeskID","VacancyStaffID","PlacementStaffid","TimeSheetStaffid","PayrollNumber","PersonName","CompanyName","TimesheetSerial",
"TimesheetLineNumber","DocumentNumber","DocumentID","DocumentDate","JournalLineNumber","SalesOrCost","Currency","PlacementID","VacancyID","TempShiftID","tempshiftdate","DocumentLineType",
"JournalClass","Quantity","Unitdescription","TaxMethod","PaID","HolPay","HolLiab","AWRHolRate","Levy",
"HolLiabNI","ActualNI","Pension","Statutory","Charged","TempdeskAnalysiscode","GoodsAmount","priceEach","VatAmount","VatCode","JournalNominalCode",
"PayrollIdentifier","Period","TimesheetSupplierCode","ShiftAnalysisCode","ShiftReferenceCode","ShiftSerialNumber","TempShiftTypeID",
"CustomString1","CustomString2","CustomString3","CustomString4","CustomString5","CustomDouble1","CustomDouble2","CustomDouble3",
"CustomDouble4","CustomDouble5","AnalysisBatch","EmploymentPosition","EssentialSkillGradeID","PayBandPayrollFlag","IsExpenses","VacancyTheirRef","VacancyRefcode","ShiftConsultantAnalysisCode",
transferbatch
from "TEMPAccountsStatistics";
--
insert into "BatchTransfers"( "BatchType","BatchNumber","TransferTime","Note" )
values( 'IQXAnalysis',@AnalysisBatch,current timestamp,string('Accounts Statistics Export by ', (select userid from staff where staffid = userstaffid ))) ;
-- drop the temporary table
drop table "pears"."TEMPAccountsStatistics"
-- return the transfer batch
-- return(@AnalysisBatch)
end // end of atomic statement
end
}