Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Keep the native currency in transfer-only daily totals (#3937)

Orange🍊ea326e1c9d35
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 5e6ac0cdd3..246c91ce69 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -174,6 +174,7 @@ module ApplicationHelper
Money.new(number_or_money).format(options)
end
+ # Format each native-currency total independently, preserving order and transfer exclusions.
def totals_by_currency(collection:, money_method:, separator: " | ", negate: false)
collection.group_by(&:currency)
.transform_values { |item| calculate_total(item, money_method, negate) }
@@ -292,6 +293,7 @@ module ApplicationHelper
normalized.downcase
end
+ # Exclude transfers while preserving the original group currency even when its total is zero.
def calculate_total(item, money_method, negate)
# Filter out transfer-type transactions from entries
# Only Entry objects have entryable transactions, Account objects don't
@@ -300,7 +302,8 @@ module ApplicationHelper
i.entryable.is_a?(Transaction) &&
i.entryable.transfer?
end
- total = items.sum(&money_method)
+ # Keep the group's currency when every entry is excluded from the total.
+ total = items.sum(Money.new(0, item.first.currency), &money_method)
negate ? -total : total
end
end
diff --git a/test/controllers/transaction_currency_totals_test.rb b/test/controllers/transaction_currency_totals_test.rb
new file mode 100644
index 0000000000..bb115d0255
--- /dev/null
+++ b/test/controllers/transaction_currency_totals_test.rb
@@ -0,0 +1,22 @@
+require "test_helper"
+
+# Verify native currency totals through the authenticated transaction response.
+class TransactionCurrencyTotalsTest < ActionDispatch::IntegrationTest
+ include EntriesTestHelper
+
+ # Verify the activity response preserves GBP after transfer exclusion empties a day.
+ test "transfer-only daily total displays the transaction currency" do
+ user = users(:family_admin)
+ user.family.update!(currency: "GBP")
+ account = accounts(:depository)
+ account.update!(currency: "GBP")
+ date = Date.current - 2.days
+ create_transaction(account: account, currency: "GBP", amount: 100, date: date, kind: "funds_movement")
+ sign_in user
+
+ get transactions_url, params: { q: { account_ids: [ account.id ], start_date: date.to_s, end_date: date.to_s } }
+
+ assert_response :success
+ assert_select "#entry-group-#{date}-totals", text: "£0.00"
+ end
+end
diff --git a/test/helpers/currency_totals_test.rb b/test/helpers/currency_totals_test.rb
new file mode 100644
index 0000000000..fa08556492
--- /dev/null
+++ b/test/helpers/currency_totals_test.rb
@@ -0,0 +1,44 @@
+require "test_helper"
+
+# Cover transfer exclusions and zero-valued groups without changing currency formatting.
+class CurrencyTotalsTest < ActionView::TestCase
+ include ApplicationHelper
+
+ test "transfer-only groups preserve their native currency for every transfer kind" do
+ Transaction::TRANSFER_KINDS.each do |kind|
+ entry = transaction_entry("GBP", 100, kind)
+ assert_equal "£0.00", total([ entry ])
+ assert_equal "£0.00", total([ entry ], negate: true)
+ end
+ end
+
+ test "mixed currencies retain group order and exclude transfers" do
+ entries = [ transaction_entry("GBP", 100, "funds_movement"),
+ transaction_entry("EUR", 20),
+ transaction_entry("EUR", 300, "cc_payment"),
+ transaction_entry("USD", -5) ]
+
+ assert_equal "£0.00 | €20.00 | -$5.00", total(entries)
+ assert_equal "£0.00 / -€20.00 / $5.00", total(entries, negate: true, separator: " / ")
+ end
+
+ test "offsetting standard transactions keep their currency" do
+ assert_equal "£0.00", total([ transaction_entry("GBP", 20), transaction_entry("GBP", -20) ])
+ end
+
+ test "empty collections and zero account balances retain existing behavior" do
+ assert_equal "", total([])
+ assert_equal "£0.00", totals_by_currency(collection: [ Account.new(currency: "GBP", balance: 0) ], money_method: :balance_money)
+ end
+
+ private
+ # Build an entry with an explicit currency and transaction kind for total regressions.
+ def transaction_entry(currency, amount, kind = "standard")
+ Entry.new(currency: currency, amount: amount, entryable: Transaction.new(kind: kind))
+ end
+
+ # Exercise the public formatter with the same Money values used by the activity views.
+ def total(entries, **options)
+ totals_by_currency(collection: entries, money_method: :amount_money, **options)
+ end
+end
diff --git a/test/system/transaction_currency_totals_test.rb b/test/system/transaction_currency_totals_test.rb
new file mode 100644
index 0000000000..3757a9fffd
--- /dev/null
+++ b/test/system/transaction_currency_totals_test.rb
@@ -0,0 +1,22 @@
+require "application_system_test_case"
+
+# Verify native currency totals in the activity page rendered by a real browser.
+class TransactionCurrencyTotalsSystemTest < ApplicationSystemTestCase
+ include EntriesTestHelper
+
+ # Verify the rendered activity total keeps the account currency through the browser flow.
+ test "a transfer-only day shows a zero total in pounds" do
+ user = users(:family_admin)
+ user.family.update!(currency: "GBP")
+ account = accounts(:depository)
+ account.update!(currency: "GBP")
+ date = Date.current - 2.days
+ create_transaction(account: account, currency: "GBP", amount: 100, date: date, kind: "funds_movement")
+ sign_in user
+
+ visit transactions_url(q: { account_ids: [ account.id ], start_date: date.to_s, end_date: date.to_s })
+
+ assert_selector "#entry-group-#{date}-totals", text: "£0.00"
+ page.save_screenshot(Rails.root.join("tmp/screenshots/transaction-currency-totals.png"))
+ end
+end