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 %> +