Files
kaffeekasse-saas/app/platform-admin.php
T
clemensandClaude Sonnet 5 ade1fd0ed9 Billing Phase 1: Tarifstatus-Datenmodell und Tariflogik je Mandant
Vorbereitung fuer Stripe Billing (Zahlungseinzug) + bestehendes Dolibarr
des Kunden (Rechnungsstellung/Buchhaltung) statt eines eigenen Rechnungs-
systems oder eines zusaetzlichen ERP nur fuer Kaffeeliste - Begruendung
in docs/billing.md.

- Neue Tabelle tenant_billing (plan_code, subscription_status, sowie
  bereits vorbereitete, noch ungenutzte Felder fuer Stripe/Dolibarr-IDs).
- app/billing.php: billing_plans() als einzige Quelle der Tarifstufen
  (synchron mit preise.php), billing_required_plan_code() anhand aktiver
  Teilnehmerzahl, billing_check_tenant() vergleicht gebuchten mit
  benoetigtem Tarif - rein informativ, kein Enforcement, solange Stripe
  nicht angebunden ist.
- Neue Mandanten starten automatisch auf plan_code='free' bei der
  Registrierung.
- mandant-einstellungen.php zeigt Owner/Admin ihren aktuellen Tarif und
  Teilnehmerstand, mit Hinweis bei Bedarf einer hoeheren Stufe.
- Back-Office zeigt zusaetzlich zur Mandantenliste den gebuchten und
  (falls abweichend) den tatsaechlich benoetigten Tarif.

Live getestet: Tarifgrenzen (10/25/50/150) mit einem Mandanten unter und
einem ueber dem Freikontingent geprueft, UI in Mandant-Einstellungen und
Back-Office verifiziert. Alle M8-Isolations-/Rollenmatrix-Tests weiterhin
gruen.

Phase 2 (Stripe) und Phase 3 (Dolibarr-Sync) folgen, sobald Test-Keys
beziehungsweise Sandbox-Zugang vorliegen.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-16 23:38:28 +02:00

132 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/bootstrap.php';
require_once __DIR__ . '/saas-auth.php';
require_once __DIR__ . '/audit.php';
/**
* Platform-admin access is deliberately a separate concept from
* tenant_memberships/saas_user_has_role(): it must never be wired into the
* regular tenant-scoped pages, or the tenant isolation those pages and
* scripts/check-m8-tenant-isolation.php rely on would silently break. It
* only gates the backoffice-*.php pages.
*/
function app_is_platform_admin(PDO $pdo, ?int $userId): bool
{
if ($userId === null) {
return false;
}
$stmt = $pdo->prepare('SELECT 1 FROM platform_admins WHERE user_id = ?');
$stmt->execute([$userId]);
return $stmt->fetchColumn() !== false;
}
/**
* Requires a real SaaS login (any tenant role, or none) plus a
* platform_admins entry. Exits with 403 otherwise.
*
* @return array{user_id: int, email: string, display_name: string}
*/
function app_require_platform_admin(PDO $pdo): array
{
$user = saas_current_user($pdo);
if ($user === null) {
header('Location: login.php');
exit;
}
if (!app_is_platform_admin($pdo, (int)$user['user_id'])) {
http_response_code(403);
exit('Kein Zugriff.');
}
return $user;
}
/**
* @return list<array{id: int, slug: string, name: string, status: string, created_at: string, participant_count: int, active_participant_count: int, balance_cents: int}>
*/
function app_backoffice_fetch_tenants(PDO $pdo): array
{
$stmt = $pdo->query(
"SELECT
t.id, t.slug, t.name, t.status, t.created_at,
COALESCE(tb.plan_code, 'free') AS plan_code,
COUNT(p.id) AS participant_count,
COALESCE(SUM(p.active), 0) AS active_participant_count,
COALESCE((
SELECT SUM(le.amount_cents)
FROM ledger_entries le
WHERE le.tenant_id = t.id AND le.voided_at IS NULL
), 0) AS balance_cents
FROM tenants t
LEFT JOIN participants p ON p.tenant_id = t.id
LEFT JOIN tenant_billing tb ON tb.tenant_id = t.id
GROUP BY t.id, t.slug, t.name, t.status, t.created_at, tb.plan_code
ORDER BY t.created_at DESC"
);
return $stmt->fetchAll();
}
/**
* @return array{tenant: array, settings: ?array, members: list<array>, recent_entries: list<array>, recent_audit: list<array>}|null
*/
function app_backoffice_fetch_tenant_detail(PDO $pdo, int $tenantId): ?array
{
$stmt = $pdo->prepare('SELECT id, slug, name, status, timezone, locale, currency_code, created_at FROM tenants WHERE id = ?');
$stmt->execute([$tenantId]);
$tenant = $stmt->fetch();
if ($tenant === false) {
return null;
}
$stmt = $pdo->prepare('SELECT * FROM tenant_settings WHERE tenant_id = ?');
$stmt->execute([$tenantId]);
$settings = $stmt->fetch() ?: null;
$stmt = $pdo->prepare(
'SELECT u.id, u.email, u.display_name, u.status, tm.role, tm.status AS membership_status
FROM tenant_memberships tm
JOIN users u ON u.id = tm.user_id
WHERE tm.tenant_id = ?
ORDER BY tm.role, u.display_name'
);
$stmt->execute([$tenantId]);
$members = $stmt->fetchAll();
$stmt = $pdo->prepare(
'SELECT le.id, le.type, le.amount_cents, le.booked_at, le.source, p.display_name
FROM ledger_entries le
JOIN participants p ON p.id = le.participant_id
WHERE le.tenant_id = ? AND le.voided_at IS NULL
ORDER BY le.booked_at DESC, le.id DESC
LIMIT 20'
);
$stmt->execute([$tenantId]);
$recentEntries = $stmt->fetchAll();
$stmt = $pdo->prepare(
'SELECT a.id, a.action, a.subject_type, a.subject_id, a.created_at, u.display_name AS actor_name
FROM audit_log a
LEFT JOIN users u ON u.id = a.actor_user_id
WHERE a.tenant_id = ?
ORDER BY a.created_at DESC, a.id DESC
LIMIT 20'
);
$stmt->execute([$tenantId]);
$recentAudit = $stmt->fetchAll();
return [
'tenant' => $tenant,
'settings' => $settings,
'members' => $members,
'recent_entries' => $recentEntries,
'recent_audit' => $recentAudit,
];
}