Class: EmployeeWorkSchedule
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- EmployeeWorkSchedule
- 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
Constants included from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Belongs to collapse
Class Method Summary collapse
Instance Method Summary collapse
- #lunch_hours_a_week ⇒ Object
- #lunch_on_a_day(day) ⇒ Object
- #total_hours_on_a_day(day) ⇒ Object
- #working_hours_a_week ⇒ Object
- #working_hours_in_timerange(date, from, to, avoid_lunch) ⇒ Object
- #working_seconds_on_a_day(day, humanized = true) ⇒ Object
- #working_status_on_a_day(date) ⇒ Object
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Class Method Details
.work_schedule_time_select ⇒ Object
51 52 53 54 55 56 57 |
# File 'app/models/employee_work_schedule.rb', line 51 def self.work_schedule_time_select result = [] (Time.zone.parse('00:00').to_i..Time.zone.parse('23:45').to_i).step(15.minutes.to_i) do |time| result << [Time.zone.at(time).strftime('%I:%M %P'), Time.zone.at(time).strftime('%H:%M')] end result end |
Instance Method Details
#employee ⇒ Employee
47 |
# File 'app/models/employee_work_schedule.rb', line 47 belongs_to :employee, inverse_of: :employee_work_schedules, optional: true |
#lunch_hours_a_week ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/models/employee_work_schedule.rb', line 125 def lunch_hours_a_week accumulated = 0 hours.each_key do |weekday| hours[weekday][:lunch].each do |range| accumulated += begin range[1].to_s.to_time rescue StandardError 0 end - begin range[0].to_s.to_time rescue StandardError 0 end end end accumulated end |
#lunch_on_a_day(day) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'app/models/employee_work_schedule.rb', line 162 def lunch_on_a_day(day) hash = hours[day.to_sym][:lunch] accumulated = 0 hash.each do |range| accumulated += begin range[1].to_s.to_time rescue StandardError 0 end - begin range[0].to_s.to_time rescue StandardError 0 end end accumulated end |
#total_hours_on_a_day(day) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'app/models/employee_work_schedule.rb', line 143 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 += begin range[1].to_s.to_time rescue StandardError 0 end - begin range[0].to_s.to_time rescue StandardError 0 end end end accumulated end |
#working_hours_a_week ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/employee_work_schedule.rb', line 107 def working_hours_a_week accumulated = 0 hours.each_key do |weekday| hours[weekday][:work].each do |range| accumulated += begin range[1].to_s.to_time rescue StandardError 0 end - begin range[0].to_s.to_time rescue StandardError 0 end end end accumulated end |
#working_hours_in_timerange(date, from, to, avoid_lunch) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/models/employee_work_schedule.rb', line 59 def working_hours_in_timerange(date, from, to, avoid_lunch) day_duration = 0 lunch = begin lunch_on_a_day(date.to_date.strftime("%a").downcase) rescue StandardError 0 end 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" ? 0 : day_duration else avoid_lunch == "true" ? day_duration - lunch : day_duration end end |
#working_seconds_on_a_day(day, humanized = true) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/employee_work_schedule.rb', line 84 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 += begin range[1].to_s.to_time rescue StandardError 0 end - begin range[0].to_s.to_time rescue StandardError 0 end end if humanized EmployeeTimeOffsController.helpers.human_duration(accumulated) else accumulated end end |
#working_status_on_a_day(date) ⇒ Object
179 180 181 |
# File 'app/models/employee_work_schedule.rb', line 179 def working_status_on_a_day(date) :not_working_closed if hours[date.strftime("%a").downcase.to_sym][:work].keys.empty? end |