fix(inventory): 修复 BOM 齐套分析时 allStockData 未加载导致可用库存为 0

This commit is contained in:
DXC
2026-04-20 18:44:35 +08:00
parent 466e94c4dd
commit a5a35777b5

View File

@ -451,6 +451,31 @@ const getTypeTag = (type: string) => {
} }
} }
// --- 核心逻辑 0加载全量库存数据BOM 齐套计算依赖此数据) ---
const ensureAllStockLoaded = async () => {
if (allStockData.value.length === 0) {
try {
const res: any = await getAllStock()
const rawMaterials = (res.materials || []).map((i: any) => ({ ...i, type: 'material', typeLabel: '采购件' }))
const rawSemis = (res.semis || []).map((i: any) => ({ ...i, type: 'semi', typeLabel: '半成品' }))
const rawProducts = (res.products || []).map((i: any) => ({ ...i, type: 'product', typeLabel: '成品' }))
const list = [...rawMaterials, ...rawSemis, ...rawProducts]
allStockData.value = list.map((i: any) => ({
...i,
name: i.name || i.material_name || i.product_name || '未知名称',
standard: i.standard || i.spec_model || '',
warehouse_location: i.warehouse_location || i.warehouse_loc || i.full_path || '',
uniqueKey: `${i.type}_${i.id}`,
available_quantity: parseFloat(i.available_quantity) || 0,
availableCount: parseFloat(i.available_quantity) || 0,
export_quantity: 1
}))
} catch (e) {
ElMessage.error('加载全量库存数据失败BOM 功能可能受影响)')
}
}
}
// --- 核心逻辑 1手动添加库存 --- // --- 核心逻辑 1手动添加库存 ---
// 服务端加载库存列表 // 服务端加载库存列表
@ -482,31 +507,8 @@ const openManualSelect = async () => {
stockPage.value = 1 stockPage.value = 1
searchKeyword.value = '' searchKeyword.value = ''
await loadStockList() await loadStockList()
await ensureAllStockLoaded()
// 仅在 BOM 关联查询需要时加载全量(一次性缓存) allStockData.value.forEach(item => item.export_quantity = 0)
if (allStockData.value.length === 0) {
try {
const res: any = await getAllStock()
const rawMaterials = (res.materials || []).map((i: any) => ({ ...i, type: 'material', typeLabel: '采购件' }))
const rawSemis = (res.semis || []).map((i: any) => ({ ...i, type: 'semi', typeLabel: '半成品' }))
const rawProducts = (res.products || []).map((i: any) => ({ ...i, type: 'product', typeLabel: '成品' }))
const list = [...rawMaterials, ...rawSemis, ...rawProducts]
allStockData.value = list.map((i: any) => ({
...i,
name: i.name || i.material_name || i.product_name || '未知名称',
standard: i.standard || i.spec_model || '',
warehouse_location: i.warehouse_location || i.warehouse_loc || i.full_path || '',
uniqueKey: `${i.type}_${i.id}`,
available_quantity: parseFloat(i.available_quantity) || 0,
availableCount: parseFloat(i.available_quantity) || 0,
export_quantity: 1
}))
} catch (e) {
ElMessage.error('加载全量库存数据失败BOM 功能可能受影响)')
}
} else {
allStockData.value.forEach(item => item.export_quantity = 0)
}
} }
// 搜索框防抖触发服务端过滤 // 搜索框防抖触发服务端过滤
@ -611,6 +613,7 @@ const openBomSelect = async () => {
} catch (e) { } catch (e) {
ElMessage.error('加载 BOM 列表失败') ElMessage.error('加载 BOM 列表失败')
} }
await ensureAllStockLoaded()
} }
// 监听 BOM 选择变化,自动加载明细并计算齐套性 // 监听 BOM 选择变化,自动加载明细并计算齐套性