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



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

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

#calculate_totalObject



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

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



105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/ecom/core/crew_time.rb', line 105

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



56
57
58
59
60
61
62
# File 'app/models/ecom/core/crew_time.rb', line 56

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

#computed_hourObject

A similar method as ‘compute_hours` but this one computes hours for the the currently presisted checkin_time and checkout_time



120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/ecom/core/crew_time.rb', line 120

def computed_hour
  # Reparse time to avoid errors caused by date differences
  range = define_range
  start = Time.zone.parse(checkin_time_was.strftime('%I:%M%p'))
  finish = Time.zone.parse(checkout_time_was.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

#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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/ecom/core/crew_time.rb', line 69

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.



92
93
94
95
96
97
98
99
100
101
# File 'app/models/ecom/core/crew_time.rb', line 92

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



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

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



32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/ecom/core/crew_time.rb', line 32

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

  if new_record? & !persisted?
    if attendance_sheet_entry.total_hours + compute_hours > 8
      errors.add(:attendance_sheet_entry, 'has more than 8 hours')
    end
  elsif attendance_sheet_entry.total_hours + compute_hours - computed_hour > 8
    errors.add(:attendance_sheet_entry, 'has more than 8 hours')
  end
end