-
Notifications
You must be signed in to change notification settings - Fork 6
/
DebtAmortizator.php
365 lines (325 loc) · 11.7 KB
/
DebtAmortizator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
namespace FinanCalc\Calculators {
use DateTime;
use FinanCalc\Interfaces\Calculator\CalculatorAbstract;
use FinanCalc\Utils\Lambdas;
use FinanCalc\Utils\MathFuncs;
use FinanCalc\Utils\Time\TimeSpan;
use FinanCalc\Utils\Time\TimeUtils;
/**
* Class DebtAmortizator
* @package FinanCalc\Calculators
*/
class DebtAmortizator extends CalculatorAbstract
{
/** @var RepaymentInstance[] */
// list of individual debt's repayments as an array of RepaymentInstance objects
protected $debtRepayments;
// principal of the debt = 'PV'
protected $debtPrincipal;
// number of periods pertaining to the interest compounding = 'n'
protected $debtNoOfCompoundingPeriods;
// length of a single period in days
/** @var TimeSpan */
protected $debtPeriodLength;
// the interest rate by which the unpaid balance is multiplied (i.e., a decimal number) = 'i'
protected $debtInterest;
// props returned by the getResultAsArray method by default
protected $propResultArray = [
"debtPrincipal",
"debtNoOfCompoundingPeriods",
"debtPeriodLength" =>
[
"years" => "debtPeriodLengthInYears",
"months" => "debtPeriodLengthInMonths",
"days" => "debtPeriodLengthInDays"
],
"debtInterest",
"debtDiscountFactor",
"debtLength" =>
[
"years" => "debtLengthInYears",
"months" => "debtLengthInMonths",
"days" => "debtLengthInDays"
],
"debtSingleRepayment",
"debtRepayments" => "debtRepaymentsAsArrays"
];
/**
* @param $debtPrincipal
* @param $debtNoOfCompoundingPeriods
* @param TimeSpan $debtPeriodLength
* @param $debtInterest
*/
public function __construct(
$debtPrincipal,
$debtNoOfCompoundingPeriods,
TimeSpan $debtPeriodLength,
$debtInterest
) {
$this->setDebtPrincipalWithoutRecalculation($debtPrincipal);
$this->setDebtNoOfCompoundingPeriodsWithoutRecalculation($debtNoOfCompoundingPeriods);
$this->setDebtPeriodLength($debtPeriodLength);
$this->setDebtInterestWithoutRecalculation($debtInterest);
$this->calculateDebtRepayments();
}
/**
* @param $debtPrincipal
*/
private function setDebtPrincipalWithoutRecalculation($debtPrincipal)
{
$this->setProperty("debtPrincipal", $debtPrincipal, Lambdas::checkIfPositive());
}
/**
* @param $debtNoOfCompoundingPeriods
*/
private function setDebtNoOfCompoundingPeriodsWithoutRecalculation($debtNoOfCompoundingPeriods)
{
$this->setProperty("debtNoOfCompoundingPeriods", $debtNoOfCompoundingPeriods, Lambdas::checkIfPositive());
}
/**
* @param $debtInterest
*/
private function setDebtInterestWithoutRecalculation($debtInterest)
{
$this->setProperty("debtInterest", $debtInterest, Lambdas::checkIfPositive());
}
/**
* @param $debtPrincipal
*/
public function setDebtPrincipal($debtPrincipal)
{
$this->setDebtPrincipalWithoutRecalculation($debtPrincipal);
$this->calculateDebtRepayments();
}
/**
* @param $debtNoOfCompoundingPeriods
*/
public function setDebtNoOfCompoundingPeriods($debtNoOfCompoundingPeriods)
{
$this->setDebtNoOfCompoundingPeriodsWithoutRecalculation($debtNoOfCompoundingPeriods);
$this->calculateDebtRepayments();
}
/**
* @param $debtPeriodLength
*/
public function setDebtPeriodLength(TimeSpan $debtPeriodLength)
{
$this->setProperty("debtPeriodLength", $debtPeriodLength, Lambdas::checkIfPositive());
}
/**
* @param $debtInterest
*/
public function setDebtInterest($debtInterest)
{
$this->setDebtInterestWithoutRecalculation($debtInterest);
$this->calculateDebtRepayments();
}
/**
* Private function populating the $debtRepayments array which represents the amortization schedule
* constructed on basis of the initial parameters passed to the constructor
*/
private function calculateDebtRepayments()
{
$this->debtRepayments = array();
$unpaidBalance = $this->debtPrincipal;
// calculate each repayment (more precisely its interest/principal components) and add it to the array
// storing the debt repayments (i.e., representing the amortization schedule)
// NOTE: rounding to two decimal places takes place when calculating the interest and principal parts
// of a single repayment
for ($i = 1; $i <= $this->debtNoOfCompoundingPeriods; $i++) {
$interestAmount = MathFuncs::mul($this->debtInterest, $unpaidBalance);
$principalAmount = MathFuncs::sub($this->getDebtSingleRepayment(), $interestAmount);
$this->debtRepayments[$i] = new RepaymentInstance($principalAmount, $interestAmount);
$unpaidBalance = MathFuncs::sub($unpaidBalance, $principalAmount);
}
}
/**
* @return string [Value of the debt principal as a string]
*/
public function getDebtPrincipal()
{
return $this->debtPrincipal;
}
/**
* @return string [Number of the debt's compounding periods as a string]
*/
public function getDebtNoOfCompoundingPeriods()
{
return $this->debtNoOfCompoundingPeriods;
}
/**
* @return TimeSpan
*/
public function getDebtPeriodLength()
{
return $this->debtPeriodLength;
}
/**
* @return string [Length of each of the debt's compounding periods in years as a string]
*/
public function getDebtPeriodLengthInYears()
{
return $this->debtPeriodLength->toYears();
}
/**
* @return string [Length of each of the debt's compounding periods in months as a string]
*/
public function getDebtPeriodLengthInMonths()
{
return $this->debtPeriodLength->toMonths();
}
/**
* @return string [Length of each of the debt's compounding periods in days as a string]
*/
public function getDebtPeriodLengthInDays()
{
return $this->debtPeriodLength->toDays();
}
/**
* @return string [Value of the debt's interest in a decimal number 'multiplier' form as a string]
*/
public function getDebtInterest()
{
return $this->debtInterest;
}
/**
* @return string [Value of the debt's discount factor as a string]
*/
public function getDebtDiscountFactor()
{
// discount factor 'v = 1/(1+i)'
return MathFuncs::div(
1,
MathFuncs::add(
1,
$this->debtInterest
)
);
}
/**
* @return string [Length of the debt in years as a string]
*/
public function getDebtLengthInYears()
{
return MathFuncs::div(
$this->getDebtLengthInDays(),
TimeUtils::getCurrentDayCountConvention()['days_in_a_year']
);
}
/**
* @return string [Length of the debt in months as a string]
*/
public function getDebtLengthInMonths()
{
return MathFuncs::div(
$this->getDebtLengthInDays(),
TimeUtils::getCurrentDayCountConvention()['days_in_a_month']
);
}
/**
* @return string [Length of the debt in years as a string]
*/
public function getDebtLengthInDays()
{
return MathFuncs::mul(
$this->debtNoOfCompoundingPeriods,
$this->debtPeriodLength->toDays()
);
}
/**
* @param DateTime $startDate [The start date of the debt]
* @return DateTime [The end date of the debt]
*/
public function getDebtEndDate(DateTime $startDate)
{
return TimeSpan
::asDurationWithStartDate($startDate, 0, 0, (int)$this->getDebtLengthInDays())
->getEndDate();
}
/**
* @return string [Value of a single debt repayment instance as a string]
*/
public function getDebtSingleRepayment()
{
// single repayment 'K = PV/((1-v^n)/i)'
return MathFuncs::div(
$this->debtPrincipal,
MathFuncs::div(
MathFuncs::sub(
1,
MathFuncs::pow(
$this->getDebtDiscountFactor(),
$this->debtNoOfCompoundingPeriods
)
),
$this->debtInterest
)
);
}
/**
* @return RepaymentInstance[] [Array of individual debt repayments (RepaymentInstances)]
*/
public function getDebtRepayments()
{
return $this->debtRepayments;
}
/**
* @return array
*/
public function getDebtRepaymentsAsArrays()
{
$repayments = array();
$i = 1;
foreach ($this->debtRepayments as $repayment) {
$repayments[$i++] = [
"principalAmount" => $repayment->getPrincipalAmount(),
"interestAmount" => $repayment->getInterestAmount(),
"totalAmount" => $repayment->getTotalAmount()
];
}
return $repayments;
}
}
/**
* Class RepaymentInstance
* @package FinanCalc\Calculators\DebtAmortizator
*/
class RepaymentInstance
{
// the "principal" part of the individual repayment instance
private $principalAmount;
// the "interest" part of the individual repayment instance
private $interestAmount;
/**
* @param string $principalAmount [Value of the amount of the payment's 'principal part' as a string]
* @param string $interestAmount [Value of the amount of the payment's 'interest part' as a string]
*/
public function __construct($principalAmount, $interestAmount)
{
$this->principalAmount = $principalAmount;
$this->interestAmount = $interestAmount;
}
/**
* @return string [Value of the amount of the payment's 'principal part' as a string]
*/
public function getPrincipalAmount()
{
return $this->principalAmount;
}
/**
* @return string [Value of the amount of the payment's 'interest part' as a string]
*/
public function getInterestAmount()
{
return $this->interestAmount;
}
/**
* @return string [Value of the total amount that the payment represents as a string]
*/
public function getTotalAmount()
{
return MathFuncs::add($this->principalAmount, $this->interestAmount);
}
}
}