The personal finance app for everyone (by everyone)
Ruby
Count next 7 days across month boundaries in Bills overview (#4562)
joaocbatista69f2d485c534
diff --git a/app/controllers/bills_controller.rb b/app/controllers/bills_controller.rbindex a8b998c9c4..3e78017776 100644--- a/app/controllers/bills_controller.rb+++ b/app/controllers/bills_controller.rb@@ -65,6 +65,7 @@ class BillsController < ApplicationController# Snooze-aware, like the rail (bills_row_date): rows list in rail order,# and a bill snoozed past the month's end belongs after it.@this_month, later = upcoming.partition { |occurrence| occurrence.effective_due_on <= month_end }+ @open_upcoming = upcoming@overdue = @overdue.sort_by(&:effective_due_on)@dormant = @dormant.sort_by(&:effective_due_on)@@ -506,7 +507,18 @@ class BillsController < ApplicationController@remaining_this_month, @unconvertible_count = total_of(owed_now) { |occurrence| occurrence.remaining_amount_money }@paid_this_month_total, _ = total_of(@paid_this_month) { |occurrence| occurrence.confirmed_allocated_money }- @due_next_seven, _ = total_of(owed_now.select { |occurrence| occurrence.effective_due_on <= today + 7 }) { |occurrence| occurrence.remaining_amount_money }+ # The next seven days run across month boundaries, so they are not+ # limited to this month's owed list. Overdue bills are excluded: they+ # already have their own figure.+ @next_seven_until = today + 6+ window = @open_upcoming.select { |occurrence| occurrence.effective_due_on.between?(today, @next_seven_until) }+ @due_next_seven, _ = total_of(window) { |occurrence| occurrence.remaining_amount_money }+ # Bills beyond this month were not in the owed count above, so their+ # conversion failures are added to it. The window reads every open bill,+ # not the one-per-series @later list, so a second occurrence of the same+ # bill inside the window is counted.+ _, beyond_month_unconvertible = total_of(window - @this_month) { |occurrence| occurrence.remaining_amount_money }+ @unconvertible_count += beyond_month_unconvertible@past_due_total, _ = total_of(@overdue) { |occurrence| occurrence.remaining_amount_money }@owed_count = owed_now.size@needs_action_count = owed_now.count { |occurrence| !occurrence.recurring_transaction.autopay? }diff --git a/app/views/bills/_month_pulse.html.erb b/app/views/bills/_month_pulse.html.erbindex 3ead012f53..73bc1ecf23 100644--- a/app/views/bills/_month_pulse.html.erb+++ b/app/views/bills/_month_pulse.html.erb@@ -45,6 +45,7 @@<p><span class="font-medium text-primary privacy-sensitive"><%= format_money(@due_next_seven) %></span><span class="text-secondary"><%= t(".pulse_next_seven") %></span>+ <span class="text-secondary"><%= t(".pulse_until", date: format_date(@next_seven_until), default: "until %{date}") %></span></p><% end %></div>diff --git a/config/locales/views/bills/en.yml b/config/locales/views/bills/en.ymlindex ca24112fa8..1a194bb99e 100644--- a/config/locales/views/bills/en.yml+++ b/config/locales/views/bills/en.yml@@ -75,6 +75,7 @@ en:pulse_paid: paidpulse_overdue: overduepulse_next_seven: next 7 days+ pulse_until: until %{date}pulse_bar_aria: "%{percent}% of this month's bills paid"next_up: Next upview_calendar: View calendardiff --git a/test/controllers/bills_controller_test.rb b/test/controllers/bills_controller_test.rbindex 570e105f2b..914987f204 100644--- a/test/controllers/bills_controller_test.rb+++ b/test/controllers/bills_controller_test.rb@@ -563,6 +563,7 @@ class BillsControllerTest < ActionDispatch::IntegrationTestassert_match I18n.t("bills.month_pulse.pulse_paid"), bodyassert_match I18n.t("bills.month_pulse.pulse_overdue"), bodyassert_match I18n.t("bills.month_pulse.pulse_next_seven"), body+ assert_match I18n.t("bills.month_pulse.pulse_until", date: ApplicationController.helpers.format_date(6.days.from_now.to_date)), body# And it ends by saying what actually happens next.assert_match I18n.t("bills.month_pulse.next_up"), body@@ -945,6 +946,32 @@ class BillsControllerTest < ActionDispatch::IntegrationTestassert_match "$77.00", response.bodyend+ # The seven-day figure must count every occurrence inside the window, even+ # when the window crosses the month end and one bill has two occurrences in it.+ test "next seven days counts every occurrence across the month end" do+ travel_to Date.new(2026, 10, 29) do+ bill = create_bill(name: "Gym membership", amount: 25, manual: true,+ anchor_date: Date.new(2026, 11, 1), expected_day_of_month: 1,+ last_occurrence_date: Date.new(2026, 10, 1),+ next_expected_date: Date.new(2026, 11, 1))+ # Creating the bill already materializes its first occurrence (Nov 1), so+ # only add the second one and let find_or_create_by! reuse the first.+ [ Date.new(2026, 11, 1), Date.new(2026, 11, 3) ].each do |due|+ bill.recurring_occurrences.find_or_create_by!(original_due_on: due) do |occurrence|+ occurrence.family = @family+ occurrence.due_on = due+ occurrence.currency = "USD"+ end+ end++ get bills_url+ assert_response :success++ assert_match "$50.00", response.body+ assert_match ApplicationController.helpers.format_date(Date.new(2026, 11, 4)), response.body+ end+ end+test "the all view lists every series and filters compose" docreate_bill(name: "Alpha bill", amount: 10)create_bill(name: "Beta paused", amount: 20, status: "paused")