Update: 2026-07-02 15:35:12

This commit is contained in:
Hamza-Ayed
2026-07-02 15:35:12 +03:00
parent 2adf195b36
commit 75e6fc42d7
17 changed files with 389 additions and 19 deletions
@@ -3,6 +3,7 @@
// تأكد أن هذا الملف يحتوي على دوال الإشعارات (notifyPassengerOnRideServer)
require_once __DIR__ . '/../../connect.php';
require_once __DIR__ . '/streak_helper.php';
// 🚀 تسجيل بداية العملية
error_log("🚀 [cancelRide.php] Request Started to Cancel Ride From Driver.");
@@ -55,6 +56,14 @@ try {
// تحديث جدول driver_orders أيضاً لتوحيد الحالة (اختياري ولكنه مفضل)
$stmtDriverOrder = $con->prepare("UPDATE driver_orders SET status = ? WHERE order_id = ?");
$stmtDriverOrder->execute([$newStatus, $id]);
// 🆕 تصفير التتابع لأن السائق ألغى الرحلة
$stmtGetDriver = $con->prepare("SELECT driver_id FROM ride WHERE id = ?");
$stmtGetDriver->execute([$id]);
$driver_id_to_reset = $stmtGetDriver->fetchColumn() ?: $user_id;
if ($driver_id_to_reset) {
handleDriverStreak($con, $driver_id_to_reset, 'reset');
}
$con->commit();
} catch (Exception $eLocal) {
@@ -8,6 +8,8 @@ try {
error_log("[cancel_ride_by_driver] Failed to connect to Ride Database: " . $e->getMessage());
}
require_once __DIR__ . '/streak_helper.php';
$rideId = filterRequest("ride_id");
$driverId = filterRequest("driver_id");
$reason = filterRequest("reason");
@@ -74,6 +76,9 @@ try {
if (isset($con_ride)) {
$con_ride->prepare($sqlRide)->execute([$statusText, $rideId]);
}
// 🆕 تصفير التتابع لأن السائق ألغى الرحلة
handleDriverStreak($con, $driverId, 'reset');
// ---------------------------------------------------------
// 4. إشعار الراكب
+36 -1
View File
@@ -7,6 +7,8 @@ try {
error_log("[finish_ride_updates] Failed to connect to Ride Database: " . $e->getMessage());
}
require_once __DIR__ . '/streak_helper.php';
// ============================================================
// finish_ride_updates.php — Atomic Server-to-Server
// ============================================================
@@ -133,6 +135,16 @@ try {
$durationMin = intval(preg_replace('/[^0-9]/', '', $actualDuration));
$calculated = ($distanceKm * $perKmRate) + ($durationMin * $perMinRate);
// 🆕 تطبيق خصم التتابع (إعفاء من العمولة) - نحدد المتغير فقط لكن لا نغير السعر على الراكب
$is_zero_commission = false;
if (hasZeroCommission($con, $driver_id)) {
$is_zero_commission = true;
error_log("[finish_ride_updates] Driver $driver_id has active 0% commission streak!");
}
// السعر النهائي يجب أن يتضمن العمولة دائماً لكي يدفعها الراكب،
// لكن إذا كانت العمولة صفر للسائق، يتم إعطاؤها للسائق بدلاً من الشركة عبر السيرفر المالي.
$calculated *= (1 + ($kazanPercent / 100));
$finalPrice = max($quotedPrice, round($calculated, 2));
@@ -198,6 +210,8 @@ try {
'authToken' => $driver_token,
'currency' => $currency, // 🆕 إرسال العملة لمخدم الدفع
'country_code' => $countryCode, // 🆕 إرسال الدولة لمخدم الدفع
'is_zero_commission' => isset($is_zero_commission) && $is_zero_commission ? 'true' : 'false', // 🆕 إرسال إعفاء العمولة
'kazanPercent' => $kazanPercent, // 🆕 إرسال نسبة العمولة متغيرة حسب الدولة
];
$ch = curl_init(WALLET_PAYMENT_URL);
@@ -244,6 +258,9 @@ try {
// ✅ Payment succeeded — COMMIT
$con->commit();
// 🆕 Update Driver Streak (Increment because ride is finished successfully)
handleDriverStreak($con, $driver_id, 'increment');
// ============================================================
// 5. Notifications (After successful commit)
@@ -287,11 +304,29 @@ try {
"المبلغ المطلوب: " . $finalPrice . " " . $currency,
$fcmData,
'Driver Finish Trip',
false
$passengerId
);
}
}
// 🆕 c) Driver notification for Zero Commission
if (isset($is_zero_commission) && $is_zero_commission && !empty($driver_token)) {
$fcmDriverData = [
'ride_id' => (string)$rideId,
'status' => 'streak_reward'
];
sendAndSaveDriverNotification(
$con,
$driver_id,
$driver_token,
"🔥 مكافأة التتابع",
"أنت بطل! لم يتم خصم عمولة لهذه الرحلة لأنك حافظت على تتابع قبول الرحلات.",
$fcmDriverData,
'Zero Commission Reward'
);
}
// ============================================================
// 6. Return Success with server-calculated price + currency
// ============================================================
+76
View File
@@ -0,0 +1,76 @@
<?php
// backend/ride/rides/streak_helper.php
function handleDriverStreak($con, $driver_id, $action) {
try {
if ($action === 'reset') {
$stmt = $con->prepare("
INSERT INTO driver_streaks (driver_id, current_streak, zero_commission_until)
VALUES (?, 0, NULL)
ON DUPLICATE KEY UPDATE current_streak = 0
");
$stmt->execute([$driver_id]);
} elseif ($action === 'increment') {
$stmt = $con->prepare("SELECT current_streak FROM driver_streaks WHERE driver_id = ?");
$stmt->execute([$driver_id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$current = $row ? intval($row['current_streak']) + 1 : 1;
$zero_until = NULL;
if ($current >= 5) {
// Reward unlocked! 0% commission until end of today
$zero_until = date('Y-m-d 23:59:59');
$current = 0; // reset for next streak
}
$stmt2 = $con->prepare("
INSERT INTO driver_streaks (driver_id, current_streak, best_streak, zero_commission_until)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
current_streak = ?,
best_streak = GREATEST(best_streak, ?),
zero_commission_until = COALESCE(?, zero_commission_until)
");
$stmt2->execute([
$driver_id,
$current,
$current,
$zero_until,
$current,
$current,
$zero_until
]);
}
// 🆕 Invalidate Redis Cache (Cache-Aside pattern)
global $redis;
if (isset($redis)) {
try {
$redis->del("driver_streak:{$driver_id}");
$redis->del("gamification_dashboard:{$driver_id}");
} catch (Exception $e) {
error_log("[streak_helper] Redis Cache Invalidation Error: " . $e->getMessage());
}
}
} catch (Exception $e) {
error_log("[streak_helper] Error: " . $e->getMessage());
}
}
function hasZeroCommission($con, $driver_id) {
try {
$stmt = $con->prepare("SELECT zero_commission_until FROM driver_streaks WHERE driver_id = ?");
$stmt->execute([$driver_id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row && !empty($row['zero_commission_until'])) {
return (strtotime($row['zero_commission_until']) > time());
}
} catch (Exception $e) {
error_log("[streak_helper] Error checking commission: " . $e->getMessage());
}
return false;
}
?>