Files
2026-08-09 16:56:13 +03:00

37 lines
1.4 KiB
PHP

<?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]);