feat(loans): payoff projection and the loan balance chart

Merged
JA
2mo ago
+2804-74

Fixes #3332

Head 5ffe712: the chart commit ffc972b27, a merge of main, and two review-round commits (726c13c, 5ffe712). #3473, which it builds on, has merged, so the diff is only the projection and the chart.

What this does

On a loan account's page the balance chart carries three series on one axis:

SeriesWhat it isStyle
Recorded balanceaccount.balance_series, origination → today, in the loan's own currencysolid
Original schedulethe amortisation schedule from #2984, origination → contracted payoff, re-amortised at each recorded rate changedashed
Projected from todaytoday's recorded balance forward, paying the schedule's repayment period by perioddashed

Solid is fact, dashed is forecast; hue is secondary, so the chart reads in greyscale and under colour-vision deficiency.

The mount point is one loan-only branch in UI::Account::Chart. A loan account with a schedule takes the loan chart; every other account type, and a loan without a schedule, renders exactly the chart it has today, and a component test asserts that (time_series_chart_controller.js is untouched). The loan chart's title is "Remaining balance"; a loan without a schedule keeps "Remaining principal balance". If maintainers would rather keep this off the account page, moving the chart to the Schedule tab is a one-file change.

The projection pays the schedule's own repayment. Loan::Simulator gains a :scheduled payment strategy whose per-period amount comes from a callable; Loan::PayoffProjection passes the schedule's payment for that position. So a loan ahead of schedule keeps paying what the contract asks and finishes early, a future recorded rate change resizes the projected repayment exactly as it resizes the schedule, and a fixed loan's projection is byte-identical to a held-payment run (asserted). Extra payments already made are in the projection without being named: they are why today's balance is what it is. Modelling future extra payments is a later change. A borrower too far behind for the contracted repayment to clear the balance gets a notice quoting the balance that would still be owed, instead of a payoff date. The projection converges only when today's balance is at or below the contract's, so the interest card can only ever show a saving; there is no "additional interest" state.

The period governs the x-domain. The payload carries domain_start and domain_end. The loan chart's picker offers a subset of the shared periods (MTD, 90D, YTD, 365D, 5Y, 10Y, All) under the shared keys and labels, so a pick stays the user's default everywhere and means the same dates it does on every other chart; a saved period the loan chart does not offer reads as All. Under All the domain runs from origination to the later of the two payoffs; otherwise it is the period's own dates, opening no earlier than origination, so a window that opens before the loan does not draw a flat lead-in. A bounded window ends today, so the forward series draw under All alone, and the cards beside the chart quote the projection whatever the window. The recorded series is queried up to today. Period is untouched.

Cards and one reference date. Projected Payoff and Interest Saved sit beside the chart inside the chart_details frame and re-render with the period picker. AccountsController#show captures one @as_of and passes it to the payload, the Schedule tab and the Overview tab. The cards state the basis of their comparison: the schedule's dates are anniversaries of origination, not the lender's posting dates, so a payment posted before its scheduled date reads as ahead until that date. The Schedule tab gains a Forecasted Payoff Date card from the same projection.

Accessibility. The SVG is focusable, and its aria-roledescription is localized. Arrow keys, Home and End step a tooltip through the scheduled dates in the window, and Escape clears it. The tooltip becomes a live region only under the keyboard, so a pointer sweep never announces. The chart has no data table of its own, because the Schedule tab carries the figures; its description names the recorded balance and both payoff dates, and says when recorded history starts after origination. That description is the SVG's aria-label and real DOM text, marked privacy-sensitive like every other balance on the page. The tooltip is built from utils/chart_tooltip, like the other chart controllers, and formats dates and money in the request's locale, as the x-axis does its tick labels. A pointer snaps to the same dates the keyboard steps through, so the tooltip's heading is always the date its figures come from. Strokes are the theme's CSS variables, so the browser repaints the lines on a theme change without the controller watching for it.

Resilience. The payoff projection is built once per request and shared by the chart and the Schedule tab's card. The payload is skipped for frame requests outside the chart card (the activity feed's pagination), and any exception in it is reported and degrades the page to today's chart rather than a 500. An account whose balance is nil projects nothing rather than raising.

Conflict with #2733

#2733 added data-time-series-chart-selectable-value="true" to the fallback chart's element. It is carried into the fallback branch here, so non-loan charts keep drag-to-select. The loan chart does not have drag-to-select; that is a known gap rather than a regression.

