From 36e4935a98ff1400103f7c46c8291ffe714605c5 Mon Sep 17 00:00:00 2001 From: Suvo Date: Thu, 16 Oct 2025 13:17:19 +0000 Subject: [PATCH] s1 --- CONTENT/ROOT_URI/Admin/Report.php | 10 +- CONTENT/ROOT_URI/Agent/commission.php | 414 ++++++++++++++++++-------- 2 files changed, 298 insertions(+), 126 deletions(-) diff --git a/CONTENT/ROOT_URI/Admin/Report.php b/CONTENT/ROOT_URI/Admin/Report.php index fa5d3a6..542eb16 100644 --- a/CONTENT/ROOT_URI/Admin/Report.php +++ b/CONTENT/ROOT_URI/Admin/Report.php @@ -84,12 +84,12 @@ function report_view($type, $dt) { Transaction ID - '.($_SESSION['type'] === 'admin' ? "Agent" : "").' + '.($_SESSION['type'] === 'admin' || $_SESSION['type'] === 'bm' ? "Agent" : "").' Time AC No Name Amount - '.($_SESSION['type'] === 'admin' ? "Remarks" : "").' + '.($_SESSION['type'] === 'admin' || $_SESSION['type'] === 'bm' ? "Remarks" : "").' '; @@ -103,7 +103,7 @@ function report_view($type, $dt) { } // ----- if admin filter option ----- - if($_SESSION['type'] === 'admin' && isset($_GET['agent']) && $_GET['agent']!="") { + if($_SESSION['type'] === 'admin' || $_SESSION['type'] === 'bm' && isset($_GET['agent']) && $_GET['agent']!="") { $agentId = $conn->real_escape_string($_GET['agent']); $sql .= " AND `AA_AGENT`='".$agentId."'"; } @@ -117,12 +117,12 @@ function report_view($type, $dt) { echo " ".$row["AT_ID"]." - ".($_SESSION['type'] === 'admin' ? "".$row["AA_AGENT"]."" : "")." + ".($_SESSION['type'] === 'admin' || $_SESSION['type'] === 'bm' ? "".$row["AA_AGENT"]."" : "")." ".$kolkataTime." ".$row["AT_ACID"]." ".$row["AA_NAME"]." ".$row["AT_AMOUNT"]." - ".($_SESSION['type'] === 'admin' ? "".($row["REMARKS"] ?? '')."" : "")." + ".($_SESSION['type'] === 'admin' || $_SESSION['type'] === 'bm' ? "".($row["REMARKS"] ?? '')."" : "")." "; $totalAmount += $row["AT_AMOUNT"]; $rowsData[] = [$row["AT_ID"], $row["AT_TIMESTAMP"], $row["AT_ACID"], $row["AA_NAME"], $row["AT_AMOUNT"]]; diff --git a/CONTENT/ROOT_URI/Agent/commission.php b/CONTENT/ROOT_URI/Agent/commission.php index db9dbcc..6fbf778 100644 --- a/CONTENT/ROOT_URI/Agent/commission.php +++ b/CONTENT/ROOT_URI/Agent/commission.php @@ -5,14 +5,298 @@ $monthStart = date("Y-m-01"); $dFrom = $_GET['dFrom'] ?? $monthStart; $dTo = $_GET['dTo'] ?? $today; -// Get login user type -$loginType = $_SESSION['type'] ?? ''; -$isAgent = ($loginType === 'agent'); +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 '
+
+
Dual Commission Report: '.$dateFrom." → ".$dateTo.'
+ Showing both Account Opening Commission and Collection Commission +
+
'; + + $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 agents first ---- + $agentsSql = "SELECT user_id, user_name, comi_rate FROM `".$GLOBALS['arif_users']."` WHERE type = 'agent'"; + + // ---- If Agent then filter only themselves ---- + if ($loginType === 'agent') { + $agentsSql .= " AND user_id = '".$loginId."'"; + } + + $agentsResult = $conn->query($agentsSql); + $agentCommissions = []; + + if ($agentsResult && $agentsResult->num_rows > 0) { + while($agent = $agentsResult->fetch_assoc()) { + $agentId = $agent['user_id']; + + // Initialize agent data with collection commission + $collectionData = calculateCollectionCommission($agentId, $dateFrom, $dateTo); + + $agentCommissions[$agentId] = [ + 'agent_name' => $agent['user_name'], + 'opening_commission' => 0, + 'collection_commission' => $collectionData['collection_commission'], + 'total_collection' => $collectionData['total_collection'], + 'account_count' => 0, + 'comi_rate' => $collectionData['comi_rate'] + ]; + } + } + + // ---- Now get accounts created in the date range and add opening commission ---- + $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); + + 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'] + ); + + $commissionAgent = $account['refferTo']; + + // If agent exists in our list, add opening commission + if (isset($agentCommissions[$commissionAgent])) { + $agentCommissions[$commissionAgent]['opening_commission'] += $openingCommission; + $agentCommissions[$commissionAgent]['account_count']++; + } + } + } + + echo ' +
+
+
+
+ + + + + + + + + + + + + + '; + + $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 " + + + + + + + + + + "; + } + } else { + echo ""; + } + + echo ' + +
Agent IDAgent NameAccountsCollection AmountCollection RateAcc Opening CommissionCollection CommissionTotal Commission
".$agentId."".$agentData['agent_name']."".$agentData['account_count']."".number_format($agentData['total_collection'], 2)."".$agentData['comi_rate']."%".number_format($agentData['opening_commission'], 2)."".number_format($agentData['collection_commission'], 2)."".number_format($totalCommission, 2)."
No commission data found
'; + + // ---- Show grand totals ---- + if ($loginType !== 'agent' && !empty($agentCommissions)) { + echo '
+
+
+
+
Opening Commission
+

