Files

186 lines
7.2 KiB
PHP

<?php
require_once('include/headscript.php');
require_once __DIR__ . '/class/mailer.php';
require_once('include/waitinglist.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Recupera i parametri GET
$idorder = isset($_GET['idorder']) ? (int) $_GET['idorder'] : 0;
$idnewbooking = isset($_GET['idnewbooking']) ? (int) $_GET['idnewbooking'] : 0;
$iduser = isset($_GET['iduser']) ? (int) $_GET['iduser'] : 0;
$idservicenew = isset($_GET['idservicenew']) ? (int) $_GET['idservicenew'] : 0;
// Valida i parametri richiesti
if ($idorder === 0 || $idnewbooking === 0 || $iduser === 0 || $idservicenew === 0) {
die("Errore: Parametri mancanti.");
}
// Riusa la connessione già aperta da bkngstm.php
$conn = $bkngstm;
// Recupera dateschedule e servicename
$query = "SELECT serviceschedule.dateschedule, service.servicename
FROM serviceschedule
LEFT JOIN service ON service.idservice = ?
WHERE serviceschedule.idserviceschedule = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("ii", $idservicenew, $idnewbooking);
$stmt->execute();
$resultnew = $stmt->get_result();
if ($resultnew->num_rows > 0) {
$rownew = $resultnew->fetch_assoc();
$newtime = $rownew['dateschedule'];
$servicename = $rownew['servicename'] ?? 'N/D';
} else {
$stmt->close();
die("Errore: Nessun orario trovato per idserviceschedule = $idnewbooking.");
}
$stmt->close();
// Formatta la data per l'email
$newtimeformat = date("d-m-Y H:i", strtotime($newtime));
// Recupera nome ed email dell'utente per la mail
$firstname = '';
$emailuser = '';
$stmtUser = $conn->prepare("SELECT first_name, email FROM auth_users WHERE id = ?");
$stmtUser->bind_param("i", $iduser);
$stmtUser->execute();
$resUser = $stmtUser->get_result();
if ($rowUser = $resUser->fetch_assoc()) {
$firstname = $rowUser['first_name'] ?? '';
$emailuser = $rowUser['email'] ?? '';
}
$stmtUser->close();
// Decide lo stato: pending se c'è posto, waiting se la classe è piena
$bookingStatus = decideBookingStatus($conn, $idnewbooking);
if ($bookingStatus === 'full') {
// Classe piena e lista d'attesa disattivata: non inseriamo nulla
header("Location: userpanel.php?full=Y");
exit();
}
// Inserisce la nuova prenotazione in bookingclass
$insertQuery = "INSERT INTO bookingclass (idserviceschedule, iduser, prevbookingstart, idprevserviceschedule, idservice, idorder, bookingstart, status)
VALUES (?, ?, NULL, '0', ?, ?, ?, ?)";
$stmt = $conn->prepare($insertQuery);
$stmt->bind_param("iiiiss", $idnewbooking, $iduser, $idservicenew, $idorder, $newtime, $bookingStatus);
if (!$stmt->execute()) {
$stmt->close();
die("Errore durante l'inserimento della nuova prenotazione: " . $conn->error);
}
$stmt->close();
// Aggiorna una prenotazione cancellata a pending (se esiste)
$updateQuery = "UPDATE bookingclass
SET status = 'pending'
WHERE iduser = ? AND idorder = ? AND status = 'cancelled'
LIMIT 1";
$stmt = $conn->prepare($updateQuery);
$stmt->bind_param("ii", $iduser, $idorder);
$stmt->execute();
$stmt->close();
// Connessione $bkngstm gestita globalmente: non la chiudiamo qui
// Parametro di ritorno diverso se finito in lista d'attesa
$redirectFlag = ($bookingStatus === 'waiting') ? 'waiting=Y' : 'reprogram=Y';
// Se non abbiamo un'email valida, salta l'invio ma completa comunque il flusso
if ($emailuser === '') {
header("Location: userpanel.php?$redirectFlag");
exit();
}
// Configura il contenuto dell'email
$messageedit = "
<p style='font-size: 14px; line-height: 190%;'>
<span style='font-size: 18px; line-height: 34.2px;'>
<strong>Ciao $firstname,</strong>
</span>
</p>
<p style='font-size: 14px; line-height: 190%;'>
<span style='font-size: 16px; line-height: 30.4px;'>
Hai inviato una richiesta di riprogrammazione della tua lezione per il servizio <strong>$servicename</strong>.
</span>
</p>
<p style='font-size: 14px; line-height: 190%;'>
<span style='font-size: 16px; line-height: 30.4px;'>
Ecco il dettaglio della riprogrammazione: $newtimeformat
</span>
</p>
<p style='font-size: 14px; line-height: 190%;'>
<span style='font-size: 16px; line-height: 30.4px;'>
La tua richiesta è in fase di accettazione! Verrai avvisata/o se la riprogrammazione è stata accettata.
</span>
</p>
<br>
<p style='font-size: 14px; line-height: 190%;'>
<span style='font-size: 16px; line-height: 30.4px;'>
Per vedere e gestire le tue lezioni clicca qui:
<a href='https://yogibook.yogasoul.it'>YogiBook</a>
</span>
</p>
<br>
<p style='font-size: 14px; line-height: 190%;'>
<span style='font-size: 16px; line-height: 30.4px;'>
Se non ricordi la password o non hai sei mai entrata in Yogibbok, per prima cosa resettare la password cliccando di seguito:
</span>
</p>
<br>
<a href='https://yogibook.yogasoul.it/password/reset' target='_blank' style='box-sizing: border-box; display: inline-block; text-decoration: none; text-align: center; color: #FFFFFF; background-color: #3AAEE0; border-radius: 4px; width: auto; max-width: 100%; font-size: 14px;'>
<span style='display: block; padding: 10px 20px; line-height: 120%;'>Reset Password</span>
</a>
<br>
<br>
<p style='font-size: 14px; line-height: 190%;'>
<span style='font-size: 16px; line-height: 30.4px;'>
Ci vediamo sul tappetino!
</span>
</p>
<p style='font-size: 14px; line-height: 190%;'>
<span style='font-size: 16px; line-height: 30.4px;'>
Il Team Yogasoul
</span>
</p>";
$buttonedit = "<a href='https://yogibook.yogasoul.it/' target='_blank' class='v-button v-font-size' style='box-sizing: border-box; display: inline-block; text-decoration: none; text-align: center; color: #FFFFFF; background-color: #3AAEE0; border-radius: 4px; width: auto; max-width: 100%; font-size: 14px;'>
<span style='display: block; padding: 10px 20px; line-height: 120%;'>YogiBook - YogaSoul</span>
</a>";
// Integra $messageedit in $mailmessage1
include('mail/emailtemplate2.php');
// Configura l'email
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = $_ENV['MAIL_HOST'];
$mail->SMTPAuth = true;
$mail->Username = $_ENV['MAIL_USERNAME'];
$mail->Password = $_ENV['MAIL_PASSWORD'];
$mail->SMTPSecure = $_ENV['MAIL_ENCRYPTION'];
$mail->Port = (int) $_ENV['MAIL_PORT'];
$mail->setFrom($_ENV['MAIL_FROM_ADDRESS'], $_ENV['MAIL_FROM_NAME']);
$mail->addAddress($emailuser);
$mail->Subject = "Proposta di riprogrammazione!";
$mail->Body = $mailmessage1;
$mail->AltBody = 'Hai inviato una richiesta di riprogrammazione della tua lezione per il servizio ' . $servicename . ' il ' . $newtimeformat . '. La tua richiesta è in fase di accettazione.';
$mail->send();
} catch (Exception $e) {
// La prenotazione è già salvata: logghiamo l'errore mail ma non blocchiamo l'utente
error_log("Errore invio mail riprogrammazione (iduser $iduser): " . $mail->ErrorInfo);
}
// Esegui il reindirizzamento
header("Location: userpanel.php?$redirectFlag");
exit();