Module: Models::CustomerQueries
- Extended by:
- ActiveSupport::Concern
- Included in:
- Customer
- Defined in:
- app/concerns/models/customer_queries.rb
Overview
Aggregate queries, uploads, line items, and room submissions for Customer.
Instance Method Summary collapse
-
#all_line_items ⇒ ActiveRecord::Relation<LineItem>
Non-shipping line items across the customer's orders, quotes, invoices, and room configurations.
-
#all_opportunities ⇒ ActiveRecord::Relation<Opportunity>
Returns every opportunity in this customer's party scope.
-
#all_orders ⇒ ActiveRecord::Relation<Order>
Returns every order in this customer's party scope.
-
#all_uploads ⇒ ActiveRecord::Relation<Upload>
Uploads attached to this customer or to any of its tax exemptions, newest first.
-
#hold_quotes_for_transmission_default? ⇒ Boolean
Whether quotes should be held for transmission by default, per the customer's primary/secondary/local sales rep preferences.
-
#next_opportunity_name(existing_name = nil) ⇒ String
Generates an unused opportunity name for this customer, suffixing a counter when the base name already exists.
-
#orders? ⇒ Boolean
Whether the customer has any non-pending active orders.
-
#project_in_progress? ⇒ Boolean
Whether the customer has any active room configuration (project in progress).
-
#quotes_complete_for_select ⇒ Array<Array(String, Integer)>
Pairs of reference number and id for the customer's complete quotes.
Instance Method Details
#all_line_items ⇒ ActiveRecord::Relation<LineItem>
Non-shipping line items across the customer's orders, quotes, invoices,
and room configurations.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/concerns/models/customer_queries.rb', line 61 def all_line_items where_sql = <<~SQL.squish (resource_type = 'Order' and resource_id IN (?)) OR (resource_type = 'Quote' and resource_id IN (?)) OR (resource_type = 'Invoice' and resource_id IN (?)) OR (resource_type = 'RoomConfiguration' and resource_id IN (?)) SQL LineItem.non_shipping.where(where_sql, customer.order_ids, customer.quote_ids, customer.invoice_ids, customer.room_configuration_ids) end |
#all_opportunities ⇒ ActiveRecord::Relation<Opportunity>
Returns every opportunity in this customer's party scope.
13 14 15 |
# File 'app/concerns/models/customer_queries.rb', line 13 def all_opportunities Query::PartyScopedOpportunities.call(self) end |
#all_orders ⇒ ActiveRecord::Relation<Order>
Returns every order in this customer's party scope.
21 22 23 |
# File 'app/concerns/models/customer_queries.rb', line 21 def all_orders Query::PartyScopedOrders.call(self) end |
#all_uploads ⇒ ActiveRecord::Relation<Upload>
Uploads attached to this customer or to any of its tax exemptions, newest first.
49 50 51 52 53 54 |
# File 'app/concerns/models/customer_queries.rb', line 49 def all_uploads te_upload_ids = tax_exemptions.where.not(upload_id: nil).pluck(:upload_id) Upload.where(resource_type: 'Party', resource_id: id) .or(Upload.where(id: te_upload_ids)) .order(Upload[:created_at].desc) end |
#hold_quotes_for_transmission_default? ⇒ Boolean
Whether quotes should be held for transmission by default, per the
customer's primary/secondary/local sales rep preferences.
103 104 105 106 |
# File 'app/concerns/models/customer_queries.rb', line 103 def hold_quotes_for_transmission_default? rep = primary_sales_rep || secondary_sales_rep || local_sales_rep rep&.heatwave_preferences&.dig('hold_quotes_for_transmission') == 'true' end |
#next_opportunity_name(existing_name = nil) ⇒ String
Generates an unused opportunity name for this customer, suffixing a
counter when the base name already exists.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/concerns/models/customer_queries.rb', line 81 def next_opportunity_name(existing_name = nil) list_of_names = opportunities.pluck(:name).compact.map(&:upcase) next_opp_number = 1 new_name = existing_name || "#{Opportunity::UNKNOWN_JOB_NAME_PREFIX} #1" loop do break unless list_of_names.include?(new_name.strip.upcase) next_opp_number += 1 new_name = if new_name&.start_with?(Opportunity::UNKNOWN_JOB_NAME_PREFIX) "#{Opportunity::UNKNOWN_JOB_NAME_PREFIX} ##{next_opp_number}" else "#{existing_name} (#{next_opp_number})" end end new_name end |
#orders? ⇒ Boolean
Whether the customer has any non-pending active orders.
42 43 44 |
# File 'app/concerns/models/customer_queries.rb', line 42 def orders? orders.active.where.not(state: 'pending').present? end |
#project_in_progress? ⇒ Boolean
Whether the customer has any active room configuration (project in progress).
28 29 30 |
# File 'app/concerns/models/customer_queries.rb', line 28 def project_in_progress? room_configurations.active.exists? end |
#quotes_complete_for_select ⇒ Array<Array(String, Integer)>
Pairs of reference number and id for the customer's complete quotes.
35 36 37 |
# File 'app/concerns/models/customer_queries.rb', line 35 def quotes_complete_for_select quotes.where(state: 'complete').pluck(:reference_number, :id) end |