fix(investments): report real dividend and interest totals
Problem
InvestmentStatement::Totals returns hardcoded zeros for dividends and interest:
dividends: 0, # Dividends come through as transactions, not trades
interest: 0, # Interest comes through as transactions, not trades
That comment was accurate until #1311, which moved investment income to Trade records with qty: 0 and price: 0 via Trade::CreateForm#create_income_trade.
Since #1311, InvestmentStatement#total_dividends, #total_interest and PeriodTotals#total_income have returned zero for every family, regardless of data. A user who records dividends through the trade form has no aggregate anywhere that reflects them.
Fix
Aggregate dividends and interest by trades.investment_activity_label in the existing single-query aggregation, and return the real values.
Property worth noting:
- No double-counting. Income trades carry
qty: 0, so they fall outside both theqty > 0(contributions) andqty < 0(withdrawals) branches already in the query. Covered by a dedicated test.
The totals cache key is bumped to v2 so warm caches stop serving the old zeros before entries_cache_version happens to change on its own.
Deliberately out of scope
These are known gaps, listed so they aren't mistaken for oversights. Each is a separate concern with its own trade-offs:
-
Trading212 and SimpleFIN dividends still report 0. They are stored as
Transactionrecords carryingextra["security_id"](seeTrading212Account::ActivitiesProcessor#process_dividend), not as Trades. Reading both shapes is a product decision — see questions below. -
Plaid dividends also report 0, for an unrelated pre-existing reason:
PlaidAccount::Investments::TransactionsProcessor#find_or_create_trade_entrycomputesamount: derived_qty(transaction) * transaction["price"], and a cash dividend hasquantity == 0, so the entry is written with amount 0. Fixing that touches a provider write path and needs its own sign-convention review; happy to open it as a follow-up.
Questions for maintainers
- How should pre-#1311 dividend transactions be treated? Instances that upgraded across #1311 have dividend history split in two:
Transactionrows before the upgrade,Traderows after. #1311 deliberately left the transactions table untouched, so no backfill exists. Options as I see them:
- leave them as-is and accept that investment income aggregates cover only post-upgrade data;
- have the aggregate also read dividend-labelled Transactions (via
investment_activity_labelplusextra["security_id"]), which makes history complete but entrenches two storage shapes; - offer a migration path.
I've kept this PR to trades-only rather than pick one. Which direction do you prefer?
-
Should provider-delivered dividends be normalized to Trades? Trading212 has a ticker and could resolve a security; SimpleFIN structurally cannot (which is why
SimplefinAccount::Investments::TransactionsProcessoris a no-op). So full normalization isn't reachable for every provider — is forward-only normalization where a security is resolvable worth doing, or should the readers handle both shapes instead? -
Should dividends appear in
IncomeStatementtoo?
All threeIncomeStatementstat classes hard-joinentryable_type = 'Transaction', so dividend income is currently absent from reports, budgets and category breakdowns.
Is investment income intended to be reported separately from ordinary income, or should it flow into both? -
Should
Reinvestmentcount as dividend income, and if so what happens tocontributions? Per the reasoning above, the two are coupled: counting reinvested distributions as income requires deciding whethercontributionsmeans all buys or only external inflows. If contributions are meant to be external only, thenReinvestment(and arguably the internal-movement labels) should leave that bucket regardless of this PR, I can do that as a follow-up. If contributions are meant to be all buys, then reinvested distributions can only be surfaced as a separate figure rather than added tototal_dividends. Which is intended?
Summary by CodeRabbit
- Bug Fixes
- Investment totals now include dividends and interest from eligible transactions.
- Income received in foreign currencies is converted correctly.
- Dividends and interest are excluded from contribution and withdrawal totals.
- All-time income totals now reflect recorded investment income accurately.
- Previously cached totals are refreshed to ensure updated results are displayed.