-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (141 loc) · 4.64 KB
/
script.js
File metadata and controls
171 lines (141 loc) · 4.64 KB
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
const checkCredit = document.getElementById("checkCredit");
const checkExpense = document.getElementById("checkExpense");
const historyContainer = document.getElementById("history");
let flag;
let nums = 0;
if (nums == 0) {
document.getElementById("history-empty").innerHTML = "Nothing to show";
}
checkCredit.addEventListener("click", () => {
if (checkCredit.checked) {
checkExpense.disabled = true;
flag = true;
} else {
checkExpense.disabled = false;
flag = false;
}
});
checkExpense.addEventListener("click", () => {
if (checkExpense.checked) {
checkCredit.disabled = true;
flag = false;
} else {
flag = true;
checkCredit.disabled = false;
}
});
const addButton = document.getElementById("add");
addButton.addEventListener("click", function () {
const titleInput = document.getElementById("title");
const amountInput = document.getElementById("amount");
const dateInput = document.getElementById("date");
const title = titleInput.value;
const amount = parseFloat(amountInput.value);
const date = dateInput.value;
if (title.trim() === "" || isNaN(amount) || date.trim() === "") {
alert("Please fill in all fields correctly.");
return;
}
if (checkCredit.checked || checkExpense.checked) {
addExpenseToHistory(flag, title, amount, date);
clearFields();
if (flag) {
updateProfitBalance(amount);
} else if (!flag) {
updateLossBalance(amount);
}
} else {
alert("Select the type of entry!");
}
nums++;
document.getElementById("history-empty").innerHTML = "";
});
function addExpenseToHistory(flag, title, amount, date) {
const historyItem = document.createElement("div");
historyItem.classList.add("history-boxes");
historyItem.innerHTML = `
<div class="line-1">
<h3>${title}</h3>
<h3 class="displayAMT">${
checkCredit.checked ? "+" : "-"
}${Math.abs(amount)} ₹</h3>
</div>
<div class="line-2">
<h5>${date}</h5>
<button class="delete-btn"><i class="fa-solid fa-trash-can" style="color: #3c2c0f;"></i></button>
</div>
`;
historyContainer.appendChild(historyItem);
const price = historyItem.querySelector(".displayAMT");
if (flag) {
price.style.color = "rgb(5, 83, 5)";
} else if (!flag) {
price.style.color = "rgb(112, 23, 23)";
}
const deleteBtn = historyItem.querySelector(".delete-btn");
deleteBtn.addEventListener("click", () => {
if (flag) {
updateProfitBalanceAfterDelete(amount);
} else if (!flag) {
updateLossBalanceAfterDelete(amount);
}
historyItem.remove();
alert("Deleted successfully!");
nums--;
if (nums == 0) {
document.getElementById("history-empty").innerHTML = "Nothing to show";
}
});
}
function clearFields() {
document.getElementById("title").value = "";
document.getElementById("amount").value = "";
document.getElementById("date").value = "";
checkCredit.checked = false;
checkExpense.checked = false;
checkCredit.disabled = false;
checkExpense.disabled = false;
}
let currentBalance = 0;
let currentProfit = 0;
let currentLoss = 0;
function updateProfitBalance(amount) {
const balance = document.getElementById("balance");
const newBalance = currentBalance + amount;
balance.innerHTML = `${newBalance} ₹`;
currentBalance = newBalance;
const profit = document.getElementById("profit");
const newProfit = currentProfit + amount;
profit.innerHTML = `+${newProfit} ₹`;
currentProfit = newProfit;
}
function updateLossBalance(amount) {
const balance = document.getElementById("balance");
const newBalance = currentBalance - amount;
balance.innerHTML = `${newBalance} ₹`;
currentBalance = newBalance;
const loss = document.getElementById("loss");
const newLoss = amount + currentLoss;
loss.innerHTML = `-${newLoss} ₹`;
currentLoss = newLoss;
}
function updateProfitBalanceAfterDelete(amount) {
const balance = document.getElementById("balance");
const newBalance = currentBalance - amount;
balance.innerHTML = `${Math.abs(newBalance)} ₹`;
currentBalance = newBalance;
const profit = document.getElementById("profit");
const newProfit = currentProfit - amount;
profit.innerHTML = `+${newProfit} ₹`;
currentProfit = newProfit;
}
function updateLossBalanceAfterDelete(amount) {
const balance = document.getElementById("balance");
const newBalance = currentBalance + amount;
balance.innerHTML = `${Math.abs(newBalance)} ₹`;
currentBalance = newBalance;
const loss = document.getElementById("loss");
const newLoss = currentLoss - amount;
loss.innerHTML = `-${newLoss} ₹`;
currentLoss = newLoss;
}