From 83696c725acd4c4bbe27a651de611b1f7a3821f3 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Thu, 16 Oct 2025 20:04:54 +0000 Subject: [PATCH] V23 --- api/save_location.php | 4 +-- driver/accept_job.php | 59 ++++++++++++++++++++++++++++++++++ driver/index.php | 43 ++++++++++++++++++++++++- driver/login_process.php | 1 + driver/update_order_status.php | 27 +++++++++++++++- 5 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 driver/accept_job.php diff --git a/api/save_location.php b/api/save_location.php index 38fc83f5..ef879cbf 100644 --- a/api/save_location.php +++ b/api/save_location.php @@ -3,7 +3,7 @@ session_start(); require_once '../db/config.php'; // Check if the user is a driver and is logged in -if (!isset($_SESSION['user_id']) || !isset($_SESSION['role']) || $_SESSION['role'] !== 'driver') { +if (!isset($_SESSION['driver_id']) || !isset($_SESSION['role']) || $_SESSION['role'] !== 'driver') { http_response_code(403); echo json_encode(['error' => 'Forbidden']); exit; @@ -25,7 +25,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $pdo = db(); // First, verify the driver is assigned to this order $stmt = $pdo->prepare("SELECT id FROM driver_assignments WHERE order_id = ? AND driver_id = ?"); - $stmt->execute([$orderId, $_SESSION['user_id']]); + $stmt->execute([$orderId, $_SESSION['driver_id']]); $assignment = $stmt->fetch(); if (!$assignment) { diff --git a/driver/accept_job.php b/driver/accept_job.php new file mode 100644 index 00000000..2145601c --- /dev/null +++ b/driver/accept_job.php @@ -0,0 +1,59 @@ +beginTransaction(); + + // Check if the order is still available + $check_stmt = $pdo->prepare('SELECT driver_id FROM orders WHERE id = ? AND status = \'ready for pickup\''); + $check_stmt->execute([$order_id]); + $order = $check_stmt->fetch(); + + if (!$order || $order['driver_id'] !== null) { + header('Location: index.php?error=Order is no longer available.'); + $pdo->rollBack(); + exit; + } + + // Assign driver and update status + $update_stmt = $pdo->prepare('UPDATE orders SET driver_id = ?, status = \'out for delivery\' WHERE id = ?'); + $update_stmt->execute([$driver_id, $order_id]); + + // Create driver assignment record + $assign_stmt = $pdo->prepare('INSERT INTO driver_assignments (order_id, driver_id) VALUES (?, ?)'); + $assign_stmt->execute([$order_id, $driver_id]); + + $pdo->commit(); + + header('Location: index.php?success=Order accepted successfully!'); + exit; + + } catch (Exception $e) { + if ($pdo->inTransaction()) { + $pdo->rollBack(); + } + // Log the error properly in a real application + header('Location: index.php?error=An error occurred. Please try again.'); + exit; + } +} else { + header('Location: index.php'); + exit; +} diff --git a/driver/index.php b/driver/index.php index c69a0a6d..e9082193 100644 --- a/driver/index.php +++ b/driver/index.php @@ -23,11 +23,52 @@ $stmt = $pdo->prepare( $stmt->execute([$driver_id]); $assigned_orders = $stmt->fetchAll(); -$order_statuses = ['preparing', 'out for delivery', 'delivered', 'cancelled']; +// Get available orders +$available_stmt = $pdo->prepare( + 'SELECT ' . + 'o.id as order_id, ' . + 'o.delivery_address, ' . + 'r.name as restaurant_name, ' . + 'r.address as restaurant_address ' . + 'FROM orders o ' . + 'JOIN restaurants r ON o.restaurant_id = r.id ' . + 'WHERE o.status = "ready for pickup" AND o.driver_id IS NULL ' . + 'ORDER BY o.created_at ASC' +); +$available_stmt->execute(); +$available_orders = $available_stmt->fetchAll(); + + +$order_statuses = ['out for delivery', 'picked up', 'delivered']; ?>
+ +
+

Available Jobs

+ +

No jobs available at the moment.

+ +
+ +
+

Order #

+

Restaurant:

+

Restaurant Address:

+

Delivery Address:

+
+ + +
+
+ +
+ +
+ +
+

My Assigned Deliveries

diff --git a/driver/login_process.php b/driver/login_process.php index d84d2c51..5b4d956f 100644 --- a/driver/login_process.php +++ b/driver/login_process.php @@ -31,6 +31,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { if ($driver['approval_status'] === 'approved' && password_verify($password, $driver['password_hash'])) { $_SESSION['driver_id'] = $driver['id']; $_SESSION['driver_name'] = $driver['full_name']; + $_SESSION['role'] = 'driver'; header("Location: index.php"); exit; } else { diff --git a/driver/update_order_status.php b/driver/update_order_status.php index 04c61914..d60adc61 100644 --- a/driver/update_order_status.php +++ b/driver/update_order_status.php @@ -13,7 +13,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { $status = $_POST['status']; $driver_id = $_SESSION['driver_id']; - $allowed_statuses = ['preparing', 'out for delivery', 'delivered', 'cancelled']; + $allowed_statuses = ['out for delivery', 'picked up', 'delivered']; if (empty($order_id) || empty($status) || !in_array($status, $allowed_statuses)) { header('Location: index.php?error=Invalid input.'); @@ -41,8 +41,33 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { $update_stmt = $pdo->prepare('UPDATE orders SET status = ? WHERE id = ?'); if ($update_stmt->execute([$status, $order_id])) { + // Notify customer by email + require_once __DIR__ . '/../mail/MailService.php'; + + $user_stmt = $pdo->prepare('SELECT u.email, u.name FROM users u JOIN orders o ON u.id = o.user_id WHERE o.id = ?'); + $user_stmt->execute([$order_id]); + $customer = $user_stmt->fetch(); + + if ($customer) { + $subject = ''; + $body = ''; + + if ($status === 'picked up') { + $subject = 'Your order is on its way!'; + $body = 'Hi ' . $customer['name'] . ',

Good news! Your order #' . $order_id . ' has been picked up by your driver and is on its way to you.

Thanks for using Majuro Eats!'; + } elseif ($status === 'delivered') { + $subject = 'Your order has been delivered!'; + $body = 'Hi ' . $customer['name'] . ',

Your order #' . $order_id . ' has been delivered. We hope you enjoy your meal!

Thanks for using Majuro Eats!'; + } + + if ($subject && $body) { + MailService::sendMail($customer['email'], $subject, $body, $body); + } + } + header('Location: index.php?success=Order status updated successfully.'); exit; + } } else { header('Location: index.php?error=Failed to update order status.'); exit;