-
Notifications
You must be signed in to change notification settings - Fork 3
/
addPerishableWidget.py
163 lines (150 loc) · 5.56 KB
/
addPerishableWidget.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
import connectDb
from AddNewPerishable import Ui_AddNewPerishable
from PyQt4 import QtGui, QtCore
def isNumber(s):
try:
float(s)
return True
except ValueError:
return False
class AddNewPerishable(QtGui.QWidget):
def __init__(self, parent):
QtGui.QWidget.__init__(self)
self.ui = Ui_AddNewPerishable()
self.ui.setupUi(self)
self.ui.create.clicked.connect(self.addProduct)
self.parent = parent
def addProduct(self):
barcode = self.ui.lineEdit_Barcode.text()
name = str(self.ui.lineEdit_Name.text())
category = str(self.ui.lineEdit_Category.text())
manu = str(self.ui.lineEdit_Manu.text())
price = self.ui.lineEdit_Price.text()
stock = self.ui.lineEdit_Stock.text()
expiry = self.ui.lineEdit_Expiry.text()
self.ui.error_Expiry.clear()
self.ui.error_Barcode.clear()
self.ui.error_Price.clear()
self.ui.error_Stock.clear()
self.ui.error_Name.clear()
flag = self.validateInput(barcode, name, category, manu, price, stock, expiry)
if flag != 0 & (len(str(barcode)) == 8):
year = expiry[:4]
month = expiry[5:7]
day = expiry[8:]
if isNumber(year) & isNumber(month) & isNumber(day) & (int(barcode)>=10000000) & (float(price)>0) & (int(stock)>0) & (int(barcode)<=99999999):
dateInput = QtCore.QDate(int(year), int(month), int(day))
if not dateInput:
self.ui.lineEdit_Expiry.clear()
self.ui.error_Expiry.setText("*incorrect input")
else:
today = QtCore.QDate.currentDate()
if today>dateInput:
self.ui.lineEdit_Expiry.clear()
self.ui.error_Expiry.setText("*incorrect date")
else:
barcode = int(barcode)
price = float(price)
stock = int(stock)
conn, cur = connectDb.connectToDatabase()
checkForExistence = "SELECT count(*) FROM product WHERE product.barcode = %d ;" % barcode
cur.execute(checkForExistence)
count = cur.fetchone()
count = count[0]
if count == 0:
if len(category) == 0:
if len(manu) == 0:
insertProduct = "INSERT INTO product VALUES(%d, '%s', NULL, NULL, %.2f, %d, 1)" % (barcode, name, price, stock)
else:
insertProduct = "INSERT INTO product VALUES(%d, '%s', NULL, '%s', %.2f, %d, 1)" % (barcode, name, manu, price, stock)
else:
if len(manu) == 0:
insertProduct = "INSERT INTO product VALUES(%d, '%s', '%s', NULL, %.2f, %d, 1)" % (barcode, name, category, price, stock)
else:
insertProduct = "INSERT INTO product VALUES(%d, '%s', '%s', '%s', %.2f, %d, 1)" % (barcode, name, category, manu, price, stock)
cur.execute(insertProduct)
insertBatch = "INSERT INTO batch VALUES(%d, CURDATE(), %d, '%s')" % (barcode, stock, expiry)
cur.execute(insertBatch)
else:
self.ui.message.setText("Cannot Add Product since the barcode already exists")
conn.commit()
connectDb.closeDatabaseConnection(conn, cur)
self.parent.viewPerishables()
self.close()
else:
if (int(barcode) > 99999999) | (int(barcode)<10000000):
self.ui.lineEdit_Barcode.clear()
self.ui.error_Barcode.setText("*incorrect input")
elif float(price) <= 0:
self.ui.lineEdit_Price.clear()
self.ui.error_Price.setText("*incorrect input")
elif int(stock) <= 0:
self.ui.lineEdit_Stock.clear()
self.ui.error_Stock.setText("*incorrect input")
else:
self.ui.lineEdit_Expiry.clear()
self.ui.error_Expiry.setText("*incorrect input")
else:
self.ui.lineEdit_Barcode.clear()
self.ui.lineEdit_Expiry.clear()
self.ui.lineEdit_Stock.clear()
self.ui.lineEdit_Price.clear()
self.ui.lineEdit_Manu.clear()
self.ui.lineEdit_Category.clear()
self.ui.lineEdit_Name.clear()
def validateInput(self, barcode, name, category, manu, price, stock, expiry):
flag = 1
if len(str(barcode)) == 8:
if isNumber(barcode):
barcode = int(barcode)
else:
flag = 0
self.ui.error_Barcode.setText("*incorrect input")
else:
flag = 0
self.ui.error_Barcode.setText("*incorrect input")
if len(str(price)) > 0:
if isNumber(price):
price = float(price)
else:
flag = 0
self.ui.error_Price.setText("*incorrect input")
else:
flag = 0
self.ui.error_Price.setText("*required")
if len(str(stock)) > 0:
if isNumber(stock):
stock = int(stock)
else:
flag = 0
self.ui.error_Stock.setText("*incorrect input")
else:
flag = 0
self.ui.error_Stock.setText("*required")
if len(name) == 0:
flag = 0
self.ui.error_Name.setText("*required")
if len(expiry) == 0:
flag = 0
self.ui.error_Expiry.setText("*required")
else:
year = expiry[:4]
month = expiry[5:7]
day = expiry[8:]
if isNumber(year) & isNumber(month) & isNumber(day):
dateInput = QtCore.QDate(int(year), int(month), int(day))
if not dateInput:
flag = 0
self.ui.error_Expiry.setText("*incorrect input")
else:
today = QtCore.QDate.currentDate()
print today
print dateInput
print today<dateInput
if today>dateInput:
flag = 0
self.ui.error_Expiry.setText("*incorrect date")
else:
flag = 0
self.ui.error_Expiry.setText("*incorrect input")
return flag