一键清零和单位转换

This commit is contained in:
2025-12-23 18:21:44 +08:00
parent 76b16df32c
commit c52d360cbb
2 changed files with 98 additions and 18 deletions

View File

@@ -617,7 +617,77 @@ class RawMaterialLibraryDialog(QDialog):
def clear_remaining(self, model):
"""清零剩余库存"""
reply = QMessageBox.question(self, "确认清零", f"确定将 {model} 的剩余量清零?\n(此操作仅逻辑清零,不删除历史记录)")
if reply == QMessageBox.Yes:
QMessageBox.information(self, "完成", f"{model} 剩余量已清零(视为全部用完)")
self.load_stock_table()
reply = QMessageBox.question(
self,
"确认清零",
f"确定将 {model} 的剩余量清零?"
)
if reply != QMessageBox.Yes:
return
try:
with self.get_conn() as conn:
# 计算当前总入库量和总消耗量
cursor_in = conn.execute(
"SELECT COALESCE(SUM(quantity), 0) FROM fabric_stock_in WHERE model = ?",
(model,)
)
total_in = cursor_in.fetchone()[0] or 0.0
cursor_out = conn.execute(
"SELECT COALESCE(SUM(consume_quantity), 0) FROM fabric_consumption WHERE model = ?",
(model,)
)
total_out = cursor_out.fetchone()[0] or 0.0
remaining = round(float(total_in) - float(total_out), 6)
if remaining <= 0:
QMessageBox.information(self, "提示", f"{model} 当前没有可清零的剩余库存。")
return
# 获取用于记录此次清零的单位:优先使用最近一次入库单位,其次是原料表里的单位,最后默认“米”
unit = ""
cursor_unit = conn.execute(
"SELECT unit FROM fabric_stock_in WHERE model = ? ORDER BY purchase_date DESC LIMIT 1",
(model,)
)
row_unit = cursor_unit.fetchone()
if row_unit and row_unit[0]:
unit = row_unit[0]
else:
cursor_fabric = conn.execute(
"SELECT unit FROM fabrics WHERE model = ?",
(model,)
)
row_fabric = cursor_fabric.fetchone()
if row_fabric and row_fabric[0]:
unit = row_fabric[0]
# 以一条“清零”消耗记录的方式写入fabric_consumption
conn.execute(
"""
INSERT INTO fabric_consumption
(style_number, model, single_usage, quantity_made, loss_rate, consume_quantity, consume_date, unit)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
"库存清零", # 特殊标记,表示这是一次手工清零操作
model,
None, # 单件用量无意义置为NULL
0, # 生产件数为0
0.0, # 损耗率0
remaining, # 一次性消耗掉当前所有剩余
datetime.now().strftime('%Y-%m-%d'),
unit,
)
)
conn.commit()
QMessageBox.information(
self,
"完成",
f"{model} 剩余量 {remaining:.3f} {unit} 已清零。"
)
self.load_stock_table()
except Exception as e:
QMessageBox.critical(self, "错误", f"清零失败: {str(e)}")