fix(scrap): completely refactor scan API return dict with safe getattr to prevent attribute errors
This commit is contained in:
@ -176,21 +176,18 @@ class ScrapService:
|
||||
|
||||
@staticmethod
|
||||
def _format_stock(item, table_type):
|
||||
"""格式化库存查询结果"""
|
||||
stock_qty = float(item.stock_quantity) if item.stock_quantity else 0
|
||||
avail_qty = float(item.available_quantity) if item.available_quantity else 0
|
||||
|
||||
"""格式化库存查询结果 - 使用安全 getattr 防止属性错误"""
|
||||
return {
|
||||
'id': item.id,
|
||||
'sku': item.sku,
|
||||
'barcode': item.barcode,
|
||||
'name': item.base.name if item.base else '',
|
||||
'spec': item.base.spec_model if item.base else '',
|
||||
'category': item.base.category if item.base else '',
|
||||
'material_type': item.base.material_type if item.base else '',
|
||||
'warehouse_loc': item.warehouse_loc or '',
|
||||
'stock_quantity': stock_qty,
|
||||
'available_quantity': avail_qty,
|
||||
'id': getattr(item, 'id', None),
|
||||
'sku': getattr(item, 'sku', ''),
|
||||
'barcode': getattr(item, 'barcode', getattr(item, 'bar_code', '')),
|
||||
'name': item.base.name if getattr(item, 'base', None) else '',
|
||||
'spec': item.base.spec_model if getattr(item, 'base', None) else '',
|
||||
'category': item.base.category if getattr(item, 'base', None) else '',
|
||||
'material_type': item.base.material_type if getattr(item, 'base', None) else '',
|
||||
'warehouse_loc': getattr(item, 'warehouse_location', ''),
|
||||
'stock_quantity': float(getattr(item, 'stock_quantity', getattr(item, 'qty_stock', 0)) or 0),
|
||||
'available_quantity': float(getattr(item, 'available_quantity', getattr(item, 'qty_available', 0)) or 0),
|
||||
'source_table': table_type,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user