1 | <?php
|
---|
2 |
|
---|
3 | session_start();
|
---|
4 | require './connect.php';
|
---|
5 |
|
---|
6 | if (!isset($_SESSION['userid'])) {
|
---|
7 | header("Location: ./Sign&Log.php");
|
---|
8 | die();
|
---|
9 | }
|
---|
10 |
|
---|
11 | try {
|
---|
12 | $conn->beginTransaction();
|
---|
13 |
|
---|
14 | //π_{users.*, member.memberid} (σ_{users.userid = :userid } (users ⨝ member))
|
---|
15 | $stmt = $conn->prepare("SELECT users.*, member.memberid
|
---|
16 | FROM users
|
---|
17 | JOIN member ON users.userid = member.userid
|
---|
18 | WHERE users.userid = :userid");
|
---|
19 | $stmt->bindParam(':userid', $_SESSION['userid'], PDO::PARAM_INT);
|
---|
20 | $stmt->execute();
|
---|
21 |
|
---|
22 | if ($stmt->rowCount() <= 0) {
|
---|
23 | header("Location: ./EditProfile.php");
|
---|
24 | exit();
|
---|
25 | }
|
---|
26 |
|
---|
27 | $personal_data = $stmt->fetch(PDO::FETCH_ASSOC);
|
---|
28 | $memberid = $personal_data['memberid']; // Get memberid here
|
---|
29 |
|
---|
30 | // items from cart
|
---|
31 | //σ_{memberid = :memberid } (cart)
|
---|
32 | $cart_query = $conn->prepare("SELECT * FROM cart WHERE memberid = :memberid");
|
---|
33 | $cart_query->bindParam(':memberid', $memberid);
|
---|
34 | $cart_query->execute();
|
---|
35 | $order_items = $cart_query->fetchAll(PDO::FETCH_ASSOC);
|
---|
36 |
|
---|
37 | if (empty($order_items)) {
|
---|
38 | echo "Cart is empty";
|
---|
39 | exit();
|
---|
40 | }
|
---|
41 |
|
---|
42 | // into loan table
|
---|
43 | //π_{copyid} (σ_{bookid = :bookid ∧ (condition = 'Good' ∨ condition = 'New')} (book_copies - π_{copyid} (σ_{status IN ('On Time', 'Soon', 'Overdue')} (loan)))
|
---|
44 | $loan_stmt = $conn->prepare("INSERT INTO loan (loandate, returndate, status, memberid, bookcopyid)
|
---|
45 | VALUES (CURRENT_DATE, :return_date, :status, :memberid, :bookcopyid)");
|
---|
46 |
|
---|
47 | $returnDate = date('Y-m-d', strtotime("+14 days"));
|
---|
48 | $status = 'On Time';
|
---|
49 |
|
---|
50 | foreach ($order_items as $item) {
|
---|
51 | // Find available copy
|
---|
52 | $copy_stmt = $conn->prepare("SELECT copyid
|
---|
53 | FROM book_copies
|
---|
54 | WHERE bookid = :bookid
|
---|
55 | AND (condition = 'Good' OR condition = 'New')
|
---|
56 | AND copyid NOT IN (
|
---|
57 | SELECT bookcopyid
|
---|
58 | FROM loan
|
---|
59 | WHERE status IN ('On Time', 'Soon', 'Overdue')
|
---|
60 | )
|
---|
61 | LIMIT 1");
|
---|
62 |
|
---|
63 | $copy_stmt->bindParam(':bookid', $item['bookid']);
|
---|
64 | $copy_stmt->execute();
|
---|
65 |
|
---|
66 | if ($copy = $copy_stmt->fetch(PDO::FETCH_ASSOC)) {
|
---|
67 | // loan record
|
---|
68 | $loan_stmt->execute([
|
---|
69 | ':return_date' => $returnDate,
|
---|
70 | ':status' => $status,
|
---|
71 | ':memberid' => $memberid,
|
---|
72 | ':bookcopyid' => $copy['copyid']
|
---|
73 | ]);
|
---|
74 |
|
---|
75 | //echo "Loan created for book copy: " . $copy['copyid'] . "<br>";
|
---|
76 | } else {
|
---|
77 | echo "No available copy for book ID: " . $item['bookid'] . "<br>";
|
---|
78 | $conn->rollBack();
|
---|
79 | exit();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | // Clear cart
|
---|
84 | $clear_cart = $conn->prepare("DELETE FROM cart WHERE memberid = :memberid");
|
---|
85 | $clear_cart->execute([':memberid' => $memberid]);
|
---|
86 |
|
---|
87 | $conn->commit();
|
---|
88 | //echo "All loans created successfully!";
|
---|
89 | header("Location: ./Profile.php");
|
---|
90 |
|
---|
91 | } catch (PDOException $e) {
|
---|
92 | $conn->rollBack();
|
---|
93 | echo "Error: " . $e->getMessage();
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | ?>
|
---|