/home3/bmscom/jupiter.bms69.com/admin_finance.php
<?php
// admin_finance.php
require_once 'includes/db.php';
if (!isAdmin()) redirect('admin_login.php');
$message = '';
// Handle New Log
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_log'])) {
$ground_id = (int)$_POST['ground_id'];
$type = $_POST['type'];
$amount = (float)$_POST['amount'];
$description = htmlspecialchars($_POST['description']);
$date = $_POST['entry_date'];
$stmt = $pdo->prepare("INSERT INTO financial_logs (ground_id, type, amount, description, entry_date) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$ground_id, $type, $amount, $description, $date]);
$message = ['type' => 'success', 'text' => 'Financial entry recorded.'];
}
// Fetch Stats per Ground
$grounds_stmt = $pdo->query("SELECT * FROM grounds");
$grounds = $grounds_stmt->fetchAll();
$stats = [];
foreach($grounds as $g) {
$sh = $pdo->prepare("SELECT SUM(CASE WHEN type='income' THEN amount ELSE 0 END) as total_income, SUM(CASE WHEN type='expense' THEN amount ELSE 0 END) as total_expense FROM financial_logs WHERE ground_id = ?");
$sh->execute([$g['id']]);
$stats[$g['id']] = $sh->fetch();
}
// Fetch recent logs
$logs_stmt = $pdo->query("SELECT f.*, g.name as ground_name FROM financial_logs f JOIN grounds g ON f.ground_id = g.id ORDER BY f.entry_date DESC, f.id DESC LIMIT 50");
$logs = $logs_stmt->fetchAll();
require_once 'includes/header.php';
?>
<div class="space-y-10">
<div class="flex flex-col md:flex-row md:items-center justify-between gap-6">
<div>
<h1 class="text-3xl font-black text-white">Financial Ledger</h1>
<p class="text-slate-400 mt-1">Revenue and expense transparency.</p>
</div>
<button onclick="document.getElementById('logModal').classList.remove('hidden')" class="px-8 py-4 bg-blue-600 hover:bg-blue-700 text-white font-black rounded-2xl shadow-xl">
Manual Entry
</button>
</div>
<!-- Stats Matrix -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-8">
<?php foreach($grounds as $g):
$rev = $stats[$g['id']]['total_income'] ?? 0;
$exp = $stats[$g['id']]['total_expense'] ?? 0;
$profit = $rev - $exp;
?>
<div class="glass p-10 rounded-[3rem] shadow-2xl relative overflow-hidden flex flex-col justify-between">
<div class="absolute -right-20 -bottom-20 w-80 h-80 bg-blue-600/5 rounded-full blur-3xl"></div>
<h2 class="text-2xl font-black text-white mb-8 border-b border-white/5 pb-6"><?php echo $g['name']; ?></h2>
<div class="space-y-6">
<div class="flex justify-between items-end">
<span class="text-xs font-black text-slate-500 uppercase tracking-widest mb-1">Total Revenue</span>
<span class="text-3xl font-black text-green-500"><?php echo number_format($rev); ?> <small class="text-xs opacity-60">PKR</small></span>
</div>
<div class="flex justify-between items-end">
<span class="text-xs font-black text-slate-500 uppercase tracking-widest mb-1">Total Expenses</span>
<span class="text-3xl font-black text-red-500"><?php echo number_format($exp); ?> <small class="text-xs opacity-60">PKR</small></span>
</div>
<div class="pt-8 border-t border-white/5 flex justify-between items-center">
<span class="text-base font-black text-white uppercase tracking-widest">Net Profit</span>
<span class="text-4xl font-black text-blue-500 shadow-blue-500/20 drop-shadow-2xl"><?php echo number_format($profit); ?> <small class="text-xs opacity-60">PKR</small></span>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<!-- Ledger Table -->
<div class="glass p-8 rounded-[2.5rem] shadow-2xl overflow-hidden">
<h3 class="text-xl font-black text-white mb-8">Recent Transactions</h3>
<div class="overflow-x-auto">
<table class="w-full text-left">
<thead>
<tr class="text-xs font-black text-slate-500 uppercase tracking-widest border-b border-white/5 pb-4">
<th class="pb-6 px-4">Date</th>
<th class="pb-6 px-4">Entity</th>
<th class="pb-6 px-4">Description</th>
<th class="pb-6 px-4">Nature</th>
<th class="pb-6 px-4 text-right">Amount</th>
</tr>
</thead>
<tbody class="divide-y divide-white/5">
<?php if(empty($logs)): ?>
<tr><td colspan="5" class="py-10 text-center text-slate-500 italic">No transactions found.</td></tr>
<?php else: ?>
<?php foreach($logs as $l): ?>
<tr class="group hover:bg-white/[0.02] transition-colors">
<td class="py-5 px-4 text-sm text-slate-300"><?php echo date('M d, Y', strtotime($l['entry_date'])); ?></td>
<td class="py-5 px-4 font-bold text-white"><?php echo $l['ground_name']; ?></td>
<td class="py-5 px-4 text-sm text-slate-400"><?php echo $l['description']; ?></td>
<td class="py-5 px-4">
<span class="text-[10px] font-black px-3 py-1 rounded-lg uppercase tracking-widest <?php echo $l['type'] == 'income' ? 'bg-green-500/10 text-green-500 border border-green-500/20' : 'bg-red-500/10 text-red-500 border border-red-500/20'; ?>">
<?php echo $l['type']; ?>
</span>
</td>
<td class="py-5 px-4 text-right font-black <?php echo $l['type'] == 'income' ? 'text-green-500' : 'text-red-500'; ?>"><?php echo number_format($l['amount']); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Modal -->
<div id="logModal" class="fixed inset-0 z-[100] hidden flex items-center justify-center p-4 bg-slate-950/90 backdrop-blur-xl animate-in fade-in">
<div class="glass w-full max-w-md rounded-[2.5rem] shadow-2xl overflow-hidden scale-in-center">
<div class="p-8 border-b border-white/5 flex justify-between items-center">
<h3 class="text-2xl font-black text-white">Financial Entry</h3>
<button onclick="document.getElementById('logModal').classList.add('hidden')" class="text-slate-400 hover:text-white text-3xl font-light">×</button>
</div>
<form method="POST" class="p-8 space-y-6">
<input type="hidden" name="add_log" value="1">
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-xs font-black text-slate-500 uppercase tracking-widest mb-2 ml-1">Ground</label>
<select name="ground_id" class="w-full bg-slate-900 border border-slate-700 rounded-2xl px-5 py-4 text-white focus:ring-4 focus:ring-blue-500/20 outline-none">
<?php foreach($grounds as $g): ?>
<option value="<?php echo $g['id']; ?>"><?php echo $g['name']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
<label class="block text-xs font-black text-slate-500 uppercase tracking-widest mb-2 ml-1">Type</label>
<select name="type" class="w-full bg-slate-900 border border-slate-700 rounded-2xl px-5 py-4 text-white focus:ring-4 focus:ring-blue-500/20 outline-none">
<option value="income">Income</option>
<option value="expense">Expense</option>
</select>
</div>
</div>
<div>
<label class="block text-xs font-black text-slate-500 uppercase tracking-widest mb-2 ml-1">Description</label>
<input type="text" name="description" required placeholder="Maintenance, Equipment, etc." class="w-full bg-slate-900 border border-slate-700 rounded-2xl px-5 py-4 text-white focus:ring-4 focus:ring-blue-500/20 outline-none">
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-xs font-black text-slate-500 uppercase tracking-widest mb-2 ml-1">Amount (PKR)</label>
<input type="number" name="amount" required step="0.01" class="w-full bg-slate-900 border border-slate-700 rounded-2xl px-5 py-4 text-white focus:ring-4 focus:ring-blue-500/20 outline-none">
</div>
<div>
<label class="block text-xs font-black text-slate-500 uppercase tracking-widest mb-2 ml-1">Date</label>
<input type="date" name="entry_date" value="<?php echo date('Y-m-d'); ?>" required class="w-full bg-slate-900 border border-slate-700 rounded-2xl px-5 py-4 text-white focus:ring-4 focus:ring-blue-500/20 outline-none">
</div>
</div>
<button type="submit" class="w-full py-5 bg-blue-600 hover:bg-blue-700 text-white font-black text-lg rounded-2xl transition-all shadow-xl shadow-blue-500/20 active:scale-[0.98]">
Record Entry
</button>
</form>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>