Files
comelifacciamo/public/userarea/admin_subscription_action.php
T
2026-09-09 08:26:26 +02:00

165 lines
5.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// admin_subscription_action.php — Azioni admin sugli abbonamenti dei saloni
// Azioni: assegna/aggiorna abbonamento, varia date, regala mesi, blocca (violazione), sblocca.
// Ogni cambiamento di stato sincronizza shops.active. Solo ROLE_ADMIN.
ini_set('display_errors', 1);
error_reporting(E_ALL);
if (session_status() === PHP_SESSION_NONE) session_start();
include('include/headscript.php');
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
if (!isset($iduserlogin)) {
header("Location: login.php");
exit;
}
if ((int)$kindofrole !== ROLE_ADMIN) {
header("Location: index.php");
exit;
}
function setFlash(string $type, string $text): void
{
$_SESSION['flash'] = ['type' => $type, 'text' => $text];
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
setFlash('danger', 'Richiesta non valida.');
header("Location: admin_subscriptions.php");
exit;
}
$shop_id = (int)($_POST['shop_id'] ?? 0);
$action = trim($_POST['action'] ?? '');
$ret = trim($_POST['ret'] ?? '');
function back(string $ret): void
{
header("Location: admin_subscriptions.php" . ($ret !== '' ? '?' . $ret : ''));
exit;
}
if ($shop_id <= 0 || $action === '') {
setFlash('danger', 'Dati azione mancanti.');
back($ret);
}
/**
* Sincronizza shops.active in base allo stato dell'abbonamento.
* active/trialing non scaduto → salone acceso; expired/blocked/pending → spento.
*/
function syncShopActive(PDO $pdo, int $shop_id, string $status, ?string $expires): void
{
$shouldBeActive = 0;
if (in_array($status, ['active', 'trialing'], true)) {
if ($expires === null || $expires >= date('Y-m-d')) {
$shouldBeActive = 1;
}
}
$u = $pdo->prepare("UPDATE shops SET active = ? WHERE id = ?");
$u->execute([$shouldBeActive, $shop_id]);
}
try {
// Il salone esiste?
$s = $pdo->prepare("SELECT id FROM shops WHERE id = ?");
$s->execute([$shop_id]);
if (!$s->fetch()) {
setFlash('danger', 'Salone non trovato.');
back($ret);
}
// Carica l'abbonamento corrente (se esiste)
$cur = $pdo->prepare("SELECT * FROM shop_subscriptions WHERE shop_id = ?");
$cur->execute([$shop_id]);
$sub = $cur->fetch(PDO::FETCH_ASSOC);
// ===== Assegna / aggiorna abbonamento (piano + date + stato) =====
if ($action === 'save') {
$plan_id = (int)($_POST['plan_id'] ?? 0) ?: null;
$status = trim($_POST['status'] ?? 'active');
$started = trim($_POST['started_at'] ?? '') ?: null;
$expires = trim($_POST['expires_at'] ?? '') ?: null;
$allowed = ['active', 'expired', 'blocked', 'trialing', 'pending'];
if (!in_array($status, $allowed, true)) {
setFlash('danger', 'Stato non valido.');
back($ret);
}
if ($sub) {
$upd = $pdo->prepare("
UPDATE shop_subscriptions
SET plan_id = ?, status = ?, started_at = ?, expires_at = ?
WHERE shop_id = ?
");
$upd->execute([$plan_id, $status, $started, $expires, $shop_id]);
} else {
$ins = $pdo->prepare("
INSERT INTO shop_subscriptions (shop_id, plan_id, status, started_at, expires_at)
VALUES (?, ?, ?, ?, ?)
");
$ins->execute([$shop_id, $plan_id, $status, $started, $expires]);
}
syncShopActive($pdo, $shop_id, $status, $expires);
setFlash('success', 'Abbonamento aggiornato.');
back($ret);
}
if (!$sub) {
setFlash('warning', 'Questo salone non ha ancora un abbonamento: usa "Assegna abbonamento".');
back($ret);
}
// ===== Regala mesi (estende expires_at) =====
if ($action === 'gift_months') {
$months = (int)($_POST['months'] ?? 0);
if ($months < 1 || $months > 24) {
setFlash('danger', 'Numero di mesi non valido (1–24).');
back($ret);
}
// Base di partenza: la scadenza attuale se futura, altrimenti oggi
$baseDate = ($sub['expires_at'] && $sub['expires_at'] >= date('Y-m-d'))
? $sub['expires_at'] : date('Y-m-d');
$newExpires = date('Y-m-d', strtotime($baseDate . " +{$months} months"));
// Regalare mesi riattiva l'abbonamento
$upd = $pdo->prepare("UPDATE shop_subscriptions SET expires_at = ?, status = 'active' WHERE shop_id = ?");
$upd->execute([$newExpires, $shop_id]);
syncShopActive($pdo, $shop_id, 'active', $newExpires);
setFlash('success', "Regalati {$months} mese/i. Nuova scadenza: " . date('d/m/Y', strtotime($newExpires)) . ".");
back($ret);
}
// ===== Blocca per violazione =====
if ($action === 'block') {
$reason = trim($_POST['block_reason'] ?? '');
$upd = $pdo->prepare("UPDATE shop_subscriptions SET status = 'blocked', block_reason = ? WHERE shop_id = ?");
$upd->execute([($reason ?: 'Violazione regolamento'), $shop_id]);
syncShopActive($pdo, $shop_id, 'blocked', $sub['expires_at']);
setFlash('success', 'Salone bloccato.');
back($ret);
}
// ===== Sblocca (torna active se non scaduto, altrimenti expired) =====
if ($action === 'unblock') {
$newStatus = ($sub['expires_at'] === null || $sub['expires_at'] >= date('Y-m-d')) ? 'active' : 'expired';
$upd = $pdo->prepare("UPDATE shop_subscriptions SET status = ?, block_reason = NULL WHERE shop_id = ?");
$upd->execute([$newStatus, $shop_id]);
syncShopActive($pdo, $shop_id, $newStatus, $sub['expires_at']);
setFlash('success', $newStatus === 'active' ? 'Salone sbloccato e riattivato.' : 'Salone sbloccato (ma abbonamento scaduto).');
back($ret);
}
setFlash('danger', 'Azione non riconosciuta.');
back($ret);
} catch (Throwable $e) {
error_log('admin_subscription_action: ' . $e->getMessage());
setFlash('danger', 'Operazione non riuscita.');
back($ret);
}