Date a new loan's opening anchor on its start date

Merged
JA
2mo ago
+173-3

Summary

Closes jaysbeekay/sure#424

A new loan's opening anchor holds the original principal, but Account.create_and_sync dated it on the form's "Opening balance date", which defaults to today minus two years. A loan that started earlier therefore had no recorded balance between its start date and that default. Every chart window opening before the default read $0 and then jumped to the principal.

With this change, when a new loan is created with an original balance and a start date on or before today, the opening anchor is dated on the start date. The form always sends an opening balance date, so the server cannot tell the default from a deliberate choice, and the start date wins (owner decision, 2026-10-12). A future start date keeps the form's date: an anchor after today would skip today's reconciliation and leave no balance for today (Gatekeeper note 2).

Upstream context: we-promise/sure#3383. The anchor-date change in Account.create_and_sync is upstream-shaped, since the method, AccountableResource#create and the account form are the same on both mains. This PR is fork-first and is not itself raised upstream. The anchor-date fix stays an upstream candidate: an upstream version would be cut separately from upstream/main, without the fork-local parts, which are:

  • the tests' reliance on the fork's loan form (rate type, term, start date) and the Loan columns behind it;
  • the plan's assertions on Loan::AmortizationSchedule / Loan#original_balance, which this PR leaves unchanged (Loan#original_balance still reads the anchor amount, which does not change);
  • Loan::PayoffChart from jaysbeekay/sure#403, which this PR does not touch.

Changes

  • app/models/account.rb, Account.create_and_sync:
    • in the loan branch (initial_balance present, account.loan?, start_date present and <= today), the anchor date becomes accountable.start_date;
    • Date.current is read once into today and used for the start-date bound, the reconciliation gate and the reconciliation date. Before this change the method read it twice. The only new date is the caller-supplied start_date; nothing else is derived.
  • config/locales/views/loans/en.yml: loans.form.start_date_hint now says a new loan's balance history starts on this date, not the opening balance date. The "Opening balance date" field stays, no ERB changed and no JavaScript was added. The ca/ru/tr/uk strings still read "leave blank to use the account's opening date", which remains true, so they are left for translators.
  • Tests in test/controllers/loans_controller_test.rb and test/models/account_test.rb.

Blast radius

Who else hits this line, and what do they pass? Re-grepped at 3b1e600e2. create_and_sync has 25 call sites besides its definition:

  • account.rb: the 9 create_from_* call sites (:300, :328, :340, :376, :406, :424, :450, :469, :502);
  • qif_import.rb:493;
  • CoinStats exchange_portfolio_account_manager.rb:54 and defi_account_manager.rb:68;
  • Brex account_flow.rb:178 and account_flow/setup.rb:127;
  • the Lunchflow (:188, :571), Sophtron (:270, :571), Up :366, Akahu :295, Monobank :354, Fio :349, Mercury (:186, :569) and Enable Banking :254 controllers;
  • AccountableResource#create:44.

Every provider and import caller passes accountable_attributes holding only subtype, tax_treatment, available_credit, Brex's per-type defaults, or nothing. None passes initial_balance or start_date. Only AccountableResource#create (the manual account forms, reached for loans via LoansController) can pass both. No api/v1 controller creates accounts. The new branch therefore stays off for every other caller, which "a provider-created loan keeps the default opening date" pins.

What else reads what I changed? The anchor date is read by:

  • Balance::BaseCalculator#calculation_start_date, Balance::ForwardCalculator / ReverseCalculator and Balance::Materializer: rows now start on the start date;
  • Account#history_start_date and UI::Account::Chart#period: "All" starts on the start date;
  • Family#oldest_entry_date: the family-wide "All" can start earlier. This is deliberate;
  • net worth and balance-sheet series: the liability appears from the start date, which is correct;
  • Loan#account_opening_anchor_date: it now returns the start date, the same date the schedule already uses, and Loan#original_balance is unchanged.

OpeningBalanceManager's rule that the anchor precedes the oldest entry cannot trigger, because a new account has no other entries. Family::DataImporter, AccountImport, QifImport and IbkrAccount::Processor call set_opening_balance directly and are unchanged.

Tests

