add new featurs like realtime 2026-5-10-1

This commit is contained in:
Hamza-Ayed
2026-05-09 23:53:48 +03:00
parent fc74c20730
commit e12d70af8c
2 changed files with 136 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
<?php
// Admin/v2/realtime_dashboard.php
require_once __DIR__ . '/../../connect.php';
// التحقق من الصلاحيات
if ($role !== 'admin' && $role !== 'super_admin') {
http_response_code(403);
echo json_encode(['error' => 'Unauthorized access. Admin role required.']);
exit;
}
$response = [
'status' => 'success',
'message' => []
];
try {
// 1. الرحلات النشطة حالياً
$stmt = $con->prepare("SELECT COUNT(*) FROM ride WHERE status IN ('wait', 'started', 'arrived')");
$stmt->execute();
$active_rides = $stmt->fetchColumn();
// 2. السائقون المتصلون حالياً (أونلاين)
$stmt = $con->prepare("SELECT COUNT(*) FROM car_locations WHERE status = 'on'");
$stmt->execute();
$online_drivers = $stmt->fetchColumn();
// 3. إيرادات اليوم
$stmt = $con->prepare("SELECT IFNULL(SUM(price_for_passenger), 0) FROM ride WHERE status = 'Finished' AND DATE(created_at) = CURDATE()");
$stmt->execute();
$revenue_today = $stmt->fetchColumn();
// إيرادات الأمس (للمقارنة)
$stmt = $con->prepare("SELECT IFNULL(SUM(price_for_passenger), 0) FROM ride WHERE status = 'Finished' AND DATE(created_at) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)");
$stmt->execute();
$revenue_yesterday = $stmt->fetchColumn();
// 4. شكاوى جديدة اليوم
$stmt = $con->prepare("SELECT COUNT(*) FROM complaint WHERE DATE(date_filed) = CURDATE() AND statusComplaint = 'Open'");
$stmt->execute();
$new_complaints = $stmt->fetchColumn();
// 5. رخص تنتهي هذا الشهر
$stmt = $con->prepare("SELECT COUNT(*) FROM driver WHERE expiry_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 30 DAY)");
$stmt->execute();
$expiring_licenses = $stmt->fetchColumn();
$response['message'] = [
'active_rides' => (int)$active_rides,
'online_drivers' => (int)$online_drivers,
'revenue_today' => (float)$revenue_today,
'revenue_yesterday' => (float)$revenue_yesterday,
'new_complaints' => (int)$new_complaints,
'expiring_licenses' => (int)$expiring_licenses
];
echo json_encode($response);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>

74
Admin/v2/smart_alerts.php Normal file
View File

@@ -0,0 +1,74 @@
<?php
// Admin/v2/smart_alerts.php
require_once __DIR__ . '/../../connect.php';
// التحقق من الصلاحيات
if ($role !== 'admin' && $role !== 'super_admin') {
http_response_code(403);
echo json_encode(['error' => 'Unauthorized access. Admin role required.']);
exit;
}
$alerts = [];
try {
// 1. شكاوى جديدة غير محلولة (مفتوحة)
$stmt = $con->prepare("SELECT id, ride_id, complaint_type, date_filed FROM complaint WHERE statusComplaint = 'Open' ORDER BY date_filed DESC LIMIT 10");
$stmt->execute();
$open_complaints = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($open_complaints as $c) {
$alerts[] = [
'type' => 'complaint',
'severity' => 'high',
'title' => 'شكوى جديدة (' . $c['complaint_type'] . ')',
'description' => "يوجد شكوى جديدة للرحلة رقم " . $c['ride_id'] . " تحتاج للمراجعة.",
'date' => $c['date_filed'],
'action_id' => $c['id']
];
}
// 2. رحلات عالقة (في الانتظار لأكثر من 15 دقيقة)
$stmt = $con->prepare("SELECT id, created_at FROM ride WHERE status = 'wait' AND created_at < DATE_SUB(NOW(), INTERVAL 15 MINUTE) LIMIT 10");
$stmt->execute();
$stuck_rides = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($stuck_rides as $r) {
$alerts[] = [
'type' => 'ride',
'severity' => 'medium',
'title' => 'رحلة عالقة قيد الانتظار',
'description' => "الرحلة رقم " . $r['id'] . " عالقة في حالة انتظار لأكثر من 15 دقيقة.",
'date' => $r['created_at'],
'action_id' => $r['id']
];
}
// 3. رخص قيادة شارفت على الانتهاء (خلال 15 يوم القادمة)
$stmt = $con->prepare("SELECT id, first_name, last_name, phone, expiry_date FROM driver WHERE expiry_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 15 DAY) LIMIT 10");
$stmt->execute();
$expiring_drivers = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($expiring_drivers as $d) {
$alerts[] = [
'type' => 'license',
'severity' => 'warning',
'title' => 'رخصة كابتن قاربت على الانتهاء',
'description' => "رخصة الكابتن " . $d['first_name'] . " " . $d['last_name'] . " ستنتهي بتاريخ " . $d['expiry_date'] . ".",
// We use current time for sorting purposes, but display the expiry date
'date' => date('Y-m-d H:i:s'),
'action_id' => $d['id']
];
}
// ترتيب التنبيهات حسب الأحدث
usort($alerts, function($a, $b) {
return strtotime($b['date']) - strtotime($a['date']);
});
echo json_encode([
'status' => 'success',
'message' => $alerts
]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>