mega-plant/lib/rows.test.ts
David d055c344de feat(running): drive the charts and the profit from the running-cost register
The machine charts plotted the workbook's monthly_income.fixed_expenses, which
is NOT the app's running cost. So the charts and the /running Overview
disagreed about the same machine in the same month — CT01 by R 40 640, DT01 by
R 121 115. The workbook's figure also carries depreciation/ownership and an
operator allocation, which are capital recovery and overhead rather than
running cost.

Every cost value now comes from the running-cost register: fixed = insurance
+ financing, running = purchases. Profit is income - fixed - running, and it
is ONE formula on every surface, so /charts, /income and the dashboard quote
the same figure for the same machine.

  R 8 456 243  fleet income, Jan-Oct
- R 4 705 292  fixed   (insurance + financing, from the register)
- R   306 387  running (purchases, from the register)
= R 3 444 564  profit   (was R 2 510 908 on the workbook's fixed)

The legacy repairs sheet comes out completely. It was the old workbook's
"Plant Monthly Running Cost" — read by no page, maintained by nobody — and a
repair is bought on a purchase order, so its money is already in Purchases.
The collection is still seeded, so the history is not lost, but it is no
longer in any total, any chart or any profit.

Also:
  - the profit badge wears the CHART's own green, and profit is green and
    loss red on every surface. It was hard-coded to the teal brand colour,
    so the label and the line it labelled were two different colours.
  - September and October now state a profit instead of a dash: the dash
    existed because the workbook had not posted those months' fixed costs,
    and the register has the premium and the instalment for every month.
  - the containers and the water trailers show profit equal to their income,
    because the running-cost model carries no cost for them.

Overheads roll-up — an audit found three things out of order, and the roll-up
total itself reconciles exactly (R 580 485,37, unexplained R 0,00):

  - the dashboard's Overhead mix legend named only five of the six sections,
    so the figures it listed added up to R 565 489 against a headline of
    R 580 485, and the donut drew a slice nobody could name.
  - the /overheads note claimed machine instalments were excluded, while the
    Bastion Finance block on the same page carried R 115 575,94 of them and
    R 11 204,76 of insurance for the same machines. The note now says so,
    with the figures read off the rows at render time.
  - two Subscriptions rows had VAT that did not balance — the sheet had the
    TOTAL in the VAT cell (115%), and annual excl/VAT against a monthly
    total. Both are corrected on the client's instruction; the roll-up total
    does not move, only the VAT breakdown does, and the seed's VAT gate now
    passes instead of exiting non-zero.

Verified: 161 tests pass (4 new ones pin the register join), the production
build is clean, the reconciliation script passes, and the charts, the profit
label and the overhead legend were checked on screen.
2026-11-05 21:24:30 +02:00

107 lines
3.8 KiB
TypeScript

// Regression tests for buildIncomeRows month aggregation.
//
// Run: node --import tsx --test lib/rows.test.ts
//
// Background: a machine can legitimately have SEVERAL income rows in one month — the
// workbook row plus one row per invoice line booked to it (one machine hired to two sites
// in a month produces two invoice rows). buildIncomeRows used to assign into months[i],
// so the last row read won and the others vanished from the month view while still being
// counted in the YTD total. SS01 September showed R20,700 when it was really R35,305.
import { test } from "node:test";
import assert from "node:assert/strict";
import { buildIncomeRows } from "./rows";
const asset = (id: string, code = "SS01") => ({
id,
code,
name: code,
category: "Plant",
type: "",
gl_account: "",
supplier: "",
monthly_excl: 0,
monthly_incl: 0,
purchase_value: 0,
is_provisional: false,
is_active: true,
}) as any;
const inc = (assetId: string, month: number, income: number, forecast = false) =>
({
id: `${assetId}-${month}-${income}`,
asset: assetId,
year: 2026,
month,
income,
fixed_expenses: 0,
is_forecast: forecast,
}) as any;
test("two invoice rows in one month are summed, not overwritten", () => {
// The real SS01 September 2026 case: PHI 167 + PHI 171 booked to the same machine.
const rows = buildIncomeRows(
[asset("a1")],
[inc("a1", 9, 14605), inc("a1", 9, 20700)],
);
assert.equal(rows.length, 1);
const r = rows[0];
assert.equal(r.months[8], 35305, "September cell must be both invoices");
assert.equal(r.income, 35305, "YTD total must agree with the month cell");
// The invariant that was broken: the cells must add up to the headline.
const sumOfMonths = r.months.reduce((s: number, v: any) => s + (v ?? 0), 0);
assert.equal(sumOfMonths, r.income, "month cells must sum to YTD income");
});
test("workbook row plus invoice rows in the same month all count", () => {
const rows = buildIncomeRows([asset("a1")], [inc("a1", 9, 0), inc("a1", 9, 14605), inc("a1", 9, 20700)]);
assert.equal(rows[0].months[8], 35305);
});
test("months with no actual data stay null", () => {
// getActualMonthlyIncome() filters forecast rows out before this point, so the guard
// here is simply "no row for that month -> render a dash".
const rows = buildIncomeRows([asset("a1")], [inc("a1", 9, 14605)]);
assert.equal(rows[0].months[8], 14605);
assert.equal(rows[0].months[9], null, "a month with no actual row must render as null");
assert.equal(rows[0].months[11], null);
});
test("per-month profit uses the accumulated month, not the last row", () => {
// Default: profit = income - fixed. Both invoices must be in the month's profit.
const rows = buildIncomeRows([asset("a1")], [inc("a1", 9, 14605), inc("a1", 9, 20700)]);
assert.equal(rows[0].profit[8], 35305);
});
test("multi-site machine attributes the VAT across both invoices", () => {
const line = (id: string, amount: number, vat: number) =>
({
id,
invoice: "i1",
line_no: 1,
machine_code: "SS-01-CASE",
asset_code: "SS01",
description: "430 Skidsteer",
qty: 1,
unit: "Hr",
rate: amount,
rate_type: "Dry Rate",
amount_excl: amount - vat,
vat,
total_incl: amount,
booked_asset: "a1",
booked_month: 9,
booked_year: 2026,
}) as any;
const rows = buildIncomeRows(
[asset("a1")],
[inc("a1", 9, 14605), inc("a1", 9, 20700)],
undefined,
[line("l1", 14605, 1905), line("l2", 20700, 2700)],
);
const r = rows[0];
assert.equal(r.invoiceIncome, 35305, "combined invoice total for the vehicle");
assert.equal(r.invoiceVat, 4605, "VAT inside that total, across both invoices");
assert.equal(r.invoiceIncome, r.months[8], "invoice total matches the September cell");
});