update commission page
This commit is contained in:
@@ -115,7 +115,36 @@ function dual_commission_report($dateFrom, $dateTo) {
|
||||
$loginType = $_SESSION['type'] ?? '';
|
||||
$loginId = $_SESSION['user_id'] ?? '';
|
||||
|
||||
// ---- Get all accounts created in the date range ----
|
||||
// ---- 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 ----
|
||||
@@ -125,9 +154,6 @@ function dual_commission_report($dateFrom, $dateTo) {
|
||||
|
||||
$accountsResult = $conn->query($accountsSql);
|
||||
|
||||
// ---- Group commissions by agent ----
|
||||
$agentCommissions = [];
|
||||
|
||||
if ($accountsResult && $accountsResult->num_rows > 0) {
|
||||
while($account = $accountsResult->fetch_assoc()) {
|
||||
// Calculate OPENING commission
|
||||
@@ -140,57 +166,13 @@ function dual_commission_report($dateFrom, $dateTo) {
|
||||
$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 agent exists in our list, add opening commission
|
||||
if (isset($agentCommissions[$commissionAgent])) {
|
||||
$agentCommissions[$commissionAgent]['opening_commission'] += $openingCommission;
|
||||
$agentCommissions[$commissionAgent]['account_count']++;
|
||||
}
|
||||
|
||||
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']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user