This commit is contained in:
seb
2026-10-12 19:48:07 +02:00
parent 64d1772842
commit 28e62093b3
25 changed files with 979 additions and 125 deletions

View File

@@ -7,8 +7,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,
};
@@ -21,34 +23,38 @@ export default function create(provider) {
const q = queriesFor(provider);
/**
* Creates a shipping for a manifest (order). The number is allocated from the
* Creates a shipping (package) for a shipment manifest. The number is allocated from the
* `shipping` numbers sequence (own prefix + counter). If state is 'shipped',
* adjusts stock by appending negative ledger movements for tracked order lines,
* writes article history for all items, and ensures the order was not already shipped.
* adjusts stock by appending negative ledger movements for tracked shipment lines,
* writes article history for all items, and ensures stock is deducted once.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - Shipping input.
* @param {number} input.manifestId - The manifest (order) id.
* @param {number} input.manifestId - The shipment manifest id.
* @param {string} input.type - Shipping type.
* @param {string} input.state - Shipping state ('shipped' or other).
* @param {string} [input.trackingCode] - Carrier tracking code.
* @param {string} [input.carrier] - Carrier name.
* @param {number} [input.warehouseId] - Optional warehouse ID for stock deduction (defaults to company's first warehouse).
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, manifestId: number, number: string, type: string, state: string}>}
* @throws {Error} If manifest is not found, order already shipped, or input is invalid.
* @returns {Promise<{id: number, manifestId: number, number: string, trackingCode: string|null, carrier: string|null, type: string, state: string}>}
* @throws {Error} If manifest is not found, manifest is not a shipment, or input is invalid.
*
* @example
* await store.shipping.create(googleAuthId, { manifestId: 42, type: 'parcel', state: 'shipped' });
* await store.shipping.create(googleAuthId, { manifestId: 42, type: 'parcel', state: 'shipped', trackingCode: '1Z999', carrier: 'UPS' });
*/
return async (googleAuthId, input = {}, tx) => {
const manifestId = requireId(input.manifestId, 'Manifest');
const type = requireText(input.type, 'Shipping type');
const state = requireText(input.state, 'Shipping state');
const trackingCode = input.trackingCode ? String(input.trackingCode).trim() : null;
const carrier = input.carrier ? String(input.carrier).trim() : null;
const isShipped = state.trim().toLowerCase() === 'shipped';
const run = async (runner, innerTx) => {
const { companyId, companyuserId } = await access(googleAuthId, runner);
// Verify manifest exists and belongs to company
// Verify manifest exists, belongs to company, and is a shipment
const manifestRes = await runner.query(q.GET_MANIFEST_HEADER_BY_ID, {
manifestId,
companyId,
@@ -57,15 +63,17 @@ export default function create(provider) {
if (!manifest) {
throw new Error('Manifest not found');
}
if (manifest.type !== 'shipment') {
throw new Error('Manifest is not a shipment');
}
let alreadyShipped = false;
if (isShipped) {
// Check if order was already shipped
// Check if shipment was already marked shipped by an existing package
const shippedCheck = await runner.query(q.CHECK_ORDER_SHIPPED, {
manifestId,
});
if (shippedCheck.rows[0]) {
throw new Error('Order already shipped');
}
alreadyShipped = Boolean(shippedCheck.rows[0]);
}
// Allocate shipping number and insert shipping record
@@ -73,6 +81,8 @@ export default function create(provider) {
const result = await runner.query(q.SHIPPING_CREATE, {
manifestId,
number,
trackingCode,
carrier,
type,
state,
});
@@ -80,12 +90,14 @@ export default function create(provider) {
id: result.insertId,
manifest_id: manifestId,
number,
tracking_code: trackingCode,
carrier,
type,
state,
};
if (isShipped) {
// Load order lines
if (isShipped && !alreadyShipped) {
// Load shipment manifest lines
const linesRes = await runner.query(q.GET_ORDER_LINES_FOR_SHIPPING, {
manifestId,
});
@@ -156,7 +168,7 @@ export default function create(provider) {
warehouse_id: warehouseId,
transaction_type: 'SHIPPING',
amount: -line.amount,
transaction_text: `Versand Auftrag ${manifestNumber}`,
transaction_text: `Versand Lieferschein ${manifestNumber}`,
},
innerTx,
);