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 <noreply@anthropic.com>
114 lines
4.2 KiB
TypeScript
114 lines
4.2 KiB
TypeScript
// 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<ReportSection, number> = {
|
||
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<ReportSection, number> = { 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;
|
||
}
|