mirror of
https://github.com/we-promise/sure.git
synced 2026-06-01 00:39:01 +00:00
* 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.
320 lines
10 KiB
Ruby
320 lines
10 KiB
Ruby
require "digest/md5"
|
|
|
|
class InvestmentStatement
|
|
include Monetizable
|
|
|
|
monetize :total_contributions, :total_dividends, :total_interest, :unrealized_gains
|
|
|
|
attr_reader :family, :user
|
|
|
|
def initialize(family, user: nil)
|
|
@family = family
|
|
@user = user || Current.user
|
|
end
|
|
|
|
# Get totals for a specific period
|
|
def totals(period: Period.current_month)
|
|
trades_in_period = family.trades
|
|
.joins(:entry)
|
|
.where(entries: { date: period.date_range, account_id: investment_account_ids })
|
|
|
|
result = totals_query(trades_scope: trades_in_period)
|
|
|
|
PeriodTotals.new(
|
|
contributions: Money.new(result[:contributions], family.currency),
|
|
withdrawals: Money.new(result[:withdrawals], family.currency),
|
|
dividends: Money.new(result[:dividends], family.currency),
|
|
interest: Money.new(result[:interest], family.currency),
|
|
trades_count: result[:trades_count],
|
|
currency: family.currency
|
|
)
|
|
end
|
|
|
|
# Net contributions (contributions - withdrawals)
|
|
def net_contributions(period: Period.current_month)
|
|
t = totals(period: period)
|
|
t.contributions - t.withdrawals
|
|
end
|
|
|
|
# Total portfolio value across all investment accounts
|
|
def portfolio_value
|
|
investment_accounts.sum { |a| convert_to_family_currency(a.balance, a.currency) }
|
|
end
|
|
|
|
def portfolio_value_money
|
|
Money.new(portfolio_value, family.currency)
|
|
end
|
|
|
|
# Total cash in investment accounts
|
|
def cash_balance
|
|
investment_accounts.sum { |a| convert_to_family_currency(a.cash_balance, a.currency) }
|
|
end
|
|
|
|
def cash_balance_money
|
|
Money.new(cash_balance, family.currency)
|
|
end
|
|
|
|
# Total holdings value
|
|
def holdings_value
|
|
portfolio_value - cash_balance
|
|
end
|
|
|
|
def holdings_value_money
|
|
Money.new(holdings_value, family.currency)
|
|
end
|
|
|
|
# All current holdings across investment accounts. Holdings are returned in
|
|
# their native currency; callers that aggregate across accounts must convert
|
|
# to family currency via convert_to_family_currency.
|
|
def current_holdings
|
|
return Holding.none unless investment_accounts.any?
|
|
|
|
# Get the latest holding for each security per account
|
|
Holding
|
|
.where(account_id: investment_account_ids)
|
|
.where.not(qty: 0)
|
|
.where(
|
|
id: Holding
|
|
.where(account_id: investment_account_ids)
|
|
.select("DISTINCT ON (holdings.account_id, holdings.security_id) holdings.id")
|
|
.order(Arel.sql("holdings.account_id, holdings.security_id, holdings.date DESC"))
|
|
)
|
|
.includes(:security, :account)
|
|
end
|
|
|
|
# Top holdings by family-currency value
|
|
def top_holdings(limit: 5)
|
|
current_holdings
|
|
.to_a
|
|
.sort_by { |h| -convert_to_family_currency(h.amount, h.currency) }
|
|
.first(limit)
|
|
end
|
|
|
|
# Portfolio allocation by security. Weights and amounts are computed in the
|
|
# family's currency so cross-currency holdings compare correctly.
|
|
def allocation
|
|
converted = current_holdings.to_a.map do |holding|
|
|
[ holding, convert_to_family_currency(holding.amount, holding.currency) ]
|
|
end
|
|
|
|
total = converted.sum { |_, value| value }
|
|
return [] if total.zero?
|
|
|
|
converted
|
|
.sort_by { |_, value| -value }
|
|
.map do |holding, value|
|
|
HoldingAllocation.new(
|
|
security: holding.security,
|
|
amount: Money.new(value, family.currency),
|
|
weight: (value / total * 100).round(2),
|
|
trend: holding.trend
|
|
)
|
|
end
|
|
end
|
|
|
|
# Unrealized gains across all holdings, summed in family currency
|
|
def unrealized_gains
|
|
current_holdings.sum do |holding|
|
|
trend = holding.trend
|
|
trend ? convert_to_family_currency(trend.value, holding.currency) : 0
|
|
end
|
|
end
|
|
|
|
# Total contributions (all time) - returns numeric for monetize
|
|
def total_contributions
|
|
all_time_totals.contributions&.amount || 0
|
|
end
|
|
|
|
# Total dividends (all time) - returns numeric for monetize
|
|
def total_dividends
|
|
all_time_totals.dividends&.amount || 0
|
|
end
|
|
|
|
# Total interest (all time) - returns numeric for monetize
|
|
def total_interest
|
|
all_time_totals.interest&.amount || 0
|
|
end
|
|
|
|
def unrealized_gains_trend
|
|
holdings = current_holdings.to_a
|
|
return nil if holdings.empty?
|
|
|
|
# Only include holdings with known cost basis in the calculation
|
|
holdings_with_cost_basis = holdings.select(&:avg_cost)
|
|
return nil if holdings_with_cost_basis.empty?
|
|
|
|
current = holdings_with_cost_basis.sum do |h|
|
|
convert_to_family_currency(h.amount, h.currency)
|
|
end
|
|
previous = holdings_with_cost_basis.sum do |h|
|
|
convert_to_family_currency(h.qty * h.avg_cost.amount, h.currency)
|
|
end
|
|
|
|
Trend.new(
|
|
current: Money.new(current, family.currency),
|
|
previous: Money.new(previous, family.currency)
|
|
)
|
|
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|
|
|
t = h.day_change
|
|
next nil unless t
|
|
curr = t.current.is_a?(Money) ? t.current.amount : t.current
|
|
prev = t.previous.is_a?(Money) ? t.previous.amount : t.previous
|
|
[
|
|
convert_to_family_currency(curr, h.currency),
|
|
convert_to_family_currency(prev, h.currency)
|
|
]
|
|
end
|
|
|
|
return nil if changes.empty?
|
|
|
|
Trend.new(
|
|
current: Money.new(changes.sum { |c, _| c }, family.currency),
|
|
previous: Money.new(changes.sum { |_, p| p }, family.currency)
|
|
)
|
|
end
|
|
|
|
# Investment accounts
|
|
def investment_accounts
|
|
@investment_accounts ||= begin
|
|
scope = family.accounts.visible.where(accountable_type: %w[Investment Crypto])
|
|
scope = scope.included_in_finances_for(user) if user
|
|
scope
|
|
end
|
|
end
|
|
|
|
private
|
|
# Today's rates for every currency present on the family's investment
|
|
# accounts and their holdings. Mirrors BalanceSheet::AccountTotals#exchange_rates.
|
|
def exchange_rates
|
|
@exchange_rates ||= begin
|
|
account_currencies = investment_accounts.map(&:currency)
|
|
holding_currencies = Holding.where(account_id: investment_account_ids).distinct.pluck(:currency)
|
|
foreign = (account_currencies + holding_currencies)
|
|
.compact
|
|
.uniq
|
|
.reject { |c| c == family.currency }
|
|
ExchangeRate.rates_for(foreign, to: family.currency, date: Date.current)
|
|
end
|
|
end
|
|
|
|
# Unwrap Money first because this codebase's Money (lib/money.rb) ignores
|
|
# the currency arg of `Money.new` when the payload is already a Money, and
|
|
# `Money * numeric` preserves the source currency — so multiplying a
|
|
# foreign-currency Money by a rate would FX-scale the amount but keep the
|
|
# wrong currency label, corrupting downstream sums.
|
|
def convert_to_family_currency(amount, from_currency)
|
|
return amount if amount.nil?
|
|
numeric = amount.is_a?(Money) ? amount.amount : amount
|
|
return numeric if from_currency == family.currency
|
|
rate = exchange_rates[from_currency] || 1
|
|
numeric * rate
|
|
end
|
|
|
|
def all_time_totals
|
|
@all_time_totals ||= totals(period: Period.all_time)
|
|
end
|
|
|
|
PeriodTotals = Data.define(:contributions, :withdrawals, :dividends, :interest, :trades_count, :currency) do
|
|
def net_flow
|
|
contributions - withdrawals
|
|
end
|
|
|
|
def total_income
|
|
dividends + interest
|
|
end
|
|
end
|
|
|
|
HoldingAllocation = Data.define(:security, :amount, :weight, :trend)
|
|
|
|
def investment_account_ids
|
|
@investment_account_ids ||= investment_accounts.pluck(:id)
|
|
end
|
|
|
|
def totals_query(trades_scope:)
|
|
sql_hash = Digest::MD5.hexdigest(trades_scope.to_sql)
|
|
|
|
Rails.cache.fetch([
|
|
"investment_statement", "totals_query", family.id, user&.id, sql_hash, family.entries_cache_version
|
|
]) { Totals.new(family, trades_scope: trades_scope).call }
|
|
end
|
|
|
|
def monetizable_currency
|
|
family.currency
|
|
end
|
|
end
|