This commit is contained in:
seb
2026-10-27 00:28:06 +01:00
parent f52ab1be0c
commit 75691e6afc
21 changed files with 2458 additions and 60 deletions

View File

@@ -20,6 +20,7 @@ import entityAutomat from '../../automats/entityAutomat';
import customergroupAutomat from '../../automats/customergroupAutomat';
import supplierAutomat from '../../automats/supplierAutomat';
import orderAutomat from '../../automats/orderAutomat';
import retoureAutomat from '../../automats/retoureAutomat';
import companyAutomat, { selectCompanyTaxrate } from '../../automats/companyAutomat';
import warehouseAutomat from '../../automats/warehouseAutomat';
import sseAutomat from '../../automats/sseAutomat';
@@ -46,6 +47,22 @@ import {
getManifestEditAutomat,
} from '../../automats/manifestDraftAutomat';
export function computeDocGrossTotal(lines) {
if (!Array.isArray(lines)) return 0;
return lines.reduce((acc, line) => {
const amount = Number(line.amount ?? line.count) || 0;
if (line.gross != null && line.gross !== '') {
const g = Number(line.gross) || 0;
return acc + amount * g;
}
const net = Number(line.net ?? line.netprice ?? line.netPrice) || 0;
const tax = Number(line.tax ?? line.taxrate ?? line.taxRate) || 0;
const rebate = Number(line.rebate) || 0;
const lineNet = amount * net * (1 - rebate / 100);
return acc + lineNet * (1 + tax / 100);
}, 0);
}
class ManifestForm extends PureComponent {
constructor(props) {
super(props);
@@ -481,7 +498,7 @@ class ManifestForm extends PureComponent {
let shipments = doc.shipments;
let payments = doc.payments;
if (kind === 'order') {
if (kind === 'order' || kind === 'retoure') {
if (!shipments) {
try {
const shRes = await shipmentApi.listByOrder(docId);
@@ -512,6 +529,10 @@ class ManifestForm extends PureComponent {
}
}
const linesGross = computeDocGrossTotal(lines);
const computedOrderTotal = doc.orderTotal ?? doc.grossTotal ?? doc.gross_total ?? (linesGross !== 0 ? linesGross : null);
const computedPaidTotal = doc.paidTotal ?? doc.paid_total ?? (Array.isArray(payments) && payments.length > 0 ? payments.reduce((acc, p) => acc + (Number(p.amount) || 0), 0) : null);
const loadedDoc = {
date: dateInputValue(doc.date),
createdAt: doc.createdAt ?? doc.created_at ?? null,
@@ -524,11 +545,11 @@ class ManifestForm extends PureComponent {
shipments: shipments || [],
payments: payments || [],
shippings: doc.shippings || [],
orderTotal: doc.orderTotal ?? null,
paidTotal: doc.paidTotal ?? null,
paid: Boolean(doc.paid),
shipped: Boolean(doc.shipped),
isShipped: Boolean(doc.isShipped ?? doc.shipped),
orderTotal: computedOrderTotal,
paidTotal: computedPaidTotal,
paid: Boolean(doc.paid ?? doc.isPaid ?? doc.is_paid),
shipped: Boolean(doc.shipped ?? doc.isShipped ?? doc.is_shipped),
isShipped: Boolean(doc.isShipped ?? doc.shipped ?? doc.is_shipped),
delivered: Boolean(doc.delivered),
};
@@ -1211,7 +1232,11 @@ class ManifestForm extends PureComponent {
async handleShipmentCreated() {
await this.loadEdit();
void orderAutomat.actions.reload();
if (this.props.kind === 'retoure') {
void retoureAutomat.actions.reload();
} else {
void orderAutomat.actions.reload();
}
}
async handleAttachmentDelete(attachment) {
@@ -1315,12 +1340,21 @@ class ManifestForm extends PureComponent {
attachments: this.state.attachments,
};
const linesGross = computeDocGrossTotal(savedDoc.lines);
const savedDocWithTotals = {
...savedDoc,
orderTotal: doc.orderTotal ?? doc.grossTotal ?? doc.gross_total ?? this.state.orderTotal ?? (linesGross !== 0 ? linesGross : null),
paidTotal: doc.paidTotal ?? doc.paid_total ?? this.state.paidTotal ?? null,
shipments: this.state.shipments,
payments: this.state.payments,
};
if (this.editAutomat && String(this.editAutomat.state.id) === String(docId)) {
this.editAutomat.actions.commitSaved(savedDoc);
this.editAutomat.actions.commitSaved(savedDocWithTotals);
}
this.setState({
...savedDoc,
...savedDocWithTotals,
isDirty: false,
dirtyFields: null,
dirtyLineKeys: [],
@@ -1430,8 +1464,8 @@ class ManifestForm extends PureComponent {
onPdfDelete={this.handleAttachmentDelete}
onPdfCreate={kind === 'quote' || kind === 'order' ? this.handleCreatePdf : undefined}
onCreateOrder={kind === 'quote' && this.props.mode === 'edit' ? this.handleCreateOrder : undefined}
onOpenCreateShipment={kind === 'order' && this.props.mode === 'edit' ? this.handleOpenCreateShipment : undefined}
deliverDisabled={kind === 'order' && this.isFullyDelivered()}
onOpenCreateShipment={(kind === 'order' || kind === 'retoure') && this.props.mode === 'edit' ? this.handleOpenCreateShipment : undefined}
deliverDisabled={(kind === 'order' || kind === 'retoure') && this.isFullyDelivered()}
onSave={this.handleSave}
onPartyChange={this.handlePartyChange}
onDateChange={this.handleDateChange}
@@ -1439,13 +1473,24 @@ class ManifestForm extends PureComponent {
onExternalNumberChange={this.handleExternalNumberChange}
/>
{status === 'ready' ? this.renderArticles() : null}
{status === 'ready' && kind === 'order' && this.props.mode === 'edit' ? (
{status === 'ready' && (kind === 'order' || kind === 'retoure') && this.props.mode === 'edit' ? (
<OrderDocumentsTiles
kind={kind}
orderId={this.props.docId}
orderNumber={number}
externalNumber={externalNumber}
orderTotal={this.state.orderTotal}
paidTotal={this.state.paidTotal}
orderTotal={
this.state.orderTotal != null && Number(this.state.orderTotal) !== 0
? this.state.orderTotal
: computeDocGrossTotal(this.state.lines)
}
paidTotal={
this.state.paidTotal != null
? this.state.paidTotal
: (Array.isArray(this.state.payments) && this.state.payments.length > 0
? this.state.payments.reduce((acc, p) => acc + (Number(p.amount) || 0), 0)
: 0)
}
paid={this.state.paid}
shipped={this.state.shipped}
delivered={this.state.delivered}
@@ -1482,6 +1527,7 @@ class ManifestForm extends PureComponent {
<OrderShipmentDialog
open={this.state.shipmentDialogOpen}
onClose={this.handleCloseShipmentDialog}
kind={kind}
orderId={this.props.docId}
orderNumber={number}
externalNumber={externalNumber}