diff --git a/src/store/ledger/add.js b/src/store/ledger/add.js index 7a602e3..f3454e1 100644 --- a/src/store/ledger/add.js +++ b/src/store/ledger/add.js @@ -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, { diff --git a/src/store/stock/adjust.js b/src/store/stock/adjust.js index 5042046..9c01338 100644 --- a/src/store/stock/adjust.js +++ b/src/store/stock/adjust.js @@ -116,7 +116,7 @@ export default function adjust(provider) { const resolvedArticles = []; for (const item of normalizedLines) { const resolved = await resolveArticle(googleAuthId, item.sku, activeTx); - const artRes = await runner.query('SELECT is_father, articlelist_id FROM article WHERE id = @articleId', { + const artRes = await runner.query('SELECT is_father, articlelist_id, track_stock FROM article WHERE id = @articleId', { articleId: resolved.articleId, }); if (artRes.rows[0]?.is_father) { @@ -127,6 +127,10 @@ export default function adjust(provider) { throw new Error(`A Stückliste cannot have manual stock adjustments (${item.sku})`); } + if (Number(artRes.rows[0]?.track_stock ?? 1) === 0) { + throw new Error(`Stock tracking is disabled for article (${item.sku})`); + } + const nameRes = await runner.query(GET_ARTICLE_NAME_SQL, { articleId: resolved.articleId, languageId: resolved.languageId,