#2733 also touched two more files this PR changes:

  • test/components/UI/account/chart_test.rb: both sides added tests at the same place. All of #2733's all-time clamp tests and all of this PR's loan chart tests are kept.
  • UI::PeriodPicker: #2733 rewrote it (a Period selection, a custom range row, date labels, explicit Turbo actions), and this PR adds options: so the loan chart offers a subset of the periods. Without options: the picker behaves exactly as #2733 left it. With options: it lists only those pairs, labels the trigger from them, and omits the custom range row. The loan chart cannot be drag-selected, and it already maps any period it does not offer, a custom range included, to All. New picker tests cover the narrowed menu, the option-labelled trigger and the absent custom row, and #2733's own picker tests pass unchanged.

Demo data

No new demo data: the chart draws the three demo loans #3473 gives real terms. They are paid from their own schedules, so the Home Mortgage and the Car Loan project their contracted payoff dates, and the Student Loan, one extra payment ahead, projects an earlier one. test/models/demo/generator_test.rb asserts that each demo loan's projection follows its balance.

Screenshots

The loan balance chart on the demo family's Home Mortgage under All. The mortgage is paid from its own schedule, so the projection lands on the contracted payoff. Taken on an earlier revision (2b13e0f15); since then the axis ticks are localized, the tooltip snaps to payment dates, and the bounded windows follow the shared periods. Under All the picture is unchanged.

Loan balance chart on the demo mortgage, light theme

Loan balance chart on the demo mortgage, dark theme

Migration notes

No migration in this commit; the columns the chart reads come from #3473's.

Changes since the first review

Addressing jjmata's review of ffc972b27 and CodeRabbit's threads, in 726c13c and 5ffe712:

  • Windows follow the shared Period contract, clamped at origination, with a fixture for a loan whose recorded history starts years after origination. YTD is the calendar year.
  • accountable_resource.rb is back to main; the form-rebuild change and its assertions move to a PR of their own.
  • Title: a new key for the loan chart only; remaining_principal_balance and its translations are untouched.
  • interest_added branch and translations removed as unreachable, with a test pinning the property.
  • Money.new(nil) on a nil balance no longer raises.
  • privacy-sensitive on the accessible description.

Verification

Model, component, controller and browser coverage, each observed to fail before the change it pins, including:

  • Projection: it pays the schedule's figure, and a variable loan ahead of schedule finishes early. A future recorded rate change moves the projected repayment by the schedule's amount. A fixed loan's projection is byte-identical to holding the payment. A loan too far behind reports a balloon and no payoff date; a loan even slightly behind does not converge.
  • Series and windows: the recorded series runs from the window's start to today and never starts before origination. Every loan window is a shared Period key and takes that period's own dates, clamped at origination. A 2016 mortgage with balances recorded from 2024 draws its recorded balance and the contract under every offered window.
  • Page and components: the legend lists only visible series, and the not-converged notice replaces the cards. Non-loan accounts mount the existing chart, and a payload that raises degrades to it. A loan without a schedule keeps its existing title. A frame request outside the chart card builds no payload. The account page builds the payoff projection once for the chart and the Schedule tab. The payload carries a localized role description.
  • In Chrome: all three lines paint in the light and the dark theme. The SVG carries its description and no data table. Arrow keys step the tooltip, and only the keyboard announces. The tooltip is formatted in the user's locale, and so are the axis's month ticks. A pointer just before a payment date heads the tooltip with that payment's month.

Locally on 5ffe712, on Ruby 3.3.6 with the version pin overridden:

  • Full bin/rails test: 9,720 runs, 4 failures, 4 errors, all environmental (ANTHROPIC_BASE_URL set in the shell, no libvips, Ruby 3.3 vs 3.4 Hash#inspect, and RecurringTransaction::PipelineTest's own PG.connect), none in files this PR touches.
  • The loan, account chart component, period picker, loans and accounts controller and i18n suites: 205 runs, 0 failures (4 skipped: the i18n key checks main also skips). Demo generator tests green.
  • bin/rubocop on the changed Ruby files, erb_lint on the changed ERB and bin/brakeman: clean.
  • test/system/loan_payoff_chart_test.rb could not run locally (ChromeDriver 147 against Chromium 141); it runs in CI.

Payload build for a 360-month loan with three years of daily balances, measured on the fork branch before this rebuild: 17 ms fixed, 68 to 83 ms variable with five changes, 30 ms for 1,200 months.

History

Squashed from jaysbeekay/sure#111, where the review history lives. The fork's plan for this delivery is docs/plans/loan-chart-upstream-delivery.md.

Summary by CodeRabbit

  • New Features
    • Loan accounts now include an interactive chart comparing scheduled, projected, and actual balances, with selectable time ranges and accessible chart details.
    • Loan schedules show a forecasted payoff date and estimated interest savings, or indicate when the current payment will leave a balance due.
    • Loan charts and payoff estimates reflect a consistent date across the account view.