Class: EmployeeTimeOff

Inherits:
ApplicationRecord show all
Includes:
ActionView::Helpers
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

Belongs to 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

Instance Method Details

#employeeEmployee

Returns:

See Also:



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

belongs_to :employee, optional: true

#events_from_today_until_end_yearObject



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

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

#events_until_todayObject



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

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

#integer_hours(seconds) ⇒ Object



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

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

#pto_planned_from_todayObject



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

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

#pto_unplanned_from_todayObject



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

def pto_unplanned_from_today
  time_off_total - (pto_used_until_today + pto_planned_from_today)
end

#pto_used_until_todayObject



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

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)
  res = (event_time_used_until_today - banked_time_until_today) / 3600
  res
end

#time_off_accruedObject



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

def time_off_accrued
  rol = rollover || 0
  nwto = not_working_timeoff || 0
  r = rol / 3600 rescue 0
  #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 
  real_accrued_today = theoric_accrued_until_today_with_rollover - pto_used_until_today
  return real_accrued_today
end

#time_off_banked_timeObject



103
104
105
# File 'app/models/employee_time_off.rb', line 103

def time_off_banked_time
  integer_hours(banked_time)
end

#time_off_banked_time_percentage_usedObject



111
112
113
# File 'app/models/employee_time_off.rb', line 111

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 0
end

#time_off_banked_time_usedObject



107
108
109
# File 'app/models/employee_time_off.rb', line 107

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

#time_off_bereavement_usedObject



115
116
117
# File 'app/models/employee_time_off.rb', line 115

def time_off_bereavement_used
  integer_hours(bereavement_used).round
end

#time_off_birthday_progressObject



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

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

#time_off_birthday_remainingObject



143
144
145
146
147
# File 'app/models/employee_time_off.rb', line 143

def time_off_birthday_remaining
  remaining = time_off_birthday_total - time_off_birthday_used
  remaining < 0 ? result = 0 : result = remaining
  return result
end

#time_off_birthday_totalObject



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

def time_off_birthday_total
  birthday.round rescue 0
end

#time_off_birthday_usedObject



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

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

#time_off_community_service_usedObject



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

def time_off_community_service_used
  integer_hours(community_service_used).round
end

#time_off_jury_duty_usedObject



131
132
133
# File 'app/models/employee_time_off.rb', line 131

def time_off_jury_duty_used
  integer_hours(jury_duty_used).round
end

#time_off_parental_leave_usedObject



127
128
129
# File 'app/models/employee_time_off.rb', line 127

def time_off_parental_leave_used
  integer_hours(parental_leave_used).round
end

#time_off_progressObject



97
98
99
100
101
# File 'app/models/employee_time_off.rb', line 97

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

#time_off_remainingObject



93
94
95
# File 'app/models/employee_time_off.rb', line 93

def time_off_remaining
  time_off_total - time_off_used
end

#time_off_short_term_disability_usedObject



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

def time_off_short_term_disability_used
  integer_hours(short_term_disability_used).round
end

#time_off_totalObject



45
46
47
# File 'app/models/employee_time_off.rb', line 45

def time_off_total
  not_working_timeoff.round rescue 0
end

#time_off_total_in_secondsObject



49
50
51
# File 'app/models/employee_time_off.rb', line 49

def time_off_total_in_seconds
  (not_working_timeoff * 3600) rescue 0
end

#time_off_unpaid_usedObject



153
154
155
# File 'app/models/employee_time_off.rb', line 153

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

#time_off_usedObject



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

def time_off_used
  integer_hours(not_working_timeoff_used)
end