Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Improve credit card overview with billing and interest insights (#4600)

Shibu M6db36338b5f8
diff --git a/app/models/credit_card.rb b/app/models/credit_card.rb
index 423eb72ec2..c671b59268 100644
--- a/app/models/credit_card.rb
+++ b/app/models/credit_card.rb
@@ -7,6 +7,13 @@ class CreditCard < ApplicationRecord
"credit_card" => { short: "Credit Card", long: "Credit Card" }
}.freeze
+ StatementSummary = Data.define(
+ :balance, :remaining, :charges, :credits, :accrued,
+ :due_date, :last_due_date, :next_close, :status, :scheduled_payment,
+ :estimated_interest, :monthly_rate
+ )
+ ScheduledPayment = Data.define(:payment, :due_date)
+
validates :statement_close_day, :due_day,
numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 31 },
allow_nil: true
@@ -119,6 +126,39 @@ class CreditCard < ApplicationRecord
sum_entries_in((last_close + 1)..as_of)
end
+ # One read of the statement cycle for the Overview tab. The view needs the
+ # closed statement, what has happened since, and whether the bill is settled;
+ # building them together keeps those figures on one `as_of` and avoids
+ # re-querying the same entries for each line.
+ def statement_summary(as_of: Date.current, viewer:)
+ return nil unless statement_cycle_configured?
+
+ last_close = last_statement_close_date(as_of: as_of)
+ balance = last_statement_balance_money(as_of: as_of)
+ window = (last_close + 1)..as_of
+ totals = cycle_totals(window)
+ charges = totals&.dig(:charges)
+ credits = totals&.dig(:credits)
+ accrued = totals&.dig(:accrued)
+ remaining = statement_remaining(balance, credits)
+ last_due_date = payment_due_date_for(last_close)
+
+ StatementSummary.new(
+ balance: balance,
+ remaining: remaining,
+ charges: charges,
+ credits: credits,
+ accrued: accrued,
+ due_date: statement_due_date(last_due_date, remaining, as_of),
+ last_due_date: last_due_date,
+ next_close: next_statement_close_date(as_of: as_of),
+ status: statement_status(balance: balance, remaining: remaining, due_date: last_due_date, as_of: as_of),
+ scheduled_payment: next_scheduled_payment(as_of: as_of, viewer: viewer),
+ estimated_interest: estimated_monthly_interest_money(remaining),
+ monthly_rate: monthly_rate
+ )
+ end
+
def minimum_payment_shortfall?(as_of: Date.current)
minimum = minimum_payment_money
balance = last_statement_balance_money(as_of: as_of)
@@ -128,7 +168,41 @@ class CreditCard < ApplicationRecord
end
private
- def sum_entries_in(window)
+ def cycle_totals(window)
+ summable = summable_entries(window)
+ return nil if summable.nil?
+
+ charges = BigDecimal(0)
+ credits = BigDecimal(0)
+ summable.pluck(:amount).each do |amount|
+ if amount.positive?
+ charges += amount
+ elsif amount.negative?
+ credits += amount
+ end
+ end
+
+ {
+ charges: Money.new(charges, account.currency),
+ credits: Money.new(credits, account.currency),
+ accrued: Money.new(charges + credits, account.currency)
+ }
+ end
+
+ def sum_entries_in(window, amount_sign: nil)
+ summable = summable_entries(window)
+ return nil if summable.nil?
+
+ amount_scope = case amount_sign
+ when :positive then summable.where("amount > 0")
+ when :negative then summable.where("amount < 0")
+ else summable
+ end
+
+ Money.new(amount_scope.sum(:amount), account.currency)
+ end
+
+ def summable_entries(window)
scoped = account.entries.excluding_pending.excluding_split_parents.where(date: window)
# An entry we cannot add up -- booked in another currency, or in none at
@@ -141,7 +215,72 @@ class CreditCard < ApplicationRecord
summable = scoped.where(currency: account.currency)
return nil if summable.count != scoped.count
- Money.new(summable.sum(:amount), account.currency)
+ summable
+ end
+
+ def statement_remaining(balance, credits)
+ return nil if balance.nil? || credits.nil?
+ return Money.new(0, balance.currency) unless balance.positive?
+
+ Money.new([ balance.amount + credits.amount, 0 ].max, balance.currency)
+ end
+
+ # Once the closed bill is settled, its due date is no longer the next one
+ # the cardholder has to meet. Keep an unpaid bill's date in place even when
+ # it has passed, so the Overview still says it is overdue.
+ def statement_due_date(last_due_date, remaining, as_of)
+ return last_due_date if last_due_date.nil? || remaining.nil?
+ return last_due_date unless remaining.zero? && last_due_date < as_of
+
+ payment_due_date_for(next_statement_close_date(as_of: as_of))
+ end
+
+ def statement_status(balance:, remaining:, due_date:, as_of:)
+ return nil if balance.nil? || remaining.nil?
+ return :no_balance unless balance.positive?
+ return :paid if remaining.zero?
+ return :overdue if due_date.present? && due_date < as_of
+ return :partial if remaining.amount < balance.amount
+
+ :unpaid
+ end
+
+ # Use the earliest open occurrence, even when its date has passed: an
+ # unpaid scheduled payment is exactly the one the cardholder needs to see.
+ # The cached next_expected_date can stay stale after a manual payment, so
+ # fall back to it only when the series has no occurrence yet.
+ def next_scheduled_payment(as_of:, viewer:)
+ RecurringTransaction
+ .accessible_by(viewer)
+ .where(destination_account_id: account.id)
+ .active
+ .includes(:recurring_occurrences)
+ .filter_map do |payment|
+ due_date = next_open_due_date(payment, as_of)
+ ScheduledPayment.new(payment: payment, due_date: due_date) if due_date
+ end
+ .min_by(&:due_date)
+ end
+
+ def next_open_due_date(payment, as_of)
+ occurrence = payment.recurring_occurrences.select(&:scheduled?).min_by(&:due_on)
+ return occurrence.effective_due_on if occurrence
+
+ payment.next_expected_date if payment.next_expected_date >= as_of
+ end
+
+ def monthly_rate
+ apr / 12 if apr.present?
+ end
+
+ def estimated_monthly_interest_money(balance)
+ return nil if apr.blank? || balance.nil?
+ return Money.new(0, account.currency) unless balance.positive?
+
+ Money.new(
+ (balance.amount * apr / 100 / 12).round(balance.currency.default_precision || 2),
+ account.currency
+ )
end
def statement_settled?(as_of: Date.current)
@@ -150,12 +289,10 @@ class CreditCard < ApplicationRecord
return true unless balance.positive?
last_close = last_statement_close_date(as_of: as_of)
- payments = account.entries.excluding_pending.excluding_split_parents
- .where(date: (last_close + 1)..as_of, currency: account.currency)
- .where("amount < 0")
- .sum(:amount).abs
+ credits = sum_entries_in((last_close + 1)..as_of, amount_sign: :negative)
+ return false if credits.nil?
- payments >= balance.amount
+ credits.amount.abs >= balance.amount
end
def day_of_month_in(base_date, day)
diff --git a/app/views/credit_cards/tabs/_overview.html.erb b/app/views/credit_cards/tabs/_overview.html.erb
index 6db97959ce..d7a4f6e120 100644
--- a/app/views/credit_cards/tabs/_overview.html.erb
+++ b/app/views/credit_cards/tabs/_overview.html.erb
@@ -6,61 +6,110 @@
<% today = as_of %>
<% card = account.credit_card %>
-<% if card.statement_cycle_configured? %>
- <% last_balance = card.last_statement_balance_money(as_of: today) %>
- <% accrued = card.accrued_since_last_close_money(as_of: today) %>
- <% due_date = card.next_payment_due_date(as_of: today) %>
- <% last_due_date = card.payment_due_date_for(card.last_statement_close_date(as_of: today)) %>
- <% due_for_last_balance = due_date.present? && due_date == last_due_date %>
- <% next_close = card.next_statement_close_date(as_of: today) %>
+<% if (summary = card.statement_summary(as_of: today, viewer: Current.user)) %>
+ <% due_for_last_balance = summary.due_date.present? && summary.due_date == summary.last_due_date %>
+ <% show_apr = card.apr.present? %>
+ <% summary_grid_class = show_apr ? "grid grid-cols-1 gap-2 sm:grid-cols-2 @3xl:grid-cols-3" : "grid grid-cols-1 gap-2 sm:grid-cols-2" %>
- <div class="grid grid-cols-3 gap-2">
- <%# The bill. This and the next card are a SPLIT of the balance in the page
- header, not two more balances: the statement that closed is what you owe
- now, and everything charged since belongs to the statement still
- building. The two add back up to the debt, which is the one number the
- header already gives; what the header cannot say is how much of it is
- already a bill, and that is what this tab is for.
-
- The due date sits under the amount rather than in a card of its own,
- because a bare date beside a balance reads as a second, separate bill. %>
- <%= summary_card title: t(".statement_balance") do %>
- <%= last_balance ? format_money(last_balance) : t(".unknown") %>
- <% if due_date && due_for_last_balance %>
- <span class="block text-sm text-secondary">
- <%= t(".due_on", date: l(due_date, format: :long)) %>
+ <div class="@container">
+ <div class="<%= summary_grid_class %>" data-testid="credit-card-summary-grid">
+ <%# The bill. This and the next card are a SPLIT of the balance in the page
+ header, not two more balances: the statement that closed is what you owe
+ now, and everything charged since belongs to the statement still
+ building. The two add back up to the debt, which is the one number the
+ header already gives; what the header cannot say is how much of it is
+ already a bill, and that is what this tab is for. %>
+ <%= summary_card title: t(".statement_balance") do %>
+ <%= summary.balance ? format_money(summary.balance) : t(".unknown") %>
+ <span class="mt-2 block space-y-1 text-sm text-secondary">
+ <% if summary.due_date && due_for_last_balance %>
+ <span class="block">
+ <%= t(".due_on", date: l(summary.due_date, format: :long)) %>
+ </span>
+ <span class="block <%= summary.status == :overdue ? "text-destructive" : "" %>">
+ <%= credit_card_due_countdown((summary.due_date - today).to_i) %>
+ </span>
+ <% end %>
+ <% if summary.remaining&.positive? && summary.balance && summary.remaining.amount < summary.balance.amount %>
+ <span class="block privacy-sensitive">
+ <%= t(".remaining_due", amount: format_money(summary.remaining)) %>
+ </span>
+ <% end %>
+ <% if summary.status == :paid %>
+ <span class="block text-success"><%= t(".status_paid") %></span>
+ <% elsif summary.status == :partial %>
+ <span class="block"><%= t(".status_partial") %></span>
+ <% end %>
+ <% if summary.scheduled_payment %>
+ <span class="block privacy-sensitive">
+ <%= t(".scheduled_payment",
+ amount: format_money(summary.scheduled_payment.payment.amount_money),
+ date: l(summary.scheduled_payment.due_date, format: :long)) %>
+ </span>
+ <% end %>
</span>
- <span class="block text-sm text-secondary"><%= credit_card_due_countdown(card.days_until_due(as_of: today)) %></span>
<% end %>
- <% end %>
- <%# A negative figure here is not a nonsense balance: paying the card down
- by more than has been charged since the close leaves a credit that
- reduces the next statement. Labelling it as such keeps the two cards
- summing to the debt in the header instead of appearing to contradict it.
+ <%# A negative figure here is not a nonsense balance: paying the card down
+ by more than has been charged since the close leaves a credit that
+ reduces the next statement. Labelling it as such keeps the two cards
+ summing to the debt in the header instead of appearing to contradict it.
- The credit is checked before the due date rather than after it. A due
- date that has already rolled past the last statement belongs to the
- statement still building, and when the cardholder holds a credit that
- statement may not produce a bill at all -- quoting a due date there
- would promise a payment the credit may well cancel. %>
- <%= summary_card title: t(".since_last_statement") do %>
- <%= accrued ? format_money(accrued) : t(".unknown") %>
- <% if accrued&.negative? %>
- <span class="block text-sm text-secondary"><%= t(".credit_toward_next") %></span>
- <% elsif due_date && !due_for_last_balance %>
- <span class="block text-sm text-secondary">
- <%= t(".due_on", date: l(due_date, format: :long)) %>
+ The credit is checked before the due date rather than after it. A due
+ date that has already rolled past the last statement belongs to the
+ statement still building, and when the cardholder holds a credit that
+ statement may not produce a bill at all -- quoting a due date there
+ would promise a payment the credit may well cancel. %>
+ <%= summary_card title: t(".since_last_statement") do %>
+ <%= summary.accrued ? format_money(summary.accrued) : t(".unknown") %>
+ <span class="mt-2 block space-y-1 text-sm text-secondary">
+ <% if summary.accrued&.negative? %>
+ <span class="block"><%= t(".credit_toward_next") %></span>
+ <% elsif summary.due_date && !due_for_last_balance %>
+ <span class="block">
+ <%= t(".due_on", date: l(summary.due_date, format: :long)) %>
+ </span>
+ <span class="block"><%= credit_card_due_countdown((summary.due_date - today).to_i) %></span>
+ <% elsif summary.next_close %>
+ <span class="block"><%= t(".closes_on", date: l(summary.next_close, format: :long)) %></span>
+ <% end %>
+ <% if summary.charges && summary.credits %>
+ <span class="flex items-center justify-between gap-4">
+ <span><%= t(".charges") %></span>
+ <span class="font-medium text-primary privacy-sensitive"><%= format_money(summary.charges) %></span>
+ </span>
+ <span class="flex items-center justify-between gap-4">
+ <span><%= t(".payments_credits") %></span>
+ <span class="font-medium text-primary privacy-sensitive"><%= format_money(summary.credits.abs) %></span>
+ </span>
+ <% end %>
</span>
- <span class="block text-sm text-secondary"><%= credit_card_due_countdown(card.days_until_due(as_of: today)) %></span>
- <% elsif next_close %>
- <span class="block text-sm text-secondary"><%= t(".closes_on", date: l(next_close, format: :long)) %></span>
<% end %>
- <% end %>
- <%= summary_card title: t(".apr") do %>
- <%= card.apr.present? ? number_to_percentage(card.apr, precision: 3) : t(".unknown") %>
- <% end %>
+ <% if show_apr %>
+ <div class="col-span-full @3xl:col-span-1">
+ <%= summary_card title: t(".annual_percentage_rate") do %>
+ <%= number_to_percentage(card.apr, precision: 3) %>
+ <span class="mt-2 block space-y-1 text-sm text-secondary">
+ <span class="flex items-center justify-between gap-4">
+ <span><%= t(".monthly_rate_label") %></span>
+ <span class="font-medium text-primary"><%= number_to_percentage(summary.monthly_rate, precision: 3) %></span>
+ </span>
+ <span class="flex items-center justify-between gap-4">
+ <span><%= t(".estimated_interest_label") %></span>
+ <span class="font-medium text-primary">
+ <%= summary.estimated_interest ? t(".per_month", amount: format_money(summary.estimated_interest)) : t(".unknown") %>
+ </span>
+ </span>
+ <span class="flex items-center justify-between gap-4">
+ <span><%= t(".balance_carried_label") %></span>
+ <span class="font-medium text-primary"><%= summary.remaining ? format_money(summary.remaining) : t(".unknown") %></span>
+ </span>
+ </span>
+ <% end %>
+ </div>
+ <% end %>
+ </div>
</div>
<%# The minimum payment earns its place here only as this comparison. On its
@@ -74,7 +123,7 @@
<div class="mt-2">
<%= render DS::Alert.new(variant: :warning) do %>
<%= t(".above_minimum",
- balance: format_money(last_balance),
+ balance: format_money(summary.balance),
minimum: format_money(card.minimum_payment_money)) %>
<% end %>
</div>
@@ -84,7 +133,7 @@
saying something different from one that totals zero: the first has
entries we cannot add up in the card's own currency. Quoting $0.00 there
would be a wrong number presented as a right one. %>
- <% if accrued.nil? || last_balance.nil? %>
+ <% if summary.accrued.nil? || summary.balance.nil? %>
<div class="mt-2">
<%= render DS::Alert.new(variant: :info) do %>
<%= t(".multi_currency_unavailable") %>
diff --git a/config/locales/views/credit_cards/en.yml b/config/locales/views/credit_cards/en.yml
index 791dd867d0..25f8532d89 100644
--- a/config/locales/views/credit_cards/en.yml
+++ b/config/locales/views/credit_cards/en.yml
@@ -51,13 +51,24 @@ en:
overview:
above_minimum: This statement came to %{balance}, above the %{minimum} minimum.
Paying only the minimum leaves the balance on the card at this APR.
+ annual_percentage_rate: Annual Percentage Rate
apr: APR
+ balance_carried_label: Balance carried
+ charges: Charges
+ estimated_interest_label: Estimated interest
+ monthly_rate_label: Monthly rate
+ per_month: "%{amount}/month"
closes_on: Next statement closes %{date}
credit_toward_next: Credit toward the next statement
due_on: "Due %{date}"
edit_card_details: Edit card details
+ payments_credits: Payments/Credits
+ remaining_due: Remaining due %{amount}
+ scheduled_payment: Scheduled payment %{amount} on %{date}
since_last_statement: Since Last Statement
statement_balance: Statement Balance
+ status_paid: Paid
+ status_partial: Partially paid
multi_currency_unavailable: This statement adds up entries recorded in more
than one currency, which cannot be totalled here without a rate for every
one of them. The balances are left blank rather than guessed.
diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb
index 61513b6754..ecd44ee79b 100644
--- a/test/controllers/accounts_controller_test.rb
+++ b/test/controllers/accounts_controller_test.rb
@@ -832,6 +832,190 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
assert_match I18n.t("credit_cards.tabs.overview.statement_balance"), response.body
end
+ test "credit card overview hides the APR card when APR is blank" do
+ sign_in users(:family_admin)
+ accounts(:credit_card).credit_card.update!(statement_close_day: 25, due_day: 5, apr: nil, minimum_payment: nil)
+
+ get account_url(accounts(:credit_card), tab: "overview")
+
+ assert_response :success
+ assert_select "h4", text: I18n.t("credit_cards.tabs.overview.annual_percentage_rate"), count: 0
+ assert_select "[data-testid='credit-card-summary-grid'][class*='sm:grid-cols-2']", count: 1
+ assert_select "[data-testid='credit-card-summary-grid'][class*='@3xl:grid-cols-3']", count: 0
+ end
+
+ test "credit card overview shows the APR card when APR is present" do
+ sign_in users(:family_admin)
+ accounts(:credit_card).credit_card.update!(statement_close_day: 25, due_day: 5, apr: 18.99, minimum_payment: nil)
+
+ get account_url(accounts(:credit_card), tab: "overview")
+
+ assert_response :success
+ assert_select "h4", text: I18n.t("credit_cards.tabs.overview.annual_percentage_rate"), count: 1
+ assert_select "[data-testid='credit-card-summary-grid'][class*='sm:grid-cols-2']", count: 1
+ assert_select "[data-testid='credit-card-summary-grid'][class*='@3xl:grid-cols-3']", count: 1
+ end
+
+ test "credit card overview shows the estimated monthly interest for the APR" do
+ sign_in users(:family_admin)
+ account = accounts(:credit_card)
+ account.credit_card.update!(statement_close_day: 10, due_day: 26, apr: 12, minimum_payment: nil)
+
+ travel_to Date.new(2026, 10, 16) do
+ account.entries.create!(
+ name: "Cycle charge", date: Date.new(2026, 9, 20), amount: 1200, currency: "USD",
+ entryable: Transaction.new
+ )
+
+ get account_url(account, tab: "overview")
+
+ assert_response :success
+ assert_match I18n.t("credit_cards.tabs.overview.estimated_interest_label"), response.body
+ assert_match I18n.t("credit_cards.tabs.overview.per_month", amount: Money.new(12, "USD").format), response.body
+ assert_match I18n.t("credit_cards.tabs.overview.monthly_rate_label"), response.body
+ assert_match I18n.t("credit_cards.tabs.overview.balance_carried_label"), response.body
+ assert_match "1.000%", response.body
+ end
+ end
+
+ test "credit card overview hides the minimum warning when minimum payment is blank" do
+ sign_in users(:family_admin)
+ account = accounts(:credit_card)
+ account.credit_card.update!(statement_close_day: 10, due_day: 26, minimum_payment: nil)
+
+ travel_to Date.new(2026, 10, 16) do
+ account.entries.create!(
+ name: "Cycle charge", date: Date.new(2026, 9, 20), amount: 100, currency: "USD",
+ entryable: Transaction.new
+ )
+
+ get account_url(account, tab: "overview")
+
+ assert_response :success
+ assert_no_match "This statement came to", response.body
+ end
+ end
+
+ test "credit card overview shows the remaining amount and partial status" do
+ sign_in users(:family_admin)
+ account = accounts(:credit_card)
+ account.credit_card.update!(statement_close_day: 10, due_day: 26)
+
+ travel_to Date.new(2026, 10, 16) do
+ account.entries.create!(
+ name: "Cycle charge", date: Date.new(2026, 9, 20), amount: 100, currency: "USD",
+ entryable: Transaction.new
+ )
+ account.entries.create!(
+ name: "Card payment", date: Date.new(2026, 10, 12), amount: -40, currency: "USD",
+ entryable: Transaction.new
+ )
+
+ get account_url(account, tab: "overview")
+
+ assert_response :success
+ assert_match I18n.t("credit_cards.tabs.overview.remaining_due", amount: Money.new(60, "USD").format), response.body
+ assert_match I18n.t("credit_cards.tabs.overview.status_partial"), response.body
+ end
+ end
+
+ test "credit card overview shows the cycle breakdown and next close" do
+ sign_in users(:family_admin)
+ account = accounts(:credit_card)
+ account.credit_card.update!(statement_close_day: 10, due_day: 26)
+
+ travel_to Date.new(2026, 10, 16) do
+ account.entries.create!(
+ name: "Cycle charge", date: Date.new(2026, 10, 12), amount: 100, currency: "USD",
+ entryable: Transaction.new
+ )
+ account.entries.create!(
+ name: "Card payment", date: Date.new(2026, 10, 14), amount: -40, currency: "USD",
+ entryable: Transaction.new
+ )
+
+ get account_url(account, tab: "overview")
+
+ assert_response :success
+ assert_match I18n.t("credit_cards.tabs.overview.charges"), response.body
+ assert_match I18n.t("credit_cards.tabs.overview.payments_credits"), response.body
+ assert_match Money.new(100, "USD").format, response.body
+ assert_match Money.new(40, "USD").format, response.body
+ assert_match I18n.t("credit_cards.tabs.overview.closes_on", date: I18n.l(Date.new(2026, 11, 10), format: :long)), response.body
+ end
+ end
+
+ test "credit card overview shows the next open scheduled card payment" do
+ sign_in users(:family_admin)
+ account = accounts(:credit_card)
+ account.credit_card.update!(statement_close_day: 10, due_day: 26)
+
+ travel_to Date.new(2026, 10, 16) do
+ series = account.inbound_recurring_transfers.create!(
+ family: account.family,
+ account: accounts(:depository),
+ name: "Card payment",
+ amount: 40,
+ currency: "USD",
+ expected_day_of_month: 24,
+ last_occurrence_date: Date.new(2026, 9, 24),
+ next_expected_date: Date.new(2026, 10, 24),
+ manual: true
+ )
+ open_due = series.recurring_occurrences.open_status.minimum(:due_on)
+
+ get account_url(account, tab: "overview")
+
+ assert_response :success
+ assert_match I18n.t(
+ "credit_cards.tabs.overview.scheduled_payment",
+ amount: Money.new(40, "USD").format,
+ date: I18n.l(open_due, format: :long)
+ ), response.body
+ end
+ end
+
+ test "credit card overview hides a scheduled payment from a member without its source" do
+ sign_in users(:family_member)
+ account = accounts(:credit_card)
+ account.credit_card.update!(statement_close_day: 10, due_day: 26)
+ account.inbound_recurring_transfers.create!(
+ family: account.family,
+ account: accounts(:investment),
+ name: "Card payment",
+ amount: 40,
+ currency: "USD",
+ expected_day_of_month: 24,
+ last_occurrence_date: 2.months.ago.to_date,
+ next_expected_date: 1.month.from_now.to_date,
+ bill_type: "transfer",
+ manual: true
+ )
+
+ get account_url(account, tab: "overview")
+
+ assert_response :success
+ assert_no_match "Scheduled payment", response.body
+ end
+
+ test "credit card overview highlights an overdue statement" do
+ sign_in users(:family_admin)
+ account = accounts(:credit_card)
+ account.credit_card.update!(statement_close_day: 5, due_day: 21)
+
+ travel_to Date.new(2026, 10, 30) do
+ account.entries.create!(
+ name: "Cycle charge", date: Date.new(2026, 9, 20), amount: 552, currency: "USD",
+ entryable: Transaction.new
+ )
+
+ get account_url(account, tab: "overview")
+
+ assert_response :success
+ assert_select "span.text-destructive", text: I18n.t("credit_cards.due.overdue", count: 9)
+ end
+ end
+
test "credit card overview shows the payment due this month for an early close day" do
sign_in users(:family_admin)
accounts(:credit_card).credit_card.update!(statement_close_day: 10, due_day: 26)
@@ -894,7 +1078,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
assert_match I18n.t("credit_cards.tabs.overview.credit_toward_next"), response.body
- assert_no_match I18n.t("credit_cards.tabs.overview.due_on", date: ""), response.body
+ assert_no_match I18n.l(Date.new(2026, 11, 26), format: :long), response.body
end
end
diff --git a/test/models/credit_card_test.rb b/test/models/credit_card_test.rb
index 0634680609..f9dddb44b3 100644
--- a/test/models/credit_card_test.rb
+++ b/test/models/credit_card_test.rb
@@ -348,8 +348,151 @@ class CreditCardTest < ActiveSupport::TestCase
assert_not card.minimum_payment_shortfall?
end
+ test "the statement summary reports the remaining amount and partial status" do
+ card = statement_card(close_day: 10, due_day: 26)
+ charge(card.account, Date.new(2026, 9, 20), 300)
+ charge(card.account, Date.new(2026, 10, 12), -100)
+
+ summary = card.statement_summary(as_of: Date.new(2026, 10, 16), viewer: users(:family_admin))
+
+ assert_equal 300, summary.balance.amount
+ assert_equal 200, summary.remaining.amount
+ assert_equal 0, summary.charges.amount
+ assert_equal(-100, summary.credits.amount)
+ assert_equal(-100, summary.accrued.amount)
+ assert_equal Date.new(2026, 10, 26), summary.due_date
+ assert_equal Date.new(2026, 10, 26), summary.last_due_date
+ assert_equal :partial, summary.status
+ end
+
+ test "the statement summary marks a settled statement as paid and moves to the next due date" do
+ card = statement_card(close_day: 10, due_day: 26)
+ charge(card.account, Date.new(2026, 9, 20), 300)
+ charge(card.account, Date.new(2026, 10, 20), -300)
+
+ summary = card.statement_summary(as_of: Date.new(2026, 10, 27), viewer: users(:family_admin))
+
+ assert_equal 0, summary.remaining.amount
+ assert_equal :paid, summary.status
+ assert_equal Date.new(2026, 11, 26), summary.due_date
+ assert_equal Date.new(2026, 10, 26), summary.last_due_date
+ end
+
+ test "the statement summary keeps an unpaid passed bill overdue" do
+ card = statement_card(close_day: 10, due_day: 26)
+ charge(card.account, Date.new(2026, 9, 20), 300)
+
+ summary = card.statement_summary(as_of: Date.new(2026, 10, 27), viewer: users(:family_admin))
+
+ assert_equal 300, summary.remaining.amount
+ assert_equal :overdue, summary.status
+ assert_equal Date.new(2026, 10, 26), summary.due_date
+ end
+
+ test "the statement summary reports no balance when the statement is zero" do
+ card = statement_card(close_day: 10, due_day: 26)
+
+ summary = card.statement_summary(as_of: Date.new(2026, 10, 16), viewer: users(:family_admin))
+
+ assert_equal 0, summary.balance.amount
+ assert_equal 0, summary.remaining.amount
+ assert_equal :no_balance, summary.status
+ end
+
+ test "the statement summary surfaces an overdue open payment instead of the stale hint" do
+ travel_to Date.new(2026, 10, 16) do
+ card = accounts(:credit_card).credit_card
+ card.update!(statement_close_day: 10, due_day: 26)
+ series = scheduled_payment_series(card, source: accounts(:depository), stale: 45.days.ago.to_date)
+
+ assert_operator series.next_expected_date, :<, Date.current
+
+ summary = card.statement_summary(as_of: Date.current, viewer: users(:family_admin))
+ open_due = series.recurring_occurrences.open_status.minimum(:due_on)
+
+ assert_equal series.id, summary.scheduled_payment.payment.id
+ assert_equal open_due, summary.scheduled_payment.due_date
+ assert_operator summary.scheduled_payment.due_date, :>, series.next_expected_date
+ assert_operator summary.scheduled_payment.due_date, :<, Date.current
+ end
+ end
+
+ test "the statement summary hides a scheduled payment the viewer cannot reach" do
+ card = accounts(:credit_card).credit_card
+ card.update!(statement_close_day: 10, due_day: 26)
+ scheduled_payment_series(card, source: accounts(:investment), stale: 45.days.ago.to_date)
+
+ member_summary = card.statement_summary(as_of: Date.current, viewer: users(:family_member))
+ admin_summary = card.statement_summary(as_of: Date.current, viewer: users(:family_admin))
+
+ assert_nil member_summary.scheduled_payment
+ assert admin_summary.scheduled_payment.present?
+ end
+
+ test "the statement summary estimates monthly interest on the remaining balance" do
+ card = statement_card(close_day: 10, due_day: 26)
+ card.update!(apr: 12)
+ charge(card.account, Date.new(2026, 9, 20), 1200)
+
+ summary = card.statement_summary(as_of: Date.new(2026, 10, 16), viewer: users(:family_admin))
+
+ assert_equal 12, summary.estimated_interest.amount
+ assert_equal 1, summary.monthly_rate
+ end
+
+ test "the statement summary rounds interest to the account currency precision" do
+ card = statement_card(close_day: 10, due_day: 26, currency: "KWD")
+ card.update!(apr: 12)
+ charge(card.account, Date.new(2026, 9, 20), 0.40)
+
+ summary = card.statement_summary(as_of: Date.new(2026, 10, 16), viewer: users(:family_admin))
+
+ assert_equal BigDecimal("0.004"), summary.estimated_interest.amount
+ assert_equal "KWD", summary.estimated_interest.currency.iso_code
+ end
+
+ test "the statement summary reports no interest on a zero remaining balance" do
+ card = statement_card(close_day: 10, due_day: 26)
+ card.update!(apr: 12)
+
+ summary = card.statement_summary(as_of: Date.new(2026, 10, 16), viewer: users(:family_admin))
+
+ assert_equal 0, summary.estimated_interest.amount
+ end
+
+ test "the statement summary has no interest estimate without an APR" do
+ card = statement_card(close_day: 10, due_day: 26)
+ card.update!(apr: nil)
+ charge(card.account, Date.new(2026, 9, 20), 1200)
+
+ summary = card.statement_summary(as_of: Date.new(2026, 10, 16), viewer: users(:family_admin))
+
+ assert_nil summary.estimated_interest
+ end
+
+ test "the statement summary refuses a remaining amount it cannot total" do
+ card = statement_card(close_day: 10, due_day: 26)
+ charge(card.account, Date.new(2026, 9, 20), 300)
+ foreign = card.account.entries.create!(
+ name: "Foreign payment",
+ date: Date.new(2026, 10, 12),
+ amount: -100,
+ currency: "EUR",
+ entryable: Transaction.new
+ )
+
+ summary = card.statement_summary(as_of: Date.new(2026, 10, 16), viewer: users(:family_admin))
+
+ assert_nil summary.remaining
+ assert_nil summary.status
+ assert_nil summary.charges
+ assert_nil summary.credits
+ assert_nil summary.accrued
+ assert foreign.persisted?
+ end
+
private
- def statement_card(close_day:, due_day:, minimum_payment: 100)
+ def statement_card(close_day:, due_day:, minimum_payment: 100, currency: "USD")
card = CreditCard.create!(
statement_close_day: close_day,
due_day: due_day,
@@ -362,7 +505,7 @@ class CreditCardTest < ActiveSupport::TestCase
family: accounts(:credit_card).family,
name: "Statement Cycle Card",
balance: 0,
- currency: "USD",
+ currency: currency,
classification: "liability",
status: "active",
accountable: card
@@ -371,6 +514,25 @@ class CreditCardTest < ActiveSupport::TestCase
card.reload
end
+ def scheduled_payment_series(card, source:, stale:)
+ series = card.account.family.recurring_transactions.create!(
+ name: "Card payment #{stale}",
+ account: source,
+ destination_account: card.account,
+ amount: 100,
+ currency: "USD",
+ status: "active",
+ bill_type: "transfer",
+ manual: true,
+ dedup_scope: "card-payment-#{card.account.id}-#{stale}",
+ expected_day_of_month: stale.day,
+ last_occurrence_date: stale << 1,
+ next_expected_date: stale
+ )
+
+ series.reload
+ end
+
# Positive amount is a charge on a liability; negative is a payment, which
# is how ForwardCalculator reads a card's entries.
def charge(account, date, amount)