Update codebase
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
// food/merchant_ops/accept.php — المطعم يقبل الطلب
|
||||
require_once __DIR__ . '/../connect_merchant.php';
|
||||
|
||||
$orderId = filterRequest('order_id', 'int');
|
||||
if (!$orderId) jsonError('order_id is required');
|
||||
|
||||
foodAssertOrderOwnership($orderId, 'merchant', (string)$food_merchant_id);
|
||||
food_transition_status($orderId, 'merchant_accepted', 'merchant', (string)$food_merchant_user_id);
|
||||
|
||||
jsonSuccess(['order_id' => $orderId, 'status' => 'merchant_accepted']);
|
||||
@@ -0,0 +1,39 @@
|
||||
<?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]);
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
// food/merchant_ops/items_toggle.php — تفعيل/إيقاف صنف (نفد من المخزون مثلاً)
|
||||
require_once __DIR__ . '/../connect_merchant.php';
|
||||
|
||||
$itemId = filterRequest('item_id', 'int');
|
||||
if (!$itemId) jsonError('item_id is required');
|
||||
|
||||
$st = $food_con->prepare("SELECT id, merchant_id, is_available FROM food_menu_items WHERE id=? LIMIT 1");
|
||||
$st->execute([$itemId]);
|
||||
$item = $st->fetch();
|
||||
if (!$item) jsonError('Item not found', 404);
|
||||
if ((int)$item['merchant_id'] !== $food_merchant_id) jsonError('Forbidden', 403);
|
||||
|
||||
$newAvailability = $item['is_available'] ? 0 : 1;
|
||||
$food_con->prepare("UPDATE food_menu_items SET is_available=? WHERE id=?")->execute([$newAvailability, $itemId]);
|
||||
|
||||
jsonSuccess(['item_id' => $itemId, 'is_available' => (bool)$newAvailability]);
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
// food/merchant_ops/preparing.php — المطعم بدأ التحضير
|
||||
require_once __DIR__ . '/../connect_merchant.php';
|
||||
|
||||
$orderId = filterRequest('order_id', 'int');
|
||||
if (!$orderId) jsonError('order_id is required');
|
||||
|
||||
foodAssertOrderOwnership($orderId, 'merchant', (string)$food_merchant_id);
|
||||
food_transition_status($orderId, 'preparing', 'merchant', (string)$food_merchant_user_id);
|
||||
|
||||
jsonSuccess(['order_id' => $orderId, 'status' => 'preparing']);
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
// food/merchant_ops/ready.php — الطلب جاهز — يبدأ حلقة إسناد السائق
|
||||
require_once __DIR__ . '/../connect_merchant.php';
|
||||
|
||||
$orderId = filterRequest('order_id', 'int');
|
||||
if (!$orderId) jsonError('order_id is required');
|
||||
|
||||
foodAssertOrderOwnership($orderId, 'merchant', (string)$food_merchant_id);
|
||||
food_transition_status($orderId, 'ready', 'merchant', (string)$food_merchant_user_id);
|
||||
|
||||
$merchantSt = $food_con->prepare("SELECT latitude, longitude FROM food_merchants WHERE id=?");
|
||||
$merchantSt->execute([$food_merchant_id]);
|
||||
$merchant = $merchantSt->fetch();
|
||||
|
||||
$offered = false;
|
||||
if ($merchant) {
|
||||
$candidates = foodFindNearbyCouriers((float)$merchant['latitude'], (float)$merchant['longitude']);
|
||||
|
||||
// استبعاد من سبق عرض هذا الطلب عليه (رفض أو انتهت مهلته)
|
||||
$priorSt = $food_con->prepare("SELECT courier_id FROM food_courier_assignments WHERE order_id=?");
|
||||
$priorSt->execute([$orderId]);
|
||||
$prior = array_column($priorSt->fetchAll(), 'courier_id');
|
||||
|
||||
foreach ($candidates as $courierId) {
|
||||
if (in_array($courierId, $prior, true)) continue;
|
||||
foodOfferOrderToCourier($orderId, $courierId);
|
||||
$offered = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$offered) {
|
||||
appLog("[FOOD][DELIVERY] No available courier found for order $orderId at ready-time", 'WARNING');
|
||||
}
|
||||
|
||||
jsonSuccess(['order_id' => $orderId, 'status' => 'ready', 'courier_offer_sent' => $offered]);
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
// food/merchant_ops/reject.php — المطعم يرفض الطلب — استرجاع فوري إن دُفع من المحفظة
|
||||
require_once __DIR__ . '/../connect_merchant.php';
|
||||
|
||||
$orderId = filterRequest('order_id', 'int');
|
||||
$reason = filterRequest('reason');
|
||||
if (!$orderId) jsonError('order_id is required');
|
||||
|
||||
$order = foodAssertOrderOwnership($orderId, 'merchant', (string)$food_merchant_id);
|
||||
food_transition_status($orderId, 'rejected', 'merchant', (string)$food_merchant_user_id, $reason);
|
||||
|
||||
if ($order['payment_method'] === 'wallet') {
|
||||
$refunded = foodWalletMove(
|
||||
(string)$order['passenger_id'], (int)$order['grand_total'], 'add',
|
||||
"food-refund-{$orderId}", "Food order #{$orderId} rejected by merchant"
|
||||
);
|
||||
$food_con->prepare(
|
||||
"INSERT INTO food_order_payments (order_id, type, amount, status) VALUES (?,'release',?,?)"
|
||||
)->execute([$orderId, (int)$order['grand_total'], $refunded ? 'success' : 'failed']);
|
||||
|
||||
if (!$refunded) appLog("[FOOD][MERCHANT][reject] refund FAILED for order $orderId — needs manual reconciliation", 'ERROR');
|
||||
}
|
||||
|
||||
jsonSuccess(['order_id' => $orderId, 'status' => 'rejected']);
|
||||
Reference in New Issue
Block a user