Convert a disposal's proceeds before subtracting its cost basis
Upstream: we-promise/sure#3616, which fixes we-promise/sure#3615 — same branch, same commit. This is the fork pre-flight.
Fork pre-flight for the upstream PR (fixes we-promise/sure#3615); base is mirror/upstream-main and the branch is rooted at upstream main, so the diff is the one upstream will see.
Fixes we-promise/sure#3615. (Qualified: a bare #3615 resolves to a fork issue that does not exist — the number only works on the upstream copy of this body.)
Behaviour change, stated plainly
A cross-currency disposal with no exchange rate for its date previously contributed a wrong figure to the Reports card total, converted at whatever rates_for returned. It now contributes nothing, and its line reads "no data".
That is the intended change and it is a visible one: a family holding foreign-listed securities with gaps in its rate history will see the realised-gain total fall, because figures that were being counted at a fabricated rate have stopped being counted at all. The alternative — keeping a number nobody can reproduce — is what R13 forbids and what this PR exists to remove.
The proceeds are converted into the basis's currency before the subtraction, at the disposal's own date, with no parity fallback — a missing rate means no figure rather than a figure at 1.0.
Why this conversion direction
MarketDataImporter#required_exchange_rate_pairs takes every entry currency against its account's as its first required pair, so a EUR disposal in a USD account has a EUR→USD row for the day it happened. USD→EUR is only ever present by accident of another account, so converting the basis into the trade's currency instead would lose disposals to rates the sync never fetches. Same figure, fewer unmeasurable disposals.
What changed
app/models/trade.rb—converted_to_basis_currency, exact date and exact direction, nil when there is no rate. PlusTrade.preload_exchange_rates, which answers a page of disposals' rates in one query instead of one per foreign disposal. A preload miss falls back to the single lookup rather than being authoritative: the preload keys the basis side on the account's currency, and a holding carried in a third currency would otherwise be excluded though its rate is on file.app/controllers/reports_controller.rb— converts from the currency the figure actually carries, adds the holdings' own currencies to the list it enumerates (a third currency previously fell through the|| 1parity fallback), and hands the partial a per-trade converted figure.app/views/reports/_investment_performance.html.erb— the per-trade line was taking the number out of theTrendand re-labelling it family currency with no conversion at all. It now renders the controller's converted figure, so the line and the card total are the same arithmetic. Conversion stays out of the template.
Migration notes
No migration. No schema change, no data rewritten.
Demo data
No demo data needed: the existing generator already produces foreign-currency trades, and the fix changes figures rather than adding a surface.
Screenshots
No visual change beyond the figures themselves — the per-trade line renders the same markup with a correctly converted amount.
Verification
bin/rails test: 9249 runs, 37391 assertions, 0 failures. One error,RecurringTransaction::PipelineTest, is a localdatabase.ymluser:/username:issue that opens a second connection as the OS user; it reproduces onmainunchanged.bin/rubocop,bundle exec erb_lint,bin/brakeman: clean.- Five mutations checked, each failing a named test: dropping the conversion, falling back to 1.0 on a missing rate, removing the rate preload, restoring the view's re-labelling, and dropping the holdings' currencies from the card's currency list.
Blast radius
Trade#realized_gain_loss has exactly two consumers, both in this diff. Trend is used widely, but this current/previous pairing is constructed in one place. holding.avg_cost also feeds Holding#trend for unrealised gains — untouched, and still in the holding's currency there.
Single-currency users see no figure move. Cross-currency users see corrected figures on Reports, which is the point.
The ExchangeRate.rates_for(...) || 1 parity fallback on the statement-conversion leg, and convert_current's Date.current holding conversion, are not touched here: they belong to the card rather than to this method, and both predate this change.