feat: every page follows the one-currency mode

Totals, info boxes, section headers, group subtotals, shares and the
per-currency charts on the dashboard, transactions, search, accounts,
merchants, statement, budgets, categories, tags, groups, piggy banks,
reports and the category, tag, budget and merchant pages sum into one
converted amount when the mode is on; charts split by currency merge
into one lane. Inputs, reconciliation and the Foreign column stay exact.

Money moved to or from a debt, loan or mortgage no longer counts as
income or spending in the transaction list boxes; it shows as From
debts and To debts. The running balance of an incoming foreign-currency
transfer shows in the account's currency.
This commit is contained in:
hh committed 2026-10-16 18:49:16 +00:00
1 parent 94466d9be9
commit 617a4700e2
61 files changed
+1427 -798

No files matched your search

-136
View File
@@ -1,136 +0,0 @@
import { api } from "#lib/api/client.js";
import { all } from "#lib/api/lists.js";
import type { ExchangeRate, Page, TransactionGroup } from "#lib/api/types.js";
import { formatMoney, toNumber } from "#lib/format.js";
import { display } from "#lib/money/display.svelte.js";
import { session } from "#lib/session.svelte.js";
const OWN_SAMPLE = 100;
const HOUR_MS = 3_600_000;
export interface Rate {
own: boolean;
rate: number;
}
export interface Part {
amount: number;
code: string;
}
export function median(values: number[]): number {
const sorted = [...values].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
}
class InPrimary {
get on() {
return display.on && display.code === session.primary?.code;
}
toggle() {
display.toggle();
}
}
export const inPrimary = new InPrimary();
function ownRates(
groups: { attributes: TransactionGroup }[],
primary: string
): Record<string, Rate> {
const implied = new Map<string, number[]>();
const add = (code: string, rate: number) =>
implied.set(code, [...(implied.get(code) ?? []), rate]);
for (const s of groups.flatMap((g) => g.attributes.transactions)) {
const amount = toNumber(s.amount);
const foreign = toNumber(s.foreign_amount);
if (!(amount && foreign && s.foreign_currency_code && s.currency_code)) {
continue;
}
if (s.currency_code === primary) {
add(s.foreign_currency_code, amount / foreign);
} else if (s.foreign_currency_code === primary) {
add(s.currency_code, foreign / amount);
}
}
return Object.fromEntries(
[...implied].map(([code, values]) => [
code,
{ own: true, rate: median(values) },
])
);
}
function listedRates(
listed: { attributes: ExchangeRate }[],
primary: string
): Record<string, Rate> {
const rates: Record<string, Rate> = {};
const dated = new Map<string, string>();
for (const { attributes: r } of listed) {
const direct = r.to_currency_code === primary;
const code = direct ? r.from_currency_code : r.to_currency_code;
const rate = toNumber(r.rate);
const date = r.date ?? "";
const usable = direct || r.from_currency_code === primary;
if (code && rate && usable && date >= (dated.get(code) ?? "")) {
dated.set(code, date);
rates[code] = { own: false, rate: direct ? rate : 1 / rate };
}
}
return rates;
}
async function loadRates(primary: string): Promise<Record<string, Rate>> {
const [listed, own] = await Promise.all([
all<ExchangeRate>("/v1/exchange-rates"),
api<Page<TransactionGroup>>("/v1/search/transactions", {
query: { limit: OWN_SAMPLE, query: "has_foreign_amount:true" },
}),
]);
return {
...ownRates(own.data, primary),
...listedRates(listed, primary),
[primary]: { own: false, rate: 1 },
};
}
export const ratesQuery = () => ({
enabled: inPrimary.on,
queryFn: () => loadRates(session.primary?.code ?? ""),
queryKey: ["dashboard-rates", session.primary?.code ?? ""],
staleTime: HOUR_MS,
});
export function inPrimaryTotal(
parts: Part[],
rates: Record<string, Rate> | undefined
): number | null {
if (!(inPrimary.on && rates)) {
return null;
}
let total = 0;
for (const part of parts) {
const rate = rates[part.code];
if (!rate) {
return null;
}
total += part.amount * rate.rate;
}
return total;
}
export function formatPrimary(amount: number, signed = false): string {
const { primary } = session;
return `≈${formatMoney(
{
amount,
code: primary?.code,
decimals: primary?.decimal_places,
symbol: primary?.symbol,
},
signed
)}`;
}