Updated Mult-Company Support and added Tax ID Fields
This commit is contained in:
@@ -16,15 +16,17 @@ function assignmentFromRow(row) {
|
||||
};
|
||||
}
|
||||
|
||||
function listEmployees(query = {}) {
|
||||
function listEmployees(query = {}, companyId) {
|
||||
return all(`
|
||||
SELECT *
|
||||
FROM employees
|
||||
WHERE (? IS NULL OR status = ?)
|
||||
WHERE (? IS NULL OR entity_id = ?)
|
||||
AND (? IS NULL OR status = ?)
|
||||
AND (? IS NULL OR name LIKE '%' || ? || '%' OR email LIKE '%' || ? || '%' OR employee_number LIKE '%' || ? || '%')
|
||||
ORDER BY name
|
||||
LIMIT 500
|
||||
`, [
|
||||
companyId || null, companyId || null,
|
||||
query.status || null,
|
||||
query.status || null,
|
||||
query.search || null,
|
||||
@@ -34,12 +36,14 @@ function listEmployees(query = {}) {
|
||||
]).map(employeeFromRow);
|
||||
}
|
||||
|
||||
function upsertEmployee(employee) {
|
||||
// Manual creates use the active company; Workday sync (no companyId) lands in the default company.
|
||||
function upsertEmployee(employee, companyId) {
|
||||
const timestamp = now();
|
||||
const entityId = companyId || (one("SELECT id FROM entities WHERE status = 'active' ORDER BY id LIMIT 1")?.id) || null;
|
||||
const workdayId = employee.workday_worker_id || employee.workdayWorkerId || employee.workerId || null;
|
||||
const existing = workdayId
|
||||
? one('SELECT id FROM employees WHERE workday_worker_id = ?', [workdayId])
|
||||
: one('SELECT id FROM employees WHERE employee_number = ? OR lower(email) = lower(?)', [employee.employee_number || '', employee.email || '']);
|
||||
: one('SELECT id FROM employees WHERE entity_id = ? AND (employee_number = ? OR lower(email) = lower(?))', [entityId, employee.employee_number || '', employee.email || '']);
|
||||
|
||||
const values = [
|
||||
workdayId,
|
||||
@@ -79,10 +83,10 @@ function upsertEmployee(employee) {
|
||||
|
||||
const result = run(
|
||||
`INSERT INTO employees (
|
||||
workday_worker_id, employee_number, name, email, department, location, manager_name,
|
||||
entity_id, workday_worker_id, employee_number, name, email, department, location, manager_name,
|
||||
status, source, source_payload, last_synced_at, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[...values, timestamp]
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[entityId, ...values, timestamp]
|
||||
);
|
||||
return one('SELECT * FROM employees WHERE id = ?', [result.lastInsertRowid]);
|
||||
}
|
||||
@@ -116,7 +120,8 @@ function assignAsset(body, userId, companyId) {
|
||||
const asset = one('SELECT * FROM assets WHERE id = ? AND (? IS NULL OR entity_id = ?)', [body.asset_id, companyId || null, companyId || null]);
|
||||
if (!asset) throw Object.assign(new Error('Asset not found'), { status: 404 });
|
||||
|
||||
const employee = body.employee_id ? one('SELECT * FROM employees WHERE id = ?', [body.employee_id]) : null;
|
||||
// Only an employee in the same company may be assigned (foreign ids resolve to no employee).
|
||||
const employee = body.employee_id ? one('SELECT * FROM employees WHERE id = ? AND (? IS NULL OR entity_id = ?)', [body.employee_id, companyId || null, companyId || null]) : null;
|
||||
const department = body.department ?? employee?.department ?? asset.department ?? null;
|
||||
const location = body.location ?? employee?.location ?? asset.location ?? null;
|
||||
|
||||
@@ -134,7 +139,7 @@ function assignAsset(body, userId, companyId) {
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[
|
||||
body.asset_id,
|
||||
body.employee_id || null,
|
||||
employee ? employee.id : null,
|
||||
department,
|
||||
location,
|
||||
'assigned',
|
||||
|
||||
@@ -21,9 +21,13 @@ function createCompany(body, userId) {
|
||||
if (!name) throw Object.assign(new Error('A company name is required'), { status: 400 });
|
||||
const ts = now();
|
||||
const result = run(
|
||||
`INSERT INTO entities (name, legal_name, tax_id, fiscal_year_end_month, base_currency, status, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, 'active', ?, ?)`,
|
||||
[name, body.legal_name || null, body.tax_id || null, Number(body.fiscal_year_end_month || 12), body.base_currency || 'USD', ts, ts]
|
||||
`INSERT INTO entities (name, legal_name, federal_tax_id, state_tax_id, local_tax_id, fiscal_year_end_month, base_currency, status, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, 'active', ?, ?)`,
|
||||
[
|
||||
name, body.legal_name || null,
|
||||
body.federal_tax_id || null, body.state_tax_id || null, body.local_tax_id || null,
|
||||
Number(body.fiscal_year_end_month || 12), body.base_currency || 'USD', ts, ts
|
||||
]
|
||||
);
|
||||
// A new company starts with the standard book set + reference data (categories/locations/depts).
|
||||
seedBooksForCompany(result.lastInsertRowid);
|
||||
@@ -36,11 +40,14 @@ function updateCompany(id, body, userId) {
|
||||
const before = getCompany(id);
|
||||
if (!before) return null;
|
||||
run(
|
||||
`UPDATE entities SET name = ?, legal_name = ?, tax_id = ?, fiscal_year_end_month = ?, base_currency = ?, updated_at = ? WHERE id = ?`,
|
||||
`UPDATE entities SET name = ?, legal_name = ?, federal_tax_id = ?, state_tax_id = ?, local_tax_id = ?,
|
||||
fiscal_year_end_month = ?, base_currency = ?, updated_at = ? WHERE id = ?`,
|
||||
[
|
||||
body.name !== undefined ? String(body.name).trim() || before.name : before.name,
|
||||
body.legal_name !== undefined ? body.legal_name : before.legal_name,
|
||||
body.tax_id !== undefined ? body.tax_id : before.tax_id,
|
||||
body.federal_tax_id !== undefined ? body.federal_tax_id : before.federal_tax_id,
|
||||
body.state_tax_id !== undefined ? body.state_tax_id : before.state_tax_id,
|
||||
body.local_tax_id !== undefined ? body.local_tax_id : before.local_tax_id,
|
||||
body.fiscal_year_end_month !== undefined ? Number(body.fiscal_year_end_month) : before.fiscal_year_end_month,
|
||||
body.base_currency !== undefined ? body.base_currency : before.base_currency,
|
||||
now(), id
|
||||
|
||||
Reference in New Issue
Block a user