diff --git a/server/src/routes/inventory.ts b/server/src/routes/inventory.ts index c533c7c..867c3fd 100644 --- a/server/src/routes/inventory.ts +++ b/server/src/routes/inventory.ts @@ -455,21 +455,31 @@ inventoryRouter.post("/bins/:id/check", (req, res) => { const item = db .prepare< [string], - { product_id: string; count_original: number; count_last_audit: number | null } + { product_id: string; weight: number; last_audit_weight: number | null; count_original: number; count_last_audit: number | null } >( - `SELECT product_id, count_original, count_last_audit FROM inventory_items WHERE id = ?`, + `SELECT product_id, weight, last_audit_weight, count_original, count_last_audit FROM inventory_items WHERE id = ?`, ) .get(itemId); if (!item) continue; - const prev = item.count_last_audit ?? item.count_original; + const product = db + .prepare<[string], { kind: string }>(`SELECT kind FROM products WHERE id = ?`) + .get(item.product_id); + const isDiscrete = product?.kind === "discrete"; + + const prev = isDiscrete + ? item.count_last_audit ?? item.count_original + : item.last_audit_weight ?? item.weight; + db.prepare( `INSERT INTO audits (inventory_id, date, mode, value, prev_value, confirmed_by) VALUES (?, ?, 'presence', ?, ?, 'bin-check')`, ).run(itemId, date, prev, prev); - db.prepare( - `UPDATE inventory_items SET count_last_audit = ? WHERE id = ?`, - ).run(prev, itemId); + if (isDiscrete) { + db.prepare( + `UPDATE inventory_items SET count_last_audit = ? WHERE id = ?`, + ).run(prev, itemId); + } } for (const itemId of goneItemIds) {