1361 lines
86 KiB
PHP

<?php
include('include/headscript.php');
// Connessione al database
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
// Verifica che iduserlogin sia definito
if (!isset($iduserlogin)) {
die("Errore: ID utente non definito.");
}
// Recupera i dati della scuola in base all'utente loggato
$stmt = $pdo->prepare("
SELECT id, name, website, email, phone, description, address_street, address_city, address_postal_code, address_province, address_country, logo, status
FROM schools
WHERE owner_id = ?
");
$stmt->execute([$iduserlogin]);
$school = $stmt->fetch();
if (!$school) {
die("Errore: Nessuna scuola trovata per l'utente loggato.");
}
$school_id = $school['id'];
$school_name = $school['name'];
// === RECUPERA IMPOSTAZIONI SCUOLA (valori predefiniti) ===
$stmt = $pdo->prepare("SELECT * FROM school_settings WHERE school_id = ?");
$stmt->execute([$school_id]);
$school_settings = $stmt->fetch() ?: [
'allow_freeze_global' => 1,
'freeze_max_days_global' => 30,
'auto_propagate_on_purchase' => 1,
'allow_full_access_rebooking' => 1,
'allowed_product_types' => 'subscription,carnet,drop_in'
];
// === PREPARA I TIPI DI PRODOTTO CONSENTITI ===
$allowed_types = array_filter(explode(',', $school_settings['allowed_product_types'] ?? 'subscription,carnet,drop_in'));
$types_map = [
'subscription' => 'Abbonamento',
'carnet' => 'Carnet',
'drop_in' => 'Lezione Singola'
];
// Recupera i prodotti della scuola con variazioni, classi e variazioni associate
$stmt = $pdo->prepare("
SELECT p.*,
p.auto_propagate_to_order AS product_auto_propagate,
pv.id AS variation_id, pv.name AS variation_name, pv.price, pv.duration_days, pv.max_entries, pv.weekly_limit, pv.max_recoveries, pv.recovery_validity_days, pv.allow_freeze, pv.freeze_max_days, pv.max_inventory, pv.current_inventory, pv.status AS variation_status, pv.auto_propagate_to_order AS variation_auto_propagate,
GROUP_CONCAT(DISTINCT CASE WHEN pct.variation_id IS NULL THEN c.name END) AS class_names,
GROUP_CONCAT(DISTINCT CASE WHEN pct.variation_id IS NULL THEN CONCAT(ct.level, ' (', ct.day_of_week, ')') END) AS variation_names,
GROUP_CONCAT(DISTINCT CASE WHEN pct.variation_id = pv.id THEN c.name END) AS variation_class_names,
GROUP_CONCAT(DISTINCT CASE WHEN pct.variation_id = pv.id THEN CONCAT(ct.level, ' (', ct.day_of_week, ')') END) AS variation_variation_names
FROM products p
LEFT JOIN product_variations pv ON p.id = pv.product_id
LEFT JOIN product_class_types pct ON (p.id = pct.product_id AND (pct.variation_id = pv.id OR pct.variation_id IS NULL))
LEFT JOIN class_types ct ON pct.class_type_id = ct.id
LEFT JOIN classes c ON ct.class_id = c.id
WHERE p.school_id = ?
GROUP BY p.id, pv.id
ORDER BY p.created_at DESC, pv.created_at DESC
");
$stmt->execute([$school_id]);
$raw_products = $stmt->fetchAll();
// Organizza i prodotti in una struttura gerarchica: prodotto -> variazioni
$products = [];
foreach ($raw_products as $row) {
$product_id = $row['id'];
if (!isset($products[$product_id])) {
$products[$product_id] = [
'id' => $row['id'],
'name' => $row['name'],
'type' => $row['type'],
'is_full_access' => $row['is_full_access'],
'status' => $row['status'],
'auto_propagate_to_order' => $row['product_auto_propagate'],
'class_names' => $row['class_names'],
'variation_names' => $row['variation_names'],
'variations' => []
];
}
if ($row['variation_id']) {
$products[$product_id]['variations'][] = [
'variation_id' => $row['variation_id'],
'variation_name' => $row['variation_name'],
'price' => $row['price'],
'duration_days' => $row['duration_days'],
'max_entries' => $row['max_entries'],
'weekly_limit' => $row['weekly_limit'],
'max_recoveries' => $row['max_recoveries'],
'recovery_validity_days' => $row['recovery_validity_days'],
'allow_freeze' => $row['allow_freeze'],
'freeze_max_days' => $row['freeze_max_days'],
'max_inventory' => $row['max_inventory'],
'current_inventory' => $row['current_inventory'],
'variation_status' => $row['variation_status'],
'auto_propagate_to_order' => $row['variation_auto_propagate'],
'class_names' => $row['variation_class_names'],
'variation_names' => $row['variation_variation_names']
];
}
}
// Recupera tutte le classi e le loro variazioni per il modale di aggiunta/modifica prodotti
$stmt = $pdo->prepare("
SELECT c.id AS class_id, c.name AS class_name, ct.id AS class_type_id, ct.level, ct.day_of_week
FROM classes c
LEFT JOIN class_types ct ON c.id = ct.class_id
WHERE c.school_id = ?
ORDER BY c.name, ct.level, ct.day_of_week
");
$stmt->execute([$school_id]);
$class_data = $stmt->fetchAll();
// Organizza i dati in una struttura gerarchica: classi -> variazioni
$all_classes = [];
foreach ($class_data as $row) {
$class_id = $row['class_id'];
if (!isset($all_classes[$class_id])) {
$all_classes[$class_id] = [
'name' => $row['class_name'],
'variations' => []
];
}
if ($row['class_type_id']) {
$all_classes[$class_id]['variations'][] = [
'id' => $row['class_type_id'],
'name' => ucfirst($row['level']) . ' (' . ucfirst($row['day_of_week']) . ')'
];
}
}
// Gestione delle azioni (aggiunta, modifica, eliminazione)
$success_message = '';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
// Aggiungi un prodotto
// Aggiungi un prodotto
if ($action === 'add_product') {
$name = $_POST['name'] ?? '';
$type = $_POST['type'] ?? '';
$variation_name = $_POST['variation_name'] ?? '';
$price = $_POST['price'] ?? 0;
$duration_days = $_POST['duration_days'] ?: null;
$max_entries = $_POST['max_entries'] ?: null;
$weekly_limit = $_POST['weekly_limit'] ?: null;
$max_recoveries = $_POST['max_recoveries'] ?: null;
$recovery_validity_days = $_POST['recovery_validity_days'] ?: null;
$allow_freeze = $_POST['allow_freeze'] ?? 0;
$freeze_max_days = $_POST['freeze_max_days'] ?: null;
$max_inventory = $_POST['max_inventory'] ?: null;
$is_full_access = $_POST['is_full_access'] ?? 0;
$auto_propagate_to_order = $_POST['auto_propagate_to_order'] ?? 0; // Nuovo campo
$class_types = $_POST['class_types'] ?? [];
if (empty($name) || empty($type) || empty($variation_name) || $price <= 0) {
$error = "Nome, tipo, nome variazione e prezzo sono obbligatori.";
} else {
// Inserisci il prodotto principale
$stmt = $pdo->prepare("
INSERT INTO products (school_id, name, type, is_full_access, auto_propagate_to_order)
VALUES (?, ?, ?, ?, ?)
");
$stmt->execute([$school_id, $name, $type, $is_full_access, $auto_propagate_to_order]);
$product_id = $pdo->lastInsertId();
// Inserisci la variazione del prodotto
$current_inventory = $max_inventory;
$stmt = $pdo->prepare("
INSERT INTO product_variations (product_id, name, price, duration_days, max_entries, weekly_limit, max_recoveries, recovery_validity_days, allow_freeze, freeze_max_days, max_inventory, current_inventory, auto_propagate_to_order)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$product_id,
$variation_name,
$price,
$duration_days,
$max_entries,
$weekly_limit,
$max_recoveries,
$recovery_validity_days,
$allow_freeze,
$freeze_max_days,
$max_inventory,
$current_inventory,
$auto_propagate_to_order // Propaga lo stesso valore del prodotto
]);
// Associa le classi selezionate (se non è full access)
if (!$is_full_access && !empty($class_types)) {
$stmt = $pdo->prepare("INSERT INTO product_class_types (product_id, class_type_id, entry_cost) VALUES (?, ?, 1)");
foreach ($class_types as $class_type_id) {
$stmt->execute([$product_id, $class_type_id]);
}
}
$success_message = "Prodotto aggiunto con successo!";
header("Location: products.php");
exit;
}
}
// add product variation
// Aggiungi una variazione
// Aggiungi una variazione
if ($action === 'add_variation') {
$product_id = $_POST['product_id'] ?? 0;
$variation_name = $_POST['variation_name'] ?? '';
$price = $_POST['price'] ?? 0;
$duration_days = $_POST['duration_days'] ?: null;
$max_entries = $_POST['max_entries'] ?: null;
$weekly_limit = $_POST['weekly_limit'] ?: null;
$max_recoveries = $_POST['max_recoveries'] ?: null;
$recovery_validity_days = $_POST['recovery_validity_days'] ?: null;
$allow_freeze = $_POST['allow_freeze'] ?? 0;
$freeze_max_days = $_POST['freeze_max_days'] ?: null;
$max_inventory = $_POST['max_inventory'] ?: null;
$current_inventory = $_POST['current_inventory'] ?? $max_inventory;
$status = $_POST['status'] ?? 'active';
$auto_propagate_to_order = $_POST['auto_propagate_to_order'] ?? 0; // Nuovo campo
$class_types = $_POST['class_types'] ?? [];
if ($product_id <= 0 || empty($variation_name) || $price <= 0) {
$error = "ID prodotto, nome variazione e prezzo sono obbligatori.";
} else {
// Verifica che il prodotto appartenga alla scuola e recupera is_full_access
$stmt = $pdo->prepare("SELECT id, is_full_access FROM products WHERE id = ? AND school_id = ?");
$stmt->execute([$product_id, $school_id]);
$product = $stmt->fetch();
if (!$product) {
$error = "Prodotto non trovato.";
} else {
$is_full_access = $product['is_full_access'];
// Inserisci la nuova variazione
$stmt = $pdo->prepare("
INSERT INTO product_variations (product_id, name, price, duration_days, max_entries, weekly_limit, max_recoveries, recovery_validity_days, allow_freeze, freeze_max_days, max_inventory, current_inventory, status, auto_propagate_to_order)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$product_id,
$variation_name,
$price,
$duration_days,
$max_entries,
$weekly_limit,
$max_recoveries,
$recovery_validity_days,
$allow_freeze,
$freeze_max_days,
$max_inventory,
$current_inventory,
$status,
$auto_propagate_to_order
]);
// Recupera l'ID della variazione appena creata
$variation_id = $pdo->lastInsertId();
// Associa le classi alla variazione (se non è full access)
if (!$is_full_access) {
if (!empty($class_types)) {
// Usa le classi selezionate nel modale
$stmt = $pdo->prepare("INSERT INTO product_class_types (product_id, variation_id, class_type_id, entry_cost) VALUES (?, ?, ?, 1)");
foreach ($class_types as $class_type_id) {
$stmt->execute([$product_id, $variation_id, $class_type_id]);
}
} else {
// Se non sono state selezionate classi, eredita quelle del prodotto
$stmt = $pdo->prepare("SELECT class_type_id FROM product_class_types WHERE product_id = ? AND variation_id IS NULL");
$stmt->execute([$product_id]);
$inherited_class_types = $stmt->fetchAll(PDO::FETCH_COLUMN);
if (!empty($inherited_class_types)) {
$stmt = $pdo->prepare("INSERT INTO product_class_types (product_id, variation_id, class_type_id, entry_cost) VALUES (?, ?, ?, 1)");
foreach ($inherited_class_types as $class_type_id) {
$stmt->execute([$product_id, $variation_id, $class_type_id]);
}
}
}
}
$success_message = "Variazione aggiunta con successo!";
header("Location: products.php");
exit;
}
}
}
// Modifica un prodotto
// Modifica un prodotto
if ($action === 'edit_product') {
$id = $_POST['id'] ?? 0;
$variation_id = $_POST['variation_id'] ?? 0;
$name = $_POST['name'] ?? '';
$type = $_POST['type'] ?? '';
$variation_name = $_POST['variation_name'] ?? '';
$price = $_POST['price'] ?? 0;
$duration_days = $_POST['duration_days'] ?: null;
$max_entries = $_POST['max_entries'] ?: null;
$weekly_limit = $_POST['weekly_limit'] ?: null;
$max_recoveries = $_POST['max_recoveries'] ?: null;
$recovery_validity_days = $_POST['recovery_validity_days'] ?: null;
$allow_freeze = $_POST['allow_freeze'] ?? 0;
$freeze_max_days = $_POST['freeze_max_days'] ?: null;
$max_inventory = $_POST['max_inventory'] ?: null;
$current_inventory = $_POST['current_inventory'] ?: null;
$is_full_access = $_POST['is_full_access'] ?? 0;
$status = $_POST['status'] ?? 'active';
$auto_propagate_to_order = $_POST['auto_propagate_to_order'] ?? 0; // Nuovo campo
$class_types = $_POST['class_types'] ?? [];
if ($id <= 0 || empty($name) || empty($type)) {
$error = "ID, nome e tipo sono obbligatori.";
} elseif ($variation_id > 0 && (empty($variation_name) || $price <= 0)) {
$error = "Nome variazione e prezzo sono obbligatori per la variazione.";
} else {
// Verifica che il prodotto appartenga alla scuola
$stmt = $pdo->prepare("SELECT id FROM products WHERE id = ? AND school_id = ?");
$stmt->execute([$id, $school_id]);
if (!$stmt->fetch()) {
$error = "Prodotto non trovato.";
} else {
// Aggiorna il prodotto principale
$stmt = $pdo->prepare("
UPDATE products
SET name = ?, type = ?, is_full_access = ?, status = ?, auto_propagate_to_order = ?
WHERE id = ?
");
$stmt->execute([$name, $type, $is_full_access, $status, $auto_propagate_to_order, $id]);
// Aggiorna la variazione, se presente
if ($variation_id > 0) {
$stmt = $pdo->prepare("
UPDATE product_variations
SET name = ?, price = ?, duration_days = ?, max_entries = ?, weekly_limit = ?, max_recoveries = ?, recovery_validity_days = ?, allow_freeze = ?, freeze_max_days = ?, max_inventory = ?, current_inventory = ?, status = ?, auto_propagate_to_order = ?
WHERE id = ? AND product_id = ?
");
$stmt->execute([
$variation_name,
$price,
$duration_days,
$max_entries,
$weekly_limit,
$max_recoveries,
$recovery_validity_days,
$allow_freeze,
$freeze_max_days,
$max_inventory,
$current_inventory,
$status,
$auto_propagate_to_order,
$variation_id,
$id
]);
}
// Aggiorna le classi associate
if ($variation_id > 0) {
// Se stiamo modificando una variazione, aggiorna le classi associate alla variazione
$stmt = $pdo->prepare("DELETE FROM product_class_types WHERE product_id = ? AND variation_id = ?");
$stmt->execute([$id, $variation_id]);
} else {
// Se stiamo modificando il prodotto, aggiorna le classi associate al prodotto
$stmt = $pdo->prepare("DELETE FROM product_class_types WHERE product_id = ? AND variation_id IS NULL");
$stmt->execute([$id]);
}
if (!$is_full_access && !empty($class_types)) {
$stmt = $pdo->prepare("INSERT INTO product_class_types (product_id, variation_id, class_type_id, entry_cost) VALUES (?, ?, ?, 1)");
foreach ($class_types as $class_type_id) {
$class_type_id = (int)$class_type_id;
if ($class_type_id > 0) {
// Verifica che il class_type_id esista
$checkStmt = $pdo->prepare("SELECT id FROM class_types WHERE id = ?");
$checkStmt->execute([$class_type_id]);
if ($checkStmt->fetch()) {
$stmt->execute([$id, $variation_id > 0 ? $variation_id : null, $class_type_id]);
}
}
}
}
$success_message = "Prodotto modificato con successo!";
header("Location: products.php");
exit;
}
}
}
// Elimina un prodotto
if ($action === 'delete_product') {
$id = $_POST['id'] ?? 0;
if ($id <= 0) {
$error = "ID non valido.";
} else {
$stmt = $pdo->prepare("DELETE FROM products WHERE id = ? AND school_id = ?");
$stmt->execute([$id, $school_id]);
$success_message = "Prodotto eliminato con successo!";
header("Location: products.php");
exit;
}
}
//delete variations product
if ($action === 'delete_variation') {
$variation_id = $_POST['variation_id'] ?? 0;
if ($variation_id <= 0) {
$error = "ID variazione non valido.";
} else {
// Verifica che la variazione appartenga a un prodotto della scuola
$stmt = $pdo->prepare("
SELECT pv.id
FROM product_variations pv
JOIN products p ON pv.product_id = p.id
WHERE pv.id = ? AND p.school_id = ?
");
$stmt->execute([$variation_id, $school_id]);
if (!$stmt->fetch()) {
$error = "Variazione non trovata.";
} else {
$stmt = $pdo->prepare("DELETE FROM product_variations WHERE id = ?");
$stmt->execute([$variation_id]);
$success_message = "Variazione eliminata con successo!";
header("Location: products.php");
exit;
}
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--favicon-->
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<style>
.variation-row {
background-color: #f8f9fa;
/* Colore di sfondo leggermente diverso per le variazioni */
}
.variation-row td:first-child {
font-style: italic;
/* Rende il nome del prodotto in corsivo per le variazioni */
}
.precompiled-field {
background-color: #fff8c4 !important;
/* giallo chiaro, elegante */
border: 1px solid #ffe08c;
}
.precompiled-field:focus {
background-color: #fffce6 !important;
/* un po' più chiaro quando scrivi */
box-shadow: 0 0 0 0.2rem rgba(255, 200, 0, 0.25);
}
</style>
</head>
<body>
<!--wrapper-->
<div class="wrapper">
<!--sidebar wrapper -->
<?php include('include/navbar.php'); ?>
<!--end sidebar wrapper -->
<!--start header -->
<?php include('include/topbar.php'); ?>
<!--end header -->
<!--start page wrapper -->
<div class="page-wrapper">
<div class="page-content">
<!-- Messaggi di successo o errore -->
<?php if ($success_message): ?>
<div class="alert alert-success" role="alert">
<?php echo htmlspecialchars($success_message); ?>
</div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger" role="alert">
<?php echo htmlspecialchars($error); ?>
</div>
<?php endif; ?>
<div class="card radius-10">
<div class="card-header">
<div class="d-flex align-items-center">
<div>
<h6 class="mb-0">Prodotti</h6>
</div>
<div class="ms-auto">
<button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#addProductModal">
<i class="bx bx-plus"></i> Aggiungi Prodotto
</button>
</div>
</div>
</div>
<div class="card-body">
<table id="productsTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Nome</th>
<th>Variazione</th>
<th>Tipo</th>
<th>Prezzo</th>
<th>Durata (giorni)</th>
<th>Ingressi</th>
<th>Inventario</th>
<th>Accesso Completo</th>
<th>Classi Associate</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php if (empty($products)): ?>
<tr>
<td colspan="10" class="text-center">Nessun prodotto trovato.</td>
</tr>
<?php else: ?>
<?php foreach ($products as $product): ?>
<!-- Riga del prodotto principale -->
<tr>
<td><?php echo htmlspecialchars($product['name']); ?></td>
<td>-</td>
<td>
<?php
$type_labels = [
'carnet' => 'Carnet',
'subscription' => 'Abbonamento',
'drop_in' => 'Lezione Singola'
];
echo $type_labels[$product['type']] ?? $product['type'];
?>
</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td><?php echo $product['is_full_access'] ? 'Sì' : 'No'; ?></td>
<td>
<?php
$class_names = $product['class_names'] ?? '';
$variation_names = $product['variation_names'] ?? '';
if ($product['is_full_access']) {
echo 'Tutte le classi';
} elseif ($class_names || $variation_names) {
echo htmlspecialchars($class_names);
if ($variation_names) {
echo '<br><small>' . htmlspecialchars($variation_names) . '</small>';
}
} else {
echo 'Nessuna classe associata';
}
?>
</td>
<td>
<button type="button" class="btn btn-sm btn-info" data-bs-toggle="modal" data-bs-target="#addVariationModal"
onclick='fillAddVariationModal(<?php echo json_encode([
"id" => $product['id'],
"name" => htmlspecialchars($product['name'], ENT_QUOTES)
]); ?>)'
data-bs-toggle="tooltip" data-bs-placement="top" title="Aggiungi Variazione">
<i class="bx bx-plus-circle"></i>
</button>
<button type="button" class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#editProductModal"
onclick='fillEditProductModal(<?php echo json_encode([
"id" => $product['id'],
"variation_id" => null,
"name" => htmlspecialchars($product['name'], ENT_QUOTES),
"variation_name" => null,
"type" => $product['type'],
"price" => null,
"duration_days" => null,
"max_entries" => null,
"weekly_limit" => null,
"max_recoveries" => null,
"recovery_validity_days" => null,
"allow_freeze" => null,
"freeze_max_days" => null,
"max_inventory" => null,
"current_inventory" => null,
"is_full_access" => $product['is_full_access'],
"status" => $product['status']
]); ?>)'
data-bs-toggle="tooltip" data-bs-placement="top" title="Modifica Prodotto">
<i class="bx bx-edit"></i>
</button>
<form action="" method="POST" style="display:inline;" onsubmit="return confirm('Sei sicuro di voler eliminare questo prodotto e tutte le sue variazioni?');">
<input type="hidden" name="action" value="delete_product">
<input type="hidden" name="id" value="<?php echo $product['id']; ?>">
<button type="submit" class="btn btn-sm btn-danger" data-bs-toggle="tooltip" data-bs-placement="top" title="Elimina Prodotto">
<i class="bx bx-trash"></i>
</button>
</form>
</td>
</tr>
<!-- Righe delle variazioni (indentate) -->
<?php foreach ($product['variations'] as $variation): ?>
<tr class="variation-row">
<td class="ps-5"><?php echo htmlspecialchars($product['name']); ?></td>
<td><?php echo htmlspecialchars($variation['variation_name']); ?></td>
<td>
<?php
echo $type_labels[$product['type']] ?? $product['type'];
?>
</td>
<td><?php echo number_format($variation['price'] ?? 0, 2); ?> €</td>
<td><?php echo $variation['duration_days'] ?? 'N/A'; ?></td>
<td><?php echo $variation['max_entries'] ?? 'Illimitati'; ?></td>
<td>
<?php
if ($variation['max_inventory'] !== null) {
echo htmlspecialchars($variation['current_inventory']) . ' / ' . htmlspecialchars($variation['max_inventory']);
} else {
echo 'Illimitato';
}
?>
</td>
<td><?php echo $product['is_full_access'] ? 'Sì' : 'No'; ?></td>
<td>
<?php
if ($product['is_full_access']) {
echo 'Tutte le classi';
} elseif ($class_names || $variation_names) {
echo htmlspecialchars($class_names);
if ($variation_names) {
echo '<br><small>' . htmlspecialchars($variation_names) . '</small>';
}
} else {
echo 'Nessuna classe associata';
}
?>
</td>
<td>
<button type="button" class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#editProductModal"
onclick='fillEditProductModal(<?php echo json_encode([
"id" => $product['id'],
"variation_id" => $variation['variation_id'],
"name" => htmlspecialchars($product['name'], ENT_QUOTES),
"variation_name" => htmlspecialchars($variation['variation_name'], ENT_QUOTES),
"type" => $product['type'],
"price" => $variation['price'],
"duration_days" => $variation['duration_days'],
"max_entries" => $variation['max_entries'],
"weekly_limit" => $variation['weekly_limit'],
"max_recoveries" => $variation['max_recoveries'],
"recovery_validity_days" => $variation['recovery_validity_days'],
"allow_freeze" => $variation['allow_freeze'],
"freeze_max_days" => $variation['freeze_max_days'],
"max_inventory" => $variation['max_inventory'],
"current_inventory" => $variation['current_inventory'],
"is_full_access" => $product['is_full_access'],
"status" => $variation['variation_status']
]); ?>)'
data-bs-toggle="tooltip" data-bs-placement="top" title="Modifica Variazione">
<i class="bx bx-edit"></i>
</button>
<form action="" method="POST" style="display:inline;" onsubmit="return confirm('Sei sicuro di voler eliminare questa variazione?');">
<input type="hidden" name="action" value="delete_variation">
<input type="hidden" name="variation_id" value="<?php echo $variation['variation_id']; ?>">
<button type="submit" class="btn btn-sm btn-danger" data-bs-toggle="tooltip" data-bs-placement="top" title="Elimina Variazione">
<i class="bx bx-trash"></i>
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<!-- ================================================================== -->
<!-- MODALE AGGIUNGI PRODOTTO (era mancante!) -->
<!-- ================================================================== -->
<div class="modal fade" id="addProductModal" tabindex="-1" aria-labelledby="addProductModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" style="max-width: 90vw; height: 90vh;">
<div class="modal-content d-flex flex-column" style="height: 100%;">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addProductModalLabel">Aggiungi Nuovo Prodotto</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="" method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="add_product">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label">Nome Prodotto <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="col-md-6">
<label class="form-label">Nome Variazione <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="variation_name" required placeholder="es. 10 ingressi">
</div>
<div class="col-md-6">
<label class="form-label">Tipo <span class="text-danger">*</span></label>
<select class="form-select precompiled-field" name="type" required>
<?php foreach ($allowed_types as $type): ?>
<?php if (isset($types_map[$type])): ?>
<option value="<?php echo $type; ?>">
<?php echo $types_map[$type]; ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label">Prezzo (€) <span class="text-danger">*</span></label>
<input type="number" step="0.01" class="form-control" name="price" required>
</div>
<div class="col-md-6">
<label class="form-label">Durata (giorni)</label>
<input type="number" class="form-control" name="duration_days">
<small class="text-muted">Lascia vuoto per lezioni singole</small>
</div>
<div class="col-md-6">
<label class="form-label">Max Ingressi</label>
<input type="number" class="form-control" name="max_entries">
<small class="text-muted">Lascia vuoto per abbonamenti illimitati</small>
</div>
<div class="col-md-6">
<label class="form-label">Limite settimanale</label>
<input type="number" class="form-control" name="weekly_limit">
</div>
<div class="col-md-6">
<label class="form-label">Max Recuperi</label>
<input type="number" class="form-control" name="max_recoveries">
</div>
<div class="col-md-6">
<label class="form-label">Validità recuperi (giorni)</label>
<input type="number" class="form-control" name="recovery_validity_days">
</div>
<div class="col-md-6">
<label class="form-label">Inventario massimo</label>
<input type="number" class="form-control" name="max_inventory">
</div>
<div class="col-md-6">
<label class="form-label">Permetti congelamento</label>
<select class="form-select precompiled-field" name="allow_freeze">
<option value="0" <?php echo !$school_settings['allow_freeze_global'] ? 'selected' : ''; ?>>No</option>
<option value="1" <?php echo $school_settings['allow_freeze_global'] ? 'selected' : ''; ?>>Sì</option>
</select>
</div>
<div class="col-md-6">
<label class="form-label">Giorni max congelamento</label>
<input type="number" class="form-control precompiled-field" name="freeze_max_days"
value="<?php echo $school_settings['freeze_max_days_global']; ?>">
</div>
<div class="col-md-6">
<label class="form-label">Accesso completo a tutte le classi</label>
<select class="form-select precompiled-field" id="add_product_is_full_access" name="is_full_access" onchange="toggleClassTypesSection(this.value, 'add')">
<option value="0" <?php echo !$school_settings['allow_full_access_rebooking'] ? 'selected' : ''; ?>>No</option>
<option value="1" <?php echo $school_settings['allow_full_access_rebooking'] ? 'selected' : ''; ?>>Sì</option>
</select>
</div>
<div class="col-md-6">
<label class="form-label">Propaga automaticamente all'ordine</label>
<select class="form-select precompiled-field" name="auto_propagate_to_order">
<option value="0" <?php echo !$school_settings['auto_propagate_on_purchase'] ? 'selected' : ''; ?>>No</option>
<option value="1" <?php echo $school_settings['auto_propagate_on_purchase'] ? 'selected' : ''; ?>>Sì</option>
</select>
</div>
<!-- SEZIONE CLASSI (nascondibile) -->
<div class="col-12" id="add_class_types_section">
<label class="form-label">Classi e orari associati</label>
<div class="border rounded p-3" style="max-height: 300px; overflow-y: auto;">
<?php foreach ($all_classes as $class_id => $class): ?>
<div class="form-check">
<input class="form-check-input class-checkbox" type="checkbox" id="add_class_<?php echo $class_id; ?>" data-class-id="<?php echo $class_id; ?>">
<label class="form-check-label fw-bold" for="add_class_<?php echo $class_id; ?>">
<?php echo htmlspecialchars($class['name']); ?>
</label>
<?php if (!empty($class['variations'])): ?>
<div class="ms-4">
<?php foreach ($class['variations'] as $variation): ?>
<div class="form-check">
<input class="form-check-input variation-checkbox" type="checkbox" name="class_types[]" value="<?php echo $variation['id']; ?>" id="add_variation_<?php echo $variation['id']; ?>" data-class-id="<?php echo $class_id; ?>">
<label class="form-check-label" for="add_variation_<?php echo $variation['id']; ?>">
<?php echo htmlspecialchars($variation['name']); ?>
</label>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annulla</button>
<button type="submit" class="btn btn-primary">Crea Prodotto</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Modale per aggiungere una variazione a un prodotto esistente -->
<!-- Modale per aggiungere una variazione a un prodotto esistente -->
<div class="modal fade" id="addVariationModal" tabindex="-1" aria-labelledby="addVariationModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" style="max-width: 90vw; height: 90vh;">
<div class="modal-content d-flex flex-column" style="height: 100%;">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addVariationModalLabel">Aggiungi Variazione</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="" method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="add_variation">
<input type="hidden" name="product_id" id="add_variation_product_id">
<div class="row">
<div class="col-md-6 mb-3">
<label for="add_variation_product_name" class="form-label">Nome Prodotto</label>
<input type="text" class="form-control" id="add_variation_product_name" disabled>
</div>
<div class="col-md-6 mb-3">
<label for="add_variation_name" class="form-label">Nome Variazione</label>
<input type="text" class="form-control" id="add_variation_name" name="variation_name" required placeholder="Es. 5 Ingressi">
</div>
<div class="col-md-6 mb-3">
<label for="add_variation_price" class="form-label">Prezzo (€)</label>
<input type="number" step="0.01" class="form-control" id="add_variation_price" name="price" required>
</div>
<div class="col-md-6 mb-3">
<label for="add_variation_duration_days" class="form-label">Durata (giorni)</label>
<input type="number" class="form-control" id="add_variation_duration_days" name="duration_days">
<small class="form-text text-muted">Lascia vuoto per lezioni singole.</small>
</div>
<div class="col-md-6 mb-3">
<label for="add_variation_max_entries" class="form-label">Numero Massimo di Ingressi</label>
<input type="number" class="form-control" id="add_variation_max_entries" name="max_entries">
<small class="form-text text-muted">Lascia vuoto per abbonamenti illimitati.</small>
</div>
<div class="col-md-6 mb-3">
<label for="add_variation_weekly_limit" class="form-label">Limite Settimanale</label>
<input type="number" class="form-control" id="add_variation_weekly_limit" name="weekly_limit">
<small class="form-text text-muted">Lascia vuoto se non applicabile.</small>
</div>
<div class="col-md-6 mb-3">
<label for="add_variation_max_recoveries" class="form-label">Numero Massimo di Recuperi</label>
<input type="number" class="form-control" id="add_variation_max_recoveries" name="max_recoveries">
<small class="form-text text-muted">Lascia vuoto se non applicabile.</small>
</div>
<div class="col-md-6 mb-3">
<label for="add_variation_recovery_validity_days" class="form-label">Validità Recuperi (giorni)</label>
<input type="number" class="form-control" id="add_variation_recovery_validity_days" name="recovery_validity_days">
<small class="form-text text-muted">Lascia vuoto se non applicabile.</small>
</div>
<div class="col-md-6 mb-3">
<label for="add_variation_max_inventory" class="form-label">Inventario Massimo</label>
<input type="number" class="form-control" id="add_variation_max_inventory" name="max_inventory">
<small class="form-text text-muted">Lascia vuoto per illimitato.</small>
</div>
<div class="col-md-6 mb-3">
<label for="add_variation_allow_freeze" class="form-label">Permetti Congelamento</label>
<select class="form-select precompiled-field" name="allow_freeze">
<option value="0" <?php echo !$school_settings['allow_freeze_global'] ? 'selected' : ''; ?>>No</option>
<option value="1" <?php echo $school_settings['allow_freeze_global'] ? 'selected' : ''; ?>>Sì</option>
</select>
</div>
<div class="col-md-6 mb-3">
<label for="add_variation_freeze_max_days" class="form-label">Giorni Massimi di Congelamento</label>
<input type="number" class="form-control precompiled-field" name="freeze_max_days"
value="<?php echo $school_settings['freeze_max_days_global']; ?>">
<small class="form-text text-muted">Lascia vuoto se non applicabile.</small>
</div>
<div class="col-md-6 mb-3">
<label for="add_variation_auto_propagate" class="form-label">Propaga in Automatico all'Ordine</label>
<select class="form-select precompiled-field" name="auto_propagate_to_order">
<option value="0" <?php echo !$school_settings['auto_propagate_on_purchase'] ? 'selected' : ''; ?>>No</option>
<option value="1" <?php echo $school_settings['auto_propagate_on_purchase'] ? 'selected' : ''; ?>>Sì</option>
</select>
<small class="form-text text-muted">Se sì, la variazione verrà aggiunta automaticamente agli ordini futuri.</small>
</div>
<div class="col-md-12 mb-3" id="add_variation_class_types_section">
<label class="form-label">Classi e Variazioni Associate</label>
<p class="text-muted small">Se non selezioni nessuna classe, verranno ereditate le classi del prodotto.</p>
<?php foreach ($all_classes as $class_id => $class): ?>
<div class="form-check">
<input class="form-check-input class-checkbox" type="checkbox" id="add_variation_class_<?php echo $class_id; ?>" data-class-id="<?php echo $class_id; ?>">
<label class="form-check-label" for="add_variation_class_<?php echo $class_id; ?>">
<?php echo htmlspecialchars($class['name']); ?>
</label>
<?php if (!empty($class['variations'])): ?>
<div class="ms-4">
<?php foreach ($class['variations'] as $variation): ?>
<div class="form-check">
<input class="form-check-input variation-checkbox" type="checkbox" name="class_types[]" value="<?php echo $variation['id']; ?>" id="add_variation_variation_<?php echo $variation['id']; ?>" data-class-id="<?php echo $class_id; ?>">
<label class="form-check-label" for="add_variation_variation_<?php echo $variation['id']; ?>">
<?php echo htmlspecialchars($variation['name']); ?>
</label>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Chiudi</button>
<button type="submit" class="btn btn-primary">Aggiungi Variazione</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Modale per modificare un prodotto -->
<div class="modal fade" id="editProductModal" tabindex="-1" aria-labelledby="editProductModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editProductModalLabel">Modifica</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="" method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="edit_product">
<input type="hidden" name="id" id="edit_product_id">
<input type="hidden" name="variation_id" id="edit_product_variation_id">
<div class="row">
<div class="col-md-6 mb-3">
<label for="edit_product_name" class="form-label">Nome Prodotto</label>
<input type="text" class="form-control" id="edit_product_name" name="name" required>
</div>
<div class="col-md-6 mb-3" id="edit_variation_name_section">
<label for="edit_product_variation_name" class="form-label">Nome Variazione</label>
<input type="text" class="form-control" id="edit_product_variation_name" name="variation_name">
</div>
<div class="col-md-6 mb-3">
<label class="form-label">Tipo <span class="text-danger">*</span></label>
<select class="form-select precompiled-field" name="type" required>
<?php foreach ($allowed_types as $type): ?>
<?php if (isset($types_map[$type])): ?>
<option value="<?php echo $type; ?>">
<?php echo $types_map[$type]; ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6 mb-3" id="edit_price_section">
<label for="edit_product_price" class="form-label">Prezzo (€)</label>
<input type="number" step="0.01" class="form-control" id="edit_product_price" name="price">
</div>
<div class="col-md-6 mb-3" id="edit_duration_days_section">
<label for="edit_product_duration_days" class="form-label">Durata (giorni)</label>
<input type="number" class="form-control" id="edit_product_duration_days" name="duration_days">
</div>
<div class="col-md-6 mb-3" id="edit_max_entries_section">
<label for="edit_product_max_entries" class="form-label">Numero Massimo di Ingressi</label>
<input type="number" class="form-control" id="edit_product_max_entries" name="max_entries">
</div>
<div class="col-md-6 mb-3" id="edit_weekly_limit_section">
<label for="edit_product_weekly_limit" class="form-label">Limite Settimanale</label>
<input type="number" class="form-control" id="edit_product_weekly_limit" name="weekly_limit">
</div>
<div class="col-md-6 mb-3" id="edit_max_recoveries_section">
<label for="edit_product_max_recoveries" class="form-label">Numero Massimo di Recuperi</label>
<input type="number" class="form-control" id="edit_product_max_recoveries" name="max_recoveries">
</div>
<div class="col-md-6 mb-3" id="edit_recovery_validity_days_section">
<label for="edit_product_recovery_validity_days" class="form-label">Validità Recuperi (giorni)</label>
<input type="number" class="form-control" id="edit_product_recovery_validity_days" name="recovery_validity_days">
</div>
<div class="col-md-6 mb-3" id="edit_max_inventory_section">
<label for="edit_product_max_inventory" class="form-label">Inventario Massimo</label>
<input type="number" class="form-control" id="edit_product_max_inventory" name="max_inventory">
</div>
<div class="col-md-6 mb-3" id="edit_current_inventory_section">
<label for="edit_product_current_inventory" class="form-label">Inventario Corrente</label>
<input type="number" class="form-control" id="edit_product_current_inventory" name="current_inventory">
</div>
<div class="col-md-6 mb-3" id="edit_allow_freeze_section">
<label for="edit_product_allow_freeze" class="form-label">Permetti Congelamento</label>
<select class="form-control" id="edit_product_allow_freeze" name="allow_freeze">
<option value="0">No</option>
<option value="1">Sì</option>
</select>
</div>
<div class="col-md-6 mb-3" id="edit_freeze_max_days_section">
<label for="edit_product_freeze_max_days" class="form-label">Giorni Massimi di Congelamento</label>
<input type="number" class="form-control" id="edit_product_freeze_max_days" name="freeze_max_days">
</div>
<div class="col-md-6 mb-3">
<label for="edit_product_is_full_access" class="form-label">Accesso Completo</label>
<select class="form-control" id="edit_product_is_full_access" name="is_full_access">
<option value="0">No</option>
<option value="1">Sì</option>
</select>
</div>
<div class="col-md-6 mb-3">
<label for="edit_product_status" class="form-label">Stato</label>
<select class="form-control" id="edit_product_status" name="status">
<option value="active">Attivo</option>
<option value="inactive">Inattivo</option>
</select>
</div>
<div class="col-md-6 mb-3">
<label for="edit_product_auto_propagate" class="form-label">Propaga in Automatico all'Ordine</label>
<select class="form-control" id="edit_product_auto_propagate" name="auto_propagate_to_order">
<option value="0">No</option>
<option value="1">Sì</option>
</select>
<small class="form-text text-muted">Se sì, il prodotto o la variazione verrà aggiunto automaticamente agli ordini futuri.</small>
</div>
<div class="col-md-12 mb-3" id="edit_class_types_section">
<label class="form-label">Classi e Variazioni Associate</label>
<?php foreach ($all_classes as $class_id => $class): ?>
<div class="form-check">
<input class="form-check-input class-checkbox" type="checkbox" id="edit_class_<?php echo $class_id; ?>" data-class-id="<?php echo $class_id; ?>">
<label class="form-check-label" for="edit_class_<?php echo $class_id; ?>">
<?php echo htmlspecialchars($class['name']); ?>
</label>
<?php if (!empty($class['variations'])): ?>
<div class="ms-4">
<?php foreach ($class['variations'] as $variation): ?>
<div class="form-check">
<input class="form-check-input variation-checkbox" type="checkbox" name="class_types[]" value="<?php echo $variation['id']; ?>" id="edit_variation_<?php echo $variation['id']; ?>" data-class-id="<?php echo $class_id; ?>">
<label class="form-check-label" for="edit_variation_<?php echo $variation['id']; ?>">
<?php echo htmlspecialchars($variation['name']); ?>
</label>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Chiudi</button>
<button type="submit" class="btn btn-primary">Salva</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!--end page wrapper -->
<!--start overlay-->
<div class="overlay toggle-icon"></div>
<!--end overlay-->
<!--Start Back To Top Button-->
<a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
<!--End Back To Top Button-->
<?php include('include/footer.php'); ?>
</div>
<!--end wrapper-->
<!-- search modal -->
<?php //include('include/searchmodal.php');
?>
<!-- end search modal -->
<!--start switcher-->
<?php //include('include/themeswitcher.php');
?>
<!--end switcher-->
<?php include('jsinclude.php'); ?>
<script>
// Funzione per gestire la visibilità della sezione "Classi e Variazioni Associate"
function toggleClassTypesSection(isFullAccess, modalType) {
const section = document.getElementById(`${modalType}_class_types_section`);
if (isFullAccess == 1) {
section.style.display = 'none';
// Deseleziona tutte le checkbox
section.querySelectorAll('.class-checkbox, .variation-checkbox').forEach(checkbox => {
checkbox.checked = false;
});
} else {
section.style.display = 'block';
}
}
$(document).ready(function() {
// Distruggi l'istanza precedente di DataTables, se esiste
if ($.fn.DataTable.isDataTable('#productsTable')) {
$('#productsTable').DataTable().destroy();
}
// Inizializza DataTables
$('#productsTable').DataTable({
"language": {
"url": "//cdn.datatables.net/plug-ins/1.10.25/i18n/Italian.json"
}
});
// Inizializza i tooltip
$('[data-bs-toggle="tooltip"]').tooltip();
// Gestione selezione/deselezione delle classi e variazioni
$('.class-checkbox').on('change', function() {
const classId = $(this).data('class-id');
const isChecked = $(this).is(':checked');
$(`.variation-checkbox[data-class-id="${classId}"]`).prop('checked', isChecked);
});
$('.variation-checkbox').on('change', function() {
const classId = $(this).data('class-id');
const classCheckbox = $(`#add_class_${classId}, #edit_class_${classId}`);
const variations = $(`.variation-checkbox[data-class-id="${classId}"]`);
const allChecked = variations.length === variations.filter(':checked').length;
const someChecked = variations.filter(':checked').length > 0;
if (allChecked) {
classCheckbox.prop('checked', true).prop('indeterminate', false);
} else if (someChecked) {
classCheckbox.prop('checked', false).prop('indeterminate', true);
} else {
classCheckbox.prop('checked', false).prop('indeterminate', false);
}
});
$('#add_product_is_full_access').on('change', function() {
toggleClassTypesSection($(this).val(), 'add');
});
$('#edit_product_is_full_access').on('change', function() {
toggleClassTypesSection($(this).val(), 'edit');
});
});
function fillAddVariationModal(data) {
document.getElementById('add_variation_product_id').value = data.id;
document.getElementById('add_variation_product_name').value = data.name;
// Recupera le classi associate al prodotto e i dettagli
fetch(`get_product_classes.php?product_id=${data.id}`)
.then(response => {
if (!response.ok) {
throw new Error('Errore nella risposta del server: ' + response.statusText);
}
return response.json();
})
.then(data => {
console.log('Dati ricevuti:', data);
// Resetta tutte le checkbox
document.querySelectorAll('#add_variation_class_types_section .variation-checkbox').forEach(checkbox => {
checkbox.checked = false;
});
document.querySelectorAll('#add_variation_class_types_section .class-checkbox').forEach(checkbox => {
checkbox.checked = false;
checkbox.indeterminate = false;
});
// Seleziona le variazioni associate al prodotto
const class_types = data.class_types || [];
document.querySelectorAll('#add_variation_class_types_section .variation-checkbox').forEach(checkbox => {
const checkboxValue = parseInt(checkbox.value);
if (class_types.map(Number).includes(checkboxValue)) {
checkbox.checked = true;
}
});
// Aggiorna lo stato delle checkbox delle classi
document.querySelectorAll('#add_variation_class_types_section .class-checkbox').forEach(classCheckbox => {
const classId = classCheckbox.getAttribute('data-class-id');
const variations = document.querySelectorAll(`#add_variation_class_types_section .variation-checkbox[data-class-id="${classId}"]`);
const checkedVariations = document.querySelectorAll(`#add_variation_class_types_section .variation-checkbox[data-class-id="${classId}"]:checked`);
if (variations.length === checkedVariations.length && variations.length > 0) {
classCheckbox.checked = true;
classCheckbox.indeterminate = false;
} else if (checkedVariations.length > 0) {
classCheckbox.checked = false;
classCheckbox.indeterminate = true;
} else {
classCheckbox.checked = false;
classCheckbox.indeterminate = false;
}
});
// Gestisci la visibilità della sezione "Classi e Variazioni Associate" e il campo auto_propagate
const isFullAccess = data.is_full_access || 0;
const autoPropagate = data.auto_propagate_to_order || 0;
const section = document.getElementById('add_variation_class_types_section');
if (isFullAccess == 1) {
section.style.display = 'none';
section.querySelectorAll('.class-checkbox, .variation-checkbox').forEach(checkbox => {
checkbox.checked = false;
});
} else {
section.style.display = 'block';
}
// Imposta il valore predefinito di auto_propagate_to_order
document.getElementById('add_variation_auto_propagate').value = autoPropagate;
})
.catch(error => {
console.error('Errore nel fetch delle classi associate:', error);
});
}
function fillEditProductModal(data) {
// Determina se stiamo modificando il prodotto o una variazione
const isVariation = data.variation_id !== null;
// Aggiorna il titolo del modale
document.getElementById('editProductModalLabel').innerText = isVariation ? 'Modifica Variazione' : 'Modifica Prodotto';
// Popola i campi comuni
document.getElementById('edit_product_id').value = data.id;
document.getElementById('edit_product_variation_id').value = data.variation_id || '';
document.getElementById('edit_product_name').value = data.name;
document.getElementById('edit_product_type').value = data.type;
document.getElementById('edit_product_is_full_access').value = data.is_full_access || 0;
document.getElementById('edit_product_status').value = data.status;
document.getElementById('edit_product_auto_propagate').value = data.auto_propagate_to_order || 0;
// Disabilita il campo "Nome Prodotto" se stiamo modificando una variazione
const productNameInput = document.getElementById('edit_product_name');
productNameInput.disabled = isVariation;
if (isVariation) {
productNameInput.classList.add('bg-light');
} else {
productNameInput.classList.remove('bg-light');
}
// Gestisci i campi specifici della variazione
const variationFields = [
'edit_variation_name_section',
'edit_price_section',
'edit_duration_days_section',
'edit_max_entries_section',
'edit_weekly_limit_section',
'edit_max_recoveries_section',
'edit_recovery_validity_days_section',
'edit_max_inventory_section',
'edit_current_inventory_section',
'edit_allow_freeze_section',
'edit_freeze_max_days_section'
];
if (isVariation) {
// Mostra i campi della variazione
variationFields.forEach(field => document.getElementById(field).style.display = 'block');
document.getElementById('edit_product_variation_name').value = data.variation_name || '';
document.getElementById('edit_product_price').value = data.price || '';
document.getElementById('edit_product_duration_days').value = data.duration_days || '';
document.getElementById('edit_product_max_entries').value = data.max_entries || '';
document.getElementById('edit_product_weekly_limit').value = data.weekly_limit || '';
document.getElementById('edit_product_max_recoveries').value = data.max_recoveries || '';
document.getElementById('edit_product_recovery_validity_days').value = data.recovery_validity_days || '';
document.getElementById('edit_product_allow_freeze').value = data.allow_freeze || 0;
document.getElementById('edit_product_freeze_max_days').value = data.freeze_max_days || '';
document.getElementById('edit_product_max_inventory').value = data.max_inventory || '';
document.getElementById('edit_product_current_inventory').value = data.current_inventory || '';
} else {
// Nascondi i campi della variazione
variationFields.forEach(field => document.getElementById(field).style.display = 'none');
}
// Recupera le classi associate al prodotto o alla variazione
const fetchUrl = isVariation ?
`get_product_classes.php?product_id=${data.id}&variation_id=${data.variation_id}` :
`get_product_classes.php?product_id=${data.id}`;
fetch(fetchUrl)
.then(response => {
if (!response.ok) {
throw new Error('Errore nella risposta del server: ' + response.statusText);
}
return response.json();
})
.then(data => {
console.log('Class types associati:', data);
// Resetta tutte le checkbox solo se necessario
document.querySelectorAll('#edit_class_types_section .variation-checkbox').forEach(checkbox => {
checkbox.checked = false;
});
document.querySelectorAll('#edit_class_types_section .class-checkbox').forEach(checkbox => {
checkbox.checked = false;
checkbox.indeterminate = false;
});
// Seleziona le variazioni associate
const class_types = data.class_types || [];
document.querySelectorAll('#edit_class_types_section .variation-checkbox').forEach(checkbox => {
const checkboxValue = parseInt(checkbox.value);
if (class_types.map(Number).includes(checkboxValue)) {
checkbox.checked = true;
}
});
// Aggiorna lo stato delle checkbox delle classi
document.querySelectorAll('#edit_class_types_section .class-checkbox').forEach(classCheckbox => {
const classId = classCheckbox.getAttribute('data-class-id');
const variations = document.querySelectorAll(`#edit_class_types_section .variation-checkbox[data-class-id="${classId}"]`);
const checkedVariations = document.querySelectorAll(`#edit_class_types_section .variation-checkbox[data-class-id="${classId}"]:checked`);
if (variations.length === checkedVariations.length && variations.length > 0) {
classCheckbox.checked = true;
classCheckbox.indeterminate = false;
} else if (checkedVariations.length > 0) {
classCheckbox.checked = false;
classCheckbox.indeterminate = true;
} else {
classCheckbox.checked = false;
classCheckbox.indeterminate = false;
}
});
// Gestisci la visibilità della sezione "Classi e Variazioni Associate" DOPO aver caricato le classi
toggleClassTypesSection(data.is_full_access, 'edit');
})
.catch(error => {
console.error('Errore nel fetch delle classi associate:', error);
// Anche in caso di errore, gestisci la visibilità
toggleClassTypesSection(data.is_full_access, 'edit');
});
}
</script>
</body>
</html>