← Back to Pull requests

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

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 16, 2026 07:50 UTC
Author

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.
  • <!-- This is an auto-generated description by cubic. -->
    ---

    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.

    <sup>Written for commit 3237dcbfeba8c508d9ecdce21f42edd34eae00c6. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/we-promise/sure/pull/5131?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>

    <!-- End of auto-generated description by cubic. -->

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    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.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    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

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: nielseulink Review duration: 20784m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #5131"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%