# coding: utf-8 class TimeoffPolicy < ActiveRecord::Base # @!attribute name # @return [String] the name of the policy # @!attribute base_vacation_days # @return [Integer] total vacation days per year # @!attribute accrue_rate # @return [Enumerable] indicate how often this policy should be ran to calculate employees vacation days balance. enum accrue_rate:[:weekly, :bi_weekly, :monthly, :yearly] # @!attribute accrue_type # @return [Enumerable] Vacation Data Accural Type # @example Assuming you have 20 vacation days per year (假设你每年有20天休假) # :accrue_by_time => if it is accrue per month, u will have 20/12 = 1.66 days on Feb 1st # :accrue_by_time_adv => if it is accrue per month, u will have 20/12 = 1.66 days on January 1st # @note if accrue_by_time_adv and :yearly, means all days will be given to user at once enum accrue_type: [:accrue_by_time, :accrue_by_time_adv] # @!attribute tentures # We’ll automatically increase your employees’ vacation accrual rate based on the milestones you set below. (These bonuses will be your default policy, but you can turn this feature off for individual employees.) # 根据您设置下面的里程碑,我们将自动增加员工的假期累积率。 (这些奖金将是你的默认策略,但您可以关闭此功能对于员工个人。) # @return [Array] List of tenture. 年假逐年递增 年假递增率 # @example # self.tentures = [{anniversary:2, extra_days: 2}, {anniversary:4, extra_days: 2}] # means if you worked for 2 years, then you have 22 days vacation. If 4 years then u have 24 days. # 如果你工作了2年指,那么你有22天假期。如果4年那么你有24天假期。 serialize :tentures # @!attribute carry_over # @return [Boolean] Carry Over ? (跨年累积) # @!attribute carry_over_cap # @return [Integer] total carry over hours # @note if carry_over_cap is ZERO, it means cannot carry over any hours. # @!attribute carry_over_expire_on # @return [Date] total carry over hours expiration date # @example # carry_over:true,carry_over_cap:16, carry_over_expire_on:nil

 # with this setting, every year carry_over 16 hours and it never expires。
 # 如果 这样就是 每年carry_over 16小时,永不过期。 # carry_over:true,carry_over_cap:16, carry_over_expire_on:"12/31/1999" # with this setting, every year carry_over 16 hours,it means all the accumulated vacations days will be expired at the end of the year. 
 # 如果 这样就是 每年carry_over 16小时,前年都不算,相当于yearly。 # 
carry_over:true,carry_over_cap:16, carry_over_expire_on:“03/01/2000” # with this setting, every year carry_over 16 hours,it means all the accumulated vacations days will be expired at the end of Feburary. # 如果 这样就是 每年carry_over 16小时,以前累积的每年3月过期。 # carry_over:true,carry_over_cap:16, carry_over_expire_on:“06/01/2000” # with this setting, every year carry_over 16 hours,it means all the accumulated vacations days will be expired at the end of May. # 如果 这样就是 每年carry_over 16小时,以前累积的每年6月过期。 # @!attribute allow_negative # @return [Boolean] Do you allow user to borrow vacation days in advance? # @!attribute negative_cap # @return [Integer] how many vacation hours that user can borrow in advance # @!attribute total_sick_days # @return [Integer] total allowed sick days (病假) # @!attribute total_personal_days # @return [Integer] total allowed personal days (个人年假) # @!attribute work_hours_per_day # @return [Integer] total work hours per day # @!attribute beginning_of_workday # @return [String] start time for every working day. '9:00 am' # @!attribute end_of_workday # @return [Integer] end time for every working day. '5:00 pm' # @example We can adjust the start and end time of our business hours # self.beginning_of_workday = "8:30 am" # self.end_of_workday = "5:30 pm" # @!attribute work_week # @return [Array] list of working week days. '%w(mon tue wed thu fri)' # @example # self.work_week = '%w(mon tue wed thu fri)' serialize :work_week # @!attribute work_hours # @return [Hash] list different working hours per day. Not sure if it is necessary. # @example as alternative we also can change the business hours for each work day: # self.work_hours = { # :mon=>["9:00","17:00"], # :fri=>["9:00","17:00"], # :sat=>["10:00","15:00"] # } serialize :work_hours # @!attribute work_dates # @return [Array] a list of working dates, which addition to the work_week setting. # @example # work_dates = ["02/01/2015","02/14/2015"] serialize :work_dates # @!attribute holidays # @return [Array] a list of holidays # @example # holidays = [{:name=>"Indepedence Day", :date=>"10/01/2014", :repeat => true}, {:name=>"New Year", :date=>"1/01/2015", :repeat => true}] serialize :holidays # @!attribute types # @return [Array] a list of types # @example # self.timeoff_types = [{:name=>"Sick Day", :type=>"sick_leave"}, {:name=>"Personal Day", :type=>"personal_leave"}] serialize :timeoff_types belongs_to :company, class_name:"Company" has_many :users, class_name:"User" # calculate how many hours has user accrued in this policies period. # @todo calculate how many hours has user accrued in this policies period. # @example Every first day of the month, this accrue method will run and it will add the accrued amount to each user. On Nov 1st, 2014 # policy = TimeoffPolicy.create!( # :hours_per_year => 160, # :accrue_rate => :accrue_by_time, # :accrue_rate => :monthly, # :allow_negative => false, # :carry_over => :yearly_cap, # :carry_over_cap => 10, # :total_personal_days => 10, # :total_sick_days => 5, # :work_hours_per_day => 8, # :created_at => '10/1/2014' # ) # user1 = User.find(1) # user1.start_date = '10/15/2014' # hours_per_month = 160 / 12 # if user1.start_date - Date.today < 1_month # hours_per_month = hours_per_month / weeks_of_worked # end # TimeoffTask.create!(:sub_type => :accrue, :user => user, :amount=> hours_per_month) # *********If it runs on Jan 1st, 2105************ # if user.total_accrue_hours > policy.carry_over_cap # TimeoffTask.create!(:sub_type => :adjustment, :user => user, :amount=> policy.carry_over_cap - user.total_accrue_hours) # end def accrue users.each do |user| TimeoffTask.create(:sub_type => :accrue, :user => user, :amount=> amount) end end def work_hours_hash hash = {} self.work_hours.each do |item| hash[item[:day]] = item end hash end # compatible with business_time def each (&block) # XXX: _weekdays keys = ['beginning_of_workday', 'end_of_workday', 'work_week', 'work_hours'] keys.each do |k| yield k, self.attributes[k] end # reformat work_dates yield 'work_dates', self.work_dates.map{|i| Date.parse(i)} # reformat holidays yield 'holidays', self.holidays.inject([]){|res, i| d = Date.parse(i[:date]) if i[:repeat] res << d.change(year: Date.today.year) res << d.change(year: Date.today.year + 1) else res << d end } end end