From 96dfd4f1f8f2e3d617fc6944a04a3007daf6bf7b Mon Sep 17 00:00:00 2001 From: le king fu Date: Sat, 7 Nov 2026 22:32:11 -0500 Subject: [PATCH] feat: one sign convention for the reports, in a pure module (#392) src/utils/incomeStatement.ts holds the rule every report will follow (ADR 0026): the SQL section of a row (SECTION_SQL on CATEGORY_JOIN_SQL, a row without an existing category being uncategorized), and the pure functions that turn signed category balances into what the screens show: orient (an expense shows its spending, the opposite of its balance), incomeStatement and statementOf (income, expenses, uncategorized, transfers, result before transfers, net), addStatements, savingsRate ((income - expenses) / income) and toCents (no float remainder, no negative zero). Co-Authored-By: Claude Opus 5.5 --- src/shared/types/index.ts | 24 ++++++ src/utils/incomeStatement.test.ts | 132 ++++++++++++++++++++++++++++++ src/utils/incomeStatement.ts | 114 ++++++++++++++++++++++++++ 3 files changed, 270 insertions(+) create mode 100644 src/utils/incomeStatement.test.ts create mode 100644 src/utils/incomeStatement.ts diff --git a/src/shared/types/index.ts b/src/shared/types/index.ts index e99ac9c..a07ddf1 100644 --- a/src/shared/types/index.ts +++ b/src/shared/types/index.ts @@ -363,6 +363,30 @@ export interface RecentTransaction { export type ReportTab = "trends" | "byCategory" | "overTime" | "budgetVsActual"; +/** + * Section of an income-statement row (#392, ADR 0026): the type of its + * category, or `uncategorized` for a transaction without an existing + * category — neither income nor expense, counted in the result (D4). + */ +export type ReportSection = "income" | "expense" | "uncategorized" | "transfer"; + +/** + * The income statement of a period (#392, ADR 0026), from the signed balances + * of its categories: `expenses` is the opposite of the balance of the expense + * categories, so a refund lowers it and it turns negative when refunds exceed + * purchases. `net` is the sum of every amount of the period. + */ +export interface IncomeStatementFigures { + income: number; + expenses: number; + uncategorized: number; + transfers: number; + /** income − expenses + uncategorized */ + resultBeforeTransfers: number; + /** resultBeforeTransfers + transfers */ + net: number; +} + /** * Shared filter state for the report pages: the active period plus the * account (import source) subset. `accountIds` is a list of `ImportSource.id` diff --git a/src/utils/incomeStatement.test.ts b/src/utils/incomeStatement.test.ts new file mode 100644 index 0000000..1c0f381 --- /dev/null +++ b/src/utils/incomeStatement.test.ts @@ -0,0 +1,132 @@ +import { describe, it, expect } from "vitest"; +import { + SECTION_ORDER, + SECTION_SQL, + addStatements, + incomeStatement, + orient, + savingsRate, + statementOf, + toCents, + toSection, +} from "./incomeStatement"; + +describe("toSection", () => { + it("keeps the three category types and makes anything else uncategorized", () => { + expect(toSection("income")).toBe("income"); + expect(toSection("expense")).toBe("expense"); + expect(toSection("transfer")).toBe("transfer"); + expect(toSection("uncategorized")).toBe("uncategorized"); + expect(toSection(null)).toBe("uncategorized"); + expect(toSection(undefined)).toBe("uncategorized"); + }); + + it("reads the section the SQL fragment gives, the uncategorized one included", () => { + expect(SECTION_SQL).toBe("CASE WHEN c.id IS NULL THEN 'uncategorized' ELSE c.type END"); + }); +}); + +describe("toCents", () => { + it("drops the floating remainder of a refund that cancels two purchases, without a negative zero", () => { + const remainder = 10.1 + 20.2 - 30.3; + expect(remainder).not.toBe(0); + expect(Object.is(toCents(remainder), 0)).toBe(true); + expect(Object.is(toCents(-0), 0)).toBe(true); + }); + + it("keeps the cents", () => { + expect(toCents(12.34)).toBe(12.34); + expect(toCents(-0.1 - 0.2)).toBe(-0.3); + }); +}); + +describe("orient", () => { + it("shows an expense category's spending: the opposite of its balance", () => { + expect(orient("expense", -80)).toBe(80); + // Refunds above purchases: a negative expense (D1). + expect(orient("expense", 50)).toBe(-50); + }); + + it("shows every other section's balance as it is", () => { + expect(orient("income", 1000)).toBe(1000); + expect(orient("transfer", -300)).toBe(-300); + expect(orient("uncategorized", 40)).toBe(40); + }); + + it("never shows a negative zero", () => { + expect(Object.is(orient("expense", 0), 0)).toBe(true); + }); +}); + +describe("incomeStatement", () => { + it("adds the signed balances by section, a split and a refund included (reproduction of #392)", () => { + // Groceries: −100, its +30 offset line and a +20 refund; −30 split to Pharmacy. + const r = incomeStatement([ + { section: "income", balance: 1000 }, + { section: "expense", balance: -100 + 30 + 20 }, + { section: "expense", balance: -30 }, + { section: "transfer", balance: -500 + 500 }, + ]); + expect(r).toEqual({ + income: 1000, + expenses: 80, + uncategorized: 0, + transfers: 0, + resultBeforeTransfers: 920, + net: 920, + }); + }); + + it("keeps the uncategorized line and a one-leg transfer apart, both in the net (edge cases of #392)", () => { + const r = incomeStatement([ + { section: "income", balance: 1000 }, + { section: "expense", balance: -80 }, + { section: "expense", balance: 50 }, + { section: "uncategorized", balance: 40 }, + { section: "transfer", balance: -300 }, + ]); + expect(r).toEqual({ + income: 1000, + expenses: 30, + uncategorized: 40, + transfers: -300, + resultBeforeTransfers: 1010, + net: 710, + }); + }); + + it("is empty without rows, and reads a null balance as zero", () => { + expect(incomeStatement([])).toEqual(statementOf(0, 0, 0, 0)); + expect(incomeStatement([{ section: "income", balance: null }]).income).toBe(0); + }); +}); + +describe("addStatements", () => { + it("adds two periods, the result lines recomputed", () => { + const sum = addStatements(statementOf(1000, 80, 0, 0), statementOf(500, 30, 40, -300)); + expect(sum).toEqual(statementOf(1500, 110, 40, -300)); + // 1500 − 110 + 40 − 300 + expect(sum.net).toBe(1130); + }); +}); + +describe("savingsRate", () => { + it("is (income − expenses) ÷ income, without transfers nor uncategorized amounts (D6)", () => { + expect(savingsRate(1000, 80)).toBe(92); + expect(savingsRate(1000, 30)).toBe(97); + }); + + it("is null when income is not positive", () => { + expect(savingsRate(0, 80)).toBeNull(); + expect(savingsRate(-10, 80)).toBeNull(); + }); +}); + +describe("SECTION_ORDER", () => { + it("reads income, expenses, the uncategorized line, then transfers", () => { + const order = Object.entries(SECTION_ORDER) + .sort((a, b) => a[1] - b[1]) + .map(([section]) => section); + expect(order).toEqual(["income", "expense", "uncategorized", "transfer"]); + }); +}); diff --git a/src/utils/incomeStatement.ts b/src/utils/incomeStatement.ts new file mode 100644 index 0000000..5b47b80 --- /dev/null +++ b/src/utils/incomeStatement.ts @@ -0,0 +1,114 @@ +// Sign convention of the reports (#392, ADR 0026). +// +// Every report sums the SIGNED amounts of a category over its period. A split +// keeps its parent's amount in the parent's category, puts each part in its +// own category and adds an offset line that cancels, in the parent's +// category, what was sent elsewhere: the signed sum of a category is exactly +// what it spent or earned, refunds included. A row takes the section of its +// category's type; a row without an existing category is « Non classé », +// neither income nor expense, counted in the result (D4). +// +// The SQL returns those signed balances with their section (`SECTION_SQL`, +// on `CATEGORY_JOIN_SQL`); this module turns them into what the screens show. +// `ABS` only sorts and displays, it never adds amounts up. + +import type { IncomeStatementFigures, ReportSection } from "../shared/types"; + +/** + * Section of a transaction `t` joined to its category `c` by + * `CATEGORY_JOIN_SQL`: the type of its category, or 'uncategorized' when the + * row has no category or its category is gone (`c.id IS NULL`). + */ +export const SECTION_SQL = "CASE WHEN c.id IS NULL THEN 'uncategorized' ELSE c.type END"; + +/** The join of every report aggregation: LEFT, so the rows without a category stay. */ +export const CATEGORY_JOIN_SQL = "LEFT JOIN categories c ON c.id = t.category_id"; + +/** + * Income-statement reading order. The uncategorized line comes after the + * expenses: it belongs to the result before transfers. + */ +export const SECTION_ORDER: Record = { + income: 0, + expense: 1, + uncategorized: 2, + transfer: 3, +}; + +/** A section read from the SQL; anything that is no category type is uncategorized. */ +export function toSection(value: unknown): ReportSection { + return value === "income" || value === "expense" || value === "transfer" ? value : "uncategorized"; +} + +/** + * Rounds to the cent and drops a negative zero. A refund that cancels two + * purchases leaves a floating remainder (10.10 + 20.20 − 30.30 is + * −3.6e-15), which a currency format shows as « -0 $ ». + */ +export function toCents(amount: number): number { + return Math.round(amount * 100) / 100 + 0; +} + +/** + * What a section shows for a signed balance: an expense shows its spending, + * the opposite of its balance (negative when refunds exceed purchases), and + * every other section its balance. + */ +export function orient(section: ReportSection, balance: number): number { + return toCents(section === "expense" ? 0 - balance : balance); +} + +/** The two result lines of four section figures (D4, D5). */ +export function statementOf( + income: number, + expenses: number, + uncategorized: number, + transfers: number, +): IncomeStatementFigures { + const resultBeforeTransfers = toCents(income - expenses + uncategorized); + return { + income, + expenses, + uncategorized, + transfers, + resultBeforeTransfers, + net: toCents(resultBeforeTransfers + transfers), + }; +} + +export const EMPTY_STATEMENT: IncomeStatementFigures = statementOf(0, 0, 0, 0); + +/** The income statement of signed balances, one or several per section. */ +export function incomeStatement( + rows: Iterable<{ section: unknown; balance: number | null }>, +): IncomeStatementFigures { + const sums: Record = { income: 0, expense: 0, uncategorized: 0, transfer: 0 }; + for (const row of rows) sums[toSection(row.section)] += Number(row.balance ?? 0); + return statementOf( + orient("income", sums.income), + orient("expense", sums.expense), + orient("uncategorized", sums.uncategorized), + orient("transfer", sums.transfer), + ); +} + +/** The income statement of two periods added up (a year to date, for instance). */ +export function addStatements( + a: IncomeStatementFigures, + b: IncomeStatementFigures, +): IncomeStatementFigures { + return statementOf( + toCents(a.income + b.income), + toCents(a.expenses + b.expenses), + toCents(a.uncategorized + b.uncategorized), + toCents(a.transfers + b.transfers), + ); +} + +/** + * Savings rate (D6), in percent: (income − expenses) ÷ income, with neither + * transfers nor uncategorized amounts; null when income is not positive. + */ +export function savingsRate(income: number, expenses: number): number | null { + return income > 0 ? ((income - expenses) / income) * 100 : null; +}