Files
comelifacciamo/public/userarea/appointment_cancel.php
T

74 lines
2.0 KiB
PHP

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
include('include/headscript.php');
$pdo = DBHandlerSelect::getInstance()->getConnection();
if (!isset($iduserlogin)) {
header("Location: login.php");
exit;
}
function setFlash(string $type, string $text): void
{
$_SESSION['flash'] = ['type' => $type, 'text' => $text];
}
$apptId = (int)($_POST['appointment_id'] ?? 0);
if ($apptId <= 0) {
setFlash('danger', 'Appuntamento non valido.');
header("Location: my_appointments.php");
exit;
}
// Verifica che l'appuntamento appartenga a un cliente di questo utente
$stmt = $pdo->prepare("
SELECT a.id, a.start_at, a.status
FROM appointments a
WHERE a.id = ?
AND a.customer_id IN (SELECT id FROM customers WHERE user_id = ?)
LIMIT 1
");
$stmt->execute([$apptId, $iduserlogin]);
$appt = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$appt) {
setFlash('danger', 'Appuntamento non trovato.');
header("Location: my_appointments.php");
exit;
}
if (in_array($appt['status'], ['cancelled', 'completed', 'no_show'], true)) {
setFlash('warning', 'Questo appuntamento non può essere annullato.');
header("Location: my_appointments.php");
exit;
}
// Controllo preavviso minimo letto da shop_settings del salone
$noticeStmt = $pdo->prepare("
SELECT COALESCE(ss.min_booking_notice_hours, 2) AS min_notice
FROM appointments a
LEFT JOIN shop_settings ss ON a.shop_id = ss.shop_id
WHERE a.id = ?
LIMIT 1
");
$noticeStmt->execute([$apptId]);
$minCancelHours = (int)($noticeStmt->fetchColumn() ?: 2);
if (strtotime($appt['start_at']) - time() <= $minCancelHours * 3600) {
setFlash('warning', "Non puoi annullare a meno di {$minCancelHours} ore dall'appuntamento. Contatta il salone.");
header("Location: my_appointments.php");
exit;
}
// Annulla
$upd = $pdo->prepare("UPDATE appointments SET status = 'cancelled', updated_at = NOW() WHERE id = ?");
$upd->execute([$apptId]);
setFlash('success', 'Appuntamento annullato con successo.');
header("Location: my_appointments.php");
exit;