-
Notifications
You must be signed in to change notification settings - Fork 1
/
Common.js
162 lines (137 loc) · 5.17 KB
/
Common.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
$(document).ready(function() {
var needToLogin = false;
/*
* Try to get the logged in user
*/
try {
if (!Parse.User.current()) {
needToLogin = true;
}
}
catch(e) {
alert('Session expired. Please login again')
needToLogin = true;
}
/*
* If the user is not logged in, go to the login page
*/
if (needToLogin) {
location = 'index.html';
}
$("#sideMenu").hide();
/*
* Common object that contains the user details and common functions
*/
var commonObject = (function() {
this.totalAmountSpentToday = 0;
this.totalAmountSpentThisMonth = 0;
this.userName = Parse.User.current().get("username");
this.monthlyBudget = Parse.User.current().get("budget");
/*
* Get the amount spent in the current month
*/
(function() {
var Item = Parse.Object.extend("Cost_Items");
var cuser = Parse.User.current().id;
var query = new Parse.Query(Item);
var date = new Date();
query.lessThanOrEqualTo("createdAt", date);
query.greaterThanOrEqualTo("createdAt", new Date(date.getFullYear(), date.getMonth(), 1));
query.equalTo("user", {
__type: "Pointer",
className: "_User",
objectId: cuser
});
query.find({
success: function (results) {
for (var i = 0 ; i < results.length ; i++) {
commonObj.totalAmountSpentThisMonth += Number(results[i].get("Amount"));
}
$(document).trigger('totalAmountSpentThisMonthLoaded');
}, error: function (error) {
console.log("Query Error:" + error.message)
}
});
})();
/*
* Calculate the daily budget according to the monthly budget and number of days of the current month
*/
this.dailyBudget = (function () {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var monthStart = new Date(year, month, 1);
var monthEnd = new Date(2015, month + 1, 1);
var monthLength = Math.floor((monthEnd - monthStart) / (1000 * 60 * 60 * 24));
var calculatedDailyBudget = (Parse.User.current().get("budget") / monthLength);
return calculatedDailyBudget;
})();
/*
* Get the amount spent in the current day
*/
(function() {
var Item = Parse.Object.extend("Cost_Items");
var cuser = Parse.User.current().id;
var query = new Parse.Query(Item);
var date = new Date();
query.lessThanOrEqualTo("createdAt", date);
query.greaterThanOrEqualTo("createdAt", new Date(date.getFullYear(), date.getMonth(), date.getDate()));
query.equalTo("user", {
__type: "Pointer",
className: "_User",
objectId: cuser
});
query.find({
success: function (results) {
for (var i = 0 ; i < results.length ; i++) {
commonObj.totalAmountSpentToday += Number(results[i].get("Amount"));
}
$(document).trigger('totalAmountSpentTodayLoaded');
}, error: function (error) {
console.log("Query Error:" + error.message)
}
});
})();
$(".userName").html(this.userName);
/*
* After the data is retrieved, populate the html with the monthly remaining budget
*/
$(document).on('totalAmountSpentThisMonthLoaded', function () {
$(".monthlyBudget").html("Monthly budget:" + " " + (commonObj.monthlyBudget - commonObj.totalAmountSpentThisMonth));
});
/*
* After the data is retrieved, populate the html with the daily remaining budget
*/
$(document).on('totalAmountSpentTodayLoaded', function () {
$(".leftToSpendToday").html("Daily budget:" + " " + parseFloat(commonObj.dailyBudget - commonObj.totalAmountSpentToday).toFixed(2));
});
$("#logoutSlideMenu").click(logOut);
$("#logoutDeskMenu").click(logOut);
/*
* Logout
*/
function logOut(){
Parse.User.logOut();
location="index.html";
}
/*
* Check if the inserted amount is in the daily budget
*/
this.isAmountInDailyBudget = (function(amount) {
if (commonObj.dailyBudget - commonObj.totalAmountSpentToday - amount >= 0) {
return true;
}
return false;
});
/*
* Check if the inserted amount is in the monthly budget
*/
this.isAmountInInMonthlyBudget = (function(amount) {
if (commonObj.monthlyBudget - commonObj.totalAmountSpentThisMonth - amount >= 0) {
return true;
}
return false;
});
});
window.commonObj = new commonObject();
});