Close card statements on uploaded statements' dates

JA
2d ago
+711-23

Fixes jaysbeekay/sure#895
Upstream issue: we-promise/sure#5442
Upstream PR: https://github.com/we-promise/sure/pull/5991 (we-promise/sure#5991, draft, position 6 of the native stack https://github.com/we-promise/sure/stack/5995); same branch, same commits.

Stack: base up/redbark-card-limit (jaysbeekay/sure#1022) ← jaysbeekay/sure#1021 ← jaysbeekay/sure#1020 ← jaysbeekay/sure#1006. Next slice: up/card-minimum-percent (jaysbeekay/sure#896).

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 whose period_end_on is 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 at as_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_balance is 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 in cycle_interest_money (from jaysbeekay/sure#1006). It returns the statement's printed closing_balance when 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 new StatementSummary#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_summary render, each statement lookup is made once.
  • due_days_after_close (integer, 1–60, check constraint + validation): payment_due_date_for returns close + N when set, before the due_day check, 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_ATTRIBUTES carries 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 #1022: 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_of counts (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: 27 still 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 the Gatekeeper 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)

#MutationCaught by
M1ignore statements8 tests
M2no lower bound on the windowearlier-cycle, day-itself
M3window starts at the fixed closeprevious close
M4statement ending after as_of countsday-itself
M5failed uploads countother-account/failed/EUR
M6no printed-balance tiebreakties
M7earliest upload winsties
M8currency not checkedEUR
M9ledger ignores the printed balance3 tests
M10grace ignores the printed balancegrace test
M11due day checked before the offset2 offset tests
M12previous = last close as of the day beforeprevious close
M13offset up to 611–60 test
M14not permittedform test
M15dropped from importerround trip
M16a statement alone makes a cycleno-close-day test
M17adjustments measured from the printed balance againfirst statement test, open-cycle update test
M18tolerance 3 or 5±4 / not 5 test
M19current window checked before the next (no early close)too-early and early-close tests
M20foreign statement still moves the close, or "" dropped from the currency filterother-currency and blank-currency tests
M21next close / due / interest cap stepped from the found closeearly and late tests
M22cycle_fixed_close without the toleranceearly, stays-its-close, one-close tests
M23due day counted from the found close, or always from the fixed closeearly and late tests; no-close-day test
M24gap row always labelled the positive way, or the amount signednegative-gap controller test
M25gap row shown when zerono-gap controller test
M26statement lookup not memoisedlookup-once test
M27residual flag's last day from the close's own monthresidual-flag test

Verification

  • Full unit suite (beastpc) on the 106a2a62b fix: 19539 runs, 1 failure, 8 errors, 9 skips. The 8 errors are ReleaseNotesWorkflowTest (no node in the container). The failure is AccountStatementImportConcurrencyTest:88, a lock-timing test this PR does not touch, which passed on rerun and on a clean base. After merging jaysbeekay/sure#1022's update (03c2777f9) and adding the residual-flag fix (a5be623df), the targeted card files ran 557 runs, 0 failures, 0 errors. The full suite on jaysbeekay/sure#1024, which carries this slice, ran 19605 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 (#1022): statement $1,858.38 due 27 Nov, since last statement $60. After: $1,813.38 due 26 Nov, since last statement $105.

ViewBeforeAfter
Overview — light, desktopbeforeafter
Overview — light, phonebeforeafter
Overview — dark, desktopbeforeafter
Overview — dark, phonebeforeafter
Edit form — light, desktopbeforeafter
Edit form — light, phonebeforeafter
Edit form — dark, desktopbeforeafter
Edit form — dark, phonebeforeafter

After the Gatekeeper 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.

ViewOverUnder
Overview — light, desktopoverunder
Overview — light, phoneoverunder
Overview — dark, desktopoverunder
Overview — dark, phoneoverunder

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_for callers, all in credit_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_gap is 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.