-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsale.py
284 lines (251 loc) · 11.4 KB
/
msale.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
from PyQt5.QtCore import pyqtSlot, QSize, Qt
from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem, QComboBox, QMessageBox, QMenu, QAction, QHeaderView, QAbstractItemView
import time
from decimal import Decimal
from string import Template
from shop_sale import ShopSale_UI
from ToDB import ToDB
from datetime import datetime
class MSale(QMainWindow, ShopSale_UI):
def __init__(self, parent=None):
super(MSale, self).__init__(parent)
self.setupUi(self)
self.db = ToDB()
# @pyqtSlot(int, int)
# def on_tableWidget_cellDoubleClicked(self, row, column):
# """
# 双击修改
# """
# print("双击修改")
#
# @pyqtSlot(QTableWidgetItem)
# def on_tableWidget_itemActivated(self, item):
# """
# 按住Enter键时
# """
# print("asda")
@pyqtSlot(int, int, int, int)
def on_tableWidget_itemChanged(self, currentRow, currentColumn, previousRow, previousColumn):
"""
当前单元格改变
"""
print("当前单元格改变")
# 查找并显示
def event_lr(self):
if self.code_radio.isChecked():
if self.line_code.text() == '':
replay = QMessageBox.warning(self, "!", "请输入条形码!", QMessageBox.Yes)
return
# self.line_code.textChanged.connect(self.searchByCode)
code = self.line_code.text()
# ["条形码", "名称", "生产厂家", "批号", "有效期", "零售价", "数量", "总计"]
sql = "select 条形码,名称,生产厂家,批号,有效期,MAX(零售价) as 零售价 from 库存 where 条形码='{0}'".format(code)
else:
# try:
# # self.line_code.textChanged.disconnect()
# except Exception as e:
# print(e)
if self.line_code.text() == '':
replay = QMessageBox.warning(self, "!", "请输入药品名称!", QMessageBox.Yes)
return
name = self.line_code.text()
sql = "select 条形码,名称,生产厂家,批号,有效期,零售价 from 库存 where 名称 like '%{0}%'".format(name)
# 情空表格
result = self.db.search(sql)
if not result:
replay = QMessageBox.warning(self, "!", "未找到!", QMessageBox.Yes)
return
try:
self.line_code.clear()
if self.Row==-1:
self.Row=0
for cur in result:
if self.Row == 0:
self.tabel_sell.itemChanged.connect(self.handleItemClick)
items = self.tabel_sell.findItems(cur[0], Qt.MatchExactly)
# 当前记录是否存在
if len(items) != 0:
row = items[0].row()
cur_count = int(self.tabel_sell.item(row, 6).text())
cur_count += 1
self.tabel_sell.setItem(row, 6, QTableWidgetItem(str(cur_count)))
one_sum = cur_count * Decimal(self.tabel_sell.item(row, 5).text())
self.tabel_sell.setItem(row, 7, QTableWidgetItem(str(one_sum)))
else:
self.tabel_sell.setRowCount(self.Row + 1)
code = QTableWidgetItem(cur[0])
name = QTableWidgetItem(cur[1])
prducer = QTableWidgetItem(cur[2])
batch = QTableWidgetItem(cur[3])
valid = QTableWidgetItem(cur[4])
cost = QTableWidgetItem(str(cur[5]))
num = QTableWidgetItem('1')
sum_price = QTableWidgetItem(str(cur[5] * 1))
self.tabel_sell.setItem(self.Row, 0, code)
self.tabel_sell.setItem(self.Row, 1, name)
self.tabel_sell.setItem(self.Row, 2, prducer)
self.tabel_sell.setItem(self.Row, 3, batch)
self.tabel_sell.setItem(self.Row, 4, valid)
# 零售价
self.tabel_sell.setItem(self.Row, 5, cost)
# 数量
self.tabel_sell.setItem(self.Row, 6, num)
self.tabel_sell.setItem(self.Row, 7, sum_price)
self.tabel_sell.item(self.Row, 0).setFlags(Qt.ItemIsEnabled)
self.tabel_sell.item(self.Row, 1).setFlags(Qt.ItemIsEnabled)
self.tabel_sell.item(self.Row, 2).setFlags(Qt.ItemIsEnabled)
self.tabel_sell.item(self.Row, 3).setFlags(Qt.ItemIsEnabled)
self.tabel_sell.item(self.Row, 4).setFlags(Qt.ItemIsEnabled)
self.tabel_sell.item(self.Row, 5).setFlags(Qt.ItemIsEnabled)
self.Row += 1
self.updateCost()
except Exception as e:
print(e)
def updateCost(self):
r_sum = 0
rows = self.tabel_sell.rowCount()
for rows_index in range(rows):
# print items[item_index].text()
num = self.tabel_sell.item(rows_index, 7)
if num:
r_sum += Decimal(num.text())
# 计算总价
self.line_sell1.setText(str(r_sum))
self.line_sell2.setText(self.line_sell1.text())
cur = Decimal(self.line_sell3.text())
if cur != 0.0:
self.line_sell4.setText(str(cur - r_sum))
def jiesuan(self):
if self.line_sell3.text()!='':
try:
self.line_sell4.setText(str((Decimal(self.line_sell3.text())-Decimal(self.line_sell2.text()))))
except Exception as e:
print("ddd",e)
def jiesuan0(self):
try:
self.line_sell3.setText("")
# self.Row=0
# self.tabel_sell.clearContents()
except Exception as e:
print("www", e)
# 确认录入存表
def event_qr(self):
# 确定是否添加有商品
if self.line_sell1.text() == "0.0":
replay = QMessageBox.warning(self, "!", "未添加商品!", QMessageBox.Yes)
else:
# 录入数据库的同时将收货信息存入txt文件中,模仿打印小票
try:
stime=datetime.now()
width = 35
price_width = 15
item_width = width - price_width
header_format = '%-*s%*s%*s%*s'
content_format = '%-*s%*s%*s%*s'
xsjl = open("销售记录.txt", "a+")
xsjl.write("="*(price_width*4+item_width)+'\n')
xsjl.write(content_format%(item_width,"名称",price_width,"单价",price_width,"数量",price_width,"总计"))
xsjl.write("\n")
xsjl.write("-" * (price_width*4+item_width)+ '\n')
goods = []
rows = self.tabel_sell.rowCount()
for rows_index in range(rows):
name = self.tabel_sell.item(rows_index, 1).text()
price = self.tabel_sell.item(rows_index, 5).text()
count = self.tabel_sell.item(rows_index, 6).text()
sum_price = self.tabel_sell.item(rows_index, 7).text()
record = content_format % (item_width, name, price_width, price, price_width, count, price_width, sum_price)
goods.append(record)
sql='''
INSERT INTO 销售 (条形码,名称,数量,时间)\
VALUES\
('%s','%s','%s','%s')
''' % (self.tabel_sell.item(rows_index, 0).text(),name,count,stime)
self.db.runSql(sql)
xsjl.write('\n'.join(goods))
xsjl.write('\n'+"-" * (price_width*4+item_width) + '\n')
xsjl.write("总计:%.2f 元\n" % float(self.line_sell1.text()))
xsjl.write("实收:%.2f 元\n" % float(self.line_sell3.text()))
xsjl.write("找零:%.2f 元\n" % float(self.line_sell4.text()))
xsjl.write("\n" + stime.strftime("%Y-%m-%d %H:%M:%S") + "\n")
xsjl.write("=" * (price_width*4+item_width) + '\n')
xsjl.close()
# 入库
sql="insert into 收入(总计,实收,找零,时间) values ('%s','%s','%s','%s')" % (self.line_sell1.text(),self.line_sell3.text(),self.line_sell4.text(),stime)
self.db.runSql(sql)
replay = QMessageBox.warning(self, "!", "销售成功,总计:%.2f 元" % float(self.line_sell1.text()))
self.event_ql()
except Exception as e:
print(e)
# 清空表格
def event_ql(self):
# 清空输入框,清除显示列表,清空暂存信息的列表
self.line_sell1.setText("0.0")
self.line_sell2.setText("0.0")
self.line_sell3.setText("0.0")
self.line_sell4.setText("0.0")
self.Row = 0
self.line_code.clear()
self.tabel_sell.clear()
self.tabel_sell.setRowCount(1)
self.tabel_sell.setColumnCount(8)
self.tabel_sell.setHorizontalHeaderLabels(["条形码", "名称", "生产厂家", "批号", "有效期", "零售价", "数量", "总计"])
# 添加背景图片
def handleItemClick(self):
try:
column=self.tabel_sell.currentColumn()
# print(self.tabel_sell.selectedItems().pop().column())
if column==6:
row = self.tabel_sell.currentRow()
cur=self.tabel_sell.item(row, 6).text()
price=Decimal(self.tabel_sell.item(row, 5).text())
result=QTableWidgetItem(str(Decimal(cur)*price))
self.tabel_sell.itemChanged.disconnect()
self.tabel_sell.setItem(row, 7, result)
self.tabel_sell.itemChanged.connect(self.handleItemClick)
self.updateCost()
except Exception as e:
print(e)
def contextMenuEvent(self, event):
pmenu = QMenu(self)
pDeleteAct = QAction('删除行', self.tabel_sell)
pmenu.addAction(pDeleteAct)
pDeleteAct.triggered.connect(self.deleterows)
pmenu.popup(self.mapToGlobal(event.pos()))
def deleterows(self):
"""
删除行
"""
rr = QMessageBox.warning(self, "注意", "是否删除该行!", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if rr == QMessageBox.Yes:
curow = self.tabel_sell.currentRow()
selections = self.tabel_sell.selectionModel()
selectedsList = selections.selectedRows()
rows = []
for r in selectedsList:
rows.append(r.row())
if len(rows) == 0:
rows.append(curow)
self.removeRows(rows, isdel_list = 1)
else:
self.removeRows(rows, isdel_list = 1)
self.updateCost()
self.Row -=1
def removeRows(self, rows, isdel_list = 0):
if isdel_list != 0:
rows.reverse()
for i in rows:
self.tabel_sell.removeRow(i)
else:
for i in range(rows-1, -1, -1):
self.tabel_sell.removeRow(i)
def searchByCode(self):
code=self.line_code.text()
time.sleep(2)
self.line_code.clear()
# if self.code_radio.isChecked():
# if len(code)==14:
# self.event_lr()
# self.line_code.clear()
# if len(code)>14:
# self.line_code.clear()