diff --git a/CONTENT/ROOT_URI/Admin/agent_View_report.php b/CONTENT/ROOT_URI/Admin/agent_View_report.php index e026297..27800a1 100644 --- a/CONTENT/ROOT_URI/Admin/agent_View_report.php +++ b/CONTENT/ROOT_URI/Admin/agent_View_report.php @@ -1,11 +1,20 @@ +// ---- Form ---- +?>

Commission Report

@@ -25,94 +34,148 @@ $dTo = $_GET['dTo'] ?? $today;
- -
-
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); - // ---- Login user type check ---- - $loginType = $_SESSION['type'] ?? ''; // login session-এ type আছে - $loginId = $_SESSION['user_id']; // login user_id + $loginType = $_SESSION['type'] ?? 'admin'; + $loginId = $_SESSION['user_id'] ?? 0; - // ---- Base 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 (!$dateFrom || !$dateTo) { + echo "
Please select valid date range!
"; + 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') { - $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"; $result = $conn->query($sql); + if (!$result) { + echo "
SQL Error: {$conn->error}
"; + return; + } - echo ' -
-
-
-
- - - - - - - - - - '; + echo " +
+
+
Commission Report: {$dateFrom} → {$dateTo} ({$slabLabel})
+
+
+
+
+
Agent IDAgent NameTotal CollectionTotal Commission
+ + + + + + + + + + + + "; - $grandTotalCommission = 0; - $grandTotalCollection = 0; + $grandCollection = $grandSelfCom = $grandReferCom = 0; - if ($result && $result->num_rows > 0) { - while($row = $result->fetch_assoc()) { - $collection = $row["total_amount"]; - $commission = ($collection * $row["comi_rate"]) / 100; + 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; - $grandTotalCollection += $collection; - $grandTotalCommission += $commission; + // ---- 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 " - - - - - - "; + + + + + + + + "; } } else { - echo ""; + echo ""; } - echo ' - -
Agent IDAgent NameTotal CollectionCollection CommissionACC Open CommissionTotal Commission
".$row["user_id"]."".$row["user_name"]."".number_format($collection,2)."".number_format($commission,2)."
{$agentId}{$row['user_name']}".number_format($collection,2)."".number_format($selfCom,2)."".number_format($referCom,2)."".number_format($totalCom,2)."
No agents found
No data found
'; - - // ---- if admin/bm then show grand total ---- + // ---- Grand Total ---- if ($loginType !== 'agent') { - echo '
- Grand Total Collection : '.number_format($grandTotalCollection,2).'
- Grand Total Commission : '.number_format($grandTotalCommission,2).' -
'; + echo " + + Grand Total: + ".number_format($grandCollection,2)." + ".number_format($grandSelfCom,2)." + ".number_format($grandReferCom,2)." + ".number_format($grandSelfCom + $grandReferCom,2)." + "; } - echo ' -
-
-
- '; + echo " + + +
+ + + "; $conn->close(); } -// ---- Call commission function ---- +// ---- CALL FUNCTION ---- commission_report($dFrom, $dTo); ?> diff --git a/CONTENT/ROOT_URI/Agent/commission.php b/CONTENT/ROOT_URI/Agent/commission.php index ce342ac..5d37a01 100644 --- a/CONTENT/ROOT_URI/Agent/commission.php +++ b/CONTENT/ROOT_URI/Agent/commission.php @@ -1,7 +1,7 @@ connect_error) die("Connection failed: " . $conn->connect_error); - // ---- Login user type check ---- - $loginType = $_SESSION['type'] ?? ''; // login session-এ type আছে - $loginId = $_SESSION['user_id']; // login user_id + $loginId = $_SESSION['user_id'] ?? 0; - // ---- Base 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 ($isAgent) { - $sql .= " AND u.user_id = '".$loginId."'"; + // ---- 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], + ]; } - $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); echo ' @@ -65,67 +83,69 @@ function commission_report($dateFrom, $dateTo, $isAgent) { '; - // Only show Agent ID and Name columns if user is not an agent - if (!$isAgent) { - echo 'Agent ID - Agent Name'; - } - - echo 'Total Collection - Total Commission - + if (!$isAgent) echo 'Agent IDAgent Name'; + echo 'Total CollectionCollection CommissionACC Open CommissionTotal Commission '; - $grandTotalCommission = 0; - $grandTotalCollection = 0; + $grandCollection = $grandSelfCom = $grandReferCom = 0; if ($result && $result->num_rows > 0) { while($row = $result->fetch_assoc()) { - $collection = $row["total_amount"]; - $commission = ($collection * $row["comi_rate"]) / 100; + $agentId = $row['user_id']; + $collection = floatval($row['total_amount']); + $selfCom = ($collection * floatval($row["comi_rate"]))/100; - $grandTotalCollection += $collection; - $grandTotalCommission += $commission; + // ---- 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 ""; - - // Only show Agent ID and Name if user is not an agent - if (!$isAgent) { - echo "".$row["user_id"]." - ".$row["user_name"].""; - } - + if (!$isAgent) echo "{$agentId}{$row['user_name']}"; echo "".number_format($collection,2)." - ".number_format($commission,2)." - "; + ".number_format($selfCom,2)." + ".number_format($referCom,2)." + ".number_format($totalCom,2)." + "; } } else { - $colspan = $isAgent ? 2 : 4; + $colspan = $isAgent ? 4 : 6; echo "No agents found"; } - echo ' - - '; - - // ---- if admin/bm then show grand total ---- if (!$isAgent) { - echo '
- Grand Total Collection : '.number_format($grandTotalCollection,2).'
- Grand Total Commission : '.number_format($grandTotalCommission,2).' -
'; + echo " + Grand Total: + ".number_format($grandCollection,2)." + ".number_format($grandSelfCom,2)." + ".number_format($grandReferCom,2)." + ".number_format($grandSelfCom+$grandReferCom,2)." + "; } - echo ' - - - - '; + echo ''; $conn->close(); } // ---- Call commission function ---- commission_report($dFrom, $dTo, $isAgent); -?> \ No newline at end of file +?> diff --git a/CONTENT/ROOT_URI/exe/receive_amount/index.php b/CONTENT/ROOT_URI/exe/receive_amount/index.php index 753dec0..92b1d94 100644 --- a/CONTENT/ROOT_URI/exe/receive_amount/index.php +++ b/CONTENT/ROOT_URI/exe/receive_amount/index.php @@ -44,7 +44,6 @@ if (isset($_POST["add_i"]) && isset($_POST["AA_ACNO"]) && is_numeric($_POST["add echo json_encode($total); exit; } } - // 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'";