'.number_format($grandTotalOpening, 2).'

+
+
+
+
+
+
+
Collection Commission
+

'.number_format($grandTotalCollection, 2).'

+
+
+
+
+
+
+
Total Commission
+

'.number_format($grandTotalCommission, 2).'

+
+
+
+
'; + } 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 '
+
+
+
+
My Summary
+

Total Accounts: '.$agentData['account_count'].'

+

Total Collection: '.number_format($agentData['total_collection'], 2).'

+
+
+
+
+
+
+
My Total Commission
+

'.number_format($totalCommission, 2).'

+
+
+
+
'; + } + + echo ' +
+
+
+
'; + + $conn->close(); +} ?>
-

Commission Report

+

Dual Commission Report

@@ -22,126 +306,14 @@ $isAgent = ($loginType === 'agent');
-
- +
+
-
-
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); - - $loginId = $_SESSION['user_id'] ?? 0; - - // ---- Slab setup for ACC opening commission ---- - $daysDiff = (strtotime($dateTo) - strtotime($dateFrom)) / (60*60*24); - if ($daysDiff >= 300) { // 1 year - $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], - ]; - } 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], - ]; - } - - // ---- 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); - - echo ' -
-
-
-
- - - '; - - if (!$isAgent) echo ''; - echo ' - - '; - - $grandCollection = $grandSelfCom = $grandReferCom = 0; - - if ($result && $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; - - // ---- ACC Open 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 ""; - if (!$isAgent) echo ""; - echo " - - - - "; - } - } else { - $colspan = $isAgent ? 4 : 6; - echo ""; - } - - if (!$isAgent) { - echo " - - - - - - "; - } - - echo '
Agent IDAgent NameTotal CollectionCollection CommissionACC Open CommissionTotal Commission
{$agentId}{$row['user_name']}".number_format($collection,2)."".number_format($selfCom,2)."".number_format($referCom,2)."".number_format($totalCom,2)."
No agents found
Grand Total:".number_format($grandCollection,2)."".number_format($grandSelfCom,2)."".number_format($grandReferCom,2)."".number_format($grandSelfCom+$grandReferCom,2)."
'; - - $conn->close(); -} - -// ---- Call commission function ---- -commission_report($dFrom, $dTo, $isAgent); -?> +// ---- Call dual commission function ---- +dual_commission_report($dFrom, $dTo); +?> \ No newline at end of file