Sha256: 279f6605831533518845e9c0b15724ce4e9aa9af74ec7f2e3c23d6fefba48da0

Contents?: true

Size: 1.72 KB

Versions: 4

Compression:

Stored size: 1.72 KB

Contents

# coding: utf-8
module LoanCreator
  class Linear < LoanCreator::Common
    def lender_timetable
      timetable = new_timetable
      reset_current_term
      @crd_end_of_period = amount
      duration_in_periods.times do |idx|
        @last_period = last_period?(idx)
        @deferred_period = idx < deferred_in_periods
        compute_current_term
        timetable << current_term
      end
      timetable
    end

    private

    def last_period?(idx)
      idx == (duration_in_periods - 1)
    end

    def compute_current_term
      # Reminder: CRD beginning of period = CRD end of period **of previous period**
      @crd_beginning_of_period = @crd_end_of_period
      @period_theoric_interests = @crd_beginning_of_period * periodic_interests_rate
      @delta_interests = @period_theoric_interests - @period_theoric_interests.round(2)
      @accrued_delta_interests += @delta_interests
      @amount_to_add = bigd(
        if @accrued_delta_interests >= bigd('0.01')
          '0.01'
        elsif @accrued_delta_interests <= bigd('-0.01')
          '-0.01'
        else
          '0'
        end
      )
      @accrued_delta_interests -= @amount_to_add
      @period_interests = @period_theoric_interests.round(2) + @amount_to_add
      @period_capital = period_capital
      @total_paid_capital_end_of_period += @period_capital
      @total_paid_interests_end_of_period += @period_interests
      @period_amount_to_pay = @period_interests + @period_capital
      @crd_end_of_period -= @period_capital
    end

    def period_capital
      if @last_period
        @crd_beginning_of_period
      elsif @deferred_period
        bigd(0)
      else
        (amount / (duration_in_periods - deferred_in_periods)).round(2)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
loan_creator-0.2.0 lib/loan_creator/linear.rb
loan_creator-0.2.1 lib/loan_creator/linear.rb
loan_creator-0.2.2 lib/loan_creator/linear.rb
loan_creator-0.2.3 lib/loan_creator/linear.rb