Class: WorkSchedule

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/work_schedule.rb

Overview

== Schema Information

Table name: work_schedules
Database name: primary

id :bigint not null, primary key
amount :decimal(, )
approved_by :integer
approved_on :date
created_by :integer
effective_date :date
expiry_date :date
notes :text
state :string default("pending"), not null
created_at :datetime not null
updated_at :datetime not null
employee_id :integer

Indexes

index_work_schedules_on_approved_by (approved_by)
index_work_schedules_on_approved_on (approved_on)
index_work_schedules_on_created_by (created_by)
index_work_schedules_on_effective_date (effective_date)
index_work_schedules_on_employee_id (employee_id)
index_work_schedules_on_expiry_date (expiry_date)
index_work_schedules_on_state (state)

Foreign Keys

fk_rails_... (approved_by => parties.id)
fk_rails_... (created_by => parties.id)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#updater

Has many collapse

Has one collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#amountObject (readonly)



44
# File 'app/models/work_schedule.rb', line 44

validates :amount, numericality: { greater_than: 0 }, presence: true

#effective_dateObject (readonly)



43
# File 'app/models/work_schedule.rb', line 43

validates :effective_date, presence: true

#skip_approvalObject

validate :validate_approved_schedules_limit, on: :create



52
53
54
# File 'app/models/work_schedule.rb', line 52

def skip_approval
  @skip_approval
end

Class Method Details

.effective_nowActiveRecord::Relation<WorkSchedule>

A relation of WorkSchedules that are effective now. Active Record Scope

Returns:

See Also:



58
59
60
61
62
63
64
65
66
# File 'app/models/work_schedule.rb', line 58

scope :effective_now, ->(exclude_schedule_id = nil) {
  where(
    state: 'approved',
    effective_date: ..Date.current,
    expiry_date: [nil, Date.current..]
  )
    .where.not(id: exclude_schedule_id)
    .order(effective_date: :desc)
}

.effective_onActiveRecord::Relation<WorkSchedule>

A relation of WorkSchedules that are effective on. Active Record Scope

Returns:

See Also:



67
68
69
# File 'app/models/work_schedule.rb', line 67

scope :effective_on, ->(date) {
  where('effective_date <= ? AND (expiry_date IS NULL OR expiry_date >= ?)', date, date).where(state: 'approved')
}

Instance Method Details

#after_approved_schedule_tasksObject



138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/work_schedule.rb', line 138

def after_approved_schedule_tasks
  # Expire the current effective schedule if applicable
  if effective_date >= Time.zone.today
    current_schedule = employee.work_schedules.effective_now(id).first
    if current_schedule.present?
      current_schedule.update(expiry_date: effective_date)
      current_schedule.expire! if current_schedule.expiry_date == Time.zone.today
    end
  end
  # Send notification email
  send_approved_email
end

#auto_assign_time_off_policyObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/models/work_schedule.rb', line 159

def auto_assign_time_off_policy
  # Calculate total hours for the schedule
  total_hours = amount

  # Find policies matching the total hours and the employee's hire date
  applicable_policies = TimeOffPolicy.where(hours_per_week: total_hours)

  # Check if any policy matches the criteria
  if applicable_policies.empty?
    errors.add(:base, "No time-off policy created for #{total_hours} hours per week. Please contact HW Team or HR to create a new Policy for this work schedule.")
    raise ActiveRecord::RecordInvalid, self
  end

  # Filter by hire date and tenure criteria
  tenure = ((effective_date - employee.employee_record.hire_date) / 365.25).floor
  selected_policy = applicable_policies.find do |policy|
    tenure >= policy.min_tenure && (policy.max_tenure.nil? || tenure < policy.max_tenure)
  end

  # If no matching policy is found
  unless selected_policy
    errors.add(:base, "No time-off policy matches the tenure criteria for #{total_hours} hours per week.")
    raise ActiveRecord::RecordInvalid, self
  end

  # Check if the same policy is already assigned
  existing_assignment = employee.time_off_policy_assignments.find_by(
    time_off_policy_id: selected_policy.id
  )

  # If already assigned, return early
  return if existing_assignment

  # Assign the policy
  employee.time_off_policy_assignments.create!(
    time_off_policy: selected_policy,
    work_schedule_id: id,
    accrual_start_date: effective_date
  )
