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
|
@staticmethod
|
||||||
def _format_stock(item, table_type):
|
def _format_stock(item, table_type):
|
||||||
"""格式化库存查询结果"""
|
"""格式化库存查询结果 - 使用安全 getattr 防止属性错误"""
|
||||||
stock_qty = float(item.stock_quantity) if item.stock_quantity else 0
|
|
||||||
avail_qty = float(item.available_quantity) if item.available_quantity else 0
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'id': item.id,
|
'id': getattr(item, 'id', None),
|
||||||
'sku': item.sku,
|
'sku': getattr(item, 'sku', ''),
|
||||||
'barcode': item.barcode,
|
'barcode': getattr(item, 'barcode', getattr(item, 'bar_code', '')),
|
||||||
'name': item.base.name if item.base else '',
|
'name': item.base.name if getattr(item, 'base', None) else '',
|
||||||
'spec': item.base.spec_model if item.base else '',
|
'spec': item.base.spec_model if getattr(item, 'base', None) else '',
|
||||||
'category': item.base.category if item.base else '',
|
'category': item.base.category if getattr(item, 'base', None) else '',
|
||||||
'material_type': item.base.material_type if item.base else '',
|
'material_type': item.base.material_type if getattr(item, 'base', None) else '',
|
||||||
'warehouse_loc': item.warehouse_loc or '',
|
'warehouse_loc': getattr(item, 'warehouse_location', ''),
|
||||||
'stock_quantity': stock_qty,
|
'stock_quantity': float(getattr(item, 'stock_quantity', getattr(item, 'qty_stock', 0)) or 0),
|
||||||
'available_quantity': avail_qty,
|
'available_quantity': float(getattr(item, 'available_quantity', getattr(item, 'qty_available', 0)) or 0),
|
||||||
'source_table': table_type,
|
'source_table': table_type,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user