135 lines
2.6 KiB
JavaScript
135 lines
2.6 KiB
JavaScript
/**
|
|
* Shared SQL for typed manifest documents (quote / order).
|
|
* Delivery keeps its own SQL with supplierId naming.
|
|
*/
|
|
|
|
export const QUOTE_TYPE = 'quote';
|
|
export const ORDER_TYPE = 'order';
|
|
|
|
export const LIST_MANIFESTS = `
|
|
SELECT
|
|
m.id,
|
|
m.[number],
|
|
m.[type],
|
|
m.entity_id AS entityId,
|
|
m.external_number AS externalNumber,
|
|
m.[date]
|
|
FROM manifest m
|
|
WHERE m.company_id = @companyId
|
|
AND m.[type] = @type
|
|
ORDER BY m.[date] DESC, m.id DESC
|
|
`;
|
|
|
|
export const GET_MANIFEST_HEADER = `
|
|
SELECT
|
|
m.id,
|
|
m.[number],
|
|
m.[type],
|
|
m.entity_id AS entityId,
|
|
m.external_number AS externalNumber,
|
|
m.[date]
|
|
FROM manifest m
|
|
WHERE m.id = @id
|
|
AND m.company_id = @companyId
|
|
AND m.[type] = @type
|
|
`;
|
|
|
|
export const GET_MANIFEST_LINES = `
|
|
SELECT
|
|
ma.id,
|
|
ma.article_id AS articleId,
|
|
a.sku,
|
|
ma.customsku,
|
|
ma.customname,
|
|
ma.amount,
|
|
ma.net,
|
|
ma.rebate,
|
|
t.rate AS tax,
|
|
ma.comment
|
|
FROM manifestarticle ma
|
|
LEFT JOIN article a ON a.id = ma.article_id
|
|
INNER JOIN tax t ON t.id = ma.tax_id
|
|
WHERE ma.manifest_id = @manifestId
|
|
ORDER BY ma.id
|
|
`;
|
|
|
|
export const CREATE_MANIFEST = `
|
|
INSERT INTO manifest (company_id, [number], [type], entity_id, external_number, [date])
|
|
OUTPUT INSERTED.id
|
|
VALUES (@companyId, @number, @type, @entityId, @externalNumber, @date)
|
|
`;
|
|
|
|
export const UPDATE_MANIFEST = `
|
|
UPDATE manifest
|
|
SET
|
|
[number] = @number,
|
|
entity_id = @entityId,
|
|
external_number = @externalNumber,
|
|
[date] = @date
|
|
WHERE id = @id
|
|
AND company_id = @companyId
|
|
AND [type] = @type
|
|
`;
|
|
|
|
export const DELETE_MANIFEST = `
|
|
DELETE FROM manifest
|
|
WHERE id = @id
|
|
AND company_id = @companyId
|
|
AND [type] = @type
|
|
`;
|
|
|
|
export const DELETE_LINES_BY_MANIFEST = `
|
|
DELETE FROM manifestarticle
|
|
WHERE manifest_id = @manifestId
|
|
`;
|
|
|
|
export const CREATE_LINE = `
|
|
INSERT INTO manifestarticle (
|
|
manifest_id,
|
|
article_id,
|
|
customsku,
|
|
customname,
|
|
amount,
|
|
net,
|
|
comment,
|
|
rebate,
|
|
tax_id
|
|
)
|
|
OUTPUT INSERTED.id
|
|
VALUES (
|
|
@manifestId,
|
|
@articleId,
|
|
@customsku,
|
|
@customname,
|
|
@amount,
|
|
@net,
|
|
@comment,
|
|
@rebate,
|
|
@taxId
|
|
)
|
|
`;
|
|
|
|
/** Resolve SKU to article id if reachable via the company category tree. */
|
|
export const RESOLVE_ARTICLE_ID_BY_SKU = `
|
|
;WITH 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 TOP 1 a.id
|
|
FROM article a
|
|
WHERE a.sku = @sku
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM categoryarticle ca
|
|
INNER JOIN category_tree ct ON ct.id = ca.category_id
|
|
WHERE ca.article_id = a.id
|
|
)
|
|
`;
|