Files

133 lines
6.9 KiB
PHP

<?php
require_once __DIR__ . '/functions.php';
require_once __DIR__ . '/app/platform-admin.php';
require_once __DIR__ . '/app/billing.php';
require_once __DIR__ . '/app/public-page-views.php';
$pdo = app_db_pdo();
$user = app_require_platform_admin($pdo);
$tenants = app_backoffice_fetch_tenants($pdo);
$billingPlans = billing_plans();
$pageViewSummary = null;
$pageViewError = false;
try {
$pageViewSummary = app_public_page_view_summary($pdo);
} catch (Throwable $e) {
$pageViewError = true;
error_log('Back-Office-Seitenaufrufe konnten nicht geladen werden: ' . $e->getMessage());
}
$legalRequests = $pdo->query(
"SELECT lr.id, lr.reference_code, lr.request_type, lr.requester_name,
lr.requester_email, lr.contract_reference, lr.request_reason,
lr.requested_end, lr.status, lr.received_at, t.name AS tenant_name
FROM legal_requests lr
LEFT JOIN tenants t ON t.id = lr.tenant_id
WHERE lr.status <> 'processed'
ORDER BY lr.received_at ASC
LIMIT 100"
)->fetchAll();
include 'header.php';
include 'headerline.php';
include 'nav.php';
?>
<section id="banner">
<div class="content">
<h2>Back-Office</h2>
<p>Nur für Betreiber-Admins sichtbar: Übersicht aller Mandanten, unabhängig von deren eigener Mitgliedschaft.
Jeder Zugriff wird im Audit-Log des jeweiligen Mandanten protokolliert.</p>
<h3>Öffentliche Seitenaufrufe</h3>
<p><small>Anonyme Seitenaufrufe ohne Cookies, IP-Adressen oder Besucherkennungen. Wiederholte Aufrufe und automatisierte Abrufe können enthalten sein; die Zahlen sind keine eindeutigen Besucher.</small></p>
<?php if ($pageViewError || $pageViewSummary === null): ?>
<div class="hint-box warning"><p>Die Seitenaufrufe sind noch nicht verfügbar. Bitte Migration 0032 ausführen.</p></div>
<?php else: ?>
<table>
<tr><th>Heute</th><th>Letzte 30 Tage</th><th>Seit Beginn der Zählung</th></tr>
<tr>
<td><?php echo number_format($pageViewSummary['totals']['today'], 0, ',', '.'); ?></td>
<td><?php echo number_format($pageViewSummary['totals']['last_30_days'], 0, ',', '.'); ?></td>
<td><?php echo number_format($pageViewSummary['totals']['all_time'], 0, ',', '.'); ?></td>
</tr>
</table>
<?php if ($pageViewSummary['pages'] === []): ?>
<div class="hint-box info"><p>Seit dem Start des Zählers wurden noch keine öffentlichen Seiten aufgerufen.</p></div>
<?php else: ?>
<table>
<tr><th>Seite</th><th>Heute</th><th>Letzte 30 Tage</th><th>Gesamt</th></tr>
<?php foreach ($pageViewSummary['pages'] as $pageView): ?>
<tr>
<td><?php echo saas_html($pageView['label']); ?></td>
<td><?php echo number_format($pageView['today'], 0, ',', '.'); ?></td>
<td><?php echo number_format($pageView['last_30_days'], 0, ',', '.'); ?></td>
<td><?php echo number_format($pageView['all_time'], 0, ',', '.'); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php endif; ?>
<h3>Offene Kündigungen und Widerrufe</h3>
<?php if ($legalRequests === []): ?>
<div class="hint-box success"><p>Keine offenen Rechtserklärungen.</p></div>
<?php else: ?>
<table>
<tr><th>Eingang</th><th>Art / Referenz</th><th>Kunde</th><th>Erklärung</th><th>Status</th><th></th></tr>
<?php foreach ($legalRequests as $request): ?>
<tr>
<td><?php echo saas_html($request['received_at']); ?></td>
<td><?php echo $request['request_type'] === 'withdrawal' ? 'Widerruf' : 'Kündigung'; ?><br><small><?php echo saas_html($request['reference_code']); ?></small></td>
<td><?php echo saas_html($request['requester_name']); ?><br><a href="mailto:<?php echo saas_html($request['requester_email']); ?>"><?php echo saas_html($request['requester_email']); ?></a><br><small><?php echo saas_html($request['tenant_name'] ?? $request['contract_reference']); ?></small></td>
<td><?php echo $request['requested_end'] !== null ? 'Ende: ' . saas_html($request['requested_end']) . '<br>' : ''; ?><?php echo nl2br(saas_html($request['request_reason'] ?? '')); ?></td>
<td><?php echo saas_html($request['status']); ?></td>
<td>
<form method="post" action="rechtserklaerung-bearbeiten.php">
<?php echo app_csrf_field(); ?>
<input type="hidden" name="request_id" value="<?php echo (int)$request['id']; ?>">
<button type="submit" class="button small">Als erledigt markieren</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<table>
<tr>
<th>Kunde</th>
<th>Kürzel</th>
<th>Status</th>
<th>Tarif</th>
<th>Erstellt</th>
<th>Teilnehmer (aktiv/gesamt)</th>
<th>Saldensumme</th>
<th></th>
</tr>
<?php foreach ($tenants as $tenant): ?>
<?php $tenantRequiredPlan = billing_required_plan_code((int)$tenant['active_participant_count']); ?>
<tr>
<td><?php echo saas_html($tenant['name']); ?></td>
<td><?php echo saas_html($tenant['slug']); ?></td>
<td><?php echo saas_html($tenant['status']); ?></td>
<td>
<?php echo saas_html($billingPlans[$tenant['plan_code']]['label'] ?? $tenant['plan_code']); ?>
<?php if ($tenantRequiredPlan !== $tenant['plan_code']): ?>
<br><small>benötigt: <?php echo saas_html($billingPlans[$tenantRequiredPlan]['label'] ?? $tenantRequiredPlan); ?></small>
<?php endif; ?>
</td>
<td><?php echo saas_html($tenant['created_at']); ?></td>
<td><?php echo (int)$tenant['active_participant_count']; ?> / <?php echo (int)$tenant['participant_count']; ?></td>
<td><?php echo saas_html(saas_format_money_cents((int)$tenant['balance_cents'])); ?> €</td>
<td><a href="backoffice-mandant.php?tenant_id=<?php echo (int)$tenant['id']; ?>" class="button">Ansehen</a></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</section>
<?php include 'footer.php'; ?>