Skip to content

Instantly share code, notes, and snippets.

@rylanb
Created April 25, 2012 20:39
Show Gist options
  • Save rylanb/2493165 to your computer and use it in GitHub Desktop.
Save rylanb/2493165 to your computer and use it in GitHub Desktop.
Calculating PTO with PTO Class
class Pto
attr_accessor :hire_date, :hire_date_vacation_adjustment, :vacation_this_year, :ytd, :eoy_accrual
def initialize(hire_date, hire_date_vacation_adjustment, vacation_this_year)
self.hire_date = hire_date
self.hire_date_vacation_adjustment = hire_date_vacation_adjustment
self.vacation_this_year = vacation_this_year
calculate_pto
end
#first_of_year = Time.local(DateTime.now.year, 1)
def calculate_pto
#Subtract one for January and add a pay period if the middle of the month has gone by
ytd_pay_periods = ((DateTime.now.month - 1) * 2) + (DateTime.now.day > 16 ? 1 : 0)
start_date = self.hire_date_vacation_adjustment.nil? ? self.hire_date.to_datetime : self.hire_date_vacation_adjustment.to_datetime
years_worked = actual_years_worked(start_date)
rate = get_rate(years_worked)
self.ytd = (ytd_pay_periods * get_rate(years_worked) ) - self.vacation_this_year.to_f
#puts "Rate: " + rate.inspect
self.eoy_accrual = calculate_year_accrual(years_worked, start_date, rate)
end
#Calculate Full Year Accrual
def calculate_year_accrual(years_worked, start_date, rate)
pay_periods = 24 #standard pay periods
first_of_year = DateTime.new(DateTime.now.year, 1, 1)
end_of_year = DateTime.new(DateTime.now.year, 12, 31)
how_many_years = ((end_of_year - start_date)/365.25).to_i
#puts "Rate: " + rate.inspect
eoy_rate = get_rate(how_many_years)
#puts "EOY Rate: " + eoy_rate.inspect
return ( (pay_periods * rate) - self.vacation_this_year.to_f) if eoy_rate == rate
#Split Year, calculate up til anniversary accrual and after anniversary accrual
anniversary = DateTime.new(DateTime.now.year, start_date.month, start_date.day)
#pay periods until anniversary * rate + after anniversary * eoy_rate
up_to_anni_pp = ( ( (anniversary.month - 1 ) * 2 ).to_i) + (anniversary.day > 16 ? 1 : 0)
after_anni_pp = ( ( (end_of_year.month - anniversary.month) ).to_i + 1 ) * 2 - (anniversary.day > 16 ? 1 : 0)
return (up_to_anni_pp * rate) + (after_anni_pp * eoy_rate) - self.vacation_this_year.to_f
end
def calculate_sick_time
#first_of_year = Time.local(DateTime.now.year, 1)
#adj_pay_periods = DateTime.now.day > 16 ? 1 : 0
return "coming soon"
end
private
def actual_years_worked(start_date)
now = Time.now.utc.to_date
now.year - start_date.year - ((now.month > start_date.month || (now.month == start_date.month && now.day >= start_date.day)) ? 0 : 1)
end
def get_rate(years_worked)
rates = [Settings.pto_accum_rate, Settings.pto_accum_rate_2_yr, Settings.pto_accum_rate_4_yr, Settings.pto_accum_rate_6_yr, Settings.pto_accum_rate_8_yr]
case
when years_worked >= 8
return rates[4]
when years_worked >= 6
return rates[3]
when years_worked >= 4
return rates[2]
when years_worked >= 2
return rates[1]
else
return rates[0]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment