192 lines
7.5 KiB
PHP
192 lines
7.5 KiB
PHP
<?php
|
|
require_once 'includes/auth.php';
|
|
require_login();
|
|
|
|
require_once __DIR__ . '/includes/header.php';
|
|
require_once 'db/config.php';
|
|
|
|
$pdo = db();
|
|
|
|
// Get distinct payment methods for the filter dropdown
|
|
$payment_methods_stmt = $pdo->query("SELECT DISTINCT payment_method FROM sales ORDER BY payment_method");
|
|
$payment_methods = $payment_methods_stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
// Filter logic
|
|
$start_date = $_GET['start_date'] ?? '';
|
|
$end_date = $_GET['end_date'] ?? '';
|
|
$payment_method = $_GET['payment_method'] ?? '';
|
|
|
|
$sql = "SELECT * FROM sales";
|
|
$conditions = [];
|
|
$params = [];
|
|
|
|
if ($start_date) {
|
|
$conditions[] = "sale_date >= ?";
|
|
$params[] = $start_date . ' 00:00:00';
|
|
}
|
|
if ($end_date) {
|
|
$conditions[] = "sale_date <= ?";
|
|
$params[] = $end_date . ' 23:59:59';
|
|
}
|
|
if ($payment_method) {
|
|
$conditions[] = "payment_method = ?";
|
|
$params[] = $payment_method;
|
|
}
|
|
|
|
if (count($conditions) > 0) {
|
|
$sql .= " WHERE " . implode(' AND ', $conditions);
|
|
}
|
|
|
|
$sql .= " ORDER BY sale_date DESC";
|
|
|
|
$sales_stmt = $pdo->prepare($sql);
|
|
$sales_stmt->execute($params);
|
|
$sales = $sales_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
?>
|
|
|
|
<div class="container-fluid">
|
|
<h1 class="mt-4">Sales Reports</h1>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
<i class="fas fa-filter me-1"></i>
|
|
Filter Sales
|
|
</div>
|
|
<div class="card-body">
|
|
<form action="reports.php" method="GET" class="row g-3">
|
|
<div class="col-md-4">
|
|
<label for="start_date" class="form-label">Start Date</label>
|
|
<input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo htmlspecialchars($start_date); ?>">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label for="end_date" class="form-label">End Date</label>
|
|
<input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo htmlspecialchars($end_date); ?>">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label for="payment_method" class="form-label">Payment Method</label>
|
|
<select id="payment_method" name="payment_method" class="form-select">
|
|
<option value="">All</option>
|
|
<?php foreach ($payment_methods as $pm): ?>
|
|
<option value="<?php echo htmlspecialchars($pm); ?>" <?php echo ($payment_method === $pm) ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($pm); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-12">
|
|
<button type="submit" class="btn btn-primary">Filter</button>
|
|
<a href="reports.php" class="btn btn-secondary">Clear</a>
|
|
<a href="_export_sales_report.php?start_date=<?php echo $start_date; ?>&end_date=<?php echo $end_date; ?>&payment_method=<?php echo $payment_method; ?>" class="btn btn-success">Export to CSV</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
<i class="fas fa-table me-1"></i>
|
|
Filtered Sales
|
|
</div>
|
|
<div class="card-body">
|
|
<table id="datatablesSimple" class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Date</th>
|
|
<th>Total Amount</th>
|
|
<th>Payment Method</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (count($sales) > 0): ?>
|
|
<?php foreach ($sales as $sale): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($sale['id']); ?></td>
|
|
<td><?php echo htmlspecialchars($sale['sale_date']); ?></td>
|
|
<td>$<?php echo htmlspecialchars(number_format($sale['total_amount'], 2)); ?></td>
|
|
<td><?php echo htmlspecialchars($sale['payment_method']); ?></td>
|
|
<td>
|
|
<button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#saleItemsModal" data-sale-id="<?php echo $sale['id']; ?>">
|
|
View Items
|
|
</button>
|
|
<a href="receipt.php?sale_id=<?php echo $sale['id']; ?>" target="_blank" class="btn btn-sm btn-secondary">
|
|
<i class="fas fa-print"></i> Receipt
|
|
</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php else: ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center">No sales found matching your criteria.</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal -->
|
|
<div class="modal fade" id="saleItemsModal" tabindex="-1" aria-labelledby="saleItemsModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-lg">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="saleItemsModalLabel">Sale Items</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Product</th>
|
|
<th>Quantity</th>
|
|
<th>Price</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="saleItemsTbody">
|
|
<!-- Items will be loaded here via JavaScript -->
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
const saleItemsModal = document.getElementById('saleItemsModal');
|
|
saleItemsModal.addEventListener('show.bs.modal', function (event) {
|
|
const button = event.relatedTarget;
|
|
const saleId = button.getAttribute('data-sale-id');
|
|
const modalBody = saleItemsModal.querySelector('#saleItemsTbody');
|
|
modalBody.innerHTML = '<tr><td colspan="3">Loading...</td></tr>';
|
|
|
|
fetch('_get_sale_items.php?sale_id=' + saleId)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
let html = '';
|
|
if(data.error){
|
|
html = `<tr><td colspan="3">${data.error}</td></tr>`;
|
|
} else {
|
|
data.forEach(item => {
|
|
html += `
|
|
<tr>
|
|
<td>${item.product_name}</td>
|
|
<td>${item.quantity}</td>
|
|
<td>$${parseFloat(item.price).toFixed(2)}</td>
|
|
</tr>
|
|
`;
|
|
});
|
|
}
|
|
modalBody.innerHTML = html;
|
|
})
|
|
.catch(error => {
|
|
console.error('Error fetching sale items:', error);
|
|
modalBody.innerHTML = '<tr><td colspan="3">Error loading items.</td></tr>';
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|