feat(schema): add articlesupplier table and manifestarticle custom sku/name fields

This commit is contained in:
seb
2026-09-14 22:43:03 +02:00
parent af416246b4
commit 2e16a22c99
9 changed files with 1708 additions and 1517 deletions

View File

@@ -528,6 +528,27 @@ BEGIN
END
`;
export const INIT_ARTICLE_SUPPLIER = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'articlesupplier')
BEGIN
CREATE TABLE articlesupplier (
id INT IDENTITY(1,1) PRIMARY KEY,
article_id INT NOT NULL,
entity_id INT NOT NULL,
price_id INT NULL,
SupplierSKU NVARCHAR(100) NULL,
SupplierStock 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,
CONSTRAINT FK_articlesupplier_price FOREIGN KEY (price_id) REFERENCES price(id) ON DELETE NO ACTION
);
CREATE INDEX IX_articlesupplier_article_entity
ON articlesupplier (article_id, entity_id);
END
`;
export const INIT_MANIFEST = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'manifest')
BEGIN
@@ -552,6 +573,8 @@ BEGIN
id INT IDENTITY(1,1) PRIMARY KEY,
manifest_id INT NOT NULL,
article_id INT NULL,
customsku NVARCHAR(100) NULL,
customname NVARCHAR(255) NULL,
amount FLOAT NOT NULL,
net FLOAT NOT NULL,
comment NVARCHAR(MAX) NULL,
@@ -559,9 +582,41 @@ BEGIN
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 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)
)
);
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 = `
@@ -773,6 +828,7 @@ export const SCHEMA_SCRIPTS = [
INIT_CATEGORYTEXT,
INIT_ARTICLENAME,
INIT_PRICE,
INIT_ARTICLE_SUPPLIER,
INIT_MANIFEST,
INIT_MANIFESTARTICLE,
INIT_LEDGER,

View File

@@ -33,6 +33,8 @@ SELECT
ma.id,
ma.article_id AS articleId,
a.sku,
ma.customsku,
ma.customname,
ma.amount,
ma.net,
ma.rebate,
@@ -79,6 +81,8 @@ export const CREATE_LINE = `
INSERT INTO manifestarticle (
manifest_id,
article_id,
customsku,
customname,
amount,
net,
comment,
@@ -89,6 +93,8 @@ OUTPUT INSERTED.id
VALUES (
@manifestId,
@articleId,
@customsku,
@customname,
@amount,
@net,
@comment,

View File

@@ -39,6 +39,8 @@ SELECT
ma.id,
ma.article_id AS articleId,
a.sku,
ma.customsku,
ma.customname,
ma.amount,
ma.net,
ma.rebate,
@@ -85,6 +87,8 @@ export const CREATE_LINE = `
INSERT INTO manifestarticle (
manifest_id,
article_id,
customsku,
customname,
amount,
net,
comment,
@@ -95,6 +99,8 @@ OUTPUT INSERTED.id
VALUES (
@manifestId,
@articleId,
@customsku,
@customname,
@amount,
@net,
@comment,

View File

@@ -7,6 +7,7 @@ export const TABLES = [
'posterminal',
'numbers',
'ledger',
'articlesupplier',
'manifestarticle',
'manifest',
'price',

View File

@@ -5,7 +5,7 @@ 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, amount: number, net: number, rebate: number, tax: number, comment: string|null}>} The normalized lines.
* @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.
* @throws {Error} If the lines are invalid.
*/
export function normalizeLines(lines) {
@@ -15,7 +15,16 @@ export function normalizeLines(lines) {
return lines.map((entry) => {
const sku = entry?.sku?.trim?.() ? entry.sku.trim() : null;
if (!sku) {
let customsku = entry?.customsku?.trim?.() ? entry.customsku.trim() : null;
let 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.
throw new Error('Invalid lines');
}
@@ -38,7 +47,7 @@ export function normalizeLines(lines) {
throw new Error('Invalid lines');
}
return { sku, amount, net, rebate, tax, comment };
return { sku, customsku, customname, amount, net, rebate, tax, comment };
});
}
@@ -72,6 +81,8 @@ export function shapeLine(row) {
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,
rebate: row.rebate,
@@ -112,17 +123,16 @@ export async function insertLines(
{ manifestId, rootCategoryId, lines, ensureTax },
) {
for (const line of lines) {
const articleId = await resolveArticleId(
pool,
tx,
rootCategoryId,
line.sku,
);
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('amount', line.amount)
.input('net', line.net)
.input('comment', line.comment)