upload stuff

This commit is contained in:
seb
2026-09-20 07:18:43 +02:00
parent 4011ebfc61
commit 5594c59d88
9 changed files with 249 additions and 344 deletions

View File

@@ -1,5 +1,7 @@
import { PureComponent } from 'react';
import Alert from '@mui/material/Alert';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import CircularProgress from '@mui/material/CircularProgress';
import IconButton from '@mui/material/IconButton';
@@ -13,11 +15,14 @@ import { enqueueSnackbar } from 'notistack';
import BackendError from '../../components/BackendError';
import SingleLineTextEdit from 'dashgui/SingleLineTextEdit';
import { PanelTile, PanelTileGrid } from 'dashgui/panels';
import withNavigate from '../../components/withNavigate';
import { StorageManagerContext } from '../../api/storageManagerContext';
import { requireDefined, requireObject } from '../../utils/apiShape';
import { backendErrorFrom } from '../../utils/backendError';
import { notifyError } from '../../utils/notify';
import { setLastDeliveryPath } from '../../automats';
import { deliveryDraftAutomat } from '../../automats/deliveryEditorAutomat';
function sortSuppliers(suppliers) {
return [...suppliers].sort((a, b) =>
@@ -29,17 +34,34 @@ function draftsFrom(suppliers) {
return Object.fromEntries(suppliers.map((row) => [row.id, row.name]));
}
function getInitialParams(props) {
const searchParams = typeof window !== 'undefined' ? new URLSearchParams(window.location.search) : null;
const prefill =
props.location?.state?.name ??
props.location?.state?.newName ??
searchParams?.get('name') ??
searchParams?.get('newName') ??
deliveryDraftAutomat.state.unmatchedSupplierName ??
'';
const returnTo =
props.location?.state?.returnTo ??
searchParams?.get('returnTo') ??
null;
return { prefill: prefill.trim(), returnTo };
}
class LieferungLieferantenPage extends PureComponent {
static contextType = StorageManagerContext;
constructor(props) {
super(props);
const { prefill } = getInitialParams(props);
this.state = {
status: 'loading',
error: null,
suppliers: [],
drafts: {},
newName: '',
newName: prefill,
busyKey: null,
};
this._cancelled = false;
@@ -49,11 +71,27 @@ class LieferungLieferantenPage extends PureComponent {
}
componentDidMount() {
setLastDeliveryPath(null);
const { returnTo, prefill } = getInitialParams(this.props);
if (!returnTo) {
setLastDeliveryPath(null);
}
if (prefill && !this.state.newName) {
this.setState({ newName: prefill });
}
this._cancelled = false;
void this.loadSuppliers();
}
componentDidUpdate(prevProps) {
if (prevProps.location !== this.props.location) {
const { prefill } = getInitialParams(this.props);
if (prefill && prefill !== this.state.newName) {
this.setState({ newName: prefill });
}
}
}
componentWillUnmount() {
this._cancelled = true;
}
@@ -94,16 +132,28 @@ class LieferungLieferantenPage extends PureComponent {
const name = this.state.newName.trim();
if (!name || this.state.busyKey != null) return;
const { returnTo } = getInitialParams(this.props);
this.setState({ busyKey: 'new' });
try {
const data = await this.context.supplier.create({ name });
requireObject(data, 'POST /api/supplier');
requireObject(data.supplier, 'supplier');
requireDefined(data.supplier.id, 'supplier.id');
const newSupplier = data.supplier;
// Assign the new supplier to the active delivery draft & clear unmatched name
deliveryDraftAutomat.actions.setDeliveryEntity(newSupplier.id);
deliveryDraftAutomat.actions.setUnmatchedSupplierName(null);
await this.refreshFromServer();
if (this._cancelled) return;
this.setState({ newName: '', busyKey: null });
enqueueSnackbar('Lieferant angelegt.', { variant: 'success' });
enqueueSnackbar(`Lieferant „${newSupplier.name || name}“ angelegt.`, { variant: 'success' });
if (returnTo && this.props.navigate) {
this.props.navigate(returnTo);
}
} catch (err) {
if (this._cancelled) return;
notifyError(err);
@@ -111,6 +161,7 @@ class LieferungLieferantenPage extends PureComponent {
}
}
async handleRename(supplier) {
const draft = String(this.state.drafts[supplier.id] ?? '').trim();
if (!draft) {
@@ -165,6 +216,8 @@ class LieferungLieferantenPage extends PureComponent {
const { status, error, suppliers, drafts, newName, busyKey } = this.state;
const busy = busyKey != null;
const { returnTo } = getInitialParams(this.props);
if (status === 'loading') {
return (
<Box sx={{ display: 'flex', justifyContent: 'center', py: 6 }}>
@@ -183,6 +236,26 @@ class LieferungLieferantenPage extends PureComponent {
<PanelTileGrid>
<PanelTile wide={2} minHeight={280} color="#bbf7d0">
<Stack spacing={2}>
{returnTo ? (
<Alert
severity="info"
sx={{ py: 0.5, alignItems: 'center' }}
action={
<Button
color="inherit"
size="small"
onClick={() => this.props.navigate?.(returnTo)}
>
Zurück zur Lieferung
</Button>
}
>
{newName
? `Lieferantenname „${newName}“ aus Dokument übernommen.`
: 'Neuen Lieferanten für die Lieferung anlegen.'}
</Alert>
) : null}
<Typography variant="body2" color="text.secondary">
Lieferanten der ausgewählten Firma.
</Typography>
@@ -271,4 +344,5 @@ class LieferungLieferantenPage extends PureComponent {
}
}
export default LieferungLieferantenPage;
export default withNavigate(LieferungLieferantenPage);

View File

@@ -1,7 +1,9 @@
import { PureComponent } from 'react';
import Alert from '@mui/material/Alert';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import CircularProgress from '@mui/material/CircularProgress';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { enqueueSnackbar } from 'notistack';
@@ -54,11 +56,14 @@ class LieferungNeuPage extends PureComponent {
textEditorAttachment: null,
articleSearchDialogOpen: false,
articleSearchLine: null,
unmatchedSupplierName: null,
};
this._cancelled = false;
this.handleRetry = this.handleRetry.bind(this);
this.handlePartyChange = this.handlePartyChange.bind(this);
this.handleCreateSupplierFromPdf = this.handleCreateSupplierFromPdf.bind(this);
this.handleDateChange = this.handleDateChange.bind(this);
this.handleNumberChange = this.handleNumberChange.bind(this);
this.handleExternalNumberChange = this.handleExternalNumberChange.bind(this);
this.handleLineChange = this.handleLineChange.bind(this);
@@ -209,8 +214,20 @@ class LieferungNeuPage extends PureComponent {
handlePartyChange(partyId) {
this.automat.actions.setDeliveryEntity(partyId);
if (partyId != null) {
this.setState({ unmatchedSupplierName: null });
this.automat.actions.setUnmatchedSupplierName(null);
}
}
handleCreateSupplierFromPdf(supplierName) {
const name = (supplierName ?? this.state.unmatchedSupplierName ?? '').trim();
this.handleClosePdfDialog();
const target = `/lieferung/lieferanten?name=${encodeURIComponent(name)}&returnTo=/lieferung/neu`;
this.props.navigate(target, { state: { name, returnTo: '/lieferung/neu' } });
}
handleDateChange(date) {
this.automat.actions.setDate(date);
}
@@ -424,7 +441,7 @@ class LieferungNeuPage extends PureComponent {
};
this.automat.actions.addAttachment(pdfAttachment);
// 2. Extract text using pdftotext-wasm to create 2nd file (textfile)
// 2. Extract text using pdfconvert-wasm to create 2nd file (textfile)
try {
const text = await extractPdfText(file, { layout: true });
const txtName = file.name.replace(/\.pdf$/i, '') + '.txt';
@@ -441,7 +458,7 @@ class LieferungNeuPage extends PureComponent {
};
this.automat.actions.addAttachment(txtAttachment);
} catch (txtErr) {
console.error('Fehler beim Extrahieren von Text mit pdftotext-wasm:', txtErr);
console.error('Fehler beim Extrahieren von Text mit pdfconvert-wasm:', txtErr);
}
} else {
// Direct text / CSV attachment
@@ -612,9 +629,17 @@ class LieferungNeuPage extends PureComponent {
const docType = result?.type ?? result?.basedata?.manifestType;
const notes = Array.isArray(result?.notes) ? result.notes : [];
const parsedSupplierName = (
result?.supplierName ??
result?.basedata?.lieferantName ??
result?.lieferantName ??
result?.basedata?.supplierName ??
''
).trim();
// If supplierId is null, try matching supplier by name from loaded delivery entities
if (supplierId == null && result?.basedata?.lieferantName && Array.isArray(this.state.deliveryEntities)) {
const nameToMatch = result.basedata.lieferantName.trim().toLowerCase();
if (supplierId == null && parsedSupplierName && Array.isArray(this.state.deliveryEntities)) {
const nameToMatch = parsedSupplierName.toLowerCase();
const matched = this.state.deliveryEntities.find(
(e) =>
e.name &&
@@ -625,6 +650,11 @@ class LieferungNeuPage extends PureComponent {
if (matched) supplierId = matched.id;
}
const unmatchedSupplierName = supplierId == null && parsedSupplierName ? parsedSupplierName : null;
this.setState({ unmatchedSupplierName });
this.automat.actions.setUnmatchedSupplierName(unmatchedSupplierName);
if (docDate) this.automat.actions.setDate(docDate);
if (supplierId != null) this.automat.actions.setDeliveryEntity(supplierId);
if (extNum != null) {
@@ -1018,16 +1048,50 @@ class LieferungNeuPage extends PureComponent {
/>
{partyId == null && (!lines || lines.length === 0) ? (
<PanelTile wide={3} minHeight={160} color="#e2e8f0">
<Typography color="text.secondary">
{cfg.partyGateMessage}
</Typography>
{this.state.unmatchedSupplierName ? (
<Stack spacing={2} sx={{ p: 1 }}>
<Typography color="text.secondary">
Lieferant <strong>„{this.state.unmatchedSupplierName}“</strong> wurde im Dokument erkannt, existiert aber noch nicht in den Stammdaten.
</Typography>
<Box>
<Button
variant="contained"
size="small"
onClick={() => this.handleCreateSupplierFromPdf(this.state.unmatchedSupplierName)}
>
Lieferant „{this.state.unmatchedSupplierName}“ anlegen
</Button>
</Box>
</Stack>
) : (
<Typography color="text.secondary">
{cfg.partyGateMessage}
</Typography>
)}
</PanelTile>
) : (
<>
{partyId == null && lines && lines.length > 0 && (
{partyId == null && (
<PanelTile wide={3} color="#fef3c7">
<Alert severity="warning" sx={{ py: 0.5 }}>
Bitte wählen Sie oben einen Lieferanten aus, um die erfassten Positionen speichern zu können.
<Alert
severity="warning"
action={
this.state.unmatchedSupplierName ? (
<Button
color="inherit"
size="small"
variant="outlined"
onClick={() => this.handleCreateSupplierFromPdf(this.state.unmatchedSupplierName)}
>
Lieferant anlegen
</Button>
) : null
}
sx={{ py: 0.5, alignItems: 'center' }}
>
{this.state.unmatchedSupplierName
? `Lieferant „${this.state.unmatchedSupplierName}“ wurde im Dokument erkannt, existiert aber noch nicht in den Stammdaten.`
: 'Bitte wählen Sie oben einen Lieferanten aus, um die erfassten Positionen speichern zu können.'}
</Alert>
</PanelTile>
)}
@@ -1069,9 +1133,12 @@ class LieferungNeuPage extends PureComponent {
open={pdfDialogOpen}
filename={pdfFileName}
data={pdfDialogData}
unmatchedSupplierName={this.state.unmatchedSupplierName}
onCreateSupplier={this.handleCreateSupplierFromPdf}
onClose={this.handleClosePdfDialog}
onAbort={this.handleAbortParse}
/>
<TextEditorDialog
open={this.state.textEditorOpen}
docId={this.props.docId ?? this.state.id}