u
This commit is contained in:
@@ -182,6 +182,7 @@ import paymentCreate from './store/payment/create.js';
|
||||
import paymentListByManifestId from './store/payment/listByManifestId.js';
|
||||
import paymentDelete from './store/payment/delete.js';
|
||||
import shippingCreate from './store/shipping/create.js';
|
||||
import shippingUpdate from './store/shipping/update.js';
|
||||
import shippingListByManifestId from './store/shipping/listByManifestId.js';
|
||||
import shippingDelete from './store/shipping/delete.js';
|
||||
import packageCreate from './store/package/create.js';
|
||||
@@ -451,6 +452,7 @@ export default class LibDashStore {
|
||||
|
||||
this.shipping = {
|
||||
create: shippingCreate(provider),
|
||||
update: shippingUpdate(provider),
|
||||
listByManifestId: shippingListByManifestId(provider),
|
||||
delete: shippingDelete(provider),
|
||||
};
|
||||
|
||||
@@ -29,7 +29,12 @@ FROM articlesupplier asup
|
||||
INNER JOIN entity e ON e.id = asup.entity_id
|
||||
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
|
||||
WHERE asup.sku = @sku
|
||||
AND cg.company_id = @companyId;
|
||||
AND cg.company_id = @companyId
|
||||
UNION
|
||||
SELECT id AS articleId
|
||||
FROM article
|
||||
WHERE sku = @sku
|
||||
AND company_id = @companyId;
|
||||
`;
|
||||
|
||||
export const INSERT_ARTICLE_HISTORY = `
|
||||
|
||||
@@ -75,6 +75,59 @@ WHERE id = @id
|
||||
AND manifest_id IN (SELECT m.id FROM manifest m WHERE m.company_id = @companyId)
|
||||
`;
|
||||
|
||||
export const GET_SHIPPING_BY_ID = `
|
||||
SELECT s.id, s.manifest_id AS manifestId, s.[number], s.[type], s.[state]
|
||||
FROM shipping s
|
||||
INNER JOIN manifest m ON m.id = s.manifest_id
|
||||
WHERE s.id = @id AND m.company_id = @companyId;
|
||||
`;
|
||||
|
||||
export const SHIPPING_UPDATE = `
|
||||
UPDATE shipping
|
||||
SET [type] = @type, [state] = @state
|
||||
OUTPUT INSERTED.id, INSERTED.manifest_id, INSERTED.[number], INSERTED.[type], INSERTED.[state]
|
||||
WHERE id = @id;
|
||||
`;
|
||||
|
||||
export const CHECK_ORDER_SHIPPED = `
|
||||
SELECT TOP 1 s.id
|
||||
FROM shipping s
|
||||
WHERE s.manifest_id = @manifestId AND LOWER(s.[state]) = 'shipped';
|
||||
`;
|
||||
|
||||
export const GET_ORDER_LINES_FOR_SHIPPING = `
|
||||
SELECT id, sku, name, amount
|
||||
FROM manifestarticle
|
||||
WHERE manifest_id = @manifestId
|
||||
ORDER BY id ASC;
|
||||
`;
|
||||
|
||||
export const GET_MANIFEST_HEADER_BY_ID = `
|
||||
SELECT id, company_id AS companyId, [number], [type], entity_id AS entityId
|
||||
FROM manifest
|
||||
WHERE id = @manifestId AND company_id = @companyId;
|
||||
`;
|
||||
|
||||
export const GET_DEFAULT_WAREHOUSE = `
|
||||
SELECT TOP 1 id, name
|
||||
FROM warehouse
|
||||
WHERE company_id = @companyId
|
||||
ORDER BY sort ASC, name ASC, id ASC;
|
||||
`;
|
||||
|
||||
export const CHECK_WAREHOUSE = `
|
||||
SELECT id, name
|
||||
FROM warehouse
|
||||
WHERE id = @warehouseId AND company_id = @companyId;
|
||||
`;
|
||||
|
||||
export const CHECK_ARTICLE_TRACK_STOCK = `
|
||||
SELECT a.sku, a.track_stock
|
||||
FROM article a
|
||||
WHERE a.company_id = @companyId
|
||||
AND (a.sku = @sku OR EXISTS (SELECT 1 FROM articlesupplier asup WHERE asup.article_id = a.id AND asup.sku = @sku));
|
||||
`;
|
||||
|
||||
export const PACKAGE_CREATE = `
|
||||
INSERT INTO package (shipping_id, entityaddress_id, [number], [type], [state], details)
|
||||
OUTPUT INSERTED.id, INSERTED.shipping_id, INSERTED.entityaddress_id, INSERTED.[number], INSERTED.[type], INSERTED.[state], INSERTED.details
|
||||
|
||||
@@ -29,7 +29,12 @@ FROM articlesupplier asup
|
||||
INNER JOIN entity e ON e.id = asup.entity_id
|
||||
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
|
||||
WHERE asup.sku = @sku
|
||||
AND cg.company_id = @companyId;
|
||||
AND cg.company_id = @companyId
|
||||
UNION
|
||||
SELECT id AS articleId
|
||||
FROM article
|
||||
WHERE sku = @sku
|
||||
AND company_id = @companyId;
|
||||
`;
|
||||
|
||||
export const INSERT_ARTICLE_HISTORY = `
|
||||
|
||||
@@ -15,6 +15,14 @@ export const {
|
||||
SHIPPING_CREATE,
|
||||
SHIPPING_LIST_BY_MANIFEST,
|
||||
SHIPPING_DELETE,
|
||||
GET_SHIPPING_BY_ID,
|
||||
SHIPPING_UPDATE,
|
||||
CHECK_ORDER_SHIPPED,
|
||||
GET_ORDER_LINES_FOR_SHIPPING,
|
||||
GET_MANIFEST_HEADER_BY_ID,
|
||||
GET_DEFAULT_WAREHOUSE,
|
||||
CHECK_WAREHOUSE,
|
||||
CHECK_ARTICLE_TRACK_STOCK,
|
||||
PACKAGE_CREATE,
|
||||
PACKAGE_LIST_BY_SHIPPING,
|
||||
PACKAGE_DELETE,
|
||||
|
||||
@@ -197,6 +197,13 @@ export default function add(provider) {
|
||||
!upperType.includes('ADJUST') &&
|
||||
!upperType.includes('LOSS') &&
|
||||
!upperType.includes('WRITE_OFF'));
|
||||
const isShipping =
|
||||
upperType === 'SHIPPING' ||
|
||||
upperType === 'ORDER_SHIPPED' ||
|
||||
(manifestArticle.manifestType === 'order' &&
|
||||
!upperType.includes('ADJUST') &&
|
||||
!upperType.includes('LOSS') &&
|
||||
!upperType.includes('WRITE_OFF'));
|
||||
|
||||
let historyText = transactionText;
|
||||
if (isDelivery) {
|
||||
@@ -224,6 +231,22 @@ export default function add(provider) {
|
||||
transactionText?.trim() ||
|
||||
(manifestArticle.manifestNumber ? `Lieferung ${manifestArticle.manifestNumber}` : 'Lieferung');
|
||||
historyText = `${base}${suffix}`;
|
||||
} else if (isShipping) {
|
||||
const details = [];
|
||||
if (manifestArticle.supplierName) {
|
||||
details.push(`Kunde: "${manifestArticle.supplierName}"`);
|
||||
}
|
||||
if (warehouseName) {
|
||||
details.push(`Lager: "${warehouseName}"`);
|
||||
}
|
||||
if (amount != null && Number.isFinite(Number(amount))) {
|
||||
details.push(`Menge: ${amount}`);
|
||||
}
|
||||
const suffix = details.length ? ` (${details.join(', ')})` : '';
|
||||
const base =
|
||||
transactionText?.trim() ||
|
||||
(manifestArticle.manifestNumber ? `Versand Auftrag ${manifestArticle.manifestNumber}` : 'Versand');
|
||||
historyText = `${base}${suffix}`;
|
||||
} else if (warehouseName && !transactionText.includes(warehouseName)) {
|
||||
historyText = `${transactionText} (Lager: "${warehouseName}")`;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { queriesFor, requireId, requireText } from '../logistics/helpers.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveManifestAccess from '../manifestattachment/resolveManifestAccess.js';
|
||||
import nextNumber from '../numbers/nextNumber.js';
|
||||
import ledgerAdd from '../ledger/add.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function shape(row) {
|
||||
@@ -16,37 +16,60 @@ function shape(row) {
|
||||
|
||||
export default function create(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const manifestAccess = resolveManifestAccess(provider);
|
||||
const allocateNumber = nextNumber(provider);
|
||||
const addLedger = ledgerAdd(provider);
|
||||
const q = queriesFor(provider);
|
||||
|
||||
/**
|
||||
* Creates a shipping for a manifest (order). The number is allocated from the
|
||||
* `shipping` numbers sequence (own prefix + counter).
|
||||
* `shipping` numbers sequence (own prefix + counter). If state is 'shipped',
|
||||
* adjusts stock by appending negative ledger movements for tracked order lines,
|
||||
* writes article history, and ensures the order was not already shipped.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {object} input - Shipping input.
|
||||
* @param {number} input.manifestId - The manifest (order) id.
|
||||
* @param {string} input.type - Shipping type.
|
||||
* @param {string} input.state - Shipping state.
|
||||
* @param {string} input.state - Shipping state ('shipped' or other).
|
||||
* @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 or input is invalid.
|
||||
* @throws {Error} If manifest is not found, order already shipped, or input is invalid.
|
||||
*
|
||||
* @example
|
||||
* await store.shipping.create(googleAuthId, { manifestId: 42, type: 'parcel', state: 'open' });
|
||||
* await store.shipping.create(googleAuthId, { manifestId: 42, type: 'parcel', state: 'shipped' });
|
||||
*/
|
||||
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 isShipped = state.trim().toLowerCase() === 'shipped';
|
||||
|
||||
const run = async (runner, innerTx) => {
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
await manifestAccess(companyId, manifestId, innerTx);
|
||||
|
||||
// Verify manifest exists and belongs to company
|
||||
const manifestRes = await runner.query(q.GET_MANIFEST_HEADER_BY_ID, {
|
||||
manifestId,
|
||||
companyId,
|
||||
});
|
||||
const manifest = manifestRes.rows[0];
|
||||
if (!manifest) {
|
||||
throw new Error('Manifest not found');
|
||||
}
|
||||
|
||||
if (isShipped) {
|
||||
// Check if order was already shipped
|
||||
const shippedCheck = await runner.query(q.CHECK_ORDER_SHIPPED, {
|
||||
manifestId,
|
||||
});
|
||||
if (shippedCheck.rows[0]) {
|
||||
throw new Error('Order already shipped');
|
||||
}
|
||||
}
|
||||
|
||||
// Allocate shipping number and insert shipping record
|
||||
const number = await allocateNumber(companyId, 'shipping', innerTx);
|
||||
|
||||
const result = await runner.query(q.SHIPPING_CREATE, {
|
||||
manifestId,
|
||||
number,
|
||||
@@ -60,12 +83,83 @@ export default function create(provider) {
|
||||
type,
|
||||
state,
|
||||
};
|
||||
|
||||
if (isShipped) {
|
||||
// Load order lines
|
||||
const linesRes = await runner.query(q.GET_ORDER_LINES_FOR_SHIPPING, {
|
||||
manifestId,
|
||||
});
|
||||
const lines = linesRes.rows ?? [];
|
||||
|
||||
// Check which lines need warehouse stock deduction
|
||||
const trackedLines = [];
|
||||
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, {
|
||||
companyId,
|
||||
sku,
|
||||
});
|
||||
const isUntracked = (trackRes.rows ?? []).some(
|
||||
(r) => Number(r.track_stock ?? 1) === 0
|
||||
);
|
||||
if (!isUntracked && (trackRes.rows ?? []).length > 0) {
|
||||
trackedLines.push({ ...line, amount });
|
||||
}
|
||||
}
|
||||
|
||||
if (trackedLines.length > 0) {
|
||||
let warehouseId = null;
|
||||
if (input.warehouseId != null) {
|
||||
const requestedWhId = requireId(input.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;
|
||||
}
|
||||
|
||||
const manifestNumber = manifest.number || String(manifestId);
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return shape(row);
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return run(makeRunner(provider, tx), tx);
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction((runner, innerTx) => run(runner, innerTx));
|
||||
}
|
||||
return run(makeRunner(provider, tx));
|
||||
return run(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,11 +4,13 @@ import makeRunner from '../runner.js';
|
||||
|
||||
/**
|
||||
* Deletes a shipping of the user's selected company. Its packages are removed by cascade.
|
||||
* Once shipped, a shipping cannot be deleted because stock movements have been ledgered.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - The shipping id.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<void>}
|
||||
* @throws {Error} If shipping is not found or is already shipped.
|
||||
*
|
||||
* @example
|
||||
* await store.shipping.delete(googleAuthId, 9);
|
||||
@@ -21,6 +23,19 @@ export default function del(provider) {
|
||||
const shippingId = requireId(id, 'Shipping');
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const check = await runner.query(q.GET_SHIPPING_BY_ID, {
|
||||
id: shippingId,
|
||||
companyId,
|
||||
});
|
||||
const shipping = check.rows[0];
|
||||
if (!shipping) {
|
||||
throw new Error('Shipping not found');
|
||||
}
|
||||
if (String(shipping.state || '').trim().toLowerCase() === 'shipped') {
|
||||
throw new Error('Shipped shipping cannot be deleted');
|
||||
}
|
||||
|
||||
await runner.query(q.SHIPPING_DELETE, { id: shippingId, companyId });
|
||||
};
|
||||
}
|
||||
|
||||
182
src/store/shipping/update.js
Normal file
182
src/store/shipping/update.js
Normal file
@@ -0,0 +1,182 @@
|
||||
import { queriesFor, requireId, requireText } from '../logistics/helpers.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import ledgerAdd from '../ledger/add.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
manifestId: row.manifest_id,
|
||||
number: row.number,
|
||||
type: row.type,
|
||||
state: row.state,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a shipping record for the user's selected company.
|
||||
* If transitioned to 'shipped', books ledger movements and adjusts stock.
|
||||
* Once shipped, shipping cannot be undone (state cannot be changed away from 'shipped').
|
||||
*
|
||||
* @param {import('../../providers/provider.js').DatabaseProvider} provider
|
||||
*/
|
||||
export default function update(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const addLedger = ledgerAdd(provider);
|
||||
const q = queriesFor(provider);
|
||||
|
||||
/**
|
||||
* Updates a shipping record.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - Shipping ID.
|
||||
* @param {object} patch - Shipping patch fields.
|
||||
* @param {string} [patch.type] - Shipping type.
|
||||
* @param {string} [patch.state] - Shipping state.
|
||||
* @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.
|
||||
*/
|
||||
return async (googleAuthId, id, patch = {}, tx) => {
|
||||
const shippingId = requireId(id, 'Shipping');
|
||||
|
||||
const run = async (runner, innerTx) => {
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const check = await runner.query(q.GET_SHIPPING_BY_ID, {
|
||||
id: shippingId,
|
||||
companyId,
|
||||
});
|
||||
const current = check.rows[0];
|
||||
if (!current) {
|
||||
throw new Error('Shipping not found');
|
||||
}
|
||||
|
||||
const isCurrentlyShipped =
|
||||
String(current.state || '').trim().toLowerCase() === 'shipped';
|
||||
const hasState = Object.prototype.hasOwnProperty.call(patch, 'state');
|
||||
const hasType = Object.prototype.hasOwnProperty.call(patch, 'type');
|
||||
|
||||
if (isCurrentlyShipped) {
|
||||
if (
|
||||
hasState &&
|
||||
String(patch.state || '').trim().toLowerCase() !== 'shipped'
|
||||
) {
|
||||
throw new Error('Shipped shipping cannot be undone');
|
||||
}
|
||||
}
|
||||
|
||||
const nextType = hasType
|
||||
? requireText(patch.type, 'Shipping type')
|
||||
: current.type;
|
||||
const nextState = hasState
|
||||
? requireText(patch.state, 'Shipping state')
|
||||
: current.state;
|
||||
const willBeShipped =
|
||||
!isCurrentlyShipped &&
|
||||
String(nextState).trim().toLowerCase() === 'shipped';
|
||||
|
||||
if (willBeShipped) {
|
||||
const manifestId = current.manifestId;
|
||||
const shippedCheck = await runner.query(q.CHECK_ORDER_SHIPPED, {
|
||||
manifestId,
|
||||
});
|
||||
if (shippedCheck.rows[0]) {
|
||||
throw new Error('Order already shipped');
|
||||
}
|
||||
|
||||
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 = [];
|
||||
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, {
|
||||
companyId,
|
||||
sku,
|
||||
});
|
||||
const isUntracked = (trackRes.rows ?? []).some(
|
||||
(r) => Number(r.track_stock ?? 1) === 0
|
||||
);
|
||||
if (!isUntracked && (trackRes.rows ?? []).length > 0) {
|
||||
trackedLines.push({ ...line, amount });
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
const manifestNumber = manifest.number || String(manifestId);
|
||||
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 updateRes = await runner.query(q.SHIPPING_UPDATE, {
|
||||
id: shippingId,
|
||||
type: nextType,
|
||||
state: nextState,
|
||||
});
|
||||
|
||||
const updatedRow = updateRes.rows[0] ?? {
|
||||
id: shippingId,
|
||||
manifest_id: current.manifestId,
|
||||
number: current.number,
|
||||
type: nextType,
|
||||
state: nextState,
|
||||
};
|
||||
|
||||
return shape(updatedRow);
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return run(makeRunner(provider, tx), tx);
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction((runner, innerTx) => run(runner, innerTx));
|
||||
}
|
||||
return run(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user