1 | <?php
|
---|
2 | session_start();
|
---|
3 | require './connect.php';
|
---|
4 |
|
---|
5 | // Check if user is logged in
|
---|
6 | if (!isset($_SESSION['userid'])) {
|
---|
7 | echo json_encode(['success' => false, 'message' => 'User not logged in']);
|
---|
8 | exit;
|
---|
9 | }
|
---|
10 |
|
---|
11 | try {
|
---|
12 | // Begin transaction
|
---|
13 | $conn->beginTransaction();
|
---|
14 |
|
---|
15 | // Get all unpaid fines for the current user
|
---|
16 | $getFines = "SELECT fineid, fineamount
|
---|
17 | FROM fine
|
---|
18 | WHERE status = 'Unpaid'
|
---|
19 | AND loanid IN (SELECT loanid FROM loan WHERE memberid = :userid)";
|
---|
20 | $stmt = $conn->prepare($getFines);
|
---|
21 | $stmt->bindParam(':userid', $_SESSION['userid'], PDO::PARAM_INT);
|
---|
22 | $stmt->execute();
|
---|
23 | $unpaidFines = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
---|
24 |
|
---|
25 | if (empty($unpaidFines)) {
|
---|
26 | throw new Exception('No unpaid fines found');
|
---|
27 | }
|
---|
28 |
|
---|
29 | // Process each unpaid fine
|
---|
30 | foreach ($unpaidFines as $fine) {
|
---|
31 | // Insert payment record
|
---|
32 | $insertPayment = "INSERT INTO finepayment (paymentdate, paymentamount, fineid)
|
---|
33 | VALUES (CURRENT_TIMESTAMP, :amount, :fineid)";
|
---|
34 | $stmt = $conn->prepare($insertPayment);
|
---|
35 | $stmt->bindParam(':amount', $fine['fineamount'], PDO::PARAM_STR);
|
---|
36 | $stmt->bindParam(':fineid', $fine['fineid'], PDO::PARAM_INT);
|
---|
37 | $stmt->execute();
|
---|
38 |
|
---|
39 | // Update fine status
|
---|
40 | $updateFine = "UPDATE fine
|
---|
41 | SET status = 'Paid',
|
---|
42 | finedate = CURRENT_TIMESTAMP
|
---|
43 | WHERE fineid = :fineid";
|
---|
44 | $stmt = $conn->prepare($updateFine);
|
---|
45 | $stmt->bindParam(':fineid', $fine['fineid'], PDO::PARAM_INT);
|
---|
46 | $stmt->execute();
|
---|
47 | }
|
---|
48 |
|
---|
49 | // Commit transaction
|
---|
50 | $conn->commit();
|
---|
51 |
|
---|
52 | echo json_encode([
|
---|
53 | 'success' => true,
|
---|
54 | 'message' => 'All fines paid successfully'
|
---|
55 | ]);
|
---|
56 |
|
---|
57 | } catch (Exception $e) {
|
---|
58 | // Rollback transaction on error
|
---|
59 | $conn->rollBack();
|
---|
60 | echo json_encode([
|
---|
61 | 'success' => false,
|
---|
62 | 'message' => $e->getMessage()
|
---|
63 | ]);
|
---|
64 | }
|
---|
65 | ?> |
---|