Robo Admin
This commit is contained in:
parent
a73358d2f1
commit
1c712d0781
20 changed files with 1194 additions and 55 deletions
114
tools/export_accounts.php
Normal file
114
tools/export_accounts.php
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
// Exportiert die Kontodaten (user_id, iban, blz, bankname, kontonr) sowie die
|
||||
// Mobilnummer (mobile) aller Spieler, die in der aktuellen oder der letzten
|
||||
// Saison aktiv waren (Zahlung geleistet), aus der Quell-Datenbank (Legacy) in
|
||||
// eine lokale CSV-Datei.
|
||||
//
|
||||
// Hintergrund: Diese Daten sollen lokal gesichert werden, bevor für einen
|
||||
// Übergangszeitraum die Kontoinformationen (iban, blz, bankname) der User
|
||||
// entfernt werden. Die Quell-DB enthält die Werte noch im Klartext.
|
||||
//
|
||||
// Aufruf (im App-Container):
|
||||
// docker compose exec app php export_accounts.php [ausgabepfad.csv]
|
||||
//
|
||||
// Standard-Ausgabepfad: tools/export_kontodaten_<zeitstempel>.csv
|
||||
|
||||
$host = getenv('DB_HOST') ?: '127.0.0.1';
|
||||
$user = getenv('DB_USER') ?: 'root';
|
||||
$pass = getenv('DB_PASS') ?: '';
|
||||
$srcDbName = getenv('DB_OLD_NAME') ?: 'tippspiel_legacy';
|
||||
|
||||
$out = $argv[1] ?? (__DIR__ . '/export_kontodaten_' . date('Ymd_His') . '.csv');
|
||||
|
||||
try {
|
||||
$pdo = new PDO("mysql:host=$host;charset=utf8mb4", $user, $pass);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// Aktuelle Saison ermitteln
|
||||
$currentId = (int)$pdo->query(
|
||||
"SELECT current_season_id FROM `$srcDbName`.`settings` ORDER BY id LIMIT 1"
|
||||
)->fetchColumn();
|
||||
|
||||
if ($currentId <= 0) {
|
||||
die("Fehler: Keine aktive Saison in der Quell-DB '$srcDbName' gefunden.\n");
|
||||
}
|
||||
|
||||
// Letzte Saison = höchste ID unterhalb der aktuellen
|
||||
$stmt = $pdo->prepare("SELECT MAX(id) FROM `$srcDbName`.`seasons` WHERE id < ?");
|
||||
$stmt->execute([$currentId]);
|
||||
$lastId = (int)$stmt->fetchColumn();
|
||||
|
||||
$seasonIds = array_values(array_unique(array_filter([$currentId, $lastId])));
|
||||
$placeholders = implode(',', array_fill(0, count($seasonIds), '?'));
|
||||
|
||||
// Alle User, die in einer der beiden Saisons bezahlt haben (paid_date gesetzt)
|
||||
$sql = "SELECT u.id AS user_id, u.name, u.email, u.iban, u.bankname, u.blz, u.kontonr, u.mobile,
|
||||
p.season_id
|
||||
FROM `$srcDbName`.`users` u
|
||||
JOIN `$srcDbName`.`payments` p ON p.user_id = u.id AND p.paid_date IS NOT NULL
|
||||
WHERE p.season_id IN ($placeholders)
|
||||
ORDER BY u.id, p.season_id";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($seasonIds);
|
||||
|
||||
$users = [];
|
||||
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
$id = (int)$row['user_id'];
|
||||
if (!isset($users[$id])) {
|
||||
$users[$id] = [
|
||||
'user_id' => $id,
|
||||
'name' => (string)$row['name'],
|
||||
'email' => (string)$row['email'],
|
||||
'iban' => (string)$row['iban'],
|
||||
'bankname' => $row['bankname'] !== null ? (string)$row['bankname'] : '',
|
||||
'blz' => $row['blz'] !== null ? (string)$row['blz'] : '',
|
||||
'kontonr' => $row['kontonr'] !== null ? (string)$row['kontonr'] : '',
|
||||
'mobile' => $row['mobile'] !== null ? (string)$row['mobile'] : '',
|
||||
'seasons' => [],
|
||||
];
|
||||
}
|
||||
$users[$id]['seasons'][] = (int)$row['season_id'];
|
||||
}
|
||||
|
||||
$fh = fopen($out, 'w');
|
||||
if ($fh === false) {
|
||||
die("Fehler: Ausgabedatei '$out' kann nicht geöffnet werden.\n");
|
||||
}
|
||||
|
||||
// UTF-8 BOM, damit Umlaute in Excel korrekt angezeigt werden
|
||||
fwrite($fh, "\xEF\xBB\xBF");
|
||||
|
||||
$header = ['user_id', 'name', 'email', 'iban', 'bankname', 'blz', 'kontonr', 'mobile', 'aktiv_aktuell', 'aktiv_letzte'];
|
||||
fputcsv($fh, $header, ';', '"', '\\');
|
||||
|
||||
$count = 0;
|
||||
foreach ($users as $u) {
|
||||
$seasons = array_unique($u['seasons']);
|
||||
fputcsv($fh, [
|
||||
$u['user_id'],
|
||||
$u['name'],
|
||||
$u['email'],
|
||||
$u['iban'],
|
||||
$u['bankname'],
|
||||
$u['blz'],
|
||||
$u['kontonr'],
|
||||
$u['mobile'],
|
||||
in_array($currentId, $seasons) ? 1 : 0,
|
||||
in_array($lastId, $seasons) ? 1 : 0,
|
||||
], ';', '"', '\\');
|
||||
$count++;
|
||||
}
|
||||
fclose($fh);
|
||||
|
||||
echo "Export abgeschlossen.\n";
|
||||
echo "Quell-DB: $srcDbName\n";
|
||||
echo "Aktuelle Saison: $currentId, letzte Saison: " . ($lastId > 0 ? (string)$lastId : '-') . "\n";
|
||||
echo "$count Spieler exportiert.\n";
|
||||
echo "Ausgabedatei: $out\n";
|
||||
|
||||
} catch (PDOException $e) {
|
||||
die("Datenbankfehler: " . $e->getMessage() . "\n");
|
||||
} catch (\Throwable $e) {
|
||||
die("Fehler: " . $e->getMessage() . "\n");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue