39728-vm/online_orders.php
2026-04-20 02:38:20 +00:00

248 lines
14 KiB
PHP

<?php
require_once __DIR__ . '/includes/app.php';
$user = require_permission('sales', 'show'); // or create a specific permission
$pageTitle = tr('طلبات المتجر', 'Online Orders');
$activeNav = 'online_orders';
$db = db();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
if ($_POST['action'] === 'update_status') {
$id = (int)$_POST['id'];
$status = $_POST['status']; // pending, accepted, completed, rejected
$stmt = $db->prepare("UPDATE online_orders SET status = ? WHERE id = ?");
$stmt->execute([$status, $id]);
set_flash('success', tr('تم تحديث حالة الطلب', 'Order status updated'));
redirect_to('online_orders.php');
} elseif ($_POST['action'] === 'delete') {
$id = (int)$_POST['id'];
$stmt = $db->prepare("DELETE FROM online_orders WHERE id = ?");
$stmt->execute([$id]);
set_flash('success', tr('تم حذف الطلب بنجاح', 'Order deleted successfully'));
redirect_to('online_orders.php');
}
}
$search = $_GET['search'] ?? '';
$date_from = $_GET['date_from'] ?? date('Y-m-d', strtotime('-30 days'));
$date_to = $_GET['date_to'] ?? date('Y-m-d');
$query = "SELECT * FROM online_orders WHERE DATE(created_at) >= ? AND DATE(created_at) <= ?";
$params = [$date_from, $date_to];
if ($search !== '') {
$query .= " AND (customer_name LIKE ? OR customer_phone LIKE ?)";
$params[] = "%$search%";
$params[] = "%$search%";
}
$query .= " ORDER BY created_at DESC";
$stmt = $db->prepare($query);
$stmt->execute($params);
$orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
require __DIR__ . '/includes/header.php';
?>
<style>
@media print {
.d-print-none { display: none !important; }
body { background: #fff !important; }
.card { border: none !important; box-shadow: none !important; }
.table { width: 100% !important; }
.table th, .table td { border-bottom: 1px solid #dee2e6 !important; }
/* Hide specific columns on print if needed */
.print-hide { display: none !important; }
}
</style>
<div class="row align-items-center mb-4 d-print-none">
<div class="col">
<h3 class="h5 mb-0 fw-bold"><i class="bi bi-cart-check me-2"></i><?= h(tr('طلبات المتجر الإلكتروني', 'Online Store Orders')) ?></h3>
</div>
</div>
<div class="d-none d-print-block mb-4 text-center">
<h2><?= h(tr('تقرير طلبات المتجر', 'Online Orders Report')) ?></h2>
<p><?= h($date_from) ?> - <?= h($date_to) ?></p>
</div>
<form method="get" class="row g-3 mb-4 align-items-end d-print-none bg-light p-3 rounded-4 shadow-sm">
<div class="col-md-4">
<label class="form-label text-muted small"><?= h(tr('بحث', 'Search')) ?></label>
<input type="text" name="search" class="form-control" placeholder="<?= h(tr('الاسم أو الهاتف', 'Name or Phone')) ?>" value="<?= h($search) ?>">
</div>
<div class="col-md-3">
<label class="form-label text-muted small"><?= h(tr('من تاريخ', 'From Date')) ?></label>
<input type="date" name="date_from" class="form-control" value="<?= h($date_from) ?>">
</div>
<div class="col-md-3">
<label class="form-label text-muted small"><?= h(tr('إلى تاريخ', 'To Date')) ?></label>
<input type="date" name="date_to" class="form-control" value="<?= h($date_to) ?>">
</div>
<div class="col-md-2 d-flex gap-2">
<button type="submit" class="btn btn-primary flex-grow-1"><i class="bi bi-search"></i> <?= h(tr('بحث', 'Search')) ?></button>
<button type="button" class="btn btn-secondary" onclick="window.print()"><i class="bi bi-printer"></i></button>
</div>
</form>
<div class="card shadow-sm border-0 rounded-4">
<div class="card-body p-0 table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="table-light text-muted">
<tr>
<th class="ps-4">#</th>
<th><?= h(tr('التاريخ', 'Date')) ?></th>
<th><?= h(tr('العميل', 'Customer')) ?></th>
<th><?= h(tr('الهاتف', 'Telephone')) ?></th>
<th><?= h(tr('العنوان', 'Address')) ?></th>
<th><?= h(tr('المبلغ', 'Amount')) ?></th>
<th><?= h(tr('الحالة', 'Status')) ?></th>
<th class="pe-4 text-end print-hide"><?= h(tr('إجراءات', 'Actions')) ?></th>
</tr>
</thead>
<tbody>
<?php if(empty($orders)): ?>
<tr><td colspan="8" class="text-center py-5 text-muted"><?= h(tr('لا توجد طلبات', 'No orders found')) ?></td></tr>
<?php else: ?>
<?php
$totalAmount = 0;
foreach($orders as $o):
$totalAmount += $o['total_amount'];
$statusClass = 'bg-secondary';
$statusText = $o['status'];
if ($o['status'] === 'pending') { $statusClass = 'bg-warning text-dark'; $statusText = tr('قيد الانتظار', 'Pending'); }
elseif ($o['status'] === 'accepted') { $statusClass = 'bg-primary'; $statusText = tr('مقبول', 'Accepted'); }
elseif ($o['status'] === 'completed') { $statusClass = 'bg-success'; $statusText = tr('مكتمل', 'Completed'); }
elseif ($o['status'] === 'rejected') { $statusClass = 'bg-danger'; $statusText = tr('مرفوض', 'Rejected'); }
$items = json_decode($o['items_json'], true) ?: [];
?>
<tr>
<td class="ps-4 fw-bold">#<?= h($o['id']) ?></td>
<td><?= h(date('Y-m-d H:i', strtotime($o['created_at']))) ?></td>
<td class="fw-bold"><?= h($o['customer_name']) ?></td>
<td><?= h($o['customer_phone']) ?></td>
<td style="max-width: 200px;" class="text-truncate" title="<?= h($o['customer_address']) ?>"><?= h($o['customer_address']) ?></td>
<td class="fw-bold text-primary"><?= h(currency($o['total_amount'])) ?></td>
<td><span class="badge <?= $statusClass ?> px-2 py-1"><?= h($statusText) ?></span></td>
<td class="pe-4 text-end print-hide">
<button class="btn btn-sm btn-light shadow-sm" onclick='viewOrder(<?= htmlspecialchars(json_encode([
"id" => $o["id"],
"name" => $o["customer_name"],
"phone" => $o["customer_phone"],
"address" => $o["customer_address"],
"subtotal" => $o["subtotal"] ?? 0, "vat" => $o["vat_amount"] ?? 0, "total" => $o["total_amount"],
"items" => $items
], JSON_UNESCAPED_UNICODE), ENT_QUOTES, "UTF-8") ?>)'>
<i class="bi bi-eye"></i>
</button>
<a href="edit_online_order.php?id=<?= $o ['id'] ?>" class="btn btn-sm btn-info shadow-sm text-white" title="<?= h(tr('تعديل الطلب', 'Edit Order')) ?>"><i class="bi bi-pencil"></i></a>
<div class="dropdown d-inline-block">
<button class="btn btn-sm btn-primary dropdown-toggle shadow-sm" type="button" data-bs-toggle="dropdown">
<i class="bi bi-gear"></i>
</button>
<ul class="dropdown-menu shadow">
<li><form method="post"><input type="hidden" name="action" value="update_status"><input type="hidden" name="id" value="<?= h($o['id']) ?>"><input type="hidden" name="status" value="pending"><button class="dropdown-item" type="submit"><?= h(tr('قيد الانتظار', 'Pending')) ?></button></form></li>
<li><form method="post"><input type="hidden" name="action" value="update_status"><input type="hidden" name="id" value="<?= h($o['id']) ?>"><input type="hidden" name="status" value="accepted"><button class="dropdown-item" type="submit"><?= h(tr('مقبول', 'Accepted')) ?></button></form></li>
<li><form method="post"><input type="hidden" name="action" value="update_status"><input type="hidden" name="id" value="<?= h($o['id']) ?>"><input type="hidden" name="status" value="completed"><button class="dropdown-item text-success fw-bold" type="submit"><?= h(tr('مكتمل', 'Completed')) ?></button></form></li>
<li><form method="post"><input type="hidden" name="action" value="update_status"><input type="hidden" name="id" value="<?= h($o['id']) ?>"><input type="hidden" name="status" value="rejected"><button class="dropdown-item text-danger fw-bold" type="submit"><?= h(tr('مرفوض', 'Rejected')) ?></button></form></li>
</ul>
</div>
<form method="post" class="d-inline" onsubmit="return confirm('<?= h(tr('هل أنت متأكد من الحذف؟', 'Are you sure you want to delete?')) ?>');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?= h($o['id']) ?>">
<button type="submit" class="btn btn-sm btn-danger shadow-sm"><i class="bi bi-trash"></i></button>
</form>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
<?php if(!empty($orders)): ?>
<tfoot class="table-light">
<tr>
<td colspan="5" class="text-end fw-bold"><?= h(tr('إجمالي المبالغ:', 'Total Amounts:')) ?></td>
<td colspan="3" class="fw-bold text-primary"><?= h(currency($totalAmount)) ?></td>
</tr>
</tfoot>
<?php endif; ?>
</table>
</div>
</div>
<!-- Order Modal (d-print-none prevents it from flashing in print if unhidden) -->
<div class="modal fade d-print-none" id="orderModal" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content rounded-4 border-0 shadow">
<div class="modal-header border-bottom-0 pb-0 pt-4 px-4">
<h5 class="modal-title fw-bold" id="orderModalLabel"><?= h(tr('تفاصيل الطلب', 'Order Details')) ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body p-4">
<div class="bg-light p-3 rounded-3 mb-4">
<div class="row">
<div class="col-6 mb-2"><small class="text-muted d-block"><?= h(tr('العميل', 'Customer')) ?></small><strong id="vName"></strong></div>
<div class="col-6 mb-2"><small class="text-muted d-block"><?= h(tr('الهاتف', 'Phone')) ?></small><strong id="vPhone"></strong></div>
<div class="col-12"><small class="text-muted d-block"><?= h(tr('العنوان', 'Address')) ?></small><strong id="vAddress"></strong></div>
</div>
</div>
<h6 class="fw-bold border-bottom pb-2 mb-3"><?= h(tr('المنتجات', 'Products')) ?></h6>
<ul class="list-group list-group-flush mb-4" id="vItems">
</ul>
<div class="d-flex justify-content-between align-items-center mb-2">
<span class="text-muted"><?= h(tr('المجموع الفرعي', 'Subtotal')) ?></span>
<span class="fw-bold" id="vSubtotal"></span>
</div>
<div class="d-flex justify-content-between align-items-center mb-3">
<span class="text-muted"><?= h(tr('الضريبة', 'VAT')) ?></span>
<span class="fw-bold" id="vVat"></span>
</div>
<div class="d-flex justify-content-between align-items-center bg-primary bg-opacity-10 text-primary p-3 rounded-3">
<h5 class="mb-0 fw-bold"><?= h(tr('الإجمالي', 'Total')) ?></h5>
<h4 class="mb-0 fw-bold" id="vTotal"></h4>
</div>
</div>
<div class="modal-footer border-top-0 pt-0 pb-4 px-4">
<button type="button" class="btn btn-secondary rounded-pill px-4" data-bs-dismiss="modal"><?= h(tr('إغلاق', 'Close')) ?></button>
</div>
</div>
</div>
</div>
<script>
function viewOrder(order) {
const modal = bootstrap.Modal.getOrCreateInstance(document.getElementById('orderModal'));
document.getElementById('vName').innerText = order.name;
document.getElementById('vPhone').innerText = order.phone;
document.getElementById('vAddress').innerText = order.address;
document.getElementById('vSubtotal').innerText = Number(order.subtotal).toFixed(2);
document.getElementById('vVat').innerText = Number(order.vat).toFixed(2);
document.getElementById('vTotal').innerText = Number(order.total).toFixed(2);
let html = '';
if (order.items && order.items.length) {
order.items.forEach(item => {
html += `<li class="list-group-item px-0 d-flex justify-content-between align-items-center">
<div>
<span class="fw-bold">${item.name}</span>
<small class="text-muted ms-2 px-2 bg-light rounded">${item.price}</small>
</div>
<span class="badge bg-secondary rounded-pill">x${item.qty}</span>
</li>`;
});
}
document.getElementById('vItems').innerHTML = html;
modal.show();
}
</script>
<?php require __DIR__ . '/includes/footer.php'; ?>