Add Credit Accounting section to game management

- New table: platform_credits (id, platform_id, credits_purchased, credit_date, payment_method, notes)
- API: credits_list, credits_create, credits_update, credits_delete actions (admin-only)
- Admin form: Credit Accounting box showing Available Credits total; Manage Credits button opens modal
- Modal: Total Credits header, add/edit/delete entries with credits, date, payment method, notes
- Game list cards: show live credit total per game (cyan, loads async)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-04 18:00:15 +00:00
parent 99079340cb
commit f50d1d481d
2 changed files with 269 additions and 1 deletions
+66
View File
@@ -104,6 +104,72 @@ switch ($action) {
echo json_encode(['success'=>true]);
break;
// ── Admin: list credits for a platform ───────────────
case 'credits_list':
if (!$isAdmin) { echo json_encode(['success'=>false,'error'=>'Forbidden']); exit; }
$pid = (int)($_GET['platform_id'] ?? 0);
if (!$pid) { echo json_encode(['success'=>false,'error'=>'platform_id required']); exit; }
$rows = db()->prepare("SELECT * FROM platform_credits WHERE platform_id=? ORDER BY credit_date DESC, id DESC");
$rows->execute([$pid]);
$credits = $rows->fetchAll();
$total = db()->prepare("SELECT COALESCE(SUM(credits_purchased),0) FROM platform_credits WHERE platform_id=?");
$total->execute([$pid]);
echo json_encode(['success'=>true,'credits'=>$credits,'total'=>(float)$total->fetchColumn()]);
break;
// ── Admin: add credit entry ───────────────────────────
case 'credits_create':
if (!$isAdmin || $_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(['success'=>false,'error'=>'Forbidden']); exit; }
$d = json_decode(file_get_contents('php://input'), true);
$pid = (int)($d['platform_id'] ?? 0);
$credits = (float)($d['credits_purchased'] ?? 0);
$date = $d['credit_date'] ?? date('Y-m-d');
$method = substr(trim($d['payment_method'] ?? ''), 0, 100);
$notes = trim($d['notes'] ?? '');
if (!$pid || $credits <= 0 || !$date) { echo json_encode(['success'=>false,'error'=>'platform_id, credits_purchased, and credit_date are required']); exit; }
$stmt = db()->prepare("INSERT INTO platform_credits (platform_id,credits_purchased,credit_date,payment_method,notes) VALUES (?,?,?,?,?)");
$stmt->execute([$pid,$credits,$date,$method,$notes]);
$newId = db()->lastInsertId();
$total = db()->prepare("SELECT COALESCE(SUM(credits_purchased),0) FROM platform_credits WHERE platform_id=?");
$total->execute([$pid]);
echo json_encode(['success'=>true,'id'=>$newId,'total'=>(float)$total->fetchColumn()]);
break;
// ── Admin: update credit entry ────────────────────────
case 'credits_update':
if (!$isAdmin || $_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(['success'=>false,'error'=>'Forbidden']); exit; }
$d = json_decode(file_get_contents('php://input'), true);
$id = (int)($d['id'] ?? 0);
$credits = (float)($d['credits_purchased'] ?? 0);
$date = $d['credit_date'] ?? date('Y-m-d');
$method = substr(trim($d['payment_method'] ?? ''), 0, 100);
$notes = trim($d['notes'] ?? '');
if (!$id || $credits <= 0 || !$date) { echo json_encode(['success'=>false,'error'=>'id, credits_purchased, and credit_date are required']); exit; }
db()->prepare("UPDATE platform_credits SET credits_purchased=?,credit_date=?,payment_method=?,notes=? WHERE id=?")
->execute([$credits,$date,$method,$notes,$id]);
$row = db()->prepare("SELECT platform_id FROM platform_credits WHERE id=?");
$row->execute([$id]);
$pid = (int)($row->fetchColumn() ?: 0);
$total = db()->prepare("SELECT COALESCE(SUM(credits_purchased),0) FROM platform_credits WHERE platform_id=?");
$total->execute([$pid]);
echo json_encode(['success'=>true,'total'=>(float)$total->fetchColumn()]);
break;
// ── Admin: delete credit entry ────────────────────────
case 'credits_delete':
if (!$isAdmin || $_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(['success'=>false,'error'=>'Forbidden']); exit; }
$d = json_decode(file_get_contents('php://input'), true);
$id = (int)($d['id'] ?? 0);
if (!$id) { echo json_encode(['success'=>false,'error'=>'ID required']); exit; }
$row = db()->prepare("SELECT platform_id FROM platform_credits WHERE id=?");
$row->execute([$id]);
$pid = (int)($row->fetchColumn() ?: 0);
db()->prepare("DELETE FROM platform_credits WHERE id=?")->execute([$id]);
$total = db()->prepare("SELECT COALESCE(SUM(credits_purchased),0) FROM platform_credits WHERE platform_id=?");
$total->execute([$pid]);
echo json_encode(['success'=>true,'total'=>(float)$total->fetchColumn()]);
break;
default:
echo json_encode(['success'=>false,'error'=>'Unknown action']);
}