Class: Loan

Inherits:
Ekylibre::Record::Base show all
Includes:
Attachable, Customizable
Defined in:
app/models/loan.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2019 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: loans

accountable_repayments_started_on :date
accounted_at                      :datetime
amount                            :decimal(19, 4)   not null
bank_guarantee_account_id         :integer
bank_guarantee_amount             :integer
cash_id                           :integer          not null
created_at                        :datetime         not null
creator_id                        :integer
currency                          :string           not null
custom_fields                     :jsonb
id                                :integer          not null, primary key
initial_releasing_amount          :boolean          default(FALSE), not null
insurance_account_id              :integer
insurance_percentage              :decimal(19, 4)   not null
insurance_repayment_method        :string
interest_account_id               :integer
interest_percentage               :decimal(19, 4)   not null
journal_entry_id                  :integer
lender_id                         :integer          not null
loan_account_id                   :integer
lock_version                      :integer          default(0), not null
name                              :string           not null
ongoing_at                        :datetime
repaid_at                         :datetime
repayment_duration                :integer          not null
repayment_method                  :string           not null
repayment_period                  :string           not null
shift_duration                    :integer          default(0), not null
shift_method                      :string
started_on                        :date             not null
state                             :string
updated_at                        :datetime         not null
updater_id                        :integer
use_bank_guarantee                :boolean

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Customizable

#custom_value, #set_custom_value, #validate_custom_fields

Methods inherited from Ekylibre::Record::Base

#already_updated?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Class Method Details

.bookkeep_repayments(options = {}) ⇒ Object

Bookkeep all repayments until today of given date which must anterior to today. :id parameter permit to filter on wanted loans.


165
166
167
168
169
170
171
172
173
# File 'app/models/loan.rb', line 165

def self.bookkeep_repayments(options = {})
  limit_on = Time.zone.today
  limit_on = [options[:until], limit_on].min if options[:until]
  repayments = LoanRepayment.bookkeepable_before(limit_on)
  repayments = repayments.of_loans(options[:id]) if options[:id]
  count = repayments.count
  repayments.find_each { |repayment| repayment.update(accountable: true) }
  count
end

Instance Method Details

#confirm(ongoing_at = nil) ⇒ Object


242
243
244
245
246
247
248
# File 'app/models/loan.rb', line 242

def confirm(ongoing_at = nil)
  return false unless can_confirm?
  reload
  self.ongoing_at ||= ongoing_at || Time.zone.now
  save!
  super
end

#current_remaining_amount(on = Date.today) ⇒ Object


223
224
225
226
227
# File 'app/models/loan.rb', line 223

def current_remaining_amount(on = Date.today)
  r = repayments.where('due_on <= ?', on).reorder(:position).last
  return nil unless r
  r.remaining_amount
end

#draft?Boolean

why ? we have state machine ?

Returns:

  • (Boolean)

230
231
232
# File 'app/models/loan.rb', line 230

def draft?
  state.to_sym == :draft
end

#generate_repaymentsObject


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/models/loan.rb', line 175

def generate_repayments
  period = if repayment_period_month?
             12
           elsif repayment_period_trimester?
             4
           elsif repayment_period_semester?
             2
           else
             1
           end

  length = if repayment_period_month?
             1.month
           elsif repayment_period_trimester?
             3.months
           elsif repayment_period_semester?
             6.months
           else
             1.year
           end

  ids = []
  Calculus::Loan
    .new(
      amount,
      repayment_duration,
      interests:  { interest_amount:  interest_percentage  / 100.0 },
      insurances: { insurance_amount: insurance_percentage / 100.0 },
      period: period,
      length: length,
      shift: self.shift_duration,
      shift_method: shift_method.to_sym,
      insurance_method: insurance_repayment_method,
      started_on: started_on
    )
    .compute_repayments(repayment_method)
    .each do |repayment|
      if r = repayments.find_by(position: repayment[:position])
        r.update_attributes!(repayment)
      else
        r = repayments.create!(repayment)
      end
      ids << r.id
    end
  repayments.destroy(repayments.where.not(id: ids))
  reload
end

#ongoing?Boolean

Returns:

  • (Boolean)

234
235
236
# File 'app/models/loan.rb', line 234

def ongoing?
  state.to_sym == :ongoing
end

#repaid?Boolean

Returns:

  • (Boolean)

238
239
240
# File 'app/models/loan.rb', line 238

def repaid?
  state.to_sym == :repaid
end

#repay(repaid_at = nil) ⇒ Object


250
251
252
253
254
255
256
# File 'app/models/loan.rb', line 250

def repay(repaid_at = nil)
  return false unless can_repay?
  reload
  self.repaid_at ||= repaid_at || Time.zone.now
  save!
  super
end