-
Notifications
You must be signed in to change notification settings - Fork 3
/
writeoffWidget.py
153 lines (144 loc) · 7.24 KB
/
writeoffWidget.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
import connectDb
import getInfo
from Writeoff import Ui_PerformWriteoff
from PyQt4 import QtGui, QtCore
def isNumber(s):
try:
float(s)
return True
except ValueError:
return False
class PerformWriteoff(QtGui.QWidget):
def __init__(self, parent):
QtGui.QWidget.__init__(self)
self.ui = Ui_PerformWriteoff()
self.ui.setupUi(self)
self.ui.nextItem.clicked.connect(self.nextProduct)
self.ui.writeoff.clicked.connect(self.completeWriteoff)
self.parent = parent
self.items = {}
def nextProduct(self):
self.ui.error_barcode.clear()
self.ui.error_qty.clear()
barcode = self.ui.lineEdit_barcode.text()
qty = self.ui.lineEdit_qty.text()
if (len(str(barcode)) == 8) & (len(str(qty)) > 0):
if (isNumber(barcode)) & (isNumber(qty)):
barcode = int(barcode)
qty = int(qty)
if (qty>0):
conn, cur = connectDb.connectToDatabase()
checkExistence = "SELECT count(*) FROM product WHERE active = 1 AND barcode = %d" % barcode
checkQty = "SELECT count(*) FROM product WHERE active = 1 AND barcode = %d AND stocklevel >= %d" % (barcode, qty)
cur.execute(checkQty)
count = cur.fetchone()
count = count[0]
if count != 0:
self.items[barcode] = qty
self.ui.lineEdit_qty.clear()
self.ui.lineEdit_barcode.clear()
else:
cur.execute(checkExistence)
count = cur.fetchone()
count = count[0]
if count != 0:
self.ui.lineEdit_qty.clear()
self.ui.error_qty.setText("*not enough stock")
else:
self.ui.lineEdit_qty.clear()
self.ui.lineEdit_barcode.clear()
self.ui.error_barcode.setText("*no such product")
connectDb.closeDatabaseConnection(conn, cur)
else:
self.ui.lineEdit_qty.clear()
self.ui.error_qty.setText("*incorrect input")
else:
if not isNumber(barcode):
self.ui.lineEdit_qty.clear()
self.ui.lineEdit_barcode.clear()
self.ui.error_barcode.setText("*incorrect input")
if not isNumber(qty):
self.ui.lineEdit_qty.clear()
self.ui.error_qty.setText("*incorrect input")
else:
if (len(str(qty)) == 0):
self.ui.error_qty.setText("*required")
if (len(str(barcode)) == 0):
self.ui.lineEdit_qty.clear()
self.ui.error_barcode.setText("*required")
elif (len(str(barcode)) != 8):
self.ui.lineEdit_qty.clear()
self.ui.error_barcode.setText("*incorrect input")
def completeWriteoff(self):
self.ui.error_barcode.clear()
self.ui.error_qty.clear()
barcode = self.ui.lineEdit_barcode.text()
qty = self.ui.lineEdit_qty.text()
if (len(str(barcode)) == 8) & (len(str(qty)) > 0):
if (isNumber(barcode)) & (isNumber(qty)):
barcode = int(barcode)
qty = int(qty)
if (qty < 0):
conn, cur = connectDb.connectToDatabase()
checkExistence = "SELECT count(*) FROM product WHERE active = 1 AND barcode = %d" % barcode
checkQty = "SELECT count(*) FROM product WHERE active = 1 AND barcode = %d AND stocklevel >= %d" % (barcode, qty)
cur.execute(checkQty)
count = cur.fetchone()
count = count[0]
if count != 0:
self.items[barcode] = qty
tid = getInfo.getNextTransactionId()
cid = 0 #Manager
cur.execute("INSERT INTO transaction VALUES (%d, %d, CURDATE(), 0.00);" % (tid, cid))
keys = self.items.keys()
cur1 = conn.cursor()
for barcode in keys:
qty = self.items.get(barcode)
cur.execute("SELECT batchdate, stock FROM batch WHERE barcode = %d;" % barcode)
stockAccountedFor = 0
for allBatches in cur.fetchall():
batchStock = allBatches[1]
batchDate = allBatches[0]
#Even after adding this batch, we are unable to reach the required stock to be removed
if stockAccountedFor+batchStock <= qty:
cur1.execute("DELETE FROM batch WHERE barcode = %d AND batchdate = \'%s\';" % (barcode,batchDate))
stockAccountedFor = stockAccountedFor + batchStock
else:
cur1.execute("UPDATE batch SET stock = stock - %d WHERE barcode=%d AND batchdate=\'%s\';" % (qty-stockAccountedFor,barcode,batchDate))
stockAccountedFor = qty
cur.execute("INSERT INTO transactiondetails VALUES(%d, %d, NULL, %d,'writeoff');" % (tid,barcode,qty))
cur.execute("UPDATE product SET stocklevel = stocklevel - %d WHERE barcode=%d;" % (qty,barcode))
conn.commit()
self.close()
else:
cur.execute(checkExistence)
count = cur.fetchone()
count = count[0]
if count != 0:
self.ui.lineEdit_qty.clear()
self.ui.error_qty.setText("*not enough stock")
else:
self.ui.lineEdit_qty.clear()
self.ui.lineEdit_barcode.clear()
self.ui.error_barcode.setText("*no such product")
connectDb.closeDatabaseConnection(conn, cur)
else:
self.ui.lineEdit_qty.clear()
self.ui.error_qty.setText("*incorrect input")
else:
if not isNumber(barcode):
self.ui.lineEdit_qty.clear()
self.ui.lineEdit_barcode.clear()
self.ui.error_barcode.setText("*incorrect input")
if not isNumber(qty):
self.ui.lineEdit_qty.clear()
self.ui.error_qty.setText("*incorrect input")
else:
if (len(str(qty)) == 0):
self.ui.error_qty.setText("*required")
if (len(str(barcode)) == 0):
self.ui.lineEdit_qty.clear()
self.ui.error_barcode.setText("*required")
elif (len(str(barcode)) != 8):
self.ui.lineEdit_qty.clear()
self.ui.error_barcode.setText("*incorrect input")