Class: OutgoingPaymentSearch

Inherits:
Search show all
Defined in:
app/models/outgoing_payment_search.rb

Overview

== Schema Information

Table name: searches
Database name: primary

id :integer not null, primary key
name :string(255)
persist :boolean default(FALSE)
pinned :boolean default(FALSE), not null
query_params :jsonb
result_set_size :integer default(0)
selected_columns :string is an Array
set_limit :integer default(25)
sort_column :string(255)
sort_columns :string default([]), not null, is an Array
sort_direction :string(255) default("ASC")
type :string(255) not null
created_at :datetime
updated_at :datetime
employee_id :integer

Indexes

employee_id_pinned (employee_id,pinned)
employee_id_type (employee_id,type)

Constant Summary

Constants inherited from Search

Search::DISTANCE_SEARCH_KEYS

Instance Attribute Summary

Attributes inherited from Search

#composite_columns, #pagy_count, #pagy_limit, #pagy_page, #sort_column1, #sort_column2, #sort_column3, #sql_select_columns

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Search

#all_composite_columns, #append_custom_column, #append_ouput_column, #append_to_sql_select_columns, #applicable_sort_terms, #apply_array_match, #apply_criteria, #apply_customer_cross_reference, #apply_distance_search, #apply_select, #apply_sort, #available_events, available_output_columns, #available_output_columns, base_search_class_name, #cleanup_search_results, composite_column, #composite_column, custom_criteria_keys, database_columns, default_columns, default_sort, #discard_excess, #effective_name, #effective_short_name, #employee, #enqueue_navbar_pinned_refresh, #fast_count, favorites, friendly_column_name, #get_pinned_results, #get_sort_term, global_favorite, has_role_for_search?, #human_query_params, instantiate_query_template, #instantiate_resource_updater, #limit_options, main_resource_class, main_resource_table, mass_actions_for_select, #mass_export, maximum_unpersisted_queries, #normalize_for_mass_update, options, options_classes, #output_columns_for_select, #pagy_from_search, #perform, #perform_selected, #pinned_query, #prepend_custom_column, #prepend_output_column, query_favorites_templates, #ransack_probe_params, recent_searches_for_employee_id, #record_list, #refresh_pinned_results, #remove_other_pins, search_name, #search_name, #search_results, search_type, search_type_humanized, select_sort_columns, #select_sort_columns, #select_statement, #set_defaults, #set_sort_term, #sort_columns_for_select, #sort_columns_to_hash, #target_geocoding, unpersisted_queries_quote_reached?, #unrecord_list, #validated_selected_columns, view_resource_class, view_resource_klass, #view_resource_klass, view_resource_table, visible?, with_search_results

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.allowed_role_idsObject



48
49
50
# File 'app/models/outgoing_payment_search.rb', line 48

def self.allowed_role_ids
  [5, 6] # Accounting rep and managers
end

Instance Method Details

#mass_approve_check(_params, current_user = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/outgoing_payment_search.rb', line 83

def mass_approve_check(_params, current_user = nil)
  path = UrlHelper.instance.accounts_payable_dashboard_path
  return { status: :error, redirect_to_path: path, message: "You don't have permission to approve checks." } \
    unless current_user&.&.can?(:approve_check, OutgoingPayment)
  return { status: :error, redirect_to_path: path, message: 'No payments were selected.' } if pinned_query.empty?
  return { status: :error, redirect_to_path: path, message: 'All payments must be checks pending review.' } \
    if pinned_query.any? { |pq| (pq.resource.category != 'check') || !pq.resource.pending_review? }

  jid = MassSearch::OutgoingPaymentApproveCheckWorker.perform_async(
    'search_id'     => id,
    'action_params' => {},
    'user_id'       => current_user&.id,
    'locale'        => I18n.locale.to_s
  )
  { status: :ok, job_id: jid }
end

#mass_print_check(params, current_user = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/outgoing_payment_search.rb', line 65

def mass_print_check(params, current_user = nil)
  path = UrlHelper.instance.accounts_payable_dashboard_path
  return { status: :error, redirect_to_path: path, message: 'No payments were selected.' } if pinned_query.empty?
  return { status: :error, redirect_to_path: path, message: 'All payments must be approved checks.' } if pinned_query.any? { |pq| (pq.resource.category != 'check') || pq.resource.no_check? || pq.resource.pending_review? }
  return { status: :error, redirect_to_path: path, message: 'You must wait for any existing jobs to finish before starting a new one.' } if pinned_query.any? { |pq| pq.resource.has_active_background_job? }

  payment_ids = pinned_query.collect(&:resource_id)
  regenerate_pdf = params[:regenerate_pdf].to_b
  job_options = {
    payment_ids: payment_ids,
    current_user_id: current_user.try(:id),
    regenerate_pdf: regenerate_pdf
  }.stringify_keys
  job_id = PrintAllChecksWorker.perform_async(job_options)
  OutgoingPayment.where(id: payment_ids).update_all(job_id: job_id)
  { status: :ok, job_id: job_id }
end

#payments_for_mass_actionObject

Loads OutgoingPayments for mass action views. Uses direct SearchResult query
instead of association to avoid evaluation issues in view rendering.



54
55
56
57
58
59
60
61
62
63
# File 'app/models/outgoing_payment_search.rb', line 54

def payments_for_mass_action
  payment_ids = SearchResult.where(search_id: id, resource_type: 'OutgoingPayment')
                            .order(:position)
                            .pluck(:resource_id)
                            .compact
  return [] if payment_ids.empty?

  payments_by_id = OutgoingPayment.where(id: payment_ids).includes(:supplier, :bank_account).index_by(&:id)
  payment_ids.filter_map { |payment_id| payments_by_id[payment_id] }
end