387 lines
13 KiB
JavaScript
387 lines
13 KiB
JavaScript
export const LIST_BY_COMPANY = `
|
|
SELECT
|
|
id,
|
|
name,
|
|
token,
|
|
pairingpin,
|
|
paired_at AS pairedAt,
|
|
clientserial AS clientserial,
|
|
maxknowntombstoneid AS maxknowntombstoneid
|
|
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,
|
|
maxknowntombstoneid AS maxknowntombstoneid
|
|
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,
|
|
maxknowntombstoneid AS maxknowntombstoneid
|
|
FROM posterminal
|
|
WHERE pairingpin = @pairingpin
|
|
`;
|
|
|
|
export const PAIR = `
|
|
UPDATE posterminal
|
|
SET token = @token, paired_at = @pairedAt, clientserial = @clientserial, pairingpin = ''
|
|
WHERE id = @id
|
|
`;
|
|
|
|
export const CREATE = `
|
|
INSERT INTO posterminal (company_id, name, token, pairingpin, paired_at, clientserial, maxknowntombstoneid)
|
|
OUTPUT INSERTED.id, INSERTED.name, INSERTED.token, INSERTED.pairingpin, INSERTED.paired_at AS pairedAt, INSERTED.clientserial AS clientserial, INSERTED.maxknowntombstoneid AS maxknowntombstoneid
|
|
VALUES (@companyId, @name, @token, @pairingpin, @pairedAt, @clientserial, @maxknowntombstoneid)
|
|
`;
|
|
|
|
export const UPDATE = `
|
|
UPDATE posterminal
|
|
SET name = @name, token = @token, pairingpin = @pairingpin, paired_at = @pairedAt, clientserial = @clientserial, maxknowntombstoneid = @maxknowntombstoneid
|
|
WHERE id = @id AND company_id = @companyId
|
|
`;
|
|
|
|
export const SET_MAX_KNOWN_TOMBSTONE_ID = `
|
|
UPDATE posterminal
|
|
SET maxknowntombstoneid = @maxknowntombstoneid
|
|
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: insert if not exists (single terminal + single category). */
|
|
export const SET_ON = `
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM posterminalcategory
|
|
WHERE posterminal_id = @posterminalId AND category_id = @categoryId
|
|
)
|
|
INSERT INTO posterminalcategory (posterminal_id, category_id)
|
|
VALUES (@posterminalId, @categoryId)
|
|
`;
|
|
|
|
/** setOff: remove a single category from a single terminal. */
|
|
export const SET_OFF = `
|
|
DELETE FROM posterminalcategory
|
|
WHERE posterminal_id = @posterminalId AND category_id = @categoryId
|
|
`;
|
|
|
|
/** 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,
|
|
maxknowntombstoneid AS maxknowntombstoneid
|
|
FROM posterminal
|
|
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) > @cursor
|
|
`;
|
|
|
|
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
|
|
`;
|
|
|
|
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 NOT IN ('Lieferanten', 'POS')
|
|
AND DATEDIFF_BIG(second, '1970-01-01', e.updated_at) > @cursor
|
|
`;
|
|
|
|
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
|
|
`;
|
|
|
|
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) > @cursor
|
|
`;
|
|
|
|
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,
|
|
DATEDIFF_BIG(second, '1970-01-01', c.updated_at) AS lastChanged
|
|
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
|
|
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
|
|
`;
|
|
|
|
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,
|
|
DATEDIFF_BIG(second, '1970-01-01', a.updated_at) 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,
|
|
(
|
|
SELECT TOP 1 CONCAT(fan.name, '|', av.value, '|', fan.sort, '|', a.parentsort)
|
|
FROM article fa
|
|
INNER JOIN attributename fan ON fan.id = fa.parentattributename_id
|
|
INNER JOIN articleattributevalue aav ON aav.article_id = a.id
|
|
INNER JOIN attributevalue av ON av.id = aav.attributevalue_id AND av.attribute_id = fan.id
|
|
WHERE a.parentarticle_id IS NOT NULL AND fa.id = a.parentarticle_id
|
|
) 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.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
|
|
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 (
|
|
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 article a
|
|
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
|
|
)
|
|
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
|
|
`;
|
|
|
|
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_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
|
|
`;
|