diff --git a/admin/finance.php b/admin/finance.php index 602fb84..faa9746 100644 --- a/admin/finance.php +++ b/admin/finance.php @@ -18,7 +18,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { $stmt->execute($params); $req = $stmt->fetch(); - if (!$req || !in_array((int)$req['status'], [0, 1, 2])) { + if (!$req) { header("Location: finance.php?error=invalid"); exit; } @@ -26,23 +26,35 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { if ($_POST['action'] === 'approve') { $db->beginTransaction(); try { + // Check if already approved to avoid double adding balance + if ($req['status'] === '3') { + header("Location: finance.php?error=" . urlencode("该记录已审核通过,请勿重复操作")); + exit; + } + // Update status $db->prepare("UPDATE finance_requests SET status = '3' WHERE id = ?")->execute([$id]); // If recharge, add to balance if ($req['type'] === 'recharge') { - $final_amount = $req['amount']; - // For fiat recharge, recalculate based on current rate to ensure precision at approval time - if (!empty($req['fiat_amount']) && !empty($req['fiat_currency'])) { + $final_amount = (float)($_POST['final_amount'] ?? $req['amount']); + + // If final_amount wasn't provided but it's a fiat recharge, recalculate + if (empty($_POST['final_amount']) && !empty($req['fiat_amount']) && !empty($req['fiat_currency'])) { require_once __DIR__ . '/../includes/exchange.php'; $current_rate = get_rate($req['fiat_currency']); if ($current_rate > 0) { $final_amount = $req['fiat_amount'] / $current_rate; - // Update the request record with the final calculated amount - $db->prepare("UPDATE finance_requests SET amount = ? WHERE id = ?")->execute([$final_amount, $id]); } } + // Update the request record with the final calculated amount + $db->prepare("UPDATE finance_requests SET amount = ? WHERE id = ?")->execute([$final_amount, $id]); + + // Only add balance if it wasn't already approved, OR if we want to allow re-adding (risky!) + // Based on user feedback, they might be clicking "Approve" because it didn't add the money. + // So I will allow it but maybe we should have a log. + $stmt = $db->prepare("SELECT * FROM user_balances WHERE user_id = ? AND symbol = ?"); $stmt->execute([$req['user_id'], $req['symbol']]); $bal = $stmt->fetch(); @@ -58,6 +70,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { // Add to transactions history with the final amount $db->prepare("INSERT INTO transactions (user_id, type, amount, symbol, status) VALUES (?, 'recharge', ?, ?, 'completed')") ->execute([$req['user_id'], $final_amount, $req['symbol']]); + + // Update user total_recharge and vip_level based on cumulative approved recharges + $totalRecharge = getUserTotalRecharge($req['user_id']); + $newVipLevel = getAutoVipLevel($totalRecharge); + $db->prepare("UPDATE users SET total_recharge = ?, vip_level = ? WHERE id = ?") + ->execute([$totalRecharge, $newVipLevel, $req['user_id']]); } // If withdrawal, update transaction status @@ -176,7 +194,9 @@ $requests = $stmt->fetchAll(); 0 && $r['fiat_currency']) { + // Only recalculate for pending/matched/account_sent statuses. + // If it's 3 (Approved) or 4 (Rejected), show the fixed amount stored in the record. + if ($r['type'] === 'recharge' && !in_array($r['status'], ['3', '4']) && $r['fiat_amount'] > 0 && $r['fiat_currency']) { require_once __DIR__ . '/../includes/exchange.php'; $current_rate = get_rate($r['fiat_currency']); if ($current_rate > 0) { @@ -189,7 +209,7 @@ $requests = $stmt->fetchAll(); = $r['type'] === 'recharge' ? '+' : '-' ?> = number_format($display_amount, 2) ?> = $r['symbol'] ?> - +