添加库存不足检查功能
在记录消耗到库存功能中增加了库存充足性验证,确保只有在所有原料库存充足时才允许记录消耗。 主要改进: - 在记录消耗前先检查所有原料的当前库存 - 如果任何原料库存不足,显示详细的库存不足信息并中止操作 - 提供清晰的提示信息,显示每种原料所需数量和实际库存数量 - 只有所有原料库存都充足时才执行记录操作 - 优化成功提示信息,显示记录的原料种类数量 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
51
main.py
51
main.py
@@ -243,7 +243,10 @@ class FabricManager(QMainWindow):
|
||||
WHERE style_number = ?
|
||||
''', (style_number,))
|
||||
rows = cursor.fetchall()
|
||||
inserted = 0
|
||||
|
||||
# 第一步:检查所有原料库存是否充足
|
||||
insufficient_materials = []
|
||||
materials_to_record = []
|
||||
|
||||
for category, fabric_type, usage_per_piece, unit in rows:
|
||||
if usage_per_piece == 0:
|
||||
@@ -273,16 +276,58 @@ class FabricManager(QMainWindow):
|
||||
consume_qty = usage_per_piece * quantity * (1 + loss_rate)
|
||||
final_unit = unit
|
||||
|
||||
# 检查当前库存
|
||||
stock_info = conn.execute('''
|
||||
SELECT
|
||||
COALESCE(SUM(CASE WHEN si.is_deleted = 0 THEN si.quantity ELSE 0 END), 0) AS total_in,
|
||||
COALESCE(SUM(CASE WHEN c.is_deleted = 0 THEN c.consume_quantity ELSE 0 END), 0) AS total_out
|
||||
FROM fabrics f
|
||||
LEFT JOIN fabric_stock_in si ON f.model = si.model
|
||||
LEFT JOIN fabric_consumption c ON f.model = c.model
|
||||
WHERE f.model = ?
|
||||
GROUP BY f.model
|
||||
''', (category,)).fetchone()
|
||||
|
||||
if stock_info:
|
||||
total_in, total_out = stock_info
|
||||
current_stock = total_in - total_out
|
||||
else:
|
||||
current_stock = 0
|
||||
|
||||
# 如果库存不足,记录下来
|
||||
if current_stock < consume_qty:
|
||||
material_name = category or "未命名材料"
|
||||
insufficient_materials.append(
|
||||
f"{material_name}: 需要 {consume_qty:.3f} {final_unit},库存仅剩 {current_stock:.3f} {final_unit}"
|
||||
)
|
||||
else:
|
||||
# 库存充足,加入待记录列表
|
||||
materials_to_record.append((
|
||||
style_number, category, usage_per_piece, quantity,
|
||||
loss_rate, consume_qty, final_unit
|
||||
))
|
||||
|
||||
# 如果有库存不足的材料,提示用户并中止操作
|
||||
if insufficient_materials:
|
||||
message = "以下原料库存不足,无法记录消耗:\n\n" + "\n".join(insufficient_materials)
|
||||
QMessageBox.warning(self, "库存不足", message)
|
||||
return
|
||||
|
||||
# 第二步:所有库存都充足,执行记录
|
||||
inserted = 0
|
||||
for material_data in materials_to_record:
|
||||
style_num, model, single_usage, qty, loss, consume_qty, final_unit = material_data
|
||||
conn.execute('''
|
||||
INSERT INTO fabric_consumption
|
||||
(style_number, model, single_usage, quantity_made, loss_rate, consume_quantity, consume_date, unit)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
''', (style_number, category, usage_per_piece, quantity, loss_rate, consume_qty, datetime.now().strftime('%Y-%m-%d'), final_unit))
|
||||
''', (style_num, model, single_usage, qty, loss, consume_qty, datetime.now().strftime('%Y-%m-%d'), final_unit))
|
||||
inserted += 1
|
||||
|
||||
conn.commit()
|
||||
|
||||
if inserted > 0:
|
||||
QMessageBox.information(self, "成功", "本次生产消耗已记录到库存!")
|
||||
QMessageBox.information(self, "成功", f"本次生产消耗已记录到库存!共记录 {inserted} 种原料。")
|
||||
else:
|
||||
QMessageBox.information(self, "提示", "本次没有可记录的原料消耗")
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user