lib/train/tax/calculator/withholding_tax.rb in train-tax-calculator-2.2.5 vs lib/train/tax/calculator/withholding_tax.rb in train-tax-calculator-2.2.6

- old
+ new

@@ -11,24 +11,35 @@ { lowest: 166_667.00, highest: 666_667.00, base: 40_833.33, excess: 0.32 }, { lowest: 666_667.00, highest: Float::INFINITY, base: 200_833.33, excess: 0.35 }, ] def self.compute(basic_salary) - taxable_income = basic_salary - Deductions.get(basic_salary) - - if basic_salary > 90_000 - monthly_taxable = ((basic_salary - 90_000) / 12.00) - taxable_income = taxable_income + monthly_taxable - end - - tax_bracket = LOOKUP_TABLE.select do |bracket| - taxable_income >= bracket[:lowest] && - taxable_income < bracket[:highest] - end.first - + taxable_income = compute_taxable_income_for(basic_salary) + tax_bracket = get_tax_bracket_for(taxable_income) withholding_tax = ((taxable_income - tax_bracket[:lowest]) * tax_bracket[:excess]) + tax_bracket[:base] + withholding_tax.round(2) end + + private + + def self.compute_taxable_income_for(basic_salary) + initial_net = basic_salary - Deductions.get(basic_salary) + + if basic_salary > 90_000 + additional_tax = ((basic_salary - 90_000) / 12.00) + return initial_net + additional_tax + end + + initial_net + end + + def self.get_tax_bracket_for(taxable_income) + LOOKUP_TABLE.select do |bracket| + taxable_income >= bracket[:lowest] && + taxable_income < bracket[:highest] + end.first + end end end