This commit is contained in:
Flatlogic Bot 2025-10-16 20:04:54 +00:00
parent 8795a633f6
commit 83696c725a
5 changed files with 130 additions and 4 deletions

View File

@ -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) {

59
driver/accept_job.php Normal file
View File

@ -0,0 +1,59 @@
<?php
session_start();
require_once __DIR__ . '/../db/config.php';
if (!isset($_SESSION['driver_id'])) {
header('Location: /driver/login.php');
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$order_id = $_POST['order_id'];
$driver_id = $_SESSION['driver_id'];
if (empty($order_id)) {
header('Location: index.php?error=Invalid order.');
exit;
}
$pdo = db();
try {
$pdo->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;
}

View File

@ -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'];
?>
<main class="container">
<div class="available-jobs">
<h2>Available Jobs</h2>
<?php if (empty($available_orders)): ?>
<p>No jobs available at the moment.</p>
<?php else: ?>
<div class="order-list">
<?php foreach ($available_orders as $order): ?>
<div class="order-card">
<h3>Order #<?php echo htmlspecialchars($order['order_id']); ?></h3>
<p><strong>Restaurant:</strong> <?php echo htmlspecialchars($order['restaurant_name']); ?></p>
<p><strong>Restaurant Address:</strong> <?php echo htmlspecialchars($order['restaurant_address']); ?></p>
<p><strong>Delivery Address:</strong> <?php echo htmlspecialchars($order['delivery_address']); ?></p>
<form action="accept_job.php" method="POST">
<input type="hidden" name="order_id" value="<?php echo $order['order_id']; ?>">
<button type="submit" class="btn-submit">Accept Job</button>
</form>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<hr>
<h1>My Assigned Deliveries</h1>
<?php if (isset($_GET['success'])): ?>

View File

@ -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 {

View File

@ -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'] . ',<br><br>Good news! Your order #' . $order_id . ' has been picked up by your driver and is on its way to you.<br><br>Thanks for using Majuro Eats!';
} elseif ($status === 'delivered') {
$subject = 'Your order has been delivered!';
$body = 'Hi ' . $customer['name'] . ',<br><br>Your order #' . $order_id . ' has been delivered. We hope you enjoy your meal!<br><br>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;