Bin check was using count_last_audit (0 for bulk items) as the audit value for all items. Now looks up product kind and uses last_audit_weight for bulk items, only updates count_last_audit for discrete items. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user