Initial commit with updated Auth and media ignored
This commit is contained in:
150
ride/cancelRide/addCancelTripFromDriverAfterApplied.php
Normal file
150
ride/cancelRide/addCancelTripFromDriverAfterApplied.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
/*
|
||||
require_once __DIR__ . '/../../connect.php';
|
||||
|
||||
// 🚀 تسجيل بداية العملية المدمجة
|
||||
error_log("🚀 [cancelRideAndLog.php] Request Started: Cancel Ride + Log Driver Action.");
|
||||
|
||||
// 1. استقبال كافة البيانات المطلوبة للعمليتين
|
||||
$rideId = filterRequest("id"); // معرف الرحلة
|
||||
$driverID = filterRequest("driver_id");// معرف السائق
|
||||
$note = filterRequest("notes"); // ملاحظات الإلغاء
|
||||
|
||||
// التحقق من البيانات الأساسية
|
||||
if (!$rideId || !$driverID) {
|
||||
error_log("❌ [cancelRideAndLog.php] Missing ID or Driver ID.");
|
||||
jsonError("Missing Required Data (id or driver_id)");
|
||||
exit;
|
||||
}
|
||||
|
||||
// إعدادات عملية الإلغاء
|
||||
$newStatus = "cancelRideFromDriver";
|
||||
// الحالات المسموح بالإلغاء فيها (تأكد من مطابقتها لقاعدة البيانات)
|
||||
$allowedStatuses = "'wait', 'waiting', 'Apply'";
|
||||
|
||||
$sqlCancel = "UPDATE `ride`
|
||||
SET `status` = '$newStatus', `updated_at` = CURRENT_TIMESTAMP
|
||||
WHERE `id` = :id
|
||||
AND `status` IN ($allowedStatuses)";
|
||||
|
||||
$params = [':id' => $rideId];
|
||||
|
||||
try {
|
||||
// =================================================================================
|
||||
// المرحلة الأولى: إلغاء الرحلة (نفس منطق السكريبت الأول)
|
||||
// =================================================================================
|
||||
|
||||
// 1. التحديث على سيرفر التتبع (Remote DB)
|
||||
error_log("🔄 [Step 1] Attempting to cancel on REMOTE Tracking DB...");
|
||||
$stmtRemote = $con_ride->prepare($sqlCancel);
|
||||
$stmtRemote->execute($params);
|
||||
$count = $stmtRemote->rowCount();
|
||||
|
||||
// إذا نجح التحديث في السيرفر البعيد (أو لم ينجح نتحقق من المحلي أيضا لضمان التزامن)
|
||||
// لكن المنطق الأساسي يعتمد على أن الرحلة قابلة للتعديل
|
||||
if ($count > 0) {
|
||||
|
||||
// 2. التحديث على السيرفر المحلي (Local DB)
|
||||
error_log("🔄 [Step 1] Remote success. Cancelling on LOCAL Main DB...");
|
||||
$stmtLocal = $con->prepare($sqlCancel);
|
||||
$stmtLocal->execute($params);
|
||||
|
||||
error_log("✅ [Step 1] Ride cancelled successfully on database.");
|
||||
|
||||
// =================================================================================
|
||||
// المرحلة الثانية: تسجيل الطلب وتنظيف البيانات (نفس منطق السكريبت الثاني)
|
||||
// لن يتم الدخول هنا إلا إذا نجح الإلغاء فعلياً
|
||||
// =================================================================================
|
||||
|
||||
error_log("🔄 [Step 2] Inserting into driver_orders and cleaning background tasks...");
|
||||
|
||||
// أ. إضافة سجل في driver_orders
|
||||
$orderStatus = 'pending'; // كما في السكريبت الثاني
|
||||
$sqlInsertOrder = "INSERT INTO driver_orders (driver_id, order_id, notes, status)
|
||||
VALUES (?, ?, ?, ?)";
|
||||
$stmtInsert = $con->prepare($sqlInsertOrder);
|
||||
$stmtInsert->execute([$driverID, $rideId, $note, $orderStatus]);
|
||||
|
||||
// ب. حذف آخر سجل من write_argument_after_applied_from_background
|
||||
// نستخدم نفس الاستعلام الفرعي الذي كنت تستخدمه
|
||||
$sqlDelete = "DELETE FROM write_argument_after_applied_from_background
|
||||
WHERE id = (
|
||||
SELECT id FROM (
|
||||
SELECT id
|
||||
FROM write_argument_after_applied_from_background
|
||||
WHERE driver_id = ?
|
||||
ORDER BY time_of_order DESC
|
||||
LIMIT 1
|
||||
) AS t
|
||||
)";
|
||||
$stmtDelete = $con->prepare($sqlDelete);
|
||||
$stmtDelete->execute([$driverID]);
|
||||
|
||||
error_log("✅ [Step 2] Driver order logged and background task cleaned.");
|
||||
|
||||
// =================================================================================
|
||||
// النهاية: إرجاع رسالة النجاح
|
||||
// =================================================================================
|
||||
jsonSuccess(null, "Ride cancelled and driver log updated successfully");
|
||||
|
||||
} else {
|
||||
// فشل الإلغاء (الرحلة غير موجودة أو حالتها لا تسمح)
|
||||
error_log("⚠️ [cancelRideAndLog.php] Failed to cancel. Status might be started/completed or ID invalid.");
|
||||
jsonError("Cannot cancel ride. Status might be started or already completed.");
|
||||
}
|
||||
|
||||
} catch (PDOException $e) {
|
||||
error_log("❌ [cancelRideAndLog.php] Database Error: " . $e->getMessage());
|
||||
jsonError("Database Error: " . $e->getMessage());
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../../connect.php';
|
||||
|
||||
$rideId = filterRequest("id");
|
||||
$driverID = filterRequest("driver_id");
|
||||
$note = filterRequest("notes");
|
||||
$status = "cancelRideFromDriver";
|
||||
|
||||
if (!$rideId || !$driverID) {
|
||||
jsonError("Missing Data");
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. محاولة الإلغاء في السيرفر البعيد
|
||||
$stmtRemote = $con_ride->prepare("UPDATE `ride` SET `status` = ?, `updated_at` = NOW() WHERE `id` = ? AND `status` IN ('wait', 'waiting', 'Apply', 'accepted')");
|
||||
$stmtRemote->execute([$status, $rideId]);
|
||||
|
||||
if ($stmtRemote->rowCount() > 0) {
|
||||
// 2. التحديث المحلي
|
||||
$con->prepare("UPDATE `ride` SET `status` = ?, `updated_at` = NOW() WHERE `id` = ?")->execute([$status, $rideId]);
|
||||
|
||||
// 3. تسجيل اللوج (كما في ملفك)
|
||||
$con->prepare("INSERT INTO driver_orders (driver_id, order_id, notes, status) VALUES (?, ?, ?, 'pending')")->execute([$driverID, $rideId, $note]);
|
||||
|
||||
// تنظيف الخلفية (اختياري حسب الحاجة)
|
||||
// ... كود التنظيف ...
|
||||
|
||||
// 4. 🔥 إشعار الراكب بالإلغاء 🔥
|
||||
$stmtPas = $con->prepare("SELECT passenger_id FROM ride WHERE id = ?");
|
||||
$stmtPas->execute([$rideId]);
|
||||
$passenger_id = $stmtPas->fetchColumn();
|
||||
|
||||
if ($passenger_id) {
|
||||
notifyPassengerOnRideServer($passenger_id, [
|
||||
'ride_id' => $rideId,
|
||||
'status' => 'cancelled',
|
||||
'msg' => 'نعتذر، قام السائق بإلغاء الرحلة'
|
||||
]);
|
||||
}
|
||||
|
||||
jsonSuccess(null, "Ride Cancelled");
|
||||
} else {
|
||||
jsonError("Cannot cancel ride (Status might be started or finished)");
|
||||
}
|
||||
|
||||
} catch (PDOException $e) {
|
||||
jsonError("DB Error: " . $e->getMessage());
|
||||
}
|
||||
?>
|
||||
?>
|
||||
Reference in New Issue
Block a user