-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlgpartno.py
384 lines (346 loc) · 13.7 KB
/
dlgpartno.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
""" dlgpartno.py - Copyright Ricky sutanto """
from PyQt5.uic import loadUi
from PyQt5.QtCore import (Qt, QSize)
from PyQt5 import QtGui
from PyQt5.QtWidgets import (
QDialog, QComboBox, QSizePolicy, QMessageBox, QLineEdit)
import resource
import dblogging
import logging
import markpage
logging.basicConfig(level=logging.ERROR)
LOGGER = logging.getLogger("dlgpartno")
class WinPartNo(QDialog):
def __init__(self, conn, userdata):
super(WinPartNo, self).__init__()
self.jobdata = None
self.dblog = conn
self.userdata = userdata
self.userId = userdata.get("user_id")
self.partData = None
self.dataBatch = None
def _show(self):
loadUi("PartCombo.ui", self)
self.cbPartNumber.setEditable(False)
# self.cbPartNumber.setMinimumHeight(50)
self.cbPartNumber.setIconSize(QSize(48, 48))
self.cbPartNumber.setSizePolicy(
QSizePolicy.Fixed, QSizePolicy.Fixed)
# self.cbPartNumber.setSizeAdjustPolicy(QComboBox.AdjustToContents)
result = self.dblog.getPartNumber()
if result != None:
# print(result)
datalist = []
for row in result:
datalist.append(row["tpl_partno"].decode("utf-8"))
self.cbPartNumber.addItems(datalist)
self.eBatch.setText("")
self.eSN.setText("")
self.eQty.setValue(0)
self.cbRemark.setChecked(False)
level = self.userdata.get("user_role")
if level == 1:
self.cbRemark.hide()
self.butConfirm.clicked.connect(self.confirm)
self.butCancel.clicked.connect(self.closewin)
# self.eBatch.textChanged.connect(self.CheckJob)
self.cbRemark.stateChanged.connect(self.remarkCheck)
self.eBatch.keyPressEvent = self.batchPress
self.eBatch.focusOutEvent = self.batchOut
self.cbPartNumber.currentIndexChanged.connect(self.partChange)
globals()["tplLine1"] = ""
globals()["tplLine2"] = ""
globals()["tplLine3"] = ""
globals()["tplLine4"] = ""
globals()["tplLine5"] = ""
globals()["tplLine6"] = ""
markpage.markWindow.PartNoData = ""
markpage.markWindow.BatchNo = ""
markpage.markWindow.SerialNo = ""
markpage.markWindow.QtyMark = 0
markpage.markWindow.isRemark = False
markpage.markWindow.SeqNo = 1
markpage.markWindow.LastSN = ""
markpage.markWindow.FirstSN = ""
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
elif event.key() == Qt.Key_Return:
event.accept()
def partChange(self, index):
self.eBatch.setText("")
self.eSN.setText("")
self.eQty.setValue(0)
self.eBatch.setFocus()
def batchPress(self, event):
if event.key() == Qt.Key_Return:
self.eSN.setFocus()
'''
pn = self.cbPartNumber.currentText()
result = self.dblog._getPartNumberData(pn)
self.partData = result
if result != None:
fieldCnt = self.partData.get("tpl_fieldcnt")
for x in range(fieldCnt):
fName = "tpl_field" + str(x+1)
sName = "tplLine" + str(x+1)
txt = self.partData.get(fName)
globals()[sName] = txt.decode("utf-8")
self.eSN.setText("")
self.eQty.setValue(0)
self.cbRemark.setChecked(False)
#print("BatchPress")
self.CheckJob()'''
else:
QLineEdit.keyPressEvent(self.eBatch, event)
def batchOut(self, focusevent):
if focusevent.lostFocus() and self.eBatch.text() != "":
pn = self.cbPartNumber.currentText()
result = self.dblog._getPartNumberData(pn)
self.partData = result
if result != None:
fieldCnt = self.partData.get("tpl_fieldcnt")
for x in range(fieldCnt):
fName = "tpl_field" + str(x+1)
sName = "tplLine" + str(x+1)
txt = self.partData.get(fName)
globals()[sName] = txt.decode("utf-8")
self.eSN.setText("")
self.CheckJob()
# self.eQty.setValue(0)
# if not self.cbRemark.isChecked():
#print("check job")
# self.cbRemark.setChecked(False)
else:
QLineEdit.focusOutEvent(self.eBatch, focusevent)
def closewin(self):
self.close()
def confirm(self):
pn = self.cbPartNumber.currentText()
bn = self.eBatch.text()
sn = self.eSN.text()
if bn == "":
self.showMessage("Batch No must not empty!")
return
if self.cbRemark.isChecked():
isSNExists = self.traceLine()
if sn != "" and bn != "":
ck = self.dblog.isSNOK(bn, pn, sn)
if ck:
self.showMessage("Invalid Serial No")
return
else:
if isSNExists and sn == "":
self.showMessage("Please complete data.")
else:
isSNExists = self.traceLine()
if self.dataBatch == None:
maxSn = 0
else:
maxSn = self.dataBatch.get("prd_sn")
if maxSn == None:
maxSn = 0
iQty = int(self.eQty.value())
if iQty <= 0:
self.showMessage("Wrong qty!")
return
iBal = iQty - maxSn
if iBal <= 0:
self.showMessage("No more S/N to mark! ")
return
#print("isSNExits", isSNExists)
if iQty != 0 and bn != "":
if isSNExists and sn == "":
self.showMessage("Please entry S/N.")
self.eQty.setFocus()
return
elif sn != "" and bn != "":
ck = self.dblog.isSNOK(bn, pn, sn)
if ck:
self.showMessage("Invalid Serial No")
self.eQty.setFocus()
return
msgBox = QMessageBox()
msgBox.setIcon(QMessageBox.Question)
msgBox.setText(
"Are You sure all data are correct?")
msgBox.setStandardButtons(
QMessageBox.Ok | QMessageBox.Cancel)
msgBox.setDefaultButton(QMessageBox.Cancel)
if msgBox.exec_() == QMessageBox.Ok:
markpage.markWindow.PartNoData = self.cbPartNumber.currentText()
markpage.markWindow.BatchNo = self.eBatch.text()
markpage.markWindow.SerialNo = self.eSN.text()
markpage.markWindow.QtyMark = self.eQty.value()
markpage.markWindow.isRemark = self.cbRemark.isChecked()
markpage.markWindow.SeqNo = 1
if markpage.markWindow.isRemark == False:
if self.dataBatch == None:
markpage.markWindow.FirstSN = self.eSN.text()
markpage.markWindow.LastSN = ""
else:
txtSN = self.dataBatch.get("min_serialno")
if txtSN == None:
markpage.markWindow.FirstSN = self.eSN.text()
else:
markpage.markWindow.FirstSN = txtSN
txtLast = self.dataBatch.get("prd_serialno")
if txtLast == None:
markpage.markWindow.LastSN = ""
else:
markpage.markWindow.LastSN = txtLast
pn = markpage.markWindow.PartNoData
bn = markpage.markWindow.BatchNo
sn = markpage.markWindow.SerialNo
qt = markpage.markWindow.QtyMark
if self.dataBatch == None:
job = [pn, bn, sn, str(qt), str(self.userId)]
self.dblog.saveJob(job)
self.closewin()
def CheckJob(self):
#print("check job")
pn = self.cbPartNumber.currentText()
bn = self.eBatch.text()
result = self.dblog.getJob(pn, bn)
if result != None:
if (result.get("job_partno") == pn) and (result.get("job_batchno") == bn):
maxQty = result.get("job_qty")
res2 = self.dblog.checkBatch(bn, pn)
if res2 != None:
#print("res2", res2)
#sn = res2.get("prd_serialno")
isSNExists = self.traceLine()
if isSNExists and self.cbRemark.isChecked():
self.eQty.setReadOnly(True)
maxSn = res2.get("prd_sn")
if maxQty == maxSn and not self.cbRemark.isChecked():
self.showMessage("No more S/N to mark! ")
self.dataBatch = res2
# self.eSN.setReadOnly(True)
# self.eQty.setReadOnly(True)
self.eBatch.setText("")
self.eBatch.setFocus()
return
sn = res2.get("prd_serialno")
if sn != None:
stSn = self._formattingSN(sn, 1)
self.eSN.setText(stSn)
else:
self.eSN.setText(result.get("job_serialno"))
else:
self.eSN.setText(result.get("job_serialno"))
self.dataBatch = res2
# self.eSN.setReadOnly(True)
# self.eSN.setEnabled(False)
if not self.cbRemark.isChecked():
qt = result.get("job_qty")
self.eQty.setValue(qt)
self.eSN.setReadOnly(False)
self.eQty.setReadOnly(True)
else:
self.dataBatch = None
self.eSN.setReadOnly(False)
self.eQty.setReadOnly(False)
self.eSN.setFocus()
else:
self.dataBatch = None
self.eSN.setReadOnly(False)
self.eQty.setReadOnly(False)
self.eSN.setFocus()
return result
def remarkCheck(self):
#print("remarkCheck:", self.cbRemark.isChecked())
if self.cbRemark.isChecked():
self.eSN.setText("")
self.eSN.setReadOnly(False)
self.eQty.setValue(1)
self.eQty.setReadOnly(True)
# self.eSN.setFocus()
self.eBatch.setText("")
self.eBatch.setFocus()
else:
if self.eBatch.text() != "":
# print("RemarkCheck")
self.CheckJob()
else:
self.eSN.setReadOnly(False)
self.eQty.setReadOnly(False)
def _formattingSN(self, txt, idx):
st = txt
if st == "":
st = self.eSN.text()
idx = 0
result = None
alpha = ""
final = ""
try:
snMapping = self.partData.get("sn_mapping")
result = self.dblog._getSNMapping(str(snMapping))
self.SnMappingData = result
if self.SnMappingData != None:
fixStart = self.SnMappingData.get("sn_fix_pos")
fixLen = self.SnMappingData.get("sn_fix_len")
alphaStart = self.SnMappingData.get("sn_alpha_pos")
alphaLen = self.SnMappingData.get("sn_alpha_len")
numPos = self.SnMappingData.get("sn_numeric_pos")
numLen = self.SnMappingData.get("sn_numeric_len")
totLen = self.SnMappingData.get("sn_total_len")
lz = self.SnMappingData.get("sn_leadingzero")
if fixStart != -1:
result = st[fixStart-1:fixLen]
# print("fix:",result)
if alphaStart != -1:
alpha = st[alphaStart-1:alphaStart-1+alphaLen]
#print("alpha:", alpha)
l = len(st)
#print("st", st)
#print("numpos:", numPos)
sn = st[numPos-1:l]
#print("seq :", sn)
stSN = int(sn)
# if idx > 0
if lz == True:
final = str(stSN+idx).zfill(numLen)
if (len(final) > numLen):
final = "1".zfill(numLen)
x = ord(alpha[len(alpha)-1])
# print(x)
x = x + 1
if x == 91:
x = 65
tmp = alpha[:len(alpha)-1]
# print(tmp)
alpha = tmp + chr(x)
# print(alpha)
else:
final = str(stSN+idx)
#print("final:", final)
result = result + alpha + final
#print("result: ", result)
except:
pass
return result
def showMessage(self, msg):
msgBox = QMessageBox()
msgBox.setIcon(QMessageBox.Critical)
msgBox.setText(msg)
msgBox.setStandardButtons(QMessageBox.Ok)
msgBox.setDefaultButton(QMessageBox.Ok)
msgBox.exec_()
def traceLine(self):
out = False
if self.partData == None:
pn = self.cbPartNumber.currentText()
self.partData = self.dblog._getPartNumberData(pn)
fieldCnt = self.partData.get("tpl_fieldcnt")
for x in range(fieldCnt):
gName = "tplLine" + str(x+1)
st = globals()[gName]
try:
idx = st.index("%sn")
if idx != -1:
out = True
break
except ValueError:
continue
return out