feat(loans): variable-rate payoff projection (#12)
Summary
Follow-up to #3296 (closing #3295), addressing #3332: the amortization schedule in #3296 is purely static, generated once from the loan's contracted terms. It never reflects the account's real, current balance, so a user who pays extra toward principal (e.g. a $44k lump sum) sees no benefit — the schedule still shows the original payoff date as if the extra payment never happened.
This PR adds a second, actual-balance-based projection alongside the original schedule.
Depends on #3296 — this branch is stacked on top of it (feat/loan-variable-rate-amortization-api) and only contains the incremental diff. Should not be merged before #3296.
Changes
Loan::PayoffProjection(app/models/loan/payoff_projection.rb) — keeps the original schedule's monthly payment fixed, starts from today's actualaccount.balance, and simulates forward until the balance clears. Because the real balance is already below what the original schedule predicts for today, the same payment retires it sooner. Scoped to fixed-rate loans for now.Loan::AmortizationMath(new) — the per-period interest/principal step, extracted out ofAmortizationSchedule#generate_scheduleso both the original schedule and this projection share identical math rather than duplicating it.Loan#payoff_projection— memoized accessor, mirrors the existingamortization_scheduleaccessor.- Schedule tab UI — two new summary cards ("Projected Payoff", "Interest Saved") shown only when the projection meaningfully diverges from the original (guards against a rounding-driven single-payment artifact between two independently-terminated simulations).
- API —
GET /api/v1/loans/:id/amortization_schedulenow includes apayoff_projectionobject (current_balance, projected_payoff_date, projected_total_interest, months_saved, interest_saved), null when not applicable. OpenAPI docs regenerated.
Caveats (documented in code)
- Treats
account.balanceas principal-only, same assumptionAmortizationSchedulemakes aboutoriginal_balance. A provider-synced mortgage balance that includes escrow will understate interest/time saved. - "Original remaining interest as of today" is computed relative to the next scheduled payment date, not true daily accrual — consistent with the rest of this feature.
- Out of scope for this PR: variable-rate interaction, and a comparison chart (tracked as a stretch goal, not bundled here — see plan discussion in #3332).
Test plan
-
bin/rails test test/models/loan/ test/models/loan_test.rb test/controllers/accounts_controller_test.rb test/controllers/api/v1/loans_controller_test.rb— all passing - Full suite:
bin/rails test— 7524 runs, 0 failures, 0 errors -
RAILS_ENV=test bundle exec rake rswag:specs:swaggerize— docs regenerated -
bin/rubocop -a,bundle exec erb_lint -a,bin/brakeman— all clean - Manual verification in a running app once #3296 lands
Closes #3332.
🤖 Generated with Claude Code
https://claude.ai/code/session_01SYkeVCo6sffP8x7Job2xFj
Summary by CodeRabbit
-
New Features
- Added payoff projections to loan schedules, showing the projected payoff date, months saved or added, and interest saved or added.
- Added payoff projection details to the amortization schedule API for eligible fixed-rate loans.
- Projections reflect the current balance and existing monthly payment.
- Unsupported, fully paid, or non-amortizing loans return no projection.
- Payoff dates now honor the loan’s scheduled payment day.
-
Documentation
- Updated API documentation for payoff projection responses.