Skip to content

Instantly share code, notes, and snippets.

@leekiernan
Last active December 19, 2015 17:38
Show Gist options
  • Save leekiernan/5992533 to your computer and use it in GitHub Desktop.
Save leekiernan/5992533 to your computer and use it in GitHub Desktop.
less a few checks.
STUDENT_LOAN_RATE = 0.09 # 9%
STUDENT_LOAN_THRESHOLD = 15_000
BASIC_TAX_THRESHOLD = 9_440
BASIC_TAX_RATE = 0.20
HIGHER_TAX_RATE = 0.40
HIGHER_TAX_THRESHOLD = 32_010
ADDITIONAL_TAX_RATE = 0.50
ADDITIONA_TAX_THRESHOLD = 150_001
NATIONAL_INSURANCE_RATE = 0.12
NATIONAL_INSURANNCE_THRESHOLD = 7_748
class Income
@@student_loan_rate = STUDENT_LOAN_RATE
@@student_loan_threshold = STUDENT_LOAN_THRESHOLD
@@basic_tax_threshold = BASIC_TAX_THRESHOLD
@@basic_tax_rate = BASIC_TAX_RATE
@@higher_tax_rate = HIGHER_TAX_RATE
@@higher_tax_threshold = HIGHER_TAX_THRESHOLD
@@additional_tax_rate = ADDITIONAL_TAX_RATE
@@additiona_tax_threshold = ADDITIONA_TAX_THRESHOLD
@@national_insurance_rate = NATIONAL_INSURANCE_RATE
@@national_insurannce_threshold = NATIONAL_INSURANNCE_THRESHOLD
def self.student_loan_rate=(n)
@@student_loan_rate = n
end
def self.student_loan_threshold=(n)
@@student_loan_threshold = n
end
def self.basic_tax_threshold=(n)
@@basic_tax_threshold = n
end
def self.basic_tax_rate=(n)
@@basic_tax_rate = n
end
def self.higher_tax_rate=(n)
@@higher_tax_rate = n
end
def self.higher_tax_threshold=(n)
@@higher_tax_threshold = n
end
def self.additional_tax_rate=(n)
@@additional_tax_rate = n
end
def self.additiona_tax_threshold=(n)
@@additiona_tax_threshold = n
end
def self.national_insurance_rate=(n)
@@national_insurance_rate = n
end
def self.national_insurannce_threshold=(n)
@@national_insurannce_threshold = n
end
def self.student_loan_rate
@@student_loan_rate
end
def self.student_loan_threshold
@@student_loan_threshold
end
def self.basic_tax_threshold
@@basic_tax_threshold
end
def self.basic_tax_rate
@@basic_tax_rate
end
def self.higher_tax_rate
@@higher_tax_rate
end
def self.higher_tax_threshold
@@higher_tax_threshold
end
def self.additional_tax_rate
@@additional_tax_rate
end
def self.additiona_tax_threshold
@@additiona_tax_threshold
end
def self.national_insurance_rate
@@national_insurance_rate
end
def self.national_insurannce_threshold
@@national_insurannce_threshold
end
def initialize(opt = {})
@student = opt[:student] ? true : false
@income = opt[:income] || 0
end
def income=(income)
@income = income
end
def income
@income
end
def student=(s=false)
@student = s ? true : false
end
def student?
@student
end
def self.calculate_tax(amount)
tax = 0
if amount > @@basic_tax_threshold
tax += (amount - @@basic_tax_threshold) * @@basic_tax_rate
end
if amount > @@higher_tax_threshold
tax += (amount - @@higher_tax_threshold) * @@higher_tax_rate
end
if amount > @@additiona_tax_threshold
tax += (amount - @@additiona_tax_threshold) * @@additional_tax_rate
end
tax.round(2)
end
def calculate_tax(amount=false)
amount ||= @income
self.class.calculate_tax(amount)
end
def calculate_nat_ins(amount=false)
amount ||= @income
nat_ins = 0
if amount > @@national_insurannce_threshold
nat_ins += (amount - @@national_insurannce_threshold) * @@national_insurance_rate
end
nat_ins.round(2)
end
def calculate_student_deduction(amount=false)
return 0 if !student?
amount ||= @income
payment = 0
if amount > @@student_loan_threshold
payment += (amount - @@student_loan_threshold) * @@student_loan_rate
end
# student? ? payment.round(2) : 0
payment.round(2)
end
def total_deductions(amount=false)
amount ||= @income
if student?
calculate_tax(amount) + calculate_nat_ins(amount) + calculate_student_deduction(amount)
else
calculate_tax(amount) + calculate_nat_ins(amount)
end
end
def net_salary(amount=false)
amount ||= @income
amount - total_deductions(amount).round(2)
end
def show_earnings
format="%20s\t%12s\t%12s\t%12s\n"
printf(format, "", "Yearly", "Monthly", "Weekly")
printf(format, "", "------", "-------", "------")
format="%20s\t%12.2f\t%12.2f\t%12.2f\n"
salary = net_salary
taxable = @income - BASIC_TAX_THRESHOLD
printf(format, "=Taxable", taxable, (taxable)/12, (taxable)/52 )
printf(format, "-Tax due", calculate_tax, (calculate_tax)/12, (calculate_tax)/52 )
printf(format, "-Nat ins due", calculate_nat_ins, (calculate_nat_ins)/12, (calculate_nat_ins)/52 )
printf(format, "-Student repayments", calculate_student_deduction, (calculate_student_deduction)/12, (calculate_student_deduction)/52 ) if student?
printf(format, "=Total deductions", total_deductions, (total_deductions/12), (total_deductions/52) )
printf(format, "=Net", salary, (salary/12), (salary/52) )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment