File: /home/bigisxfd/www/cowork/extend_booking.php
<?php
// Include necessary files
include_once("functions.php");
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["bookingId"]) && isset($_POST["extensionDuration"])) {
$bookingId = $_POST["bookingId"];
$extensionDuration = $_POST["extensionDuration"];
// Fetch the booking details from the database based on the bookingId
$query = "SELECT startdate, enddate FROM Bookings WHERE bookingid = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $bookingId);
$stmt->execute();
$stmt->bind_result($startTime, $endTime);
$stmt->fetch();
$stmt->close();
// Calculate new end time after extension
$extendedEndTime = date("Y-m-d H:i:s", strtotime($endTime . "+$extensionDuration hours"));
// Update the booking record with the new end time and payment status
$updateQuery = "UPDATE Bookings SET enddate = ?, paymentstatus = 0 WHERE bookingid = ?";
$updateStmt = $conn->prepare($updateQuery);
$updateStmt->bind_param("si", $extendedEndTime, $bookingId);
if ($updateStmt->execute()) {
// Insert a record into the Orders table
$insertOrderQuery = "INSERT INTO Orders (bookingid, memberid, userid, date, amount, items) VALUES (?, ?, ?, NOW(), ?, ?)";
$insertOrderStmt = $conn->prepare($insertOrderQuery);
// Get the member ID and user ID from the Bookings table
$memberIdQuery = "SELECT memberid, userid FROM Bookings WHERE bookingid = ?";
$memberIdStmt = $conn->prepare($memberIdQuery);
$memberIdStmt->bind_param("i", $bookingId);
$memberIdStmt->execute();
$memberIdStmt->bind_result($memberId, $userId);
$memberIdStmt->fetch();
$memberIdStmt->close();
// Calculate the order amount based on extension duration
if ($extensionDuration == 1) {
$orderAmount = 150; // 1 Hour Extension
$extensionText = "1 Hour Extension";
} elseif ($extensionDuration == 3) {
$orderAmount = 250; // 3 Hours Extension
$extensionText = "3 Hours Extension";
} else {
// Invalid extension duration
echo "Invalid extension duration.";
exit;
}
// Bind parameters and execute the insert query
$insertOrderStmt->bind_param("iiids", $bookingId, $memberId, $userId, $orderAmount, $extensionText);
if ($insertOrderStmt->execute()) {
// Order insertion successful
echo "Booking extended successfully and order inserted.";
} else {
// Error occurred while inserting order
echo "Error extending booking and inserting order.";
}
$insertOrderStmt->close();
} else {
// Error occurred while updating the booking
echo "Error extending booking.";
}
$updateStmt->close();
} else {
// Invalid request
echo "Invalid request.";
}
// Close the database connection
$conn->close();
?>