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.

See Also:

Instance Method Summary collapse

Instance Method Details

#all_line_itemsActiveRecord::Relation<LineItem>

Non-shipping line items across the customer's orders, quotes, invoices,
and room configurations.

Returns:

  • (ActiveRecord::Relation<LineItem>)

    non-shipping line items for all
    of the customer's transactional records



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_opportunitiesActiveRecord::Relation<Opportunity>

Returns every opportunity in this customer's party scope.

Returns:

  • (ActiveRecord::Relation<Opportunity>)

    all opportunities visible to this customer

See Also:



13
14
15
# File 'app/concerns/models/customer_queries.rb', line 13

def all_opportunities
  Query::PartyScopedOpportunities.call(self)
end

#all_ordersActiveRecord::Relation<Order>

Returns every order in this customer's party scope.

Returns:

  • (ActiveRecord::Relation<Order>)

    all orders visible to this customer

See Also:



21
22
23
# File 'app/concerns/models/customer_queries.rb', line 21

def all_orders
  Query::PartyScopedOrders.call(self)
end

#all_uploadsActiveRecord::Relation<Upload>

Uploads attached to this customer or to any of its tax exemptions, newest first.

Returns:

  • (ActiveRecord::Relation<Upload>)

    party uploads plus tax-exemption uploads



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.

Returns:

  • (Boolean)

    true when the resolved sales rep has
    hold_quotes_for_transmission enabled



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.

Parameters:

  • existing_name (String, nil) (defaults to: nil)

    base name to start from; when nil,
    begins at #{Opportunity::UNKNOWN_JOB_NAME_PREFIX} #1

Returns:

  • (String)

    the first name not already taken by the customer's
    opportunities (comparison is case-insensitive)



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.

Returns:

  • (Boolean)

    true when an active, non-pending order exists



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).

Returns:

  • (Boolean)

    true when at least one active room configuration exists



28
29
30
# File 'app/concerns/models/customer_queries.rb', line 28

def project_in_progress?
  room_configurations.active.exists?
end

#quotes_complete_for_selectArray<Array(String, Integer)>

Pairs of reference number and id for the customer's complete quotes.

Returns:

  • (Array<Array(String, Integer)>)

    [reference_number, id] pairs for select options



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