OwlCyberSecurity - MANAGER
Edit File: add_booking.php
<?php // session_start(); // Uncomment this if sessions are not started elsewhere and you need them require_once './config/config.php'; require_once './includes/auth_validate.php'; // --- NEW LOGIC: Calculate Next Booking ID based on total row count --- $db = getDbInstance(); // Get database instance for calculation // Fetch the total number of rows in the bookings table $total_rows_result = $db->get('bookings', null, 'COUNT(*) as total_rows'); // Extract the count value $total_rows = ($total_rows_result && isset($total_rows_result[0]['total_rows'])) ? $total_rows_result[0]['total_rows'] : 0; // Calculate the next booking ID (total rows + 1) $next_booking_id = $total_rows + 1; // --- END NEW LOGIC --- if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Re-initialize or ensure $db is available in this scope for POST processing $db = getDbInstance(); $db->startTransaction(); try { // 1. Handle Customer $customer_data = [ 'full_name' => $_POST['name'], 'father_name' => $_POST['father_name'], 'mobile_no' => $_POST['mobile_no'], 'cnic' => $_POST['cnic'], 'address' => $_POST['address'] ]; // Check if customer exists by CNIC $customer = $db->where('cnic', $_POST['cnic'])->getOne('customers'); if (!$customer) { $customer_id = $db->insert('customers', $customer_data); if (!$customer_id) throw new Exception('Failed to insert customer: ' . $db->getLastError()); } else { // If customer exists, update their details if needed, then use existing ID $db->where('customer_id', $customer['customer_id'])->update('customers', $customer_data); $customer_id = $customer['customer_id']; } // 2. Handle Vehicle $vehicle_data = [ 'vehicle_number' => $_POST['vehicle_no'], 'transport_type' => $_POST['transport_type'] ]; // Check if vehicle exists by vehicle_number $vehicle = $db->where('vehicle_number', $_POST['vehicle_no'])->getOne('vehicles'); if (!$vehicle) { $vehicle_id = $db->insert('vehicles', $vehicle_data); if (!$vehicle_id) throw new Exception('Failed to insert vehicle: ' . $db->getLastError()); } else { // If vehicle exists, update its details if needed, then use existing ID $db->where('vehicle_id', $vehicle['vehicle_id'])->update('vehicles', $vehicle_data); $vehicle_id = $vehicle['vehicle_id']; } // 3. Handle Driver $driver_data = ['driver_name' => $_POST['driver_name']]; // Check if driver exists by driver_name $driver = $db->where('driver_name', $_POST['driver_name'])->getOne('drivers'); if (!$driver) { $driver_id = $db->insert('drivers', $driver_data); if (!$driver_id) throw new Exception('Failed to insert driver: ' . $db->getLastError()); } else { // If driver exists, update its details if needed, then use existing ID $db->where('driver_id', $driver['driver_id'])->update('drivers', $driver_data); $driver_id = $driver['driver_id']; } // 4. Create Booking // IMPORTANT: DO NOT include 'booking_id' in $booking_data here. // The database's AUTO_INCREMENT will assign it automatically. $booking_data = [ 'customer_id' => $customer_id, 'driver_id' => $driver_id, 'vehicle_id' => $vehicle_id, 'booking_date' => $_POST['booking_date'], 'start_tour_date' => $_POST['start_tour_date'], 'end_tour_date' => $_POST['end_tour_date'], 'tour_name' => $_POST['tour_name'], 'rate_per_day' => $_POST['rate_per_day'], 'num_vehicles' => $_POST['num_vehicles'], 'no_of_days' => $_POST['no_of_days'], 'total_amount' => $_POST['total_amount'], 'advance' => $_POST['advance'], 'finalization_date' => $_POST['finalization_date'] ]; $booking_id = $db->insert('bookings', $booking_data); // Database assigns the ID here if (!$booking_id) throw new Exception('Failed to create booking: ' . $db->getLastError()); $db->commit(); $_SESSION['success'] = "Booking created successfully with ID: " . $booking_id; // Show actual assigned ID header('Location: booking.php'); exit(); } catch (Exception $e) { $db->rollback(); $_SESSION['failure'] = "Error creating booking: " . $e->getMessage(); header('Location: add_booking.php'); exit(); } } $edit = false; // For 'add_booking' page, $edit is always false require_once 'includes/header.php'; ?> <div id="page-wrapper"> <div class="row"> <div class="col-lg-12"> <h2 class="page-header">Create New Booking</h2> </div> </div> <?php include('./includes/flash_messages.php') ?> <form class="form" action="" method="post" id="booking_form"> <?php // Pass the calculated next_booking_id to the form // $next_booking_id is already defined above this include include('./forms/booking_form.php'); ?> </form> </div> <script type="text/javascript"> $(document).ready(function(){ $("#booking_form").validate({ rules: { name: { required: true, minlength: 3 }, father_name: { required: true, minlength: 3 }, mobile_no: { required: true, minlength: 11, maxlength: 13 }, cnic: { required: true, minlength: 15 }, booking_date: { required: true, date: true }, address: { required: true }, start_tour_date: { required: true, date: true }, end_tour_date: { required: true, date: true }, tour_name: { required: true }, rate_per_day: { required: true, number: true }, num_vehicles: { required: true, digits: true, min: 1 }, no_of_days: { required: true, digits: true }, transport_type: { required: true }, total_amount: { required: true, number: true }, advance: { required: true, number: true }, vehicle_no: { required: true }, driver_name: { required: true }, finalization_date: { required: true, date: true } }, messages: { name: "Please enter full name", start_tour_date: "Please select a start tour date", end_tour_date: "Please select an end tour date", num_vehicles: { required: "Please enter the number of vehicles", digits: "Please enter a valid number", min: "Number of vehicles must be at least 1" }, // ... add more messages as needed } }); }); </script> <?php include_once 'includes/footer.php'; ?>