This commit is contained in:
seb
2026-10-17 03:57:36 +02:00
parent e63abfe2c0
commit 6df833b362
4 changed files with 40 additions and 26 deletions

View File

@@ -67,7 +67,7 @@ export function createManifestDraftAutomat({ kind, name }) {
/** @param {Array<object>} lines */ /** @param {Array<object>} lines */
setLines(lines) { setLines(lines) {
const list = Array.isArray(lines) && lines.length > 0 ? lines : [emptyLine()]; const list = Array.isArray(lines) ? lines : [];
automat.setState({ lines: list }); automat.setState({ lines: list });
}, },
@@ -100,11 +100,11 @@ export function createManifestDraftAutomat({ kind, name }) {
removeLine(key) { removeLine(key) {
const current = automat.state.lines || []; const current = automat.state.lines || [];
const next = current.filter((l) => l.key !== key); const next = current.filter((l) => l.key !== key);
automat.setState({ lines: next.length > 0 ? next : [emptyLine()] }); automat.setState({ lines: next });
}, },
removeAllLines() { removeAllLines() {
automat.setState({ lines: [emptyLine()] }); automat.setState({ lines: [] });
}, },
/** @param {Array<object>} attachments */ /** @param {Array<object>} attachments */
@@ -423,7 +423,7 @@ export function createManifestEditAutomat({ kind, name }) {
const partyId = doc.partyId ?? null; const partyId = doc.partyId ?? null;
const number = doc.number ?? ''; const number = doc.number ?? '';
const externalNumber = doc.externalNumber != null ? String(doc.externalNumber) : ''; const externalNumber = doc.externalNumber != null ? String(doc.externalNumber) : '';
const lines = Array.isArray(doc.lines) && doc.lines.length > 0 ? doc.lines : [emptyLine()]; const lines = Array.isArray(doc.lines) ? doc.lines : [];
const attachments = Array.isArray(doc.attachments) ? doc.attachments : []; const attachments = Array.isArray(doc.attachments) ? doc.attachments : [];
const baseline = { const baseline = {
@@ -508,7 +508,7 @@ export function createManifestEditAutomat({ kind, name }) {
const partyId = savedDoc.partyId ?? null; const partyId = savedDoc.partyId ?? null;
const number = savedDoc.number ?? automat.state.number; const number = savedDoc.number ?? automat.state.number;
const externalNumber = savedDoc.externalNumber != null ? String(savedDoc.externalNumber) : ''; const externalNumber = savedDoc.externalNumber != null ? String(savedDoc.externalNumber) : '';
const lines = Array.isArray(savedDoc.lines) && savedDoc.lines.length > 0 ? savedDoc.lines : [emptyLine()]; const lines = Array.isArray(savedDoc.lines) ? savedDoc.lines : [];
const attachments = Array.isArray(savedDoc.attachments) ? savedDoc.attachments : automat.state.attachments; const attachments = Array.isArray(savedDoc.attachments) ? savedDoc.attachments : automat.state.attachments;
const baseline = { const baseline = {

View File

@@ -82,7 +82,7 @@ class ManifestForm extends PureComponent {
partyId: initialDoc?.partyId ?? null, partyId: initialDoc?.partyId ?? null,
number: initialDoc?.number || '', number: initialDoc?.number || '',
externalNumber: initialDoc?.externalNumber || '', externalNumber: initialDoc?.externalNumber || '',
lines: initialDoc?.lines?.length > 0 ? initialDoc.lines : [emptyLine()], lines: Array.isArray(initialDoc?.lines) ? initialDoc.lines : (props.mode === 'create' ? [emptyLine()] : []),
attachments: initialDoc?.attachments || [], attachments: initialDoc?.attachments || [],
shipments: initialEdit?.shipments || [], shipments: initialEdit?.shipments || [],
payments: initialEdit?.payments || [], payments: initialEdit?.payments || [],
@@ -197,7 +197,7 @@ class ManifestForm extends PureComponent {
date: state.date || '', date: state.date || '',
partyId: state.partyId, partyId: state.partyId,
externalNumber: state.externalNumber || '', externalNumber: state.externalNumber || '',
lines: state.lines?.length > 0 ? state.lines : [emptyLine()], lines: state.lines || [],
attachments: state.attachments || [], attachments: state.attachments || [],
})); }));
} }
@@ -209,7 +209,7 @@ class ManifestForm extends PureComponent {
date: state.date || '', date: state.date || '',
partyId: state.partyId, partyId: state.partyId,
externalNumber: state.externalNumber || '', externalNumber: state.externalNumber || '',
lines: state.lines?.length > 0 ? state.lines : [emptyLine()], lines: state.lines || [],
attachments: state.attachments || [], attachments: state.attachments || [],
shipments: state.shipments || [], shipments: state.shipments || [],
payments: state.payments || [], payments: state.payments || [],
@@ -371,8 +371,7 @@ class ManifestForm extends PureComponent {
requireObject(docRes[cfg.docField], cfg.docField); requireObject(docRes[cfg.docField], cfg.docField);
const doc = docRes[cfg.docField]; const doc = docRes[cfg.docField];
const apiLines = requireArray(doc.lines, `${cfg.docField}.lines`); const apiLines = requireArray(doc.lines, `${cfg.docField}.lines`);
const lines = const lines = apiLines.map(lineFromApi);
apiLines.length > 0 ? apiLines.map(lineFromApi) : [emptyLine()];
// Restore attachments if available // Restore attachments if available
const apiFiles = doc.files ?? []; const apiFiles = doc.files ?? [];
@@ -456,7 +455,7 @@ class ManifestForm extends PureComponent {
partyId: state.partyId, partyId: state.partyId,
number: state.number || '', number: state.number || '',
externalNumber: state.externalNumber || '', externalNumber: state.externalNumber || '',
lines: state.lines?.length > 0 ? state.lines : [emptyLine()], lines: state.lines || [],
attachments: state.attachments || [], attachments: state.attachments || [],
isDirty: false, isDirty: false,
dirtyFields: state.dirtyFields, dirtyFields: state.dirtyFields,
@@ -787,7 +786,7 @@ class ManifestForm extends PureComponent {
} }
} }
const next = prev.lines.filter((line) => line.key !== key); const next = prev.lines.filter((line) => line.key !== key);
return { lines: next.length > 0 ? next : [emptyLine()], isDirty: true }; return { lines: next, isDirty: true };
}); });
} }
@@ -823,10 +822,10 @@ class ManifestForm extends PureComponent {
return null; return null;
} }
enqueueSnackbar('Nicht ausgelieferte Positionen wurden entfernt.', { variant: 'info' }); enqueueSnackbar('Nicht ausgelieferte Positionen wurden entfernt.', { variant: 'info' });
return { lines: remaining.length > 0 ? remaining : [emptyLine()], isDirty: true }; return { lines: remaining, isDirty: true };
} }
} }
return { lines: [emptyLine()], isDirty: true }; return { lines: [], isDirty: true };
}); });
} }
@@ -1097,7 +1096,7 @@ class ManifestForm extends PureComponent {
if (this.state.saving) return false; if (this.state.saving) return false;
if (mode === 'edit' && docId == null) return false; if (mode === 'edit' && docId == null) return false;
const requireAtLeastOne = mode === 'create'; const isEdit = mode === 'edit';
let body; let body;
try { try {
body = await buildManifestBody( body = await buildManifestBody(
@@ -1112,8 +1111,10 @@ class ManifestForm extends PureComponent {
{ {
kind: cfg.kind, kind: cfg.kind,
partyField: cfg.partyField, partyField: cfg.partyField,
requireAtLeastOne, requireAtLeastOne: true,
externalNumber: cfg.externalNumber, externalNumber: cfg.externalNumber,
isUpdate: isEdit,
mode,
}, },
); );
} catch (err) { } catch (err) {
@@ -1151,8 +1152,7 @@ class ManifestForm extends PureComponent {
number: doc.number ?? '', number: doc.number ?? '',
externalNumber: externalNumber:
doc.externalNumber != null ? String(doc.externalNumber) : '', doc.externalNumber != null ? String(doc.externalNumber) : '',
lines: lines: apiLines.map(lineFromApi),
apiLines.length > 0 ? apiLines.map(lineFromApi) : [emptyLine()],
attachments: this.state.attachments, attachments: this.state.attachments,
}; };

View File

@@ -114,8 +114,21 @@ class ManifestLines extends PureComponent {
} }
} }
return Array.isArray(lines) && if (!lines || lines.length === 0) {
lines.map((line, index) => { return (
<TableRow>
<TableCell
colSpan={columns.length + 1}
align="center"
sx={{ py: 3, color: 'text.secondary', fontSize: '0.82rem' }}
>
Keine Positionen vorhanden
</TableCell>
</TableRow>
);
}
return lines.map((line, index) => {
const lineKey = line.key ?? index; const lineKey = line.key ?? index;
const rawSku = line.sku != null ? String(line.sku).trim() : ''; const rawSku = line.sku != null ? String(line.sku).trim() : '';
const isOnShipment = kind === 'order' && Boolean(rawSku && shippedSkus.has(rawSku.toLowerCase())); const isOnShipment = kind === 'order' && Boolean(rawSku && shippedSkus.has(rawSku.toLowerCase()));

View File

@@ -454,12 +454,14 @@ export function collectFilesForUpload(attachments = [], { onlyNew = false } = {}
*/ */
export async function buildManifestBody( export async function buildManifestBody(
form, form,
{ partyField, requireAtLeastOne, externalNumber: useExternalNumber, kind }, { partyField, requireAtLeastOne, externalNumber: useExternalNumber, kind, isUpdate, mode },
) { ) {
const date = form.date; const date = form.date;
if (!date) throw new Error('Datum fehlt.'); if (!date) throw new Error('Datum fehlt.');
const externalNumber = String(form.externalNumber ?? '').trim(); const externalNumber = String(form.externalNumber ?? '').trim();
const isEdit = isUpdate != null ? Boolean(isUpdate) : (mode === 'edit' || !requireAtLeastOne);
const shouldRequireLine = requireAtLeastOne !== false;
let body; let body;
if (kind === 'delivery') { if (kind === 'delivery') {
@@ -468,7 +470,7 @@ export async function buildManifestBody(
body = { body = {
externalNumber: externalNumber || null, externalNumber: externalNumber || null,
date, date,
lines: buildLines(form.lines, { requireAtLeastOne }), lines: buildLines(form.lines, { requireAtLeastOne: shouldRequireLine }),
}; };
} else { } else {
let number = String(form.number ?? '').trim(); let number = String(form.number ?? '').trim();
@@ -479,7 +481,7 @@ export async function buildManifestBody(
body = { body = {
number, number,
date, date,
lines: buildLines(form.lines, { requireAtLeastOne }), lines: buildLines(form.lines, { requireAtLeastOne: shouldRequireLine }),
}; };
if (useExternalNumber || form.externalNumber != null) { if (useExternalNumber || form.externalNumber != null) {
body.externalNumber = externalNumber || null; body.externalNumber = externalNumber || null;
@@ -490,14 +492,13 @@ export async function buildManifestBody(
if (!Number.isNaN(pid)) { if (!Number.isNaN(pid)) {
body[partyField] = pid; body[partyField] = pid;
} }
} else if (!requireAtLeastOne) { } else if (isEdit) {
body[partyField] = null; body[partyField] = null;
} }
// Add files if attachments exist // Add files if attachments exist
if (form.attachments && form.attachments.length > 0) { if (form.attachments && form.attachments.length > 0) {
const isUpdate = !requireAtLeastOne; const filesToUpload = collectFilesForUpload(form.attachments, { onlyNew: isEdit });
const filesToUpload = collectFilesForUpload(form.attachments, { onlyNew: isUpdate });
if (filesToUpload.length > 0) { if (filesToUpload.length > 0) {
const formData = new FormData(); const formData = new FormData();
body.filesMeta = filesToUpload.map((item) => ({ body.filesMeta = filesToUpload.map((item) => ({