-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
492 lines (425 loc) · 15 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<html>
<head>
<script src="js/jquery-latest.min.js"
type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.tablesorter.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="css/budget.css">
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<script>
/*
* Globals
*/
var _categories;
var _catTotals;
var _subcategories={};
var _subcatFull;
function setupPaycheckPage(){
$.each (_categories,function(){
var cat = this;
$.each(_subcategories[cat],function(){
var subcat = this;
$("#paycheckPage").append("<label>"+subcat+"</label>");
});
});
}
/*
* Budget Change function
*/
function changeInput(){
var row= $(this).closest("tr");
var other=null;
if ( $(this).val() == "")
return;
if( $(this).hasClass("amount")){
other = $(row).find(".percentage");
}else if ( $(this).hasClass("percentage")){
other = $(row).find(".amount");
}
// can only have percentage or amount
if ( $(other).val() != "" && $(other).val()!= "0"){
$(other).val("0");
}
if ( $(this).val() != ""){
$(this).closest("tr").addClass("changed");
}else{
$(this).closest("tr").removeClass("changed");
}
}
function budgetChange(){
changeInput.call(this);
var amtTot = 0;
var percTot = 0;
$.each($("#budgetMain").find("tr"), function(){
var perc = $(this).find(".percentage").val();
var amt = $(this).find(".amount").val();
if( $(this).find(".subcat").html() == "Tithes")
return true;
if ( perc != "")
percTot+= parseFloat(perc);
if ( amt != "")
amtTot += parseFloat(amt);
});
$("#budgetAmtTotal").val(amtTot);
$("#budgetPercTotal").val(percTot);
if(percTot > 100)
$("#budgetPercTotal").addClass("red");
else
$("#budgetPercTotal").removeClass("red");
}
function paycheckChange(){
changeInput.call(this);
var tot = 0;
$.each($("#paycheckMain").find(".changed").find("input"), function(){
var amt = $(this).val();
if(amt != ""){
tot += parseFloat(amt);
}
});
$("#paycheckTotal").val($("#paycheckIncome").val() - tot);
if ( $("#paycheckTotal").val() < 0)
$("#paycheckTotal").addClass("red");
else
$("#paycheckTotal").removeClass("red");
}
function transactionChange(){
var month= $("#transactionMonth").val();
$("#transactionMain").empty();
$.ajax({ type: "POST"
, url: "php/getTransactions.php"
, data: {"month": month}
, success: function(data){
if(data.length > 0){
var table = '<table class="sortable" id="transactionTable"><thead> <tr><th>Date</th><th>Account</th><th>Change</th><th>Total</th></tr><thead></table>';
var body = '<tbody></tbody>';
var tmp = data.split("\n");
tmp = tmp.slice(0,tmp.length-1);
$.each(tmp,function(){
var o = JSON.parse(this);
var row = '<tr></tr>';
row = $(row).append('<td>'+o.Date+'</td>');
row = $(row).append('<td>'+o.Account+'</td>');
row = $(row).append('<td>'+o.Change+'</td>');
row = $(row).append('<td>'+o.Total+'</td>');
body = $(body).append(row);
});
table = $(table).append(body);
$("#transactionMain").append(table);
$("#transactionTable").tablesorter();
}else
$("#transactionMain").append("<p>No Results Found</p>");
}
});
}
/*
* Save Buttons
*/
function budgetSave(){
var changed = $("#budgetMain").find(".changed");
var statement="";
$.each(changed,function(){
var subCat = $(this).find(".subcat");
var amount= $(this).find(".amount").val();
var percent= $(this).find(".percentage").val();
if ( amount == "")
amount = "0";
if (percent == "")
percent = "0";
statement+="update subcategories set amount="+amount+", percentage="+percent+" where name=\""+$(subCat).html()+"\";";
$(this).removeClass("changed");
});
$.ajax({type: "POST"
,url: "php/updateSubCategories.php"
,data:{statement:statement}
,success: function(data){
refreshSubcategories();
}
});
console.log(statement);
}
function paycheckEnter(){
var inc = parseFloat($("#paycheckIncome").val());
var total = inc;
var poorFlag = 0;
//Handle Tithing First
$("#paycheck-Tithes-amount").val(Math.floor(inc*10)/100);
changeInput.call($("#paycheck-Tithes-amount"));
total -= Math.floor(inc*10)/100;
inc -= Math.floor(inc*10)/100;
// Handle Standard amounts
$.each(_subcatFullPriority, function(){
var sub = this;
var amt = sub.amt;
var id = sub.name.replace(/[\/'\(\) ]/g,"_");
console.log(id);
if (sub.name == "Tithes")
return true;
if (amt != "" && amt != "0"){
if ((inc -amt) <0){
alert("Not enough Money");
poorFlag =1;
return false;
}
inc -= amt;
total -= amt;
$("#paycheck-"+id+"-amount").val(amt);
changeInput.call($("#paycheck-"+id+"-amount"));
}
});
if ( poorFlag == 1){
$("#paycheckTotal").val(total);
return;
}
if( total > 0 && poorFlag != 1){
//Now Handle Percentages
$.each(_subcatFullPriority,function(){
var sub = this;
var perc = sub.perc;
var id = sub.name.replace(/[\/'\(\) ]/g,"_");
if (sub.name == "Tithes")
return true;
if ( perc != "" && perc != "0"){
total -= Math.floor(inc * perc) / 100.0;
$("#paycheck-"+id+"-amount").val(Math.floor(inc*perc)/100.0);
changeInput.call($("#paycheck-"+id+"-amount"));
}
});
}
$("#paycheckTotal").val(total);
}
function paycheckSave(){
var changed = $("#paycheckMain").find(".changed");
var statement="";
$.each(changed,function(){
var subCat = $(this).find(".subcat");
var amount= $(this).find(".amount").val();
if ( amount != "" && amount != "0" && amount != 0){
statement+="update subcategories set total=round(total+"+amount+",2) where name=\""+$(subCat).html()+"\";";
$(this).removeClass("changed");
}
});
$.ajax({type: "POST"
,url: "php/updateSubCategories.php"
,data:{statement:statement}
,success: function(data){
refreshSubcategories();
}
});
console.log(statement);
}
/*
* Refresh Functions
*/
function refreshCategories(){
$.ajax({ type: "POST"
, url: "php/getCategoriesTotals.php"
, success: function(data){
_catTotals = data.split("\n");
_catTotals = _catTotals.slice(0,_catTotals.length -1);
_catTotals = _catTotals.map(function(obj){return JSON.parse(obj);});
refreshSummaryPage();
}
});
}
function refreshSubcategories(){
$.ajax({ type: "POST"
, url: "php/getSubcategoriesFull.php"
, success: function(data){
_subcatFull = data.split("\n");
_subcatFull = _subcatFull.slice(0,_subcatFull.length -1);
_subcatFull = _subcatFull.map(function(obj){
return JSON.parse(obj);
});
refreshBudgetPage();
refreshPaycheckPage();
refreshCategories();
}
});
$.ajax({ type: "POST"
, url: "php/getSubcategoriesFullPriority.php"
, success: function(data){
_subcatFullPriority = data.split("\n");
_subcatFullPriority = _subcatFullPriority.slice(0,_subcatFullPriority.length -1);
_subcatFullPriority = _subcatFullPriority.map(function(obj){
return JSON.parse(obj);
});
refreshBudgetPage();
refreshPaycheckPage();
refreshCategories();
}
});
}
/*
* Summary Page Setup
*/
function refreshSummaryPage(){
$("#summaryPage").empty();
var table = '<table><tr><th>Account</th><th>$</th></tr></table>';
var total = 0;
$.each(_catTotals,function(){
var cat = this;
total += parseFloat(cat.total);
var tmp= '<tr></tr>';
tmp = $(tmp).load("templates/summaryCategoryTemplateNew.html",
function(){
tmp.html(tmp.html().replace(/{{ text }}/g,cat.name).replace(/{{ total }}/g,cat.total));
}
);
table= $(table).append(tmp);
});
var tmp = '<tr style="font-weight:bold"></tr>';
tmp = $(tmp).load("templates/summaryCategoryTemplateNew.html",
function(){
tmp.html(tmp.html().replace(/{{ text }}/g,"TOTAL:").replace(/{{ total }}/g,total));
});
table=$(table).append(tmp);
$("#summaryPage").append(table);
}
/*
* Budget Page Setup
*/
function refreshBudgetPage(){
$("#budgetMain").empty();
var tmp = '<table><tr><th>Account</th> <th>Total</th> <th>Amount</th> <th>Percentage</th></tr></table>';
var amtTotal = 0;
var percTotal = 0;
$.each(_subcatFull, function(){
var sc = this;
var amt = sc.amt;
var perc = sc.perc;
var id = sc.name.replace(/ /g,"_");
var total = sc.total;
var cat = sc.parent.replace(/ /g,"_");
var sub = '<tr class="'+cat+'"></tr>';
if ( amt != "")
amtTotal += parseFloat(amt);
if (perc != "" && sc.name != "Tithes")
percTotal += parseFloat(perc);
sub = $(sub).load("templates/subcategoriesTemplate.html"
, function(){
sub.html(sub.html().replace(/{{ name }}/g, sc.name).replace(/{{ amt }}/g,amt).replace(/{{ perc }}/g,perc).replace(/{{ id }}/g,id).replace(/{{ total }}/g,total));
sub.find("input").blur(budgetChange);
});
tmp = $(tmp).append(sub);
});
$("#budgetMain").append(tmp);
$("#budgetAmtTotal").val(amtTotal);
$("#budgetPercTotal").val(percTotal);
if ( $("#budgetPercTotal").val() > 100)
$("#budgetPercTotal").addClass("red");
else
$("#budgetPercTotal").removeClass("red");
$.each(_catTotals,function(){
var cat = this.name.replace(/ /g,"_");
var lab = "<label><b>"+cat.replace(/_/g," ")+"</b></label>";
$(lab).insertBefore($("#budgetMain").find("tr."+cat).first());
});
}
/*
* Paycheck Page setup
*/
function refreshPaycheckPage(){
$("#paycheckMain").empty();
$("#paycheckTotal").val("");
var tmp = '<table><tr><th>Account</th><th>Total</th><th>Amount</th></tr></table>';
$.each(_subcatFull, function(){
var sc = this;
var amt = sc.amt;
var perc = sc.perc;
var id = sc.name.replace(/[\/'\(\) ]/g,"_");
var total = sc.total;
var cat = sc.parent;
var sub = '<tr class="'+cat+'"></tr>';
sub = $(sub).load("templates/paycheckTemplate.html"
, function(){
sub.html(sub.html().replace(/{{ name }}/g, sc.name).replace(/{{ id }}/g,id).replace(/{{ total }}/g,total));
sub.find("input").blur(paycheckChange);
});
tmp = $(tmp).append(sub);
});
$("#paycheckMain").append(tmp);
$.each(_catTotals,function(){
var cat = this.name;
var lab = "<label><b>"+cat+"</b></label>";
$(lab).insertBefore($("#paycheckMain").find("tr."+cat).first());
});
}
/*
* Start Method
*/
function start(){
/* $.ajax({ type: "POST"
, url: "php/getCategories.php"
, success: function(data){
_categories = data.split("\n");
_categories = _categories.slice(0,_categories.length -1);
handleCategories(_categories);
}
});
*/
refreshCategories();
refreshSubcategories();
$(function(){
$("#main").tabs();
});
$("#paycheckIncome").keyup(function(event){if (event.keyCode == 13) $("#paycheckEnterButton").click();});
}
window.onload=function(){start();};
</script>
<body>
<div id="main">
<ul>
<li><a href="#summaryPage">Summary</a></li>
<li><a href="#budget">Budget</a></li>
<li><a href="#paycheckPage">Update</a></li>
<li><a href="#transactionPage">History</a></li>
</ul>
<div id="summaryPage">
</div>
<div id="budget">
<div id="budgetMain">
</div>
<div>
<label style="padding-right:214px;">Totals</label>
<input id="budgetAmtTotal" type="number" val="0" style="width: 100px"/>
<input id="budgetPercTotal" type="number" val="0" style="width: 100px"/>
<button id="budgetSave" onclick="budgetSave()">Save</button>
</div>
</div>
<div id="paycheckPage">
<div>
<label>Income: </label>
<input id="paycheckIncome" type="number" style="width: 150px;" min=0;/>
<button id="paycheckEnterButton"onclick="paycheckEnter()">Enter</button>
</div>
<div id="paycheckMain"></div>
<div>
<label>Remainder</label>
<input id="paycheckTotal" type="number" value="0" style="width:100px;"/>
<button id="paycheckSave" onclick="paycheckSave()">Update</button>
</div>
</div>
<div id="transactionPage">
<div>
<select id="transactionMonth" onchange="transactionChange()">
<option>Select Month...</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
</div>
<div id="transactionMain"></div>
</div>
</div>
</body>
</html>