一键清零和单位转换
This commit is contained in:
@@ -86,45 +86,55 @@ class FabricManager(QMainWindow):
|
||||
try:
|
||||
with self.get_conn() as conn:
|
||||
cursor = conn.execute('''
|
||||
SELECT category, fabric_type, usage_per_piece, unit
|
||||
SELECT model, category, fabric_type, usage_per_piece, unit
|
||||
FROM garment_materials
|
||||
WHERE style_number = ?
|
||||
''', (style_number,))
|
||||
rows = cursor.fetchall()
|
||||
inserted = 0
|
||||
for category, fabric_type, usage_per_piece, unit in rows:
|
||||
for model, category, fabric_type, usage_per_piece, unit in rows:
|
||||
if usage_per_piece == 0:
|
||||
continue
|
||||
|
||||
# 获取该原料在入库时使用的单位
|
||||
|
||||
# 优先使用精确原料型号(model);如无型号则退回到分类名称(兼容旧数据)
|
||||
raw_identifier = model if model else category
|
||||
|
||||
# 获取该原料在入库时使用的单位(最近一次入库)
|
||||
stock_cursor = conn.execute('''
|
||||
SELECT unit FROM fabric_stock_in
|
||||
WHERE model = ?
|
||||
ORDER BY purchase_date DESC
|
||||
LIMIT 1
|
||||
''', (category,))
|
||||
''', (raw_identifier,))
|
||||
stock_unit_row = stock_cursor.fetchone()
|
||||
|
||||
# 如果有入库记录,使用入库单位;否则使用原来的单位
|
||||
|
||||
total_usage = usage_per_piece * quantity * (1 + loss_rate)
|
||||
|
||||
# 如果有入库记录,则按入库单位扣减库存;必要时进行单位换算
|
||||
if stock_unit_row:
|
||||
stock_unit = stock_unit_row[0]
|
||||
# 如果单位不同,需要转换
|
||||
if unit != stock_unit:
|
||||
consume_qty = self.convert_unit_value(usage_per_piece * quantity * (1 + loss_rate), unit, stock_unit, category)
|
||||
# 单位不同,尝试进行单位转换(米/码/公斤互转依赖面料幅宽和克重)
|
||||
consume_qty = self.convert_unit_value(
|
||||
total_usage,
|
||||
unit,
|
||||
stock_unit,
|
||||
fabric_model=model if model else None
|
||||
)
|
||||
final_unit = stock_unit
|
||||
else:
|
||||
consume_qty = usage_per_piece * quantity * (1 + loss_rate)
|
||||
consume_qty = total_usage
|
||||
final_unit = unit
|
||||
else:
|
||||
# 没有入库记录,使用原单位
|
||||
consume_qty = usage_per_piece * quantity * (1 + loss_rate)
|
||||
# 没有入库记录,只能按原单位记录消耗
|
||||
consume_qty = total_usage
|
||||
final_unit = unit
|
||||
|
||||
|
||||
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_number, raw_identifier, usage_per_piece, quantity, loss_rate, consume_qty, datetime.now().strftime('%Y-%m-%d'), final_unit))
|
||||
inserted += 1
|
||||
conn.commit()
|
||||
if inserted > 0:
|
||||
|
||||
@@ -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)}")
|
||||
Reference in New Issue
Block a user