This commit is contained in:
seb
2026-10-23 04:59:04 +02:00
parent 8a085986e5
commit 8002ca56e6
8 changed files with 454 additions and 66 deletions

View File

@@ -93,20 +93,32 @@ FROM posterminalcategory
WHERE posterminal_id = @posterminalId
`;
/** setOn: insert if not exists (single terminal + single category). */
/** setOn: enable a category and all its subcategories on a terminal. */
export const SET_ON = `
IF NOT EXISTS (
SELECT 1 FROM posterminalcategory
WHERE posterminal_id = @posterminalId AND category_id = @categoryId
;WITH cats AS (
SELECT id FROM category WHERE id = @categoryId
UNION ALL
SELECT c.id FROM category c INNER JOIN cats ON c.parent_id = cats.id
)
INSERT INTO posterminalcategory (posterminal_id, category_id)
VALUES (@posterminalId, @categoryId)
SELECT @posterminalId, cats.id
FROM cats
WHERE NOT EXISTS (
SELECT 1 FROM posterminalcategory pc
WHERE pc.posterminal_id = @posterminalId AND pc.category_id = cats.id
)
`;
/** setOff: remove a single category from a single terminal. */
/** setOff: remove a category and all its subcategories from a terminal. */
export const SET_OFF = `
;WITH cats AS (
SELECT id FROM category WHERE id = @categoryId
UNION ALL
SELECT c.id FROM category c INNER JOIN cats ON c.parent_id = cats.id
)
DELETE FROM posterminalcategory
WHERE posterminal_id = @posterminalId AND category_id = @categoryId
WHERE posterminal_id = @posterminalId
AND category_id IN (SELECT id FROM cats)
`;
/** setOnAll: enable ALL company categories on one terminal. */
@@ -170,7 +182,7 @@ WHERE EXISTS (
WHERE ca.article_id = a.id
AND pc.posterminal_id = @posterminalId
)
AND DATEDIFF_BIG(second, '1970-01-01', a.updated_at) > @cursor
AND (DATEDIFF_BIG(second, '1970-01-01', a.updated_at) * 1000000 + (a.id % 1000000)) > @cursor
`;
export const COUNT_CATEGORIES = `
@@ -219,7 +231,7 @@ WHERE a.articlelist_id IS NOT NULL
WHERE ca.article_id = a.id
AND pc.posterminal_id = @posterminalId
)
AND DATEDIFF_BIG(second, '1970-01-01', a.updated_at) > @cursor
AND (DATEDIFF_BIG(second, '1970-01-01', a.updated_at) * 1000000 + (a.id % 1000000)) > @cursor
`;
export const MAX_ORDER_ID = `
@@ -263,7 +275,7 @@ SELECT TOP (@limit)
COALESCE(pr.net, 0) AS netPrice,
COALESCE(ptax.rate, ctax.rate, 0) AS taxRate,
a.created_at AS createdAt,
DATEDIFF_BIG(second, '1970-01-01', a.updated_at) AS lastChanged,
(DATEDIFF_BIG(second, '1970-01-01', a.updated_at) * 1000000 + (a.id % 1000000)) AS lastChanged,
COALESCE(
(
SELECT TOP 1 p.hash
@@ -321,6 +333,7 @@ SELECT TOP (@limit)
) AS sort,
a.track_stock AS trackStock,
CASE
WHEN a.track_stock = 0 THEN 0
WHEN a.articlelist_id IS NOT NULL AND EXISTS (
SELECT 1 FROM articlelistarticle ala WHERE ala.articlelist_id = a.articlelist_id
) THEN COALESCE((
@@ -339,6 +352,7 @@ SELECT TOP (@limit)
OR ma.sku IN (SELECT asup.sku FROM articlesupplier asup WHERE asup.article_id = ca.id)
)
WHERE m.company_id = a.company_id
AND l.warehouse_id IS NOT NULL
GROUP BY ca.id
) comp_stock ON comp_stock.comp_id = ala.article_id
WHERE ala.articlelist_id = a.articlelist_id
@@ -350,6 +364,7 @@ SELECT TOP (@limit)
INNER JOIN manifestarticle ma ON ma.id = l.manifestarticle_id
INNER JOIN manifest m ON m.id = ma.manifest_id
WHERE m.company_id = a.company_id
AND l.warehouse_id IS NOT NULL
AND (
ma.sku = a.sku
OR ma.sku IN (SELECT asup.sku FROM articlesupplier asup WHERE asup.article_id = a.id)
@@ -373,8 +388,8 @@ WHERE EXISTS (
WHERE ca.article_id = a.id
AND pc.posterminal_id = @posterminalId
)
AND DATEDIFF_BIG(second, '1970-01-01', a.updated_at) > @cursor
ORDER BY DATEDIFF_BIG(second, '1970-01-01', a.updated_at) ASC, a.id ASC
AND (DATEDIFF_BIG(second, '1970-01-01', a.updated_at) * 1000000 + (a.id % 1000000)) > @cursor
ORDER BY (DATEDIFF_BIG(second, '1970-01-01', a.updated_at) * 1000000 + (a.id % 1000000)) ASC
`;
export const LIST_POS_PRODUCT_CATEGORIES = `