diff --git a/app/controllers/incomes_controller.rb b/app/controllers/incomes_controller.rb index 28c4486..87d3890 100644 --- a/app/controllers/incomes_controller.rb +++ b/app/controllers/incomes_controller.rb @@ -61,31 +61,20 @@ def destroy end def income_switch - totals = FixedExpense.total_costs if params[:enabled] == "0" - salary = Income.tax_on_income(income_type: "Salary") - respond_to do |format| format.turbo_stream { render turbo_stream: turbo_stream.replace("hourly_budget", partial: "budget/salary_budget", - locals: { - totals: totals, - income: salary - }) + locals: build_locals(salary)) } end else - hourly = Income.tax_on_income(income_type: "Hourly") - respond_to do |format| format.turbo_stream { render turbo_stream: turbo_stream.replace("salary_budget", partial: "budget/hourly_budget", - locals: { - totals: totals, - income: hourly - }) + locals: build_locals(hourly)) } end end @@ -102,4 +91,23 @@ def set_income def income_params params.require(:income).permit(:income_type, :rate, :hours, :weekly_income) end + + def build_locals(income) + income_fixed_expense = IncomeFixedExpenseService.new + + { + total_annual_cost: income_fixed_expense.total_annual_cost, + total_monthly_cost: income_fixed_expense.total_monthly_cost, + total_bi_weekly_cost: income_fixed_expense.total_bi_weekly_cost, + income: income + } + end + + def hourly + Income.tax_on_income(income_type: "Hourly") + end + + def salary + Income.tax_on_income(income_type: "Salary") + end end diff --git a/app/models/income.rb b/app/models/income.rb index fa69c73..bd3155c 100644 --- a/app/models/income.rb +++ b/app/models/income.rb @@ -54,8 +54,6 @@ def is_salary? def self.tax_on_income(income_type:) income = Income.find_by(income_type: income_type) - taxable_income = IncomeTaxCalculator.new(income: income) - taxable_income.calculate_taxes - taxable_income + IncomeTaxCalculatorService.new(income: income) end end diff --git a/app/services/income_fixed_expense_service.rb b/app/services/income_fixed_expense_service.rb new file mode 100644 index 0000000..ac67c3c --- /dev/null +++ b/app/services/income_fixed_expense_service.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class IncomeFixedExpenseService + attr_reader :total_annual_cost, :total_monthly_cost, :total_bi_weekly_cost + + def initialize + @total_annual_cost = FixedExpense.total_annual_cost + @total_monthly_cost = FixedExpense.total_monthly_cost + @total_bi_weekly_cost = FixedExpense.total_bi_weekly_cost + end +end diff --git a/app/poros/income_tax_calculator.rb b/app/services/income_tax_calculator_service.rb similarity index 55% rename from app/poros/income_tax_calculator.rb rename to app/services/income_tax_calculator_service.rb index 0a1f112..e98be81 100644 --- a/app/poros/income_tax_calculator.rb +++ b/app/services/income_tax_calculator_service.rb @@ -1,4 +1,4 @@ -class IncomeTaxCalculator +class IncomeTaxCalculatorService attr_reader :income, :annual_income, :federal_tax, @@ -14,20 +14,7 @@ class IncomeTaxCalculator def initialize(income:) @income = income - @annual_income = income.weekly_income_cents * 52 - @federal_tax = nil - @net_after_fed_tax = nil - @state_tax = nil - @total_net_income = nil - @bi_weekly_net_income = nil - @daily_income = nil - @weekly_income = nil - @monthly_income = nil - @quarterly_income = nil - @biannual_income = nil - end - - def calculate_taxes + @annual_income = income.weekly_income * 52 @federal_tax = calculate_fed_tax @net_after_fed_tax = calculate_net_after_fed_tax @state_tax = calculate_state_tax @@ -43,45 +30,44 @@ def calculate_taxes private def calculate_fed_tax - bracket = FederalTaxBracket.where("bottom_range_cents <= ?", @annual_income).order(:bottom_range_cents).last + bracket = FederalTaxBracket.where("bottom_range_cents <= ?", @annual_income.cents).order(:bottom_range_cents).last rated = bracket.rate * @annual_income - Money.new(rated + bracket.cumulative_cents, "USD") + rated + bracket.cumulative end def calculate_state_tax - state_tax = (@net_after_fed_tax.fractional * 0.0463).to_i - Money.new(state_tax, "USD") + @net_after_fed_tax * 0.0463 end def calculate_net_after_fed_tax - Money.new(@annual_income - @federal_tax.fractional, "USD") + @annual_income - @federal_tax end def calculate_total_net_income - Money.new(@net_after_fed_tax.fractional - @state_tax.fractional, "USD") + @net_after_fed_tax - @state_tax end def calculate_bi_weekly_income - Money.new(@total_net_income.fractional / 26) + @total_net_income / 26 end def calculate_daily_income - Money.new(@total_net_income.fractional / 365) + @total_net_income / 365 end def calculate_weekly_income - Money.new(@total_net_income.fractional / 52) + @total_net_income / 52 end def calculate_monthly_income - Money.new(@total_net_income.fractional / 12) + @total_net_income / 12 end def calculate_quarterly_income - Money.new(@total_net_income.fractional / 4) + @total_net_income / 4 end def calculate_biannual_income - Money.new(@total_net_income.fractional / 2) + @total_net_income / 2 end end