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