36557-vm/ledger.php
Flatlogic Bot 08fcb2dae0 0.2
2025-12-01 21:25:15 +00:00

157 lines
7.8 KiB
PHP

<?php
session_start();
require_once 'auth.php';
require_once 'db/config.php';
if (!is_logged_in()) {
header("Location: login.php");
exit;
}
$db = db();
// 1. Fetch user data
$stmt = $db->prepare("SELECT * FROM users WHERE id = :id");
$stmt->execute([':id' => $_SESSION['user_id']]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// 2. Pagination Logic
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$records_per_page = 25;
$offset = ($page - 1) * $records_per_page;
// Get total number of transactions for the user
$total_stmt = $db->prepare("SELECT COUNT(*) FROM transactions WHERE user_id = :user_id");
$total_stmt->execute([':user_id' => $user['id']]);
$total_records = $total_stmt->fetchColumn();
$total_pages = ceil($total_records / $records_per_page);
// 3. Fetch Transactions for the current page
$stmt = $db->prepare("SELECT * FROM transactions WHERE user_id = :user_id ORDER BY created_at DESC LIMIT :limit OFFSET :offset");
$stmt->bindValue(':user_id', $user['id'], PDO::PARAM_INT);
$stmt->bindValue(':limit', $records_per_page, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$transactions = $stmt->fetchAll(PDO::FETCH_ASSOC);
$site_name = 'Kutumbh Infra';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Transaction Ledger - <?php echo htmlspecialchars($site_name); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
<link href="assets/css/dashboard.css" rel="stylesheet">
</head>
<body>
<div class="sidebar">
<a href="index.php" class="logo"><?php echo htmlspecialchars($site_name); ?></a>
<ul class="nav flex-column">
<li class="nav-item"><a class="nav-link" href="dashboard.php"><i class="bi bi-grid"></i> Dashboard</a></li>
<?php if (is_agent()): ?>
<li class="nav-item"><a class="nav-link" href="submit_booking.php"><i class="bi bi-journal-plus"></i> Submit Booking</a></li>
<?php endif; ?>
<li class="nav-item"><a class="nav-link" href="genealogy.php"><i class="bi bi-diagram-3"></i> Genealogy Tree</a></li>
<li class="nav-item"><a class="nav-link active" href="ledger.php"><i class="bi bi-receipt"></i> Ledger</a></li>
<li class="nav-item"><a class="nav-link" href="withdraw.php"><i class="bi bi-cash-coin"></i> Withdraw</a></li>
<?php if (is_admin() || is_super_admin()): ?>
<li class="nav-item"><hr></li>
<li class="nav-item-header">Admin Menu</li>
<li class="nav-item"><a class="nav-link" href="admin/bookings.php"><i class="bi bi-calendar-check"></i> Manage Bookings</a></li>
<?php endif; ?>
<?php if (is_super_admin()): ?>
<li class="nav-item"><a class="nav-link" href="admin_dashboard.php"><i class="bi bi-people"></i> User Management</a></li>
<li class="nav-item"><a class="nav-link" href="edit_content.php"><i class="bi bi-pencil-square"></i> Edit Content</a></li>
<?php endif; ?>
<li class="nav-item"><hr></li>
<li class="nav-item"><a class="nav-link" href="logout.php"><i class="bi bi-box-arrow-left"></i> Logout</a></li>
</ul>
</div>
<div class="main-content">
<header class="header">
<h1 class="h3 mb-0">Transaction Ledger</h1>
<div class="dropdown">
<a href="#" class="d-block link-dark text-decoration-none dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-person-circle fs-4"></i> <span class="d-none d-sm-inline mx-1"><?php echo htmlspecialchars($user['name']); ?></span>
</a>
<ul class="dropdown-menu text-small dropdown-menu-end">
<li><span class="dropdown-item-text"><strong><?php echo htmlspecialchars($user['name']); ?></strong><br><small><?php echo htmlspecialchars($user['role']); ?></small></span></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="logout.php">Sign out</a></li>
</ul>
</div>
</header>
<main class="container-fluid">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">Your Transactions</h5>
<div class="text-end">
<h6 class="mb-0">Wallet Balance</h6>
<span class="fs-5">₹<?php echo number_format($user['wallet_balance'], 2); ?></span>
</div>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Date</th>
<th>Type</th>
<th>Amount</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php if (empty($transactions)): ?>
<tr><td colspan="4" class="text-center">No transactions found.</td></tr>
<?php else: ?>
<?php foreach ($transactions as $tx): ?>
<tr>
<td><?php echo date('d M Y, H:i', strtotime($tx['created_at'])); ?></td>
<td><span class="badge bg-secondary"><?php echo ucwords(str_replace('_', ' ', $tx['type'])); ?></span></td>
<td class="<?php echo $tx['amount'] >= 0 ? 'text-success' : 'text-danger'; ?> fw-bold">
<?php echo ($tx['amount'] >= 0 ? '+' : '-') . ' ₹' . number_format(abs($tx['amount']), 2); ?>
</td>
<td><?php echo htmlspecialchars($tx['description']); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<!-- Pagination Controls -->
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
<?php if ($page > 1): ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $page - 1; ?>">Previous</a></li>
<?php endif; ?>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php echo $i == $page ? 'active' : ''; ?>"><a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
<?php endfor; ?>
<?php if ($page < $total_pages): ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $page + 1; ?>">Next</a></li>
<?php endif; ?>
</ul>
</nav>
</div>
</div>
</main>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>