Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(bills): explain why a used-up transaction cannot pay a bill (#4376)

Anthonyc7a3748aa87d
diff --git a/app/controllers/recurring_allocations_controller.rb b/app/controllers/recurring_allocations_controller.rb
index 01473017f6..c6d67e5998 100644
--- a/app/controllers/recurring_allocations_controller.rb
+++ b/app/controllers/recurring_allocations_controller.rb
@@ -113,9 +113,13 @@ class RecurringAllocationsController < ApplicationController
def allocation_error_message(error)
case error
+ # The two subclasses before OverAllocationError, which would otherwise
+ # claim them.
+ when RecurringTransaction::Allocator::AlreadyAllocatedError,
+ ActiveRecord::RecordNotUnique then t("recurring_allocations.already_allocated")
+ when RecurringTransaction::Allocator::EntryExhaustedError then t("recurring_allocations.entry_exhausted")
when RecurringTransaction::Allocator::OverAllocationError then t("recurring_allocations.over_allocation")
when RecurringTransaction::Allocator::MissingRateError then t("recurring_allocations.missing_rate")
- when ActiveRecord::RecordNotUnique then t("recurring_allocations.already_allocated")
else t("recurring_allocations.invalid")
end
end
diff --git a/app/models/recurring_transaction/allocator.rb b/app/models/recurring_transaction/allocator.rb
index add4cb2a07..662c90d456 100644
--- a/app/models/recurring_transaction/allocator.rb
+++ b/app/models/recurring_transaction/allocator.rb
@@ -16,6 +16,12 @@ class RecurringTransaction
# they sum to the full expected amount.
class Allocator
class OverAllocationError < StandardError; end
+ # Nothing of the entry is left: other allocations already take all of it.
+ # Subclasses, so every caller that refuses over-allocation refuses these
+ # too. AlreadyAllocatedError is the case where this occurrence is the one
+ # holding the entry.
+ class EntryExhaustedError < OverAllocationError; end
+ class AlreadyAllocatedError < OverAllocationError; end
class MissingRateError < StandardError; end
# Postgres keeps single-key and two-key advisory locks in separate spaces,
@@ -351,6 +357,21 @@ class RecurringTransaction
capacity = entry_capacity(entry, entry.amount.abs)
+ # The default amount is bounded by what the entry has left, so an
+ # exhausted entry defaults to zero. Zero passes the comparison below
+ # and used to surface only as the allocation's own "greater than 0"
+ # validation, which the user read as an unexplained failure.
+ unless capacity.positive?
+ # Used up by THIS occurrence means a repeated link (a double submit,
+ # a stale tab). With capacity left the unique index reports that as
+ # already allocated, and "other payments due" would be untrue here.
+ if occurrence.allocations.exists?(entry: entry)
+ raise AlreadyAllocatedError, I18n.t("recurring_transactions.allocator.already_allocated")
+ end
+
+ raise EntryExhaustedError, I18n.t("recurring_transactions.allocator.entry_exhausted")
+ end
+
if source_amount > capacity + RecurringOccurrence::CLOSE_EPSILON
raise OverAllocationError, I18n.t("recurring_transactions.allocator.over_allocated",
amount: source_amount, capacity: capacity,
diff --git a/config/locales/models/recurring_transaction/en.yml b/config/locales/models/recurring_transaction/en.yml
index d92aada6cd..a186c72ff6 100644
--- a/config/locales/models/recurring_transaction/en.yml
+++ b/config/locales/models/recurring_transaction/en.yml
@@ -5,6 +5,8 @@ en:
missing_rate: "no exchange rate from %{from} to %{to}; enter an amount explicitly"
unmeasurable: "this transaction carries an allocation that cannot be converted to %{currency}, so what is left of it cannot be measured. Add an exchange rate for %{date} or remove the existing payment first"
over_allocated: "allocating %{amount} exceeds the transaction's remaining %{capacity} %{currency}"
+ entry_exhausted: "this transaction's full amount is already applied to other payments due"
+ already_allocated: "this transaction is already applied to this bill"
exceeds_remaining: "%{amount} exceeds the %{remaining} remaining on this cycle"
activerecord:
attributes:
diff --git a/config/locales/models/recurring_transaction/fr.yml b/config/locales/models/recurring_transaction/fr.yml
index f9539603e7..72e22726d6 100644
--- a/config/locales/models/recurring_transaction/fr.yml
+++ b/config/locales/models/recurring_transaction/fr.yml
@@ -27,6 +27,8 @@ fr:
merchant_or_name_required: Le commerçant ou le nom doit être présent
recurring_transactions:
allocator:
+ already_allocated: cette transaction est déjà affectée à cette facture
+ entry_exhausted: le montant de cette transaction est déjà entièrement affecté à d'autres échéances
exceeds_remaining: "%{amount} dépasse les %{remaining} restants sur ce cycle"
missing_rate: aucun taux de change de %{from} vers %{to} ; saisissez un montant explicitement
over_allocated: affecter %{amount} dépasse les %{capacity} %{currency} restants sur la transaction
diff --git a/config/locales/views/recurring_allocations/en.yml b/config/locales/views/recurring_allocations/en.yml
index 409454936d..60cf48a72a 100644
--- a/config/locales/views/recurring_allocations/en.yml
+++ b/config/locales/views/recurring_allocations/en.yml
@@ -4,6 +4,7 @@ en:
over_allocation: That would allocate more than the transaction's amount
missing_rate: No exchange rate available; enter the amount explicitly
already_allocated: That transaction is already applied to this bill
+ entry_exhausted: That transaction's full amount is already applied to other payments due
invalid: The payment could not be recorded
create:
success: Payment applied
diff --git a/config/locales/views/recurring_allocations/fr.yml b/config/locales/views/recurring_allocations/fr.yml
index d076b2b9e3..d739e50546 100644
--- a/config/locales/views/recurring_allocations/fr.yml
+++ b/config/locales/views/recurring_allocations/fr.yml
@@ -8,6 +8,7 @@ fr:
success: Paiement affecté
destroy:
success: Paiement délié
+ entry_exhausted: Le montant de cette transaction est déjà entièrement affecté à d'autres échéances
invalid: Le paiement n'a pas pu être enregistré
missing_rate: Aucun taux de change disponible ; saisissez le montant explicitement
over_allocation: Cela affecterait plus que le montant de la transaction
diff --git a/test/controllers/recurring_occurrences_controller_test.rb b/test/controllers/recurring_occurrences_controller_test.rb
index f576d40232..0da1eabb2b 100644
--- a/test/controllers/recurring_occurrences_controller_test.rb
+++ b/test/controllers/recurring_occurrences_controller_test.rb
@@ -403,6 +403,39 @@ class RecurringOccurrencesControllerTest < ActionDispatch::IntegrationTest
assert_equal I18n.t("recurring_allocations.over_allocation"), flash[:alert]
end
+ # The picker keeps offering entries other cycles have used up (one charge
+ # can pay two bills), so linking one must say why it is refused.
+ test "linking a transaction another cycle already used up says so" do
+ entry = accounts(:depository).entries.create!(
+ date: Date.current, amount: 10, currency: "USD", name: "Netflix charge",
+ entryable: Transaction.new(merchant: merchants(:netflix))
+ )
+ earlier = @series.recurring_occurrences.create!(
+ family: @family, original_due_on: Date.current - 25, due_on: Date.current - 25, currency: "USD"
+ )
+ RecurringTransaction::Allocator.new(earlier).allocate!(entry: entry, amount: 10)
+
+ post recurring_occurrence_allocations_url(@occurrence, entry_id: entry.id)
+
+ assert_equal 0, @occurrence.allocations.count
+ assert_equal I18n.t("recurring_allocations.entry_exhausted"), flash[:alert]
+ end
+
+ # A double submit, or a tab left open after the first link went through.
+ test "linking again a transaction this cycle already used up says it is on this bill" do
+ entry = accounts(:depository).entries.create!(
+ date: Date.current, amount: 10, currency: "USD", name: "Netflix charge",
+ entryable: Transaction.new(merchant: merchants(:netflix))
+ )
+ post recurring_occurrence_allocations_url(@occurrence, entry_id: entry.id)
+ assert_equal 1, @occurrence.allocations.count
+
+ post recurring_occurrence_allocations_url(@occurrence, entry_id: entry.id)
+
+ assert_equal 1, @occurrence.allocations.count
+ assert_equal I18n.t("recurring_allocations.already_allocated"), flash[:alert]
+ end
+
# Sharing is per account. A member with no share on the brokerage must not be
# able to settle a bill with a transaction from it, which would both spend an
# obligation against money they cannot see and echo the charge back to them.
diff --git a/test/models/recurring_transaction/allocator_test.rb b/test/models/recurring_transaction/allocator_test.rb
index eacbd674ab..551a496d14 100644
--- a/test/models/recurring_transaction/allocator_test.rb
+++ b/test/models/recurring_transaction/allocator_test.rb
@@ -182,6 +182,32 @@ class RecurringTransaction::AllocatorTest < ActiveSupport::TestCase
end
end
+ test "an entry another occurrence already uses up is refused as exhausted" do
+ lump = entry_for(500)
+ @allocator.allocate!(entry: lump)
+ free_date = (Date.current + 2.months).beginning_of_month + 9
+ september = @rent.recurring_occurrences.create!(
+ family: @family, original_due_on: free_date, due_on: free_date, currency: "USD"
+ )
+
+ # With no amount the default is what the entry has left, which is zero.
+ # That used to reach the allocation's "greater than 0" validation instead.
+ assert_raises Allocator::EntryExhaustedError do
+ Allocator.new(september).allocate!(entry: lump)
+ end
+ assert_empty september.allocations
+ end
+
+ test "relinking an entry this occurrence already uses up is a duplicate, not exhaustion" do
+ lump = entry_for(500)
+ @allocator.allocate!(entry: lump)
+
+ assert_raises Allocator::AlreadyAllocatedError do
+ @allocator.allocate!(entry: lump)
+ end
+ assert_equal 1, @occurrence.allocations.where(entry: lump).count
+ end
+
test "the same entry cannot be allocated twice to one occurrence" do
# Partial amounts so the capacity guard passes; the unique index is the
# backstop this test pins.