class TaxEasy::Financial::Brazil::IOF

Public Class Methods

new(overrides = {}) click to toggle source
# File lib/tax_easy/financial/brazil/iof.rb, line 8
def initialize(overrides = {})
  @iof_day = overrides.fetch(:iof_day) { 0.000041 }
  @iof_additional = overrides.fetch(:iof_additional) { 0.0038 }
  @installment_calculator = overrides.fetch(:installment_calculator) { TaxEasy::Calculator::Installment.new }
end

Public Instance Methods

calculate(loan_value, financing_time_months, interest_rate, loan_date = Date.today) click to toggle source

Calculates IOF for Loan in Brazil @param loan_value [Float] @param financing_time_months [Integer] The number of payments to be made @param interest_rate [Float] The interest rate as decimal (not per cent) per period @param loan_date [Date] The loan request date @return [Float]

@example

TaxEasy::Financial::Brazil::IOF.new.calculate(20_000, 48, 1.5) # ==> 356.70671942746617
# File lib/tax_easy/financial/brazil/iof.rb, line 24
def calculate(loan_value, financing_time_months, interest_rate, loan_date = Date.today)
  balance = loan_value
  installment = installment_value(interest_rate, financing_time_months, loan_value)
  installment_date = loan_date.next_month
  iof = 0

  financing_time_months.step(1, -1) do |_|
    amortization = installment - interest_part(balance, interest_rate)

    days = past_days(loan_date, installment_date)

    iof += amortization*(@iof_day*(minimal_days(days)))
    iof += amortization*@iof_additional

    balance -= amortization
    installment_date = installment_date.next_month
  end

  loan_iof(iof, loan_value)
end

Private Instance Methods

installment_value(interest_rate, financing_time_months, loan_value) click to toggle source

Calcualtes installment value for loan

# File lib/tax_easy/financial/brazil/iof.rb, line 48
def installment_value(interest_rate, financing_time_months, loan_value)
  @installment_calculator.calculate(interest_rate, financing_time_months, loan_value)
end
interest_part(balance, interest_rate) click to toggle source

Interest part to be payed on specific installment

# File lib/tax_easy/financial/brazil/iof.rb, line 58
def interest_part(balance, interest_rate)
  balance*(interest_rate/100)
end
loan_iof(iof, loan_value) click to toggle source

Calculates the final IOF base on IOF not loan value

# File lib/tax_easy/financial/brazil/iof.rb, line 53
def loan_iof(iof, loan_value)
  (iof*loan_value)/(loan_value-iof)
end
minimal_days(past_days) click to toggle source

Minimal days from 365 to calculate IOF

# File lib/tax_easy/financial/brazil/iof.rb, line 68
def minimal_days(past_days)
  [past_days, 365].min
end
past_days(start_date, end_date) click to toggle source

Past days from specific date

# File lib/tax_easy/financial/brazil/iof.rb, line 63
def past_days(start_date, end_date)
  (end_date - start_date).to_i
end