The personal finance app for everyone (by everyone)
Ruby
Stop calling a paused bill's leftover overdue anywhere (#3971)
Guillem Arias Fauste809c7f82e147
diff --git a/app/controllers/bills_controller.rb b/app/controllers/bills_controller.rbindex 6957865e47..03172d728d 100644--- a/app/controllers/bills_controller.rb+++ b/app/controllers/bills_controller.rb@@ -326,7 +326,7 @@ class BillsController < ApplicationControllercase statuswhen "overdue" then occurrence.overdue?- when "due" then occurrence.derived_state == :due+ when "due" then occurrence.due?when "partial" then occurrence.partially_paid?when "paid" then occurrence.paid?else falsediff --git a/app/helpers/bills_helper.rb b/app/helpers/bills_helper.rbindex 7cd0a04f6b..3d0a9a43c6 100644--- a/app/helpers/bills_helper.rb+++ b/app/helpers/bills_helper.rb@@ -327,12 +327,6 @@ module BillsHelpert("bills.attention.amount_changed") if recently_changedend- # A paused or dismissed bill's leftover is past its date, but nobody is- # paying it, so it isn't late: no red on its row, its drawers or its page.- def bills_overdue?(occurrence)- occurrence.recurring_transaction.active? && occurrence.overdue?- end-# A bill row's one line of context, in the same order in every section. It# truncates from the end, so the facts you can most do without come last.def bills_row_subline(occurrence, suggestion: nil)diff --git a/app/models/assistant/function/bills_support.rb b/app/models/assistant/function/bills_support.rbindex 776a33a91f..e95e510b75 100644--- a/app/models/assistant/function/bills_support.rb+++ b/app/models/assistant/function/bills_support.rb@@ -104,10 +104,19 @@ module Assistant::Function::BillsSupportexpected = occurrence.resolved_expected_amountpaid = occurrence.confirmed_allocated+ # Nobody is paying a bill that isn't active, so its leftover reports the+ # pause, not a schedule state that calls it overdue.+ series = occurrence.recurring_transaction+ state = if occurrence.scheduled? && !series.active?+ display_status(series)+ else+ occurrence.derived_state.to_s+ end+{due_on: occurrence.due_on.iso8601,effective_due_on: occurrence.effective_due_on.iso8601,- state: occurrence.derived_state.to_s,+ state: state,expected: Money.new(expected, occurrence.currency).format,paid: Money.new(paid, occurrence.currency).format,remaining: Money.new([ expected - paid, 0 ].max, occurrence.currency).format,diff --git a/app/models/assistant/function/get_bills.rb b/app/models/assistant/function/get_bills.rbindex d199a1cc97..d0c623f175 100644--- a/app/models/assistant/function/get_bills.rb+++ b/app/models/assistant/function/get_bills.rb@@ -18,7 +18,9 @@ class Assistant::Function::GetBills < Assistant::Functionbank data, awaiting the user's confirmation -- not yet a real bill),"paused" (set aside by the user), "ended" (dismissed or finished), "all".- payment_state filters on the CURRENT occurrence instead: "overdue", "due",- "upcoming", "partial" (partly paid), "paid".+ "upcoming", "partial" (partly paid), "paid". Nobody is paying a paused or ended+ bill, so its unpaid occurrence is never "overdue" or "due"; its state reads the+ bill's status instead.- bill_type "income" is a declared income schedule (paycheck), not an obligation.Amounts are always positive magnitudes; bill_type carries the direction.- totals exclude income and transfers (a transfer moves money, it is not spend),@@ -192,8 +194,8 @@ class Assistant::Function::GetBills < Assistant::Functioncase statewhen "overdue" then occurrence.overdue?- when "due" then occurrence.derived_state == :due- when "upcoming" then occurrence.derived_state == :upcoming+ when "due" then occurrence.due?+ when "upcoming" then occurrence.recurring_transaction.active? && occurrence.derived_state == :upcomingwhen "partial" then occurrence.partially_paid?when "paid" then occurrence.paid?else falsediff --git a/app/models/recurring_occurrence.rb b/app/models/recurring_occurrence.rbindex b7122dadc1..6a0cacf6ad 100644--- a/app/models/recurring_occurrence.rb+++ b/app/models/recurring_occurrence.rb@@ -110,8 +110,16 @@ class RecurringOccurrence < ApplicationRecordendend+ # Nobody is paying a bill that isn't active, so its leftover is neither late+ # nor due, whatever the dates say. derived_state stays the raw schedule+ # state: the matcher reads it to keep a late payment able to settle the+ # leftover.def overdue?- derived_state == :overdue+ recurring_transaction.active? && derived_state == :overdue+ end++ def due?+ recurring_transaction.active? && derived_state == :dueend# --- Lifecycle actions. Closing freezes the resolved amount so the row isdiff --git a/app/views/bills/_calendar_chip.html.erb b/app/views/bills/_calendar_chip.html.erbindex 0414660cdf..2a6277ead0 100644--- a/app/views/bills/_calendar_chip.html.erb+++ b/app/views/bills/_calendar_chip.html.erb@@ -13,7 +13,7 @@endelsif occurrence.skipped? || occurrence.missed?"bg-surface-inset text-subdued line-through"- elsif occurrence.derived_state == :overdue+ elsif occurrence.overdue?"bg-destructive/10 text-destructive"else"bg-surface-inset text-primary"diff --git a/app/views/bills/_occurrence.html.erb b/app/views/bills/_occurrence.html.erbindex a371ddc970..075e5bb893 100644--- a/app/views/bills/_occurrence.html.erb+++ b/app/views/bills/_occurrence.html.erb@@ -32,7 +32,7 @@Every section has it, so the avatars line up down the stacked card.Only from @lg. Below it the subline leads with the same date, in thesame colour. %>- <div class="hidden @lg:block w-14 shrink-0 text-xs <%= bills_overdue?(occurrence) ? "text-destructive font-medium" : "text-secondary" %>">+ <div class="hidden @lg:block w-14 shrink-0 text-xs <%= occurrence.overdue? ? "text-destructive font-medium" : "text-secondary" %>"><%= bills_rail_date(occurrence) %></div>@@ -57,7 +57,7 @@facts. The schedule is always on it, which is also what tellssubscription tiers with one name and one amount apart. %><p class="text-xs text-secondary truncate">- <span class="@lg:hidden <%= "text-destructive" if bills_overdue?(occurrence) %>"><%= bills_row_date(occurrence) %> ·</span>+ <span class="@lg:hidden <%= "text-destructive" if occurrence.overdue? %>"><%= bills_row_date(occurrence) %> ·</span><%= bills_row_subline(occurrence, suggestion: suggestion) %></p><% if series.notes.present? %>diff --git a/app/views/bills/_summary.html.erb b/app/views/bills/_summary.html.erbindex 25a9d40033..f01528016a 100644--- a/app/views/bills/_summary.html.erb+++ b/app/views/bills/_summary.html.erb@@ -24,7 +24,7 @@<%= t("bills.summary.remaining", amount: format_money(current_occurrence.remaining_amount_money)) %><% end %></p>- <p class="text-xs <%= bills_overdue?(current_occurrence) ? "text-destructive" : "text-secondary" %>">+ <p class="text-xs <%= current_occurrence.overdue? ? "text-destructive" : "text-secondary" %>"><span class="privacy-sensitive"><%= t("bills.partial_progress", paid: format_money(paid), expected: format_money(expected)) %></span>· <%= occurrence_due_label(current_occurrence) %></p>diff --git a/app/views/bills/show.html.erb b/app/views/bills/show.html.erbindex 8171b5fd7d..1559f65c63 100644--- a/app/views/bills/show.html.erb+++ b/app/views/bills/show.html.erb@@ -72,7 +72,7 @@<%= t("bills.summary.remaining", amount: format_money(@current_occurrence.remaining_amount_money)) %><% end %></p>- <p class="text-xs <%= bills_overdue?(@current_occurrence) ? "text-destructive" : "text-secondary" %>">+ <p class="text-xs <%= @current_occurrence.overdue? ? "text-destructive" : "text-secondary" %>"><span class="privacy-sensitive"><%= t("bills.partial_progress",paid: format_money(@current_occurrence.confirmed_allocated_money),diff --git a/app/views/recurring_occurrences/show.html.erb b/app/views/recurring_occurrences/show.html.erbindex e5e62cb0a7..b1688f9fe5 100644--- a/app/views/recurring_occurrences/show.html.erb+++ b/app/views/recurring_occurrences/show.html.erb@@ -22,7 +22,7 @@<h2 id="<%= dialog.title_id %>" class="text-lg font-medium text-primary truncate"><%= series.display_name %></h2>- <p class="text-sm <%= bills_overdue?(@occurrence) ? "text-destructive" : "text-secondary" %>">+ <p class="text-sm <%= @occurrence.overdue? ? "text-destructive" : "text-secondary" %>"><%= occurrence_due_label(@occurrence) %></p></div>@@ -66,7 +66,7 @@<%= render DS::ProgressRing.new(percent: percent,size: 56,- tone: @occurrence.paid? ? :success : (bills_overdue?(@occurrence) ? :destructive : :neutral)+ tone: @occurrence.paid? ? :success : (@occurrence.overdue? ? :destructive : :neutral)) %></div></div>diff --git a/test/controllers/bills_controller_test.rb b/test/controllers/bills_controller_test.rbindex f5383eec1a..b66b74dd19 100644--- a/test/controllers/bills_controller_test.rb+++ b/test/controllers/bills_controller_test.rb@@ -211,7 +211,7 @@ class BillsControllerTest < ActionDispatch::IntegrationTestlast_occurrence_date: 2.months.ago.to_date, next_expected_date: late)bill.mark_inactive!occurrence = bill.recurring_occurrences.open_status.sole- assert occurrence.overdue?, "premise: the leftover is past its grace"+ assert_equal :overdue, occurrence.derived_state, "premise: the leftover is past its grace"get bills_url@@ -234,7 +234,7 @@ class BillsControllerTest < ActionDispatch::IntegrationTestlast_occurrence_date: 2.months.ago.to_date, next_expected_date: late)bill.mark_inactive!occurrence = bill.recurring_occurrences.open_status.sole- assert occurrence.overdue?, "premise: the leftover is past its grace"+ assert_equal :overdue, occurrence.derived_state, "premise: the leftover is past its grace"paused = I18n.t("recurring_transactions.status.inactive")@@ -534,6 +534,31 @@ class BillsControllerTest < ActionDispatch::IntegrationTestassert_no_match "Late Co", response.body, "lifecycle filtering still works"end+ # Payment state is what is late and what is still owed, and nobody owes a+ # paused bill's leftover. The Paused filter is where it lives.+ test "the all view's overdue and due filters leave out a paused bill's leftover" do+ late_day = 10.days.ago.to_date+ late = create_bill(name: "Paused late", amount: 75, expected_day_of_month: late_day.day,+ last_occurrence_date: 2.months.ago.to_date, next_expected_date: late_day)+ grace_day = 2.days.ago.to_date+ in_grace = create_bill(name: "Paused grace", amount: 76, expected_day_of_month: grace_day.day,+ last_occurrence_date: 2.months.ago.to_date, next_expected_date: grace_day)+ assert late.current_occurrence.overdue?, "Paused late must be overdue before the pause"+ assert in_grace.current_occurrence.due?, "Paused grace must be due before the pause"+ [ late, in_grace ].each(&:mark_inactive!)++ %w[overdue due].each do |status|+ get bills_url(view: "all", q: { status: status })+ assert_response :success+ assert_no_match "Paused late", response.body, status+ assert_no_match "Paused grace", response.body, status+ end++ get bills_url(view: "all", q: { status: "paused" })+ assert_match "Paused late", response.body+ assert_match "Paused grace", response.body+ end+test "index cannot see another family's bills" dofamilies(:empty).recurring_transactions.create!(name: "Someone else's rent",@@ -784,6 +809,27 @@ class BillsControllerTest < ActionDispatch::IntegrationTestassert_match Date.current.strftime("%B %Y"), response.bodyend+ # The calendar loads paused bills too, and painted a paused bill's leftover+ # as a red overdue chip.+ test "the calendar paints a paused bill's leftover neutral" do+ travel_to Date.current.beginning_of_month + 14 do+ late_day = Date.current.beginning_of_month + 4+ active = create_bill(name: "Active late", amount: 60, expected_day_of_month: late_day.day,+ last_occurrence_date: 2.months.ago.to_date, next_expected_date: late_day)+ paused = create_bill(name: "Paused late", amount: 61, expected_day_of_month: late_day.day,+ last_occurrence_date: 2.months.ago.to_date, next_expected_date: late_day)+ paused.mark_inactive!++ get bills_url(view: "calendar")++ assert_response :success+ chip = ->(series) { recurring_occurrence_path(series.recurring_occurrences.find_by!(due_on: late_day)) }+ assert_select "a.text-destructive[href=?]", chip.(active)+ assert_select "a.text-primary[href=?]", chip.(paused)+ assert_select "a.text-destructive[href=?]", chip.(paused), count: 0+ end+ end+test "the calendar materializes a far-future month on demand" docreate_bill(name: "Forward bill", amount: 25)target = (Date.current + 6.months).beginning_of_monthdiff --git a/test/helpers/bills_helper_test.rb b/test/helpers/bills_helper_test.rbindex d41023d742..25047fa15a 100644--- a/test/helpers/bills_helper_test.rb+++ b/test/helpers/bills_helper_test.rb@@ -303,7 +303,6 @@ class BillsHelperTest < ActionView::TestCasereason = I18n.t("bills.attention.overdue", count: 30)assert_includes bills_row_subline(occurrence), %(<span class="text-destructive">#{reason}</span>)- assert bills_overdue?(occurrence)assert_includes bills_row_subline(occurrence, suggestion: :pending), I18n.t("bills.attention.needs_review")assert_not_includes bills_row_subline(occurrence, suggestion: :pending), "text-destructive"@@ -315,7 +314,6 @@ class BillsHelperTest < ActionView::TestCaseoccurrence.recurring_transaction.status = "inactive"assert_not_includes bills_row_subline(occurrence), "text-destructive"- assert_not bills_overdue?(occurrence)end# The drawer, the bill's page and the payment drawer print this label, anddiff --git a/test/models/assistant/function/get_bills_test.rb b/test/models/assistant/function/get_bills_test.rbindex e1bf07063b..6b062c986e 100644--- a/test/models/assistant/function/get_bills_test.rb+++ b/test/models/assistant/function/get_bills_test.rb@@ -71,6 +71,37 @@ class Assistant::Function::GetBillsTest < ActiveSupport::TestCaseendend+ # Nobody is paying a paused bill, so its leftover is neither overdue nor+ # due, and the occurrence names the pause the way the series does.+ test "a paused bill's leftover is not overdue or due, and reads paused" do+ late_day = 10.days.ago.to_date+ late = create_series(name: "Paused late", amount: 75, expected_day_of_month: late_day.day,+ anchor_date: late_day, last_occurrence_date: 2.months.ago.to_date,+ next_expected_date: late_day)+ grace_day = 2.days.ago.to_date+ in_grace = create_series(name: "Paused grace", amount: 76, expected_day_of_month: grace_day.day,+ anchor_date: grace_day, last_occurrence_date: 2.months.ago.to_date,+ next_expected_date: grace_day)+ assert_equal [ "Paused late" ], call_tool("payment_state" => "overdue")[:bills].map { |bill| bill[:name] }+ assert_equal [ "Paused grace" ], call_tool("payment_state" => "due")[:bills].map { |bill| bill[:name] }+ future_day = Date.current + 20+ future = create_series(name: "Paused future", amount: 77, expected_day_of_month: future_day.day,+ anchor_date: future_day, last_occurrence_date: future_day - 1.month,+ next_expected_date: future_day)+ # Pausing drops future rows, except one already carrying a payment.+ RecurringTransaction::Allocator.new(future.current_occurrence).allocate!(amount: "10")+ assert_equal [ "Paused future" ], call_tool("payment_state" => "upcoming")[:bills].map { |bill| bill[:name] }+ [ late, in_grace, future ].each(&:mark_inactive!)++ assert_empty call_tool("status" => "all", "payment_state" => "overdue")[:bills]+ assert_empty call_tool("status" => "paused", "payment_state" => "due")[:bills]+ assert_empty call_tool("status" => "paused", "payment_state" => "upcoming")[:bills]++ result = call_tool("status" => "all")+ assert_equal 0, result[:totals][:overdue_count]+ assert_equal %w[paused paused paused], result[:bills].map { |bill| bill.dig(:current_occurrence, :state) }+ end+test "search matches the merchant behind a nameless series" docreate_series(name: nil, merchant: merchants(:netflix), amount: 15.99)create_series(name: "Water", amount: 80)diff --git a/test/models/recurring_occurrence_test.rb b/test/models/recurring_occurrence_test.rbindex 56de786f66..1a817f6590 100644--- a/test/models/recurring_occurrence_test.rb+++ b/test/models/recurring_occurrence_test.rb@@ -60,6 +60,27 @@ class RecurringOccurrenceTest < ActiveSupport::TestCaseendend+ # Nobody is paying a bill that isn't active, so its leftover isn't late or+ # due. The schedule state stays raw: the matcher reads it to keep a late+ # payment able to settle the leftover.+ test "a leftover of a bill that isn't active is neither overdue nor due" do+ travel_to Date.new(2026, 8, 13) do+ overdue = create_occurrence(due_on: Date.new(2026, 8, 5), original: Date.new(2026, 8, 5))+ due = create_occurrence(due_on: Date.new(2026, 8, 11), original: Date.new(2026, 8, 11))+ assert overdue.overdue?+ assert due.due?++ %w[inactive paused ended].each do |status|+ @series.update!(status: status)++ assert_not overdue.reload.overdue?, status+ assert_equal :overdue, overdue.derived_state, status+ assert_not due.reload.due?, status+ assert_equal :due, due.derived_state, status+ end+ end+ end+test "partial payment is exact-sum, never tolerance" dooccurrence = create_occurrenceoccurrence.allocations.create!(allocated_amount: 10, currency: "USD", source: "user_created")diff --git a/test/models/recurring_transaction/matcher_test.rb b/test/models/recurring_transaction/matcher_test.rbindex f529d5321a..ec5518ca23 100644--- a/test/models/recurring_transaction/matcher_test.rb+++ b/test/models/recurring_transaction/matcher_test.rb@@ -173,6 +173,20 @@ class RecurringTransaction::MatcherTest < ActiveSupport::TestCaseendend+ # Pausing keeps the open leftover, and a late payment must still reach it:+ # the window of an overdue leftover runs to today, paused or not.+ test "a late payment still matches a paused bill's overdue leftover" do+ series = create_series(name: "POWER CO", amount: 60, day_offset: -20)+ occurrence = series.recurring_occurrences.order(:due_on).first+ series.mark_inactive!+ assert_not occurrence.overdue?+ entry = create_entry(amount: 60, date: Date.current, name: "POWER CO")++ @matcher.run!++ assert_equal entry, occurrence.allocations.sole.entry+ end+test "backfill mode writes confirmed history only, never suggestions" doseries = create_series(name: "CITY WATER", amount: 80, day_offset: -7)exact_series = create_series(name: "POWER CO", amount: 60, day_offset: 2)