- A.1: fixed_expenses per month now threaded through to chart bucket - A.2: null = no data (forecast), 0 = real plotted zero — distinguishable - A.3: x-axis clipped to last actual month (IncomeAreaChart + IncomeBarChart + MachineLineChart) - A.4: chart hues as CSS vars (--color-chart-income/fixed/running) — deeper steps of same hues, validated for CVD - A.5: chart-type toggle (Lines / Cost stack / Profit) + localStorage persistence + dynamic legend - A.6: profit on charts page comes from computeProfit() - B.1: deleted /expenses/plant-overheads, /expenses/insurance, /expenses/tracking (all empty); added /expenses/vehicles; ExpenseSectionPage now has an empty state - B.2: captured R 157,200 Personnel block, R 100,000 Maintenance, R 10,000 Petty Cash; roll-up reconciles to R 675,162.93 (= sum of non-Plant non-Santam rows; workbook grand total R 1,110,486.87 is internally off by R 14,477.56 vs its own rows) - B.3: seed flags incl<excl, VAT>20%, broken VAT math — does NOT silently correct; 4 anomalies reported on Charl-CCS + Buildsmart - B.4: noted that workbook Vehicle Costs subtotal skips Santam (app R 9,673.87 is correct; workbook is wrong) - B.2 reconciliation assertion added to verify-cost-reconciliation.mjs - Sidebar updated: Vehicles added, Insurance/Tracking/Plant-Overheads removed
252 lines
9.5 KiB
JavaScript
252 lines
9.5 KiB
JavaScript
// Seed PocketBase with data extracted from data/source/*.json
|
|
// Regenerate those files with `node scripts/extract.mjs`
|
|
//
|
|
// Usage: PB_ADMIN=... PB_PASS=... node scripts/seed.mjs
|
|
import PocketBase from "pocketbase";
|
|
import { readFileSync } from "node:fs";
|
|
import { fileURLToPath } from "node:url";
|
|
import { dirname, resolve } from "node:path";
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const DATA_DIR = resolve(__dirname, "..", "data", "source");
|
|
|
|
// Read required env — fail loudly if missing (per Part 5.2)
|
|
const PB = process.env.NEXT_PUBLIC_PB_URL;
|
|
const EMAIL = process.env.PB_ADMIN;
|
|
const PASS = process.env.PB_PASS;
|
|
const errors = [];
|
|
if (!PB) errors.push("NEXT_PUBLIC_PB_URL");
|
|
if (!EMAIL) errors.push("PB_ADMIN");
|
|
if (!PASS) errors.push("PB_PASS");
|
|
if (errors.length) {
|
|
console.error(`✗ Missing required env: ${errors.join(", ")}`);
|
|
console.error(" Copy .env.example to .env.local and fill in.");
|
|
process.exit(1);
|
|
}
|
|
|
|
const ASSETS_COLL = "assets";
|
|
const INCOME_COLL = "monthly_income";
|
|
const RUN_COLL = "monthly_running_costs";
|
|
const OVERHEADS_COLL = "overheads";
|
|
const COST_LINES_COLL = "cost_lines";
|
|
|
|
const assets = JSON.parse(readFileSync(resolve(DATA_DIR, "assets.json"), "utf8"));
|
|
const machines = JSON.parse(readFileSync(resolve(DATA_DIR, "machines.json"), "utf8"));
|
|
const mrc = JSON.parse(readFileSync(resolve(DATA_DIR, "mrc.json"), "utf8"));
|
|
const overheads = JSON.parse(readFileSync(resolve(DATA_DIR, "overheads.json"), "utf8"));
|
|
const costLines = JSON.parse(readFileSync(resolve(DATA_DIR, "cost_lines.json"), "utf8"));
|
|
|
|
const pb = new PocketBase(PB);
|
|
pb.autoCancellation(false);
|
|
|
|
console.log(`→ Connecting to ${PB} as ${EMAIL}`);
|
|
await pb.collection("_superusers").authWithPassword(EMAIL, PASS); // Part 5.3: _superusers, not admins
|
|
console.log(" ✓ authenticated");
|
|
|
|
// Wipe existing rows so we can re-seed cleanly
|
|
for (const c of [ASSETS_COLL, INCOME_COLL, RUN_COLL, OVERHEADS_COLL, COST_LINES_COLL]) {
|
|
const items = await pb.collection(c).getFullList();
|
|
for (const r of items) await pb.collection(c).delete(r.id);
|
|
}
|
|
console.log(" ✓ wiped existing rows");
|
|
|
|
// === Build alias map for code mismatches between graph sheet and asset register ===
|
|
// Per Part 1.4 / Part B.3: derived from the workbook, not invented. Unmatched rows
|
|
// must produce a loud failure, not a silent ghost.
|
|
const codeAliases = {
|
|
"428E": "TLB02", // CAT 425F (428E) → TLB02
|
|
"430": "SS01", // CASE420 (430) → first SS01 (handled below)
|
|
};
|
|
function resolveCode(graphCode, graphName) {
|
|
if (!graphCode) return null;
|
|
if (codeAliases[graphCode]) {
|
|
// Special-case 430 — first match wins unless name indicates second
|
|
if (graphCode === "430" && /SS02/i.test(graphName)) return "SS02";
|
|
return codeAliases[graphCode];
|
|
}
|
|
return graphCode;
|
|
}
|
|
|
|
// Name normalisation for fallback matching
|
|
function normalizeName(n) {
|
|
return String(n).toLowerCase().replace(/[^a-z0-9]+/g, "").slice(0, 12);
|
|
}
|
|
|
|
// === Insert assets ===
|
|
console.log(`\n→ Seeding ${assets.length} assets`);
|
|
const codeToId = {};
|
|
const nameToId = {};
|
|
let ok = 0;
|
|
const seedErrors = [];
|
|
for (const a of assets) {
|
|
try {
|
|
const rec = await pb.collection(ASSETS_COLL).create({
|
|
code: a.code,
|
|
name: a.name,
|
|
category: a.category || "Plant",
|
|
type: a.type || "", // Part C.2
|
|
gl_account: a.gl_account || "",
|
|
supplier: a.supplier || "",
|
|
monthly_excl: a.monthly_excl || 0,
|
|
monthly_incl: a.monthly_incl || 0,
|
|
purchase_value: a.purchase_value || 0,
|
|
is_provisional: a.is_provisional || false, // Part C.3
|
|
});
|
|
if (a.code) codeToId[a.code] = rec.id;
|
|
nameToId[normalizeName(a.name)] = rec.id;
|
|
ok++;
|
|
} catch (e) {
|
|
seedErrors.push(`asset "${a.name}": ${e.response?.data?.message || e.message}`);
|
|
}
|
|
}
|
|
console.log(` ✓ ${ok}/${assets.length} assets inserted`);
|
|
|
|
// === Insert cost_lines (Part C.1 — finance/insurance/tracking per machine) ===
|
|
console.log(`\n→ Seeding ${costLines.length} cost_lines`);
|
|
let clRows = 0;
|
|
const clErrors = [];
|
|
for (const cl of costLines) {
|
|
const assetId = codeToId[cl.asset_code];
|
|
if (!assetId) { clErrors.push(`cost_line asset_code=${cl.asset_code}: asset not found`); continue; }
|
|
try {
|
|
await pb.collection(COST_LINES_COLL).create({
|
|
asset: assetId,
|
|
year: cl.year,
|
|
month: cl.month,
|
|
kind: cl.kind,
|
|
amount: cl.amount,
|
|
});
|
|
clRows++;
|
|
} catch (e) {
|
|
clErrors.push(`cost_line ${cl.asset_code} ${cl.kind} m=${cl.month}: ${e.response?.data?.message || e.message}`);
|
|
}
|
|
}
|
|
console.log(` ✓ ${clRows} cost_lines inserted`);
|
|
|
|
// === Insert monthly_income from Machine Graph Data ===
|
|
// Part 1.5: monthly_income no longer carries running_cost — we derive it from monthly_running_costs on read.
|
|
// Part 1.4: alias map + synthetic codes for uncoded items, fail loudly on unmatched rows.
|
|
console.log("\n→ Seeding monthly_income from Machine Graph Data");
|
|
let incomeRows = 0;
|
|
const incomeErrors = [];
|
|
const lastActualMonth = (() => {
|
|
// Last month with any recorded income across the whole workbook
|
|
let m = 0;
|
|
for (const x of machines) for (const d of x.data) if (d.income > 0 && d.month > m) m = d.month;
|
|
return m;
|
|
})();
|
|
for (const m of machines) {
|
|
const resolvedCode = resolveCode(m.code, m.machine);
|
|
let assetId = resolvedCode ? codeToId[resolvedCode] : null;
|
|
if (!assetId && m.machine) assetId = nameToId[normalizeName(m.machine)];
|
|
// Part B.3: NO synthetic code, NO auto-create. Unmatched → loud failure.
|
|
if (!assetId) {
|
|
incomeErrors.push(`machine "${m.machine}" (code=${m.code || "—"}) — no asset matched. Add a code or sequential alias to assets.json and re-seed.`);
|
|
continue;
|
|
}
|
|
for (const d of m.data) {
|
|
const isForecast = d.month > lastActualMonth;
|
|
try {
|
|
await pb.collection(INCOME_COLL).create({
|
|
asset: assetId,
|
|
year: 2026,
|
|
month: d.month,
|
|
income: d.income,
|
|
fixed_expenses: d.fixed_expenses,
|
|
is_forecast: isForecast, // Part 1.2
|
|
// running_cost removed (Part 1.5) — derived from monthly_running_costs at read time
|
|
});
|
|
incomeRows++;
|
|
} catch (e) {
|
|
incomeErrors.push(`income ${m.code || m.machine} m=${d.month}: ${e.response?.data?.message || e.message}`);
|
|
}
|
|
}
|
|
}
|
|
console.log(` ✓ ${incomeRows} income rows inserted`);
|
|
|
|
// === Insert monthly_running_costs ===
|
|
console.log("\n→ Seeding monthly_running_costs");
|
|
let runRows = 0;
|
|
const runErrors = [];
|
|
for (const r of mrc) {
|
|
let assetId = codeToId[r.code];
|
|
if (!assetId) assetId = nameToId[normalizeName(r.name)];
|
|
if (!assetId) { runErrors.push(`running ${r.code || r.name} m=${r.month}: no asset matched`); continue; }
|
|
if (!r.amount) continue;
|
|
try {
|
|
await pb.collection(RUN_COLL).create({
|
|
asset: assetId,
|
|
year: 2026,
|
|
month: r.month,
|
|
amount: r.amount,
|
|
});
|
|
runRows++;
|
|
} catch (e) {
|
|
runErrors.push(`running ${r.code} m=${r.month}: ${e.response?.data?.message || e.message}`);
|
|
}
|
|
}
|
|
console.log(` ✓ ${runRows} running-cost rows inserted`);
|
|
|
|
// === Insert overheads ===
|
|
// Part 1.3: skip "Plant Overheads" — those rows are machines already in assets.
|
|
// Part B.4: skip "Santam versekering" — double-counted in the workbook's own subtotal.
|
|
console.log("\n→ Seeding overheads (excluding Plant Overheads + Santam)");
|
|
let ohRows = 0;
|
|
const ohErrors = [];
|
|
const VAT_WARN = []; // Part B.3: collect VAT anomalies — DO NOT silent-correct, report loudly
|
|
for (const o of overheads) {
|
|
if (o.section === "Plant Overheads") continue; // Part 1.3
|
|
if (o.description === "Santam versekering") continue; // Part B.4
|
|
|
|
// Part B.3: VAT validation
|
|
if (o.amount_incl > 0 && o.amount_excl > 0) {
|
|
const computedIncl = o.amount_excl + o.vat;
|
|
if (Math.abs(computedIncl - o.amount_incl) > 0.5) {
|
|
VAT_WARN.push(`VAT math broken: "${o.description}" (${o.section}) — excl ${o.amount_excl} + vat ${o.vat} = ${computedIncl}, but incl is ${o.amount_incl}`);
|
|
}
|
|
if (o.amount_incl < o.amount_excl) {
|
|
VAT_WARN.push(`incl < excl: "${o.description}" (${o.section}) — excl ${o.amount_excl}, incl ${o.amount_incl}`);
|
|
}
|
|
if (o.amount_excl > 0 && (o.vat / o.amount_excl) > 0.20) {
|
|
VAT_WARN.push(`VAT > 20%: "${o.description}" (${o.section}) — ${((o.vat / o.amount_excl) * 100).toFixed(1)}%`);
|
|
}
|
|
}
|
|
|
|
try {
|
|
await pb.collection(OVERHEADS_COLL).create({
|
|
section: o.section,
|
|
description: o.description,
|
|
gl_account: o.gl_account || "",
|
|
supplier: o.supplier || "",
|
|
qty: o.qty || 0,
|
|
unit_price: o.unit_price || 0,
|
|
amount_excl: o.amount_excl || 0,
|
|
vat: o.vat || 0,
|
|
amount_incl: o.amount_incl || 0,
|
|
});
|
|
ohRows++;
|
|
} catch (e) {
|
|
ohErrors.push(`overhead "${o.description}": ${e.response?.data?.message || e.message}`);
|
|
}
|
|
}
|
|
console.log(` ✓ ${ohRows} overhead rows inserted`);
|
|
|
|
if (VAT_WARN.length > 0) {
|
|
console.log(`\n ⚠ ${VAT_WARN.length} VAT anomalies (Part B.3 — flagged, NOT corrected):`);
|
|
for (const w of VAT_WARN) console.log(` - ${w}`);
|
|
}
|
|
|
|
console.log("\n=== Summary ===");
|
|
console.log(`Assets: ${ok}/${assets.length}`);
|
|
console.log(`Cost lines: ${clRows}`);
|
|
console.log(`Income: ${incomeRows}`);
|
|
console.log(`Running: ${runRows}`);
|
|
console.log(`Overheads: ${ohRows}`);
|
|
|
|
const totalErrors = seedErrors.length + clErrors.length + incomeErrors.length + runErrors.length + ohErrors.length;
|
|
if (totalErrors > 0) {
|
|
console.error(`\n✗ ${totalErrors} errors — seed FAILED loudly:`);
|
|
for (const e of [...seedErrors, ...clErrors, ...incomeErrors, ...runErrors, ...ohErrors]) console.error(` - ${e}`);
|
|
process.exit(2);
|
|
}
|
|
console.log("\n✅ Seed complete");
|