feat: calculate semi-inbound cost based on BOM code
Co-authored-by: aider (openai/DeepSeek-V3.2-Thinking) <aider@aider.chat>
This commit is contained in:
@ -156,8 +156,14 @@ class SemiInboundService:
|
|||||||
|
|
||||||
in_qty = float(data.get('in_quantity') or 0)
|
in_qty = float(data.get('in_quantity') or 0)
|
||||||
raw_cost = float(data.get('raw_material_cost') or 0)
|
raw_cost = float(data.get('raw_material_cost') or 0)
|
||||||
# 【重要修改】:把前端的 unit_total_cost(单件成本)存入原数据库的 manual_cost 字段中
|
bom_code = data.get('bom_code')
|
||||||
unit_cost = float(data.get('unit_total_cost') or raw_cost)
|
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
|
total_value = unit_cost * in_qty
|
||||||
|
|
||||||
next_global_id = 0
|
next_global_id = 0
|
||||||
@ -304,13 +310,23 @@ class SemiInboundService:
|
|||||||
|
|
||||||
if 'raw_material_cost' in data:
|
if 'raw_material_cost' in data:
|
||||||
stock.raw_material_cost = float(data['raw_material_cost'])
|
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:
|
if 'unit_total_cost' in data or qty_changed:
|
||||||
qty = float(stock.in_quantity or 1)
|
qty = float(stock.in_quantity or 1)
|
||||||
# 使用存入 manual_cost 的单价计算总价
|
stock.total_price = unit_cost * qty
|
||||||
stock.total_price = float(stock.manual_cost or 0) * qty
|
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return stock
|
return stock
|
||||||
|
|||||||
@ -807,11 +807,24 @@ const form = reactive({
|
|||||||
})
|
})
|
||||||
|
|
||||||
// === 监听计算总成本 ===
|
// === 监听计算总成本 ===
|
||||||
watch([() => form.unit_total_cost, () => form.in_quantity], ([unit, qty]) => {
|
const computeTotalPrice = () => {
|
||||||
const unitNum = Number(unit || 0)
|
let unitCost = 0
|
||||||
const qtyNum = Number(qty || 1)
|
if (form.bom_code) {
|
||||||
form.total_price = Number((unitNum * qtyNum).toFixed(2))
|
// 如果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
|
// BOM Search Logic
|
||||||
|
|||||||
Reference in New Issue
Block a user