Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
43.1 KB
Raw Blame
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)
    account_ids = totals_account_ids

    result = totals_query(account_ids: account_ids, date_range: period.date_range)

    PeriodTotals.new(
      contributions: Money.new(result[:contributions], family.currency),
      withdrawals: Money.new(result[:withdrawals], family.currency),
      buys: Money.new(result[:buys], family.currency),
      sells: Money.new(result[:sells], 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.
  #
  # Memoized: top_holdings, allocation, unrealized_gains, unrealized_gains_trend,
  # and day_change each call this, so an unmemoized version ran the same
  # DISTINCT ON query up to 5x per dashboard/report request.
  def current_holdings
    @current_holdings ||= Holding::CurrentForInvestmentAccounts
      .new(investment_account_ids)
      .relation
      .includes(:security, :account)
  end

  # current_holdings as an Array, with the average-cost fallback for every
  # holding that has no stored cost basis computed in one query and handed
  # to the holding (Holding.preload_avg_costs), so the roll-up's trends, the
  # unrealized gains and the holdings table do not run
  # Holding#calculate_avg_cost's two queries per holding. Memoized.
  def holdings_with_avg_costs
    @holdings_with_avg_costs ||= current_holdings.to_a.tap { |holdings| Holding.preload_avg_costs(holdings) }
  end

  # Top investments rolled up by security across accounts, ranked by
  # family-currency value. Weight is % of total portfolio (including cash).
  # Presence is gated on holdings totals — not Account#balance — so a stale
  # zero portfolio_value still surfaces real positions.
  def top_holdings(limit: 5)
    rolled_up = holdings_rolled_up_by_security
    return [] if rolled_up.empty?

    holdings_total = rolled_up.sum { |_, value, _| value }
    return [] if holdings_total.zero?

    total = weight_denominator(holdings_total)

    # Rank/limit on value first; only then compute cost-basis trends for the
    # rows that will be rendered. The cost basis behind them is preloaded for
    # every holding in one query (holdings_with_avg_costs).
    rolled_up
      .first(limit)
      .map do |security, value, holdings|
        HoldingAllocation.new(
          security: security,
          amount: Money.new(value, family.currency),
          weight: (value / total * 100).round(2),
          trend: combined_holding_trend(holdings)
        )
      end
  end

  # Portfolio allocation by security (rolled up across accounts), plus one
  # cash row for the part of each account's balance no holding accounts for,
  # so the weights sum to 100 and every security carries the same weight here
  # as in #top_holdings.
  #
  # The cash row is the account cash every grouping files
  # (`account_cash_total`): per account, balance less holdings, positive
  # only. Not Account#cash_balance, which can be stale or default to the whole
  # balance; and not one residual across the portfolio, which would let one
  # account's margin net against another account's cash.
  def allocation
    rolled_up = holdings_rolled_up_by_security
    holdings_total = rolled_up.sum { |_, value, _| value }
    total = weight_denominator(holdings_total)
    return [] if total.zero?

    rows = rolled_up.map do |security, value, holdings|
      HoldingAllocation.new(
        security: security,
        amount: Money.new(value, family.currency),
        weight: (value / total * 100).round(2),
        trend: combined_holding_trend(holdings)
      )
    end

    cash = account_cash_total
    if cash.positive?
      rows << HoldingAllocation.new(
        security: nil,
        amount: Money.new(cash, family.currency),
        weight: (cash / total * 100).round(2),
        trend: nil
      )
    end

    rows
  end

  # Allocation of the portfolio grouped by a classification column of the
  # security, by :account, :currency or :kind (cash, crypto, standard), or by
  # :security (the #allocation roll-up).
  # Every grouping measures the same portfolio: the holdings plus each
  # account's cash -- its balance less its holdings, positive only
  # (`positive_account_cash`) -- placed where the grouping says cash belongs.
  # So every grouping and every level of the drill-down sums to the same
  # total, and weights sum to 100 within rounding. Segments are sorted largest
  # first. Amounts convert to the family currency at the latest rate; a
  # currency with no rate counts 1:1 (`convert_to_family_currency`).
  ALLOCATION_GROUPINGS = %w[security asset_class asset_sub_class sector region account currency kind].freeze

  # The bucket a holding falls into when the column it is grouped by is empty.
  # It is a real segment, not a gap, so a portfolio of unclassified securities
  # is told it has nothing classified yet rather than handed an empty list.
  UNCLASSIFIED = "unclassified".freeze

  # Where account cash and cash securities are filed: the asset class and the
  # sub-class a cash balance belongs to. Values from Security::ASSET_CLASSES
  # and Security::ASSET_SUB_CLASSES.
  CASH_ASSET_CLASS = "liquidity".freeze
  CASH_SUB_CLASS = "cash".freeze

  # One level down from a classification segment: asset class -> sub-class ->
  # positions. Security, sector, region, account, currency and kind do not
  # nest, so they return [].
  #
  # `parent` is the asset class the sub-class was reached through. A sub-class
  # name such as `unclassified` repeats across asset classes, so without it the
  # bottom level would list other classes' positions too.
  def allocation_children(by, bucket, parent: nil)
    case by.to_s
    when "asset_class" then allocation_sub_classes_within(bucket)
    when "asset_sub_class" then allocation_holdings_within(:asset_sub_class, bucket, CASH_SUB_CLASS, parent: parent)
    else []
    end
  end

  # Anything that is not another grouping, a missing one included, is the
  # security roll-up, so a grouping name taken from a query string cannot raise
  # and a request without one shows the default.
  def allocation_by(by)
    case by.to_s
    when "account" then allocation_by_account
    when "currency" then allocation_by_currency
    when "kind" then allocation_by_kind
    when "asset_class" then allocation_by_classification(:asset_class, cash_bucket: CASH_ASSET_CLASS)
    when "asset_sub_class" then allocation_by_classification(:asset_sub_class, cash_bucket: CASH_SUB_CLASS)
    when "sector" then allocation_by_classification(:sector)
    when "region" then allocation_by_classification(:region)
    else allocation_by_security
    end
  end

  # Every security the family holds as one row, with its positions across
  # accounts, sorted for a holdings table. Built from the preloaded
  # current_holdings (holdings_with_avg_costs) and previous_holdings, so the
  # query count does not grow with the number of holdings.
  #
  # Cost basis and unrealised P&L cover the positions whose Holding#avg_cost
  # is known -- a stored cost basis, or the average of the purchases -- and a
  # row with any position outside them is flagged missing_cost_basis. Weights
  # share top_holdings' denominator.
  #
  # sort: one of HOLDINGS_SORT_KEYS; dir: "asc" or "desc". Each falls back on
  # its own -- an unknown sort to "value", an unknown dir to "desc" -- so the
  # query string cannot raise and a valid half of it is kept.
  HOLDINGS_SORT_KEYS = %w[value weight return day_change name].freeze
  HOLDINGS_SORT_DIRECTIONS = %w[asc desc].freeze

  # The [sort, dir] #holdings_table_rows actually applies, so a caller can
  # show the table's headers the order its rows are in.
  def self.holdings_sort(sort, dir)
    [
      HOLDINGS_SORT_KEYS.include?(sort.to_s) ? sort.to_s : "value",
      HOLDINGS_SORT_DIRECTIONS.include?(dir.to_s) ? dir.to_s : "desc"
    ]
  end

  def holdings_table_rows(sort: "value", dir: "desc")
    rolled_up = holdings_rolled_up_by_security
    return [] if rolled_up.empty?

    total = weight_denominator(rolled_up.sum { |_, value, _| value })

    rows = rolled_up.map do |security, value, positions|
      build_holdings_table_row(security, value, positions, total)
    end

    sort_holdings_table_rows(rows, sort: sort, dir: dir)
  end

  STALE_PRICE_AFTER_DAYS = 5
  DATA_QUALITY_KINDS = %i[missing_cost_basis stale_price provider negative_position].freeze
  DataQualityIssue = Data.define(:kind, :holding, :security, :detail, :account)

  # Holdings the user should look at before trusting the figures: no cost
  # basis anywhere (Holding#avg_cost is nil: nothing stored and nothing the
  # trades can compute, by the same rules the holdings tab applies), a
  # provider that is not healthy, or, where the provider is healthy, a last
  # quote dated more than STALE_PRICE_AFTER_DAYS before as_of or none on or
  # before it. A quote is a non-zero price row, or the price a provider
  # wrote on a holding it synced. Cash securities are never flagged. Issues
  # sort by kind, ticker, security and account, so ties keep their order
  # between requests. The cost bases come from the batched preload and the
  # quote dates from two queries, so the list is bounded regardless of the
  # number of holdings. A position that went below zero on any day
  # (Account#negative_positions, the account page's warning) is flagged even
  # once it recovered, so it reads a few queries per investment account.
  # `as_of` is the price cut-off only; the holdings are today's.
  def data_quality_issues(as_of: Date.current)
    as_of = as_of.to_date
    holdings = holdings_with_avg_costs

    issues = []

    investment_accounts.each do |account|
      account.negative_positions.each do |position|
        issues << DataQualityIssue.new(kind: :negative_position, holding: nil, security: position.security, detail: position, account: account)
      end
    end

    holdings.each do |holding|
      next if holding.security.cash?

      if holding.avg_cost.nil?
        issues << DataQualityIssue.new(kind: :missing_cost_basis, holding: holding, security: holding.security, detail: nil, account: holding.account)
      end
    end

    holdings.map(&:security).uniq.each do |security|
      next if security.cash?

      status = security.provider_status
      if status != :ok
        issues << DataQualityIssue.new(kind: :provider, holding: nil, security: security, detail: status, account: nil)
        next
      end

      latest = latest_quote_dates(as_of)[security.id]
      if latest.nil? || latest < as_of - STALE_PRICE_AFTER_DAYS
        issues << DataQualityIssue.new(kind: :stale_price, holding: nil, security: security, detail: latest, account: nil)
      end
    end

    issues.sort_by do |issue|
      [ DATA_QUALITY_KINDS.index(issue.kind), issue.security.ticker.to_s, issue.security.id.to_s, issue.account&.id.to_s,
        issue.holding&.id.to_s ]
    end
  end

  # Unrealized gains across all holdings, summed in family currency
  def unrealized_gains
    holdings_with_avg_costs.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 = holdings_with_avg_costs
    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 * #{ExchangeRate.rate_sql(from: "b.currency", to: ":currency", on: "b.date")}), 0)
          FROM balances b
          JOIN accounts a ON a.id = b.account_id
          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 * #{ExchangeRate.rate_sql(from: "b.currency", to: ":currency", on: ":period_start")}), 0)
          FROM accounts a
          INNER JOIN balances b ON b.account_id = a.id
          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

  # The snapshot before each current holding, keyed by the current holding's
  # id: the latest row for the same account, security and currency dated
  # before the current row, which is what Holding#day_change compares
  # against. One query for every holding rather than one per holding, so a
  # page that lists every position does not issue a query per row. Memoized
  # like current_holdings.
  def previous_holdings
    @previous_holdings ||= begin
      current_ids = current_holdings.map(&:id)

      if current_ids.any?
        # Keyed by the current holding, not by account and security: a provider
        # can report one security in two currencies, and each current row has
        # its own previous snapshot in its own currency.
        Holding.find_by_sql([ <<~SQL.squish, { current_ids: current_ids } ]).index_by(&:current_holding_id)
          SELECT prior.*, current_holdings.id AS current_holding_id
          FROM holdings current_holdings
          JOIN LATERAL (
            SELECT holdings.*
            FROM holdings
            WHERE holdings.account_id = current_holdings.account_id
              AND holdings.security_id = current_holdings.security_id
              AND holdings.currency = current_holdings.currency
              AND holdings.date < current_holdings.date
            ORDER BY holdings.date DESC
            LIMIT 1
          ) prior ON TRUE
          WHERE current_holdings.id IN (:current_ids)
        SQL
      else
        {}
      end
    end
  end

  # Day change for one current holding against its previous snapshot, in the
  # holding's currency, or nil without a prior snapshot. Same result as
  # Holding#day_change, without its per-holding query.
  def holding_day_change(holding)
    return nil unless holding.amount_money

    previous = previous_holdings[holding.id]
    return nil unless previous&.amount_money

    Trend.new(current: holding.amount_money, previous: previous.amount_money)
  end

  # Day change across portfolio, summed in family currency
  def day_change
    changes = current_holdings.to_a.filter_map do |h|
      t = holding_day_change(h)
      next nil unless t
      [
        convert_to_family_currency(t.current.amount, h.currency),
        convert_to_family_currency(t.previous.amount, 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 ||= report_accounts.where(accountable_type: %w[Investment Crypto])
  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.
    #
    # A currency with no rate at all converts to 0: the amount is left out of
    # every sum here rather than counted at 1. Both sides of a trend
    # come from the same holding, so they drop out together.
    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]
      rate ? numeric * rate : 0
    end

    def all_time_totals
      @all_time_totals ||= totals(period: Period.all_time)
    end

    PeriodTotals = Data.define(:contributions, :withdrawals, :buys, :sells, :dividends, :interest, :trades_count, :currency) do
      def net_flow
        contributions - withdrawals
      end

      def total_income
        dividends + interest
      end
    end

    # `security` is nil only for the cash row #allocation appends.
    HoldingAllocation = Data.define(:security, :amount, :weight, :trend) do
      def cash? = security.nil?
      def ticker = cash? ? CASH_TICKER : security.ticker
      def name = cash? ? I18n.t("models.investment_statement.cash") : (security.name.presence || ticker)
      def amount_money = amount
    end

    CASH_TICKER = "CASH".freeze

    AllocationSegment = Data.define(:id, :name, :amount, :weight)

    # Each account's holdings plus its cash, by the rule every other grouping
    # files cash by. Not Account#balance: an account below its holdings (margin,
    # or a stale zero balance) would otherwise show less than it holds, or drop
    # out of the grouping altogether.
    # The #allocation rows as segments, without the per-security trend a
    # segment does not carry. build_segments' total is the holdings plus
    # account_cash_total, which is weight_denominator, so each weight matches
    # #allocation's.
    def allocation_by_security
      segments = holdings_rolled_up_by_security.map do |security, value, _holdings|
        [ security.id, security.name.presence || security.ticker, value ]
      end
      segments << [ "cash", I18n.t("models.investment_statement.cash"), account_cash_total ]
      build_segments(segments)
    end

    def allocation_by_account
      cash = positive_account_cash.to_h { |account, value| [ account.id, value ] }
      segments = investment_accounts.map do |account|
        [ account.id, account.name, account_holdings_totals[account.id] + cash.fetch(account.id, 0) ]
      end
      build_segments(segments)
    end

    # Value by the currency each holding is priced in, plus each account's
    # cash (`positive_account_cash`) in the account's currency. Reads
    # `allocatable_holdings` so a corrupt negative row cannot net against a good
    # one here while the drill-down below drops it.
    def allocation_by_currency
      grouped = Hash.new(0)
      allocatable_holdings.each { |holding| grouped[holding.currency] += convert_to_family_currency(holding.amount, holding.currency) }
      positive_account_cash.each { |account, value| grouped[account.currency] += value }
      build_segments(grouped.map { |currency, value| [ currency, currency, value ] })
    end

    # Value by what kind of instrument each holding is -- cash, crypto or a
    # standard security -- from the security's own flags, with each account's
    # cash (`positive_account_cash`) under cash. Reads `allocatable_holdings` for the
    # same reason as `allocation_by_currency`.
    def allocation_by_kind
      grouped = Hash.new(0)
      allocatable_holdings.each do |holding|
        kind = if holding.security.cash? then "cash"
        elsif holding.security.crypto? then "crypto"
        else "standard"
        end
        grouped[kind] += convert_to_family_currency(holding.amount, holding.currency)
      end
      grouped["cash"] += account_cash_total if account_cash_total.positive?
      build_segments(grouped.map { |kind, value| [ kind, kind, value ] })
    end

    # One shape for all four classification groupings: read the column off the
    # security, fall back to UNCLASSIFIED, and place each account's cash
    # (`positive_account_cash`) where that grouping says it belongs.
    #
    # `cash_bucket` is the argument that matters. Cash is not a holding -- it
    # sits on the account -- so every grouping has to say where it goes, and
    # the honest answer differs. A cash balance IS liquidity, and IS cash as a
    # sub-class. It has no sector and no region, so for those it falls to
    # UNCLASSIFIED rather than being invented into someone's slice.
    def allocation_by_classification(column, cash_bucket: nil)
      grouped = Hash.new(0)

      allocatable_holdings.each do |holding|
        value = convert_to_family_currency(holding.amount, holding.currency)
        grouped[classification_bucket(holding.security, column, cash_bucket)] += value
      end

      grouped[cash_bucket || UNCLASSIFIED] += account_cash_total if account_cash_total.positive?

      build_segments(grouped.map { |bucket, value| [ bucket, bucket, value ] })
    end

    # Sub-classes inside one asset class. Cash is carried here too: it belongs
    # to `liquidity`/`cash`, so expanding Liquidity has to show it rather than
    # an empty list that contradicts the parent row's amount.
    def allocation_sub_classes_within(asset_class)
      grouped = Hash.new(0)

      holdings_classified_as(:asset_class, asset_class, CASH_ASSET_CLASS).each do |holding|
        bucket = classification_bucket(holding.security, :asset_sub_class, CASH_SUB_CLASS)
        grouped[bucket] += convert_to_family_currency(holding.amount, holding.currency)
      end

      grouped[CASH_SUB_CLASS] += account_cash_total if asset_class == CASH_ASSET_CLASS && account_cash_total.positive?

      build_segments(grouped.map { |bucket, value| [ bucket, bucket, value ] })
    end

    # The bottom of the ladder. Named by security so the row reads as a position
    # rather than as another bucket.
    #
    # It lists one row per security, summed in the family currency, and adds the
    # account cash the parent counts (`account_cash_rows`), so at every level a
    # bucket's positions sum to the bucket.
    def allocation_holdings_within(column, bucket, cash_bucket = nil, parent: nil)
      cash_rows = account_cash_rows(bucket, cash_bucket, parent)

      # One row per security: `current_holdings` has a row per account, so a
      # security held in several accounts, in any currencies, is summed here.
      grouped = Hash.new(0)
      names = {}
      holdings_classified_as(column, bucket, cash_bucket, parent: parent).each do |holding|
        grouped[holding.security_id] += convert_to_family_currency(holding.amount, holding.currency)
        names[holding.security_id] ||= holding.security.name.presence || holding.security.ticker
      end
      build_segments(grouped.map { |id, value| [ id, names[id], value ] } + cash_rows)
    end

    # Account cash as a position of the cash bucket: ONE row, the sum over
    # accounts of each account's cash (balance less holdings) in the family
    # currency where it is positive -- the rule `allocation_sub_classes_within` and
    # `allocation_by_classification` count it by. It belongs to Liquidity/Cash
    # only, so any other bucket, or a cash sub-class reached from another asset
    # class, gets none.
    def account_cash_rows(bucket, cash_bucket, parent)
      return [] unless cash_bucket.present? && bucket == cash_bucket
      return [] unless parent.nil? || parent == CASH_ASSET_CLASS

      [ [ "cash", I18n.t("models.investment_statement.cash"), account_cash_total ] ]
    end

    # Each investment account with cash, and that cash in the family currency.
    # An account's cash is its balance less its allocatable holdings, each
    # converted on its own and then subtracted, kept only where positive. Every
    # allocation grouping and level counts account cash through this one rule,
    # so a parent bucket and the positions below it cannot disagree about it,
    # and no grouping counts a balance another grouping drops.
    #
    # Not Account#cash_balance. It can be absent (a manual or never-synced
    # account), or default to the whole balance when a provider sends no cash
    # figure (`Account::ProviderImportAdapter#update_balance`), which would
    # count the holdings twice. `Balance::BaseCalculator` derives an
    # investment account's cash the same way, so on synced data the two agree.
    # Per account, so one account's margin cannot net against another's cash.
    def positive_account_cash
      @positive_account_cash ||= investment_accounts.filter_map do |account|
        balance = convert_to_family_currency(account.balance.to_d, account.currency)
        cash = balance - account_holdings_totals[account.id]
        [ account, cash ] if cash.positive?
      end
    end

    # Each account's allocatable holdings in the family currency, by account id.
    def account_holdings_totals
      @account_holdings_totals ||= allocatable_holdings.each_with_object(Hash.new(0)) do |holding, totals|
        totals[holding.account_id] += convert_to_family_currency(holding.amount, holding.currency)
      end
    end

    def account_cash_total
      positive_account_cash.sum { |_, value| value }
    end

    # One rule for which bucket a holding falls in, shared by the grouping, the
    # drill-down and the filter, so the three cannot disagree.
    #
    # A cash security is answered from `cash?` rather than from its columns. A
    # non-primary-currency cash position is a real holding -- `Security.cash_for`
    # creates one per currency -- and its `asset_class` may be nil. Reading the
    # column alone would file the family's euros under Unclassified while the
    # account's own euro cash balance sat under Liquidity: two answers for the
    # same money on one chart.
    def classification_bucket(security, column, cash_bucket)
      return cash_bucket if security.cash? && cash_bucket.present?

      security.public_send(column).presence || UNCLASSIFIED
    end

    # The allocatable holdings filed under `bucket` for `column`, which is what
    # the bottom level lists. `parent`, when given, also requires the holding's
    # asset class to match, because a sub-class name such as `unclassified`
    # repeats across asset classes (see `allocation_children`).
    def holdings_classified_as(column, bucket, cash_bucket = nil, parent: nil)
      allocatable_holdings.select do |holding|
        classification_bucket(holding.security, column, cash_bucket) == bucket &&
          (parent.nil? || classification_bucket(holding.security, :asset_class, CASH_ASSET_CLASS) == parent)
      end
    end

    # The allocation ladder's holdings, filtered by the per-row rule
    # `holdings_rolled_up_by_security` applies: a negative amount is corrupt
    # data (`Holding::Materializer` writes through `upsert_all`, skipping
    # validation). It is applied before anything sums, so a level above cannot
    # net a negative row that the bottom level then drops.
    def allocatable_holdings
      @allocatable_holdings ||= current_holdings.select do |holding|
        convert_to_family_currency(holding.amount, holding.currency).positive?
      end
    end

    def build_segments(rows)
      rows = rows.reject { |_, _, value| value.nil? || value <= 0 }
      total = rows.sum { |_, _, value| value }
      return [] if total.zero?

      rows
        .sort_by { |_, _, value| -value }
        .map do |id, name, value|
          AllocationSegment.new(id: id.to_s, name: name, amount: Money.new(value, family.currency), weight: (value / total * 100).round(2))
        end
    end

    # One row of #holdings_table_rows: a security rolled up across the
    # accounts that hold it, in family currency.
    HoldingsTableRow = Data.define(
      :security, :positions, :accounts_count, :qty, :avg_cost, :amount, :weight,
      :unrealized, :day_change, :day_change_positions, :missing_cost_basis
    ) do
      def ticker = security.ticker
      # The Day change sums only the positions with a previous snapshot, while
      # the row's value sums them all.
      def day_change_partial? = day_change.present? && day_change_positions < positions.size
      def name = security.name.presence || ticker
      def amount_money = amount
    end

    # Builds one rolled-up row of the holdings table for `security`: the summed
    # quantity across `positions`, its average cost and unrealised return over
    # the positions whose basis is known, and its weight against the caller's
    # `total` -- which is chosen by #weight_denominator, not here.
    def build_holdings_table_row(security, value, positions, total)
      qty = positions.sum(&:qty)

      # Same rule as combined_holding_trend: the return covers the positions
      # whose cost basis is known, and a position is known when
      # Holding#avg_cost answers -- which includes the trade-history fallback,
      # preloaded for every holding by holdings_with_avg_costs. Reading
      # `cost_basis` directly instead would hide a return on this table that
      # top_holdings counts for the same security.
      known = positions.select(&:avg_cost)
      missing_cost_basis = known.size < positions.size

      cost = nil
      unrealized = nil
      known_qty = known.sum(&:qty)
      if known.any?
        cost = known.sum { |holding| convert_to_family_currency(holding.qty * holding.avg_cost.amount, holding.currency) }
        known_value = known.sum { |holding| convert_to_family_currency(holding.amount, holding.currency) }
        unrealized = Trend.new(current: Money.new(known_value, family.currency), previous: Money.new(cost, family.currency))
      end

      day_changes = positions.filter_map do |holding|
        trend = holding_day_change(holding)
        next unless trend
        [ convert_to_family_currency(trend.current.amount, holding.currency), convert_to_family_currency(trend.previous.amount, holding.currency) ]
      end
      day_change = if day_changes.any?
        Trend.new(
          current: Money.new(day_changes.sum(&:first), family.currency),
          previous: Money.new(day_changes.sum(&:last), family.currency)
        )
      end

      HoldingsTableRow.new(
        security: security,
        positions: positions,
        accounts_count: positions.map(&:account_id).uniq.size,
        qty: qty,
        avg_cost: (cost && known_qty.positive?) ? Money.new(cost / known_qty, family.currency) : nil,
        amount: Money.new(value, family.currency),
        weight: total.zero? ? 0 : (value / total * 100).round(2),
        unrealized: unrealized,
        day_change: day_change,
        day_change_positions: day_changes.size,
        missing_cost_basis: missing_cost_basis
      )
    end

    def sort_holdings_table_rows(rows, sort:, dir:)
      sort, dir = self.class.holdings_sort(sort, dir)

      # Rows without the sorted figure go last whichever direction is asked.
      # Ties break by name, then security id, in both directions, so rows with
      # the same figure keep their order between requests.
      present, absent = rows.partition { |row| sort_key_present?(row, sort) }
      present = present.sort do |a, b|
        order = sort_value(a, sort) <=> sort_value(b, sort)
        order = -order if dir == "desc"
        order.nonzero? || (tiebreak(a) <=> tiebreak(b))
      end
      present + absent.sort_by { |row| tiebreak(row) }
    end

    def tiebreak(row)
      [ row.name.downcase, row.security.id.to_s ]
    end

    def sort_value(row, sort)
      case sort
      when "value" then row.amount.amount
      when "weight" then row.weight
      when "return" then row.unrealized.value.amount
      when "day_change" then row.day_change.value.amount
      when "name" then row.name.downcase
      end
    end

    def sort_key_present?(row, sort)
      case sort
      when "return" then row.unrealized.present?
      when "day_change" then row.day_change.present?
      else true
      end
    end

    # Groups current holdings by security and sums family-currency value.
    # Returns [[security, value, holdings], ...] sorted by value descending.
    # Callers that need return trends should call combined_holding_trend only
    # for rows they will render (e.g. after top_holdings applies its limit).
    # Shared by top_holdings and allocation so one security cannot report two
    # different percentages.
    #
    # The denominator is the holdings total plus the account cash every
    # grouping files -- the total each allocation grouping sums to -- rather
    # than portfolio_value: Account#balance can lag the holdings (a stale zero)
    # or fall below them (margin, an unsettled buy) and push a weight past
    # 100%. Account cash is positive only, so the denominator is never below
    # the holdings. It equals portfolio_value unless an account's balance is
    # below its holdings.
    def weight_denominator(holdings_total)
      holdings_total + account_cash_total
    end

    # Memoized: top_holdings, allocation and holdings_table_rows all start
    # here, and the grouping and FX conversion need only run once per instance.
    def holdings_rolled_up_by_security
      @holdings_rolled_up_by_security ||= holdings_with_avg_costs
        .group_by(&:security_id)
        .filter_map do |_security_id, holdings|
          # Filter row by row rather than on the netted total. current_holdings
          # is DISTINCT ON (account_id, security_id), so one security held in two
          # accounts yields two rows; a negative row in one would otherwise net
          # against the good row in the other and understate the position.
          positive_holdings = holdings.select do |holding|
            convert_to_family_currency(holding.amount, holding.currency).positive?
          end
          next if positive_holdings.empty?

          security = positive_holdings.first.security
          value = positive_holdings.sum { |h| convert_to_family_currency(h.amount, h.currency) }

          # positive_holdings, not holdings, so the trend excludes the bad row too.
          [ security, value, positive_holdings ]
        end
        .sort_by { |_, value, _| -value }
    end

    # Latest price date per held security on or before as_of, so a price
    # recorded after as_of cannot make a past check look fresh.
    # The last quote on or before `as_of` per held security: a real price row
    # (not a carried-forward or provisional row, nor a zero placeholder, which
    # the price importer skips too), or the date of a holding a provider
    # synced with its own price, whichever is later.
    def latest_quote_dates(as_of)
      @latest_quote_dates ||= {}
      @latest_quote_dates[as_of] ||= read_latest_quote_dates(as_of)
    end

    def read_latest_quote_dates(as_of)
      security_ids = current_holdings.map(&:security_id).uniq
      priced = Security::Price
        .where(security_id: security_ids)
        .where(date: ..as_of, carried_forward: false, provisional: false)
        .where("price > 0")
        .group(:security_id)
        .maximum(:date)
      return priced if current_holdings.none?(&:account_provider_id)

      synced = Holding
        .where(security_id: security_ids, account_id: current_holdings.map(&:account_id).uniq)
        .where.not(account_provider_id: nil)
        .where(date: ..as_of)
        .where("price > 0")
        .group(:security_id)
        .maximum(:date)
      priced.merge(synced) { |_id, price_date, holding_date| [ price_date, holding_date ].max }
    end

    def combined_holding_trend(holdings)
      currents = []
      previouses = []

      holdings.each do |holding|
        trend = holding.trend
        next unless trend

        currents << convert_to_family_currency(trend.current, holding.currency)
        previouses << convert_to_family_currency(trend.previous, holding.currency)
      end

      return nil if currents.empty?

      Trend.new(
        current: Money.new(currents.sum, family.currency),
        previous: Money.new(previouses.sum, family.currency)
      )
    end

    def investment_account_ids
      @investment_account_ids ||= investment_accounts.pluck(:id)
    end

    def report_accounts
      scope = family.accounts.visible.included_in_reports
      user ? scope.included_in_finances_for(user) : scope
    end

    # The accounts #totals reads: the investment accounts, plus the cash
    # account a broker settles their trades on when it keeps the cash in an
    # account of its own. Trade Republic does: its Cash account is a
    # depository account, and each trade's settlement there is linked to the
    # Portfolio or Crypto account as a confirmed transfer
    # (TradeRepublicAccount.settlement_account_ids_for). Taken as one scope,
    # those settlements are internal, like a buy paid from an investment
    # account's own cash, and money is counted where it enters or leaves the
    # broker: a transfer between the Cash account and the user's bank. As on
    # every account the totals read, a cash row counts only when it records
    # a flow (Totals#recorded_flow_sql), so card payments and other unlinked
    # rows on the Cash account are neither. Nor is a credit card or loan
    # payment made from it: Totals is told which accounts settle trades, and
    # leaves those payments out there (see Totals#recorded_flow_sql). Income
    # the broker books there (dividends, interest) is read with it. The Cash
    # account passes the same report and visibility filters as the investment
    # accounts. A user who does not have it in their finances therefore gets
    # the investment accounts alone, on which each settlement counts as a
    # contribution or a withdrawal again.
    #
    # Only a provider that books trade settlements this way is followed, not
    # every account of the same connection: a bank connection with a checking
    # and a brokerage account would otherwise make every deposit from that
    # checking account internal, and it would never be counted.
    def totals_account_ids
      @totals_account_ids ||= (investment_account_ids + settlement_account_ids).uniq
    end

    def settlement_account_ids
      @settlement_account_ids ||= if investment_account_ids.empty?
        []
      else
        report_accounts.where(id: TradeRepublicAccount.settlement_account_ids_for(investment_account_ids)).pluck(:id)
      end
    end

    def totals_query(account_ids:, date_range:)
      if account_ids.empty?
        return Totals.new(family, account_ids: account_ids, date_range: date_range).call
      end

      # Which of those accounts settle trades changes what Totals counts
      # (see Totals#recorded_flow_sql), so it is part of the key too.
      account_ids_hash = Digest::MD5.hexdigest("#{account_ids.sort.join(",")}|#{settlement_account_ids.sort.join(",")}")

      Rails.cache.fetch([
        "investment_statement", "totals_query/v3", ExchangeRate::CONVERSION_CACHE_VERSION, family.id, user&.id, family.currency,
        account_ids_hash, date_range.begin, date_range.end, family.entries_cache_version,
        # A rate stored later can complete totals that left an amount out.
        ExchangeRate.updated_at_version,
        transfers_version(account_ids)
      ]) { Totals.new(family, account_ids: account_ids, settlement_account_ids: settlement_account_ids, date_range: date_range).call }
    end

    # Contributions and withdrawals depend on which transactions a confirmed
    # Transfer links (Portfolio::FlowClassifier), and linking, confirming or
    # removing a transfer between two existing transactions need not write
    # to either entry, so entries_cache_version alone would miss it. The
    # count catches a removal; the timestamp catches a confirmation.
    def transfers_version(account_ids)
      leg_ids = Entry.where(account_id: account_ids, entryable_type: "Transaction").select(:entryable_id)
      count, max_at = Transfer
        .where(inflow_transaction_id: leg_ids)
        .or(Transfer.where(outflow_transaction_id: leg_ids))
        .pick(Arel.sql("count(*)"), Arel.sql("max(transfers.updated_at)"))
      "#{count}-#{max_at&.to_f}"
    end

    def monetizable_currency
      family.currency
    end
end