add account opening comission function
This commit is contained in:
@@ -14,13 +14,14 @@
|
||||
$ca["ac_no"]="GV".$_POST["ac_type"].date("Ymd")."L";
|
||||
$AA_BAL=$_POST["Mature_Value"];$AA_BAL=$AA_BAL-($AA_BAL*2);
|
||||
$ca["ac_agent"] = $_POST["ac_agent"] ?? null;
|
||||
$ca["reffer_agent"] = $_POST["reffer_agent"] ?? null;
|
||||
//GC_new_entry($ca);
|
||||
|
||||
//echo $GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db'];
|
||||
|
||||
$t="Loan";
|
||||
$stmt = $conn->prepare("INSERT INTO ".$GLOBALS['arif_ac']." (`AA_BAL`,`AA_G1_DETAILS`,`AA_G2_DETAILS`,`AA_INSTALLMENT`,`AA_ICARD_NO`,`AA_INTEREST`,`AA_DATE`,`AA_DATE_MATURE`,`AA_NO_OF_PAYMENT`,`AA_AMOUNT`,`AA_MATURE_VALUE`,`AA_TYPE`,`AA_ACTYPE`,`AA_NAME`, `AA_PHONE`, `AA_EMAIL`, `AA_ADDRESS`, `AA_ADMIN`, `AA_AGENT`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
$stmt->bind_param("issssssssssssssssss" ,$AA_BAL,$_POST["AA_G1_DETAILS"],$_POST["AA_G2_DETAILS"],$_POST["CA_installment"],$_POST["AA_ICARD_NO"],$_POST["CA_interest"],$_POST["date_today"],$_POST["date_mature"],$_POST["CA_No_Of_Payment"],$_POST["CA_Amount"],$_POST["Mature_Value"], $t,$ca["ac_type"],$ca["ac_name"],$ca["ac_phone"],$ca["ac_mail"],$ca["ac_address"],$_SESSION["EMAIL"], $ca["ac_agent"]);
|
||||
$stmt = $conn->prepare("INSERT INTO ".$GLOBALS['arif_ac']." (`AA_BAL`,`AA_G1_DETAILS`,`AA_G2_DETAILS`,`AA_INSTALLMENT`,`AA_ICARD_NO`,`AA_INTEREST`,`AA_DATE`,`AA_DATE_MATURE`,`AA_NO_OF_PAYMENT`,`AA_AMOUNT`,`AA_MATURE_VALUE`,`AA_TYPE`,`AA_ACTYPE`,`AA_NAME`, `AA_PHONE`, `AA_EMAIL`, `AA_ADDRESS`, `AA_ADMIN`, `AA_AGENT`, `refferTo`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
$stmt->bind_param("isssssssssssssssssss" ,$AA_BAL,$_POST["AA_G1_DETAILS"],$_POST["AA_G2_DETAILS"],$_POST["CA_installment"],$_POST["AA_ICARD_NO"],$_POST["CA_interest"],$_POST["date_today"],$_POST["date_mature"],$_POST["CA_No_Of_Payment"],$_POST["CA_Amount"],$_POST["Mature_Value"], $t,$ca["ac_type"],$ca["ac_name"],$ca["ac_phone"],$ca["ac_mail"],$ca["ac_address"],$_SESSION["EMAIL"], $ca["ac_agent"], $ca["reffer_agent"]);
|
||||
|
||||
if($stmt->execute()){
|
||||
$lid=$conn->insert_id;$ca["ac_no"]=$ca["ac_no"].$lid;
|
||||
@@ -116,6 +117,17 @@
|
||||
<small class="form-text text-muted">Dedicate a Agent</small>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<div class="form-group">
|
||||
<select class="form-control" name="reffer_agent">
|
||||
<option>-Reffer a Agent-</option>
|
||||
<?php foreach ($agentList as $agent): ?>
|
||||
<option Value="<?php echo htmlspecialchars($agent['user_id']) ?>"><?php echo htmlspecialchars($agent['user_id']); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<small class="form-text text-muted">Reffer a Agent</small>
|
||||
</div>
|
||||
</td>
|
||||
</tr></table>
|
||||
<div class="form-group">
|
||||
<input type="text" class="form-control" id="date_today" name="date_today" value="<?php echo date('Y-m-d');?>" >
|
||||
|
||||
@@ -13,11 +13,12 @@ if(isset($_POST["ac_type"]) && isset($_POST["ac_name"]) && $_POST["ac_name"]!=""
|
||||
$ca["ac_address"]=$_POST["ac_address"];
|
||||
$ca["ac_no"]="GV".$_POST["ac_type"].date("Ymd")."R";
|
||||
$ca["ac_agent"] = $_POST["ac_agent"] ?? null;
|
||||
$ca["reffer_agent"] = $_POST["reffer_agent"] ?? null;
|
||||
|
||||
|
||||
$t="Recurring";
|
||||
$stmt = $conn->prepare("INSERT INTO ".$GLOBALS['arif_ac']." (`AA_INTEREST`,`AA_DATE`,`AA_ICARD_NO`,`AA_DATE_MATURE`,`AA_NO_OF_PAYMENT`,`AA_INSTALLMENT`,`AA_MATURE_VALUE`,`AA_TYPE`,`AA_ACTYPE`,`AA_NAME`, `AA_PHONE`, `AA_EMAIL`, `AA_ADDRESS`, `AA_NOMINEE_DETAILS`, `AA_ADMIN`, `AA_AGENT`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
$stmt->bind_param("ssssssssssssssss" ,$_POST["CA_interest"],$_POST["date_today"],$_POST["AA_ICARD_NO"],$_POST["date_mature"],$_POST["CA_No_Of_Payment"],$_POST["CA_Amount"],$_POST["Mature_Value"], $t,$ca["ac_type"],$ca["ac_name"],$ca["ac_phone"],$ca["ac_mail"],$ca["ac_address"],$_POST["AA_NOMINEE_DETAILS"],$_SESSION["EMAIL"], $ca["ac_agent"]);
|
||||
$stmt = $conn->prepare("INSERT INTO ".$GLOBALS['arif_ac']." (`AA_INTEREST`,`AA_DATE`,`AA_ICARD_NO`,`AA_DATE_MATURE`,`AA_NO_OF_PAYMENT`,`AA_INSTALLMENT`,`AA_MATURE_VALUE`,`AA_TYPE`,`AA_ACTYPE`,`AA_NAME`, `AA_PHONE`, `AA_EMAIL`, `AA_ADDRESS`, `AA_NOMINEE_DETAILS`, `AA_ADMIN`, `AA_AGENT`, `refferTo`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
$stmt->bind_param("sssssssssssssssss" ,$_POST["CA_interest"],$_POST["date_today"],$_POST["AA_ICARD_NO"],$_POST["date_mature"],$_POST["CA_No_Of_Payment"],$_POST["CA_Amount"],$_POST["Mature_Value"], $t,$ca["ac_type"],$ca["ac_name"],$ca["ac_phone"],$ca["ac_mail"],$ca["ac_address"],$_POST["AA_NOMINEE_DETAILS"],$_SESSION["EMAIL"], $ca["ac_agent"], $ca["reffer_agent"]);
|
||||
|
||||
if($stmt->execute()){
|
||||
$lid=$conn->insert_id;$ca["ac_no"]=$ca["ac_no"].$lid;
|
||||
@@ -96,6 +97,17 @@ if(isset($_POST["ac_type"]) && isset($_POST["ac_name"]) && $_POST["ac_name"]!=""
|
||||
<small class="form-text text-muted">Dedicate a Agent</small>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<div class="form-group">
|
||||
<select class="form-control" name="reffer_agent">
|
||||
<option>-Reffer a Agent-</option>
|
||||
<?php foreach ($agentList as $agent): ?>
|
||||
<option Value="<?php echo htmlspecialchars($agent['user_id']) ?>"><?php echo htmlspecialchars($agent['user_id']); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<small class="form-text text-muted">Reffer a Agent</small>
|
||||
</div>
|
||||
</td>
|
||||
</tr></table>
|
||||
<div class="form-group">
|
||||
<input type="text" class="form-control" id="date_today" name="date_today" value="<?php echo date('Y-m-d');?>" required>
|
||||
|
||||
@@ -1,23 +1,320 @@
|
||||
<?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');
|
||||
// ---- Default date range ----
|
||||
$today = date("Y-m-d");
|
||||
$monthStart = date("Y-m-01");
|
||||
$dFrom = $_GET['dFrom'] ?? $monthStart;
|
||||
$dTo = $_GET['dTo'] ?? $today;
|
||||
|
||||
// ---- Form ----
|
||||
function calculateOpeningCommission($accountCycle, $accountType, $totalAmount, $installmentAmount, $refferTo, $totalPayments) {
|
||||
// Commission chart data - Account Opening Commission
|
||||
$commissionChart = [
|
||||
'180' => ['50' => 10, '100' => 25, '200' => 60, '500' => 100, 'above' => 150],
|
||||
'360' => ['50' => 25, '100' => 60, '200' => 150, '500' => 200, 'above' => 250],
|
||||
'25' => ['50' => 10, '100' => 25, '200' => 60, '500' => 100, 'above' => 150],
|
||||
'50' => ['50' => 20, '100' => 50, '200' => 120, '500' => 170, 'above' => 200],
|
||||
'6' => ['50' => 10, '100' => 25, '200' => 60, '500' => 100, 'above' => 150],
|
||||
'12' => ['50' => 20, '100' => 50, '200' => 120, '500' => 170, 'above' => 200]
|
||||
];
|
||||
|
||||
$commission = 0;
|
||||
|
||||
// Loan account commission (0.5% of total amount)
|
||||
if ($accountType === 'Loan') {
|
||||
$commission = $totalAmount * 0.005;
|
||||
return $commission;
|
||||
}
|
||||
|
||||
// Recurring account commission - ONLY for exact amounts in chart (50, 100, 200, 500, above 500)
|
||||
if ($accountType === 'Recurring') {
|
||||
$planKey = '';
|
||||
|
||||
if ($accountCycle === 'D') {
|
||||
$planKey = ($totalPayments <= 180) ? '180' : '360';
|
||||
}
|
||||
elseif ($accountCycle === 'W') {
|
||||
$planKey = ($totalPayments <= 25) ? '25' : '50';
|
||||
}
|
||||
elseif ($accountCycle === 'M') {
|
||||
$planKey = ($totalPayments <= 6) ? '6' : '12';
|
||||
}
|
||||
|
||||
if (isset($commissionChart[$planKey])) {
|
||||
$plan = $commissionChart[$planKey];
|
||||
|
||||
// ONLY give commission for exact amounts that are in the chart
|
||||
if ($installmentAmount == 50) {
|
||||
$commission = $plan['50'];
|
||||
} elseif ($installmentAmount == 100) {
|
||||
$commission = $plan['100'];
|
||||
} elseif ($installmentAmount == 200) {
|
||||
$commission = $plan['200'];
|
||||
} elseif ($installmentAmount == 500) {
|
||||
$commission = $plan['500'];
|
||||
} elseif ($installmentAmount > 500) {
|
||||
$commission = $plan['above'];
|
||||
}
|
||||
// For amounts like 20, 30, 40, 70, 150, 300 etc. - NO COMMISSION (commission remains 0)
|
||||
}
|
||||
}
|
||||
|
||||
return $commission;
|
||||
}
|
||||
|
||||
function calculateCollectionCommission($agentId, $dateFrom, $dateTo) {
|
||||
$conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
|
||||
|
||||
// Get agent's commission rate
|
||||
$agentSql = "SELECT comi_rate FROM `".$GLOBALS['arif_users']."` WHERE user_id = '$agentId'";
|
||||
$agentResult = $conn->query($agentSql);
|
||||
$comiRate = 0;
|
||||
|
||||
if ($agentResult && $agentResult->num_rows > 0) {
|
||||
$agentData = $agentResult->fetch_assoc();
|
||||
$comiRate = $agentData['comi_rate'];
|
||||
}
|
||||
|
||||
// Get total collection amount for this agent's accounts
|
||||
$collectionSql = "SELECT COALESCE(SUM(t.AT_AMOUNT),0) as total_collection
|
||||
FROM `".$GLOBALS['arif_tran']."` t
|
||||
JOIN `".$GLOBALS['arif_ac']."` a ON t.AT_ACID = a.AA_ACNO
|
||||
WHERE a.AA_AGENT = '$agentId'
|
||||
AND t.AT_TIMESTAMP BETWEEN '$dateFrom 00:00:00' AND '$dateTo 23:59:59'";
|
||||
|
||||
$collectionResult = $conn->query($collectionSql);
|
||||
$totalCollection = 0;
|
||||
|
||||
if ($collectionResult && $collectionResult->num_rows > 0) {
|
||||
$collectionData = $collectionResult->fetch_assoc();
|
||||
$totalCollection = $collectionData['total_collection'];
|
||||
}
|
||||
|
||||
$conn->close();
|
||||
|
||||
// Calculate collection commission
|
||||
$collectionCommission = ($totalCollection * $comiRate) / 100;
|
||||
|
||||
return [
|
||||
'total_collection' => $totalCollection,
|
||||
'comi_rate' => $comiRate,
|
||||
'collection_commission' => $collectionCommission
|
||||
];
|
||||
}
|
||||
|
||||
function dual_commission_report($dateFrom, $dateTo) {
|
||||
echo '<div class="container mt-4">
|
||||
<div class="alert alert-primary shadow-sm">
|
||||
<h5 class="mb-0">Dual Commission Report: '.$dateFrom." → ".$dateTo.'</h5>
|
||||
<small class="text-muted">Showing both Account Opening Commission and Collection Commission</small>
|
||||
</div>
|
||||
</div>';
|
||||
|
||||
$conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
|
||||
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
|
||||
|
||||
// ---- Login user type check ----
|
||||
$loginType = $_SESSION['type'] ?? '';
|
||||
$loginId = $_SESSION['user_id'] ?? '';
|
||||
|
||||
// ---- Get all accounts created in the date range ----
|
||||
$accountsSql = "SELECT * FROM `".$GLOBALS['arif_ac']."` WHERE `AA_TIMESTAMP` BETWEEN '".$dateFrom." 00:00:00' AND '".$dateTo." 23:59:59'";
|
||||
|
||||
// ---- if Agent then filter only their accounts ----
|
||||
if ($loginType === 'agent') {
|
||||
$accountsSql .= " AND `refferTo` = '".$loginId."'";
|
||||
}
|
||||
|
||||
$accountsResult = $conn->query($accountsSql);
|
||||
|
||||
// ---- Group commissions by agent ----
|
||||
$agentCommissions = [];
|
||||
|
||||
if ($accountsResult && $accountsResult->num_rows > 0) {
|
||||
while($account = $accountsResult->fetch_assoc()) {
|
||||
// Calculate OPENING commission
|
||||
$openingCommission = calculateOpeningCommission(
|
||||
$account['AA_ACTYPE'],
|
||||
$account['AA_TYPE'],
|
||||
$account['AA_AMOUNT'] ?? 0,
|
||||
$account['AA_INSTALLMENT'],
|
||||
$account['refferTo'],
|
||||
$account['AA_NO_OF_PAYMENT']
|
||||
);
|
||||
|
||||
// Determine which agent gets commission
|
||||
$commissionAgent = $account['refferTo'];
|
||||
|
||||
// If agent is viewing, only show their own data
|
||||
if ($loginType === 'agent' && $commissionAgent !== $loginId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($agentCommissions[$commissionAgent])) {
|
||||
// Get agent name
|
||||
$agentNameSql = "SELECT user_name FROM `".$GLOBALS['arif_users']."` WHERE user_id = '".$commissionAgent."'";
|
||||
$agentNameResult = $conn->query($agentNameSql);
|
||||
$agentName = $agentNameResult && $agentNameResult->num_rows > 0 ? $agentNameResult->fetch_assoc()['user_name'] : $commissionAgent;
|
||||
|
||||
$agentCommissions[$commissionAgent] = [
|
||||
'agent_name' => $agentName,
|
||||
'opening_commission' => 0,
|
||||
'collection_commission' => 0,
|
||||
'total_collection' => 0,
|
||||
'account_count' => 0,
|
||||
'comi_rate' => 0
|
||||
];
|
||||
|
||||
// Calculate COLLECTION commission for this agent
|
||||
$collectionData = calculateCollectionCommission($commissionAgent, $dateFrom, $dateTo);
|
||||
$agentCommissions[$commissionAgent]['collection_commission'] = $collectionData['collection_commission'];
|
||||
$agentCommissions[$commissionAgent]['total_collection'] = $collectionData['total_collection'];
|
||||
$agentCommissions[$commissionAgent]['comi_rate'] = $collectionData['comi_rate'];
|
||||
}
|
||||
|
||||
$agentCommissions[$commissionAgent]['opening_commission'] += $openingCommission;
|
||||
$agentCommissions[$commissionAgent]['account_count']++;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- If no data found and user is agent, show their own record with zero commission ----
|
||||
if ($loginType === 'agent' && empty($agentCommissions)) {
|
||||
$agentNameSql = "SELECT user_name, comi_rate FROM `".$GLOBALS['arif_users']."` WHERE user_id = '".$loginId."'";
|
||||
$agentNameResult = $conn->query($agentNameSql);
|
||||
if ($agentNameResult && $agentNameResult->num_rows > 0) {
|
||||
$agentData = $agentNameResult->fetch_assoc();
|
||||
$collectionData = calculateCollectionCommission($loginId, $dateFrom, $dateTo);
|
||||
|
||||
$agentCommissions[$loginId] = [
|
||||
'agent_name' => $agentData['user_name'],
|
||||
'opening_commission' => 0,
|
||||
'collection_commission' => $collectionData['collection_commission'],
|
||||
'total_collection' => $collectionData['total_collection'],
|
||||
'account_count' => 0,
|
||||
'comi_rate' => $collectionData['comi_rate']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
echo '
|
||||
<div class="container mt-3">
|
||||
<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>Accounts</th>
|
||||
<th>Collection Amount</th>
|
||||
<th>Collection Rate</th>
|
||||
<th>Opening Commission</th>
|
||||
<th>Collection Commission</th>
|
||||
<th>Total Commission</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>';
|
||||
|
||||
$grandTotalOpening = 0;
|
||||
$grandTotalCollection = 0;
|
||||
$grandTotalCommission = 0;
|
||||
$grandTotalCollectionAmount = 0;
|
||||
|
||||
if (!empty($agentCommissions)) {
|
||||
foreach($agentCommissions as $agentId => $agentData) {
|
||||
$totalCommission = $agentData['opening_commission'] + $agentData['collection_commission'];
|
||||
|
||||
$grandTotalOpening += $agentData['opening_commission'];
|
||||
$grandTotalCollection += $agentData['collection_commission'];
|
||||
$grandTotalCommission += $totalCommission;
|
||||
$grandTotalCollectionAmount += $agentData['total_collection'];
|
||||
|
||||
echo "
|
||||
<tr>
|
||||
<td>".$agentId."</td>
|
||||
<td>".$agentData['agent_name']."</td>
|
||||
<td class='text-center'>".$agentData['account_count']."</td>
|
||||
<td class='text-end'>".number_format($agentData['total_collection'], 2)."</td>
|
||||
<td class='text-center'>".$agentData['comi_rate']."%</td>
|
||||
<td class='text-end text-primary'>".number_format($agentData['opening_commission'], 2)."</td>
|
||||
<td class='text-end text-success'>".number_format($agentData['collection_commission'], 2)."</td>
|
||||
<td class='text-end fw-bold'>".number_format($totalCommission, 2)."</td>
|
||||
</tr>";
|
||||
}
|
||||
} else {
|
||||
echo "<tr><td colspan='8' class='text-center text-muted'>No commission data found</td></tr>";
|
||||
}
|
||||
|
||||
echo '
|
||||
</tbody>
|
||||
</table>';
|
||||
|
||||
// ---- Show grand totals ----
|
||||
if ($loginType !== 'agent' && !empty($agentCommissions)) {
|
||||
echo '<div class="row mt-4">
|
||||
<div class="col-md-4">
|
||||
<div class="card bg-primary text-white">
|
||||
<div class="card-body text-center">
|
||||
<h6>Opening Commission</h6>
|
||||
<h4>'.number_format($grandTotalOpening, 2).'</h4>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<div class="card bg-success text-white">
|
||||
<div class="card-body text-center">
|
||||
<h6>Collection Commission</h6>
|
||||
<h4>'.number_format($grandTotalCollection, 2).'</h4>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<div class="card bg-info text-white">
|
||||
<div class="card-body text-center">
|
||||
<h6>Total Commission</h6>
|
||||
<h4>'.number_format($grandTotalCommission, 2).'</h4>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>';
|
||||
} elseif ($loginType === 'agent' && !empty($agentCommissions)) {
|
||||
// Show individual agent summary
|
||||
$agentData = reset($agentCommissions); // Get first (and only) agent data
|
||||
$totalCommission = $agentData['opening_commission'] + $agentData['collection_commission'];
|
||||
|
||||
echo '<div class="row mt-4">
|
||||
<div class="col-md-6">
|
||||
<div class="card bg-light">
|
||||
<div class="card-body text-center">
|
||||
<h6>My Summary</h6>
|
||||
<p>Total Accounts: <b>'.$agentData['account_count'].'</b></p>
|
||||
<p>Total Collection: <b>'.number_format($agentData['total_collection'], 2).'</b></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="card bg-info text-white">
|
||||
<div class="card-body text-center">
|
||||
<h6>My Total Commission</h6>
|
||||
<h4>'.number_format($totalCommission, 2).'</h4>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>';
|
||||
}
|
||||
|
||||
echo '
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>';
|
||||
|
||||
$conn->close();
|
||||
}
|
||||
?>
|
||||
|
||||
<div class="container mt-4">
|
||||
<div class="card shadow-lg p-4 rounded-3">
|
||||
<h4 class="mb-3">Commission Report</h4>
|
||||
<h4 class="mb-3">Dual Commission Report</h4>
|
||||
<form method="get" class="row g-3">
|
||||
<div class="col-md-3">
|
||||
<label class="form-label">From</label>
|
||||
@@ -27,155 +324,14 @@ $dTo = $_GET['dTo'] ?? $today;
|
||||
<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 class="col-md-3 d-flex align-items-end">
|
||||
<button type="submit" class="btn btn-info w-100" style="margin-top: 25px;">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);
|
||||
?>
|
||||
// ---- Call dual commission function ----
|
||||
dual_commission_report($dFrom, $dTo);
|
||||
?>
|
||||
226
CONTENT/ROOT_URI/Admin/data.json
Normal file
226
CONTENT/ROOT_URI/Admin/data.json
Normal file
@@ -0,0 +1,226 @@
|
||||
[
|
||||
{
|
||||
"AA_ID": "1766",
|
||||
"AA_ADMIN": null,
|
||||
"AA_ACNO": "GVD20251001R1766",
|
||||
"AA_ACTYPE": "D",
|
||||
"AA_TIMESTAMP": "2025-10-01 07:21:20",
|
||||
"AA_BAL": "600",
|
||||
"AA_NAME": "BHANJAN BAR",
|
||||
"AA_FATHER": null,
|
||||
"AA_ICARD_NO": "7063172814",
|
||||
"AA_EMAIL": "",
|
||||
"AA_PHONE": "7063172814",
|
||||
"AA_ADDRESS": "KUMRA SCHOOL MATH",
|
||||
"AA_TYPE": "Recurring",
|
||||
"AA_INTEREST": "5.634",
|
||||
"AA_INSTALLMENT": "100",
|
||||
"AA_AMOUNT": null,
|
||||
"AA_FINE": "25",
|
||||
"AA_NO_OF_PAYMENT": "360",
|
||||
"AA_NO_OF_PAYPAID": "5",
|
||||
"AA_DATE_MATURE": "2026-11-1",
|
||||
"AA_DATE": "2025-10-01",
|
||||
"AA_G2_DETAILS": null,
|
||||
"AA_G1_DETAILS": null,
|
||||
"AA_MATURE_VALUE": "38000",
|
||||
"AA_NOMINEE_DETAILS": "Name:\r\nDOB:\r\nRelation:\r\nID:\r\n ",
|
||||
"AA_AGENT": "RUKSANA@KHATUN",
|
||||
"CLOSING_DATE": null,
|
||||
"STATUS": null,
|
||||
"allowMultiple": "1",
|
||||
"refferTo": "PUJA@KIRTANIA"
|
||||
},
|
||||
{
|
||||
"AA_ID": "1767",
|
||||
"AA_ADMIN": null,
|
||||
"AA_ACNO": "GVF20251003F1767",
|
||||
"AA_ACTYPE": "F",
|
||||
"AA_TIMESTAMP": "2025-10-03 04:46:15",
|
||||
"AA_BAL": "0",
|
||||
"AA_NAME": "SELIMA BIBI",
|
||||
"AA_FATHER": null,
|
||||
"AA_ICARD_NO": "3176 5017 7202",
|
||||
"AA_EMAIL": "",
|
||||
"AA_PHONE": "",
|
||||
"AA_ADDRESS": "",
|
||||
"AA_TYPE": "FD",
|
||||
"AA_INTEREST": "5.634",
|
||||
"AA_INSTALLMENT": "0",
|
||||
"AA_AMOUNT": "50",
|
||||
"AA_FINE": "0",
|
||||
"AA_NO_OF_PAYMENT": "180",
|
||||
"AA_NO_OF_PAYPAID": "0",
|
||||
"AA_DATE_MATURE": "2790-1-3",
|
||||
"AA_DATE": "2025-10-03",
|
||||
"AA_G2_DETAILS": null,
|
||||
"AA_G1_DETAILS": null,
|
||||
"AA_MATURE_VALUE": "92",
|
||||
"AA_NOMINEE_DETAILS": "Name:\r\nDOB:\r\nRelation:\r\nID:\r\n ",
|
||||
"AA_AGENT": "RUKSANA@KHATUN",
|
||||
"CLOSING_DATE": null,
|
||||
"STATUS": null,
|
||||
"allowMultiple": "0",
|
||||
"refferTo": "PUJA@KIRTANIA"
|
||||
},
|
||||
{
|
||||
"AA_ID": "1768",
|
||||
"AA_ADMIN": null,
|
||||
"AA_ACNO": "GVD20251003R1768",
|
||||
"AA_ACTYPE": "D",
|
||||
"AA_TIMESTAMP": "2025-10-03 04:47:35",
|
||||
"AA_BAL": "200",
|
||||
"AA_NAME": "SELIMA BIBI",
|
||||
"AA_FATHER": null,
|
||||
"AA_ICARD_NO": "3176 5017 7202",
|
||||
"AA_EMAIL": "",
|
||||
"AA_PHONE": "933638783",
|
||||
"AA_ADDRESS": "KAMDEBKATI",
|
||||
"AA_TYPE": "Recurring",
|
||||
"AA_INTEREST": "5.634",
|
||||
"AA_INSTALLMENT": "50",
|
||||
"AA_AMOUNT": null,
|
||||
"AA_FINE": "20",
|
||||
"AA_NO_OF_PAYMENT": "180",
|
||||
"AA_NO_OF_PAYPAID": "4",
|
||||
"AA_DATE_MATURE": "2026-5-3",
|
||||
"AA_DATE": "2025-10-03",
|
||||
"AA_G2_DETAILS": null,
|
||||
"AA_G1_DETAILS": null,
|
||||
"AA_MATURE_VALUE": "9250",
|
||||
"AA_NOMINEE_DETAILS": "Name:\r\nDOB:\r\nRelation:\r\nID:\r\n ",
|
||||
"AA_AGENT": "RUKSANA@KHATUN",
|
||||
"CLOSING_DATE": null,
|
||||
"STATUS": null,
|
||||
"allowMultiple": "0",
|
||||
"refferTo": "RUKSANA@KHATUN"
|
||||
},
|
||||
{
|
||||
"AA_ID": "1769",
|
||||
"AA_ADMIN": null,
|
||||
"AA_ACNO": "GVD20251003R1769",
|
||||
"AA_ACTYPE": "D",
|
||||
"AA_TIMESTAMP": "2025-10-03 10:37:07",
|
||||
"AA_BAL": "60",
|
||||
"AA_NAME": "BHUPAL MONDAL",
|
||||
"AA_FATHER": null,
|
||||
"AA_ICARD_NO": "7007 7717 9952",
|
||||
"AA_EMAIL": "",
|
||||
"AA_PHONE": "7699665158",
|
||||
"AA_ADDRESS": "KUMRA",
|
||||
"AA_TYPE": "Recurring",
|
||||
"AA_INTEREST": "5.634",
|
||||
"AA_INSTALLMENT": "30",
|
||||
"AA_AMOUNT": null,
|
||||
"AA_FINE": "10",
|
||||
"AA_NO_OF_PAYMENT": "360",
|
||||
"AA_NO_OF_PAYPAID": "2",
|
||||
"AA_DATE_MATURE": "2026-11-3",
|
||||
"AA_DATE": "2025-10-03",
|
||||
"AA_G2_DETAILS": null,
|
||||
"AA_G1_DETAILS": null,
|
||||
"AA_MATURE_VALUE": "11400",
|
||||
"AA_NOMINEE_DETAILS": "Name:\r\nDOB:\r\nRelation:\r\nID:\r\n ",
|
||||
"AA_AGENT": "RUKSANA@KHATUN",
|
||||
"CLOSING_DATE": null,
|
||||
"STATUS": null,
|
||||
"allowMultiple": "0",
|
||||
"refferTo": "RUKSANA@KHATUN"
|
||||
},
|
||||
{
|
||||
"AA_ID": "1770",
|
||||
"AA_ADMIN": null,
|
||||
"AA_ACNO": "GVD20251003R1770",
|
||||
"AA_ACTYPE": "D",
|
||||
"AA_TIMESTAMP": "2025-10-03 10:52:17",
|
||||
"AA_BAL": "40",
|
||||
"AA_NAME": "RENU BISWAS",
|
||||
"AA_FATHER": null,
|
||||
"AA_ICARD_NO": "8768690705",
|
||||
"AA_EMAIL": "",
|
||||
"AA_PHONE": "8768690705",
|
||||
"AA_ADDRESS": "KUMRA, KUSHI PALACE",
|
||||
"AA_TYPE": "Recurring",
|
||||
"AA_INTEREST": "5.634",
|
||||
"AA_INSTALLMENT": "20",
|
||||
"AA_AMOUNT": null,
|
||||
"AA_FINE": "10",
|
||||
"AA_NO_OF_PAYMENT": "360",
|
||||
"AA_NO_OF_PAYPAID": "2",
|
||||
"AA_DATE_MATURE": "2026-11-3",
|
||||
"AA_DATE": "2025-10-03",
|
||||
"AA_G2_DETAILS": null,
|
||||
"AA_G1_DETAILS": null,
|
||||
"AA_MATURE_VALUE": "7600",
|
||||
"AA_NOMINEE_DETAILS": "Name:HARIDAS BISWAS\r\nDOB:\r\nRelation:\r\nID:\r\n ",
|
||||
"AA_AGENT": "RUKSANA@KHATUN",
|
||||
"CLOSING_DATE": null,
|
||||
"STATUS": null,
|
||||
"allowMultiple": "0",
|
||||
"refferTo": "PUJA@KIRTANIA"
|
||||
},
|
||||
{
|
||||
"AA_ID": "1771",
|
||||
"AA_ADMIN": null,
|
||||
"AA_ACNO": "GVD20251005R1771",
|
||||
"AA_ACTYPE": "D",
|
||||
"AA_TIMESTAMP": "2025-10-05 12:55:40",
|
||||
"AA_BAL": "100",
|
||||
"AA_NAME": "NARESH CH BAIRAGI ",
|
||||
"AA_FATHER": null,
|
||||
"AA_ICARD_NO": "12345678",
|
||||
"AA_EMAIL": "",
|
||||
"AA_PHONE": "9733773954",
|
||||
"AA_ADDRESS": "KAMDEVKATHI ",
|
||||
"AA_TYPE": "Recurring",
|
||||
"AA_INTEREST": "5.647",
|
||||
"AA_INSTALLMENT": "100",
|
||||
"AA_AMOUNT": null,
|
||||
"AA_FINE": "5",
|
||||
"AA_NO_OF_PAYMENT": "180",
|
||||
"AA_NO_OF_PAYPAID": "1",
|
||||
"AA_DATE_MATURE": "2026-5-5",
|
||||
"AA_DATE": "2025-10-05",
|
||||
"AA_G2_DETAILS": null,
|
||||
"AA_G1_DETAILS": null,
|
||||
"AA_MATURE_VALUE": "18501",
|
||||
"AA_NOMINEE_DETAILS": "Name:\r\nDOB:\r\nRelation:\r\nID:\r\n ",
|
||||
"AA_AGENT": "PUJA@KIRTANIA",
|
||||
"CLOSING_DATE": null,
|
||||
"STATUS": null,
|
||||
"allowMultiple": "0",
|
||||
"refferTo": "RUKSANA@KHATUN"
|
||||
},
|
||||
{
|
||||
"AA_ID": "1772",
|
||||
"AA_ADMIN": null,
|
||||
"AA_ACNO": "GVD20251006R1772",
|
||||
"AA_ACTYPE": "D",
|
||||
"AA_TIMESTAMP": "2025-10-06 09:37:16",
|
||||
"AA_BAL": "200",
|
||||
"AA_NAME": "RIYAJUL MONDAL",
|
||||
"AA_FATHER": null,
|
||||
"AA_ICARD_NO": "123456",
|
||||
"AA_EMAIL": "",
|
||||
"AA_PHONE": "9093692025",
|
||||
"AA_ADDRESS": "PANCHGHARIA",
|
||||
"AA_TYPE": "Recurring",
|
||||
"AA_INTEREST": "5.467",
|
||||
"AA_INSTALLMENT": "200",
|
||||
"AA_AMOUNT": null,
|
||||
"AA_FINE": "5",
|
||||
"AA_NO_OF_PAYMENT": "360",
|
||||
"AA_NO_OF_PAYPAID": "1",
|
||||
"AA_DATE_MATURE": "2026-11-6",
|
||||
"AA_DATE": "2025-10-06",
|
||||
"AA_G2_DETAILS": null,
|
||||
"AA_G1_DETAILS": null,
|
||||
"AA_MATURE_VALUE": "75882",
|
||||
"AA_NOMINEE_DETAILS": "Name: ARBINA BIBI \r\nDOB:\r\nRelation:\r\nID:\r\n ",
|
||||
"AA_AGENT": "RUKSANA@KHATUN",
|
||||
"CLOSING_DATE": null,
|
||||
"STATUS": null,
|
||||
"allowMultiple": "0",
|
||||
"refferTo": "PUJA@KIRTANIA"
|
||||
}
|
||||
]
|
||||
@@ -63,11 +63,7 @@ function commission_report($dateFrom, $dateTo, $isAgent) {
|
||||
}
|
||||
|
||||
// ---- 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'";
|
||||
$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."'";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user