refactor: simplify cost calculation to 3 fields, drop manual_cost

Co-authored-by: aider (openai/DeepSeek-V3.2-Thinking) <aider@aider.chat>
This commit is contained in:
dxc
2026-03-02 10:24:51 +08:00
parent b688480892
commit 545cd86632
4 changed files with 65 additions and 32 deletions

View File

@ -172,8 +172,8 @@ class SemiInboundService:
in_qty = float(data.get('in_quantity') or 0)
raw_cost = float(data.get('raw_material_cost') or 0)
manual_cost = float(data.get('manual_cost') or 0)
unit_total_cost = raw_cost + manual_cost
manual_cost = 0.0 # 字段已弃用,保持向后兼容
unit_total_cost = float(data.get('unit_total_cost') or raw_cost or 0)
total_value = unit_total_cost * in_qty
next_global_id = 0
@ -220,7 +220,8 @@ class SemiInboundService:
production_end_time=p_end,
production_time_range=time_range_str,
raw_material_cost=raw_cost,
manual_cost=manual_cost,
manual_cost=manual_cost, # 保持 0.0
unit_total_cost=unit_total_cost,
total_price=total_value,
arrival_photo=json.dumps(arrival_list),
quality_report_link=json.dumps(quality_report_list),
@ -312,7 +313,6 @@ class SemiInboundService:
stock.production_time_range = raw_range
qty_changed = False
cost_changed = False
if 'in_quantity' in data:
new_qty = float(data['in_quantity'])
diff = new_qty - float(stock.in_quantity)
@ -321,15 +321,15 @@ class SemiInboundService:
stock.stock_quantity = float(stock.stock_quantity) + diff
stock.available_quantity = float(stock.available_quantity) + diff
qty_changed = True
if 'raw_material_cost' in data:
stock.raw_material_cost = float(data['raw_material_cost'])
cost_changed = True
if 'manual_cost' in data:
stock.manual_cost = float(data['manual_cost'])
cost_changed = True
if cost_changed or qty_changed:
unit_total = float(stock.raw_material_cost) + float(stock.manual_cost)
stock.total_price = float(stock.in_quantity) * unit_total
if 'unit_total_cost' in data:
stock.unit_total_cost = float(data['unit_total_cost'])
if 'unit_total_cost' in data or qty_changed:
qty = float(stock.in_quantity or 1)
stock.total_price = float(stock.unit_total_cost or 0) * qty
db.session.commit()
return stock
@ -503,4 +503,4 @@ class SemiInboundService:
return [r[0] for r in records if r[0]]
except Exception:
traceback.print_exc()
return []
return []