static-variables / jweb /ac1 /src /api /agent_claim.php
fellybikush's picture
Upload 99 files
0dff816 verified
raw
history blame
7.16 kB
<?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;
}
}
}
?>