Files
arif_grafin/CONTENT/ROOT_URI/Admin/agent_View_report.php

182 lines
6.3 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// ---- Session start ----
if (session_status() === PHP_SESSION_NONE) session_start();
// ---- Default Dates ----
$today = date('Y-m-d');
$monthStart = date('Y-m-01');
$dFrom = $_GET['dFrom'] ?? $monthStart;
$dTo = $_GET['dTo'] ?? $today;
// ---- Form ----
?>
<div class="container mt-4">
<div class="card shadow-lg p-4 rounded-3">
<h4 class="mb-3">Commission Report</h4>
<form method="get" class="row g-3">
<div class="col-md-3">
<label class="form-label">From</label>
<input value="<?= $dFrom ?>" type="date" name="dFrom" class="form-control" required>
</div>
<div class="col-md-3">
<label class="form-label">To</label>
<input value="<?= $dTo ?>" type="date" name="dTo" class="form-control" required>
</div>
<div class="col-md-3 d-flex align-items-end" style="margin-top: 25px;">
<button type="submit" class="btn btn-info w-100">Generate Report</button>
</div>
</form>
</div>
</div>
<?php
// ==============================
// MAIN FUNCTION
// ==============================
function commission_report($dateFrom, $dateTo) {
$conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
$loginType = $_SESSION['type'] ?? 'admin';
$loginId = $_SESSION['user_id'] ?? 0;
if (!$dateFrom || !$dateTo) {
echo "<div class='alert alert-warning'>Please select valid date range!</div>";
return;
}
// ---- Slab setup ----
$daysDiff = (strtotime($dateTo) - strtotime($dateFrom)) / (60 * 60 * 24);
if ($daysDiff >= 300) {
$slabs = [
['min'=>50, 'max'=>99, 'commission'=>45],
['min'=>100,'max'=>199, 'commission'=>70],
['min'=>200,'max'=>499, 'commission'=>110],
['min'=>500,'max'=>1000, 'commission'=>150],
['min'=>1001,'max'=>9999999,'commission'=>200],
];
$slabLabel = "1 Year (360 Days)";
} else {
$slabs = [
['min'=>50, 'max'=>99, 'commission'=>20],
['min'=>100,'max'=>199, 'commission'=>30],
['min'=>200,'max'=>499, 'commission'=>50],
['min'=>500,'max'=>1000, 'commission'=>70],
['min'=>1001,'max'=>9999999,'commission'=>100],
];
$slabLabel = "6 Months (180 Days)";
}
// ---- Query ----
$sql = "SELECT u.user_id, u.user_name, u.comi_rate, COALESCE(SUM(t.AT_AMOUNT),0) AS total_amount FROM `{$GLOBALS['arif_users']}` u LEFT JOIN `{$GLOBALS['arif_ac']}` a ON u.user_id = a.AA_AGENT LEFT JOIN `{$GLOBALS['arif_tran']}` t ON a.AA_ACNO = t.AT_ACID AND t.AT_TIMESTAMP BETWEEN '{$dateFrom} 00:00:00' AND '{$dateTo} 23:59:59' WHERE u.type = 'agent'";
if ($loginType === 'agent') {
$sql .= " AND u.user_id = '{$loginId}'";
}
$sql .= " GROUP BY u.user_id, u.user_name, u.comi_rate ORDER BY u.user_name ASC";
$result = $conn->query($sql);
if (!$result) {
echo "<div class='alert alert-danger'>SQL Error: {$conn->error}</div>";
return;
}
echo "
<div class='container mt-4'>
<div class='alert alert-primary shadow-sm'>
<h5 class='mb-0'>Commission Report: {$dateFrom} → {$dateTo} <small class='text-muted'>({$slabLabel})</small></h5>
</div>
<div class='card shadow-sm rounded-3'>
<div class='card-body'>
<div class='table-responsive'>
<table class='table table-bordered table-hover align-middle'>
<thead class='table-light'>
<tr>
<th>Agent ID</th>
<th>Agent Name</th>
<th>Total Collection</th>
<th>Collection Commission</th>
<th>ACC Open Commission</th>
<th>Total Commission</th>
</tr>
</thead>
<tbody>
";
$grandCollection = $grandSelfCom = $grandReferCom = 0;
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$agentId = $row['user_id'];
$collection = floatval($row['total_amount']);
$selfCom = ($collection * floatval($row['comi_rate'])) / 100;
// ---- Refer Collection ----
$refQuery = "SELECT COALESCE(SUM(t.AT_AMOUNT),0) AS refer_total FROM `{$GLOBALS['arif_ac']}` a LEFT JOIN `{$GLOBALS['arif_tran']}` t ON a.AA_ACNO = t.AT_ACID AND t.AT_TIMESTAMP BETWEEN '{$dateFrom} 00:00:00' AND '{$dateTo} 23:59:59' WHERE a.AA_AGENT = '{$agentId}'";
$refRes = $conn->query($refQuery);
$referCollection = 0;
if ($refRes && $refRes->num_rows > 0) {
$referCollection = floatval($refRes->fetch_assoc()['refer_total']);
}
// ---- Refer Commission ----
$referCom = 0;
foreach ($slabs as $s) {
if ($referCollection >= $s['min'] && $referCollection <= $s['max']) {
$referCom = $s['commission'];
break;
}
}
$totalCom = $selfCom + $referCom;
$grandCollection += $collection;
$grandSelfCom += $selfCom;
$grandReferCom += $referCom;
echo "
<tr>
<td>{$agentId}</td>
<td>{$row['user_name']}</td>
<td>".number_format($collection,2)."</td>
<td>".number_format($selfCom,2)."</td>
<td>".number_format($referCom,2)."</td>
<td><b>".number_format($totalCom,2)."</b></td>
</tr>";
}
} else {
echo "<tr><td colspan='6' class='text-center text-muted'>No data found</td></tr>";
}
// ---- Grand Total ----
if ($loginType !== 'agent') {
echo "
<tr class='table-secondary fw-bold'>
<td colspan='2' class='text-end'>Grand Total:</td>
<td>".number_format($grandCollection,2)."</td>
<td>".number_format($grandSelfCom,2)."</td>
<td>".number_format($grandReferCom,2)."</td>
<td>".number_format($grandSelfCom + $grandReferCom,2)."</td>
</tr>";
}
echo "
</tbody>
</table>
</div>
</div>
</div>
</div>";
$conn->close();
}
// ---- CALL FUNCTION ----
commission_report($dFrom, $dTo);
?>