-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
276 lines (224 loc) · 8.09 KB
/
Copy pathscript.js
File metadata and controls
276 lines (224 loc) · 8.09 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const descriptionInput = document.getElementById('description');
const amountInput = document.getElementById('amount' );
const incomeButton = document.getElementById( 'income-button');
const expenseButton = document.getElementById('expense-button');
const addButton = document.getElementById('add-button');
const transactionsList = document.getElementById('transactions-list');
const balanceDisplay = document.getElementById('balance');
const totalIncomeDisplay = document.getElementById('total-income');
const totalExpenseDisplay = document.getElementById('total-expense');
const rateDisplay = document.getElementById('rate-display');
let exchangeRate = 0.18;
const buttonReais = document.getElementById('brasil');
const buttonDollar = document.getElementById('usa');
const sortSelect = document.getElementById('sort-select');
const exportCSV = document.getElementById('export');
let transactions = JSON.parse(localStorage.getItem('transactions')) || [];
let currentType = 'income';
let currentCurrency = localStorage.getItem('currency') || 'R$';
function updateTodayDate() {
const todayDate = document.getElementById('today-date');
if (!todayDate) return;
const locale = currentCurrency === 'R$' ? 'pt-BR' : 'en-US';
todayDate.textContent = new Date().toLocaleDateString(locale, {
weekday: 'long',
month: 'long',
day: 'numeric'
});
}
async function fetchExchangeRate() {
try {
const response = await fetch('https://api.frankfurter.dev/v2/rate/BRL/USD');
const data = await response.json();
exchangeRate = data.rate;
rateDisplay.textContent = `1 BRL = ${exchangeRate.toFixed(4)}USD`;
} catch (error) {
rateDisplay.textContent = 'Rate unavailable, using estimate';
console.log('Exchange rate fetch failed:', error);
}
}
buttonReais.addEventListener('click', function(){
currentCurrency = 'R$'
localStorage.setItem('currency', 'R$')
buttonReais.classList.add('active');
buttonDollar.classList.remove('active');
rateDisplay.textContent = `1 BRL = ${exchangeRate.toFixed(4)} USD`;
renderTransactions();
updateSummaryAndChart();
updateTodayDate()
});
buttonDollar.addEventListener('click', function(){
currentCurrency = '$'
localStorage.setItem('currency', '$')
buttonReais.classList.remove('active');
buttonDollar.classList.add('active');
rateDisplay.textContent = `1 BRL = ${exchangeRate.toFixed(4)} USD`;
renderTransactions();
updateSummaryAndChart();
updateTodayDate()
});
sortSelect.addEventListener('change', function() {
renderTransactions();
});
incomeButton.addEventListener('click', function() {
currentType = 'income'
incomeButton.classList.add('active')
expenseButton.classList.remove('active')
});
expenseButton.addEventListener('click', function() {
currentType = 'expense'
incomeButton.classList.remove('active')
expenseButton.classList.add('active')
});
function getSortedTransactions() {
const sorted= [...transactions];
const value= sortSelect.value
switch(value){
case 'newest':
sorted.sort((a,b) => b.date - a.date)
return sorted
case 'oldest':
sorted.sort((a,b) => a.date - b.date)
return sorted
case 'highest':
sorted.sort((a,b) => b.amount - a.amount)
return sorted
case 'lowest':
sorted.sort((a,b) => a.amount - b.amount)
return sorted
}
}
function getDisplayAmount(amount){
if (currentCurrency ==='$'){
return (amount * exchangeRate).toFixed(2)
} return amount.toFixed(2);
}
function renderTransactions() {
transactionsList.innerHTML = ''
if(transactions.length ===0){
transactionsList.innerHTML ='<li>No transactions yet</li>';
return
}
const sorted = getSortedTransactions();
for(let i= 0; i< sorted.length; i++){
const li = document.createElement('li')
const sign = sorted[i].type == 'income' ? '+' : '-';
const date = new Date(sorted[i].date);
const dateFormatted = date.toLocaleDateString('pt-BR', {
month: 'short',
day: 'numeric'
});
li.innerHTML = `
<span class="description ${sorted[i].type}"> ${sorted[i].description}</span>
<span class="date">${dateFormatted}</span>
<span class="amount ${sorted[i].type}"> ${sign}${currentCurrency}${getDisplayAmount(sorted[i].amount)}</span>
<button class="delete-button" onclick="deleteTransaction('${sorted[i].date}')">✕</button>`
transactionsList.appendChild(li);
}
}
function updateSummaryAndChart(){
let totalIncome = 0
let totalExpense = 0
for(let i = 0;i< transactions.length; i++){
if(transactions[i].type == 'income'){
totalIncome += transactions[i].amount;
}else{
totalExpense +=transactions[i].amount;
}
}
const balance = totalIncome - totalExpense;
if ( balance < 0){
balanceDisplay.classList.add("negative");
}else{
balanceDisplay.classList.remove("negative");
}
totalIncomeDisplay.innerHTML = `${currentCurrency}${getDisplayAmount(totalIncome)}`
totalExpenseDisplay.innerHTML = `${currentCurrency}${getDisplayAmount(totalExpense)}`
balanceDisplay.innerHTML = `${currentCurrency}${getDisplayAmount(balance)}`
chart.data.datasets[0].data = [totalIncome, totalExpense];
chart.update()
}
function deleteTransaction(date){
transactions = transactions.filter(t=> t.date !== Number(date))
saveTransactions();
renderTransactions()
updateSummaryAndChart();
}
function saveTransactions(){
localStorage.setItem('transactions', JSON.stringify(transactions));
}
function addTransaction() {
const description = descriptionInput.value.trim()
const amount = parseFloat(parseFloat(amountInput.value).toFixed(2));
if (description === ''|| isNaN(amount) || amount <=0) {
alert('Please enter a valid description and amount')
return
}
const transaction = {
description: description, amount: amount, type: currentType, date: Date.now()
}
transactions.push(transaction);
saveTransactions();
renderTransactions();
updateSummaryAndChart();
descriptionInput.value = '';
amountInput.value = ''
}
const ctx = document.getElementById('budget-chart').getContext('2d');
const chart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['Income', 'Expenses'],
datasets: [{
data: [1.5, 1],
backgroundColor: ['#2fe170', '#e12a2a'],
borderWidth: 0,
hoverOffset: 10,
}]
},
options: {
cutout: '72%',
animation: { animateRotate:true, duration: 2000, easing: 'easeInOutCubic'},
plugins: {
legend: {
position: 'bottom'
}
}
}
})
if(currentCurrency === '$'){
buttonDollar.classList.add('active')
buttonReais.classList.remove('active')
}
function exportToCSV() {
if (transactions.length === 0) {
alert('No transactions to export')
return
}
const header ='Description,Amount,Type,Date';
const rows = [];
for (let i =0; i < transactions.length;i++) {
const t = transactions[i];
const description = t.description
const amount = getDisplayAmount(t.amount)
const type = t.type
const date = new Date(t.date).toLocaleDateString('pt-BR', { month: 'short',
day: 'numeric',
year: 'numeric'
});
rows.push(`${description},${amount},${type},${date}`);
}
const csvContent = header + '\n' + rows.join('\n')
const blob = new Blob([csvContent], { type: 'text/csv' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a');
a.href = url;
a.download = `budgetpro-transactions-${new Date().toLocaleDateString('pt-BR')}.csv`;
a.click()
URL.revokeObjectURL(url);
}
exportCSV.addEventListener('click', exportToCSV);
addButton.addEventListener('click' , addTransaction)
renderTransactions()
updateSummaryAndChart()
fetchExchangeRate()