yogiboook_new/public/userarea/admin_subscriptions.php
2026-01-23 08:13:41 +01:00

544 lines
26 KiB
PHP

<?php
// admin_subscriptions.php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include('include/headscript.php');
// DB
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
// ---- Auth check ----
if (!isset($iduserlogin)) {
die("Access denied.");
}
// ---- Admin check (Vanguard usually uses role_id=1 for admin) ----
// Adjust role_id list if needed.
$stmt = $pdo->prepare("SELECT role_id, email FROM auth_users WHERE id = ? LIMIT 1");
$stmt->execute([$iduserlogin]);
$me = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$me || !in_array((int)$me['role_id'], [1])) {
die("Access denied: admin only.");
}
// ---- Handle POST actions ----
$success_message = null;
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$action = $_POST['action'];
// Update school status (local app status)
if ($action === 'update_school_status') {
$school_id = (int)($_POST['school_id'] ?? 0);
$new_status = $_POST['status'] ?? 'active';
$allowed = ['active', 'inactive', 'suspended'];
if ($school_id <= 0 || !in_array($new_status, $allowed, true)) {
$error = "Invalid request.";
} else {
$stmt = $pdo->prepare("UPDATE schools SET status = ? WHERE id = ?");
if ($stmt->execute([$new_status, $school_id])) {
$success_message = "School status updated.";
} else {
$error = "Failed updating school status.";
}
}
}
// NOTE: Stripe actions (cancel, resume, change plan) should call Stripe API.
// Here we provide placeholders so UI is ready.
if ($action === 'flag_cancel_at_period_end') {
$sub_id = (int)($_POST['subscription_row_id'] ?? 0);
$flag = (int)($_POST['flag'] ?? 0);
$school_id = (int)($_POST['school_id'] ?? 0);
if ($sub_id <= 0 || $school_id <= 0 || !in_array($flag, [0, 1], true)) {
$error = "Invalid request.";
} else {
$stmt = $pdo->prepare("
UPDATE school_subscriptions
SET cancel_at_period_end = ?
WHERE id = ? AND school_id = ?
");
$ok = $stmt->execute([$flag, $sub_id, $school_id]);
if ($ok) {
$success_message = $flag ? "Marked cancel at period end (LOCAL)." : "Unmarked cancel at period end (LOCAL).";
} else {
$error = "Failed updating subscription flag.";
}
}
}
}
// ---- Filters (GET) ----
$q = trim($_GET['q'] ?? '');
$plan_id = (int)($_GET['plan_id'] ?? 0);
$sub_status = trim($_GET['sub_status'] ?? ''); // e.g. active, trialing, past_due, canceled, incomplete, unpaid
$school_status = trim($_GET['school_status'] ?? ''); // active, inactive, suspended
$has_sub = $_GET['has_sub'] ?? ''; // '1' or '0' or ''
$where = [];
$params = [];
// Search by school name/email/owner email
if ($q !== '') {
$where[] = "(s.name LIKE ? OR s.email LIKE ? OR ou.email LIKE ? OR CONCAT(ou.first_name,' ',ou.last_name) LIKE ?)";
$like = '%' . $q . '%';
$params[] = $like;
$params[] = $like;
$params[] = $like;
$params[] = $like;
}
if ($plan_id > 0) {
$where[] = "ss.plan_id = ?";
$params[] = $plan_id;
}
if ($sub_status !== '') {
$where[] = "ss.status = ?";
$params[] = $sub_status;
}
if ($school_status !== '') {
$where[] = "s.status = ?";
$params[] = $school_status;
}
if ($has_sub === '1') {
$where[] = "ss.id IS NOT NULL";
} elseif ($has_sub === '0') {
$where[] = "ss.id IS NULL";
}
$sqlWhere = '';
if (!empty($where)) {
$sqlWhere = "WHERE " . implode(" AND ", $where);
}
// ---- Load plans for filter dropdown ----
$stmt = $pdo->prepare("SELECT id, code, name, currency, unit_amount, `interval`, interval_count, is_active FROM billing_plans ORDER BY is_active DESC, unit_amount ASC, name ASC");
$stmt->execute();
$plans = $stmt->fetchAll(PDO::FETCH_ASSOC);
// ---- Main query: schools + subscription + owner + plan ----
$stmt = $pdo->prepare("
SELECT
s.id AS school_id,
s.name AS school_name,
s.email AS school_email,
s.status AS school_status,
s.created_at AS school_created_at,
ou.id AS owner_id,
ou.first_name AS owner_first_name,
ou.last_name AS owner_last_name,
ou.email AS owner_email,
ss.id AS subscription_row_id,
ss.stripe_customer_id,
ss.stripe_subscription_id,
COALESCE(ss.status, 'none') AS subscription_status,
ss.current_period_start,
ss.current_period_end,
ss.trial_start,
ss.trial_end,
ss.cancel_at_period_end,
ss.updated_at AS subscription_updated_at,
bp.id AS plan_id,
bp.code AS plan_code,
bp.name AS plan_name,
bp.currency,
bp.unit_amount,
bp.`interval`,
bp.interval_count
FROM schools s
JOIN auth_users ou ON ou.id = s.owner_id
LEFT JOIN school_subscriptions ss ON ss.school_id = s.id
LEFT JOIN billing_plans bp ON bp.id = ss.plan_id
$sqlWhere
ORDER BY
-- Put schools without subscription at the bottom
CASE WHEN ss.id IS NULL THEN 1 ELSE 0 END ASC,
-- Show problematic subscriptions first
FIELD(ss.status, 'past_due','unpaid','incomplete','incomplete_expired','canceled','paused','trialing','active') ASC,
s.created_at DESC
");
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// ---- Helpers ----
function h($v)
{
return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
function moneyFmt($unit_amount, $currency)
{
if ($unit_amount === null || $currency === null) return '—';
$amount = ((int)$unit_amount) / 100;
return number_format($amount, 2, ',', '.') . ' ' . strtoupper($currency);
}
function dateFmt($dt)
{
if (!$dt) return '—';
// Accept both timestamp and datetime strings
$ts = is_numeric($dt) ? (int)$dt : strtotime($dt);
if (!$ts) return '—';
return date('d/m/Y', $ts);
}
function badgeClassForSub($status)
{
$map = [
'active' => 'bg-success',
'trialing' => 'bg-info',
'past_due' => 'bg-warning',
'unpaid' => 'bg-danger',
'incomplete' => 'bg-warning',
'incomplete_expired' => 'bg-danger',
'canceled' => 'bg-secondary',
'paused' => 'bg-secondary',
'none' => 'bg-secondary',
];
return $map[$status] ?? 'bg-dark';
}
function badgeClassForSchool($status)
{
$map = [
'active' => 'bg-success',
'inactive' => 'bg-secondary',
'suspended' => 'bg-danger',
];
return $map[$status] ?? 'bg-dark';
}
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<div class="card radius-10 mb-3">
<div class="card-body">
<div class="d-flex align-items-center justify-content-between">
<div>
<h5 class="mb-0">Admin Subscriptions</h5>
<div class="text-muted small">Schools + Stripe subscription status overview</div>
</div>
<div class="d-flex gap-2">
<a href="admin_billing_plans.php" class="btn btn-outline-primary">
<i class="bx bx-list-ul"></i> Billing Plans
</a>
</div>
</div>
</div>
</div>
<?php if ($success_message): ?>
<div class="alert alert-success"><?php echo h($success_message); ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo h($error); ?></div>
<?php endif; ?>
<!-- Filters -->
<div class="card radius-10 mb-4">
<div class="card-body">
<form method="GET" class="row g-2 align-items-end">
<div class="col-md-4">
<label class="form-label">Search</label>
<input type="text" class="form-control" name="q" value="<?php echo h($q); ?>" placeholder="School / email / owner">
</div>
<div class="col-md-3">
<label class="form-label">Plan</label>
<select class="form-control" name="plan_id">
<option value="0">All plans</option>
<?php foreach ($plans as $p): ?>
<option value="<?php echo (int)$p['id']; ?>" <?php echo ((int)$p['id'] === $plan_id) ? 'selected' : ''; ?>>
<?php
$label = $p['name'] . ' (' . $p['code'] . ') - ' . moneyFmt($p['unit_amount'], $p['currency']);
$label .= ' / ' . $p['interval_count'] . ' ' . $p['interval'];
if (!(int)$p['is_active']) $label .= ' [inactive]';
echo h($label);
?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label class="form-label">Subscription status</label>
<select class="form-control" name="sub_status">
<option value="">All</option>
<?php foreach (['none', 'active', 'trialing', 'past_due', 'unpaid', 'incomplete', 'incomplete_expired', 'canceled', 'paused'] as $st): ?>
<option value="<?php echo h($st); ?>" <?php echo ($sub_status === $st) ? 'selected' : ''; ?>>
<?php echo h($st); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label class="form-label">School status</label>
<select class="form-control" name="school_status">
<option value="">All</option>
<?php foreach (['active', 'inactive', 'suspended'] as $st): ?>
<option value="<?php echo h($st); ?>" <?php echo ($school_status === $st) ? 'selected' : ''; ?>>
<?php echo h($st); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-1">
<label class="form-label">Has sub</label>
<select class="form-control" name="has_sub">
<option value="" <?php echo ($has_sub === '') ? 'selected' : ''; ?>>All</option>
<option value="1" <?php echo ($has_sub === '1') ? 'selected' : ''; ?>>Yes</option>
<option value="0" <?php echo ($has_sub === '0') ? 'selected' : ''; ?>>No</option>
</select>
</div>
<div class="col-md-12 d-flex gap-2 mt-2">
<button class="btn btn-primary" type="submit">
<i class="bx bx-filter"></i> Apply
</button>
<a class="btn btn-outline-secondary" href="admin_subscriptions.php">Reset</a>
</div>
</form>
</div>
</div>
<!-- Results -->
<div class="card radius-10">
<div class="card-body">
<div class="d-flex align-items-center justify-content-between mb-3">
<div>
<h6 class="mb-0">Schools</h6>
<div class="text-muted small"><?php echo (int)count($rows); ?> rows</div>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped table-bordered align-middle mb-0">
<thead>
<tr>
<th style="min-width:260px;">School</th>
<th style="min-width:220px;">Owner</th>
<th style="min-width:190px;">School status</th>
<th style="min-width:160px;">Subscription</th>
<th style="min-width:220px;">Plan</th>
<th style="min-width:200px;">Period</th>
<th style="min-width:200px;">Trial</th>
<th style="min-width:140px;">Cancel at period end</th>
<th style="min-width:260px;">Stripe IDs</th>
<th style="min-width:160px;">Updated</th>
<th style="min-width:180px;">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($rows as $row): ?>
<?php
$hasSubscription = !empty($row['subscription_row_id']);
$subStatus = $row['subscription_status'] ?? 'none';
?>
<tr>
<td>
<div class="fw-bold"><?php echo h($row['school_name']); ?></div>
<div class="text-muted small">
ID: <?php echo (int)$row['school_id']; ?> · <?php echo h($row['school_email']); ?>
</div>
<div class="text-muted small">
Created: <?php echo h(dateFmt($row['school_created_at'])); ?>
</div>
</td>
<td>
<div class="fw-bold"><?php echo h(trim(($row['owner_first_name'] ?? '') . ' ' . ($row['owner_last_name'] ?? ''))); ?></div>
<div class="text-muted small"><?php echo h($row['owner_email']); ?></div>
</td>
<td>
<form method="POST" class="d-flex gap-2 align-items-center">
<input type="hidden" name="action" value="update_school_status">
<input type="hidden" name="school_id" value="<?php echo (int)$row['school_id']; ?>">
<select name="status" class="form-control form-control-sm" style="min-width:140px;">
<?php foreach (['active', 'inactive', 'suspended'] as $st): ?>
<option value="<?php echo h($st); ?>" <?php echo ($row['school_status'] === $st) ? 'selected' : ''; ?>>
<?php echo h($st); ?>
</option>
<?php endforeach; ?>
</select>
<button type="submit" class="btn btn-sm btn-outline-primary">
Save
</button>
</form>
<div class="mt-2">
<span class="badge <?php echo h(badgeClassForSchool($row['school_status'])); ?>">
<?php echo h($row['school_status']); ?>
</span>
</div>
</td>
<td>
<span class="badge <?php echo h(badgeClassForSub($subStatus)); ?>">
<?php echo h($subStatus); ?>
</span>
<?php if ($hasSubscription): ?>
<div class="text-muted small mt-2">
Qty: <?php echo (int)($row['quantity'] ?? 1); ?>
</div>
<?php endif; ?>
</td>
<td>
<?php if ($hasSubscription && !empty($row['plan_id'])): ?>
<div class="fw-bold"><?php echo h($row['plan_name']); ?></div>
<div class="text-muted small">
<?php echo h($row['plan_code']); ?> · <?php echo h(moneyFmt($row['unit_amount'], $row['currency'])); ?>
</div>
<div class="text-muted small">
Every <?php echo (int)$row['interval_count']; ?> <?php echo h($row['interval']); ?>
</div>
<?php else: ?>
<?php endif; ?>
</td>
<td>
<?php if ($hasSubscription): ?>
<div class="text-muted small">
Start: <?php echo h(dateFmt($row['current_period_start'])); ?>
</div>
<div class="text-muted small">
End: <?php echo h(dateFmt($row['current_period_end'])); ?>
</div>
<?php else: ?>
<?php endif; ?>
</td>
<td>
<?php if ($hasSubscription): ?>
<div class="text-muted small">
Start: <?php echo h(dateFmt($row['trial_start'])); ?>
</div>
<div class="text-muted small">
End: <?php echo h(dateFmt($row['trial_end'])); ?>
</div>
<?php else: ?>
<?php endif; ?>
</td>
<td>
<?php if ($hasSubscription): ?>
<?php if ((int)$row['cancel_at_period_end'] === 1): ?>
<span class="badge bg-warning">Yes</span>
<?php else: ?>
<span class="badge bg-secondary">No</span>
<?php endif; ?>
<?php else: ?>
<?php endif; ?>
</td>
<td>
<?php if ($hasSubscription): ?>
<div class="text-muted small">
cust: <?php echo h($row['stripe_customer_id'] ?: '—'); ?>
</div>
<div class="text-muted small">
sub: <?php echo h($row['stripe_subscription_id'] ?: '—'); ?>
</div>
<?php else: ?>
<?php endif; ?>
</td>
<td>
<?php echo h(dateFmt($row['subscription_updated_at'])); ?>
</td>
<td>
<?php if ($hasSubscription): ?>
<form method="POST" class="d-inline">
<input type="hidden" name="action" value="flag_cancel_at_period_end">
<input type="hidden" name="subscription_row_id" value="<?php echo (int)$row['subscription_row_id']; ?>">
<input type="hidden" name="school_id" value="<?php echo (int)$row['school_id']; ?>">
<input type="hidden" name="flag" value="<?php echo ((int)$row['cancel_at_period_end'] === 1) ? 0 : 1; ?>">
<?php if ((int)$row['cancel_at_period_end'] === 1): ?>
<button type="submit" class="btn btn-sm btn-outline-success">
Unmark
</button>
<?php else: ?>
<button type="submit" class="btn btn-sm btn-outline-warning">
Mark
</button>
<?php endif; ?>
</form>
<?php else: ?>
<span class="text-muted">—</span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($rows)): ?>
<tr>
<td colspan="11" class="text-center text-muted py-4">No results</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div> <!-- /page-content -->
</div> <!-- /page-wrapper -->
<?php if (file_exists('include/footer.php')) include('include/footer.php'); ?>
</div> <!-- /wrapper -->
<?php if (file_exists('jsinclude.php')) include('jsinclude.php'); ?>
</body>
</html>