feat(schema): replace manifestarticle article_id FK with sku, rename articlesupplier sku/stock fields
This commit is contained in:
@@ -536,8 +536,8 @@ BEGIN
|
||||
article_id INT NOT NULL,
|
||||
entity_id INT NOT NULL,
|
||||
price_id INT NULL,
|
||||
supplierSKU NVARCHAR(100) NULL,
|
||||
supplierStock FLOAT NULL,
|
||||
sku NVARCHAR(100) NULL,
|
||||
stock FLOAT NULL,
|
||||
pu FLOAT NOT NULL CONSTRAINT DF_articlesupplier_pu DEFAULT 1,
|
||||
CONSTRAINT FK_articlesupplier_article FOREIGN KEY (article_id) REFERENCES article(id) ON DELETE CASCADE,
|
||||
CONSTRAINT FK_articlesupplier_entity FOREIGN KEY (entity_id) REFERENCES entity(id) ON DELETE CASCADE,
|
||||
@@ -572,8 +572,7 @@ BEGIN
|
||||
CREATE TABLE manifestarticle (
|
||||
id INT IDENTITY(1,1) PRIMARY KEY,
|
||||
manifest_id INT NOT NULL,
|
||||
article_id INT NULL,
|
||||
customsku NVARCHAR(100) NULL,
|
||||
sku NVARCHAR(100) NULL,
|
||||
customname NVARCHAR(255) NULL,
|
||||
amount FLOAT NOT NULL,
|
||||
net FLOAT NOT NULL,
|
||||
@@ -581,44 +580,12 @@ BEGIN
|
||||
rebate FLOAT NOT NULL CONSTRAINT DF_manifestarticle_rebate DEFAULT 0,
|
||||
tax_id INT NOT NULL,
|
||||
CONSTRAINT FK_manifestarticle_manifest FOREIGN KEY (manifest_id) REFERENCES manifest(id) ON DELETE CASCADE,
|
||||
CONSTRAINT FK_manifestarticle_article FOREIGN KEY (article_id) REFERENCES article(id) ON DELETE SET NULL,
|
||||
CONSTRAINT FK_manifestarticle_tax FOREIGN KEY (tax_id) REFERENCES tax(id) ON DELETE CASCADE,
|
||||
CONSTRAINT CK_manifestarticle_article_or_custom CHECK (
|
||||
(article_id IS NOT NULL AND customsku IS NULL AND customname IS NULL)
|
||||
OR (article_id IS NULL)
|
||||
)
|
||||
CONSTRAINT FK_manifestarticle_tax FOREIGN KEY (tax_id) REFERENCES tax(id) ON DELETE CASCADE
|
||||
);
|
||||
END
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM sys.columns
|
||||
WHERE object_id = OBJECT_ID(N'dbo.manifestarticle') AND name = N'customsku'
|
||||
)
|
||||
BEGIN
|
||||
EXEC('ALTER TABLE dbo.manifestarticle ADD customsku NVARCHAR(100) NULL');
|
||||
END
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM sys.columns
|
||||
WHERE object_id = OBJECT_ID(N'dbo.manifestarticle') AND name = N'customname'
|
||||
)
|
||||
BEGIN
|
||||
EXEC('ALTER TABLE dbo.manifestarticle ADD customname NVARCHAR(255) NULL');
|
||||
END
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM sys.check_constraints
|
||||
WHERE name = N'CK_manifestarticle_article_or_custom'
|
||||
)
|
||||
BEGIN
|
||||
EXEC('ALTER TABLE dbo.manifestarticle ADD CONSTRAINT CK_manifestarticle_article_or_custom
|
||||
CHECK (
|
||||
(article_id IS NOT NULL AND customsku IS NULL AND customname IS NULL)
|
||||
OR (article_id IS NULL)
|
||||
)');
|
||||
END
|
||||
`;
|
||||
|
||||
|
||||
export const INIT_LEDGER = `
|
||||
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'ledger')
|
||||
BEGIN
|
||||
|
||||
@@ -10,22 +10,6 @@ FROM article
|
||||
WHERE id = @articleId
|
||||
`;
|
||||
|
||||
export const GET_NAME_FOR_DELETE = `
|
||||
SELECT TOP 1 at.name
|
||||
FROM articletext at
|
||||
WHERE at.article_id = @articleId AND at.language_id = @languageId
|
||||
`;
|
||||
|
||||
export const DETACH_MANIFEST_ARTICLES = `
|
||||
UPDATE manifestarticle
|
||||
SET article_id = NULL,
|
||||
comment = CONCAT(
|
||||
ISNULL(comment, ''),
|
||||
CASE WHEN comment IS NULL OR comment = '' THEN '' ELSE ' ' END,
|
||||
@suffix
|
||||
)
|
||||
WHERE article_id = @articleId
|
||||
`;
|
||||
|
||||
export const DELETE_PICS_BY_ARTICLE = `
|
||||
DELETE p
|
||||
|
||||
@@ -31,9 +31,7 @@ WHERE m.id = @id
|
||||
export const GET_DELIVERY_LINES = `
|
||||
SELECT
|
||||
ma.id,
|
||||
ma.article_id AS articleId,
|
||||
a.sku,
|
||||
ma.customsku,
|
||||
ma.sku,
|
||||
ma.customname,
|
||||
ma.amount,
|
||||
ma.net,
|
||||
@@ -41,7 +39,6 @@ SELECT
|
||||
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
|
||||
@@ -80,8 +77,7 @@ WHERE manifest_id = @manifestId
|
||||
export const CREATE_LINE = `
|
||||
INSERT INTO manifestarticle (
|
||||
manifest_id,
|
||||
article_id,
|
||||
customsku,
|
||||
sku,
|
||||
customname,
|
||||
amount,
|
||||
net,
|
||||
@@ -92,8 +88,7 @@ INSERT INTO manifestarticle (
|
||||
OUTPUT INSERTED.id
|
||||
VALUES (
|
||||
@manifestId,
|
||||
@articleId,
|
||||
@customsku,
|
||||
@sku,
|
||||
@customname,
|
||||
@amount,
|
||||
@net,
|
||||
@@ -102,27 +97,3 @@ VALUES (
|
||||
@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
|
||||
)
|
||||
`;
|
||||
|
||||
@@ -37,9 +37,7 @@ WHERE m.id = @id
|
||||
export const GET_MANIFEST_LINES = `
|
||||
SELECT
|
||||
ma.id,
|
||||
ma.article_id AS articleId,
|
||||
a.sku,
|
||||
ma.customsku,
|
||||
ma.sku,
|
||||
ma.customname,
|
||||
ma.amount,
|
||||
ma.net,
|
||||
@@ -47,7 +45,6 @@ SELECT
|
||||
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
|
||||
@@ -86,8 +83,7 @@ WHERE manifest_id = @manifestId
|
||||
export const CREATE_LINE = `
|
||||
INSERT INTO manifestarticle (
|
||||
manifest_id,
|
||||
article_id,
|
||||
customsku,
|
||||
sku,
|
||||
customname,
|
||||
amount,
|
||||
net,
|
||||
@@ -98,8 +94,7 @@ INSERT INTO manifestarticle (
|
||||
OUTPUT INSERTED.id
|
||||
VALUES (
|
||||
@manifestId,
|
||||
@articleId,
|
||||
@customsku,
|
||||
@sku,
|
||||
@customname,
|
||||
@amount,
|
||||
@net,
|
||||
@@ -108,27 +103,3 @@ VALUES (
|
||||
@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
|
||||
)
|
||||
`;
|
||||
|
||||
@@ -3,10 +3,8 @@ import {
|
||||
DELETE_BY_ID,
|
||||
DELETE_OPTIONSLIST,
|
||||
DELETE_PICS_BY_ARTICLE,
|
||||
DETACH_MANIFEST_ARTICLES,
|
||||
GET_CHILD_COUNT,
|
||||
GET_LIST_IDS,
|
||||
GET_NAME_FOR_DELETE,
|
||||
} from '../../sql/articleDelete.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
import request from '../request.js';
|
||||
@@ -26,7 +24,7 @@ export default function del(pool) {
|
||||
* await store.article.delete(42, 'SKU-1');
|
||||
*/
|
||||
return async (googleAuthId, sku) => {
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku);
|
||||
const { articleId } = await resolve(googleAuthId, sku);
|
||||
|
||||
const childCount = await pool
|
||||
.request()
|
||||
@@ -37,15 +35,6 @@ export default function del(pool) {
|
||||
throw new Error('Article has children');
|
||||
}
|
||||
|
||||
const nameResult = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.input('languageId', languageId)
|
||||
.query(GET_NAME_FOR_DELETE);
|
||||
|
||||
const name = nameResult.recordset[0]?.name ?? sku;
|
||||
const suffix = `article ${name} deleted`;
|
||||
|
||||
const listIds = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
@@ -57,11 +46,6 @@ export default function del(pool) {
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('suffix', suffix)
|
||||
.query(DETACH_MANIFEST_ARTICLES);
|
||||
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.query(DELETE_PICS_BY_ARTICLE);
|
||||
|
||||
@@ -41,7 +41,7 @@ export default function create(pool) {
|
||||
|
||||
const lines = normalizeLines(body.lines ?? []);
|
||||
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -69,7 +69,6 @@ export default function create(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId,
|
||||
rootCategoryId,
|
||||
lines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
@@ -56,7 +56,7 @@ export default function update(pool) {
|
||||
normalizedLines = normalizeLines(patch.lines);
|
||||
}
|
||||
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -125,7 +125,6 @@ export default function update(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId: id,
|
||||
rootCategoryId,
|
||||
lines: normalizedLines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { CREATE_LINE, RESOLVE_ARTICLE_ID_BY_SKU } from '../sql/manifestDoc.js';
|
||||
import { CREATE_LINE } from '../sql/manifestDoc.js';
|
||||
import request from './request.js';
|
||||
|
||||
/**
|
||||
* Normalizes and validates an array of manifest line inputs.
|
||||
*
|
||||
* @param {Array<object>} lines - The raw line inputs.
|
||||
* @returns {Array<{sku: string|null, customsku: string|null, customname: string|null, amount: number, net: number, rebate: number, tax: number, comment: string|null}>} The normalized lines.
|
||||
* @returns {Array<{sku: string|null, customname: string|null, amount: number, net: number, rebate: number, tax: number, comment: string|null}>} The normalized lines.
|
||||
* @throws {Error} If the lines are invalid.
|
||||
*/
|
||||
export function normalizeLines(lines) {
|
||||
@@ -15,16 +15,11 @@ export function normalizeLines(lines) {
|
||||
|
||||
return lines.map((entry) => {
|
||||
const sku = entry?.sku?.trim?.() ? entry.sku.trim() : null;
|
||||
let customsku = entry?.customsku?.trim?.() ? entry.customsku.trim() : null;
|
||||
let customname =
|
||||
const customname =
|
||||
entry?.customname?.trim?.() ? entry.customname.trim() : null;
|
||||
|
||||
// If an article SKU is present, the custom fields must be null.
|
||||
if (sku) {
|
||||
customsku = null;
|
||||
customname = null;
|
||||
} else if (!customsku && !customname) {
|
||||
// Must have either a catalog SKU or at least one custom field.
|
||||
// Must have either an SKU or a custom name.
|
||||
if (!sku && !customname) {
|
||||
throw new Error('Invalid lines');
|
||||
}
|
||||
|
||||
@@ -47,7 +42,7 @@ export function normalizeLines(lines) {
|
||||
throw new Error('Invalid lines');
|
||||
}
|
||||
|
||||
return { sku, customsku, customname, amount, net, rebate, tax, comment };
|
||||
return { sku, customname, amount, net, rebate, tax, comment };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -79,9 +74,7 @@ export function parseDate(value) {
|
||||
export function shapeLine(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
articleId: row.articleId ?? null,
|
||||
sku: row.sku ?? null,
|
||||
customsku: row.customsku ?? null,
|
||||
customname: row.customname ?? null,
|
||||
amount: row.amount,
|
||||
net: row.net,
|
||||
@@ -91,28 +84,14 @@ export function shapeLine(row) {
|
||||
};
|
||||
}
|
||||
|
||||
async function resolveArticleId(pool, tx, rootCategoryId, sku) {
|
||||
const result = await request(pool, tx)
|
||||
.input('rootCategoryId', rootCategoryId)
|
||||
.input('sku', sku)
|
||||
.query(RESOLVE_ARTICLE_ID_BY_SKU);
|
||||
|
||||
const articleId = result.recordset[0]?.id;
|
||||
if (!articleId) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
return articleId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts manifest lines for a document, resolving each SKU to an article id
|
||||
* Inserts manifest lines for a document, storing each SKU directly
|
||||
* and ensuring the tax rate.
|
||||
*
|
||||
* @param {object} pool - The mssql connection pool.
|
||||
* @param {object} tx - The transaction.
|
||||
* @param {object} params - The insert parameters.
|
||||
* @param {number} params.manifestId - The manifest id.
|
||||
* @param {number} params.rootCategoryId - The company root category id.
|
||||
* @param {Array<object>} params.lines - The normalized lines.
|
||||
* @param {Function} params.ensureTax - The tax ensure function.
|
||||
* @returns {Promise<void>}
|
||||
@@ -120,19 +99,15 @@ async function resolveArticleId(pool, tx, rootCategoryId, sku) {
|
||||
export async function insertLines(
|
||||
pool,
|
||||
tx,
|
||||
{ manifestId, rootCategoryId, lines, ensureTax },
|
||||
{ manifestId, lines, ensureTax },
|
||||
) {
|
||||
for (const line of lines) {
|
||||
const articleId = line.sku
|
||||
? await resolveArticleId(pool, tx, rootCategoryId, line.sku)
|
||||
: null;
|
||||
const taxId = await ensureTax(line.tax, tx);
|
||||
|
||||
await request(pool, tx)
|
||||
.input('manifestId', manifestId)
|
||||
.input('articleId', articleId)
|
||||
.input('customsku', line.sku ? null : line.customsku ?? null)
|
||||
.input('customname', line.sku ? null : line.customname ?? null)
|
||||
.input('sku', line.sku ?? null)
|
||||
.input('customname', line.customname ?? null)
|
||||
.input('amount', line.amount)
|
||||
.input('net', line.net)
|
||||
.input('comment', line.comment)
|
||||
@@ -141,3 +116,4 @@ export async function insertLines(
|
||||
.query(CREATE_LINE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ export default function create(pool) {
|
||||
: null;
|
||||
|
||||
const lines = normalizeLines(body.lines ?? []);
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -63,7 +63,6 @@ export default function create(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId,
|
||||
rootCategoryId,
|
||||
lines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
@@ -52,7 +52,7 @@ export default function update(pool) {
|
||||
normalizedLines = normalizeLines(patch.lines);
|
||||
}
|
||||
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -121,7 +121,6 @@ export default function update(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId: id,
|
||||
rootCategoryId,
|
||||
lines: normalizedLines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
@@ -37,7 +37,7 @@ export default function create(pool) {
|
||||
: null;
|
||||
|
||||
const lines = normalizeLines(body.lines ?? []);
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -63,7 +63,6 @@ export default function create(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId,
|
||||
rootCategoryId,
|
||||
lines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
@@ -52,7 +52,7 @@ export default function update(pool) {
|
||||
normalizedLines = normalizeLines(patch.lines);
|
||||
}
|
||||
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
@@ -121,7 +121,6 @@ export default function update(pool) {
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId: id,
|
||||
rootCategoryId,
|
||||
lines: normalizedLines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user