110 lines
4.0 KiB
PHP
110 lines
4.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Elenco delle lezioni in calendario verso cui spostare una prenotazione (uso scuola).
|
|
* GET: id = idbookingclass
|
|
*
|
|
* Restituisce le lezioni future (prossimi 90 giorni) della stessa classe
|
|
* e delle classi associate (associateclass), con posti occupati e flag.
|
|
*/
|
|
ob_start();
|
|
require_once('include/headscript.php');
|
|
|
|
function jsonOut(array $payload, int $code = 200): void
|
|
{
|
|
if (ob_get_length() !== false) ob_end_clean();
|
|
http_response_code($code);
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode($payload, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$id = (int) ($_GET['id'] ?? 0);
|
|
if ($id <= 0) {
|
|
jsonOut(['success' => false, 'message' => 'Prenotazione non valida.'], 400);
|
|
}
|
|
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
try {
|
|
// Prenotazione + ordine collegato
|
|
$st = $pdo->prepare("
|
|
SELECT bc.idbookingclass, bc.iduser, bc.idservice, bc.idserviceschedule, bc.idorder,
|
|
o.expireon, o.reprogrammed, o.maxreschedule
|
|
FROM bookingclass bc
|
|
LEFT JOIN orderbook o ON o.idorderbook = bc.idorder
|
|
WHERE bc.idbookingclass = ?
|
|
");
|
|
$st->execute([$id]);
|
|
$bk = $st->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$bk) {
|
|
jsonOut(['success' => false, 'message' => 'Prenotazione non trovata.'], 404);
|
|
}
|
|
|
|
// Classi ammesse: la stessa + quelle associate
|
|
$services = [(int) $bk['idservice']];
|
|
$as = $pdo->prepare("SELECT idassociateservice FROM associateclass WHERE idmainservice = ?");
|
|
$as->execute([(int) $bk['idservice']]);
|
|
foreach ($as->fetchAll(PDO::FETCH_COLUMN) as $sid) {
|
|
$services[] = (int) $sid;
|
|
}
|
|
$services = array_values(array_unique($services));
|
|
$ph = implode(',', array_fill(0, count($services), '?'));
|
|
|
|
$sql = "
|
|
SELECT ss.idserviceschedule, ss.dateschedule,
|
|
s.servicename, s.maxcapacity,
|
|
(SELECT COUNT(*) FROM bookingclass b
|
|
WHERE b.idserviceschedule = ss.idserviceschedule
|
|
AND b.status IN ('booked','pending')) AS occupied,
|
|
(SELECT COUNT(*) FROM bookingclass b2
|
|
WHERE b2.idserviceschedule = ss.idserviceschedule
|
|
AND b2.iduser = ?) AS mine
|
|
FROM serviceschedule ss
|
|
JOIN service s ON s.idservice = ss.idservice
|
|
WHERE ss.idservice IN ($ph)
|
|
AND ss.dateschedule > NOW()
|
|
AND ss.dateschedule <= DATE_ADD(NOW(), INTERVAL 90 DAY)
|
|
ORDER BY ss.dateschedule
|
|
LIMIT 200";
|
|
$ls = $pdo->prepare($sql);
|
|
$ls->execute(array_merge([(int) $bk['iduser']], $services));
|
|
|
|
$giorni = ['Dom', 'Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab'];
|
|
$expiry = $bk['expireon'] ? strtotime($bk['expireon'] . ' 23:59:59') : null;
|
|
|
|
$lessons = [];
|
|
foreach ($ls->fetchAll(PDO::FETCH_ASSOC) as $l) {
|
|
$ts = strtotime($l['dateschedule']);
|
|
$max = $l['maxcapacity'] !== null ? (int) $l['maxcapacity'] : null;
|
|
$occ = (int) $l['occupied'];
|
|
|
|
$lessons[] = [
|
|
'id' => (int) $l['idserviceschedule'],
|
|
'label' => $giorni[(int) date('w', $ts)] . ' ' . date('d/m/Y H:i', $ts),
|
|
'servicename' => $l['servicename'],
|
|
'occupied' => $occ,
|
|
'max' => $max,
|
|
'full' => $max !== null && $occ >= $max,
|
|
'mine' => (int) $l['mine'] > 0,
|
|
'after_expiry' => $expiry !== null && $ts > $expiry,
|
|
];
|
|
}
|
|
|
|
jsonOut([
|
|
'success' => true,
|
|
'lessons' => $lessons,
|
|
'order' => [
|
|
'has_order' => $bk['idorder'] !== null,
|
|
'reprogrammed' => (int) ($bk['reprogrammed'] ?? 0),
|
|
'maxreschedule' => (int) ($bk['maxreschedule'] ?? 0),
|
|
'expireon' => $bk['expireon'] ? date('d/m/Y', strtotime($bk['expireon'])) : null,
|
|
],
|
|
]);
|
|
} catch (PDOException $e) {
|
|
error_log("admin_reprogram_lessons #{$id}: " . $e->getMessage());
|
|
jsonOut(['success' => false, 'message' => 'Errore nel caricamento delle lezioni.'], 500);
|
|
}
|