diff --git a/client/src/components/Budget/useCostsLedger.test.ts b/client/src/components/Budget/useCostsLedger.test.ts index 91e6cb0bc..f0b6db478 100644 --- a/client/src/components/Budget/useCostsLedger.test.ts +++ b/client/src/components/Budget/useCostsLedger.test.ts @@ -16,7 +16,7 @@ import type { CostsSettlementResponse } from './costsModel'; import { useCostsLedger } from './useCostsLedger'; import { useFreezeMissingRates } from './useFreezeMissingRates'; -// FE-BUDGET-LEDGER-001 to FE-BUDGET-LEDGER-021 +// FE-BUDGET-LEDGER-001 to FE-BUDGET-LEDGER-022 const fx = vi.hoisted(() => ({ // One stable converter, the way useExchangeRates hands out a memoised one: USD is half a euro. @@ -394,6 +394,26 @@ describe('useCostsLedger: another currency for this trip (#1683)', () => { expect(createMock()).not.toHaveBeenCalled(); localStorage.removeItem('trek:costs-currency:7'); }); + it('FE-BUDGET-LEDGER-022: the owe and owed cards count only flows read in the currency shown', async () => { + localStorage.removeItem('trek:costs-currency:7'); + const { result } = setup(); + await waitFor(() => expect(result.current.totals.owe).toBe(15)); + expect(result.current.totals.owed).toBe(5); + settlementMock().mockRejectedValueOnce(new Error('Settlement not available offline')); + act(() => result.current.setBase('JPY')); + await waitFor(() => expect(result.current.settlementError).toBe(true)); + expect(result.current.totals.owe).toBe(0); + expect(result.current.totals.owed).toBe(0); + settlementMock().mockResolvedValueOnce({ ...SETTLEMENT, currency: 'EUR' }); + act(() => result.current.loadSettlement()); + await waitFor(() => expect(result.current.settlementError).toBe(false)); + expect(result.current.totals.owe).toBe(0); + settlementMock().mockResolvedValueOnce({ ...SETTLEMENT, currency: 'JPY' }); + act(() => result.current.loadSettlement()); + await waitFor(() => expect(result.current.totals.owe).toBe(15)); + expect(result.current.totals.owed).toBe(5); + localStorage.removeItem('trek:costs-currency:7'); + }); }); describe('useCostsLedger: writes', () => { diff --git a/client/src/components/Budget/useCostsLedger.ts b/client/src/components/Budget/useCostsLedger.ts index 031c3a18d..2911e9364 100644 --- a/client/src/components/Budget/useCostsLedger.ts +++ b/client/src/components/Budget/useCostsLedger.ts @@ -216,7 +216,8 @@ export function useCostsLedger({ // A settle-up books the flows in the display currency, so only flows read in it // may be booked: not after a failed read, and not while they are in another one. // And only by someone who may edit the costs at all. - const canSettle = canEdit && !settlementError && settledIn === base && flows.length > 0; + const flowsInBase = !settlementError && settledIn === base; + const canSettle = canEdit && flowsInBase && flows.length > 0; const splitting = useMemo( () => showsSplitting(trip?.costs_mode, memberCount, budgetItems, settlement, me), [trip?.costs_mode, memberCount, budgetItems, settlement, me] @@ -227,9 +228,10 @@ export function useCostsLedger({ const knownNames = useMemo(() => knownPersonNames(budgetItems, settlement), [budgetItems, settlement]); // `ctx` carries the rates. They can land after the expenses, and without it in the // deps the cards kept the sums they were first added up with while the rows moved on. + // The owe and owed cards print in the display currency too, so they count the same flows. const totals = useMemo( - () => computeTotals(budgetItems, flows, ctx, shareOf, leftOutCats), - [budgetItems, flows, ctx, shareOf, leftOutCats] + () => computeTotals(budgetItems, flowsInBase ? flows : [], ctx, shareOf, leftOutCats), + [budgetItems, flowsInBase, flows, ctx, shareOf, leftOutCats] ); const filtered = useMemo( () =>