Spaces:
Configuration error

File size: 2,722 Bytes
8df07b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
```php
<?php
include 'includes/auth.php';
redirectIfNotAdmin();
include 'config/database.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $claimID = $_POST['claimID'];
    $status = $_POST['status'];
    $notes = $_POST['notes'] ?? '';
    $adminId = $_SESSION['admin_id'];
    
    $db = new Database();
    $connection = $db->getConnection();
    
    try {
        $connection->beginTransaction();
        
        // Update claim status
        $updateQuery = "UPDATE claims SET status = :status WHERE claimId = :claimID";
        $updateStmt = $connection->prepare($updateQuery);
        $updateStmt->bindParam(':status', $status);
        $updateStmt->bindParam(':claimID', $claimID);
        $updateStmt->execute();
        
        // Get claim details for notifications
        $detailsQuery = "
            SELECT c.*, i.description, i.itemID, u.userId, u.name as ownerName
            FROM claims c
            JOIN items i ON c.itemID = i.itemID
            JOIN users u ON c.ownerUserId = u.userId
            WHERE c.claimId = :claimID
        ";
        $detailsStmt = $connection->prepare($detailsQuery);
        $detailsStmt->bindParam(':claimID', $claimID);
        $detailsStmt->execute();
        $details = $detailsStmt->fetch(PDO::FETCH_ASSOC);
        
        // Update item status based on claim result
        $itemStatus = $status == 'approved' ? 'returned' : 'available';
        $itemQuery = "UPDATE items SET status = :status WHERE itemID = :itemID";
        $itemStmt = $connection->prepare($itemQuery);
        $itemStmt->bindParam(':status', $itemStatus);
        $itemStmt->bindParam(':itemID', $details['itemID']);
        $itemStmt->execute();
        
        // Create notification for owner
        $message = $status == 'approved' 
            ? "Your claim for '{$details['description']}' has been approved! The item will be returned to you."
            : "Your claim for '{$details['description']}' has been rejected. Reason: $notes";
        
        $notificationQuery = "INSERT INTO notifications (userId, message) VALUES (:userId, :message)";
        $notificationStmt = $connection->prepare($notificationQuery);
        $notificationStmt->bindParam(':userId', $details['userId']);
        $notificationStmt->bindParam(':message', $message);
        $notificationStmt->execute();
        
        $connection->commit();
        $_SESSION['success'] = "Claim processed successfully!";
        
    } catch (Exception $e) {
        $connection->rollBack();
        $_SESSION['error'] = "Error processing claim: " . $e->getMessage();
    }
    
    header("Location: verifications.php");
    exit();
} else {
    header("Location: verifications.php");
    exit();
}
?>
```