-
Notifications
You must be signed in to change notification settings - Fork 0
/
reserve_intro.py
316 lines (276 loc) · 12.6 KB
/
reserve_intro.py
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import tkinter as tk
from tkinter import messagebox
import pymysql
from datetime import date
import datetime
con = pymysql.connect(
host='localhost',
user='root',
password='xuan1234', #CHANGE THIS
database='library'
)
class reserve_window:
def __init__(self, root):
self.root = root
self.root.title("Reservations")
tk.Label(self.root, text="Select one of the options below: ").grid(row=0,columnspan=2)
tk.Label(self.root, text="Book reservation").grid(row=1,column=0)
tk.Label(self.root, text="Book reservation cancellation").grid(row=2,column=0)
btn_reserve = tk.Button(self.root, text="Reservations", command=self.reserve_page)
btn_reserve.grid(row=1, column=1)
btn_reservation_cancellation = tk.Button(self.root, text="Reservation Cancellations", command=self.reservation_cancellation_page)
btn_reservation_cancellation.grid(row=2, column=1)
btn_return_to_menu = tk.Button(self.root, text="Back to main menu", width=15, height=2, command=lambda: self.root.destroy())
btn_return_to_menu.grid(row=3, columnspan=2)
def reservation_cancellation_page(self):
self.new_window = tk.Toplevel(self.root)
self.app = reservation_cancellation_intro(self.new_window)
def reserve_page(self):
self.new_window = tk.Toplevel(self.root)
self.app = reserve_intro(self.new_window)
class reserve_intro:
def __init__(self, root):
self.root = root
self.root.title("Reservation")
start = tk.Label(self.root, text="To Reserve a Book, Please Enter Information Below:")
start.grid(row=0, columnspan=2)
tk.Label(self.root, text='Accession Number').grid(row=1, column=0, pady=5)
tk.Label(self.root, text='Membership ID').grid(row=2, column=0, pady=5)
self.acc_num = tk.Entry(self.root, width=30)
self.acc_num.insert(0, "Used to identify an instance of book")
self.acc_num.grid(row=1, column=1)
self.mem_id = tk.Entry(self.root, width=30)
self.mem_id.insert(0, "A unique alphanumeric id that distinguishes every member")
self.mem_id.grid(row=2, column=1)
reserve_btn = tk.Button(self.root, text="Reserve book", padx=20, pady=10, command=lambda: self.reserve())
reserve_btn.grid(row=3, column=0)
btn_return_to_menu = tk.Button(self.root, text="Back to main menu", padx=20, pady=10,
command=lambda: self.root.destroy())
btn_return_to_menu.grid(row=3, column=1)
def reserve(self):
acc_num = self.acc_num.get()
con = pymysql.connect(
host='localhost',
user='root',
password='xuan1234', # CHANGE THIS
database='library'
)
with con:
with con.cursor() as cursor:
try:
t = 'SELECT * FROM library.book WHERE accession_number = %s; '
cursor.execute(t, acc_num)
result = cursor.fetchone()
print(result)
self.new_window = tk.Toplevel(self.root)
self.app = temp_reserve(self.new_window, result, self.mem_id.get())
con.commit()
except:
messagebox.showwarning("showwarning", "Book does not exist!")
return
class temp_reserve:
def __init__(self, root, result, mem):
self.root = root
self.root.title("Confirmation")
tk.Label(self.root, text="Please confirm reservation details to be correct: ").grid(row=0, columnspan=2)
self.result = result
self.mem_id = mem
for i in range(4):
tk.Label(self.root, text=f"{self.result[i]}").grid(row=i + 1,columnspan = 2)
confirm_btn = tk.Button(self.root, text="Confirm Reserve", padx=20, pady=10, command=lambda: self.confirm())
confirm_btn.grid(row=6, column=0)
back_btn = tk.Button(self.root, text="Back to reservation function", padx=20, pady=10,
command=lambda: self.root.destroy())
back_btn.grid(row=6, column=1)
def confirm(self):
con = pymysql.connect(
host='localhost',
user='root',
password='xuan1234', # CHANGE THIS
database='library'
)
with con:
with con.cursor() as cursor:
# check if book is on loan
# if book is on loan, give member the option to reserve
# can only reserve a maximum of 2 books
if self.check_quota(self.mem_id):
messagebox.showwarning("showwarning", "ERROR! \n Member reservation quota exceeded.")
self.root.destroy()
return
# can only reserve books if there is no outstanding fines
if self.check_fines(self.mem_id):
messagebox.showwarning("showwarning", "ERROR! \n Member has outstanding fines.")
self.root.destroy()
return
# can only be reserved if the book is not reserved by someone else
if self.check_reserved(self.result[0], self.mem_id):
messagebox.showwarning("showwarning", "ERROR! \n Book currently reserved by someone else.")
self.root.destroy()
return
curr_date = date.today()
t = f'INSERT INTO library.reservation (`accession_number`, `membership_id`, `reserved_date`) VALUES ("{self.result[0]}", "{self.mem_id}", "{curr_date}"); '
cursor.execute(t)
con.commit()
messagebox.showinfo("showinfo", "Book reserved")
self.root.destroy()
# correct
def check_quota(self, mem_id):
con = pymysql.connect(
host='localhost',
user='root',
password='xuan1234', # CHANGE THIS
database='library'
)
with con:
with con.cursor() as cursor:
try:
t = 'SELECT accession_number FROM library.reservation WHERE membership_id = %s; '
cursor.execute(t, mem_id)
result = cursor.fetchall()
print(len(result))
if len(result) >= 2:
return True
else:
return False
except:
return False
# correct
def check_fines(self,mem_id):
con = pymysql.connect(
host='localhost',
user='root',
password='xuan1234', #CHANGE THIS
database='library'
)
with con:
with con.cursor() as cursor:
try:
t = 'SELECT * FROM library.fine WHERE membership_id_fined = %s; '
cursor.execute(t, mem_id)
result = cursor.fetchone()
print(result)
print("here")
if result[2]:
return True
else:
pass
t2 = f'SELECT * FROM library.book WHERE membership_id_borrowed = "{mem_id}" AND due_date < {date.today()} ; '
cursor.execute(t2)
result2 = cursor.fetchall()
print(result2)
print("here2")
if result2:
return True
else:
return False
except:
return False
# correct
def check_reserved(self, acc_num, mem_id):
con = pymysql.connect(
host='localhost',
user='root',
password='xuan1234', # CHANGE THIS
database='library'
)
with con:
with con.cursor() as cursor:
try:
t = 'SELECT * FROM library.reservation WHERE accession_number = %s '
cursor.execute(t, acc_num)
result = cursor.fetchone()
if result[1] != mem_id:
return True
else:
return False
except:
return False
class reservation_cancellation_intro:
def __init__(self, root):
self.root = root
self.root.title("Reservation Cancellation")
start = tk.Label(self.root, text="To Cancel Your Reservation for a Book, Please Enter Information Below:")
start.grid(row=0, columnspan=2)
tk.Label(self.root, text='Accession Number').grid(row=1, column=0, pady=5)
tk.Label(self.root, text='Membership ID').grid(row=2, column=0, pady=5)
self.acc_num = tk.Entry(self.root, width=30)
self.acc_num.insert(0, "Used to identify an instance of book")
self.acc_num.grid(row=1, column=1)
self.mem_id = tk.Entry(self.root, width=30)
self.mem_id.insert(0, "A unique alphanumeric id that distinguishes every member")
self.mem_id.grid(row=2, column=1)
reserve_btn = tk.Button(self.root, text="Cancel Book Reservation", padx=20, pady=10,
command=lambda: self.reserve_cancellation())
reserve_btn.grid(row=3, column=0)
btn_return_to_menu = tk.Button(self.root, text="Back to main menu", padx=20, pady=10,
command=lambda: self.root.destroy())
btn_return_to_menu.grid(row=3, column=1)
def reserve_cancellation(self):
acc_num = self.acc_num.get()
con = pymysql.connect(
host='localhost',
user='root',
password='xuan1234', # CHANGE THIS
database='library'
)
with con:
with con.cursor() as cursor:
try:
t = 'SELECT * FROM library.reservation WHERE accession_number = %s; '
cursor.execute(t, acc_num)
result = cursor.fetchone()
self.new_window = tk.Toplevel(self.root)
self.app = temp_reserve_cancellation(self.new_window, result, self.mem_id.get())
print(result)
except:
messagebox.showwarning("showwarning", "Book reservation does not exist!")
return
class temp_reserve_cancellation:
def __init__(self, root, result, mem):
self.root = root
self.root.title("Confirmation")
tk.Label(self.root, text="Please confirm reservation cancellation details to be correct: ").grid(row=0,columnspan=2)
self.result = result
self.mem_id = mem
con = pymysql.connect(
host='localhost',
user='root',
password='xuan1234',
database='library'
)
with con:
with con.cursor() as cursor:
t1 = f'SELECT title FROM library.book WHERE accession_number = "{self.result[0]}"; '
cursor.execute(t1)
title = cursor.fetchone()[0]
t2 = f'SELECT name FROM library.membership WHERE membership_id = "{self.result[1]}"; '
cursor.execute(t2)
name = cursor.fetchone()[0]
final = (self.result[0], title, self.result[1], name, date.today())
for i in range(len(final)):
tk.Label(self.root, text= f"{final[i]}").grid(row=i+1,columnspan=2)
confirm_btn = tk.Button(self.root, text="Confirm Reservation cancellation", padx=20, pady=10,
command=lambda: self.confirm(self.result[0],self.result[1]))
confirm_btn.grid(row=6, column=0)
back_btn = tk.Button(self.root, text="Back to reservation cancellation function", padx=20, pady=10,
command=lambda: self.root.destroy())
back_btn.grid(row=6, column=1)
def confirm(self, acc_num, mem_id):
con = pymysql.connect(
host='localhost',
user='root',
password='xuan1234', # CHANGE THIS
database='library'
)
with con:
with con.cursor() as cursor:
try:
t = 'DELETE FROM library.reservation WHERE membership_id = %s AND accession_number = %s '
cursor.execute(t, (mem_id, acc_num))
con.commit()
messagebox.showinfo("showinfo", "Book reservation deleted")
self.root.destroy()
except:
messagebox.showwarning("showwarning", "Book reservation does not exist!")
return