Files
dashlibstorage/src/providers/sqlite3/queries/posterminal.js
2026-10-23 00:47:38 +02:00

279 lines
9.7 KiB
JavaScript

import * as source from '../../mssql/queries/posterminal.js';
import { translateQueries } from './translate.js';
const queries = translateQueries(source, 'posterminal');
export const {
CREATE, DELETE_BY_COMPANY_AND_ID, GET_BY_ID, LIST_BY_COMPANY,
LIST_CATEGORY_IDS, POSTERMINAL_IDS_BY_COMPANY, SET_OFF, SET_OFF_ALL, UPDATE,
FIND_BY_PAIRINGPIN, PAIR, SET_MAX_KNOWN_OBITUARY_ID,
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,
LIST_POS_MANDATED_OPTIONS,
TOUCH_LASTSEEN, RECORD_OBITUARY, COUNT_DELETED_ENTITIES,
} = queries;
export const SET_ON = `
INSERT INTO posterminalcategory (posterminal_id, category_id)
VALUES (@posterminalId, @categoryId)
ON CONFLICT (posterminal_id, category_id) DO NOTHING
`;
export const SET_ON_ALL = `
WITH RECURSIVE 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, id FROM cats WHERE 1
ON CONFLICT (posterminal_id, category_id) DO NOTHING
`;
export const LIST_POS_CUSTOMER_GROUPS = `
SELECT
cg.id,
cg.name,
CASE WHEN cg.id = co.poscustomergroup_id THEN '1' ELSE '0' END AS standard,
'0.000000' AS discountPercent,
CAST(strftime('%s', cg.updated_at) AS INTEGER) AS lastChanged
FROM customergroup cg
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 CAST(strftime('%s', cg.updated_at) AS INTEGER) > @cursor
ORDER BY lastChanged ASC, cg.id ASC
LIMIT @limit
`;
export const LIST_POS_CUSTOMERS = `
SELECT
e.id,
e.customergroup_id AS customergroupId,
e.first_name AS firstName,
e.name AS lastName,
e."number" AS customerNumber,
(
SELECT 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
LIMIT 1
) AS street,
(
SELECT 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
LIMIT 1
) AS streetNumber,
(
SELECT 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
LIMIT 1
) AS city,
(
SELECT 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
LIMIT 1
) AS postalCode,
(
SELECT 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
LIMIT 1
) AS "state",
(
SELECT 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
LIMIT 1
) AS phone,
(
SELECT 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
LIMIT 1
) AS email,
CAST(strftime('%s', e.updated_at) AS INTEGER) AS lastChanged
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 CAST(strftime('%s', e.updated_at) AS INTEGER) > @cursor
ORDER BY lastChanged ASC, e.id ASC
LIMIT @limit
`;
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.parentarticle_id IS NOT NULL AND ap2.article_id = a.parentarticle_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_parent AS isParent,
a.parentarticle_id AS parentArticleId,
(
SELECT 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
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
`;
export const LIST_DELETED_ENTITIES = `
SELECT
id,
entity_type AS entityType,
entity_id AS entityId
FROM posterminalobituary
WHERE posterminal_id = @posterminalId AND id > @cursor
ORDER BY id ASC
LIMIT @limit
`;