-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
372 lines (317 loc) · 17.5 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Expense Tracker</title>
<!-- Third-party CSS files -->
<link href="bootstrap.min.css" rel="stylesheet" />
<!-- Custom CSS -->
<style>
input.ng-invalid {
border: 1px solid red;
}
</style>
</head>
<body ng-app="et2">
<div ng-controller="et2Controller" class="container pull-left">
<h4>Expense Tracker <small>Enter expense record details</small></h4>
<hr/>
<form name="frmExpense" class="form-horizontal" role="form">
<div class="form-group" >
<label class="col-sm-2 control-label" for="expDate">Date</label>
<div class="col-sm-2">
<div class="input-group">
<!-- note: type=date not recognized by all browsers, e.g. FF,IE -->
<input type="text" class="form-control" name="expDate" id="expDate" readonly ng-model="expense.Date"
datepicker-popup="{{format}}" datepicker-options="dateOptions" required is-open="opened" date-disabled="false" close-text="Close" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar" ng-click="open($event)"></span>
</span>
</div>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': frmExpense.expCategory.$error.required }">
<label class="col-sm-2 control-label" for="expCategory">Category</label>
<div class="col-sm-2">
<select id="expCategory" name="expCategory" class="form-control" ng-model="expense.Category" required
ng-options="category.code as category.desc for category in categoryList">
<option value="">Select category...</option>
</select>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': frmExpense.expAmount.$error.required || frmExpense.expAmount.$error.pattern }" >
<label class="col-sm-2 control-label" for="expAmount">Amount</label>
<div class="col-sm-2">
<input type="text" class="form-control" id="expAmount" name="expAmount" ng-model="expense.Amount" ng-pattern="amountPattern" required />
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': frmExpense.expComment.$error.required || frmExpense.expComment.$error.maxlength}">
<label class="col-sm-2 control-label" for="expComment">Comment</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="expComment" name="expComment" ng-model="expense.Comment" required ng-maxlength="45" />
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': frmExpense.expPaidBy.$error.required }">
<label class="col-sm-2 control-label" for="expPaidBy">Paid By</label>
<div class="col-sm-2">
<select id="expPaidBy" name="expPaidBy" class="form-control" ng-model="expense.PaidBy" required
ng-options="paidBy.code as paidBy.desc for paidBy in paidByList">
<option value="">Select paid by...</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button ng-click="save()" class="btn btn-default btn-primary" ng-disabled="frmExpense.$invalid">Save Expense Record</button>
</div>
</div>
</form>
<!-- handy to show what form json looks like -->
<!-- <pre>form = {{expense | json}}</pre> -->
<!-- DIVs for success/failure alerts -->
<div ng-cloak class="ng-cloak alert alert-success col-sm-offset-2 col-sm-4" ng-show="showSuccessAlert">
<button type="button" class="close" ng-click="switchBoolean('showSuccessAlert')">×</button>
<strong>Expense record saved</strong>
</div>
<div ng-cloak class="ng-cloak alert alert-danger col-sm-offset-2 col-sm-4" ng-show="showDangerAlert">
<button type="button" class="close" ng-click="switchBoolean('showDangerAlert')">×</button>
<strong>Expense record was not saved</strong>
</div>
<hr/>
<div class="col-sm-12">
<button ng-click="openDialog(1)" class="btn btn-default">List All Expenses</button>
<button ng-click="openDialog(2)" class="btn btn-default">Compare To Budget</button>
<button ng-click="openDialog(3)" class="btn btn-default">Prepare Budget</button>
<button ng-click="openDialog(4)" class="btn btn-default">Back-Up</button>
</div>
</div>
<!-- Third-party Javascript files -->
<script src="angular.js"></script>
<script src="ui-bootstrap-tpls.min.js"></script>
<!-- Custom Javascript -->
<script>
angular.module('et2', ['ui.bootstrap']);
var et2Controller = function ($scope,$http,$filter,$modal) {
$scope.expense = {};
$scope.showSuccessAlert = false;
$scope.showDangerAlert = false;
// initialise date field & datepicker options
$scope.today = function() {
$scope.expense.Date = new Date();
};
$scope.today();
$scope.clear = function () {
$scope.expense.Date = null;
};
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 0 // week starts Sunday
};
$scope.format = 'yyyy-MM-dd';
// regex pattern for amount field
$scope.amountPattern = /^\d{1,4}(\.\d{1,2})?$/; // 0000.00
// initialise paid-by drop-down list
$scope.expPaidBy = null;
$http({
method: 'GET',
url: 'backend/getPaidByList.php',
data: { }
}).success(function (result) {
$scope.paidByList = result;
});
// initialise categories drop-down list
$scope.expCategory = null;
$http({
method: 'GET',
url: 'backend/getCategoryList.php',
data: { }
}).success(function (result) {
$scope.categoryList = result;
//console.log(result);
});
// initialise some modal parm defaults
var t = new Date();
$scope.month = t.getMonth()+1;
$scope.year = t.getFullYear();
//$scope.categories = $scope.categoryList;
//{"code":"ALL","desc":"All Categories"}
$scope.switchBoolean = function (value) {
$scope[value] = !$scope[value];
};
// AJAX call to save expense record
$scope.save = function() {
$scope.expense.DateStr = $filter('date')($scope.expense.Date,'yyyy-MM-dd');
$http({
method: 'POST',
url: 'backend/saveExpense.php',
data: $scope.expense
}).success(function (result) {
if (result) {
$scope.expense = {}; // clear form fields
$scope.today(); // set date field
$scope.showSuccessAlert = true;
$scope.showDangerAlert = false;
} else {
$scope.showSuccessAlert = false;
$scope.showDangerAlert = true;
}
});
};
$scope.openDialog = function(n) { // refer: http://plnkr.co/edit/D29YjKGbY63OSa1EeixT?p=preview
//console.log('openDialog '+n);
if (n===1) {
// dialog options
$scope.opts = {
backdrop: true,
backdropClick: true,
dialogFade: false,
keyboard: false,
size: 'lg',
templateUrl : 'views/listExpenses.html',
controller : ModalInstanceCtrl,
resolve: {} // empty storage
};
// resolve what's being passed in
$scope.opts.resolve.item = function() {
var all = [{code:"ALL",desc:"ALL"}];
return angular.copy({
month : $scope.month.toString(),
year : $scope.year.toString(),
categoryList : all.concat($scope.categoryList),
category : 'ALL',
expenseList : null,
showDropdowns: true
}); // pass to dialog
};
// open the modal dialog
var modalInstance = $modal.open($scope.opts);
modalInstance.result.then(function(){
//on ok button press
},function(){
//on cancel button press
//console.log("modal closed");
});
} // if n===1
if (n===2) {
$scope.opts = {
backdrop: true,
backdropClick: true,
dialogFade: false,
keyboard: false,
size: 'lg',
templateUrl : 'views/compareBudget.html',
controller : ModalInstanceCtrl,
resolve: {}
};
$scope.opts.resolve.item = function() {
return angular.copy({
month : $scope.month.toString(),
year : $scope.year.toString(),
compareList : null
});
};
var modalInstance = $modal.open($scope.opts);
} // if n===2
};
$scope.listExpenses = function() {
//console.log('list expenses (in dialog) for::');
//console.log($scope.item.month);
//console.log($scope.item.year);
//console.log($scope.item.category);
$http({
method: 'POST',
url: 'backend/getExpenseList.php',
data: $scope.item
}).success(function (result) {
if (result) {
var resultTotal = 0;
result.forEach(function(exp) {
resultTotal += parseFloat(exp.amount);
});
$scope.item.expenseList = result;
$scope.item.expenseTotal = resultTotal;
} else {
console.log('getExpenseList failed');
}
}); // http
}; // listExpenses function
$scope.compareBudget = function() {
console.log('compareBudget (in dialog) for::');
console.log($scope.item.month);
console.log($scope.item.year);
$http({
method: 'POST',
url: 'backend/compareBudget.php',
data: $scope.item
}).success(function (result) {
if (result) {
var resultTotalBudgeted = 0;
var resultTotalActual = 0;
result.forEach(function(comp) {
resultTotalBudgeted += parseFloat(comp.budgeted);
resultTotalActual += parseFloat(comp.actual);
});
$scope.item.compareList = result;
$scope.item.compareTotalBudgeted = resultTotalBudgeted;
$scope.item.compareTotalActual = resultTotalActual;
if (resultTotalActual > resultTotalBudgeted) {
$scope.item.compareTotalStatus = 'over';
} else {
$scope.item.compareTotalStatus = 'ok';
}
} else {
console.log('compareBudget failed');
}
}); // http
}; // compareBudget function
}; // var et2Controller
var ModalInstanceCtrl = function($scope, $modalInstance, $modal, item) {
$scope.item = item;
//console.log($scope.item.categoryList);
$scope.close = function () {
$modalInstance.dismiss('cancel');
};
$scope.openSubDialog = function(mth,yr,cat) {
console.log('in openSubDialog()::'+mth+yr+cat);
/*
// @TODO fetch expense list first ???
// sub-dialog options
$scope.opts = {
backdrop: true,
backdropClick: true,
dialogFade: false,
keyboard: false,
size: 'lg',
templateUrl : 'views/listExpenses.html',
controller : SubModalInstanceCtrl,
resolve: {} // empty storage
};
// resolve what's being passed in
$scope.opts.resolve.item = function() {
return angular.copy({
month : mth,
year : yr,
category : cat,
expenseList : null,
showDropdowns: false
}); // pass to dialog
};
// open the modal sub-dialog
var subModalInstance = $modal.open($scope.opts);
*/
}; // openSubDialog()
}; // var ModalInstanceCtrl
var SubModalInstanceCtrl = function($scope, $modalInstance, $modal, item) {
$scope.item = item;
$scope.close = function () {
$modalInstance.dismiss('cancel');
};
}; // var SubModalInstanceCtrl
</script>
</body>
</html>