Files
yogibook_aury_new/public/rebookandgo.php
T

149 lines
7.0 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;
$idpreviousbooking = isset($_GET['idpreviousbooking']) ? (int) $_GET['idpreviousbooking'] : 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;
if ($idpreviousbooking === 0 || $idnewbooking === 0 || $iduser === 0 || $idservicenew === 0) {
die("Errore: Parametri mancanti.");
}
// Riusa la connessione già aperta da bkngstm.php
$conn = $bkngstm;
// Nuovo orario della lezione scelta
$stmt = $conn->prepare("SELECT dateschedule FROM serviceschedule WHERE idserviceschedule = ?");
$stmt->bind_param("i", $idnewbooking);
$stmt->execute();
$resultnew = $stmt->get_result();
$rownew = $resultnew->fetch_assoc();
$newtime = $rownew['dateschedule'] ?? null;
$stmt->close();
if (!$newtime) {
die("Errore: orario della nuova lezione non trovato.");
}
// Dati della prenotazione precedente
$stmt = $conn->prepare("SELECT bookingstart, idserviceschedule, idorder FROM bookingclass WHERE idbookingclass = ?");
$stmt->bind_param("i", $idpreviousbooking);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
$stmt->close();
die("Errore: prenotazione precedente non trovata.");
}
$row = $result->fetch_assoc();
$prevBookingStart = $row['bookingstart'];
$idPrevServiceSchedule = (int) $row['idserviceschedule'];
$globalidorder = (int) $row['idorder'];
$stmt->close();
// Cancella la prenotazione precedente
$stmt = $conn->prepare("DELETE FROM bookingclass WHERE idbookingclass = ?");
$stmt->bind_param("i", $idpreviousbooking);
$stmt->execute();
$stmt->close();
// Decide lo stato: pending se c'è posto, waiting se la classe è piena
$bookingStatus = decideBookingStatus($conn, $idnewbooking);
// Inserisci la nuova prenotazione
$stmt = $conn->prepare(
"INSERT INTO bookingclass (idserviceschedule, iduser, prevbookingstart, idprevserviceschedule, idservice, idorder, bookingstart, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
);
$stmt->bind_param("iisiisss", $idnewbooking, $iduser, $prevBookingStart, $idPrevServiceSchedule, $idservicenew, $globalidorder, $newtime, $bookingStatus);
$stmt->execute();
$stmt->close();
// Incrementa il contatore riprogrammazioni sull'ordine
$stmt = $conn->prepare("UPDATE orderbook SET reprogrammed = reprogrammed + 1 WHERE idorderbook = ?");
$stmt->bind_param("i", $globalidorder);
$stmt->execute();
$stmt->close();
// 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();
$newtimeformat = date("d-m-Y H:i", strtotime($newtime));
$redirectFlag = ($bookingStatus === 'waiting') ? 'waiting=Y' : 'reprogram=Y';
// Se manca l'email, completa comunque il flusso senza inviare
if ($emailuser === '') {
header("Location: userpanel.php?$redirectFlag");
exit();
}
// Contenuto mail
$messageedit = " <p style='font-size: 14px; line-height: 190%;'><span style='font-size: 18px; line-height: 34.2px;'><strong><span style='line-height: 34.2px; font-size: 18px;'> Ciao $firstname , </span></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 </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 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: https://yogibook.yogasoul.it </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' class='v-button v-font-size' style='box-sizing: border-box;display: inline-block;text-decoration: none;-webkit-text-size-adjust: none;text-align: center;color: #FFFFFF; background-color: #3AAEE0; border-radius: 4px;-webkit-border-radius: 4px; -moz-border-radius: 4px; width:auto; max-width:100%; overflow-wrap: break-word; word-break: break-word; word-wrap:break-word; mso-border-alt: none;font-size: 14px;'>
<span style='display:block;padding:10px 20px;line-height:120%;'><span style='line-height: 16.8px;'>Reset Password</span></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;-webkit-text-size-adjust: none;text-align: center;color: #FFFFFF; background-color: #3AAEE0; border-radius: 4px;-webkit-border-radius: 4px; -moz-border-radius: 4px; width:auto; max-width:100%; overflow-wrap: break-word; word-break: break-word; word-wrap:break-word; mso-border-alt: none;font-size: 14px;'>
<span style='display:block;padding:10px 20px;line-height:120%;'><span style='line-height: 16.8px;'>YogiBook - YogaSoul</span></span>
</a>";
include('mail/emailtemplate2.php');
$htmlContent = $mailmessage1;
$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->CharSet = 'UTF-8';
$mail->isHTML(true);
$mail->setFrom($_ENV['MAIL_FROM_ADDRESS'], $_ENV['MAIL_FROM_NAME']);
$mail->addAddress($emailuser);
$mail->Subject = "Proposta di riprogrammazione!";
$mail->Body = $htmlContent;
$mail->AltBody = 'Hai inviato una richiesta di riprogrammazione. La tua richiesta è in fase di accettazione.';
$mail->send();
} catch (Exception $e) {
error_log("Errore invio mail riprogrammazione (iduser $iduser): " . $mail->ErrorInfo);
}
header("Location: userpanel.php?$redirectFlag");
exit();