Close card statements on uploaded statements' dates
Closes #5442
Fork pre-flight: jaysbeekay/sure#1023 (same commits)
Stacked on up/redbark-card-limit.
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
A card's statement cycle came from two fixed days of the month. Banks like Bankwest close on a different day each month (11th–15th) and set the due date 14 days later. An uploaded statement linked to the card now gives the cycle's real close and its printed closing balance, and a new "Days from close to due date" field sets the due date as an offset from the close. When the printed balance and the account's records disagree at the close, the Overview shows the difference as its own line, so the cards still add up to the header. Without a matching statement nothing changes.
What changed
CreditCard#last_statement_close_date: each cycle is found from its fixed close. A stored (not failed) statement on this account in the card's currency whoseperiod_end_onis within ±4 days of a cycle's fixed close (STATEMENT_CLOSE_TOLERANCE_DAYS = 4) closes that cycle. The next cycle's window is checked first, so a close that comes early is found before its fixed day. Then the current cycle's window, capped atas_of. Otherwise the fixed close, as before. A statement more than 4 days off is ignored, and a cycle has one close, so a second statement in the same window does not close it twice. Ties for one close: a printed balance first, then the later upload.#previous_statement_close_date: the close as it stood the day before the last cycle's window opened, so it follows uploaded statements too.#next_statement_close_date, the due date and the cycle-interest cap step one month from the cycle's fixed close, not from the close that was found (close_after). A close found early in the month before, or late in the month after, therefore stays its own cycle's close. A due day counts from the cycle's fixed close. A due offset counts from the real close.closed_balanceis the one helper read by the statement balance (statement_ledger→last_statement_balance_money,statement_summary,statement_settled?,accrued_since_last_close_money) and by the grace check incycle_interest_money(fromup/card-daily-interest). It returns the statement's printedclosing_balancewhen there is one, and the balance records otherwise. A statement in another currency is ignored for both the date and the balance, and the records stand. A statement with a blank currency counts as the card's own.statement_ledger: the open cycle's balance updates are measured from the records at the close, not from the printed bill. The difference between the printed balance and the records at the close is a newStatementSummary#statement_gap, which is nil when either side is unknown.- Overview, Statement Balance card: when the gap is not zero it shows a line of its own. "Not in your account's records" appears when the printed bill is higher, and "On your records, not on the statement" when the records are higher. The amount is shown without a sign. With that line, statement + since last statement ± gap = the header balance.
- Within one
statement_summaryrender, each statement lookup is made once. due_days_after_close(integer, 1–60, check constraint + validation):payment_due_date_forreturns close + N when set, before thedue_daycheck, so it wins over a due day and works without one. No holiday adjustment.- Form: "Days from close to due date" with a hint, after the cycle days; controller permits it;
Family::DataImporter::CREDIT_CARD_DETAIL_ATTRIBUTEScarries it.
One reading to confirm: the revised plan says "a stored statement alone doesn't make the cycle configured for next/previous close; statement_summary for that card is specified and tested." I kept statement_cycle_configured? requiring a close day, so a card with statements but no close day has no last close either and statement_summary is nil (tested, no raise). Coverage is measured against the fixed close, which such a card does not have.
Tests (red on up/redbark-card-limit: 8 failures, 3 errors unknown attribute 'due_days_after_close')
test/models/credit_card_test.rb, card closing on the 13th:
- statement ending 15 Jul printed 2,242.12, entries summing to 1,300 through 15 Jul and 1,000 through 13 Jul, as_of 20 Jul → close 15 Jul, balance 2,242.12, open-cycle charges 50 (16 Jul only)
- accrued since the last statement from 16 Jul: 50 (292.12 from 13 Jul)
period_end_on == as_ofcounts (15 Jul); on 14 Jul → fixed 13 Jul, not June's 12 Jun statement- 14 Aug with only July uploaded → 13 Aug, not 15 Jul
- close 11 Dec found on 12 Dec, before the fixed 13 Dec
- previous close 12 Jun (uploaded) and next close 13 Aug (forecast)
- another account's statement and a failed upload ignored; a EUR statement moves neither the close nor the balance; a statement with a blank currency counts
- nil printed balance → summed 1,300
- ties: printed beats nil-balance later upload; of two printed, the later upload wins
- no close day + statement → last/previous/next nil, summary nil
- grace reads the printed balance: ledger owes 1,000 (interest > 0), printed 0 → interest 0
- offset 14: 15 Jul → 29 Jul; 31 Jan → 14 Feb; with
due_day: 27still 29 Jul; offset cleared → 27 Jul - known miss named: 11 Dec + 14 → 25 Dec (printed 29 Dec)
- offset 1, 60, nil valid; 0, 61 invalid
Added after review (each failed first as noted):
- the first statement test now also asserts accrued 50, adjustments 0 and gap 942.12 (before: accrued −892.12, adjustments −942.12)
- "a balance update in the open cycle is measured from the records, not the printed bill": records 1,350 → 1,450 on 18 Jul → adjustments 100, accrued 150, gap 942.12 (before: −842.12)
- "without a printed balance there is no gap between the bill and the records"; "without a balance record at the close, the printed bill stands and the open cycle is unknown" (before: adjustments −1,242.12, not nil)
- "an uploaded close counts up to 4 days either side of the fixed day, not 5" (before: 18 Jun found); "a statement too early for its cycle does not close it twice" (8 Aug / 13 Aug); "a cycle has one close, so a second statement in its window is not the previous close"; "a close found early stays that cycle's close once the fixed day passes"
- "a statement in another currency moves neither the close nor the balance" (before: close 15 Jul, expected 13 Jul); "a statement with a blank currency is the card's own" (before: 13 Jul, expected 15 Jul)
- "a close found early in the month before is still its own cycle's close for the next close, due date and interest" (close day 2, statement 30 Jan → next close 2 Mar, due 25 Feb, interest 5.42; before: next close 2 Feb); "a close found late in the month after…" (close day 30, statement 3 Feb → next close 28 Feb, due 20 Feb; before: 30 Mar); "…reads the earlier statement's payment through its own cycle for the residual flag" (before: residual flagged while interest read grace)
- "without a close day, a due day falls in the month of the date it is given"
- "one statement summary looks each statement range up once, and the next one looks again" (before: 4 queries, expected 2)
accounts_controller_test.rb: "credit card overview shows a printed statement's gap from the records as its own line" ($200.00, and no "credit toward the next statement"; before: no gap row), "…labels a gap the records hold and the statement does not" (before: translation missing), "…shows no gap line when the statement matches the records" (a stored statement equal to the records)
credit_cards_controller_test.rb: form renders the field and saves 14. data_importer_test.rb: round trip.
Mutations (each alone)
| # | Mutation | Caught by |
|---|---|---|
| M1 | ignore statements | 8 tests |
| M2 | no lower bound on the window | earlier-cycle, day-itself |
| M3 | window starts at the fixed close | previous close |
| M4 | statement ending after as_of counts | day-itself |
| M5 | failed uploads count | other-account/failed/EUR |
| M6 | no printed-balance tiebreak | ties |
| M7 | earliest upload wins | ties |
| M8 | currency not checked | EUR |
| M9 | ledger ignores the printed balance | 3 tests |
| M10 | grace ignores the printed balance | grace test |
| M11 | due day checked before the offset | 2 offset tests |
| M12 | previous = last close as of the day before | previous close |
| M13 | offset up to 61 | 1–60 test |
| M14 | not permitted | form test |
| M15 | dropped from importer | round trip |
| M16 | a statement alone makes a cycle | no-close-day test |
| M17 | adjustments measured from the printed balance again | first statement test, open-cycle update test |
| M18 | tolerance 3 or 5 | ±4 / not 5 test |
| M19 | current window checked before the next (no early close) | too-early and early-close tests |
| M20 | foreign statement still moves the close, or "" dropped from the currency filter | other-currency and blank-currency tests |
| M21 | next close / due / interest cap stepped from the found close | early and late tests |
| M22 | cycle_fixed_close without the tolerance | early, stays-its-close, one-close tests |
| M23 | due day counted from the found close, or always from the fixed close | early and late tests; no-close-day test |
| M24 | gap row always labelled the positive way, or the amount signed | negative-gap controller test |
| M25 | gap row shown when zero | no-gap controller test |
| M26 | statement lookup not memoised | lookup-once test |
| M27 | residual flag's last day from the close's own month | residual-flag test |
Verification
- Full unit suite on the 106a2a62b fix:
19539 runs, 1 failure, 8 errors, 9 skips. The 8 errors areReleaseNotesWorkflowTest(nonodein the container). The failure isAccountStatementImportConcurrencyTest:88, a lock-timing test this PR does not touch, which passed on rerun and on a clean base. After mergingup/redbark-card-limit's update (03c2777f9) and adding the residual-flag fix (a5be623df), the targeted card files ran557 runs, 0 failures, 0 errors. The full suite onup/card-minimum-percent, which carries this slice, ran19605 runs, 0 failures(the same 8 errors). bin/rubocop: no offenses.erb_lint: no errors.bin/brakeman: 0 warnings.
Screenshots
Seeded card: close day 13, due day 27 (offset 14 after), an uploaded statement closing on the 12th printed $1,813.38, and a $45 charge on the 13th. Before (up/redbark-card-limit): statement $1,858.38 due 27 Nov, since last statement $60. After: $1,813.38 due 26 Nov, since last statement $105.
| View | Before | After |
|---|---|---|
| Overview — light, desktop | ![]() | ![]() |
| Overview — light, phone | ![]() | ![]() |
| Overview — dark, desktop | ![]() | ![]() |
| Overview — dark, phone | ![]() | ![]() |
| Edit form — light, desktop | ![]() | ![]() |
| Edit form — light, phone | ![]() | ![]() |
| Edit form — dark, desktop | ![]() | ![]() |
| Edit form — dark, phone | ![]() | ![]() |
After the review fixes (106a2a62b), the gap line. "Over" has a printed $2,013.38 against records of $1,813.38: "Not in your account's records $200.00", and 2,013.38 + 105 − 200 = 1,918.38, the header. "Under" has a printed $1,613.38: "On your records, not on the statement $200.00", and 1,613.38 + 105 + 200 = 1,918.38.
| View | Over | Under |
|---|---|---|
| Overview — light, desktop | ![]() | ![]() |
| Overview — light, phone | ![]() | ![]() |
| Overview — dark, desktop | ![]() | ![]() |
| Overview — dark, phone | ![]() | ![]() |
Migration notes
20261128120200_add_due_days_after_close_to_credit_cards: one nullable integer with a 1–60 check constraint. Existing rows NULL (fixed due day as before).
Blast radius
last_statement_close_date/payment_due_date_forcallers, all incredit_card.rb:previous_/next_statement_close_date,next_payment_due_date,days_until_due,last_statement_balance_money,accrued_since_last_close_money,statement_summary(balance, due date, status,last_due_date),statement_settled?,minimum_payment_shortfall?(via the balance),cycle_interest_money(grace and the cycle cap). No caller outside the model (grep: only the Plaid processor writes the day fields; the controller and form permit/show them).StatementSummary#statement_gapis new; its only reader is the Overview's Statement Balance card.- Each statement lookup is one indexed query (
index_account_statements_on_account_period), made once per summary render; a card with no statements behaves exactly as before (existing cycle tests unchanged). - Overlap: upstream #5271 and #5687 touch
credit_card.rb; textual only.
Summary by cubic
Closes #5442. A credit card's statement cycle now follows the real close dates of uploaded statements instead of fixed days of the month, and a due date can be set as an offset from the close.
- An uploaded statement ending within 4 days of a cycle's fixed close replaces that close; its printed closing balance becomes the statement balance.
- The printed bill still stands when entries before the close can't be summed in the card's currency, but open-cycle adjustments and the gap stay unknown.
- A new "Days from close to due date" field (1–60) sets the due date and overrides the fixed due day.
- When the printed balance differs from the account's records at the close, the Overview shows the difference on its own line so the cards still add up to the header.
- Interest reads unknown instead of zero when a printed bill has no balance records behind it or the records begin after the cycle's first day, and imports drop a due offset outside 1–60 instead of failing the whole import.
- Without a matching statement, behavior is unchanged.
Migration
- Adds a nullable
due_days_after_closecolumn with a 1–60 check constraint; existing rows keep the fixed due day.
Summary by CodeRabbit
- New Features
- Set a credit card’s payment due date to 1–60 days after its statement closes; this overrides a fixed due day.
- Eligible uploaded statements can provide statement close dates and printed balances for summaries and interest calculations.
- Statement summaries show the difference between the printed statement balance and recorded account balance, indicating whether records are missing from or exceed the statement.
- Credit-card exports and imports retain the configured due-date offset.























