add account opening comission function

This commit is contained in:
2025-10-06 07:31:15 +00:00
parent b00a2bcdc9
commit 34dadd8de5
3 changed files with 198 additions and 116 deletions

View File

@@ -1,11 +1,20 @@
<?php <?php
// ---- Default date range ---- ini_set('display_errors', 1);
$today = date("Y-m-d"); // Today date ini_set('display_startup_errors', 1);
$monthStart = date("Y-m-01"); // first date of each month 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; $dFrom = $_GET['dFrom'] ?? $monthStart;
$dTo = $_GET['dTo'] ?? $today; $dTo = $_GET['dTo'] ?? $today;
?>
// ---- Form ----
?>
<div class="container mt-4"> <div class="container mt-4">
<div class="card shadow-lg p-4 rounded-3"> <div class="card shadow-lg p-4 rounded-3">
<h4 class="mb-3">Commission Report</h4> <h4 class="mb-3">Commission Report</h4>
@@ -25,94 +34,148 @@ $dTo = $_GET['dTo'] ?? $today;
</div> </div>
</div> </div>
<?php <?php
// ==============================
// MAIN FUNCTION
// ==============================
function commission_report($dateFrom, $dateTo) { function commission_report($dateFrom, $dateTo) {
echo '<div class="container mt-4">
<div class="alert alert-primary shadow-sm">
<h5 class="mb-0">Commission Report: '.$dateFrom." → ".$dateTo.'</h5>
</div>
</div>';
$conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']); $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error); if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
// ---- Login user type check ---- $loginType = $_SESSION['type'] ?? 'admin';
$loginType = $_SESSION['type'] ?? ''; // login session-এ type আছে $loginId = $_SESSION['user_id'] ?? 0;
$loginId = $_SESSION['user_id']; // login user_id
// ---- Base Query ---- if (!$dateFrom || !$dateTo) {
$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'"; 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 Agent self data filter ----
if ($loginType === 'agent') { if ($loginType === 'agent') {
$sql .= " AND u.user_id = '".$loginId."'"; $sql .= " AND u.user_id = '{$loginId}'";
} }
$sql .= " GROUP BY u.user_id, u.user_name, u.comi_rate ORDER BY u.user_name ASC"; $sql .= " GROUP BY u.user_id, u.user_name, u.comi_rate ORDER BY u.user_name ASC";
$result = $conn->query($sql); $result = $conn->query($sql);
if (!$result) {
echo "<div class='alert alert-danger'>SQL Error: {$conn->error}</div>";
return;
}
echo ' echo "
<div class="container mt-3"> <div class='container mt-4'>
<div class="card shadow-sm rounded-3"> <div class='alert alert-primary shadow-sm'>
<div class="card-body"> <h5 class='mb-0'>Commission Report: {$dateFrom} → {$dateTo} <small class='text-muted'>({$slabLabel})</small></h5>
<div class="table-responsive"> </div>
<table class="table table-bordered table-hover align-middle"> <div class='card shadow-sm rounded-3'>
<thead class="table-light"> <div class='card-body'>
<tr> <div class='table-responsive'>
<th>Agent ID</th> <table class='table table-bordered table-hover align-middle'>
<th>Agent Name</th> <thead class='table-light'>
<th>Total Collection</th> <tr>
<th>Total Commission</th> <th>Agent ID</th>
</tr> <th>Agent Name</th>
</thead> <th>Total Collection</th>
<tbody>'; <th>Collection Commission</th>
<th>ACC Open Commission</th>
<th>Total Commission</th>
</tr>
</thead>
<tbody>
";
$grandTotalCommission = 0; $grandCollection = $grandSelfCom = $grandReferCom = 0;
$grandTotalCollection = 0;
if ($result && $result->num_rows > 0) { if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
$collection = $row["total_amount"]; $agentId = $row['user_id'];
$commission = ($collection * $row["comi_rate"]) / 100; $collection = floatval($row['total_amount']);
$selfCom = ($collection * floatval($row['comi_rate'])) / 100;
$grandTotalCollection += $collection; // ---- Refer Collection ----
$grandTotalCommission += $commission; $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 " echo "
<tr> <tr>
<td>".$row["user_id"]."</td> <td>{$agentId}</td>
<td>".$row["user_name"]."</td> <td>{$row['user_name']}</td>
<td>".number_format($collection,2)."</td> <td>".number_format($collection,2)."</td>
<td>".number_format($commission,2)."</td> <td>".number_format($selfCom,2)."</td>
</tr>"; <td>".number_format($referCom,2)."</td>
<td><b>".number_format($totalCom,2)."</b></td>
</tr>";
} }
} else { } else {
echo "<tr><td colspan='4' class='text-center text-muted'>No agents found</td></tr>"; echo "<tr><td colspan='6' class='text-center text-muted'>No data found</td></tr>";
} }
echo ' // ---- Grand Total ----
</tbody>
</table>';
// ---- if admin/bm then show grand total ----
if ($loginType !== 'agent') { if ($loginType !== 'agent') {
echo '<h5 class="text-end"> echo "
Grand Total Collection : <b>'.number_format($grandTotalCollection,2).'</b><br> <tr class='table-secondary fw-bold'>
Grand Total Commission : <b>'.number_format($grandTotalCommission,2).'</b> <td colspan='2' class='text-end'>Grand Total:</td>
</h5>'; <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 ' echo "
</div> </tbody>
</div> </table>
</div> </div>
'; </div>
</div>
</div>";
$conn->close(); $conn->close();
} }
// ---- Call commission function ---- // ---- CALL FUNCTION ----
commission_report($dFrom, $dTo); commission_report($dFrom, $dTo);
?> ?>

