feat: integrate seasonal surge multipliers and grid-specific commission discounts into pricing logic
This commit is contained in:
@@ -118,6 +118,12 @@ function calculateDynamicPrice($country, $minFare, $distance, $duration, $kazanR
|
||||
global $redis, $redisLocation, $con;
|
||||
|
||||
$surgeMultiplier = 1.0;
|
||||
$kazanDiscountFactor = 1.0;
|
||||
|
||||
// كود الدولة المستخدم بمفاتيح Redis (surge:seasonal:{CC}, surge:kazan_discounts:{CC})
|
||||
$countryCodeMap = ['Jordan' => 'JO', 'Syria' => 'SY', 'Egypt' => 'EG', 'Iraq' => 'IQ'];
|
||||
$countryCode = $countryCodeMap[$kazanRow['country'] ?? ''] ?? strtoupper(substr($kazanRow['country'] ?? '', 0, 2));
|
||||
|
||||
if (isset($redis) && $redis !== null) {
|
||||
try {
|
||||
$refLat = 33.5;
|
||||
@@ -125,11 +131,11 @@ function calculateDynamicPrice($country, $minFare, $distance, $duration, $kazanR
|
||||
$grid_lat = round((float)$passengerLat / $grid_size) * $grid_size;
|
||||
$grid_lng = round((float)$passengerLng / $grid_size) * $grid_size;
|
||||
$grid_id = $grid_lat . "_" . $grid_lng;
|
||||
|
||||
|
||||
// Demand is handled by Main Redis (prefix automatically applied)
|
||||
$demandCount = (int)$redis->get("demand:grid:" . $grid_id);
|
||||
$availableDrivers = 0;
|
||||
|
||||
|
||||
// Check competitor surge opportunities
|
||||
$competitorSurgeMultiplier = 1.0;
|
||||
$surgeOpsJson = $redis->get("surge:opportunities");
|
||||
@@ -139,7 +145,7 @@ function calculateDynamicPrice($country, $minFare, $distance, $duration, $kazanR
|
||||
$competitorSurgeMultiplier = (float)$surgeOps[$grid_id];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Driver locations are handled by Location Redis (no prefix)
|
||||
try {
|
||||
if (isset($redisLocation) && $redisLocation !== null) {
|
||||
@@ -147,7 +153,7 @@ function calculateDynamicPrice($country, $minFare, $distance, $duration, $kazanR
|
||||
$availableDrivers = count($drivers);
|
||||
}
|
||||
} catch (Exception $e) {}
|
||||
|
||||
|
||||
if ($demandCount > 0) {
|
||||
$surgeRatio = ($availableDrivers > 0) ? ($demandCount / $availableDrivers) : $demandCount;
|
||||
if ($surgeRatio > 1.2) {
|
||||
@@ -155,18 +161,40 @@ function calculateDynamicPrice($country, $minFare, $distance, $duration, $kazanR
|
||||
$surgeMultiplier = min(3.0, $surgeMultiplier); // Cap at 3.0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Auto-Adaptive Pricing: Apply competitor surge if it's higher
|
||||
if ($competitorSurgeMultiplier > $surgeMultiplier) {
|
||||
$surgeMultiplier = $competitorSurgeMultiplier;
|
||||
}
|
||||
|
||||
// التسعير الموسمي (رمضان/عيد/طقس سيء) — cron_seasonal_pricing.php
|
||||
// نطبّق الأعلى بين مضاعف الذروة الحالي والمضاعف الموسمي، بنفس منطق
|
||||
// "خذ الأعلى" المستخدم أصلاً بين الطلب الداخلي وذروة المنافسين
|
||||
$seasonalJson = $redis->get("surge:seasonal:{$countryCode}");
|
||||
if ($seasonalJson) {
|
||||
$seasonalData = json_decode($seasonalJson, true);
|
||||
$seasonalMultiplier = (float)($seasonalData['multiplier'] ?? 1.0);
|
||||
if ($seasonalMultiplier > $surgeMultiplier) {
|
||||
$surgeMultiplier = $seasonalMultiplier;
|
||||
}
|
||||
}
|
||||
|
||||
// خصم العمولة بمناطق ذروة المنافسين — cron_kazan_adjuster.php
|
||||
// خريطة grid_id → معامل خصم (0.70 أو 0.85) لنفس الـ grid_id المحسوب فوق
|
||||
$kazanDiscountsJson = $redis->get("surge:kazan_discounts:{$countryCode}");
|
||||
if ($kazanDiscountsJson) {
|
||||
$kazanDiscounts = json_decode($kazanDiscountsJson, true);
|
||||
if (is_array($kazanDiscounts) && isset($kazanDiscounts[$grid_id])) {
|
||||
$kazanDiscountFactor = (float)$kazanDiscounts[$grid_id];
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {}
|
||||
}
|
||||
|
||||
$naturePrice = (float) ($kazanRow['normalMinPrice'] ?? 0);
|
||||
$heavyPrice = (float) ($kazanRow['peakMinPrice'] ?? 0);
|
||||
$latePrice = (float) ($kazanRow['lateMinPrice'] ?? 0);
|
||||
$kazanPercent = (float) ($kazanRow['kazanPercent'] ?? 10);
|
||||
$kazanPercent = (float) ($kazanRow['kazanPercent'] ?? 10) * $kazanDiscountFactor;
|
||||
$startPrice = (float) ($kazanRow['startPrice'] ?? 0);
|
||||
|
||||
// === General Settings ===
|
||||
|
||||
Reference in New Issue
Block a user