Class: SalesRepQueue

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/sales_rep_queue.rb

Overview

== Schema Information

Table name: sales_rep_queues
Database name: primary

id :integer not null, primary key
apply_paired_sales_rep :boolean
local_sales_rep_ids :integer default([]), is an Array
local_sales_rep_stack :integer default([]), is an Array
local_sales_rep_weights :integer default([]), is an Array
primary_sales_rep_ids :integer default([]), is an Array
primary_sales_rep_stack :integer default([]), is an Array
primary_sales_rep_weights :integer default([]), is an Array
secondary_sales_rep_ids :integer default([]), is an Array
secondary_sales_rep_stack :integer default([]), is an Array
secondary_sales_rep_weights :integer default([]), is an Array
created_at :datetime
updated_at :datetime
creator_id :integer
store_id :integer
updater_id :integer

Indexes

idx_store_id (store_id)

Constant Summary collapse

SALES_REP_TYPES =

Recognised sales rep types.

%i[local_sales_rep primary_sales_rep secondary_sales_rep].freeze

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#store_idObject (readonly)

Validates one queue per store.

Validations:



37
# File 'app/models/sales_rep_queue.rb', line 37

validates :store_id, uniqueness: true

Instance Method Details

#first_eligible_sales_queue_entry(customer) ⇒ SalesRepQueueEntry?

First queue entry whose customer filter matches the given customer.

Parameters:

  • customer (Customer)

    customer being assigned

Returns:



47
48
49
# File 'app/models/sales_rep_queue.rb', line 47

def first_eligible_sales_queue_entry(customer)
  sales_rep_queue_entries.to_a.find { |qe| qe.applies_to_customer?(customer) }
end

#fix_orderObject

Move the empty customer filter (the default) to the bottom



84
85
86
# File 'app/models/sales_rep_queue.rb', line 84

def fix_order
  sales_rep_queue_entries.reject(&:customer_filter).each(&:move_to_bottom)
end

#get_next_sales_rep_assignments_for_customer(customer) ⇒ Hash{Symbol => Employee}?

Sales rep assignments for the given customer, per the first matching queue entry.

Parameters:

  • customer (Customer)

    customer being assigned

Returns:

  • (Hash{Symbol => Employee}, nil)

    rep-type → employee map, or nil when no entry applies



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/sales_rep_queue.rb', line 54

def get_next_sales_rep_assignments_for_customer(customer)
  # find out which sales rep queue entry criteria apply
  selected_sales_reps = {}
  sales_rep_queue_entry_that_applies = first_eligible_sales_queue_entry(customer)
  return unless sales_rep_queue_entry_that_applies

  sales_rep_types = SALES_REP_TYPES.dup

  sales_rep_types.each do |sales_rep_type|
    if (selected_sales_rep = sales_rep_queue_entry_that_applies.get_sales_rep(sales_rep_type))
      selected_sales_reps[:"#{sales_rep_type}_id"] = selected_sales_rep.id
    end
    # check if paired sales rep is to be applied and if so, that there is one defined
    next unless sales_rep_queue_entry_that_applies.apply_paired_sales_rep && (backup_rep = selected_sales_rep.try(:employee_record).try(:backup_rep))

    # yes, so local gets applied first with paired being primary
    case sales_rep_type
    when :local_sales_rep
      selected_sales_reps[:primary_sales_rep_id] = backup_rep.try(:id)
      sales_rep_types.delete(:primary_sales_rep)
    when :primary_sales_rep
      # otherwise primary gets applied with paired being secondary
      selected_sales_reps[:secondary_sales_rep_id] = backup_rep.try(:id)
      sales_rep_types.delete(:secondary_sales_rep)
    end
  end
  selected_sales_reps
end

#sales_rep_queue_entriesActiveRecord::Relation<SalesRepQueueEntry>

Queue entries (sales reps) in assignment order.

Returns:

See Also:



34
# File 'app/models/sales_rep_queue.rb', line 34

has_many :sales_rep_queue_entries, -> { order(:position) }, inverse_of: :sales_rep_queue

#storeStore

Store this round-robin assignment queue belongs to.

Returns:

See Also:



32
# File 'app/models/sales_rep_queue.rb', line 32

belongs_to :store, optional: true

#to_sString

Store name, for select dropdowns and logs.

Returns:

  • (String)


90
91
92
# File 'app/models/sales_rep_queue.rb', line 90

def to_s
  store.name
end