Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix: allow re-linking payment to new bill after original is ended (#3663)

joaocbatista3758cb8aabb8
diff --git a/app/models/recurring_transaction/allocator.rb b/app/models/recurring_transaction/allocator.rb
index 06bb4fd85b..01262c5a6e 100644
--- a/app/models/recurring_transaction/allocator.rb
+++ b/app/models/recurring_transaction/allocator.rb
@@ -284,17 +284,28 @@ class RecurringTransaction
end
end
- # True once any allocation on this entry lacks a source amount, which
- # makes every capacity sum over it meaningless.
+ # Allocations that count toward an entry's capacity: everything except
+ # tombstones from ended series (a deleted or dismissed bill). Counting
+ # those would permanently consume the entry and block re-use on a new
+ # bill (see #3592). Defined once so the capacity checks stay in sync.
+ def active_allocations_for(entry)
+ RecurringAllocation
+ .where(entry: entry)
+ .joins(recurring_occurrence: :recurring_transaction)
+ .where.not(recurring_transactions: { status: "ended" })
+ end
+
+ # True once any allocation on this entry (excluding those from ended
+ # series) lacks a source amount, which makes every capacity sum over it
+ # meaningless.
def unmeasurable_entry?(entry)
- RecurringAllocation.where(entry: entry, source_amount: nil).exists?
+ active_allocations_for(entry).where(source_amount: nil).exists?
end
# How much of the entry is not yet spoken for, in the entry's currency.
def entry_capacity(entry, entry_total)
- already = RecurringAllocation.where(entry: entry).sum(
- "COALESCE(source_amount, allocated_amount)"
- )
+ already = active_allocations_for(entry)
+ .sum("COALESCE(recurring_allocations.source_amount, recurring_allocations.allocated_amount)")
[ entry_total - already, 0 ].max
end
@@ -353,10 +364,11 @@ class RecurringTransaction
# The first such allocation is a judgement the user made about a
# transaction they can see. A second one is a transaction silently paying
# an unbounded number of bills, so an unmeasurable entry takes no more
- # than one.
+ # than one. Allocations from ended series are excluded throughout so a
+ # deleted bill never blocks re-use of the same entry on a replacement.
def guard_entry_capacity!(entry, source_amount)
if source_amount.nil? || unmeasurable_entry?(entry)
- return if RecurringAllocation.where(entry: entry).none?
+ return if active_allocations_for(entry).none?
raise OverAllocationError, I18n.t("recurring_transactions.allocator.unmeasurable",
currency: entry.currency, date: entry.date)
diff --git a/test/models/recurring_transaction/allocator_test.rb b/test/models/recurring_transaction/allocator_test.rb
index 0f6e72ee09..6dedef5295 100644
--- a/test/models/recurring_transaction/allocator_test.rb
+++ b/test/models/recurring_transaction/allocator_test.rb
@@ -366,6 +366,84 @@ class RecurringTransaction::AllocatorTest < ActiveSupport::TestCase
assert_equal 95, occurrence.reload.allocations.sum(:allocated_amount)
end
+ test "a payment can be linked to a new bill after the original bill is ended" do
+ # Reproduces: we-promise/sure#3592
+ # entry_capacity was summing allocations from ended series, making the
+ # entry appear fully spent and blocking re-use on a new bill.
+ payment = entry_for(2000)
+
+ # Link the payment to the original bill's occurrence and close it.
+ @allocator.allocate!(entry: payment)
+ assert @occurrence.reload.paid?
+
+ # End the original series (what the app does when a user "deletes" a bill).
+ @rent.update!(status: "ended")
+
+ # Create a replacement bill for the same obligation.
+ replacement = @family.recurring_transactions.create!(
+ account: @account,
+ name: "Watson Property",
+ amount: 2000,
+ currency: "USD",
+ expected_day_of_month: 29,
+ last_occurrence_date: Date.current,
+ next_expected_date: 1.month.from_now.to_date,
+ status: "active",
+ manual: true,
+ dedup_scope: "replacement"
+ )
+ new_occurrence = replacement.recurring_occurrences.create!(
+ family: @family,
+ original_due_on: (Date.current + 1.month).beginning_of_month + 9,
+ due_on: (Date.current + 1.month).beginning_of_month + 9,
+ currency: "USD"
+ )
+
+ # Linking the same payment to the new bill must succeed.
+ assert_nothing_raised do
+ Allocator.new(new_occurrence).allocate!(entry: payment)
+ end
+
+ assert new_occurrence.reload.paid?
+ end
+
+ test "a payment fully linked to an active bill cannot be re-linked to another active bill" do
+ # The ended-series exclusion must NOT weaken the double-allocation guard for
+ # allocations that belong to a still-active series: a payment already spent
+ # on an active bill stays spent.
+ payment = entry_for(2000)
+
+ @allocator.allocate!(entry: payment)
+ assert @occurrence.reload.paid?
+
+ # A second, still-active bill (the original @rent is left active).
+ other = @family.recurring_transactions.create!(
+ account: @account,
+ name: "Unrelated Active Bill",
+ amount: 2000,
+ currency: "USD",
+ expected_day_of_month: 29,
+ last_occurrence_date: Date.current,
+ next_expected_date: 1.month.from_now.to_date,
+ status: "active",
+ manual: true,
+ dedup_scope: "unrelated"
+ )
+ other_occurrence = other.recurring_occurrences.create!(
+ family: @family,
+ original_due_on: (Date.current + 1.month).beginning_of_month + 9,
+ due_on: (Date.current + 1.month).beginning_of_month + 9,
+ currency: "USD"
+ )
+
+ # The entry is fully consumed by the active allocation, so there is no
+ # capacity left for another active bill.
+ assert_raises(ActiveRecord::RecordInvalid) do
+ Allocator.new(other_occurrence).allocate!(entry: payment)
+ end
+ assert_not other_occurrence.reload.paid?
+ end
+
private
def foreign_entry(amount:, currency:)