52 lines
2.0 KiB
PHP
52 lines
2.0 KiB
PHP
<?php
|
|
// transit/trip/start.php — السائق يبدأ الرحلة
|
|
|
|
require_once __DIR__ . '/../../transit/connect_app.php';
|
|
|
|
requireTransitFields(['trip_id', 'driver_transit_id', 'lat', 'lng']);
|
|
|
|
$tripId = filterRequest('trip_id', 'int');
|
|
$driverId = filterRequest('driver_transit_id', 'int');
|
|
$lat = (float)filterRequest('lat');
|
|
$lng = (float)filterRequest('lng');
|
|
|
|
$st = $transit_con->prepare(
|
|
"SELECT t.id, t.route_id, t.status, r.name_ar AS route_name, d.main_driver_id
|
|
FROM transit_trips t
|
|
JOIN transit_routes r ON r.id = t.route_id
|
|
JOIN transit_drivers d ON d.id = t.driver_id
|
|
WHERE t.id=? AND t.driver_id=? LIMIT 1"
|
|
);
|
|
$st->execute([$tripId, $driverId]);
|
|
$trip = $st->fetch();
|
|
|
|
if (!$trip) jsonError('Trip not found', 404);
|
|
if ($trip['status'] === 'started') jsonError('Trip already started', 409);
|
|
if ($trip['status'] === 'completed') jsonError('Trip already completed', 409);
|
|
if ($trip['status'] === 'cancelled') jsonError('Trip was cancelled', 409);
|
|
if (!$trip['main_driver_id']) jsonError('Driver has no linked main account — cannot start live tracking', 422);
|
|
|
|
$transit_con->prepare(
|
|
"UPDATE transit_trips SET status='started', started_at=NOW(), current_stop_seq=1 WHERE id=?"
|
|
)->execute([$tripId]);
|
|
|
|
transitUpdateBusPosition($tripId, $lat, $lng);
|
|
|
|
// تخزين ملكية الرحلة — يتحقق منها driver_socket قبل أي بثّ حي للراكبين
|
|
transitSetTripOwner($tripId, (string)$trip['main_driver_id'], (int)$trip['route_id']);
|
|
|
|
global $redis;
|
|
if ($redis) {
|
|
$redis->set("transit:trip:{$tripId}:status", 'started');
|
|
$redis->expire("transit:trip:{$tripId}:status", 86400);
|
|
}
|
|
|
|
transitSendTopicNotification(
|
|
transitRouteTopic((int)$trip['route_id']),
|
|
'الباص انطلق الآن',
|
|
'خط ' . $trip['route_name'] . ' بدأ رحلته',
|
|
['type' => 'transit_started', 'trip_id' => (string)$tripId]
|
|
);
|
|
|
|
jsonSuccess(['trip_id' => $tripId, 'status' => 'started', 'started_at' => date('Y-m-d H:i:s')], 'Trip started');
|