89 lines
4.5 KiB
PHP
89 lines
4.5 KiB
PHP
<?php
|
|
// Admin/transit/org/details.php — تفاصيل وتحليلات مؤسسة واحدة (لفريق سيرو)
|
|
// POST/GET: org_id
|
|
|
|
require_once __DIR__ . '/../../../connect.php';
|
|
|
|
if ($role !== 'admin' && $role !== 'super_admin') {
|
|
jsonError('Unauthorized: Admin access required', 403);
|
|
}
|
|
|
|
try { $transit_con = Database::get('transit'); }
|
|
catch (Exception $e) { jsonError('Transit service unavailable', 503); }
|
|
|
|
$orgId = filterRequest('org_id', 'int');
|
|
if (!$orgId) jsonError('org_id is required', 400);
|
|
|
|
$st = $transit_con->prepare("SELECT * FROM transit_orgs WHERE id=? LIMIT 1");
|
|
$st->execute([$orgId]);
|
|
$org = $st->fetch();
|
|
if (!$org) jsonError('Organization not found', 404);
|
|
|
|
// فك تشفير هاتف التواصل للعرض الإداري فقط
|
|
if (!empty($org['contact_phone'])) {
|
|
$org['contact_phone'] = $encryptionHelper->decryptData($org['contact_phone']) ?: null;
|
|
}
|
|
|
|
// ── العدّادات الأساسية ───────────────────────────────────────
|
|
$counts = [
|
|
'drivers_total' => (int)$transit_con->query("SELECT COUNT(*) FROM transit_drivers WHERE org_id=$orgId")->fetchColumn(),
|
|
'drivers_active' => (int)$transit_con->query("SELECT COUNT(*) FROM transit_drivers WHERE org_id=$orgId AND status='active'")->fetchColumn(),
|
|
'vehicles_total' => (int)$transit_con->query("SELECT COUNT(*) FROM transit_vehicles WHERE org_id=$orgId")->fetchColumn(),
|
|
'vehicles_active' => (int)$transit_con->query("SELECT COUNT(*) FROM transit_vehicles WHERE org_id=$orgId AND is_active=1")->fetchColumn(),
|
|
'routes_total' => (int)$transit_con->query("SELECT COUNT(*) FROM transit_routes WHERE org_id=$orgId")->fetchColumn(),
|
|
'routes_active' => (int)$transit_con->query("SELECT COUNT(*) FROM transit_routes WHERE org_id=$orgId AND status='active'")->fetchColumn(),
|
|
'enrollments_active' => (int)$transit_con->query("SELECT COUNT(*) FROM transit_enrollments WHERE org_id=$orgId AND status='active'")->fetchColumn(),
|
|
'enrollments_pending' => (int)$transit_con->query("SELECT COUNT(*) FROM transit_enrollments WHERE org_id=$orgId AND status='pending'")->fetchColumn(),
|
|
];
|
|
|
|
// ── الرحلات: اليوم / هذا الأسبوع / هذا الشهر / إجمالي ──────────
|
|
$today = date('Y-m-d');
|
|
$weekStart = date('Y-m-d', strtotime('monday this week'));
|
|
$monthStart = date('Y-m-01');
|
|
|
|
$stTrips = $transit_con->prepare(
|
|
"SELECT
|
|
SUM(trip_date = ?) AS today_count,
|
|
SUM(trip_date >= ?) AS week_count,
|
|
SUM(trip_date >= ?) AS month_count,
|
|
COUNT(*) AS total_count,
|
|
SUM(status='completed') AS completed_count,
|
|
SUM(status='cancelled') AS cancelled_count,
|
|
SUM(status='no_show') AS no_show_count,
|
|
AVG(CASE WHEN status='completed' THEN delay_minutes END) AS avg_delay_minutes,
|
|
SUM(CASE WHEN status='completed' AND started_at IS NOT NULL AND completed_at IS NOT NULL
|
|
THEN TIMESTAMPDIFF(MINUTE, started_at, completed_at) ELSE 0 END) AS total_minutes_driven
|
|
FROM transit_trips WHERE org_id = ?"
|
|
);
|
|
$stTrips->execute([$today, $weekStart, $monthStart, $orgId]);
|
|
$tripStats = $stTrips->fetch();
|
|
|
|
$trips = [
|
|
'today' => (int)($tripStats['today_count'] ?? 0),
|
|
'this_week' => (int)($tripStats['week_count'] ?? 0),
|
|
'this_month' => (int)($tripStats['month_count'] ?? 0),
|
|
'total' => (int)($tripStats['total_count'] ?? 0),
|
|
'completed' => (int)($tripStats['completed_count'] ?? 0),
|
|
'cancelled' => (int)($tripStats['cancelled_count'] ?? 0),
|
|
'no_show' => (int)($tripStats['no_show_count'] ?? 0),
|
|
'avg_delay_minutes' => round((float)($tripStats['avg_delay_minutes'] ?? 0), 1),
|
|
'total_hours_driven' => round(((int)($tripStats['total_minutes_driven'] ?? 0)) / 60, 1),
|
|
];
|
|
|
|
// ── الخطوط مع ملخص لكل خط ─────────────────────────────────────
|
|
$stRoutes = $transit_con->prepare(
|
|
"SELECT r.id, r.name_ar, r.status, r.distance_km,
|
|
(SELECT COUNT(*) FROM transit_stops s WHERE s.route_id = r.id) AS stops_count,
|
|
(SELECT COUNT(*) FROM transit_trips t WHERE t.route_id = r.id AND t.status='completed') AS completed_trips
|
|
FROM transit_routes r WHERE r.org_id = ? ORDER BY r.name_ar ASC"
|
|
);
|
|
$stRoutes->execute([$orgId]);
|
|
$routes = $stRoutes->fetchAll();
|
|
|
|
jsonSuccess([
|
|
'org' => $org,
|
|
'counts' => $counts,
|
|
'trips' => $trips,
|
|
'routes' => $routes,
|
|
]);
|