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

Merged
JA
1mo ago
+236-1

Closes #3383

Fork pre-flight: jaysbeekay/sure#440 (same branch, same commit). Its CI, review history and the mutation results live there.

Problem

A new loan's opening anchor holds its original principal, but Account.create_and_sync dates it on the account form's "Opening balance date", which defaults to today minus two years. The loan's own start date is never consulted. A loan that started earlier therefore has no recorded balance between its start date and that default:

  • the first balance row is the default date, not the start date;
  • a chart window opening before it reads $0 and then jumps to the principal, which is what #3383 shows;
  • Loan::PayoffChart's recorded line starts two years back rather than at origination.

Reproduced on main (3e35aaa07): a loan created with a start date of 2020-01-15, a $400,000 original balance and a $399,600 current balance gets its anchor dated 2024-10-13, and its monthly balance series from 2020-01-15 opens at 0.0.

Change

Account.create_and_sync: when initial_balance is present, the account is a loan and the loan has a start_date, the opening anchor is dated on that start date instead of opening_balance_date. The form always sends an opening balance date, so the server cannot tell the default from a deliberate choice; the loan's start date wins. Loan already validates start_date as on or before today, so the anchor never lands after today's reconciliation (#3301), and a test pins that.

The start date moves the anchor only when it is after Entry.min_supported_date; otherwise the form's opening date is used, as on main. There is no lower bound on the start date input, and the schedule keeps the real start date.

loans.form.start_date_hint now says a new loan's balance history starts on this date. The other locales' hints stay true and are left for translators.

Non-test diff: 9 lines in app/models/account.rb (6 of them comment) and 1 locale line.

Who else is affected

  • Every other create_and_sync caller (the create_from_* methods, the crypto-exchange path, QifImport, CoinStats, Brex, and the Lunchflow, Sophtron, Up, Akahu, Monobank, Fio, Mercury and Enable Banking controllers) passes no initial_balance and no loan start_date, so the new branch stays off for them. A test pins the SimpleFIN loan path.
  • Importers that call set_opening_balance directly are unchanged.
  • Readers of the anchor date: balances, the account chart's "All" period, Family#oldest_entry_date and net-worth series now start from the loan's start date. Loan#origination_date already preferred start_date and is unchanged.

Tests

Positive, each failing on main before the fix:

  • "a new loan's opening anchor is dated on its start date" (Expected: 2020-01-15 / Actual: 2024-10-13)
  • "a new loan's recorded balance opens on its principal at its start date" (Expected: 400000 / Actual: 0.0)
  • "create_and_sync dates a loan's opening anchor on its start date"
  • "a loan started today has its opening anchor today and no reconciliation"

Negative, passing before and after:

  • "a new loan keeps its entered current balance" (#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"
  • "create_and_sync keeps a depository account's opening balance date"
  • "a provider-created loan keeps the default opening date"
  • "create_and_sync rejects a loan with a future start date and writes no anchor"

Boundary at Entry.min_supported_date:

  • "a loan whose start date is on the earliest supported entry date keeps the form's opening date"
  • "a loan started the day after the earliest supported date is anchored on its start date"
  • controller: "a loan started on the earliest supported entry date is created with the form's opening date"

Each guard was broken deliberately and a test caught it, except dropping account.loan?, which is equivalent (only Loan accepts initial_balance) and is kept for intent. The table is on the fork pre-flight.

Validation

  • bin/rails test (on 43d571b07): 11834 runs, 54595 assertions, 0 failures, 0 errors, 34 skips
  • bin/rubocop -f github: no offenses on the changed files
  • bin/brakeman --no-pager: 0 warnings
  • No ERB, JavaScript, CSS or API change; no system tests apply.

Migration notes

None. Existing loans keep their anchor; an owner can edit the opening balance valuation's date. The anchor does not move when start_date is edited later.

Summary by CodeRabbit

  • Improvements
    • Loans with an initial balance now begin their balance history on the drawdown date when it is later than the supported-date boundary. Otherwise, the supplied opening-balance date is used.
    • Updated loan form guidance to clarify which date anchors balance history and what date to use when no drawdown date is entered.
  • Bug Fixes
    • Loans with a future drawdown date can’t be created.