From 52083d5774d09c58f45cd585af28e0472a67cb86 Mon Sep 17 00:00:00 2001 From: CrossDrain <32982516+CrossDrain@users.noreply.github.com> Date: Thu, 28 May 2026 12:49:04 +0000 Subject: [PATCH] feat(reports): add Period Return card to Investment Performance (#1962) * feat(reports): add Period Return card to Investment Performance tab Surfaces market-only return (absolute + %) for the selected period using net_market_flows from the balances table, excluding contributions and withdrawals. Appears in both the interactive report and the print view. * docs: remove TODOS.md; fold FX fallback caveat into PR description The single V2 item (Period Return's 1:1 FX fallback on missing rates) is now documented under Known Limitations in the PR description, so a tracked file in the repo root is redundant. * fix(investment_statement): align start_value denominator scope and FX handling Add status filter to match absolute_return, and move FX conversion into SQL so pre-period balances are found even when an account's currency was changed after balances were recorded. --- app/controllers/reports_controller.rb | 1 + app/models/investment_statement.rb | 71 +++++++++++++++++++ .../reports/_investment_performance.html.erb | 18 ++++- app/views/reports/print.html.erb | 11 +++ config/locales/views/reports/en.yml | 2 + test/models/investment_statement_test.rb | 45 ++++++++++++ 6 files changed, 147 insertions(+), 1 deletion(-) diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 26a03f23d..9c5bd46a5 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -471,6 +471,7 @@ class ReportsController < ApplicationController has_investments: true, portfolio_value: investment_statement.portfolio_value_money, unrealized_trend: investment_statement.unrealized_gains_trend, + period_return_trend: investment_statement.period_return_trend(period: @period), period_contributions: period_totals.contributions, period_withdrawals: period_totals.withdrawals, top_holdings: investment_statement.top_holdings(limit: 5), diff --git a/app/models/investment_statement.rb b/app/models/investment_statement.rb index e4f79fee1..bb7a836a9 100644 --- a/app/models/investment_statement.rb +++ b/app/models/investment_statement.rb @@ -156,6 +156,77 @@ class InvestmentStatement ) end + def period_return_trend(period: Period.current_month) + currency = family.currency + account_ids = investment_account_ids + return nil if account_ids.empty? + + absolute_return = ActiveRecord::Base.connection.select_value( + ActiveRecord::Base.sanitize_sql_array([ + <<~SQL.squish, + SELECT COALESCE(SUM(b.net_market_flows * COALESCE(er.rate, 1)), 0) + FROM balances b + JOIN accounts a ON a.id = b.account_id + LEFT JOIN exchange_rates er ON ( + er.date = b.date + AND er.from_currency = b.currency + AND er.to_currency = :currency + ) + WHERE a.id IN (:account_ids) + AND a.family_id = :family_id + AND a.status IN ('draft', 'active') + AND b.date BETWEEN :start_date AND :end_date + SQL + { + currency: currency, + account_ids: account_ids, + family_id: family.id, + start_date: period.date_range.begin, + end_date: period.date_range.end + } + ]) + ).to_d + + period_start = period.date_range.begin + + # Single query for all accounts' most recent pre-period balance (strict < to avoid + # double-counting the first day's net_market_flows in both the denominator and absolute_return). + # FX conversion is done in SQL (matching absolute_return) so balance rows whose currency + # differs from the account's current currency (e.g. after a currency change) are still picked up. + start_value = ActiveRecord::Base.connection.select_value( + ActiveRecord::Base.sanitize_sql_array([ + <<~SQL.squish, + SELECT COALESCE(SUM(b.end_balance * COALESCE(er.rate, 1)), 0) + FROM accounts a + INNER JOIN balances b ON b.account_id = a.id + LEFT JOIN exchange_rates er ON ( + er.date = :period_start + AND er.from_currency = b.currency + AND er.to_currency = :currency + ) + INNER JOIN ( + SELECT b2.account_id, MAX(b2.date) AS max_date + FROM balances b2 + WHERE b2.account_id IN (:account_ids) + AND b2.date < :period_start + GROUP BY b2.account_id + ) latest ON latest.account_id = b.account_id AND b.date = latest.max_date + WHERE a.id IN (:account_ids) + AND a.family_id = :family_id + AND a.status IN ('draft', 'active') + SQL + { account_ids: account_ids, period_start: period_start, family_id: family.id, currency: currency } + ]) + ).to_d + + return nil if start_value.zero? + + Trend.new( + current: Money.new(start_value + absolute_return, currency), + previous: Money.new(start_value, currency) + ) + end + # Day change across portfolio, summed in family currency def day_change changes = current_holdings.to_a.filter_map do |h| diff --git a/app/views/reports/_investment_performance.html.erb b/app/views/reports/_investment_performance.html.erb index 23a922ebc..96f9fc8e6 100644 --- a/app/views/reports/_investment_performance.html.erb +++ b/app/views/reports/_investment_performance.html.erb @@ -3,7 +3,7 @@ <% if investment_metrics[:has_investments] %>
+ <%= format_money(Money.new(investment_metrics[:period_return_trend].value, Current.family.currency)) %> + (<%= investment_metrics[:period_return_trend].percent_formatted %>) +
+ <% else %> +<%= t("reports.investment_performance.no_data") %>
+ <% end %> +