-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
323 lines (265 loc) · 13.3 KB
/
index.html
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Amortization Schedule</title>
<!-- CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<!-- jQuery and JS bundle w/ Popper.js -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
crossorigin="anonymous"></script>
<style>
td {
font-weight: bold;
}
table thead {
background-color: #ccc;
}
</style>
<script src="dateStuff.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<span class="float-left">
<a href="https://github.com/truedeity/AmortizationSchedule">GITHUB</a>
</span>
<h1 class="float-right">Amortization Schedule</h1>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon3">Loan Amount:</span>
</div>
<input type="text" class="form-control" id="LoanAmount" value="10000" data-type='currency' />
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon3">Interest Rate:</span>
</div>
<input type="text" class="form-control" id="InterestRate" value="3.5" />
</div>
</div>
<div class="col-md-4">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon3">Years:</span>
</div>
<input type="number" class="form-control" id="years" value="1" />
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon3">Date:</span>
</div>
<input type="date" class="form-control" id="PaymentDate" value="2020-10-15" />
<button id="calcualte" class="btn btn-secondary" onclick="javascript:Calculate()">Calculate</button>
</div>
</div>
<div class="col-md-4">
<table class="table ">
<tr>
<td>Payment Amount: <span class="float-right" style="font-weight: bold;" id="paymentAmt"></span>
</td>
</tr>
<tr>
<td>Rate Per Period: <span class="float-right" id="ratePerPeriod"></span></td>
</tr>
<tr>
<td>Total Interest: <span class="float-right" id="totalInterest"></span></td>
</tr>
</table>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table id="results" class="table ">
<thead>
<tr>
<td>No.</td>
<td>Due Date</td>
<td>Payment Due</td>
<td>Additional Payment</td>
<td>Interest</td>
<td>Principal</td>
<td>Balance</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<a id="dlink" style="display:none;"></a>
<input type="button" class="btn btn-info" disabled="disabled"
onclick="tableToExcel('results', 'Amortization Schedule', 'Amortization.xls')" value="Export to Excel">
</div>
<script type="text/javascript">
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
// These options are needed to round to whole numbers if that's what you want.
// minimumFractionDigits: 0,
// maximumFractionDigits: 0,
});
var delay = (function () {
var timer = null;
return function (callback, ms) {
clearTimeout(timer);
timer = setTimeout(() => {
callback();
}, ms);
}
})();
function Calculate() {
$("input").removeAttr("disabled")
var balance = formatter.format(parseFloat($("#LoanAmount").val()));
$("#results tbody tr").remove();
var str = "<tr><td colspan='6'></td><td style='font-weight:bold;'>" + balance + "</td></tr>"
$("#results").find('tbody').append(str);
var paymentDate = $("#PaymentDate").val();
var loanAmount = parseFloat($("#LoanAmount").val()).toFixed(2);
var interestRate = parseFloat($("#InterestRate").val()).toFixed(2);
var years = parseFloat($("#years").val()).toFixed(2);
var compoundPeriod = 4; // monthly
var periodsPerYear = 12;
var runningAmount = loanAmount;
var numberOfPayments = years * periodsPerYear;
var indexPeriods = [1, 2, 4, 6, 12, 24, 26, 52];
var ratePerPeriod = ((1 + Math.pow(interestRate / indexPeriods[compoundPeriod], indexPeriods[compoundPeriod] / periodsPerYear)) - 1);
var df = DiscountFactor(interestRate / periodsPerYear, numberOfPayments)
var discountFactor = df.reduce((a, b) => { return a + b; });
var monthlyPaymentOrig = CalculateMonthlyPayment(loanAmount, numberOfPayments, interestRate / 100 / 12);
var monthlyPayment = CalculateMonthlyPayment(loanAmount, numberOfPayments, interestRate / 100 / 12);
var totalInterestPayments = 0;
for (var paymentNumber = 1; paymentNumber <= numberOfPayments; paymentNumber++) {
var terminate = false;
var additionalPaymentAmount = GetAdditionalPayment(paymentNumber);
if (runningAmount - ((monthlyPayment + additionalPaymentAmount) - ratePerPeriod * runningAmount * .01) < 0) {
monthlyPayment = runningAmount + ratePerPeriod * runningAmount * .01;
additionalPaymentAmount = 0;
terminate = true;
}
var interestPayment = ratePerPeriod * runningAmount * .01;
var principalPayment = (monthlyPayment + additionalPaymentAmount) - interestPayment;
runningAmount = runningAmount - principalPayment;
totalInterestPayments += interestPayment;
CreateEntry(
paymentNumber,
getFormattedDate(new Date(paymentDate).addMonths(paymentNumber)),
formatter.format(monthlyPayment),
additionalPaymentAmount,
formatter.format(interestPayment),
formatter.format(principalPayment),
formatter.format(runningAmount)
);
if (terminate) break;
}
$("#ratePerPeriod").text(parseFloat(ratePerPeriod).toFixed(3) + "%");
$("#paymentAmt").text(formatter.format(monthlyPaymentOrig));
$("#totalInterest").text(formatter.format(totalInterestPayments));
}
function CalculateMonthlyPayment(p, n, i) {
if (i == 0) return p / n;
return p * i * (Math.pow(1 + i, n)) / (Math.pow(1 + i, n) - 1);
}
function CreateEntry(num, dueDt, payDue, addtlPay, interest, principal, balance) {
var str = "<tr>" +
"<td>" + num + "</td>" +
"<td>" + dueDt + "</td>" +
"<td>" + payDue + "</td>" +
"<td> <input type='text' data-pmtnumber='" + num + "' name='additionalPayment' class='form-control' onkeyup='additionalPaymentKeyUp(this)' value='" + addtlPay + "'></input> </td>" +
"<td>" + interest + "</td>" +
"<td>" + principal + "</td>" +
"<td>" + balance + "</td>" +
"</tr>"
$("#results").find('tbody').append(str);
}
function DiscountFactor(rate, numOfPeriods) {
var dfs = [], discountFactor;
for (var i = 1; i <= numOfPeriods; i++) {
discountFactor = 1 / Math.pow((1 + rate / 100), (i - 1));
roundedDiscountFactor = Math.ceil(discountFactor * 1000) / 1000;
dfs.push(roundedDiscountFactor);
}
return dfs;
};
function GetAdditionalPayment(paymentNumber) {
var additionalPaymentAmount = 0;
var storageData = localStorage.getItem("additionalPayments");
if (storageData && storageData.indexOf('{') > -1) {
var jsonStored = JSON.parse(storageData);
for (var item in jsonStored) {
if (jsonStored[item].paymentNumber == paymentNumber) {
additionalPaymentAmount = parseFloat(jsonStored[item].additionalPayment);
}
}
}
return additionalPaymentAmount;
}
function additionalPaymentKeyUp(input) {
delay(() => {
var trs = $("#results tr");
$.each(trs, (index, tr) => {
var additionalPaymentAmount = $(tr).find("input").val();
var paymentNumber = parseInt($(tr).find("input").data("pmtnumber"));
var data = {
paymentNumber: paymentNumber,
additionalPayment: isNaN(additionalPaymentAmount) ? 0 : additionalPaymentAmount
};
var storageData = localStorage.getItem("additionalPayments");
if (storageData && storageData.indexOf('{') > -1) {
var jsonStored = JSON.parse(storageData);
var isUpdated = false;
for (var item in jsonStored) {
if (jsonStored[item].paymentNumber == data.paymentNumber) {
jsonStored[item].additionalPayment = data.additionalPayment;
isUpdated = true;
}
}
if (!isUpdated) {
jsonStored.push(data);
}
localStorage.setItem("additionalPayments", JSON.stringify(jsonStored));
} else {
var save = [];
save.push(data);
localStorage.setItem("additionalPayments", JSON.stringify(save));
}
storageData = localStorage.getItem("additionalPayments");
console.log(storageData);
});
Calculate();
}, 1000)
}
function getFormattedDate(date) {
var year = date.getFullYear();
var month = (1 + date.getMonth()).toString().padStart(2, '0');
var day = date.getDate().toString().padStart(2, '0');
return month + '/' + day + '/' + year;
}
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
return function (table, name, filename) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = filename;
document.getElementById("dlink").click();
}
})()
</script>
</body>
</html>