-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_main.py
553 lines (455 loc) · 22.6 KB
/
user_main.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import json, requests
import os
from datetime import datetime
from random import randint
from PyQt5 import uic
from PyQt5.QtWidgets import (QMainWindow, QPushButton, QScrollArea,
QLabel, QLineEdit,
QGridLayout, QTableWidgetItem,
QWidget, QMessageBox)
from PyQt5.QtGui import QPixmap, QIcon, QBrush, QColor
from PyQt5.QtCore import QSize, QDate
from user_widget import SparepartWidget, custom_vertikal_bar, custom_horizontal_bar
API_KEY = str(os.getenv("FIREBASE_API"))
PDF_API = "498aMTUyMzA6MTIzMTA6dW1BTEVHMmVCVTR5Q1ZFQw="
FIREBASE_URL = "https://projectbengkel-f2242-default-rtdb.asia-southeast1.firebasedatabase.app/"
# class user main
class UserMain(QMainWindow):
def __init__(self, account):
super(UserMain, self).__init__()
self.account = account
self.setup_ui()
self.set_icon()
self.show()
# fungsi setup ui
def setup_ui(self):
self.main_page = uic.loadUi('file_ui/user_main_menu.ui', self)
self.create_account = self.findChild(QPushButton, 'push_sparepart')
self.create_account.clicked.connect(self.to_sparepart)
self.main_page.push_reservation.clicked.connect(self.to_reservation)
self.main_page.push_service_list.clicked.connect(self.to_service_list)
self.main_page.push_mybooking.clicked.connect(self.to_my_booking)
def to_sparepart(self):
self.navigate_to(SparepartMenu(self.account))
def to_reservation(self):
self.navigate_to(ReservationMenu(self.account))
def to_service_list(self):
self.navigate_to(ServiceListMenu(self.account))
# fungsi untuk ke page history booking, jika tidak ada data, maka akan ada notifikasi
def to_my_booking(self):
try:
self.navigate_to(MyBookingPage(self.account))
except:
self.alert()
def navigate_to(self, window):
if hasattr(self, 'main_page'):
self.main_page.close()
self.window = window
self.window.show()
# fungsi untuk setup icon
def set_icon(self):
self.label = self.findChild(QLabel, 'logo')
pixmap = QPixmap('Pictures/image 2.png')
self.label.setPixmap(pixmap)
self.label.setScaledContents(True)
self.push_sparepart = self.findChild(QPushButton, 'push_sparepart')
self.push_sparepart.setIcon(QIcon("Pictures/sparepart.png"))
self.push_sparepart.setIconSize(QSize(70, 75))
self.push_reservation = self.findChild(QPushButton, 'push_reservation')
self.push_reservation.setIcon(QIcon("Pictures/reservation.png"))
self.push_reservation.setIconSize(QSize(90, 95))
self.push_service_list = self.findChild(QPushButton, 'push_service_list')
self.push_service_list.setIcon(QIcon("Pictures/service.png"))
self.push_service_list.setIconSize(QSize(110, 115))
def alert(self):
alert = QMessageBox(self)
alert.setWindowTitle('Alert!')
alert.setText("You haven't made a reservation yet, please make a reservation first")
alert.setIcon(QMessageBox.Warning)
alert.setStyleSheet("color: white;")
button = alert.exec()
if button == QMessageBox.Ok:
pass
# class sparepart
class SparepartMenu(QMainWindow):
def __init__(self, account):
super(SparepartMenu, self).__init__()
self.account = account
self.setup_ui()
self.show()
# fungsi setup ui
def setup_ui(self):
self.sparepart_menu = uic.loadUi('file_ui/sparepart_menu.ui', self)
self.label = self.findChild(QLabel, 'logo')
pixmap = QPixmap('Pictures/image 2.png')
self.label.setPixmap(pixmap)
self.label.setScaledContents(True)
self.sparepart_menu.push_back.clicked.connect(self.to_user_main)
self.sparepart_menu.Combobox.addItems(["Car", 'Motorcycle'])
self.selected_item = self.sparepart_menu.Combobox.currentText()
self.scrollable_layout = SparepartWidget("Sparepart/Car")
self.sparepart_menu.verticalLayout.addWidget(self.scrollable_layout)
self.sparepart_menu.Combobox.currentIndexChanged.connect(self.insert_sparepart_menu)
self.sparepart_menu.push_next.clicked.connect(self.to_confirmation)
def to_user_main(self):
self.navigate_to(UserMain(self.account))
def to_confirmation(self):
self.navigate_to(ConfirmationPage(self.scrollable_layout.get_data_buy(), self.account, self.selected_item))
def navigate_to(self, window):
if hasattr(self, 'sparepart_menu'):
self.sparepart_menu.close()
self.window = window
self.window.show()
# fungsi untuk menaruh widget sparepart ke dalam layout vertical
def insert_sparepart_menu(self):
self.selected_item = self.sparepart_menu.Combobox.currentText()
self.link = f"/Sparepart/{self.selected_item}"
for i in reversed(range(self.sparepart_menu.verticalLayout.count())):
self.sparepart_menu.verticalLayout.itemAt(i).widget().setParent(None)
self.scrollable_layout = SparepartWidget(self.link)
self.sparepart_menu.verticalLayout.addWidget(self.scrollable_layout)
# class reservasi
class ReservationMenu(QMainWindow):
def __init__(self, account):
super(ReservationMenu, self).__init__()
self.account = account
self.dt = None
self.setup_ui()
self.show()
# fungsi setup ui
def setup_ui(self):
self.reservation_page = uic.loadUi('file_ui/reservation.ui', self)
label = self.findChild(QLabel, 'logo')
pixmap = QPixmap('Pictures/image 2.png')
label.setPixmap(pixmap)
label.setScaledContents(True)
self.name = self.findChild(QLineEdit, 'name_input')
self.reservation_page.type_input.addItem("Car")
self.reservation_page.type_input.addItem("Motorcycle")
self.reservation_page.type_input.currentIndexChanged.connect(self.insert_type)
self.reservation_page.push_back.clicked.connect(self.to_user_main)
self.reservation_page.dateEdit.setMinimumDate(QDate.currentDate())
self.reservation_page.push_booking.clicked.connect(self.booking_button)
self.insert_type()
def to_user_main(self):
if hasattr(self, 'reservation_page'):
self.reservation_page.close()
self.user_main = UserMain(self.account).show()
# untuk mengambil hari dari input user
def get_date(self):
date = self.reservation_page.dateEdit.date()
time = self.reservation_page.timeEdit.time()
self.dt = datetime(date.year(), date.month(), date.day(), time.hour(), time.minute(), 0)
return self.dt
# untuk memasukkan list service ke dalam combobox secara dinamis
def insert_type(self):
self.selected_type = self.reservation_page.type_input.currentText()
link = f"ServiceList/{self.selected_type}"
services = self.get_database(link)
list_service = []
for service in services.keys():
list_service.append(service)
self.reservation_page.service_list.clear()
self.reservation_page.service_list.addItems(list_service)
# fungsi untuk menyimpan data reservasi
def booking_button(self):
self.selected_service = self.service_list.currentText()
current_date = self.get_date()
data_booking = {str(current_date): {'service': self.selected_service
}}
self.update_database(f"Booking/{self.account}", data_booking) # menaruh data booking pada account yang login
data_sales = {str(current_date.date()): {self.selected_service: 1}} # data sales
current_data_service = self.get_database(f'Sales/{self.selected_type}/Service') # mengambil data penjualan service berdasarkan type
if str(current_date.date()) in current_data_service.keys(): # check jika di hari yang sama sudah ada booking/belum
if self.selected_service in current_data_service[str(current_date.date())].keys(): # check jika service yang sama sudah ada atau belum
current_data_service[str(current_date.date())][self.selected_service] += 1 # jika ya maka jumlah pemesanan service akan menambah
data_sales = current_data_service
else:
current_data_service[str(current_date.date())][self.selected_service] = 1 # jika tidak maka jumlah pemesanan pada service itu akan disetting 1
data_sales = current_data_service
self.update_database(f"Sales/{self.selected_type}/Service", data_sales) # update data sales service
self.success_book()
self.to_user_main()
def get_database(self, directory):
response_get = requests.get(FIREBASE_URL + f'/{directory}.json?auth=' + API_KEY)
database = response_get.json()
return database
def update_database(self, directory, data):
requests.patch(FIREBASE_URL + f'/{directory}.json?auth=' + API_KEY, json = data)
def success_book(self):
success = QMessageBox(self)
success.setWindowTitle('Thank You!')
success.setText("Your reservation has been successfull!")
success.setIcon(QMessageBox.Information)
success.setStyleSheet("color: white;")
button = success.exec()
if button == QMessageBox.Ok:
pass
# class service list page
class ServiceListMenu(QMainWindow):
def __init__(self, account):
super(ServiceListMenu, self).__init__()
self.account = account
self.setup_ui()
self.show()
def setup_ui(self):
self.servicelist_menu = uic.loadUi('file_ui/service_list.ui', self)
self.servicelist_menu.push_back.clicked.connect(self.to_user_main)
self.servicelist_menu.Combobox.addItems(["Car", 'Motorcycle'])
selected_item = self.servicelist_menu.Combobox.currentText()
self.link = f"/ServiceList/{selected_item}"
self.servicelist_menu.verticalLayout.addWidget(self.list_service())
self.servicelist_menu.Combobox.currentIndexChanged.connect(self.insert_list)
label = self.findChild(QLabel, 'logo_sparepart')
pixmap = QPixmap('Pictures/image 2.png')
label.setPixmap(pixmap)
label.setScaledContents(True)
def to_user_main(self):
if hasattr(self, 'servicelist_menu'):
self.servicelist_menu.close()
self.user_main = UserMain(self.account)
self.user_main.show()
# ngambil type yang dipilih dan memasukkan widget pada fungsi list_service ke dalam layout
def insert_list(self):
selected_item = self.servicelist_menu.Combobox.currentText()
self.link = f"/ServiceList/{selected_item}"
for i in reversed(range(self.servicelist_menu.verticalLayout.count())):
self.servicelist_menu.verticalLayout.itemAt(i).widget().setParent(None)
self.servicelist_menu.verticalLayout.addWidget(self.list_service())
# membuat widget untuk menampilkan list
def list_service(self):
scroll_area = QScrollArea(self)
scroll_area.setWidgetResizable(True)
content_widget = QWidget()
scroll_area.setWidget(content_widget)
scroll_area.verticalScrollBar().setStyleSheet(str(custom_vertikal_bar()))
scroll_area.horizontalScrollBar().setStyleSheet(str(custom_horizontal_bar()))
scroll_area.setStyleSheet("border: 5px;")
content_layout = QGridLayout(content_widget)
data = self.get_database(self.link)
labels = ['No.', 'Name', 'Description', 'Price']
for i, label in enumerate(labels):
label = QLabel(label)
label.setStyleSheet("color: white;")
content_layout.addWidget(label, 0, i)
for i, (key, value) in enumerate(data.items(), start = 1):
row_data = [i, key, value['description'], f"{value['price']}k"]
for j, item in enumerate(row_data):
label = QLabel(str(item))
label.setStyleSheet("color: white;")
if item == value['description']:
label.setWordWrap(True)
label.setFixedHeight(100)
else:
label.setFixedHeight(50)
content_layout.addWidget(label, i, j)
content_layout.setRowMinimumHeight(i, 120)
return scroll_area
def get_database(self, directory):
response_get = requests.get(FIREBASE_URL + f'/{directory}.json?auth=' + API_KEY)
database = response_get.json()
return database
def update_database(self, directory, data):
requests.patch(FIREBASE_URL + f'/{directory}.json?auth=' + API_KEY, json = data)
# class confirmation page
class ConfirmationPage(QMainWindow):
def __init__(self, data, account, type):
super(ConfirmationPage, self).__init__()
self.data = data
self.account = account
self.type = type
self.setup_ui()
self.insert_table()
self.show()
def setup_ui(self):
self.confirmation_page = uic.loadUi('file_ui/shopping_cart.ui', self)
label = self.findChild(QLabel, 'logo_sparepart')
pixmap = QPixmap('Pictures/image 2.png')
label.setPixmap(pixmap)
label.setScaledContents(True)
self.confirmation_page.push_back.clicked.connect(self.to_sparepart)
self.confirmation_page.push_buy.clicked.connect(self.clicked_buy)
# class untuk memasukkan data pembelian sparepart ke table
def insert_table(self):
self.total_quantity = self.data[-1]['Total quantity']
self.total_price = self.data[-1]['Total price']
data_buy = self.data[:-1]
# self.confirmation_page.tableWidget.setStyleSheet("border: 5px;")
self.confirmation_page.tableWidget.setStyleSheet("QTableWidget {background-color: #2D2E43; border: 2px #d0d2f9;border-radius: 10px;gridline-color: #ccc;}\
QHeaderView::section {background-color: #aabade;color: #14212b;padding: 5px;border: 1px solid #ccc;font-size: 14px;}\
QTableWidgetItem {border: 1px solid #ccc;padding: 5px;font-size: 12px;color: #ffffff; /* Text color */}")
self.confirmation_page.tableWidget.setColumnCount(3)
self.confirmation_page.tableWidget.setRowCount(len(data_buy) + 1)
self.confirmation_page.tableWidget.setColumnWidth(0, 175)
self.confirmation_page.tableWidget.setColumnWidth(1, 100)
self.confirmation_page.tableWidget.setColumnWidth(2, 136)
self.confirmation_page.tableWidget.setHorizontalHeaderLabels(['Name', 'Quantity', 'Price'])
i = 0
for data in data_buy:
item1 = QTableWidgetItem(f"{data['name']}")
item1.setForeground(QBrush(QColor(255, 255, 255)))
self.confirmation_page.tableWidget.setItem(i, 0, item1)
item2 = QTableWidgetItem(" " * 12 + f"{data['quantity']}")
item2.setForeground(QBrush(QColor(255, 255, 255)))
self.confirmation_page.tableWidget.setItem(i, 1, item2)
item3 = QTableWidgetItem(" " * 12 + str(data['price']) + 'k')
item3.setForeground(QBrush(QColor(255, 255, 255)))
self.confirmation_page.tableWidget.setItem(i, 2, item3)
i += 1
total = QTableWidgetItem('Total')
total.setForeground(QBrush(QColor(255, 255, 255)))
self.confirmation_page.tableWidget.setItem(i, 0, total)
total1 = QTableWidgetItem(" " * 12 + str(self.total_quantity))
total1.setForeground(QBrush(QColor(255, 255, 255)))
self.confirmation_page.tableWidget.setItem(i, 1, total1)
total2 = QTableWidgetItem(" " * 12 + str(self.total_price) + 'k')
total2.setForeground(QBrush(QColor(255, 255, 255)))
self.confirmation_page.tableWidget.setItem(i, 2, total2)
def to_sparepart(self):
if hasattr(self, 'confirmation_page'):
self.confirmation_page.close()
self.sparepart_page = SparepartMenu(self.account)
self.sparepart_page.show()
# save pdf dalam file
def save_pdf(self, content, filename):
with open(filename, 'wb') as f:
f.write(content)
# class untuk update data sales berdasarkan tanggal dan type
def update_sales(self):
current_date = str(datetime.now().date())
data_sales = self.get_database(f'Sales/{self.type}/Sparepart') # mengambil data sparepart berdasarkan type
data_temp = {}
for data in self.data[:-1]:
data_temp.update({data['name']: {'quantity': data['quantity'], 'total': data['total']}}) # membuat data yang dikirim dari class sparepart menjadi dictionary
# check apakah tanggal dibeli sudah ada atau belum
if current_date in data_sales.keys():
for name, temp_data in data_temp.items():
if name in data_sales[current_date]:
data_sales[current_date][name]['quantity'] += temp_data['quantity'] # menambahkan data yang ada pada firebase dengan data yang dibeli jika sparepart yang dibeli sudah ada di database
else:
data_sales[current_date][name] = temp_data # membuat cabang baru
else:
data_sales = {current_date:data_temp} # menambahkan data pembelian berdasarkan tanggal jika tidak ada tanggal itu pada database
self.update_database(f"Sales/{self.type}/Sparepart", data_sales)
def get_database(self, directory):
response_get = requests.get(FIREBASE_URL + f'/{directory}.json?auth=' + API_KEY)
database = response_get.json()
return database
def update_database(self, directory, data):
requests.patch(FIREBASE_URL + f'/{directory}.json?auth=' + API_KEY, json = data)
# fungsi untuk mengambil invoice.pdf dari APITemplate.io
def get_pdf(self):
data_buy = self.data[:-1]
account = self.account
template_id = "93477b23ac6dee8a"
data = {
"brand_name": "CMech Service",
"invoice_no": f"{str(self.random_with_N_digits(3))} {str(self.random_with_N_digits(6))}",
"invoice_date": f"{str(datetime.now().date())}",
"account_name": f"{account.split('@', 1)[0]}",
"bank_det": "Bank Masyarakat Sejahtera",
"company_name": "CMech Service",
"street_address": "24 Los Angeles, India",
"items":None,
"terms" : "Lorem ipsum dolor sit, amet consectetur",
"conditions" : "ipsum dolor sit, amet consectetur",
"tel1": "+123 456 7890",
"tell2": "+777 500 5485",
"mail1": "[email protected]",
"mail2": "[email protected]",
"address": "Lorem ipsum-40"
}
data['items'] = data_buy
response = requests.post(
f"https://rest.apitemplate.io/v2/create-pdf?template_id={template_id}",
headers = {"X-API-KEY": f"{PDF_API}"},
json = data
)
if response.status_code == 200:
pdf_content = json.loads(response.content)['download_url']
response_content = requests.get(pdf_content)
with open('invoice.pdf', 'wb') as file:
file.write(response_content.content)
return pdf_content
else:
print(f"Error: {response.status_code}")
return None
# kembali ke confirmation page
def clicked_buy(self):
self.get_pdf()
self.success_buy()
self.update_sales()
if hasattr(self, 'confirmation_page'):
self.confirmation_page.close()
UserMain(self.account).show()
def success_buy(self):
success = QMessageBox(self)
success.setWindowTitle('Thank You!')
success.setText("Your transaction has been successfull!")
success.setIcon(QMessageBox.Information)
success.setStyleSheet("color: white;")
button = success.exec()
if button == QMessageBox.Ok:
pass
# random number buat nomor invoice
def random_with_N_digits(self, n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)
# class buat booking
class MyBookingPage(QMainWindow):
def __init__(self, account):
super(MyBookingPage, self).__init__()
self.account = account
self.setup_ui()
self.show()
def setup_ui(self):
self.my_booking_page = uic.loadUi('file_ui/my_booking.ui', self)
self.my_booking_page.push_back.clicked.connect(self.to_user_main)
self.my_booking_page.verticalLayout.addWidget(self.list_booking())
label = self.findChild(QLabel, 'logo_sparepart')
pixmap = QPixmap('Pictures/image 2.png')
label.setPixmap(pixmap)
label.setScaledContents(True)
def to_user_main(self):
if hasattr(self, 'my_booking_page'):
self.my_booking_page.close()
UserMain(self.account).show()
# memasukkan widget scroll ke dalam layout
def list_booking(self):
scroll_area = QScrollArea(self)
scroll_area.setWidgetResizable(True)
content_widget = QWidget()
scroll_area.setWidget(content_widget)
scroll_area.verticalScrollBar().setStyleSheet(str(custom_vertikal_bar()))
scroll_area.horizontalScrollBar().setStyleSheet(str(custom_horizontal_bar()))
scroll_area.setStyleSheet("border: 5px;")
content_layout = QGridLayout(content_widget)
link = f"Booking/{self.account}"
data = self.get_database(link)
labels = ['No.', ' ' * 15 + 'Date', ' ' * 15 + 'Service', 'Status']
for i, label in enumerate(labels):
label = QLabel(label)
label.setStyleSheet("color: white;")
label.setFixedHeight(12)
if label == 'No.':
content_layout.setColumnMinimumWidth(0, 3)
content_layout.addWidget(label, 0, i)
format_time = "%Y-%m-%d %H:%M:%S"
for i, (key, value) in enumerate(data.items(), start = 1):
status = 'Waiting'
if datetime.strptime(key, format_time) < datetime.now():
status = 'Finished'
row_data = [i, key, value['service'], status]
for j, item in enumerate(row_data):
label = QLabel(str(item))
if item == value['service']:
label.setWordWrap(True)
label.setStyleSheet("color: white;")
label.setFixedHeight(20)
content_layout.addWidget(label, i, j)
return scroll_area
def get_database(self, directory):
response_get = requests.get(FIREBASE_URL + f'/{directory}.json?auth=' + API_KEY)
database = response_get.json()
return database