-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
143 lines (122 loc) · 4.14 KB
/
user.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
(function () {
if ('ACTIVEUSER' in localStorage) {
activeuser = localStorage.getItem('ACTIVEUSER');
userStr = localStorage.getItem(activeuser);
if (userStr) {
user = JSON.parse(userStr);
uname.innerHTML = user.uname;
console.log(user);
display();
} else {
console.error('No user data found in localStorage');
window.location = './signin.html';
}
} else {
window.location = './signin.html';
}
})();
function signout() {
localStorage.removeItem('ACTIVEUSER');
window.location = './index.html';
}
function addIncome() {
const currentdate = new Date();
const datetime = `${currentdate.getDate()}/${currentdate.getMonth() + 1}/${currentdate.getFullYear()} , ${currentdate.getHours()}:${currentdate.getMinutes()}:${currentdate.getSeconds()}`;
const newAmt = document.getElementById('unia').value;
const newTyp = document.getElementById('unit').value;
if (!user.uinc) user.uinc = [];
user.uinc.push([newTyp, newAmt, datetime]);
localStorage.setItem(activeuser, JSON.stringify(user));
display();
}
function addExpense() {
const currentdate = new Date();
const datetime = `${currentdate.getDate()}/${currentdate.getMonth() + 1}/${currentdate.getFullYear()} , ${currentdate.getHours()}:${currentdate.getMinutes()}:${currentdate.getSeconds()}`;
const newAmt = parseInt(document.getElementById('unea').value);
const newTyp = document.getElementById('unet').value;
// Calculate total income and total expense
const totalIncome = user.uinc.reduce((total, n) => total + parseInt(n[1]), 0);
const totalExpense = user.uexp.reduce((total, n) => total + parseInt(n[1]), 0);
if ((totalExpense + newAmt) <= totalIncome) {
if (!user.uexp) user.uexp = [];
user.uexp.push([newTyp, newAmt, datetime]);
localStorage.setItem(activeuser, JSON.stringify(user));
display();
} else {
alert('Expense cannot be greater than total income.');
}
}
function clearData() {
user.uinc = [];
user.uexp = [];
localStorage.setItem(activeuser, JSON.stringify(user));
display();
}
function display() {
const rows = user.uinc.map(n => `
<tr>
<th scope="row">${n[1]}</th>
<td>${n[0]}</th>
<td>${n[2]}</td>
</tr>
`).join('');
const rows2 = user.uexp.map(n => `
<tr>
<th scope="row">${n[1]}</th>
<td>${n[0]}</th>
<td>${n[2]}</td>
</tr>
`).join('');
if (user.uinc.length > 0) {
const tinc = user.uinc.reduce((total, n) => total + parseInt(n[1]), 0);
console.log(tinc);
document.getElementById('ti').innerHTML = tinc;
} else {
document.getElementById('ti').innerHTML = 0;
}
if (user.uexp.length > 0) {
const texp = user.uexp.reduce((total, n) => total + parseInt(n[1]), 0);
document.getElementById('te').innerHTML = texp;
} else {
document.getElementById('te').innerHTML = 0;
}
if (user.uinc.length > 0 && user.uexp.length > 0) {
const tbal = user.uinc.reduce((total, n) => total + parseInt(n[1]), 0) - user.uexp.reduce((total, n) => total + parseInt(n[1]), 0);
document.getElementById('tb').innerHTML = tbal;
} else {
document.getElementById('tb').innerHTML = 0;
}
document.getElementById('idet').innerHTML = rows;
document.getElementById('edet').innerHTML = rows2;
// Pie chart values
const expenseTypes = user.uexp.map(n => n[0]);
const expenseValues = user.uexp.map(n => parseInt(n[1]));
const balance = user.uinc.reduce((total, n) => total + parseInt(n[1]), 0) - user.uexp.reduce((total, n) => total + parseInt(n[1]), 0);
// Adding balance as the first value
const xValues = ['Balance', ...expenseTypes];
const yValues = [balance, ...expenseValues];
const barColors = [
"#E32525", "#e65555", "#e39696", "#e6c8c8", "#e3d5d5", "#e3d5d5", "#c2b0b0",
"#9c9191", "#6e6565", "#423d3d", "#211e1e", "#141414", "#261a1a", "#261a1a",
"#7a3636", "#a33939", "#c93434",
];
new Chart("myChart", {
type: "pie",
data: {
labels: xValues,
datasets: [{
backgroundColor: barColors,
data: yValues,
borderWidth: 0
}]
},
options: {
title: {
display: false,
},
legend: {
display: false,
}
},
});
}