274 lines
12 KiB
JavaScript
274 lines
12 KiB
JavaScript
import * as source from '../../mssql/queries/article.js';
|
|
import { translateQueries } from './translate.js';
|
|
|
|
const queries = translateQueries(source, 'article');
|
|
export const {
|
|
ADD_CATEGORY, ADD_EAN, ARTICLE_LIST_COLUMNS, CREATE, CREATE_CHILD,
|
|
DELETE_CATEGORIES_BY_ARTICLE, DELETE_EANS_BY_ARTICLE, EAN_EXISTS,
|
|
GET_BRAND_BY_COMPANY_AND_ID,
|
|
GET_CATEGORY_ID_BY_SKU, GET_CATEGORY_TREE_FLAT, GET_ID_BY_SKU,
|
|
LIST_CHILDREN_BY_PARENT_ID,
|
|
SET_PARENT_SORT,
|
|
SET_IS_PARENT, TOGGLE_PARENT, UNLINK_CHILD, UPDATE_BRAND, UPDATE_PARENT_BY_SKU, UPDATE_TRACK_STOCK, UPDATE_UNIT,
|
|
UPDATE_UNITAMOUNT, UPDATE_UNIT_AND_AMOUNT, UPDATE_WEIGHT,
|
|
} = queries;
|
|
|
|
export const ARTICLE_LIST_JOINS = `
|
|
LEFT JOIN articletext at
|
|
ON at.article_id = a.id AND at.language_id = @languageId
|
|
LEFT JOIN price pr
|
|
ON pr.article_id = a.id
|
|
AND pr.customergroup_id = @posCustomerGroupId
|
|
AND pr.minmultiplier = 1
|
|
LEFT JOIN tax t ON t.id = pr.tax_id
|
|
LEFT JOIN pic ON pic.id = (
|
|
SELECT ap.pic_id FROM articlepic ap
|
|
WHERE ap.article_id = a.id ORDER BY ap.sort, ap.id LIMIT 1
|
|
)
|
|
LEFT JOIN brand b ON b.id = a.brand_id
|
|
LEFT JOIN pic brand_pic ON brand_pic.id = (
|
|
SELECT bp.pic_id FROM brandpic bp
|
|
WHERE bp.brand_id = b.id ORDER BY bp.sort, bp.id LIMIT 1
|
|
)
|
|
LEFT JOIN article parent ON parent.id = a.parentarticle_id
|
|
`;
|
|
|
|
export const RESOLVE_ARTICLE_ACCESS_BY_SKU = `
|
|
SELECT
|
|
a.id AS articleId,
|
|
ca.category_id AS categoryId,
|
|
co.root_category_id AS rootCategoryId,
|
|
co.id AS companyId,
|
|
co.default_language_id AS languageId,
|
|
co.poscustomergroup_id AS posCustomerGroupId,
|
|
cu.id AS companyuserId
|
|
FROM article a
|
|
INNER JOIN company co ON co.id = a.company_id
|
|
INNER JOIN google_auth ga ON ga.id = @googleAuthId
|
|
INNER JOIN companyuser cu
|
|
ON cu.company_id = a.company_id AND cu.google_auth_id = @googleAuthId
|
|
LEFT JOIN categoryarticle ca ON ca.article_id = a.id
|
|
WHERE a.sku = @sku
|
|
AND (ga.selected_company IS NULL OR a.company_id = ga.selected_company)
|
|
LIMIT 1
|
|
`;
|
|
|
|
const JSON_EANS = `(SELECT COALESCE(json_group_array(json_object('ean', ean)), '[]')
|
|
FROM (SELECT ean FROM articleean WHERE article_id = a.id ORDER BY ean))`;
|
|
const JSON_CATEGORIES = `(SELECT COALESCE(json_group_array(json_object('category_id', category_id)), '[]')
|
|
FROM (SELECT category_id FROM categoryarticle WHERE article_id = a.id ORDER BY category_id))`;
|
|
|
|
export const SEARCH_ARTICLES = `
|
|
WITH RECURSIVE category_tree AS (
|
|
SELECT id FROM category WHERE id = @rootCategoryId
|
|
UNION ALL SELECT c.id FROM category c
|
|
INNER JOIN category_tree ct ON c.parent_id = ct.id
|
|
)
|
|
SELECT a.id, a.sku, at.name,
|
|
${JSON_EANS} AS eansJson,
|
|
${JSON_CATEGORIES} AS categoryIdsJson
|
|
FROM article a
|
|
LEFT JOIN articletext at ON at.article_id = a.id AND at.language_id = @languageId
|
|
WHERE EXISTS (
|
|
SELECT 1 FROM categoryarticle ca
|
|
INNER JOIN category_tree ct ON ct.id = ca.category_id
|
|
WHERE ca.article_id = a.id
|
|
)
|
|
ORDER BY a.id
|
|
`;
|
|
|
|
export const GET_ARTICLE_EXPORT = `
|
|
WITH RECURSIVE category_tree AS (
|
|
SELECT id FROM category WHERE id = @categoryId
|
|
UNION ALL SELECT c.id FROM category c
|
|
INNER JOIN category_tree ct ON c.parent_id = ct.id
|
|
)
|
|
SELECT DISTINCT a.id, a.sku, at.name, at.slug, u.si AS unitSi,
|
|
ut.symbol AS unitSymbol, a.unitamount, a.track_stock, b.name AS brandName,
|
|
pr.net, t.rate AS taxRate,
|
|
${JSON_EANS} AS eansJson,
|
|
${JSON_CATEGORIES} AS categoryIdsJson,
|
|
(SELECT COALESCE(json_group_array(json_object('hash', hash)), '[]')
|
|
FROM (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, ap.id)) AS fullHashesJson
|
|
FROM article a
|
|
INNER JOIN categoryarticle ca ON ca.article_id = a.id
|
|
INNER JOIN category_tree ct ON ct.id = ca.category_id
|
|
LEFT JOIN articletext at ON at.article_id = a.id AND at.language_id = @languageId
|
|
LEFT JOIN unit u ON u.id = a.unit_id
|
|
LEFT JOIN unittext ut ON ut.unit_id = u.id AND ut.language_id = @languageId
|
|
LEFT JOIN brand b ON b.id = a.brand_id
|
|
LEFT JOIN price pr ON pr.article_id = a.id
|
|
AND pr.customergroup_id = @posCustomerGroupId AND pr.minmultiplier = 1
|
|
LEFT JOIN tax t ON t.id = pr.tax_id
|
|
ORDER BY a.id
|
|
`;
|
|
|
|
export const GET_ARTICLE_BY_SKU_FULL = `
|
|
WITH auth AS (
|
|
SELECT a.id AS articleId, ca.category_id AS categoryId, co.root_category_id AS rootCategoryId,
|
|
co.id AS companyId, co.default_language_id AS languageId,
|
|
co.poscustomergroup_id AS posCustomerGroupId, cu.id AS companyuserId
|
|
FROM article a
|
|
INNER JOIN company co ON co.id = a.company_id
|
|
INNER JOIN google_auth ga ON ga.id = @googleAuthId
|
|
INNER JOIN companyuser cu
|
|
ON cu.company_id = a.company_id AND cu.google_auth_id = @googleAuthId
|
|
LEFT JOIN categoryarticle ca ON ca.article_id = a.id
|
|
WHERE a.sku = @sku
|
|
AND (ga.selected_company IS NULL OR a.company_id = ga.selected_company)
|
|
LIMIT 1
|
|
)
|
|
SELECT auth.*,
|
|
json_object(
|
|
'id', a.id, 'sku', a.sku, 'name', at.name, 'slug', at.slug,
|
|
'unitamount', a.unitamount, 'weight', a.weight,
|
|
'track_stock', a.track_stock,
|
|
'unitSi', u.si, 'unitBasesi', u.basesi,
|
|
'unitBasesifactor', u.basesifactor, 'unitSymbol', ut.symbol,
|
|
'unitName', ut.name,
|
|
'brandId', b.id, 'brandName', b.name,
|
|
'brandFullHash', bp.hash,
|
|
'brandThumbW', bp.thumbw, 'brandThumbH', bp.thumbh,
|
|
'brandThumbHash', bp.thumbhash,
|
|
'optionsCount', (SELECT COUNT(*) FROM optionslistarticle WHERE optionslist_id = a.optionslist_id),
|
|
'optionslistId', a.optionslist_id,
|
|
'optionslistItemsJson', json((SELECT COALESCE(json_group_array(json_object(
|
|
'id', ola.id,
|
|
'articleId', ola.article_id,
|
|
'sku', a_opt.sku,
|
|
'name', at_opt.name,
|
|
'count', ola.count,
|
|
'sort', ola.sort,
|
|
'mandated', ola.mandated
|
|
)), '[]') FROM optionslistarticle ola
|
|
INNER JOIN article a_opt ON a_opt.id = ola.article_id
|
|
LEFT JOIN articletext at_opt ON at_opt.article_id = a_opt.id AND at_opt.language_id = auth.languageId
|
|
WHERE ola.optionslist_id = a.optionslist_id
|
|
ORDER BY ola.sort ASC, ola.id ASC)),
|
|
'articleListCount', (SELECT COUNT(*) FROM articlelistarticle WHERE articlelist_id = a.articlelist_id),
|
|
'articlelistId', a.articlelist_id,
|
|
'articlelistItemsJson', json((SELECT COALESCE(json_group_array(json_object(
|
|
'id', ala.id,
|
|
'articleId', ala.article_id,
|
|
'sku', a_item.sku,
|
|
'name', at_item.name,
|
|
'count', ala.count,
|
|
'sort', ala.sort
|
|
)), '[]') FROM articlelistarticle ala
|
|
INNER JOIN article a_item ON a_item.id = ala.article_id
|
|
LEFT JOIN articletext at_item ON at_item.article_id = a_item.id AND at_item.language_id = auth.languageId
|
|
WHERE ala.articlelist_id = a.articlelist_id
|
|
ORDER BY ala.sort ASC, ala.id ASC)),
|
|
'childCount', (SELECT COUNT(*) FROM article WHERE parentarticle_id = a.id),
|
|
'isParent', a.is_parent,
|
|
'parentAttributeNameId', a.parentattributename_id,
|
|
'parentAttributeName', fan.name,
|
|
'parentAttributeExclusive', fan.exclusive,
|
|
'parentArticleId', a.parentarticle_id, 'parentSku', parent.sku,
|
|
'childrenJson', json((SELECT COALESCE(json_group_array(json_object(
|
|
'id', child.id,
|
|
'sku', child.sku,
|
|
'parentsort', child.parentsort,
|
|
'name', at_child.name,
|
|
'attributeValueId', av.id,
|
|
'attributeValue', av.value
|
|
)), '[]') FROM article child
|
|
LEFT JOIN articletext at_child ON at_child.article_id = child.id AND at_child.language_id = auth.languageId
|
|
LEFT JOIN (
|
|
articleattributevalue aav
|
|
INNER JOIN attributevalue av ON av.id = aav.attributevalue_id AND av.attribute_id = a.parentattributename_id
|
|
) ON aav.article_id = child.id
|
|
WHERE child.parentarticle_id = a.id
|
|
ORDER BY child.parentsort ASC, child.id ASC)),
|
|
'categoryIdsJson', json(${JSON_CATEGORIES}),
|
|
'photosJson', json((SELECT COALESCE(json_group_array(json_object(
|
|
'sort', sort, 'fullHash', hash, 'fullW', w, 'fullH', h,
|
|
'fullSize', size, 'thumbW', thumbw, 'thumbH', thumbh,
|
|
'thumbHash', thumbhash
|
|
)), '[]') FROM (
|
|
SELECT ap.sort, p.hash, p.w, p.h, p.size, p.thumbw, p.thumbh, p.thumbhash
|
|
FROM articlepic ap INNER JOIN pic p ON p.id = ap.pic_id
|
|
WHERE ap.article_id = a.id ORDER BY ap.sort, ap.id
|
|
))),
|
|
'eansJson', json(${JSON_EANS}),
|
|
'pricesJson', json((SELECT COALESCE(json_group_array(json_object(
|
|
'minmultiplier', minmultiplier, 'net', net, 'taxRate', rate,
|
|
'customergroupId', customergroup_id
|
|
)), '[]') FROM (
|
|
SELECT pr.minmultiplier, pr.net, t.rate, pr.customergroup_id
|
|
FROM price pr INNER JOIN tax t ON t.id = pr.tax_id
|
|
WHERE pr.article_id = a.id ORDER BY pr.customergroup_id, pr.minmultiplier
|
|
))),
|
|
'suppliersJson', json((SELECT COALESCE(json_group_array(json_object(
|
|
'id', id, 'articleId', article_id, 'entityId', entity_id,
|
|
'supplierName', supplier_name, 'sku', sku, 'pu', pu,
|
|
'stock', stock, 'sort', sort,
|
|
'price', CASE WHEN net IS NULL THEN NULL ELSE json_object(
|
|
'net', net, 'taxId', tax_id, 'taxRate', tax_rate
|
|
) END
|
|
)), '[]') FROM (
|
|
SELECT s.id, s.article_id, s.entity_id, e.name AS supplier_name,
|
|
s.sku, s.pu, s.stock, s.sort,
|
|
s.net, s.tax_id, t.rate AS tax_rate
|
|
FROM articlesupplier s INNER JOIN entity e ON e.id = s.entity_id
|
|
LEFT JOIN tax t ON t.id = s.tax_id
|
|
WHERE s.article_id = a.id ORDER BY s.sort, s.id
|
|
))),
|
|
'warehousesJson', json((SELECT COALESCE(json_group_array(json_object(
|
|
'id', w.id, 'name', w.name, 'sort', w.sort,
|
|
'stock', 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
|
|
AND l.warehouse_id = w.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(stock_calc.stock, 0)
|
|
END
|
|
)), '[]') FROM warehouse w
|
|
LEFT JOIN (
|
|
SELECT l.warehouse_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
|
|
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)
|
|
)
|
|
GROUP BY l.warehouse_id
|
|
) stock_calc ON stock_calc.warehouse_id = w.id
|
|
WHERE w.company_id = a.company_id
|
|
ORDER BY w.sort ASC, w.name ASC))
|
|
) AS articleJson
|
|
FROM auth
|
|
INNER JOIN article a ON a.id = auth.articleId
|
|
LEFT JOIN articletext at ON at.article_id = a.id AND at.language_id = auth.languageId
|
|
LEFT JOIN unit u ON u.id = a.unit_id
|
|
LEFT JOIN unittext ut ON ut.unit_id = u.id AND ut.language_id = auth.languageId
|
|
LEFT JOIN brand b ON b.id = a.brand_id
|
|
LEFT JOIN pic bp ON bp.id = (
|
|
SELECT pic_id FROM brandpic WHERE brand_id = b.id ORDER BY sort, id LIMIT 1
|
|
)
|
|
LEFT JOIN article parent ON parent.id = a.parentarticle_id
|
|
LEFT JOIN attributename fan ON fan.id = a.parentattributename_id
|
|
`; |