File size: 4,061 Bytes
0dff816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
class User {
    private $conn;
    private $table_name = "users";

    public $id;
    public $username;
    public $email;
    public $tier;
    public $package;
    public $balance;
    public $total_deposits;
    public $total_withdrawals;
    public $rewards;
    public $referral_code;
    public $referred_by;

    public function __construct($db) {
        $this->conn = $db;
    }

    // Generate unique referral code
    private function generateReferralCode($username) {
        $base_code = strtoupper(substr(preg_replace('/[^a-zA-Z0-9]/', '', $username), 0, 3));
        $random_number = mt_rand(100, 999);
        return $base_code . $random_number;
    }

    // Get user by ID
    public function getUserById($user_id) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 1";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $user_id);
        $stmt->execute();
        
        if ($stmt->rowCount() > 0) {
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            $this->id = $row['id'];
            $this->username = $row['username'];
            $this->email = $row['email'];
            $this->tier = $row['tier'];
            $this->package = $row['package'];
            $this->balance = $row['balance'];
            $this->total_deposits = $row['total_deposits'];
            $this->total_withdrawals = $row['total_withdrawals'];
            $this->rewards = $row['rewards'];
            $this->referral_code = $row['referral_code'];
            $this->referred_by = $row['referred_by'];
            return true;
        }
        return false;
    }

    // Get user by referral code
    public function getUserByReferralCode($referral_code) {
        $query = "SELECT id, username FROM " . $this->table_name . " WHERE referral_code = ? LIMIT 1";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $referral_code);
        $stmt->execute();
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }

    // Create referral code for user
    public function createReferralCode($user_id) {
        $user = $this->getUserById($user_id);
        if (!$user) return false;

        if (empty($this->referral_code)) {
            $referral_code = $this->generateReferralCode($this->username);
            
            // Ensure uniqueness
            while ($this->getUserByReferralCode($referral_code)) {
                $referral_code = $this->generateReferralCode($this->username);
            }

            $query = "UPDATE " . $this->table_name . " SET referral_code = ? WHERE id = ?";
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(1, $referral_code);
            $stmt->bindParam(2, $user_id);
            
            if ($stmt->execute()) {
                $this->referral_code = $referral_code;
                return $referral_code;
            }
        }
        return $this->referral_code;
    }

    // Get team members
    public function getTeamMembers($user_id) {
        $query = "SELECT u.username, u.email, u.tier, u.created_at, r.status, r.commission_earned 
                  FROM users u 
                  JOIN referrals r ON u.id = r.referred_id 
                  WHERE r.referrer_id = ? 
                  ORDER BY u.created_at DESC";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $user_id);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    // Get team statistics
    public function getTeamStats($user_id) {
        $query = "SELECT 
                    COUNT(*) as total_members,
                    SUM(CASE WHEN DATE(r.created_at) = CURDATE() THEN 1 ELSE 0 END) as active_today,
                    COALESCE(SUM(r.commission_earned), 0) as team_earnings
                  FROM referrals r 
                  WHERE r.referrer_id = ? AND r.status = 'completed'";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $user_id);
        $stmt->execute();
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }
}
?>