====== pears.Company ====== Generated schema reference. Regenerate this page from the SQL unload; keep hand-maintained business notes in the narrative namespace. ===== Description ===== Base Company / Client Record ===== Columns ===== ^ Column ^ Type ^ Null ^ Default ^ Comment ^ | **companyid** | char(20) | NOT NULL | | | | name | char(60) | NOT NULL | | | | keyname | char(60) | NOT NULL | | | | clientcode | char(12) | NULL | | | | fsflag | char(1) | NULL | | | | bms_id | char(12) | NULL | | | | addr1 | char(40) | NULL | | | | addr2 | char(40) | NULL | | | | addr3 | char(40) | NULL | | | | town | char(30) | NULL | | | | county | char(30) | NULL | | | | country | char(30) | NULL | | | | postcode | char(20) | NULL | | | | alert | char(100) | NULL | | | | notes | long varchar | NULL | | | | invoiceaddress | smallint | NULL | | | | TempChargeCode | char(100) | NULL | | Allows for the setting of of client or group based temp charge schemes | | VatOnMargin | smallint | NULL | | | | TempShiftCode | char(12) | NULL | | Allows for the setting of of client or group based standard shifts | | status | char(1) | NOT NULL | 'P' | | | source | char(1) | NULL | | | | ParentCompanyID | char(20) | NULL | | | | currency | char(3) | NULL | | Only used if params.homecurrency is set. Otherwise currency follows the default currency of the logged in user. | | divisionid | char(20) | NULL | | | | staffid | char(20) | NULL | | Made available in V2. Hidden by default | | TransferNotes | long varchar | NULL | | | | RemoteSelfBill | tinyint | NULL | | Identifies a company downloaded by a master agency which self-bills | | TempHolidayCode | char(12) | NULL | | Allows client specific holidays for rate script use | | registrationdate | date | NULL | current date | Added V2.2.2.11 Will be null for existing companies | | lastcontactevent | timestamp | NULL | | Added V2.2.2.13 Will be null for existing companies | | ExtraNotes | long varchar | NULL | | | | DefaultRateScheme | char(20) | NULL | | | | CompanyWarning | long varchar | NULL | | | | DefaultDocPack | char(20) | NULL | | | | SupplierCode | char(12) | NULL | | Self bill account for secondary agency | | OriginID | char(20) | NULL | | This is the new Source | | PrivateSector | smallint | NULL | 0 | Flag to indicate public/private | | IsPortal | smallint | NULL | 0 | | | AutoMatchUrgency | tinyint | NULL | 0 | Identifies automatch priority for a company | | VATReverseCharge | tinyint | NULL | | Construction rules on VAT charging | | AllowPAYE | tinyint | NULL | 1 | | | AllowLTD | tinyint | NULL | 1 | | | AllowLTDF | tinyint | NULL | 1 | Limited + HMRCEngagement = F | | AllowSelf | tinyint | NULL | 1 | | | CompanySalesStatusID | char(20) | NULL | | | | TempComplianceCode | char(12) | NULL | | Allows client specific compliance domains | | DefaultVacancyRoleID | char(20) | NULL | | | | ETimesheetProv | tinyint | NOT NULL | 0 | | | InvoiceWorkedShifts | smallint | NULL | 0 | Invoice Shifts off Company Accounts | | QuestionDepartmentID | char(2) | NULL | | Department used for question, defaults to user department if not set | ===== Primary Key ===== * companyid ===== Foreign Keys ===== ^ Constraint ^ Columns ^ References ^ Delete/update action ^ | vacancyclass | source | [[database:tables:pears_vacancyclass|pears.vacancyclass (classcode)]] | | | staff | staffid | [[database:tables:pears_staff|pears.staff (staffid)]] | | | Company | ParentCompanyID | [[database:tables:pears_company|pears.Company (companyid)]] | ON DELETE SET NULL | | division | divisionid | [[database:tables:pears_division|pears.Division (divisionid)]] | ON DELETE SET NULL | | origin | OriginID | [[database:tables:pears_origin|pears.Origin (OriginID)]] | | | CompanySalesStatus | CompanySalesStatusID | [[database:tables:pears_companysalesstatus|pears.CompanySalesStatus (CompanySalesStatusID)]] | ON DELETE SET NULL | | VacancyRole | DefaultVacancyRoleID | [[database:tables:pears_vacancyrole|pears.VacancyRole (VacancyRoleID)]] | ON DELETE SET NULL | | companystatus | status | [[database:tables:pears_companystatus|pears.CompanyStatus (CompanyStatusID)]] | NOT NULL; | | Department | QuestionDepartmentID | [[database:tables:pears_department|pears.Department (departmentid)]] | ON DELETE SET NULL | ===== Referenced By ===== ^ Table ^ Constraint ^ Columns ^ Referenced columns ^ | [[database:tables:pears_accountoverrideinvoiceaddress|pears.AccountOverrideInvoiceAddress]] | Company | InvoiceCompanyID | companyid | | [[database:tables:pears_awrcompany|pears.AWRcompany]] | Company | companyid | companyid | | [[database:tables:pears_awrweeklydetail|pears.AWRWeeklyDetail]] | Company | CompanyID | companyid | | [[database:tables:pears_cardreaderoverridesettings|pears.CardReaderOverrideSettings]] | company | Companyid | companyid | | [[database:tables:pears_cascadedeliveryaddress|pears.CascadeDeliveryAddress]] | Company | CompanyID | companyid | | [[database:tables:pears_cascadedshift|pears.CascadedShift]] | Company | SecondaryAgencyID | companyid | | [[database:tables:pears_cascadedvacancy|pears.CascadedVacancy]] | Company | SecondaryAgencyID | companyid | | [[database:tables:pears_cascaderule|pears.CascadeRule]] | Company | CompanyID | companyid | | [[database:tables:pears_company|pears.Company]] | Company | ParentCompanyID | companyid | | [[database:tables:pears_companyaccount|pears.CompanyAccount]] | Company | CompanyID | companyid | | [[database:tables:pears_companydeptdoctype|pears.CompanyDeptDocType]] | company | companyid | companyid | | [[database:tables:pears_companysds|pears.CompanySDS]] | company | CompanyID | companyid | | [[database:tables:pears_ebtimesheet|pears.EBTimeSheet]] | Company | SecondaryAgencyID | companyid | | [[database:tables:pears_employment|pears.employment]] | company | companyid | companyid | | [[database:tables:pears_iqxnetmessagecompanyrecipient|pears.IQXNetMessageCompanyRecipient]] | Company | CompanyID | companyid | | [[database:tables:pears_offlimits|pears.OffLimits]] | Company | CompanyID | companyid | | [[database:tables:pears_pay_employee|pears.Pay_Employee]] | Company | SecondaryAgencyID | companyid | | [[database:tables:pears_personshiftpreference|pears.PersonShiftPreference]] | Company | companyid | companyid | | [[database:tables:pears_staffhistory|pears.StaffHistory]] | Company | CompanyID | companyid | | [[database:tables:pears_statushistory|pears.StatusHistory]] | Company | CompanyID | companyid | | [[database:tables:pears_tempdeskagencypoolmember|pears.TempDeskAgencyPoolMember]] | Company | CompanyID | companyid | | [[database:tables:pears_tempjobtype|pears.TempJobType]] | Company | SecondaryAgencyID | companyid | | [[database:tables:pears_temptimesheet|pears.TempTimeSheet]] | Company | SecondaryAgencyID | companyid | | [[database:tables:pears_withholds|pears.WithHolds]] | company | CompanyID | companyid | ===== Indexes ===== ^ Name ^ Type ^ Columns ^ Detail ^ | company_keyname | Index | keyname | | | company_postcode | Index | postcode | | | cotown | Index | town | | | company_clientcode | Index | clientcode | | | person_lastcontactevent | Index | lastcontactevent | | | company_notestext | Text index | notes | CONFIGURATION "SYS"."default_char" IMMEDIATE REFRESH | ===== Triggers ===== ^ Name ^ Timing ^ Event ^ | companyupdate | after | update of "name", "clientcode","addr1","addr2","addr3","town","county","country","postcode", "invoiceaddress","privatesector" order 3 | | updateinvaddress | before | update of "invoiceaddress" order 2 | | insertinvoiceaddress | before | insert order 1 | | WPK_company_SECAGENCY | after | insert,delete,update order 10 | | Company_InsertTrim | before | insert order 4 | | Company_UpdateTrim | before | update order 4 | | InsertStatusCompany | after | insert order 2 | | UpdateStatusCompany | after | update of "status", "CompanySalesStatusID" order 2 | | CompanyAudit | before | update of "name", "keyname","addr1","addr2","addr3","town","county","country","postcode","status","ParentCompanyID","divisionid","staffid","alert", "originid","ClientCode","notes","PrivateSector","companywarning","CompanySalesStatusID" order 1 | | CompanyKeyWordsInsert | after | insert order 20 | | CompanyKeyWordsUpdate | after | update of "Name", "Addr1","Addr2","Addr3","Town","County","PostCode" order 21 | ===== Original SQL ===== -- IQX database structure split by table -- Source: IQXDatabaseStructure - with comments.sql -- Table: "pears"."Company" -- Table comment: Base Company / Client Record -- Statement count: 56 CREATE TABLE "pears"."Company" ( "companyid" char(20) NOT NULL ,"name" char(60) NOT NULL ,"keyname" char(60) NOT NULL check("length"("trim"("keyname")) > 0) ,"clientcode" char(12) NULL ,"fsflag" char(1) NULL ,"bms_id" char(12) NULL ,"addr1" char(40) NULL ,"addr2" char(40) NULL ,"addr3" char(40) NULL ,"town" char(30) NULL ,"county" char(30) NULL ,"country" char(30) NULL ,"postcode" char(20) NULL ,"alert" char(100) NULL ,"notes" long varchar NULL ,"invoiceaddress" smallint NULL ,"TempChargeCode" char(100) NULL ,"VatOnMargin" smallint NULL ,"TempShiftCode" char(12) NULL ,"status" char(1) NOT NULL DEFAULT 'P' ,"source" char(1) NULL ,"ParentCompanyID" char(20) NULL ,"currency" char(3) NULL ,"divisionid" char(20) NULL ,"staffid" char(20) NULL ,"TransferNotes" long varchar NULL ,"RemoteSelfBill" tinyint NULL ,"TempHolidayCode" char(12) NULL ,"registrationdate" date NULL DEFAULT current date ,"lastcontactevent" timestamp NULL ,"ExtraNotes" long varchar NULL ,"DefaultRateScheme" char(20) NULL ,"CompanyWarning" long varchar NULL ,"DefaultDocPack" char(20) NULL ,"SupplierCode" char(12) NULL ,"OriginID" char(20) NULL ,"PrivateSector" smallint NULL DEFAULT 0 ,"IsPortal" smallint NULL DEFAULT 0 ,"AutoMatchUrgency" tinyint NULL DEFAULT 0 ,"VATReverseCharge" tinyint NULL ,"AllowPAYE" tinyint NULL DEFAULT 1 ,"AllowLTD" tinyint NULL DEFAULT 1 ,"AllowLTDF" tinyint NULL DEFAULT 1 ,"AllowSelf" tinyint NULL DEFAULT 1 ,"CompanySalesStatusID" char(20) NULL ,"TempComplianceCode" char(12) NULL ,"DefaultVacancyRoleID" char(20) NULL ,"ETimesheetProv" tinyint NOT NULL DEFAULT 0 ,"InvoiceWorkedShifts" smallint NULL DEFAULT 0 ,"QuestionDepartmentID" char(2) NULL ,PRIMARY KEY ("companyid" ASC) ) go COMMENT ON COLUMN "pears"."Company"."TempChargeCode" IS 'Allows for the setting of of client or group based temp charge schemes' go COMMENT ON COLUMN "pears"."Company"."TempShiftCode" IS 'Allows for the setting of of client or group based standard shifts' go COMMENT ON COLUMN "pears"."Company"."currency" IS 'Only used if params.homecurrency is set. Otherwise currency follows the default currency of the logged in user.' go COMMENT ON COLUMN "pears"."Company"."staffid" IS 'Made available in V2. Hidden by default' go COMMENT ON COLUMN "pears"."Company"."RemoteSelfBill" IS 'Identifies a company downloaded by a master agency which self-bills' go COMMENT ON COLUMN "pears"."Company"."TempHolidayCode" IS 'Allows client specific holidays for rate script use' go COMMENT ON COLUMN "pears"."Company"."registrationdate" IS 'Added V2.2.2.11 Will be null for existing companies' go COMMENT ON COLUMN "pears"."Company"."lastcontactevent" IS 'Added V2.2.2.13 Will be null for existing companies' go COMMENT ON COLUMN "pears"."Company"."SupplierCode" IS 'Self bill account for secondary agency' go COMMENT ON COLUMN "pears"."Company"."OriginID" IS 'This is the new Source' go COMMENT ON COLUMN "pears"."Company"."PrivateSector" IS 'Flag to indicate public/private' go COMMENT ON COLUMN "pears"."Company"."AutoMatchUrgency" IS 'Identifies automatch priority for a company' go COMMENT ON COLUMN "pears"."Company"."VATReverseCharge" IS 'Construction rules on VAT charging' go COMMENT ON COLUMN "pears"."Company"."AllowLTDF" IS 'Limited + HMRCEngagement = F' go COMMENT ON COLUMN "pears"."Company"."TempComplianceCode" IS 'Allows client specific compliance domains' go COMMENT ON COLUMN "pears"."Company"."InvoiceWorkedShifts" IS 'Invoice Shifts off Company Accounts' go COMMENT ON COLUMN "pears"."Company"."QuestionDepartmentID" IS 'Department used for question, defaults to user department if not set' go COMMENT ON TABLE "pears"."Company" IS 'Base Company / Client Record' go ALTER TABLE "pears"."Company" ADD FOREIGN KEY "vacancyclass" ("source" ASC) REFERENCES "pears"."vacancyclass" ("classcode") go ALTER TABLE "pears"."Company" ADD FOREIGN KEY "staff" ("staffid" ASC) REFERENCES "pears"."staff" ("staffid") go ALTER TABLE "pears"."Company" ADD FOREIGN KEY "Company" ("ParentCompanyID" ASC) REFERENCES "pears"."Company" ("companyid") ON DELETE SET NULL go ALTER TABLE "pears"."Company" ADD FOREIGN KEY "division" ("divisionid" ASC) REFERENCES "pears"."Division" ("divisionid") ON DELETE SET NULL go ALTER TABLE "pears"."Company" ADD FOREIGN KEY "origin" ("OriginID" ASC) REFERENCES "pears"."Origin" ("OriginID") go ALTER TABLE "pears"."Company" ADD FOREIGN KEY "CompanySalesStatus" ("CompanySalesStatusID" ASC) REFERENCES "pears"."CompanySalesStatus" ("CompanySalesStatusID") ON DELETE SET NULL go ALTER TABLE "pears"."Company" ADD FOREIGN KEY "VacancyRole" ("DefaultVacancyRoleID" ASC) REFERENCES "pears"."VacancyRole" ("VacancyRoleID") ON DELETE SET NULL go ALTER TABLE "pears"."Company" ADD NOT NULL FOREIGN KEY "companystatus" ("status" ASC) REFERENCES "pears"."CompanyStatus" ("CompanyStatusID") go ALTER TABLE "pears"."Company" ADD FOREIGN KEY "Department" ("QuestionDepartmentID" ASC) REFERENCES "pears"."Department" ("departmentid") ON DELETE SET NULL go CREATE INDEX "company_keyname" ON "pears"."Company" ( "keyname" ) go CREATE INDEX "company_postcode" ON "pears"."Company" ( "postcode" ) go CREATE INDEX "cotown" ON "pears"."Company" ( "town" ) go CREATE INDEX "company_clientcode" ON "pears"."Company" ( "clientcode" ) go CREATE INDEX "person_lastcontactevent" ON "pears"."Company" ( "lastcontactevent" DESC ) go CREATE TEXT INDEX "company_notestext" ON "pears"."Company" ( "notes" ) CONFIGURATION "SYS"."default_char" IMMEDIATE REFRESH go create trigger "companyupdate" after update of "name", "clientcode","addr1","addr2","addr3","town","county","country","postcode", "invoiceaddress","privatesector" order 3 on "pears"."Company" referencing new as "new_comp" for each row begin update "companyaccount" set "transferbatch" = 0 where "companyid" = "new_comp"."companyid" and "transferbatch" > 0; if "new_comp"."invoiceaddress" = 1 then update "companyaccount" key join "company" set "companyaccount"."transferbatch" = 0 where "company"."companyid" <> "new_comp"."companyid" and "company"."clientcode" = "new_comp"."clientcode" and "companyaccount"."transferbatch" > 0 else if update("clientcode") then update "company" as "c" set "currency" = "b"."currency" from "company" as "c","company" as "b" where "c"."companyid" = "new_comp"."companyid" and "b"."companyid" = "getinvoicecompanyid"("new_comp"."clientcode"); update "companyaccount" as "c" set "creditlimit" = "b"."creditlimit","dayscredit" = "b"."dayscredit","groupinvoice" = "b"."groupinvoice", "ernioninvoice" = "b"."ernioninvoice","invoiceemail" = "b"."invoiceemail","onstop" = "b"."onstop","TheirRefRequiredInvoice" = "b"."TheirRefRequiredInvoice", "Documenttemplateid" = "b"."Documenttemplateid","PDFInvCreate" = "b"."PDFInvCreate","PDFInvIncTS" = "b"."PDFInvIncTS","FixedNI" = "b"."FixedNI", "VATNumber" = "b"."VATNumber","VATExempt" = "b"."VATExempt","PDFExtraDocs" = "b"."PDFExtraDocs","INVOICEMANAGEMENTGROUP" = "b"."INVOICEMANAGEMENTGROUP","FixedWTR" = "b"."FixedWTR" from "companyaccount" as "c","companyaccount" as "b" where "c"."companyid" = "new_comp"."companyid" and "b"."companyid" = "getinvoicecompanyid"("new_comp"."clientcode") end if end if end go COMMENT TO PRESERVE FORMAT ON TRIGGER "pears"."Company"."companyupdate" IS {create trigger companyupdate after update of "name", "clientcode","addr1","addr2","addr3","town","county","country","postcode", "invoiceaddress","privatesector" order 3 on "pears"."Company" referencing new as "new_comp" for each row begin update "companyaccount" set "transferbatch" = 0 where "companyid" = "new_comp"."companyid" and "transferbatch" > 0; if "new_comp"."invoiceaddress" = 1 then update "companyaccount" key join "company" set "companyaccount"."transferbatch" = 0 where "company"."companyid" <> "new_comp"."companyid" and "company"."clientcode" = "new_comp"."clientcode" and "companyaccount"."transferbatch" > 0 else if update("clientcode") then update "company" as "c" set "currency" = "b"."currency" from "company" as "c","company" as "b" where "c"."companyid" = "new_comp"."companyid" and "b"."companyid" = "getinvoicecompanyid"("new_comp"."clientcode"); update "companyaccount" as "c" set "creditlimit" = "b"."creditlimit","dayscredit" = "b"."dayscredit","groupinvoice" = "b"."groupinvoice", "ernioninvoice" = "b"."ernioninvoice","invoiceemail" = "b"."invoiceemail","onstop" = "b"."onstop","TheirRefRequiredInvoice" = "b"."TheirRefRequiredInvoice", "Documenttemplateid" = "b"."Documenttemplateid","PDFInvCreate" = "b"."PDFInvCreate","PDFInvIncTS" = "b"."PDFInvIncTS","FixedNI" = "b"."FixedNI", "VATNumber" = "b"."VATNumber","VATExempt" = "b"."VATExempt","PDFExtraDocs" = "b"."PDFExtraDocs","INVOICEMANAGEMENTGROUP" = "b"."INVOICEMANAGEMENTGROUP","FixedWTR" = "b"."FixedWTR" from "companyaccount" as "c","companyaccount" as "b" where "c"."companyid" = "new_comp"."companyid" and "b"."companyid" = "getinvoicecompanyid"("new_comp"."clientcode") end if end if end } go create trigger "updateinvaddress" before update of "invoiceaddress" order 2 on "pears"."Company" referencing old as "old_company" new as "new_company" for each row when("isnull"("new_company"."invoiceaddress",0) = 1 and "isnull"("old_company"."invoiceaddress",0) = 0 and "new_company"."clientcode" is not null) begin update "company" set "invoiceaddress" = null where "clientcode" = "new_company"."clientcode" and "companyid" <> "new_company"."companyid" end go COMMENT TO PRESERVE FORMAT ON TRIGGER "pears"."Company"."updateinvaddress" IS {create trigger updateinvaddress before update of invoiceaddress order 2 on pears.Company referencing old as old_company new as new_company for each row when(isnull(new_company.invoiceaddress,0) = 1 and isnull(old_company.invoiceaddress,0) = 0 and new_company.clientcode is not null) begin update company set invoiceaddress = null where clientcode = new_company.clientcode and companyid <> new_company.companyid end } go create trigger "insertinvoiceaddress" before insert order 1 on "pears"."Company" referencing new as "new_company" for each row when("new_company"."invoiceaddress" = 1 and "new_company"."clientcode" is not null) begin update "company" set "invoiceaddress" = null where "clientcode" = "new_company"."clientcode" and "companyid" <> "new_company"."companyid" end go COMMENT TO PRESERVE FORMAT ON TRIGGER "pears"."Company"."insertinvoiceaddress" IS {create trigger insertinvoiceaddress before insert order 1 on pears.Company referencing new as new_company for each row when(new_company.invoiceaddress = 1 and new_company.clientcode is not null) begin update company set invoiceaddress = null where clientcode = new_company.clientcode and companyid <> new_company.companyid end } go create trigger "WPK_company_SECAGENCY" after insert,delete,update order 10 on "pears"."Company" referencing old as "oldc" new as "newc" for each row begin declare "doit" smallint; if inserting then if "newc"."status" = 'A' then set "doit" = 1 end if end if; if deleting then if "oldc"."status" = 'A' then set "doit" = 1 end if end if; if updating then if(("newc"."status" = 'A') or("oldc"."status" = 'A')) then if(("newc"."status" <> "oldc"."status") or("oldc"."name" <> "newc"."name")) then set "doit" = 1 end if end if end if; if "doit" = 1 then call "WPKTrackChange"('P','SECAGENCY') end if end go COMMENT TO PRESERVE FORMAT ON TRIGGER "pears"."Company"."WPK_company_SECAGENCY" IS {create trigger WPK_company_SECAGENCY after insert,delete,update order 10 on pears.Company referencing old as oldc new as newc for each row begin declare doit smallint; if inserting then if newc.status = 'A' then set doit=1 end if end if; if deleting then if oldc.status = 'A' then set doit=1 end if end if; if updating then if((newc.status = 'A') or(oldc.status = 'A')) then if((newc.status <> oldc.status) or(oldc.name <> newc.name)) then set doit=1 end if end if end if; if doit = 1 then call WPKTrackChange('P','SECAGENCY') end if end } go create trigger "Company_InsertTrim" before insert order 4 on "pears"."company" referencing new as "new_p" for each row begin set "new_p"."keyname" = "trim"("new_p"."keyname"); set "new_p"."clientcode" = "trim"("new_p"."clientcode") end go COMMENT TO PRESERVE FORMAT ON TRIGGER "pears"."Company"."Company_InsertTrim" IS {create trigger Company_InsertTrim before insert order 4 on pears.company referencing new as new_p for each row begin set new_p.keyname = trim(new_p.keyname); set new_p.clientcode = trim(new_p.clientcode) end } go create trigger "Company_UpdateTrim" before update order 4 on "pears"."company" referencing new as "new_p" for each row begin set "new_p"."keyname" = "trim"("new_p"."keyname"); set "new_p"."clientcode" = "trim"("new_p"."clientcode") end go COMMENT TO PRESERVE FORMAT ON TRIGGER "pears"."Company"."Company_UpdateTrim" IS {create trigger Company_UpdateTrim before update order 4 on pears.company referencing new as new_p for each row begin set new_p.keyname = trim(new_p.keyname); set new_p.clientcode = trim(new_p.clientcode) end } go create trigger "InsertStatusCompany" after insert order 2 on "pears"."Company" referencing new as "new_co" for each row begin insert into "StatusHistory"( "StatusHistoryID","companyid","staffid","newstatus" ) values ( "uniquekey"("new_co"."companyid"),"new_co"."companyid","userstaffid","new_co"."status" ) ; insert into "StatusHistory"( "StatusHistoryID","companyid","staffid","newstatus","IsSales" ) values ( "uniquekey"("new_co"."companyid"),"new_co"."companyid","userstaffid","new_co"."CompanySalesStatusID",1 ) end go COMMENT TO PRESERVE FORMAT ON TRIGGER "pears"."Company"."InsertStatusCompany" IS {create trigger InsertStatusCompany after insert order 2 on pears.Company referencing new as new_co for each row begin insert into StatusHistory (StatusHistoryID, companyid, staffid, newstatus) values (uniquekey(new_co.companyid),new_co.companyid, userstaffid,new_co.status); insert into StatusHistory (StatusHistoryID, companyid, staffid, newstatus, IsSales) values (uniquekey(new_co.companyid),new_co.companyid, userstaffid,new_co.CompanySalesStatusID,1) end } go create trigger "UpdateStatusCompany" after update of "status", "CompanySalesStatusID" order 2 on "pears"."Company" referencing old as "old_co" new as "new_co" for each row begin declare "ID" char(20); if update("status") then select first "StatusHistoryID" into "ID" from "StatusHistory" where "companyid" = "new_co"."companyid" and "IsSales" = 0 and "datediff"("minute","whenentered",current timestamp) < 1 and "newstatus" = "old_co"."status" and "oldstatus" = "new_co"."status" order by "whenentered" desc; if "ID" is not null then delete from "StatusHistory" where "StatusHistoryID" = "ID" else insert into "StatusHistory"( "StatusHistoryID","companyid","staffid","oldstatus","newstatus" ) values ( "uniquekey"("new_co"."companyid"),"new_co"."companyid","userstaffid","old_co"."status","new_co"."status" ) end if end if; if update("CompanySalesStatusID") then select first "StatusHistoryID" into "ID" from "StatusHistory" where "companyid" = "new_co"."companyid" and "IsSales" = 1 and "datediff"("minute","whenentered",current timestamp) < 1 and "newstatus" = "old_co"."CompanySalesStatusID" and "oldstatus" = "new_co"."CompanySalesStatusID" order by "whenentered" desc; if "ID" is not null then delete from "StatusHistory" where "StatusHistoryID" = "ID" else insert into "StatusHistory"( "StatusHistoryID","companyid","staffid","oldstatus","newstatus","IsSales" ) values ( "uniquekey"("new_co"."companyid"),"new_co"."companyid","userstaffid","old_co"."CompanySalesStatusID","new_co"."CompanySalesStatusID",1 ) end if end if end go COMMENT TO PRESERVE FORMAT ON TRIGGER "pears"."Company"."UpdateStatusCompany" IS {create trigger UpdateStatusCompany after update of status, CompanySalesStatusID order 2 on pears.Company referencing old as old_co new as new_co for each row begin declare ID char(20); if update(status) then select first StatusHistoryID into ID from StatusHistory where companyid = new_co.companyid and IsSales = 0 and datediff(minute, whenentered, current timestamp) < 1 and newstatus = old_co.status and oldstatus = new_co.status order by whenentered desc; if ID is not null then delete from StatusHistory where StatusHistoryID = ID else insert into StatusHistory (StatusHistoryID, companyid, staffid, oldstatus, newstatus) values (uniquekey(new_co.companyid),new_co.companyid, userstaffid, old_co.status, new_co.status) end if; end if; if update(CompanySalesStatusID) then select first StatusHistoryID into ID from StatusHistory where companyid = new_co.companyid and IsSales = 1 and datediff(minute, whenentered, current timestamp) < 1 and newstatus = old_co.CompanySalesStatusID and oldstatus = new_co.CompanySalesStatusID order by whenentered desc; if ID is not null then delete from StatusHistory where StatusHistoryID = ID else insert into StatusHistory (StatusHistoryID, companyid, staffid, oldstatus, newstatus, IsSales) values (uniquekey(new_co.companyid),new_co.companyid, userstaffid, old_co.CompanySalesStatusID, new_co.CompanySalesStatusID, 1) end if; end if end } go create trigger "CompanyAudit" before update of "name", "keyname","addr1","addr2","addr3","town","county","country","postcode","status","ParentCompanyID","divisionid","staffid","alert", "originid","ClientCode","notes","PrivateSector","companywarning","CompanySalesStatusID" order 1 on "pears"."Company" referencing old as "old_comp" new as "new_comp" for each row when(exists(select * from "AuditItems" where "AreaName" = 'Company' and "AuditFlag" = 1)) begin declare @AuditList long varchar; declare @OldDescrip char(250); declare @NewDescrip char(250); declare @CompName char(250); select "string"(',',"list"("ItemName"),',') into @AuditList from "AuditItems" where "AreaName" = 'Company' and "AuditFlag" = 1; set @CompName = "old_comp"."Name"; -- Status if "locate"(@AuditList,',Status,') > 0 and update("Status") then select "name" into @OldDescrip from "status" where "type" = 'C' and "status"."status" = "old_comp"."status"; select "name" into @NewDescrip from "status" where "type" = 'C' and "status"."status" = "new_comp"."status"; call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Status Updated - ',@CompName),"string"("old_comp"."Status",' - ',@OldDescrip),"string"("new_comp"."Status",' - ',@NewDescrip)) end if; -- SalesStatus if "locate"(@AuditList,',SalesStatus,') > 0 and update("companysalesstatusid") then select "name" into @OldDescrip from "companysalesstatus" where "companysalesstatusid" = "old_comp"."companysalesstatusid"; select "name" into @NewDescrip from "companysalesstatus" where "companysalesstatusid" = "new_comp"."companysalesstatusid"; call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Sales Status Updated - ',@CompName),"string"("old_comp"."companysalesstatusid",' - ',@OldDescrip),"string"("new_comp"."companysalesstatusid",' - ',@NewDescrip)) end if; -- Division if "locate"(@AuditList,',Division,') > 0 and update("DivisionID") then select "name" into @OldDescrip from "Division" where "divisionid" = "old_comp"."DivisionID"; select "name" into @NewDescrip from "Division" where "divisionid" = "new_comp"."DivisionID"; call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Division Updated - ',@CompName),@OldDescrip,@NewDescrip) end if; -- Name if "locate"(@AuditList,',Name,') > 0 and update("Name") then call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Name Updated - ',@CompName),"old_comp"."name","new_comp"."name") end if; -- Notes if "locate"(@AuditList,',Notes,') > 0 and update("Notes") then call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Notes Updated - ',@CompName),"old_comp"."notes","new_comp"."notes") end if; if "locate"(@AuditList,',Warning,') > 0 and update("companywarning") then call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Warning Updated - ',@CompName),"old_comp"."companywarning","new_comp"."companywarning") end if; if "locate"(@AuditList,',KeyName,') > 0 and update("KeyName") then call "AuditLog"('COMPANY',"old_comp"."companyid","string"('KeyName Updated - ',@CompName),"old_comp"."keyname","new_comp"."keyname") end if; -- Parent if "locate"(@AuditList,',Parent,') > 0 and update("ParentCompanyID") then select "name" into @OldDescrip from "Company" where "companyid" = "old_comp"."ParentCompanyID"; select "name" into @NewDescrip from "Company" where "companyid" = "new_comp"."ParentCompanyID"; call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Parent Company Updated - ',@CompName),@OldDescrip,@NewDescrip) end if; -- Address if "locate"(@AuditList,',Address,') > 0 and(update("addr1") or update("addr2") or update("addr3") or update("town") or update("county") or update("country") or update("postcode")) then call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Address Updated - ',@CompName),"string"("old_comp"."addr1",', ',"old_comp"."addr2",', ',"old_comp"."addr3",', ',"old_comp"."town",', ',"old_comp"."county",', ',"old_comp"."postcode",', ',"old_comp"."country"),"string"("new_comp"."addr1",', ',"new_comp"."addr2",', ',"new_comp"."addr3",', ',"new_comp"."town",', ',"new_comp"."county",', ',"new_comp"."postcode",', ',"new_comp"."country")) end if; -- Consultant if "locate"(@AuditList,',Consultant,') > 0 and update("StaffID") then select "userid" into @OldDescrip from "staff" where "staffid" = "old_comp"."StaffID"; select "userid" into @NewDescrip from "staff" where "staffid" = "new_comp"."StaffID"; call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Consultant Updated - ',@CompName),@OldDescrip,@NewDescrip); insert into "staffhistory"( "staffhistoryid","companyid","oldstaff","newstaff","whoentered","whenentered" ) values( "uniquekey"("new_comp"."StaffID"),"new_comp"."companyid","old_comp"."StaffID","new_comp"."StaffID","userstaffid",current timestamp ) end if; -- Alert if "locate"(@AuditList,',Alert,') > 0 and update("Alert") then call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Alert Updated - ',@CompName),"old_comp"."Alert","new_comp"."Alert") end if; -- Source if "locate"(@AuditList,',Source,') > 0 and update("Originid") then select "descrip" into @OldDescrip from "origin" where "originid" = "old_comp"."originid"; select "descrip" into @NewDescrip from "origin" where "originid" = "new_comp"."originid"; call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Source Updated - ',@CompName),@OldDescrip,@NewDescrip) end if; -- Account Code if "locate"(@AuditList,',Account Code,') > 0 and update("ClientCode") then call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Account Code - ',@CompName),"old_comp"."ClientCode","new_comp"."ClientCode") end if; if "locate"(@AuditList,',Private Sector Outside IR35,') > 0 and update("PrivateSector") then call "AuditLog"('COMPANY',"old_comp"."companyid","string"('Private Sector Outside IR35 - ',@CompName),"old_comp"."PrivateSector","new_comp"."PrivateSector") end if end go COMMENT TO PRESERVE FORMAT ON TRIGGER "pears"."Company"."CompanyAudit" IS {create trigger CompanyAudit before update of name,keyname,addr1,addr2,addr3,town,county,country,postcode,status,ParentCompanyID,divisionid,staffid,alert, originid,ClientCode,notes,PrivateSector, companywarning, CompanySalesStatusID order 1 on pears.Company referencing old as old_comp new as new_comp for each row when(exists(select* from AuditItems where AreaName = 'Company' and AuditFlag = 1)) begin declare @AuditList long varchar; declare @OldDescrip char(250); declare @NewDescrip char(250); declare @CompName char(250); select string(',',list(ItemName),',') into @AuditList from AuditItems where AreaName = 'Company' and AuditFlag = 1; set @CompName=old_comp.Name; -- Status if locate(@AuditList,',Status,') > 0 and update(Status) then select name into @OldDescrip from status where type = 'C' and status.status = old_comp.status; select name into @NewDescrip from status where type = 'C' and status.status = new_comp.status; call AuditLog('COMPANY',old_comp.companyid,string('Status Updated - ',@CompName),string(old_comp.Status,' - ',@OldDescrip),string(new_comp.Status,' - ',@NewDescrip)) end if; -- SalesStatus if locate(@AuditList,',SalesStatus,') > 0 and update(companysalesstatusid) then select name into @OldDescrip from companysalesstatus where companysalesstatusid = old_comp.companysalesstatusid; select name into @NewDescrip from companysalesstatus where companysalesstatusid = new_comp.companysalesstatusid; call AuditLog('COMPANY',old_comp.companyid,string('Sales Status Updated - ',@CompName),string(old_comp.companysalesstatusid,' - ',@OldDescrip),string(new_comp.companysalesstatusid,' - ',@NewDescrip)) end if; -- Division if locate(@AuditList,',Division,') > 0 and update(DivisionID) then select name into @OldDescrip from Division where divisionid = old_comp.DivisionID; select name into @NewDescrip from Division where divisionid = new_comp.DivisionID; call AuditLog('COMPANY',old_comp.companyid,string('Division Updated - ',@CompName),@OldDescrip,@NewDescrip) end if; -- Name if locate(@AuditList,',Name,') > 0 and update(Name) then call AuditLog('COMPANY',old_comp.companyid,string('Name Updated - ',@CompName),old_comp.name,new_comp.name) end if; -- Notes if locate(@AuditList,',Notes,') > 0 and update(Notes) then call AuditLog('COMPANY',old_comp.companyid,string('Notes Updated - ',@CompName),old_comp.notes,new_comp.notes) end if; if locate(@AuditList,',Warning,') > 0 and update(companywarning) then call AuditLog('COMPANY',old_comp.companyid,string('Warning Updated - ',@CompName),old_comp.companywarning,new_comp.companywarning) end if; if locate(@AuditList,',KeyName,') > 0 and update(KeyName) then call AuditLog('COMPANY',old_comp.companyid,string('KeyName Updated - ',@CompName),old_comp.keyname,new_comp.keyname) end if; -- Parent if locate(@AuditList,',Parent,') > 0 and update(ParentCompanyID) then select name into @OldDescrip from Company where companyid = old_comp.ParentCompanyID; select name into @NewDescrip from Company where companyid = new_comp.ParentCompanyID; call AuditLog('COMPANY',old_comp.companyid,string('Parent Company Updated - ',@CompName),@OldDescrip,@NewDescrip) end if; -- Address if locate(@AuditList,',Address,') > 0 and(update(addr1) or update(addr2) or update(addr3) or update(town) or update(county) or update(country) or update(postcode)) then call AuditLog('COMPANY',old_comp.companyid,string('Address Updated - ',@CompName),string(old_comp.addr1,', ',old_comp.addr2,', ',old_comp.addr3,', ',old_comp.town,', ',old_comp.county,', ',old_comp.postcode,', ',old_comp.country),string(new_comp.addr1,', ',new_comp.addr2,', ',new_comp.addr3,', ',new_comp.town,', ',new_comp.county,', ',new_comp.postcode,', ',new_comp.country)) end if; -- Consultant if locate(@AuditList,',Consultant,') > 0 and update(StaffID) then select userid into @OldDescrip from staff where staffid = old_comp.StaffID; select userid into @NewDescrip from staff where staffid = new_comp.StaffID; call AuditLog('COMPANY',old_comp.companyid,string('Consultant Updated - ',@CompName),@OldDescrip,@NewDescrip); insert into staffhistory (staffhistoryid, companyid, oldstaff, newstaff, whoentered, whenentered) values (uniquekey(new_comp.StaffID),new_comp.companyid, old_comp.StaffID, new_comp.StaffID, userstaffid, current timestamp) end if; -- Alert if locate(@AuditList,',Alert,') > 0 and update(Alert) then call AuditLog('COMPANY',old_comp.companyid,string('Alert Updated - ',@CompName),old_comp.Alert,new_comp.Alert) end if; -- Source if locate(@AuditList,',Source,') > 0 and update(Originid) then select descrip into @OldDescrip from origin where originid = old_comp.originid; select descrip into @NewDescrip from origin where originid = new_comp.originid; call AuditLog('COMPANY',old_comp.companyid,string('Source Updated - ',@CompName),@OldDescrip,@NewDescrip) end if; -- Account Code if locate(@AuditList,',Account Code,') > 0 and update(ClientCode) then call AuditLog('COMPANY',old_comp.companyid,string('Account Code - ',@CompName),old_comp.ClientCode,new_comp.ClientCode) end if; if locate(@AuditList,',Private Sector Outside IR35,') > 0 and update(PrivateSector) then call AuditLog('COMPANY',old_comp.companyid,string('Private Sector Outside IR35 - ',@CompName),old_comp.PrivateSector,new_comp.PrivateSector) end if end } go create trigger "CompanyKeyWordsInsert" after insert order 20 on "pears"."Company" referencing new as "NewRow" for each row begin insert into "CompanyKeyWords"( "CompanyID","RefreshRequired" ) on existing update defaults off values( "NewRow"."CompanyID",1 ) end go COMMENT TO PRESERVE FORMAT ON TRIGGER "pears"."Company"."CompanyKeyWordsInsert" IS {create trigger CompanyKeyWordsInsert after insert order 20 on pears.Company referencing new as NewRow for each row begin insert into CompanyKeyWords(CompanyID,RefreshRequired) on existing update values(NewRow.CompanyID,1); end } go create trigger "CompanyKeyWordsUpdate" after update of "Name", "Addr1","Addr2","Addr3","Town","County","PostCode" order 21 on "pears"."Company" referencing new as "NewRow" for each row begin update "CompanyKeyWords" set "RefreshRequired" = 1 where "CompanyID" = "NewRow"."CompanyID" end go COMMENT TO PRESERVE FORMAT ON TRIGGER "pears"."Company"."CompanyKeyWordsUpdate" IS {create trigger CompanyKeyWordsUpdate after update of Name,Addr1,Addr2,Addr3,Town,County,PostCode order 21 on pears.Company referencing new as NewRow for each row begin update CompanyKeyWords set RefreshRequired = 1 where CompanyID = NewRow.CompanyID; end } go