Measure the Reports card's realised gains with the hub's implementation

Merged
JA
2mo ago
+262-115

Closes #167.

The Reports card and the portfolio hub each measured realised P&L from sell trades, to
their own rules. The card now reads Portfolio::RealizedGains — the implementation P41
gates — and keeps its own account scope by passing it in.

One figure moves, and the issue's own impact analysis had it the other way round

The analysis on #167
concluded that the parity fallback was "already gone" from the realised line, on the strength
of this guard in the card:

return nil unless rate.to_d.positive?     # reports_controller.rb, before this PR

It is not gone, because the rate that guard checks has already been substituted.
ExchangeRate.rates_for ends in rate&.rate || 1, so a pair with no row for that date
comes back as 1 — a positive number, indistinguishable from a genuine one-to-one rate. The
guard catches a stored 0 and lets parity straight through.

Measured, not argued. With main's controller restored under the new test:

ReportsControllerTest#test_a_disposal_with_no_rate_for_its_trade_date_is_excluded,_not_converted_at_parity
100 EUR with no rate for its date is not $100.00.
Expected: "$0.00"
  Actual: "$100.00"

A 100 EUR gain, no EUR→USD rate on file for the trade date, reported as $100.00 on a USD
family. Portfolio::RealizedGains reads the rate rows directly — "DELIBERATELY NOT
ExchangeRate.rates_for", as the comment there says — so the same disposal is excluded and
named.

What else changes

Signal, not arithmetic. A disposal the card cannot measure is now listed with the reason it
could not, under the treatment it belongs to, in the same order and wording the hub's section
uses. Before, it was dropped silently: a total missing a disposal looked exactly like a total
with nothing to miss.

What deliberately does not change

  • The account scope. Both scopes already honour included_in_reports — the hub's runs
    through BalanceSheet::HistoricalAccountScope#relation, which applies it — so that is not
    the difference between them, contrary to how #167's body frames it. The differences are that
    the hub restricts to historical statuses, to Investment/Crypto accountables and to the
    accounts a user may see, and that it stops a disabled account on its cut-off date. Each of
    those would move a figure on a page people already read. The accounts are passed in, and no
    active_until_dates is passed.
  • The unrealised line. It still converts holdings at Date.current through the same
    rates_for helper, and therefore still at parity where a rate is missing. That is the
    helper's defect, not this card's: five callers inherit it, including
    BalanceSheet::AccountTotals and Accountable. Raised upstream as we-promise/sure#3640.
    This PR does not close the parity problem — it removes it from one leg of one figure.

Blast radius

Who else hits this line, and what do they pass?

Portfolio::RealizedGains had one caller, InvestmentStatement#realized_gains, which passes
the historical scope and its cut-off dates. It now has two. The change to it is additive: the
per-disposal record it always built internally is exposed as #disposals, and #buckets,
#net, #excluded_trades and #any? are derived from the same list they were derived from
before. Its 20 tests, including the three P41 query-count gates, pass unchanged.

The two preloads added to its trade query (:security, account: :accountable) are what the
Reports partial reads per line. Constant, not per disposal, so the flat query counts P41 gates
still hold — those tests assert few == many across 2 and 6 disposals rather than a magic
number, and they still do.

Tests, and the mutation each was watched failing under

TestMutationObserved
a disposal with no rate for its trade date is excluded, not converted at paritymain's controller restored$100.00 instead of $0.00
an unmeasurable disposal is named on the card and does not move the totalexcluded_trades: dropped from the payloadreasons list empty
a disposal in an account excluded from reports stays out of the card's totalscope mutated to Current.family.accounts.historical$200.00 instead of $100.00
a disposal in a disabled account still counts on the cardthe hub's active_until_dates passed in$0.00 instead of $100.00

The last one did not fail the first time I mutated it: disabling the account today puts the
cut-off at yesterday, which a disposal earlier in the month is not after, so the hub would have
kept it too. The fixture now sets disabled_at to the disposal's own date, and the test
records why.

P41 is extended to name the three card tests, so a future re-fork of these rules in the
controller cannot leave the contract still claiming one implementation.
bin/rails portfolio:verify_contract_coverage was watched failing on a renamed row before
being trusted.

What is NOT measured

#167's triage plan asked for the magnitude first — how many real disposals hit
missing_cost_basis versus missing_exchange_rate. The development corpus has no disposals
at all
(Trade.where("qty < 0").count == 0), so there is nothing here to measure it on. What
the table above establishes is the direction and the mechanism, on constructed fixtures, and
this PR does not claim a typical magnitude.