|
1 | 1 | import { ExpenseService } from './localStorage/expenseLocal.js'; |
2 | 2 | import { CategoryService } from './localStorage/categoryLocal.js'; |
| 3 | +import { BudgetService } from './localStorage/budgetLocal.js'; |
3 | 4 |
|
4 | 5 | let charts = {}; |
5 | 6 |
|
6 | 7 | function loadDashboardData() { |
| 8 | + const expenses = ExpenseService.getAll(); |
| 9 | + const categories = CategoryService.getAll(); |
| 10 | + |
| 11 | + // Budget Section |
| 12 | + const budgets = BudgetService.getAll(); |
| 13 | + const budgetSection = document.getElementById("budgetSection"); |
| 14 | + const budgetBar = document.getElementById("budgetProgressBar"); |
| 15 | + const budgetText = document.getElementById("budgetStatusText"); |
| 16 | + |
| 17 | + if (budgets.length > 0) { |
| 18 | + const latestBudget = budgets[budgets.length - 1]; |
| 19 | + const totalBudget = parseFloat(latestBudget.amount); |
| 20 | + const fromDate = new Date(latestBudget.fromDate); |
| 21 | + const toDate = new Date(latestBudget.toDate); |
| 22 | + |
| 23 | + const expensesInBudgetRange = expenses.filter(exp => { |
| 24 | + const expDate = new Date(exp.date); |
| 25 | + return expDate >= fromDate && expDate <= toDate; |
| 26 | + }); |
| 27 | + |
| 28 | + const spent = expensesInBudgetRange.reduce((acc, curr) => acc + parseFloat(curr.amount), 0); |
| 29 | + const percentage = Math.min(((spent / totalBudget) * 100), 100).toFixed(1); |
| 30 | + |
| 31 | + budgetBar.style.width = `${percentage}%`; |
| 32 | + budgetBar.innerText = `${percentage}%`; |
| 33 | + |
| 34 | + budgetText.innerText = `You have spent ₹${spent.toFixed(2)} out of ₹${totalBudget.toFixed(2)} between ${latestBudget.fromDate} and ${latestBudget.toDate}.`; |
| 35 | + |
| 36 | + // ✅ Show the section |
| 37 | + budgetSection.classList.remove("hidden"); |
| 38 | + |
| 39 | + } else { |
| 40 | + // ❌ Hide the section |
| 41 | + budgetSection.classList.add("hidden"); |
| 42 | + } |
| 43 | + |
| 44 | + |
7 | 45 | try { |
8 | | - const expenses = ExpenseService.getAll(); |
9 | | - const categories = CategoryService.getAll(); |
| 46 | + |
10 | 47 | const categoryMap = {}; |
11 | 48 | categories.forEach((cat) => { |
12 | 49 | categoryMap[cat.category_id] = cat.name; |
|
0 commit comments