Class: Ecom::Core::CrewTime

Inherits:
ApplicationRecord show all
Defined in:
app/models/ecom/core/crew_time.rb

Constant Summary collapse

MORNING =

Time Ranges

:morning
AFTERNOON =
:afternoon
FULL_DAY =
:full_day

Instance Method Summary collapse

Instance Method Details

#calculate_hoursObject



39
40
41
42
43
44
45
# File 'app/models/ecom/core/crew_time.rb', line 39

def calculate_hours
  self.hours = if checkout_time.nil? || checkin_time.nil?
                 0
               else
                 compute_hours
               end
end

#calculate_totalObject



47
48
49
# File 'app/models/ecom/core/crew_time.rb', line 47

def calculate_total
  compute_total_for_entry if saved_change_to_hours?
end

#compute_hoursObject

A method to adjust time ranges by trimming any time value outside of the defined morning and afternoon ranges



100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/ecom/core/crew_time.rb', line 100

def compute_hours
  # Reparse time to avoid errors caused by date differences
  range = define_range
  start = Time.zone.parse(checkin_time.strftime('%I:%M%p'))
  finish = Time.zone.parse(checkout_time.strftime('%I:%M%p'))
  day_part = find_range(start, finish)
  left = start.before?(range[day_part][:start]) ? range[day_part][:start] : start
  right = finish.after?(range[day_part][:finish]) ? range[day_part][:finish] : finish
  time = (right - left) / 1.hour
  time -= 1 if day_part == FULL_DAY
  time
end

#compute_total_for_entryObject



51
52
53
54
55
56
57
# File 'app/models/ecom/core/crew_time.rb', line 51

def compute_total_for_entry
  attendance_sheet_entry.total_hours = Ecom::Core::CrewTime.where(
    attendance_sheet_entry: attendance_sheet_entry,
    revised: false
  ).sum(:hours)
  attendance_sheet_entry.save
end

#define_rangeObject

A method to get the available time ranges at a given point. We cannot define the range variables as a constant because the parsing should be done at the exact moment we are about to do time range calculations to avoid errors caused by date mismatches



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/ecom/core/crew_time.rb', line 64

def define_range
  morning = {
    start: Time.zone.parse('5:00 AM'),
    finish: Time.zone.parse('9:00 AM')
  }
  afternoon = {
    start: Time.zone.parse('10:00 AM'),
    finish: Time.zone.parse('2:00 PM')
  }
  full_day = {
    start: Time.zone.parse('5:00 AM'),
    finish: Time.zone.parse('2:00 PM')
  }

  {
    morning: morning,
    afternoon: afternoon,
    full_day: full_day
  }
end

#find_range(start, finish) ⇒ Object

A method to check if checkin and checkout range falls in the morning, afternoon, or both.



87
88
89
90
91
92
93
94
95
96
# File 'app/models/ecom/core/crew_time.rb', line 87

def find_range(start, finish)
  range = define_range
  if start.before?(range[:morning][:finish]) && finish.before?(range[:afternoon][:start])
    :morning
  elsif start.after?(range[:morning][:finish]) && finish.after?(range[:afternoon][:start])
    :afternoon
  else
    :full_day
  end
end

#time_range_validationObject



25
26
27
28
29
# File 'app/models/ecom/core/crew_time.rb', line 25

def time_range_validation
  return unless checkin_time && checkout_time && checkout_time <= checkin_time

  errors.add(:checkout_time, "can't be less than checkin time.")
end

#total_time_validationObject



31
32
33
34
35
36
37
# File 'app/models/ecom/core/crew_time.rb', line 31

def total_time_validation
  return if checkout_time.nil? || checkin_time.nil?

  return unless attendance_sheet_entry.total_hours + compute_hours > 8

  errors.add(:attendance_sheet_entry, 'has more than 8 hours')
end