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 i prodotti della scuola con classi e variazioni associate $stmt = $pdo->prepare(" SELECT p.*, GROUP_CONCAT(DISTINCT c.name) AS class_names, GROUP_CONCAT(DISTINCT CONCAT(ct.level, ' (', ct.day_of_week, ')')) AS variation_names FROM products p LEFT JOIN product_class_types pct ON p.id = pct.product_id 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 ORDER BY p.created_at DESC "); $stmt->execute([$school_id]); $products = $stmt->fetchAll(); // 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 if ($action === 'add_product') { $name = $_POST['name'] ?? ''; $type = $_POST['type'] ?? ''; $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; $class_types = $_POST['class_types'] ?? []; if (empty($name) || empty($type) || $price <= 0) { $error = "Nome, tipo e prezzo sono obbligatori."; } else { $current_inventory = $max_inventory; // All'inizio, l'inventario corrente è uguale al massimo $stmt = $pdo->prepare(" INSERT INTO products (school_id, name, type, price, duration_days, max_entries, weekly_limit, max_recoveries, recovery_validity_days, allow_freeze, freeze_max_days, max_inventory, current_inventory) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute([ $school_id, $name, $type, $price, $duration_days, $max_entries, $weekly_limit, $max_recoveries, $recovery_validity_days, $allow_freeze, $freeze_max_days, $max_inventory, $current_inventory ]); $product_id = $pdo->lastInsertId(); // Associa le classi selezionate if (!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; } } // Modifica un prodotto if ($action === 'edit_product') { $id = $_POST['id'] ?? 0; $name = $_POST['name'] ?? ''; $type = $_POST['type'] ?? ''; $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; $status = $_POST['status'] ?? 'active'; $class_types = $_POST['class_types'] ?? []; if ($id <= 0 || empty($name) || empty($type) || $price <= 0) { $error = "ID, nome, tipo e prezzo sono obbligatori."; } else { $stmt = $pdo->prepare(" UPDATE products SET name = ?, type = ?, price = ?, duration_days = ?, max_entries = ?, weekly_limit = ?, max_recoveries = ?, recovery_validity_days = ?, allow_freeze = ?, freeze_max_days = ?, max_inventory = ?, current_inventory = ?, status = ? WHERE id = ? AND school_id = ? "); $stmt->execute([ $name, $type, $price, $duration_days, $max_entries, $weekly_limit, $max_recoveries, $recovery_validity_days, $allow_freeze, $freeze_max_days, $max_inventory, $current_inventory, $status, $id, $school_id ]); // Aggiorna le classi associate $stmt = $pdo->prepare("DELETE FROM product_class_types WHERE product_id = ?"); $stmt->execute([$id]); if (!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([$id, $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; } } } ?>
Prodotti
Nome Tipo Prezzo Durata (giorni) Ingressi Inventario Classi Associate Azioni
Nessun prodotto trovato.
'Carnet', 'subscription' => 'Abbonamento', 'drop_in' => 'Lezione Singola' ]; echo $type_labels[$product['type']] ?? $product['type']; ?> ' . htmlspecialchars($variation_names) . ''; } } else { echo 'Nessuna classe associata'; } ?>