1 | <?php |
||
19 | class EqualPrincipalPaymentScheduleCalculator implements PaymentScheduleCalculator |
||
20 | { |
||
21 | /** |
||
22 | * @var Period[] |
||
23 | */ |
||
24 | private $schedulePeriods; |
||
25 | |||
26 | /** |
||
27 | * @var float |
||
28 | */ |
||
29 | private $principalAmount; |
||
30 | |||
31 | /** |
||
32 | * @var float |
||
33 | */ |
||
34 | private $dailyInterestRate; |
||
35 | |||
36 | /** |
||
37 | * @var float |
||
38 | */ |
||
39 | private $totalInterest; |
||
40 | |||
41 | /** |
||
42 | * PaymentSchedule constructor. |
||
43 | * @param Period[] $schedulePeriods |
||
44 | * @param float $principalAmount |
||
45 | * @param float $dailyInterestRate |
||
46 | */ |
||
47 | 2 | public function __construct( |
|
48 | $schedulePeriods, |
||
49 | $principalAmount, |
||
50 | $dailyInterestRate |
||
51 | ){ |
||
|
|||
52 | 2 | $this->schedulePeriods = $schedulePeriods; |
|
53 | 2 | $this->principalAmount = $principalAmount; |
|
54 | 2 | $this->dailyInterestRate = $dailyInterestRate; |
|
55 | 2 | $this->totalInterest = 0.0; |
|
56 | 2 | } |
|
57 | |||
58 | /** |
||
59 | * @return float |
||
60 | */ |
||
61 | 2 | public function getTotalInterest() |
|
62 | { |
||
63 | 2 | return $this->totalInterest; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param float $totalInterest |
||
68 | */ |
||
69 | public function setTotalInterest($totalInterest) |
||
70 | { |
||
71 | $this->totalInterest = $totalInterest; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | 2 | public function calculateSchedule() |
|
78 | { |
||
79 | /** |
||
80 | * @var Payment[] $payments |
||
81 | */ |
||
82 | 2 | $payments = []; |
|
83 | 2 | $numberOfPeriods = count($this->schedulePeriods); |
|
84 | 2 | $paymentPrincipal = $this->principalAmount/$numberOfPeriods; |
|
85 | 2 | $totalPrincipalToPay = $this->principalAmount; |
|
86 | |||
87 | 2 | for ($i=0; $i<$numberOfPeriods ; $i++) { |
|
88 | 2 | $payment = new Payment($this->schedulePeriods[$i]); |
|
89 | // Payment principal |
||
90 | 2 | $payment->setPrincipal($paymentPrincipal); |
|
91 | // Payment interest |
||
92 | 2 | $paymentInterest = $this->calculatePaymentInterest($totalPrincipalToPay, $this->dailyInterestRate, $payment->getPeriod()->daysLength); |
|
93 | 2 | $payment->setInterest($paymentInterest); |
|
94 | // Payment totals |
||
95 | 2 | $totalPrincipalToPay-=$paymentPrincipal; |
|
96 | 2 | $payment->setPrincipalBalanceLeft($totalPrincipalToPay); |
|
97 | |||
98 | 2 | $payments[] = $payment; |
|
99 | 2 | $this->totalInterest += $paymentInterest; |
|
100 | } |
||
101 | |||
102 | 2 | return $payments; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param float $remainingPrincipalAmount |
||
107 | * @param float $dailyInterestRate |
||
108 | * @param integer $periodInDays |
||
109 | * @return float |
||
110 | */ |
||
111 | 2 | private function calculatePaymentInterest($remainingPrincipalAmount, $dailyInterestRate, $periodInDays) |
|
115 | } |