Accrue card statement interest daily instead of APR/12

JA
2d ago
+2078-106

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_summary uses it for estimated_interest; estimated_monthly_interest_money (APR/12) is removed. monthly_rate and its "Monthly rate" row are unchanged.
  • credit_cards.day_count_convention (string, default actual_365, check constraint), validated against CreditCard::DAY_COUNT_BASES; select on the card form; permitted in CreditCardsController; 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_month is removed from them.
  • After the approval (0a05b499a): a private cycle_grace returns grace, residual, accruing or unknown, and both cycle_interest_money and the summary use it. StatementSummary#residual_interest feeds the helper, which shows "$5.92, residual interest from an earlier statement not paid in full" (new key interest_residual in en and the 8 locales that translate its neighbours). balance_records_behind? runs before the grace check.
  • Loan::AmortizationMath is not touched: its accrual_factor has 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_360 valid, thirty_360 rejected
  • 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)

#MutationCaught by
M1payment counts on its posting dayworked example, actual/360, day late, day in credit
M2arecorded day in credit netsday in credit
M2bcarried day in credit netsday in credit
M3purchases from the next day (start-of-day balance)purchase from its date, day late
M4grace ignores the due dateday late, day in credit
M5no grace at allpaid on due date, no-balance statement
M6actual/360 divides by 365actual/360
M7no cap at the cycle closestops at its close
M8summary keeps APR/125 summary/Overview tests
M9basis not validateddefault/list test, 422 test
M10basis not permittedform save, 422 test
M11basis dropped on importround trip
M12foreign entries not refusedforeign-currency test
M13rounds to 2 places regardless of currencyKWD test
M14zero figure gets the wordinghelper: zero shown bare
M15due day itself treated as passedhelper: due today
M16undated form removedhelper: no due date
M17Overview dated by summary.due_datelate-payment Overview test
M18a de key droppedi18n interest lines
M19lowering balance update counts from the next daybalance update test
M20unknown not handledhelper: unknown
M21the earlier statement not checked (paid alone)carried-statement test
M22the earlier statement read at the last close insteadcarried, summary-flag and Overview tests
M23an unreadable earlier statement counts as carriedunreadable-earlier test
M24residual whenever not in gracesummary-flag test (accruing case), 2 Overview tests
M25residual flag or its helper/view wiring droppedsummary-flag, helper and Overview tests
M26stale check removed, or limited to the cyclethe three stale tests
M27same-day due pushed to next monthdue-on-close-day test

Verification

  • Full unit suite (beastpc) on ba3f335d4: 19462 runs, 117098 assertions, 0 failures, 8 errors, 9 skips — the 8 errors are ReleaseNotesWorkflowTest (no node in 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.yml without 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 likewise interest_not_paid_by and interest_if_not_paid). 71d3ff766 adds the three pt-BR lines with English's placeholders, and drops per_month from 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 8 ReleaseNotesWorkflowTest errors), and accounts_test.rb plus account_activity_test.rb give 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:migrate from upstream main's schema adds the column and chk_credit_cards_day_count_convention; db:rollback removes both (verified in a container). db/schema.rb keeps 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 constraint chk_credit_cards_day_count_convention (actual_365, actual_360). Existing rows take the default; nothing to backfill or run after deploy.
  • Reversible: db:rollback drops 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.

ViewBeforeAfter
Overview — estimated interest, light, desktopbeforeafter
Overview — estimated interest, light, phonebeforeafter
Overview — estimated interest, dark, desktopbeforeafter
Overview — estimated interest, dark, phonebeforeafter
Edit form — interest basis, light, desktopbeforeafter
Edit form — interest basis, light, phonebeforeafter
Edit form — interest basis, dark, desktopbeforeafter
Edit form — interest basis, dark, phonebeforeafter

Blast radius

  • CreditCard#statement_summary → estimated_interest: read only by credit_cards/tabs/_overview.html.erb (grep estimated_interest in app/). The other summary members are unchanged.
  • statement_settled? / next_payment_due_date: unchanged; grace uses its own predicate.
  • Loan::AmortizationMath and 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 the credit_cards block (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.