Module: Models::EmployeeActivityCapacity

Extended by:
ActiveSupport::Concern
Included in:
EmployeeWorkStatus
Defined in:
app/concerns/models/employee_activity_capacity.rb

Overview

Activity capacity calculations and daily limits for Employee.

Instance Method Summary collapse

Instance Method Details

#activity_load_on_day(date, priority = nil) ⇒ Integer

Number of open, non-note activities already targeted on the given day.

Parameters:

  • date (Date)

    the day to count activities for

  • priority (Integer, Array<Integer>, nil) (defaults to: nil)

    optional priority (or priorities) to restrict the count to

Returns:

  • (Integer)

    the day's current activity load



56
57
58
59
60
61
62
63
# File 'app/concerns/models/employee_activity_capacity.rb', line 56

def activity_load_on_day(date, priority = nil)
  rel = activities.non_notes
                  .open_activities
                  .where(Activity[:target_datetime].gteq(date.beginning_of_day))
                  .where(Activity[:target_datetime].lteq(date.end_of_day))
  rel = rel.joins(:activity_type).where(activity_types: { priority: }) if priority.present?
  rel.count
end

#can_take_activities_on_day?(date, priority = nil) ⇒ Boolean

Whether the employee can take on more activities on the given day.

Parameters:

  • date (Date)

    the day to check capacity for

  • priority (Integer, Array<Integer>, nil) (defaults to: nil)

    optional activity priority (or priorities) to check against tier limits

Returns:

  • (Boolean)

    true when the day's capacity is not yet exhausted



11
12
13
14
15
16
# File 'app/concerns/models/employee_activity_capacity.rb', line 11

def can_take_activities_on_day?(date, priority = nil)
  return false unless working_on_day?(date)
  return activity_load_on_day(date) < maximum_activities_per_day if priority.blank?

  check_activity_capacity_by_priority(date, priority)
end

#maximum_activities_per_day(date = nil) ⇒ Integer

Maximum number of activities the employee can take per day.

Parameters:

  • date (Date, nil) (defaults to: nil)

    optional day to scale the limit by that day's capacity ratio; nil uses full capacity

Returns:

  • (Integer)

    the daily activity limit, rounded



21
22
23
24
25
26
27
28
29
30
# File 'app/concerns/models/employee_activity_capacity.rb', line 21

def maximum_activities_per_day(date = nil)
  if date
    return 0 unless working_on_day?(date)

    ratio = capacity_ratio(date)
  end
  ratio ||= 1.0
  max = employee_record.try(:activities_per_day) || EmployeeRecord::DEFAULT_ACTIVITIES_PER_DAY
  (max * ratio).round
end

#maximum_priority_tier_activities_per_day(date = nil) ⇒ Integer

Maximum number of priority-tier activities the employee can take per day.

Parameters:

  • date (Date, nil) (defaults to: nil)

    optional day to scale the limit by that day's capacity ratio

Returns:

  • (Integer)

    the daily priority-tier activity limit, rounded



41
42
43
# File 'app/concerns/models/employee_activity_capacity.rb', line 41

def maximum_priority_tier_activities_per_day(date = nil)
  ((maximum_activities_per_day(date) * priority_tier_activities_ratio) / 100).round
end

#maximum_standard_tier_activities_per_day(date = nil) ⇒ Integer

Maximum number of standard-tier (non-priority) activities the employee can take per day.

Parameters:

  • date (Date, nil) (defaults to: nil)

    optional day to scale the limit by that day's capacity ratio

Returns:

  • (Integer)

    the daily standard-tier activity limit



48
49
50
# File 'app/concerns/models/employee_activity_capacity.rb', line 48

def maximum_standard_tier_activities_per_day(date = nil)
  maximum_activities_per_day(date) - maximum_priority_tier_activities_per_day(date)
end

#priority_tier_activities_ratioInteger

Percentage of the daily capacity reserved for priority-tier activities.

Returns:

  • (Integer)

    the priority-tier ratio (0-100)



34
35
36
# File 'app/concerns/models/employee_activity_capacity.rb', line 34

def priority_tier_activities_ratio
  employee_record.try(:activities_per_day_priority_tiers) || EmployeeRecord::DEFAULT_ACTIVITIES_PER_DAY_PRIORITY_TIERS
end