feat(schema): rename manifestarticle customname to name, add pu, and use taxrate

This commit is contained in:
seb
2026-09-16 10:05:24 +02:00
parent 63f83b6822
commit 7b3d2141fa
7 changed files with 1566 additions and 1550 deletions

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|null, customname: string|null, amount: number, net: number, rebate: number, tax: number, comment: string|null}>} The normalized lines.
* @returns {Array<{sku: string|null, name: string|null, pu: number, 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,25 +15,35 @@ export function normalizeLines(lines) {
return lines.map((entry) => {
const sku = entry?.sku?.trim?.() ? entry.sku.trim() : null;
const customname =
entry?.customname?.trim?.() ? entry.customname.trim() : null;
const name =
entry?.name?.trim?.()
? entry.name.trim()
: entry?.customname?.trim?.()
? entry.customname.trim()
: null;
// Must have either an SKU or a custom name.
if (!sku && !customname) {
// Must have either an SKU or a name.
if (!sku && !name) {
throw new Error('Invalid lines');
}
const pu =
entry?.pu === undefined || entry?.pu === null ? 1 : entry.pu;
const amount = entry?.amount;
const net = entry?.net;
const rebate =
entry?.rebate === undefined || entry?.rebate === null ? 0 : entry.rebate;
const tax = entry?.tax;
const tax =
entry?.taxrate !== undefined && entry?.taxrate !== null
? entry.taxrate
: entry?.tax;
const comment =
entry?.comment === undefined || entry?.comment === null
? null
: String(entry.comment);
if (
!Number.isFinite(pu) ||
!Number.isFinite(amount) ||
!Number.isFinite(net) ||
!Number.isFinite(rebate) ||
@@ -42,7 +52,7 @@ export function normalizeLines(lines) {
throw new Error('Invalid lines');
}
return { sku, customname, amount, net, rebate, tax, comment };
return { sku, name, pu, amount, net, rebate, tax, comment };
});
}
@@ -72,28 +82,31 @@ export function parseDate(value) {
* @returns {object} The shaped line.
*/
export function shapeLine(row) {
const tax = row.taxrate !== undefined ? Number(row.taxrate) : Number(row.tax);
return {
id: row.id,
sku: row.sku ?? null,
customname: row.customname ?? null,
name: row.name ?? null,
pu: row.pu !== undefined && row.pu !== null ? Number(row.pu) : 1,
amount: row.amount,
net: row.net,
rebate: row.rebate,
tax: row.tax,
tax,
taxrate: tax,
comment: row.comment ?? null,
};
}
/**
* Inserts manifest lines for a document, storing each SKU directly
* and ensuring the tax rate.
* Inserts manifest lines for a document, storing each SKU and taxrate directly,
* and ensuring the tax rate exists in the tax catalog.
*
* @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 {Array<object>} params.lines - The normalized lines.
* @param {Function} params.ensureTax - The tax ensure function.
* @param {Function} [params.ensureTax] - The tax ensure function.
* @returns {Promise<void>}
*/
export async function insertLines(
@@ -102,17 +115,20 @@ export async function insertLines(
{ manifestId, lines, ensureTax },
) {
for (const line of lines) {
const taxId = await ensureTax(line.tax, tx);
if (ensureTax) {
await ensureTax(line.tax, tx);
}
await request(pool, tx)
.input('manifestId', manifestId)
.input('sku', line.sku ?? null)
.input('customname', line.customname ?? null)
.input('name', line.name ?? null)
.input('pu', line.pu ?? 1)
.input('amount', line.amount)
.input('net', line.net)
.input('comment', line.comment)
.input('rebate', line.rebate)
.input('taxId', taxId)
.input('taxrate', line.tax)
.query(CREATE_LINE);
}
}