fix: consolidar stock global en cliente si el API aún no agrupa

Co-authored-by: alberto.martinez <alberto.martinez@mrdev.mx>
This commit is contained in:
Cursor Agent 2026-09-08 20:42:13 +00:00
parent e528d217d3
commit af5bbd7566
No known key found for this signature in database

View file

@ -456,11 +456,30 @@ async function loadWarehouses() {
}));
}
function aggregateClient(rows: StockRow[]): StockRow[] {
const map = new Map<number, StockRow>();
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;