finrb 1.0.1 → 1.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 +4 -4
- data/CHANGELOG.md +50 -0
- data/CONTRIBUTING.md +89 -0
- data/README.md +58 -2
- data/SECURITY.md +41 -0
- data/lib/finrb/accounting.rb +93 -74
- data/lib/finrb/amortization.rb +145 -44
- data/lib/finrb/cashflows.rb +32 -9
- data/lib/finrb/config.rb +5 -5
- data/lib/finrb/rates.rb +3 -21
- data/lib/finrb/ratios.rb +41 -40
- data/lib/finrb/returns.rb +138 -39
- data/lib/finrb/transaction.rb +0 -11
- data/lib/finrb/tvm.rb +154 -52
- data/lib/finrb/validation.rb +42 -0
- data/lib/finrb/version.rb +1 -1
- data/lib/finrb/yields.rb +42 -31
- data/sig/finrb.rbs +39 -4
- metadata +17 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8474be54c480d3150125ba2a00e7b935fbfb10281ce857304889ebca48839bac
|
|
4
|
+
data.tar.gz: 571f0f38b0a662c749df453e314cb3f916efe27e2f466896e600719a9048aef1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 31edbc91ddfe6bfd433daaae2598be24b7fe22d604e40745b238a66c01ad8cd597d9a441b4643f9cd0a1adc5230cd2e3780fc1ba791e5799e921ffb0a6bc6e59
|
|
7
|
+
data.tar.gz: 59b760dfb7f1c27686d34362ad9da310b2bb20ccfc440b42f39e6c551fd14c1ac134dd3ed8795e0cf9d4274c264e085a95b59339bd71b5c11e6763f99f9b82a1
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,55 @@
|
|
|
1
1
|
# finrb changelog
|
|
2
2
|
|
|
3
|
+
## 1.2.0
|
|
4
|
+
|
|
5
|
+
### Validation and financial correctness
|
|
6
|
+
|
|
7
|
+
- Apply the shared finite-decimal validation contract across yields, returns,
|
|
8
|
+
accounting, ratios, cashflows, rates, amortization, and TVM helpers.
|
|
9
|
+
- Replace abbreviated validation errors with descriptive financial terms such
|
|
10
|
+
as period count, future value, face value, and compounding periods.
|
|
11
|
+
- Add explicit denominator, rate, share-count, inventory, useful-life, and
|
|
12
|
+
residual-value domain checks while preserving valid decimal calculations and
|
|
13
|
+
return types.
|
|
14
|
+
- Fix LIFO ending-inventory retention when a sale is satisfied by the second
|
|
15
|
+
purchase layer, preserving inventory cost conservation.
|
|
16
|
+
- Reject invalid weighted-return vectors and replace the previous unsolicited
|
|
17
|
+
weighted-portfolio warning with an explicit validation error.
|
|
18
|
+
|
|
19
|
+
### Development and project maintenance
|
|
20
|
+
|
|
21
|
+
- Add deterministic `benchmark:run` scenarios using `benchmark-ips` for IRR,
|
|
22
|
+
XIRR, and amortization scaling, with known-root, equation-residual, solver
|
|
23
|
+
evaluation, and schedule-reconciliation preflight diagnostics.
|
|
24
|
+
- Add contributor and security guidance, and link the project entry documents
|
|
25
|
+
from the README.
|
|
26
|
+
- Include contributor and security documentation in packaged gem metadata.
|
|
27
|
+
|
|
28
|
+
## 1.1.0
|
|
29
|
+
|
|
30
|
+
### Investment returns and risk
|
|
31
|
+
|
|
32
|
+
- Add compound annual growth rate (CAGR) with explicit value and period-domain validation.
|
|
33
|
+
- Add modified internal rate of return (MIRR) with separate financing and reinvestment rates.
|
|
34
|
+
- Add sample and population volatility, downside deviation, Sortino ratio, and maximum drawdown.
|
|
35
|
+
- Add compound-return and square-root-of-time volatility annualization helpers.
|
|
36
|
+
- Define the statistical conventions explicitly: volatility is sample-based by default, downside deviation includes all observations in its denominator, and maximum drawdown is returned as a non-negative loss fraction.
|
|
37
|
+
|
|
38
|
+
### Loan schedules
|
|
39
|
+
|
|
40
|
+
- Expose each amortization period as an immutable `Finrb::Amortization::Entry` containing its period, opening and closing balances, payment, interest, principal, additional principal, balloon settlement, and interest-only state.
|
|
41
|
+
- Preserve the existing cashflow convention: payments are negative, while balances, interest, principal repaid, and additional principal are non-negative.
|
|
42
|
+
- Add contractual balloon targets. Regular installments amortize toward the target and the final payment settles the residual, including cent-rounding reconciliation.
|
|
43
|
+
- Add leading interest-only periods, including zero-rate periods and combinations with balloon loans. Remaining principal amortizes over the rest of the term.
|
|
44
|
+
- Add upfront and financed origination fees with separate `principal`, `net_proceeds`, and `amount_financed` values. Financed fees enter the opening balance; upfront fees reduce borrower proceeds.
|
|
45
|
+
- Correct schedule period numbering across rate segments and reused payment templates.
|
|
46
|
+
|
|
47
|
+
### API and documentation
|
|
48
|
+
|
|
49
|
+
- Add RBS declarations and API examples for all new return metrics and amortization features.
|
|
50
|
+
- Refine README compatibility badges to identify tested MRI versions, x86-64/ARM64 architectures, and experimental JRuby/TruffleRuby coverage.
|
|
51
|
+
- Remove redundant YARD `@api` annotations and make the internal amortization calculation methods genuinely private.
|
|
52
|
+
|
|
3
53
|
## 1.0.1
|
|
4
54
|
|
|
5
55
|
### Runtime compatibility
|
data/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Contributing to finrb
|
|
2
|
+
|
|
3
|
+
finrb welcomes focused bug fixes, documentation improvements, tests, and
|
|
4
|
+
financial calculations that fit the project's scope. Before starting a large
|
|
5
|
+
feature or public API change, open an issue so its financial conventions and
|
|
6
|
+
design can be agreed upon first.
|
|
7
|
+
|
|
8
|
+
## Development setup
|
|
9
|
+
|
|
10
|
+
finrb requires Ruby 3.3 or newer. MRI 3.3, 3.4, and 4.0 are supported; JRuby
|
|
11
|
+
and TruffleRuby are tested as informational compatibility targets.
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
bundle install
|
|
15
|
+
bundle exec rake quality
|
|
16
|
+
bundle exec rubocop
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
`rake quality` runs the RSpec suite with coverage, verifies maintained API
|
|
20
|
+
examples, and validates the packaged RBS declarations. Additional checks are
|
|
21
|
+
available for changes that affect their domains:
|
|
22
|
+
|
|
23
|
+
```shell
|
|
24
|
+
bundle exec rake security:audit
|
|
25
|
+
bundle exec rake package:verify
|
|
26
|
+
bundle exec rake benchmark:run
|
|
27
|
+
bundle exec rake solver:verify
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
The solver verification task requires the optional Python environment
|
|
31
|
+
documented in [spec/fixtures/README.md](spec/fixtures/README.md). The Python
|
|
32
|
+
packages are development references and are not gem dependencies.
|
|
33
|
+
|
|
34
|
+
## Making changes
|
|
35
|
+
|
|
36
|
+
- Preserve unrelated code and behavior. Keep pull requests small enough to
|
|
37
|
+
review their financial and numerical consequences directly.
|
|
38
|
+
- Follow the local Ruby style and run RuboCop. Comments should explain a
|
|
39
|
+
non-obvious reason or convention, not restate the code.
|
|
40
|
+
- Route public numeric inputs through `Finrb::Validation`. Use `ArgumentError`
|
|
41
|
+
for malformed inputs and namespaced finrb errors for financial or numerical
|
|
42
|
+
domains.
|
|
43
|
+
- Preserve `Flt::DecNum` calculations and return types unless the change has a
|
|
44
|
+
documented compatibility reason.
|
|
45
|
+
- Update RBS declarations and user documentation when a public contract
|
|
46
|
+
changes.
|
|
47
|
+
- Do not add an unreleased changelog section for ordinary development work.
|
|
48
|
+
The maintainer prepares the changelog as part of an actual release.
|
|
49
|
+
|
|
50
|
+
## Numerical and financial changes
|
|
51
|
+
|
|
52
|
+
A formula compiling or producing a plausible number is not sufficient
|
|
53
|
+
verification. Describe the convention being implemented and test the relevant
|
|
54
|
+
invariants, boundaries, and failure modes.
|
|
55
|
+
|
|
56
|
+
For a solver or precision-sensitive change, include as appropriate:
|
|
57
|
+
|
|
58
|
+
- known-root or algebraically constructed cases;
|
|
59
|
+
- normalized equation residuals;
|
|
60
|
+
- zero, negative-rate, long-horizon, and scale-sensitive cases;
|
|
61
|
+
- attributable fixtures from a reputable independent implementation under
|
|
62
|
+
matching financial conventions;
|
|
63
|
+
- deterministic seeds for generated cases; and
|
|
64
|
+
- benchmarks that compare the same scenario across revisions, runtimes, or
|
|
65
|
+
architectures rather than ranking unrelated calculations.
|
|
66
|
+
|
|
67
|
+
Reference fixtures must record their source, version, conventions, generation
|
|
68
|
+
method, and tolerance. Do not replace committed fixtures merely to make a
|
|
69
|
+
changed implementation pass without explaining why the reference changed.
|
|
70
|
+
|
|
71
|
+
## Pull requests
|
|
72
|
+
|
|
73
|
+
Use the pull request template and include:
|
|
74
|
+
|
|
75
|
+
- the financial or technical problem;
|
|
76
|
+
- the intended contract and compatibility impact;
|
|
77
|
+
- the evidence used to verify correctness; and
|
|
78
|
+
- any checks skipped because an optional runtime or external reference was
|
|
79
|
+
unavailable.
|
|
80
|
+
|
|
81
|
+
Commit subjects in this repository commonly use the form
|
|
82
|
+
`domain [Category]: Imperative summary`, for example:
|
|
83
|
+
|
|
84
|
+
```text
|
|
85
|
+
cashflows [Fix]: Preserve negative-root selection
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
By contributing, you agree that your work is provided under the repository's
|
|
89
|
+
LGPL-3.0-or-later license.
|
data/README.md
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
[](https://github.com/ncs1/finrb/actions/workflows/ci.yml)
|
|
4
4
|
[](https://github.com/ncs1/finrb/actions/workflows/codeql.yml)
|
|
5
5
|
[](https://github.com/ncs1/finrb/actions/workflows/rubocop.yml)
|
|
6
|
+
[](https://github.com/ncs1/finrb/actions/workflows/ci.yml)
|
|
7
|
+
[](https://github.com/ncs1/finrb/actions/workflows/ci.yml)
|
|
8
|
+
[](https://github.com/ncs1/finrb/actions/workflows/ci.yml)
|
|
6
9
|
|
|
7
10
|
Precision-first financial mathematics for Ruby.
|
|
8
11
|
|
|
@@ -61,8 +64,8 @@ API explicitly returns another financial object, such as `Finrb::Rate`.
|
|
|
61
64
|
| `Finrb::Ratios` | Liquidity, leverage, profitability, and per-share ratios |
|
|
62
65
|
| `Finrb::Accounting` | Inventory costing and depreciation |
|
|
63
66
|
|
|
64
|
-
The
|
|
65
|
-
its
|
|
67
|
+
The [API and examples guide](docs/api.md) documents each financial domain and
|
|
68
|
+
its public calculations. Packaged RBS declarations are available under `sig/`.
|
|
66
69
|
|
|
67
70
|
## Cashflows
|
|
68
71
|
|
|
@@ -117,6 +120,15 @@ loan = Finrb::Amortization.new(250_000, rate)
|
|
|
117
120
|
loan.payment # => Flt::DecNum('-1229.85')
|
|
118
121
|
loan.interest.sum # => Flt::DecNum('192745.98')
|
|
119
122
|
loan.balance # => Flt::DecNum('0.00')
|
|
123
|
+
|
|
124
|
+
first = loan.schedule.first
|
|
125
|
+
first.opening_balance
|
|
126
|
+
first.interest
|
|
127
|
+
first.principal
|
|
128
|
+
first.payment
|
|
129
|
+
first.balloon_payment
|
|
130
|
+
first.interest_only?
|
|
131
|
+
first.closing_balance
|
|
120
132
|
```
|
|
121
133
|
|
|
122
134
|
Pass several duration-bearing rates for an adjustable-rate schedule. A block
|
|
@@ -130,6 +142,48 @@ end
|
|
|
130
142
|
|
|
131
143
|
Payments and interest follow the sign convention used throughout finrb:
|
|
132
144
|
money received is positive and money paid is negative.
|
|
145
|
+
Schedule balances, interest, principal repaid, and additional principal are
|
|
146
|
+
non-negative; the schedule's payment field is negative.
|
|
147
|
+
|
|
148
|
+
Set a residual principal target to create a balloon loan. Regular installments
|
|
149
|
+
amortize only the non-balloon portion, and the final payment settles the stated
|
|
150
|
+
balloon:
|
|
151
|
+
|
|
152
|
+
```ruby
|
|
153
|
+
balloon_loan = Finrb::Amortization.new(250_000, rate, balloon: 100_000)
|
|
154
|
+
balloon_loan.schedule.last.balloon_payment # => Flt::DecNum('100000')
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
`balloon` is the contractual residual target. Because regular postings are
|
|
158
|
+
rounded to cents, the actual `balloon_payment` in the final schedule row can
|
|
159
|
+
differ from that target by a few cents.
|
|
160
|
+
|
|
161
|
+
Leading interest-only periods defer scheduled principal repayment and amortize
|
|
162
|
+
the balance over the remaining term:
|
|
163
|
+
|
|
164
|
+
```ruby
|
|
165
|
+
interest_only = Finrb::Amortization.new(250_000, rate, interest_only_periods: 24)
|
|
166
|
+
interest_only.schedule.first.interest_only? # => true
|
|
167
|
+
interest_only.schedule.first.principal # => Flt::DecNum('0')
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Origination fees can either reduce the borrower's net proceeds or be added to
|
|
171
|
+
the financed balance:
|
|
172
|
+
|
|
173
|
+
```ruby
|
|
174
|
+
cash_fee = Finrb::Amortization.new(250_000, rate, origination_fee: 2_500)
|
|
175
|
+
cash_fee.net_proceeds # => Flt::DecNum('247500')
|
|
176
|
+
cash_fee.amount_financed # => Flt::DecNum('250000')
|
|
177
|
+
|
|
178
|
+
financed_fee = Finrb::Amortization.new(
|
|
179
|
+
250_000,
|
|
180
|
+
rate,
|
|
181
|
+
origination_fee: 2_500,
|
|
182
|
+
finance_origination_fee: true
|
|
183
|
+
)
|
|
184
|
+
financed_fee.net_proceeds # => Flt::DecNum('250000')
|
|
185
|
+
financed_fee.amount_financed # => Flt::DecNum('252500')
|
|
186
|
+
```
|
|
133
187
|
|
|
134
188
|
## Configuration
|
|
135
189
|
|
|
@@ -284,6 +338,8 @@ Docker, batching, and tolerance details.
|
|
|
284
338
|
- [RubyGems](https://rubygems.org/gems/finrb)
|
|
285
339
|
- [Source](https://github.com/ncs1/finrb)
|
|
286
340
|
- [Issue tracker](https://github.com/ncs1/finrb/issues)
|
|
341
|
+
- [Contributing](CONTRIBUTING.md)
|
|
342
|
+
- [Security policy](SECURITY.md)
|
|
287
343
|
|
|
288
344
|
## Acknowledgements
|
|
289
345
|
|
data/SECURITY.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Security policy
|
|
2
|
+
|
|
3
|
+
## Supported versions
|
|
4
|
+
|
|
5
|
+
Security fixes are made against the current released finrb line. Older
|
|
6
|
+
versions may receive a fix when practical, but they are not guaranteed to be
|
|
7
|
+
maintained. Users should reproduce an issue on the newest release before
|
|
8
|
+
reporting it when possible.
|
|
9
|
+
|
|
10
|
+
JRuby and TruffleRuby are informational compatibility targets. A
|
|
11
|
+
runtime-specific vulnerability may also need to be reported to the relevant
|
|
12
|
+
Ruby implementation or dependency maintainers.
|
|
13
|
+
|
|
14
|
+
## Reporting a vulnerability
|
|
15
|
+
|
|
16
|
+
Do not disclose a suspected vulnerability in a public issue, discussion, or
|
|
17
|
+
pull request. Use GitHub's private vulnerability reporting for finrb:
|
|
18
|
+
|
|
19
|
+
<https://github.com/ncs1/finrb/security/advisories/new>
|
|
20
|
+
|
|
21
|
+
If private reporting is unavailable, contact the maintainer through the email
|
|
22
|
+
listed in the gem metadata. Include only enough information in the initial
|
|
23
|
+
message to establish a private channel.
|
|
24
|
+
|
|
25
|
+
A useful report contains:
|
|
26
|
+
|
|
27
|
+
- affected finrb and Ruby versions;
|
|
28
|
+
- the affected API or packaged artifact;
|
|
29
|
+
- minimal reproduction steps;
|
|
30
|
+
- the expected and observed impact; and
|
|
31
|
+
- any known mitigations or disclosure constraints.
|
|
32
|
+
|
|
33
|
+
Please avoid including production credentials, private financial data, access
|
|
34
|
+
tokens, or other secrets. There is no guaranteed response or remediation
|
|
35
|
+
timeline; this project is maintained by one person. Reports will be evaluated
|
|
36
|
+
according to reproducibility, impact, and available maintainer capacity.
|
|
37
|
+
|
|
38
|
+
Numerical disagreement alone is normally a correctness bug rather than a
|
|
39
|
+
security vulnerability. Treat it as security-sensitive when it can cross a
|
|
40
|
+
trust boundary, bypass validation, enable denial of service, corrupt packaged
|
|
41
|
+
artifacts, or predictably cause unsafe downstream financial decisions.
|
data/lib/finrb/accounting.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative 'decimal'
|
|
4
4
|
require_relative 'errors'
|
|
5
|
+
require_relative 'validation'
|
|
5
6
|
|
|
6
7
|
module Finrb
|
|
7
8
|
# Inventory costing and depreciation calculations.
|
|
@@ -34,74 +35,32 @@ module Finrb
|
|
|
34
35
|
# @example
|
|
35
36
|
# Finrb::Accounting.cogs(uinv=2,pinv=2,units=[3,5],price=[3,5],sinv=7,method="WAC")
|
|
36
37
|
def self.cogs(uinv:, pinv:, units:, price:, sinv:, method: 'FIFO')
|
|
37
|
-
uinv =
|
|
38
|
-
pinv = Flt::DecNum(pinv.to_s)
|
|
39
|
-
units = wrap_array(units).map { |value| Flt::DecNum(value.to_s) }
|
|
40
|
-
price = wrap_array(price).map { |value| Flt::DecNum(value.to_s) }
|
|
41
|
-
sinv = Flt::DecNum(sinv.to_s)
|
|
42
|
-
method = method.to_s
|
|
38
|
+
uinv, pinv, units, price, sinv, method = inventory_inputs(uinv:, pinv:, units:, price:, sinv:, method:)
|
|
43
39
|
|
|
44
40
|
n = units.size
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
cost_of_goods = sinv * pinv
|
|
53
|
-
ending_inventory = (uinv - sinv) * pinv
|
|
54
|
-
(0...n).each do |i|
|
|
55
|
-
ending_inventory += (units[i] * price[i])
|
|
56
|
-
end
|
|
57
|
-
else
|
|
58
|
-
cost_of_goods = uinv * pinv
|
|
59
|
-
sinv -= uinv
|
|
60
|
-
(0...n).each do |i|
|
|
61
|
-
if sinv <= units[i]
|
|
62
|
-
cost_of_goods += (sinv * price[i])
|
|
63
|
-
ending_inventory = (units[i] - sinv) * price[i]
|
|
64
|
-
if i < n
|
|
65
|
-
temp = i + 1
|
|
66
|
-
(temp...n).each do |j|
|
|
67
|
-
ending_inventory += (units[j] * price[j])
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
sinv = 0
|
|
71
|
-
break
|
|
72
|
-
else
|
|
73
|
-
cost_of_goods += (units[i] * price[i])
|
|
74
|
-
sinv -= units[i]
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
raise(Error, "Inventory is not enough to sell\n") if sinv.positive?
|
|
78
|
-
end
|
|
79
|
-
when 'WAC'
|
|
80
|
-
ending_inventory = uinv * pinv
|
|
81
|
-
tu = uinv
|
|
41
|
+
cost_of_goods = Flt::DecNum(0)
|
|
42
|
+
ending_inventory = Flt::DecNum(0)
|
|
43
|
+
case method
|
|
44
|
+
when 'FIFO'
|
|
45
|
+
if sinv <= uinv
|
|
46
|
+
cost_of_goods = sinv * pinv
|
|
47
|
+
ending_inventory = (uinv - sinv) * pinv
|
|
82
48
|
(0...n).each do |i|
|
|
83
49
|
ending_inventory += (units[i] * price[i])
|
|
84
|
-
tu += units[i]
|
|
85
|
-
end
|
|
86
|
-
if tu >= sinv
|
|
87
|
-
cost_of_goods = ending_inventory / tu * sinv
|
|
88
|
-
ending_inventory = ending_inventory / tu * (tu - sinv)
|
|
89
|
-
else
|
|
90
|
-
raise(Error, "Inventory is not enough to sell\n")
|
|
91
50
|
end
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
51
|
+
else
|
|
52
|
+
cost_of_goods = uinv * pinv
|
|
53
|
+
sinv -= uinv
|
|
54
|
+
(0...n).each do |i|
|
|
95
55
|
if sinv <= units[i]
|
|
96
56
|
cost_of_goods += (sinv * price[i])
|
|
97
57
|
ending_inventory = (units[i] - sinv) * price[i]
|
|
98
|
-
if i
|
|
99
|
-
temp = i
|
|
100
|
-
temp
|
|
58
|
+
if i < n
|
|
59
|
+
temp = i + 1
|
|
60
|
+
(temp...n).each do |j|
|
|
101
61
|
ending_inventory += (units[j] * price[j])
|
|
102
62
|
end
|
|
103
63
|
end
|
|
104
|
-
ending_inventory += (uinv * pinv)
|
|
105
64
|
sinv = 0
|
|
106
65
|
break
|
|
107
66
|
else
|
|
@@ -109,18 +68,52 @@ module Finrb
|
|
|
109
68
|
sinv -= units[i]
|
|
110
69
|
end
|
|
111
70
|
end
|
|
112
|
-
if sinv.positive?
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
71
|
+
raise(DomainError, 'Available inventory is insufficient for the requested sale.') if sinv.positive?
|
|
72
|
+
end
|
|
73
|
+
when 'WAC'
|
|
74
|
+
ending_inventory = uinv * pinv
|
|
75
|
+
tu = uinv
|
|
76
|
+
(0...n).each do |i|
|
|
77
|
+
ending_inventory += (units[i] * price[i])
|
|
78
|
+
tu += units[i]
|
|
79
|
+
end
|
|
80
|
+
if tu.zero? && sinv.zero?
|
|
81
|
+
cost_of_goods = Flt::DecNum(0)
|
|
82
|
+
ending_inventory = Flt::DecNum(0)
|
|
83
|
+
elsif tu >= sinv
|
|
84
|
+
cost_of_goods = ending_inventory / tu * sinv
|
|
85
|
+
ending_inventory = ending_inventory / tu * (tu - sinv)
|
|
86
|
+
else
|
|
87
|
+
raise(DomainError, 'Available inventory is insufficient for the requested sale.')
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
when 'LIFO'
|
|
91
|
+
(n - 1).downto(0).each do |i|
|
|
92
|
+
if sinv <= units[i]
|
|
93
|
+
cost_of_goods += (sinv * price[i])
|
|
94
|
+
ending_inventory = (units[i] - sinv) * price[i]
|
|
95
|
+
if i.positive?
|
|
96
|
+
temp = i - 1
|
|
97
|
+
temp.downto(0).each do |j|
|
|
98
|
+
ending_inventory += (units[j] * price[j])
|
|
99
|
+
end
|
|
118
100
|
end
|
|
101
|
+
ending_inventory += (uinv * pinv)
|
|
102
|
+
sinv = 0
|
|
103
|
+
break
|
|
104
|
+
else
|
|
105
|
+
cost_of_goods += (units[i] * price[i])
|
|
106
|
+
sinv -= units[i]
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
if sinv.positive?
|
|
110
|
+
if sinv <= uinv
|
|
111
|
+
cost_of_goods += (sinv * pinv)
|
|
112
|
+
ending_inventory += ((uinv - sinv) * pinv)
|
|
113
|
+
else
|
|
114
|
+
raise(DomainError, 'Available inventory is insufficient for the requested sale.')
|
|
119
115
|
end
|
|
120
116
|
end
|
|
121
|
-
|
|
122
|
-
else
|
|
123
|
-
raise(Error, "length of units and price are not the same\n")
|
|
124
117
|
end
|
|
125
118
|
|
|
126
119
|
{
|
|
@@ -129,6 +122,20 @@ module Finrb
|
|
|
129
122
|
}
|
|
130
123
|
end
|
|
131
124
|
|
|
125
|
+
def self.inventory_inputs(uinv:, pinv:, units:, price:, sinv:, method:)
|
|
126
|
+
uinv = Validation.non_negative_decimal(uinv, name: 'beginning inventory units')
|
|
127
|
+
pinv = Validation.non_negative_decimal(pinv, name: 'beginning inventory unit cost')
|
|
128
|
+
units = inventory_values(units, name: 'purchase units')
|
|
129
|
+
price = inventory_values(price, name: 'purchase unit cost')
|
|
130
|
+
sinv = Validation.non_negative_decimal(sinv, name: 'units sold')
|
|
131
|
+
method = method.to_s
|
|
132
|
+
raise(ArgumentError, 'Inventory costing method must be FIFO, LIFO, or WAC.') unless %w[FIFO LIFO WAC].include?(method)
|
|
133
|
+
raise(ArgumentError, 'Purchase units and unit costs must have equal lengths.') unless units.size == price.size
|
|
134
|
+
|
|
135
|
+
[uinv, pinv, units, price, sinv, method]
|
|
136
|
+
end
|
|
137
|
+
private_class_method :inventory_inputs
|
|
138
|
+
|
|
132
139
|
# Depreciation Expense Recognition -- double-declining balance (DDB), the most common declining balance method, which applies two times the straight-line rate to the declining balance.
|
|
133
140
|
#
|
|
134
141
|
# @param cost cost of long-lived assets
|
|
@@ -137,11 +144,10 @@ module Finrb
|
|
|
137
144
|
# @example
|
|
138
145
|
# Finrb::Accounting.ddb(cost=1200,rv=200,t=5)
|
|
139
146
|
def self.ddb(cost:, rv:, t:)
|
|
140
|
-
cost =
|
|
141
|
-
rv =
|
|
142
|
-
t =
|
|
143
|
-
|
|
144
|
-
raise(Error, 't should be larger than 1') if t < 2
|
|
147
|
+
cost = Validation.non_negative_decimal(cost, name: 'asset cost')
|
|
148
|
+
rv = residual_value(rv, cost:)
|
|
149
|
+
t = Validation.positive_integer(t, name: 'useful life')
|
|
150
|
+
raise(DomainError, 'Useful life must be at least 2 periods for double-declining depreciation.') if t < 2
|
|
145
151
|
|
|
146
152
|
ddb = [Flt::DecNum(0)] * t
|
|
147
153
|
ddb[0] = cost * 2 / t
|
|
@@ -170,11 +176,24 @@ module Finrb
|
|
|
170
176
|
# @example
|
|
171
177
|
# Finrb::Accounting.slde(cost=1200,rv=200,t=5)
|
|
172
178
|
def self.slde(cost:, rv:, t:)
|
|
173
|
-
cost =
|
|
174
|
-
rv =
|
|
175
|
-
t =
|
|
179
|
+
cost = Validation.non_negative_decimal(cost, name: 'asset cost')
|
|
180
|
+
rv = residual_value(rv, cost:)
|
|
181
|
+
t = Validation.positive_decimal(t, name: 'useful life', error: DomainError)
|
|
176
182
|
|
|
177
183
|
((cost - rv) / t)
|
|
178
184
|
end
|
|
185
|
+
|
|
186
|
+
def self.inventory_values(values, name:)
|
|
187
|
+
wrap_array(values).map { |value| Validation.non_negative_decimal(value, name:) }
|
|
188
|
+
end
|
|
189
|
+
private_class_method :inventory_values
|
|
190
|
+
|
|
191
|
+
def self.residual_value(value, cost:)
|
|
192
|
+
value = Validation.non_negative_decimal(value, name: 'residual value')
|
|
193
|
+
raise(DomainError, 'Residual value must not exceed asset cost.') if value > cost
|
|
194
|
+
|
|
195
|
+
value
|
|
196
|
+
end
|
|
197
|
+
private_class_method :residual_value
|
|
179
198
|
end
|
|
180
199
|
end
|