DailyExpenseManager / index.html
Ragulselvakumar's picture
Fix expense and budget calculations
4b16792 verified
Raw History Blame Contribute Delete
18.6 kB
<!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> <input type="text" id="expenseName" placeholder="Example: Groceries"> </div> <div> <label>Amount (₹)</label> <input type="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> <input type="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"> <input type="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"> <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 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 WEEK Monday - 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 expenses are 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> <button class="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> </body> </html>
<!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;