|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author: Vova Lando <[email protected]> |
|
4
|
|
|
* @package: LoanPaymentsCalculator |
|
5
|
|
|
* @subpackage: |
|
6
|
|
|
* @created: 06/09/2017 13:30 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace cog\LoanPaymentsCalculator\PaymentSchedule; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use cog\LoanPaymentsCalculator\Payment\Payment; |
|
13
|
|
|
use cog\LoanPaymentsCalculator\Period\Period; |
|
14
|
|
|
use cog\LoanPaymentsCalculator\Schedule\Schedule; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class AnnuityPaymentScheduleCalculator implements PaymentScheduleCalculator |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Schedule[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private $schedulePeriods; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var float |
|
26
|
|
|
*/ |
|
27
|
|
|
private $principalAmount; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var float |
|
31
|
|
|
*/ |
|
32
|
|
|
private $dailyInterestRate; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* AnnuityPaymentScheduleCalculator constructor. |
|
36
|
|
|
* @param Period[] $schedulePeriods |
|
37
|
|
|
* @param float $principalAmount |
|
38
|
|
|
* @param float $dailyInterestRate |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function __construct($schedulePeriods, $principalAmount, $dailyInterestRate) |
|
41
|
|
|
{ |
|
42
|
2 |
|
$this->schedulePeriods = $schedulePeriods; |
|
|
|
|
|
|
43
|
2 |
|
$this->principalAmount = $principalAmount; |
|
44
|
2 |
|
$this->dailyInterestRate = $dailyInterestRate; |
|
45
|
2 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritdoc |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function calculateSchedule() |
|
51
|
|
|
{ |
|
52
|
|
|
/** |
|
53
|
|
|
* @var Payment[] $payments |
|
54
|
|
|
*/ |
|
55
|
2 |
|
$payments = []; |
|
56
|
2 |
|
$numberOfPeriods = count($this->schedulePeriods); |
|
57
|
2 |
|
$periodInterestRate = $this->calculateInterestPerPeriod(); |
|
58
|
2 |
|
$paymentAmount = $this->calculateAnnuityPaymentAmount($periodInterestRate); |
|
59
|
2 |
|
$totalPrincipalToPay = $this->principalAmount; |
|
60
|
|
|
|
|
61
|
2 |
|
for ($i=0; $i<$numberOfPeriods; $i++) { |
|
62
|
2 |
|
$payment = new Payment($this->schedulePeriods[$i]); |
|
|
|
|
|
|
63
|
|
|
// Payment interest |
|
64
|
2 |
|
$paymentInterest = $totalPrincipalToPay * $periodInterestRate; |
|
65
|
2 |
|
$payment->setInterest($paymentInterest); |
|
66
|
|
|
// Payment principal |
|
67
|
2 |
|
$paymentPrincipal = $i == $numberOfPeriods-1 ? $totalPrincipalToPay : $paymentAmount - $paymentInterest; |
|
68
|
2 |
|
$payment->setPrincipal($paymentPrincipal); |
|
69
|
|
|
// Payment totals |
|
70
|
2 |
|
$totalPrincipalToPay-=$paymentPrincipal; |
|
71
|
2 |
|
$payment->setPrincipalBalanceLeft($totalPrincipalToPay); |
|
72
|
|
|
|
|
73
|
2 |
|
$payments[] = $payment; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
return $payments; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param $interestPerPeriod |
|
|
|
|
|
|
81
|
|
|
* @return float|int |
|
82
|
|
|
*/ |
|
83
|
2 |
|
private function calculateAnnuityPaymentAmount($interestPerPeriod) |
|
84
|
|
|
{ |
|
85
|
|
|
// Payment = InterestPerPeriod x TotalPrincipal / 1 - ( 1 + InterestPerPeriod)^(-numberOfPeriods) |
|
86
|
2 |
|
return (($interestPerPeriod) * $this->principalAmount) / (1 - pow(1 + ($interestPerPeriod), -count($this->schedulePeriods))); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return float |
|
91
|
|
|
*/ |
|
92
|
2 |
|
private function calculateInterestPerPeriod() |
|
93
|
|
|
{ |
|
94
|
2 |
|
$startDate = $this->schedulePeriods[0]->startDate; |
|
|
|
|
|
|
95
|
2 |
|
$endDate = $this->schedulePeriods[count($this->schedulePeriods)-1]->endDate; |
|
|
|
|
|
|
96
|
2 |
|
$daysDiff = $startDate->diff($endDate)->days; |
|
97
|
|
|
|
|
98
|
2 |
|
return $this->dailyInterestRate * $daysDiff/count($this->schedulePeriods); |
|
99
|
|
|
} |
|
100
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..