| Server IP : 72.60.21.38 / Your IP : 216.73.216.25 Web Server : LiteSpeed System : Linux uk-fast-web1372.main-hosting.eu 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64 User : u390967363 ( 390967363) PHP Version : 8.2.30 Disable Function : system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail MySQL : OFF | cURL : ON | WGET : ON | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /home/u390967363/domains/aibenproperties.com/public_html/app/ |
Upload File : |
<?php
session_start();
require_once 'includes/db.php';
require_once 'includes/functions.php';
// Access Control
if (!isset($_SESSION['user_id']) || !in_array($_SESSION['user_role'], ['super_admin', 'admin', 'estate_manager', 'sales_manager'])) {
header("Location: dashboard.php");
exit;
}
$companyId = getCurrentCompanyId();
$companyFilter = $companyId ? " AND d.company_id = $companyId" : "";
$commCompanyFilter = $companyId ? " AND c.company_id = $companyId" : "";
// --- FILTERS ---
$start_date = $_GET['start_date'] ?? date('Y-01-01'); // Start of year
$end_date = $_GET['end_date'] ?? date('Y-m-t');
$view_type = $_GET['type'] ?? 'performance'; // performance, deals, commissions
// --- DATA FETCHING ---
// 1. Agent Performance
// Group by Agent, Count Deals, Sum Value, Sum Commissions
// Need to join deals and commissions separately or aggregate subqueries.
// Simpler approach: Fetch users with role='agent' and then count deals/commissions for them.
// OR Group Deals by Agent.
$performanceData = [];
if ($view_type == 'performance') {
$perfQuery = "
SELECT
u.id as agent_id,
u.name as agent_name,
COUNT(d.id) as total_deals,
SUM(CASE WHEN d.status = 'closed' THEN d.deal_value ELSE 0 END) as total_sales_value,
COUNT(CASE WHEN d.status = 'closed' THEN 1 END) as closed_deals,
(SELECT SUM(amount) FROM commissions c WHERE c.agent_id = u.id AND c.status = 'paid') as total_commission_paid
FROM users u
LEFT JOIN deals d ON u.id = d.agent_id
WHERE u.role IN ('agent', 'sales_agent')
GROUP BY u.id
ORDER BY total_sales_value DESC
";
// Note: dealing with company_id might be tricky if agents work across companies, but usually they are scoped.
// If company_id is in users table:
if ($companyId) {
$perfQuery = str_replace("WHERE u.role", "WHERE u.company_id = $companyId AND u.role", $perfQuery);
}
try {
$perfStmt = $pdo->prepare($perfQuery);
$perfStmt->execute();
$performanceData = $perfStmt->fetchAll();
} catch (Exception $e) {
// Fallback if deals table columns mismatch
$performanceData = [];
}
}
// 2. Deals Pipeline
$dealsData = [];
if ($view_type == 'deals') {
$dealsQuery = "
SELECT d.*, u.name as agent_name, p.title as property_title, c.name as client_name
FROM deals d
LEFT JOIN users u ON d.agent_id = u.id
LEFT JOIN properties p ON d.property_id = p.id
LEFT JOIN users c ON d.client_id = c.id
WHERE d.created_at BETWEEN ? AND ?
$companyFilter
ORDER BY d.created_at DESC
";
try {
$dealsStmt = $pdo->prepare($dealsQuery);
$dealsStmt->execute([$start_date . ' 00:00:00', $end_date . ' 23:59:59']);
$dealsData = $dealsStmt->fetchAll();
} catch (Exception $e) {
$dealsData = [];
}
}
// 3. Commissions
$commData = [];
if ($view_type == 'commissions') {
$commQuery = "
SELECT c.*, u.name as agent_name, d.reference as deal_ref
FROM commissions c
LEFT JOIN users u ON c.agent_id = u.id
LEFT JOIN deals d ON c.deal_id = d.id
WHERE c.created_at BETWEEN ? AND ?
$commCompanyFilter
ORDER BY c.created_at DESC
";
try {
$commStmt = $pdo->prepare($commQuery);
$commStmt->execute([$start_date . ' 00:00:00', $end_date . ' 23:59:59']);
$commData = $commStmt->fetchAll();
} catch (Exception $e) {
$commData = [];
}
}
include 'includes/header.php';
?>
<div class="container-fluid py-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<div>
<h1 class="h3 text-gray-800">Sales & Agent Reports</h1>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="reports.php">Reports</a></li>
<li class="breadcrumb-item active">Sales</li>
</ol>
</nav>
</div>
<div>
<button class="btn btn-outline-secondary me-2" onclick="window.print()"><i class="fa-solid fa-print"></i> Print</button>
</div>
</div>
<!-- Filter Form -->
<div class="card shadow mb-4">
<div class="card-body">
<form method="GET" class="row g-3 align-items-end">
<input type="hidden" name="type" value="<?= $view_type ?>">
<div class="col-md-3">
<label class="form-label fw-bold">Start Date</label>
<input type="date" name="start_date" class="form-control" value="<?= $start_date ?>">
</div>
<div class="col-md-3">
<label class="form-label fw-bold">End Date</label>
<input type="date" name="end_date" class="form-control" value="<?= $end_date ?>">
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-primary w-100">Apply Filter</button>
</div>
</form>
</div>
</div>
<!-- Navigation Tabs -->
<ul class="nav nav-tabs mb-4">
<li class="nav-item">
<a class="nav-link <?= $view_type == 'performance' ? 'active' : '' ?>" href="?type=performance&start_date=<?= $start_date ?>&end_date=<?= $end_date ?>">Agent Performance</a>
</li>
<li class="nav-item">
<a class="nav-link <?= $view_type == 'deals' ? 'active' : '' ?>" href="?type=deals&start_date=<?= $start_date ?>&end_date=<?= $end_date ?>">Deal Pipeline</a>
</li>
<li class="nav-item">
<a class="nav-link <?= $view_type == 'commissions' ? 'active' : '' ?>" href="?type=commissions&start_date=<?= $start_date ?>&end_date=<?= $end_date ?>">Commissions</a>
</li>
</ul>
<!-- Content Area -->
<div class="card shadow mb-4">
<div class="card-body">
<?php if ($view_type == 'performance'): ?>
<h5 class="card-title mb-4">Top Performing Agents</h5>
<div class="table-responsive">
<table class="table table-bordered table-striped" width="100%" cellspacing="0">
<thead>
<tr>
<th>Agent Name</th>
<th class="text-center">Total Deals</th>
<th class="text-center">Closed Deals</th>
<th class="text-end">Total Sales Value</th>
<th class="text-end">Commission Paid</th>
</tr>
</thead>
<tbody>
<?php if (empty($performanceData)): ?>
<tr><td colspan="5" class="text-center text-muted py-4">No agent data found.</td></tr>
<?php else: ?>
<?php foreach ($performanceData as $row): ?>
<tr>
<td><?= htmlspecialchars($row['agent_name']) ?></td>
<td class="text-center"><?= $row['total_deals'] ?></td>
<td class="text-center"><?= $row['closed_deals'] ?></td>
<td class="text-end fw-bold"><?= formatCurrency($row['total_sales_value'] ?? 0) ?></td>
<td class="text-end text-success"><?= formatCurrency($row['total_commission_paid'] ?? 0) ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<?php elseif ($view_type == 'deals'): ?>
<h5 class="card-title mb-4">Deal Pipeline</h5>
<div class="table-responsive">
<table class="table table-bordered table-striped" width="100%" cellspacing="0">
<thead>
<tr>
<th>Date</th>
<th>Deal Ref</th>
<th>Property</th>
<th>Client</th>
<th>Agent</th>
<th>Status</th>
<th class="text-end">Value</th>
</tr>
</thead>
<tbody>
<?php if (empty($dealsData)): ?>
<tr><td colspan="7" class="text-center text-muted py-4">No deals found for this period.</td></tr>
<?php else: ?>
<?php foreach ($dealsData as $row): ?>
<tr>
<td><?= formatDate($row['created_at']) ?></td>
<td><?= htmlspecialchars($row['reference']) ?></td>
<td><?= htmlspecialchars($row['property_title']) ?></td>
<td><?= htmlspecialchars($row['client_name']) ?></td>
<td><?= htmlspecialchars($row['agent_name']) ?></td>
<td><span class="badge <?= getStatusBadgeClass($row['status']) ?>"><?= ucfirst($row['status']) ?></span></td>
<td class="text-end fw-bold"><?= formatCurrency($row['deal_value'] ?? 0) ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<?php elseif ($view_type == 'commissions'): ?>
<h5 class="card-title mb-4">Commission Payouts</h5>
<div class="table-responsive">
<table class="table table-bordered table-striped" width="100%" cellspacing="0">
<thead>
<tr>
<th>Date Paid</th>
<th>Agent</th>
<th>Deal Ref</th>
<th>Status</th>
<th class="text-end">Amount</th>
</tr>
</thead>
<tbody>
<?php if (empty($commData)): ?>
<tr><td colspan="5" class="text-center text-muted py-4">No commission records found.</td></tr>
<?php else: ?>
<?php foreach ($commData as $row): ?>
<tr>
<td><?= $row['date_paid'] ? formatDate($row['date_paid']) : '-' ?></td>
<td><?= htmlspecialchars($row['agent_name']) ?></td>
<td><?= htmlspecialchars($row['deal_ref']) ?></td>
<td><span class="badge <?= getStatusBadgeClass($row['status']) ?>"><?= ucfirst($row['status']) ?></span></td>
<td class="text-end fw-bold"><?= formatCurrency($row['amount']) ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>