Companies Developer Guide - edmozley/freeitsm GitHub Wiki

๐Ÿ› ๏ธ Companies - Developer Guide

Since 5.3.0 ยท #279 ยท API: REST API: Companies ยท User guide: Multi-company

A support business wanted its ~3,000 small-business customers in FreeITSM as companies, kept in step from an ERP - in Greece a business is known by its tax ID (AFM), not its name or domain, and many use Gmail. This slice adds the reference, the tax ID, the API to sync them, and a fix for their mail.

๐Ÿ“ Files

File Role
tenants.external_ref VARCHAR(100), tenants.tax_id VARCHAR(50) Unique when set (uq_tenants_external_ref, uq_tenants_tax_id; NULLs never collide)
includes/services/companies.php CompaniesService - every rule once: save(), upsert(), findByExternalRef(), get(), serialize(), refsReady()
api/system/save_tenant.php System โ†’ Companies' save - now a thin call to the service
api/system/get_tenants.php, system/companies/index.php The list returns and shows the two references; a search box filters by name, reference, tax ID, ticket code and domain. The dialog is the shared editor below (5.9.0)
includes/company_editor.php The ONE company editor (5.9.0, #419) - see the shared editor
includes/people.php, people/company.php The company's People page shows them
api/v1/resources/companies.php GET /companies[/{id}], POST /companies, PATCH /companies/{id}, POST /companies/upsert; apiCompaniesRequireManager()
api/v1/resources/users.php employee_id read, written and filtered (apiUserEmployeeIdReady())
includes/tenancy.php resolveTicketTenantForEmail() step (3c) - a known person's company
tests/companies-279.php (24), tests/api-companies.php (60)

๐Ÿ”‘ The rules - CompaniesService::save()

  • A name is required (150 characters at most).
  • A ticket code, an outside reference and a tax ID are each unique when set, compared ignoring case after trimming and collapsing spaces; a clash is a conflict ServiceError (HTTP 409) naming the company that has it.
  • The Default company cannot be made inactive.
  • Only the keys present change on an update; an empty string clears the code, the reference or the tax ID.
  • Before Database Verification the two columns do not exist: refsReady() is false, the System page simply does not send them, and the API answers "run Database Verification" if they are sent.

upsert() finds the company by external_ref and calls save() with its id, or with none.

๐Ÿงฉ The shared editor (5.9.0)

Until 5.9.0 the full form lived inside system/companies/index.php, and the person editor had grown a second, name-only "+ New company" box (#398). A company made from a person then needed a trip to System โ†’ Companies for its tax ID, reference and custom fields - which is what @pappas81's sync matches on (#419). Two forms for one record drift, so there is now one: includes/company_editor.php, modelled on includes/person_editor.php.

require_once '../includes/company_editor.php';
companyEditorRender();   // once per page; prints nothing for a non-administrator
CompanyEditor.open(company | null, {
    onSaved:       saved => {},   // { id, name }
    onChanged:     () => {},      // a domain, sender, logo or support tick changed (System -> Companies refreshes its list)
    onUseExisting: c => {},       // the duplicate warning's button says "Use" instead of "Open"
    prefill:       { email: '[email protected]' }   // a NEW company for this person
});
CompanyEditor.openById(id, opts);
Host How
System โ†’ Companies Add, Edit, ?new=1; onSaved/onChanged reload the list
The person editor (People, Tickets โ†’ Users, Assets โ†’ Users) personEditorRender() calls companyEditorRender(); + New company... (shown only when window.CompanyEditor exists) opens it with prefill and onUseExisting, then reloads its company list and chooses the company
People โ†’ Companies (New) and a company's page (Edit) in place, administrators
  • Its own element ids (coeโ€ฆ) and CSS scoped to #coeOverlay, because it lands on Tickets, Assets and People pages with their own .form-field, fName ... It links system-page.css for the .sys-modal look wherever it is.
  • Its strings are system.companies.*, exported into the script by PHP - the host page may not export the system namespace.
  • ticketFieldsScriptOnce() prints ticket-fields.js once however many editors want it.
  • โš ๏ธ TRAP: #coeOverlay [hidden] { display: none !important; } - the scoped .checkbox-field { display: flex } beat the hidden attribute, so a hidden routing tick still drew an empty checkbox.
  • Escape closes only this dialog (a capture-phase listener), never the person editor under it.
  • Suggested name from the domain: drop the top-level domain and a second-level label (co, com, org ...), title-case the rest. Only into an empty box.
  • Routing: after the company is saved, add_tenant_domain.php (a real domain) or add_tenant_sender.php (a public provider's address - get_freemail_domains.php decides). A refusal is said; the company stays made.
  • Duplicates are worked out in the browser from get_tenants.php: tax IDs compared with spaces, punctuation and a leading country prefix removed (stricter than the server's unique check, which only ignores case and spacing - so EL 123 456 789 and 123456789 are caught here though the server would allow both); the outside reference ignoring case; names ignoring case, accents, punctuation and legal-form words (ltd, gmbh, ae, ike ...), equal or one a word-prefix of the other (4+ letters); and a company that already has the person's domain. Identifier matches sort first.

Proved on the test install with a temporary second company (removed): the suggested name and tick, a tax ID typed without prefix matching, Use, Save creating the company with its domain routed and chosen for the person, Edit from System โ†’ Companies (domains, routing, support) and from People, a Gmail address offering the address rather than the domain, and a phone-width view.

๐Ÿ”’ Who may write

The System page is administrators only. The API holds a key to the same rule, copied from apiAnalystsRequireProvisioner(): scoped to all companies, and acting as a System administrator. A PATCH checks the company is within the key's scope first (404, the same as unknown), then the administrator rule (403).

๐Ÿ“จ Mail from a known person - (3c)

resolveTicketTenantForEmail() used to reach a person's company only through a linked address (#308). A person whose main address is a Gmail address, filed under a company on their People page, still went to triage unless the address was also added as one of the company's specific senders - which is what the reporter hit. Step (3c), after a mapped domain and before triage: the person whose main address it is (users.email is unique, so one or nobody), if they have a company. Last on purpose - a company's specific senders and a mapped domain still win.

๐Ÿงช Verifying

FREEITSM_TEST_DATABASE=freeitsm_prtest FREEITSM_TEST_URL=http://localhost/freeitsm-prtest php tests/companies-279.php
FREEITSM_TEST_DATABASE=freeitsm_prtest FREEITSM_TEST_URL=http://localhost/freeitsm-prtest php tests/api-companies.php

โœ… Custom fields (5.4.0)

Custom fields on companies and people shipped in 5.4.0, on the shared TypedFields engine - see Custom fields on companies and people - Developer Guide. CompaniesService::save() and upsert() take custom_fields, an ActorContext and a source.

โฌœ Still to come

A searchable company picker in place of drop-downs once there are hundreds (#358).