/
UI.py
480 lines (369 loc) · 20.9 KB
/
UI.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
from PyQt5 import QtWidgets, QtCore, uic
from PyQt5.QtWidgets import QDialog, QMessageBox
from PyQt5.QtGui import QIcon, QColor
from PyQt5.uic import uiparser
from datetime import datetime, date, timedelta
from Customer import Customer
import platform
import DB
import os
import json
dictionary = {}
with open('./files/data/dictionary.json', 'r', encoding="utf8") as file:
dictionary = json.load(file)
roomDictionary = {}
with open('./files/data/roomDictionary.json', 'r', encoding="utf8") as file:
roomDictionary = json.load(file)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, months):
super(MainWindow, self).__init__()
# Load the main UI file
uic.loadUi('./files/UI/MainWindow.ui', self)
self.months = months
self.ConnectLogicToObjects()
self.UpdateTableData()
# Adding pointers to all the objects of the UI
def ConnectLogicToObjects(self):
self.centralWidget().layout().setContentsMargins(10, 10, 10, 10)
self.tableWidget = self.findChild(QtWidgets.QTableWidget, 'tableWidget')
self.tableWidget.cellDoubleClicked.connect(self.cellDoubleClicked)
self.SetTableStyle()
self.monthSelection = self.findChild(QtWidgets.QComboBox, 'monthSelection')
self.monthSelection.setCurrentIndex(datetime.today().month - 1)
self.currentMonth = datetime.today().month - 1
self.yearSelection = self.findChild(QtWidgets.QDateEdit, 'yearSelection')
self.yearSelection.setDate(datetime.today())
self.showButton = self.findChild(QtWidgets.QPushButton, 'showButton')
self.showButton.clicked.connect(self.UpdateTableData)
self.addBooking = self.findChild(QtWidgets.QPushButton, 'addBooking')
self.addBooking.clicked.connect(self.AddBookingClicked)
self.deleteBooking = self.findChild(QtWidgets.QPushButton, 'deleteBooking')
self.deleteBooking.clicked.connect(self.DeleteBookingPressed)
self.roomTypeSelection = self.findChild(QtWidgets.QComboBox, 'roomTypeSelection')
self.roomTypeSelection.addItems(roomDictionary.values())
self.addRoom = self.findChild(QtWidgets.QAction, 'addRoom')
self.addRoom.triggered.connect(self.addRoomPressed)
self.removeRoom = self.findChild(QtWidgets.QAction, 'removeRoom')
self.removeRoom.triggered.connect(self.removeRoomPressed)
def SetTableStyle(self):
OSVersion = f"{platform.system()} {platform.release()}"
if OSVersion == "Windows 10":
self.tableWidget.setStyleSheet("QHeaderView::section{"
"border-top:0px solid #c8c8c8;"
"border-left:0px solid #c8c8c8;"
"border-right:2px solid #c8c8c8;"
"border-bottom: 2px solid #c8c8c8;"
"background-color:white;"
"padding:4px;"
"}"
"QTableCornerButton::section{"
"border-top:0px solid #c8c8c8;"
"border-left:0px solid #c8c8c8;"
"border-right:2px solid #c8c8c8;"
"border-bottom: 2px solid #c8c8c8;"
"background-color:white;"
"}")
def UpdateTableData(self):
# Create connection to database
conn = DB.CreateConnection()
# get the customer data for the current month
data = DB.GetCustomersByMonth(conn, self.monthSelection.currentIndex() + 1, self.yearSelection.date().year(), self.roomTypeSelection.currentIndex())
# get the rooms based on the room type
rooms = DB.GetRoomsByType(conn, self.roomTypeSelection.currentIndex())
#
if self.roomTypeSelection.currentIndex() == 0:
rowNumber = DB.GetRoomNumber(conn)
else:
rowNumber = DB.GetRoomNumber(conn, self.roomTypeSelection.currentIndex())
conn.close()
currentRow = self.tableWidget.currentRow()
currentColumn = self.tableWidget.currentColumn()
# set up the table rows
try:
# set the number of rows based on the selected roomtype
self.tableWidget.setRowCount(0)
for item in rooms:
rowPosition = self.tableWidget.rowCount()
self.tableWidget.insertRow(rowPosition)
self.tableWidget.setVerticalHeaderItem(rowPosition, QtWidgets.QTableWidgetItem(str(item)))
self.tableWidget.setCurrentCell(currentRow, currentColumn)
# set the number of columns based on the selected month
if self.currentMonth != (self.monthSelection.currentIndex() + 1):
self.tableWidget.setColumnCount(0)
self.tableWidget.setColumnCount(self.months[f'{self.monthSelection.currentIndex() + 1}'])
self.currentMonth = self.monthSelection.currentIndex() + 1
for item in data:
try:
row = rooms.index(item.RoomID) # find the row by searching the room list
column = item.CheckIn.day - 1 # starting column is the check in day
span = item.NumberOfStayNights # how many cells to merge based on the stay days
if item.CheckIn.month < (self.monthSelection.currentIndex() + 1): # if the CheckIn date is on a previous month calculate difference
column = 0
delta = -(item.CheckIn - date(self.yearSelection.date().year(), self.monthSelection.currentIndex() + 1, 1))
span = item.NumberOfStayNights - delta.days + 1 # set span to difference
self.tableWidget.setItem(row, column, QtWidgets.QTableWidgetItem(f'"{item.Name}" Άτομα: {item.People} Τιμή ανά βράδυ: {item.PricePerNight}'))
temp = self.tableWidget.item(row, column) # access the item just created
temp.setBackground(QColor(dictionary[f"{item.BookingType}"][1][0], dictionary[f"{item.BookingType}"][1][1], dictionary[f"{item.BookingType}"][1][2], alpha=150)) # set the background color of the item based on the dictionary
temp.setData(1, item.CustomerID) # set the metadata of the item to the CustomerID
self.tableWidget.setSpan(row, column, 1, span) # merge the cells
except ValueError:
pass
del data # clean up memory by deleting the customer data from memory
except TypeError:
return
def cellDoubleClicked(self, row, column):
try:
conn = DB.CreateConnection()
CustomerInfo = DB.GetCustomerByID(conn, self.tableWidget.item(row, column).data(1))
conn.close()
window = CustomerInfoWindow(CustomerInfo)
edited = window.exec___()
if edited:
conn = DB.CreateConnection()
rooms = DB.GetRoomsByType(conn, self.roomTypeSelection.currentIndex())
conn.close()
self.UpdateTableData()
except AttributeError:
return
def AddBookingClicked(self):
window = CustomerDataWindow('Δεδομένα Κράτησης')
data = window.GetData()
if data:
conn = DB.CreateConnection()
occupiedDates = DB.GetRoomOccupiedDates(conn, data.RoomID, data.CheckIn.year)
for item in occupiedDates:
if not (((data.CheckIn - item[0]).days < 0 and (data.CheckOut - item[0]).days <= 0) or ((data.CheckIn - item[1]).days >= 0 and (data.CheckOut - item[1]).days > 0)):
conn.close()
MessageBox('Σφάλμα', f'<p style="text-align:center;font-size:18px"><b>Ουπς...</p><p style="font-size:18px">Οι ημερομηνίες <b>"{data.CheckIn} - {data.CheckOut}"</b> συμπίπτουν με άλλη κράτηση.</p>', QMessageBox.Ok)
return
DB.AddCustomer(conn, data)
rooms = DB.GetRoomsByType(conn, self.roomTypeSelection.currentIndex())
conn.close()
self.UpdateTableData()
def DeleteBookingPressed(self):
try:
item = self.tableWidget.currentItem()
text = f"Είστε σίγουροι ότι θέλετε να διαγράψετε την κράτηση;\n\n{item.text()}"
test = MessageBox("Προσοχή", text)
if test == QMessageBox.Yes:
conn = DB.CreateConnection()
DB.DeleteCustomer(conn, item.data(1))
conn.close()
self.UpdateTableData()
except AttributeError as e:
return
def addRoomPressed(self):
window = AddRoomWindow()
temp = window.exec__()
if temp:
self.UpdateTableData()
def removeRoomPressed(self):
window = RemoveRoomWindow()
temp = window.exec__()
if temp:
self.UpdateTableData()
def closeEvent(self, event):
close = MessageBox()
if close == QMessageBox.Yes:
event.accept()
else:
event.ignore()
class CustomerInfoWindow(QtWidgets.QDialog):
def __init__(self, customerInfo):
super(CustomerInfoWindow, self).__init__()
# Load the main UI file
uic.loadUi('./files/UI/CustomerInfo.ui', self)
self.setWindowFlags(QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint)
self.edited = None
self.ConnectLogicToObjects(customerInfo)
def ConnectLogicToObjects(self, customerInfo):
self.textLabel = self.findChild(QtWidgets.QLabel, 'textLabel')
self.SetLabelText(customerInfo)
self.OKButton = self.findChild(QtWidgets.QPushButton, 'OKButton')
self.OKButton.clicked.connect(self.OKClicked)
self.editButton = self.findChild(QtWidgets.QPushButton, 'editButton')
self.editButton.clicked.connect(lambda EditInfo: self.EditInfo(customerInfo))
def SetLabelText(self, customerInfo):
text = f"""<p><b>Όνομα κράτησης:</b> {customerInfo.Name}</p>
<p> <b>Αριθμός ατόμων:</b> {customerInfo.People}</p>
<p> <b>Check in:</b> {customerInfo.CheckIn}</p>
<p> <b>Check out:</b> {customerInfo.CheckOut}</p>
<p> <b>Διανυκτερεύσεις:</b> {customerInfo.NumberOfStayNights}</p>
<p> <b>Τιμή ανά βράδυ:</b> {customerInfo.PricePerNight}€</p>
<p> <b>Σύνολο:</b> {customerInfo.TotalPrice}€</p>
<p> <b>Δωμάτιο:</b> {customerInfo.RoomID} ({self.GetRoomType(customerInfo.RoomID)})</p>
<p> <b>Κράτηση από:</b> {dictionary[f"{customerInfo.BookingType}"][0]}</p>
<p> <b>Σχόλια:</b> {customerInfo.Comments}</p>"""
self.textLabel.setText(text)
def EditInfo(self, customerInfo):
window = CustomerDataWindow(customerInfo=customerInfo)
data = window.GetData()
try:
if data != customerInfo: # if the data from the edit window are different from before, update the database and set the edited flag to 1
conn = DB.CreateConnection()
# Check if the new date is equal or within the old date, if it isn't go into the loop
if not ((data.CheckIn >= customerInfo.CheckIn) and (data.CheckOut <= customerInfo.CheckOut)):
occupiedDates = DB.GetRoomOccupiedDates(conn, data.RoomID, data.CheckIn.year, [customerInfo.CheckIn, customerInfo.CheckOut])
for item in occupiedDates:
# Check if the new date is between the occupied dates
if not (((data.CheckIn - item[0]).days < 0 and (data.CheckOut - item[0]).days < 0) or ((data.CheckIn - item[1]).days >= 0 and (data.CheckOut - item[1]).days >= 0)):
# print(occupiedDates)
conn.close()
MessageBox('Σφάλμα', f'<p style="text-align:center;font-size:18px"><b>Ουπς...</p><p style="font-size:18px">Οι ημερομηνίες <b>"{data.CheckIn} - {data.CheckOut}"</b> συμπίπτουν με άλλη κράτηση.</p>', QMessageBox.Ok)
del occupiedDates
return
del occupiedDates
self.SetLabelText(data)
DB.UpdateCustomer(conn, customerInfo.CustomerID, data)
conn.close()
self.edited = 1
except AttributeError as e:
return
def OKClicked(self):
self.close()
def GetRoomType(self, roomID):
conn = DB.CreateConnection()
a = roomDictionary[f'{DB.GetRoomType(conn, roomID)}']
conn.close()
return a
def exec___(self):
temp = self.exec_()
return self.edited
class CustomerDataWindow(QtWidgets.QDialog):
def __init__(self, title='Δεδομένα πελάτη', customerInfo=None):
super(CustomerDataWindow, self).__init__()
# Load the main UI file
uic.loadUi('./files/UI/CustomerData.ui', self)
self.setWindowFlags(QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint)
self.setWindowTitle(title)
self.ConnectLogicToObjects()
if customerInfo:
self.InitializeData(customerInfo)
def ConnectLogicToObjects(self):
self.bookingTypeInput = self.findChild(QtWidgets.QComboBox, 'bookingTypeInput')
data = []
for item in dictionary.values():
data.append(item[0])
self.bookingTypeInput.addItems(data)
self.checkInInput = self.findChild(QtWidgets.QDateEdit, 'checkInInput')
self.checkInInput.setDate(datetime.today())
self.checkInInput.dateChanged.connect(self.CheckInChanged)
self.checkOutInput = self.findChild(QtWidgets.QDateEdit, 'checkOutInput')
self.checkOutInput.setDate(datetime.today() + timedelta(5))
self.newDate = QtCore.QDate.currentDate()
self.checkOutInput.dateChanged.connect(self.CheckOutChanged)
self.commentInput = self.findChild(QtWidgets.QTextEdit, 'commentInput')
self.nameinput = self.findChild(QtWidgets.QLineEdit, 'nameInput')
self.peopleInput = self.findChild(QtWidgets.QSpinBox, 'peopleInput')
self.pricePerNightInput = self.findChild(QtWidgets.QDoubleSpinBox, 'pricePerNightInput')
self.roomIDInput = self.findChild(QtWidgets.QSpinBox, 'roomIDInput')
def InitializeData(self, customerInfo):
self.bookingTypeInput.setCurrentIndex(customerInfo.BookingType - 1)
self.checkInInput.setDate(customerInfo.CheckIn)
self.checkOutInput.setDate(customerInfo.CheckOut)
self.commentInput.setText(customerInfo.Comments)
self.nameinput.setText(customerInfo.Name)
self.peopleInput.setValue(customerInfo.People)
self.pricePerNightInput.setValue(customerInfo.PricePerNight)
self.roomIDInput.setValue(customerInfo.RoomID)
def GetTypeList(self):
temp = roomDictionary.values()
itemList = []
for item in temp:
itemList.append(item[0])
return itemList
def CheckInChanged(self, date):
self.checkOutInput.setDate(date.addDays(5))
def CheckOutChanged(self):
if self.newDate <= self.checkInInput.date():
self.newDate = self.checkInInput.date().addDays(5)
self.checkOutInput.setDate(self.checkInInput.date().addDays(5))
MessageBox(title='Προσοχή', text='<p style="text-align:left;font-size:18px"><b>Ο αριθμός διανυκτερεύσεων δεν μπορεί να είναι μικρότερος απο 1', buttons=QMessageBox.Ok)
return
def GetData(self):
if self.exec_() == QDialog.Accepted:
delta = self.checkOutInput.date().toPyDate() - self.checkInInput.date().toPyDate()
numberOfNights = delta.days
return Customer(self.nameinput.text(),
self.checkInInput.date().toPyDate(),
self.checkOutInput.date().toPyDate(),
self.roomIDInput.value(),
self.bookingTypeInput.currentIndex() + 1,
self.pricePerNightInput.value(),
self.peopleInput.value(),
Comments=self.commentInput.toPlainText(),
NumberOfStayNights=numberOfNights,
TotalPrice=(numberOfNights) * self.pricePerNightInput.value())
class AddRoomWindow(QtWidgets.QDialog):
def __init__(self):
super(AddRoomWindow, self).__init__()
# Load the main UI file
uic.loadUi('./files/UI/AddRoom.ui', self)
self.setWindowFlags(QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint)
self.ConnectLogicToObjects()
def ConnectLogicToObjects(self):
self.roomIDInput = self.findChild(QtWidgets.QSpinBox, 'roomIDInput')
self.roomTypeInput = self.findChild(QtWidgets.QComboBox, 'roomTypeInput')
self.roomTypeInput.addItems(roomDictionary.values())
def exec__(self):
if self.exec_() == QDialog.Accepted:
conn = DB.CreateConnection()
DB.AddRoom(conn, self.roomIDInput.value(), self.roomTypeInput.currentIndex() + 1)
conn.close()
return 1
class RemoveRoomWindow(QtWidgets.QDialog):
def __init__(self):
super(RemoveRoomWindow, self).__init__()
# Load the main UI file
uic.loadUi('./files/UI/DeleteRoom.ui', self)
self.setWindowFlags(QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint)
self.ConnectLogicToObjects()
def ConnectLogicToObjects(self):
self.listWidget = self.findChild(QtWidgets.QListWidget, "listWidget")
self.listWidget.addItems(self.GetRooms())
self.listWidget.setCurrentRow(-1)
def GetRooms(self):
conn = DB.CreateConnection()
rooms = DB.GetRoomsByType(conn)
conn.close()
if rooms:
rooms = [str(i) for i in rooms]
return rooms
def exec__(self):
if self.exec_() == QDialog.Accepted:
row = self.listWidget.currentRow()
item = self.listWidget.item(row)
if item:
msg = MessageBox('Προσοχή', f'Είστε σίγουροι ότι θέλετε να διαγράψετε το δωμάτιο "{item.text()}";')
if msg == QMessageBox.Yes:
conn = DB.CreateConnection()
DB.DeleteRoom(conn, int(item.text()))
conn.close()
return 1
class LoadingWindow(QtWidgets.QDialog):
def __init__(self, text, title='Κρατήσεις'):
super(LoadingWindow, self).__init__()
# Load the main UI file
uic.loadUi('./files/UI/LoadingWindow.ui', self)
self.setWindowTitle(title)
self.label = self.findChild(QtWidgets.QLabel, 'label')
self.label.setText(text)
class ErrorWindow(QtWidgets.QDialog):
def __init__(self, text, title="Error"):
super(ErrorWindow, self).__init__()
# Load the main UI file
uic.loadUi('./files/UI/ErrorWindow.ui', self)
self.setWindowTitle(title)
self.label = self.findChild(QtWidgets.QLabel, 'label')
self.label.setText(text)
def MessageBox(title='Έξοδος', text="Είστε σίγουροι ότι θέλετε να κλείσετε το πρόγραμμα;", buttons=QMessageBox.Yes | QMessageBox.Cancel):
"""
:param title: Window title
:param text: Window Text
"""
msgBox = QMessageBox()
msgBox.setWindowTitle(title)
msgBox.setText(text)
msgBox.setStandardButtons(buttons)
msgBox = msgBox.exec()
return msgBox