Updated Mult-Company Support and added Tax ID Fields

This commit is contained in:
2026-06-08 01:04:52 -05:00
parent db614a6707
commit 63d767c6b0
6 changed files with 69 additions and 27 deletions

View File

@@ -119,6 +119,9 @@ function initialize() {
name TEXT NOT NULL,
legal_name TEXT,
tax_id TEXT,
federal_tax_id TEXT,
state_tax_id TEXT,
local_tax_id TEXT,
fiscal_year_end_month INTEGER NOT NULL DEFAULT 12,
base_currency TEXT NOT NULL DEFAULT 'USD',
status TEXT NOT NULL DEFAULT 'active',
@@ -140,6 +143,7 @@ function initialize() {
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY AUTOINCREMENT,
entity_id INTEGER REFERENCES entities(id),
workday_worker_id TEXT UNIQUE,
employee_number TEXT,
name TEXT NOT NULL,
@@ -781,12 +785,18 @@ function migrate() {
ensureColumn('entities', 'status', "TEXT NOT NULL DEFAULT 'active'");
ensureColumn('entities', 'disposed_at', 'TEXT');
ensureColumn('entities', 'disposed_reason', 'TEXT');
// Separate federal / state / optional local tax ids; the legacy single tax_id becomes the federal one.
ensureColumn('entities', 'federal_tax_id', 'TEXT');
ensureColumn('entities', 'state_tax_id', 'TEXT');
ensureColumn('entities', 'local_tax_id', 'TEXT');
run('UPDATE entities SET federal_tax_id = tax_id WHERE federal_tax_id IS NULL AND tax_id IS NOT NULL');
ensureColumn('pm_plans', 'entity_id', 'INTEGER REFERENCES entities(id)');
ensureColumn('alerts', 'entity_id', 'INTEGER REFERENCES entities(id)');
rebuildBooksPerCompany();
// Phase 2: scope reference data, contacts, templates, saved reports per company.
ensureColumn('contacts', 'entity_id', 'INTEGER REFERENCES entities(id)');
ensureColumn('saved_reports', 'entity_id', 'INTEGER REFERENCES entities(id)');
ensureColumn('employees', 'entity_id', 'INTEGER REFERENCES entities(id)');
rebuildReferenceValuesPerCompany();
rebuildAssetTemplatesPerCompany();
rebuildPartsPerCompany();
@@ -917,7 +927,7 @@ function backfillCompanyScope() {
if (!def) return;
// OR IGNORE so a pre-existing duplicate (e.g. two templates with the same name) can't break the
// new per-company UNIQUE constraints — the duplicate is left unscoped rather than crashing init.
for (const table of ['assets', 'books', 'pm_plans', 'alerts', 'reference_values', 'contacts', 'asset_templates', 'saved_reports', 'parts']) {
for (const table of ['assets', 'books', 'pm_plans', 'alerts', 'reference_values', 'contacts', 'asset_templates', 'saved_reports', 'parts', 'employees']) {
run(`UPDATE OR IGNORE ${table} SET entity_id = ? WHERE entity_id IS NULL`, [def.id]);
}
// Preserve "everyone sees all" on upgrade: grant each existing user every existing company,
@@ -1142,9 +1152,9 @@ function seed() {
run(
`INSERT OR IGNORE INTO employees (
employee_number, name, email, department, location, manager_name, status, source, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
['E-1001', 'Jordan Avery', 'jordan.avery@example.com', 'Operations', 'Headquarters', 'DepreCore Admin', 'active', 'seed', createdAt, createdAt]
entity_id, employee_number, name, email, department, location, manager_name, status, source, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[refEntity ? refEntity.id : null, 'E-1001', 'Jordan Avery', 'jordan.avery@example.com', 'Operations', 'Headquarters', 'DepreCore Admin', 'active', 'seed', createdAt, createdAt]
);
run(