diff --git a/src/__integration__/report-totals.test.ts b/src/__integration__/report-totals.test.ts index 31ac7d5..2bd229e 100644 --- a/src/__integration__/report-totals.test.ts +++ b/src/__integration__/report-totals.test.ts @@ -455,6 +455,41 @@ describe("the account filter (#273, #279) keeps every report on the chosen sourc }); }); +describe("a negative previous month: a change keeps the sign of the change (review of PR #536)", () => { + // September: a refund alone in Butcher, an expense of −50. October: 80 spent + // in Butcher, 10 in Groceries. Butcher rises by 130, +260 % of the size of + // its previous expense, as the section total (`pct`) already reads it. + beforeEach(() => { + insert("2026-09-12", "REMBOURSEMENT BOUCHERIE", 50, BUTCHER); + insert("2026-10-03", "BOUCHERIE", -80, BUTCHER); + insert("2026-10-05", "EPICERIE", -10, GROCERIES); + }); + + it("Compare: the line, its group and the section total agree on the sign", async () => { + const rows = await getCompareMonthOverMonth(2026, 10); + const line = (id: number) => rows.find((r) => r.categoryId === id && !r.is_parent)!; + expect(line(BUTCHER)).toMatchObject({ previousAmount: -50, currentAmount: 80, deltaAbs: 130, deltaPct: 260 }); + // Cumulative: −50 by the end of September, 30 by the end of October. + expect(line(BUTCHER)).toMatchObject({ cumulativePreviousAmount: -50, cumulativeCurrentAmount: 30, cumulativeDeltaPct: 160 }); + const group = rows.find((r) => r.categoryId === GROCERY_AND_MARKET && r.is_parent)!; + expect(group).toMatchObject({ previousAmount: -50, currentAmount: 90, deltaAbs: 140, deltaPct: 280 }); + expect(group).toMatchObject({ cumulativeDeltaAbs: 90, cumulativeDeltaPct: 180 }); + const { expense } = compareResults(rows); + expect([expense.monthPrevious, expense.monthCurrent, expense.monthDelta]).toEqual([-50, 90, 140]); + }); + + it("Cartes: the mover and the expense card read +260 % and +280 %", async () => { + const snapshot = await getCartesSnapshot(2026, 10, "month"); + expect(snapshot.topMoversUp.find((m) => m.categoryId === BUTCHER)).toMatchObject({ deltaAbs: 130, deltaPct: 260 }); + expect(snapshot.kpis.expenses).toMatchObject({ current: 90, previousMonth: -50, deltaMoMAbs: 140, deltaMoMPct: 280 }); + }); + + it("Highlights: the mover reads +260 %", async () => { + const highlights = await getHighlights(2026, 10, 2026, 30, 5, 10, TODAY); + expect(highlights.topMovers.find((m) => m.categoryId === BUTCHER)).toMatchObject({ deltaAbs: 130, deltaPct: 260 }); + }); +}); + describe("each case of #392 alone: the monthly statement and the compare agree with the sum of the period", () => { const cases: Array<[string, () => unknown, Partial]> = [ [ diff --git a/src/services/reportService.ts b/src/services/reportService.ts index d391554..d6d5af7 100644 --- a/src/services/reportService.ts +++ b/src/services/reportService.ts @@ -434,7 +434,8 @@ export async function getHighlights( const current = orient("expense", Number(r.current_total ?? 0)); const previous = orient("expense", Number(r.previous_total ?? 0)); const deltaAbs = toCents(current - previous); - const deltaPct = previous === 0 ? null : (deltaAbs / previous) * 100; + // On the size of the previous amount: a spending can be negative (ADR 0026). + const deltaPct = previous === 0 ? null : (deltaAbs / Math.abs(previous)) * 100; // Highlights only exposes a monthly view, so the cumulative block mirrors // the monthly values — keeps the CategoryDelta shape uniform for // downstream consumers that never read the cumulative block. @@ -497,7 +498,10 @@ interface RawDeltaRow { /** * Turns the signed balances of compareDeltaSql() into what each row shows * (ADR 0026): an expense category its spending, the opposite of its balance, - * every other section its balance. The row keeps its section. + * every other section its balance. The row keeps its section. A change in + * percent is taken on the size of the previous amount, which can be negative + * (refunds above purchases), as `pct` of compareResults.ts takes it: from −50 + * to 80 is +260 %, never −260 %. */ function rowsToDeltas(rows: RawDeltaRow[]): CategoryDelta[] { return rows.map((r) => { @@ -505,12 +509,12 @@ function rowsToDeltas(rows: RawDeltaRow[]): CategoryDelta[] { const monthCurrent = orient(section, Number(r.month_current_total ?? 0)); const monthPrevious = orient(section, Number(r.month_previous_total ?? 0)); const monthDeltaAbs = toCents(monthCurrent - monthPrevious); - const monthDeltaPct = monthPrevious === 0 ? null : (monthDeltaAbs / monthPrevious) * 100; + const monthDeltaPct = monthPrevious === 0 ? null : (monthDeltaAbs / Math.abs(monthPrevious)) * 100; const cumCurrent = orient(section, Number(r.cumulative_current_total ?? 0)); const cumPrevious = orient(section, Number(r.cumulative_previous_total ?? 0)); const cumDeltaAbs = toCents(cumCurrent - cumPrevious); - const cumDeltaPct = cumPrevious === 0 ? null : (cumDeltaAbs / cumPrevious) * 100; + const cumDeltaPct = cumPrevious === 0 ? null : (cumDeltaAbs / Math.abs(cumPrevious)) * 100; return { categoryId: r.category_id, @@ -808,15 +812,15 @@ export function buildCompareTree( previousAmount, currentAmount, deltaAbs, - // Match rowsToDeltas' leaf formula (signed denominator) so a single-child - // parent shows the same % as its child. - deltaPct: previousAmount !== 0 ? (deltaAbs / previousAmount) * 100 : null, + // Match rowsToDeltas' leaf formula (the size of the previous amount) so a + // single-child parent shows the same % as its child. + deltaPct: previousAmount !== 0 ? (deltaAbs / Math.abs(previousAmount)) * 100 : null, cumulativePreviousAmount, cumulativeCurrentAmount, cumulativeDeltaAbs, cumulativeDeltaPct: cumulativePreviousAmount !== 0 - ? (cumulativeDeltaAbs / cumulativePreviousAmount) * 100 + ? (cumulativeDeltaAbs / Math.abs(cumulativePreviousAmount)) * 100 : null, parent_id: parentId, is_parent: true, @@ -1225,7 +1229,9 @@ function extractDelta( ): { abs: number | null; pct: number | null } { if (current === null || previous === null) return { abs: null, pct: null }; const abs = current - previous; - const pct = previous === 0 ? null : (abs / previous) * 100; + // On the size of the previous value: expenses, « Solde net » and the savings + // rate can all be negative (ADR 0026). + const pct = previous === 0 ? null : (abs / Math.abs(previous)) * 100; return { abs, pct }; }