loan_creator 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.gitlab-ci.yml +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +37 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +133 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/loan_creator.rb +19 -0
- data/lib/loan_creator/borrower_timetable.rb +42 -0
- data/lib/loan_creator/bullet.rb +39 -0
- data/lib/loan_creator/common.rb +117 -0
- data/lib/loan_creator/excel_formulas.rb +37 -0
- data/lib/loan_creator/in_fine.rb +12 -0
- data/lib/loan_creator/initialize_bigdecimal.rb +6 -0
- data/lib/loan_creator/linear.rb +57 -0
- data/lib/loan_creator/standard.rb +75 -0
- data/lib/loan_creator/term.rb +70 -0
- data/lib/loan_creator/timetable.rb +62 -0
- data/lib/loan_creator/version.rb +3 -0
- data/loan_creator.gemspec +30 -0
- metadata +172 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9aa67fe88f64cf983f07ee79e4fffd9c124a6a79
|
4
|
+
data.tar.gz: 8c953567926083ae563102f9001d21adb770d4f9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 407a1e8724d0dcba174d7c41991d034432f13aea6864d6db898fb0ac9db85fdacb391b9f676782d4b9e7085b6a49f861278938061c671b3622d8631a44932662
|
7
|
+
data.tar.gz: 8feda4e05585c7337c9a4d6ca15bc65d1aaecbf7598c1497690d535d025587de1c0b9e5b473d89be955cde55ca0c8df6e8798b09e1be1ea608dfea67542473d1
|
data/.gitignore
ADDED
data/.gitlab-ci.yml
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
Exclude:
|
4
|
+
- 'db/**/*'
|
5
|
+
- 'vendor/**/*'
|
6
|
+
DisplayCopNames: true
|
7
|
+
|
8
|
+
Documentation:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Metrics/BlockLength:
|
12
|
+
Exclude:
|
13
|
+
- spec/**/*
|
14
|
+
|
15
|
+
Metrics/LineLength:
|
16
|
+
Max: 120
|
17
|
+
|
18
|
+
Metrics/MethodLength:
|
19
|
+
Max: 20
|
20
|
+
|
21
|
+
Style/FrozenStringLiteralComment:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Style/ClassAndModuleChildren:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Style/Lambda:
|
28
|
+
EnforcedStyle: literal
|
29
|
+
|
30
|
+
Style/NumericPredicate:
|
31
|
+
EnforcedStyle: comparison
|
32
|
+
|
33
|
+
Naming/VariableNumber:
|
34
|
+
EnforcedStyle: snake_case
|
35
|
+
|
36
|
+
Lint/UnifiedInteger:
|
37
|
+
Enabled: false
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
loan_creator
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.7
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 thibaulth
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
# LoanCreator
|
2
|
+
|
3
|
+
`loan_creator` gem intends to provide a set of methods to allow automatic generation of loan timetables, for simulation, from a lender point of view and from a borrower point of view, regarding financial rounding differences. As of today, the gem makes the borrower support any rounding issue. In a later work, an option should be provided to decide who supports such issues.
|
4
|
+
|
5
|
+
Link to Loan_Creator excel simulator [Click here] (Excel)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'loan_creator'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install loan_creator
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Parent module
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
module LoanCreator
|
29
|
+
```
|
30
|
+
|
31
|
+
There are four types of loans. All inherit from a `LoanCreator::Common` class.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
LoanCreator::Standard
|
35
|
+
LoanCreator::Linear
|
36
|
+
LoanCreator::InFine
|
37
|
+
LoanCreator::Bullet
|
38
|
+
```
|
39
|
+
|
40
|
+
Each instance of one of the previous classes has the following attributes:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
:period
|
44
|
+
:amount
|
45
|
+
:annual_interests_rate
|
46
|
+
:starts_at
|
47
|
+
:duration_in_periods
|
48
|
+
:deferred_in_periods (default to zero)
|
49
|
+
```
|
50
|
+
|
51
|
+
There is also a `LoanCreator::Timetable` class dedicated to record the data of the loans' terms. Each instance of `LoanCreator::Timetable` represents an array of `LoanCreator::Term` records, each having the following attributes:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
# Term number (starts at 1)
|
55
|
+
:index
|
56
|
+
|
57
|
+
# Term date
|
58
|
+
:date
|
59
|
+
|
60
|
+
# Remaining due capital at the beginning of the term
|
61
|
+
:crd_beginning_of_period
|
62
|
+
|
63
|
+
# Remaining due capital at the end of the term
|
64
|
+
:crd_end_of_period
|
65
|
+
|
66
|
+
# Theoricaly due interests
|
67
|
+
:period_theoric_interests
|
68
|
+
|
69
|
+
# Difference between theorical and real (rounded) due interests
|
70
|
+
:delta_interests
|
71
|
+
|
72
|
+
# Accrued interests' delta
|
73
|
+
:accrued_delta_interests
|
74
|
+
|
75
|
+
# Adjustment of -0.01 0 or +0.01 cent depending on accrued_delta_interests
|
76
|
+
:amount_to_add
|
77
|
+
|
78
|
+
# Interests to pay this term
|
79
|
+
:period_interests
|
80
|
+
|
81
|
+
# Capital to pay this term
|
82
|
+
:period_capital
|
83
|
+
|
84
|
+
# Total capital paid so far (including current term)
|
85
|
+
:total_paid_capital_end_of_period
|
86
|
+
|
87
|
+
# Total interests paid so far (including current term)
|
88
|
+
:total_paid_interests_end_of_period
|
89
|
+
|
90
|
+
# Amount to pay this term
|
91
|
+
:period_amount_to_pay
|
92
|
+
```
|
93
|
+
|
94
|
+
`#periodic_interests_rate` renders a precise calculation of the loan's periodic interests rate based on two inputs: `#annual_interests_rate` and `#period`.
|
95
|
+
|
96
|
+
`#lender_timetable` shall be defined in each loan class. It renders an instance of `LoanCreator::Timetable` which contains an ascending order array of `LoanCreator::Term`. It takes into account financial rounding differences and makes the borrower
|
97
|
+
support all those differences.
|
98
|
+
|
99
|
+
`.borrower_timetable(*lenders_timetables)` (class method) intends to sum each attribute of each provided `lender_timetable` on each term and thus to provide an ascending order array of `LoanCreator::Term`. It should be used for the borrower of a loan, once all lenders and their lending amounts
|
100
|
+
are known. It makes the borrower support all financial rounding differences.
|
101
|
+
|
102
|
+
## Explanation
|
103
|
+
|
104
|
+
`Standard` loan generates terms with constant payments.
|
105
|
+
|
106
|
+
`Linear` loan generates terms with constant capital share payment.
|
107
|
+
|
108
|
+
`Standard` and `Linear` loans may be capital-deferred, i.e. capital repayment is delayed.\
|
109
|
+
Interests are to be payed normally during this period.
|
110
|
+
|
111
|
+
`InFine` loan generates terms where terms' payments are composed by interests only.\
|
112
|
+
Capital share shall be repaid in full at loan's end.
|
113
|
+
|
114
|
+
`Bullet` loan generates terms where terms' payments are zero. \
|
115
|
+
Interests are capitalized, i.e. added to the borrowed capital on each term.\
|
116
|
+
Capital share shall be repaid in full and all interests paid at loan's end.
|
117
|
+
|
118
|
+
There is no deferred time for `InFine` and `Bullet` loans as it would be equivalent to increasing loan's duration.
|
119
|
+
|
120
|
+
## Development
|
121
|
+
|
122
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
123
|
+
|
124
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
125
|
+
|
126
|
+
## Contributing
|
127
|
+
|
128
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/loan_creator.
|
129
|
+
|
130
|
+
|
131
|
+
## License
|
132
|
+
|
133
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "loan_creator"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/loan_creator.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'bigdecimal'
|
3
|
+
|
4
|
+
require 'loan_creator/initialize_bigdecimal'
|
5
|
+
require 'loan_creator/version'
|
6
|
+
|
7
|
+
module LoanCreator
|
8
|
+
BIG_DECIMAL_DIGITS = 14
|
9
|
+
|
10
|
+
autoload :ExcelFormulas, 'loan_creator/excel_formulas'
|
11
|
+
autoload :BorrowerTimetable, 'loan_creator/borrower_timetable'
|
12
|
+
autoload :Common, 'loan_creator/common'
|
13
|
+
autoload :Standard, 'loan_creator/standard'
|
14
|
+
autoload :Linear, 'loan_creator/linear'
|
15
|
+
autoload :InFine, 'loan_creator/in_fine'
|
16
|
+
autoload :Bullet, 'loan_creator/bullet'
|
17
|
+
autoload :Timetable, 'loan_creator/timetable'
|
18
|
+
autoload :Term, 'loan_creator/term'
|
19
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module LoanCreator
|
2
|
+
module BorrowerTimetable
|
3
|
+
BORROWER_FINANCIAL_ATTRIBUTES = [
|
4
|
+
:crd_beginning_of_period,
|
5
|
+
:crd_end_of_period,
|
6
|
+
:period_interests,
|
7
|
+
:period_capital,
|
8
|
+
:total_paid_capital_end_of_period,
|
9
|
+
:total_paid_interests_end_of_period,
|
10
|
+
:period_amount_to_pay
|
11
|
+
].freeze
|
12
|
+
|
13
|
+
def borrower_timetable(*lenders_timetables)
|
14
|
+
raise ArgumentError.new('Array of LoanCreator::Timetable expected') unless Array === lenders_timetables
|
15
|
+
raise ArgumentError.new('At least one LoanCreator::Timetable expected') unless lenders_timetables.length > 0
|
16
|
+
lenders_timetables.each do |lender_timetable|
|
17
|
+
raise ArgumentError.new('Array of LoanCreator::Timetable expected') unless LoanCreator::Timetable === lender_timetable
|
18
|
+
end
|
19
|
+
|
20
|
+
borrower_timetable = LoanCreator::Timetable.new(
|
21
|
+
starts_at: lenders_timetables.first.starts_at,
|
22
|
+
period: lenders_timetables.first.period
|
23
|
+
)
|
24
|
+
|
25
|
+
# Borrower timetable is not concerned with computation-related value (delta, etc.),
|
26
|
+
# thus we start with all values to zero, then we override only BORROWER_FINANCIAL_ATTRIBUTES.
|
27
|
+
all_zero = LoanCreator::Term::ARGUMENTS.each_with_object({}) { |k, h| h[k] = bigd('0') }
|
28
|
+
|
29
|
+
# Group lenders' terms by index
|
30
|
+
transposed_terms = lenders_timetables.map(&:terms).transpose
|
31
|
+
# For each term, sum each required element
|
32
|
+
# First borrower's term contains the sums of lenders' first terms' elements (LoanCreator::Term::ARGUMENT), etc.
|
33
|
+
transposed_terms.each do |arr|
|
34
|
+
term = BORROWER_FINANCIAL_ATTRIBUTES.each_with_object({}) do |k, h|
|
35
|
+
h[k] = arr.inject(bigd('0')) { |sum, tt| sum + tt.send(k) }
|
36
|
+
end
|
37
|
+
borrower_timetable << LoanCreator::Term.new(all_zero.merge(term))
|
38
|
+
end
|
39
|
+
borrower_timetable
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module LoanCreator
|
2
|
+
class Bullet < LoanCreator::Common
|
3
|
+
def lender_timetable
|
4
|
+
raise ArgumentError.new(:deferred_in_periods) unless deferred_in_periods == 0
|
5
|
+
timetable = new_timetable
|
6
|
+
reset_current_term
|
7
|
+
@crd_beginning_of_period = amount
|
8
|
+
@crd_end_of_period = amount
|
9
|
+
(duration_in_periods - 1).times { timetable << current_term }
|
10
|
+
compute_last_term
|
11
|
+
timetable << current_term
|
12
|
+
timetable
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def compute_last_term
|
18
|
+
@crd_end_of_period = bigd('0')
|
19
|
+
@period_interests = total_interests
|
20
|
+
@period_capital = @crd_beginning_of_period
|
21
|
+
@total_paid_capital_end_of_period = @period_capital
|
22
|
+
@total_paid_interests_end_of_period = @period_interests
|
23
|
+
@period_amount_to_pay = @period_capital + @period_interests
|
24
|
+
end
|
25
|
+
|
26
|
+
# Capital * (periodic_interests_rate ^(total_terms))
|
27
|
+
#
|
28
|
+
def total_payment
|
29
|
+
amount.mult(
|
30
|
+
(bigd(1) + periodic_interests_rate) ** bigd(duration_in_periods),
|
31
|
+
BIG_DECIMAL_DIGITS
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
def total_interests
|
36
|
+
total_payment - amount
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module LoanCreator
|
2
|
+
class Common
|
3
|
+
extend BorrowerTimetable
|
4
|
+
|
5
|
+
PERIODS_IN_MONTHS = {
|
6
|
+
month: 1,
|
7
|
+
quarter: 3,
|
8
|
+
semester: 6,
|
9
|
+
year: 12
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
REQUIRED_ATTRIBUTES = [
|
13
|
+
:period,
|
14
|
+
:amount,
|
15
|
+
:annual_interests_rate,
|
16
|
+
:starts_at,
|
17
|
+
:duration_in_periods
|
18
|
+
].freeze
|
19
|
+
|
20
|
+
OPTIONAL_ATTRIBUTES = {
|
21
|
+
# attribute: default_value
|
22
|
+
deferred_in_periods: 0
|
23
|
+
}.freeze
|
24
|
+
|
25
|
+
attr_reader *REQUIRED_ATTRIBUTES
|
26
|
+
attr_reader *OPTIONAL_ATTRIBUTES.keys
|
27
|
+
|
28
|
+
def initialize(**options)
|
29
|
+
@options = options
|
30
|
+
require_attributes
|
31
|
+
set_attributes
|
32
|
+
validate_attributes
|
33
|
+
end
|
34
|
+
|
35
|
+
def periodic_interests_rate_percentage
|
36
|
+
@periodic_interests_rate_percentage ||=
|
37
|
+
annual_interests_rate.div(12 / PERIODS_IN_MONTHS[period], BIG_DECIMAL_DIGITS)
|
38
|
+
end
|
39
|
+
|
40
|
+
def periodic_interests_rate
|
41
|
+
@periodic_interests_rate ||=
|
42
|
+
periodic_interests_rate_percentage.div(100, BIG_DECIMAL_DIGITS)
|
43
|
+
end
|
44
|
+
|
45
|
+
def lender_timetable
|
46
|
+
raise NotImplementedError
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.bigd(value)
|
50
|
+
BigDecimal.new(value, BIG_DECIMAL_DIGITS)
|
51
|
+
end
|
52
|
+
|
53
|
+
def bigd(value)
|
54
|
+
self.class.bigd(value)
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def require_attributes
|
60
|
+
REQUIRED_ATTRIBUTES.each { |k| raise ArgumentError.new(k) unless @options.fetch(k, nil) }
|
61
|
+
end
|
62
|
+
|
63
|
+
def set_attributes
|
64
|
+
REQUIRED_ATTRIBUTES.each { |k| instance_variable_set(:"@#{k}", @options.fetch(k)) }
|
65
|
+
OPTIONAL_ATTRIBUTES.each { |k,v| instance_variable_set(:"@#{k}", @options.fetch(k, v)) }
|
66
|
+
end
|
67
|
+
|
68
|
+
def validate(key, &block)
|
69
|
+
raise unless block.call(instance_variable_get(:"@#{key}"))
|
70
|
+
rescue
|
71
|
+
raise ArgumentError.new(key)
|
72
|
+
end
|
73
|
+
|
74
|
+
def validate_attributes
|
75
|
+
validate(:period) { |v| PERIODS_IN_MONTHS.keys.include?(v) }
|
76
|
+
validate(:amount) { |v| v.is_a?(BigDecimal) && v > 0 }
|
77
|
+
validate(:annual_interests_rate) { |v| v.is_a?(BigDecimal) && v >= 0 }
|
78
|
+
validate(:starts_at) { |v| !!Date.parse(v) }
|
79
|
+
validate(:duration_in_periods) { |v| v.is_a?(Integer) && v > 0 }
|
80
|
+
validate(:deferred_in_periods) { |v| v.is_a?(Integer) && v >= 0 && v < duration_in_periods }
|
81
|
+
end
|
82
|
+
|
83
|
+
def reset_current_term
|
84
|
+
@crd_beginning_of_period = bigd('0')
|
85
|
+
@crd_end_of_period = bigd('0')
|
86
|
+
@period_theoric_interests = bigd('0')
|
87
|
+
@delta_interests = bigd('0')
|
88
|
+
@accrued_delta_interests = bigd('0')
|
89
|
+
@amount_to_add = bigd('0')
|
90
|
+
@period_interests = bigd('0')
|
91
|
+
@period_capital = bigd('0')
|
92
|
+
@total_paid_capital_end_of_period = bigd('0')
|
93
|
+
@total_paid_interests_end_of_period = bigd('0')
|
94
|
+
@period_amount_to_pay = bigd('0')
|
95
|
+
end
|
96
|
+
|
97
|
+
def current_term
|
98
|
+
LoanCreator::Term.new(
|
99
|
+
crd_beginning_of_period: @crd_beginning_of_period,
|
100
|
+
crd_end_of_period: @crd_end_of_period,
|
101
|
+
period_theoric_interests: @period_theoric_interests,
|
102
|
+
delta_interests: @delta_interests,
|
103
|
+
accrued_delta_interests: @accrued_delta_interests,
|
104
|
+
amount_to_add: @amount_to_add,
|
105
|
+
period_interests: @period_interests,
|
106
|
+
period_capital: @period_capital,
|
107
|
+
total_paid_capital_end_of_period: @total_paid_capital_end_of_period,
|
108
|
+
total_paid_interests_end_of_period: @total_paid_interests_end_of_period,
|
109
|
+
period_amount_to_pay: @period_amount_to_pay
|
110
|
+
)
|
111
|
+
end
|
112
|
+
|
113
|
+
def new_timetable
|
114
|
+
LoanCreator::Timetable.new(starts_at: starts_at, period: period)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# Source: https://gist.github.com/mattetti/1015948
|
2
|
+
|
3
|
+
module LoanCreator::ExcelFormulas
|
4
|
+
def ipmt(rate, per, nper, pv, fv=0, type=0)
|
5
|
+
p = _pmt(rate, nper, pv, fv, 0);
|
6
|
+
ip = -(pv * _pow1p(rate, per - 1) * rate + p * _pow1pm1(rate, per - 1))
|
7
|
+
(type == 0) ? ip : ip / (1 + rate)
|
8
|
+
end
|
9
|
+
|
10
|
+
def ppmt(rate, per, nper, pv, fv=0, type=0)
|
11
|
+
p = _pmt(rate, nper, pv, fv, type)
|
12
|
+
ip = ipmt(rate, per, nper, pv, fv, type)
|
13
|
+
p - ip
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
|
18
|
+
def _pmt(rate, nper, pv, fv=0, type=0)
|
19
|
+
((-pv * _pvif(rate, nper) - fv ) / ((bigd('1.0') + rate * type) * _fvifa(rate, nper)))
|
20
|
+
end
|
21
|
+
|
22
|
+
def _pow1pm1(x, y)
|
23
|
+
(x <= -1) ? ((1 + x) ** y) - 1 : Math.exp(y * Math.log(bigd('1.0') + x)) - 1
|
24
|
+
end
|
25
|
+
|
26
|
+
def _pow1p(x, y)
|
27
|
+
(x.abs > bigd('0.5')) ? ((1 + x) ** y) : Math.exp(y * Math.log(bigd('1.0') + x))
|
28
|
+
end
|
29
|
+
|
30
|
+
def _pvif(rate, nper)
|
31
|
+
_pow1p(rate, nper)
|
32
|
+
end
|
33
|
+
|
34
|
+
def _fvifa(rate, nper)
|
35
|
+
(rate == 0) ? nper : _pow1pm1(rate, nper) / rate
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module LoanCreator
|
2
|
+
class InFine < LoanCreator::Common
|
3
|
+
# InFine is the same as a Linear loan with (duration - 1) deferred periods.
|
4
|
+
# Thus we're generating a Linear loan instead of rewriting already existing code.
|
5
|
+
def lender_timetable
|
6
|
+
raise ArgumentError.new(:deferred_in_periods) unless deferred_in_periods == 0
|
7
|
+
options = { deferred_in_periods: duration_in_periods - 1 }
|
8
|
+
options = REQUIRED_ATTRIBUTES.each_with_object(options) { |k,h| h[k] = send(k) }
|
9
|
+
LoanCreator::Linear.new(options).lender_timetable
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
# Round towards the nearest neighbor, unless both neighbors are
|
2
|
+
# equidistant, in which case round towards the even neighbor
|
3
|
+
# (Bank rounding)
|
4
|
+
# usage of BigDecimal method: div(value, digits)
|
5
|
+
# usage of BigDecimal method: mult(value, digits)
|
6
|
+
BigDecimal.mode(BigDecimal::ROUND_HALF_EVEN, true)
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module LoanCreator
|
3
|
+
class Linear < LoanCreator::Common
|
4
|
+
def lender_timetable
|
5
|
+
timetable = new_timetable
|
6
|
+
reset_current_term
|
7
|
+
@crd_end_of_period = amount
|
8
|
+
duration_in_periods.times do |idx|
|
9
|
+
@last_period = last_period?(idx)
|
10
|
+
@deferred_period = idx < deferred_in_periods
|
11
|
+
compute_current_term
|
12
|
+
timetable << current_term
|
13
|
+
end
|
14
|
+
timetable
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def last_period?(idx)
|
20
|
+
idx == (duration_in_periods - 1)
|
21
|
+
end
|
22
|
+
|
23
|
+
def compute_current_term
|
24
|
+
# Reminder: CRD beginning of period = CRD end of period **of previous period**
|
25
|
+
@crd_beginning_of_period = @crd_end_of_period
|
26
|
+
@period_theoric_interests = @crd_beginning_of_period * periodic_interests_rate
|
27
|
+
@delta_interests = @period_theoric_interests - @period_theoric_interests.round(2)
|
28
|
+
@accrued_delta_interests += @delta_interests
|
29
|
+
@amount_to_add = bigd(
|
30
|
+
if @accrued_delta_interests >= bigd('0.01')
|
31
|
+
'0.01'
|
32
|
+
elsif @accrued_delta_interests <= bigd('-0.01')
|
33
|
+
'-0.01'
|
34
|
+
else
|
35
|
+
'0'
|
36
|
+
end
|
37
|
+
)
|
38
|
+
@accrued_delta_interests -= @amount_to_add
|
39
|
+
@period_interests = @period_theoric_interests.round(2) + @amount_to_add
|
40
|
+
@period_capital = period_capital
|
41
|
+
@total_paid_capital_end_of_period += @period_capital
|
42
|
+
@total_paid_interests_end_of_period += @period_interests
|
43
|
+
@period_amount_to_pay = @period_interests + @period_capital
|
44
|
+
@crd_end_of_period -= @period_capital
|
45
|
+
end
|
46
|
+
|
47
|
+
def period_capital
|
48
|
+
if @last_period
|
49
|
+
@crd_beginning_of_period
|
50
|
+
elsif @deferred_period
|
51
|
+
bigd(0)
|
52
|
+
else
|
53
|
+
(amount / (duration_in_periods - deferred_in_periods)).round(2)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module LoanCreator
|
2
|
+
class Standard < LoanCreator::Common
|
3
|
+
include LoanCreator::ExcelFormulas
|
4
|
+
|
5
|
+
def lender_timetable
|
6
|
+
timetable = new_timetable
|
7
|
+
reset_current_term
|
8
|
+
@crd_end_of_period = amount
|
9
|
+
duration_in_periods.times do |idx|
|
10
|
+
@last_period = last_period?(idx)
|
11
|
+
@deferred_period = idx < deferred_in_periods
|
12
|
+
compute_current_term(idx)
|
13
|
+
timetable << current_term
|
14
|
+
end
|
15
|
+
timetable
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def last_period?(idx)
|
21
|
+
idx == (duration_in_periods - 1)
|
22
|
+
end
|
23
|
+
|
24
|
+
def compute_current_term(idx)
|
25
|
+
@crd_beginning_of_period = @crd_end_of_period
|
26
|
+
@period_theoric_interests = period_theoric_interests(idx)
|
27
|
+
@delta_interests = @period_theoric_interests - @period_theoric_interests.round(2)
|
28
|
+
@accrued_delta_interests += @delta_interests
|
29
|
+
@amount_to_add = bigd(
|
30
|
+
if @accrued_delta_interests >= bigd('0.01')
|
31
|
+
'0.01'
|
32
|
+
elsif @accrued_delta_interests <= bigd('-0.01')
|
33
|
+
'-0.01'
|
34
|
+
else
|
35
|
+
'0'
|
36
|
+
end
|
37
|
+
)
|
38
|
+
@accrued_delta_interests -= @amount_to_add
|
39
|
+
@period_interests = @period_theoric_interests.round(2) + @amount_to_add
|
40
|
+
@period_capital = period_capital(idx)
|
41
|
+
@total_paid_capital_end_of_period += @period_capital
|
42
|
+
@total_paid_interests_end_of_period += @period_interests
|
43
|
+
@period_amount_to_pay = @period_interests + @period_capital
|
44
|
+
@crd_end_of_period -= @period_capital
|
45
|
+
end
|
46
|
+
|
47
|
+
def period_theoric_interests(idx)
|
48
|
+
if @deferred_period
|
49
|
+
@crd_beginning_of_period * periodic_interests_rate
|
50
|
+
else
|
51
|
+
-ipmt(
|
52
|
+
periodic_interests_rate,
|
53
|
+
(idx + 1) - deferred_in_periods,
|
54
|
+
duration_in_periods - deferred_in_periods,
|
55
|
+
amount
|
56
|
+
)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def period_capital(idx)
|
61
|
+
if @last_period
|
62
|
+
@crd_beginning_of_period
|
63
|
+
elsif @deferred_period
|
64
|
+
bigd(0)
|
65
|
+
else
|
66
|
+
-ppmt(
|
67
|
+
periodic_interests_rate,
|
68
|
+
(idx + 1) - deferred_in_periods,
|
69
|
+
duration_in_periods - deferred_in_periods,
|
70
|
+
amount
|
71
|
+
).round(2)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module LoanCreator
|
2
|
+
class Term
|
3
|
+
|
4
|
+
ARGUMENTS = [
|
5
|
+
# Remaining due capital at the beginning of the term
|
6
|
+
:crd_beginning_of_period,
|
7
|
+
|
8
|
+
# Remaining due capital at the end of the term
|
9
|
+
:crd_end_of_period,
|
10
|
+
|
11
|
+
# Theoricaly due interests
|
12
|
+
:period_theoric_interests,
|
13
|
+
|
14
|
+
# Difference between theorical and real (rounded) due interests
|
15
|
+
:delta_interests,
|
16
|
+
|
17
|
+
# Accrued interests' delta
|
18
|
+
:accrued_delta_interests,
|
19
|
+
|
20
|
+
# Adjustment of -0.01, 0 or +0.01 cent depending on accrued_delta_interests
|
21
|
+
:amount_to_add,
|
22
|
+
|
23
|
+
# Interests to pay this term
|
24
|
+
:period_interests,
|
25
|
+
|
26
|
+
# Capital to pay this term
|
27
|
+
:period_capital,
|
28
|
+
|
29
|
+
# Total capital paid so far (including current term)
|
30
|
+
:total_paid_capital_end_of_period,
|
31
|
+
|
32
|
+
# Total interests paid so far (including current term)
|
33
|
+
:total_paid_interests_end_of_period,
|
34
|
+
|
35
|
+
# Amount to pay this term
|
36
|
+
:period_amount_to_pay
|
37
|
+
].freeze
|
38
|
+
|
39
|
+
ATTRIBUTES = [
|
40
|
+
# Term number (starts at 1)
|
41
|
+
# This value is to be set by Timetable
|
42
|
+
:index,
|
43
|
+
|
44
|
+
# Term date
|
45
|
+
# This value is to be set by Timetable
|
46
|
+
:date,
|
47
|
+
|
48
|
+
# These values are to be specified during Term's initialization
|
49
|
+
*ARGUMENTS
|
50
|
+
].freeze
|
51
|
+
|
52
|
+
attr_accessor *ATTRIBUTES
|
53
|
+
|
54
|
+
def initialize(**options)
|
55
|
+
ARGUMENTS.each { |k| instance_variable_set(:"@#{k}", options.fetch(k)) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_csv
|
59
|
+
ATTRIBUTES.map { |k| instance_variable_get(:"@#{k}") }.join(',')
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_s
|
63
|
+
to_csv
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_h
|
67
|
+
ATTRIBUTES.each_with_object({}) { |k, h| h[k] = instance_variable_get(:"@#{k}") }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module LoanCreator
|
3
|
+
class Timetable
|
4
|
+
# Used to calculate next term's date (see ActiveSupport#advance)
|
5
|
+
PERIODS = {
|
6
|
+
month: { months: 1 },
|
7
|
+
quarter: { months: 3 },
|
8
|
+
semester: { months: 6 },
|
9
|
+
year: { years: 1 }
|
10
|
+
}
|
11
|
+
|
12
|
+
attr_reader :terms, :starts_at, :period
|
13
|
+
|
14
|
+
def initialize(starts_at:, period:)
|
15
|
+
@terms = []
|
16
|
+
@starts_at = (Date === starts_at ? starts_at : Date.parse(starts_at))
|
17
|
+
raise ArgumentError.new(:period) unless PERIODS.keys.include?(period)
|
18
|
+
@period = period
|
19
|
+
end
|
20
|
+
|
21
|
+
def <<(term)
|
22
|
+
raise ArgumentError.new('LoanCreator::Term expected') unless LoanCreator::Term === term
|
23
|
+
term.index = autoincrement_index
|
24
|
+
term.date = autoincrement_date
|
25
|
+
@terms << term
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def reset_indexes_and_dates
|
30
|
+
@autoincrement_index = 0
|
31
|
+
@autoincrement_date = @starts_at
|
32
|
+
@terms.each do |term|
|
33
|
+
term[:index] = autoincrement_index
|
34
|
+
term[:date] = autoincrement_date
|
35
|
+
end
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_csv(header: true)
|
40
|
+
output = []
|
41
|
+
output << terms.first.to_h.keys.join(',') if header
|
42
|
+
terms.each { |t| output << t.to_csv }
|
43
|
+
output
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
# First term index of a timetable term is 1
|
49
|
+
def autoincrement_index
|
50
|
+
@autoincrement_index ||= 0
|
51
|
+
@autoincrement_index += 1
|
52
|
+
end
|
53
|
+
|
54
|
+
# First term date of timetable term is the starts_at given date
|
55
|
+
def autoincrement_date
|
56
|
+
@autoincrement_date ||= @starts_at
|
57
|
+
date = @autoincrement_date
|
58
|
+
@autoincrement_date = @autoincrement_date.advance(PERIODS.fetch(@period))
|
59
|
+
date
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'loan_creator/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'loan_creator'
|
7
|
+
spec.version = LoanCreator::VERSION
|
8
|
+
spec.authors = %w[thibaulth nicob younes.serraj]
|
9
|
+
spec.email = ['thibault@capsens.eu', 'nicolas.besnard@capsens.eu', 'younes.serraj@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Create and update timetables from input data'
|
12
|
+
spec.homepage = 'https://capsens.githost.io/capsens/loan-creator'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
spec.add_development_dependency 'simplecov', '~> 0.16'
|
26
|
+
spec.add_development_dependency 'byebug', '~> 11.0'
|
27
|
+
|
28
|
+
spec.add_runtime_dependency 'bigdecimal'
|
29
|
+
spec.add_runtime_dependency 'activesupport'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: loan_creator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- thibaulth
|
8
|
+
- nicob
|
9
|
+
- younes.serraj
|
10
|
+
autorequire:
|
11
|
+
bindir: exe
|
12
|
+
cert_chain: []
|
13
|
+
date: 2020-09-16 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.13'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1.13'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rake
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - "~>"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '10.0'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '10.0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: rspec
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - "~>"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '3.0'
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: simplecov
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0.16'
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "~>"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0.16'
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: byebug
|
73
|
+
requirement: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - "~>"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '11.0'
|
78
|
+
type: :development
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - "~>"
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '11.0'
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: bigdecimal
|
87
|
+
requirement: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
type: :runtime
|
93
|
+
prerelease: false
|
94
|
+
version_requirements: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
name: activesupport
|
101
|
+
requirement: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
type: :runtime
|
107
|
+
prerelease: false
|
108
|
+
version_requirements: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
description:
|
114
|
+
email:
|
115
|
+
- thibault@capsens.eu
|
116
|
+
- nicolas.besnard@capsens.eu
|
117
|
+
- younes.serraj@gmail.com
|
118
|
+
executables: []
|
119
|
+
extensions: []
|
120
|
+
extra_rdoc_files: []
|
121
|
+
files:
|
122
|
+
- ".gitignore"
|
123
|
+
- ".gitlab-ci.yml"
|
124
|
+
- ".rspec"
|
125
|
+
- ".rubocop.yml"
|
126
|
+
- ".ruby-gemset"
|
127
|
+
- ".ruby-version"
|
128
|
+
- ".travis.yml"
|
129
|
+
- Gemfile
|
130
|
+
- LICENSE.txt
|
131
|
+
- README.md
|
132
|
+
- Rakefile
|
133
|
+
- bin/console
|
134
|
+
- bin/setup
|
135
|
+
- lib/loan_creator.rb
|
136
|
+
- lib/loan_creator/borrower_timetable.rb
|
137
|
+
- lib/loan_creator/bullet.rb
|
138
|
+
- lib/loan_creator/common.rb
|
139
|
+
- lib/loan_creator/excel_formulas.rb
|
140
|
+
- lib/loan_creator/in_fine.rb
|
141
|
+
- lib/loan_creator/initialize_bigdecimal.rb
|
142
|
+
- lib/loan_creator/linear.rb
|
143
|
+
- lib/loan_creator/standard.rb
|
144
|
+
- lib/loan_creator/term.rb
|
145
|
+
- lib/loan_creator/timetable.rb
|
146
|
+
- lib/loan_creator/version.rb
|
147
|
+
- loan_creator.gemspec
|
148
|
+
homepage: https://capsens.githost.io/capsens/loan-creator
|
149
|
+
licenses:
|
150
|
+
- MIT
|
151
|
+
metadata: {}
|
152
|
+
post_install_message:
|
153
|
+
rdoc_options: []
|
154
|
+
require_paths:
|
155
|
+
- lib
|
156
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
requirements: []
|
167
|
+
rubyforge_project:
|
168
|
+
rubygems_version: 2.5.2.3
|
169
|
+
signing_key:
|
170
|
+
specification_version: 4
|
171
|
+
summary: Create and update timetables from input data
|
172
|
+
test_files: []
|