Debug Tools
Create Test Appointment
🧪 Create Test Appointment
";
try {
$db = getDatabase();
$pdo = $db->getConnection();
// Check if we should create appointment
if (isset($_POST['create_appointment'])) {
$start_datetime = $_POST['start_datetime'];
$end_datetime = $_POST['end_datetime'];
$first_name = $_POST['first_name'] ?: 'Test';
$last_name = $_POST['last_name'] ?: 'User';
$email = $_POST['email'] ?: 'test@example.com';
$phone = $_POST['phone'] ?: '+33123456789';
}
// Check if we should create random appointment
if (isset($_POST['create_random_appointment'])) {
// Get available slots first
$available_slots = [];
for ($i = 0; $i < 7; $i++) {
$date = date('Y-m-d', strtotime("+$i days"));
// Get availability cache for this date
$cache_query = "
SELECT slot_start, slot_end, service_id
FROM availability_cache
WHERE DATE(slot_start) = ?
ORDER BY slot_start ASC
";
$stmt = $pdo->prepare($cache_query);
$stmt->execute([$date]);
$cache_slots = $stmt->fetchAll();
// Get existing appointments for this date
$appointments = [];
try {
$appt_query = "
SELECT start_datetime, end_datetime
FROM appointments
WHERE DATE(start_datetime) = ?
AND status IN ('confirmed', 'pending', 'CONFIRMED', 'PENDING')
ORDER BY start_datetime ASC
";
$stmt = $pdo->prepare($appt_query);
$stmt->execute([$date]);
$appointments = $stmt->fetchAll();
} catch (PDOException $e) {
// Continue without appointments if error
}
// Generate 30-minute slots from cache that don't conflict with appointments
foreach ($cache_slots as $cache_slot) {
$slot_start_time = strtotime($cache_slot['slot_start']);
$slot_end_time = strtotime($cache_slot['slot_end']);
$current_time = $slot_start_time;
while ($current_time < $slot_end_time) {
$service_end_time = $current_time + (30 * 60); // 30 minutes
if ($service_end_time > $slot_end_time) {
break;
}
// Check if available (no appointment conflicts)
$is_available = true;
foreach ($appointments as $appointment) {
$appt_start = strtotime($appointment['start_datetime']);
$appt_end = strtotime($appointment['end_datetime']);
if (($current_time < $appt_end) && ($service_end_time > $appt_start)) {
$is_available = false;
break;
}
}
if ($is_available) {
$available_slots[] = [
'start' => date('Y-m-d H:i:s', $current_time),
'end' => date('Y-m-d H:i:s', $service_end_time)
];
}
$current_time += (30 * 60);
}
}
}
if (!empty($available_slots)) {
// Pick a random slot
$random_slot = $available_slots[array_rand($available_slots)];
$start_datetime = $random_slot['start'];
$end_datetime = $random_slot['end'];
// Generate random customer data
$first_name = getRandomFirstName();
$last_name = getRandomLastName();
$email = getRandomEmail($first_name, $last_name);
$phone = getRandomPhone();
} else {
echo "
No available slots found for random appointment creation!
";
}
}
// Process appointment creation (both manual and random)
if ((isset($_POST['create_appointment']) || isset($_POST['create_random_appointment'])) && isset($start_datetime)) {
try {
$pdo->beginTransaction();
// Generate booking ID
$booking_id = generateULID();
// Insert or update customer
$customer_email = strtolower(trim($email));
$stmt = $pdo->prepare("
INSERT INTO customers (email, phone, first_name, last_name)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
phone = VALUES(phone),
first_name = VALUES(first_name),
last_name = VALUES(last_name)
");
$stmt->execute([$customer_email, $phone, $first_name, $last_name]);
// Get customer ID
$stmt = $pdo->prepare("SELECT id FROM customers WHERE email = ?");
$stmt->execute([$customer_email]);
$customer_id = $stmt->fetchColumn();
// Insert appointment
$stmt = $pdo->prepare("
INSERT INTO appointments (
booking_id, service_id, customer_id, start_datetime, end_datetime,
status, cancellation_token
) VALUES (?, ?, ?, ?, ?, 'CONFIRMED', ?)
");
$cancellation_token = generateCancellationToken($booking_id, $customer_email);
$stmt->execute([
$booking_id,
'service_30min',
$customer_id,
$start_datetime,
$end_datetime,
$cancellation_token
]);
$pdo->commit();
echo "
✅ Appointment Created Successfully!
Booking ID: $booking_id
Start: $start_datetime
End: $end_datetime
Customer: $first_name $last_name ($email)
";
} catch (Exception $e) {
$pdo->rollback();
echo "
Error: " . htmlspecialchars($e->getMessage()) . "
";
}
}
// Get available slots for next 7 days
$available_slots = [];
for ($i = 0; $i < 7; $i++) {
$date = date('Y-m-d', strtotime("+$i days"));
// Get availability cache for this date
$cache_query = "
SELECT slot_start, slot_end, service_id
FROM availability_cache
WHERE DATE(slot_start) = ?
ORDER BY slot_start ASC
";
$stmt = $pdo->prepare($cache_query);
$stmt->execute([$date]);
$cache_slots = $stmt->fetchAll();
// Get existing appointments for this date
$appointments = [];
try {
$appt_query = "
SELECT start_datetime, end_datetime
FROM appointments
WHERE DATE(start_datetime) = ?
AND status IN ('confirmed', 'pending', 'CONFIRMED', 'PENDING')
ORDER BY start_datetime ASC
";
$stmt = $pdo->prepare($appt_query);
$stmt->execute([$date]);
$appointments = $stmt->fetchAll();
} catch (PDOException $e) {
// Continue without appointments if error
}
// Generate 30-minute slots from cache that don't conflict with appointments
foreach ($cache_slots as $cache_slot) {
$slot_start_time = strtotime($cache_slot['slot_start']);
$slot_end_time = strtotime($cache_slot['slot_end']);
$current_time = $slot_start_time;
while ($current_time < $slot_end_time) {
$service_end_time = $current_time + (30 * 60); // 30 minutes
if ($service_end_time > $slot_end_time) {
break;
}
// Check if available (no appointment conflicts)
$is_available = true;
foreach ($appointments as $appointment) {
$appt_start = strtotime($appointment['start_datetime']);
$appt_end = strtotime($appointment['end_datetime']);
if (($current_time < $appt_end) && ($service_end_time > $appt_start)) {
$is_available = false;
break;
}
}
if ($is_available) {
$available_slots[] = [
'start' => date('Y-m-d H:i:s', $current_time),
'end' => date('Y-m-d H:i:s', $service_end_time),
'date' => date('Y-m-d', $current_time),
'time' => date('H:i', $current_time)
];
}
$current_time += (30 * 60);
}
}
}
echo "
";
// Form to create appointment
echo "
";
if (empty($available_slots)) {
echo "
No available slots found!
Make sure you have availability_cache entries for the next 7 days.
";
} else {
echo "
";
// Random appointment form
echo "
";
}
echo "
";
// List of available slots
echo "
";
if (empty($available_slots)) {
echo "
No available slots found.
";
} else {
echo "
";
$current_date = '';
foreach ($available_slots as $slot) {
$slot_date = date('l, F j', strtotime($slot['date']));
if ($current_date !== $slot_date) {
if ($current_date !== '') echo "
";
echo "
$slot_date ";
echo "
";
$current_date = $slot_date;
}
echo "{$slot['time']} ";
}
if (!empty($available_slots)) echo "
";
echo "
";
echo "
Total: " . count($available_slots) . " available slots
";
}
echo "
";
echo "
"; // Close row
// Recent appointments
echo "