Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Convert foreign currency amounts in InvestmentFlowStatement (#5858)

Vincenzo Guzziadb671af7515
diff --git a/app/models/investment_flow_statement.rb b/app/models/investment_flow_statement.rb
index ce90b9ac87..ea31f30785 100644
--- a/app/models/investment_flow_statement.rb
+++ b/app/models/investment_flow_statement.rb
@@ -1,16 +1,8 @@
class InvestmentFlowStatement
include Monetizable
- CONTRIBUTIONS_TOTAL_SQL = Arel.sql(
- "COALESCE(ABS(SUM(CASE WHEN transactions.investment_activity_label = 'Contribution' " \
- "THEN entries.amount ELSE 0 END)), 0)"
- )
- WITHDRAWALS_TOTAL_SQL = Arel.sql(
- "COALESCE(ABS(SUM(CASE WHEN transactions.investment_activity_label = 'Withdrawal' " \
- "THEN entries.amount ELSE 0 END)), 0)"
- )
INVESTMENT_ACCOUNT_TYPES = %w[Investment Crypto].freeze
- private_constant :CONTRIBUTIONS_TOTAL_SQL, :WITHDRAWALS_TOTAL_SQL, :INVESTMENT_ACCOUNT_TYPES
+ private_constant :INVESTMENT_ACCOUNT_TYPES
attr_reader :family, :user
@@ -39,9 +31,22 @@ class InvestmentFlowStatement
scope = scope.where(entries: { account_id: account_ids })
end
+ # Convert each entry into the family currency on its own date; entries whose
+ # currency has no stored rate evaluate to NULL and are skipped by SUM rather
+ # than counted at parity (see ExchangeRate.rate_sql).
+ rate = ExchangeRate.rate_sql(from: "entries.currency", to: ":currency", on: "entries.date")
+ contributions_sql = Transaction.sanitize_sql_array([
+ "COALESCE(ABS(SUM(CASE WHEN transactions.investment_activity_label = 'Contribution' THEN entries.amount * (#{rate}) ELSE 0 END)), 0)",
+ { currency: family.currency }
+ ])
+ withdrawals_sql = Transaction.sanitize_sql_array([
+ "COALESCE(ABS(SUM(CASE WHEN transactions.investment_activity_label = 'Withdrawal' THEN entries.amount * (#{rate}) ELSE 0 END)), 0)",
+ { currency: family.currency }
+ ])
+
contributions, withdrawals = scope.pick(
- CONTRIBUTIONS_TOTAL_SQL,
- WITHDRAWALS_TOTAL_SQL
+ Arel.sql(contributions_sql),
+ Arel.sql(withdrawals_sql)
)
PeriodTotals.new(
diff --git a/test/models/investment_flow_statement_test.rb b/test/models/investment_flow_statement_test.rb
index 4127bf281b..33883bf4e7 100644
--- a/test/models/investment_flow_statement_test.rb
+++ b/test/models/investment_flow_statement_test.rb
@@ -122,6 +122,40 @@ class InvestmentFlowStatementTest < ActiveSupport::TestCase
assert_equal Money.new(0, "USD"), InvestmentFlowStatement.new(family, user: member).period_totals(period: period).contributions
end
+ test "period totals convert foreign currency flows to the family currency" do
+ @family.update!(currency: "GBP")
+ period = Period.custom(start_date: Date.new(2026, 3, 1), end_date: Date.new(2026, 3, 31))
+
+ ExchangeRate.create!(from_currency: "USD", to_currency: "GBP", date: Date.new(2026, 3, 5), rate: 0.8)
+ ExchangeRate.create!(from_currency: "SEK", to_currency: "GBP", date: Date.new(2026, 3, 1), rate: 0.075)
+
+ create_flow(label: "Contribution", amount: -100, date: Date.new(2026, 3, 5), currency: "GBP")
+ create_flow(label: "Contribution", amount: -100, date: Date.new(2026, 3, 5), currency: "USD")
+ create_flow(label: "Contribution", amount: -1000, date: Date.new(2026, 3, 10), currency: "SEK")
+ create_flow(label: "Withdrawal", amount: 50, date: Date.new(2026, 3, 5), currency: "USD")
+
+ totals = InvestmentFlowStatement.new(@family, user: @user).period_totals(period: period)
+
+ assert_equal Money.new(255, "GBP"), totals.contributions
+ assert_equal Money.new(40, "GBP"), totals.withdrawals
+ assert_equal Money.new(215, "GBP"), totals.net_flow
+ end
+
+ test "period totals omit foreign currency flows without a stored exchange rate" do
+ @family.update!(currency: "GBP")
+ period = Period.custom(start_date: Date.new(2026, 4, 1), end_date: Date.new(2026, 4, 30))
+
+ create_flow(label: "Contribution", amount: -100, date: Date.new(2026, 4, 5), currency: "GBP")
+ create_flow(label: "Contribution", amount: -10000, date: Date.new(2026, 4, 5), currency: "JPY")
+ create_flow(label: "Withdrawal", amount: 5000, date: Date.new(2026, 4, 5), currency: "JPY")
+
+ totals = InvestmentFlowStatement.new(@family, user: @user).period_totals(period: period)
+
+ assert_equal Money.new(100, "GBP"), totals.contributions
+ assert_equal Money.new(0, "GBP"), totals.withdrawals
+ assert_equal Money.new(100, "GBP"), totals.net_flow
+ end
+
private
def create_matched_contribution(family, from:, to:)
inflow = to.entries.create!(
@@ -139,12 +173,12 @@ class InvestmentFlowStatementTest < ActiveSupport::TestCase
inflow
end
- def create_flow(label:, amount:, date:)
+ def create_flow(label:, amount:, date:, currency: "USD")
@account.entries.create!(
name: label,
amount: amount,
date: date,
- currency: "USD",
+ currency: currency,
entryable: Transaction.new(
kind: "standard",
investment_activity_label: label