-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
1126 lines (867 loc) · 38.9 KB
/
utilities.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from utilities import *
from customer import *
from administrator import *
from pymongo import MongoClient
from tabulate import tabulate
from dateutil.relativedelta import relativedelta
import mysql.connector
import sys
import re
import collections
import pymongo
import pprint
import datetime
import time
import tkinter as tk
from tkinter import font as tkfont
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
# from dns.tsig import sign
###################################### CONNECT TO DB FUNCTIONS ######################################################
def connectDB():
import mysql.connector
mysqldb = mysql.connector.connect(
host='localhost',
user='root',
passwd='password',
db='business')
mycursor = mysqldb.cursor()
return mysqldb, mycursor
def connectMongo():
from pymongo import MongoClient
myclient = MongoClient("mongodb://127.0.0.1:27017/")
mydb_Business = myclient["Business"]
myItems_col = mydb_Business["items"]
myProducts_col = mydb_Business["products"]
return myItems_col, myProducts_col
###################################### COMMON FUNCTIONS ######################################################
def emptyCursor(mycursor):
return len(mycursor.fetchall()) == 0
# Prints out the list of items purchased by the customer
def printPurchasedList(username):
from tabulate import tabulate
mysqldb, mycursor = connectDB()
try:
query = f"SELECT * FROM business.purchased WHERE CustomerID = '{username}'"
mycursor.execute(query)
result = mycursor.fetchall()
if len(list(result)) > 0:
print(tabulate(result, headers=[
"Customer ID", "Item ID", "Purchase Date", "Warranty End Date"]))
print("\n")
else:
return 0
except:
mysqldb.rollback()
mysqldb.close()
###################################### DISPLAY RECORDS ###########################################################
def displayCustomerRecords():
from tabulate import tabulate
mysqldb, mycursor = connectDB()
try:
mycursor.execute("SELECT * from customer")
result = mycursor.fetchall()
print(tabulate(result, headers=[
"Customer ID", "name", "gender", "email address", "phone number", "address", "password"]))
except:
print('Error: Unable to fetch data.')
mysqldb.close()
def displayAdminRecords():
from tabulate import tabulate
mysqldb, mycursor = connectDB()
try:
mycursor.execute("SELECT * from administrator")
result = mycursor.fetchall()
print(tabulate(result, headers=[
"Administrator ID", "name", "gender", "phone number", "password"]))
except:
print('Error: Unable to fetch data.')
mysqldb.close()
def displayProduct():
from tabulate import tabulate
mysqldb, mycursor = connectDB()
try:
query = f"SELECT Category, Model, Price, Warranty, Inventory FROM business.products"
mycursor.execute(query)
result = mycursor.fetchall()
print(tabulate(result, headers=[
"Category", "Model", "Price ($)", "Warranty (months)", "Inventory"]))
except:
mysqldb.rollback()
mysqldb.close()
###################################### MONGOL DB SEARCH FUNCTION ######################################################
def isAdministrator(username):
mysqldb, mycursor = connectDB()
try:
mycursor.execute(
f"SELECT * FROM business.administrator WHERE AdministratorID = '{username}'")
if emptyCursor(mycursor):
return False
else:
return True
except:
mysqldb.rollback()
mysqldb.close()
return False
def inventoryStocksSearch(category, model, color, powersupply, factory, productionYear):
myItems_col, myProducts_col = connectMongo()
myquery = {"Category": category,
"Model": model, "PurchaseStatus": "Unsold"}
if color != "-":
myquery["Color"] = color
if powersupply != "-":
myquery["PowerSupply"] = powersupply
if factory != "-":
myquery["Factory"] = factory
if productionYear != "-":
myquery["ProductionYear"] = str(productionYear)
return myItems_col.count_documents(myquery)
def noOfItemSold(category, model, color, powersupply, factory, productionYear):
myItems_col, myProducts_col = connectMongo()
myquery = {"Category": category, "Model": model, "PurchaseStatus": "Sold"}
if color != "-":
myquery["Color"] = color
if powersupply != "-":
myquery["PowerSupply"] = powersupply
if factory != "-":
myquery["Factory"] = factory
if productionYear != "-":
myquery["ProductionYear"] = str(productionYear)
return myItems_col.count_documents(myquery)
def searchItem(category, model, color, warranty, powersupply, factory, productionYear):
import pandas as pd
pd.set_option('display.min_rows', 50)
pd.set_option('display.expand_frame_repr', True)
myItems_col, myProducts_col = connectMongo()
try:
# category = stuff[selection]["Category"]
# model = stuff[selection]["Model"]
myquery = {"PurchaseStatus": "Unsold"}
if category != "-":
myquery["Category"] = category
if model != "-":
myquery["Model"] = model
if warranty != "-":
myquery["Warranty (months)"] = int(warranty)
if color != "-":
myquery["Color"] = color
if powersupply != "-":
myquery["PowerSupply"] = powersupply
if factory != "-":
myquery["Factory"] = factory
if productionYear != "-":
myquery["ProductionYear"] = str(productionYear)
result = myItems_col.find(
myquery, {"_id": 0, "ServiceStatus": 0, "PurchaseStatus": 0})
dataFrame = pd.DataFrame(result)
print(dataFrame, "\n")
return dataFrame
except:
print("Invalid option selected, please select valid option", "\n")
def advancedSearch(username, category, model, price, warranty, color, powersupply, factory, productionYear):
import pandas as pd
myItems_col, myProducts_col = connectMongo()
def checksearch():
option = input("Do you want to search the individual items (Y/N) ?")
print("")
if (option == 'Y'):
print("There are a total of " + str(len(result)) +
" products to select your item")
print("Please choose from option 0 to " +
str(len(result) - 1), "\n")
selection = int(input("Select your option: "))
print("")
print("Searching for products...\n")
searchItem(result, selection, color, warranty,
powersupply, factory, productionYear)
elif (option == 'N'):
print("Search cancelled")
print("Thank you and good bye")
else:
print("Invalid input")
checksearch()
try:
myquery = {}
if category != "-":
myquery["Category"] = category
if model != "-":
myquery["Model"] = model
if price != "-":
if price == "Below $100":
price1 = 0
price2 = 100
if price == "$100-$150":
price1 = 100
price2 = 150
elif price == "$150-$200":
price1 = 150
price2 = 200
myquery["Price ($)"] = {'$gte': price1, '$lte': price2}
if warranty != "-":
myquery["Warranty (months)"] = int(warranty)
if(isAdministrator(username)):
result = list(myProducts_col.find(
myquery, {"_id": 0, "ProductID": 0}))
dataFrame = pd.DataFrame(result)
listInventory = []
soldProduct = []
for x in result:
listInventory.append(inventoryStocksSearch(
x["Category"], x["Model"], color, powersupply, factory, productionYear))
soldProduct.append(noOfItemSold(
x["Category"], x["Model"], color, powersupply, factory, productionYear))
dataFrame.insert(4, "Inventory", listInventory, True)
dataFrame.insert(5, "Number of items Sold", soldProduct, True)
print(dataFrame)
return dataFrame
else:
result = list(myProducts_col.find(
myquery, {"_id": 0, "Cost ($)": 0, "ProductID": 0}))
dataFrame = pd.DataFrame(result)
listInventory = []
for x in result:
listInventory.append(inventoryStocksSearch(
x["Category"], x["Model"], color, powersupply, factory, productionYear))
dataFrame.insert(4, "Inventory", listInventory, True)
print(dataFrame)
return dataFrame
except:
print("Error in search", "\n")
def itemSearch(username, itemId):
import pandas as pd
myItems_col, myProducts_col = connectMongo()
myquery = {"ItemID": str(itemId)}
if (isAdministrator(username)):
try:
result = list(myItems_col.find(myquery, {"_id": 0}))
dataFrame = pd.DataFrame(result)
print(dataFrame)
return dataFrame
except:
print("Error in search", "\n")
else:
print("Not Administrator, cancelling item ID search")
#################################### WARRANTY, SERVICE AND PAYMENTS ####################################################
# Prints out the list of items under servicing for customer
def printItemServicingList(username):
from tabulate import tabulate
mysqldb, mycursor = connectDB()
try:
query = f"SELECT ItemID, RequestStatus FROM business.request WHERE CustomerID = '{username}'"
mycursor.execute(query)
result = mycursor.fetchall()
print(tabulate(result, headers=[
"Request ID", "Request Date", "Request Status", "Customer ID", "Item ID"]))
print("\n")
return list(result)
except:
mysqldb.rollback()
mysqldb.close()
def printItemIDServicingList(username):
from tabulate import tabulate
mysqldb, mycursor = connectDB()
try:
query = f"SELECT RequestID, RequestStatus, ItemID FROM business.request WHERE CustomerID = '{username}'"
mycursor.execute(query)
result = mycursor.fetchall()
print(tabulate(result, headers=[
"Request ID", "Request Date", "Request Status", "Service Status", "Customer ID", "Item ID"]))
print(result)
return list(result)
except:
mysqldb.rollback()
mysqldb.close()
# Prints out the list of items purchased by a customer
def getPurchasedItems(username):
from tabulate import tabulate
mysqldb, mycursor = connectDB()
try:
query = f"SELECT ItemID FROM business.purchased WHERE CustomerID = '{username}'"
mycursor.execute(query)
result = mycursor.fetchall()
print(tabulate(result, headers=["Item ID"]))
print("\n")
return list(result)
except:
mysqldb.rollback()
mysqldb.close()
def getPurchasedItemsFull(username):
from tabulate import tabulate
mysqldb, mycursor = connectDB()
try:
query = f"SELECT * FROM business.purchased WHERE CustomerID = '{username}'"
mycursor.execute(query)
result = mycursor.fetchall()
print(tabulate(result, headers=[
"Customer ID", "Item ID", "Purchase Date", "Warranty End Date"]))
print("\n")
return list(result)
except:
mysqldb.rollback()
mysqldb.close()
# Prints out the list of items pending for service payment for the customer
def printServicePayment(username):
from tabulate import tabulate
mysqldb, mycursor = connectDB()
try:
query = f"SELECT RequestID, DueDate, ServiceFeeAmount FROM business.servicefee WHERE CustomerID = '{username}'"
mycursor.execute(query)
result = mycursor.fetchall()
print(tabulate(result, headers=[
"Request ID", "Due Date", "Service Fee Amount"]))
print("\n")
return list(result)
except:
mysqldb.rollback()
mysqldb.close()
# Obtain the warranty end date when customer purchase the item
def getWarrantyEndDate(date, item):
from dateutil.relativedelta import relativedelta
import datetime
mysqldb, mycursor = connectDB()
try:
query = f"SELECT p.Warranty FROM items s, products p WHERE s.ItemID = '{item}' AND s.ProductID = p.ProductID "
mycursor.execute(query)
result = mycursor.fetchone()
warranty = int(list(result)[0])
return date + relativedelta(months=warranty)
except:
print("Error occurred in search\n")
mysqldb.rollback()
mysqldb.close()
# Obtaining the due date for the service payment
def getDueDate(date):
from dateutil.relativedelta import relativedelta
import datetime
return date + datetime.timedelta(days=10)
# Obtaining the service fee amount that the customer needs to pay for
def getServiceFeeAmount(item):
mysqldb, mycursor = connectDB()
try:
query = f"SELECT p.Cost FROM items s, products p WHERE s.ItemID = '{item}' AND s.ProductID = p.ProductID "
mycursor.execute(query)
result = mycursor.fetchone()
cost = int(list(result)[0])
return 40 + (0.2 * cost)
except:
print("Error occurred in search\n")
mysqldb.rollback()
mysqldb.close()
# This function checks whether the item purchased by customer is still under warranty
def checkWarranty(itemid, time):
mysqldb, mycursor = connectDB()
try:
myquery = f"SELECT WarrantyEndDate FROM business.purchased WHERE ItemID = {itemid}"
mycursor.execute(myquery)
result = mycursor.fetchone()
retrieveDate = list(result)
if time <= retrieveDate[0]:
return "Submitted"
else:
return "Submitted and Waiting for payment"
except:
mysqldb.rollback()
mysqldb.close()
# This function checks whether the item purchased by customer is still under warranty and updates the service status
def checkServiceStatus(itemid, time):
mysqldb, mycursor = connectDB()
try:
myquery = f"SELECT WarrantyEndDate FROM business.purchased WHERE ItemID = {itemid}"
mycursor.execute(myquery)
result = mycursor.fetchone()
retrieveDate = list(result)
if time <= retrieveDate[0]:
return "Waiting for approval"
else:
return " "
except:
mysqldb.rollback()
mysqldb.close()
def getRequestID(customer, itemid):
mysqldb, mycursor = connectDB()
try:
myquery = f"SELECT RequestID FROM business.request WHERE ItemID = {itemid} AND CustomerID = '{customer}'"
mycursor.execute(myquery)
result = mycursor.fetchall()
retrieveID = result
return retrieveID
except:
mysqldb.rollback()
mysqldb.close()
# This method does not work
def creatingServicePayment(username, timing):
mysqldb, mycursor = connectDB()
mycursor.execute(f"SELECT (RequestID, ItemID) FROM business.request WHERE CustomerID = '{username}'"
f"AND RequestStatus = 'Submitted and Waiting for payment'")
if not (emptyCursor(mycursor)):
result = mycursor.fetchall()
listOfID = list(result)
print(listOfID)
for row in listOfID:
requestid = row[0]
dueDate = getDueDate(timing)
serviceFee = getServiceFeeAmount(row[1])
mycursor.execute(
f"SELECT * FROM business.servicepayment WHERE RequestID = {requestid}")
if emptyCursor(mycursor):
newQuery = f"INSERT INTO business.servicepayment " \
f"(PaymentCreationDate, DueDate, ServiceFeeAmount, RequestID, CustomerID) VALUES " \
f"(DATE('{timing}'), DATE('{dueDate}'), " \
f"{serviceFee}, {requestid}, '{username}')"
mycursor.execute(newQuery)
mysqldb.commit()
print("Successfully inserted into Service Payment Table")
# This function is meant for customers to view their service payment
def viewServicePayment(username):
mysqldb, mycursor = connectDB()
noServicePayment = "You do not have any service payment to settle"
try:
mycursor.execute(
f"SELECT * FROM business.servicefee WHERE CustomerID = '{username}'")
if len(mycursor.fetchall()) == 0:
print(noFineMsg)
else:
mycursor.execute(
f"SELECT * FROM business.servicefee WHERE CustomerID = '{username}'")
result = mycursor.fetchall()
print(tabulate(result, headers=[
"Payment Creation Date", "Due Date", "Service Fee", "Request ID", "Customer ID"]))
print("\n")
except:
mysqldb.rollback()
mysqldb.close()
def getItemIDFromServiceRequest(request):
mysqldb, mycursor = connectDB()
try:
mycursor.execute(
f"SELECT ItemID FROM business.request WHERE RequestID = {request}")
result = list(mycursor.fetchone())
return result[0]
except:
mysqldb.rollback()
mysqldb.close()
def getRequestIDFromServiceRequest(itemID):
mysqldb, mycursor = connectDB()
try:
mycursor.execute(
f"SELECT RequestID FROM business.request WHERE ItemID = {itemID} AND (RequestStatus LIKE 'Submitted' OR RequestStatus LIKE 'Submitted and Waiting for payment')")
result = list(mycursor.fetchone())
return result[0]
except:
mysqldb.rollback()
mysqldb.close()
# This function facilitates the customer's request for servicing of their products
def requestForService(username, itemID):
mysqldb, mycursor = connectDB()
successRequestItem = "You have successfully requested for servicing"
timing = datetime.datetime.now()
try:
itemInput = itemID
mycursor.execute(
f"SELECT * FROM business.purchased WHERE ItemID = {itemInput}")
if emptyCursor(mycursor):
errorMsg = "Invalid ItemID input"
print(errorMsg)
return False
else:
mycursor.execute(f"SELECT * FROM business.request "
f"WHERE ItemID = {itemInput} AND (RequestStatus LIKE 'Approved' OR RequestStatus LIKE 'Submitted' OR RequestStatus LIKE 'Submitted and Waiting for payment' OR RequestStatus LIKE 'In Progress')")
if emptyCursor(mycursor):
requestStatus = checkWarranty(itemInput, timing)
# serviceStatus = checkServiceStatus(itemInput, timing)
sqlQuery = f"INSERT INTO business.request " \
f"(RequestDate, RequestStatus, CustomerID, ItemID) " \
f"VALUES (DATE('{timing}'), '{requestStatus}', '{username}', {itemInput})"
mycursor.execute(sqlQuery)
mysqldb.commit()
requestID = getRequestIDFromServiceRequest(itemInput)
print(requestID)
if requestStatus == "Submitted and Waiting for payment":
deadline = getDueDate(timing)
servicefee = getServiceFeeAmount(itemInput)
print(deadline)
print(servicefee)
nextQuery = f"INSERT INTO business.servicefee " \
f"(PaymentCreationDate, DueDate, ServiceFeeAmount, CustomerID, RequestID)" \
f"VALUES (DATE('{timing}'), DATE('{deadline}'), {servicefee}, '{username}', {requestID})"
mycursor.execute(nextQuery)
print(
"Your Warranty has expired please pay your servicefee within the deadline")
mysqldb.commit()
else:
sqlQuery = f"INSERT INTO business.service (ItemID, RequestID) " \
f"VALUES ({itemInput}, {requestID})"
mycursor.execute(sqlQuery)
sqlQuery = f"UPDATE business.items SET ServiceStatus = 'Waiting for approval' WHERE ItemID = {itemInput}"
mycursor.execute(sqlQuery)
print("Successfully inserted in to Service Table")
mysqldb.commit()
print(successRequestItem)
return True
else:
notice = "Request has already been submitted for the item!"
print(notice)
return False
except:
mysqldb.rollback()
mysqldb.close()
# This function cancels the customer's request for servicing of their products
def cancelRequest(username, requestID):
mysqldb, mycursor = connectDB()
noRequestForServicing = "You have not request any item for servicing yet"
successCancelRequest = "You have successfully cancelled your request"
try:
mycursor.execute(
f"SELECT * FROM business.request WHERE CustomerID = '{username}'")
if len(mycursor.fetchall()) == 0:
print(noRequestForServicing)
return False
else:
# mycursor.execute(f"SELECT * FROM business.request WHERE RequestID = {requestId} "
# requestId = input(
# "Enter the Request ID that you wish to cancel your request: ")
mycursor.execute(f"SELECT * FROM business.request WHERE RequestID = {requestID} "
f"AND (RequestStatus NOT LIKE 'Approved' AND RequestStatus NOT LIKE 'Cancelled' "
f"AND RequestStatus NOT LIKE 'Completed')")
if len(mycursor.fetchall()) == 0:
invalidID = "Invalid ItemID entered"
print(invalidID)
return False
else:
sqlQuery = f"UPDATE business.request SET RequestStatus = 'Cancelled' WHERE RequestID = {requestID}"
mycursor.execute(sqlQuery)
itemIDDelete = getItemIDFromServiceRequest(requestID)
mycursor.execute(
f"DELETE FROM business.service WHERE RequestID = {requestID}")
mysqldb.commit()
mycursor.execute(
f"DELETE FROM business.servicefee WHERE CustomerID = '{username}' AND RequestID = {requestID}")
mysqldb.commit()
sqlQuery = f"UPDATE business.items SET ServiceStatus = '' WHERE ItemID = {itemIDDelete}"
mycursor.execute(sqlQuery)
print(successCancelRequest)
return True
except:
mysqldb.rollback()
mysqldb.close()
# This function enables the customer to pay for their service fee should they incurred // Change to accept Request ID
def payingServicePayment(username, requestId):
mysqldb, mycursor = connectDB()
noNeedToPay = "There is no Service Payment Required"
successPaid = "Successfully paid the Service payment"
dateOfPayment = datetime.datetime.now()
try:
mycursor.execute(
f"SELECT * FROM business.servicefee WHERE CustomerID = '{username}'")
if len(mycursor.fetchall()) == 0: # Check whether the customer have any service payment to pay
print(noNeedToPay)
return False
else:
mycursor.execute(f"SELECT * FROM business.request WHERE RequestID = {requestId} "
f"AND RequestStatus = 'Submitted and Waiting for payment' AND CustomerID = '{username}'")
if len(mycursor.fetchall()) == 0:
print("Invalid RequestID entered")
return False
else:
itemId = getItemIDFromServiceRequest(requestId)
mycursor.execute(f"SELECT DueDate FROM business.servicefee WHERE RequestID = {requestId} "
f"AND CustomerID = '{username}'")
result = list(mycursor.fetchone())
if dateOfPayment > result[0]:
print(
"Service is cancelled as the payment date is beyond the due date")
sqlQuery = f"UPDATE business.request SET RequestStatus = 'Cancelled' " \
f"WHERE RequestID = {requestId}"
mycursor.execute(sqlQuery)
mycursor.execute(
f"DELETE FROM business.servicefee WHERE CustomerID = '{username}' AND RequestID = {requestId}")
mysqldb.commit()
elif dateOfPayment <= result[0]:
sqlQuery = f"UPDATE business.request SET RequestStatus = 'In Progress' " \
f"WHERE RequestID = {requestId}"
mycursor.execute(sqlQuery)
sqlQuery = f"INSERT INTO business.service (ItemID, RequestID) " \
f"VALUES ({itemId}, {requestId})"
mycursor.execute(sqlQuery)
sqlQuery = f"UPDATE business.items SET ServiceStatus = 'Waiting for approval' WHERE ItemID = {itemId}"
mycursor.execute(sqlQuery)
mycursor.execute(
f"UPDATE business.servicefee SET PaymentSettlementDate = DATE('{dateOfPayment}')"
f"WHERE RequestID = {requestId} "
f"AND CustomerID = '{username}'")
mysqldb.commit()
print("Service fee is paid")
return True
except:
mysqldb.rollback()
mysqldb.close()
################################################# SEARCH AND CHECKOUT ##################################################
# This function enable customers to checkout their items
def checkoutItem(username, itemID):
mysqldb, mycursor = connectDB()
itemNotAvailable = "Item is not available or already sold!"
timing = datetime.datetime.now()
successPurchaseItem = "You have successfully purchased the item"
try:
mycursor.execute(
f"SELECT * FROM business.items WHERE ItemID = '{itemID}' AND PurchaseStatus = 'Unsold'")
if emptyCursor(mycursor):
return False
else:
sqlQuery = f"INSERT INTO business.purchased " \
f"VALUES('{username}', {itemID}, DATE('{timing}'), DATE('{getWarrantyEndDate(timing, itemID)}'))"
mycursor.execute(sqlQuery)
updateQuery = f"UPDATE items SET PurchaseStatus = 'Sold' WHERE ItemID = '{itemID}'"
mycursor.execute(updateQuery)
print("Item table is updated successfully!")
mysqldb.commit()
print(successPurchaseItem)
return True
except:
mysqldb.rollback()
mysqldb.close()
# def basicProductSearchByCategory(category):
# from tabulate import tabulate
# mysqldb, mycursor = connectDB()
# try:
# query = f"SELECT Category, Model, Price, Warranty, Inventory FROM business.products WHERE Category = '{category}'"
# mycursor.execute(query)
# result = mycursor.fetchall()
# if len(list(result)) > 0:
# print(tabulate(result, headers=[
# "Product Category", "Model", "Price", "Warranty", "Inventory"]))
# print("\n")
# listOfProd = list(result)
# option = input(
# "Do you want to search the individual items (Y/N) ? ")
# if option == 'Y':
# print("There are a total of " + str(len(result)) +
# " products to select your item")
# selection = input("Select your option: ")
# print("Searching for products...\n")
# listOfItemAvailable(listOfProd, int(selection) - 1)
# else:
# print("Invalid search\n")
# except:
# mysqldb.rollback()
# mysqldb.close()
# def basicProductSearchByModel(model):
# from tabulate import tabulate
# mysqldb, mycursor = connectDB()
# try:
# query = f"SELECT Category, Model, Price, Warranty, Inventory FROM products WHERE Model = '{model}'"
# mycursor.execute(query)
# result = mycursor.fetchall()
# if len(list(result)) > 0:
# print(tabulate(result, headers=[
# "Product Category", "Model", "Price", "Warranty", "Inventory"]))
# print("\n")
# listOfProd = list(result)
# option = input(
# "Do you want to search the individual items (Y/N) ? ")
# if option == 'Y':
# print("There are a total of " + str(len(result)) +
# " products to select your item")
# selection = input("Select your option: ")
# print("Searching for products...\n")
# listOfItemAvailable(listOfProd, int(selection) - 1)
# else:
# print("Invalid search\n")
# except:
# mysqldb.rollback()
# mysqldb.close()
# def listOfItemAvailable(keyword, index):
# from tabulate import tabulate
# mysqldb, mycursor = connectDB()
# try:
# query = f"SELECT ItemID, Category, Color, Factory, PowerSupply, Model, ProductionYear " \
# f"FROM business.items s WHERE s.Category = '{keyword[index][0]}' AND s.Model = '{keyword[index][1]}' " \
# f"AND s.PurchaseStatus = 'Unsold'"
# mycursor.execute(query)
# result = mycursor.fetchall()
# if len(list(result)) > 0:
# print(tabulate(result, headers=["ItemID", "Category", "Color", "Factory", "PowerSupply",
# "Model", "Production Year"]))
# print("\n")
# else:
# print("There are no items available\n")
# except:
# mysqldb.rollback()
# mysqldb.close()
# def basicProductFilterByPrice(price):
# from tabulate import tabulate
# mysqldb, mycursor = connectDB()
# try:
# query = f"SELECT Category, Model, Price, Warranty, Inventory FROM business.products WHERE Price <= {price}"
# mycursor.execute(query)
# result = mycursor.fetchall()
# if len(list(result)) > 0:
# print(tabulate(result, headers=[
# "Product Category", "Model", "Price", "Warranty", "Inventory"]))
# print('\n')
# listOfProd = list(result)
# option = input(
# "Do you want to search the individual items (Y/N) ? ")
# if option == 'Y':
# print("There are a total of " + str(len(result)) +
# " products to select your item")
# selection = input("Select your option: ")
# print("Searching for products...\n")
# listOfItemAvailable(listOfProd, int(selection) - 1)
# else:
# print("Invalid search\n")
# except:
# mysqldb.rollback()
# mysqldb.close()
# def basicProductFilterByColor(color):
# from tabulate import tabulate
# mysqldb, mycursor = connectDB()
# try:
# query = f"SELECT Category, Model, Price, Warranty, (SELECT COUNT(s.ItemID) FROM business.items s WHERE " \
# f"s.Category = p.Category AND s.Model = p.Model AND s.PurchaseStatus = 'Unsold' " \
# f"AND s.Color = '{color}') FROM business.products p"
# mycursor.execute(query)
# result = mycursor.fetchall()
# if len(list(result)) > 0:
# print(tabulate(result, headers=[
# "Product Category", "Model", "Price", "Warranty", "Inventory"]))
# print("\n")
# listOfProd = list(result)
# option = input(
# "Do you want to search the individual items (Y/N) ? ")
# if option == 'Y':
# print("There are a total of " + str(len(result)) +
# " products to select your item")
# selection = input("Select your option: ")
# print("Searching for products...\n")
# listOfItemAvailableByColor(
# listOfProd, int(selection) - 1, color)
# else:
# print("Invalid search\n")
# except:
# mysqldb.rollback()
# mysqldb.close()
# def listOfItemAvailableByColor(keyword, index, color):
# from tabulate import tabulate
# mysqldb, mycursor = connectDB()
# try:
# query = f"SELECT ItemID, Category, Color, Factory, PowerSupply, Model, ProductionYear " \
# f"FROM items s WHERE s.Category = '{keyword[index][0]}' AND s.Model = '{keyword[index][1]}' " \
# f"AND s.PurchaseStatus = 'Unsold' AND s.Color = '{color}'"
# mycursor.execute(query)
# result = mycursor.fetchall()
# if len(list(result)) > 0:
# print(tabulate(result, headers=["ItemID", "Category", "Color", "Factory", "PowerSupply",
# "Model", "Production Year"]))
# print("\n")
# else:
# print("There are no items available\n")
# except:
# mysqldb.rollback()
# mysqldb.close()