amortizy 1.0.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/.DS_Store +0 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +12 -0
- data/lib/.DS_Store +0 -0
- data/lib/amortizy/engine.rb +516 -0
- data/lib/amortizy/version.rb +5 -0
- data/lib/amortizy.rb +8 -0
- data/sig/amortizy.rbs +4 -0
- metadata +71 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: '098cde951446da868e5cc5790b832794a09dd7ba991ffb34125f02053e59489e'
|
|
4
|
+
data.tar.gz: 923b3440f1046b7c0c92483997c24934623f234c5e47f78333b9d87ca8536bfa
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 01c8a63684018625527eb633d6b7febc03b92d37ed20221b6138dc66080908ba4d5a0ef4649ae65172cbe67a9d1cb6c455ad70d95804ef9b64e9df083c75ec1b
|
|
7
|
+
data.tar.gz: d57d565cf0631f7f7016776111b1c1fe680549ae1a7583f30ba2c0cdf7b44bf2650e029a43e1593368f162c9209c1c5bca2d93c3eb5e7e95e96c98571b5d2de8
|
data/.DS_Store
ADDED
|
Binary file
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Zapata
|
|
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,43 @@
|
|
|
1
|
+
# Amortizy
|
|
2
|
+
|
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
|
4
|
+
|
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/amortizy`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
+
|
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/amortizy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/amortizy/blob/main/CODE_OF_CONDUCT.md).
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
40
|
+
|
|
41
|
+
## Code of Conduct
|
|
42
|
+
|
|
43
|
+
Everyone interacting in the Amortizy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/amortizy/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/lib/.DS_Store
ADDED
|
Binary file
|
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
# Amortization Schedule Generator
|
|
2
|
+
#
|
|
3
|
+
# Generates comprehensive loan amortization schedules with support for:
|
|
4
|
+
# - Grace periods with capitalized interest
|
|
5
|
+
# - Interest-only payment periods
|
|
6
|
+
# - Origination and additional fees
|
|
7
|
+
# - Simple or precomputed interest methods
|
|
8
|
+
# - Bank business day calculations (Federal Reserve holidays)
|
|
9
|
+
# - Daily or weekly payment frequencies
|
|
10
|
+
|
|
11
|
+
require 'date'
|
|
12
|
+
require 'csv'
|
|
13
|
+
require 'holidays'
|
|
14
|
+
|
|
15
|
+
module Amortizy
|
|
16
|
+
class AmortizationSchedule
|
|
17
|
+
attr_reader :start_date, :principal, :term_months, :annual_rate, :frequency
|
|
18
|
+
|
|
19
|
+
def initialize(start_date:, principal:, term_months:, annual_rate:, frequency:, origination_fee: 0, additional_fee: 0, additional_fee_label: 'Additional Fee', additional_fee_treatment: :distributed, bank_days_only: false, interest_only_periods: 0, grace_period_days: 0, interest_method: :simple)
|
|
20
|
+
@start_date = Date.parse(start_date.to_s)
|
|
21
|
+
@principal = principal.to_f
|
|
22
|
+
@term_months = term_months.to_i
|
|
23
|
+
@annual_rate = annual_rate.to_f / 100.0
|
|
24
|
+
@frequency = frequency.to_sym
|
|
25
|
+
@origination_fee = origination_fee.to_f
|
|
26
|
+
@additional_fee = additional_fee.to_f
|
|
27
|
+
@additional_fee_label = additional_fee_label.to_s
|
|
28
|
+
@additional_fee_treatment = additional_fee_treatment.to_sym
|
|
29
|
+
@bank_days_only = bank_days_only
|
|
30
|
+
@interest_only_periods = interest_only_periods.to_i
|
|
31
|
+
@grace_period_days = grace_period_days.to_i
|
|
32
|
+
@interest_method = interest_method.to_sym
|
|
33
|
+
|
|
34
|
+
validate_term_months!
|
|
35
|
+
validate_frequency!
|
|
36
|
+
validate_fee_treatment!
|
|
37
|
+
validate_interest_only_periods!
|
|
38
|
+
validate_interest_method!
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def generate(output: :console, csv_path: nil)
|
|
42
|
+
case output
|
|
43
|
+
when :console
|
|
44
|
+
generate_console_output
|
|
45
|
+
when :csv
|
|
46
|
+
raise ArgumentError, "csv_path required for CSV output" unless csv_path
|
|
47
|
+
generate_csv_output(csv_path)
|
|
48
|
+
else
|
|
49
|
+
raise ArgumentError, "Output must be :console or :csv"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def validate_frequency!
|
|
56
|
+
unless [:daily, :weekly].include?(@frequency)
|
|
57
|
+
raise ArgumentError, "Frequency must be :daily or :weekly"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def validate_term_months!
|
|
62
|
+
unless [6, 9, 12, 15, 18].include?(@term_months)
|
|
63
|
+
raise ArgumentError, "Term must be 6, 9, 12, 15, or 18 months"
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def validate_fee_treatment!
|
|
68
|
+
unless [:distributed, :add_to_principal, :separate_payment].include?(@additional_fee_treatment)
|
|
69
|
+
raise ArgumentError, "Additional fee treatment must be :distributed, :add_to_principal, or :separate_payment"
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def validate_interest_only_periods!
|
|
74
|
+
total_payments = calculate_total_payments
|
|
75
|
+
if @interest_only_periods >= total_payments
|
|
76
|
+
raise ArgumentError, "Interest-only periods (#{@interest_only_periods}) must be less than total payments (#{total_payments})"
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def validate_interest_method!
|
|
81
|
+
unless [:simple, :precomputed].include?(@interest_method)
|
|
82
|
+
raise ArgumentError, "Interest method must be :simple or :precomputed"
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def calculate_total_payments
|
|
87
|
+
payment_schedule = {
|
|
88
|
+
6 => { daily: 124, weekly: 27 },
|
|
89
|
+
9 => { daily: 185, weekly: 39 },
|
|
90
|
+
12 => { daily: 248, weekly: 53 },
|
|
91
|
+
15 => { daily: 312, weekly: 65 },
|
|
92
|
+
18 => { daily: 370, weekly: 79 }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
payment_schedule[@term_months][@frequency]
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def calculate_average_days_per_period
|
|
99
|
+
return 1 if @frequency == :daily && !@bank_days_only
|
|
100
|
+
return 7 if @frequency == :weekly && !@bank_days_only
|
|
101
|
+
|
|
102
|
+
if @bank_days_only
|
|
103
|
+
total_payments = calculate_total_payments
|
|
104
|
+
current_date = first_payment_date
|
|
105
|
+
total_days = 0
|
|
106
|
+
sample_size = [30, total_payments].min
|
|
107
|
+
|
|
108
|
+
(1..sample_size).each do |i|
|
|
109
|
+
next_date = next_payment_date(current_date)
|
|
110
|
+
total_days += calculate_days_between(current_date, next_date)
|
|
111
|
+
current_date = next_date
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
return total_days.to_f / sample_size
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
@frequency == :daily ? 1 : 7
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def estimate_total_loan_days
|
|
121
|
+
total_payments = calculate_total_payments
|
|
122
|
+
avg_days_per_period = calculate_average_days_per_period
|
|
123
|
+
total_payments * avg_days_per_period
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def calculate_precomputed_total_interest
|
|
127
|
+
principal_for_interest = initial_principal_with_origination
|
|
128
|
+
total_days = estimate_total_loan_days
|
|
129
|
+
principal_for_interest * @annual_rate * (total_days / 365.0)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def precomputed_interest_per_payment
|
|
133
|
+
total_payments = calculate_total_payments
|
|
134
|
+
calculate_precomputed_total_interest / total_payments
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def initial_principal_with_origination
|
|
138
|
+
@principal + @origination_fee
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def grace_period_interest
|
|
142
|
+
return 0.0 if @grace_period_days == 0
|
|
143
|
+
grace_rate = (@annual_rate / 365.0) * @grace_period_days
|
|
144
|
+
initial_principal_with_origination * grace_rate
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def effective_principal
|
|
148
|
+
base_principal = initial_principal_with_origination + grace_period_interest
|
|
149
|
+
|
|
150
|
+
case @additional_fee_treatment
|
|
151
|
+
when :add_to_principal
|
|
152
|
+
base_principal + @additional_fee
|
|
153
|
+
else
|
|
154
|
+
base_principal
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def first_payment_date
|
|
159
|
+
if @grace_period_days > 0
|
|
160
|
+
grace_end_date = @start_date + @grace_period_days
|
|
161
|
+
next_bank_day(grace_end_date)
|
|
162
|
+
else
|
|
163
|
+
@start_date
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def calculate_payment
|
|
168
|
+
total_payments = calculate_total_payments
|
|
169
|
+
principal_payments = total_payments - @interest_only_periods
|
|
170
|
+
|
|
171
|
+
if @interest_method == :precomputed
|
|
172
|
+
principal_payment_portion = effective_principal / principal_payments
|
|
173
|
+
interest_portion = precomputed_interest_per_payment
|
|
174
|
+
|
|
175
|
+
if @additional_fee_treatment == :distributed
|
|
176
|
+
principal_payment_portion + interest_portion + (@additional_fee / total_payments)
|
|
177
|
+
else
|
|
178
|
+
principal_payment_portion + interest_portion
|
|
179
|
+
end
|
|
180
|
+
else
|
|
181
|
+
days_per_period = calculate_average_days_per_period
|
|
182
|
+
period_rate = (@annual_rate / 365.0) * days_per_period
|
|
183
|
+
|
|
184
|
+
base_payment = if period_rate == 0
|
|
185
|
+
effective_principal / principal_payments
|
|
186
|
+
else
|
|
187
|
+
effective_principal * (period_rate * (1 + period_rate)**principal_payments) /
|
|
188
|
+
((1 + period_rate)**principal_payments - 1)
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
if @additional_fee_treatment == :distributed
|
|
192
|
+
base_payment + (@additional_fee / total_payments)
|
|
193
|
+
else
|
|
194
|
+
base_payment
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def federal_holiday?(date)
|
|
200
|
+
holidays = Holidays.on(date, :federalreserve, :observed)
|
|
201
|
+
!holidays.empty?
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def bank_day?(date)
|
|
205
|
+
return true unless @bank_days_only
|
|
206
|
+
return false if date.saturday? || date.sunday?
|
|
207
|
+
return false if federal_holiday?(date)
|
|
208
|
+
true
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def next_bank_day(date)
|
|
212
|
+
return date unless @bank_days_only
|
|
213
|
+
|
|
214
|
+
current = date
|
|
215
|
+
until bank_day?(current)
|
|
216
|
+
current += 1
|
|
217
|
+
end
|
|
218
|
+
current
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def next_payment_date(current_date)
|
|
222
|
+
case @frequency
|
|
223
|
+
when :daily
|
|
224
|
+
next_date = current_date + 1
|
|
225
|
+
when :weekly
|
|
226
|
+
next_date = current_date + 7
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
next_bank_day(next_date)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def calculate_days_between(start_date, end_date)
|
|
233
|
+
(end_date - start_date).to_i
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def generate_schedule_data
|
|
237
|
+
payment_amount = calculate_payment
|
|
238
|
+
balance = effective_principal
|
|
239
|
+
accrued_interest = 0.0
|
|
240
|
+
payment_date = first_payment_date
|
|
241
|
+
previous_payment_date = first_payment_date
|
|
242
|
+
payment_number = 0
|
|
243
|
+
total_payments = calculate_total_payments
|
|
244
|
+
schedule_data = []
|
|
245
|
+
|
|
246
|
+
if @grace_period_days > 0
|
|
247
|
+
grace_interest = grace_period_interest
|
|
248
|
+
schedule_data << {
|
|
249
|
+
payment_number: 'Grace',
|
|
250
|
+
date: first_payment_date,
|
|
251
|
+
principal_payment: 0.0,
|
|
252
|
+
interest_payment: 0.0,
|
|
253
|
+
additional_fee_payment: 0.0,
|
|
254
|
+
total_payment: 0.0,
|
|
255
|
+
principal_balance: balance,
|
|
256
|
+
accrued_interest: 0.0,
|
|
257
|
+
total_balance: balance,
|
|
258
|
+
payment_type: 'Grace Period',
|
|
259
|
+
days_in_period: @grace_period_days,
|
|
260
|
+
grace_interest_capitalized: grace_interest
|
|
261
|
+
}
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
if @additional_fee_treatment == :separate_payment && @additional_fee > 0
|
|
265
|
+
payment_date = next_payment_date(payment_date)
|
|
266
|
+
schedule_data << {
|
|
267
|
+
payment_number: 0,
|
|
268
|
+
date: payment_date,
|
|
269
|
+
principal_payment: 0.0,
|
|
270
|
+
interest_payment: 0.0,
|
|
271
|
+
additional_fee_payment: @additional_fee,
|
|
272
|
+
total_payment: @additional_fee,
|
|
273
|
+
principal_balance: balance,
|
|
274
|
+
accrued_interest: 0.0,
|
|
275
|
+
total_balance: balance,
|
|
276
|
+
payment_type: 'Additional Fee Payment',
|
|
277
|
+
days_in_period: 0
|
|
278
|
+
}
|
|
279
|
+
previous_payment_date = payment_date
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
additional_fee_per_payment = @additional_fee_treatment == :distributed ? (@additional_fee / total_payments) : 0.0
|
|
283
|
+
precomputed_interest = @interest_method == :precomputed ? precomputed_interest_per_payment : 0.0
|
|
284
|
+
|
|
285
|
+
while payment_number < total_payments && balance > 0.01
|
|
286
|
+
payment_number += 1
|
|
287
|
+
payment_date = next_payment_date(previous_payment_date)
|
|
288
|
+
days_in_period = calculate_days_between(previous_payment_date, payment_date)
|
|
289
|
+
|
|
290
|
+
if @interest_method == :precomputed
|
|
291
|
+
interest_payment = precomputed_interest
|
|
292
|
+
else
|
|
293
|
+
period_rate = (@annual_rate / 365.0) * days_in_period
|
|
294
|
+
interest_payment = balance * period_rate
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
accrued_interest += interest_payment
|
|
298
|
+
is_interest_only = payment_number <= @interest_only_periods
|
|
299
|
+
|
|
300
|
+
if is_interest_only
|
|
301
|
+
principal_payment = 0.0
|
|
302
|
+
payment_type = 'Interest Only'
|
|
303
|
+
else
|
|
304
|
+
principal_payment = [payment_amount - interest_payment - additional_fee_per_payment, balance].min
|
|
305
|
+
principal_payment = balance if payment_number == total_payments
|
|
306
|
+
payment_type = 'Regular Payment'
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
total_payment = principal_payment + interest_payment + additional_fee_per_payment
|
|
310
|
+
balance -= principal_payment
|
|
311
|
+
balance = 0 if balance < 0.01
|
|
312
|
+
total_balance = balance + accrued_interest
|
|
313
|
+
|
|
314
|
+
schedule_data << {
|
|
315
|
+
payment_number: payment_number,
|
|
316
|
+
date: payment_date,
|
|
317
|
+
principal_payment: principal_payment,
|
|
318
|
+
interest_payment: interest_payment,
|
|
319
|
+
additional_fee_payment: additional_fee_per_payment,
|
|
320
|
+
total_payment: total_payment,
|
|
321
|
+
principal_balance: balance,
|
|
322
|
+
accrued_interest: accrued_interest,
|
|
323
|
+
total_balance: total_balance,
|
|
324
|
+
payment_type: payment_type,
|
|
325
|
+
days_in_period: days_in_period
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
accrued_interest = 0.0
|
|
329
|
+
previous_payment_date = payment_date
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
schedule_data
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
def generate_console_output
|
|
336
|
+
puts format_header
|
|
337
|
+
puts "-" * 195
|
|
338
|
+
|
|
339
|
+
generate_schedule_data.each do |row|
|
|
340
|
+
if row[:payment_type] == 'Grace Period'
|
|
341
|
+
puts format_grace_row(
|
|
342
|
+
row[:payment_number],
|
|
343
|
+
row[:date],
|
|
344
|
+
row[:grace_interest_capitalized],
|
|
345
|
+
row[:principal_balance],
|
|
346
|
+
row[:days_in_period]
|
|
347
|
+
)
|
|
348
|
+
else
|
|
349
|
+
puts format_row(
|
|
350
|
+
row[:payment_number],
|
|
351
|
+
row[:date],
|
|
352
|
+
row[:principal_payment],
|
|
353
|
+
row[:interest_payment],
|
|
354
|
+
row[:additional_fee_payment],
|
|
355
|
+
row[:total_payment],
|
|
356
|
+
row[:principal_balance],
|
|
357
|
+
row[:accrued_interest],
|
|
358
|
+
row[:total_balance],
|
|
359
|
+
row[:payment_type],
|
|
360
|
+
row[:days_in_period]
|
|
361
|
+
)
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
print_summary
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def generate_csv_output(csv_path)
|
|
369
|
+
CSV.open(csv_path, 'w') do |csv|
|
|
370
|
+
csv << [
|
|
371
|
+
'Payment Number',
|
|
372
|
+
'Date',
|
|
373
|
+
'Days in Period',
|
|
374
|
+
'Principal Payment',
|
|
375
|
+
'Interest Payment',
|
|
376
|
+
'Additional Fee Payment',
|
|
377
|
+
'Total Payment',
|
|
378
|
+
'Principal Balance Remaining',
|
|
379
|
+
'Accrued Interest',
|
|
380
|
+
'Total Balance',
|
|
381
|
+
'Payment Type',
|
|
382
|
+
'Grace Interest Capitalized'
|
|
383
|
+
]
|
|
384
|
+
|
|
385
|
+
generate_schedule_data.each do |row|
|
|
386
|
+
csv << [
|
|
387
|
+
row[:payment_number],
|
|
388
|
+
row[:date].strftime("%Y-%m-%d"),
|
|
389
|
+
row[:days_in_period],
|
|
390
|
+
format("%.2f", row[:principal_payment] || 0),
|
|
391
|
+
format("%.2f", row[:interest_payment] || 0),
|
|
392
|
+
format("%.2f", row[:additional_fee_payment] || 0),
|
|
393
|
+
format("%.2f", row[:total_payment] || 0),
|
|
394
|
+
format("%.2f", row[:principal_balance]),
|
|
395
|
+
format("%.2f", row[:accrued_interest] || 0),
|
|
396
|
+
format("%.2f", row[:total_balance]),
|
|
397
|
+
row[:payment_type],
|
|
398
|
+
row[:grace_interest_capitalized] ? format("%.2f", row[:grace_interest_capitalized]) : ''
|
|
399
|
+
]
|
|
400
|
+
end
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
puts "CSV file generated: #{csv_path}"
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
def print_summary
|
|
407
|
+
puts "\n" + "=" * 195
|
|
408
|
+
puts "LOAN SUMMARY"
|
|
409
|
+
puts "=" * 195
|
|
410
|
+
puts "Loan Start Date: #{@start_date.strftime('%Y-%m-%d')}"
|
|
411
|
+
puts "First Payment Date: #{first_payment_date.strftime('%Y-%m-%d')}"
|
|
412
|
+
puts "Term: #{@term_months} months"
|
|
413
|
+
puts "Payment Frequency: #{@frequency.to_s.capitalize}"
|
|
414
|
+
puts "Grace Period: #{@grace_period_days} days"
|
|
415
|
+
|
|
416
|
+
if @grace_period_days > 0
|
|
417
|
+
puts "Grace Period Interest (Capitalized): $#{format('%.2f', grace_period_interest)}"
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
puts "\nOriginal Principal: $#{format('%.2f', @principal)}"
|
|
421
|
+
puts "Origination Fee: $#{format('%.2f', @origination_fee)} (added to principal)"
|
|
422
|
+
puts "Additional Fee: $#{format('%.2f', @additional_fee)}"
|
|
423
|
+
puts "#{@additional_fee_label} Treatment: #{@additional_fee_treatment.to_s.split('_').map(&:capitalize).join(' ')}"
|
|
424
|
+
puts "Bank Days Only: #{@bank_days_only}"
|
|
425
|
+
puts "Interest-Only Periods: #{@interest_only_periods}"
|
|
426
|
+
puts "Interest Method: #{@interest_method.to_s.capitalize}"
|
|
427
|
+
|
|
428
|
+
puts "\nPrincipal after Origination Fee: $#{format('%.2f', initial_principal_with_origination)}"
|
|
429
|
+
|
|
430
|
+
if @grace_period_days > 0
|
|
431
|
+
puts "Principal after Grace Period (with capitalized interest): $#{format('%.2f', initial_principal_with_origination + grace_period_interest)}"
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
case @additional_fee_treatment
|
|
435
|
+
when :add_to_principal
|
|
436
|
+
puts "Total Principal (with all fees): $#{format('%.2f', effective_principal)}"
|
|
437
|
+
when :distributed
|
|
438
|
+
puts "#{@additional_fee_label} per payment: $#{format('%.2f', @additional_fee / calculate_total_payments)}"
|
|
439
|
+
when :separate_payment
|
|
440
|
+
puts "#{@additional_fee_label} collected as separate payment"
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
if @interest_method == :precomputed
|
|
444
|
+
puts "\nPrecomputed Interest Calculation:"
|
|
445
|
+
puts " Estimated Total Loan Days: #{format('%.0f', estimate_total_loan_days)}"
|
|
446
|
+
puts " Total Precomputed Interest: $#{format('%.2f', calculate_precomputed_total_interest)}"
|
|
447
|
+
puts " Interest per Payment: $#{format('%.2f', precomputed_interest_per_payment)}"
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
schedule_data = generate_schedule_data
|
|
451
|
+
total_interest = schedule_data.sum { |row| row[:interest_payment] || 0 }
|
|
452
|
+
total_additional_fees = schedule_data.sum { |row| row[:additional_fee_payment] || 0 }
|
|
453
|
+
total_paid = effective_principal + total_interest + (@additional_fee_treatment == :separate_payment ? @additional_fee : 0)
|
|
454
|
+
|
|
455
|
+
puts "\nTotal Interest Paid (during payments): $#{format('%.2f', total_interest)}"
|
|
456
|
+
if @grace_period_days > 0
|
|
457
|
+
puts "Total Interest Including Grace Period: $#{format('%.2f', total_interest + grace_period_interest)}"
|
|
458
|
+
end
|
|
459
|
+
puts "Total #{@additional_fee_label} Paid: $#{format('%.2f', total_additional_fees)}"
|
|
460
|
+
puts "Total Amount Paid: $#{format('%.2f', total_paid)}"
|
|
461
|
+
puts "=" * 195
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
def format_header
|
|
465
|
+
format(
|
|
466
|
+
"%-8s %-12s %-8s %15s %15s %18s %18s %20s %18s %18s %20s",
|
|
467
|
+
"Payment",
|
|
468
|
+
"Date",
|
|
469
|
+
"Days",
|
|
470
|
+
"Principal Pmt",
|
|
471
|
+
"Interest Pmt",
|
|
472
|
+
@additional_fee_label,
|
|
473
|
+
"Total Payment",
|
|
474
|
+
"Principal Balance",
|
|
475
|
+
"Accrued Interest",
|
|
476
|
+
"Total Balance",
|
|
477
|
+
"Payment Type"
|
|
478
|
+
)
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
def format_grace_row(payment_num, date, grace_interest, principal_balance, days)
|
|
482
|
+
format(
|
|
483
|
+
"%-8s %-12s %-8d %15s %15s %18s %18s %20.2f %18s %18.2f %20s",
|
|
484
|
+
"Grace",
|
|
485
|
+
date.strftime("%Y-%m-%d"),
|
|
486
|
+
days,
|
|
487
|
+
"---",
|
|
488
|
+
"+#{format('%.2f', grace_interest)}",
|
|
489
|
+
"---",
|
|
490
|
+
"0.00",
|
|
491
|
+
principal_balance,
|
|
492
|
+
"---",
|
|
493
|
+
principal_balance,
|
|
494
|
+
"Grace Period"
|
|
495
|
+
)
|
|
496
|
+
end
|
|
497
|
+
|
|
498
|
+
def format_row(payment_num, date, principal_pmt, interest_pmt, additional_fee_pmt, total_pmt, principal_balance, accrued_interest, total_balance, payment_type, days)
|
|
499
|
+
format(
|
|
500
|
+
"%-8s %-12s %-8d %15.2f %15.2f %18.2f %18.2f %20.2f %18.2f %18.2f %20s",
|
|
501
|
+
payment_num == 0 ? "Fee" : payment_num.to_s,
|
|
502
|
+
date.strftime("%Y-%m-%d"),
|
|
503
|
+
days,
|
|
504
|
+
principal_pmt,
|
|
505
|
+
interest_pmt,
|
|
506
|
+
additional_fee_pmt,
|
|
507
|
+
total_pmt,
|
|
508
|
+
principal_balance,
|
|
509
|
+
accrued_interest,
|
|
510
|
+
total_balance,
|
|
511
|
+
payment_type
|
|
512
|
+
)
|
|
513
|
+
end
|
|
514
|
+
end
|
|
515
|
+
end
|
|
516
|
+
|
data/lib/amortizy.rb
ADDED
data/sig/amortizy.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: amortizy
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Rich Zapata
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: holidays
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '8.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '8.0'
|
|
26
|
+
description: A comprehensive Ruby gem for generating loan amortization schedules with
|
|
27
|
+
advanced features including grace periods, interest-only payments, federal bank
|
|
28
|
+
holidays, and multiple interest calculation methods
|
|
29
|
+
email:
|
|
30
|
+
- rzapata@gmail.com
|
|
31
|
+
executables: []
|
|
32
|
+
extensions: []
|
|
33
|
+
extra_rdoc_files: []
|
|
34
|
+
files:
|
|
35
|
+
- ".DS_Store"
|
|
36
|
+
- CHANGELOG.md
|
|
37
|
+
- CODE_OF_CONDUCT.md
|
|
38
|
+
- LICENSE.txt
|
|
39
|
+
- README.md
|
|
40
|
+
- Rakefile
|
|
41
|
+
- lib/.DS_Store
|
|
42
|
+
- lib/amortizy.rb
|
|
43
|
+
- lib/amortizy/engine.rb
|
|
44
|
+
- lib/amortizy/version.rb
|
|
45
|
+
- sig/amortizy.rbs
|
|
46
|
+
homepage: https://github.com/zapatify/amortizy
|
|
47
|
+
licenses:
|
|
48
|
+
- MIT
|
|
49
|
+
metadata:
|
|
50
|
+
allowed_push_host: https://rubygems.org
|
|
51
|
+
homepage_uri: https://github.com/zapatify/amortizy
|
|
52
|
+
source_code_uri: https://github.com/zapatify/amortizy
|
|
53
|
+
changelog_uri: https://github.com/zapatify/amortizy/blob/main/CHANGELOG.md
|
|
54
|
+
rdoc_options: []
|
|
55
|
+
require_paths:
|
|
56
|
+
- lib
|
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 2.7.0
|
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
- - ">="
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: '0'
|
|
67
|
+
requirements: []
|
|
68
|
+
rubygems_version: 3.7.2
|
|
69
|
+
specification_version: 4
|
|
70
|
+
summary: A gem to generate amortization schedules
|
|
71
|
+
test_files: []
|