Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
14.6 KB
Raw Blame
class SimplefinAccount::Investments::HoldingsProcessor
  def initialize(simplefin_account)
    @simplefin_account = simplefin_account
  end

  def process
    simplefin_account.with_lock { process_holdings }
  end

  def process_holdings
    if holdings_data.empty?
      close_positions if simplefin_account.raw_holdings_payload == []
      return
    end
    return unless [ "Investment", "Crypto" ].include?(account&.accountable_type)

    # SimpleFIN reports one record per LOT, so a single position can arrive as
    # several records sharing a security -- a 401k that splits employee deferral
    # from employer match is the common case. `holdings` is uniquely indexed on
    # (account_id, security_id, date, currency), so importing lots one at a time
    # makes each overwrite the last and the account silently loses the value of
    # every lot but one. Combine them into the position they represent first.
    positions = {}

    holdings_data.each do |simplefin_holding|
      begin
        symbol = simplefin_holding["symbol"].presence
        holding_id = simplefin_holding["id"]
        description = simplefin_holding["description"].to_s.strip

        Rails.logger.debug({ event: "simplefin.holding.start", sfa_id: simplefin_account.id, account_id: account&.id, id: holding_id, symbol: symbol, raw: simplefin_holding }.to_json)

        unless holding_id.present?
          Rails.logger.debug({ event: "simplefin.holding.skip", reason: "missing_id", id: holding_id, symbol: symbol }.to_json)
          next
        end

        # If symbol is missing but we have a description, create a synthetic ticker
        # This allows tracking holdings like 401k funds that don't have standard symbols
        # Append a hash suffix to ensure uniqueness for similar descriptions
        if symbol.blank? && description.present?
          normalized = description.gsub(/[^a-zA-Z0-9]/, "_").upcase.truncate(24, omission: "")
          hash_suffix = Digest::MD5.hexdigest(description)[0..4].upcase
          symbol = "CUSTOM:#{normalized}_#{hash_suffix}"
          Rails.logger.info("SimpleFin: using synthetic ticker #{symbol} for holding #{holding_id} (#{description})")
        end

        unless symbol.present?
          Rails.logger.debug({ event: "simplefin.holding.skip", reason: "no_symbol_or_description", id: holding_id }.to_json)
          next
        end

        security = resolve_security(symbol, simplefin_holding["description"])
        unless security.present?
          Rails.logger.debug({ event: "simplefin.holding.skip", reason: "unresolved_security", id: holding_id, symbol: symbol }.to_json)
          next
        end

        # Parse provider data with robust fallbacks across SimpleFin sources
        # NOTE: "value" is intentionally excluded from the market_value fallback chain
        # because some brokerages (e.g. Vanguard, Fidelity) use "value" to mean cost basis,
        # which would cause the system to display average cost as current price. (GH #1182)
        qty = parse_decimal(any_of(simplefin_holding, %w[shares quantity qty units]))
        market_value = parse_decimal(any_of(simplefin_holding, %w[market_value current_value]))
        raw_cost_basis, cost_basis_source_key = cost_basis_from(simplefin_holding)
        cost_basis = normalize_cost_basis(raw_cost_basis, qty, cost_basis_source_key, institution_reports_total_basis?)

        # Derive price from market_value when possible; otherwise fall back to any price field
        fallback_price = parse_decimal(any_of(simplefin_holding, %w[purchase_price price unit_price average_cost avg_cost]))
        price = if qty > 0 && market_value > 0
          market_value / qty
        else
          fallback_price || 0
        end

        # Compute an amount we can persist (some providers omit market_value)
        computed_amount = if market_value > 0
          market_value
        elsif qty > 0 && price > 0
          qty * price
        else
          0
        end

        # SimpleFIN holdings represent a current snapshot, not historical positions.
        # Always use today's date regardless of the `created` timestamp (which is when
        # the holding was first seen by SimpleFIN, not when we observed it).
        holding_date = Date.current

        # Skip zero positions with no value to avoid invisible rows
        next if qty.to_d.zero? && computed_amount.to_d.zero?

        currency = simplefin_holding["currency"].presence || "USD"
        position = positions[[ security.id, currency, holding_date ]] ||= {
          security: security,
          currency: currency,
          date: holding_date,
          qty: 0.to_d,
          amount: 0.to_d,
          # cost_basis is stored PER SHARE, so lots combine into a
          # share-weighted average rather than a sum. That average is only
          # meaningful if EVERY lot reported one -- see basis_complete.
          basis_value: 0.to_d,
          basis_qty: 0.to_d,
          basis_complete: true,
          fallback_price: nil,
          external_ids: []
        }

        position[:qty] += qty.to_d
        position[:amount] += computed_amount.to_d
        position[:external_ids] << "simplefin_#{holding_id}_#{holding_date}"
        position[:fallback_price] ||= price if price.to_d.positive?

        if qty.to_d.positive?
          if cost_basis.present?
            position[:basis_value] += cost_basis.to_d * qty.to_d
            position[:basis_qty] += qty.to_d
          else
            # Averaging over only the lots that reported a basis would apply
            # that figure to shares whose cost is genuinely unknown, inventing
            # cost and therefore inventing gain/loss. Unknown has to stay
            # unknown for the whole position.
            position[:basis_complete] = false
          end
        end
      rescue => e
        ctx = (defined?(symbol) && symbol.present?) ? " #{symbol}" : ""
        Rails.logger.error "Error processing SimpleFin holding#{ctx}: #{e.message}"
      end
    end

    positions.each_value do |position|
      qty = position[:qty]
      amount = position[:amount]

      price = if qty.positive? && amount.positive?
        amount / qty
      else
        position[:fallback_price] || 0
      end

      cost_basis = if position[:basis_complete] && position[:basis_qty].positive?
        position[:basis_value] / position[:basis_qty]
      end

      # Sorted so the identifier stays stable across syncs when a provider
      # reorders lots. If the chosen lot later disappears, import_holding falls
      # back to matching on security/date/currency and updates the same row.
      external_id = position[:external_ids].sort.first

      saved = import_adapter.import_holding(
        security: position[:security],
        quantity: qty,
        amount: amount,
        currency: position[:currency],
        date: position[:date],
        price: price,
        cost_basis: cost_basis,
        external_id: external_id,
        account_provider_id: simplefin_account.account_provider&.id,
        source: "simplefin",
        delete_future_holdings: false  # SimpleFin tracks each holding uniquely
      )

      Rails.logger.debug({ event: "simplefin.holding.saved", account_id: account&.id, holding_id: saved.id, security_id: saved.security_id, qty: saved.qty.to_s, amount: saved.amount.to_s, currency: saved.currency, date: saved.date, external_id: saved.external_id, lots: position[:external_ids].size }.to_json)
    rescue => e
      Rails.logger.error "Error importing SimpleFin position #{position[:security]&.ticker}: #{e.message}"
    end
  end

  private :process_holdings

  private
    attr_reader :simplefin_account

    def close_positions
      return unless [ "Investment", "Crypto" ].include?(account&.accountable_type)
      provider = simplefin_account.ensure_account_provider!
      return unless provider

      if account.simplefin_account_id == simplefin_account.id
        account.holdings.where(account_provider_id: nil)
          .where("external_id ~ ?", "^simplefin_.+_[0-9]{4}-[0-9]{2}-[0-9]{2}$")
          .update_all(account_provider_id: provider.id)
      end

      snapshot_date = Date.current
      holdings = account.holdings.where(account_provider: provider)
        .where("date <= ?", snapshot_date).includes(:security, :provider_security)
        .order(date: :desc, id: :asc).to_a
        .uniq { |holding| [ holding.provider_security_id || holding.security_id, holding.currency ] }

      holdings.each do |holding|
        next if holding.qty.zero? && holding.amount.zero?

        holding.with_lock do
          next unless holding.account_provider_id == provider.id

          collision = account.holdings.find_by(security_id: holding.security_id, currency: holding.currency, date: snapshot_date)
          next if collision && collision.account_provider_id != provider.id

          closed = holding.date == snapshot_date ? holding : holding.dup
          closed.assign_attributes(
            date: snapshot_date, qty: 0, amount: 0, price: 0,
            external_id: "simplefin_closed_#{provider.id}_#{holding.provider_security_id || holding.security_id}_#{holding.currency}_#{snapshot_date}"
          )
          Holding.transaction(requires_new: true) { closed.save! }
        end
      rescue ActiveRecord::RecordNotUnique
        next
      end
    end

    def import_adapter
      @import_adapter ||= Account::ProviderImportAdapter.new(account)
    end

    def account
      simplefin_account.current_account
    end

    def holdings_data
      # Use the dedicated raw_holdings_payload field
      simplefin_account.raw_holdings_payload || []
    end

    def cost_basis_from(simplefin_holding)
      %w[cost_basis basis total_cost value].each do |key|
        raw = simplefin_holding[key]
        next if raw.nil? || raw.to_s.strip.empty?

        return [ parse_decimal(raw), key ]
      end

      [ nil, nil ]
    end

    # Sure stores holding cost_basis as per-share average cost. SimpleFIN
    # brokerages are inconsistent about which field carries which shape:
    #
    #   - total_cost / value: always a total position cost per the SimpleFIN
    #     spec and observed payloads; divide by qty unconditionally.
    #   - cost_basis / basis: the spec calls this per-share, and most
    #     brokerages comply. Keep these values unchanged by default.
    #
    # Exception: a small allowlist of brokerages (Vanguard, Fidelity, Schwab)
    # is known to populate cost_basis with the total position cost in
    # violation of the spec (#1718, #1182, #2626). For those connections
    # only, divide by qty.
    #
    # An earlier revision of this fix used a magnitude heuristic
    # (share_price × √qty midpoint). It was withdrawn because a legitimate
    # per-share basis on a holding with a large unrealized loss
    # (e.g. 100 shares with basis $100 now worth $5) trips the midpoint and
    # gets mis-divided to $1/share — corrupting compliant providers. The
    # allowlist trades some manual maintenance for that safety.
    def normalize_cost_basis(raw_cost_basis, qty, source_key, total_basis_institution = false)
      return nil if raw_cost_basis.nil?

      if %w[total_cost value].include?(source_key) ||
         (total_basis_institution && %w[cost_basis basis].include?(source_key))
        return nil unless qty.to_d.positive?
        return raw_cost_basis / qty
      end

      raw_cost_basis
    end

    # Institutions known to populate the SimpleFIN `cost_basis` / `basis`
    # field with the total position cost rather than the per-share value the
    # spec requires. Matched as case-insensitive substrings against the
    # account's stored org name and domain.
    TOTAL_BASIS_INSTITUTIONS = %w[vanguard fidelity schwab etrade e*trade].freeze

    def institution_reports_total_basis?
      org = simplefin_account.respond_to?(:org_data) ? simplefin_account.org_data : nil
      return false if org.blank?

      candidates = [ org["name"], org[:name], org["domain"], org[:domain] ].compact.map(&:to_s).map(&:downcase)
      return false if candidates.empty?

      TOTAL_BASIS_INSTITUTIONS.any? { |needle| candidates.any? { |c| c.include?(needle) } }
    end

    def resolve_security(symbol, description)
      # Normalize crypto tickers to a distinct namespace so they don't collide with equities
      sym = symbol.to_s.upcase
      is_crypto_account = account&.accountable_type == "Crypto" || simplefin_account.name.to_s.downcase.include?("crypto")
      is_crypto_symbol  = %w[BTC ETH SOL DOGE LTC BCH].include?(sym)
      mentions_crypto   = description.to_s.downcase.include?("crypto")

      if !sym.include?(":") && (is_crypto_account || is_crypto_symbol || mentions_crypto)
        sym = "CRYPTO:#{sym}"
      end

      # Custom tickers (from holdings without symbols) should always be offline
      is_custom = sym.start_with?("CUSTOM:")

      # Use Security::Resolver to find or create the security, but be resilient
      begin
        if is_custom
          # Skip resolver for custom tickers - create offline security directly
          raise "Custom ticker - skipping resolver"
        end
        Security::Resolver.new(sym).resolve
      rescue => e
        # If provider search fails or any unexpected error occurs, fall back to an offline security
        Rails.logger.warn "SimpleFin: resolver failed for symbol=#{sym}: #{e.class} - #{e.message}; falling back to offline security" unless is_custom
        Security.find_or_initialize_by(ticker: sym).tap do |sec|
          sec.offline = true if sec.respond_to?(:offline) && sec.offline != true
          sec.name = description.presence if sec.name.blank? && description.present?
          sec.save! if sec.changed?
        end
      end
    end

    def parse_holding_date(created_timestamp)
      return nil unless created_timestamp

      case created_timestamp
      when Integer
        Time.at(created_timestamp).to_date
      when String
        Date.parse(created_timestamp)
      else
        nil
      end
    rescue ArgumentError => e
      Rails.logger.error "Failed to parse SimpleFin holding date #{created_timestamp}: #{e.message}"
      nil
    end

    # Returns the first non-empty value for any of the provided keys in the given hash
    def any_of(hash, keys)
      return nil unless hash.respond_to?(:[])
      Array(keys).each do |k|
        # Support symbol or string keys
        v = hash[k]
        v = hash[k.to_s] if v.nil?
        v = hash[k.to_sym] if v.nil?
        return v if !v.nil? && v.to_s.strip != ""
      end
      nil
    end

    def parse_decimal(value)
      return 0 unless value.present?

      case value
      when String
        BigDecimal(value)
      when Numeric
        BigDecimal(value.to_s)
      else
        BigDecimal("0")
      end
    rescue ArgumentError => e
      Rails.logger.error "Failed to parse SimpleFin decimal value #{value}: #{e.message}"
      BigDecimal("0")
    end
end