fix: map correct database fields for spec and location in excel export

This commit is contained in:
DXC
2026-03-18 14:37:09 +08:00
parent 00781422eb
commit 41a4518911

View File

@ -567,27 +567,19 @@ def export_stocktake():
# 安全获取 sku
stock_sku = getattr(stock, 'sku', None) or getattr(stock, 'SKU', None) or '-'
# 使用 base_id 或 material_id 关联查询物料基础表(安全方式)
# 使用 base_id 关联查询物料基础表
material = None
base_id = getattr(stock, 'base_id', None) or getattr(stock, 'material_id', None)
base_id = getattr(stock, 'base_id', None)
if base_id:
material = MaterialBase.query.get(base_id)
else:
# 如果没有 base_id尝试用 code 兜底查询
if hasattr(MaterialBase, 'code') and stock_sku != '-':
material = MaterialBase.query.filter_by(code=stock_sku).first()
# 规格型号:优先material 取,再 fallback 到 stock
spec = (
getattr(material, 'specification', None) or
getattr(material, 'spec', None) or
getattr(stock, 'spec_model', None) or
getattr(stock, 'standard', None) or
'-'
)
# 规格型号:从 MaterialBase 的 spec_model 字段获取
spec = getattr(material, 'spec_model', None) if material else '-'
if not spec or spec == '-':
spec = getattr(stock, 'spec_model', None) or getattr(stock, 'standard', None) or '-'
# 库位:获取真实物理库位
location = getattr(stock, 'location', None) or getattr(stock, 'position', None) or '-'
# 库位:从库存表的 warehouse_location 字段获取
location = getattr(stock, 'warehouse_location', None) or '-'
return {
'name': material.name if material else stock_sku,