172 lines
8.1 KiB
PHP
172 lines
8.1 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;
|
|
}
|
|
|
|
// Staff Sales by Payment Type
|
|
$staffPaymentStmt = $pdo->prepare("
|
|
SELECT
|
|
u.full_name as staff_name,
|
|
u.full_name_ar as staff_name_ar,
|
|
pt.name as payment_name,
|
|
SUM(o.total_amount) as total_amount
|
|
FROM orders o
|
|
JOIN users u ON o.user_id = u.id
|
|
LEFT JOIN payment_types pt ON o.payment_type_id = pt.id
|
|
WHERE DATE(o.created_at) BETWEEN ? AND ?
|
|
AND o.status != 'cancelled'
|
|
$outletCondition
|
|
GROUP BY o.user_id, o.payment_type_id
|
|
ORDER BY u.full_name ASC, total_amount DESC
|
|
");
|
|
$staffPaymentStmt->execute($queryParams);
|
|
$staffPaymentSales = $staffPaymentStmt->fetchAll();
|
|
|
|
// Summary by payment type
|
|
$paymentSummary = [];
|
|
$grandTotal = 0;
|
|
foreach ($staffPaymentSales as $row) {
|
|
$pName = $row['payment_name'] ?? 'N/A';
|
|
if (!isset($paymentSummary[$pName])) {
|
|
$paymentSummary[$pName] = 0;
|
|
}
|
|
$paymentSummary[$pName] += $row['total_amount'];
|
|
$grandTotal += $row['total_amount'];
|
|
}
|
|
?>
|
|
|
|
<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">Staff Sales Report</h2>
|
|
<p class="text-muted mb-0">Sales breakdown by staff and payment method</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_staff.php" class="btn btn-light text-nowrap">Reset</a>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="row g-4">
|
|
<div class="col-lg-8">
|
|
<div class="card border-0 shadow-sm h-100">
|
|
<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-person-badge me-2 text-primary"></i> Detailed Staff Sales</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">Staff Name</th>
|
|
<th>Payment Type</th>
|
|
<th class="text-end pe-3">Total Amount</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($staffPaymentSales)): ?>
|
|
<tr><td colspan="3" class="text-center py-5 text-muted">No sales found for the selected criteria</td></tr>
|
|
<?php else: ?>
|
|
<?php
|
|
$currentStaff = '';
|
|
foreach ($staffPaymentSales as $row):
|
|
$showStaff = ($currentStaff != $row['staff_name']);
|
|
$currentStaff = $row['staff_name'];
|
|
?>
|
|
<tr class="<?= $showStaff ? 'border-top' : '' ?>">
|
|
<td class="ps-3">
|
|
<?php if ($showStaff): ?>
|
|
<div class="fw-bold text-dark"><?= htmlspecialchars($row['staff_name']) ?></div>
|
|
<?php if ($row['staff_name_ar']): ?>
|
|
<small class="text-muted"><?= htmlspecialchars($row['staff_name_ar']) ?></small>
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<span class="badge bg-light text-dark border"><?= htmlspecialchars($row['payment_name'] ?? 'N/A') ?></span>
|
|
</td>
|
|
<td class="text-end pe-3 fw-bold"><?= format_currency($row['total_amount']) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-lg-4">
|
|
<div class="card border-0 shadow-sm mb-4">
|
|
<div class="card-header bg-white py-3 border-0">
|
|
<h5 class="fw-bold mb-0"><i class="bi bi-pie-chart me-2 text-success"></i> Payment Summary</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<ul class="list-group list-group-flush">
|
|
<?php foreach ($paymentSummary as $pName => $amount): ?>
|
|
<li class="list-group-item d-flex justify-content-between align-items-center px-0">
|
|
<span><?= htmlspecialchars($pName) ?></span>
|
|
<span class="fw-bold"><?= format_currency($amount) ?></span>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
<li class="list-group-item d-flex justify-content-between align-items-center px-0 bg-light mt-2 p-2 rounded">
|
|
<span class="fw-bold">Grand Total</span>
|
|
<span class="fw-bold text-primary fs-5"><?= format_currency($grandTotal) ?></span>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|