Update: 2026-06-21 02:07:00

This commit is contained in:
Hamza-Ayed
2026-06-21 02:07:00 +03:00
parent af3dcae5b7
commit b2fae9ec66
23 changed files with 1412 additions and 210 deletions

View File

@@ -109,6 +109,38 @@ function getPerKmRate($carType, $kazanRow) {
}
function calculateDynamicPrice($country, $minFare, $distance, $duration, $kazanRow, $startNameAddress, $endNameAddress, $destLat, $destLng, $passengerLat, $passengerLng, $carType = 'Speed') {
global $redis, $redisLocation;
$surgeMultiplier = 1.0;
if (isset($redis) && $redis !== null) {
try {
$grid_size = 0.0135;
$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;
// Driver locations are handled by Location Redis (no prefix)
try {
if (isset($redisLocation) && $redisLocation !== null) {
$drivers = $redisLocation->georadius('geo:drivers:available', $grid_lng, $grid_lat, 0.75, 'km');
$availableDrivers = count($drivers);
}
} catch (Exception $e) {}
if ($demandCount > 0) {
$surgeRatio = ($availableDrivers > 0) ? ($demandCount / $availableDrivers) : $demandCount;
if ($surgeRatio > 1.2) {
$surgeMultiplier = 1.0 + ($surgeRatio - 1.2) * 0.5;
$surgeMultiplier = min(3.0, $surgeMultiplier); // Cap at 3.0
}
}
} catch (Exception $e) {}
}
$naturePrice = (float) ($kazanRow['naturePrice'] ?? 0);
$heavyPrice = (float) ($kazanRow['heavyPrice'] ?? 0);
$latePrice = (float) ($kazanRow['latePrice'] ?? 0);
@@ -200,6 +232,9 @@ function calculateDynamicPrice($country, $minFare, $distance, $duration, $kazanR
$fare = $billableDistance * $perKmSpeed;
$fare += $billableMinutes * $effectivePerMin;
// Apply Redis Geohash Surge Multiplier
$fare *= $surgeMultiplier;
if ($airportCtx) $fare += $airportAddon;
if ($damascusAirportBoundCtx || $isInDamascusAirportBoundCtx) {
$fare += $damascusAirportBoundAddon;
@@ -244,13 +279,27 @@ if (!empty($promo_code)) {
$negativeBalance = 0;
if (!empty($passenger_id)) {
try {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redisKey = "passenger_debt_" . $passenger_id;
$redisDebt = $redis->get($redisKey);
if ($redisDebt !== false) {
$negativeBalance = (float) $redisDebt;
$redisInstance = null;
if (isset($redis) && $redis !== null) {
$redisInstance = $redis;
} else if (extension_loaded('redis')) {
$localRedis = new Redis();
$redisHost = getenv('REDIS_MAIN_HOST') ?: getenv('REDIS_HOST') ?: '127.0.0.1';
$redisPort = (int)(getenv('REDIS_MAIN_PORT') ?: getenv('REDIS_PORT') ?: 6379);
$redisPass = getenv('REDIS_MAIN_PASSWORD') ?: getenv('REDIS_MAIN_AUTH') ?: getenv('REDIS_PASSWORD') ?: getenv('REDIS_AUTH');
if ($localRedis->connect($redisHost, $redisPort, 1.5)) {
if ($redisPass) $localRedis->auth($redisPass);
$localRedis->setOption(Redis::OPT_PREFIX, 'siro:');
$redisInstance = $localRedis;
}
}
if ($redisInstance !== null) {
$redisKey = "passenger_debt_" . $passenger_id;
$redisDebt = $redisInstance->get($redisKey);
if ($redisDebt !== false) {
$negativeBalance = (float) $redisDebt;
}
}
} catch (Exception $e) {
$negativeBalance = 0;