← Back to Pull requests

Accrue card statement interest daily instead of APR/12 #5986

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 30, 2026 18:41 UTC
Author

Part of #5444
Fork pre-flight: jaysbeekay/sure#1006 (same commits)

Bottom of the credit-card stack, based on main.

Verified on the stack's top commit 6f85ab855 (up/card-next-statement, with current main merged in at the bottom and each slice merged up the chain): full unit suite 19760 runs, 0 failures, 8 errors, 9 skips (the 8 are ReleaseNotesWorkflowTest, no node in the test container); the card system test credit_card_limit_chart_test 8 runs, 0 failures.

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)

    | # | 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 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.

    | View | Before | After |
    |---|---|---|
    | Overview — estimated interest, light, desktop | !before | !after |
    | Overview — estimated interest, light, phone | !before | !after |
    | Overview — estimated interest, dark, desktop | !before | !after |
    | Overview — estimated interest, dark, phone | !before | !after |
    | Edit form — interest basis, light, desktop | !before | !after |
    | Edit form — interest basis, light, phone | !before | !after |
    | Edit form — interest basis, dark, desktop | !before | !after |
    | Edit form — interest basis, dark, phone | !before | !after |

    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'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.

    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'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 the daily-rate slice (up/card-daily-rate).
  • <!-- This is an auto-generated description by cubic. -->
    ---

    Summary by cubic


    Accrues credit card statement interest daily from balance records instead of applying APR/12 to the remaining balance, so the Overview's "Estimated interest" now reflects the actual cycle and payment timing. Cards gain a day_count_convention field (actual/365 or actual/360) set on the edit form and preserved through export/import. The Overview label explains what the figure would cost if the statement is not paid in full by its due date, and grace (zero interest) applies only when the previous statement and the one before it were each paid in full by their due dates; residual interest after a carried statement is labeled as such. The wording is inclusive of the due date itself — a payment on that day counts — consistently across all translated locales. Interest is unknown when balance records are behind the entries.

    Migration

  • Adds credit_cards.day_count_convention (string, default actual_365) with a check constraint; existing rows take the default, reversible via db:rollback.
  • <sup>Written for commit 971b70c99938e42aa40711a43e3c0c72484e6658. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/we-promise/sure/pull/5986?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> <a href="https://www.cubic.dev/action/auto-fix/pr/we-promise/sure/5986?returnTo=https%3A%2F%2Fgithub.com%2Fwe-promise%2Fsure%2Fpull%2F5986&source=description" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-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
    Choose Actual/365 or Actual/360 as the basis for calculating credit-card interest. The selection is saved with the card and retained when card details are imported.
    Improvements
    Credit-card interest estimates now reflect daily balances and statement-cycle activity rather than a monthly estimate.
    The overview shows estimated interest with context about payment due dates, overdue statements, and residual interest from earlier statements.
    Interest estimates are shown as unknown when the available information is insufficient to calculate them.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jaysbeekay Review duration: 6963m
    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 #5986"
    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%