|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
function getDBConnection() { |
|
|
static $conn; |
|
|
if (!$conn) { |
|
|
$host = '127.0.0.1'; |
|
|
$dbname = 'jmdb'; |
|
|
$username = 'root'; |
|
|
$password = 'YourStrongPassword123'; |
|
|
|
|
|
try { |
|
|
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password); |
|
|
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
|
} catch(PDOException $e) { |
|
|
die("Connection failed: " . $e->getMessage()); |
|
|
} |
|
|
} |
|
|
return $conn; |
|
|
} |
|
|
|
|
|
|
|
|
function getPendingAgents($sponsorId) { |
|
|
$conn = getDBConnection(); |
|
|
$stmt = $conn->prepare(" |
|
|
SELECT aa.*, u.username, u.email |
|
|
FROM agent_applications aa |
|
|
JOIN users u ON aa.user_id = u.id |
|
|
WHERE aa.sponsor_id = ? AND aa.status = 'pending' |
|
|
ORDER BY aa.applied_at DESC |
|
|
"); |
|
|
$stmt->execute([$sponsorId]); |
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
|
} |
|
|
|
|
|
|
|
|
function getAgentStats($sponsorId) { |
|
|
$conn = getDBConnection(); |
|
|
|
|
|
|
|
|
$stats = [ |
|
|
'pending' => 0, |
|
|
'approved' => 0, |
|
|
'rejected' => 0, |
|
|
'documents_needed' => 0, |
|
|
'total_applications' => 0, |
|
|
'commission_this_month' => 0.00, |
|
|
'total_commission' => 0.00 |
|
|
]; |
|
|
|
|
|
try { |
|
|
|
|
|
$stmt = $conn->prepare(" |
|
|
SELECT |
|
|
status, |
|
|
COUNT(*) as count |
|
|
FROM agent_applications |
|
|
WHERE sponsor_id = ? |
|
|
GROUP BY status |
|
|
"); |
|
|
$stmt->execute([$sponsorId]); |
|
|
$statusCounts = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
|
|
|
|
foreach ($statusCounts as $row) { |
|
|
$stats[$row['status']] = $row['count']; |
|
|
$stats['total_applications'] += $row['count']; |
|
|
} |
|
|
|
|
|
|
|
|
$stmt = $conn->prepare(" |
|
|
SELECT |
|
|
COALESCE(SUM(CASE WHEN MONTH(created_at) = MONTH(CURRENT_DATE()) THEN commission_amount ELSE 0 END), 0) as commission_this_month, |
|
|
COALESCE(SUM(commission_amount), 0) as total_commission |
|
|
FROM commissions |
|
|
WHERE sponsor_id = ? AND status = 'paid' |
|
|
"); |
|
|
$stmt->execute([$sponsorId]); |
|
|
$commissionData = $stmt->fetch(PDO::FETCH_ASSOC); |
|
|
|
|
|
if ($commissionData) { |
|
|
$stats['commission_this_month'] = $commissionData['commission_this_month']; |
|
|
$stats['total_commission'] = $commissionData['total_commission']; |
|
|
} |
|
|
} catch (Exception $e) { |
|
|
error_log("Error getting agent stats: " . $e->getMessage()); |
|
|
} |
|
|
|
|
|
return $stats; |
|
|
} |
|
|
|
|
|
|
|
|
function reviewAgentApplication($agentId, $adminId, $status, $notes = '') { |
|
|
$conn = getDBConnection(); |
|
|
|
|
|
try { |
|
|
$conn->beginTransaction(); |
|
|
|
|
|
|
|
|
$stmt = $conn->prepare(" |
|
|
UPDATE agent_applications |
|
|
SET status = ?, reviewed_by = ?, reviewed_at = NOW(), review_notes = ? |
|
|
WHERE id = ? |
|
|
"); |
|
|
$stmt->execute([$status, $adminId, $notes, $agentId]); |
|
|
|
|
|
|
|
|
if ($status === 'approved') { |
|
|
$stmt = $conn->prepare(" |
|
|
UPDATE users u |
|
|
JOIN agent_applications aa ON u.id = aa.user_id |
|
|
SET u.user_type = 'agent' |
|
|
WHERE aa.id = ? |
|
|
"); |
|
|
$stmt->execute([$agentId]); |
|
|
} |
|
|
|
|
|
$conn->commit(); |
|
|
return true; |
|
|
} catch (Exception $e) { |
|
|
$conn->rollBack(); |
|
|
error_log("Error reviewing agent: " . $e->getMessage()); |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function searchAgents($sponsorId, $searchTerm) { |
|
|
$conn = getDBConnection(); |
|
|
$searchTerm = "%$searchTerm%"; |
|
|
|
|
|
$stmt = $conn->prepare(" |
|
|
SELECT aa.*, u.username, u.email |
|
|
FROM agent_applications aa |
|
|
JOIN users u ON aa.user_id = u.id |
|
|
WHERE aa.sponsor_id = ? |
|
|
AND (aa.full_name LIKE ? OR aa.phone LIKE ? OR u.username LIKE ? OR aa.id LIKE ?) |
|
|
ORDER BY aa.applied_at DESC |
|
|
"); |
|
|
$stmt->execute([$sponsorId, $searchTerm, $searchTerm, $searchTerm, $searchTerm]); |
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
|
} |
|
|
|
|
|
|
|
|
function getAgentDetails($agentId) { |
|
|
$conn = getDBConnection(); |
|
|
$stmt = $conn->prepare(" |
|
|
SELECT aa.*, u.username, u.email, u.created_at as user_joined |
|
|
FROM agent_applications aa |
|
|
JOIN users u ON aa.user_id = u.id |
|
|
WHERE aa.id = ? |
|
|
"); |
|
|
$stmt->execute([$agentId]); |
|
|
return $stmt->fetch(PDO::FETCH_ASSOC); |
|
|
} |
|
|
?> |