Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
42.1 KB
Raw Blame
class Family < ApplicationRecord
  has_many :financekit_items, dependent: :destroy
  has_many :financekit_account_lineages, dependent: :destroy
  has_many :financekit_conflicts, dependent: :destroy

  include FioConnectable
  include Syncable, AutoTransferMatchable, Subscribeable, VectorSearchable
  include PlaidConnectable, SimplefinConnectable, LunchflowConnectable, AkahuConnectable, EnableBankingConnectable, TruelayerConnectable
  include CoinbaseConnectable, BinanceConnectable, KrakenConnectable, CoinspotConnectable, CoinstatsConnectable, SnaptradeConnectable, MercuryConnectable, BrexConnectable, SophtronConnectable
  include IndexaCapitalConnectable, IbkrConnectable, WiseConnectable
  include UpConnectable
  include MonobankConnectable
  include Trading212Connectable
  include TradeRepublicConnectable
  include QuestradeConnectable
  include RedbarkConnectable
  include OnchainWalletConnectable
  include SharesightConnectable
  include AiPromptable
  include AiSettings

  DATE_FORMATS = [
    [ "MM-DD-YYYY", "%m-%d-%Y" ],
    [ "DD.MM.YYYY", "%d.%m.%Y" ],
    [ "DD-MM-YYYY", "%d-%m-%Y" ],
    [ "YYYY-MM-DD", "%Y-%m-%d" ],
    [ "DD/MM/YYYY", "%d/%m/%Y" ],
    [ "YYYY/MM/DD", "%Y/%m/%d" ],
    [ "MM/DD/YYYY", "%m/%d/%Y" ],
    [ "D/MM/YYYY", "%e/%m/%Y" ],
    [ "YYYY.MM.DD", "%Y.%m.%d" ],
    [ "YYYYMMDD", "%Y%m%d" ],
    # QIF month-name imports rely on QifParser preserving normalized spaces.
    [ "DD MMM YYYY", "%d %b %Y" ]
  ].freeze


  MONIKERS = [ "Family", "Group" ].freeze
  SHARING_DEFAULTS = %w[shared private].freeze

  has_many :users, dependent: :destroy
  has_many :accounts, dependent: :destroy
  has_many :invitations, dependent: :destroy

  has_many :imports, dependent: :destroy
  has_many :import_sessions, dependent: :destroy
  has_many :import_source_mappings, dependent: :destroy
  has_many :family_exports, dependent: :destroy
  has_many :account_statements, dependent: :destroy

  has_many :entries, through: :accounts
  has_many :transactions, through: :accounts
  has_many :rules, dependent: :destroy
  has_many :rule_groups, dependent: :destroy
  has_many :trades, through: :accounts
  has_many :holdings, through: :accounts

  has_many :tags, dependent: :destroy
  has_many :categories, dependent: :destroy
  has_many :categorization_comparisons, dependent: :destroy
  has_many :merchants, dependent: :destroy, class_name: "FamilyMerchant"

  has_many :budgets, dependent: :destroy
  has_many :budget_categories, through: :budgets

  has_many :goals, dependent: :destroy

  # Net inflow into every depository account linked to any primary-currency
  # goal, over the given window. Transfers between linked accounts net to zero
  # because both sides of an internal move land inside the same account set;
  # external transfers (e.g. checking → linked savings) net positive.
  #
  # Scoped to the family's primary currency: mixed-currency families would
  # otherwise sum raw EUR + USD numbers and surface the result as primary.
  # Foreign-currency goals are excluded from this KPI until FX conversion is
  # added.
  #
  # Entry amount convention in Sure: inflow is negative, so flip the sign.
  # Result is allowed to go negative (net outflow last 30d) so the headline
  # reflects reality; the controller decides how to render.
  def savings_inflow_velocity(range: 30.days.ago.to_date..Date.current, account_ids: nil)
    ids = account_ids || goal_linked_account_ids
    return 0 if ids.empty?

    net = Entry
      .joins("INNER JOIN transactions ON transactions.id = entries.entryable_id AND entries.entryable_type = 'Transaction'")
      .where(account_id: ids, date: range)
      .where(excluded: false)
      .merge(Transaction.excluding_pending)
      .sum(:amount)

    -net.to_d
  end

  # Two velocity windows in a single pair of sums that share one
  # account-id lookup. The kpi tile on the index reads both the current
  # 30d window and the prior 30d window; without this helper the
  # `accounts.joins(:goal_accounts)…pluck(:id)` query runs twice per
  # request even though the answer is identical.
  def savings_inflow_windows(window_days: 30, now: Date.current)
    ids = goal_linked_account_ids
    {
      current: savings_inflow_velocity(range: (now - window_days)..now, account_ids: ids),
      prior:   savings_inflow_velocity(range: (now - 2 * window_days)..(now - window_days - 1), account_ids: ids)
    }
  end

  private

    # Depository accounts linked to this family's goals, restricted to the
    # primary currency until FX is added. Memoized for the lifetime of the
    # Family instance so a single request that reads velocity twice (the
    # KPI tile uses current vs prior 30d) doesn't re-run the join+pluck.
    # `accounts` is already scoped by the has_many association, and the
    # join restricts to this family's goals — so cross-family bleed
    # remains impossible.
    def goal_linked_account_ids
      @goal_linked_account_ids ||= accounts
        .joins(:goal_accounts)
        .where(goal_accounts: { goal_id: goals.select(:id) })
        .where(currency: primary_currency_code)
        .distinct
        .pluck(:id)
    end

  public

  has_many :llm_usages, dependent: :destroy
  has_many :recurring_transactions, dependent: :destroy
  has_many :recurring_occurrences, dependent: :destroy
  has_many :insights, dependent: :destroy

  # Families with at least one opted-in member. Lets a job filter in one
  # indexed query rather than loading every family and asking each in Ruby.
  scope :with_preview_features, -> { where(id: User.with_preview_features.select(:family_id)) }

  # Family-level rollup of the per-user preview flag, for callers that run
  # without a Current.user (the nightly insights job). Preview access is a
  # personal preference but the data it produces is family-scoped, so one
  # opted-in member is enough to generate for the family.
  #
  # EXISTS rather than `users.any?(&:preview_features_enabled?)`: the job asks
  # this once per family, and the block form would load and instantiate every
  # member just to answer a boolean.
  #
  # Never gate UI on this — visibility is per-user, and this answers "somebody
  # in the household opted in", so a view using it would show the feature to a
  # user who explicitly opted out. Use the PreviewGateable helper (Current.user)
  # for anything a person sees.
  def preview_features_enabled?
    users.with_preview_features.exists?
  end

  validates :locale, inclusion: { in: I18n.available_locales.map(&:to_s) }
  validates :date_format, inclusion: { in: DATE_FORMATS.map(&:last) }
  validates :month_start_day, inclusion: { in: 1..28 }
  validates :moniker, inclusion: { in: MONIKERS }
  validates :default_account_sharing, inclusion: { in: SHARING_DEFAULTS }
  validates :personal_budgets, inclusion: { in: [ true, false ] }
  validates :household_budget_enabled, inclusion: { in: [ true, false ] }
  validates :auto_generate_transaction_names, inclusion: { in: [ true, false ] }
  validates :investment_contributions_as_spending, inclusion: { in: [ true, false ] }
  validates :manual_reconciliation, inclusion: { in: [ true, false ] }
  validates :categories_locked, inclusion: { in: [ true, false ] }
  # The searchable timezone select submits "" while no zone is saved yet;
  # store nil so provider date parsing keeps its default instead of raising.
  # Rails' friendly names (e.g. "Pacific Time (US & Canada)") are stored as
  # their IANA identifier, the value the timezone select offers.
  normalizes :timezone, with: ->(timezone) {
    timezone = timezone.strip
    ActiveSupport::TimeZone::MAPPING[timezone] || timezone.presence
  }
  validate :timezone_must_be_a_known_zone, if: :timezone_changed?
  validate :currency_must_be_known, if: :currency_changed?

  before_validation :normalize_currency
  before_validation :normalize_enabled_currencies!
  after_create_commit :add_classifier_rules

  def primary_currency_code
    self.class.normalize_currency_code(currency) || "USD"
  end

  def default_currency_for_country
    self.class.default_currency_for_country(country)
  end

  def self.default_currency_for_country(country)
    country_currency = ISO3166::Country.new(country.to_s.upcase)&.currency_code
    normalize_currency_code(country_currency) || "USD"
  end

  def self.default_currency_by_country
    LanguagesHelper::COUNTRY_MAPPING.keys.index_with { |country| default_currency_for_country(country) }
  end

  def self.normalize_currency_code(value)
    return if value.blank?

    Money::Currency.new(value).iso_code
  rescue Money::Currency::UnknownCurrencyError, ArgumentError
    nil
  end

  # Callers should still enqueue the normal family sync immediately. Plaid's
  # refresh is asynchronous, and its polling chain schedules a distinct item
  # sync after the cursor advances (or after the bounded polling fallback), so
  # fresh transactions are imported even if the baseline family sync runs first.
  def request_plaid_transactions_refreshes_later(source:)
    enqueued_job = PlaidTransactionsRefreshAllJob.perform_later(self, source: source)
    return enqueued_job if enqueued_job

    capture_plaid_refresh_enqueue_failure(source:, error_class: "ActiveJob::EnqueueError")
    nil
  rescue => error
    capture_plaid_refresh_enqueue_failure(source:, error_class: error.class.name)
    nil
  end

  def custom_enabled_currencies?
    enabled_currencies.present?
  end

  def enabled_currency_codes(extra: [])
    selected_codes = if custom_enabled_currencies?
      [ primary_currency_code, *Array(enabled_currencies) ]
    else
      Money::Currency.as_options.map(&:iso_code)
    end

    normalize_currency_codes([ *selected_codes, *Array(extra) ])
  end

  def enabled_currency_objects(extra: [])
    enabled_currency_codes(extra:).map { |code| Money::Currency.new(code) }
  end

  def secondary_enabled_currency_objects(extra: [])
    enabled_currency_objects(extra:).reject { |currency| currency.iso_code == primary_currency_code }
  end

  def capture_plaid_refresh_enqueue_failure(source:, error_class:)
    DebugLogEntry.capture(
      category: "provider_sync",
      level: "warn",
      message: "Plaid transaction refresh could not be enqueued; continuing with normal sync",
      source: source,
      provider_key: "plaid",
      family: self,
      metadata: { error_class: error_class }
    )
  rescue => logging_error
    Rails.logger.warn(
      "Plaid refresh enqueue diagnostic failed: #{logging_error.class.name}"
    )
  end
  private :capture_plaid_refresh_enqueue_failure


  def moniker_label
    case moniker.presence
    when nil, "Family"
      I18n.t("shared.family_moniker.singular", default: "Family")
    when "Group"
      I18n.t("shared.family_moniker.group_singular", default: "Group")
    else
      moniker
    end
  end

  def moniker_label_plural
    case moniker.presence
    when nil, "Family"
      I18n.t("shared.family_moniker.plural", default: "Families")
    when "Group"
      I18n.t("shared.family_moniker.group_plural", default: "Groups")
    else
      "#{moniker}s"
    end
  end

  def share_all_by_default?
    default_account_sharing == "shared"
  end

  # Shares every existing account in this family (except ones the user already
  # owns) with the given user, honoring the family's default sharing policy and
  # the user's role. Guests receive read_only; members/admins receive read_write.
  #
  # This is the single entry point for "a member just joined, give them the
  # accounts the family shares by default." It self-guards on the sharing policy
  # and family membership so every membership path (SSO JIT sign-up, token
  # registration, mobile SSO) can call it without reintroducing
  # the "member joined but sees nothing" bug. No-op when sharing is disabled or
  # there is nothing to share, and idempotent on re-run.
  def auto_share_existing_accounts_with(user)
    return unless share_all_by_default?
    # Load-bearing security guard: insert_all below bypasses AccountShare's
    # user_in_same_family / cannot_share_with_owner validations, so this
    # membership check is the ONLY thing preventing cross-family sharing.
    # Do not drop it when refactoring.
    return unless user&.persisted? && user.family_id == id

    permission = user.guest? ? "read_only" : "read_write"
    records = accounts.where.not(owner_id: user.id).pluck(:id).map do |account_id|
      { account_id: account_id, user_id: user.id, permission: permission,
        include_in_finances: true, created_at: Time.current, updated_at: Time.current }
    end

    AccountShare.insert_all(records, unique_by: %i[account_id user_id]) if records.any?
  end

  # The calendar date `time` falls on where the family lives. A background job
  # runs in the app's zone (UTC unless configured), so a sync that dates what it
  # finds by `Date.current` can land a day off the user's calendar. A blank or
  # unknown zone falls back to the app's, as Localize#resolved_timezone does.
  def local_date(time = Time.current)
    zone = timezone.present? ? ActiveSupport::TimeZone[timezone] : nil
    time.in_time_zone(zone || Time.zone).to_date
  end

  def uses_custom_month_start?
    month_start_day != 1
  end

  def custom_month_start_for(date)
    if date.day >= month_start_day
      Date.new(date.year, date.month, month_start_day)
    else
      previous_month = date - 1.month
      Date.new(previous_month.year, previous_month.month, month_start_day)
    end
  end

  def custom_month_end_for(date)
    start_date = custom_month_start_for(date)
    next_month_start = start_date + 1.month
    next_month_start - 1.day
  end

  def current_custom_month_period
    start_date = custom_month_start_for(Date.current)
    end_date = custom_month_end_for(Date.current)
    Period.custom(start_date: start_date, end_date: end_date)
  end

  def assigned_merchants
    merchant_ids = transactions.where.not(merchant_id: nil).pluck(:merchant_id).uniq
    Merchant.where(id: merchant_ids)
  end

  # Generates missing Brandfetch logos for provider merchants that have a
  # website, so records that got their website before Brandfetch was configured
  # self-heal (issue #2925). Returns the number of logos generated.
  #
  # Logos are cosmetic: a failure here must not fail the callers (post-sync
  # rule application, LLM merchant enhancement).
  def backfill_provider_merchant_logos
    ProviderMerchant.where(id: transactions.select(:merchant_id)).backfill_logos
  rescue StandardError => e
    Rails.logger.error("Failed to backfill provider merchant logos for family #{id}: #{e.message}")
    0
  end

  def available_merchants
    assigned_ids = transactions.where.not(merchant_id: nil).pluck(:merchant_id).uniq
    recently_unlinked_ids = FamilyMerchantAssociation
      .where(family: self)
      .recently_unlinked
      .pluck(:merchant_id)
    family_merchant_ids = merchants.pluck(:id)
    Merchant.where(id: (assigned_ids + recently_unlinked_ids + family_merchant_ids).uniq)
  end

  # Merchant names already associated with this family (via any provider, or a
  # manually created FamilyMerchant) -- used to recognize a merchant embedded in
  # noisy provider text (e.g. Enable Banking's remittance lines) without
  # inventing a new one from scratch. Deliberately excludes recently-unlinked
  # merchants (unlike available_merchants), since those were explicitly removed.
  def known_merchant_names
    (assigned_merchants.pluck(:name) + merchants.pluck(:name)).uniq
  end

  def assigned_merchants_for(user)
    merchant_ids = Transaction.joins(:entry)
      .where(entries: { account_id: accounts.accessible_by(user).select(:id) })
      .where.not(merchant_id: nil)
      .distinct
      .pluck(:merchant_id)
    Merchant.where(id: merchant_ids)
  end

  # Merchants a rule can set on transactions: the family's own plus the
  # provider merchants already on transactions the user can see. Unlike
  # available_merchants_for, recently unlinked merchants are left out, since a
  # rule that set one again would undo the unlink.
  def assignable_merchants_for(user)
    assigned_ids = Transaction.joins(:entry)
      .where(entries: { account_id: accounts.accessible_by(user).select(:id) })
      .select(:merchant_id)
    Merchant.where(id: merchants.select(:id)).or(Merchant.where(type: "ProviderMerchant", id: assigned_ids))
  end

  def available_merchants_for(user)
    assigned_ids = Transaction.joins(:entry)
      .where(entries: { account_id: accounts.accessible_by(user).select(:id) })
      .where.not(merchant_id: nil)
      .distinct
      .pluck(:merchant_id)
    recently_unlinked_ids = FamilyMerchantAssociation
      .where(family: self)
      .recently_unlinked
      .pluck(:merchant_id)
    family_merchant_ids = merchants.pluck(:id)
    Merchant.where(id: (assigned_ids + recently_unlinked_ids + family_merchant_ids).uniq)
  end

  def bayes_settings_object
    Family::BayesSettings.new(bayes_settings)
  end

  # Merges changes into the stored classifier settings; a blank number goes
  # back to its default.
  def update_bayes_settings!(changes)
    update!(bayes_settings: bayes_settings_object.merge(changes).to_stored)
  end

  def auto_categorize_transactions_later(transactions, rule_run_id: nil)
    AutoCategorizeJob.perform_later(self, transaction_ids: transactions.pluck(:id), rule_run_id: rule_run_id)
  end

  # The LLM only: Bayes is its own rule (Category classifier), so what it
  # categorized is no longer uncategorized here.
  def auto_categorize_transactions(transaction_ids)
    AutoCategorizer.new(self, transaction_ids: transaction_ids).auto_categorize
  end

  # Runs Bayes over the uncategorized transactions in scope the user hasn't
  # locked, transfer legs aside (they never belong in Uncategorized), and
  # excluded transactions aside too, as the uncategorized badge and Quick
  # Categorize already do (Entry.uncategorized_transactions); those it isn't
  # sure about are tried again with a better model next time. Returns the
  # modified count.
  def bayes_categorize_uncategorized(scope = categorizable_transactions)
    scope = scope.with_entry.where(entries: { excluded: false }, category_id: nil)
                 .where.not(kind: Transaction::UNCATEGORIZED_EXCLUDED_KINDS).enrichable(:category_id)
    requested = scope.count
    return 0 if requested.zero?

    result = Family::BayesCategorizer.new(self, settings: bayes_settings_object).classify_and_apply(scope.select(:id))
    DebugLogEntry.capture(
      category: "auto_categorization",
      level: "info",
      message: "Bayesian categorization categorized #{result.categorized_ids.size} of #{requested} transactions",
      source: self.class.name,
      family: self,
      metadata: { requested_count: requested, categorized_count: result.categorized_ids.size, modified_count: result.modified_count }
    )
    result.modified_count
  end

  # Copies near-duplicates' merchant to every transaction in scope without
  # one the user hasn't locked, transfers aside. Returns how many got one.
  def fill_similar_merchants(scope = categorizable_transactions)
    ids = scope.where(merchant_id: nil).where.not(kind: Transaction::TRANSFER_KINDS).enrichable(:merchant_id).pluck(:id)
    return 0 if ids.empty?

    filled = Family::SimilarMerchantFiller.new(self).fill(ids)
    DebugLogEntry.capture(
      category: "auto_categorization",
      level: "info",
      message: "Near-duplicate merchant fill gave #{filled} of #{ids.size} transactions a merchant",
      source: self.class.name,
      family: self,
      metadata: { requested_count: ids.size, filled_count: filled }
    )
    filled
  end

  # The matcher and Bayes are rules (Merchant matcher, Category classifier),
  # so they run in the family's rule order; whether one is on is whether a
  # rule with its action is. Settings → Classifier switches them.
  CLASSIFIER_ACTION_TYPES = %w[fill_similar_merchants bayes_categorize].freeze

  def self.classifier_rule_name(action_type)
    action_type == "bayes_categorize" ? I18n.t("rules.classifier_rules.category_classifier") : I18n.t("rules.classifier_rules.merchant_matcher")
  end

  def classifier_rules(action_type)
    rules.where(id: Rule::Action.where(action_type: action_type).select(:rule_id))
  end

  def classifier_rule_active?(action_type)
    classifier_rules(action_type).exists?(active: true)
  end

  def set_classifier_rule_active!(action_type, active)
    classifier_rules(action_type).find_each { |rule| rule.update!(active: active) }
  end

  # A rule with no conditions at the top of the order, the others moving
  # down one. Rule groups share the rules' positions (see Rule::Layout), so
  # they move too.
  def add_classifier_rule!(action_type, active: true)
    Rule.transaction do
      Rule.lock_positions!(self)
      rules.update_all("position = position + 1")
      rule_groups.update_all("position = position + 1")
      rules.create!(name: self.class.classifier_rule_name(action_type), resource_type: "transaction", active: active, position: 1,
                    actions_attributes: [ { action_type: action_type } ])
    end
  end

  # Unlocks the categories Bayes locked itself (lock_results) and the
  # merchants the matcher did (lock_similar_merchants) that are still what
  # they set, so rules and AI may change them again. The values stay. All or
  # none; returns how many were unlocked.
  def clear_bayes_locks!
    locked_by_bayes = transactions.where("transactions.locked_attributes ? 'category_id'")
                                  .category_from("bayes", metadata: { locked_by_bayes: true })
    locked_by_matcher = transactions.where("transactions.locked_attributes ? 'merchant_id'")
                                    .where(Transaction.enriched_from_sql("merchant_id", "similar", metadata: { locked_by_similar: true }))
    Transaction.transaction do
      unlock_own_locks(locked_by_bayes, "category_id", "bayes", "locked_by_bayes") +
        unlock_own_locks(locked_by_matcher, "merchant_id", "similar", "locked_by_similar")
    end
  end

  def auto_detect_transaction_merchants_later(transactions, rule_run_id: nil)
    AutoDetectMerchantsJob.perform_later(self, transaction_ids: transactions.pluck(:id), rule_run_id: rule_run_id)
  end

  def auto_detect_transaction_merchants(transaction_ids)
    AutoMerchantDetector.new(self, transaction_ids: transaction_ids).auto_detect
  end

  # Memoized per user: the layout renders the account sidebar on every page
  # (mobile + desktop, each with 3 tabs), so a single request can ask for the
  # balance sheet many times. Rebuilding it repeats the account/sync/exchange-
  # rate queries it depends on.
  def balance_sheet(user: Current.user)
    @balance_sheets ||= {}
    @balance_sheets[user&.id] ||= BalanceSheet.new(self, user: user)
  end

  def income_statement(user: Current.user, accounts: nil)
    IncomeStatement.new(self, user: user, accounts: accounts)
  end

  # Returns the Investment Contributions category for this family, creating it if it doesn't exist.
  # This is used for auto-categorizing transfers to investment accounts.
  # Always uses the family's locale to ensure consistent category naming across all users.
  # Transaction kinds left out of spending/income lists. With
  # investment_contributions_as_spending off, money moved into investment
  # accounts is reported as invested rather than spent.
  def analytics_excluded_kinds
    return Transaction::BUDGET_EXCLUDED_KINDS if investment_contributions_as_spending?

    Transaction::BUDGET_EXCLUDED_KINDS + [ "investment_contribution" ]
  end

  def investment_contributions_category
    # Find ALL legacy categories (created under old request-locale behavior)
    legacy = categories.where(name: Category.all_investment_contributions_names).order(:created_at).to_a

    if legacy.any?
      keeper = legacy.first
      duplicates = legacy[1..]

      # Reassign transactions and subcategories from duplicates to keeper
      if duplicates.any?
        duplicate_ids = duplicates.map(&:id)
        categories.where(parent_id: duplicate_ids).update_all(parent_id: keeper.id)
        Transaction.reassign_category!(transactions.where(category_id: duplicate_ids), keeper.id)
        BudgetCategory.where(category_id: duplicate_ids).update_all(category_id: keeper.id)
        categories.where(id: duplicate_ids).delete_all
      end

      # Rename keeper to family's locale name if needed
      I18n.with_locale(locale) do
        correct_name = Category.investment_contributions_name
        keeper.update!(name: correct_name) unless keeper.name == correct_name
      end
      return keeper
    end

    # Create new category using family's locale
    I18n.with_locale(locale) do
      categories.find_or_create_by!(name: Category.investment_contributions_name) do |cat|
        cat.color = "#0d9488"
        cat.lucide_icon = "trending-up"
      end
    end
  rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid
    # Handle race condition: another process created the category
    I18n.with_locale(locale) do
      categories.find_by!(name: Category.investment_contributions_name)
    end
  end

  # Returns account IDs for tax-advantaged accounts (401k, IRA, HSA, etc.)
  # Used to exclude these accounts from budget/cashflow calculations.
  # Tax-advantaged accounts are retirement savings, not daily expenses.
  def tax_advantaged_account_ids
    @tax_advantaged_account_ids ||= begin
      # Investment accounts derive tax_treatment from subtype
      tax_advantaged_subtypes = Investment::SUBTYPES.select do |_, meta|
        meta[:tax_treatment].in?(%i[tax_deferred tax_exempt tax_advantaged])
      end.keys

      investment_ids = accounts
        .joins("INNER JOIN investments ON investments.id = accounts.accountable_id AND accounts.accountable_type = 'Investment'")
        .where(investments: { subtype: tax_advantaged_subtypes })
        .pluck(:id)

      # Crypto accounts have an explicit tax_treatment column
      crypto_ids = accounts
        .joins("INNER JOIN cryptos ON cryptos.id = accounts.accountable_id AND accounts.accountable_type = 'Crypto'")
        .where(cryptos: { tax_treatment: %w[tax_deferred tax_exempt] })
        .pluck(:id)

      investment_ids + crypto_ids + tax_advantaged_depository_account_ids
    end
  end

  # Memoized per user for the same reason as #balance_sheet above.
  def investment_statement(user: Current.user)
    @investment_statements ||= {}
    @investment_statements[user&.id] ||= InvestmentStatement.new(self, user: user)
  end

  def eu?
    country != "US" && country != "CA"
  end

  def requires_securities_data_provider?
    # If family has any trades, they need a provider for historical prices
    trades.any?
  end

  def requires_exchange_rates_data_provider?
    # If family has any accounts not denominated in the family's currency, they need a provider for historical exchange rates
    return true if accounts.where.not(currency: self.currency).any?

    # If family has any entries in different currencies, they need a provider for historical exchange rates
    uniq_currencies = entries.pluck(:currency).uniq
    return true if uniq_currencies.count > 1
    return true if uniq_currencies.count > 0 && uniq_currencies.first != self.currency

    false
  end

  def missing_data_provider?
    (requires_securities_data_provider? && Security.provider.nil?) ||
    (requires_exchange_rates_data_provider? && ExchangeRate.provider.nil?)
  end

  # Currencies the family's accounts, entries or holdings use that have no
  # stored exchange rate into the family currency on any date. Totals built on
  # #rate_sql leave amounts in them out rather than counting them at 1;
  # totals built on #rates_for include them only if the provider supplies a
  # rate, which is then stored. The pages that show those totals say so.
  #
  # Pass the viewer's accounts so the note covers the totals they see: only
  # visible accounts count, and a member is not told about the currency of an
  # account shared with someone else.
  def currencies_without_exchange_rate(accounts: self.accounts)
    ExchangeRate.currencies_without_rate(currencies_in_use(accounts), to: currency)
  end

  # Currencies today's totals convert whose newest rate into the family
  # currency is too old to count as today's, mapped to that rate's date. Today's
  # totals use it, and the same pages say so. Only balances and current holdings
  # count: a currency only old entries use is converted at its own date's rate,
  # so an old newest rate is not out of date for it.
  def stale_exchange_rate_dates(accounts: self.accounts)
    ExchangeRate.stale_rate_dates(currencies_held_now(accounts), to: currency)
  end

  # Returns securities with plan restrictions for a specific provider
  # @param provider [String] The provider name (e.g., "TwelveData")
  # @return [Array<Hash>] Array of hashes with ticker, name, required_plan, provider
  def securities_with_plan_restrictions(provider:)
    security_ids = trades.joins(:security).pluck("securities.id").uniq
    return [] if security_ids.empty?

    restrictions = Security.plan_restrictions_for(security_ids, provider: provider)
    return [] if restrictions.empty?

    Security.where(id: restrictions.keys).map do |security|
      restriction = restrictions[security.id]
      {
        ticker: security.ticker,
        name: security.name,
        required_plan: restriction[:required_plan],
        provider: restriction[:provider]
      }
    end
  end

  def oldest_entry_date
    entries.order(:date).first&.date || Date.current
  end

  # Earliest Transaction/Trade entry date: the family's first real activity.
  # Unlike oldest_entry_date (any entry type; kept as-is for budgets and the
  # AI balance sheet), valuations and opening balances don't count here, so a
  # single old valuation can't stretch All-range charts back for years (#4007).
  # Returns nil when the family has no Transaction/Trade entries yet.
  def earliest_activity_date
    entries.where(entryable_type: %w[Transaction Trade]).minimum(:date)
  end

  # Used for invalidating family / balance sheet related aggregation queries
  def build_cache_key(key, invalidate_on_data_updates: false)
    # Our data sync process updates this timestamp whenever any family account successfully completes a data update.
    # By including it in the cache key, we can expire caches every time family account data changes.
    data_invalidation_key = invalidate_on_data_updates ? latest_sync_completed_at : nil

    [
      id,
      currency,
      key,
      data_invalidation_key,
      accounts_status_version
    ].compact.join("_")
  end

  # Used for invalidating entry related aggregation queries
  def entries_cache_version
    "#{entries.count}-#{entries.maximum(:updated_at)&.to_f || 0}-#{categories_version}"
  end

  # Used for caches keyed on entries (e.g. the transactions index's
  # uncategorized count). Counts capture deletions, and full-precision
  # timestamps distinguish updates within the same second.
  def entries_version
    "#{entries.count}-#{entries.maximum(:updated_at)&.to_f}-#{categories_version}"
  end

  # Category deletion can nullify a late assignment without touching its
  # entry; category metadata also affects cached entry results.
  def categories_version
    "#{categories.count}-#{categories.maximum(:updated_at)&.to_f || 0}"
  end

  # Tag changes write taggings without touching their entries, so caches
  # whose results depend on tag membership key on this too. The tag count
  # catches a merge or a deletion, which moves or drops taggings without
  # adding one.
  def tags_version
    tag_count, tag_max_at = tags.pick(Arel.sql("count(*)"), Arel.sql('max("tags"."updated_at")'))
    tagging_count, tagging_max_at = Tagging.joins(:tag).where(tags: { family_id: id })
      .pick(Arel.sql("count(*)"), Arel.sql('max("taggings"."updated_at")'))
    "#{tag_count}-#{tag_max_at&.to_f}-#{tagging_count}-#{tagging_max_at&.to_f}"
  end

  # Editing a holding (its manual cost basis, or unlocking it) changes
  # neither its account nor any entry, so caches whose results depend on
  # holdings, such as the Reports body's gains, key on this too. The count
  # catches a removed holding.
  def holdings_version
    count, max_at = holdings.pick(Arel.sql("count(*)"), Arel.sql('max("holdings"."updated_at")'))
    "#{count}-#{max_at&.to_f}"
  end

  # Used for invalidating caches keyed on recurring transactions (e.g. the
  # transactions index's projected recurring list). See #entries_version for
  # why .count is included alongside the timestamp.
  def recurring_transactions_version
    "#{recurring_transactions.count}-#{recurring_transactions.maximum(:updated_at)&.to_f}"
  end

  # Used for invalidating caches whose results depend on which accounts are
  # active/draft vs. disabled (e.g. AccountsController#toggle_active changes
  # nothing on entries, but changes which entries `uncategorized_transactions`
  # considers accessible).
  def accounts_status_version
    count, max_at = accounts.pick(Arel.sql("count(*)"), Arel.sql('max("accounts"."updated_at")'))
    "#{count}-#{max_at&.to_f}"
  end

  # Used for invalidating caches that render merchant name/logo for recurring
  # transactions (e.g. the transactions index's projected recurring list).
  # Recurring detection copies `transaction.merchant_id` (see
  # RecurringTransaction::Identifier), which can point at either a
  # family-owned FamilyMerchant or a shared ProviderMerchant -- editing either
  # (e.g. a manual rename, or ProviderMerchant::Enhancer updating a shared
  # provider merchant's name/logo) doesn't touch `recurring_transactions`.
  # Scoped to only the merchants actually referenced by this family's
  # recurring transactions, rather than all family merchants, so unrelated
  # merchant edits don't bust the cache unnecessarily.
  def recurring_transaction_merchants_version
    merchant_ids = recurring_transactions.where.not(merchant_id: nil).distinct.pluck(:merchant_id)
    return "0-" if merchant_ids.empty?

    scope = Merchant.where(id: merchant_ids)
    "#{scope.count}-#{scope.maximum(:updated_at)&.to_f}"
  end

  def self_hoster?
    Rails.application.config.app_mode.self_hosted?
  end

  # Lazy so existing families get a token on first render, and resetting is
  # revocation.
  def bills_feed_token!
    return bills_feed_token if bills_feed_token.present?

    update!(bills_feed_token: SecureRandom.urlsafe_base64(24))
    bills_feed_token
  end

  def reset_bills_feed_token!
    update!(bills_feed_token: SecureRandom.urlsafe_base64(24))
    bills_feed_token
  end

  # The URL a member subscribes to carries the MEMBER's identity, because the
  # feed must honor per-account sharing: a member who can reach a subset of
  # accounts must not receive the whole family's obligations. The family
  # secret never appears in the URL; only a digest of it does, so rotating
  # `bills_feed_token` still revokes every previously shared URL at once.
  def bills_feed_token_for(user)
    self.class.bills_feed_verifier.generate([ user.id, bills_feed_stamp! ])
  end

  def bills_feed_stamp!
    Digest::SHA256.hexdigest(bills_feed_token!).first(16)
  end

  # Non-minting read for the verification side: a family that never rendered
  # a feed link has no token, and a signed URL from some earlier life must
  # not conjure one into existence to match against.
  def bills_feed_stamp
    return nil if bills_feed_token.blank?

    Digest::SHA256.hexdigest(bills_feed_token).first(16)
  end

  def self.bills_feed_verifier
    Rails.application.message_verifier("bills-user-feed")
  end

  # The family's timezone, validated and with a safe fallback -- used both by
  # `Localize#switch_timezone` (request path) and `Sync#perform` (background
  # path), so a scheduled entry's "has it arrived yet?" check (Entry#scheduled?)
  # agrees with the family's own clock in both places instead of silently
  # defaulting to the app's zone (UTC) only in the background.
  #
  # `timezone` is a free-text IANA name (e.g. from an older DB dump, or a zone
  # the tzdata maintainers later renamed, like the historical "Europe/Kiev" ->
  # "Europe/Kyiv" switch), so this re-validates rather than trusting the
  # column -- see `timezone_must_be_a_known_zone` below.
  def resolved_time_zone
    return Time.zone if timezone.blank?

    zone = ActiveSupport::TimeZone[timezone]
    return zone if zone.present?

    log_invalid_timezone_once
    Time.zone
  end

  # How often to write a DebugLogEntry for the same (family, bad value)
  # pair. `resolved_time_zone` runs on every request and every sync, so
  # without this an affected family would write one row per call forever.
  INVALID_TIMEZONE_LOG_INTERVAL = 1.day
  private_constant :INVALID_TIMEZONE_LOG_INTERVAL

  private
    # Currencies of the given visible accounts' balances, and of each
    # account's holdings on its latest holding date.
    def currencies_held_now(scope)
      account_ids = self.accounts.visible.where(id: scope.select(:id)).select(:id)
      latest = Holding.where(account_id: account_ids).group(:account_id).select("account_id, MAX(date) AS date")
      (
        Account.where(id: account_ids).distinct.pluck(:currency) +
          Holding.joins("JOIN (#{latest.to_sql}) latest ON latest.account_id = holdings.account_id AND latest.date = holdings.date")
            .distinct.pluck(:currency)
      ).compact.uniq
    end

    # Currencies used by the given accounts that are visible, by their entries,
    # their trades and their holdings. Memoised per scope, so repeated reads in
    # one render share one set of queries.
    def currencies_in_use(scope)
      account_ids = self.accounts.visible.where(id: scope.select(:id)).pluck(:id).sort
      @currencies_in_use ||= {}
      @currencies_in_use[account_ids] ||= (
        Account.where(id: account_ids).distinct.pluck(:currency) +
          Entry.where(account_id: account_ids).distinct.pluck(:currency) +
          Trade.joins(:entry).where(entries: { account_id: account_ids }).distinct.pluck("trades.currency") +
          Holding.where(account_id: account_ids).distinct.pluck(:currency)
      ).compact.uniq
    end

    def log_invalid_timezone_once
      cache_key = [ "invalid_family_timezone", id, timezone ]

      # `fetch` is read-then-write, not atomic -- two concurrent
      # requests/jobs could both see a miss and both log. `write(unless_exist:
      # true)` maps to Redis's atomic SET NX in production, so only one ever
      # wins the lease and logs.
      lease_acquired = Rails.cache.write(cache_key, true, expires_in: INVALID_TIMEZONE_LOG_INTERVAL, unless_exist: true)
      return unless lease_acquired

      DebugLogEntry.capture(
        category: "other",
        level: "warn",
        message: "Invalid family timezone #{timezone.inspect}, falling back to #{Time.zone.name}",
        source: "Family#resolved_time_zone",
        family: self
      )
    end

    # Bayes first, so the matcher lands above it.
    def add_classifier_rules
      CLASSIFIER_ACTION_TYPES.reverse_each { |action_type| add_classifier_rule!(action_type) }
    end

    def categorizable_transactions
      transactions.visible.with_entry.merge(Entry.excluding_split_parents)
    end

    # In batches, like Transaction.reassign_category!. update_all skips the
    # touch a save carries to the entry (Family#entries_cache_version), so the
    # entries are touched too.
    def unlock_own_locks(scope, attribute, source, flag)
      ids = scope.lock("FOR UPDATE OF transactions").pluck(:id)
      ids.each_slice(Transaction::REASSIGN_TOUCH_BATCH_SIZE) do |batch|
        Transaction.where(id: batch).update_all([ "locked_attributes = locked_attributes - ?, updated_at = ?", attribute, Time.current ])
        # Without the flag, a lock the user sets later counts as confirming it.
        DataEnrichment.where(enrichable_type: "Transaction", enrichable_id: batch, attribute_name: attribute, source: source)
                      .drop_metadata_flag!(flag)
        Entry.where(entryable_type: "Transaction", entryable_id: batch).touch_all
      end
      ids.size
    end

    # Mirrors the inline `investment_ids` / `crypto_ids` SQL blocks in
    # `tax_advantaged_account_ids`. Joins `depositories` and filters by
    # `Depository::TAX_ADVANTAGED_SUBTYPES` (currently `%w[hsa]`). Extracted
    # rather than inlined because the existing two blocks are already long
    # enough; the extraction keeps `tax_advantaged_account_ids` readable.
    def tax_advantaged_depository_account_ids
      accounts
        .joins("INNER JOIN depositories ON depositories.id = accounts.accountable_id AND accounts.accountable_type = 'Depository'")
        .where(depositories: { subtype: Depository::TAX_ADVANTAGED_SUBTYPES })
        .pluck(:id)
    end

    def normalize_enabled_currencies!
      if enabled_currencies.blank?
        self.enabled_currencies = nil
        return
      end

      normalized_codes = normalize_currency_codes([ primary_currency_code, *Array(enabled_currencies) ])
      all_codes = Money::Currency.as_options.map(&:iso_code)
      all_selected = normalized_codes.size == all_codes.size && (normalized_codes - all_codes).empty?
      self.enabled_currencies = all_selected ? nil : normalized_codes
    end

    def normalize_currency
      self.currency = self.class.normalize_currency_code(currency) || currency
    end

    def currency_must_be_known
      errors.add(:currency, :invalid) if self.class.normalize_currency_code(currency).nil?
    end

    def normalize_currency_codes(values)
      Array(values).filter_map { |value| self.class.normalize_currency_code(value) }.uniq
    end

    # Not a plain `inclusion: { in: ActiveSupport::TimeZone.all.map(&:name) }`
    # on purpose: the settings form submits `tz.tzinfo.identifier` (e.g.
    # "America/New_York"), not `tz.name` (e.g. "Eastern Time (US & Canada)")
    # -- see LanguagesHelper#timezone_options. For every zone Rails ships,
    # those two differ, so an inclusion check against `.name` would reject
    # every legitimate value the form actually submits. `ActiveSupport::TimeZone[]`
    # resolves both forms, and is the same lookup `Localize#resolved_timezone`
    # uses at request time, so "valid at save time" and "valid when rendering"
    # can't drift apart.
    #
    # Only runs when timezone is actually being changed (see the `if:` on the
    # `validate` call above). A family that already has a stale value from
    # before this validation existed (the exact case in #390) must still be
    # able to save unrelated changes -- e.g. a settings update, or any
    # background job touching the record -- without being blocked by a field
    # nobody is currently trying to set. That value still can't crash a
    # request either way, since Localize#resolved_timezone falls back safely
    # regardless of whether this validation ever ran.
    def timezone_must_be_a_known_zone
      return if timezone.blank?

      errors.add(:timezone, :invalid) if ActiveSupport::TimeZone[timezone].blank?
    end
end