diff --git a/web-panel/pages/almacen.vue b/web-panel/pages/almacen.vue index b3b8548..09f57fc 100644 --- a/web-panel/pages/almacen.vue +++ b/web-panel/pages/almacen.vue @@ -456,11 +456,30 @@ async function loadWarehouses() { })); } +function aggregateClient(rows: StockRow[]): StockRow[] { + const map = new Map(); + for (const row of rows) { + const id = Number(row.material_id); + if (!id) continue; + const cur = map.get(id); + if (!cur) { + map.set(id, { ...row, warehouse_id: undefined, warehouse_count: 1 }); + continue; + } + cur.qty_on_hand = Number(cur.qty_on_hand || 0) + Number(row.qty_on_hand || 0); + cur.stock_value = Number(cur.stock_value || 0) + Number(row.stock_value || 0); + cur.warehouse_count = Number(cur.warehouse_count || 1) + 1; + } + return [...map.values()]; +} + async function loadStock() { if (isGlobalView.value) { const r = await api<{ stock?: StockRow[]; consolidated?: StockRow[] }>("/v1/warehouses/stock/global"); - stockRows.value = withRowKeys(r.stock ?? [], "d"); - consolidated.value = withRowKeys(r.consolidated ?? [], "c"); + const detail = r.stock ?? []; + stockRows.value = withRowKeys(detail, "d"); + const cons = r.consolidated ?? []; + consolidated.value = withRowKeys(cons.length ? cons : aggregateClient(detail), "c"); return; } const id = selectedWarehouse.value?.id;