Added Multi-Company Support
This commit is contained in:
@@ -99,8 +99,9 @@ function planFromRow(row, { withPhotoData = true } = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
function listPlans() {
|
||||
return all('SELECT * FROM pm_plans ORDER BY name').map((row) => planFromRow(row, { withPhotoData: false }));
|
||||
function listPlans(companyId) {
|
||||
return all('SELECT * FROM pm_plans WHERE (? IS NULL OR entity_id = ?) ORDER BY name', [companyId || null, companyId || null])
|
||||
.map((row) => planFromRow(row, { withPhotoData: false }));
|
||||
}
|
||||
|
||||
function getPlan(id) {
|
||||
@@ -162,13 +163,14 @@ function resolveEstimatedMinutes(body, fallback) {
|
||||
return fallback ?? null;
|
||||
}
|
||||
|
||||
function createPlan(body, userId) {
|
||||
function createPlan(body, userId, companyId) {
|
||||
const ts = now();
|
||||
return tx(() => {
|
||||
const result = run(
|
||||
`INSERT INTO pm_plans (name, description, category, frequency_value, frequency_unit, estimated_minutes, instructions, lifespan_adjust, created_by, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
`INSERT INTO pm_plans (entity_id, name, description, category, frequency_value, frequency_unit, estimated_minutes, instructions, lifespan_adjust, created_by, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[
|
||||
companyId || null,
|
||||
body.name || 'PM plan', body.description || null, body.category || null,
|
||||
Number(body.frequency_value) || 1, FREQUENCY_UNITS.includes(body.frequency_unit) ? body.frequency_unit : 'months',
|
||||
resolveEstimatedMinutes(body, null), body.instructions || null, body.lifespan_adjust ? 1 : 0,
|
||||
@@ -609,12 +611,14 @@ function runScheduledRecompute() {
|
||||
}
|
||||
|
||||
// Maintenance-cost "ledger" for the PM book: actual PM spend per asset for a year.
|
||||
function pmBookLedger(year) {
|
||||
function pmBookLedger(year, companyId) {
|
||||
const rows = all(
|
||||
`SELECT c.cost, c.completed_at, a.asset_id, a.description
|
||||
FROM pm_completions c
|
||||
JOIN asset_pm_plans ap ON ap.id = c.asset_pm_id
|
||||
JOIN assets a ON a.id = ap.asset_id`
|
||||
JOIN assets a ON a.id = ap.asset_id
|
||||
WHERE (? IS NULL OR a.entity_id = ?)`,
|
||||
[companyId || null, companyId || null]
|
||||
);
|
||||
const byAsset = {};
|
||||
let total = 0;
|
||||
|
||||
Reference in New Issue
Block a user