|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Amortization Schedule Calculator |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Jiangbianwanghai\BankLoan; |
|
6
|
|
|
class BankLoan |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var int |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $loanAmount = 100000; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var int |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $year = 1; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var float |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $interestRate = 0; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $interestRateChangeIndex = 0; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $bank = 'PBC'; // The people's bank of China |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
private $_monthlyinterestRate = 0; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
private $_interestPartTemp = 0; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* init param |
|
45
|
|
|
* |
|
46
|
|
|
* @param int $config['loanAmount'] |
|
|
|
|
|
|
47
|
|
|
* @param int $config['year'] |
|
|
|
|
|
|
48
|
|
|
* @param int $config['interestRate'] |
|
|
|
|
|
|
49
|
|
|
* @param int $config['interestRateChangeIndex'] |
|
|
|
|
|
|
50
|
|
|
* |
|
51
|
|
|
*/ |
|
52
|
9 |
|
public function __construct($config) |
|
53
|
|
|
{ |
|
54
|
9 |
|
if (isset($config['loanAmount']) && !empty($config['loanAmount'])) { |
|
55
|
9 |
|
$this->loanAmount = $config['loanAmount']; |
|
56
|
9 |
|
} |
|
57
|
9 |
View Code Duplication |
if (isset($config['year']) && !empty($config['year'])) { |
|
|
|
|
|
|
58
|
9 |
|
$this->year = $config['year']; |
|
59
|
9 |
|
} |
|
60
|
9 |
View Code Duplication |
if (isset($config['interestRateChangeIndex']) && !empty($config['interestRateChangeIndex'])) { |
|
|
|
|
|
|
61
|
3 |
|
$this->interestRateChangeIndex = $config['interestRateChangeIndex']; |
|
62
|
3 |
|
} |
|
63
|
9 |
|
if (isset($config['interestRate']) && !empty($config['interestRate'])) { |
|
64
|
3 |
|
$this->interestRate = $config['interestRate']/100; |
|
|
|
|
|
|
65
|
3 |
|
} else { |
|
66
|
6 |
|
$this->_getinterestRate(); |
|
67
|
|
|
} |
|
68
|
9 |
|
$this->_monthlyinterestRate = $this->interestRate/12; |
|
|
|
|
|
|
69
|
9 |
|
$this->period = $this->year*12; |
|
|
|
|
|
|
70
|
9 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Equal Loan Payments |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
* |
|
77
|
|
|
*/ |
|
78
|
9 |
|
public function getELP() |
|
79
|
|
|
{ |
|
80
|
9 |
|
$output = []; |
|
81
|
9 |
|
$paymentAmount = $this->loanAmount*($this->_monthlyinterestRate*pow(1+$this->_monthlyinterestRate, $this->year*12))/(pow(1+$this->_monthlyinterestRate, $this->year*12)-1); // Payment Amount |
|
82
|
9 |
|
$loanAmount = $this->loanAmount; |
|
83
|
9 |
|
$initPrincipalPart = 0; // Init Prncipal Part |
|
84
|
9 |
|
for ($i=1; $i <= $this->period; $i++) { |
|
85
|
9 |
|
$loanAmount = $loanAmount - $initPrincipalPart; |
|
86
|
9 |
|
$interestPart = $loanAmount*$this->_monthlyinterestRate; |
|
87
|
9 |
|
$output['period'][$i]['ip'] = sprintf("%.2f", $interestPart); // Interest Part |
|
88
|
9 |
|
$output['period'][$i]['pa'] = sprintf("%.2f", $paymentAmount); // Payment Amount |
|
89
|
9 |
|
$principal = $initPrincipalPart = $paymentAmount - $interestPart; |
|
90
|
9 |
|
$output['period'][$i]['pp'] = sprintf("%.2f", $principal); // Principal Part |
|
91
|
9 |
|
$output['period'][$i]['bo'] = sprintf("%.2f", abs($loanAmount-$principal)); // Banlance Owed |
|
92
|
9 |
|
} |
|
93
|
9 |
|
return $output; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Equal Principal Payments |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
* |
|
101
|
|
|
*/ |
|
102
|
9 |
|
public function getEPP() |
|
103
|
|
|
{ |
|
104
|
9 |
|
$output = []; |
|
105
|
9 |
|
$principalPart = $this->loanAmount/($this->year*12); // Principal Part |
|
106
|
9 |
|
$loanAmountInterest = 0; |
|
107
|
9 |
|
$loanAmount = $this->loanAmount; |
|
108
|
9 |
|
$equalAll = $equalItem = 0; |
|
109
|
9 |
|
for ($i=1; $i <= $this->period; $i++) { |
|
110
|
9 |
|
if ($i > 1) |
|
111
|
9 |
|
$loanAmount = $loanAmount - $principalPart; |
|
112
|
9 |
|
$interestPart = $loanAmount*$this->_monthlyinterestRate; |
|
113
|
9 |
|
$loanAmountInterest += $interestPart; |
|
114
|
9 |
|
$output['period'][$i]['ip'] = sprintf("%.2f", $interestPart); // Interest Part |
|
115
|
9 |
|
$output['period'][$i]['pp'] = sprintf("%.2f", $principalPart); // Principal Part |
|
116
|
9 |
|
$output['period'][$i]['pa'] = sprintf("%.2f", $principalPart+$interestPart); // Payment Amount |
|
117
|
9 |
|
$output['period'][$i]['bo'] = sprintf("%.2f", $loanAmount - $principalPart); // Balance Owed |
|
118
|
9 |
|
$i > 1 && $equalItem = $this->_interestPartTemp - $interestPart; |
|
119
|
9 |
|
$this->_interestPartTemp = $interestPart; |
|
120
|
9 |
|
$equalAll += $equalItem; |
|
121
|
9 |
|
} |
|
122
|
9 |
|
$output['ti'] = sprintf("%.2f", $loanAmountInterest); // loanAmount Interest |
|
123
|
9 |
|
$output['tp'] = sprintf("%.2f", $this->loanAmount+$loanAmountInterest); // loanAmount Payments |
|
124
|
9 |
|
$output['equal'] = sprintf("%.2f", $equalAll/($this->year*12 - 1)); |
|
125
|
9 |
|
return $output; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get ank interestRate |
|
130
|
|
|
*/ |
|
131
|
6 |
|
private function _getinterestRate() |
|
132
|
|
|
{ |
|
133
|
6 |
|
$config = require(__DIR__.'/config.php'); |
|
134
|
6 |
|
if ($this->interestRateChangeIndex) { |
|
135
|
3 |
|
if (isset($config[$this->bank][$this->interestRateChangeIndex])) { |
|
136
|
3 |
|
$currConfig = $config[$this->bank][$this->interestRateChangeIndex]; |
|
137
|
3 |
|
} |
|
138
|
3 |
|
} else { |
|
139
|
3 |
|
$currConfig = end($config[$this->bank]); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
6 |
|
if (empty($this->interestRate)) { |
|
143
|
6 |
|
if ($this->year <= 0.6) { |
|
144
|
|
|
$interestRate = $currConfig[0]; |
|
|
|
|
|
|
145
|
6 |
|
} elseif ($this->year <= 1) { |
|
146
|
|
|
$interestRate = $currConfig[1]; |
|
147
|
6 |
|
} elseif ($this->year <= 3) { |
|
148
|
|
|
$interestRate = $currConfig[2]; |
|
149
|
6 |
|
} elseif ($this->year <= 5) { |
|
150
|
3 |
|
$interestRate = $currConfig[3]; |
|
151
|
3 |
|
} else { |
|
152
|
3 |
|
$interestRate = $currConfig[4]; |
|
153
|
|
|
} |
|
154
|
6 |
|
$this->interestRate = $interestRate/100; |
|
|
|
|
|
|
155
|
6 |
|
} |
|
156
|
6 |
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.