645 lines
21 KiB
JavaScript
645 lines
21 KiB
JavaScript
export const LIST_BY_COMPANY = `
|
|
SELECT
|
|
id,
|
|
name,
|
|
token,
|
|
pairingpin,
|
|
paired_at AS pairedAt,
|
|
clientserial AS clientserial,
|
|
maxknownobituaryid AS maxknownobituaryid,
|
|
root_category_id AS rootCategoryId,
|
|
sync_root_articles AS syncRootArticles,
|
|
lastseen_at AS lastseenAt,
|
|
updated_at AS updatedAt
|
|
FROM posterminal
|
|
WHERE company_id = @companyId
|
|
ORDER BY id
|
|
`;
|
|
|
|
export const GET_BY_ID = `
|
|
SELECT
|
|
id,
|
|
company_id AS companyId,
|
|
name,
|
|
token,
|
|
pairingpin,
|
|
paired_at AS pairedAt,
|
|
clientserial AS clientserial,
|
|
maxknownobituaryid AS maxknownobituaryid,
|
|
root_category_id AS rootCategoryId,
|
|
sync_root_articles AS syncRootArticles,
|
|
lastseen_at AS lastseenAt,
|
|
updated_at AS updatedAt
|
|
FROM posterminal
|
|
WHERE id = @id
|
|
`;
|
|
|
|
export const FIND_BY_PAIRINGPIN = `
|
|
SELECT
|
|
id,
|
|
company_id AS companyId,
|
|
name,
|
|
token,
|
|
pairingpin,
|
|
paired_at AS pairedAt,
|
|
clientserial AS clientserial,
|
|
maxknownobituaryid AS maxknownobituaryid,
|
|
root_category_id AS rootCategoryId,
|
|
sync_root_articles AS syncRootArticles,
|
|
lastseen_at AS lastseenAt,
|
|
updated_at AS updatedAt
|
|
FROM posterminal
|
|
WHERE pairingpin = @pairingpin
|
|
`;
|
|
|
|
export const PAIR = `
|
|
UPDATE posterminal
|
|
SET token = @token, paired_at = @pairedAt, clientserial = @clientserial, pairingpin = NULL, lastseen_at = @pairedAt, updated_at = SYSUTCDATETIME()
|
|
WHERE id = @id
|
|
`;
|
|
|
|
export const CREATE = `
|
|
INSERT INTO posterminal (company_id, name, token, pairingpin, paired_at, clientserial, maxknownobituaryid, root_category_id, sync_root_articles, lastseen_at, updated_at)
|
|
OUTPUT INSERTED.id, INSERTED.name, INSERTED.token, INSERTED.pairingpin, INSERTED.paired_at AS pairedAt, INSERTED.clientserial AS clientserial, INSERTED.maxknownobituaryid AS maxknownobituaryid, INSERTED.root_category_id AS rootCategoryId, INSERTED.sync_root_articles AS syncRootArticles, INSERTED.lastseen_at AS lastseenAt, INSERTED.updated_at AS updatedAt
|
|
VALUES (@companyId, @name, @token, @pairingpin, @pairedAt, @clientserial, @maxknownobituaryid, @rootCategoryId, @syncRootArticles, @lastseenAt, SYSUTCDATETIME())
|
|
`;
|
|
|
|
export const UPDATE = `
|
|
UPDATE posterminal
|
|
SET name = @name, token = @token, pairingpin = @pairingpin, paired_at = @pairedAt, clientserial = @clientserial, maxknownobituaryid = @maxknownobituaryid, root_category_id = @rootCategoryId, sync_root_articles = @syncRootArticles, lastseen_at = @lastseenAt, updated_at = SYSUTCDATETIME()
|
|
WHERE id = @id AND company_id = @companyId
|
|
`;
|
|
|
|
export const TOUCH_LASTSEEN = `
|
|
UPDATE posterminal
|
|
SET lastseen_at = SYSUTCDATETIME()
|
|
WHERE id = @id
|
|
`;
|
|
|
|
export const SET_MAX_KNOWN_OBITUARY_ID = `
|
|
UPDATE posterminal
|
|
SET maxknownobituaryid = @maxknownobituaryid
|
|
WHERE id = @id
|
|
`;
|
|
|
|
export const DELETE_BY_COMPANY_AND_ID = `
|
|
DELETE FROM posterminal
|
|
WHERE id = @id AND company_id = @companyId
|
|
`;
|
|
|
|
export const LIST_CATEGORY_IDS = `
|
|
SELECT category_id AS categoryId
|
|
FROM posterminalcategory
|
|
WHERE posterminal_id = @posterminalId
|
|
`;
|
|
|
|
/** setOn: enable a category and all its subcategories on a terminal. */
|
|
export const SET_ON = `
|
|
;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)
|
|
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 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 IN (SELECT id FROM cats)
|
|
`;
|
|
|
|
/** setOnAll: enable ALL company categories on one terminal. */
|
|
export const SET_ON_ALL = `
|
|
;WITH cats AS (
|
|
SELECT id FROM category WHERE id = @rootCategoryId
|
|
UNION ALL
|
|
SELECT c.id FROM category c INNER JOIN cats ON c.parent_id = cats.id
|
|
)
|
|
INSERT INTO posterminalcategory (posterminal_id, category_id)
|
|
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
|
|
)
|
|
`;
|
|
|
|
/** setOffAll: remove ALL category links from one terminal. */
|
|
export const SET_OFF_ALL = `
|
|
DELETE FROM posterminalcategory WHERE posterminal_id = @posterminalId
|
|
`;
|
|
|
|
/** For category tree: map category -> posterminal ids (company-scoped). */
|
|
export const POSTERMINAL_IDS_BY_COMPANY = `
|
|
SELECT
|
|
pc.category_id AS categoryId,
|
|
pc.posterminal_id AS posterminalId
|
|
FROM posterminalcategory pc
|
|
INNER JOIN posterminal pt ON pt.id = pc.posterminal_id
|
|
WHERE pt.company_id = @companyId
|
|
`;
|
|
|
|
|
|
export const FIND_BY_TOKEN = `
|
|
SELECT
|
|
id,
|
|
company_id AS companyId,
|
|
name,
|
|
token,
|
|
pairingpin,
|
|
paired_at AS pairedAt,
|
|
clientserial AS clientserial,
|
|
maxknownobituaryid AS maxknownobituaryid,
|
|
root_category_id AS rootCategoryId,
|
|
sync_root_articles AS syncRootArticles,
|
|
lastseen_at AS lastseenAt,
|
|
updated_at AS updatedAt
|
|
FROM posterminal
|
|
WHERE token = @token
|
|
`;
|
|
|
|
export const COUNT_PRODUCTS = `
|
|
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 (
|
|
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 (
|
|
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 (
|
|
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(*) 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 = `
|
|
SELECT ISNULL(MAX(m.id), 0) AS maxOrderId
|
|
FROM manifest m
|
|
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = m.company_id
|
|
WHERE m.[type] = 'order'
|
|
`;
|
|
|
|
export const LIST_POS_CATEGORIES = `
|
|
SELECT TOP (@limit)
|
|
c.id,
|
|
c.parent_id AS pid,
|
|
c.sort,
|
|
COALESCE(ct.name, '') AS name,
|
|
co.root_category_id AS rootCategoryId,
|
|
(
|
|
SELECT TOP 1 p.hash
|
|
FROM categorypic cp
|
|
INNER JOIN pic p ON p.id = cp.pic_id
|
|
WHERE cp.category_id = c.id
|
|
ORDER BY cp.sort ASC, cp.id ASC
|
|
) AS imgHash,
|
|
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 (co.root_category_id IS NULL OR c.id <> co.root_category_id)
|
|
ORDER BY sq.syncId ASC
|
|
`;
|
|
|
|
export const LIST_POS_PRODUCTS = `
|
|
SELECT TOP (@limit)
|
|
a.id,
|
|
a.sku,
|
|
COALESCE(at.name, a.sku, '') AS name,
|
|
COALESCE(pr.net, 0) AS netPrice,
|
|
COALESCE(ptax.rate, ctax.rate, 0) AS taxRate,
|
|
a.created_at AS createdAt,
|
|
sq.syncId AS lastChanged,
|
|
COALESCE(
|
|
(
|
|
SELECT TOP 1 p.hash
|
|
FROM articlepic ap
|
|
INNER JOIN pic p ON p.id = ap.pic_id
|
|
WHERE ap.article_id = a.id
|
|
ORDER BY ap.sort ASC, ap.id ASC
|
|
),
|
|
(
|
|
SELECT TOP 1 p2.hash
|
|
FROM articlepic ap2
|
|
INNER JOIN pic p2 ON p2.id = ap2.pic_id
|
|
WHERE a.parentarticle_id IS NOT NULL AND ap2.article_id = a.parentarticle_id
|
|
ORDER BY ap2.sort ASC, ap2.id ASC
|
|
)
|
|
) AS imgHash,
|
|
(
|
|
SELECT TOP 1 ae.ean
|
|
FROM articleean ae
|
|
WHERE ae.article_id = a.id
|
|
ORDER BY ae.id ASC
|
|
) AS barcode,
|
|
a.is_parent AS isParent,
|
|
a.parentarticle_id AS parentArticleId,
|
|
a.parentsort AS parentsort,
|
|
(
|
|
SELECT TOP 1 av.value
|
|
FROM articleattributevalue aav
|
|
INNER JOIN attributevalue av ON av.id = aav.attributevalue_id
|
|
LEFT JOIN article fa ON fa.id = a.parentarticle_id
|
|
WHERE aav.article_id = a.id
|
|
AND (fa.parentattributename_id IS NULL OR av.attribute_id = fa.parentattributename_id)
|
|
ORDER BY CASE WHEN av.attribute_id = fa.parentattributename_id THEN 0 ELSE 1 END, aav.sort ASC, aav.id ASC
|
|
) AS variantName,
|
|
CASE
|
|
WHEN a.articlelist_id IS NOT NULL AND EXISTS (
|
|
SELECT 1 FROM articlelistarticle ala WHERE ala.articlelist_id = a.articlelist_id
|
|
) THEN '1'
|
|
ELSE '0'
|
|
END AS isCompositeProduct,
|
|
(
|
|
SELECT COUNT(*)
|
|
FROM categoryarticle ca2
|
|
INNER JOIN article a2 ON a2.id = ca2.article_id
|
|
LEFT JOIN articletext at2 ON at2.article_id = a2.id AND at2.language_id = co.default_language_id
|
|
WHERE ca2.category_id = (
|
|
SELECT MIN(ca1.category_id)
|
|
FROM categoryarticle ca1
|
|
WHERE ca1.article_id = a.id
|
|
)
|
|
AND (
|
|
COALESCE(at2.name, a2.sku, '') < COALESCE(at.name, a.sku, '')
|
|
OR (COALESCE(at2.name, a2.sku, '') = COALESCE(at.name, a.sku, '') AND a2.id < a.id)
|
|
)
|
|
) 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((
|
|
SELECT CASE
|
|
WHEN MIN(COALESCE(comp_stock.stock, 0) / ala.count) < 0 THEN 0
|
|
ELSE FLOOR(MIN(COALESCE(comp_stock.stock, 0) / ala.count))
|
|
END
|
|
FROM articlelistarticle ala
|
|
LEFT JOIN (
|
|
SELECT ca.id AS comp_id, SUM(l.amount) AS stock
|
|
FROM ledger l
|
|
INNER JOIN manifestarticle ma ON ma.id = l.manifestarticle_id
|
|
INNER JOIN manifest m ON m.id = ma.manifest_id
|
|
INNER JOIN article ca ON (
|
|
ma.sku = ca.sku
|
|
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
|
|
), 0)
|
|
WHEN a.articlelist_id IS NOT NULL THEN 0
|
|
ELSE COALESCE((
|
|
SELECT SUM(l.amount)
|
|
FROM ledger l
|
|
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)
|
|
)
|
|
), 0)
|
|
END AS fBestand,
|
|
co.id AS companyId,
|
|
co.root_category_id AS rootCategoryId,
|
|
co.poscustomergroup_id AS posCustomerGroupId
|
|
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
|
|
LEFT JOIN price pr ON pr.article_id = a.id AND pr.customergroup_id = co.poscustomergroup_id AND pr.minmultiplier = 1
|
|
LEFT JOIN tax ptax ON ptax.id = pr.tax_id
|
|
LEFT JOIN tax ctax ON ctax.id = co.taxrate
|
|
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
|
|
)
|
|
ORDER BY sq.syncId ASC
|
|
`;
|
|
|
|
export const LIST_POS_PRODUCT_CATEGORIES = `
|
|
SELECT
|
|
ca.article_id AS articleId,
|
|
ca.category_id AS categoryId
|
|
FROM categoryarticle ca
|
|
INNER JOIN posterminalcategory pc ON pc.category_id = ca.category_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 ca.category_id <> co.root_category_id)
|
|
ORDER BY ca.article_id ASC, ca.category_id ASC
|
|
`;
|
|
|
|
export const LIST_POS_CUSTOMER_GROUPS = `
|
|
SELECT TOP (@limit)
|
|
cg.id,
|
|
cg.name,
|
|
CASE WHEN cg.id = co.poscustomergroup_id THEN '1' ELSE '0' END AS standard,
|
|
'0.000000' AS discountPercent,
|
|
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'
|
|
ORDER BY sq.syncId ASC
|
|
`;
|
|
|
|
export const LIST_POS_CUSTOMERS = `
|
|
SELECT TOP (@limit)
|
|
e.id,
|
|
e.customergroup_id AS customergroupId,
|
|
e.first_name AS firstName,
|
|
e.name AS lastName,
|
|
e.[number] AS customerNumber,
|
|
(
|
|
SELECT TOP 1 ea.street
|
|
FROM entityaddress ea
|
|
WHERE ea.entity_id = e.id
|
|
ORDER BY CASE WHEN ea.addresstype = 'billing' THEN 0 ELSE 1 END, ea.sort ASC, ea.id ASC
|
|
) AS street,
|
|
(
|
|
SELECT TOP 1 ea.streetnumber
|
|
FROM entityaddress ea
|
|
WHERE ea.entity_id = e.id
|
|
ORDER BY CASE WHEN ea.addresstype = 'billing' THEN 0 ELSE 1 END, ea.sort ASC, ea.id ASC
|
|
) AS streetNumber,
|
|
(
|
|
SELECT TOP 1 ea.city
|
|
FROM entityaddress ea
|
|
WHERE ea.entity_id = e.id
|
|
ORDER BY CASE WHEN ea.addresstype = 'billing' THEN 0 ELSE 1 END, ea.sort ASC, ea.id ASC
|
|
) AS city,
|
|
(
|
|
SELECT TOP 1 ea.zip
|
|
FROM entityaddress ea
|
|
WHERE ea.entity_id = e.id
|
|
ORDER BY CASE WHEN ea.addresstype = 'billing' THEN 0 ELSE 1 END, ea.sort ASC, ea.id ASC
|
|
) AS postalCode,
|
|
(
|
|
SELECT TOP 1 ea.county
|
|
FROM entityaddress ea
|
|
WHERE ea.entity_id = e.id
|
|
ORDER BY CASE WHEN ea.addresstype = 'billing' THEN 0 ELSE 1 END, ea.sort ASC, ea.id ASC
|
|
) AS [state],
|
|
(
|
|
SELECT TOP 1 ec.contact
|
|
FROM entitycontact ec
|
|
WHERE ec.entity_id = e.id AND ec.contacttype IN ('phone', 'mobile', 'tel')
|
|
ORDER BY ec.sort ASC, ec.id ASC
|
|
) AS phone,
|
|
(
|
|
SELECT TOP 1 ec.contact
|
|
FROM entitycontact ec
|
|
WHERE ec.entity_id = e.id AND ec.contacttype IN ('email', 'mail')
|
|
ORDER BY ec.sort ASC, ec.id ASC
|
|
) AS email,
|
|
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'
|
|
ORDER BY sq.syncId ASC
|
|
`;
|
|
|
|
|
|
export const LIST_POS_CUSTOMER_GROUP_IDS = `
|
|
SELECT cg.id
|
|
FROM customergroup cg
|
|
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = cg.company_id
|
|
WHERE cg.name <> 'Lieferanten'
|
|
ORDER BY cg.id ASC
|
|
`;
|
|
|
|
|
|
export const LIST_POS_PRODUCT_PRICES = `
|
|
SELECT
|
|
pr.article_id AS articleId,
|
|
pr.customergroup_id AS customerGroupId,
|
|
MIN(pr.net) AS netPrice
|
|
FROM price pr
|
|
INNER JOIN article a ON a.id = pr.article_id
|
|
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = a.company_id
|
|
WHERE pr.minmultiplier = 1
|
|
GROUP BY pr.article_id, pr.customergroup_id
|
|
`;
|
|
|
|
export const LIST_POS_MANDATED_OPTIONS = `
|
|
SELECT
|
|
a.id AS articleId,
|
|
ola.count,
|
|
COALESCE(opt_at.name, opt_a.sku) AS depositName,
|
|
opt_pr.net AS depositNetPrice,
|
|
COALESCE(opt_ptax.rate, opt_ctax.rate, 0) AS depositTaxRate
|
|
FROM article a
|
|
INNER JOIN optionslistarticle ola ON ola.optionslist_id = a.optionslist_id AND ola.mandated = 1
|
|
INNER JOIN article opt_a ON opt_a.id = ola.article_id
|
|
INNER JOIN company co ON co.id = a.company_id
|
|
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = a.company_id
|
|
LEFT JOIN articletext opt_at ON opt_at.article_id = opt_a.id AND opt_at.language_id = co.default_language_id
|
|
LEFT JOIN price opt_pr ON opt_pr.article_id = opt_a.id AND opt_pr.customergroup_id = co.poscustomergroup_id AND opt_pr.minmultiplier = 1
|
|
LEFT JOIN tax opt_ptax ON opt_ptax.id = opt_pr.tax_id
|
|
LEFT JOIN tax opt_ctax ON opt_ctax.id = co.taxrate
|
|
ORDER BY ola.sort ASC, ola.id ASC
|
|
`;
|
|
|
|
export const LIST_POS_PARENT_OPTIONS = `
|
|
SELECT
|
|
fa.id AS parentArticleId,
|
|
COALESCE(fan.name, 'Variante') AS attributeName,
|
|
av.value AS variantValue
|
|
FROM article fa
|
|
INNER JOIN posterminal pt ON pt.id = @posterminalId AND pt.company_id = fa.company_id
|
|
INNER JOIN article child ON child.parentarticle_id = fa.id
|
|
LEFT JOIN attributename fan ON fan.id = fa.parentattributename_id
|
|
LEFT JOIN articleattributevalue aav ON aav.article_id = child.id
|
|
LEFT JOIN attributevalue av ON av.id = aav.attributevalue_id AND (fa.parentattributename_id IS NULL OR av.attribute_id = fa.parentattributename_id)
|
|
WHERE fa.is_parent = 1
|
|
ORDER BY fa.id ASC, child.parentsort ASC, child.id ASC
|
|
`;
|
|
|
|
export const RECORD_OBITUARY = `
|
|
INSERT INTO posterminalobituary (posterminal_id, entity_type, entity_id)
|
|
SELECT pt.id, @entityType, @entityId
|
|
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
|
|
WHERE posterminal_id = @posterminalId AND id > @cursor
|
|
`;
|
|
|
|
export const LIST_DELETED_ENTITIES = `
|
|
SELECT TOP (@limit)
|
|
id,
|
|
entity_type AS entityType,
|
|
entity_id AS entityId
|
|
FROM posterminalobituary
|
|
WHERE posterminal_id = @posterminalId AND id > @cursor
|
|
ORDER BY id ASC
|
|
`;
|
|
|