-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
794 lines (711 loc) · 33 KB
/
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
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
# Form implementation generated from reading ui file 'gui.ui'
#
# Created by: PyQt5 UI code generator 6.4.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QTableWidgetItem, QTableWidget,QDialog, QVBoxLayout, QLabel, QLineEdit, QDialogButtonBox,QFileDialog
from PyQt5.QtCore import Qt
import sys, ressource_rc, sqlite3, csv
from datetime import datetime
class CouponDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Coupon Dialog")
# Create the layout
layout = QVBoxLayout(self)
# Create the coupon label and input field
coupon_label = QLabel("Coupon:")
self.coupon_input = QLineEdit()
# Create the description label and input field
description_label = QLabel("Description:")
self.description_input = QLineEdit()
# Create the dialog buttons
button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
# Connect the accepted and rejected signals to appropriate slots
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)
# Add the widgets to the layout
layout.addWidget(coupon_label)
layout.addWidget(self.coupon_input)
layout.addWidget(description_label)
layout.addWidget(self.description_input)
layout.addWidget(button_box)
def get_coupon_description(self):
# Execute the dialog as a modal dialog
if self.exec() == QDialog.DialogCode.Accepted:
# Return the entered coupon value and description
return self.coupon_input.text(), self.description_input.text()
else:
return None, None
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(860, 720)
MainWindow.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(parent=MainWindow)
self.centralwidget.setStyleSheet(
"#header{\n"
" background-color: rgb(236, 236, 236);\n"
"}\n"
"#sub_header{\n"
" background-color: rgb(236, 236, 236);\n"
" border: 2px solid rgb(0, 170, 0);\n"
" margin: 30px;\n"
" border-radius:10px;\n"
"}\n"
"#footer{\n"
" background-color: rgb(236, 236, 236);\n"
" border: 2px solid rgb(0, 170, 0);\n"
" margin: 30px;\n"
" border-radius:10px;\n"
"}\n"
"#body{\n"
" background-color: rgb(236, 236, 236);\n"
"}\n"
""
)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setObjectName("verticalLayout")
self.header = QtWidgets.QWidget(parent=self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Fixed
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.header.sizePolicy().hasHeightForWidth())
self.header.setSizePolicy(sizePolicy)
self.header.setMaximumSize(QtCore.QSize(16777215, 100))
font = QtGui.QFont()
font.setPointSize(18)
self.header.setFont(font)
self.header.setAutoFillBackground(False)
self.header.setObjectName("header")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.header)
self.horizontalLayout.setObjectName("horizontalLayout")
self.import_2 = QtWidgets.QPushButton(parent=self.header)
self.import_2.clicked.connect(self.import_csv_to_db)
self.import_2.setStyleSheet(
"""
QPushButton{
background-color: rgb(255, 255, 255);
border-radius:10px
}
QPushButton:hover{
background-color: rgb(230, 230, 230);
border-radius:10px
}
"""
)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Policy.Fixed, QtWidgets.QSizePolicy.Policy.Fixed
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.import_2.sizePolicy().hasHeightForWidth())
self.import_2.setSizePolicy(sizePolicy)
self.import_2.setMinimumSize(QtCore.QSize(300, 50))
font = QtGui.QFont()
font.setFamily("Poppins")
font.setPointSize(18)
self.import_2.setFont(font)
self.import_2.setAutoFillBackground(False)
self.import_2.setFlat(True)
self.import_2.setObjectName("import_2")
self.horizontalLayout.addWidget(self.import_2)
self.export_2 = QtWidgets.QPushButton(parent=self.header)
self.export_2.clicked.connect(self.export_db_to_csv)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Policy.Fixed, QtWidgets.QSizePolicy.Policy.Fixed
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.export_2.sizePolicy().hasHeightForWidth())
self.export_2.setSizePolicy(sizePolicy)
self.export_2.setMinimumSize(QtCore.QSize(270, 50))
font = QtGui.QFont()
font.setFamily("Poppins")
font.setPointSize(18)
self.export_2.setFont(font)
self.export_2.setAutoFillBackground(False)
self.export_2.setStyleSheet(
"""
QPushButton{
background-color: rgb(255, 255, 255);
border-radius:10px
}
QPushButton:hover{
background-color: rgb(230, 230, 230);
border-radius:10px
}
"""
)
self.export_2.setFlat(True)
self.export_2.setObjectName("export_2")
self.horizontalLayout.addWidget(self.export_2)
self.remaining = QtWidgets.QLabel(parent=self.header)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Policy.Fixed, QtWidgets.QSizePolicy.Policy.Fixed
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.remaining.sizePolicy().hasHeightForWidth())
self.remaining.setSizePolicy(sizePolicy)
self.remaining.setMinimumSize(QtCore.QSize(250, 60))
font = QtGui.QFont()
font.setFamily("Poppins")
font.setPointSize(18)
self.remaining.setFont(font)
self.remaining.setAutoFillBackground(False)
self.remaining.setStyleSheet(
"background-color: rgb(255, 170, 0);\n" "border-radius:10px"
)
self.remaining.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.remaining.setObjectName("remaining")
self.horizontalLayout.addWidget(self.remaining)
self.verticalLayout.addWidget(self.header)
self.sub_header = QtWidgets.QWidget(parent=self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Fixed
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.sub_header.sizePolicy().hasHeightForWidth())
self.sub_header.setSizePolicy(sizePolicy)
self.sub_header.setMinimumSize(QtCore.QSize(0, 200))
self.sub_header.setMaximumSize(QtCore.QSize(16777215, 220))
self.sub_header.setAutoFillBackground(False)
self.sub_header.setObjectName("sub_header")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.sub_header)
self.horizontalLayout_2.setContentsMargins(30, 30, 30, 30)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.create_section = QtWidgets.QWidget(parent=self.sub_header)
self.create_section.setObjectName("create_section")
self.gridLayout = QtWidgets.QGridLayout(self.create_section)
self.gridLayout.setObjectName("gridLayout")
self.coupon_section = QtWidgets.QWidget(parent=self.create_section)
self.coupon_section.setObjectName("coupon_section")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.coupon_section)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.coupon_label = QtWidgets.QLabel(parent=self.coupon_section)
font = QtGui.QFont()
font.setPointSize(15)
self.coupon_label.setFont(font)
self.coupon_label.setStyleSheet("color:rgb(100, 100, 100)")
self.coupon_label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.coupon_label.setObjectName("coupon_label")
self.verticalLayout_2.addWidget(self.coupon_label)
self.coupon_input = QtWidgets.QLineEdit(parent=self.coupon_section)
font = QtGui.QFont()
font.setPointSize(13)
self.coupon_input.setFont(font)
self.coupon_input.setStyleSheet("border:none;\n" "border-radius:10px;")
self.coupon_input.setObjectName("coupon_input")
self.verticalLayout_2.addWidget(self.coupon_input)
self.gridLayout.addWidget(self.coupon_section, 2, 0, 1, 1)
self.coupon_section_2 = QtWidgets.QWidget(parent=self.create_section)
self.coupon_section_2.setObjectName("coupon_section_2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.coupon_section_2)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.coupon_label_2 = QtWidgets.QLabel(parent=self.coupon_section_2)
font = QtGui.QFont()
font.setPointSize(15)
self.coupon_label_2.setFont(font)
self.coupon_label_2.setStyleSheet("color:rgb(100, 100, 100)")
self.coupon_label_2.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.coupon_label_2.setObjectName("coupon_label_2")
self.verticalLayout_3.addWidget(self.coupon_label_2)
self.coupon_input_2 = QtWidgets.QLineEdit(parent=self.coupon_section_2)
font = QtGui.QFont()
font.setPointSize(13)
self.coupon_input_2.setFont(font)
self.coupon_input_2.setStyleSheet("border:none;\n" "border-radius:10px;")
self.coupon_input_2.setText("")
self.coupon_input_2.setObjectName("coupon_input_2")
self.verticalLayout_3.addWidget(self.coupon_input_2)
self.gridLayout.addWidget(self.coupon_section_2, 0, 0, 2, 1)
self.create_coupon = QtWidgets.QPushButton(parent=self.create_section)
self.create_coupon.clicked.connect(self.add_coupon_to_db)
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(True)
self.create_coupon.setFont(font)
self.create_coupon.setStyleSheet(
"QPushButton {background-color: rgb(0, 170, 0);\n"
"border-radius: 10px;\n"
"color: rgb(255, 255, 255);\n"
"padding:10px}\n"
"\n"
"QPushButton:hover {\n"
" background-color: rgb(138, 226, 52);\n"
"}"
)
self.create_coupon.setIconSize(QtCore.QSize(40, 40))
self.create_coupon.setDefault(False)
self.create_coupon.setFlat(True)
self.create_coupon.setObjectName("create_coupon")
self.gridLayout.addWidget(self.create_coupon, 0, 2, 1, 1)
self.start_section = QtWidgets.QWidget(parent=self.create_section)
self.start_section.setMinimumSize(QtCore.QSize(200, 0))
self.start_section.setObjectName("start_section")
self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.start_section)
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.start_label = QtWidgets.QLabel(parent=self.start_section)
font = QtGui.QFont()
font.setPointSize(15)
self.start_label.setFont(font)
self.start_label.setStyleSheet("color:rgb(100, 100, 100)")
self.start_label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.start_label.setObjectName("start_label")
self.verticalLayout_5.addWidget(self.start_label)
self.start = QtWidgets.QDateTimeEdit(parent=self.start_section)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Policy.Fixed, QtWidgets.QSizePolicy.Policy.Fixed
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.start.sizePolicy().hasHeightForWidth())
self.start.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(-1)
font.setBold(True)
font.setItalic(False)
self.start.setFont(font)
self.start.setStyleSheet(
"QDateTimeEdit {\n"
" border: 2px solid #999999;\n"
" border-radius: 5px;\n"
" padding: 5px;\n"
" background-color: #FFFFFF;\n"
" color: #333333;\n"
" font-size:15px;\n"
" font-weight:bold\n"
"}\n"
"\n"
"QDateTimeEdit::drop-down {\n"
" subcontrol-origin: padding;\n"
" subcontrol-position: right center;\n"
" width: 20px;\n"
" border:none\n"
"}\n"
"\n"
"QDateTimeEdit::down-arrow {\n"
" image: url(:/ressource/icons/chevron-down.svg);\n"
" width: 20px;\n"
" height: auto;\n"
"}\n"
"QDateTimeEdit::up-arrow {\n"
" image: url(:/ressource/icons/chevron-up.svg);\n"
"}"
)
self.start.setDate(QtCore.QDate(2023, 9, 14))
self.start.setMinimumDateTime(
QtCore.QDateTime(QtCore.QDate(2000, 1, 1), QtCore.QTime(0, 0, 0))
)
self.start.setCurrentSection(QtWidgets.QDateTimeEdit.Section.DaySection)
self.start.setCalendarPopup(True)
self.start.setObjectName("start")
self.verticalLayout_5.addWidget(self.start)
self.gridLayout.addWidget(self.start_section, 0, 1, 3, 1)
self.end_section = QtWidgets.QWidget(parent=self.create_section)
self.end_section.setObjectName("end_section")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.end_section)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.end_label_2 = QtWidgets.QLabel(parent=self.end_section)
font = QtGui.QFont()
font.setPointSize(15)
self.end_label_2.setFont(font)
self.end_label_2.setStyleSheet("color:rgb(100, 100, 100)")
self.end_label_2.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.end_label_2.setObjectName("end_label_2")
self.verticalLayout_4.addWidget(self.end_label_2)
self.end = QtWidgets.QDateTimeEdit(parent=self.end_section)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Policy.Fixed, QtWidgets.QSizePolicy.Policy.Fixed
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.end.sizePolicy().hasHeightForWidth())
self.end.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(-1)
font.setBold(True)
font.setItalic(False)
self.end.setFont(font)
self.end.setStyleSheet(
"QDateTimeEdit {\n"
" border: 2px solid #999999;\n"
" border-radius: 5px;\n"
" padding: 5px;\n"
" background-color: #FFFFFF;\n"
" color: #333333;\n"
" font-size:15px;\n"
" font-weight:bold\n"
"}\n"
"\n"
"QDateTimeEdit::drop-down {\n"
" subcontrol-origin: padding;\n"
" subcontrol-position: right center;\n"
" width: 20px;\n"
" border:none\n"
"}\n"
"\n"
"QDateTimeEdit::down-arrow {\n"
" image: url(:/ressource/icons/chevron-down.svg);\n"
" width: 20px;\n"
" height: auto;\n"
"}\n"
"QDateTimeEdit::up-arrow {\n"
" image: url(:/ressource/icons/chevron-up.svg);\n"
"}"
)
self.end.setDate(QtCore.QDate(2023, 9, 14))
self.end.setMinimumDateTime(
QtCore.QDateTime(QtCore.QDate(2000, 1, 1), QtCore.QTime(0, 0, 0))
)
self.end.setCurrentSection(QtWidgets.QDateTimeEdit.Section.DaySection)
self.end.setCalendarPopup(True)
self.end.setObjectName("end")
self.verticalLayout_4.addWidget(self.end)
self.gridLayout.addWidget(self.end_section, 1, 2, 2, 1)
self.horizontalLayout_2.addWidget(self.create_section)
self.verticalLayout.addWidget(self.sub_header)
self.body = QtWidgets.QWidget(parent=self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Policy.Preferred,
QtWidgets.QSizePolicy.Policy.Preferred,
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.body.sizePolicy().hasHeightForWidth())
self.body.setSizePolicy(sizePolicy)
self.body.setMinimumSize(QtCore.QSize(0, 200))
font = QtGui.QFont()
font.setKerning(False)
self.body.setFont(font)
self.body.setAutoFillBackground(False)
self.body.setObjectName("body")
self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.body)
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.tableWidget = QtWidgets.QTableWidget(parent=self.body)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.tableWidget.setColumnCount(5) # Set the number of columns
self.tableWidget.setRowCount(20) # Set the number of rows
# Set custom column titles
column_titles = ["Coupon", "Description", "Start", "End", "State"]
self.tableWidget.setHorizontalHeaderLabels(column_titles)
self.tableWidget.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
self.horizontalLayout_5.addWidget(self.tableWidget)
self.widget = QtWidgets.QWidget(parent=self.body)
self.widget.setObjectName("widget")
self.verticalLayout_6 = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout_6.setObjectName("verticalLayout_6")
self.pushButton = QtWidgets.QPushButton(parent=self.widget)
self.pushButton.clicked.connect(self.delete_selected_row)
self.pushButton.setText("")
icon = QtGui.QIcon()
icon.addPixmap(
QtGui.QPixmap(":/ressource/icons/trash-2.svg"),
QtGui.QIcon.Mode.Normal,
QtGui.QIcon.State.Off,
)
self.pushButton.setIcon(icon)
self.pushButton.setIconSize(QtCore.QSize(30, 30))
self.pushButton.setObjectName("pushButton")
self.verticalLayout_6.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(parent=self.widget)
self.pushButton_2.clicked.connect(self.edit_values)
self.pushButton_2.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(
QtGui.QPixmap(":/ressource/icons/edit.svg"),
QtGui.QIcon.Mode.Normal,
QtGui.QIcon.State.Off,
)
self.pushButton_2.setIcon(icon1)
self.pushButton_2.setIconSize(QtCore.QSize(30, 30))
self.pushButton_2.setObjectName("pushButton_2")
self.verticalLayout_6.addWidget(self.pushButton_2)
self.pushButton_3 = QtWidgets.QPushButton(parent=self.widget)
self.pushButton_3.clicked.connect(self.display_data_in_table)
self.pushButton_3.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(
QtGui.QPixmap(":/ressource/icons/refresh-ccw.svg"),
QtGui.QIcon.Mode.Normal,
QtGui.QIcon.State.Off,
)
self.pushButton_3.setIcon(icon2)
self.pushButton_3.setIconSize(QtCore.QSize(30, 30))
self.pushButton_3.setObjectName("pushButton_3")
self.verticalLayout_6.addWidget(self.pushButton_3)
self.horizontalLayout_5.addWidget(self.widget)
self.verticalLayout.addWidget(self.body)
self.footer = QtWidgets.QWidget(parent=self.centralwidget)
self.footer.setMaximumSize(QtCore.QSize(16777215, 200))
self.footer.setObjectName("footer")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.footer)
self.horizontalLayout_3.setContentsMargins(30, 30, 30, 30)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.test_coupon_wrapper = QtWidgets.QWidget(parent=self.footer)
self.test_coupon_wrapper.setMaximumSize(QtCore.QSize(16777215, 200))
self.test_coupon_wrapper.setObjectName("test_coupon_wrapper")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.test_coupon_wrapper)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.test_coupon_input = QtWidgets.QLineEdit(parent=self.test_coupon_wrapper)
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
self.test_coupon_input.setFont(font)
self.test_coupon_input.setObjectName("test_coupon_input")
self.horizontalLayout_4.addWidget(self.test_coupon_input)
self.test_coupon_button = QtWidgets.QPushButton(parent=self.test_coupon_wrapper)
self.test_coupon_button.clicked.connect(self.test_coupon)
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(True)
self.test_coupon_button.setFont(font)
self.test_coupon_button.setStyleSheet(
"QPushButton {background-color: rgb(0, 170, 0);\n"
"border-radius: 10px;\n"
"color: rgb(255, 255, 255);\n"
"padding:10px}\n"
"\n"
"QPushButton:hover {\n"
" background-color: rgb(138, 226, 52);\n"
"}"
)
self.test_coupon_button.setObjectName("test_coupon_button")
self.horizontalLayout_4.addWidget(self.test_coupon_button)
self.test_coupon_label = QtWidgets.QLabel(parent=self.test_coupon_wrapper)
self.test_coupon_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.test_coupon_label.setMinimumSize(QtCore.QSize(150, 0))
font = QtGui.QFont()
font.setPointSize(18)
self.test_coupon_label.setFont(font)
self.test_coupon_label.setText("")
self.test_coupon_label.setObjectName("test_coupon_label")
self.horizontalLayout_4.addWidget(self.test_coupon_label)
self.horizontalLayout_3.addWidget(self.test_coupon_wrapper)
self.verticalLayout.addWidget(self.footer)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.display_data_in_table()
def import_csv_to_db(self):
# Prompt the user to choose a CSV file
file_path, _ = QFileDialog.getOpenFileName(None, "Select CSV File")
if not file_path:
return
# Connect to the SQLite3 database
conn = sqlite3.connect("coupons.db")
cursor = conn.cursor()
# Truncate the existing table
cursor.execute("DELETE FROM coupons")
# Read data from the CSV file
with open(file_path, "r") as file:
csv_reader = csv.reader(file)
next(csv_reader) # Skip the header row
# Insert data into the table
for row in csv_reader:
# Extract values from the CSV row
id_val = int(row[0]) # Assuming id is the first column
coupon = row[1]
description = row[2]
start = row[3]
end = row[4]
state = row[5]
# Insert the data into the table
cursor.execute("INSERT INTO coupons (row_index, coupon, description, start, end, state) VALUES (?, ?, ?, ?, ?, ?)",
(id_val, coupon, description, start, end, state))
# Commit the changes and close the database connection
conn.commit()
conn.close()
self.display_data_in_table()
def export_db_to_csv(self):
# Prompt the user to choose a folder
folder_path = QFileDialog.getExistingDirectory(None, "Select Folder")
if not folder_path:
return
# Connect to the SQLite3 database
conn = sqlite3.connect("coupons.db")
cursor = conn.cursor()
# Execute a SELECT query to fetch all data from the table
cursor.execute("SELECT * FROM coupons")
rows = cursor.fetchall()
# Get the column names from the cursor description
column_names = [description[0] for description in cursor.description]
# Create the output file path
output_file = folder_path + "/coupons.csv"
# Export data to CSV file
with open(output_file, "w") as file:
# Write the column names as the first row
file.write(",".join(column_names) + "\n")
# Write each row of data to the CSV file
for row in rows:
file.write(",".join(str(value) for value in row) + "\n")
# Close the database connection
conn.close()
def count_active_states(self):
conn = sqlite3.connect("coupons.db")
cursor = conn.cursor()
# Count the number of rows with 'active' state
cursor.execute("SELECT COUNT(*) FROM coupons WHERE state = 'active'")
active_count = cursor.fetchone()[0]
# Count the total number of rows in the table
cursor.execute("SELECT COUNT(*) FROM coupons")
total_rows = cursor.fetchone()[0]
conn.close()
self.remaining.setText(f"{active_count}/{total_rows} remaining")
def edit_values(self):
selected_items = self.tableWidget.selectedItems()
if selected_items:
values = CouponDialog().get_coupon_description()
if values[0].strip() == "" and values[0].strip() == "":
return
conn = sqlite3.connect("coupons.db")
cursor = conn.cursor()
query = "UPDATE coupons SET coupon = ?, description = ? WHERE row_index = ?"
cursor.execute(query, (values[0], values[1], selected_items[0].row()+1))
conn.commit()
conn.close()
self.display_data_in_table()
def delete_selected_row(self):
selected_items = self.tableWidget.selectedItems()
if selected_items:
row = selected_items[0].row()
self.tableWidget.removeRow(row)
conn = sqlite3.connect("coupons.db")
cursor = conn.cursor()
query = "DELETE FROM coupons WHERE row_index = ?"
cursor.execute(query, (row + 1,)) # Add 1 to the row number to match SQLite IDs
conn.commit()
conn.close()
self.display_data_in_table()
def add_coupon_to_db(self):
# Create a connection to the SQLite database
conn = sqlite3.connect("coupons.db")
cursor = conn.cursor()
coupon = self.coupon_input.text()
description = self.coupon_input_2.text()
start_date_str = self.start.text()
end_date_str = self.end.text()
# Parse the date strings into datetime objects
start_date = datetime.strptime(start_date_str, "%d/%m/%Y %I:%M %p")
end_date = datetime.strptime(end_date_str, "%d/%m/%Y %I:%M %p")
# Format the dates as strings
start_date_str = start_date.strftime("%d/%m/%Y %I:%M %p")
end_date_str = end_date.strftime("%d/%m/%Y %I:%M %p")
# Insert the new coupon into the database
cursor.execute(
"INSERT INTO coupons (coupon, description, start, end, state) VALUES (?, ?, ?, ?, ?)",
(coupon, description, start_date_str, end_date_str, ""),
)
# Commit the database changes and close the connection
conn.commit()
conn.close()
self.coupon_input.setText("")
self.coupon_input_2.setText("")
self.display_data_in_table()
def update_row_index(self):
# Connect to the database
conn = sqlite3.connect('coupons.db')
cursor = conn.cursor()
# Retrieve all rows from the coupons table
cursor.execute("SELECT rowid, * FROM coupons")
rows = cursor.fetchall()
# Iterate over the rows and update the row_index value
for index, row in enumerate(rows, start=1):
rowid = row[0]
cursor.execute("UPDATE coupons SET row_index = ? WHERE rowid = ?", (index, rowid))
# Commit the changes and close the connection
conn.commit()
conn.close()
def test_coupon(self):
val = self.test_coupon_input.text()
conn = sqlite3.connect('coupons.db') # Replace 'your_database.db' with the actual name of your database file
cursor = conn.cursor()
# Assuming your table is named 'coupons'
query = "SELECT state FROM coupons WHERE coupon = ? AND state = 'active'"
cursor.execute(query, (val,))
result = cursor.fetchone()
conn.close()
if result is not None:
self.test_coupon_label.setText("Active")
else:
self.test_coupon_label.setText("Inactive")
def display_data_in_table(self):
# Create a connection to the SQLite database
conn = sqlite3.connect("coupons.db")
cursor = conn.cursor()
# Retrieve data from the "coupons" table
cursor.execute("SELECT * FROM coupons")
data = cursor.fetchall()
if len(data) == 0:
self.tableWidget.setRowCount(0)
self.tableWidget.setColumnCount(0)
return
# Set the number of rows and columns in the table widget
self.tableWidget.setRowCount(len(data))
self.tableWidget.setColumnCount(len(data[0])-1)
columns = ["Coupon", "Description","Start","End", "State"]
self.tableWidget.setHorizontalHeaderLabels(columns)
# Populate the table widget with the retrieved data
for row, row_data in enumerate(data):
for col, value in enumerate(row_data[1:]):
if col == 4: # Column index of the "state" column
# Check the dates and update the "state" cell accordingly
start_date = datetime.strptime(row_data[3], "%d/%m/%Y %I:%M %p")
end_date = datetime.strptime(row_data[4], "%d/%m/%Y %I:%M %p")
current_date = datetime.now().strftime("%d/%m/%Y %I:%M %p")
current_date = datetime.strptime(current_date, "%d/%m/%Y %I:%M %p")
if current_date < start_date:
value = "incoming"
elif current_date <= end_date:
value = "active"
else:
value = "expired"
# Update the "state" cell in the table widget
item = QTableWidgetItem(value)
self.tableWidget.setItem(row, col, item)
# Update the "state" column in the database
cursor.execute(
"UPDATE coupons SET state = ? WHERE coupon = ?",
(value, row_data[1]),
)
else:
# Populate other cells in the table widget
item = QTableWidgetItem(str(value))
self.tableWidget.setItem(row, col, item)
# Commit the database changes and close the connection
conn.commit()
conn.close()
self.count_active_states()
self.update_row_index()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Coupon managment tool"))
self.import_2.setText(_translate("MainWindow", "Import CSV"))
self.export_2.setText(_translate("MainWindow", "Export CSV"))
self.remaining.setText(_translate("MainWindow", "0/1 remaining"))
self.coupon_label.setText(_translate("MainWindow", "Coupon"))
self.coupon_label_2.setText(_translate("MainWindow", "Description"))
self.create_coupon.setText(_translate("MainWindow", "Create Coupon"))
self.start_label.setText(_translate("MainWindow", "Start time"))
self.start.setDisplayFormat(_translate("MainWindow", "d/M/yyyy h:mm AP"))
self.end_label_2.setText(_translate("MainWindow", "End time"))
self.end.setDisplayFormat(_translate("MainWindow", "d/M/yyyy h:mm AP"))
self.test_coupon_button.setText(_translate("MainWindow", "Test coupon"))
if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec())