-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselling.py
261 lines (215 loc) · 9.79 KB
/
selling.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
from PyQt5 import QtPrintSupport, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
from datetime import datetime
import sqlite3
import main_window
sqlConnect = sqlite3.connect("products.db")
cur = sqlConnect.cursor()
defaultImg = 'img/store.png'
class SellProduct(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Selling Product")
self.setWindowIcon(QIcon('icons/sell.svg'))
self.UI()
style_sheet = """
QLabel {
font-size: 15px;
}
QPushButton {
background-color: #5AA1C2;
color: #ffffff;
border-radius: 10px;
font: bold 14px;
min-width: 10em;
padding: 6px;
}
"""
self.setStyleSheet(style_sheet)
self.show()
def UI(self):
self.widgets()
self.layouts()
self.fetchData()
def widgets(self):
##############Widgets of Top Layout##############
self.productImg = QLabel(self)
self.img = QPixmap('icons/sell.svg')
self.productImg.setPixmap(self.img)
self.productImg.setAlignment(Qt.AlignCenter)
self.titleText = QLabel("Sell Product")
self.titleText.setStyleSheet("QLabel {font-size: 30px}")
self.titleText.setAlignment(Qt.AlignCenter)
##############Widgets of Bottom Layout##############
self.productCombo = QComboBox()
self.productCombo.currentIndexChanged.connect(self.changeComboProductValue)
self.memberCombo = QComboBox()
self.quantityCombo = QComboBox()
self.discountCombo = QComboBox()
self.submitBtn = QPushButton("Submit")
self.submitBtn.clicked.connect(self.sellProduct)
def layouts(self):
##############Main Layout##############
self.mainLayout = QVBoxLayout()
self.topLayout = QVBoxLayout()
self.bottomLayout = QFormLayout()
self.topFrame = QFrame()
self.bottomFrame = QFrame()
##############Add Widgets##############
##############Widgets of Top Layout##############
self.topLayout.addWidget(self.productImg)
self.topLayout.addWidget(self.titleText)
self.topFrame.setLayout(self.topLayout)
##############Widgets of Bottom Layout##############
self.bottomLayout.addRow(QLabel("Product: "), self.productCombo)
self.bottomLayout.addRow(QLabel("Selling to: "), self.memberCombo)
self.bottomLayout.addRow(QLabel("Quantity: "), self.quantityCombo)
self.bottomLayout.addRow(QLabel("Discount: "), self.discountCombo)
self.bottomLayout.addRow(QLabel(""), self.submitBtn)
self.bottomFrame.setLayout(self.bottomLayout)
self.mainLayout.addWidget(self.topFrame)
self.mainLayout.addWidget(self.bottomFrame)
self.setLayout(self.mainLayout)
def fetchData(self):
query1 = "SELECT * FROM products WHERE product_availability =?"
query2 = "SELECT member_id, member_fname from members"
products = cur.execute(query1, ('Available',)).fetchall()
members = cur.execute(query2).fetchall()
quantity = products[0][4]
for product in products:
self.productCombo.addItem(product[1], product[0])
for member in members:
self.memberCombo.addItem(member[1], member[0])
self.quantityCombo.clear()
for i in range(1, quantity + 1):
self.quantityCombo.addItem(str(i))
self.discountCombo.clear()
for discount in range(5, 51):
self.discountCombo.addItem(str(discount) + "%")
def changeComboProductValue(self):
self.quantityCombo.clear()
product_id = self.productCombo.currentData()
query = ("SELECT product_quota FROM products WHERE product_id =?")
quota = cur.execute(query, (product_id,)).fetchone()
for i in range(1, quota[0] + 1):
self.quantityCombo.addItem(str(i))
def sellProduct(self):
global productName, productId, memberName, memberId, quantity, discount, discount_text
productName = self.productCombo.currentText()
productId = self.productCombo.currentData()
memberName = self.memberCombo.currentText()
memberId = self.memberCombo.currentData()
quantity = int(self.quantityCombo.currentText())
discount_text = self.discountCombo.currentText()
discount = discount_text.split("%")
discount = int(discount[0])
self.confirm = ConfirmWindow()
self.close()
class ConfirmWindow(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Sell Product")
self.setWindowIcon(QIcon("icons/icon.ico"))
self.UI()
style_sheet = """
QLabel {
font-size: 15px;
}
QPushButton {
background-color: #5AA1C2;
color: #ffffff;
border-radius: 10px;
font: bold 14px;
min-width: 10em;
padding: 6px;
}
QTextEdit {
font-size: 15px
}
"""
self.setStyleSheet(style_sheet)
self.show()
def UI(self):
self.widgets()
self.layouts()
def widgets(self):
##############widgets of top layout################
self.productImg = QLabel()
self.img = QPixmap('icons/sell.svg')
self.productImg.setPixmap(self.img)
self.productImg.setAlignment(Qt.AlignCenter)
self.titleText = QLabel("Sell Product")
self.titleText.setStyleSheet("QLabel {font-size: 30px}")
self.titleText.setAlignment(Qt.AlignCenter)
##############widgets of bottom layout################
global productName, productId, memberName, memberId, quantity, price, discount, discount_text
priceQuery = "SELECT product_price FROM products WHERE product_id =?"
price = cur.execute(priceQuery, (productId,)).fetchone()
discount_amount = ((quantity * price[0]) * discount) / 100
self.amount = quantity * price[0] - discount_amount
self.productName = QLabel()
self.productName.setText(productName)
self.memberName = QLabel()
self.memberName.setText(memberName)
self.amountLabel = QLabel()
self.amountLabel.setText(str(price[0]) + "x" + str(quantity) + " = $" + str(self.amount))
x = datetime.now()
self.dateString = '{}/{}/{}'.format(x.day, x.month, x.year)
self.dateSold = QLabel()
self.dateSold.setText(self.dateString)
self.confirmBtn = QPushButton("Confirm")
self.confirmBtn.clicked.connect(self.confirm)
self.textEdit = QTextEdit(self)
self.textEdit.setReadOnly(True)
self.textEdit.append("########Selling Receipt#######")
self.textEdit.append("Product: " + productName)
self.textEdit.append("Customer Name: " + memberName)
self.textEdit.append("Quantity: " + str(quantity))
self.textEdit.append("Discount: " + str(discount_text))
self.textEdit.append("Amount: $" + str(price[0]) + "x" + str(quantity) + " = $" + str(self.amount))
def layouts(self):
self.mainLayout = QVBoxLayout()
self.topLayout = QVBoxLayout()
self.bottomLayout = QFormLayout()
self.topFrame = QFrame()
self.bottomFrame = QFrame()
##############Add Widgets################
self.topLayout.addWidget(self.titleText)
self.topLayout.addWidget(self.productImg)
self.topFrame.setLayout(self.topLayout)
self.bottomLayout.addWidget(self.textEdit)
self.bottomLayout.addRow(QLabel(""), self.confirmBtn)
self.bottomFrame.setLayout(self.bottomLayout)
self.mainLayout.addWidget(self.topFrame)
self.mainLayout.addWidget(self.bottomFrame)
self.setLayout(self.mainLayout)
def confirm(self):
global productName, productId, memberName, memberId, quantity, discount
try:
sellQuery = "INSERT INTO 'sellings' (selling_product_id, selling_member_id, selling_quantity, discount_rate, selling_amount, selling_date) VALUES (?,?,?,?,?,?)"
quotaQuery = ("SELECT product_quota FROM products WHERE product_id =?")
cur.execute(sellQuery, (productId, memberId, quantity, discount, self.amount, self.dateString))
self.quota = cur.execute(quotaQuery, (productId,)).fetchone()
sqlConnect.commit()
if (quantity == self.quota[0]):
updateQuotaQuery = "UPDATE products set product_quota =?, product_availability =? WHERE product_id =? "
cur.execute(updateQuotaQuery, (0, 'UnAvailable', productId))
sqlConnect.commit()
else:
newQuota = (self.quota[0] - quantity)
updateQuotaQuery = "UPDATE products set product_quota =? WHERE product_id =?"
cur.execute(updateQuotaQuery, (newQuota, productId))
sqlConnect.commit()
mbox = QMessageBox.question(self, "Success", "Would you like to print a selling receipt?", QMessageBox.Yes | QMessageBox.No)
if mbox == QMessageBox.Yes:
self.printReceipt()
self.close()
except:
QMessageBox.information(self, "Info", "Something went wrong")
def printReceipt(self):
# Create printer
dialog = QtPrintSupport.QPrintDialog()
if dialog.exec_() == QtWidgets.QDialog.Accepted:
self.textEdit.document().print_(dialog.printer())