-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_availability.php
More file actions
29 lines (24 loc) · 847 Bytes
/
Copy pathcheck_availability.php
File metadata and controls
29 lines (24 loc) · 847 Bytes
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
<?php
include('../includes/db.php');
$car_id = intval($_GET['car_id']);
$start = $_GET['start'];
$end = $_GET['end'];
// 1. Get total quantity from cars table
$carQ = mysqli_query($conn, "SELECT quantity FROM cars WHERE car_id=$car_id");
$car = mysqli_fetch_assoc($carQ);
$total_quantity = $car['quantity'];
// 2. Count overlapping bookings
$check = mysqli_query($conn, "
SELECT COUNT(*) as booked_count
FROM bookings
WHERE car_id=$car_id
AND status IN ('Pending Payment','Awaiting Approval','Approved')
AND start_date <= '$end'
AND end_date >= '$start'
");
$booked = mysqli_fetch_assoc($check);
// 3. Remaining available cars
$available = $total_quantity - $booked['booked_count'];
if ($available < 0) $available = 0;
header('Content-Type: application/json');
echo json_encode(['available' => $available]);