Loans: let interest accrue on the basis the lender actually uses
Closes #3638.
The problem
Every loan period is charged one twelfth of the annual interest, whatever the period actually holds — Loan::Simulator#monthly_rate divides the annual rate by 12 and that figure is used for every month. January and February are charged the same interest on the same balance.
Many lenders do not accrue that way. actual/365 is the standard basis for UK and Australian mortgages and is normally printed on the statement, so a borrower on that basis cannot reconcile a single row of our schedule against their lender's. On 300,000 at 6%: January is 28.77 light, February 119.18 heavy.
Over a plain year the differences cancel exactly, which is why this has never shown up as an annual discrepancy. The problem is narrower — a convention the loan is genuinely written under cannot be expressed at all.
What this adds
A day_count_convention on the loan, with three options:
thirty_360— a flat twelfth. 30/360 reduces to 1/12 exactly, so this is what Sure does today, to the digit.actual_365— days in the period over 365.actual_actual— days over that calendar year's own length, split at 1 January when a period straddles it.
The default is the load-bearing decision, not the feature
Loan::Simulator is the single accrual point. Loan::AmortizationSchedule runs through it, and so does Loan::PayoffProjection, which passes the loan's convention to it. So this reaches every loan figure in the product. A default that moved would silently restate every existing schedule.
thirty_360 is the default. accrual_factor is a hard branch back to the original monthly_rate for that convention, so the default path is bit-identical rather than merely close.
The evidence is test/models/loan/default_convention_baseline_test.rb. It asserts nine scenarios against figures captured from main at 20352e1a0, whose engine has no day-count convention, not read back out of the patched engine. A digest covers every field of every row, so a default that moved by a cent on any row fails. The scenarios:
- a fixed loan over 360 periods;
- a rate change inside a period (re-amortising);
- a rate change on a payment date;
- a held payment through a rate change;
- a stored loan's schedule;
- projections on contract, ahead of it and behind it;
- total cost with decreasing-life insurance.
All nine match on this branch. Removing the thirty_360 shortcut fails all seven tests. The full report is in the thread (comment).
It is worth saying why the default is not actual_actual, which sounds like the conservative choice. It is not: it charges 31/365 in January and 28/365 in February, reaching a flat twelfth only when a whole calendar year is summed. Measured across 30-year lifetimes from sixteen start dates it lands between −£212 and +£241 against today's arithmetic, because a loan's periods do not align to calendar years and its final period is partial. Only 30/360 is identical row for row.
One design point worth agreeing
The day count applies to what a period charges, not to what sizes the payment.
AmortizationMath.level_payment is an annuity formula over one constant periodic rate. If the day count reached the sizing, the rate would differ every month and under :reamortize the payment would be rebuilt every period and stop being level. Sizing therefore stays on the nominal monthly rate — which is also what the borrower's contracted payment is actually set from.
That split forced one further change: the straddle correction previously asked "did the rate move mid-period?" by comparing the two derived rates, which answers yes every month once a day count is in play. It now compares the rates themselves, which is what it always meant and is day-count independent.
Tests
Thirty-five in total:
- 22 in
day_count_convention_test.rb; - 7 in
default_convention_baseline_test.rb(above); - 2 in
loans_controller_test.rbfor the form; - 3 in
loans_helper_test.rbfor its labels and locale fallback; - 1 in
payoff_projection_test.rb.
The load-bearing one is negative: a default loan's schedule is identical row for row, asserted against hand-computed arithmetic rather than against the engine, so it cannot pass by comparing the engine with itself. Also covered: each convention against hand-computed figures for 28-, 29-, 30- and 31-day months, the year-boundary split (17/365 + 14/366, asserted to differ from the naive 31/365 first so it cannot pass vacuously), that a varying day count does not resize the payment, that an unsupported convention is refused, and that the choice round-trips through both create and update. Two more cover:
- a rate change inside a period on actual/365. The payment is re-sized once, on that period's day-counted charge, and never again. Pointing the straddle guard back at the derived rates fails it, and so does dropping the first-period interest.
- insurance. Reassigning the convention restates a decreasing-life premium without a reload. Removing
day_count_conventionfromSCHEDULE_INPUTSfails it.
Insurance (#3327)
Loan::Insurance keeps its flat monthly rate. A decreasing-life premium is charged on each period's opening balance, so it follows the schedule's day count. A level-term premium does not.
Sequencing
#3474 merged on 2026-09-27 and is in this branch. The engine has not changed on main since. The default-convention check promised here is now the baseline test above, reported in the thread.
Provenance
Built and reviewed on a fork first, at jaysbeekay/sure#191, against a mirror of this repository's main. Same branch, same commits — this is that branch, not a cherry-pick.
Brought level with main at 20352e1a0 (merge 2c9c80321). That resolved three conflicts with #3327, each two additions at the same spot:
loans_controller.rb: both sets of permitted params.loans/_form.html.erb: the day-count select keeps its place after the term, and the insurance fields follow in their own row.db/schema.rb: both sides' columns and constraints.
git diff main -- db/schema.rb is this PR's column and check constraint and nothing else. The version line stays main's 2026_09_29_120000, because this migration (20260918210000) is older. Running it on a database built from main's schema produces that column and constraint exactly.
CI on a94a53904: green here and in the fork. On this PR, run 38252454700 passes unit tests, system tests, lint, JS lint and both scans, and security-scan passes too. In the fork (jaysbeekay/sure#191), run 38367408835 passes the same six jobs: 11,821 unit runs and 238 system runs, 0 failures and 0 errors.
Screenshots
The loan edit form, before (main) and after (this branch). The only change is the Interest basis select and its hint, after the term. The insurance fields from #3327 follow unchanged.
Before (main) | After (this branch) |
|---|---|
![]() | ![]() |
Happy to split the column and the engine change into separate PRs if you would rather review them apart.
Rebased onto main (6 Oct)
Rebased onto main at ea38fa6a3; the commits carry the same changes under new SHAs. SHAs elsewhere in this description refer to the commits as they were before.
This is the second rebase today. The first, onto eb34c06f0, resolved conflicts with main that this one did not meet again: db/schema.rb (both sides' loans columns and check constraints kept), app/views/loans/_form.html.erb (the day-count select straight after the term, then the insurance-rate row from main, as the earlier merge placed them; the rebase first put them the other way round, corrected in 21ecce2be), app/controllers/loans_controller.rb (both sides' permitted attributes), and app/models/loan/simulator.rb: main's :scheduled strategy and payment_amount kept beside this PR's day-count argument and validation, and main's restructured sizing now compares the sizing rate with monthly_rate(accrual_annual), as this PR does, since accrual_rate is a per-period factor under a day count.
dcbb325dd→79ee5598eLet a loan accrue on the basis its lender actually usesd4f08a71f→9fe269f42Address the review: a select that saved nothing, and two real gapsbe060f114→8163410ceAssert the convention round-trips through create as well as update4689bdecd→ab8d2962cProject a loan on the day-count basis its schedule charges596789147→01af8b19dCover the leap-year-opening boundary and the settling row on actual/365f9d02031b→df7c60880Pin that an untranslated locale falls back to English labels315dcc8ab→7114d8c7dCorrect two comments the merge left stale79d634e7d→932452649Prove the default convention against the engine before this changef1e7db861→7be721a09Refuse a period that ends before it starts, and correct the form hint5143c8b4d→d46b829a9Cover the three day-count paths the sweeps found untested21ecce2be→1f91361e0Put the day-count select back directly after the term
bin/rails test on c420066e0 (the same tree as 1f91361e0, recommitted only to re-run CI after an unrelated flaky PropertiesEditTest): 10,612 runs, 45,178 assertions, 0 failures, 0 errors, 33 skips. Loan and account system tests: 16 runs, 0 failures. bin/rubocop on the changed files: no offenses.
Summary by CodeRabbit
- New Features
- Choose an interest day-count method for each loan: 30/360, Actual/365, or Actual/Actual.
- Loan schedules and payoff projections calculate interest using the selected method. Existing loans default to 30/360.
- The loan form includes descriptions of the available methods.

