u
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { delivery as defaultDeliveryApi, ledger as defaultLedgerApi } from '../api';
|
||||
import { delivery as defaultDeliveryApi, ledger as defaultLedgerApi, stock as defaultStockApi } from '../api';
|
||||
import artikelAutomat, { getArticle } from '../automats/artikelAutomat';
|
||||
import articleIndexAutomat from '../automats/articleIndexAutomat';
|
||||
|
||||
@@ -92,7 +92,7 @@ export async function fetchDeliveryLedgerSummary(deliveryId, { deliveryApi = def
|
||||
let article = null;
|
||||
|
||||
const promises = [
|
||||
ledgerApi.listByManifestarticleId(line.id, 'DELIVERY').catch((err) => {
|
||||
ledgerApi.listByManifestarticleId(line.id).catch((err) => {
|
||||
console.error(`[deliveryLedger] Failed to fetch ledger for line ${line.id}:`, err);
|
||||
return null;
|
||||
}),
|
||||
@@ -118,9 +118,10 @@ export async function fetchDeliveryLedgerSummary(deliveryId, { deliveryApi = def
|
||||
|
||||
const trackStock = isLineStockTracked(line, article);
|
||||
|
||||
const deliveryEntries = ledgerEntries.filter(
|
||||
(e) => String(e.transaction_type).toUpperCase() === 'DELIVERY'
|
||||
);
|
||||
const deliveryEntries = ledgerEntries.filter((e) => {
|
||||
const t = String(e.transaction_type).toUpperCase();
|
||||
return t === 'ACCEPTANCE' || t === 'RETURN' || t === 'DELIVERY';
|
||||
});
|
||||
const bookedAmount = deliveryEntries.reduce((sum, e) => sum + Number(e.amount || 0), 0);
|
||||
const targetAmount = Number(line.amount || 0);
|
||||
const remainingAmount = trackStock ? Math.max(0, targetAmount - bookedAmount) : 0;
|
||||
@@ -171,13 +172,13 @@ export async function fetchDeliveryLedgerSummary(deliveryId, { deliveryApi = def
|
||||
}
|
||||
|
||||
/**
|
||||
* Books specified delivery lines to the ledger as goods receipts (DELIVERY).
|
||||
* Books specified delivery lines to the ledger as goods receipts via /stock/accept.
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} params.delivery - The delivery object (used for transaction memo and fallback)
|
||||
* @param {Array<{ manifestarticleId?: number, id?: number, amount: number, sku?: string }>} params.linesToBook
|
||||
* @param {number|string} params.warehouseId - Mandatory warehouse ID
|
||||
* @param {Object} [params.ledgerApi]
|
||||
* @param {Object} [params.stockApi]
|
||||
* @param {string} [params.transactionText]
|
||||
* @returns {Promise<Array<Object>>}
|
||||
*/
|
||||
@@ -185,7 +186,7 @@ export async function bookDeliveryLines({
|
||||
delivery,
|
||||
linesToBook,
|
||||
warehouseId,
|
||||
ledgerApi = defaultLedgerApi,
|
||||
stockApi = defaultStockApi,
|
||||
transactionText,
|
||||
}) {
|
||||
const numericWarehouseId = Number(warehouseId);
|
||||
@@ -203,28 +204,25 @@ export async function bookDeliveryLines({
|
||||
: `Lieferung ${delivery?.number || delivery?.id || ''}`
|
||||
).trim();
|
||||
|
||||
const results = [];
|
||||
const items = linesToBook
|
||||
.map((item) => ({
|
||||
manifestarticleId: Number(item.manifestarticleId ?? item.id),
|
||||
warehouseId: numericWarehouseId,
|
||||
text: memo,
|
||||
}))
|
||||
.filter((item) => item.manifestarticleId && Number.isInteger(item.manifestarticleId) && item.manifestarticleId > 0);
|
||||
|
||||
if (items.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const res = await stockApi.accept({ items });
|
||||
|
||||
for (const item of linesToBook) {
|
||||
const manifestarticleId = Number(item.manifestarticleId ?? item.id);
|
||||
const amount = Number(item.amount);
|
||||
|
||||
if (!manifestarticleId || !Number.isFinite(amount) || amount <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const res = await ledgerApi.add({
|
||||
manifestarticle_id: manifestarticleId,
|
||||
warehouse_id: numericWarehouseId,
|
||||
transaction_type: 'DELIVERY',
|
||||
amount,
|
||||
transaction_text: memo,
|
||||
});
|
||||
results.push(res?.ledger ?? res);
|
||||
|
||||
if (item.sku) {
|
||||
void artikelAutomat.actions.reloadArticle(item.sku).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
return Array.isArray(res?.accepted) ? res.accepted : (res ? [res] : []);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user