-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loan.php
141 lines (111 loc) · 3.8 KB
/
Loan.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace PHPLoan;
class Schedule
{
public $numpayment;
public $payment;
public $interest;
public $principal;
public $balance;
}
class Loan
{
/**
* @desc Calculates the amount of every monthly payment based on the loan
* principal, interest rate and number of payments.
*
* @param Float $P Principal (loan amount)
* @param Float $r Annual interest rate
* @param Integer $n Number of payments (months)
*
* @return Float $A Monthly payment amount
*
* (i * (1 + i)) ** n)
* A = P * ---------------------
* (1 + i) ** n - 1
*
*/
public static function calculateMonthlyPayment( $P , $r, $n )
{
$i = $r / 1200; // Monthly interest rate
$A = $P * (( $i * (1 + $i) ** $n ) / ( (1 + $i) ** $n - 1 ));
return $A;
}
/**
* @desc Calculates the loan principal based on the number of payments,
* interest rate and payment amount.
*
* @param Integer $n Number of payments (months)
* @param Float $r Annual interest rate
* @param Float $A Monthly payment amount
*
* @return Float $P Loan principal
*
* ((1 + i) ** n) - 1
* P = A * --------------------
* i * ((1 + i) ** n)
*
*/
public static function calculatePrincipal( $n, $r, $A )
{
$i = $r / 1200; // Monthly interest rate
$P = $A * ( (1 + $i) ** $n - 1 ) / ( $i * ((1 + $i) ** $n ));
return $P;
}
/**
* @desc Calculates the number of payments based on the principal,
* interest rate and payment amount.
*
* @param Integer $P Principal (loan amount)
* @param Float $r Annual interest rate
* @param Float $A Monthly payment amount
*
* @return Float $n Number of monthly payments
*
* log(1 - i * P/A)
* n = ------------------
* log(1 + i)
*
*/
public static function calculateNumPayments( $P, $r, $A )
{
$i = $r / 1200; // Monthly interest rate
$n = (Int)abs(log(1 - $i * $P/$A) / log(1+$i));
return $n;
}
/**
* @desc Returns the loan payment schedule
*
* @param Float $P Principal (loan amount)
* @param Float $r Annual interest rate
* @param Integer $n Number of payments (months)
*
* @return Object $output
*
* $output->numpayment Payment order number
* $output->payment Monthly payment amount
* $output->interest Monthly interests amount |____ sums $output->payment
* $output->principal Monthly principal amount |
* $output->balance Pending capital
*
*/
public static function getSchedule( $P, $r, $n )
{
// Monthly payment amount
$A = Loan::calculateMonthlyPayment( $P , $r, $n );
for ($i=1; $i <= $n ; $i++) {
// Interest amount on outstanding balance
$interest = $P * $r/1200;
// Portion of payment that amortizes capital
$principal = $A - $interest;
$P = $P - $principal;
$output[$i] = new Schedule;
$output[$i]->numpayment = $i;
$output[$i]->payment = number_format( $A, 2, ".", "," );
$output[$i]->interest = number_format( $interest, 2, ".", "," );
$output[$i]->principal = number_format( $principal, 2, ".", "," );
$output[$i]->balance = number_format( $P, 2, ".", "," );
}
return $output;
}
}