Module: EmployeeWorkSchedulesHelper

Includes:
EmployeeTimeOffsHelper
Included in:
EmployeeEventsController
Defined in:
app/helpers/employee_work_schedules_helper.rb

Overview

== Schema Information

Table name: employee_work_schedules

id :integer not null, primary key
employee_id :integer
created_at :datetime not null
updated_at :datetime not null
legacy_week :integer
week :datetime
monday_from :datetime
tuesday_from :datetime
wednesday_from :datetime
thursday_from :datetime
friday_from :datetime
saturday_from :datetime
sunday_from :datetime
monday_to :datetime
tuesday_to :datetime
wednesday_to :datetime
thursday_to :datetime
friday_to :datetime
saturday_to :datetime
sunday_to :datetime
lunch :datetime

Instance Method Summary collapse

Methods included from EmployeeTimeOffsHelper

#human_duration, #humanize, #humanize_partial_hours, #humanize_short, #integer_hours

Methods included from TimeOffPlannedWorkDayHelper

#time_off_planned_work_day_summary

Instance Method Details

#define_all_wk_fields(hash) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/helpers/employee_work_schedules_helper.rb', line 31

def define_all_wk_fields(hash)
  d = hash.deep_symbolize_keys
  d.each_key do |weekday|
    d[weekday].each_key do |slot|
      time_ranges = {}
      work = 0
      lunch = 0
      d[weekday][slot].each_key do |range|
        if slot.to_s == "work"
          work = work + 1
        elsif slot.to_s == "lunch"
          lunch = lunch + 1
        end
        if !range.to_s.to_time.nil?
          time_ranges[range.to_s] = d[weekday][slot][range]
        end
      end
      if slot.to_s == "work" && work < 3
        ((work+1)..3).each do |work_range|
          time_ranges["#{slot}_from_#{work_range}"] = "#{slot}_to_#{work_range}"
        end
      elsif slot.to_s == "lunch" && lunch < 2
        ((lunch+1)..2).each do |lunch_range|
          time_ranges["#{slot}_from_#{lunch_range}"] = "#{slot}_to_#{lunch_range}"
        end
      end
      d[weekday][slot] = time_ranges
    end
  end
  return d
end

#schedule_in_specific_day(hash, day) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/employee_work_schedules_helper.rb', line 88

def schedule_in_specific_day(hash, day)
  unless hash.nil?
    working_hours = hash[day.to_sym][:work]
    lunch_hours = hash[day.to_sym][:lunch]
    w_from = (working_hours.to_a.first[0].to_s.to_time.strftime("%I:%M %p") rescue 0)
    w_to = (working_hours.to_a.last[1].to_s.to_time.strftime("%I:%M %p") rescue 0)
    lunch_string = ""
    lunch_hours.each do |lunch|
      l_from = (lunch[0].to_s.to_time.strftime("%I:%M %p") rescue 0)
      l_to = (lunch[1].to_s.to_time.strftime("%I:%M %p") rescue 0)
      lunch_string = lunch_string + "Lunch: #{l_from} - #{l_to} "
    end
    return "Working: #{w_from} - #{w_to}. #{lunch_string}"
  end
end

#working_hours_a_week_giving_hash(hash) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/helpers/employee_work_schedules_helper.rb', line 73

def working_hours_a_week_giving_hash(hash)
  accumulated = 0
  hash.each_key do |weekday|
    weekday_hsh = hash.dig(weekday,:work)
    if weekday_hsh.present?
      weekday_hsh.each do |range|
        accumulated += ((range[1].to_s.to_time rescue 0) - (range[0].to_s.to_time rescue 0)) rescue 0
      end
    end
  end
  human_duration = [accumulated / 3600, ((accumulated / 60) % 60)].map { |t| t.to_i.to_s.rjust(2,'0') }.join(':') + " hours" unless accumulated.nil?
  human_duration ||= 0
  return human_duration
end

#working_seconds_on_a_day_giving_hash(hash, day) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'app/helpers/employee_work_schedules_helper.rb', line 63

def working_seconds_on_a_day_giving_hash(hash, day)
  working_hours = hash.dig(day.to_sym,:work)
  return 0 if working_hours.blank?
  accumulated = 0
  working_hours.each do |range|
    accumulated += ((range[1].to_s.to_time rescue 0) - (range[0].to_s.to_time rescue 0) rescue 0)
  end
  return accumulated
end