require File.expand_path('../../config/boot', __FILE__) require File.expand_path('../../config/environment', __FILE__) require 'clockwork' include Clockwork every(1.day, 'Yearly Carry Job', :if => lambda { |t| t.day == 1 and t.month == 1}, :at => '0:00'){ now = Time.zone.now if now.month == 1 and now.day == 1 # clean sick days and personal days User.all.each do |user| next if not user.policy.present? Task.create(sub_type: :personal, user: user, total_hours: user.policy.total_personal_days * user.policy.work_hours_per_day - user.total_remain_personal_hours) Task.create(sub_type: :sick, user: user, total_hours: user.policy.total_sick_days * user.policy.work_hours_per_day - user.total_remain_sick_hours) end TimeoffPolicy.where(carry_over: :co_no_cap).each do |policy| policy.users.each do |user| Task.create(sub_type: :accrue, user: user, total_hours: -user.total_remain_vacation_hours) end end TimeoffPolicy.where(carry_over: :co_total_cap).each do |policy| policy.users.each do |user| remain = user.total_remain_vacation_hours if remain > user.policy.carry_over_cap Task.create(sub_type: :accrue, user: user, total_hours: -(remain - user.policy.carry_over_cap)) end end end # @todo calculate yearly_cap, take vacation days of the year before last year into account # carry_over = nolimit, sick days and personal days have been cleaned, do nothing to vacation days end } # use multi job to make job info log more clear every(1.day, 'Yearly Accrue Job', :if => lambda { |t| t.day == 1 and t.month == 1}, :at => '0:00'){ TimeoffPolicy.where(accrue_rate: :yearly).each do |policy| policy.users.each do |user| Task.create(sub_type: :accrue, user: user, total_hours: user.total_vacation_hours) end end } every(1.day, 'Monthly Accrue Job', :if => lambda { |t| t.day == 1 }, :at => '0:00'){ # @todo differentiate :accrue_by_time and :accrue_by_time_adv TimeoffPolicy.where(accrue_rate: :monthly).each do |policy| policy.users.each do |user| Task.create(sub_type: :accrue, user: user, total_hours: (user.total_vacation_hours / 12.0 - 0.005).round(2)) end end }