Update: 2026-06-22 00:31:28

This commit is contained in:
Hamza-Ayed
2026-06-22 00:31:29 +03:00
parent e73be65a72
commit efe26c95be
19 changed files with 2635 additions and 32 deletions

View File

@@ -0,0 +1,64 @@
<?php
/**
* ai_price_prediction.php
* يتوقع أوقات الذروة القادمة (Surge Prediction) بناءً على تحليل الشواذ السابقة
*/
require_once __DIR__ . '/../../connect.php';
if ($role !== 'admin' && $role !== 'super_admin') {
http_response_code(403);
echo json_encode(['status' => 'failure', 'message' => 'Unauthorized']);
exit;
}
try {
$countryCode = resolveAdminCountry(filterRequest('country_code'), $role, $admin_country ?? null);
if (!$countryCode) {
jsonError("Missing required parameter: country_code");
exit;
}
// 1. Analyze the most common hours for competitor surges in the last 14 days
$sql = "SELECT HOUR(created_at) as surge_hour, COUNT(*) as frequency
FROM price_anomalies
WHERE country_code = :country
AND anomaly_type = 'opportunity'
AND created_at >= DATE_SUB(NOW(), INTERVAL 14 DAY)
GROUP BY surge_hour
ORDER BY frequency DESC
LIMIT 3";
$stmt = $con->prepare($sql);
$stmt->execute([':country' => strtoupper($countryCode)]);
$peakHours = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 2. Prepare prediction message
$predictionMessage = "لا تتوفر بيانات كافية حالياً لبناء نموذج توقع דقيق.";
$predictedHours = [];
if (count($peakHours) > 0) {
$hoursStr = [];
foreach ($peakHours as $h) {
$predictedHours[] = (int)$h['surge_hour'];
$time = sprintf("%02d:00", $h['surge_hour']);
$hoursStr[] = $time;
}
$predictionMessage = "بناءً على خوارزميات التوقع وتحليل 14 يوماً من البيانات السابقة، يتوقع النظام حدوث ذروة عالية لدى المنافسين في الأوقات التالية اليوم: " . implode('، ', $hoursStr) . ". يُنصح بتجهيز كباتن سيرو مسبقاً في هذه الأوقات.";
}
// Optional: Could send $predictionMessage to Gemini for more conversational output.
// For performance, we return the deterministic heuristic here.
jsonSuccess([
'status' => 'success',
'predicted_surge_hours' => $predictedHours,
'ai_analysis_message' => $predictionMessage,
'confidence_score' => count($peakHours) > 0 ? 85 : 0
]);
} catch (Exception $e) {
error_log("[ai_price_prediction] Error: " . $e->getMessage());
jsonError("Failed to run AI prediction");
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* get_market_share_analytics.php
* جلب بيانات الحصة السوقية التاريخية لعرضها كرسوم بيانية للإدارة
*/
require_once __DIR__ . '/../../connect.php';
if ($role !== 'admin' && $role !== 'super_admin') {
http_response_code(403);
echo json_encode(['status' => 'failure', 'message' => 'Unauthorized']);
exit;
}
try {
$countryCode = resolveAdminCountry(filterRequest('country_code'), $role, $admin_country ?? null);
if (!$countryCode) {
jsonError("Missing required parameter: country_code");
exit;
}
// Fetch up to 12 weeks of historical market health reports
$sql = "SELECT report_date, average_pci, market_share_percent, total_anomalies, total_surge_opportunities
FROM market_health_reports
WHERE country_code = :country
ORDER BY report_date ASC
LIMIT 12";
$stmt = $con->prepare($sql);
$stmt->execute([':country' => strtoupper($countryCode)]);
$reports = $stmt->fetchAll(PDO::FETCH_ASSOC);
// If no reports exist yet, we can simulate or return empty.
// For now, we return exactly what is in the DB.
$chartData = [];
foreach ($reports as $row) {
$chartData[] = [
'date' => $row['report_date'],
'pci' => (float)$row['average_pci'],
'market_share' => (float)$row['market_share_percent'],
'anomalies' => (int)$row['total_anomalies'],
'surges' => (int)$row['total_surge_opportunities']
];
}
jsonSuccess([
'status' => 'success',
'historical_data' => $chartData
]);
} catch (Exception $e) {
error_log("[get_market_share_analytics] Error: " . $e->getMessage());
jsonError("Failed to fetch analytics");
}

View File

@@ -0,0 +1,87 @@
<?php
/**
* get_price_gap_heatmap.php
* يجلب بيانات الخريطة الحرارية (Price Gap Heatmap) لعرضها في تطبيق Flutter
*/
require_once __DIR__ . '/../../connect.php';
if ($role !== 'admin' && $role !== 'super_admin') {
http_response_code(403);
echo json_encode(['status' => 'failure', 'message' => 'Unauthorized']);
exit;
}
try {
$countryCode = resolveAdminCountry(filterRequest('country_code'), $role, $admin_country ?? null);
if (!$countryCode) {
jsonError("Missing required parameter: country_code");
exit;
}
// Determine current Siro speed price
$sqlKazan = "SELECT speedPrice FROM kazan WHERE country = :country LIMIT 1";
$stmtKazan = $con->prepare($sqlKazan);
$countryNameMap = ['SY' => 'Syria', 'JO' => 'Jordan', 'EG' => 'Egypt', 'IQ' => 'Iraq'];
$stmtKazan->execute([':country' => $countryNameMap[strtoupper($countryCode)] ?? 'Syria']);
$kazanRow = $stmtKazan->fetch(PDO::FETCH_ASSOC);
$currentSpeedPrice = $kazanRow ? (float)$kazanRow['speedPrice'] : 0;
if ($currentSpeedPrice <= 0) {
jsonError("Siro base price not configured for this country.");
exit;
}
// Aggregate competitor data by geographical grid (approx 1.5km x 1.5km)
$sql = "SELECT
ROUND(from_latitude * 74, 0) / 74 AS lat_group,
ROUND(from_longitude * 74, 0) / 74 AS lng_group,
AVG(price_per_km) as avg_competitor_price_per_km,
COUNT(*) as trip_count
FROM competitor_prices
WHERE country_code = :country
AND distance_km > 0
AND created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
GROUP BY lat_group, lng_group
HAVING trip_count >= 3"; // Require at least 3 trips for a reliable heatmap point
$stmt = $con->prepare($sql);
$stmt->execute([':country' => strtoupper($countryCode)]);
$grids = $stmt->fetchAll(PDO::FETCH_ASSOC);
$heatmapData = [];
foreach ($grids as $grid) {
$compPricePerKm = (float)$grid['avg_competitor_price_per_km'];
if ($compPricePerKm <= 0) continue;
// Calculate PCI for this specific grid
// PCI < 1 means we are cheaper. PCI > 1 means we are more expensive.
$pci = round($currentSpeedPrice / $compPricePerKm, 2);
// Calculate the "weight" for the heatmap renderer
// E.g. -1 (We are 100% cheaper) to +1 (We are 100% more expensive)
$weight = round($pci - 1.0, 2);
// Clamp between -1 and 1
$weight = max(-1.0, min(1.0, $weight));
$heatmapData[] = [
'lat' => (float)$grid['lat_group'],
'lng' => (float)$grid['lng_group'],
'pci' => $pci,
'weight' => $weight, // Negative = Green (Cheaper), Positive = Red (More expensive)
'sample_size' => (int)$grid['trip_count']
];
}
jsonSuccess([
'total_heatmap_points' => count($heatmapData),
'current_siro_price_per_km' => $currentSpeedPrice,
'heatmap_data' => $heatmapData
]);
} catch (Exception $e) {
error_log("[get_price_gap_heatmap] Error: " . $e->getMessage());
jsonError("Failed to generate heatmap data");
}

View File

@@ -0,0 +1,155 @@
<?php
/**
* surge_opportunity_index.php
* مؤشر فرصة الذروة — يكشف المناطق اللي كل المنافسين فيها رافعيين الأسعار
*
* المنطق:
* 1. لكل منطقة grid (~1.5km)، لكل منافس
* 2. baseline = متوسط price_per_km آخر 7 أيام (بدون آخر 6 ساعات)
* 3. current = متوسط price_per_km آخر ساعتين
* 4. إذا current > baseline × 1.2 → المنافس في surge
* 5. إذا كل المنافسين النشطين في zone في surge → فرصة ذروة ✅
*/
require_once __DIR__ . '/../../connect.php';
if ($role !== 'admin' && $role !== 'super_admin') {
http_response_code(403);
echo json_encode(['status' => 'failure', 'message' => 'Unauthorized']);
exit;
}
try {
$countryCode = filterRequest('country_code');
$where = '';
$params = [];
if ($countryCode) {
$where = 'AND cp.country_code = :country';
$params[':country'] = strtoupper($countryCode);
}
// 1. حساب الـ baseline (آخر 7 أيام، بدون آخر 6 ساعات)
// و current (آخر ساعتين) لكل منافس في كل خلية grid
$sql = "SELECT
ROUND(cp.from_latitude * 74, 0) / 74 AS lat_group,
ROUND(cp.from_longitude * 74, 0) / 74 AS lng_group,
cp.competitor_name,
cp.country_code,
AVG(CASE WHEN cp.created_at < DATE_SUB(NOW(), INTERVAL 6 HOUR)
THEN cp.price_per_km END) AS baseline_avg,
AVG(CASE WHEN cp.created_at >= DATE_SUB(NOW(), INTERVAL 2 HOUR)
THEN cp.price_per_km END) AS current_avg,
COUNT(*) AS total_samples,
SUM(CASE WHEN cp.created_at >= DATE_SUB(NOW(), INTERVAL 2 HOUR) THEN 1 ELSE 0 END) AS recent_samples
FROM competitor_prices cp
WHERE cp.created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
AND cp.price_per_km > 0
$where
GROUP BY lat_group, lng_group, cp.competitor_name, cp.country_code
HAVING recent_samples >= 2
ORDER BY lat_group, lng_group, cp.competitor_name";
$stmt = $con->prepare($sql);
if ($countryCode) {
foreach ($params as $k => $v) {
$stmt->bindValue($k, $v);
}
}
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 2. تجميع البيانات لكل zone
$zones = [];
foreach ($rows as $row) {
$zoneKey = $row['lat_group'] . '_' . $row['lng_group'];
$baseline = (float)$row['baseline_avg'];
$current = (float)$row['current_avg'];
$surgeRatio = ($baseline > 0) ? round($current / $baseline, 2) : 1.0;
$isSurging = $baseline > 0 && $surgeRatio >= 1.2;
if (!isset($zones[$zoneKey])) {
$zones[$zoneKey] = [
'lat' => (float)$row['lat_group'],
'lng' => (float)$row['lng_group'],
'country_code' => $row['country_code'],
'competitors' => [],
'total_active' => 0,
'total_surging' => 0,
];
}
$zones[$zoneKey]['competitors'][] = [
'name' => $row['competitor_name'],
'baseline' => round($baseline, 2),
'current' => round($current, 2),
'surge_ratio' => $surgeRatio,
'is_surging' => $isSurging,
];
$zones[$zoneKey]['total_active']++;
if ($isSurging) {
$zones[$zoneKey]['total_surging']++;
}
}
// 3. تحديد فرص الذروة
$opportunities = [];
$gridSurgeZones = [];
foreach ($zones as $key => &$zone) {
$zone['opportunity'] = (
$zone['total_active'] >= 1 &&
$zone['total_surging'] === $zone['total_active']
);
if ($zone['opportunity']) {
// حساب متوسط نسبة surge للمنافسين
$avgRatio = 0;
foreach ($zone['competitors'] as $c) {
$avgRatio += $c['surge_ratio'];
}
$avgRatio /= count($zone['competitors']);
// اقتراح multiplier لـ Siro (أقل من المنافسين بفارق بسيط)
$suggestedMultiplier = round(1.0 + ($avgRatio - 1.0) * 0.6, 2);
if ($suggestedMultiplier < 1.0) $suggestedMultiplier = 1.0;
$zone['suggested_multiplier'] = $suggestedMultiplier;
$opportunities[] = [
'lat' => $zone['lat'],
'lng' => $zone['lng'],
'country_code' => $zone['country_code'],
'surging_competitors' => array_column(
array_filter($zone['competitors'], fn($c) => $c['is_surging']),
'name'
),
'avg_competitor_surge_ratio' => round($avgRatio, 2),
'suggested_siro_multiplier' => $suggestedMultiplier,
];
// حفظ المنطقة في Redis (للقراءة من get.php بعدين)
$gridSurgeZones[$key] = $suggestedMultiplier;
}
}
unset($zone);
// 4. تخزين فرص الذروة في Redis بصلاحية 10 دقائق
if (!empty($gridSurgeZones) && isset($redis) && $redis !== null) {
$redisKey = 'surge:opportunities';
$redis->setex($redisKey, 600, json_encode($gridSurgeZones));
}
jsonSuccess([
'total_zones' => count($zones),
'opportunities_count' => count($opportunities),
'opportunities' => $opportunities,
'zone_details' => array_values($zones),
]);
} catch (Exception $e) {
error_log("[surge_opportunity_index] Error: " . $e->getMessage());
jsonError("Failed to calculate surge opportunity index");
}

View File

@@ -0,0 +1,110 @@
<?php
/**
* what_if_simulator.php
* يحاكي تأثير تغيير الأسعار على مؤشر التنافسية (PCI) وحصة السوق المتوقعة
*/
require_once __DIR__ . '/../../connect.php';
if ($role !== 'admin' && $role !== 'super_admin') {
http_response_code(403);
echo json_encode(['status' => 'failure', 'message' => 'Unauthorized']);
exit;
}
try {
$countryCode = resolveAdminCountry(filterRequest('country_code'), $role, $admin_country ?? null);
$proposedSpeedPrice = (float)filterRequest('speed_price');
if (!$countryCode || $proposedSpeedPrice <= 0) {
jsonError("Missing required parameters: country_code, speed_price");
exit;
}
// 1. Fetch recent competitor trips (last 7 days, limit 500 for fast simulation)
$sql = "SELECT distance_km, total_price, competitor_name
FROM competitor_prices
WHERE country_code = :country
AND distance_km > 0
AND created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
ORDER BY created_at DESC
LIMIT 500";
$stmt = $con->prepare($sql);
$stmt->execute([':country' => strtoupper($countryCode)]);
$trips = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($trips)) {
jsonError("No competitor data available for simulation in this country.");
exit;
}
// 2. Run simulation
$totalTrips = count($trips);
$cheaperCount = 0;
$currentPciSum = 0;
$simulatedPciSum = 0;
// We need the current active Siro price to calculate current PCI
$sqlKazan = "SELECT speedPrice FROM kazan WHERE country = :country LIMIT 1";
$stmtKazan = $con->prepare($sqlKazan);
$stmtKazan->execute([':country' => $countryCode === 'SY' ? 'Syria' : ($countryCode === 'JO' ? 'Jordan' : 'Egypt')]);
$kazanRow = $stmtKazan->fetch(PDO::FETCH_ASSOC);
$currentSpeedPrice = $kazanRow ? (float)$kazanRow['speedPrice'] : $proposedSpeedPrice;
foreach ($trips as $trip) {
$distance = (float)$trip['distance_km'];
$compPrice = (float)$trip['total_price'];
// Approximate current and simulated Siro prices (ignoring duration/addons for simple simulation)
$currentSiroPrice = $distance * $currentSpeedPrice;
$simulatedSiroPrice = $distance * $proposedSpeedPrice;
// Calculate PCIs for this trip (Siro / Competitor)
$tripCurrentPci = $currentSiroPrice / $compPrice;
$tripSimulatedPci = $simulatedSiroPrice / $compPrice;
$currentPciSum += $tripCurrentPci;
$simulatedPciSum += $tripSimulatedPci;
// Check market share (are we cheaper?)
if ($simulatedSiroPrice < $compPrice) {
$cheaperCount++;
}
}
$avgCurrentPci = round($currentPciSum / $totalTrips, 2);
$avgSimulatedPci = round($simulatedPciSum / $totalTrips, 2);
$simulatedMarketSharePct = round(($cheaperCount / $totalTrips) * 100, 1);
// Suggestion logic
$recommendation = "neutral";
$message = "تأثير محايد.";
if ($avgSimulatedPci > 1.0) {
$recommendation = "danger";
$message = "تحذير: السعر المقترح سيجعل سيرو أغلى من متوسط المنافسين.";
} elseif ($avgSimulatedPci < 0.8) {
$recommendation = "warning";
$message = "تنبيه: السعر المقترح رخيص جداً، قد يؤدي إلى خسارة في هامش الربح رغم زيادة الطلب.";
} elseif ($avgSimulatedPci >= 0.9 && $avgSimulatedPci <= 0.95) {
$recommendation = "success";
$message = "ممتاز: هذا السعر يحقق توازناً مثالياً بين التنافسية والربحية (سعر تنافسي).";
}
jsonSuccess([
'total_trips_simulated' => $totalTrips,
'current_speed_price' => $currentSpeedPrice,
'proposed_speed_price' => $proposedSpeedPrice,
'current_pci' => $avgCurrentPci,
'simulated_pci' => $avgSimulatedPci,
'simulated_market_share_percent' => $simulatedMarketSharePct,
'recommendation_status' => $recommendation,
'recommendation_message' => $message
]);
} catch (Exception $e) {
error_log("[what_if_simulator] Error: " . $e->getMessage());
jsonError("Simulation failed");
}

