File "get_booking_details.php"

Full path: /home/itsevak/public_html/prepaiddev.itsevak.com/operator/get_booking_details.php
File size: 3.18 B (3.18 KB bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php
require_once '../includes/functions.php';

// Require login and operator access
requireLogin();
if ($_SESSION['user_type'] !== 'operator') {
    http_response_code(403);
    echo json_encode(['success' => false, 'message' => 'Access denied']);
    exit;
}

$user = getCurrentUser();
$db = Database::getInstance();

// Check if operator has active session
$currentSession = $db->fetch(
    "SELECT * FROM operator_sessions WHERE operator_id = ? AND status = 'active'",
    [$user['id']]
);

if (!$currentSession) {
    http_response_code(403);
    echo json_encode(['success' => false, 'message' => 'No active operator session']);
    exit;
}

// Get booking ID from request
$bookingId = $_GET['booking_id'] ?? '';

if (empty($bookingId)) {
    http_response_code(400);
    echo json_encode(['success' => false, 'message' => 'Missing booking ID']);
    exit;
}

try {
    // Get detailed booking information
    $booking = $db->fetch(
        "SELECT b.*, p.name as passenger_name, p.phone as passenger_phone, 
                tl.name as to_location_name, tl.city as to_city,
                fl.name as from_location_name, fl.city as from_city,
                ar.number_plate, ar.unique_local_id, ao.name as owner_name
         FROM bookings b 
         JOIN passengers p ON b.passenger_id = p.id 
         JOIN to_locations tl ON b.to_location_id = tl.id 
         LEFT JOIN from_locations fl ON b.from_location_id = fl.id
         LEFT JOIN auto_rickshaws ar ON b.auto_rickshaw_id = ar.id
         LEFT JOIN auto_owners ao ON ar.owner_id = ao.id
         WHERE b.id = ? AND b.operator_id = ?",
        [$bookingId, $user['id']]
    );
    
    if (!$booking) {
        http_response_code(404);
        echo json_encode(['success' => false, 'message' => 'Booking not found or not accessible']);
        exit;
    }
    
    // Format the data for display
    $formattedBooking = [
        'id' => $booking['id'],
        'passenger_name' => $booking['passenger_name'],
        'passenger_phone' => $booking['passenger_phone'],
        'booking_type' => $booking['booking_type'],
        'status' => $booking['status'],
        'from_location_name' => $booking['from_location_name'],
        'to_location_name' => $booking['to_location_name'],
        'to_city' => $booking['to_city'],
        'auto_rickshaw_id' => $booking['auto_rickshaw_id'],
        'number_plate' => $booking['number_plate'],
        'unique_local_id' => $booking['unique_local_id'],
        'owner_name' => $booking['owner_name'],
        'commission_amount' => $booking['commission_amount'],
        'fare_amount' => $booking['fare_amount'],
        'total_amount' => $booking['total_amount'],
        'created_at' => $booking['created_at'],
        'confirmation_time' => $booking['confirmation_time'],
        'completion_time' => $booking['completion_time']
    ];
    
    echo json_encode([
        'success' => true,
        'booking' => $formattedBooking
    ]);
    
} catch (Exception $e) {
    error_log('Error fetching booking details: ' . $e->getMessage());
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => 'Internal server error']);
}
?>