148 lines
6.6 KiB
PHP
148 lines
6.6 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/header.php';
|
|
|
|
// Check permission
|
|
if (function_exists('require_permission')) {
|
|
require_permission('reports_view');
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
// Date and Outlet Filter
|
|
$startDate = $_GET['start_date'] ?? date('Y-m-d', strtotime('-7 days'));
|
|
$endDate = $_GET['end_date'] ?? date('Y-m-d');
|
|
$outletId = $_GET['outlet_id'] ?? '';
|
|
|
|
// Fetch Outlets for filter
|
|
$outletsStmt = $pdo->query("SELECT id, name, name_ar FROM outlets WHERE is_deleted = 0 ORDER BY name ASC");
|
|
$allOutlets = $outletsStmt->fetchAll();
|
|
|
|
// Base query additions
|
|
$outletCondition = "";
|
|
$queryParams = [$startDate, $endDate];
|
|
if (!empty($outletId)) {
|
|
$outletCondition = " AND o.outlet_id = ? ";
|
|
$queryParams[] = $outletId;
|
|
}
|
|
|
|
// Product Sales & Profit
|
|
$productSalesStmt = $pdo->prepare("
|
|
SELECT
|
|
p.name as product_name,
|
|
p.name_ar as product_name_ar,
|
|
SUM(oi.quantity) as qty_sold,
|
|
SUM(oi.quantity * oi.unit_price) as total_amount,
|
|
SUM(oi.quantity * (oi.unit_price - IFNULL(p.cost_price, 0))) as total_profit
|
|
FROM order_items oi
|
|
JOIN orders o ON oi.order_id = o.id
|
|
JOIN products p ON oi.product_id = p.id
|
|
WHERE DATE(o.created_at) BETWEEN ? AND ?
|
|
AND o.status != 'cancelled'
|
|
$outletCondition
|
|
GROUP BY p.id
|
|
ORDER BY qty_sold DESC
|
|
");
|
|
$productSalesStmt->execute($queryParams);
|
|
$productSales = $productSalesStmt->fetchAll();
|
|
|
|
// Totals for the footer
|
|
$totalQty = 0;
|
|
$totalAmount = 0;
|
|
$totalProfit = 0;
|
|
foreach ($productSales as $p) {
|
|
$totalQty += $p['qty_sold'];
|
|
$totalAmount += $p['total_amount'];
|
|
$totalProfit += $p['total_profit'];
|
|
}
|
|
?>
|
|
|
|
<div class="container-fluid p-0">
|
|
<div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-3">
|
|
<div>
|
|
<h2 class="fw-bold mb-1">Product Sales Report</h2>
|
|
<p class="text-muted mb-0">Detailed breakdown of product performance and profitability</p>
|
|
</div>
|
|
<form class="row g-2 align-items-center" method="GET">
|
|
<div class="col-auto">
|
|
<select name="outlet_id" class="form-select" style="min-width: 180px;">
|
|
<option value="">All Outlets</option>
|
|
<?php foreach ($allOutlets as $o): ?>
|
|
<option value="<?= $o['id'] ?>" <?= $outletId == $o['id'] ? 'selected' : '' ?>>
|
|
<?= htmlspecialchars($o['name']) ?> <?= $o['name_ar'] ? '('.$o['name_ar'].')' : '' ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-auto">
|
|
<div class="input-group">
|
|
<span class="input-group-text bg-white border-end-0 text-muted"><i class="bi bi-calendar"></i></span>
|
|
<input type="date" name="start_date" class="form-control border-start-0" value="<?= htmlspecialchars($startDate) ?>">
|
|
</div>
|
|
</div>
|
|
<div class="col-auto">
|
|
<div class="input-group">
|
|
<span class="input-group-text bg-white border-end-0 text-muted"><i class="bi bi-calendar"></i></span>
|
|
<input type="date" name="end_date" class="form-control border-start-0" value="<?= htmlspecialchars($endDate) ?>">
|
|
</div>
|
|
</div>
|
|
<div class="col-auto">
|
|
<div class="d-flex gap-2">
|
|
<button type="submit" class="btn btn-primary px-4 text-nowrap">Filter</button>
|
|
<a href="report_products.php" class="btn btn-light text-nowrap">Reset</a>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-header bg-white py-3 border-0 d-flex justify-content-between align-items-center">
|
|
<h5 class="fw-bold mb-0"><i class="bi bi-box-seam me-2 text-dark"></i> Products</h5>
|
|
<button onclick="window.print()" class="btn btn-sm btn-outline-secondary"><i class="bi bi-printer me-1"></i> Print</button>
|
|
</div>
|
|
<div class="card-body p-0">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead class="bg-light">
|
|
<tr>
|
|
<th class="ps-3">Product Name</th>
|
|
<th class="text-center">Qty Sold</th>
|
|
<th class="text-end">Total Amount</th>
|
|
<th class="text-end pe-3">Estimated Profit</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($productSales)): ?>
|
|
<tr><td colspan="4" class="text-center py-5 text-muted">No sales found for the selected criteria</td></tr>
|
|
<?php else: ?>
|
|
<?php foreach ($productSales as $prod): ?>
|
|
<tr>
|
|
<td class="ps-3">
|
|
<div class="fw-bold text-dark"><?= htmlspecialchars($prod['product_name']) ?></div>
|
|
<?php if ($prod['product_name_ar']): ?>
|
|
<small class="text-muted d-block"><?= htmlspecialchars($prod['product_name_ar']) ?></small>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="text-center"><?= number_format($prod['qty_sold']) ?></td>
|
|
<td class="text-end fw-bold"><?= format_currency($prod['total_amount']) ?></td>
|
|
<td class="text-end pe-3 fw-bold text-success"><?= format_currency($prod['total_profit']) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
<?php if (!empty($productSales)): ?>
|
|
<tfoot class="bg-light fw-bold">
|
|
<tr>
|
|
<td class="ps-3 text-uppercase">Total</td>
|
|
<td class="text-center"><?= number_format($totalQty) ?></td>
|
|
<td class="text-end"><?= format_currency($totalAmount) ?></td>
|
|
<td class="text-end pe-3 text-success"><?= format_currency($totalProfit) ?></td>
|
|
</tr>
|
|
</tfoot>
|
|
<?php endif; ?>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|