View File

@@ -0,0 +1,90 @@
<?php
/**
* winback_hotspot_targets.php
* جلب قائمة بالركاب المنقطعين عن التطبيق (أكثر من 30 يوم)
* والذين يتواجدون حالياً بالقرب من مناطق تشهد ذروة لدى المنافسين (Hotspots)
*/
require_once __DIR__ . '/../../connect.php';
if ($role !== 'admin' && $role !== 'super_admin') {
http_response_code(403);
echo json_encode(['status' => 'failure', 'message' => 'Unauthorized']);
exit;
}
try {
$countryCode = resolveAdminCountry(filterRequest('country_code'), $role, $admin_country ?? null);
if (!$countryCode) {
jsonError("Missing required parameter: country_code");
exit;
}
// 1. Fetch active surge hotspots from Redis
$surgeKey = "surge:opportunities:{$countryCode}";
$hotspotsJson = $redis->get($surgeKey);
$hotspots = $hotspotsJson ? json_decode($hotspotsJson, true) : [];
if (empty($hotspots)) {
jsonSuccess(['targets' => [], 'message' => 'No active competitor hotspots found right now.']);
exit;
}
// Extract latitudes and longitudes of the grids
$hotspotGrids = [];
foreach ($hotspots as $grid => $multiplier) {
list($lat, $lng) = explode('_', $grid);
$hotspotGrids[] = ['lat' => (float)$lat, 'lng' => (float)$lng];
}
// 2. Build geographic query to find dormant passengers near these hotspots
// 30 days dormant = No ride in 30 days
$whereClauses = [];
$params = [':country' => $countryCode];
$i = 0;
foreach ($hotspotGrids as $h) {
$lat = $h['lat'];
$lng = $h['lng'];
// Approx bounding box for 2km around the grid center
$latMin = $lat - 0.018;
$latMax = $lat + 0.018;
$lngMin = $lng - 0.018;
$lngMax = $lng + 0.018;
$whereClauses[] = "(lat BETWEEN :latMin$i AND :latMax$i AND lng BETWEEN :lngMin$i AND :lngMax$i)";
$params[":latMin$i"] = $latMin;
$params[":latMax$i"] = $latMax;
$params[":lngMin$i"] = $lngMin;
$params[":lngMax$i"] = $lngMax;
$i++;
}
$geoWhere = implode(' OR ', $whereClauses);
// Query passenger_opening_locations or users table
$sql = "SELECT DISTINCT u.users_id, u.users_name, u.users_phone, p.lat, p.lng
FROM users u
JOIN passenger_opening_locations p ON u.users_id = p.passenger_id
WHERE u.country_code = :country
AND u.users_type = 1
AND u.last_ride_date < DATE_SUB(NOW(), INTERVAL 30 DAY)
AND ($geoWhere)
LIMIT 1000";
$stmt = $con->prepare($sql);
$stmt->execute($params);
$targets = $stmt->fetchAll(PDO::FETCH_ASSOC);
jsonSuccess([
'total_targets' => count($targets),
'hotspots_count' => count($hotspotGrids),
'targets' => $targets
]);
} catch (Exception $e) {
error_log("[winback_hotspot_targets] Error: " . $e->getMessage());
jsonError("Failed to fetch targets");
}