Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
39.2 KB
Raw Blame
class TransactionsController < ApplicationController
  include EntryableResource
  include FilterClearable

  before_action :set_entry_for_unlock, only: :unlock
  before_action :set_entry_for_tags, only: :update_tags
  before_action :store_params!, only: :index

  helper_method :new_transaction_idempotency_key

  def show
    super
    assign_mark_recurring_state
  end

  def new
    prefill_params_from_duplicate!
    super
    apply_duplicate_attributes!
    set_new_transaction_form_options
  end

  def index
    @q = search_params
    @accessible_account_ids = Current.user.accessible_accounts.pluck(:id)
    @search = Transaction::Search.new(Current.family, filters: @q, accessible_account_ids: @accessible_account_ids)

    base_scope = @search.transactions_scope
                       .reverse_chronological
                       .includes(
                         { entry: :account },
                         :category, { merchant: { logo_attachment: :blob } }, :tags,
                         # Union of #2643 counterpart UI + Skylight category-menu N+1:
                         # - outflow rows need inflow_transaction (to_account) for both
                         #   counterpart display and Transfer#categorizable?/#payment?
                         # - inflow rows need outflow_transaction (from_account) for
                         #   counterpart display, inflow_transaction (to_account)
                         #   for the category menu on the same row, and the
                         #   outflow's category, which the inflow row shows
                         {
                           transfer_as_outflow: {
                             inflow_transaction: { entry: :account }
                           }
                         },
                         {
                           transfer_as_inflow: {
                             inflow_transaction: { entry: :account },
                             outflow_transaction: [ :category, { entry: :account } ]
                           }
                         }
                       )

    @pagy, @transactions = pagy(base_scope, limit: safe_per_page(stored_params["per_page"]))
    Transaction::ActivitySecurityPreloader.new(@transactions).preload

    @transfer_hint_transaction_ids = Current.family.transaction_ids_with_transfer_candidates(@transactions, user: Current.user)

    # Preload split parent data
    entry_ids = @transactions.map { |t| t.entry.id }

    # Load split parent entries for grouped display (only when grouping is enabled)
    @split_parents = if Current.user.show_split_grouped?
      split_parent_ids = @transactions.filter_map { |t| t.entry.parent_entry_id }.uniq
      if split_parent_ids.any?
        Entry.where(id: split_parent_ids)
             .includes(:account, entryable: [ :category, { merchant: { logo_attachment: :blob } } ])
             .index_by(&:id)
      else
        {}
      end
    else
      {}
    end

    # Preload which entries on this page are split parents (have children) to avoid N+1
    @split_parent_entry_ids = Entry.split_parent_ids_among(entry_ids)

    # Counts what the categorize wizard can act on, so read-only shares don't
    # keep the "Categorize" menu item visible.
    @uncategorized_count = Rails.cache.fetch(uncategorized_count_cache_key) do
      Current.annotatable_entries.uncategorized_transactions.count
    end

    # Load projected recurring transactions for next 10 days. Only the IDs are
    # cached: cached ActiveRecord objects outlive schema changes across
    # upgrades and raise MissingAttributeError for columns added since.
    projected_recurring_ids = Rails.cache.fetch(projected_recurring_cache_key, expires_in: 1.day) do
      Current.family.recurring_transactions
                    .accessible_by(Current.user)
                    .active
                    .where("next_expected_date <= ? AND next_expected_date >= ?",
                           10.days.from_now.to_date,
                           Date.current)
                    .pluck(:id)
    end
    @projected_recurring = if projected_recurring_ids.empty?
      []
    else
      Current.family.recurring_transactions.accessible_by(Current.user)
                    .where(id: projected_recurring_ids).includes(merchant: { logo_attachment: :blob }).to_a
    end

    @breadcrumbs = [ [ t("breadcrumbs.home"), root_path ], [ t("breadcrumbs.transactions"), nil ] ]
  end

  def clear_filter
    updated_params = {
      "q" => without_cleared_filter(search_params),
      "page" => params[:page],
      "per_page" => params[:per_page]
    }

    # Add flag to indicate filters were explicitly cleared
    updated_params["filter_cleared"] = "1" if updated_params["q"].blank?

    Current.session.update!(prev_transaction_page_params: updated_params)

    redirect_to transactions_path(updated_params)
  end

  def create
    account = Current.user.accessible_accounts.find_by(id: params.dig(:entry, :account_id))

    if account.nil?
      @entry = Current.family.entries.new(entry_params)
      @entry.valid?
      clear_stale_auto_generated_name!(@entry)
      set_new_transaction_form_options
      render :new, status: :unprocessable_entity
      return
    end

    return unless require_account_permission!(account)

    idempotency_key = submitted_idempotency_key

    # Sequential double-submit guard: the form was already submitted
    # successfully once (double-click, browser retry, user reopening the
    # dialog after a slow response) and the first request already committed
    # by the time this one runs. Treat it as a success instead of creating a
    # second, identical entry.
    if idempotency_key && (existing_entry = find_duplicate_manual_entry(account, idempotency_key))
      respond_with_created_entry(existing_entry)
      return
    end

    @entry = account.entries.new(entry_params_with_idempotency_key(idempotency_key))

    if @entry.save
      @entry.sync_account_later
      @entry.lock_saved_attributes!
      @entry.mark_user_modified!
      @entry.transaction.lock_attr!(:tag_ids) if @entry.transaction.tags.any?
      @entry.apply_immediate_rules_later

      respond_with_created_entry(@entry)
    else
      clear_stale_auto_generated_name!(@entry)
      set_new_transaction_form_options
      render :new, status: :unprocessable_entity
    end
  rescue ActiveRecord::RecordNotUnique
    # Concurrent-request backstop: two near-simultaneous submissions both
    # passed the pre-check above (neither saw the other's row yet) and both
    # reached #save. The partial unique index on
    # entries(account_id, idempotency_key) lets exactly one INSERT win;
    # this rescues the loser and redirects it to the winning entry instead of
    # creating a duplicate or surfacing a 500 to the user.
    existing_entry = idempotency_key && find_duplicate_manual_entry(account, idempotency_key)
    raise unless existing_entry

    respond_with_created_entry(existing_entry)
  end

  def update
    rule_input = @entry.rule_input_snapshot

    if @entry.update(permitted_entry_params)
      sync_needed = @entry.saved_changes_affect_balances?
      transaction = @entry.transaction
      transaction.record_category_usage!

      if needs_rule_notification?(transaction)
        flash[:cta] = {
          type: "category_rule",
          category_id: transaction.category_id,
          category_name: transaction.category.name,
          merchant_name: @entry.name
        }
      end

      @entry.lock_saved_attributes!
      @entry.mark_user_modified!
      @entry.transaction.lock_attr!(:tag_ids) if @entry.transaction.tags.any?
      @entry.sync_account_later if sync_needed
      @entry.apply_immediate_rules_later(changed_from: rule_input)

      notes_changed = @entry.saved_change_to_notes?

      # Reload to ensure fresh state for turbo stream rendering
      @entry.reload

      assign_mark_recurring_state

      respond_to do |format|
        format.html { redirect_back_or_to account_path(@entry.account), notice: t(".updated") }
        format.turbo_stream do
          in_split_group = helpers.in_split_group?(@entry, params[:grouped])
          # The row draws the "Transfer?" hint, and its cache key records
          # whether it applies, so the hint is worked out as the list does.
          @transfer_hint_transaction_ids = Current.family.transaction_ids_with_transfer_candidates([ @entry.transaction ], user: Current.user)
          render turbo_stream: [
            turbo_stream.replace(
              dom_id(@entry, :header),
              partial: "transactions/header",
              locals: { entry: @entry }
            ),
            turbo_stream.replace(
              dom_id(@entry, :protection),
              partial: "entries/protection_indicator",
              locals: { entry: @entry, unlock_path: unlock_transaction_path(@entry.transaction) }
            ),
            (turbo_stream.replace(
              dom_id(@entry, :notes),
              partial: "transactions/notes",
              locals: { entry: @entry, can_annotate: can_annotate_entry? }
            ) if params[:entry]&.key?(:notes) && notes_changed),
            (turbo_stream.replace(
              dom_id(@entry, :mark_recurring),
              partial: "transactions/mark_recurring",
              locals: { entry: @entry }
            ) if can_edit_entry? && !@entry.split_child?),
            turbo_stream.replace(
              dom_id(@entry, :category_provenance),
              partial: "transactions/category_provenance",
              locals: { entry: @entry }
            ),
            turbo_stream.replace(
              dom_id(@entry),
              partial: "entries/entry",
              locals: { entry: @entry, in_split_group: in_split_group }
            ),
            *flash_notification_stream_items
          ].compact
        end
      end
    else
      assign_mark_recurring_state
      render :show, status: :unprocessable_entity
    end
  end

  def update_tags
    return unless require_account_permission!(@entry.account, :annotate, redirect_path: transaction_path(@entry))

    rule_input = @entry.rule_input_snapshot

    # The transaction-row tag picker toggles one tag at a time; the drawer's
    # multiselect sends the full set, plus the set it last saved so only its
    # own changes apply and tags a rule added meanwhile stay.
    if params[:toggle_tag_id].present?
      @toggled_tag = Current.family.tags.find(params[:toggle_tag_id])
      @entry.transaction.toggle_tag!(@toggled_tag)
    elsif params.key?(:previous_tag_ids)
      previous_ids = Array(params[:previous_tag_ids]).reject(&:blank?)
      @entry.transaction.merge_tag_changes!(tag_ids_param, previous_ids, available_tags: Current.family.tags)
    else
      @entry.transaction.replace_tags!(tag_ids_param, available_tags: Current.family.tags)
    end

    @entry.lock_saved_attributes!
    @entry.mark_user_modified!
    @entry.transaction.lock_attr!(:tag_ids)
    @entry.apply_immediate_rules_later(changed_from: rule_input)

    respond_to do |format|
      # JSON stays first so Accept: */* callers keep the original response.
      format.json { render json: { tag_ids: @entry.transaction.tag_ids } }
      # Without Turbo the row picker's button_to posts as plain HTML.
      format.html { redirect_back_or_to transaction_path(@entry) }
      format.turbo_stream do
        transaction = @entry.transaction
        streams = %i[desktop mobile].map do |variant|
          turbo_stream.replace(
            dom_id(transaction, "tag_summary_#{variant}"),
            partial: "tags/summary",
            locals: { transaction: transaction, variant: variant }
          )
        end
        if @toggled_tag
          # autofocus hands keyboard focus back to the re-rendered option,
          # which Turbo focuses after the stream renders.
          streams << turbo_stream.replace(
            "#{dom_id(@entry, :tag_option)}_#{@toggled_tag.id}",
            partial: "tag/dropdowns/row",
            locals: { tag: @toggled_tag, entry: @entry, selected: transaction.tag_ids.include?(@toggled_tag.id), autofocus: true }
          )
        end
        render turbo_stream: streams
      end
    end
  end

  def merge_duplicate
    transaction = accessible_transactions.includes(entry: :account).find(params[:id])

    return unless require_account_permission!(transaction.entry.account)

    if transaction.merge_with_duplicate!
      flash[:notice] = t("transactions.merge_duplicate.success")
    else
      flash[:alert] = t("transactions.merge_duplicate.failure")
    end

    redirect_to transactions_path
  rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid,
         ActiveRecord::RecordNotDestroyed, ActiveRecord::Deadlocked,
         ActiveRecord::LockWaitTimeout => e
    Rails.logger.error("Failed to merge duplicate transaction #{params[:id]}: #{e.message}")
    flash[:alert] = t("transactions.merge_duplicate.failure")
    redirect_to transactions_path
  end

  def dismiss_duplicate
    transaction = accessible_transactions.includes(entry: :account).find(params[:id])

    return unless require_account_permission!(transaction.entry.account)

    if transaction.dismiss_duplicate_suggestion!
      flash[:notice] = t("transactions.dismiss_duplicate.success")
    else
      flash[:alert] = t("transactions.dismiss_duplicate.failure")
    end

    redirect_back_or_to transactions_path
  rescue ActiveRecord::RecordInvalid => e
    Rails.logger.error("Failed to dismiss duplicate suggestion for transaction #{params[:id]}: #{e.message}")
    flash[:alert] = t("transactions.dismiss_duplicate.failure")
    redirect_back_or_to transactions_path
  end

  # Offer transaction conversion only for account types that can own trades.
  def convert_to_trade
    @transaction = accessible_transactions.includes(entry: :account).find(params[:id])
    @entry = @transaction.entry

    return unless require_account_permission!(@entry.account)

    unless @entry.account.supports_trades?
      flash[:alert] = t("transactions.convert_to_trade.errors.not_investment_account")
      redirect_back_or_to transactions_path
      return
    end

    render :convert_to_trade
  end

  # Lock and recheck the source entry before creating a trade and excluding the transaction.
  def create_trade_from_transaction
    @transaction = accessible_transactions.includes(entry: :account).find(params[:id])
    @entry = @transaction.entry

    return unless require_account_permission!(@entry.account)

    # Pre-transaction validations
    unless @entry.account.supports_trades?
      flash[:alert] = t("transactions.convert_to_trade.errors.not_investment_account")
      redirect_back_or_to transactions_path
      return
    end

    if @entry.excluded?
      flash[:alert] = t("transactions.convert_to_trade.errors.already_converted")
      redirect_back_or_to transactions_path
      return
    end

    # Resolve security before transaction
    security = resolve_security_for_conversion
    return if performed? # Early exit if redirect already happened

    activity_label = params[:investment_activity_label].presence
    # Serialize replacements on the source, including requests already in flight.
    ActiveRecord::Base.transaction do
      # Entry#transaction is its delegated transaction record, so use an
      # explicit DB transaction rather than ActiveRecord's instance with_lock.
      @entry.lock!
      if @entry.excluded?
        flash[:alert] = t("transactions.convert_to_trade.errors.already_converted")
        redirect_back_or_to transactions_path
        next
      end

      # Infer missing values from the locked source amount, including edits
      # committed while the security was being resolved.
      qty, price = calculate_qty_and_price
      next if performed?

      # Infer sell from the refreshed source: negative amount means money in.
      is_sell = activity_label == "Sell" || (activity_label.blank? && @entry.amount < 0)
      # For trades: positive qty = buy (money out), negative qty = sell (money in)
      signed_qty = is_sell ? -qty : qty
      trade_amount = qty * price
      # Sells bring money in (negative amount), Buys take money out (positive amount)
      signed_amount = is_sell ? -trade_amount : trade_amount

      # Default activity label if not provided
      activity_label ||= is_sell ? "Sell" : "Buy"

      # Create trade entry with note about conversion
      conversion_note = t("transactions.convert_to_trade.conversion_note",
        original_name: @entry.name,
        original_date: I18n.l(@entry.date, format: :long))

      new_entry = @entry.account.entries.create!(
        name: params[:trade_name] || Trade.build_name(is_sell ? "sell" : "buy", qty, security.ticker),
        date: @entry.date,
        amount: signed_amount,
        currency: @entry.currency,
        notes: conversion_note,
        entryable: Trade.new(
          security: security,
          qty: signed_qty,
          price: price,
          currency: @entry.currency,
          investment_activity_label: activity_label
        )
      )

      # Mark the new trade as user-modified to protect from sync
      new_entry.lock_saved_attributes!
      new_entry.mark_user_modified!

      # Mark original transaction as excluded (soft delete)
      @entry.update!(excluded: true)
    end

    return if performed?

    flash[:notice] = t("transactions.convert_to_trade.success")
    redirect_to account_path(@entry.account), status: :see_other
  rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
    flash[:alert] = t("transactions.convert_to_trade.errors.conversion_failed", error: e.message)
    redirect_back_or_to transactions_path, status: :see_other
  rescue StandardError => e
    flash[:alert] = t("transactions.convert_to_trade.errors.unexpected_error", error: e.message)
    redirect_back_or_to transactions_path, status: :see_other
  end

  def unlock
    return unless require_account_permission!(@entry.account)

    @entry.unlock_for_sync!
    flash[:notice] = t("entries.unlock.success")

    redirect_back_or_to transactions_path
  end

  # Advances the manual reconciled_status through unreconciled -> cleared ->
  # reconciled -> unreconciled. Scoped to manual (unsynced) accounts — synced
  # accounts already get duplicate-matching (Account::ProviderImportAdapter)
  # and statement-level reconciliation (AccountStatement).
  def reconcile
    return head :not_found unless Current.family.manual_reconciliation?

    transaction = accessible_transactions.includes(entry: :account).find(params[:id])
    entry = transaction.entry

    return unless require_account_permission!(entry.account)

    unless entry.account.manual?
      flash[:alert] = t("transactions.reconcile.not_manual_account")
      redirect_back_or_to transactions_path
      return
    end

    entry.advance_reconciled_status!
    flash[:notice] = t("transactions.reconcile.success", status: t("transactions.reconcile.statuses.#{entry.reconciled_status}"))

    redirect_back_or_to transactions_path
  rescue ActiveRecord::RecordInvalid => e
    DebugLogEntry.capture(
      category: "reconciliation",
      level: "error",
      message: "Failed to update reconciled_status for transaction #{params[:id]}: #{e.message}",
      source: "TransactionsController#reconcile",
      family: Current.family,
      user: Current.user,
      account: entry&.account,
      metadata: {
        transaction_id: params[:id],
        entry_id: entry&.id,
        error_class: e.class.name,
        error_message: e.message
      }
    )
    flash[:alert] = t("transactions.reconcile.failure")
    redirect_back_or_to transactions_path
  end

  def mark_as_recurring
    transaction = accessible_transactions.includes(entry: :account).find(params[:id])

    return unless require_account_permission!(transaction.entry.account)

    begin
      # Check if a recurring transaction already exists for this pattern.
      # The UI disables the button ahead of time using the same lookup, but
      # this guard remains as the authoritative check (e.g. stale page, direct
      # POST). The check and the create hold the entry lock the Allocator and
      # the add-bill form take, so neither can slip in between them.
      blocked = nil
      Entry.transaction do
        RecurringTransaction::Allocator.lock_entry!(transaction.entry)
        blocked = mark_recurring_blocked_message(transaction)
        RecurringTransaction.create_from_transaction(transaction) unless blocked
      end

      if blocked
        flash[:alert] = blocked
        redirect_back_or_to transactions_path
        return
      end

      respond_to do |format|
        format.html do
          flash[:notice] = t("recurring_transactions.marked_as_recurring")
          redirect_back_or_to transactions_path
        end
      end
    rescue ActiveRecord::RecordInvalid => e
      respond_to do |format|
        format.html do
          flash[:alert] = t("recurring_transactions.creation_failed")
          redirect_back_or_to transactions_path
        end
      end
    rescue ActiveRecord::RecordNotUnique
      # Another request created the same (account, name/merchant, amount,
      # currency) pattern between the check above and this create — the DB
      # unique index is the authoritative backstop for that race.
      respond_to do |format|
        format.html do
          flash[:alert] = t("recurring_transactions.already_exists")
          redirect_back_or_to transactions_path
        end
      end
    rescue StandardError => e
      respond_to do |format|
        format.html do
          flash[:alert] = t("recurring_transactions.unexpected_error")
          redirect_back_or_to transactions_path
        end
      end
    end
  end

  def exchange_rate
    account = Current.family.accounts.find(params[:account_id])
    currency_from = params[:currency]
    date = params[:date]&.to_date || Date.current

    if account.currency == currency_from
      render json: { same_currency: true, rate: 1.0 }
    else
      rate_obj = ExchangeRate.find_or_fetch_rate(
        from: currency_from,
        to: account.currency,
        date: date
      )

      if rate_obj.nil?
        return render json: { error: "Exchange rate not found" }, status: :not_found
      end

      rate_value = rate_obj.is_a?(Numeric) ? rate_obj : rate_obj.rate

      render json: { rate: rate_value.to_f, account_currency: account.currency }
    end
  end

  def name_suggestions
    unknown_names = LanguagesHelper::SUPPORTED_LOCALES.map { |locale| I18n.t("transactions.unknown_name", locale: locale) }
    transactions = Current.family.transactions.merge(Account.accessible_by(Current.user)).where.not(entries: { name: unknown_names })
    query = params[:q].to_s.strip

    @suggestions = if query.length < Transaction::MIN_LENGTH_FOR_NAME_SUGGESTION
      names = recent_transaction_names.select { |name| name.downcase.include?(query.downcase) }
      transactions.name_suggestions_for_names(names)
    else
      transactions.name_suggestions_for(query)
    end
  end

  private
    def uncategorized_count_cache_key
      "transactions_uncategorized_count/v5/#{Current.family.id}/#{Current.user.id}/" \
        "#{Current.family.entries_version}/#{Current.family.accounts_status_version}/#{Current.account_share_version}"
    end

    def projected_recurring_cache_key
      "transactions_projected_recurring/v6/#{Current.family.id}/#{Current.user.id}/#{Date.current}/" \
        "#{Current.family.recurring_transactions_version}/#{Current.family.accounts_status_version}/" \
        "#{Current.family.recurring_transaction_merchants_version}/#{Current.account_share_version}"
    end

    def covering_series_for(entry)
      Current.family.recurring_transactions.accessible_by(Current.user).covering(entry)
    end

    def mark_recurring_blocked_message(transaction)
      if covering_series_for(transaction.entry)
        t("recurring_transactions.already_covered")
      elsif RecurringTransaction.identity_taken_by(transaction)
        t("recurring_transactions.already_exists")
      end
    end

    def assign_mark_recurring_state
      return unless can_edit_entry? && !@entry.split_child?

      blocked = mark_recurring_blocked_message(@entry.transaction)

      @mark_recurring_href = mark_as_recurring_transaction_path(@entry.transaction)
      @mark_recurring_subtitle_class = blocked ? "text-subdued" : "text-secondary"
      @mark_recurring_subtitle = blocked || t("transactions.show.mark_recurring_subtitle")
      @mark_recurring_disabled = blocked.present?
      @mark_recurring_title = blocked
      @mark_recurring_button_class = blocked ? "disabled:opacity-50" : nil
    end

    def accessible_transactions
      Current.family.transactions
        .joins(entry: :account)
        .merge(Account.accessible_by(Current.user))
    end

    def duplicate_source
      return @duplicate_source if defined?(@duplicate_source)
      @duplicate_source = if params[:duplicate_entry_id].present?
        source = Current.family.entries.joins(:account).merge(Account.accessible_by(Current.user)).find_by(id: params[:duplicate_entry_id])
        source if source&.transaction?
      end
    end

    def prefill_params_from_duplicate!
      return unless duplicate_source
      params[:nature] ||= duplicate_source.amount.negative? ? "inflow" : "outflow"
      params[:account_id] ||= duplicate_source.account_id.to_s
    end

    def apply_duplicate_attributes!
      return unless duplicate_source
      @entry.assign_attributes(
        name: duplicate_source.name,
        amount: duplicate_source.amount.abs,
        currency: duplicate_source.currency,
        notes: duplicate_source.notes
      )
      @entry.entryable.assign_attributes(
        category_id: duplicate_source.entryable.category_id,
        merchant_id: duplicate_source.entryable.merchant_id
      )
      @entry.entryable.tag_ids = duplicate_source.entryable.tag_ids
    end

    def set_entry_for_unlock
      transaction = accessible_transactions.find(params[:id])
      @entry = transaction.entry
    end

    def needs_rule_notification?(transaction)
      return false if Current.user.guest?
      return false if Current.user.rule_prompts_disabled

      if Current.user.rule_prompt_dismissed_at.present?
        time_since_last_rule_prompt = Time.current - Current.user.rule_prompt_dismissed_at
        return false if time_since_last_rule_prompt < 1.day
      end

      transaction.saved_change_to_category_id? && transaction.category_id.present? &&
      transaction.eligible_for_category_rule?
    end

    def entry_params
      entry_params = params.require(:entry).permit(
        :name, :date, :amount, :currency, :excluded, :notes, :nature, :entryable_type,
        entryable_attributes: [ :id, :category_id, :merchant_id, :kind, :investment_activity_label, :exchange_rate, { tag_ids: [] } ]
      )

      nature = entry_params.delete(:nature)

      entry_params.delete(:amount) if entry_params[:amount].blank?
      entry_params.delete(:date) if entry_params[:date].blank?

      if nature.present? && entry_params[:amount].present?
        signed_amount = nature == "inflow" ? -entry_params[:amount].to_d : entry_params[:amount].to_d
        entry_params = entry_params.merge(amount: signed_amount)
      end

      entry_params
    end

    def entry_params_with_idempotency_key(idempotency_key)
      return entry_params unless idempotency_key

      # A dedicated column, deliberately not external_id/source: those are
      # provider-linkage fields (Entry#linked? = external_id.present?), and
      # reusing them here would make a manual entry look provider-synced -
      # disabling its date/nature/amount/currency fields in the editor, and
      # hiding it from future provider dedup matching.
      entry_params.merge(idempotency_key: idempotency_key)
    end

    def find_duplicate_manual_entry(account, idempotency_key)
      account.entries.find_by(idempotency_key: idempotency_key)
    end

    # The hidden "entry[idempotency_key]" field is rendered fresh (a random
    # UUID) every time the new-transaction form loads, and echoed back
    # unchanged by the browser on submit. It's never trusted for anything but
    # de-duplication scoped to the current user's own account (see #create),
    # so we only require that it looks like a UUID we could have generated -
    # anything else (missing field, tampered value, non-string type from a
    # malformed request) just disables the idempotency check for that
    # request rather than being treated as an error.
    UUID_FORMAT = /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i
    private_constant :UUID_FORMAT

    def submitted_idempotency_key
      key = params.dig(:entry, :idempotency_key)
      key if key.is_a?(String) && key.match?(UUID_FORMAT)
    end

    def new_transaction_idempotency_key
      @new_transaction_idempotency_key ||= submitted_idempotency_key || SecureRandom.uuid
    end

    def respond_with_created_entry(entry)
      flash[:notice] = t(".created")

      respond_to do |format|
        format.html { redirect_back_or_to account_path(entry.account) }
        format.turbo_stream { stream_redirect_back_or_to(account_path(entry.account)) }
      end
    end

    def tag_ids_param
      Array(params[:tag_ids]).reject(&:blank?)
    end

    def set_entry_for_tags
      set_entry
    end

    # Entry#set_default_name may have already filled in a category/merchant
    # -derived name during this failed validation attempt. A blank
    # submission must still render blank -- otherwise the generated text
    # becomes the field's literal value, and if the user then changes the
    # category/merchant and resubmits, that stale generated name (no longer
    # blank) skips the callback entirely and gets saved as-is.
    def clear_stale_auto_generated_name!(entry)
      entry.name = "" if entry_params[:name].blank?
    end

    def set_new_transaction_form_options
      accessible_accounts_scope = accessible_accounts

      @account_currencies = accessible_accounts_scope.pluck(:id, :currency).to_h
      @manual_accounts = accessible_accounts_scope
        .manual
        .active
        .alphabetically
        .includes(:account_providers, logo_attachment: :blob)
        .to_a
      @categories = Current.family.categories.alphabetically_by_hierarchy.to_a
      @merchants = Current.family.available_merchants_for(Current.user).with_attached_logo.alphabetically.to_a
      @tags = Current.family.tags.alphabetically.to_a
    end

    # Autocomplete suggestions for the name field, only queried when the
    # auto-generate setting is on: lets a user reuse an actual previous name
    # instead of leaving the field blank (which would produce a generic
    # fallback), the same way it curbs bulk-identical generated names.
    def recent_transaction_names
      return [] unless Current.family.auto_generate_transaction_names?

      # Current.accessible_entries (not Entry.family_scope), so a non-admin
      # family member never sees names from accounts that weren't shared
      # with them -- family-wide settings still respect per-account sharing.
      # Its underlying Account.accessible_by scope is itself a `.distinct`
      # query, so ORDER BY created_at must stay in the pluck'd column list
      # (a plain "pluck(:name)" here would hit Postgres's "SELECT DISTINCT
      # ... ORDER BY expressions must appear in select list"); dedup by name
      # happens in Ruby afterward instead.
      #
      # Checked across every supported locale (like Entry#generic_name?'s
      # unknown-name check), not just the current one -- otherwise an old
      # entry named under a different locale's fallback (e.g. "Unbekannte
      # Transaktion") would still surface as a "recent name" suggestion. A
      # full generic_name? reuse isn't practical here since it also needs a
      # loaded entryable.category, which isn't available cheaply from a pluck.
      unknown_names = LanguagesHelper::SUPPORTED_LOCALES.map { |locale| I18n.t("transactions.unknown_name", locale: locale) }

      Current.accessible_entries
        .where(entryable_type: "Transaction")
        .where.not(name: unknown_names)
        .order(created_at: :desc)
        .limit(200)
        .pluck(:name, :created_at)
        .map(&:first)
        .uniq
        .first(20)
    end

    # Filters entry_params based on the user's permission on the account.
    # read_write users can only annotate (category, tags, notes, merchant).
    # read_only users cannot update anything.
    def permitted_entry_params
      permitted = case entry_permission
      when :owner, :full_control
        entry_params
      when :read_write
        # Annotate only: category, tags, merchant, notes
        ep = entry_params.slice(*AccountShare::ANNOTATE_ENTRY_ATTRIBUTES)
        if entry_params[:entryable_attributes].present?
          ep[:entryable_attributes] = entry_params[:entryable_attributes].slice(:id, *AccountShare::ANNOTATE_TRANSACTION_ATTRIBUTES)
        end
        ep
      else
        {} # read_only — no edits allowed
      end

      # A split part's amount, currency and kind belong to the split; the editor
      # disables them, and a change would make the parts stop adding up to the
      # parent or turn a transfer leg back into a plain transaction.
      if @entry.split_child?
        permitted = permitted.except(:amount, :currency)
        permitted[:entryable_attributes] = permitted[:entryable_attributes].except(:kind) if permitted[:entryable_attributes]
      end

      # A matched transfer's category lives on its outflow leg; a category
      # sent for the inflow leg would look saved but never reach a budget.
      if permitted[:entryable_attributes]&.key?(:category_id) && @entry.transaction.category_set_on_transfer_outflow?
        permitted[:entryable_attributes] = permitted[:entryable_attributes].except(:category_id)
      end

      permitted
    end

    # Permitted search filters from the q param, with blank values and
    # unsupported ai_status / attachment_status values dropped.
    def search_params
      cleaned_params = params.fetch(:q, {})
              .permit(
                :start_date, :end_date, :search, :amount,
                :amount_operator, :active_accounts_only,
                accounts: [], account_ids: [],
                categories: [], merchants: [], types: [], tags: [], status: [], ai_status: [],
                attachment_status: [], reconcile_status: []
              )
              .to_h
              .compact_blank

      cleaned_params.delete(:amount_operator) unless cleaned_params[:amount].present?

      if cleaned_params[:ai_status]
        cleaned_params[:ai_status] &= Transaction::Search::AI_STATUSES
        cleaned_params.delete(:ai_status) if cleaned_params[:ai_status].empty?
      end

      if cleaned_params[:attachment_status]
        cleaned_params[:attachment_status] &= Transaction::Search::ATTACHMENT_STATUSES
        cleaned_params.delete(:attachment_status) if cleaned_params[:attachment_status].empty?
      end

      cleaned_params
    end

    def store_params!
      if should_restore_params?
        params_to_restore = {}

        params_to_restore[:q] = stored_params["q"].presence || {}
        params_to_restore[:page] = stored_params["page"].presence || 1
        params_to_restore[:per_page] = stored_params["per_page"].presence || 50

        redirect_to transactions_path(params_to_restore)
      else
        Current.session.update!(
          prev_transaction_page_params: {
            q: search_params,
            page: params[:page],
            per_page: params[:per_page].presence || stored_params["per_page"]
          }
        )
      end
    end

    def should_restore_params?
      request.query_parameters.blank? && (stored_params["q"].present? || stored_params["page"].present? || stored_params["per_page"].present?)
    end

    def stored_params
      Current.session.prev_transaction_page_params
    end

    # Helper methods for convert_to_trade

    # Resolve the allowed ticker/provider before locking the source, avoiding network calls under its lock.
    def resolve_security_for_conversion
      user_country = Current.family.country

      if params[:security_id] == "__custom__"
        # User selected "Enter custom ticker" - check for combobox selection or manual entry
        if params[:ticker].present?
          # Combobox selection: format is "SYMBOL|EXCHANGE|PROVIDER"
          parsed = Security.parse_combobox_id(params[:ticker])
          if parsed[:ticker].blank?
            flash[:alert] = t("transactions.convert_to_trade.errors.enter_ticker")
            redirect_back_or_to transactions_path
            return nil
          end
          Security::Resolver.new(
            parsed[:ticker].strip,
            exchange_operating_mic: parsed[:exchange_operating_mic] || params[:exchange_operating_mic].presence,
            country_code: user_country,
            price_provider: parsed[:price_provider]
          ).resolve
        elsif params[:custom_ticker].present?
          # Manual entry from combobox's name_when_new or fallback text field
          Security::Resolver.new(
            params[:custom_ticker].strip,
            exchange_operating_mic: params[:exchange_operating_mic].presence,
            country_code: user_country
          ).resolve
        else
          flash[:alert] = t("transactions.convert_to_trade.errors.enter_ticker")
          redirect_back_or_to transactions_path
          return nil
        end
      elsif params[:security_id].present?
        found = Security.find_by(id: params[:security_id])
        unless found
          flash[:alert] = t("transactions.convert_to_trade.errors.security_not_found")
          redirect_back_or_to transactions_path
          return nil
        end
        found
      elsif params[:ticker].present?
        # Direct combobox (no existing holdings) - format is "SYMBOL|EXCHANGE|PROVIDER"
        parsed = Security.parse_combobox_id(params[:ticker])
        if parsed[:ticker].blank?
          flash[:alert] = t("transactions.convert_to_trade.errors.enter_ticker")
          redirect_back_or_to transactions_path
          return nil
        end
        Security::Resolver.new(
          parsed[:ticker].strip,
          exchange_operating_mic: parsed[:exchange_operating_mic] || params[:exchange_operating_mic].presence,
          country_code: user_country,
          price_provider: parsed[:price_provider]
        ).resolve
      elsif params[:custom_ticker].present?
        # Manual entry from combobox's name_when_new (no existing holdings path)
        Security::Resolver.new(
          params[:custom_ticker].strip,
          exchange_operating_mic: params[:exchange_operating_mic].presence,
          country_code: user_country
        ).resolve
      end.tap do |security|
        if security.nil? && !performed?
          flash[:alert] = t("transactions.convert_to_trade.errors.select_security")
          redirect_back_or_to transactions_path
        end
      end
    end

    # Validate submitted trade values and infer a missing value from the locked amount.
    def calculate_qty_and_price
      amount = @entry.amount.abs
      qty = params[:qty].present? ? params[:qty].to_d.abs : nil
      price = params[:price].present? ? params[:price].to_d : nil

      if qty.nil? && price.nil?
        flash[:alert] = t("transactions.convert_to_trade.errors.enter_qty_or_price")
        redirect_back_or_to transactions_path, status: :see_other
        return [ nil, nil ]
      elsif qty.nil? && price.present? && price > 0
        qty = (amount / price).round(6)
      elsif price.nil? && qty.present? && qty > 0
        price = (amount / qty).round(4)
      end

      if qty.nil? || qty <= 0 || price.nil? || price <= 0
        flash[:alert] = t("transactions.convert_to_trade.errors.invalid_qty_or_price")
        redirect_back_or_to transactions_path, status: :see_other
        return [ nil, nil ]
      end

      [ qty, price ]
    end
end