Class: MudratProjector::TaxCalculation

Inherits:
Object
  • Object
show all
Defined in:
lib/mudrat_projector/tax_calculation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(projector, household, values_hash) ⇒ TaxCalculation

Returns a new instance of TaxCalculation.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mudrat_projector/tax_calculation.rb', line 7

def initialize projector, household, values_hash
  @values_hash = values_hash
  @projector = projector
  @w2_gross = 0
  @se_gross = 0
  @pretax_deductions = 0
  @hsa_contribs = Hash.new { |h,k| h[k] = 0 }
  @household = household
  @itemized_deduction = 0
  @taxes_withheld = 0
  @other_gross = 0
  extend_year_shim
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/mudrat_projector/tax_calculation.rb', line 21

def method_missing method_name, *args
  if args.empty? && values_hash.has_key?(method_name)
    values_hash.fetch method_name
  else
    super
  end
end

Instance Attribute Details

#householdObject (readonly)

Returns the value of attribute household.



5
6
7
# File 'lib/mudrat_projector/tax_calculation.rb', line 5

def household
  @household
end

#other_grossObject (readonly)

Returns the value of attribute other_gross.



4
5
6
# File 'lib/mudrat_projector/tax_calculation.rb', line 4

def other_gross
  @other_gross
end

#pretax_deductionsObject (readonly)

Returns the value of attribute pretax_deductions.



4
5
6
# File 'lib/mudrat_projector/tax_calculation.rb', line 4

def pretax_deductions
  @pretax_deductions
end

#projectorObject (readonly)

Returns the value of attribute projector.



5
6
7
# File 'lib/mudrat_projector/tax_calculation.rb', line 5

def projector
  @projector
end

#se_grossObject (readonly)

Returns the value of attribute se_gross.



4
5
6
# File 'lib/mudrat_projector/tax_calculation.rb', line 4

def se_gross
  @se_gross
end

#taxes_withheldObject (readonly)

Returns the value of attribute taxes_withheld.



5
6
7
# File 'lib/mudrat_projector/tax_calculation.rb', line 5

def taxes_withheld
  @taxes_withheld
end

#values_hashObject (readonly)

Returns the value of attribute values_hash.



3
4
5
# File 'lib/mudrat_projector/tax_calculation.rb', line 3

def values_hash
  @values_hash
end

#w2_grossObject (readonly)

Returns the value of attribute w2_gross.



4
5
6
# File 'lib/mudrat_projector/tax_calculation.rb', line 4

def w2_gross
  @w2_gross
end

Instance Method Details

#<<(transaction) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mudrat_projector/tax_calculation.rb', line 29

def << transaction
  @w2_gross += transaction.salaries_and_wages
  @se_gross += transaction.business_profit
  @other_gross += transaction.dividend_income
  @itemized_deduction += transaction.charitable_contributions
  @itemized_deduction += transaction.interest_paid
  @itemized_deduction += transaction.taxes_paid
  @taxes_withheld     += transaction.taxes_withheld
  transaction.debits.each do |entry|
     = projector.fetch entry.
    if .type == :asset && .tag?(:hsa)
      revenue = transaction.credits.select { |e| a = projector.fetch(e.); a.type == :revenue && a.tag?(:salary)}
      @pretax_deductions += revenue.reduce 0 do |s,e| s + e.amount; end
      @hsa_contribs[] += [entry.amount - pretax_deductions, 0].max
    end
  end
end

#adjustmentsObject



47
48
49
# File 'lib/mudrat_projector/tax_calculation.rb', line 47

def adjustments
  (self_employment_tax / 2) + hsa_contributions
end

#agiObject



51
52
53
# File 'lib/mudrat_projector/tax_calculation.rb', line 51

def agi
  total_income - adjustments
end

#bracketsObject



55
56
57
58
59
60
# File 'lib/mudrat_projector/tax_calculation.rb', line 55

def brackets
  bracket_caps = super.tap do |list|
    list.push Float::INFINITY
  end
  values_hash.fetch(:bracket_rates).zip bracket_caps
end

#deductionObject



62
63
64
# File 'lib/mudrat_projector/tax_calculation.rb', line 62

def deduction
  [itemized_deduction, standard_deduction].max
end

#effective_rateObject



66
67
68
# File 'lib/mudrat_projector/tax_calculation.rb', line 66

def effective_rate
  (((gross - net) / gross) * 100).round 2
end

#exemptionObject



70
71
72
# File 'lib/mudrat_projector/tax_calculation.rb', line 70

def exemption
  personal_exemption * household.exemptions
end

#grossObject



74
75
76
# File 'lib/mudrat_projector/tax_calculation.rb', line 74

def gross
  w2_gross + se_gross + other_gross
end

#hsa_contributionsObject



92
93
94
95
96
97
98
# File 'lib/mudrat_projector/tax_calculation.rb', line 92

def hsa_contributions
  @hsa_contribs.reduce 0 do |sum, (, amount)|
    type = [:individual, :family, :senior].detect { |tag| .tag? tag }
    max = hsa_limit.fetch type
    sum + [amount, max].min
  end
end

#income_taxObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mudrat_projector/tax_calculation.rb', line 78

def income_tax
  amount_taxed = 0
  brackets.reduce 0 do |sum, (rate, cap)|
    if cap == Float::INFINITY
      in_bracket = taxable_income - amount_taxed
    else
      in_bracket = [cap, taxable_income].min - amount_taxed
    end
    amount_taxed += in_bracket
    t = (in_bracket * (rate / 100))
    sum + t
  end
end

#itemized_deductionObject



100
101
102
# File 'lib/mudrat_projector/tax_calculation.rb', line 100

def itemized_deduction
  @itemized_deduction
end

#medicare_withheldObject



104
105
106
# File 'lib/mudrat_projector/tax_calculation.rb', line 104

def medicare_withheld
  w2_gross * (hi_rate / 100)
end

#netObject



108
109
110
# File 'lib/mudrat_projector/tax_calculation.rb', line 108

def net
  gross - taxes
end

#self_employment_taxObject



112
113
114
115
# File 'lib/mudrat_projector/tax_calculation.rb', line 112

def self_employment_tax
  rate = (oasdi_rate + hi_rate) / 100
  se_gross * (1 - rate) * (rate * 2)
end

#social_security_withheldObject



117
118
119
# File 'lib/mudrat_projector/tax_calculation.rb', line 117

def social_security_withheld
  [w2_gross, oasdi_wage_base].min * (oasdi_rate / 100)
end

#taxable_incomeObject



121
122
123
# File 'lib/mudrat_projector/tax_calculation.rb', line 121

def taxable_income
  agi - deduction - exemption
end

#taxesObject



125
126
127
# File 'lib/mudrat_projector/tax_calculation.rb', line 125

def taxes
  withholding_tax + income_tax + self_employment_tax
end

#taxes_owedObject



129
130
131
# File 'lib/mudrat_projector/tax_calculation.rb', line 129

def taxes_owed
  taxes - taxes_withheld
end

#total_incomeObject



133
134
135
# File 'lib/mudrat_projector/tax_calculation.rb', line 133

def total_income
  gross - pretax_deductions
end

#withholding_taxObject



141
142
143
# File 'lib/mudrat_projector/tax_calculation.rb', line 141

def withholding_tax
  medicare_withheld + social_security_withheld
end

#yearObject



137
138
139
# File 'lib/mudrat_projector/tax_calculation.rb', line 137

def year
  projector.from.year
end