Class: PurchaseOrderSearch

Inherits:
Search show all
Defined in:
app/models/purchase_order_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, allowed_role_ids, #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, 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_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

.custom_criteria_keysObject



128
129
130
# File 'app/models/purchase_order_search.rb', line 128

def self.custom_criteria_keys
  %w[item_ids item_states]
end

.item_filtered_sql(item_ids, item_states) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'app/models/purchase_order_search.rb', line 94

def self.item_filtered_sql(item_ids, item_states)
  item_count_filtered_sql = +"select sum(poi.quantity) from purchase_order_items poi where poi.purchase_order_id = view_purchase_orders.id"
  item_count_filtered_sql << " and poi.item_id IN (#{item_ids.join(',')})" if item_ids.present?
  if item_states.present?
    state_filter = item_states.map { |e| "'#{e}'" }.join(',')
    item_count_filtered_sql << " and poi.state IN (#{state_filter})"
  end
  item_count_filtered_sql
end

.item_filtered_without_items_already_receipted_sql(item_ids, item_states) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/purchase_order_search.rb', line 104

def self.item_filtered_without_items_already_receipted_sql(item_ids, item_states)
  item_count_filtered_sql = +"select sum(quantity) from (
                              select sum(poi.quantity) as quantity
                              from purchase_order_items poi
                              where poi.purchase_order_id = view_purchase_orders.id"
  item_count_filtered_sql << " and poi.item_id = #{item_ids.first} " if item_ids.present?
  if item_states.present?
    state_filter = item_states.map { |e| "'#{e}'" }.join(',')
    item_count_filtered_sql << " and poi.state IN (#{state_filter})"
  end
  item_count_filtered_sql << "union all
                              select (sum(si.quantity) *-1)  as quantity
                              from purchase_order_items poi2
                              inner join shipment_items si on poi2.id = si.purchase_order_item_id and si.state = 'fully_receipted'
                              where poi2.purchase_order_id = view_purchase_orders.id "
  item_count_filtered_sql << " and poi2.item_id = #{item_ids.first} " if item_ids.present?
  if item_states.present?
    state_filter = item_states.map { |e| "'#{e}'" }.join(',')
    item_count_filtered_sql << " and poi2.state IN (#{state_filter})"
  end
  item_count_filtered_sql << " )a"
  item_count_filtered_sql
end

.search_nameObject



140
141
142
# File 'app/models/purchase_order_search.rb', line 140

def self.search_name
  'PO'
end

Instance Method Details

#apply_custom_criteria(results) ⇒ Object



132
133
134
135
136
137
138
# File 'app/models/purchase_order_search.rb', line 132

def apply_custom_criteria(results)
  return results unless query_params['item_ids'].present? || query_params['item_states'].present?

  item_count_filtered_sql = self.class.item_filtered_sql(query_params['item_ids'], query_params['item_states'])
  prepend_custom_column(:item_count_filtered, raw: "(#{item_count_filtered_sql}) as item_count_filtered", data_type: :integer)
  results.where("(#{item_count_filtered_sql}) > 0")
end