The personal finance app for everyone (by everyone)
Ruby
Count next month's bills in a pay period's due total (#4556)
Dov Benyomin Sohacheski70ad5d09e031
diff --git a/app/helpers/bills_helper.rb b/app/helpers/bills_helper.rbindex 3472938a9d..e7a0689a5e 100644--- a/app/helpers/bills_helper.rb+++ b/app/helpers/bills_helper.rb@@ -51,8 +51,10 @@ module BillsHelper# period, so the section template can drop a marker between groups without# pre-bucketing the rows. Each marker carries its period and the summed# obligations due inside it. Dated like the rail, so a snoozed row counts- # toward the period it sits in.- def bills_pay_period_markers(occurrences, pay_periods)+ # toward the period it sits in. A period can run past the rows' month, so+ # the total sums `counted`, which reaches beyond it. A bill with no rate into+ # `currency` is left out; the month pulse already counts it as unconvertible.+ def bills_pay_period_markers(occurrences, pay_periods, currency:, counted: occurrences)return {} if pay_periods.blank?seen = Set.new@@ -63,8 +65,12 @@ module BillsHelperperiod = pay_periods[index]markers[occurrence.id] = {period: period,- due_total: occurrences.select { |o| o.effective_due_on.between?(period.starts_on, period.ends_on) }- .sum { |o| o.resolved_expected_amount.abs }+ due_total: counted.select { |o| o.effective_due_on.between?(period.starts_on, period.ends_on) }+ .sum do |o|+ o.resolved_expected_amount_money.exchange_to(currency).amount.abs+ rescue Money::ConversionError+ 0+ end}endenddiff --git a/app/views/bills/_occurrence_section.html.erb b/app/views/bills/_occurrence_section.html.erbindex efda4bdedc..0f8a0391e8 100644--- a/app/views/bills/_occurrence_section.html.erb+++ b/app/views/bills/_occurrence_section.html.erb@@ -1,7 +1,7 @@-<%# locals: (title:, occurrences:, suggestions: {}, meta: nil, pay_periods: []) %>+<%# locals: (title:, occurrences:, suggestions: {}, meta: nil, pay_periods: [], pay_period_occurrences: occurrences) %><% return if occurrences.empty? %>-<% markers = bills_pay_period_markers(occurrences, pay_periods) %>+<% markers = bills_pay_period_markers(occurrences, pay_periods, currency: Current.family.currency, counted: pay_period_occurrences) %><%# No shell by default: bills/index stacks the month sections in one. %><%= render DS::Section.new(title: title, count: occurrences.size, inset: false, divided: false) do |section| %>diff --git a/app/views/bills/index.html.erb b/app/views/bills/index.html.erbindex ee12df48cf..0ff35023e6 100644--- a/app/views/bills/index.html.erb+++ b/app/views/bills/index.html.erb@@ -86,7 +86,7 @@occurrences: @overdue,suggestions: @suggestions_by_occurrence,meta: (@past_due_total && @past_due_total.amount.positive? ? t(".attention_overdue_total", amount: format_money(@past_due_total)) : nil) %>- <%= render "bills/occurrence_section", title: t(".this_month"), occurrences: @month_rows, suggestions: @suggestions_by_occurrence, pay_periods: @month_pay_periods %>+ <%= render "bills/occurrence_section", title: t(".this_month"), occurrences: @month_rows, suggestions: @suggestions_by_occurrence, pay_periods: @month_pay_periods, pay_period_occurrences: @paid_this_month + @open_upcoming %><%= render "bills/occurrence_section", title: t(".later"), occurrences: @later, suggestions: @suggestions_by_occurrence %><%= render "bills/occurrence_section", title: t(".dormant"), occurrences: @dormant, suggestions: @suggestions_by_occurrence %></div>diff --git a/test/controllers/bills_controller_test.rb b/test/controllers/bills_controller_test.rbindex 08fe978a8a..294afd16dc 100644--- a/test/controllers/bills_controller_test.rb+++ b/test/controllers/bills_controller_test.rb@@ -1177,6 +1177,25 @@ class BillsControllerTest < ActionDispatch::IntegrationTestassert_match I18n.t("bills.paycheck.safe_after_bills"), response.bodyend+ test "a pay period marker totals the bills due after the month's end" do+ travel_to Date.new(2026, 10, 23)+ [ 21, 26 ].each do |day|+ payday = Date.new(2026, 10, day)+ @family.recurring_transactions.create!(+ name: "Paycheck #{day}", account: accounts(:depository), amount: -1840, currency: "USD",+ bill_type: "income", expected_day_of_month: day, anchor_date: payday,+ last_occurrence_date: payday, next_expected_date: payday.next_month, status: "active", manual: true+ )+ end+ create_bill(name: "Rent", amount: 900, expected_day_of_month: 28, next_expected_date: Date.new(2026, 10, 28))+ create_bill(name: "Power", amount: 80, expected_day_of_month: 5, next_expected_date: Date.new(2026, 11, 5))++ get bills_url++ assert_response :success+ assert_match I18n.t("bills.pay_period.due_before_date", amount: "$980.00", date: I18n.l(Date.new(2026, 11, 21), format: :short)), response.body+ end+# A lone materialized paycheck landing today collapses the planner's# boundary list to a single date, which yields an empty plan; the page must# treat that like no plan instead of crashing on plan.last.diff --git a/test/helpers/bills_helper_test.rb b/test/helpers/bills_helper_test.rbindex dbeaac4063..a191def96e 100644--- a/test/helpers/bills_helper_test.rb+++ b/test/helpers/bills_helper_test.rb@@ -419,7 +419,7 @@ class BillsHelperTest < ActionView::TestCasesecond_inside = stub_occurrence("Power", 80, id: "two", due_on: Date.new(2026, 9, 10))outside = stub_occurrence("Later", 10, id: "three", due_on: Date.new(2026, 9, 20))- markers = bills_pay_period_markers([ first_inside, second_inside, outside ], [ period ])+ markers = bills_pay_period_markers([ first_inside, second_inside, outside ], [ period ], currency: "USD")assert_equal [ "one" ], markers.keysassert_equal 2230, markers["one"][:due_total]@@ -435,17 +435,42 @@ class BillsHelperTest < ActionView::TestCasesnoozed = stub_occurrence("Gym", 40, id: "gym", due_on: Date.new(2026, 9, 17), effective_due_on: Date.new(2026, 9, 24))power = stub_occurrence("Power", 80, id: "power", due_on: Date.new(2026, 9, 25))- markers = bills_pay_period_markers([ rent, snoozed, power ], [ this_week, next_week ])+ markers = bills_pay_period_markers([ rent, snoozed, power ], [ this_week, next_week ], currency: "USD")assert_equal %w[rent gym], markers.keysassert_equal 900, markers["rent"][:due_total]assert_equal 120, markers["gym"][:due_total]end+ test "a pay period running past the month counts the bills due after it" do+ period = OpenStruct.new(starts_on: Date.new(2026, 10, 26), ends_on: Date.new(2026, 11, 20))+ rent = stub_occurrence("Rent", 900, id: "rent", due_on: Date.new(2026, 10, 28))+ power = stub_occurrence("Power", 80, id: "power", due_on: Date.new(2026, 11, 5))+ gym = stub_occurrence("Gym", 40, id: "gym", due_on: Date.new(2026, 11, 25))++ markers = bills_pay_period_markers([ rent ], [ period ], currency: "USD", counted: [ rent, power, gym ])++ assert_equal %w[rent], markers.keys+ assert_equal 980, markers["rent"][:due_total]+ end++ test "a pay period total converts each bill into the family currency and leaves out one with no rate" do+ ExchangeRate.stubs(:provider).returns(nil)+ period = OpenStruct.new(starts_on: Date.new(2026, 10, 26), ends_on: Date.new(2026, 11, 20))+ rent = stub_occurrence("Rent", 900, id: "rent", due_on: Date.new(2026, 10, 28))+ gym = stub_occurrence("Gym", 40, id: "gym", due_on: Date.new(2026, 11, 2), currency: "EUR")+ tax = stub_occurrence("Tax", 500, id: "tax", due_on: Date.new(2026, 11, 9), currency: "JPY")+ ExchangeRate.create!(from_currency: "EUR", to_currency: "USD", date: Date.current, rate: 2)++ markers = bills_pay_period_markers([ rent ], [ period ], currency: "USD", counted: [ rent, gym, tax ])++ assert_equal 980, markers["rent"][:due_total]+ end+test "no pay periods means no markers" dooccurrence = stub_occurrence("Rent", 1, id: "x", due_on: Date.current)- assert_empty bills_pay_period_markers([ occurrence ], [])+ assert_empty bills_pay_period_markers([ occurrence ], [], currency: "USD")endtest "month progress divides paid, overdue and upcoming out of one total" do@@ -638,12 +663,13 @@ class BillsHelperTest < ActionView::TestCaseprivate- def stub_occurrence(name, amount, id:, due_on: Date.current, effective_due_on: due_on)+ def stub_occurrence(name, amount, id:, due_on: Date.current, effective_due_on: due_on, currency: "USD")OpenStruct.new(id: id,due_on: due_on,effective_due_on: effective_due_on,resolved_expected_amount: amount,+ resolved_expected_amount_money: Money.new(amount, currency),recurring_transaction: OpenStruct.new(display_name: name))enddiff --git a/test/models/recurring_transaction/paycheck_planner_test.rb b/test/models/recurring_transaction/paycheck_planner_test.rbindex 9a394d93d1..00af9356be 100644--- a/test/models/recurring_transaction/paycheck_planner_test.rb+++ b/test/models/recurring_transaction/paycheck_planner_test.rb@@ -348,7 +348,7 @@ class RecurringTransaction::PaycheckPlannerTest < ActiveSupport::TestCaseassert_not plan.first.short?, "so $100 in the bank is not short"assert_equal %w[Internet Power Water], plan[1].items_due.map { |item| item.occurrence.recurring_transaction.name },"payday's window carries both, in the order of the dates they were snoozed to"- markers = ApplicationController.helpers.bills_pay_period_markers([ power ], plan)+ markers = ApplicationController.helpers.bills_pay_period_markers([ power ], plan, currency: @family.currency)assert_equal plan[1], markers.dig(power.id, :period), "the overview marks it under the same paycheck"end