Class: ActivityTypeAssignmentQueue

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

Overview

Links an activity assignment queue to the activity types
== Schema Information

Table name: activity_type_assignment_queues
Database name: primary

id :integer not null, primary key
activity_type_id :integer not null
assignment_queue_id :integer not null
company_id :integer not null
fallback_employee_id :integer

Indexes

activity_type_assignment_queues_assignment_queue_id_idx (assignment_queue_id)
activity_type_assignment_queues_company_id_idx (company_id)
idx_activity_types_assignment_queues (activity_type_id,assignment_queue_id,company_id) UNIQUE

Foreign Keys

activity_type_assignment_queues_activity_type_id_fk (activity_type_id => activity_types.id)
activity_type_assignment_queues_assignment_queue_id_fk (assignment_queue_id => assignment_queues.id)
activity_type_assignment_queues_company_id_fk (company_id => companies.id)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Class Method Summary 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::EventPublishable

#publish_event

Class Method Details

.employees_id_by_role_id(rid) ⇒ Object



112
113
114
# File 'app/models/activity_type_assignment_queue.rb', line 112

def self.employees_id_by_role_id(rid)
  Employee.active_employees.by_direct_role_id(rid).pluck(:id)
end

Instance Method Details

#activity_typeActivityType



29
# File 'app/models/activity_type_assignment_queue.rb', line 29

belongs_to :activity_type, inverse_of: :activity_type_assignment_queues, optional: true

#assignment_queueAssignmentQueue



31
# File 'app/models/activity_type_assignment_queue.rb', line 31

belongs_to :assignment_queue, inverse_of: :activity_type_assignment_queues, optional: true

#companyCompany

Returns:

See Also:



30
# File 'app/models/activity_type_assignment_queue.rb', line 30

belongs_to :company, inverse_of: :activity_type_assignment_queues, optional: true

#fallback_employeeEmployee

Returns:

See Also:



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

belongs_to :fallback_employee, class_name: 'Employee', optional: true

#get_first_resource(customer, current_user_id = nil, target_date = nil, priority = nil, assignment_matrix = nil, resource = nil, options = {}) ⇒ Object

Based on a customer will iterate through the queue determined for the activity type and return the first resource
customer is the customer
current_user_id is the logged in customer, in batch mode pass the creator or the updater or hte activity to this.
target_date is when the task is due, if nil then we don't check if the rep has availability that day



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
# File 'app/models/activity_type_assignment_queue.rb', line 38

def get_first_resource(customer, current_user_id=nil, target_date = nil, priority = nil, assignment_matrix=nil, resource=nil, options={}) #, time_sensitive = false)
  excluded_rep_ids ||= [] #Here we will maintain an array of rep that have been skipped so we can be more efficient in reprocessing.
  assigned_rep_id = nil
  queue_entries = [] + assignment_queue.queue
  rep_ids = queue_entries.map do |queue_entry|
    AssignmentQueue.interpolate_with_customer(queue_entry,
                                              customer,
                                              current_user_id,
                                              target_date,
                                              excluded_rep_ids,
                                              assignment_matrix,
                                              resource)
  end
  rep_ids = rep_ids.compact.uniq #Keep it tidy
  if rep_ids.empty?
  	#Unassignable, try fallback
  	assigned_rep_id = fallback_employee_id
  	raise AssignmentQueue::UnassignableActivity unless assigned_rep_id
  else
   rep_ids.each do |rep_id|
     logger.debug " ** Queue entry yielded rep id #{rep_id}"
     can_take_activities = false
      if block_given? #Use the block to determine rep availabilities.
       can_take_activities = yield(rep_id)
     elsif target_date.nil? or options[:ignore_target_date]
        can_take_activities = true
      else
       rep = Employee.find(rep_id)
       can_take_activities = rep.can_take_activities_on_day?(target_date,priority)
     end
     if can_take_activities
        assigned_rep_id = rep_id
        break
      else
       logger.debug " ** Rep #{rep_id} is not working or unable to take more activities on #{target_date}, skipping"
       excluded_rep_ids << rep_id
     end
   end
 end
  assigned_rep_id
end

#get_full_queue_resources(c, current_user_id = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/activity_type_assignment_queue.rb', line 80

def get_full_queue_resources(c, current_user_id=nil)
  queue_entries = []
  rep_ids = []
  assignment_queue.queue.each do |q|
    case q
    when 'primary_sales_rep'
      queue_entries << c.try(:primary_sales_rep_id)
    when 'primary_sales_rep_backup'
      queue_entries << c.try(:primary_sales_rep).try(:backup_rep_id)
    when 'secondary_sales_rep'
      queue_entries << c.secondary_sales_rep_id
    when 'local_sales_rep'
      queue_entries << c.local_sales_rep_id
    when 'service_rep'
      queue_entries << c.service_rep_id
    when 'current_user'
      queue_entries << current_user_id
    else
      t,eid=q.split('|')
      case t
      when 'Employee'
        queue_entries << eid.to_i
      when 'LBR'
        queue_entries += self.class.employees_id_by_role_id(eid.to_i).shuffle
      end
    end
    queue_entries = queue_entries.uniq.compact
    break if !activity_type.priority_tier? and queue_entries.present? #only need one entry for low tiers
  end
  queue_entries
end