Class: EmployeeWorkSchedule

Inherits:
ApplicationRecord show all
Defined in:
app/models/employee_work_schedule.rb

Overview

== Schema Information

Table name: employee_work_schedules
Database name: primary

id :integer not null, primary key
breaks :text
friday_from :datetime
friday_to :datetime
holidays :text
hours :jsonb
hours_legacy :text
lunch :datetime
monday_from :datetime
monday_to :datetime
saturday_from :datetime
saturday_to :datetime
sunday_from :datetime
sunday_to :datetime
thursday_from :datetime
thursday_to :datetime
time_zone :string
tuesday_from :datetime
tuesday_to :datetime
wednesday_from :datetime
wednesday_to :datetime
week :datetime
work_timezone :integer default(1)
created_at :datetime not null
updated_at :datetime not null
employee_id :integer

Indexes

employee_work_schedules_employee_id_idx (employee_id)

Foreign Keys

fk_rails_... (employee_id => parties.id) ON DELETE => cascade

Constant Summary collapse

WORK_TIMEZONE_PLACEHOLDER =

DB default for work_timezone; rows often keep this without a real selection.
Daily Focus prompt hints skip this id so we do not emit noisy "work_timezone id 1" lines.

1

Belongs to collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.work_schedule_time_selectObject



50
51
52
53
54
55
56
# File 'app/models/employee_work_schedule.rb', line 50

def self.work_schedule_time_select
  result = []
  ('00:00'.to_time.to_i .. '23:45'.to_time.to_i).step(15.minutes.to_i) do |time|
    result << [Time.at(time).strftime('%I:%M %P'), Time.at(time).strftime('%H:%M')]
  end
  result
end

Instance Method Details

#employeeEmployee

Returns:

See Also:



46
# File 'app/models/employee_work_schedule.rb', line 46

belongs_to :employee, inverse_of: :employee_work_schedules, optional: true

#lunch_hours_a_weekObject



105
106
107
108
109
110
111
112
113
# File 'app/models/employee_work_schedule.rb', line 105

def lunch_hours_a_week
  accumulated = 0
  hours.each_key do |weekday|
    hours[weekday][:lunch].each do |range|
      accumulated += (range[1].to_s.to_time rescue 0) - (range[0].to_s.to_time rescue 0)
    end
  end
  return accumulated
end

#lunch_on_a_day(day) ⇒ Object



126
127
128
129
130
131
132
133
# File 'app/models/employee_work_schedule.rb', line 126

def lunch_on_a_day(day)
  hash = hours[day.to_sym][:lunch]
  accumulated = 0
  hash.each do |range|
    accumulated += (range[1].to_s.to_time rescue 0) - (range[0].to_s.to_time rescue 0)
  end
  return accumulated
end

#total_hours_on_a_day(day) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'app/models/employee_work_schedule.rb', line 115

def total_hours_on_a_day(day)
  hash = hours[day.to_sym]
  accumulated = 0
  hash.each_key do |slot|
    hash[slot].each do |range|
      accumulated += (range[1].to_s.to_time rescue 0) - (range[0].to_s.to_time rescue 0)
    end
  end
  return accumulated
end

#working_hours_a_weekObject



95
96
97
98
99
100
101
102
103
# File 'app/models/employee_work_schedule.rb', line 95

def working_hours_a_week
  accumulated = 0
  hours.each_key do |weekday|
    hours[weekday][:work].each do |range|
      accumulated += (range[1].to_s.to_time rescue 0) - (range[0].to_s.to_time rescue 0)
    end
  end
  return accumulated
end

#working_hours_in_timerange(date, from, to, avoid_lunch) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/employee_work_schedule.rb', line 58

def working_hours_in_timerange(date, from, to, avoid_lunch)
  allow_lunch_check = true
  day_duration = 0
  lunch = self.lunch_on_a_day(date.to_date.strftime("%a").downcase) rescue 0
  hash = hours
  hash[date.to_date.strftime("%a").downcase.to_sym].each_key do |slot|
    hash[date.to_date.strftime("%a").downcase.to_sym][slot].each do |range|
      array1 = (range[0].to_s.to_time.to_i..range[1].to_s.to_time.to_i).step(1.minute.to_i).to_a
      array2 = (from.to_time.to_i..to.to_time.to_i).step(1.minute.to_i).to_a
      array3 = (array1 & array2)
      array3.pop
      day_duration += (array3.size)*60 #if slot.to_s == "work"
      #lunch += (array3.size)*60 if slot.to_s == "lunch"
    end
  end
  if day_duration - lunch < 0
    avoid_lunch == "true" ? request_time = 0 : request_time = day_duration
  else
    avoid_lunch == "true" ? request_time = day_duration - lunch : request_time = day_duration
  end
  return request_time
end

#working_seconds_on_a_day(day, humanized = true) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/employee_work_schedule.rb', line 81

def working_seconds_on_a_day(day, humanized=true)
  working_hours = hours.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)
  end
  if humanized
    return EmployeeTimeOffsController.helpers.human_duration(accumulated)
  else
    return accumulated
  end
end

#working_status_on_a_day(date) ⇒ Object



135
136
137
138
139
# File 'app/models/employee_work_schedule.rb', line 135

def working_status_on_a_day(date)
  if hours[date.strftime("%a").downcase.to_sym][:work].keys.size == 0
    return :not_working_closed
  end
end