Class: EmployeeTimeOff

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

Overview

== Schema Information

Table name: employee_time_offs
Database name: primary

id :integer not null, primary key
accumulated :integer default(0)
banked_time :integer default(0)
banked_time_type :string
bereavement :integer default(0)
bereavement_used :integer default(0)
birthday :integer default(1)
birthday_used :integer default(0)
community_service_used :integer default(0)
control_value :decimal(, ) default(1.0)
jury_duty :integer default(0)
jury_duty_used :integer default(0)
not_working_timeoff :decimal(, )
not_working_timeoff_used :integer default(0)
parental_leave :integer default(0)
parental_leave_used :integer default(0)
real_rollover_more_than_15_days :integer
rollover :integer
short_term_disability :integer default(0)
short_term_disability_used :integer default(0)
unpaid_timeoff_used :integer default(0)
year :integer
created_at :datetime not null
updated_at :datetime not null
employee_id :integer

Indexes

index_employee_time_offs_on_employee_id_and_year (employee_id,year) UNIQUE

Constant Summary

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Belongs to collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Method Details

#employeeEmployee

Returns:

See Also:



38
# File 'app/models/employee_time_off.rb', line 38

belongs_to :employee, optional: true

#events_from_today_until_end_yearObject



66
67
68
# File 'app/models/employee_time_off.rb', line 66

def events_from_today_until_end_year
  employee.employee_events.where('date > ? AND date < ?', Time.zone.now, Time.zone.now.end_of_year).where(status: 'not_working_timeoff')
end

#events_until_todayObject



62
63
64
# File 'app/models/employee_time_off.rb', line 62

def events_until_today
  employee.employee_events.where('date > ? AND date < ?', Time.zone.now.beginning_of_year, Time.zone.now).where(status: 'not_working_timeoff')
end

#integer_hours(seconds) ⇒ Object



40
41
42
43
44
# File 'app/models/employee_time_off.rb', line 40

def integer_hours(seconds)
  (seconds.to_f / 3600).to_f.round(2)
rescue StandardError
  0
end

#pto_planned_from_todayObject



76
77
78
# File 'app/models/employee_time_off.rb', line 76

def pto_planned_from_today
  events_from_today_until_end_year.sum(:event_time) / 3600
end

#pto_unplanned_from_todayObject



80
81
82
# File 'app/models/employee_time_off.rb', line 80

def pto_unplanned_from_today
  time_off_total - (pto_used_until_today + pto_planned_from_today)
end

#pto_used_until_todayObject



70
71
72
73
74
# File 'app/models/employee_time_off.rb', line 70

def pto_used_until_today
  event_time_used_until_today = events_until_today.sum(:event_time)
  banked_time_until_today = events_until_today.sum(:broken_down_time)
  (event_time_used_until_today - banked_time_until_today) / 3600
end

#time_off_accruedObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/employee_time_off.rb', line 84

def time_off_accrued
  rol = rollover || 0
  nwto = not_working_timeoff || 0
  r = begin
    rol / 3600
  rescue StandardError
    0
  end
  # nwtou = (not_working_timeoff_used || 0) / 3600

  biweekly_accrual_rate = ((nwto - r) / 26).to_f # How many hours the person accrues every 2 weeks.
  theoric_accrued_until_today = biweekly_accrual_rate * (Date.current.cweek / 2) # How mmany hours PTO this person accrued until today without looking at pto used already
  theoric_accrued_until_today_with_rollover = theoric_accrued_until_today + r
  theoric_accrued_until_today_with_rollover - pto_used_until_today
end

#time_off_banked_timeObject



118
119
120
# File 'app/models/employee_time_off.rb', line 118

def time_off_banked_time
  integer_hours(banked_time)
end

#time_off_banked_time_percentage_usedObject



128
129
130
131
132
# File 'app/models/employee_time_off.rb', line 128

def time_off_banked_time_percentage_used
  (time_off_banked_time_used * 100) / (time_off_banked_time + time_off_banked_time_used).to_f
rescue StandardError
  0
end

#time_off_banked_time_usedObject



122
123
124
125
126
# File 'app/models/employee_time_off.rb', line 122

def time_off_banked_time_used
  integer_hours(employee.employee_events.where('extract(year from date) = ?', Time.current.year).where(broken_down: true).sum('broken_down_time'))
rescue StandardError
  0
end

#time_off_bereavement_usedObject



134
135
136
# File 'app/models/employee_time_off.rb', line 134

def time_off_bereavement_used
  integer_hours(bereavement_used).round
end

#time_off_birthday_progressObject



169
170
171
172
173
# File 'app/models/employee_time_off.rb', line 169

def time_off_birthday_progress
  (time_off_birthday_used * 100) / time_off_birthday_total
rescue StandardError
  0
end

#time_off_birthday_remainingObject



164
165
166
167
# File 'app/models/employee_time_off.rb', line 164

def time_off_birthday_remaining
  remaining = time_off_birthday_total - time_off_birthday_used
  [remaining, 0].max
end

#time_off_birthday_totalObject



154
155
156
157
158
# File 'app/models/employee_time_off.rb', line 154

def time_off_birthday_total
  birthday.round
rescue StandardError
  0
end

#time_off_birthday_usedObject



160
161
162
# File 'app/models/employee_time_off.rb', line 160

def time_off_birthday_used
  (integer_hours(birthday_used) / 8).round
end

#time_off_community_service_usedObject



142
143
144
# File 'app/models/employee_time_off.rb', line 142

def time_off_community_service_used
  integer_hours(community_service_used).round
end

#time_off_jury_duty_usedObject



150
151
152
# File 'app/models/employee_time_off.rb', line 150

def time_off_jury_duty_used
  integer_hours(jury_duty_used).round
end

#time_off_parental_leave_usedObject



146
147
148
# File 'app/models/employee_time_off.rb', line 146

def time_off_parental_leave_used
  integer_hours(parental_leave_used).round
end

#time_off_progressObject



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

def time_off_progress
  used = begin
    ((not_working_timeoff_used || 0) * 100)
  rescue StandardError
    0
  end
  current = begin
    ((not_working_timeoff || 0) * 3600)
  rescue StandardError
    0
  end
  current.zero? ? 0 : (used / current)
end

#time_off_remainingObject



100
101
102
# File 'app/models/employee_time_off.rb', line 100

def time_off_remaining
  time_off_total - time_off_used
end

#time_off_short_term_disability_usedObject



138
139
140
# File 'app/models/employee_time_off.rb', line 138

def time_off_short_term_disability_used
  integer_hours(short_term_disability_used).round
end

#time_off_totalObject



46
47
48
49
50
# File 'app/models/employee_time_off.rb', line 46

def time_off_total
  not_working_timeoff.round
rescue StandardError
  0
end

#time_off_total_in_secondsObject



52
53
54
55
56
# File 'app/models/employee_time_off.rb', line 52

def time_off_total_in_seconds
  (not_working_timeoff * 3600)
rescue StandardError
  0
end

#time_off_unpaid_usedObject



175
176
177
# File 'app/models/employee_time_off.rb', line 175

def time_off_unpaid_used
  integer_hours(unpaid_timeoff_used || 0).round
end

#time_off_usedObject



58
59
60
# File 'app/models/employee_time_off.rb', line 58

def time_off_used
  integer_hours(not_working_timeoff_used)
end