-
Notifications
You must be signed in to change notification settings - Fork 0
/
financeManager.js
183 lines (159 loc) · 6.3 KB
/
financeManager.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
// DOM Elements
const transactionsBody = document.getElementById("transactions-body");
const customerSelect = document.getElementById("customer-select");
const customerBody = document.getElementById("customer-body");
// Financial summary elements
const gstAmountElement = document.getElementById("gst-amount");
const paidGstAmountElement = document.getElementById("paid-gst-amount");
const unpaidBillsCountElement = document.getElementById("bills-count");
const paidBillsCountElement = document.getElementById("paid-bills-count");
const receivablesAmountElement = document.getElementById("receivables-amount");
// Load saved transactions on page load
document.addEventListener("DOMContentLoaded", () => {
loadTransactions();
updateFinancialSummary();
});
// Add a new transaction
function addNewTransaction() {
const customerName = document.getElementById("customer-name").value.trim();
const description = document.getElementById("transaction-description").value.trim();
const amount = parseFloat(document.getElementById("transaction-amount").value);
const type = document.getElementById("transaction-type").value;
if (!customerName || !description || isNaN(amount)) {
alert("Please fill in all fields.");
return;
}
const transaction = {
date: new Date().toLocaleDateString(),
customer: customerName,
description: description,
amount: amount.toFixed(2),
type: type,
};
// Save transaction and refresh the table
saveTransaction(transaction);
displayTransaction(transaction);
updateCustomerSelect(customerName);
// Update financial summary
updateFinancialSummary();
// Clear form inputs
document.getElementById("customer-name").value = "";
document.getElementById("transaction-description").value = "";
document.getElementById("transaction-amount").value = "";
document.getElementById("transaction-type").value = "Income";
}
// Save a transaction to Local Storage
function saveTransaction(transaction) {
const transactions = JSON.parse(localStorage.getItem("transactions")) || [];
transactions.push(transaction);
localStorage.setItem("transactions", JSON.stringify(transactions));
}
// Load transactions from Local Storage
function loadTransactions() {
const transactions = JSON.parse(localStorage.getItem("transactions")) || [];
transactions.forEach((transaction) => {
displayTransaction(transaction);
updateCustomerSelect(transaction.customer);
});
}
// Display a transaction in the main table
function displayTransaction(transaction) {
const row = document.createElement("tr");
row.innerHTML = `
<td>${transaction.date}</td>
<td>${transaction.customer}</td>
<td>${transaction.description}</td>
<td>$${transaction.amount}</td>
<td>${transaction.type}</td>
<td>
<button class="remove-btn" onclick="removeTransaction(this)">Remove</button>
</td>
`;
transactionsBody.appendChild(row);
}
// Update the customer dropdown menu
function updateCustomerSelect(customer) {
if (![...customerSelect.options].some((option) => option.value === customer)) {
const option = document.createElement("option");
option.value = customer;
option.textContent = customer;
customerSelect.appendChild(option);
}
}
// View transactions for the selected customer
function viewCustomerTransactions() {
const selectedCustomer = customerSelect.value;
customerBody.innerHTML = ""; // Clear the customer table
if (!selectedCustomer) return;
const transactions = JSON.parse(localStorage.getItem("transactions")) || [];
const customerTransactions = transactions.filter(
(transaction) => transaction.customer === selectedCustomer
);
customerTransactions.forEach((transaction) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${transaction.date}</td>
<td>${transaction.description}</td>
<td>$${transaction.amount}</td>
<td>${transaction.type}</td>
`;
customerBody.appendChild(row);
});
}
// Remove a transaction
function removeTransaction(button) {
const row = button.parentElement.parentElement;
const customer = row.children[1].textContent;
const description = row.children[2].textContent;
const amount = parseFloat(row.children[3].textContent.replace("$", ""));
const type = row.children[4].textContent;
// Remove transaction from the table
row.remove();
// Remove transaction from Local Storage
const transactions = JSON.parse(localStorage.getItem("transactions")) || [];
const updatedTransactions = transactions.filter(
(t) =>
!(t.customer === customer && t.description === description && t.amount == amount && t.type === type)
);
localStorage.setItem("transactions", JSON.stringify(updatedTransactions));
// Refresh customer transactions if viewing
viewCustomerTransactions();
// Update financial summary
updateFinancialSummary();
}
// Update the financial summary
function updateFinancialSummary() {
const transactions = JSON.parse(localStorage.getItem("transactions")) || [];
let pendingGst = 0,
paidGst = 0,
unpaidBills = 0,
paidBills = 0,
totalReceivables = 0;
transactions.forEach((transaction) => {
switch (transaction.type) {
case "GST":
pendingGst += parseFloat(transaction.amount);
break;
case "Paid GST":
paidGst += parseFloat(transaction.amount);
break;
case "Bill":
unpaidBills += 1;
totalReceivables += parseFloat(transaction.amount);
break;
case "Paid Bill":
paidBills += 1;
break;
case "Income":
totalReceivables += parseFloat(transaction.amount);
break;
default:
break;
}
});
gstAmountElement.textContent = `${pendingGst.toFixed(2)}`;
paidGstAmountElement.textContent = `${paidGst.toFixed(2)}`;
unpaidBillsCountElement.textContent = unpaidBills;
paidBillsCountElement.textContent = paidBills;
receivablesAmountElement.textContent = `${totalReceivables.toFixed(2)}`;
}