This commit is contained in:
seb
2026-10-27 06:48:35 +00:00
parent bab5c151d0
commit ccb81cdb26
84 changed files with 5072 additions and 3625 deletions

View File

@@ -249,6 +249,38 @@ WHERE articlelist_id IN (
)
`;
export const ENQUEUE_SYNC_BY_ARTICLE = `
INSERT INTO posterminalsyncqueue (posterminal_id, entity_type, entity_id)
SELECT pt.id, 'article', a.id
FROM article a
INNER JOIN posterminal pt ON pt.company_id = a.company_id
WHERE a.id = @articleId
`;
export const ENQUEUE_OPTION_PARENT_ARTICLES_SYNC = `
INSERT INTO posterminalsyncqueue (posterminal_id, entity_type, entity_id)
SELECT pt.id, 'article', a.id
FROM article a
INNER JOIN posterminal pt ON pt.company_id = a.company_id
WHERE a.optionslist_id IN (
SELECT ola.optionslist_id
FROM optionslistarticle ola
WHERE ola.article_id = @articleId
)
`;
export const ENQUEUE_STUECKLISTE_ARTICLES_SYNC = `
INSERT INTO posterminalsyncqueue (posterminal_id, entity_type, entity_id)
SELECT pt.id, 'article', a.id
FROM article a
INNER JOIN posterminal pt ON pt.company_id = a.company_id
WHERE a.articlelist_id IN (
SELECT ala.articlelist_id
FROM articlelistarticle ala
WHERE ala.article_id = @articleId
)
`;
export const CREATE = `
INSERT INTO article (company_id, sku, unit_id, unitamount, weight, track_stock)

View File

