Files
Siro/backend/ride/rides/cancelRideFromDriver.php
2026-06-16 17:47:19 +03:00

108 lines
4.7 KiB
PHP
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// cancelRideFromDriver.php
// تأكد أن هذا الملف يحتوي على دوال الإشعارات (notifyPassengerOnRideServer)
require_once __DIR__ . '/../../connect.php';
// 🚀 تسجيل بداية العملية
error_log("🚀 [cancelRide.php] Request Started to Cancel Ride From Driver.");
$id = filterRequest("id"); // Ride ID
if (!$id) {
error_log("❌ [cancelRide.php] Missing Ride ID.");
jsonError("Missing ID");
exit;
}
// الحالة الجديدة (إلغاء نهائي من طرف السائق)
$newStatus = "cancelRideFromDriver";
// الحالات المسموح بإلغاء الرحلة فيها فقط
// نسمح بالإلغاء إذا كانت في وضع الانتظار أو القبول المبدئي
$allowedStatuses = "'wait', 'waiting', 'Apply', 'accepted', 'arrive'";
try {
// ---------------------------------------------------------
// 1. التحديث على سيرفر التتبع (Remote DB)
// ---------------------------------------------------------
// نستخدم شرط الحالة لضمان عدم إلغاء رحلة بدأت بالفعل (Start/Begin)
$sql = "UPDATE `ride`
SET `status` = ?, `updated_at` = CURRENT_TIMESTAMP
WHERE `id` = ?
AND `status` IN ($allowedStatuses)";
// استخدام Prepared Statements للأمان
$stmtRemote = $con_ride->prepare($sql);
$stmtRemote->execute([$newStatus, $id]);
$count = $stmtRemote->rowCount();
error_log(" [cancelRide.php] Remote DB Rows Affected: $count");
// التحقق: هل تم التحديث؟
if ($count > 0) {
// ---------------------------------------------------------
// 2. التحديث على السيرفر المحلي (Local DB)
// ---------------------------------------------------------
// نبدأ معاملة لضمان تكامل البيانات
if (isset($con)) {
$con->beginTransaction();
try {
$stmtLocal = $con->prepare($sql);
$stmtLocal->execute([$newStatus, $id]);
// تحديث جدول driver_orders أيضاً لتوحيد الحالة (اختياري ولكنه مفضل)
$stmtDriverOrder = $con->prepare("UPDATE driver_orders SET status = ? WHERE order_id = ?");
$stmtDriverOrder->execute([$newStatus, $id]);
$con->commit();
} catch (Exception $eLocal) {
$con->rollBack();
error_log("⚠️ Local DB Update Failed: " . $eLocal->getMessage());
}
}
// ---------------------------------------------------------
// 3. 🔥 إشعار الراكب عبر السوكيت (القطعة المفقودة) 🔥
// ---------------------------------------------------------
// أ. جلب معرف الراكب لإرسال الإشعار له
// نستخدم connection الرحلات لضمان وجود البيانات
$stmtPas = $con_ride->prepare("SELECT passenger_id FROM ride WHERE id = ?");
$stmtPas->execute([$id]);
$passenger_id = $stmtPas->fetchColumn();
if ($passenger_id) {
$payload = [
'ride_id' => $id,
'status' => 'cancelled', // هذه الحالة يستقبلها الفلاتر ويغلق الواجهة
'msg' => 'للأسف، قام السائق بإلغاء الرحلة.'
];
// استدعاء الدالة المعرفة في functions.php/connect.php
if (function_exists('notifyPassengerOnRideServer')) {
notifyPassengerOnRideServer($passenger_id, $payload);
error_log("📡 [cancelRide.php] Notification sent to Passenger ID: $passenger_id");
} else {
error_log("⚠️ [cancelRide.php] Function notifyPassengerOnRideServer not found!");
}
}
// ---------------------------------------------------------
// 4. إنهاء العملية
// ---------------------------------------------------------
error_log("✅ [cancelRide.php] Ride cancelled successfully.");
jsonSuccess(null, "Ride cancelled successfully");
} else {
// الفشل يعني أن الرحلة غير موجودة أو حالتها لا تسمح بالإلغاء (مثلاً بدأت بالفعل)
error_log("⚠️ [cancelRide.php] Failed. ID invalid OR Status not allowed (maybe started?).");
jsonError("Cannot cancel ride. Status might be started or already completed.");
}
} catch (PDOException $e) {
error_log("❌ [cancelRide.php] Database Error: " . $e->getMessage());
jsonError("Database Error");
}
?>