This commit is contained in:
seb
2026-10-01 21:15:55 +02:00
parent 84e547660e
commit 856d5419a1
2 changed files with 25 additions and 1 deletions

View File

@@ -131,6 +131,26 @@ export default function add(provider) {
throw new Error('Manifest article not found');
}
// Stock guard: warehouse-scoped ledger entries affect article stock
// (stock is SUM(ledger.amount) GROUP BY warehouse). Reject bookings for
// articles with stock tracking disabled before anything is written.
if (warehouseId != null) {
const sku = manifestArticle.sku ? String(manifestArticle.sku).trim() : '';
if (sku) {
const trackRes = await runner.query(
`SELECT a.sku, a.track_stock FROM article a
WHERE a.company_id = @companyId
AND (a.sku = @sku
OR EXISTS (SELECT 1 FROM articlesupplier asup WHERE asup.article_id = a.id AND asup.sku = @sku))`,
{ companyId, sku },
);
const disabled = (trackRes.rows ?? []).find((r) => Number(r.track_stock ?? 1) === 0);
if (disabled) {
throw new Error(`Stock tracking is disabled for article (${sku})`);
}
}
}
let warehouseName = null;
if (warehouseId != null) {
const whCheck = await runner.query(queries.CHECK_WAREHOUSE_ACCESS, {