This commit is contained in:
seb
2026-10-21 03:31:52 +02:00
parent e3761bc48b
commit e9e98d041c
14 changed files with 72 additions and 35 deletions

View File

@@ -33,7 +33,7 @@ export default function create(provider) {
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - The shipment input.
* @param {number} input.orderId - The parent order manifest id.
* @param {Array<{sku: string, amount?: number, name?: string, pu?: number, net?: number, rebate?: number, taxrate?: number, comment?: string}>} input.lines - The shipment positions.
* @param {Array<{sku?: string, orderLineId?: number, amount?: number, name?: string, pu?: number, net?: number, rebate?: number, taxrate?: number, comment?: string}>} input.lines - The shipment positions. Order lines with a SKU are referenced by `sku`; SKU-less order lines by `orderLineId` (matched across shipments by name).
* @param {string|Date} [input.date] - Optional shipment date (defaults to current date).
* @param {'new'|'delivered'|'returned'} [input.state='new'] - Optional delivery state (defaults to 'new').
* @param {string} [input.externalNumber] - Optional external delivery note number (defaults to order.externalNumber or order.number).
@@ -82,15 +82,21 @@ export default function create(provider) {
throw new Error('Order has no lines');
}
// Lines are keyed by SKU; SKU-less lines are keyed by name (shipment lines copy the name).
const lineKey = (sku, name) => {
const s = String(sku ?? '').trim();
return s || `\u0000name:${String(name ?? '').trim()}`;
};
const orderAmountBySku = new Map();
const orderLineBySku = new Map();
const orderLineById = new Map();
for (const ol of orderLines) {
const sku = String(ol.sku ?? '').trim();
if (!sku) continue;
orderAmountBySku.set(sku, (orderAmountBySku.get(sku) || 0) + Number(ol.amount || 0));
if (!orderLineBySku.has(sku)) {
orderLineBySku.set(sku, ol);
const key = lineKey(ol.sku, ol.name);
orderLineById.set(Number(ol.id), ol);
orderAmountBySku.set(key, (orderAmountBySku.get(key) || 0) + Number(ol.amount || 0));
if (!orderLineBySku.has(key)) {
orderLineBySku.set(key, ol);
}
}
@@ -107,8 +113,8 @@ export default function create(provider) {
});
const shippedAmountBySku = new Map();
for (const row of shippedRes.rows ?? []) {
const sku = String(row.sku ?? '').trim();
shippedAmountBySku.set(sku, Number(row.shippedAmount || 0));
const key = lineKey(row.sku, row.name);
shippedAmountBySku.set(key, (shippedAmountBySku.get(key) || 0) + Number(row.shippedAmount || 0));
}
// 5. Determine warehouse and load stock for tracked articles
@@ -124,6 +130,7 @@ export default function create(provider) {
const remainingStockBySku = new Map();
for (const sku of orderAmountBySku.keys()) {
if (sku.startsWith('\u0000name:')) continue; // SKU-less lines carry no stock
const artRes = await runner.query(q.GET_ARTICLE_TRACK_STOCK, { companyId, sku });
const art = artRes.rows[0];
const isTracked = Number(art?.track_stock ?? 0) === 1;
@@ -152,9 +159,25 @@ export default function create(provider) {
const proposedBySku = new Map();
for (const line of input.lines) {
const sku = String(line?.sku ?? '').trim();
if (!sku || !orderAmountBySku.has(sku)) {
throw new Error(`Article (${sku}) is not part of order (${order.number})`);
const rawSku = String(line?.sku ?? '').trim();
let sku = rawSku;
let shipSku = rawSku;
let label = rawSku;
if (!rawSku) {
// SKU-less line: referenced by order line id
const target = orderLineById.get(Number(line?.orderLineId));
if (!target || String(target.sku ?? '').trim()) {
throw new Error(`Article (${line?.orderLineId ?? ''}) is not part of order (${order.number})`);
}
sku = lineKey(null, target.name);
shipSku = null;
label = String(target.name ?? '').trim();
}
if (!orderAmountBySku.has(sku)) {
throw new Error(`Article (${label}) is not part of order (${order.number})`);
}
if (Number(orderLineBySku.get(sku)?.shippable) === 0) {
throw new Error(`Article (${label}) is not shippable`);
}
const ordered = orderAmountBySku.get(sku) || 0;
@@ -170,14 +193,14 @@ export default function create(provider) {
} else {
// Default to remaining quantity
if (remaining <= 0) {
throw new Error(`Shipment amount exceeds ordered amount for article (${sku})`);
throw new Error(`Shipment amount exceeds ordered amount for article (${label})`);
}
amount = remaining;
}
const currentProposed = proposedBySku.get(sku) || 0;
if (amount > 0 && alreadyShipped + currentProposed + amount > ordered) {
throw new Error(`Shipment amount exceeds ordered amount for article (${sku})`);
throw new Error(`Shipment amount exceeds ordered amount for article (${label})`);
}
const info = articleInfoBySku.get(sku);
@@ -193,14 +216,15 @@ export default function create(provider) {
const ol = orderLineBySku.get(sku);
linesToInsert.push({
sku,
name: line.name ?? ol?.name ?? sku,
sku: shipSku || null,
name: shipSku ? (line.name ?? ol?.name ?? shipSku) : ol.name,
pu: line.pu !== undefined ? line.pu : (ol?.pu ?? 1),
amount,
net: line.net !== undefined ? Number(line.net) : Number(ol?.net ?? 0),
rebate: line.rebate !== undefined ? Number(line.rebate) : Number(ol?.rebate ?? 0),
taxrate: line.taxrate !== undefined ? Number(line.taxrate) : Number(ol?.taxrate ?? 0),
comment: line.comment !== undefined ? (line.comment ? String(line.comment) : null) : (ol?.comment ?? null),
shippable: true,
});
}