162 lines
7.9 KiB
PHP
162 lines
7.9 KiB
PHP
<?php
|
|
include 'includes/header.php';
|
|
|
|
$message = '';
|
|
|
|
// Handle form submission for adding a new expense
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_expense'])) {
|
|
$driver_id = $_POST['driver_id'] ?? null;
|
|
$vehicle_id = $_POST['vehicle_id'] ?? null;
|
|
$expense_date = $_POST['expense_date'] ?? '';
|
|
$category = $_POST['category'] ?? '';
|
|
$amount = $_POST['amount'] ?? '';
|
|
$description = $_POST['description'] ?? '';
|
|
|
|
if ($driver_id && $expense_date && $category && $amount) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare(
|
|
'INSERT INTO expenses (driver_id, vehicle_id, expense_date, category, amount, description) VALUES (?, ?, ?, ?, ?, ?)'
|
|
);
|
|
$stmt->execute([$driver_id, $vehicle_id, $expense_date, $category, $amount, $description]);
|
|
$message = '<div class="alert alert-success">Expense added successfully!</div>';
|
|
} catch (PDOException $e) {
|
|
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
|
}
|
|
} else {
|
|
$message = '<div class="alert alert-warning">Please fill in all required fields.</div>';
|
|
}
|
|
}
|
|
|
|
// Fetch all expenses from the database
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->query('SELECT e.*, d.name as driver_name, v.make, v.model, v.license_plate FROM expenses e LEFT JOIN drivers d ON e.driver_id = d.id LEFT JOIN vehicles v ON e.vehicle_id = v.id ORDER BY e.expense_date DESC');
|
|
$expenses = $stmt->fetchAll();
|
|
|
|
// Fetch drivers and vehicles for the form
|
|
$drivers_stmt = $pdo->query('SELECT id, name FROM drivers ORDER BY name');
|
|
$drivers = $drivers_stmt->fetchAll();
|
|
$vehicles_stmt = $pdo->query('SELECT id, make, model, license_plate FROM vehicles ORDER BY make, model');
|
|
$vehicles = $vehicles_stmt->fetchAll();
|
|
|
|
} catch (PDOException $e) {
|
|
$expenses = [];
|
|
$drivers = [];
|
|
$vehicles = [];
|
|
$message .= '<div class="alert alert-danger">Error fetching data: ' . $e->getMessage() . '</div>';
|
|
}
|
|
|
|
include 'includes/sidebar.php';
|
|
?>
|
|
|
|
<div class="container-fluid">
|
|
<h2 class="mb-4">Expense Management</h2>
|
|
|
|
<?php echo $message; ?>
|
|
|
|
<div class="card p-3 mb-4">
|
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
<h5 class="mb-0">All Expenses</h5>
|
|
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addExpenseModal">+ Add New Expense</button>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>Date</th>
|
|
<th>Driver</th>
|
|
<th>Vehicle</th>
|
|
<th>Category</th>
|
|
<th>Amount</th>
|
|
<th>Description</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($expenses)): ?>
|
|
<tr>
|
|
<td colspan="7" class="text-center">No expenses found.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($expenses as $expense): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($expense['expense_date']); ?></td>
|
|
<td><?php echo htmlspecialchars($expense['driver_name'] ?? 'N/A'); ?></td>
|
|
<td><?php echo htmlspecialchars(($expense['make'] ?? 'N/A') . ' ' . ($expense['model'] ?? '') . ' (' . ($expense['license_plate'] ?? '') . ')'); ?></td>
|
|
<td><?php echo htmlspecialchars($expense['category']); ?></td>
|
|
<td>$<?php echo htmlspecialchars(number_format($expense['amount'], 2)); ?></td>
|
|
<td><?php echo htmlspecialchars($expense['description']); ?></td>
|
|
<td>
|
|
<a href="#" class="btn btn-sm btn-outline-primary"><i data-feather="edit-2"></i></a>
|
|
<a href="#" class="btn btn-sm btn-outline-danger"><i data-feather="trash-2"></i></a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Add Expense Modal -->
|
|
<div class="modal fade" id="addExpenseModal" tabindex="-1" aria-labelledby="addExpenseModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="addExpenseModalLabel">Add New Expense</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<form action="expenses.php" method="POST">
|
|
<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" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="driver_id" class="form-label">Driver</label>
|
|
<select class="form-select" id="driver_id" name="driver_id" required>
|
|
<option value="">Select Driver</option>
|
|
<?php foreach ($drivers as $driver): ?>
|
|
<option value="<?php echo $driver['id']; ?>"><?php echo htmlspecialchars($driver['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="vehicle_id" class="form-label">Vehicle (Optional)</label>
|
|
<select class="form-select" id="vehicle_id" name="vehicle_id">
|
|
<option value="">Select Vehicle</option>
|
|
<?php foreach ($vehicles as $vehicle): ?>
|
|
<option value="<?php echo $vehicle['id']; ?>"><?php echo htmlspecialchars($vehicle['make'] . ' ' . $vehicle['model'] . ' (' . $vehicle['license_plate'] . ')'); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="category" class="form-label">Category</label>
|
|
<select class="form-select" id="category" name="category" required>
|
|
<option value="">Select Category</option>
|
|
<option value="Fuel">Fuel</option>
|
|
<option value="Maintenance">Maintenance</option>
|
|
<option value="Tolls">Tolls</option>
|
|
<option value="Food">Food</option>
|
|
<option value="Other">Other</option>
|
|
</select>
|
|
</div>
|
|
<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="description" class="form-label">Description (Optional)</label>
|
|
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
|
|
</div>
|
|
<button type="submit" name="add_expense" class="btn btn-primary">Add Expense</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|