View File

@@ -1,7 +1,7 @@
<?php <?php
// ---- Default date range ---- // ---- Default date range ----
$today = date("Y-m-d"); // Today date $today = date("Y-m-d");
$monthStart = date("Y-m-01"); // first date of each month $monthStart = date("Y-m-01");
$dFrom = $_GET['dFrom'] ?? $monthStart; $dFrom = $_GET['dFrom'] ?? $monthStart;
$dTo = $_GET['dTo'] ?? $today; $dTo = $_GET['dTo'] ?? $today;
@@ -40,20 +40,38 @@ function commission_report($dateFrom, $dateTo, $isAgent) {
$conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']); $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error); if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
// ---- Login user type check ---- $loginId = $_SESSION['user_id'] ?? 0;
$loginType = $_SESSION['type'] ?? ''; // login session-এ type আছে
$loginId = $_SESSION['user_id']; // login user_id
// ---- Base Query ---- // ---- Slab setup for ACC opening commission ----
$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'"; $daysDiff = (strtotime($dateTo) - strtotime($dateFrom)) / (60*60*24);
if ($daysDiff >= 300) { // 1 year
// ---- if Agent self data filter ---- $slabs = [
if ($isAgent) { ['min'=>50, 'max'=>99, 'commission'=>45],
$sql .= " AND u.user_id = '".$loginId."'"; ['min'=>100,'max'=>199, 'commission'=>70],
['min'=>200,'max'=>499, 'commission'=>110],
['min'=>500,'max'=>1000, 'commission'=>150],
['min'=>1001,'max'=>9999999,'commission'=>200],
];
} else { // 6 months
$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],
];
} }
$sql .= " GROUP BY u.user_id, u.user_name, u.comi_rate ORDER BY u.user_name ASC"; // ---- Base Query for collection ----
$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 ($isAgent) $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); $result = $conn->query($sql);
echo ' echo '
@@ -65,67 +83,69 @@ function commission_report($dateFrom, $dateTo, $isAgent) {
<thead class="table-light"> <thead class="table-light">
<tr>'; <tr>';
// Only show Agent ID and Name columns if user is not an agent if (!$isAgent) echo '<th>Agent ID</th><th>Agent Name</th>';
if (!$isAgent) { echo '<th>Total Collection</th><th>Collection Commission</th><th>ACC Open Commission</th><th>Total Commission</th></tr>
echo '<th>Agent ID</th>
<th>Agent Name</th>';
}
echo '<th>Total Collection</th>
<th>Total Commission</th>
</tr>
</thead> </thead>
<tbody>'; <tbody>';
$grandTotalCommission = 0; $grandCollection = $grandSelfCom = $grandReferCom = 0;
$grandTotalCollection = 0;
if ($result && $result->num_rows > 0) { if ($result && $result->num_rows > 0) {
while($row = $result->fetch_assoc()) { while($row = $result->fetch_assoc()) {
$collection = $row["total_amount"]; $agentId = $row['user_id'];
$commission = ($collection * $row["comi_rate"]) / 100; $collection = floatval($row['total_amount']);
$selfCom = ($collection * floatval($row["comi_rate"]))/100;
$grandTotalCollection += $collection; // ---- ACC Open Commission ----
$grandTotalCommission += $commission; $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']);
$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>"; echo "<tr>";
if (!$isAgent) echo "<td>{$agentId}</td><td>{$row['user_name']}</td>";
// Only show Agent ID and Name if user is not an agent
if (!$isAgent) {
echo "<td>".$row["user_id"]."</td>
<td>".$row["user_name"]."</td>";
}
echo "<td>".number_format($collection,2)."</td> echo "<td>".number_format($collection,2)."</td>
<td>".number_format($commission,2)."</td> <td>".number_format($selfCom,2)."</td>
</tr>"; <td>".number_format($referCom,2)."</td>
<td><b>".number_format($totalCom,2)."</b></td>
</tr>";
} }
} else { } else {
$colspan = $isAgent ? 2 : 4; $colspan = $isAgent ? 4 : 6;
echo "<tr><td colspan='$colspan' class='text-center text-muted'>No agents found</td></tr>"; echo "<tr><td colspan='$colspan' class='text-center text-muted'>No agents found</td></tr>";
} }
echo '
</tbody>
</table>';
// ---- if admin/bm then show grand total ----
if (!$isAgent) { if (!$isAgent) {
echo '<h5 class="text-end"> echo "<tr class='table-secondary fw-bold'>
Grand Total Collection : <b>'.number_format($grandTotalCollection,2).'</b><br> <td colspan='2' class='text-end'>Grand Total:</td>
Grand Total Commission : <b>'.number_format($grandTotalCommission,2).'</b> <td>".number_format($grandCollection,2)."</td>
</h5>'; <td>".number_format($grandSelfCom,2)."</td>
<td>".number_format($grandReferCom,2)."</td>
<td>".number_format($grandSelfCom+$grandReferCom,2)."</td>
</tr>";
} }
echo ' echo '</tbody></table></div></div></div></div>';
</div>
</div>
</div>
';
$conn->close(); $conn->close();
} }
// ---- Call commission function ---- // ---- Call commission function ----
commission_report($dFrom, $dTo, $isAgent); commission_report($dFrom, $dTo, $isAgent);
?> ?>

View File

@@ -44,7 +44,6 @@ if (isset($_POST["add_i"]) && isset($_POST["AA_ACNO"]) && is_numeric($_POST["add
echo json_encode($total); exit; echo json_encode($total); exit;
} }
} }
// 3. Update Account // 3. Update Account
$sql_upd = "UPDATE `".$GLOBALS['arif_ac']."` SET `AA_FINE` = `AA_FINE` + 5, `AA_BAL` = `AA_BAL` + $add_i, `AA_NO_OF_PAYPAID` = `AA_NO_OF_PAYPAID` + $ins_no WHERE `AA_ACNO` = '$AA_ACNO'"; $sql_upd = "UPDATE `".$GLOBALS['arif_ac']."` SET `AA_FINE` = `AA_FINE` + 5, `AA_BAL` = `AA_BAL` + $add_i, `AA_NO_OF_PAYPAID` = `AA_NO_OF_PAYPAID` + $ins_no WHERE `AA_ACNO` = '$AA_ACNO'";