-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
170 lines (139 loc) · 5.75 KB
/
Copy pathscript.js
File metadata and controls
170 lines (139 loc) · 5.75 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
let expenses = JSON.parse(localStorage.getItem('expenses')) || [];
let editingId = null;
const exchangeRates = {};
async function fetchExchangeRates() {
try {
const response = await fetch('https://api.frankfurter.app/latest');
const data = await response.json();
const inrResponse = await fetch('https://api.frankfurter.app/latest?from=INR');
const inrData = await inrResponse.json();
Object.assign(exchangeRates, data.rates, {
[data.base]: 1,
'INR': 82.5
});
if (inrData && inrData.rates) {
exchangeRates['INR'] = 1 / inrData.rates.EUR;
}
} catch (error) {
console.error('Error fetching exchange rates:', error);
exchangeRates['INR'] = 82.5;
}
}
fetchExchangeRates();
function convertAmount(amount, from, to) {
if (from === to) return amount;
if (!exchangeRates[from] || !exchangeRates[to]) return amount;
const toEUR = amount / exchangeRates[from];
return toEUR * exchangeRates[to];
}
function formatCurrency(amount, currency) {
return currency === 'INR' ? `₹${amount.toFixed(2)}` : `${currency} ${amount.toFixed(2)}`;
}
function saveExpenses() {
localStorage.setItem('expenses', JSON.stringify(expenses));
}
function calculateTotal(category = null) {
const baseCurrency = document.getElementById('baseCurrency').value;
return expenses.reduce((total, expense) => {
if (category && expense.category !== category) return total;
return total + convertAmount(expense.amount, expense.currency, baseCurrency);
}, 0);
}
function updateSummary() {
const baseCurrency = document.getElementById('baseCurrency').value;
document.getElementById('totalExpenses').textContent = formatCurrency(calculateTotal(), baseCurrency);
document.getElementById('foodTotal').textContent = formatCurrency(calculateTotal('food'), baseCurrency);
document.getElementById('transportationTotal').textContent = formatCurrency(calculateTotal('transportation'), baseCurrency);
document.getElementById('housingTotal').textContent = formatCurrency(calculateTotal('housing'), baseCurrency);
}
function renderExpenses() {
const tbody = document.getElementById('expensesList');
const baseCurrency = document.getElementById('baseCurrency').value;
tbody.innerHTML = '';
expenses.forEach(expense => {
const tr = document.createElement('tr');
const convertedAmount = convertAmount(expense.amount, expense.currency, baseCurrency);
tr.innerHTML = `
<td>${new Date(expense.date).toLocaleDateString()}</td>
<td>${expense.description}</td>
<td class="capitalize">${expense.category}</td>
<td>
${formatCurrency(expense.amount, expense.currency)}
${expense.currency !== baseCurrency ?
`<span class="text-gray-500 text-sm">(${formatCurrency(convertedAmount, baseCurrency)})</span>` : ''}
</td>
<td>
<button onclick="editExpense('${expense.id}')" class="action-btn edit-btn">✏️</button>
<button onclick="deleteExpense('${expense.id}')" class="action-btn delete-btn">🗑️</button>
</td>
`;
tbody.appendChild(tr);
});
}
document.getElementById('expenseForm').addEventListener('submit', (e) => {
e.preventDefault();
const amount = Number(document.getElementById('amount').value);
const currency = document.getElementById('currency').value;
const category = document.getElementById('category').value;
const description = document.getElementById('description').value;
if (editingId) {
expenses = expenses.map(expense =>
expense.id === editingId
? { ...expense, amount, currency, category, description, date: new Date().toISOString() }
: expense
);
editingId = null;
document.querySelector('.submit-btn').textContent = 'Add Expense';
} else {
expenses.push({
id: Date.now().toString(),
amount,
currency,
category,
description,
date: new Date().toISOString()
});
}
saveExpenses();
updateSummary();
renderExpenses();
e.target.reset();
});
function editExpense(id) {
const expense = expenses.find(e => e.id === id);
if (!expense) return;
editingId = id;
document.getElementById('amount').value = expense.amount;
document.getElementById('currency').value = expense.currency;
document.getElementById('category').value = expense.category;
document.getElementById('description').value = expense.description;
document.querySelector('.submit-btn').textContent = 'Update Expense';
}
function deleteExpense(id) {
if (!confirm('Are you sure you want to delete this expense?')) return;
expenses = expenses.filter(expense => expense.id !== id);
saveExpenses();
updateSummary();
renderExpenses();
}
document.getElementById('baseCurrency').addEventListener('change', () => {
updateSummary();
renderExpenses();
});
function addINRtoCurrencySelectors() {
const currencySelectors = ['baseCurrency', 'currency'];
currencySelectors.forEach(selectorId => {
const select = document.getElementById(selectorId);
if (select && !select.querySelector('option[value="INR"]')) {
const inrOption = document.createElement('option');
inrOption.value = 'INR';
inrOption.textContent = 'INR';
select.appendChild(inrOption);
}
});
}
document.addEventListener('DOMContentLoaded', () => {
addINRtoCurrencySelectors();
updateSummary();
renderExpenses();
});