end

#before_approved_schedule_tasksObject

def send_udpate_email
InternalMailer.update_work_schedule(self).deliver_now
end



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/work_schedule.rb', line 122

def before_approved_schedule_tasks
  ActiveRecord::Base.transaction do
    self.approved_on = Time.zone.today

    # Auto-assign a time-off policy
    auto_assign_time_off_policy
    return true
  rescue ActiveRecord::RecordInvalid => e
    errors.add(:base, "Time-off policy assignment failed: #{e.message}")
    false # Halt the state transition
  rescue StandardError => e
    errors.add(:base, "Unexpected error during approval: #{e.message}")
    false # Halt the state transition
  end
end

#changes_summaryObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'app/models/work_schedule.rb', line 216

def changes_summary
  summary = {}
  # Track changes on the main work schedule attributes
  saved_changes.each do |attribute, values|
    old_value, new_value = values
    summary[attribute] = { old: old_value, new: new_value }
  end

  # Track changes on each work schedule day
  work_schedule_days.each do |day|
    day_changes = {}
    day.saved_changes.each do |attribute, values|
      old_value, new_value = values
      day_changes[attribute] = { old: old_value, new: new_value }
    end
    summary[day] = day_changes unless day_changes.empty?
  end

  summary
end

#check_approval_requirementObject



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

def check_approval_requirement
  if skip_approval.to_b
    approve
  else
    send_create_email
  end
end

#creatorEmployee

Returns:

See Also:



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

belongs_to  :creator, class_name: 'Employee', foreign_key: 'created_by'

#employeeEmployee

Returns:

See Also:



37
# File 'app/models/work_schedule.rb', line 37

belongs_to  :employee

#is_current?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'app/models/work_schedule.rb', line 155

def is_current?
  approved? && effective_date <= Time.zone.today && (expiry_date.nil? || expiry_date > Time.zone.today)
end

#parentObject



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

def parent
  employee
end

#send_approved_emailObject



151
152
153
# File 'app/models/work_schedule.rb', line 151

def send_approved_email
  WorkforceMailer.new_work_schedule_state(self).deliver_now
end

#send_create_emailObject



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

def send_create_email
  WorkforceMailer.new_work_schedule_request(self).deliver_now
end

#time_off_policyTimeOffPolicy



41
# File 'app/models/work_schedule.rb', line 41

has_one     :time_off_policy, through: :time_off_policy_assignment

#time_off_policy_assignmentTimeOffPolicyAssignment



40
# File 'app/models/work_schedule.rb', line 40

has_one     :time_off_policy_assignment, dependent: :destroy

#to_detailed_jsonObject



237
238
239
240
241
242
243
244
245
246
247
# File 'app/models/work_schedule.rb', line 237

def to_detailed_json
  as_json(
    only: %i[id employee_id amount effective_date expiry_date state notes],
    include: {
      work_schedule_days: {
        only: %i[day_of_week hours],
        methods: %i[day_name formatted_start_time formatted_end_time formatted_lunch_times]
      }
    }
  )
end

#update_policy_assignmentObject



200
201
202
203
204
205
206
207
208
209
210
# File 'app/models/work_schedule.rb', line 200

def update_policy_assignment
  # if work_schedule_days_changed?
  #   employee.time_off_policy_assignments.find_by(work_schedule_id: id)&.destroy
  # end

  return unless saved_change_to_effective_date? || work_schedule_days_changed?

  # Recalculate and reassign the policy
  employee.time_off_policy_assignments.find_by(work_schedule_id: id)&.destroy
  auto_assign_time_off_policy
end

#work_schedule_daysActiveRecord::Relation<WorkScheduleDay>

Returns:

See Also:



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

has_many    :work_schedule_days, dependent: :destroy

#work_schedule_days_changed?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'app/models/work_schedule.rb', line 212

def work_schedule_days_changed?
  work_schedule_days.any? { |day| day.saved_changes.present? }
end