Files
TREK/shared/src/money/money.ts
T
Maurice 2149fd5315 refactor(budget): the receipt split by line lives in shared so the server can split a receipt the way the cost editor does
calculateTicketShares in the cost editor now converts the hundredths of the
new shared splitTicketLines. Same arithmetic as before: a line nobody is
ticked for is carried by everyone on the receipt and the leftover hundredths
of a line go to the lowest user ids first.

Refs #2604
2026-10-12 16:39:07 +02:00

122 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Money arithmetic both sides need to agree on to the cent.
*
* The ledger is netted in whole hundredths of the currency ("minor units" here
* always means hundredths, whatever the currency's display precision: a yen
* amount is held as yen × 100 like every other one). Floats are only the
* storage format; every sum, split and comparison happens on these integers.
*/
/**
* Currencies whose display precision is not two decimals. HUF is kept
* zero-decimal by app convention even though ISO lists two.
*/
const ZERO_DECIMAL_CURRENCIES: ReadonlySet<string> = new Set([
'JPY',
'KRW',
'VND',
'CLP',
'ISK',
'HUF',
'BIF',
'DJF',
'GNF',
'KMF',
'PYG',
'RWF',
'UGX',
'VUV',
'XAF',
'XOF',
'XPF',
]);
const THREE_DECIMAL_CURRENCIES: ReadonlySet<string> = new Set(['BHD', 'IQD', 'JOD', 'KWD', 'LYD', 'OMR', 'TND']);
/** How many decimals a currency is shown with (0, 2 or 3). Case-insensitive; unknown codes get 2. */
export function currencyDecimals(currency: string): number {
const cur = currency.toUpperCase();
if (ZERO_DECIMAL_CURRENCIES.has(cur)) return 0;
if (THREE_DECIMAL_CURRENCIES.has(cur)) return 3;
return 2;
}
/** An amount in whole hundredths, rounded half away from zero the way `Math.round` does for positives. */
export function toMinor(amount: number): number {
return Math.round(amount * 100);
}
/** The sum of several amounts in whole hundredths: each one is rounded first, so float noise never adds up. */
export function sumMinor(amounts: readonly number[]): number {
return amounts.reduce((sum, amount) => sum + toMinor(amount), 0);
}
/**
* Split `totalMinor` equally across `members`, in whole hundredths, keyed by
* user id.
*
* Largest remainder by rotation: the leftover hundredths go to consecutive
* members starting at `itemId % n` (members ordered by user id), so across
* several expenses the rounding evens out instead of always favouring the same
* person. The remainder is `total - base * n` with a floored base, never `%`,
* so a negative total (a refund, #2176) still yields a remainder in [0, n) and
* the shares always sum back to the total exactly.
*/
export function splitEqualShares(
totalMinor: number,
members: readonly { user_id: number }[],
itemId: number,
): Record<number, number> {
const n = members.length;
if (n === 0) return {};
const base = Math.floor(totalMinor / n);
const remainder = totalMinor - base * n;
const sorted = [...members].sort((a, b) => a.user_id - b.user_id);
const startIndex = itemId % n;
const shares: Record<number, number> = {};
sorted.forEach((member, i) => {
const hasExtra = (i - startIndex + n) % n < remainder;
shares[member.user_id] = base + (hasExtra ? 1 : 0);
});
return shares;
}
/** One line of an itemized receipt (#1658): its price as typed and who is in on it. */
export interface TicketLine {
price: string | number;
parts: readonly number[];
}
/**
* Per-person shares of a receipt split line by line, in whole hundredths keyed
* by user id, plus the receipt total.
*
* Every hundredth lands on somebody: a line nobody is ticked for is carried by
* everyone who appears anywhere on the receipt, so the shares add back up to
* the total (#1382). Within a line the leftover hundredths go to the lowest
* user ids first. A receipt with nobody on it at all splits nothing.
*
* The cost editor previews with this and the server re-splits a stored receipt
* with it when somebody leaves the trip, so the two never round apart.
*/
export function splitTicketLines(lines: readonly TicketLine[]): { shares: Record<number, number>; totalMinor: number } {
const shares: Record<number, number> = {};
let totalMinor = 0;
const everyone = [...new Set(lines.flatMap((line) => line.parts))].sort((a, b) => a - b);
for (const line of lines) {
const priceMinor = toMinor(Number.parseFloat(String(line.price)) || 0);
totalMinor += priceMinor;
const ids = line.parts.length > 0 ? [...new Set(line.parts)].sort((a, b) => a - b) : everyone;
const n = ids.length;
if (n === 0) continue;
const base = Math.floor(priceMinor / n);
const remainder = priceMinor - base * n;
ids.forEach((id, i) => {
shares[id] = (shares[id] ?? 0) + base + (i < remainder ? 1 : 0);
});
}
return { shares, totalMinor };
}