Classify every investment cash movement in one place

Draft
JA
2mo ago
+1813-86

Summary

balances.cash_inflows does not distinguish an external deposit from a dividend landing in cash. Nothing downstream can therefore tell money the investor put in from money the investments earned — and those belong on opposite sides of a return: a deposit in the denominator, a dividend in the numerator. Counting one as the other misstates the figure in opposite directions, so a time-weighted return cannot be read off balances as they stand.

Portfolio::FlowClassifier answers that for one entry, or for a set of entry ids in one query, with the same rules either way. Five classes — external_inflow, external_outflow, income, fee, internal — decided by the activity label on the Trade or Transaction, the transfer's counterpart, and whether that counterpart is inside the scope being measured.

Model only, no callers. Nothing reads it yet; the return series that does follows as its own PR.

Part of #3494.

Fork pre-flight: jaysbeekay/sure#225 — same branch, same commits, so there is no cherry-pick and no divergence to reconcile. That is where this change's CI and Gatekeeper history lives, which is not visible from here.

⚠️ Stacked on #3557

This branches from up/pending-sql-parity, so the diff below carries that PR's eight commits until it merges. The reviewable part of this one is three files:

  • app/models/portfolio/flow_classifier.rb
  • test/models/portfolio/flow_classifier_test.rb
  • test/support/portfolio_flow_test_helper.rb

It stacks because the classifier's SQL path calls Transaction.pending_sql, which #3557 introduces. The alternative was to inline a second copy of the pending expression here — which is precisely the inconsistency #3557 exists to remove, so I would rather wait than re-create it. Rebased onto main the moment #3557 lands.

The one design choice worth arguing about

Scope decides what "internal" means. A position journalled between two accounts you own is internal and moves nothing. The same journal, to an account outside the scope being measured, is external — because from that scope's point of view the money left. So the classifier takes scope_account_ids and answers relative to it, rather than having one global answer per entry.

A label added to Transaction::ACTIVITY_LABELS without a rule in LABEL_RULES fails the test that compares the two, so a new label cannot silently fall into whatever the else-branch happens to be.

Demo data

No demo data needed: nothing renders it yet. The PR that introduces a caller carries the demo data for what it shows.

Migration notes

No migration.

Verification

  • bin/rails test test/models/portfolio/flow_classifier_test.rb: 22 runs, 239 assertions, 0 failures
  • bin/rails test (full, in the dev container): 9269 runs, 37768 assertions, 0 failures. One error, RecurringTransaction::PipelineTest, is a local database.yml user:/username: issue that opens a second connection as the OS user; it reproduces on main.
  • bin/rubocop: clean on all three files.
  • Three mutations checked: stopping the SQL path from skipping pending rows (2 failures), stopping the Ruby path from doing the same (3), and treating an in-scope security journal as external (2).

Blast radius

Who else hits this line, and what do they pass? Nobody — the class is new and has no callers in this diff. It reads entries, trades and transactions and writes nothing. The only shared thing it touches is the pending definition, which it takes from Transaction rather than restating.