u
This commit is contained in:
@@ -8,6 +8,7 @@ export const {
|
||||
FIND_BY_PAIRINGPIN, PAIR,
|
||||
FIND_BY_TOKEN, COUNT_PRODUCTS, COUNT_CATEGORIES, COUNT_CUSTOMERS,
|
||||
COUNT_CUSTOMER_GROUPS, COUNT_COMPOSITE_PRODUCTS, MAX_ORDER_ID,
|
||||
LIST_POS_PRODUCT_CATEGORIES, LIST_POS_CUSTOMER_GROUP_IDS, LIST_POS_PRODUCT_PRICES,
|
||||
} = queries;
|
||||
|
||||
export const SET_ON = `
|
||||
@@ -25,4 +26,157 @@ WITH RECURSIVE cats AS (
|
||||
INSERT INTO posterminalcategory (posterminal_id, category_id)
|
||||
SELECT @posterminalId, id FROM cats WHERE 1
|
||||
ON CONFLICT (posterminal_id, category_id) DO NOTHING
|
||||
`;
|
||||
`;
|
||||
|
||||
export const LIST_POS_CATEGORIES = `
|
||||
SELECT
|
||||
c.id,
|
||||
c.parent_id AS pid,
|
||||
c.sort,
|
||||
COALESCE(ct.name, '') AS name,
|
||||
co.root_category_id AS rootCategoryId,
|
||||
(
|
||||
SELECT 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
|
||||
LIMIT 1
|
||||
) AS imgHash,
|
||||
CAST(strftime('%s', c.updated_at) AS INTEGER) 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 CAST(strftime('%s', c.updated_at) AS INTEGER) > @cursor
|
||||
ORDER BY lastChanged ASC, c.id ASC
|
||||
LIMIT @limit
|
||||
`;
|
||||
|
||||
export const LIST_POS_PRODUCTS = `
|
||||
SELECT
|
||||
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,
|
||||
CAST(strftime('%s', a.updated_at) AS INTEGER) AS lastChanged,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT 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
|
||||
LIMIT 1
|
||||
),
|
||||
(
|
||||
SELECT p2.hash
|
||||
FROM articlepic ap2
|
||||
INNER JOIN pic p2 ON p2.id = ap2.pic_id
|
||||
WHERE a.fatherarticle_id IS NOT NULL AND ap2.article_id = a.fatherarticle_id
|
||||
ORDER BY ap2.sort ASC, ap2.id ASC
|
||||
LIMIT 1
|
||||
)
|
||||
) AS imgHash,
|
||||
(
|
||||
SELECT ae.ean
|
||||
FROM articleean ae
|
||||
WHERE ae.article_id = a.id
|
||||
ORDER BY ae.id ASC
|
||||
LIMIT 1
|
||||
) AS barcode,
|
||||
a.is_father AS isParent,
|
||||
a.fatherarticle_id AS parentArticleId,
|
||||
(
|
||||
SELECT fan.name || '|' || av.value || '|' || fan.sort || '|' || a.fathersort
|
||||
FROM article fa
|
||||
INNER JOIN attributename fan ON fan.id = fa.fatherattributename_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.fatherarticle_id IS NOT NULL AND fa.id = a.fatherarticle_id
|
||||
LIMIT 1
|
||||
) 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 CAST(MIN(COALESCE(comp_stock.stock, 0) / ala.count) AS INTEGER)
|
||||
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 CAST(strftime('%s', a.updated_at) AS INTEGER) > @cursor
|
||||
ORDER BY lastChanged ASC, a.id ASC
|
||||
LIMIT @limit
|
||||
`;
|
||||
|
||||
Reference in New Issue
Block a user