fix(costs): read a foreign-currency expense at the rate it was booked at

The server freezes the FX rate when an expense or a settle-up transfer is
entered and settles the balances with it, but the client ignored the column
and converted every amount with today's rate. A bill paid in dollars was
therefore worth something different each time it was opened, which a tester
noticed after settling up: the figure moved under him, and the row no longer
matched what had actually changed hands.

convertBooked applies the server's own rule, in one place for both shells and
the transfer rows: an amount already in the trip currency is left to the live
step, a frozen rate converts to the trip currency before the live step, and a
rate of 1 or none at all is a row written before the freeze existed and keeps
converting live.
This commit is contained in:
Maurice
2026-09-17 22:04:01 +02:00
parent 0e2d255d81
commit 7f87dd512d
6 changed files with 135 additions and 15 deletions
@@ -1,3 +1,4 @@
import { convertBooked } from '../../../../hooks/useExchangeRates'
import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from 'react'
import {
AlertCircle, ArrowDown, ArrowLeftRight, ArrowRight, ArrowUp, Check, ChevronDown, ChevronUp,
@@ -754,7 +755,8 @@ function PaymentRow({ settlement, ctx, base, locale, t, personName, canEdit, onE
onUndo: () => void
}) {
const cur = (settlement.currency || base).toUpperCase()
const amount = ctx.convert(settlement.amount, cur)
// At the rate it was settled at, not today's (#1445), matching the desktop ledger.
const amount = convertBooked(settlement.amount, settlement.currency, settlement.exchange_rate, ctx.tripCurrency, ctx.convert)
return (
<div className="mt-2 flex items-center gap-[6px]">
<div className="relative min-w-0 flex-1 rounded-2xl border border-[color:var(--m-rowbr)] bg-m-card px-3 py-[12px]">
@@ -1,6 +1,7 @@
import type { BudgetParticipantFinal, CostCategory } from '@trek/shared'
import { paidByUser, readUserNote, settlementDate, splitEqualShares } from '../../../../components/Budget/CostsPanel.helpers'
import { catMeta, COST_CATEGORY_LIST } from '../../../../components/Budget/costsCategories'
import { convertBooked } from '../../../../hooks/useExchangeRates'
import { currencyDecimals } from '../../../../utils/formatters'
import type { BudgetItem } from '../../../../types'
@@ -26,14 +27,19 @@ export function currencyOf(e: BudgetItem, ctx: CostsCtx): string {
return (e.currency || ctx.tripCurrency).toUpperCase()
}
/** Expense total converted to the display/base currency. */
/** An amount of this expense in the display currency, at the rate the expense was booked at. */
export function booked(amount: number, e: BudgetItem, ctx: CostsCtx): number {
return convertBooked(amount, e.currency, e.exchange_rate, ctx.tripCurrency, ctx.convert)
}
/** Expense total converted to the display/base currency, at its booked rate. */
export function baseTotal(e: BudgetItem, ctx: CostsCtx): number {
return ctx.convert(e.total_price || 0, currencyOf(e, ctx))
return booked(e.total_price || 0, e, ctx)
}
/** How much `ctx.me` personally fronted for this expense, in the base currency. */
export function myPaidOf(e: BudgetItem, ctx: CostsCtx): number {
return ctx.convert(paidByUser(e, ctx.me), currencyOf(e, ctx))
return booked(paidByUser(e, ctx.me), e, ctx)
}
/** A given member's share of this expense (explicit custom amount, else equal split), base currency. */
@@ -41,10 +47,10 @@ export function memberShareOf(e: BudgetItem, userId: number, ctx: CostsCtx): num
const member = (e.members || []).find(m => m.user_id === userId)
if (!member) return 0
if (member.amount !== null && member.amount !== undefined) {
return ctx.convert(member.amount, currencyOf(e, ctx))
return booked(member.amount, e, ctx)
}
const shares = splitEqualShares(e.total_price || 0, e.members || [], e.id)
return ctx.convert(shares[userId] || 0, currencyOf(e, ctx))
return booked(shares[userId] || 0, e, ctx)
}
/** `ctx.me`'s own share — the common case of {@link memberShareOf}. */
@@ -84,6 +90,9 @@ export interface CostsSettlement {
amount: number
// Legacy rows predate this column (null) and are read as the display currency.
currency?: string | null
// The rate frozen when the transfer was settled, units of `currency` per 1 trip
// currency (#1445). Absent, or exactly 1, on rows written before the freeze existed.
exchange_rate?: number
created_at?: string
// The day the transfer actually happened; editable, unlike created_at (when it
// was recorded). Null/absent on rows predating this field.