yogibook_aury_new/public/rebook-from-cancel.php
2025-10-15 20:59:49 +02:00

407 lines
14 KiB
PHP

<?php
// Abilita visualizzazione errori PHP (solo per debug)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Includi headscript.php
require_once('include/headscript.php');
// Connessione al database
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connessione al database fallita: " . $conn->connect_error);
}
// Inizializza log
$logFile = 'rebook_from_cancel_log.txt';
$logMessage = "Esecuzione riprogrammazione: " . date('Y-m-d H:i:s') . "\n";
// Log del database utilizzato
$logMessage .= "Database connesso: $dbname\n";
// Recupera parametri GET
if (!isset($_GET['idbookingclass']) || !isset($_GET['token'])) {
$logMessage .= "Parametri mancanti: idbookingclass o token\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
echo "<h1>Riprogrammazione non possibile</h1>";
echo "<p>Parametri mancanti.</p>";
echo "<a href='https://yogibook.yogasoul.it'>Torna al portale</a>";
$conn->close();
exit;
}
$idbookingclass = filter_var($_GET['idbookingclass'], FILTER_VALIDATE_INT);
$token = $_GET['token'];
if (!$idbookingclass) {
$logMessage .= "Errore: idbookingclass non valido: " . $_GET['idbookingclass'] . "\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
echo "<h1>Riprogrammazione non possibile</h1>";
echo "<p>Parametro idbookingclass non valido.</p>";
echo "<a href='https://yogibook.yogasoul.it'>Torna al portale</a>";
$conn->close();
exit;
}
// Verifica validità della prenotazione
$query = "SELECT bc.*, ob.expireon, au.email, au.first_name, s.servicename
FROM bookingclass bc
LEFT JOIN orderbook ob ON bc.idorder = ob.order_id
LEFT JOIN auth_users au ON bc.iduser = au.id
LEFT JOIN service s ON bc.idservice = s.idservice
WHERE bc.idbookingclass = ?
AND bc.cancellation_token = ?
AND bc.status = 'booked'
AND bc.bookingstart > NOW()";
$stmt = $conn->prepare($query);
if (!$stmt) {
$logMessage .= "Errore preparazione query per ID $idbookingclass: " . $conn->error . "\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
echo "<h1>Riprogrammazione non possibile</h1>";
echo "<p>Errore nella preparazione della query.</p>";
echo "<a href='https://yogibook.yogasoul.it'>Torna al portale</a>";
$conn->close();
exit;
}
$stmt->bind_param("is", $idbookingclass, $token);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
$logMessage .= "Tentativo di riprogrammazione fallito per ID $idbookingclass: link non valido o lezione non prenotata\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
echo "<h1>Riprogrammazione non possibile</h1>";
echo "<p>Il link non è valido o la lezione non è prenotata.</p>";
echo "<a href='https://yogibook.yogasoul.it'>Torna al portale</a>";
$stmt->close();
$conn->close();
exit;
}
$row = $result->fetch_assoc();
$bookingstart = $row['bookingstart'];
$newtimeformat = date("d-m-Y H:i", strtotime($bookingstart));
$expireon = $row['expireon'] ? date("d-m-Y", strtotime($row['expireon'])) : "sconosciuta";
$emailuser = $row['email'];
$firstname = $row['first_name'] ?? 'Utente';
$servicename = $row['servicename'] ?? 'Sconosciuta';
$iduser = $row['iduser'];
$idserviceordered = $row['idservice'];
$idorder = $row['idorder'];
// Verifica il limite di cancellazione/riprogrammazione
$lessonTime = new DateTime($bookingstart);
$hour = (int)$lessonTime->format('H');
$minute = (int)$lessonTime->format('i');
$isBefore1700 = ($hour < 17) || ($hour === 17 && $minute === 0);
$currentTime = new DateTime();
$lessonDate = $lessonTime->format('Y-m-d');
if ($isBefore1700) {
$deadline = new DateTime("$lessonDate 00:01:00");
} else {
$deadline = new DateTime("$lessonDate 12:00:00");
}
if ($currentTime > $deadline) {
$logMessage .= "Tentativo di riprogrammazione fallito per ID $idbookingclass: orario oltre il limite (" . $deadline->format('Y-m-d H:i:s') . ")\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
echo "<h1>Riprogrammazione non possibile</h1>";
echo "<p>Non è possibile riprogrammare la lezione dopo le " . ($isBefore1700 ? "00:01" : "12:00") . " del giorno della lezione.</p>";
echo "<a href='https://yogibook.yogasoul.it'>Torna al portale</a>";
$stmt->close();
$conn->close();
exit;
}
// Recupera la data di scadenza dell'ordine
$expiryDate = new DateTime($row['expireon']);
// Query sulla tabella associateclass
$sql = "SELECT idassociateservice FROM associateclass WHERE idmainservice = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $idserviceordered);
$stmt->execute();
$result = $stmt->get_result();
$idassociateservices = array();
array_push($idassociateservices, $idserviceordered);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$idassociateservices[] = $row['idassociateservice'];
}
}
$stmt->close();
// Verifica se è stata specificata una richiesta per cambiare il mese
if (isset($_GET['prev_month'])) {
$currentMonthStart = $_GET['prev_month'] . '-01';
} elseif (isset($_GET['next_month'])) {
$currentMonthStart = $_GET['next_month'] . '-01';
} else {
$currentMonthStart = date("Y-m-01");
}
$currentMonthEnd = date("Y-m-t", strtotime($currentMonthStart));
// Aggiungi filtro per la data di scadenza
$expiryCondition = '';
if ($expiryDate) {
$expiryCondition = "AND ss.dateschedule <= '{$expiryDate->format('Y-m-d 23:59:59')}'";
}
// Query per le lezioni disponibili
$placeholders = implode(',', array_fill(0, count($idassociateservices), '?'));
$query = "SELECT ss.*, s.servicename, s.colorclass, s.maxcapacity
FROM serviceschedule ss
LEFT JOIN service s ON ss.idservice = s.idservice
WHERE ss.dateschedule BETWEEN ? AND DATE_ADD(?, INTERVAL 1 DAY)
$expiryCondition
AND ss.idservice IN ($placeholders)
ORDER BY ss.dateschedule";
$logMessage .= "Query lezioni disponibili: $query\n";
$logMessage .= "Parametri: " . json_encode(array_merge([$currentMonthStart, $currentMonthEnd], $idassociateservices)) . "\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
$stmt = $conn->prepare($query);
if (!$stmt) {
$logMessage .= "Errore preparazione query per lezioni disponibili: " . $conn->error . "\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
echo "<h1>Errore</h1>";
echo "<p>Errore nella preparazione della query per le lezioni disponibili.</p>";
echo "<a href='https://yogibook.yogasoul.it'>Torna al portale</a>";
$conn->close();
exit;
}
$types = 'ss' . str_repeat('i', count($idassociateservices));
$params = array_merge([$currentMonthStart, $currentMonthEnd], $idassociateservices);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$bookedclass = $stmt->get_result();
if ($conn->error) {
$logMessage .= "Errore esecuzione query per lezioni disponibili: " . $conn->error . "\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
echo "<h1>Errore</h1>";
echo "<p>Errore nell'esecuzione della query: " . $conn->error . "</p>";
echo "<a href='https://yogibook.yogasoul.it'>Torna al portale</a>";
$conn->close();
exit;
}
// Mappa dei mesi in italiano
$italianMonths = [
"January" => "Gennaio",
"February" => "Febbraio",
"March" => "Marzo",
"April" => "Aprile",
"May" => "Maggio",
"June" => "Giugno",
"July" => "Luglio",
"August" => "Agosto",
"September" => "Settembre",
"October" => "Ottobre",
"November" => "Novembre",
"December" => "Dicembre"
];
$logMessage .= "Caricata pagina di riprogrammazione per ID $idbookingclass, mese: $currentMonthStart\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="utf-8" />
<title>YogiBook - Riprogramma Lezione</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta content="YogiBook - Prenotazione facile YogaSOul" name="description" />
<meta content="Advanced Creative Solutions" author />
<link rel="shortcut icon" href="assets/images/favicon.ico">
<link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
<link href="assets/css/app.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<style>
.custom-card {
margin: 10px auto;
display: flex;
width: 90%;
max-width: 700px;
background-color: white;
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);
border-radius: 8px;
overflow: hidden;
cursor: pointer;
transition: transform 0.2s;
}
.custom-card:hover {
transform: translateY(-5px);
}
.custom-date-box {
flex: 1;
background-color: red;
color: white;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 0;
font-size: 60px;
font-weight: bold;
border-top-left-radius: 8px;
border-bottom-left-radius: 8px;
}
.custom-day {
line-height: 1;
}
.custom-month {
font-size: 28px;
}
.custom-event-details {
flex: 2;
display: flex;
flex-direction: column;
padding: 10px 20px;
background-color: lightblue;
}
.custom-heading {
margin-top: 0;
font-size: 24px;
}
.custom-paragraph {
margin-bottom: 5px;
}
.custom-actions {
display: none;
flex-direction: row;
justify-content: space-between;
margin-top: 10px;
}
.custom-card.expanded .custom-actions {
display: flex;
}
.custom-action-button {
background-color: #f0f0f0;
border: none;
padding: 8px 12px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.2s;
}
.custom-action-button:hover {
background-color: #e0e0e0;
}
@media (max-width: 768px) {
.custom-card {
flex-direction: column;
}
.custom-date-box,
.custom-event-details {
width: 100%;
border-radius: 0;
}
.custom-event-time {
font-size: 24px;
}
}
.month-navigation {
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 20px;
}
.month-nav-button {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
}
.current-month {
font-size: 24px;
margin: 0 20px;
}
.booking-details {
margin-top: 10px;
border-top: 1px solid #ccc;
padding-top: 10px;
}
.booking-button {
background-color: #1ebf99;
color: white;
border: none;
padding: 8px 12px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.2s;
}
.booking-button:hover {
background-color: #17a081;
}
</style>
</head>
<body>
<div class="container">
<h1>Riprogramma Lezione</h1>
<p>Lezione attuale: <?php echo htmlspecialchars($servicename); ?> del <?php echo htmlspecialchars($newtimeformat); ?></p>
<p>Scadenza abbonamento: <?php echo htmlspecialchars($expireon); ?></p>
<div class="month-navigation">
<button class="month-nav-button" onclick="window.location.href='?idbookingclass=<?php echo $idbookingclass; ?>&token=<?php echo urlencode($token); ?>&prev_month=<?php echo date('Y-m', strtotime($currentMonthStart . ' -1 month')); ?>'"><i class="fas fa-chevron-left"></i></button>
<span class="current-month"><?php echo $italianMonths[date('F', strtotime($currentMonthStart))]; ?> <?php echo date('Y', strtotime($currentMonthStart)); ?></span>
<button class="month-nav-button" onclick="window.location.href='?idbookingclass=<?php echo $idbookingclass; ?>&token=<?php echo urlencode($token); ?>&next_month=<?php echo date('Y-m', strtotime($currentMonthStart . ' +1 month')); ?>'"><i class="fas fa-chevron-right"></i></button>
</div>
<?php while ($row = $bookedclass->fetch_assoc()) { ?>
<div class="custom-card" onclick="this.classList.toggle('expanded')">
<div class="custom-date-box" style="background-color: <?php echo htmlspecialchars($row['colorclass']); ?>;">
<div class="custom-day"><?php echo date('d', strtotime($row['dateschedule'])); ?></div>
<div class="custom-month"><?php echo $italianMonths[date('F', strtotime($row['dateschedule']))]; ?></div>
</div>
<div class="custom-event-details">
<h3 class="custom-heading"><?php echo htmlspecialchars($row['servicename']); ?></h3>
<p class="custom-paragraph"><strong>Giorno:</strong> <?php echo htmlspecialchars($row['scheduleday']); ?></p>
<p class="custom-paragraph"><strong>Orario:</strong> <?php echo htmlspecialchars($row['startingtime']); ?></p>
<p class="custom-paragraph"><strong>Durata:</strong> <?php echo htmlspecialchars($row['durationtime']); ?> ore</p>
<div class="custom-actions">
<button class="custom-action-button booking-button" onclick="window.location.href='book_lesson.php?idserviceschedule=<?php echo $row['idserviceschedule']; ?>&idbookingclass=<?php echo $idbookingclass; ?>&token=<?php echo urlencode($token); ?>'">Prenota</button>
</div>
</div>
</div>
<?php } ?>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
console.log("Caricata pagina di riprogrammazione per ID <?php echo $idbookingclass; ?>, mese: <?php echo $currentMonthStart; ?>");
});
</script>
</body>
</html>
<?php
$stmt->close();
$conn->close();
?>