-
Notifications
You must be signed in to change notification settings - Fork 1
/
Reports.js
executable file
·330 lines (257 loc) · 9.69 KB
/
Reports.js
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
$(document).ready(function() {
var expanseDetails = {};
/*
* Object to populate the data for the bar graph
*/
var barData = {
labels:[],
datasets: []
};
/*
* Object to populate the data for the pie graph
*/
var pieData = [];
$("#showReport").hide();
$("#Items-List").hide();
$("#showDetailsMobile").accordion({
active: false,
collapsible: true
});
$("#SDatePicker").datepicker();
$("#EDatePicker").datepicker();
$(".DatePicker").on('change',function(){
if ($(this).val()) {
$(this).css('border-color', 'white');
}
});
$("#Cancel").click(function(){
location="Reports.html";
});
$("#newReport").click(function(event){
location.reload();
});
/*
* Retrieve the report data for the selected dates
*/
$("#Retrieve").click(function() {
var startDateElement = $("#SDatePicker");
var endDateElement = $("#EDatePicker");
var startDate = startDateElement.val();
var endDate = endDateElement.val();
if (!startDate) {
startDateElement.css('border-color', 'red');
startDateElement.focus();
return;
}
if (!endDate) {
endDateElement.css('border-color', 'red');
endDateElement.focus();
return;
}
var startDateAsDate = new Date(startDate);
var endDateAsDate = new Date(endDate);
endDateAsDate.setHours(23);
endDateAsDate.setMinutes(59);
endDateAsDate.setSeconds(59);
if (startDateAsDate > endDateAsDate) {
alert('startDateAsDate > endDateAsDate');
return;
}
(function () {
var Item = Parse.Object.extend("Cost_Items");
var cuser = Parse.User.current().id;
var query = new Parse.Query(Item);
query.lessThanOrEqualTo("createdAt", endDateAsDate);
query.greaterThanOrEqualTo("createdAt", startDateAsDate);
query.equalTo("user", {
__type: "Pointer",
className: "_User",
objectId: cuser
});
query.find({
success: function (results) {
for (var i = 0 ; i < results.length ; i++) {
var category = results[i].get("Category");
var subCategory = results[i].get("SubCategory");
var amount = Number(results[i].get("Amount"));
var date = results[i]["createdAt"];
if (category && amount && !isNaN(amount) && subCategory && date) {
var singleExpanseDetail = {};
singleExpanseDetail['subCategory'] = subCategory;
singleExpanseDetail['amount'] = amount;
singleExpanseDetail['date'] = utils.formatDate(date);
if (expanseDetails[category]) {
expanseDetails[category].push(singleExpanseDetail);
}
else {
expanseDetails[category] = [singleExpanseDetail];
}
}
}
$(document).trigger('dataLoaded');
}, error: function (error) {
console.log("Query Error:" + error.message)
}
});
})();
});
/*
* Populate the reports details after the data is loaded
*/
$(document).on('dataLoaded', function(){
createItemList();
fillPieChartData();
fillBarChartData();
});
/*
* Create the textual report
*/
function createItemList(){
var isDataEmpty = true;
for (var category in expanseDetails) {
isDataEmpty = false;
var categoryListItem = document.createElement("li");
categoryListItem.id = category;
categoryListItem.className = 'existing_items';
var totalCategoryAmount = 0;
var subCategories = expanseDetails[category];
for (var i = 0 ; i < subCategories.length ; i++){
totalCategoryAmount += subCategories[i].amount;
}
var categoryHeader = document.createElement("h3");
categoryHeader.textContent = category + ":" + " " + totalCategoryAmount;
categoryListItem.appendChild(categoryHeader);
var subCategoriesWrapper = document.createElement("div");
for (var i = 0 ; i < subCategories.length ; i++){
var subCategoryElement = document.createElement("p");
subCategoryElement.textContent = subCategories[i].subCategory + ' - ' + subCategories[i].amount + ' - ' + subCategories[i].date;
subCategoriesWrapper.appendChild(subCategoryElement);
}
categoryListItem.appendChild(subCategoriesWrapper);
$("#reportForm").slideUp(400);
$("#showReport").show();
$("#Items-List").show();
$('#Items-List').append(categoryListItem);
}
if (isDataEmpty){
var categoryListItem = document.createElement("li");
categoryListItem.id = 'emptyResult';
categoryListItem.className = 'existing_items';
var categoryHeader = document.createElement("h3");
categoryHeader.textContent = 'No data for the selected dates';
categoryListItem.appendChild(categoryHeader);
$("#reportForm").slideUp(400);
$("#showReport").show();
$("#Items-List").show();
$('#Items-List').append(categoryListItem);
}
else {
$('#reportButton').show();
$('#pieChartButton').show();
$('#barChartButton').show();
}
}
/*
* Fill the pie chart data
*/
function fillPieChartData(){
for (var category in expanseDetails) {
var totalCategoryAmount = 0;
var subCategories = expanseDetails[category];
for (var i = 0 ; i < subCategories.length ; i++){
totalCategoryAmount += subCategories[i].amount;
}
pieData.push(
{
value: totalCategoryAmount,
label: category,
color: utils.getRandomColor()
});
}
}
/*
* Fill the bar chart data
*/
function fillBarChartData(){
var dailyBudgetForBarChart =
{
label: 'Daily Budget',
fillColor: '#382765',
data: []
};
var amountSpentForBarChart =
{
label: 'Amount spent',
fillColor: '#7BC225',
data: []
};
var amountSpentPerDay = {};
for (var category in expanseDetails) {
var subCategories = expanseDetails[category];
for (var i = 0; i < subCategories.length; i++) {
var formattedDate = (subCategories[i].date).split(' ')[0];
if (amountSpentPerDay.hasOwnProperty(formattedDate)) {
amountSpentPerDay[formattedDate] += subCategories[i].amount;
}
else {
amountSpentPerDay[formattedDate] = subCategories[i].amount;
}
}
}
for (var date in amountSpentPerDay) {
barData.labels.push(date);
dailyBudgetForBarChart.data.push(commonObj.dailyBudget);
amountSpentForBarChart.data.push(amountSpentPerDay[date]);
}
barData.datasets.push(dailyBudgetForBarChart);
barData.datasets.push(amountSpentForBarChart);
}
var MSM = $("#mobileMenuButton");
MSM.click(function(event) {
$("#sideMenu").slideDown(400);
});
var CMSM = $("#closeMobileMenuButton");
CMSM.click(function(event) {
$("#sideMenu").slideUp(200);
});
var slidePieChartWrapperUp = false;
/*
* Pie chart button click event
*/
$('#pieChartButton').click(function(){
$("#showReport").slideUp(400);
$("#barChartWrapper").slideUp(400);
$(".barChartInfoWrapper").hide();
$("#pieChartWrapper").show();
$("#pieChartWrapper").slideDown(400);
var pieContext = document.getElementById('pieChart').getContext('2d');
var pieChart = new Chart(pieContext).Pie(pieData);
slidePieChartWrapperUp = true;
});
/*
* Bar chart button click event
*/
$('#barChartButton').click(function(){
$("#showReport").slideUp(400);
if (slidePieChartWrapperUp) {
$("#pieChartWrapper").slideUp(400);
}
$("#pieChartWrapper").hide();
$("#barChartWrapper").slideDown(400);
$(".barChartInfoWrapper").show();
var barContext = document.getElementById('barChart').getContext('2d');
var barChart = new Chart(barContext).Bar(barData);
});
/*
* Textual report button click event
*/
$('#reportButton').click(function(){
if (slidePieChartWrapperUp) {
$("#pieChartWrapper").slideUp(400);
}
$("#pieChartWrapper").hide();
$("#barChartWrapper").slideUp(400);
$(".barChartInfoWrapper").hide();
$("#showReport").slideDown(400);
});
});