Skip to content

Instantly share code, notes, and snippets.

@leewaa
Last active October 3, 2024 14:10
Show Gist options
  • Select an option

  • Save leewaa/44d901ffc23f4f56632cd2f38bfd6b96 to your computer and use it in GitHub Desktop.

Select an option

Save leewaa/44d901ffc23f4f56632cd2f38bfd6b96 to your computer and use it in GitHub Desktop.

German Denkmal tax claims calculator

This is a very basic service object for calculating the potential tax savings for money spent on a Denkmal protected real-estate. It does nothing fancy and takes two inputs for the calculation. The calculation itself is very basic and is meant to be only a rough guide. There might be special considerations that need to be added.

class DenkmalTaxSavingsCalculator
  TAX_BRACKETS = [
    { limit: 11600, rate: 0.0 },
    { limit: 52000, rate: 0.22 },
    { limit: 91600, rate: 0.30 },
    { limit: 115600, rate: 0.33 },
    { limit: 277825, rate: 0.42 },
    { limit: Float::INFINITY, rate: 0.45 }
  ]
  CLAIMABLE_PERCENT_PER_YEAR = [0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.07, 0.07, 0.07, 0.07]

  def self.call(yearly_gross_income, denkmal_spending)
    new(yearly_gross_income, denkmal_spending).call
  end

  def call
    tax_per_year = calculate_tax(yearly_gross_income)
    total_tax_paid = tax_per_year * CLAIMABLE_PERCENT_PER_YEAR.count
    total_tax_paid += calculate_tax(yearly_gross_income)

    taxed_paid_years = []

    CLAIMABLE_PERCENT_PER_YEAR.each do |claimable_percent|
      claimable_amount = denkmal_spending * claimable_percent

      taxed_paid_years << calculate_tax(yearly_gross_income - claimable_amount)
    end

    average_saving = tax_per_year - (taxed_paid_years.sum / taxed_paid_years.count)
    total_saving = total_tax_paid - taxed_paid_years.sum

    puts "######### Tax calculater #########\n"
    puts "-- Without claim"
    puts "Total tax".ljust(20) + total_tax_paid.round(2).to_s.rjust(10)
    puts "Tax per year".ljust(20) + tax_per_year.round(2).to_s.rjust(10)
    puts ""

    puts "-- With claim"
    puts "Total tax".ljust(20) + taxed_paid_years.sum.round(2).to_s.rjust(10)
    puts "Tax per year avg".ljust(20) + (taxed_paid_years.sum / taxed_paid_years.count).round(2).to_s.rjust(10)
    puts "Avg savin".ljust(20) + average_saving.round(2).to_s.rjust(10)
    puts "Total saving".ljust(20) + total_saving.round(2).to_s.rjust(10)
    puts ""
    puts '######### End #########'
  end

  private

  attr_reader :yearly_gross_income, :denkmal_spending

  def initialize(yearly_gross_income, denkmal_spending)
    @yearly_gross_income = yearly_gross_income
    @denkmal_spending = denkmal_spending
  end

  def calculate_tax(income)
    tax = 0.0
    previous_limit = 0

    TAX_BRACKETS.each do |bracket|
      if income > bracket[:limit]
        taxable_income = bracket[:limit] - previous_limit
        tax += taxable_income * bracket[:rate]
        previous_limit = bracket[:limit]
      else
        taxable_income = income - previous_limit
        tax += taxable_income * bracket[:rate]
        break
      end
    end

    tax
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment