diff --git a/inventory-backend/app/services/inbound/semi_service.py b/inventory-backend/app/services/inbound/semi_service.py index 6baee4f..45f84c1 100644 --- a/inventory-backend/app/services/inbound/semi_service.py +++ b/inventory-backend/app/services/inbound/semi_service.py @@ -156,8 +156,14 @@ class SemiInboundService: in_qty = float(data.get('in_quantity') or 0) raw_cost = float(data.get('raw_material_cost') or 0) - # 【重要修改】:把前端的 unit_total_cost(单件成本)存入原数据库的 manual_cost 字段中 - unit_cost = float(data.get('unit_total_cost') or raw_cost) + bom_code = data.get('bom_code') + bom_version = data.get('bom_version') + # 根据是否有BOM决定单价 + if bom_code and bom_version: + # 使用BOM成本作为单价 + unit_cost = SemiInboundService.calculate_bom_cost(bom_code, bom_version) + else: + unit_cost = raw_cost total_value = unit_cost * in_qty next_global_id = 0 @@ -304,13 +310,23 @@ class SemiInboundService: if 'raw_material_cost' in data: stock.raw_material_cost = float(data['raw_material_cost']) - if 'unit_total_cost' in data: - stock.manual_cost = float(data['unit_total_cost']) # 映射到 manual_cost 物理字段 + + # 决定单件成本:有BOM用BOM成本,否则用raw_material_cost + bom_code = data.get('bom_code', stock.bom_code) + bom_version = data.get('bom_version', stock.bom_version) + raw_cost = float(data.get('raw_material_cost', stock.raw_material_cost or 0)) + if bom_code and bom_version: + try: + unit_cost = SemiInboundService.calculate_bom_cost(bom_code, bom_version) + except Exception: + unit_cost = raw_cost + else: + unit_cost = raw_cost + stock.manual_cost = unit_cost if 'unit_total_cost' in data or qty_changed: qty = float(stock.in_quantity or 1) - # 使用存入 manual_cost 的单价计算总价 - stock.total_price = float(stock.manual_cost or 0) * qty + stock.total_price = unit_cost * qty db.session.commit() return stock diff --git a/inventory-web/src/views/stock/inbound/semi.vue b/inventory-web/src/views/stock/inbound/semi.vue index d756717..4a2ca9c 100644 --- a/inventory-web/src/views/stock/inbound/semi.vue +++ b/inventory-web/src/views/stock/inbound/semi.vue @@ -807,11 +807,24 @@ const form = reactive({ }) // === 监听计算总成本 === -watch([() => form.unit_total_cost, () => form.in_quantity], ([unit, qty]) => { - const unitNum = Number(unit || 0) - const qtyNum = Number(qty || 1) - form.total_price = Number((unitNum * qtyNum).toFixed(2)) -}) +const computeTotalPrice = () => { + let unitCost = 0 + if (form.bom_code) { + // 如果BOM编号有值,则单价采用基于BOM成本 (unit_total_cost) + unitCost = Number(form.unit_total_cost || 0) + } else { + // 如果BOM编号为空,则单价采用估算成本 (raw_material_cost) + unitCost = Number(form.raw_material_cost || 0) + } + const qty = Number(form.in_quantity || 1) + form.total_price = Number((unitCost * qty).toFixed(2)) +} + +watch( + () => [form.bom_code, form.unit_total_cost, form.raw_material_cost, form.in_quantity], + () => computeTotalPrice(), + { immediate: true, deep: false } +) // ------------------------------------ // BOM Search Logic