← Back to Pull requests

Fix/loan payment principal split #4343

Open
unknown opened this pull request with 383 lines diff across 4 files
View on GitHub
unknown unknown commented on Oct 20, 2026 11:35 UTC
Author

Correct loan principal/interest splits and show cumulative paid totals

Summary

  • Fixed loan balance calculation — Interest is now accounted for before reducing principal. Previously, full payments were subtracted from principal in full, understating balances (and overstating principal paid).

  • Smarter linked transfer handling — Full EMIs linked from another bank are detected against the scheduled instalment and split into interest and principal components. Linked transfers that match the scheduled principal remain principal-only; larger or irregular transfers are still treated as prepayments.

  • Applies to all loan subtypes — Uses daily reducing-balance interest consistently across every loan subtype.

  • New overview metric — The loan overview now shows a cumulative “Paid so far” breakdown of principal vs. interest.

  • Review follow-ups — Same-day transactions are processed before valuation resets, unpaid interest carries into later unlinked payments, and the overview fallback uses the latest snapshot on or before the selected date, preferring a newer current anchor that is not dated in the future.

  • Recalculation on next sync — Existing loan balances are recalculated on the next sync. Expect an upward correction where interest was previously reducing principal.

  • No migrations or live-data changes — Safe to deploy without data backfills.
  • Validation

    | Check | Result |
    | --- | --- |
    | Full Rails suite | 12,125 tests, 56,358 assertions, 0 failures/errors |
    | RuboCop | 2,987 files, no offenses |
    | Brakeman | 0 security warnings |

    Notes for reviewers

  • The core change is in the balance/amortisation logic: payment allocation now follows interest-first, then principal, rather than applying the full amount against principal.

  • Linked-transfer classification logic distinguishes scheduled-instalment matches (split interest/principal) from principal-only matches and prepayments (larger/irregular amounts).

  • Because balances are recalculated on the next sync, some users will see their outstanding balance increase — this is the expected correction, not a regression.
  • <img width="2228" height="1967" alt="image" src="https://github.com/user-attachments/assets/0628c5e8-5fcc-4e7a-81fe-2657749e3557" />

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

    Summary by CodeRabbit

  • New Features

  • - Loan repayment progress now shows cumulative principal and interest paid, combined totals, and payment percentages as of the selected date.
    - Amortized loan payments now separate principal and interest more accurately, improving balance and repayment calculations.
    - Repayment details account for linked payments, prepayments, drawdowns, and variable interest rates.
    - Loans without amortization terms retain their existing payment behavior.

  • Localization

  • - Added English and French labels for paid-to-date repayment details.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 383 lines (+287 / -96) across 4 files Reviewer: jaysbeekay Review duration: 2642m
    All Vouch review confidence checks passed
    Residual risk 0.35 = Change Risk (0.66) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.66 · 383 lines diff across 4 files
    0.66
    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.356
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.066
    Real PR Conversation Timeline
    1 comments
    "Review active on #4343"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 46.7% reviewed sensitive_module
    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: 66.2% (HIGH) · Higher = larger blast radius
    66.2%
    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: 35.3% · Moderate risk
    35.3%