Accrue card statement interest daily instead of APR/12
Fixes jaysbeekay/sure#902
Upstream issue: we-promise/sure#5444
Upstream PR: https://github.com/we-promise/sure/pull/5986 (we-promise/sure#5986, draft, position 1 of the native stack https://github.com/we-promise/sure/stack/5995); same branch, same commits.
Summary
The card Overview's "Estimated interest" was the remaining statement balance times APR/12. It is now the interest the open cycle has accrued so far, from the account's daily balance records, at the card's daily periodic rate. Cards get an interest basis (actual/365 by default, or actual/360) on the existing edit form.
Day rules, as settled in the issue's revised Triage Plan:
- each day charges that day's balance; a charge counts from the day it posts, a payment (any credit) from the day after;
- a day in credit contributes 0, it does not offset other days;
- the days are summed, then rounded once to the currency's precision;
- nothing accrues when the previous statement was paid in full by its due date (a separate predicate,
statement_paid_by_due_date?;statement_settled?is untouched); a payment after the due date does not restore grace; - the cycle after a carried statement still accrues (residual interest), even when the next statement is paid in full. The statement before the last one is checked as well. If it was carried, the cycle accrues, and the Overview labels the figure as residual interest. If it cannot be read, it counts as not carried;
- a due day on the close day keeps upstream's rule (we-promise/sure#4270): the statement falls due the day it closes, so a payment after that day leaves the cycle accruing;
- the interest is unknown, not a figure, when any entry up to the cycle's last day is dated after the account's newest balance record (the records are behind);
- the current APR applies to the whole cycle (APR history is out of scope);
- refunds and other credits count toward "paid in full", as in
statement_settled?.
A balance update (valuation) that lowers the balance counts from its own day, not the day after: it states what is owed on that date and has no inflow on its balance record. A test pins it (1,000 then 500 from 21 Sep: 12.33 at 18%; the next-day rule would give 12.58).
What changed
CreditCard#cycle_interest_money(last_close:, as_of:): the accrual, capped at the cycle's close.statement_summaryuses it forestimated_interest;estimated_monthly_interest_money(APR/12) is removed.monthly_rateand its "Monthly rate" row are unchanged.credit_cards.day_count_convention(string, defaultactual_365, check constraint), validated againstCreditCard::DAY_COUNT_BASES; select on the card form; permitted inCreditCardsController; kept through export/import (Family::DataImporter::CREDIT_CARD_DETAIL_ATTRIBUTES).- Overview wording,
CreditCardsHelper#credit_card_interest_if_unpaid: the line says what missing the due date costs: "$2.37 if not paid in full by Dec 14, 2026" (the closed statement's due date), "$2.37, not paid in full by Dec 14, 2026" once that date has passed, and "$2.37 if the statement is not paid in full" for a card without a due day; a zero figure, where grace applies, is shown bare. The three keys are translated in en, de, es, fr, id, nb, nl and zh-CN (the locales that translate the label);per_monthis removed from them. - After the approval (0a05b499a): a private
cycle_gracereturns grace, residual, accruing or unknown, and bothcycle_interest_moneyand the summary use it.StatementSummary#residual_interestfeeds the helper, which shows "$5.92, residual interest from an earlier statement not paid in full" (new keyinterest_residualin en and the 8 locales that translate its neighbours).balance_records_behind?runs before the grace check. Loan::AmortizationMathis not touched: itsaccrual_factorhas no actual/360 branch (falls through to actual/actual), and open upstream PR we-promise/sure#5212 edits that file. The daily rate is APR / 365 or 360, which is all the card needs.
Tests (red on upstream main, green on this branch)
test/models/credit_card_test.rb
- worked example: 1,000 for 10 days, 500 paid on day 10, 30-day cycle at 18% → 9.86 (was no method)
- 30/31/28-day cycles, 1,000 unpaid at 18% → 14.79 / 15.29 / 13.81
- cycle stops at its close (as_of three weeks later still 14.79)
- purchase on day 15 accrues from day 15 → 15.29 (14.79 if from day 16)
- day in credit counts 0, with and without its own balance record → 13.81 (13.32 if netted)
- previous statement paid in full on its due date → 0; one day late → 15.39
- statement with no balance → new purchases accrue 0
- no due day: a payment after the cycle closed does not restore its grace → 14.79
- actual/360 → 10.00
- no APR → nil; entry in another currency → nil
- default
actual_365,actual_360valid,thirty_360rejected - rewritten: summary 1,200 at 12% over 11–16 Oct: 12 → 2.37; KWD 0.40: 0.004 → 0.001 (3-place rounding)
- summary in a foreign-currency cycle also has no interest
test/controllers/credit_cards_controller_test.rb: form renders the basis select (with aria-describedby hint) and saves actual_360; thirty_360 is a 422 and the stored basis is unchanged.
test/controllers/accounts_controller_test.rb: Overview shows "$2.37 if not paid in full by Oct 26, 2026" (was "$12.00/month"); after a late payment it shows "$7.10, not paid in full by Oct 26, 2026", the closed statement's due date, though the tab's own due date has moved to 26 Nov.
test/helpers/credit_cards_helper_test.rb (new): each wording, a zero figure shown bare, unknown.
test/i18n_test.rb#test_card_interest_lines_exist_beside_the_interest_label (new): every locale that translates estimated_interest_label carries the three forms with English's placeholders; failed before with en: credit_cards.tabs.overview.interest_if_not_paid_by is missing.
test/models/credit_card_test.rb: a balance update that lowers the balance counts from its own day → 12.33.
test/models/family/data_importer_test.rb: the basis round-trips through full export/import.
Added after the approval (0a05b499a), each red first:
- "a cycle after a carried statement still accrues when the next statement is paid in full" → 5.92 (was 0); control "a cycle accrues nothing when the last two statements were paid in full" → 0
- "a cycle accrues nothing when the earlier statement cannot be read" → 0 (was an error)
- "the statement summary flags interest that accrues only for an earlier carried statement" and "the statement summary does not flag a cycle in grace" (covers the accruing case)
- "a due day on the close day leaves the cycle accruing, as the statement falls due the day it closes" → due 28 Sep, 0.74, not flagged residual
- "cycle interest is unknown when an entry in the cycle is newer than the balance records", "…when an entry before the cycle is newer…", "…rather than in grace when a stale record shows the statement cleared" → nil
- helper "residual interest says it comes from an earlier statement, not from this one's payment"; controller "credit card overview labels interest left by an earlier carried statement"; the I18n interest-lines test includes
interest_residual
Mutations (each applied alone, targeted files re-run; every one caught)
| # | Mutation | Caught by |
|---|---|---|
| M1 | payment counts on its posting day | worked example, actual/360, day late, day in credit |
| M2a | recorded day in credit nets | day in credit |
| M2b | carried day in credit nets | day in credit |
| M3 | purchases from the next day (start-of-day balance) | purchase from its date, day late |
| M4 | grace ignores the due date | day late, day in credit |
| M5 | no grace at all | paid on due date, no-balance statement |
| M6 | actual/360 divides by 365 | actual/360 |
| M7 | no cap at the cycle close | stops at its close |
| M8 | summary keeps APR/12 | 5 summary/Overview tests |
| M9 | basis not validated | default/list test, 422 test |
| M10 | basis not permitted | form save, 422 test |
| M11 | basis dropped on import | round trip |
| M12 | foreign entries not refused | foreign-currency test |
| M13 | rounds to 2 places regardless of currency | KWD test |
| M14 | zero figure gets the wording | helper: zero shown bare |
| M15 | due day itself treated as passed | helper: due today |
| M16 | undated form removed | helper: no due date |
| M17 | Overview dated by summary.due_date | late-payment Overview test |
| M18 | a de key dropped | i18n interest lines |
| M19 | lowering balance update counts from the next day | balance update test |
| M20 | unknown not handled | helper: unknown |
| M21 | the earlier statement not checked (paid alone) | carried-statement test |
| M22 | the earlier statement read at the last close instead | carried, summary-flag and Overview tests |
| M23 | an unreadable earlier statement counts as carried | unreadable-earlier test |
| M24 | residual whenever not in grace | summary-flag test (accruing case), 2 Overview tests |
| M25 | residual flag or its helper/view wiring dropped | summary-flag, helper and Overview tests |
| M26 | stale check removed, or limited to the cycle | the three stale tests |
| M27 | same-day due pushed to next month | due-on-close-day test |
Verification
- Full unit suite (beastpc) on ba3f335d4:
19462 runs, 117098 assertions, 0 failures, 8 errors, 9 skips— the 8 errors areReleaseNotesWorkflowTest(nonodein the container), environment noise. - On 0a05b499a (after the approval):
19498 runs, 117206 assertions, 0 failures, 8 errors, 9 skips(the same 8). Targeted card, helper, controller and I18n tests: 309 runs, 0 failures. - Merged with upstream main 12cd4e1a5 (457462302). Upstream had since added
config/locales/views/credit_cards/pt-BR.ymlwithout the three interest lines, so the new I18n test failed on the merge (pt-BR: credit_cards.tabs.overview.interest_if_not_paid_by is missing, and likewiseinterest_not_paid_byandinterest_if_not_paid). 71d3ff766 adds the three pt-BR lines with English's placeholders, and dropsper_monthfrom pt-BR as from every other locale (nothing reads it). Full unit suite on 71d3ff766:19487 runs, 117189 assertions, 0 failures, 8 errors, 9 skips(the same 8ReleaseNotesWorkflowTesterrors), andaccounts_test.rbplusaccount_activity_test.rbgive 26 runs, 0 failures. Upstream changed nothing on the card pages between e0d9c9c16 and 12cd4e1a5, so the screenshots below still show this branch's behaviour. bin/rubocop(10 changed Ruby files): no offenses.erb_lint(2 ERB files): no errors.bin/brakeman: 0 warnings. No JS changed.- Migration:
db:migratefrom upstream main's schema adds the column andchk_credit_cards_day_count_convention;db:rollbackremoves both (verified in a container).db/schema.rbkeeps the file's alphabetical column order and constraint formatting.
Migration notes
- Adds
credit_cards.day_count_convention(string,NOT NULL, default'actual_365') and check constraintchk_credit_cards_day_count_convention(actual_365,actual_360). Existing rows take the default; nothing to backfill or run after deploy. - Reversible:
db:rollbackdrops the constraint and the column.
Screenshots
Before = upstream main e0d9c9c16, after = this branch; same data (18% APR, $700 carried, partly paid, due today). The Overview's "$10.50/month" becomes "$7.61 if not paid in full by Nov 26, 2026"; the form gains Interest basis. Overview "after" images retaken on ba3f335d4.
| View | Before | After |
|---|---|---|
| Overview — estimated interest, light, desktop | ![]() | ![]() |
| Overview — estimated interest, light, phone | ![]() | ![]() |
| Overview — estimated interest, dark, desktop | ![]() | ![]() |
| Overview — estimated interest, dark, phone | ![]() | ![]() |
| Edit form — interest basis, light, desktop | ![]() | ![]() |
| Edit form — interest basis, light, phone | ![]() | ![]() |
| Edit form — interest basis, dark, desktop | ![]() | ![]() |
| Edit form — interest basis, dark, phone | ![]() | ![]() |
Blast radius
CreditCard#statement_summary→estimated_interest: read only bycredit_cards/tabs/_overview.html.erb(grepestimated_interestin app/). The other summary members are unchanged.statement_settled?/next_payment_due_date: unchanged; grace uses its own predicate.Loan::AmortizationMathand every loan schedule: untouched.Family::DataImporter::CREDIT_CARD_DETAIL_ATTRIBUTES: one attribute added; older exports without it keep the column default (existing "without their details" test still green).- Performance: the Overview gains a few COUNT/SUM queries and two balance reads per render.
Overlap with we-promise/sure#5271 (annual fees, open)
git merge-tree of this branch with #5271's head (49db1a05) auto-merges credit_card.rb, the controller, _form.html.erb, _overview.html.erb, en.yml and the tests. Two textual conflicts, both rebase-only:
db/schema.rb: the version line and thecredit_cardsblock (both add columns and a check constraint) — regenerate.Family::DataImporter::CREDIT_CARD_DETAIL_ATTRIBUTES: both append to the same one-line list — keep both attributes.
Neither change needs the other's code.
Owner decisions (2026-11-26)
- A. Grace as planned: previous statement paid in full by its due date → 0. Revised after the approval (2026-11-28): residual interest is charged on the cycle after a carried statement, with its own label; stale balance records give "unknown"; the due day on the close day keeps we-promise/sure#4270's same-day rule.
- B. The figure says what late payment would cost (wording above), replacing "so far this cycle". The example covered the dated case; the past-due wording ("$2.37, not paid in full by ...") and showing a zero figure bare are my additions for the cases it did not cover, and the undated wording is the no-due-date case B asked for.
- C. Refunds count toward "paid in full", as
statement_settled?does. - D. Daily periodic rate in place of the APR/12 "Monthly rate" row: follow-up jaysbeekay/sure#1009.















