Subscription Plans (Admin)
Gestione piani abbonamento delle scuole
Elenco Piani
| Code | Nome | Prezzo | Intervallo | Trial | Stripe Product | Stripe Price * | Stato | Azioni |
|---|---|---|---|---|---|---|---|---|
| 0 ? ((int)$p['trial_days'] . ' gg') : '—'; ?> | — |
getConnection(); // Check login if (!isset($iduserlogin)) { die("Errore: ID utente non definito."); } /** * Check if the current user is admin. * IMPORTANT: set your real admin role_id(s) here. */ function isAdmin(PDO $pdo, int $userId): bool { // TODO: adjust these role ids according to your system $adminRoleIds = [1]; // e.g. 1 = superadmin $stmt = $pdo->prepare("SELECT role_id FROM auth_users WHERE id = ?"); $stmt->execute([$userId]); $row = $stmt->fetch(PDO::FETCH_ASSOC); if (!$row) return false; return in_array((int)$row['role_id'], $adminRoleIds, true); } if (!isAdmin($pdo, (int)$iduserlogin)) { die("Accesso negato: pagina riservata all'amministratore."); } function formatMoneyFromCents(int $cents, string $currency): string { $amount = number_format($cents / 100, 2, ',', '.'); return $amount . ' ' . strtoupper($currency); } // Handle POST actions if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? ''; // Common fields $code = trim($_POST['code'] ?? ''); $name = trim($_POST['name'] ?? ''); $description = trim($_POST['description'] ?? ''); $stripe_product_id = trim($_POST['stripe_product_id'] ?? ''); $stripe_price_id = trim($_POST['stripe_price_id'] ?? ''); $currency = strtoupper(trim($_POST['currency'] ?? 'EUR')); $unit_amount = (int)($_POST['unit_amount'] ?? 0); // cents $interval = in_array(($_POST['interval'] ?? ''), ['day', 'week', 'month', 'year'], true) ? $_POST['interval'] : 'month'; $interval_count = max(1, (int)($_POST['interval_count'] ?? 1)); $trial_days = max(0, (int)($_POST['trial_days'] ?? 0)); $is_active = isset($_POST['is_active']) ? 1 : 0; // ADD if ($action === 'add_plan') { if ($code === '' || $name === '') { $error = "Code e Nome sono obbligatori."; } elseif ($stripe_price_id === '') { $error = "Stripe Price ID è obbligatorio (campo NOT NULL in tabella)."; } elseif (strlen($currency) !== 3) { $error = "Currency deve essere nel formato ISO (es. EUR)."; } elseif ($unit_amount < 0) { $error = "Unit amount non può essere negativo."; } else { try { $stmt = $pdo->prepare(" INSERT INTO billing_plans (code, name, description, stripe_product_id, stripe_price_id, currency, unit_amount, `interval`, interval_count, trial_days, is_active, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW()) "); $stmt->execute([ $code, $name, $description ?: null, $stripe_product_id ?: null, $stripe_price_id, $currency, $unit_amount, $interval, $interval_count, $trial_days, $is_active ]); $success_message = "Piano creato con successo."; } catch (PDOException $e) { $error = "Errore durante la creazione del piano: " . $e->getMessage(); } } } // EDIT if ($action === 'edit_plan') { $id = (int)($_POST['id'] ?? 0); if ($id <= 0) { $error = "ID piano non valido."; } elseif ($code === '' || $name === '') { $error = "Code e Nome sono obbligatori."; } elseif ($stripe_price_id === '') { $error = "Stripe Price ID è obbligatorio (campo NOT NULL in tabella)."; } elseif (strlen($currency) !== 3) { $error = "Currency deve essere nel formato ISO (es. EUR)."; } elseif ($unit_amount < 0) { $error = "Unit amount non può essere negativo."; } else { try { $stmt = $pdo->prepare(" UPDATE billing_plans SET code = ?, name = ?, description = ?, stripe_product_id = ?, stripe_price_id = ?, currency = ?, unit_amount = ?, `interval` = ?, interval_count = ?, trial_days = ?, is_active = ?, updated_at = NOW() WHERE id = ? "); $stmt->execute([ $code, $name, $description ?: null, $stripe_product_id ?: null, $stripe_price_id, $currency, $unit_amount, $interval, $interval_count, $trial_days, $is_active, $id ]); $success_message = "Piano aggiornato con successo."; } catch (PDOException $e) { $error = "Errore durante l'aggiornamento del piano: " . $e->getMessage(); } } } // DISABLE if ($action === 'disable_plan') { $id = (int)($_POST['id'] ?? 0); if ($id <= 0) { $error = "ID piano non valido."; } else { $stmt = $pdo->prepare("UPDATE billing_plans SET is_active = 0, updated_at = NOW() WHERE id = ?"); $stmt->execute([$id]); $success_message = "Piano disattivato."; } } // ENABLE if ($action === 'enable_plan') { $id = (int)($_POST['id'] ?? 0); if ($id <= 0) { $error = "ID piano non valido."; } else { $stmt = $pdo->prepare("UPDATE billing_plans SET is_active = 1, updated_at = NOW() WHERE id = ?"); $stmt->execute([$id]); $success_message = "Piano riattivato."; } } } // Fetch plans $stmt = $pdo->prepare("SELECT * FROM billing_plans ORDER BY is_active DESC, name ASC"); $stmt->execute(); $plans = $stmt->fetchAll(PDO::FETCH_ASSOC); ?>