65 lines
3.2 KiB
PHP
65 lines
3.2 KiB
PHP
<?php
|
|
// food/courier/order_details.php — تفاصيل مهمة توصيل واحدة (شاشة المهمة عند السائق)
|
|
// أصناف الطلب + تفصيل المبالغ + هاتف المطعم للتواصل أثناء الاستلام
|
|
require_once __DIR__ . '/../connect_courier.php';
|
|
|
|
$orderId = filterRequest('order_id', 'int');
|
|
if (!$orderId) jsonError('order_id is required');
|
|
|
|
// يفشل لو لم يكن الطلب مُسنداً لهذا السائق — لا وصول لطلبات الآخرين
|
|
foodAssertOrderOwnership($orderId, 'courier', $food_courier_id);
|
|
|
|
$st = $food_con->prepare(
|
|
"SELECT o.id, o.status, o.merchant_id, o.items_total, o.delivery_fee, o.service_fee, o.discount,
|
|
o.grand_total, o.payment_method, o.customer_note, o.delivery_lat, o.delivery_lng,
|
|
o.created_at, o.ready_at, o.courier_assigned_at, o.picked_up_at, o.delivered_at,
|
|
CASE WHEN o.status IN ('courier_assigned','picked_up') THEN o.delivery_address ELSE NULL END AS delivery_address,
|
|
m.name_ar AS merchant_name_ar, m.address AS merchant_address, m.latitude AS merchant_lat,
|
|
m.longitude AS merchant_lng, m.avg_prep_minutes
|
|
FROM food_orders o
|
|
JOIN food_merchants m ON m.id = o.merchant_id
|
|
WHERE o.id = ? LIMIT 1"
|
|
);
|
|
$st->execute([$orderId]);
|
|
$order = $st->fetch();
|
|
if (!$order) jsonError('Order not found', 404);
|
|
|
|
$itemsSt = $food_con->prepare(
|
|
"SELECT name_ar_snapshot, quantity, unit_price, line_total, option_price_json
|
|
FROM food_order_items WHERE order_id=? ORDER BY id ASC"
|
|
);
|
|
$itemsSt->execute([$orderId]);
|
|
$items = array_map(static function (array $i): array {
|
|
return [
|
|
'name_ar' => $i['name_ar_snapshot'],
|
|
'quantity' => (int)$i['quantity'],
|
|
'unit_price' => (int)$i['unit_price'],
|
|
'line_total' => (int)$i['line_total'],
|
|
'options' => $i['option_price_json'] ? json_decode($i['option_price_json'], true) : null,
|
|
];
|
|
}, $itemsSt->fetchAll());
|
|
|
|
// هاتف المطعم — يُعاد فقط أثناء المهمة النشطة (قبل التسليم)، لا في السجل
|
|
$merchantPhone = null;
|
|
if (in_array($order['status'], ['courier_assigned', 'picked_up'], true)) {
|
|
$phoneSt = $food_con->prepare(
|
|
"SELECT phone FROM food_merchant_users
|
|
WHERE merchant_id=? AND is_active=1 ORDER BY role='owner' DESC, id ASC LIMIT 1"
|
|
);
|
|
$phoneSt->execute([$order['merchant_id']]);
|
|
$merchantPhone = $phoneSt->fetch()['phone'] ?? null;
|
|
}
|
|
|
|
// ملاحظة: لا هاتف للزبون هنا — قاعدة siro_food معزولة ولا تحمل بيانات الراكب،
|
|
// وممنوع Database::get('main') داخل backend/food/. التواصل مع الزبون يبقى عبر
|
|
// عنوان التسليم والملاحظة، إلى أن تُضاف قناة اتصال مقنّعة كما في الرحلات.
|
|
$order['items'] = $items;
|
|
$order['items_count'] = array_sum(array_column($items, 'quantity'));
|
|
$order['merchant_phone'] = $merchantPhone;
|
|
$order['cash_to_collect'] = $order['payment_method'] === 'cash' ? (int)$order['grand_total'] : 0;
|
|
$order['courier_owes'] = $order['payment_method'] === 'cash'
|
|
? (int)$order['grand_total'] - (int)$order['delivery_fee']
|
|
: 0;
|
|
|
|
jsonSuccess(['order' => $order]);
|