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