This commit is contained in:
seb
2026-10-17 18:36:18 +02:00
parent 582bf04476
commit 45c1067d9f
5 changed files with 325 additions and 249 deletions

View File

@@ -118,10 +118,41 @@ export async function fetchDeliveryLedgerSummary(deliveryId, { deliveryApi = def
const trackStock = isLineStockTracked(line, article);
const deliveryEntries = ledgerEntries.filter((e) => {
const acceptanceEntries = ledgerEntries.filter((e) => {
const t = String(e.transaction_type).toUpperCase();
return t === 'ACCEPTANCE' || t === 'RETURN' || t === 'DELIVERY';
});
const warehouseEntries = ledgerEntries.filter((e) => {
const t = String(e.transaction_type).toUpperCase();
return t === 'WAREHOUSE';
});
let deliveryEntries;
if (acceptanceEntries.length > 0) {
deliveryEntries = acceptanceEntries.map((acc, idx) => {
const matchingWh =
warehouseEntries.find(
(w) =>
w.warehouse_id != null &&
(w.transaction_text === acc.transaction_text ||
Math.abs(new Date(w.created_at) - new Date(acc.created_at)) < 15000)
) || warehouseEntries[idx] || null;
return {
...acc,
warehouse_id: matchingWh?.warehouse_id ?? acc.warehouse_id ?? null,
warehouse_amount: matchingWh?.amount ?? null,
};
});
} else if (warehouseEntries.length > 0) {
deliveryEntries = warehouseEntries.map((w) => ({
...w,
warehouse_id: w.warehouse_id ?? null,
warehouse_amount: w.amount ?? null,
}));
} else {
deliveryEntries = [];
}
const bookedAmount = deliveryEntries.reduce((sum, e) => sum + Number(e.amount || 0), 0);
const targetAmount = Number(line.amount || 0);
const remainingAmount = Math.max(0, targetAmount - bookedAmount);