u
This commit is contained in:
@@ -6,8 +6,10 @@ import makeRunner from '../runner.js';
|
||||
function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
manifestId: row.manifest_id,
|
||||
manifestId: row.manifest_id ?? row.manifestId,
|
||||
number: row.number,
|
||||
trackingCode: row.tracking_code ?? row.trackingCode ?? null,
|
||||
carrier: row.carrier ?? null,
|
||||
type: row.type,
|
||||
state: row.state,
|
||||
};
|
||||
@@ -33,10 +35,12 @@ export default function update(provider) {
|
||||
* @param {object} patch - Shipping patch fields.
|
||||
* @param {string} [patch.type] - Shipping type.
|
||||
* @param {string} [patch.state] - Shipping state.
|
||||
* @param {string} [patch.trackingCode] - Carrier tracking code.
|
||||
* @param {string} [patch.carrier] - Carrier name.
|
||||
* @param {number} [patch.warehouseId] - Warehouse ID if transitioning to 'shipped'.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, manifestId: number, number: string, type: string, state: string}>}
|
||||
* @throws {Error} If shipping is not found, cannot be undone, or order already shipped.
|
||||
* @returns {Promise<{id: number, manifestId: number, number: string, trackingCode: string|null, carrier: string|null, type: string, state: string}>}
|
||||
* @throws {Error} If shipping is not found, cannot be undone, or manifest not found.
|
||||
*/
|
||||
return async (googleAuthId, id, patch = {}, tx) => {
|
||||
const shippingId = requireId(id, 'Shipping');
|
||||
@@ -57,6 +61,8 @@ export default function update(provider) {
|
||||
String(current.state || '').trim().toLowerCase() === 'shipped';
|
||||
const hasState = Object.prototype.hasOwnProperty.call(patch, 'state');
|
||||
const hasType = Object.prototype.hasOwnProperty.call(patch, 'type');
|
||||
const hasTrackingCode = Object.prototype.hasOwnProperty.call(patch, 'trackingCode');
|
||||
const hasCarrier = Object.prototype.hasOwnProperty.call(patch, 'carrier');
|
||||
|
||||
if (isCurrentlyShipped) {
|
||||
if (
|
||||
@@ -73,6 +79,12 @@ export default function update(provider) {
|
||||
const nextState = hasState
|
||||
? requireText(patch.state, 'Shipping state')
|
||||
: current.state;
|
||||
const nextTrackingCode = hasTrackingCode
|
||||
? (patch.trackingCode ? String(patch.trackingCode).trim() : null)
|
||||
: (current.tracking_code ?? current.trackingCode ?? null);
|
||||
const nextCarrier = hasCarrier
|
||||
? (patch.carrier ? String(patch.carrier).trim() : null)
|
||||
: (current.carrier ?? null);
|
||||
const willBeShipped =
|
||||
!isCurrentlyShipped &&
|
||||
String(nextState).trim().toLowerCase() === 'shipped';
|
||||
@@ -82,86 +94,86 @@ export default function update(provider) {
|
||||
const shippedCheck = await runner.query(q.CHECK_ORDER_SHIPPED, {
|
||||
manifestId,
|
||||
});
|
||||
if (shippedCheck.rows[0]) {
|
||||
throw new Error('Order already shipped');
|
||||
}
|
||||
const alreadyShipped = Boolean(shippedCheck.rows[0]);
|
||||
|
||||
const manifestRes = await runner.query(q.GET_MANIFEST_HEADER_BY_ID, {
|
||||
manifestId,
|
||||
companyId,
|
||||
});
|
||||
const manifest = manifestRes.rows[0] ?? { number: String(manifestId) };
|
||||
|
||||
// Load lines & book ledger
|
||||
const linesRes = await runner.query(q.GET_ORDER_LINES_FOR_SHIPPING, {
|
||||
manifestId,
|
||||
});
|
||||
const lines = linesRes.rows ?? [];
|
||||
const trackedLines = [];
|
||||
const untrackedLines = [];
|
||||
for (const line of lines) {
|
||||
const sku = line.sku ? String(line.sku).trim() : '';
|
||||
const amount = Number(line.amount);
|
||||
if (!sku || !Number.isFinite(amount) || amount <= 0) continue;
|
||||
|
||||
const trackRes = await runner.query(q.CHECK_ARTICLE_TRACK_STOCK, {
|
||||
if (!alreadyShipped) {
|
||||
const manifestRes = await runner.query(q.GET_MANIFEST_HEADER_BY_ID, {
|
||||
manifestId,
|
||||
companyId,
|
||||
sku,
|
||||
});
|
||||
const isUntracked = (trackRes.rows ?? []).some(
|
||||
(r) => Number(r.track_stock ?? 1) === 0
|
||||
);
|
||||
if (isUntracked) {
|
||||
untrackedLines.push({ ...line, amount });
|
||||
} else if ((trackRes.rows ?? []).length > 0) {
|
||||
trackedLines.push({ ...line, amount });
|
||||
}
|
||||
}
|
||||
const manifest = manifestRes.rows[0] ?? { number: String(manifestId) };
|
||||
|
||||
const manifestNumber = manifest.number || String(manifestId);
|
||||
|
||||
let customerName = null;
|
||||
if (manifest.entityId) {
|
||||
const entRes = await runner.query('SELECT name FROM entity WHERE id = @entityId', {
|
||||
entityId: manifest.entityId,
|
||||
// Load lines & book ledger
|
||||
const linesRes = await runner.query(q.GET_ORDER_LINES_FOR_SHIPPING, {
|
||||
manifestId,
|
||||
});
|
||||
customerName = entRes.rows[0]?.name ?? null;
|
||||
}
|
||||
const lines = linesRes.rows ?? [];
|
||||
const trackedLines = [];
|
||||
const untrackedLines = [];
|
||||
for (const line of lines) {
|
||||
const sku = line.sku ? String(line.sku).trim() : '';
|
||||
const amount = Number(line.amount);
|
||||
if (!sku || !Number.isFinite(amount) || amount <= 0) continue;
|
||||
|
||||
if (trackedLines.length > 0) {
|
||||
let warehouseId = null;
|
||||
if (patch.warehouseId != null) {
|
||||
const requestedWhId = requireId(patch.warehouseId, 'Warehouse');
|
||||
const whCheck = await runner.query(q.CHECK_WAREHOUSE, {
|
||||
warehouseId: requestedWhId,
|
||||
const trackRes = await runner.query(q.CHECK_ARTICLE_TRACK_STOCK, {
|
||||
companyId,
|
||||
sku,
|
||||
});
|
||||
if (!whCheck.rows[0]) {
|
||||
throw new Error('Warehouse not found');
|
||||
}
|
||||
warehouseId = requestedWhId;
|
||||
} else {
|
||||
const defaultWh = await runner.query(q.GET_DEFAULT_WAREHOUSE, {
|
||||
companyId,
|
||||
});
|
||||
if (!defaultWh.rows[0]) {
|
||||
throw new Error('Warehouse not found');
|
||||
}
|
||||
warehouseId = defaultWh.rows[0].id;
|
||||
}
|
||||
|
||||
for (const line of trackedLines) {
|
||||
await addLedger(
|
||||
googleAuthId,
|
||||
{
|
||||
manifestarticle_id: line.id,
|
||||
warehouse_id: warehouseId,
|
||||
transaction_type: 'SHIPPING',
|
||||
amount: -line.amount,
|
||||
transaction_text: `Versand Auftrag ${manifestNumber}`,
|
||||
},
|
||||
innerTx,
|
||||
const isUntracked = (trackRes.rows ?? []).some(
|
||||
(r) => Number(r.track_stock ?? 1) === 0
|
||||
);
|
||||
if (isUntracked) {
|
||||
untrackedLines.push({ ...line, amount });
|
||||
} else if ((trackRes.rows ?? []).length > 0) {
|
||||
trackedLines.push({ ...line, amount });
|
||||
}
|
||||
}
|
||||
|
||||
const manifestNumber = manifest.number || String(manifestId);
|
||||
|
||||
let customerName = null;
|
||||
if (manifest.entityId) {
|
||||
const entRes = await runner.query('SELECT name FROM entity WHERE id = @entityId', {
|
||||
entityId: manifest.entityId,
|
||||
});
|
||||
customerName = entRes.rows[0]?.name ?? null;
|
||||
}
|
||||
|
||||
if (trackedLines.length > 0) {
|
||||
let warehouseId = null;
|
||||
if (patch.warehouseId != null) {
|
||||
const requestedWhId = requireId(patch.warehouseId, 'Warehouse');
|
||||
const whCheck = await runner.query(q.CHECK_WAREHOUSE, {
|
||||
warehouseId: requestedWhId,
|
||||
companyId,
|
||||
});
|
||||
if (!whCheck.rows[0]) {
|
||||
throw new Error('Warehouse not found');
|
||||
}
|
||||
warehouseId = requestedWhId;
|
||||
} else {
|
||||
const defaultWh = await runner.query(q.GET_DEFAULT_WAREHOUSE, {
|
||||
companyId,
|
||||
});
|
||||
if (!defaultWh.rows[0]) {
|
||||
throw new Error('Warehouse not found');
|
||||
}
|
||||
warehouseId = defaultWh.rows[0].id;
|
||||
}
|
||||
|
||||
for (const line of trackedLines) {
|
||||
await addLedger(
|
||||
googleAuthId,
|
||||
{
|
||||
manifestarticle_id: line.id,
|
||||
warehouse_id: warehouseId,
|
||||
transaction_type: 'SHIPPING',
|
||||
amount: -line.amount,
|
||||
transaction_text: `Versand Lieferschein ${manifestNumber}`,
|
||||
},
|
||||
innerTx,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,12 +182,16 @@ export default function update(provider) {
|
||||
id: shippingId,
|
||||
type: nextType,
|
||||
state: nextState,
|
||||
trackingCode: nextTrackingCode,
|
||||
carrier: nextCarrier,
|
||||
});
|
||||
|
||||
const updatedRow = updateRes.rows[0] ?? {
|
||||
id: shippingId,
|
||||
manifest_id: current.manifestId,
|
||||
number: current.number,
|
||||
tracking_code: nextTrackingCode,
|
||||
carrier: nextCarrier,
|
||||
type: nextType,
|
||||
state: nextState,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user