144 lines
5.9 KiB
PHP
144 lines
5.9 KiB
PHP
<?php
|
|
require_once("env.php");
|
|
require_once("connection.php");
|
|
require_once("functions.php");
|
|
session_start();
|
|
|
|
|
|
// Get user ID from session (assuming user is logged in)
|
|
$user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
|
|
|
|
// Validate user session
|
|
if (!$user_id) {
|
|
echo "<script>alert('User is not logged in. Please log in to make a booking.'); window.location.href = 'login.php';</script>";
|
|
exit();
|
|
}
|
|
$is_member = getUserMemberStatus($user_id);
|
|
|
|
// Check if the form has been submitted
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
// Input variables from the form (use default values if not provided)
|
|
$members = isset($_POST['members']) ? intval($_POST['members']) : 0; // Default to 1 vehicle
|
|
$num_adults = isset($_POST['non-members']) ? intval($_POST['non-members']) : 0; // Default to 1 adult
|
|
$course_id = isset($_POST['course_id']) ? intval($_POST['course_id']) : 0; // Default to 0 children
|
|
checkAndRedirectCourseBooking($course_id);
|
|
// Fetch trip costs from the database
|
|
$query = "SELECT date, cost_members, cost_nonmembers, course_type FROM courses WHERE course_id = ?";
|
|
$stmt = $conn->prepare($query);
|
|
$stmt->bind_param('i', $course_id);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
// Check if trip exists
|
|
if ($result->num_rows === 0) {
|
|
$response = ['error' => 'Trip not found.'];
|
|
header('Content-Type: application/json');
|
|
echo json_encode($response);
|
|
exit();
|
|
}
|
|
|
|
// Fetch trip details
|
|
$course = $result->fetch_assoc();
|
|
$type = $course['course_type'];
|
|
$date = $course['date'];
|
|
$cost_members = intval($course['cost_members']);
|
|
$cost_nonmembers = intval($course['cost_nonmembers']);
|
|
|
|
if ($type === "driver_training") {
|
|
$description = "Basic 4X4 Driver Training Course " . $date;
|
|
} elseif ($type === "bush_mechanics") {
|
|
$description = "Bush Mechanics Course " . $date;
|
|
} elseif ($type === "rescue_recovery") {
|
|
$description = "Rescue & Recovery Training Course " . $date;
|
|
} else {
|
|
$description = "General Course " . $date; // Default fallback description
|
|
}
|
|
|
|
// Assume the membership status is determined elsewhere
|
|
$is_member = getUserMemberStatus($user_id);
|
|
|
|
// Initialize total and discount amount
|
|
$total = 0;
|
|
|
|
// Calculate total based on membership
|
|
if ($is_member) {
|
|
$num_members = 1 + $members;
|
|
$total = (($cost_members) + ($members * $cost_members) + ($num_adults * $cost_nonmembers));
|
|
$payment_amount = $total;
|
|
} else {
|
|
$num_members = 0;
|
|
$total = (($cost_nonmembers) + ($num_adults * $cost_nonmembers));
|
|
$payment_amount = $total;
|
|
$num_adults = $num_adults + 1;
|
|
}
|
|
|
|
$status = "AWAITING PAYMENT";
|
|
$type = 'course';
|
|
$payment_id = uniqid();
|
|
$num_vehicles = 1;
|
|
$discountAmount = 0;
|
|
$eft_id = strtoupper("COURSE ".date("m-d", strtotime($date))." ".getInitialSurname($user_id));
|
|
|
|
|
|
// Insert booking into the database
|
|
$sql = "INSERT INTO bookings (booking_type, user_id, from_date, to_date, num_vehicles, num_adults, total_amount, discount_amount, status, payment_id, course_id, course_non_members, eft_id)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
$stmt = $conn->prepare($sql);
|
|
|
|
if (!$stmt) {
|
|
die("Preparation failed: " . $conn->error);
|
|
}
|
|
|
|
$stmt->bind_param('sissiiddssiis', $type, $user_id, $date, $date, $num_vehicles, $num_members, $total, $discountAmount, $status, $payment_id, $course_id, $num_adults, $eft_id);
|
|
|
|
if ($stmt->execute()) {
|
|
$booking_id = $conn->insert_id;
|
|
|
|
if ($payment_amount < 1) {
|
|
if (processZeroPayment($payment_id, $payment_amount, $description)) {
|
|
echo "<script>alert('Booking successfully created!'); window.location.href = 'bookings.php';</script>";
|
|
} else {
|
|
$error_message = $stmt->error;
|
|
echo "Error processing booking: $error_message";
|
|
}
|
|
} else {
|
|
addEFT($eft_id, $booking_id, $user_id, $status, $payment_amount, $description);
|
|
sendInvoice(getEmail($user_id), getFullName($user_id), $eft_id, formatCurrency($payment_amount), $description);
|
|
sendAdminNotification('New Course Booking - '.getFullName($user_id), getFullName($user_id).' has booked for '.$description);
|
|
header("Location: payment_confirmation.php?token=".encryptData($booking_id, $salt));
|
|
exit(); // Ensure no further code is executed after the redirect
|
|
}
|
|
} else {
|
|
// Handle error if insert fails and echo the MySQL error
|
|
$error_message = $stmt->error;
|
|
echo "Error processing booking: $error_message";
|
|
}
|
|
|
|
// if ($stmt->execute()) {
|
|
// if ($payment_amount < 1) {
|
|
// if (processZeroPayment($payment_id, $payment_amount, $description)) {
|
|
// echo "<script>alert('Booking successfully created!'); window.location.href = 'bookings.php';</script>";
|
|
// } else {
|
|
// $error_message = $stmt->error;
|
|
// echo "Error processing booking: $error_message";
|
|
// }
|
|
// } else {
|
|
// if (processPayment($payment_id, $payment_amount, $description)) {
|
|
// echo "<script>alert('Booking successfully created!'); window.location.href = 'bookings.php';</script>";
|
|
// } else {
|
|
// $error_message = $stmt->error;
|
|
// echo "Error processing booking: $error_message";
|
|
// }
|
|
// }
|
|
// } else {
|
|
// // Handle error if insert fails and echo the MySQL error
|
|
// $error_message = $stmt->error;
|
|
// echo "Error processing booking: $error_message";
|
|
// }
|
|
|
|
$stmt->close();
|
|
$conn->close();
|
|
} else {
|
|
echo "Invalid request.";
|
|
}
|