40 lines
1.8 KiB
PHP
40 lines
1.8 KiB
PHP
<?php
|
|
// food/merchant_ops/incoming.php — الطلبات الحالية للمطعم
|
|
require_once __DIR__ . '/../connect_merchant.php';
|
|
|
|
$statusFilter = filterRequest('status') ?: 'active';
|
|
$allowed = ['active', 'pending', 'merchant_accepted', 'preparing', 'ready', 'history'];
|
|
if (!in_array($statusFilter, $allowed, true)) $statusFilter = 'active';
|
|
|
|
if ($statusFilter === 'active') {
|
|
$sql = "SELECT id, passenger_id, status, items_total, delivery_fee, grand_total, created_at
|
|
FROM food_orders WHERE merchant_id=? AND status IN ('pending','merchant_accepted','preparing','ready','courier_assigned','picked_up')
|
|
ORDER BY created_at ASC";
|
|
$params = [$food_merchant_id];
|
|
} elseif ($statusFilter === 'history') {
|
|
$sql = "SELECT id, passenger_id, status, items_total, delivery_fee, grand_total, created_at, delivered_at
|
|
FROM food_orders WHERE merchant_id=? AND status IN ('delivered','rejected','cancelled_by_customer','cancelled_by_merchant','cancelled_system')
|
|
ORDER BY created_at DESC LIMIT 100";
|
|
$params = [$food_merchant_id];
|
|
} else {
|
|
$sql = "SELECT id, passenger_id, status, items_total, delivery_fee, grand_total, created_at
|
|
FROM food_orders WHERE merchant_id=? AND status=? ORDER BY created_at ASC";
|
|
$params = [$food_merchant_id, $statusFilter];
|
|
}
|
|
|
|
$st = $food_con->prepare($sql);
|
|
$st->execute($params);
|
|
$orders = $st->fetchAll();
|
|
|
|
if ($orders) {
|
|
$ids = implode(',', array_map('intval', array_column($orders, 'id')));
|
|
$itemsSt = $food_con->query("SELECT order_id, name_ar_snapshot, quantity FROM food_order_items WHERE order_id IN ($ids)");
|
|
$allItems = $itemsSt ? $itemsSt->fetchAll() : [];
|
|
foreach ($orders as &$o) {
|
|
$o['items'] = array_values(array_filter($allItems, fn($i) => (int)$i['order_id'] === (int)$o['id']));
|
|
}
|
|
unset($o);
|
|
}
|
|
|
|
jsonSuccess(['orders' => $orders]);
|