class MudratProjector::TaxCalculator

Constants

EXPENSE_ACCOUNT_ID
HOUSEHOLD_TYPES
Household

Attributes

household[RW]

Public Class Methods

new(projector: nil, household: {}) click to toggle source
# File lib/mudrat_projector/tax_calculator.rb, line 83
def initialize projector: nil, household: {}
  @household   = Household.new(
    household.fetch(:filing_status),
    household.fetch(:exemptions),
  )
  @projector   = projector
  @values_hash = parse_yaml
  unless projector.account_exists? EXPENSE_ACCOUNT_ID
    projector.add_account EXPENSE_ACCOUNT_ID, type: :expense
  end
  unless projector.account_exists? :owed_taxes
    projector.add_account :owed_taxes, type: :liability
  end
end
project(projector, to: end_date, household: nil) { |calculation| ... } click to toggle source
# File lib/mudrat_projector/tax_calculator.rb, line 25
def self.project projector, to: end_date, household: nil
  tax_calculator = new projector: projector, household: household
  (projector.from.year..to.year).each do
    calculation = tax_calculator.calculate!
    yield calculation if block_given?
  end
  tax_calculator.projector
end

Public Instance Methods

calculate!() click to toggle source
# File lib/mudrat_projector/tax_calculator.rb, line 98
def calculate!
  end_of_calendar_year = Date.new year, 12, 31
  calculation = TaxCalculation.new projector, household, @values_hash
  next_projector = projector.project to: end_of_calendar_year, build_next: true do |transaction|
    calculation << TransactionWrapper.new(self, transaction).tap(&:calculate!)
  end
  final_transaction = Transaction.new(
    date: end_of_calendar_year,
    debit:  { amount: calculation.taxes_owed, account_id: EXPENSE_ACCOUNT_ID },
    credit: { amount: calculation.taxes_owed, account_id: :owed_taxes },
  )
  projector.apply_transaction final_transaction
  @projector = next_projector
  @values_hash = parse_yaml
  calculation
end
year() click to toggle source
# File lib/mudrat_projector/tax_calculator.rb, line 115
def year
  projector.from.year
end

Private Instance Methods

parse_yaml() click to toggle source
# File lib/mudrat_projector/tax_calculator.rb, line 121
def parse_yaml
  yaml = File.expand_path '../tax_values_by_year.yml', __FILE__
  by_year = YAML.load File.read(yaml)
  max_year = by_year.keys.max
  parsed = by_year.fetch([year, max_year].min).tap do |hash|
    recursively_symbolize_keys! hash
  end
  hash = parsed.fetch household.filing_status
  parsed.each_with_object hash do |(key, value), hash|
    hash[key] = value unless HOUSEHOLD_TYPES.include? key
  end
  hash
end
recursively_symbolize_keys!(hash) click to toggle source
# File lib/mudrat_projector/tax_calculator.rb, line 135
def recursively_symbolize_keys! hash
  hash.keys.each do |key|
    value = hash.delete key
    recursively_symbolize_keys! value if value.is_a? Hash
    key = key.respond_to?(:to_sym) ? key.to_sym : key
    hash[key] = value
  end
end