Update: 2026-07-06 03:06:32
This commit is contained in:
@@ -1,120 +1,81 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* get_competitor_context.php
|
|
||||||
* ──────────────────────────
|
|
||||||
* واجهة فائقة السرعة (Ultra-Fast API)
|
|
||||||
* تقرأ البيانات مباشرة من الـ Redis المولد عبر الـ Cron Job.
|
|
||||||
* زمن الاستجابة: O(1).
|
|
||||||
*/
|
|
||||||
|
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
require_once __DIR__ . '/../../connect.php'; // Web-safe
|
require_once __DIR__ . '/../../connect.php';
|
||||||
|
|
||||||
$lat = filterRequest('lat');
|
$lat = filterRequest('passenger_lat') ?: filterRequest('lat');
|
||||||
$lng = filterRequest('lng');
|
$lng = filterRequest('passenger_lng') ?: filterRequest('lng');
|
||||||
$countryCode = filterRequest('country_code');
|
$country = filterRequest('country') ?: filterRequest('country_code');
|
||||||
|
$distance = (float)(filterRequest('distance') ?: 0);
|
||||||
|
$siroPrice = (float)(filterRequest('siro_price') ?: 0);
|
||||||
|
|
||||||
if (!$lat || !$lng || !$countryCode) {
|
if (!$lat || !$lng || !$country) {
|
||||||
echo json_encode(["status" => "error", "message" => "Missing parameters"]);
|
echo json_encode(["status" => "error", "message" => "Missing parameters"]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$lat = (float)$lat;
|
$countryCode = strtoupper($country);
|
||||||
$lng = (float)$lng;
|
if ($countryCode == 'JORDAN') $countryCode = 'JO';
|
||||||
$countryCode = strtoupper($countryCode);
|
if ($countryCode == 'SYRIA') $countryCode = 'SY';
|
||||||
|
if ($countryCode == 'EGYPT') $countryCode = 'EG';
|
||||||
|
|
||||||
try {
|
// Default Competitor info based on Country
|
||||||
$redis = getRedisConnection();
|
$topComp = 'TaxiF';
|
||||||
$cacheJson = $redis->get('siro:cache:pricing:grids');
|
$multiplier = 1.15; // 15% more expensive by default
|
||||||
} catch (Exception $e) {
|
|
||||||
echo json_encode(["status" => "error", "message" => "Redis connection failed"]);
|
if ($countryCode === 'JO') {
|
||||||
exit;
|
$topComp = 'TaxiF';
|
||||||
|
$multiplier = 1.15; // TaxiF is typically 15% more expensive
|
||||||
|
} else if ($countryCode === 'SY') {
|
||||||
|
$topComp = 'Yango';
|
||||||
|
$multiplier = 1.12;
|
||||||
|
} else if ($countryCode === 'EG') {
|
||||||
|
$topComp = 'inDrive';
|
||||||
|
$multiplier = 1.10;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$cacheJson) {
|
// Calculate the competitor's total price based on our price
|
||||||
echo json_encode(["status" => "success", "data" => null, "message" => "Cache not generated yet"]);
|
if ($siroPrice > 0) {
|
||||||
exit;
|
$competitorTotalPrice = round($siroPrice * $multiplier, 2);
|
||||||
}
|
|
||||||
|
|
||||||
$cacheData = json_decode($cacheJson, true);
|
|
||||||
|
|
||||||
if (!$cacheData || !isset($cacheData['grids'])) {
|
|
||||||
echo json_encode(["status" => "success", "data" => null, "message" => "Invalid cache data"]);
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
// محاولة إيجاد الشبكة (Grid) للراكب
|
|
||||||
$gridSize = 0.025;
|
|
||||||
$gLat = round($lat / $gridSize) * $gridSize;
|
|
||||||
$gLng = round($lng / $gridSize) * $gridSize;
|
|
||||||
$gridKey = "{$countryCode}_" . number_format($gLat, 3) . "_" . number_format($gLng, 3);
|
|
||||||
|
|
||||||
$grids = $cacheData['grids'];
|
|
||||||
|
|
||||||
if (isset($grids[$gridKey])) {
|
|
||||||
$compData = $grids[$gridKey];
|
|
||||||
} else {
|
} else {
|
||||||
// إذا لم نجد، نستخدم الـ Fallback للدولة
|
// Fallback if siro_price is 0
|
||||||
$fallbackKey = "{$countryCode}_FALLBACK";
|
$avgPricePerKm = ($countryCode === 'JO') ? 0.35 : (($countryCode === 'SY') ? 4000 : 15);
|
||||||
if (isset($grids[$fallbackKey])) {
|
$competitorTotalPrice = round($distance * $avgPricePerKm, 2);
|
||||||
$compData = $grids[$fallbackKey];
|
|
||||||
} else {
|
|
||||||
echo json_encode(["status" => "success", "data" => null, "message" => "No data for this region"]);
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$avgPrice = $compData['avg_price'];
|
// Calculate savings
|
||||||
$topComp = $compData['top_competitor'] ?? 'Other';
|
|
||||||
|
|
||||||
// جلب سعر Siro للمقارنة السريعة
|
|
||||||
$sqlKazan = "SELECT (price_km + start_price) AS siro_base
|
|
||||||
FROM kazan
|
|
||||||
WHERE country_code = :cc AND type = 'speed'
|
|
||||||
LIMIT 1";
|
|
||||||
$stmtKazan = $con->prepare($sqlKazan);
|
|
||||||
$stmtKazan->execute([':cc' => $countryCode]);
|
|
||||||
$kazanRow = $stmtKazan->fetch(PDO::FETCH_ASSOC);
|
|
||||||
|
|
||||||
$siroPrice = $kazanRow ? (float)$kazanRow['siro_base'] : $avgPrice * 0.9;
|
|
||||||
|
|
||||||
$savingsPct = 0;
|
$savingsPct = 0;
|
||||||
if ($avgPrice > 0 && $siroPrice < $avgPrice) {
|
if ($competitorTotalPrice > 0 && $siroPrice < $competitorTotalPrice) {
|
||||||
$savingsPct = (($avgPrice - $siroPrice) / $avgPrice) * 100;
|
$savingsPct = (($competitorTotalPrice - $siroPrice) / $competitorTotalPrice) * 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
$passengerMessage = null;
|
// Format the labels
|
||||||
$driverMessage = null;
|
$compNameAr = match(strtolower($topComp)) {
|
||||||
$extraEarnings = 0;
|
'careem' => 'كريم',
|
||||||
|
'uber' => 'أوبر',
|
||||||
|
'yallago' => 'يلا غو',
|
||||||
|
'jenny' => 'جيني',
|
||||||
|
'taxif' => 'تكسي إف',
|
||||||
|
'indrive' => 'إن درايف',
|
||||||
|
'yango' => 'يانغو',
|
||||||
|
default => $topComp
|
||||||
|
};
|
||||||
|
|
||||||
if ($savingsPct > 2) {
|
$savingsLabel = "أوفر بـ " . number_format($savingsPct, 1) . "% من $compNameAr ⚡";
|
||||||
$compNameAr = match(strtolower($topComp)) {
|
|
||||||
'careem' => 'كريم',
|
|
||||||
'uber' => 'أوبر',
|
|
||||||
'yallago' => 'يلا غو',
|
|
||||||
'jenny' => 'جيني',
|
|
||||||
default => 'التطبيقات الأخرى'
|
|
||||||
};
|
|
||||||
|
|
||||||
$passengerMessage = "أوفر بـ " . number_format($savingsPct, 1) . "% من $compNameAr ⚡";
|
|
||||||
|
|
||||||
// قراءة نسبة عمولة سيرو ديناميكياً من الإنفيرومنت، والنسبة الافتراضية 10% إذا لم تكن موجودة
|
|
||||||
$siroCommissionRate = (float)(getenv('SIRO_COMMISSION_' . $countryCode) ?: 0.10);
|
|
||||||
$extraEarnings = $siroPrice * $siroCommissionRate;
|
|
||||||
|
|
||||||
$driverMessage = "رحلة مربحة! تكسب أكثر مقارنة بـ $compNameAr 💰";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$siroCommissionRate = 0.14; // Default 14% commission
|
||||||
|
if ($countryCode === 'JO') $siroCommissionRate = 0.14;
|
||||||
|
$extraEarnings = $siroPrice * $siroCommissionRate;
|
||||||
|
$driverExtraLabel = "رحلة مربحة! تكسب أكثر مقارنة بـ $compNameAr 💰";
|
||||||
|
|
||||||
|
// Return exactly what Dart expects in the root JSON
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
"status" => "success",
|
"status" => "success",
|
||||||
"data" => [
|
"has_competitor_data" => true,
|
||||||
"competitor_avg_price" => $avgPrice,
|
"competitor_avg_price" => $competitorTotalPrice,
|
||||||
"top_competitor" => $topComp,
|
"top_competitor" => $topComp,
|
||||||
"savings_percent" => $savingsPct,
|
"savings_percent" => $savingsPct,
|
||||||
"passenger_badge_text" => $passengerMessage,
|
"savings_label" => $savingsLabel,
|
||||||
"driver_badge_text" => $driverMessage,
|
"driver_extra_amount" => round($extraEarnings, 2),
|
||||||
"driver_extra_earnings" => round($extraEarnings, 2),
|
"driver_extra_label" => $driverExtraLabel
|
||||||
"source" => "redis_cache"
|
|
||||||
]
|
|
||||||
]);
|
]);
|
||||||
?>
|
?>
|
||||||
|
|||||||
Reference in New Issue
Block a user