File size: 7,157 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
class AgentClaim {
    private $conn;
    private $table_name = "agent_claims";

    public $id;
    public $user_id;
    public $username;
    public $email;
    public $claim_type;
    public $amount;
    public $description;
    public $status;
    public $evidence_file;
    public $created_at;
    public $updated_at;
    public $approved_at;
    public $approved_by;
    public $rejection_reason;

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

    // Create new claim
    public function create() {
        try {
            $query = "INSERT INTO " . $this->table_name . " 
                     (user_id, username, email, claim_type, amount, description, evidence_file) 
                     VALUES (:user_id, :username, :email, :claim_type, :amount, :description, :evidence_file)";
            
            $stmt = $this->conn->prepare($query);
            
            // Sanitize inputs
            $this->user_id = htmlspecialchars(strip_tags($this->user_id));
            $this->username = htmlspecialchars(strip_tags($this->username));
            $this->email = htmlspecialchars(strip_tags($this->email));
            $this->claim_type = htmlspecialchars(strip_tags($this->claim_type));
            $this->amount = htmlspecialchars(strip_tags($this->amount));
            $this->description = htmlspecialchars(strip_tags($this->description));
            $this->evidence_file = htmlspecialchars(strip_tags($this->evidence_file));
            
            // Bind parameters
            $stmt->bindParam(":user_id", $this->user_id);
            $stmt->bindParam(":username", $this->username);
            $stmt->bindParam(":email", $this->email);
            $stmt->bindParam(":claim_type", $this->claim_type);
            $stmt->bindParam(":amount", $this->amount);
            $stmt->bindParam(":description", $this->description);
            $stmt->bindParam(":evidence_file", $this->evidence_file);
            
            if ($stmt->execute()) {
                return $this->conn->lastInsertId();
            }
            return false;
            
        } catch (PDOException $exception) {
            error_log("Create Claim Error: " . $exception->getMessage());
            return false;
        }
    }

    // Get claims by user ID
    public function getClaimsByUser($user_id, $status = null) {
        try {
            $query = "SELECT * FROM " . $this->table_name . " WHERE user_id = :user_id";
            
            if ($status) {
                $query .= " AND status = :status";
            }
            
            $query .= " ORDER BY created_at DESC";
            
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(":user_id", $user_id);
            
            if ($status) {
                $stmt->bindParam(":status", $status);
            }
            
            $stmt->execute();
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
            
        } catch (PDOException $exception) {
            error_log("Get Claims Error: " . $exception->getMessage());
            return [];
        }
    }

    // Get claim by ID
    public function getClaimById($id) {
        try {
            $query = "SELECT ac.*, u.full_name, u.phone 
                     FROM " . $this->table_name . " ac 
                     JOIN users u ON ac.user_id = u.id 
                     WHERE ac.id = :id";
            
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(":id", $id);
            $stmt->execute();
            
            return $stmt->fetch(PDO::FETCH_ASSOC);
            
        } catch (PDOException $exception) {
            error_log("Get Claim Error: " . $exception->getMessage());
            return false;
        }
    }

    // Update claim status
    public function updateStatus($id, $status, $approved_by = null, $rejection_reason = null) {
        try {
            $query = "UPDATE " . $this->table_name . " 
                     SET status = :status, 
                         updated_at = CURRENT_TIMESTAMP";
            
            if ($status == 'approved') {
                $query .= ", approved_at = CURRENT_TIMESTAMP, approved_by = :approved_by";
            }
            
            if ($status == 'rejected' && $rejection_reason) {
                $query .= ", rejection_reason = :rejection_reason";
            }
            
            $query .= " WHERE id = :id";
            
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(":status", $status);
            $stmt->bindParam(":id", $id);
            
            if ($status == 'approved') {
                $stmt->bindParam(":approved_by", $approved_by);
            }
            
            if ($status == 'rejected' && $rejection_reason) {
                $stmt->bindParam(":rejection_reason", $rejection_reason);
            }
            
            return $stmt->execute();
            
        } catch (PDOException $exception) {
            error_log("Update Status Error: " . $exception->getMessage());
            return false;
        }
    }

    // Get claim statistics for user
    public function getClaimStatistics($user_id) {
        try {
            $query = "SELECT 
                COUNT(*) as total_claims,
                SUM(CASE WHEN status = 'approved' THEN 1 ELSE 0 END) as approved_claims,
                SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending_claims,
                SUM(CASE WHEN status = 'rejected' THEN 1 ELSE 0 END) as rejected_claims,
                SUM(CASE WHEN status = 'processing' THEN 1 ELSE 0 END) as processing_claims,
                SUM(CASE WHEN status = 'approved' THEN amount ELSE 0 END) as approved_amount,
                SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) as pending_amount,
                SUM(CASE WHEN status = 'processing' THEN amount ELSE 0 END) as processing_amount,
                SUM(amount) as total_amount
            FROM " . $this->table_name . " 
            WHERE user_id = :user_id";
            
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(":user_id", $user_id);
            $stmt->execute();
            
            return $stmt->fetch(PDO::FETCH_ASSOC);
            
        } catch (PDOException $exception) {
            error_log("Statistics Error: " . $exception->getMessage());
            return [];
        }
    }

    // Check if user has pending claims
    public function hasPendingClaims($user_id) {
        try {
            $query = "SELECT COUNT(*) as pending_count 
                     FROM " . $this->table_name . " 
                     WHERE user_id = :user_id AND status = 'pending'";
            
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(":user_id", $user_id);
            $stmt->execute();
            
            $result = $stmt->fetch(PDO::FETCH_ASSOC);
            return $result['pending_count'] > 0;
            
        } catch (PDOException $exception) {
            error_log("Pending Check Error: " . $exception->getMessage());
            return false;
        }
    }
}
?>