Fix expense and budget calculations
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Daily Expense Manager</title><style>* {box-sizing: border-box;margin: 0;padding: 0;font-family: Arial, sans-serif;}body {background: #f4f6f8;color: #222;padding: 20px;}.container {max-width: 1100px;margin: auto;}header {background: #2563eb;color: white;padding: 25px;border-radius: 15px;text-align: center;margin-bottom: 20px;}header h1 {margin-bottom: 8px;}.form-box,.budget-box,.history-box,.insight-box {background: white;padding: 20px;border-radius: 15px;margin-bottom: 20px;box-shadow: 0 3px 12px rgba(0,0,0,0.08);}h2 {margin-bottom: 15px;color: #1e3a8a;}.form-grid {display: grid;grid-template-columns: repeat(2, 1fr);gap: 15px;}label {font-weight: bold;display: block;margin-bottom: 6px;}input,select,textarea {width: 100%;padding: 11px;border: 1px solid #ccc;border-radius: 8px;font-size: 15px;}textarea {resize: vertical;}.full {grid-column: 1 / -1;}button {background: #2563eb;color: white;border: none;padding: 12px 20px;border-radius: 8px;cursor: pointer;font-size: 15px;font-weight: bold;}button:hover {opacity: 0.9;}.dashboard {display: grid;grid-template-columns: repeat(4, 1fr);gap: 15px;margin-bottom: 20px;}.card {background: white;padding: 20px;border-radius: 15px;text-align: center;box-shadow: 0 3px 12px rgba(0,0,0,0.08);}.card h3 {font-size: 15px;color: #555;margin-bottom: 10px;}.amount {font-size: 24px;font-weight: bold;color: #2563eb;}.budget-row {display: flex;gap: 10px;}.budget-row input {flex: 1;}.budget-info {margin-top: 15px;font-size: 17px;line-height: 1.8;}.table-container {overflow-x: auto;}table {width: 100%;border-collapse: collapse;min-width: 700px;}th,td {padding: 12px;border-bottom: 1px solid #ddd;text-align: left;}th {background: #eff6ff;}.delete-btn {background: #dc2626;padding: 7px 12px;font-size: 13px;}.empty {text-align: center;padding: 20px;color: #777;}.insight {background: #eff6ff;padding: 15px;border-radius: 10px;line-height: 1.7;}.search-box {margin-bottom: 15px;}footer {text-align: center;margin-top: 25px;color: #777;}@media (max-width: 800px) {.dashboard {grid-template-columns: repeat(2, 1fr);}.form-grid {grid-template-columns: 1fr;}.full {grid-column: auto;}}@media (max-width: 500px) {body {padding: 10px;}.dashboard {grid-template-columns: 1fr;}.budget-row {flex-direction: column;}header h1 {font-size: 24px;}}</style></head><body><div class="container"><header><h1>💰 Daily Expense Manager</h1><p>Track and manage your daily expenses easily.</p></header><!-- Dashboard --><div class="dashboard"><div class="card"><h3>💵 Today's Expense</h3><div class="amount" id="todayAmount">₹0</div></div><div class="card"><h3>📅 This Week</h3><div class="amount" id="weekAmount">₹0</div></div><div class="card"><h3>🗓️ This Month</h3><div class="amount" id="monthAmount">₹0</div></div><div class="card"><h3>💰 Total Expenses</h3><div class="amount" id="totalAmount">₹0</div></div></div><!-- Add Expense --><div class="form-box"><h2>➕ Add Expense</h2><div class="form-grid"><div><label>Date</label><input type="date" id="date"></div><div><label>Expense Name</label><input type="text" id="expenseName"placeholder="Example: Groceries"></div><div><label>Amount (₹)</label><input type="number" id="amount"placeholder="Example: 850"min="0"></div><div><label>Category</label><select id="category"><option value="">Select Category</option><option>🍔 Food</option><option>🛒 Shopping</option><option>🚗 Transport</option><option>🏠 Home</option><option>💊 Medical</option><option>🎬 Entertainment</option><option>📱 Bills</option><option>📦 Other</option></select></div><div><label>Payment Method</label><select id="payment"><option value="">Select Payment</option><option>UPI</option><option>Cash</option><option>Debit Card</option><option>Credit Card</option><option>Net Banking</option></select></div><div><label>Description</label><input type="text" id="description"placeholder="Optional"></div><div class="full"><button onclick="addExpense()">➕ Add Expense</button></div></div></div><!-- Budget --><div class="budget-box"><h2>💰 Monthly Budget</h2><div class="budget-row"><input type="number" id="budget"placeholder="Enter monthly budget (₹)"><button onclick="saveBudget()">Save Budget</button></div><div class="budget-info"><p>Budget: <strong id="budgetDisplay">₹0</strong></p><p>Spent: <strong id="budgetSpent">₹0</strong></p><p>Remaining: <strong id="remaining">₹0</strong></p></div></div><!-- History --><div class="history-box"><h2>📋 Expense History</h2><div class="search-box"><input type="text"id="search"placeholder="🔍 Search expense, category or payment..."oninput="displayExpenses()"></div><div class="table-container"><table><thead><tr><th>Date</th><th>Expense</th><th>Amount</th><th>Category</th><th>Payment</th><th>Description</th><th>Action</th></tr></thead><tbody id="expenseTable"></tbody></table></div><div id="emptyMessage" class="empty">No expenses added yet.</div></div><!-- Insights --><div class="insight-box"><h2>🤖 Spending Insights</h2><div class="insight" id="insight">Add some expenses to see your spending insights.</div></div><footer>© 2026 Daily Expense Manager</footer></div><script>// Load saved expenseslet expenses = JSON.parse(localStorage.getItem("expenses")) || [];// Load budgetlet monthlyBudget =Number(localStorage.getItem("monthlyBudget")) || 0;// Set today's date automaticallydocument.getElementById("date").value =new Date().toISOString().split("T")[0];// Add Expensefunction addExpense() {const date = document.getElementById("date").value;const name = document.getElementById("expenseName").value.trim();const amount = Number(document.getElementById("amount").value);const category = document.getElementById("category").value;const payment = document.getElementById("payment").value;const description =document.getElementById("description").value.trim();if (!date || !name || !amount || !category || !payment) {alert("Please fill all required fields.");return;}const expense = {id: Date.now(),date: date,name: name,amount: amount,category: category,payment: payment,description: description};expenses.push(expense);localStorage.setItem("expenses",JSON.stringify(expenses));// Clear formdocument.getElementById("expenseName").value = "";document.getElementById("amount").value = "";document.getElementById("category").value = "";document.getElementById("payment").value = "";document.getElementById("description").value = "";updateApp();alert("Expense added successfully! ✅");}// Delete Expensefunction deleteExpense(id) {if (confirm("Delete this expense?")) {expenses =expenses.filter(expense => expense.id !== id);localStorage.setItem("expenses",JSON.stringify(expenses));updateApp();}}// Save Budgetfunction saveBudget() {const value =Number(document.getElementById("budget").value);if (value <= 0) {alert("Please enter a valid budget.");return;}monthlyBudget = value;localStorage.setItem("monthlyBudget",monthlyBudget);document.getElementById("budget").value = "";updateApp();alert("Budget saved successfully! ✅");}// Format currencyfunction money(value) {return "₹" + value.toLocaleString("en-IN");}// Get today's datefunction getToday() {return new Date().toISOString().split("T")[0];}// Check same monthfunction sameMonth(date) {const d = new Date(date);const now = new Date();return d.getMonth() === now.getMonth()&& d.getFullYear() === now.getFullYear();}// Check this weekfunction sameWeek(date) {const d = new Date(date);const now = new Date();const difference =now.getTime() - d.getTime();const days =difference / (1000 * 60 * 60 * 24);return days >= 0 && days < 7;}// Update Dashboardfunction updateDashboard() {const today = getToday();const todayTotal =expenses.filter(e => e.date === today).reduce((sum, e) => sum + e.amount, 0);const weekTotal =expenses.filter(e => sameWeek(e.date)).reduce((sum, e) => sum + e.amount, 0);const monthTotal =expenses.filter(e => sameMonth(e.date)).reduce((sum, e) => sum + e.amount, 0);const total =expenses.reduce((sum, e) => sum + e.amount, 0);document.getElementById("todayAmount").textContent = money(todayTotal);document.getElementById("weekAmount").textContent = money(weekTotal);document.getElementById("monthAmount").textContent = money(monthTotal);document.getElementById("totalAmount").textContent = money(total);// Budgetdocument.getElementById("budgetDisplay").textContent = money(monthlyBudget);document.getElementById("budgetSpent").textContent = money(monthTotal);const remaining =monthlyBudget - monthTotal;document.getElementById("remaining").textContent = money(remaining);}// Display expensesfunction displayExpenses() {const table =document.getElementById("expenseTable");const search =document.getElementById("search").value.toLowerCase();table.innerHTML = "";const filtered =expenses.filter(e =>e.name.toLowerCase().includes(search) ||e.category.toLowerCase().includes(search) ||e.payment.toLowerCase().includes(search));document.getElementById("emptyMessage").style.display =filtered.length === 0 ? "block" : "none";filtered.slice().reverse().forEach(expense => {const row =document.createElement("tr");row.innerHTML = `<td>${expense.date}</td><td>${expense.name}</td><td>${money(expense.amount)}</td><td>${expense.category}</td><td>${expense.payment}</td><td>${expense.description || "-"}</td><td><buttonclass="delete-btn"onclick="deleteExpense(${expense.id})">Delete</button></td>`;table.appendChild(row);});}// Spending Insightsfunction updateInsights() {const insight =document.getElementById("insight");if (expenses.length === 0) {insight.innerHTML ="Add some expenses to see your spending insights.";return;}const monthExpenses =expenses.filter(e => sameMonth(e.date));if (monthExpenses.length === 0) {insight.innerHTML ="No expenses recorded this month.";return;}// Category totalsconst categories = {};monthExpenses.forEach(e => {if (!categories[e.category]) {categories[e.category] = 0;}categories[e.category] += e.amount;});let highestCategory = "";let highestAmount = 0;for (const category in categories) {if (categories[category] > highestAmount) {highestAmount =categories[category];highestCategory = category;}}const monthTotal =monthExpenses.reduce((sum, e) => sum + e.amount, 0);let message =`💡 You spent the most on <strong>${highestCategory}</strong>this month: <strong>${money(highestAmount)}</strong>.<br><br>📊 Your total spending this month is<strong>${money(monthTotal)}</strong>.`;if (monthlyBudget > 0) {const remaining =monthlyBudget - monthTotal;if (remaining >= 0) {message +=`<br><br>✅ You have<strong>${money(remaining)}</strong>remaining in your budget.`;} else {message +=`<br><br>⚠️ You exceeded your monthly budget by<strong>${money(Math.abs(remaining))}</strong>.`;}}insight.innerHTML = message;}// Update everythingfunction updateApp() {updateDashboard();displayExpenses();updateInsights();}// Start appupdateApp();</script>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Daily Expense Manager</title><style>* {box-sizing: border-box;margin: 0;padding: 0;font-family: Arial, sans-serif;}body {background: #f4f6f8;color: #222;padding: 20px;}.container {max-width: 1100px;margin: auto;}header {background: #2563eb;color: white;padding: 25px;border-radius: 15px;text-align: center;margin-bottom: 20px;}header h1 {margin-bottom: 8px;}.form-box,.budget-box,.history-box,.insight-box {background: white;padding: 20px;border-radius: 15px;margin-bottom: 20px;box-shadow: 0 3px 12px rgba(0,0,0,0.08);}h2 {margin-bottom: 15px;color: #1e3a8a;}.form-grid {display: grid;grid-template-columns: repeat(2, 1fr);gap: 15px;}label {font-weight: bold;display: block;margin-bottom: 6px;}input,select {width: 100%;padding: 11px;border: 1px solid #ccc;border-radius: 8px;font-size: 15px;}.full {grid-column: 1 / -1;}button {background: #2563eb;color: white;border: none;padding: 12px 20px;border-radius: 8px;cursor: pointer;font-size: 15px;font-weight: bold;}button:hover {opacity: 0.9;}.dashboard {display: grid;grid-template-columns: repeat(4, 1fr);gap: 15px;margin-bottom: 20px;}.card {background: white;padding: 20px;border-radius: 15px;text-align: center;box-shadow: 0 3px 12px rgba(0,0,0,0.08);}.card h3 {font-size: 15px;color: #555;margin-bottom: 10px;}.amount {font-size: 24px;font-weight: bold;color: #2563eb;}.budget-row {display: flex;gap: 10px;}.budget-row input {flex: 1;}.budget-info {margin-top: 15px;font-size: 17px;line-height: 1.8;}.table-container {overflow-x: auto;}table {width: 100%;border-collapse: collapse;min-width: 750px;}th,td {padding: 12px;border-bottom: 1px solid #ddd;text-align: left;}th {background: #eff6ff;}.delete-btn {background: #dc2626;padding: 7px 12px;font-size: 13px;}.empty {text-align: center;padding: 20px;color: #777;}.insight {background: #eff6ff;padding: 15px;border-radius: 10px;line-height: 1.7;}.search-box {margin-bottom: 15px;}footer {text-align: center;margin-top: 25px;color: #777;}@media (max-width: 800px) {.dashboard {grid-template-columns: repeat(2, 1fr);}.form-grid {grid-template-columns: 1fr;}.full {grid-column: auto;}}@media (max-width: 500px) {body {padding: 10px;}.dashboard {grid-template-columns: 1fr;}.budget-row {flex-direction: column;}header h1 {font-size: 24px;}}</style></head><body><div class="container"><header><h1>💰 Daily Expense Manager</h1><p>Track and manage your daily expenses easily.</p></header><!-- DASHBOARD --><div class="dashboard"><div class="card"><h3>💵 Today's Expense</h3><div class="amount" id="todayAmount">₹0</div></div><div class="card"><h3>📅 This Week</h3><div class="amount" id="weekAmount">₹0</div></div><div class="card"><h3>🗓️ This Month</h3><div class="amount" id="monthAmount">₹0</div></div><div class="card"><h3>💰 Total Expenses</h3><div class="amount" id="totalAmount">₹0</div></div></div><!-- ADD EXPENSE --><div class="form-box"><h2>➕ Add Expense</h2><div class="form-grid"><div><label>Date</label><input type="date" id="date"></div><div><label>Expense Name</label><inputtype="text"id="expenseName"placeholder="Example: Groceries"></div><div><label>Amount (₹)</label><inputtype="number"id="amount"placeholder="Example: 850"min="1"></div><div><label>Category</label><select id="category"><option value="">Select Category</option><option>🍔 Food</option><option>🛒 Shopping</option><option>🚗 Transport</option><option>🏠 Home</option><option>💊 Medical</option><option>🎬 Entertainment</option><option>📱 Bills</option><option>📦 Other</option></select></div><div><label>Payment Method</label><select id="payment"><option value="">Select Payment</option><option>UPI</option><option>Cash</option><option>Debit Card</option><option>Credit Card</option><option>Net Banking</option></select></div><div><label>Description</label><inputtype="text"id="description"placeholder="Example: Monthly grocery"></div><div class="full"><button onclick="addExpense()">➕ Add Expense</button></div></div></div><!-- BUDGET --><div class="budget-box"><h2>💰 Monthly Budget</h2><div class="budget-row"><inputtype="number"id="budget"placeholder="Enter monthly budget ₹"min="1"><button onclick="saveBudget()">Save Budget</button></div><div class="budget-info"><p>Budget:<strong id="budgetDisplay">₹0</strong></p><p>Spent:<strong id="budgetSpent">₹0</strong></p><p>Remaining:<strong id="remaining">₹0</strong></p></div></div><!-- HISTORY --><div class="history-box"><h2>📋 Expense History</h2><div class="search-box"><inputtype="text"id="search"placeholder="🔍 Search expense, category or payment..."oninput="displayExpenses()"></div><div class="table-container"><table><thead><tr><th>Date</th><th>Expense</th><th>Amount</th><th>Category</th><th>Payment</th><th>Description</th><th>Action</th></tr></thead><tbody id="expenseTable"></tbody></table></div><div id="emptyMessage" class="empty">No expenses added yet.</div></div><!-- INSIGHTS --><div class="insight-box"><h2>🤖 Spending Insights</h2><div class="insight" id="insight">Add some expenses to see your spending insights.</div></div><footer>© 2026 Daily Expense Manager</footer></div><script>/* =========================LOAD SAVED DATA========================= */let expenses =JSON.parse(localStorage.getItem("expenses")) || [];let monthlyBudget =Number(localStorage.getItem("monthlyBudget")) || 0;/* =========================LOCAL DATE========================= */function getLocalDate() {const today = new Date();const year = today.getFullYear();const month =String(today.getMonth() + 1).padStart(2, "0");const day =String(today.getDate()).padStart(2, "0");return `${year}-${month}-${day}`;}/* Set today's date */document.getElementById("date").value =getLocalDate();/* =========================MONEY FORMAT========================= */function money(value) {return "₹" +Number(value).toLocaleString("en-IN");}/* =========================ADD EXPENSE========================= */function addExpense() {const date =document.getElementById("date").value;const name =document.getElementById("expenseName").value.trim();const amount =Number(document.getElementById("amount").value);const category =document.getElementById("category").value;const payment =document.getElementById("payment").value;const description =document.getElementById("description").value.trim();/* Check required fields */if (!date ||!name ||amount <= 0 ||!category ||!payment) {alert("Please fill all required fields.");return;}/* Create expense */const expense = {id: Date.now(),date: date,name: name,amount: amount,category: category,payment: payment,description: description};/* Add to existing expenses */expenses.push(expense);/* Save ALL expenses */localStorage.setItem("expenses",JSON.stringify(expenses));/* Clear fields */document.getElementById("expenseName").value = "";document.getElementById("amount").value = "";document.getElementById("category").value = "";document.getElementById("payment").value = "";document.getElementById("description").value = "";/* Update everything */updateApp();alert("Expense added successfully! ✅");}/* =========================DELETE EXPENSE========================= */function deleteExpense(id) {if (confirm("Are you sure you want to delete this expense?")) {expenses =expenses.filter(expense => expense.id !== id);localStorage.setItem("expenses",JSON.stringify(expenses));updateApp();}}/* =========================SAVE MONTHLY BUDGET========================= */function saveBudget() {const value =Number(document.getElementById("budget").value);if (value <= 0) {alert("Please enter a valid monthly budget.");return;}monthlyBudget = value;localStorage.setItem("monthlyBudget",monthlyBudget);document.getElementById("budget").value = "";updateApp();alert("Monthly budget saved successfully! ✅");}/* =========================CHECK CURRENT MONTH========================= */function sameMonth(dateString) {const parts =dateString.split("-");const year =Number(parts[0]);const month =Number(parts[1]);const today =new Date();return (year === today.getFullYear() &&month === today.getMonth() + 1);}/* =========================CHECK CURRENT WEEKMonday - Sunday========================= */function sameWeek(dateString) {const parts =dateString.split("-");const expenseDate =new Date(Number(parts[0]),Number(parts[1]) - 1,Number(parts[2]));const today =new Date();today.setHours(0, 0, 0, 0);expenseDate.setHours(0, 0, 0, 0);/* Find Monday */const day =today.getDay();const difference =day === 0 ? 6 : day - 1;const weekStart =new Date(today);weekStart.setDate(today.getDate() - difference);weekStart.setHours(0, 0, 0, 0);/* Find Sunday */const weekEnd =new Date(weekStart);weekEnd.setDate(weekStart.getDate() + 6);weekEnd.setHours(23, 59, 59, 999);return (expenseDate >= weekStart &&expenseDate <= weekEnd);}/* =========================UPDATE DASHBOARD========================= */function updateDashboard() {const today =getLocalDate();/* TODAY */const todayTotal =expenses.filter(expense =>expense.date === today).reduce((total, expense) =>total + Number(expense.amount),0);/* THIS WEEK */const weekTotal =expenses.filter(expense =>sameWeek(expense.date)).reduce((total, expense) =>total + Number(expense.amount),0);/* THIS MONTH */const monthTotal =expenses.filter(expense =>sameMonth(expense.date)).reduce((total, expense) =>total + Number(expense.amount),0);/* ALL EXPENSES */const total =expenses.reduce((sum, expense) =>sum + Number(expense.amount),0);/* Show dashboard */document.getElementById("todayAmount").textContent =money(todayTotal);document.getElementById("weekAmount").textContent =money(weekTotal);document.getElementById("monthAmount").textContent =money(monthTotal);document.getElementById("totalAmount").textContent =money(total);/* =====================BUDGET===================== *//*IMPORTANT:All current-month expensesare included in budget.*/const spent =monthTotal;const remaining =monthlyBudget - spent;document.getElementById("budgetDisplay").textContent =money(monthlyBudget);document.getElementById("budgetSpent").textContent =money(spent);document.getElementById("remaining").textContent =money(remaining);}/* =========================EXPENSE HISTORY========================= */function displayExpenses() {const table =document.getElementById("expenseTable");const search =document.getElementById("search").value.toLowerCase().trim();table.innerHTML = "";const filtered =expenses.filter(expense =>expense.name.toLowerCase().includes(search)||expense.category.toLowerCase().includes(search)||expense.payment.toLowerCase().includes(search)||expense.date.includes(search));document.getElementById("emptyMessage").style.display =filtered.length === 0? "block": "none";/* Newest first */filtered.slice().reverse().forEach(expense => {const row =document.createElement("tr");row.innerHTML = `<td>${expense.date}</td><td>${expense.name}</td><td>${money(expense.amount)}</td><td>${expense.category}</td><td>${expense.payment}</td><td>${expense.description || "-"}</td><td><buttonclass="delete-btn"onclick="deleteExpense(${expense.id})">Delete</button></td>`;table.appendChild(row);});}/* =========================SPENDING INSIGHTS========================= */function updateInsights() {const insight =document.getElementById("insight");const monthExpenses =expenses.filter(expense =>sameMonth(expense.date));if (monthExpenses.length === 0) {insight.innerHTML ="Add some expenses this month to see your spending insights.";return;}/* Category totals */const categories = {};monthExpenses.forEach(expense => {if (!categories[expense.category]) {categories[expense.category] = 0;}categories[expense.category] +=Number(expense.amount);});let highestCategory = "";let highestAmount = 0;for (const category in categories) {if (categories[category] >highestAmount) {highestAmount =categories[category];highestCategory =category;}}const monthTotal =monthExpenses.reduce((sum, expense) =>sum + Number(expense.amount),0);let message = `💡 You spent the most on<strong>${highestCategory}</strong>this month:<strong>${money(highestAmount)}</strong><br><br>📊 Your total spending this month is<strong>${money(monthTotal)}</strong>.`;/* Budget message */if (monthlyBudget > 0) {const remaining =monthlyBudget - monthTotal;if (remaining >= 0) {message += `<br><br>✅ You have<strong>${money(remaining)}</strong>remaining in your monthly budget.`;} else {message += `<br><br>⚠️ You exceeded your monthly budget by<strong>${money(Math.abs(remaining))}</strong>.`;}}insight.innerHTML = message;}/* =========================UPDATE ENTIRE APP========================= */function updateApp() {updateDashboard();displayExpenses();updateInsights();}/* =========================START APP========================= */updateApp();</script>
index.html+993-564