72 lines
2.8 KiB
PHP
72 lines
2.8 KiB
PHP
<?php
|
|
// transit/trip/today.php — السائق يجلب رحلاته اليوم (تُنشأ تلقائياً من الجداول)
|
|
|
|
require_once __DIR__ . '/../../transit/connect_app.php';
|
|
|
|
if ($transit_user_role !== 'driver') jsonError('Only drivers can access this endpoint', 403);
|
|
|
|
// استخرج سجل السائق من JWT — لا نثق بـ driver_transit_id من العميل
|
|
$chk = $transit_con->prepare(
|
|
"SELECT id, org_id FROM transit_drivers WHERE main_driver_id=? AND status='active' LIMIT 1"
|
|
);
|
|
$chk->execute([(string)$transit_user_id]);
|
|
$driver = $chk->fetch();
|
|
if (!$driver) jsonError('Driver not found or not active — activate your account first', 403);
|
|
$driverTransitId = (int)$driver['id'];
|
|
|
|
$today = date('Y-m-d');
|
|
|
|
// أنشئ رحلات اليوم من الجداول النشطة إن لم تُنشأ بعد
|
|
$stScheds = $transit_con->prepare(
|
|
"SELECT id AS schedule_id, route_id, vehicle_id, days_mask
|
|
FROM transit_schedules
|
|
WHERE driver_id=? AND org_id=? AND is_active=1
|
|
AND valid_from <= ? AND (valid_until IS NULL OR valid_until >= ?)"
|
|
);
|
|
$stScheds->execute([$driverTransitId, $driver['org_id'], $today, $today]);
|
|
|
|
foreach ($stScheds->fetchAll() as $sch) {
|
|
if (!transitDayActive((int)$sch['days_mask'])) continue;
|
|
$ex = $transit_con->prepare("SELECT id FROM transit_trips WHERE schedule_id=? AND trip_date=? LIMIT 1");
|
|
$ex->execute([$sch['schedule_id'], $today]);
|
|
if (!$ex->fetch()) {
|
|
$transit_con->prepare(
|
|
"INSERT INTO transit_trips
|
|
(schedule_id, route_id, org_id, driver_id, vehicle_id, trip_date, status)
|
|
VALUES (?,?,?,?,?,?,'scheduled')"
|
|
)->execute([
|
|
$sch['schedule_id'], $sch['route_id'], $driver['org_id'],
|
|
$driverTransitId, $sch['vehicle_id'], $today,
|
|
]);
|
|
}
|
|
}
|
|
|
|
// اجلب رحلات اليوم مع التفاصيل
|
|
$stTrips = $transit_con->prepare(
|
|
"SELECT t.id, t.route_id, t.status, t.delay_minutes, t.current_stop_seq,
|
|
t.started_at, t.completed_at,
|
|
r.name_ar AS route_name, r.polyline,
|
|
sc.departure_time,
|
|
v.plate AS vehicle_plate, v.capacity
|
|
FROM transit_trips t
|
|
JOIN transit_routes r ON r.id = t.route_id
|
|
JOIN transit_schedules sc ON sc.id = t.schedule_id
|
|
LEFT JOIN transit_vehicles v ON v.id = t.vehicle_id
|
|
WHERE t.driver_id=? AND t.trip_date=?
|
|
ORDER BY sc.departure_time ASC"
|
|
);
|
|
$stTrips->execute([$driverTransitId, $today]);
|
|
$trips = $stTrips->fetchAll();
|
|
|
|
$stStops = $transit_con->prepare(
|
|
"SELECT sequence, name_ar, latitude, longitude, geofence_radius, eta_offset_min
|
|
FROM transit_stops WHERE route_id=? ORDER BY sequence ASC"
|
|
);
|
|
foreach ($trips as &$trip) {
|
|
$stStops->execute([$trip['route_id']]);
|
|
$trip['stops'] = $stStops->fetchAll();
|
|
}
|
|
unset($trip);
|
|
|
|
jsonSuccess(['date' => $today, 'trips' => $trips]);
|