first commit
This commit is contained in:
43
backend/Admin/v2/analytics/driver_ranking.php
Normal file
43
backend/Admin/v2/analytics/driver_ranking.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
// Admin/v2/analytics/driver_ranking.php
|
||||
require_once __DIR__ . '/../../../connect.php';
|
||||
|
||||
if ($role !== 'admin' && $role !== 'super_admin') {
|
||||
http_response_code(403);
|
||||
echo json_encode(['error' => 'Unauthorized access.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// أفضل 10 كباتن حسب عدد الرحلات المكتملة
|
||||
$stmt = $con->prepare("
|
||||
SELECT
|
||||
d.id, d.first_name, d.last_name, d.phone,
|
||||
COUNT(r.id) as completed_rides,
|
||||
SUM(r.price) as total_revenue
|
||||
FROM driver d
|
||||
JOIN ride r ON d.id = r.driver_id
|
||||
WHERE r.status = 'Finished'
|
||||
GROUP BY d.id, d.first_name, d.last_name, d.phone
|
||||
ORDER BY completed_rides DESC
|
||||
LIMIT 10
|
||||
");
|
||||
$stmt->execute();
|
||||
$top_drivers = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// فك تشفير الأسماء
|
||||
foreach ($top_drivers as &$driver) {
|
||||
$driver['first_name'] = $encryptionHelper->decryptData($driver['first_name']);
|
||||
$driver['last_name'] = $encryptionHelper->decryptData($driver['last_name']);
|
||||
$driver['phone'] = $encryptionHelper->decryptData($driver['phone']);
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'data' => $top_drivers
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
58
backend/Admin/v2/analytics/growth.php
Normal file
58
backend/Admin/v2/analytics/growth.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
// Admin/v2/analytics/growth.php
|
||||
require_once __DIR__ . '/../../../connect.php';
|
||||
|
||||
if ($role !== 'admin' && $role !== 'super_admin') {
|
||||
http_response_code(403);
|
||||
echo json_encode(['error' => 'Unauthorized access.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// نمو الركاب لآخر 30 يوم
|
||||
$stmt = $con->prepare("
|
||||
SELECT DATE(created_at) as date, COUNT(*) as new_passengers
|
||||
FROM passengers
|
||||
WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
|
||||
GROUP BY DATE(created_at)
|
||||
ORDER BY date ASC
|
||||
");
|
||||
$stmt->execute();
|
||||
$passenger_growth = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// نمو السائقين لآخر 30 يوم
|
||||
$stmt = $con->prepare("
|
||||
SELECT DATE(created_at) as date, COUNT(*) as new_drivers
|
||||
FROM driver
|
||||
WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
|
||||
GROUP BY DATE(created_at)
|
||||
ORDER BY date ASC
|
||||
");
|
||||
$stmt->execute();
|
||||
$driver_growth = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// إجمالي الأعداد الحالية
|
||||
$stmt = $con->prepare("SELECT COUNT(*) FROM passengers");
|
||||
$stmt->execute();
|
||||
$total_passengers = $stmt->fetchColumn();
|
||||
|
||||
$stmt = $con->prepare("SELECT COUNT(*) FROM driver");
|
||||
$stmt->execute();
|
||||
$total_drivers = $stmt->fetchColumn();
|
||||
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'data' => [
|
||||
'passenger_daily' => $passenger_growth,
|
||||
'driver_daily' => $driver_growth,
|
||||
'totals' => [
|
||||
'passengers' => (int)$total_passengers,
|
||||
'drivers' => (int)$total_drivers
|
||||
]
|
||||
]
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
52
backend/Admin/v2/analytics/revenue.php
Normal file
52
backend/Admin/v2/analytics/revenue.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
// Admin/v2/analytics/revenue.php
|
||||
require_once __DIR__ . '/../../../connect.php';
|
||||
|
||||
if ($role !== 'admin' && $role !== 'super_admin') {
|
||||
http_response_code(403);
|
||||
echo json_encode(['error' => 'Unauthorized access.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// إحصائيات الإيرادات لآخر 30 يوم
|
||||
$stmt = $con->prepare("
|
||||
SELECT
|
||||
DATE(created_at) as date,
|
||||
SUM(price) as total_revenue,
|
||||
SUM(price - price_for_driver) as company_profit,
|
||||
COUNT(*) as total_rides
|
||||
FROM ride
|
||||
WHERE status = 'Finished'
|
||||
AND created_at >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
|
||||
GROUP BY DATE(created_at)
|
||||
ORDER BY date ASC
|
||||
");
|
||||
$stmt->execute();
|
||||
$daily_stats = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// ملخص عام
|
||||
$stmt = $con->prepare("
|
||||
SELECT
|
||||
SUM(price) as total_revenue_all,
|
||||
SUM(price - price_for_driver) as total_profit_all,
|
||||
AVG(price) as avg_ride_price
|
||||
FROM ride
|
||||
WHERE status = 'Finished'
|
||||
AND created_at >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
|
||||
");
|
||||
$stmt->execute();
|
||||
$summary = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'data' => [
|
||||
'daily' => $daily_stats,
|
||||
'summary' => $summary
|
||||
]
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
44
backend/Admin/v2/financial/settlements.php
Normal file
44
backend/Admin/v2/financial/settlements.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
// Admin/v2/financial/settlements.php
|
||||
require_once __DIR__ . '/../../../connect.php';
|
||||
|
||||
if ($role !== 'admin' && $role !== 'super_admin') {
|
||||
http_response_code(403);
|
||||
echo json_encode(['error' => 'Unauthorized access.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// جلب السائقين الذين لديهم مستحقات أو مديونية
|
||||
// الحسبة: إجمالي (price_for_driver) من الرحلات المكتملة
|
||||
$stmt = $con->prepare("
|
||||
SELECT
|
||||
d.id, d.first_name, d.last_name, d.phone,
|
||||
SUM(r.price_for_driver) as total_earned,
|
||||
COUNT(r.id) as total_rides
|
||||
FROM driver d
|
||||
LEFT JOIN ride r ON d.id = r.driver_id AND r.status = 'Finished'
|
||||
GROUP BY d.id
|
||||
HAVING total_earned > 0
|
||||
ORDER BY total_earned DESC
|
||||
LIMIT 50
|
||||
");
|
||||
$stmt->execute();
|
||||
$drivers = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// فك تشفير البيانات
|
||||
foreach ($drivers as &$driver) {
|
||||
$driver['first_name'] = $encryptionHelper->decryptData($driver['first_name']);
|
||||
$driver['last_name'] = $encryptionHelper->decryptData($driver['last_name']);
|
||||
$driver['phone'] = $encryptionHelper->decryptData($driver['phone']);
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'data' => $drivers
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
34
backend/Admin/v2/financial/stats.php
Normal file
34
backend/Admin/v2/financial/stats.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
// Admin/v2/financial/stats.php
|
||||
require_once __DIR__ . '/../../../connect.php';
|
||||
|
||||
if ($role !== 'admin' && $role !== 'super_admin') {
|
||||
http_response_code(403);
|
||||
echo json_encode(['error' => 'Unauthorized access.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// إحصائيات مالية عامة
|
||||
$stmt = $con->prepare("
|
||||
SELECT
|
||||
SUM(price_for_passenger) as total_revenue,
|
||||
SUM(price_for_driver) as total_driver_pay,
|
||||
SUM(price_for_passenger - price_for_driver) as total_platform_commission,
|
||||
(SELECT SUM(amount) FROM payments WHERE payment_method = 'Cash') as cash_payments,
|
||||
(SELECT SUM(amount) FROM payments WHERE payment_method != 'Cash') as digital_payments
|
||||
FROM ride
|
||||
WHERE status = 'Finished'
|
||||
");
|
||||
$stmt->execute();
|
||||
$stats = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'data' => $stats
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
102
backend/Admin/v2/quality/blacklist_manager.php
Normal file
102
backend/Admin/v2/quality/blacklist_manager.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
// Admin/v2/quality/blacklist_manager.php
|
||||
require_once __DIR__ . '/../../../connect.php';
|
||||
// require_once __DIR__ . '/../../../encrypt_decrypt.php';
|
||||
require_once __DIR__ . '/../security/audit_logs_helper.php'; // إذا كان متاحاً، وإلا سننفذ الإدخال مباشرة
|
||||
|
||||
if ($role !== 'admin' && $role !== 'super_admin') {
|
||||
jsonError("Unauthorized", 403);
|
||||
}
|
||||
|
||||
$action_type = filterRequest('action_type') ?: 'get_all';
|
||||
|
||||
try {
|
||||
if ($action_type === 'get_all') {
|
||||
// جلب قائمة السائقين المحظورين
|
||||
$stmt_drivers = $con->prepare("
|
||||
SELECT id, driver_id, phone, reason, created_at, 'driver' as type
|
||||
FROM blacklist_driver
|
||||
ORDER BY created_at DESC
|
||||
");
|
||||
$stmt_drivers->execute();
|
||||
$blocked_drivers = $stmt_drivers->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// جلب قائمة الركاب المحظورين
|
||||
$stmt_passengers = $con->prepare("
|
||||
SELECT id, phone, phone_normalized, reason, expires_at, created_at, 'passenger' as type
|
||||
FROM passenger_blacklist
|
||||
ORDER BY created_at DESC
|
||||
");
|
||||
$stmt_passengers->execute();
|
||||
$blocked_passengers = $stmt_passengers->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// فك التشفير عن الأرقام إذا كانت مشفرة
|
||||
foreach ($blocked_drivers as &$bd) {
|
||||
$decrypted_phone = $encryptionHelper->decryptData($bd['phone']);
|
||||
if ($decrypted_phone) $bd['phone'] = $decrypted_phone;
|
||||
}
|
||||
|
||||
foreach ($blocked_passengers as &$bp) {
|
||||
$decrypted_phone = $encryptionHelper->decryptData($bp['phone']);
|
||||
if ($decrypted_phone) $bp['phone'] = $decrypted_phone;
|
||||
}
|
||||
|
||||
jsonSuccess([
|
||||
'drivers' => $blocked_drivers,
|
||||
'passengers' => $blocked_passengers
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($action_type === 'unblock_driver') {
|
||||
$phone = filterRequest('phone');
|
||||
if (!$phone) jsonError("Phone is required");
|
||||
|
||||
$enc_phone = $encryptionHelper->encryptData($phone);
|
||||
|
||||
$stmt = $con->prepare("DELETE FROM blacklist_driver WHERE phone = ? OR phone = ?");
|
||||
$stmt->execute([$phone, $enc_phone]);
|
||||
|
||||
if ($stmt->rowCount() > 0) {
|
||||
// تسجيل في الـ Audit Log
|
||||
$log_stmt = $con->prepare("INSERT INTO admin_audit_log (admin_id, admin_phone, action, table_name, entity_type, details) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
$log_stmt->execute([
|
||||
$user_id, 'Admin', 'unblock_driver', 'blacklist_driver', 'driver',
|
||||
json_encode(['phone' => $phone, 'action' => 'Unblocked driver'])
|
||||
]);
|
||||
|
||||
jsonSuccess(null, "Driver unblocked successfully");
|
||||
} else {
|
||||
jsonError("Driver not found in blacklist");
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($action_type === 'unblock_passenger') {
|
||||
$phone_normalized = filterRequest('phone_normalized');
|
||||
if (!$phone_normalized) jsonError("Normalized Phone is required");
|
||||
|
||||
$stmt = $con->prepare("DELETE FROM passenger_blacklist WHERE phone_normalized = ?");
|
||||
$stmt->execute([$phone_normalized]);
|
||||
|
||||
if ($stmt->rowCount() > 0) {
|
||||
// تسجيل في الـ Audit Log
|
||||
$log_stmt = $con->prepare("INSERT INTO admin_audit_log (admin_id, admin_phone, action, table_name, entity_type, details) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
$log_stmt->execute([
|
||||
$user_id, 'Admin', 'unblock_passenger', 'passenger_blacklist', 'passenger',
|
||||
json_encode(['phone_normalized' => $phone_normalized, 'action' => 'Unblocked passenger'])
|
||||
]);
|
||||
|
||||
jsonSuccess(null, "Passenger unblocked successfully");
|
||||
} else {
|
||||
jsonError("Passenger not found in blacklist");
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
jsonError("Invalid action_type", 400);
|
||||
|
||||
} catch (Exception $e) {
|
||||
jsonError("Blacklist action failed: " . $e->getMessage(), 500);
|
||||
}
|
||||
?>
|
||||
105
backend/Admin/v2/quality/driver_scorecard.php
Normal file
105
backend/Admin/v2/quality/driver_scorecard.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
// Admin/v2/quality/driver_scorecard.php
|
||||
require_once __DIR__ . '/../../../connect.php';
|
||||
// require_once __DIR__ . '/../../../encrypt_decrypt.php';
|
||||
|
||||
// التحقق من الصلاحيات
|
||||
if ($role !== 'admin' && $role !== 'super_admin') {
|
||||
jsonError("Unauthorized", 403);
|
||||
}
|
||||
|
||||
$driver_id = filterRequest('driver_id');
|
||||
if (!$driver_id) {
|
||||
jsonError("Missing driver_id", 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$scorecard = [];
|
||||
|
||||
// 1. البيانات الأساسية للسائق
|
||||
$stmt = $con->prepare("
|
||||
SELECT id, first_name, last_name, phone, status, created_at, expiry_date
|
||||
FROM driver
|
||||
WHERE id = ?
|
||||
");
|
||||
$stmt->execute([$driver_id]);
|
||||
$driver = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$driver) {
|
||||
jsonError("Driver not found", 404);
|
||||
}
|
||||
|
||||
// فك التشفير للبيانات الأساسية
|
||||
if (!empty($driver['first_name'])) $driver['first_name'] = $encryptionHelper->decryptData($driver['first_name']) ?: $driver['first_name'];
|
||||
if (!empty($driver['last_name'])) $driver['last_name'] = $encryptionHelper->decryptData($driver['last_name']) ?: $driver['last_name'];
|
||||
if (!empty($driver['phone'])) $driver['phone'] = $encryptionHelper->decryptData($driver['phone']) ?: $driver['phone'];
|
||||
|
||||
$scorecard['basic_info'] = $driver;
|
||||
|
||||
// 2. إحصائيات الرحلات (نسبة الإنجاز والإلغاء)
|
||||
$stmt = $con->prepare("
|
||||
SELECT
|
||||
COUNT(*) as total_rides,
|
||||
SUM(CASE WHEN status = 'Finished' THEN 1 ELSE 0 END) as completed_rides,
|
||||
SUM(CASE WHEN status = 'cancel' AND cancel_by = 'driver' THEN 1 ELSE 0 END) as driver_cancellations,
|
||||
SUM(CASE WHEN status = 'cancel' AND cancel_by = 'passenger' THEN 1 ELSE 0 END) as passenger_cancellations
|
||||
FROM ride
|
||||
WHERE driver_id = ?
|
||||
");
|
||||
$stmt->execute([$driver_id]);
|
||||
$rides = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
// حساب نسبة الإنجاز
|
||||
$total = (int)$rides['total_rides'];
|
||||
$completed = (int)$rides['completed_rides'];
|
||||
$rides['completion_rate'] = $total > 0 ? round(($completed / $total) * 100, 2) : 0;
|
||||
|
||||
$scorecard['rides_stats'] = $rides;
|
||||
|
||||
// 3. التقييمات
|
||||
$stmt = $con->prepare("SELECT IFNULL(AVG(rating_driver), 0) as avg_rating FROM ride WHERE driver_id = ? AND rating_driver > 0");
|
||||
$stmt->execute([$driver_id]);
|
||||
$scorecard['rating'] = round($stmt->fetchColumn(), 2);
|
||||
|
||||
// 4. تحليل السلوك (Behavior)
|
||||
// نستخدم جدول driver_behavior لجمع المتوسطات
|
||||
$stmt = $con->prepare("
|
||||
SELECT
|
||||
IFNULL(AVG(behavior_score), 100) as avg_behavior_score,
|
||||
IFNULL(AVG(max_speed), 0) as avg_max_speed,
|
||||
IFNULL(SUM(hard_brakes), 0) as total_hard_brakes,
|
||||
IFNULL(SUM(rapid_accelerations), 0) as total_rapid_accel
|
||||
FROM driver_behavior
|
||||
WHERE driver_id = ?
|
||||
");
|
||||
$stmt->execute([$driver_id]);
|
||||
$scorecard['behavior'] = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
// 5. الشكاوى (Complaints)
|
||||
$stmt = $con->prepare("
|
||||
SELECT
|
||||
COUNT(*) as total_complaints,
|
||||
SUM(CASE WHEN statusComplaint = 'Open' THEN 1 ELSE 0 END) as open_complaints,
|
||||
SUM(CASE WHEN statusComplaint = 'Resolved' THEN 1 ELSE 0 END) as resolved_complaints
|
||||
FROM complaint
|
||||
WHERE driver_id = ?
|
||||
");
|
||||
$stmt->execute([$driver_id]);
|
||||
$scorecard['complaints'] = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
// 6. تقييم شامل (Overall Score) من 100
|
||||
// وزن التقييم: 40% إنجاز رحلات، 30% تقييم ركاب (محول لـ 100)، 30% سلوك قيادة، وخصم للشكاوى
|
||||
$completion_score = $rides['completion_rate'] * 0.4;
|
||||
$rating_score = ($scorecard['rating'] / 5) * 100 * 0.3;
|
||||
$behavior_score = $scorecard['behavior']['avg_behavior_score'] * 0.3;
|
||||
$complaint_penalty = $scorecard['complaints']['total_complaints'] * 5; // خصم 5 نقاط عن كل شكوى
|
||||
|
||||
$overall = $completion_score + $rating_score + $behavior_score - $complaint_penalty;
|
||||
$scorecard['overall_score'] = max(0, min(100, round($overall, 1)));
|
||||
|
||||
jsonSuccess($scorecard);
|
||||
|
||||
} catch (Exception $e) {
|
||||
jsonError("Failed to fetch scorecard: " . $e->getMessage(), 500);
|
||||
}
|
||||
?>
|
||||
62
backend/Admin/v2/realtime_dashboard.php
Normal file
62
backend/Admin/v2/realtime_dashboard.php
Normal 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()]);
|
||||
}
|
||||
?>
|
||||
66
backend/Admin/v2/security/audit_logs.php
Normal file
66
backend/Admin/v2/security/audit_logs.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
// Admin/v2/security/audit_logs.php
|
||||
|
||||
// ── سجل تتبع ────────────────────────────────────────────
|
||||
$debugFile = __DIR__ . '/../../../logs/audit_debug.txt';
|
||||
$logDir = dirname($debugFile);
|
||||
if (!is_dir($logDir)) @mkdir($logDir, 0777, true);
|
||||
|
||||
@file_put_contents($debugFile, "[" . date('Y-m-d H:i:s') . "] === REQUEST START ===\n", FILE_APPEND);
|
||||
|
||||
try {
|
||||
require_once __DIR__ . '/../../../connect.php';
|
||||
|
||||
@file_put_contents($debugFile, " → connect.php & encryption OK. user_id=$user_id | role=$role\n", FILE_APPEND);
|
||||
} catch (Exception $e) {
|
||||
@file_put_contents($debugFile, " → Loading FAILED: " . $e->getMessage() . "\n", FILE_APPEND);
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'failure', 'message' => 'loading failed: ' . $e->getMessage()]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// ── فحص الصلاحيات ────────────────────────────────────────
|
||||
if ($role !== 'super_admin' && $role !== 'admin') {
|
||||
@file_put_contents($debugFile, " → BLOCKED: role=$role\n", FILE_APPEND);
|
||||
jsonError("Unauthorized. role=$role", 403);
|
||||
}
|
||||
|
||||
try {
|
||||
// استعلام لجلب السجلات مع محاولة جلب الاسم من جدول الموظفين أو جدول المشرفين
|
||||
$stmt = $con->prepare("
|
||||
SELECT
|
||||
l.id, l.admin_id, l.action, l.table_name, l.record_id, l.details, l.created_at,
|
||||
COALESCE(e.name, au.name) as admin_name_raw
|
||||
FROM admin_audit_log l
|
||||
LEFT JOIN employee e ON l.admin_id COLLATE utf8mb4_general_ci = e.id COLLATE utf8mb4_general_ci
|
||||
LEFT JOIN adminUser au ON l.admin_id COLLATE utf8mb4_general_ci = au.id COLLATE utf8mb4_general_ci
|
||||
OR l.admin_id COLLATE utf8mb4_general_ci = au.email COLLATE utf8mb4_general_ci
|
||||
ORDER BY l.created_at DESC
|
||||
LIMIT 100
|
||||
");
|
||||
$stmt->execute();
|
||||
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// معالجة البيانات: فك تشفير الأسماء إذا كانت مشفرة
|
||||
foreach ($logs as &$log) {
|
||||
$rawName = $log['admin_name_raw'];
|
||||
if (!empty($rawName)) {
|
||||
// محاولة فك التشفير
|
||||
$decrypted = $encryptionHelper->decryptData($rawName);
|
||||
$log['admin_name'] = ($decrypted !== false) ? $decrypted : $rawName;
|
||||
} else {
|
||||
$log['admin_name'] = 'أدمن غير معروف';
|
||||
}
|
||||
unset($log['admin_name_raw']);
|
||||
}
|
||||
|
||||
$count = count($logs);
|
||||
@file_put_contents($debugFile, " → SUCCESS: fetched $count logs\n", FILE_APPEND);
|
||||
|
||||
jsonSuccess($logs);
|
||||
|
||||
} catch (Exception $e) {
|
||||
@file_put_contents($debugFile, " → QUERY ERROR: " . $e->getMessage() . "\n", FILE_APPEND);
|
||||
jsonError('Query failed: ' . $e->getMessage(), 500);
|
||||
}
|
||||
?>
|
||||
77
backend/Admin/v2/smart_alerts.php
Normal file
77
backend/Admin/v2/smart_alerts.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?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) {
|
||||
// فك تشفير البيانات الحساسة
|
||||
$firstName = $encryptionHelper->decryptData($d['first_name']);
|
||||
$lastName = $encryptionHelper->decryptData($d['last_name']);
|
||||
|
||||
$alerts[] = [
|
||||
'type' => 'license',
|
||||
'severity' => 'warning',
|
||||
'title' => 'رخصة كابتن قاربت على الانتهاء',
|
||||
'description' => "رخصة الكابتن " . $firstName . " " . $lastName . " ستنتهي بتاريخ " . $d['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()]);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user