226 lines
11 KiB
PHP
226 lines
11 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
|
|
|
|
|
|
$pdo = db();
|
|
$message = null;
|
|
$message_type = '';
|
|
|
|
if (!$pdo) {
|
|
$message = "Database connection failed. Please check configuration.";
|
|
$message_type = 'danger';
|
|
} else {
|
|
// Handle Delete
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['delete'])) {
|
|
$id_to_delete = filter_input(INPUT_GET, 'delete', FILTER_VALIDATE_INT);
|
|
if ($id_to_delete) {
|
|
try {
|
|
$stmt = $pdo->prepare("DELETE FROM expenses WHERE id = ?");
|
|
$stmt->execute([$id_to_delete]);
|
|
$_SESSION['message'] = 'Expense deleted successfully.';
|
|
$_SESSION['message_type'] = 'success';
|
|
} catch (PDOException $e) {
|
|
$_SESSION['message'] = 'Error deleting expense: ' . $e->getMessage();
|
|
$_SESSION['message_type'] = 'danger';
|
|
}
|
|
}
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
|
|
// Handle Add
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_expense'])) {
|
|
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_FLOAT);
|
|
$category_id = filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
|
|
$payment_method = htmlspecialchars(trim($_POST['payment_method']));
|
|
$expense_date = $_POST['expense_date']; // Basic validation, consider more robust date validation
|
|
$notes = htmlspecialchars(trim($_POST['notes']));
|
|
|
|
if ($amount && $category_id && !empty($payment_method) && !empty($expense_date)) {
|
|
try {
|
|
$stmt = $pdo->prepare("INSERT INTO expenses (amount, category_id, payment_method, expense_date, notes) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([$amount, $category_id, $payment_method, $expense_date, $notes]);
|
|
$_SESSION['message'] = 'Expense added successfully!';
|
|
$_SESSION['message_type'] = 'success';
|
|
} catch (PDOException $e) {
|
|
$_SESSION['message'] = 'Error adding expense: ' . $e->getMessage();
|
|
$_SESSION['message_type'] = 'danger';
|
|
}
|
|
} else {
|
|
$_SESSION['message'] = 'Please fill all required fields correctly.';
|
|
$_SESSION['message_type'] = 'warning';
|
|
}
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
|
|
// Flash message handling
|
|
if (isset($_SESSION['message'])) {
|
|
$message = $_SESSION['message'];
|
|
$message_type = $_SESSION['message_type'];
|
|
unset($_SESSION['message']);
|
|
unset($_SESSION['message_type']);
|
|
}
|
|
|
|
// Fetch data for display
|
|
$categories = [];
|
|
$expenses = [];
|
|
if ($pdo) {
|
|
try {
|
|
$categories = $pdo->query("SELECT * FROM categories ORDER BY name ASC")->fetchAll();
|
|
$expenses = $pdo->query("SELECT e.*, c.name as category_name FROM expenses e JOIN categories c ON e.category_id = c.id ORDER BY e.expense_date DESC, e.id DESC LIMIT 20")->fetchAll();
|
|
} catch (PDOException $e) {
|
|
$message = "Error fetching data: " . $e->getMessage();
|
|
$message_type = 'danger';
|
|
}
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Personal Expense Tracker</title>
|
|
<meta name="description" content="A personal expense tracker web application built with Flatlogic.">
|
|
<meta name="keywords" content="expense tracker, budget, finance, personal finance, money management, flatlogic generator">
|
|
<meta property="og:title" content="Personal Expense Tracker">
|
|
<meta property="og:description" content="A personal expense tracker web application built with Flatlogic.">
|
|
<meta property="og:image" content="">
|
|
<meta name="twitter:card" content="summary_large_image">
|
|
<meta name="twitter:image" content="">
|
|
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
|
|
<?php if ($message): ?>
|
|
<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 11">
|
|
<div id="liveToast" class="toast show" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<div class="toast-header bg-<?php echo $message_type; ?> text-white">
|
|
<strong class="me-auto"><?php echo $message_type === 'success' ? 'Success' : 'Notice'; ?></strong>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
</div>
|
|
<div class="toast-body">
|
|
<?php echo htmlspecialchars($message); ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<header class="p-4 mb-4 gradient-header">
|
|
<div class="container">
|
|
<h1 class="h3">Personal Expense Tracker</h1>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="container">
|
|
<div class="row g-4">
|
|
<!-- Add Expense Form -->
|
|
<div class="col-lg-4">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2 class="h5 mb-0">Add New Expense</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<form action="index.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="amount" class="form-label">Amount</label>
|
|
<input type="number" step="0.01" class="form-control" id="amount" name="amount" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="expense_date" class="form-label">Date</label>
|
|
<input type="date" class="form-control" id="expense_date" name="expense_date" value="<?php echo date('Y-m-d'); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="category_id" class="form-label">Category</label>
|
|
<select class="form-select" id="category_id" name="category_id" required>
|
|
<option value="">Select Category</option>
|
|
<?php foreach ($categories as $category): ?>
|
|
<option value="<?php echo $category['id']; ?>"><?php echo htmlspecialchars($category['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="payment_method" class="form-label">Payment Method</label>
|
|
<input type="text" class="form-control" id="payment_method" name="payment_method" required value="Cash">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="notes" class="form-label">Notes</label>
|
|
<textarea class="form-control" id="notes" name="notes" rows="2"></textarea>
|
|
</div>
|
|
<button type="submit" name="add_expense" class="btn btn-primary w-100">
|
|
<i class="bi bi-plus-circle"></i> Add Expense
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Recent Transactions -->
|
|
<div class="col-lg-8">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2 class="h5 mb-0">Recent Transactions</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>Date</th>
|
|
<th>Amount</th>
|
|
<th>Category</th>
|
|
<th>Payment Method</th>
|
|
<th>Notes</th>
|
|
<th>Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($expenses)): ?>
|
|
<tr>
|
|
<td colspan="6" class="text-center">No expenses recorded yet.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($expenses as $expense): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($expense['expense_date']); ?></td>
|
|
<td>$<?php echo htmlspecialchars(number_format($expense['amount'], 2)); ?></td>
|
|
<td><?php echo htmlspecialchars($expense['category_name']); ?></td>
|
|
<td><?php echo htmlspecialchars($expense['payment_method']); ?></td>
|
|
<td><?php echo htmlspecialchars($expense['notes']); ?></td>
|
|
<td>
|
|
<a href="index.php?delete=<?php echo $expense['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this expense?');">
|
|
<i class="bi bi-trash"></i>
|
|
</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<footer class="container mt-4 text-center text-muted">
|
|
<p>© <?php echo date("Y"); ?> Personal Expense Tracker. Built with Flatlogic.</p>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
</body>
|
|
</html>
|