Loans: record when the fixed period ends and project the balance then

NI
11d ago
+4070-97

Step 3.2 of #4955. Builds on #5130; until that one is merged, only 05bf5dc52 and ea71a04d6 belong to this pull request.

Problem

A mortgage's terms are fixed for a period: a Dutch fixed-rate period, a Canadian renewal term. What the borrower wants to know is how much they will still owe when that period ends. Sure has no place for that date, and a payment the lender has already announced for a later date cannot be recorded until the day it takes effect.

What changes

  • Each row under "Term changes" gets a "Fixed until" date. A row can carry it alone: a row dated on the origination date with only that field records the period the loan opened with. Such a row does not change the schedule.
  • The Overview tab shows "Balance at End of Fixed Period": the balance the payoff projection leaves on that day, starting from today's balance, with the date under it.
  • The balance chart marks that day with a line, a dot on the projected line and a label. The accessible description names the date and the balance.
  • A row dated in the future can carry a payment and a fixed-until date, for a payment the lender has announced. An extra repayment or a confirmed balance dated ahead is still refused: the projection would read a planned repayment as a shortfall.
  • The period in force is the latest row with a fixed-until date that has taken effect. A later row that starts a period of its own ends it early (an early renewal); a payment-only row does not.
  • A fixed-until date cannot be after the end of the loan's term.
  • The family export and import carry the date. The assistant's get_accounts reports it as fixed_until.

Known limits

  • Only the period in force is shown and marked. There is no history of periods on the chart and no payment frequency.
  • The fixed-until date is not tied to the rate. A rate reset is still a rate change on an adjustable loan.
  • The balance is the projection's, from today's balance. For a borrower on contract it equals the schedule's.
  • Locales other than en and nl fall back to English, except the two chart keys in de, which a test requires.

Migration

Adds loan_term_changes.term_end_date and replaces the check constraint chk_loan_term_changes_has_amount with chk_loan_term_changes_records_something (an amount or an end date), plus chk_loan_term_changes_term_ends_after_start. No backfill. Rolling back deletes rows that record only an end date, because the old constraint cannot hold them.

Validation

  • bin/rails test, with main (627fbfa39) merged through #5130: 19,439 runs, 0 failures.
  • The loan system tests: 18 runs, 0 failures. test/system/loan_term_changes_test.rb covers the form field, the card and the marker.
  • RuboCop, ERB lint and Brakeman clean.
  • The rollback was run on a test database with an end-date-only row and a row with a payment and an end date: the first is deleted, the second kept, and the migration applies again.

Summary by cubic

Loans can now record changes the lender makes or announces — extra repayments, confirmed balances, new payments, and how long the current terms stay fixed. The recorded changes shape the amortization schedule, and a loan with any of them quotes the repayment in force, as on a variable loan; the balance chart also labels the schedule "Current schedule" once terms change.

  • Each row under "Term changes" carries a "Fixed until" date; a row on the origination date with only that field records the opening period without changing the schedule, and a later one ends the period in force early, as an early renewal would.
  • The Overview tab shows "Balance at End of Fixed Period," projected from today's balance; the balance chart marks the day with a line, a dot on the projected line and a label.
  • An extra repayment on a due date is taken off after that day's payment; one outside the grace days is counted in full.
  • Rows with duplicate dates, a payment that does not cover interest, a date after the last payment, or an extra repayment after a balance confirmed at zero are refused; while a stored row leaves the loan owing at the end, both Total Cost cards say Unknown.
  • The projection runs through a payment pause when a later payment repays the loan.
  • A lender-announced payment can be dated ahead; an extra repayment or confirmed balance dated ahead is still refused.
  • A fixed-until date cannot fall after the end of the loan's term.
  • A loan form keeps term changes saved since it was opened; reopening a loan shows them rather than the rows the stale form would send.
  • Family export/import and the assistant's get_accounts carry the date as fixed_until; imported term changes are validated against the schedule, and in a chunked import they wait for the next form save.

Migration

Adds loan_term_changes.term_end_date and replaces the check constraint with one that accepts an amount or an end date. Rolling back deletes rows that record only an end date.

Summary by CodeRabbit

  • New Features
    • Record dated loan changes, including extra repayments, confirmed balances, updated payments, and fixed-term end dates.
    • See how recorded changes affect projected payments, balances, interest, and payoff dates. Fixed-term end dates and projected balances appear on the loan overview and payoff chart.
    • Loan data exports and imports preserve recorded term changes.
  • Improvements
    • Invalid changes are flagged when they conflict with the loan’s dates, schedule, or interest requirements.
    • Loan schedules show balance adjustments, and total cost is displayed as unknown when the loan is projected to retain a balance after its final payment.

Screenshots

The balance chart marks the end of the fixed period, and the Overview tab shows the projected balance on that day.

Chart marker and the Balance at End of Fixed Period card

The loan form: a row dated on the origination date with only a fixed-until date records the period the loan opened with.

The Fixed until field on a term change row