Positive (fail-first on 3b1e600e2, before the fix):

  • "a new loan's opening anchor is dated on its start date": Expected: Wed, 15 Jan 2020 / Actual: Sat, 12 Oct 2024
  • "a new loan's recorded balance opens on its principal at its start date": the first value of the monthly balance_series from the start date was 0.0, expected 400,000. It then asserts balances.minimum(:date) is the start date (it was 2024-10-12).
  • "create_and_sync dates a loan's opening anchor on its start date" (model level): Expected: Wed, 15 Jan 2020 / Actual: Sat, 12 Oct 2024
  • "a loan started today has its opening anchor today and no reconciliation": Expected: 1 / Actual: 2 valuations. Before the fix, the anchor sat two years back and a reconciliation was added today.

Negative (pass before and after):

  • "a new loan keeps its entered current balance": today's valuation and balance row are 399,600 (we-promise/sure#3301);
  • "a loan without a start date keeps the form's opening date";
  • "a loan with a start date but no original balance keeps the form's opening date". This one was added beyond the plan, to pin the initial_balance guard: without a principal, the anchor holds the current balance, which was not the balance on the start date;
  • "a loan with a future start date keeps the form's opening date and today's balance";
  • "create_and_sync keeps a depository account's opening balance date";
  • "a provider-created loan keeps the default opening date" (SimpleFIN create_from_simplefin_account(..., "Loan")).

The negative tests post an opening date of today minus three years. OpeningBalanceManager#default_date is two years, so a mutant that passed nil through would otherwise be indistinguishable.

Mutations

Each mutation was applied to the committed fix and reverted with git restore. git diff HEAD was empty afterwards.

MutationResultFailing tests (examples)
Remove the overridered, 4 failuresopening anchor dated on its start date; recorded balance opens on its principal; started today; model start-date test
Drop account.loan?survives (equivalent)Only Loan accepts initial_balance, and only loans has start_date, so the guard is unreachable without it. It is kept for intent
Apply to every accountable (use account.start_date)red, 11 failuresdepository keeps its opening balance date; provider-created loan keeps the default; and others
Drop the start_date presence guardred, 19 errorsNoMethodError on nil <= today across every create
Let a nil start date through (nil? ||)red, 5 failuresloan without a start date keeps the form's opening date; depository keeps its opening balance date
Reconcile on the start date instead of todayred, 5 failures + 2 errorsa new loan keeps its entered current balance; future start date keeps today's balance
Drop the on-or-before-today boundred, 1 failurea loan with a future start date keeps the form's opening date and today's balance
Drop the initial_balance guardred, 1 failurea loan with a start date but no original balance keeps the form's opening date

Validation

  • Neighbouring suites: 653 runs, 5972 assertions, 0 failures, 0 errors, 0 skips. They cover account_test, account/opening_balance_manager_test, account/chartable_test, balance/forward_calculator_test, balance/reverse_calculator_test, loans_controller_test, UI/account/chart_test, test/models/loan/, family/data_importer_test, qif_import_test, account_import_test and account_simplefin_creation_test.
  • Full bin/rails test: 12393 runs, 53472 assertions, 0 failures, 0 errors, 35 skips (no environmental failures this run).
  • bin/rubocop -f github on the changed Ruby files: no offenses.
  • bin/brakeman --no-pager: no warnings.
  • erb_lint: no ERB changed. On the two forms involved, it reports 2 errors on app/views/loans/_form.html.erb's rate-change inputs (missing autocomplete), which this PR does not touch and which are unchanged from main.
  • Checked against upstream AGENTS.md:
    • logic is in the model, the controller is untouched;
    • Minitest with fixtures, and Mocha only for sync_later;
    • i18n for the changed text, with no ERB, CSS or API change;
    • the commit subject is under 72 characters.

Evidence checklist

  • This PR covers jaysbeekay/sure#424 only.
  • The new tests are named above, with fail-first output.
  • Mutation results are attached in the table above.
  • The fixtures are built inside each test. Dates and amounts are explicit, matching the issue's repro (400,000 principal, 399,600 current, 2020-01-15).
  • Amounts are compared as BigDecimal/Money. No new financial calculation is added.
  • No personal data is included.

Deliberately left out

  • Existing loans: no data migration. An owner can edit the opening balance valuation's date.
  • Later edits: the anchor does not move when start_date is edited after creation.
  • The zero lead-in before the start date: that belongs to jaysbeekay/sure#403's cut-off.
  • The Schedule-tab chart's missing origination point: jaysbeekay/sure#100 / jaysbeekay/sure#390.
  • No upstream PR from this branch: this is fork-first. An upstream version of the anchor-date fix is a separate, later step.