Loans: record term changes in the schedule

NI
11d ago
+3348-93

Step 3.1 of #4955.

Problem

After an extra repayment the lender lowers the payment or shortens the term. Sure has no place to record that. A transfer to the loan lowers the balance, but the Schedule tab, the monthly payment and the payoff date stay on the original contract, so they no longer match the lender.

What changes

The loan form gets a "Term changes" list, for fixed and adjustable loans. Each row has an effective date and at least one of: an extra repayment, a balance the lender confirmed, a new payment.

  • Payment left blank: the schedule spreads the new balance over the remaining term. Lower payment, same end date.
  • Payment entered: it holds until the rate changes or the next term change. Same payment, shorter term.
  • The Schedule tab, the monthly payment on Overview, the payoff chart, the payoff projection, the assistant's get_accounts and the #4470 replay follow the rows.
  • A form opened before somebody else saved keeps their rows, as #4445 does for rate changes: it removes only the rows it showed, and a row left unchanged in it does not overwrite a later edit.
  • The chart calls the schedule of a loan with term changes "Current schedule" instead of "Original schedule".
  • A loan without term changes is unchanged: default_convention_baseline_test.rb passes untouched.

Rules, chosen so the schedule and the #4470 replay land on the same balance:

  • An extra repayment within the 5 grace days after a due date lowers the balance that period is charged on. Later in the period it comes off on the next due date, after that period's interest. On a due date itself it comes off after that day's payment, and the payment changes from the next due date.
  • A confirmed balance part-way through a period charges the rest of the period on that balance. On a due date it is the balance after that day's payment.
  • Refused: a row dated on or before origination, in the future or after the schedule's last payment, a balance above zero confirmed for the last payment of the term, an extra repayment after the loan is repaid (on the last payment date, or after a balance confirmed at zero), two rows with the same date, a row with both an extra repayment and a balance, a payment that does not cover its period's interest, and a date or amount the columns cannot hold.

Term changes are in the family export and import. A single-file import refuses a row the form would refuse; a chunked import session leaves that to the next save of the form, because a later chunk can bring the loan's opening history.

Known limits

  • A rate reset on a fixed loan still needs the rate type set to Adjustable and a rate change. Rate changes stay in variable_rate_schedule, as agreed in #4955.
  • A shorter term is not stored. After a later rate change or term change the lender's payment has to be entered again, or the schedule returns to the original end date. The Term card on Overview keeps showing the original term.
  • A lender that charges interest by the day will differ from the schedule in the month of a mid-period extra repayment. A confirmed balance on the next due date brings the schedule back to the lender's figure.
  • Shortening the term does not re-check stored rows. A row can then leave a balance owing after the last payment: the schedule has no payoff date, and Total Cost shows "Unknown". Or an extra repayment lands on the new last payment date and has no effect. The next save of the form refuses both.
  • An extra repayment is recorded as a term change and as a transfer or balance update. Deriving one from the other is not part of this change.
  • On an insured loan the replay takes the premium out of a linked transfer, so the projection is one premium off in the month of a change.
  • On a loan with principal_only_transfers on and the replay active, an extra repayment entered as a plain transaction on the loan account, not as a transfer, pays the period's interest first in the replay. Until the next due date the projection charges that interest once more.
  • The projection pays the schedule's remaining rows. When a held payment ends the schedule before the original maturity and the borrower is behind, the not-paid-off notice measures at the schedule's last date, although its text says "original maturity".
  • The row list copies the rate-change list's controller and markup. One shared controller for both is a follow-up; it would also fix the rate-change "Remove" button closing the edit dialog on main.

Step 3.2 follows in its own pull request: the end of a fixed period, the projected balance on that day, a chart marker and a payment dated ahead.

Migration

Adds the table loan_term_changes (foreign key to loans with cascade, unique on loan and effective date, five check constraints). No change to existing tables and no backfill.

Validation

  • bin/rails test, with main (627fbfa39) merged: 19,409 runs, 0 failures. The merge resolved the conflicts with #4445: the rows now save under its schedule lock.
  • The loan system tests: 17 runs, 0 failures. New: test/system/loan_term_changes_test.rb. In a two-window browser run, a form left open kept the row the other window saved.
  • RuboCop, ERB lint and Brakeman clean. Biome reports nothing in the new controller.
  • Replay parity tests in test/models/loan/principal_payments_test.rb: a borrower who pays the schedule ends on the schedule's balance to the cent, with and without principal_only_transfers, for a variable rate, actual day counts and an insured loan.

Summary by cubic

Adds a "Term changes" list to the loan form so the schedule records what the lender changed after origination: an extra repayment, a confirmed balance, or a new payment. The Schedule tab, the monthly payment on Overview, the payoff projection, the assistant get_accounts, and the #4470 replay follow these rows, and the rows travel with family export and import. Saving term changes starts a sync since the replay reads the schedule; loans without any are unchanged. The payoff chart labels a loan with term changes as "Current schedule" instead of "Original schedule".

Rules

  • A blank payment spreads the new balance over the remaining term (lower payment, same end date); an entered payment holds until the next rate or term change (same payment, shorter term).
  • An extra repayment inside the 5 grace days after a due date lowers the balance that period is charged on; on a due date it applies after that day's payment, later in the period on the next one.
  • A confirmed balance part-way through a period charges the rest of the period on that balance; on a due date it is the balance after that day's payment.
  • Validation refuses a row dated on or before origination, in the future or after the schedule's last payment, a row with both an extra repayment and a balance, two rows on the same date, a balance that leaves the loan owing after its last payment, a payment that doesn't cover its period's interest, amounts the columns can't hold, and an extra repayment after the loan is repaid. A single-file import refuses the same rows; a chunked import leaves that to the next form save. The refusal of a late row holds whatever order the rows are submitted in.
  • A form opened before another save keeps rows saved since it was opened: it removes only the rows it showed and leaves unchanged rows alone.
  • The payoff projection runs through a period with no scheduled payment when a later one repays the loan; when a stored row leaves the loan owing past its last payment, both Total Cost cards read "Unknown".

Known limits

  • Adds table loan_term_changes (cascade foreign key, unique on loan and effective date, five check constraints); no backfill runs.
  • The shorter term is not stored; after a later change the payment must be entered again or the schedule returns to the original end date.
  • The form's hint asks to record an extra repayment also as a transfer and a confirmed balance as a balance update; one is not derived from the other.
  • On an insured loan, the replay takes the premium out of a linked transfer, so the projection is one premium off in the month of a change.

Summary by CodeRabbit

  • New Features
    • Record dated loan term changes for fixed- and variable-rate loans, including extra repayments, confirmed balances, and revised payments.
    • Add or remove changes in the loan form and see their effects on payment, payoff projections, and schedule balance adjustments.
    • Include loan term changes when exporting and importing family data.
  • Bug Fixes
    • Flag invalid or conflicting changes before saving, including changes outside the loan schedule and payments below accrued interest.
    • Show loan cost as unknown when a schedule has no payoff total.

Screenshots

The loan form with two term changes: an extra repayment, then an extra repayment with the payment the lender set.

Loan form with the Term changes list

The Schedule tab: the payment drops from the row after the extra repayment, and the row shows the term change.

Schedule tab with a term change row