-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcancelbooking.php
More file actions
135 lines (117 loc) · 4.67 KB
/
cancelbooking.php
File metadata and controls
135 lines (117 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
session_start();
require_once 'includes/config.php';
require_once 'includes/auth.php';
// Check if user is logged in
if (!is_logged_in()) {
header('Location: login.php');
exit;
}
// Check if booking_id is provided
if (!isset($_GET['booking_id'])) {
header('Location: mybookings.php');
exit;
}
$booking_id = $_GET['booking_id'];
// Get booking details
$stmt = $pdo->prepare("SELECT b.*, s.departure_time
FROM bookings b
JOIN schedules s ON b.schedule_id = s.schedule_id
WHERE b.booking_id = :booking_id AND b.user_id = :user_id");
$stmt->execute([
':booking_id' => $booking_id,
':user_id' => $_SESSION['user_id']
]);
$booking = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$booking) {
header('Location: mybookings.php');
exit;
}
// Check if booking can be cancelled (not already cancelled and departure time in future)
if ($booking['status'] == 'Cancelled' || strtotime($booking['departure_time']) < time()) {
$_SESSION['error'] = "This booking cannot be cancelled.";
header('Location: mybookings.php');
exit;
}
// Only process refund if payment_status is 'Paid'
$isPaid = (isset($booking['payment_status']) && strtolower($booking['payment_status']) === 'paid');
// Calculate refund amount based on new cancellation policy (only if paid)
$refundAmount = 0;
if ($isPaid) {
$departureTime = new DateTime($booking['departure_time']);
$now = new DateTime();
$interval = $now->diff($departureTime);
$hoursDiff = ($interval->days * 24) + $interval->h + ($interval->i / 60);
if ($hoursDiff > 24) {
$refundAmount = $booking['total_amount'] * 0.5;
} elseif ($hoursDiff > 6) {
$refundAmount = $booking['total_amount'] * 0.8;
} elseif ($hoursDiff > 0) {
$refundAmount = $booking['total_amount'];
} else {
$refundAmount = 0;
}
}
// Start transaction
$pdo->beginTransaction();
try {
// Set payment_status based on whether booking was paid
if ($isPaid) {
$paymentStatus = 'Refunded';
} else {
$paymentStatus = 'Cancelled'; // or 'Not Paid'
}
// Update booking status and payment_status
$stmt = $pdo->prepare("UPDATE bookings SET status = 'Cancelled', payment_status = :payment_status WHERE booking_id = :booking_id");
$stmt->execute([
':payment_status' => $paymentStatus,
':booking_id' => $booking_id
]);
// Update available seats
$seat_count = count(explode(',', $booking['seat_numbers']));
$stmt = $pdo->prepare("UPDATE schedules SET available_seats = available_seats + :seat_count
WHERE schedule_id = (SELECT schedule_id FROM bookings WHERE booking_id = :booking_id)");
$stmt->execute([
':seat_count' => $seat_count,
':booking_id' => $booking_id
]);
// Record refund if applicable (only if paid)
if ($isPaid && $refundAmount > 0) {
$stmt = $pdo->prepare("INSERT INTO payments (booking_id, amount, payment_method, status)
VALUES (:booking_id, :amount, 'Refund', 'Success')");
$stmt->execute([
':booking_id' => $booking_id,
':amount' => -$refundAmount
]);
// Credit refund to user's wallet
$stmt = $pdo->prepare("UPDATE users SET wallet_balance = wallet_balance + :amount WHERE user_id = :user_id");
$stmt->execute([
':amount' => $refundAmount,
':user_id' => $_SESSION['user_id']
]);
// Log wallet transaction if table exists (optional)
try {
$stmt = $pdo->prepare("INSERT INTO wallet_transactions (user_id, amount, type, description) VALUES (:user_id, :amount, 'credit', 'Booking Refund')");
$stmt->execute([
':user_id' => $_SESSION['user_id'],
':amount' => $refundAmount
]);
} catch (Exception $e) {
// Ignore if wallet_transactions table does not exist
}
}
$pdo->commit();
if ($isPaid && $refundAmount > 0) {
$_SESSION['success'] = "Booking cancelled successfully. Refund amount: ₹" . number_format($refundAmount, 2);
} elseif ($isPaid && $refundAmount == 0) {
$_SESSION['success'] = "Booking cancelled successfully. No refund as per cancellation policy.";
} else {
$_SESSION['success'] = "Booking cancelled successfully. No payment was made, so no refund.";
}
} catch (Exception $e) {
$pdo->rollBack();
$_SESSION['error'] = "Cancellation failed: " . $e->getMessage();
}
header('Location: mybookings.php');
exit;
?>