Skip to content

Commit

Permalink
Refactor user of services
Browse files Browse the repository at this point in the history
  • Loading branch information
neb417 committed Nov 16, 2023
1 parent 9bf37c2 commit 25a482a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 43 deletions.
34 changes: 21 additions & 13 deletions app/controllers/incomes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 1 addition & 3 deletions app/models/income.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions app/services/income_fixed_expense_service.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class IncomeTaxCalculator
class IncomeTaxCalculatorService
attr_reader :income,
:annual_income,
:federal_tax,
Expand All @@ -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
Expand All @@ -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

0 comments on commit 25a482a

Please sign in to comment.