-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.php
174 lines (154 loc) · 6.42 KB
/
tools.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* Created by PhpStorm.
* User: Nick
* Date: 11/12/2016
* Time: 3:07 PM
*/
require './includes/header.php';
//require_once ('../../pdo_config.php');
/*======================================================================================================================
* Get values the user input from the form and check for errors and missing values
*/
if (isset($_GET['send'])) {
//Check for errors and missing values
$missing = array();
$errors = array();
$pvalue = trim(filter_input(INPUT_GET, 'pvalue', FILTER_SANITIZE_STRING)); //returns a string
if (empty($pvalue))
$missing[]='pvalue';
$interest = trim(filter_input(INPUT_GET, 'interest', FILTER_SANITIZE_STRING));
if (empty($interest))
$missing[]='interest';
$terms = trim(filter_input(INPUT_GET, 'terms', FILTER_SANITIZE_STRING));
if (empty($terms))
$missing[]='terms';
$years = trim(filter_input(INPUT_GET, 'years', FILTER_SANITIZE_STRING));
if (empty($years))
$missing[]='years';
}
/*======================================================================================================================
* npvFormula takes 3 parameters and calculates the future value
* This is a basic function that can easily be expanded upon to calculate loan repayments,
* future values of investments, number of terms required to finish paying a loan, etc.
*/
/**
* @param $pv the present value of the investment
* @param $i the interest rate
* @param $n number of terms
* @return int future value after calculating the compounding interest
*/
function npvFormula($pv, $i, $y, $n){
//These variables will be needed for future calculations, but possibly in different functions
$p = 0; //principle
$fv = 0; //future value
$pmt = 0;
$m = 1; //number of terms as an integer value
//All values are needed to calculate the future value
$i = $i; //interest
$n = $n; //number of terms as a string
$pv = $pv; //present value
$y = $y; //number of years
//Convert terms
if($n == "annual"){
$m = 1;
}
elseif($n == "semiannual"){
$m = 2;
}
elseif ($n == "monthly"){
$m = 12;
}
elseif ($n == "weekly"){
$m = 52;
}
//Convert nominative interest rate to EAR
$ear = ((1 + $i/$m) ** $m) - 1;
//Convert percent value to a decimal
$i = $i / 100;
$ear = $ear / 100;
//Calculate the future value using the effective annual interest rate
$fv = $pv * ((1 + $ear) ** $y);
return $fv;
}
?>
<main>
<div class="container">
<h2>Net Present Value</h2>
<p>Enter values to calculate the net present value.</p>
<form method="get" action="tools.php">
<fieldset>
<legend>NPV Calculator</legend>
<?php if ($missing || $errors) { ?>
<p class="label label-warning">Please fix the item(s) indicated.</p>
<?php } ?>
<p>
<label for="pvalue">Present Value:
<?php if ($missing && in_array('pvalue', $missing)) { ?>
<span class="label label-warning">Please enter your first name</span>
<?php } ?> </label>
<input name="pvalue" id="pvalue" type="text"
<?php if (isset($pvalue)) {
echo 'value="' . htmlspecialchars($pvalue) . '"';
} ?>
>
</p>
<p>
<label for="interest">Interest:
<?php if ($missing && in_array('interest', $missing)) { ?>
<span class="label label-warning">What is the interest rate?</span>
<?php } ?> </label>
<input name="interest" id="interest" type="text"
<?php if (isset($interest)) {
echo 'value="' . htmlspecialchars($interest) . '"';
} ?>
>
</p>
<p>
<label for="years">Number of years:
<?php if ($missing && in_array('years', $missing)) { ?>
<span class="label label-warning">Enter the number of years</span>
<?php } ?>
</label>
<input name="years" id="years" type="text"
<?php if (isset($years)) {
echo 'value="' . htmlspecialchars($years) . '"';
} ?>
>
</p>
<!----------------------------------------------------------------------------------------------------->
<!-- ToDo: Get terms from user via monthly, bimonthly, annual, semiannual, in order to calculate EAR -->
<p>
<?php if ($missing && in_array('terms', $missing)) {?>
<span class="label label-warning">Enter the number of terms</span>
<?php }?>
<label for="terms">Frequency of compounding interest: </label>
<select name="terms" id="terms">
<option value="annual"
<?php if($terms=="annual") { echo "selected"; }?>>Annual</option>
<option value="semiannual"
<?php if($terms=="semiannual"){ echo "selected"; }?>>Semi-Annual</option>
<option value="monthly"
<?php if($terms=="monthly"){ echo "selected"; }?>>Monthly</option>
<option value="weekly"
<?php if($terms=="weekly"){ echo "selected"; }?>>Weekly</option>
</select>
</p>
<!----------------------------------------------------------------------------------------------------->
<p>
<input name="send" type="submit" value="Calculate">
</p>
</fieldset>
</form>
<!-- Output the calculation -->
<?php
if (!$missing && !$errors) {
$fv_calc = npvFormula($pvalue, $interest, $years, $terms);
echo '<h2>';
printf("The future value is: $%.2f", $fv_calc);
echo '</h2>';
}
?>
</div>
</main>
<?php require './includes/footer.php'; ?>