@@ -240,3 +240,40 @@ WHERE id IN (
)
`;
export const ENQUEUE_SUBTREE_CATEGORIES_SYNC = `
;WITH cats AS (
SELECT id FROM category WHERE id = @id
UNION ALL
SELECT c.id FROM category c INNER JOIN cats ON c.parent_id = cats.id
)
INSERT INTO posterminalsyncqueue (posterminal_id, entity_type, entity_id)
SELECT pt.id, 'category', cats.id
FROM cats
INNER JOIN posterminal pt ON pt.company_id = @companyId
`;
export const ENQUEUE_SUBTREE_ARTICLES_SYNC = `
;WITH cats AS (
SELECT id FROM category WHERE id = @id
UNION ALL
SELECT c.id FROM category c INNER JOIN cats ON c.parent_id = cats.id
),
arts AS (
SELECT ca.article_id AS id FROM categoryarticle ca WHERE ca.category_id IN (SELECT id FROM cats)
UNION
SELECT child.id FROM article child
WHERE child.parentarticle_id IN (
SELECT ca.article_id FROM categoryarticle ca WHERE ca.category_id IN (SELECT id FROM cats)
)
UNION
SELECT parent.parentarticle_id AS id FROM article parent
WHERE parent.id IN (
SELECT ca.article_id FROM categoryarticle ca WHERE ca.category_id IN (SELECT id FROM cats)
) AND parent.parentarticle_id IS NOT NULL
)
INSERT INTO posterminalsyncqueue (posterminal_id, entity_type, entity_id)
SELECT pt.id, 'article', arts.id
FROM arts
INNER JOIN posterminal pt ON pt.company_id = @companyId
`;

View File

@@ -136,6 +136,12 @@ WHERE id = @id
AND customergroup_id = @customergroupId
`;
export const TOUCH_UPDATED_AT = `
UPDATE entity
SET updated_at = SYSUTCDATETIME()
WHERE id = @id
`;
export const DELETE_BY_COMPANY_AND_ID = `
DELETE e
FROM entity e

View File

@@ -172,66 +172,100 @@ WHERE token = @token
`;
export const COUNT_PRODUCTS = `
SELECT COUNT(DISTINCT a.id) AS count
FROM article a
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = a.company_id
WHERE EXISTS (
SELECT 1
FROM categoryarticle ca
INNER JOIN posterminalcategory pc ON pc.category_id = ca.category_id
WHERE ca.article_id = a.id
AND pc.posterminal_id = @posterminalId
)
AND (DATEDIFF_BIG(second, '1970-01-01', a.updated_at) * 1000000 + (a.id % 1000000)) > @cursor
SELECT COUNT(*) AS count
FROM (
SELECT MAX(q.id) AS syncId, a.id
FROM posterminalsyncqueue q
INNER JOIN article a ON a.id = q.entity_id
INNER JOIN posterminal pt ON pt.id = q.posterminal_id AND pt.company_id = a.company_id
WHERE q.posterminal_id = @posterminalId
AND q.entity_type = 'article'
AND EXISTS (
SELECT 1
FROM categoryarticle ca
INNER JOIN posterminalcategory pc ON pc.category_id = ca.category_id
WHERE ca.article_id = a.id
AND pc.posterminal_id = @posterminalId
)
GROUP BY a.id
HAVING MAX(q.id) > @cursor
) t
`;
export const COUNT_CATEGORIES = `
SELECT COUNT(*) AS count
FROM category c
INNER JOIN posterminalcategory pc ON pc.category_id = c.id
INNER JOIN posterminal pt ON pt.id = pc.posterminal_id
INNER JOIN company co ON co.id = pt.company_id
WHERE pc.posterminal_id = @posterminalId
AND (co.root_category_id IS NULL OR c.id <> co.root_category_id)
AND DATEDIFF_BIG(second, '1970-01-01', c.updated_at) > @cursor
FROM (
SELECT MAX(q.id) AS syncId, c.id
FROM posterminalsyncqueue q
INNER JOIN category c ON c.id = q.entity_id
INNER JOIN posterminalcategory pc ON pc.category_id = c.id AND pc.posterminal_id = q.posterminal_id
INNER JOIN posterminal pt ON pt.id = pc.posterminal_id
INNER JOIN company co ON co.id = pt.company_id
WHERE q.posterminal_id = @posterminalId
AND q.entity_type = 'category'
AND (co.root_category_id IS NULL OR c.id <> co.root_category_id)
GROUP BY c.id
HAVING MAX(q.id) > @cursor
) t
`;
export const COUNT_CUSTOMERS = `
SELECT COUNT(*) AS count
FROM entity e
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = cg.company_id
WHERE cg.name <> 'Lieferanten'
AND DATEDIFF_BIG(second, '1970-01-01', e.updated_at) > @cursor
FROM (
SELECT MAX(q.id) AS syncId, e.id
FROM posterminalsyncqueue q
INNER JOIN entity e ON e.id = q.entity_id
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
INNER JOIN posterminal pt ON pt.id = q.posterminal_id AND pt.company_id = cg.company_id
WHERE q.posterminal_id = @posterminalId
AND q.entity_type = 'customer'
AND cg.name <> 'Lieferanten'
GROUP BY e.id
HAVING MAX(q.id) > @cursor
) t
`;
export const COUNT_CUSTOMER_GROUPS = `
SELECT COUNT(*) AS count
FROM customergroup cg
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = cg.company_id
WHERE cg.name <> 'Lieferanten'
AND DATEDIFF_BIG(second, '1970-01-01', cg.updated_at) > @cursor
FROM (
SELECT MAX(q.id) AS syncId, cg.id
FROM posterminalsyncqueue q
INNER JOIN customergroup cg ON cg.id = q.entity_id
INNER JOIN posterminal pt ON pt.id = q.posterminal_id AND pt.company_id = cg.company_id
WHERE q.posterminal_id = @posterminalId
AND q.entity_type = 'customergroup'
AND cg.name <> 'Lieferanten'
GROUP BY cg.id
HAVING MAX(q.id) > @cursor
) t
`;
export const COUNT_COMPOSITE_PRODUCTS = `
SELECT COUNT(DISTINCT a.id) AS count
FROM article a
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = a.company_id
WHERE a.articlelist_id IS NOT NULL
AND EXISTS (
SELECT 1
FROM articlelistarticle ala
WHERE ala.articlelist_id = a.articlelist_id
)
AND EXISTS (
SELECT 1
FROM categoryarticle ca
INNER JOIN posterminalcategory pc ON pc.category_id = ca.category_id
WHERE ca.article_id = a.id
AND pc.posterminal_id = @posterminalId
)
AND (DATEDIFF_BIG(second, '1970-01-01', a.updated_at) * 1000000 + (a.id % 1000000)) > @cursor
SELECT COUNT(*) AS count
FROM (
SELECT MAX(q.id) AS syncId, a.id
FROM posterminalsyncqueue q
INNER JOIN article a ON a.id = q.entity_id
INNER JOIN posterminal pt ON pt.id = q.posterminal_id AND pt.company_id = a.company_id
WHERE q.posterminal_id = @posterminalId
AND q.entity_type = 'article'
AND a.articlelist_id IS NOT NULL
AND EXISTS (
SELECT 1
FROM articlelistarticle ala
WHERE ala.articlelist_id = a.articlelist_id
)
AND EXISTS (
SELECT 1
FROM categoryarticle ca
INNER JOIN posterminalcategory pc ON pc.category_id = ca.category_id
WHERE ca.article_id = a.id
AND pc.posterminal_id = @posterminalId
)
GROUP BY a.id
HAVING MAX(q.id) > @cursor
) t
`;
export const MAX_ORDER_ID = `
@@ -255,16 +289,21 @@ SELECT TOP (@limit)
WHERE cp.category_id = c.id
ORDER BY cp.sort ASC, cp.id ASC
) AS imgHash,
DATEDIFF_BIG(second, '1970-01-01', c.updated_at) AS lastChanged
FROM category c
INNER JOIN posterminalcategory pc ON pc.category_id = c.id
sq.syncId AS lastChanged
FROM (
SELECT entity_id, MAX(id) AS syncId
FROM posterminalsyncqueue
WHERE posterminal_id = @posterminalId AND entity_type = 'category'
GROUP BY entity_id
HAVING MAX(id) > @cursor
) sq
INNER JOIN category c ON c.id = sq.entity_id
INNER JOIN posterminalcategory pc ON pc.category_id = c.id AND pc.posterminal_id = @posterminalId
INNER JOIN posterminal pt ON pt.id = pc.posterminal_id
INNER JOIN company co ON co.id = pt.company_id
LEFT JOIN categorytext ct ON ct.category_id = c.id AND ct.language_id = co.default_language_id
WHERE pc.posterminal_id = @posterminalId
AND (co.root_category_id IS NULL OR c.id <> co.root_category_id)
AND DATEDIFF_BIG(second, '1970-01-01', c.updated_at) > @cursor
ORDER BY DATEDIFF_BIG(second, '1970-01-01', c.updated_at) ASC, c.id ASC
WHERE (co.root_category_id IS NULL OR c.id <> co.root_category_id)
ORDER BY sq.syncId ASC
`;
export const LIST_POS_PRODUCTS = `
@@ -275,7 +314,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) * 1000000 + (a.id % 1000000)) AS lastChanged,
sq.syncId AS lastChanged,
COALESCE(
(
SELECT TOP 1 p.hash
@@ -374,7 +413,14 @@ SELECT TOP (@limit)
co.id AS companyId,
co.root_category_id AS rootCategoryId,
co.poscustomergroup_id AS posCustomerGroupId
FROM article a
FROM (
SELECT entity_id, MAX(id) AS syncId
FROM posterminalsyncqueue
WHERE posterminal_id = @posterminalId AND entity_type = 'article'
GROUP BY entity_id
HAVING MAX(id) > @cursor
) sq
INNER JOIN article a ON a.id = sq.entity_id
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = a.company_id
INNER JOIN company co ON co.id = a.company_id
LEFT JOIN articletext at ON at.article_id = a.id AND at.language_id = co.default_language_id
@@ -388,8 +434,7 @@ WHERE EXISTS (
WHERE ca.article_id = a.id
AND pc.posterminal_id = @posterminalId
)
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
ORDER BY sq.syncId ASC
`;
export const LIST_POS_PRODUCT_CATEGORIES = `
@@ -411,13 +456,19 @@ SELECT TOP (@limit)
cg.name,
CASE WHEN cg.id = co.poscustomergroup_id THEN '1' ELSE '0' END AS standard,
'0.000000' AS discountPercent,
DATEDIFF_BIG(second, '1970-01-01', cg.updated_at) AS lastChanged
FROM customergroup cg
sq.syncId AS lastChanged
FROM (
SELECT entity_id, MAX(id) AS syncId
FROM posterminalsyncqueue
WHERE posterminal_id = @posterminalId AND entity_type = 'customergroup'
GROUP BY entity_id
HAVING MAX(id) > @cursor
) sq
INNER JOIN customergroup cg ON cg.id = sq.entity_id
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = cg.company_id
INNER JOIN company co ON co.id = pt.company_id
WHERE cg.name <> 'Lieferanten'
AND DATEDIFF_BIG(second, '1970-01-01', cg.updated_at) > @cursor
ORDER BY DATEDIFF_BIG(second, '1970-01-01', cg.updated_at) ASC, cg.id ASC
ORDER BY sq.syncId ASC
`;
export const LIST_POS_CUSTOMERS = `
@@ -469,13 +520,19 @@ SELECT TOP (@limit)
WHERE ec.entity_id = e.id AND ec.contacttype IN ('email', 'mail')
ORDER BY ec.sort ASC, ec.id ASC
) AS email,
DATEDIFF_BIG(second, '1970-01-01', e.updated_at) AS lastChanged
FROM entity e
sq.syncId AS lastChanged
FROM (
SELECT entity_id, MAX(id) AS syncId
FROM posterminalsyncqueue
WHERE posterminal_id = @posterminalId AND entity_type = 'customer'
GROUP BY entity_id
HAVING MAX(id) > @cursor
) sq
INNER JOIN entity e ON e.id = sq.entity_id
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = cg.company_id
WHERE cg.name <> 'Lieferanten'
AND DATEDIFF_BIG(second, '1970-01-01', e.updated_at) > @cursor
ORDER BY DATEDIFF_BIG(second, '1970-01-01', e.updated_at) ASC, e.id ASC
ORDER BY sq.syncId ASC
`;
@@ -541,6 +598,34 @@ FROM posterminal pt
WHERE pt.company_id = @companyId AND pt.token IS NOT NULL
`;
export const RECORD_SYNC_QUEUE = `
INSERT INTO posterminalsyncqueue (posterminal_id, entity_type, entity_id)
SELECT pt.id, @entityType, @entityId
FROM posterminal pt
WHERE pt.company_id = @companyId
`;
export const SEED_SYNC_QUEUE_FOR_TERMINAL = `
INSERT INTO posterminalsyncqueue (posterminal_id, entity_type, entity_id)
SELECT @posterminalId, 'customergroup', cg.id
FROM customergroup cg
WHERE cg.company_id = @companyId
UNION ALL
SELECT @posterminalId, 'customer', e.id
FROM entity e
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
WHERE cg.company_id = @companyId
UNION ALL
SELECT @posterminalId, 'category', c.id
FROM category c
INNER JOIN posterminalcategory pc ON pc.category_id = c.id
WHERE pc.posterminal_id = @posterminalId
UNION ALL
SELECT @posterminalId, 'article', a.id
FROM article a
WHERE a.company_id = @companyId
`;
export const COUNT_DELETED_ENTITIES = `
SELECT COUNT(*) AS count
FROM posterminalobituary