Module: EmployeeWorkSchedulesHelper

Includes:
EmployeeTimeOffsHelper
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_planned_hours, #time_off_planned_work_day_summary, #time_off_planned_work_day_usual_summary

Instance Method Details

#define_all_wk_fields(hash) ⇒ Object



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
# File 'app/helpers/employee_work_schedules_helper.rb', line 32

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 += 1
        elsif slot.to_s == "lunch"
          lunch += 1
        end
        time_ranges[range.to_s] = d[weekday][slot][range] unless range.to_s.to_time.nil?
      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
  d
end

#schedule_in_specific_day(hash, day) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/helpers/employee_work_schedules_helper.rb', line 112

def schedule_in_specific_day(hash, day)
  return if hash.nil?

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

#working_hours_a_week_giving_hash(hash) ⇒ Object



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/helpers/employee_work_schedules_helper.rb', line 85

def working_hours_a_week_giving_hash(hash)
  accumulated = 0
  hash.each_key do |weekday|
    weekday_hsh = hash.dig(weekday, :work)
    next if weekday_hsh.blank?

    weekday_hsh.each do |range|
      accumulated += begin
        (begin
          range[1].to_s.to_time
        rescue StandardError
          0
        end - begin
          range[0].to_s.to_time
        rescue StandardError
          0
        end)
      rescue StandardError
        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
  human_duration
end

#working_seconds_on_a_day_giving_hash(hash, day) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/helpers/employee_work_schedules_helper.rb', line 62

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 += begin
      begin
        range[1].to_s.to_time
      rescue StandardError
        0
      end - begin
        range[0].to_s.to_time
      rescue StandardError
        0
      end
    rescue StandardError
      0
    end
  end
  accumulated
end