Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Keep a due-day shift from adding a second occurrence to a paid cycle (#4380)

jaysbeekaya9f223f4d57a
diff --git a/app/models/recurring_transaction/occurrence_generator.rb b/app/models/recurring_transaction/occurrence_generator.rb
index a05fde0f24..e4551d7df3 100644
--- a/app/models/recurring_transaction/occurrence_generator.rb
+++ b/app/models/recurring_transaction/occurrence_generator.rb
@@ -70,7 +70,10 @@ class RecurringTransaction
end
def upsert_window(from, through)
- pairs = series.schedule.occurrence_pairs_between(from, through)
+ schedule = series.schedule
+ pairs = schedule.occurrence_pairs_between(from, through)
+ stood_in = stood_in_for(schedule, from, through)
+ pairs = pairs.reject { |pair| stood_in.include?(pair.original_due_on) }
return 0 if pairs.empty?
now = Time.current
@@ -90,5 +93,41 @@ class RecurringTransaction
result = RecurringOccurrence.insert_all(rows, unique_by: "idx_recurring_occurrences_identity")
result.rows.size
end
+
+ # Rows a schedule edit kept from the previous rule are paid or past-due
+ # occurrences the current schedule no longer produces. Each already is
+ # its cycle, so a new occurrence beside one (a due day moved by
+ # detection) would be a second row for the same cycle. Each kept row
+ # stands in for ONE occurrence: the nearest within half a cycle, so the
+ # other day of a close semimonthly pair (the 2nd and 5th, after the 1st
+ # moved to the 2nd) is still generated. A row the current schedule
+ # produces never counts, so two close days of one schedule both stay.
+ #
+ # Returns the original_due_ons of the occurrences stood in for. Searched
+ # beyond the window, so a kept row's nearest occurrence just outside it
+ # is not mistaken for a farther one inside.
+ def stood_in_for(schedule, from, through)
+ reach = half_cycle(schedule)
+ nearby = schedule.occurrence_pairs_between(from - reach, through + reach)
+ current = nearby.map(&:original_due_on).to_set
+
+ series.recurring_occurrences
+ .where(due_on: (from - reach)..(through + reach))
+ .pluck(:original_due_on, :due_on)
+ .reject { |original_due_on, _due_on| current.include?(original_due_on) }
+ .filter_map do |_original_due_on, due_on|
+ nearby.select { |pair| (due_on - pair.due_on).abs <= reach }
+ .min_by { |pair| [ (due_on - pair.due_on).abs, pair.due_on ] }
+ &.original_due_on
+ end
+ .to_set
+ end
+
+ # Just under half the cycle length, as RecurringTransaction::Matcher's
+ # window_for, so generation and matching agree on a cycle's reach.
+ def half_cycle(schedule)
+ cycle_days = (365.25 / schedule.occurrences_per_year).floor
+ [ (cycle_days - 1) / 2, 1 ].max
+ end
end
end
diff --git a/test/controllers/recurring_transactions_controller_test.rb b/test/controllers/recurring_transactions_controller_test.rb
index 3264a22a40..81775aeac1 100644
--- a/test/controllers/recurring_transactions_controller_test.rb
+++ b/test/controllers/recurring_transactions_controller_test.rb
@@ -636,6 +636,34 @@ class RecurringTransactionsControllerTest < ActionDispatch::IntegrationTest
assert_not_nil @recurring_transaction.anchor_date
end
+ # The bill form moves a due day through the frequency picker, which rewrites
+ # the rule and regenerates on save. Two moves in a row (27, 24, 22) must
+ # leave the paid October cycle with its one occurrence.
+ test "moving a paid bill's due day twice keeps its cycle to one occurrence" do
+ travel_to Date.new(2026, 10, 28) do
+ sign_in @user
+ october = Date.new(2026, 10, 1)..Date.new(2026, 10, 31)
+
+ patch recurring_transaction_url(@recurring_transaction),
+ params: { recurring_transaction: { frequency_preset: "monthly", frequency_day_of_month: "27" } }
+ paid = @recurring_transaction.recurring_occurrences.find_by!(due_on: Date.new(2026, 10, 27))
+ paid.allocations.create!(allocated_amount: 5, currency: "USD", source: "user_created")
+
+ [ 24, 22 ].each do |day|
+ patch recurring_transaction_url(@recurring_transaction),
+ params: { recurring_transaction: { frequency_preset: "monthly", frequency_day_of_month: day.to_s } }
+
+ assert_redirected_to recurring_transactions_url
+ assert_equal [ paid.id ], @recurring_transaction.recurring_occurrences.where(due_on: october).pluck(:id),
+ "moving the due day to the #{day.ordinalize} adds no second October occurrence"
+ assert_includes @recurring_transaction.recurring_occurrences.pluck(:due_on), Date.new(2026, 11, day)
+ end
+
+ assert_equal 22, @recurring_transaction.reload.expected_day_of_month
+ assert_not_includes @recurring_transaction.recurring_occurrences.pluck(:due_on), Date.new(2026, 11, 24)
+ end
+ end
+
test "update with an unchanged frequency does not rewrite the rules" do
patch recurring_transaction_url(@recurring_transaction),
params: { recurring_transaction: { frequency_preset: "weekly", frequency_weekday: "3" } }
diff --git a/test/models/recurring_transaction/occurrence_generator_test.rb b/test/models/recurring_transaction/occurrence_generator_test.rb
index eed354e8ba..b544e50ab9 100644
--- a/test/models/recurring_transaction/occurrence_generator_test.rb
+++ b/test/models/recurring_transaction/occurrence_generator_test.rb
@@ -50,6 +50,9 @@ class RecurringTransaction::OccurrenceGeneratorTest < ActiveSupport::TestCase
assert_includes remaining.map(&:id), paid.id, "allocated rows survive schedule edits"
assert_not_includes remaining.map(&:id), future.id, "unallocated future rows are rebuilt"
assert_includes remaining.map(&:due_on), Date.new(2026, 8, 20)
+ # 15 days from the paid Sep 5 row is outside the monthly half cycle, so a
+ # deliberate move to another week still gets its own occurrence.
+ assert_includes remaining.map(&:due_on), Date.new(2026, 9, 20)
end
end
@@ -99,4 +102,139 @@ class RecurringTransaction::OccurrenceGeneratorTest < ActiveSupport::TestCase
assert_equal 0, Generator.new(@series).generate!, "adjusted occurrence does not re-insert"
end
end
+
+ # #4300: detection moving a bill's due day by a day regenerates the schedule.
+ # The old cycle's row survives (paid or past due), and it already is this
+ # cycle, so no second occurrence may appear beside it.
+ test "a due-day shift does not add a second occurrence to a paid cycle" do
+ travel_to Date.new(2026, 10, 16) do
+ RecurringTransaction::FrequencyPreset.apply(@series, preset: "monthly", day_of_month: "6")
+ @series.save!
+ paid = @series.recurring_occurrences.reload.find_by!(due_on: Date.new(2026, 10, 6))
+ paid.allocations.create!(allocated_amount: 5, currency: "USD", source: "user_created")
+
+ RecurringTransaction::FrequencyPreset.apply(@series, preset: "monthly", day_of_month: "5")
+ @series.save!
+
+ dues = @series.recurring_occurrences.reload.pluck(:due_on)
+ assert_not_includes dues, Date.new(2026, 10, 5)
+ assert_includes dues, Date.new(2026, 11, 5)
+ end
+ end
+
+ test "a due-day shift does not add a second occurrence beside an unpaid past-due one" do
+ travel_to Date.new(2026, 10, 16) do
+ RecurringTransaction::FrequencyPreset.apply(@series, preset: "monthly", day_of_month: "6")
+ @series.save!
+
+ RecurringTransaction::FrequencyPreset.apply(@series, preset: "monthly", day_of_month: "5")
+ @series.save!
+
+ october = @series.recurring_occurrences.reload.where(due_on: Date.new(2026, 10, 1)..Date.new(2026, 10, 31))
+ assert_equal [ Date.new(2026, 10, 6) ], october.pluck(:due_on)
+ end
+ end
+
+ test "a later due-day shift does not duplicate the cycle either" do
+ travel_to Date.new(2026, 10, 16) do
+ # The fixture is already monthly on the 5th, so saving regenerates nothing.
+ Generator.new(@series).generate!
+ paid = @series.recurring_occurrences.reload.find_by!(due_on: Date.new(2026, 10, 5))
+ paid.allocations.create!(allocated_amount: 5, currency: "USD", source: "user_created")
+
+ RecurringTransaction::FrequencyPreset.apply(@series, preset: "monthly", day_of_month: "6")
+ @series.save!
+
+ dues = @series.recurring_occurrences.reload.pluck(:due_on)
+ assert_not_includes dues, Date.new(2026, 10, 6)
+ assert_includes dues, Date.new(2026, 11, 6)
+ end
+ end
+
+ # Two days of one schedule a few days apart are both real occurrences: only
+ # a row the current schedule no longer produces stands in for a cycle.
+ test "a semimonthly bill on close days keeps both occurrences" do
+ # On Aug 3 the 90-day horizon ends on Nov 1, between the two days, so the
+ # Nov 5 occurrence is added by a later run, with Nov 1 already there.
+ travel_to Date.new(2026, 8, 3) do
+ RecurringTransaction::FrequencyPreset.apply(@series, preset: "semimonthly", day_of_month: "1", second_day_of_month: "5")
+ @series.save!
+ assert_includes @series.recurring_occurrences.reload.pluck(:due_on), Date.new(2026, 11, 1)
+ assert_not_includes @series.recurring_occurrences.pluck(:due_on), Date.new(2026, 11, 5)
+ end
+
+ travel_to Date.new(2026, 8, 8) do
+ Generator.new(@series).generate!
+
+ dues = @series.recurring_occurrences.reload.pluck(:due_on)
+ [ Date.new(2026, 9, 1), Date.new(2026, 9, 5), Date.new(2026, 11, 1), Date.new(2026, 11, 5) ].each do |date|
+ assert_includes dues, date
+ end
+ assert_equal 0, Generator.new(@series).generate!, "steady state inserts nothing"
+ end
+ end
+
+ # A kept row stands in for one occurrence, the nearest, not for every
+ # occurrence within half a cycle: semimonthly days a few days apart are both
+ # within reach of it.
+ test "a due-day shift on a close semimonthly bill keeps the other day" do
+ travel_to Date.new(2026, 10, 3) do
+ RecurringTransaction::FrequencyPreset.apply(@series, preset: "semimonthly", day_of_month: "1", second_day_of_month: "5")
+ @series.save!
+ paid = @series.recurring_occurrences.reload.find_by!(due_on: Date.new(2026, 10, 1))
+ paid.allocations.create!(allocated_amount: 5, currency: "USD", source: "user_created")
+ assert_includes @series.recurring_occurrences.pluck(:due_on), Date.new(2026, 10, 5)
+
+ RecurringTransaction::FrequencyPreset.apply(@series, preset: "semimonthly", day_of_month: "2", second_day_of_month: "5")
+ @series.save!
+
+ dues = @series.recurring_occurrences.reload.pluck(:due_on)
+ assert_not_includes dues, Date.new(2026, 10, 2), "the kept Oct 1 row already is this occurrence"
+ assert_includes dues, Date.new(2026, 10, 5), "the unchanged second day is rebuilt"
+ assert_includes dues, Date.new(2026, 11, 2)
+ assert_includes dues, Date.new(2026, 11, 5)
+ end
+ end
+
+ # #4300 is reached by a hand edit of the due day too, and the reporter moved
+ # it twice (27, then 24, then 22). The second edit must not resurrect a row
+ # beside the paid cycle either, and the later cycles follow the latest day.
+ # update! of the column is the API's edit; the bill form's is in the
+ # controller test.
+ test "two successive due-day edits keep a paid cycle to one occurrence" do
+ travel_to Date.new(2026, 10, 28) do
+ @series.update!(expected_day_of_month: 27)
+ paid = @series.recurring_occurrences.reload.find_by!(due_on: Date.new(2026, 10, 27))
+ paid.allocations.create!(allocated_amount: 5, currency: "USD", source: "user_created")
+ october = Date.new(2026, 10, 1)..Date.new(2026, 10, 31)
+
+ @series.update!(expected_day_of_month: 24)
+ assert_equal [ paid.id ], @series.recurring_occurrences.reload.where(due_on: october).pluck(:id),
+ "the first edit adds no Oct 24 row beside the paid Oct 27 one"
+ assert_includes @series.recurring_occurrences.pluck(:due_on), Date.new(2026, 11, 24)
+
+ @series.update!(expected_day_of_month: 22)
+ assert_equal [ paid.id ], @series.recurring_occurrences.reload.where(due_on: october).pluck(:id),
+ "the second edit adds no Oct 22 or Oct 24 row beside the paid Oct 27 one"
+ dues = @series.recurring_occurrences.pluck(:due_on)
+ assert_includes dues, Date.new(2026, 11, 22)
+ assert_not_includes dues, Date.new(2026, 11, 24), "the first edit's future rows are rebuilt on the new day"
+ end
+ end
+
+ test "two successive due-day edits keep an unpaid past-due cycle to one occurrence" do
+ travel_to Date.new(2026, 10, 28) do
+ @series.update!(expected_day_of_month: 27)
+ past_due = @series.recurring_occurrences.reload.find_by!(due_on: Date.new(2026, 10, 27))
+ october = Date.new(2026, 10, 1)..Date.new(2026, 10, 31)
+
+ @series.update!(expected_day_of_month: 24)
+ @series.update!(expected_day_of_month: 22)
+
+ assert_equal [ past_due.id ], @series.recurring_occurrences.reload.where(due_on: october).pluck(:id)
+ dues = @series.recurring_occurrences.pluck(:due_on)
+ assert_includes dues, Date.new(2026, 11, 22)
+ assert_not_includes dues, Date.new(2026, 11, 24)
+ end
+ end
end