1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Kauri\Loan; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class PaymentsCalculator implements PaymentsCalculatorInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var PaymentAmountCalculatorInterface |
12
|
|
|
*/ |
13
|
|
|
private $paymentAmountCalculator; |
14
|
|
|
/** |
15
|
|
|
* @var InterestAmountCalculatorInterface |
16
|
|
|
*/ |
17
|
|
|
private $interestAmountCalculator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* PaymentsCalculator constructor. |
21
|
|
|
* @param PaymentAmountCalculatorInterface $paymentAmountCalculator |
22
|
|
|
* @param InterestAmountCalculatorInterface $interestAmountCalculator |
23
|
|
|
*/ |
24
|
4 |
|
public function __construct( |
25
|
|
|
PaymentAmountCalculatorInterface $paymentAmountCalculator, |
26
|
|
|
InterestAmountCalculatorInterface $interestAmountCalculator |
27
|
|
|
) { |
28
|
4 |
|
$this->paymentAmountCalculator = $paymentAmountCalculator; |
29
|
4 |
|
$this->interestAmountCalculator = $interestAmountCalculator; |
30
|
4 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param PaymentPeriodsInterface $paymentPeriods |
34
|
|
|
* @param float $amountOfPrincipal |
35
|
|
|
* @param float $yearlyInterestRate |
36
|
|
|
* @param int $calculationMode |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
4 |
|
public function calculatePayments( |
40
|
|
|
PaymentPeriodsInterface $paymentPeriods, |
41
|
|
|
float $amountOfPrincipal, |
42
|
|
|
float $yearlyInterestRate, |
43
|
|
|
int $calculationMode |
44
|
|
|
): array { |
45
|
4 |
|
$payments = array(); |
46
|
|
|
|
47
|
4 |
|
$numberOfPayments = $paymentPeriods->getNoOfPeriods(); |
48
|
4 |
|
$principalLeft = $amountOfPrincipal; |
49
|
|
|
|
50
|
4 |
|
foreach ($paymentPeriods->getPeriods() as $key => $period) { |
51
|
4 |
|
$ratePerPeriod = $paymentPeriods->getRatePerPeriod($period, $yearlyInterestRate, $calculationMode); |
52
|
4 |
|
$numberOfPeriods = $paymentPeriods->getNumberOfRemainingPeriods($period, $calculationMode); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Calculate payment amount |
56
|
|
|
*/ |
57
|
4 |
|
$paymentAmount = $this->paymentAmountCalculator->getPaymentAmount($principalLeft, $ratePerPeriod, |
58
|
|
|
$numberOfPeriods); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Calculate interest part |
62
|
|
|
*/ |
63
|
4 |
|
$interest = $this->interestAmountCalculator->getInterestAmount($principalLeft, $ratePerPeriod); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Calculate principal part |
67
|
|
|
*/ |
68
|
4 |
|
if ($key < $numberOfPayments) { |
69
|
3 |
|
$principal = $paymentAmount - $interest; |
70
|
|
|
} else { |
71
|
4 |
|
$principal = $principalLeft; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Calculate balance left |
76
|
|
|
*/ |
77
|
4 |
|
$principalLeft = round($principalLeft - $principal, 2); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Compose payment data |
81
|
|
|
*/ |
82
|
|
|
$paymentData = array( |
83
|
4 |
|
'sequence_no' => $key, |
84
|
4 |
|
'payment' => $interest + $principal, |
85
|
4 |
|
'principal' => $principal, |
86
|
4 |
|
'interest' => $interest, |
87
|
4 |
|
'principal_left' => $principalLeft, |
88
|
4 |
|
'period' => $period |
89
|
|
|
); |
90
|
|
|
|
91
|
4 |
|
$payments[$key] = $paymentData; |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
return $payments; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|