2026-12-04 22:43:40 -08:00
|
|
|
|
import { App as AntApp } from 'antd';
|
|
|
|
|
|
import { useOrderAccess } from './useOrderAccess';
|
|
|
|
|
|
import { useEffect, useRef, useState } from 'react';
|
|
|
|
|
|
import { Calc } from '../../shared';
|
|
|
|
|
|
import { navigate } from '../../lib/router';
|
|
|
|
|
|
import { orderViews } from './orderViews';
|
|
|
|
|
|
import { Dialog, queues, useRead } from './OrdersPage';
|
|
|
|
|
|
|
|
|
|
|
|
const stop = e => {
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
};
|
|
|
|
|
|
const messageOf = err => (err instanceof Error ? err.message : String(err));
|
|
|
|
|
|
const readForm = form => Object.fromEntries(new FormData(form));
|
|
|
|
|
|
function DetailNavigation({ ids, id, open, blocked }) {
|
|
|
|
|
|
if (!ids) return null;
|
|
|
|
|
|
const i = ids.indexOf(id);
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="order-detail-navigation-row" role="group" aria-label="切换订单">
|
|
|
|
|
|
<button data-action="order-detail-prev" disabled={blocked || i <= 0} onClick={() => open(ids[i - 1])}>
|
|
|
|
|
|
← 上一条
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<span aria-live="polite">
|
|
|
|
|
|
{i + 1} / {ids.length}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<button
|
|
|
|
|
|
data-action="order-detail-next"
|
|
|
|
|
|
disabled={blocked || i < 0 || i >= ids.length - 1}
|
|
|
|
|
|
onClick={() => open(ids[i + 1])}
|
|
|
|
|
|
>
|
|
|
|
|
|
下一条 →
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<small>按当前筛选后的销售订单顺序查看</small>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function OrderActionDialog({
|
|
|
|
|
|
selected: id,
|
|
|
|
|
|
client,
|
|
|
|
|
|
state,
|
|
|
|
|
|
session,
|
|
|
|
|
|
refresh,
|
|
|
|
|
|
refreshState,
|
|
|
|
|
|
listIds,
|
|
|
|
|
|
openOrder,
|
|
|
|
|
|
close,
|
|
|
|
|
|
onLost,
|
|
|
|
|
|
printStatuses,
|
|
|
|
|
|
}) {
|
|
|
|
|
|
const base = '/api/orders/' + encodeURIComponent(id);
|
|
|
|
|
|
const [generation, reload] = useState(0),
|
|
|
|
|
|
[form, setForm] = useState(null),
|
|
|
|
|
|
[error, setError] = useState('');
|
|
|
|
|
|
const [ui, setUi] = useState({}),
|
|
|
|
|
|
[expenses, setExpenses] = useState([]);
|
|
|
|
|
|
const current = useRef(0);
|
|
|
|
|
|
useEffect(
|
|
|
|
|
|
() => () => {
|
|
|
|
|
|
current.current++;
|
|
|
|
|
|
},
|
|
|
|
|
|
[],
|
|
|
|
|
|
);
|
|
|
|
|
|
const details = useRead(async () => {
|
|
|
|
|
|
const [order, ...results] = await Promise.allSettled(
|
|
|
|
|
|
['', '/change', '/ownership', '/cancellation'].map(path => client.api(base + path)),
|
|
|
|
|
|
);
|
|
|
|
|
|
if (order.status === 'rejected') {
|
|
|
|
|
|
access.revoke(order.reason);
|
|
|
|
|
|
throw order.reason;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (order.value.id !== id) throw new Error('返回的订单与所选订单不一致,请重试读取。');
|
|
|
|
|
|
const checks = orderViews({ state, session });
|
|
|
|
|
|
const validators = [
|
|
|
|
|
|
checks.validateOrderChangeRequest,
|
|
|
|
|
|
checks.validateOrderOwnershipRequest,
|
|
|
|
|
|
checks.validateOrderCancellationRequest,
|
|
|
|
|
|
];
|
|
|
|
|
|
const entries = results.map((r, i) => {
|
|
|
|
|
|
const entry = { id, user: state.user.id, revision: order.value.revision };
|
|
|
|
|
|
if (r.status === 'rejected') {
|
|
|
|
|
|
if (access.revoke(r.reason)) throw r.reason;
|
|
|
|
|
|
return { ...entry, error: r.reason.message };
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
const data = r.value;
|
|
|
|
|
|
if (data.revision !== order.value.revision || !Array.isArray(data.requests))
|
|
|
|
|
|
throw new Error('订单已有新记录,请重新读取订单后复核。');
|
|
|
|
|
|
data.requests.forEach(request => validators[i](request, id));
|
|
|
|
|
|
return { ...entry, data };
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
return { ...entry, error: err.message };
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
return { order: order.value, entries };
|
|
|
|
|
|
}, [id, generation, state.user.id].join('/'));
|
|
|
|
|
|
const original = details.value?.order;
|
|
|
|
|
|
const access = useOrderAccess({ client, state, id, watch: !!original, onLost });
|
|
|
|
|
|
const printStatus = printStatuses[original?.printStatus?.id || id];
|
|
|
|
|
|
const order = original && printStatus ? { ...original, printStatus } : original;
|
|
|
|
|
|
const [change, ownership, cancellation] = details.value?.entries || [];
|
2026-12-05 03:21:56 -08:00
|
|
|
|
const pending = client.pending();
|
2026-12-04 22:43:40 -08:00
|
|
|
|
const ctx = {
|
|
|
|
|
|
state,
|
|
|
|
|
|
session,
|
|
|
|
|
|
currentOrder: order,
|
|
|
|
|
|
orderChangeDetail: change,
|
|
|
|
|
|
orderOwnershipDetail: ownership,
|
|
|
|
|
|
orderCancellationDetail: cancellation,
|
|
|
|
|
|
orderAfterSaleFormEntry: order && {
|
|
|
|
|
|
id,
|
|
|
|
|
|
revision: order.revision,
|
|
|
|
|
|
user: state.user.id,
|
|
|
|
|
|
forceApproval: ui.forceApproval,
|
|
|
|
|
|
},
|
2026-12-05 03:21:56 -08:00
|
|
|
|
ui: {
|
|
|
|
|
|
...ui,
|
|
|
|
|
|
actionBlocked: client.busy || !!pending,
|
|
|
|
|
|
retryBusy: client.busy,
|
|
|
|
|
|
retryKey: pending?.key === ui.pendingKey ? ui.pendingKey : null,
|
|
|
|
|
|
},
|
2026-12-04 22:43:40 -08:00
|
|
|
|
navigation: (
|
|
|
|
|
|
<DetailNavigation ids={listIds} id={id} open={openOrder} blocked={!!form || client.busy || !!client.pending()} />
|
|
|
|
|
|
),
|
|
|
|
|
|
};
|
|
|
|
|
|
const views = orderViews(ctx);
|
|
|
|
|
|
function clearForm() {
|
|
|
|
|
|
current.current++;
|
|
|
|
|
|
setForm(null);
|
|
|
|
|
|
setError('');
|
|
|
|
|
|
setExpenses([]);
|
|
|
|
|
|
setUi({});
|
|
|
|
|
|
}
|
|
|
|
|
|
const loadAfter = useRead(() => client.api(base + '/after-sale'), form?.type === 'after' && id + '/' + generation);
|
|
|
|
|
|
let panel,
|
|
|
|
|
|
visibleError = error;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (form?.type === 'after') {
|
|
|
|
|
|
if (loadAfter.error) visibleError = loadAfter.error;
|
|
|
|
|
|
else if (!loadAfter.value) panel = <p role="status">正在读取售后资料…</p>;
|
|
|
|
|
|
else if (
|
|
|
|
|
|
loadAfter.value.orderId !== id ||
|
|
|
|
|
|
loadAfter.value.revision !== order.revision ||
|
|
|
|
|
|
!loadAfter.value.eligible ||
|
|
|
|
|
|
loadAfter.value.record ||
|
|
|
|
|
|
!views.orderAfterSalePocketKeys().length ||
|
|
|
|
|
|
order.lines.some(
|
|
|
|
|
|
l => l.unit !== 'g' || !l.costParts || l.costParts.material == null || l.costParts.labor == null,
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
visibleError = loadAfter.value.blockedReason || '请重新核对原单成本、自有货盘与售后资格。';
|
|
|
|
|
|
else
|
|
|
|
|
|
panel = orderViews({
|
|
|
|
|
|
...ctx,
|
|
|
|
|
|
ui: {
|
|
|
|
|
|
...ui,
|
|
|
|
|
|
expenses: expenses.map(key => (
|
|
|
|
|
|
<div key={key} data-expense-id={key}>
|
|
|
|
|
|
{views.orderAfterSaleExpenseHTML()}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)),
|
|
|
|
|
|
},
|
|
|
|
|
|
}).orderAfterSaleFormHTML(order);
|
|
|
|
|
|
} else if (form?.type === 'action') panel = views.showAction(form.kind);
|
|
|
|
|
|
else if (form)
|
|
|
|
|
|
panel = {
|
|
|
|
|
|
change: views.showOrderChangeForm,
|
|
|
|
|
|
ownership: views.showOrderOwnershipForm,
|
|
|
|
|
|
cancellation: views.showOrderCancellationForm,
|
|
|
|
|
|
}[form.type](form.kind);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
visibleError = messageOf(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
function refreshDetail() {
|
|
|
|
|
|
clearForm();
|
|
|
|
|
|
reload(v => v + 1);
|
|
|
|
|
|
refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
async function onClick(event) {
|
|
|
|
|
|
const target = event.target.closest('[data-action]');
|
|
|
|
|
|
if (!target) return;
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
const { action, id: targetId } = target.dataset;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (action === 'close-dialog' || action === 'order-detail-prev' || action === 'order-detail-next') return;
|
|
|
|
|
|
if (action === 'print') {
|
|
|
|
|
|
window.open('/print/' + encodeURIComponent(id) + '?mode=full', '_blank', 'noopener');
|
|
|
|
|
|
return await client.mutate(base + '/print', { mode: 'full' });
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'recovery-print')
|
|
|
|
|
|
return window.open('/print/recoveries/' + encodeURIComponent(targetId), '_blank', 'noopener');
|
|
|
|
|
|
if (action === 'edit-order') return navigate('order-edit', { search: '?order=' + encodeURIComponent(id) });
|
|
|
|
|
|
if (action === 'edit') return navigate('create', { search: '?draft=' + encodeURIComponent(id) });
|
|
|
|
|
|
if (action === 'goto-team-funds') return navigate('team-funds');
|
|
|
|
|
|
if (action === 'goto-material-pools') return navigate('material-pools');
|
|
|
|
|
|
if (action === 'open-price-lock')
|
|
|
|
|
|
return navigate('price-locks', { search: '?lock=' + encodeURIComponent(targetId) });
|
|
|
|
|
|
if (action === 'recovery-open')
|
|
|
|
|
|
return navigate('recoveries', { search: '?recovery=' + encodeURIComponent(targetId) });
|
|
|
|
|
|
if (action.includes('reload') || action.includes('retry')) return refreshDetail();
|
|
|
|
|
|
if (action.includes('close') || action === 'hide-action') return clearForm();
|
|
|
|
|
|
if (client.busy || client.pending()) throw new Error('请先恢复原操作。');
|
|
|
|
|
|
if (action === 'order-after-sale-preview') {
|
|
|
|
|
|
const node = target.form;
|
|
|
|
|
|
const input = views.orderAfterSaleInput(node);
|
|
|
|
|
|
const version = ++current.current;
|
|
|
|
|
|
const signature = JSON.stringify(input);
|
|
|
|
|
|
node.elements.verified.checked = false;
|
|
|
|
|
|
setUi(old => ({
|
|
|
|
|
|
...old,
|
|
|
|
|
|
preview: null,
|
|
|
|
|
|
verified: false,
|
|
|
|
|
|
afterError: '',
|
|
|
|
|
|
previewLoading: true,
|
|
|
|
|
|
previewStatus: '正在按原单成本核对售后…',
|
|
|
|
|
|
}));
|
|
|
|
|
|
let result;
|
|
|
|
|
|
try {
|
|
|
|
|
|
result = await client.api(base + '/after-sale-preview', input);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (current.current === version) throw err;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (current.current !== version) return;
|
|
|
|
|
|
if (result.before?.id !== id || result.after?.id !== id || result.before.revision !== order.revision)
|
|
|
|
|
|
throw new Error('售后预览与当前订单不一致,请重新读取。');
|
|
|
|
|
|
const record = result.afterSale || result.after.orderAfterSale;
|
|
|
|
|
|
views.orderAfterSaleSummaryHTML(record, { preview: true });
|
|
|
|
|
|
setUi(old => ({
|
|
|
|
|
|
...old,
|
|
|
|
|
|
preview: { record, signature },
|
|
|
|
|
|
previewLoading: false,
|
|
|
|
|
|
previewStatus: '预览已更新,请核对后勾选确认。',
|
|
|
|
|
|
}));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'order-after-sale-add-expense' || action === 'order-after-sale-remove-expense') {
|
|
|
|
|
|
current.current++;
|
|
|
|
|
|
if (target.form?.elements.verified) target.form.elements.verified.checked = false;
|
|
|
|
|
|
setUi(v => ({
|
|
|
|
|
|
...v,
|
|
|
|
|
|
preview: null,
|
|
|
|
|
|
previewLoading: false,
|
|
|
|
|
|
verified: false,
|
|
|
|
|
|
previewStatus: '资料已修改,请重新核对售后预览。',
|
|
|
|
|
|
}));
|
|
|
|
|
|
const key = target.closest('[data-expense-id]')?.dataset.expenseId;
|
|
|
|
|
|
setExpenses(items =>
|
|
|
|
|
|
action.endsWith('add-expense') ? [...items, crypto.randomUUID()] : items.filter(x => x !== key),
|
|
|
|
|
|
);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
clearForm();
|
|
|
|
|
|
if (action === 'order-after-sale-open') return setForm({ type: 'after' });
|
|
|
|
|
|
if (action.startsWith('order-ownership-'))
|
|
|
|
|
|
return setForm({ type: 'ownership', kind: action.slice('order-ownership-'.length) });
|
|
|
|
|
|
if (action.endsWith('-order-change')) return setForm({ type: 'change', kind: action.split('-')[0] });
|
|
|
|
|
|
if (action.endsWith('-order-cancellation')) return setForm({ type: 'cancellation', kind: action.split('-')[0] });
|
|
|
|
|
|
if (action.startsWith('show-')) {
|
|
|
|
|
|
const kind = action.slice(5);
|
|
|
|
|
|
if (views.orderActionUsesChangeForm(kind)) {
|
|
|
|
|
|
const data = change?.data;
|
|
|
|
|
|
if (!data || change.error) throw new Error('请重新读取订单并核对修改申请。');
|
|
|
|
|
|
const eligibility = data.actions?.[kind] || data;
|
|
|
|
|
|
if (!eligibility.eligible || data.pending || order.cancellationPending)
|
|
|
|
|
|
throw new Error(eligibility.reason || '本单暂不能提交变更申请,请重新读取。');
|
|
|
|
|
|
}
|
|
|
|
|
|
setUi({ action: kind, formKey: current.current });
|
|
|
|
|
|
return setForm({ type: 'action', kind });
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (action === 'order-after-sale-preview')
|
|
|
|
|
|
setUi(old => ({
|
|
|
|
|
|
...old,
|
|
|
|
|
|
preview: null,
|
|
|
|
|
|
previewLoading: false,
|
|
|
|
|
|
verified: false,
|
|
|
|
|
|
stale: [403, 404, 409].includes(err.status),
|
|
|
|
|
|
afterError: messageOf(err),
|
|
|
|
|
|
previewStatus: '预览未完成,请核对提示后重新预览。',
|
|
|
|
|
|
}));
|
|
|
|
|
|
else setError(messageOf(err));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
function onChange(e) {
|
|
|
|
|
|
const target = e.target,
|
|
|
|
|
|
node = target.form;
|
|
|
|
|
|
if (!node) return;
|
|
|
|
|
|
if (form?.type === 'action') {
|
|
|
|
|
|
const values = readForm(node);
|
|
|
|
|
|
if (
|
|
|
|
|
|
target.name === 'date' &&
|
|
|
|
|
|
views.orderActionRequiresApproval(form.kind, ui.date) !==
|
|
|
|
|
|
views.orderActionRequiresApproval(form.kind, values.date) &&
|
|
|
|
|
|
node.elements.verified
|
|
|
|
|
|
)
|
|
|
|
|
|
node.elements.verified.checked = false;
|
|
|
|
|
|
const recipient = ui.paymentMethod === '信用卡' ? ui.recipient : node.elements.payment_recipient?.value;
|
|
|
|
|
|
const showingCard = node.elements.credit_card_fee_payer?.disabled === false;
|
|
|
|
|
|
setUi(old => ({
|
|
|
|
|
|
...old,
|
|
|
|
|
|
action: form.kind,
|
|
|
|
|
|
values,
|
|
|
|
|
|
date: values.date,
|
|
|
|
|
|
paymentMethod: values.payment_method,
|
|
|
|
|
|
recipient,
|
|
|
|
|
|
lastCardBasis: showingCard ? values.amount : old.lastCardBasis,
|
|
|
|
|
|
}));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (form?.type !== 'after') return;
|
|
|
|
|
|
if (target.name === 'verified') {
|
|
|
|
|
|
setUi(old => ({ ...old, verified: target.checked }));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
current.current++;
|
|
|
|
|
|
const data = readForm(node);
|
|
|
|
|
|
let cents = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
cents = order.netReceived - Calc.cents(data.refund_amount);
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
/* 未填写时保留待填写提示。 */
|
|
|
|
|
|
}
|
|
|
|
|
|
if (target.name === 'refund_account')
|
|
|
|
|
|
node.elements.refund_payer.value = views.orderAfterSaleAccountPayer(target.value);
|
|
|
|
|
|
node.elements.verified.checked = false;
|
|
|
|
|
|
const lines = Array.from(node.querySelectorAll('[data-after-sale-line]'), row => ({
|
|
|
|
|
|
pocket: row.querySelector('[name=return_pocket]').value,
|
|
|
|
|
|
disposition: row.querySelector('[name=return_disposition]').value,
|
|
|
|
|
|
}));
|
|
|
|
|
|
setUi(old => ({
|
|
|
|
|
|
...old,
|
|
|
|
|
|
lines,
|
|
|
|
|
|
preview: null,
|
|
|
|
|
|
previewLoading: false,
|
|
|
|
|
|
verified: false,
|
|
|
|
|
|
date: data.date,
|
|
|
|
|
|
previewStatus: '资料已修改,请重新核对售后预览。',
|
|
|
|
|
|
reasonRequired: cents > 0,
|
|
|
|
|
|
retained: cents === null ? '待填写退款金额' : cents < 0 ? '退款超出净实收' : '¥' + (cents / 100).toFixed(2),
|
|
|
|
|
|
}));
|
|
|
|
|
|
}
|
|
|
|
|
|
async function submit(e) {
|
|
|
|
|
|
stop(e);
|
|
|
|
|
|
const node = e.target,
|
|
|
|
|
|
data = readForm(node),
|
|
|
|
|
|
kind = node.dataset.kind;
|
|
|
|
|
|
let submittedDirect = false;
|
2026-12-05 03:21:56 -08:00
|
|
|
|
let submittedKey;
|
|
|
|
|
|
const retryKey = e.nativeEvent.submitter?.dataset.retryKey;
|
2026-12-04 22:43:40 -08:00
|
|
|
|
try {
|
2026-12-05 03:21:56 -08:00
|
|
|
|
if (client.busy) return;
|
|
|
|
|
|
if (retryKey) {
|
|
|
|
|
|
const job = client.pending();
|
|
|
|
|
|
if (!job || job.key !== retryKey || job.key !== ui.pendingKey) return;
|
|
|
|
|
|
submittedKey = job.key;
|
|
|
|
|
|
submittedDirect = job.url === base + '/' + kind;
|
|
|
|
|
|
await client.retryPending();
|
|
|
|
|
|
refreshDetail();
|
|
|
|
|
|
await refreshState();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (client.pending()) return;
|
2026-12-04 22:43:40 -08:00
|
|
|
|
let url, payload;
|
|
|
|
|
|
if (form.type === 'after') {
|
|
|
|
|
|
payload = views.orderAfterSaleInput(node);
|
|
|
|
|
|
if (ui.stale || ui.previewLoading || ui.preview?.signature !== JSON.stringify(payload))
|
|
|
|
|
|
throw new Error('请先重新核对本次售后预览。');
|
|
|
|
|
|
if (change?.data?.pending || order.cancellationPending || views.orderAfterSaleRecord(order))
|
|
|
|
|
|
throw new Error('本单已变动或有待复核申请,请重新读取。');
|
|
|
|
|
|
if (!ui.verified) throw new Error('请核对售后预览,并勾选实物、实际退款和成本确认。');
|
|
|
|
|
|
const approve = views.orderAfterSaleApproval(payload.date);
|
|
|
|
|
|
submittedDirect = !approve;
|
|
|
|
|
|
if (approve) payload.action = 'return';
|
|
|
|
|
|
url = base + (approve ? '/change' : '/return');
|
|
|
|
|
|
} else if (form.type === 'action') {
|
|
|
|
|
|
payload = views.actionInput(node);
|
|
|
|
|
|
url = base + '/' + kind;
|
|
|
|
|
|
if (views.orderActionUsesChangeForm(kind)) {
|
|
|
|
|
|
if (ui.stale || !change?.data) throw new Error('请重新读取订单后核对本次申请。');
|
|
|
|
|
|
const approve = views.orderActionRequiresApproval(kind, payload.date);
|
|
|
|
|
|
payload = {
|
|
|
|
|
|
...payload,
|
|
|
|
|
|
...(approve ? { action: kind } : {}),
|
|
|
|
|
|
revision: order.revision,
|
|
|
|
|
|
verified: data.verified === 'on',
|
|
|
|
|
|
reason: (data.change_reason || data.reason || '').trim(),
|
|
|
|
|
|
};
|
|
|
|
|
|
if (!payload.reason || payload.reason.length > 200)
|
|
|
|
|
|
throw new Error((approve ? '申请原因' : '登记原因') + '请填写1~200字。');
|
|
|
|
|
|
if (!payload.verified) throw new Error('请核实本次金额、付款人或账户、退货去向,并勾选确认。');
|
|
|
|
|
|
submittedDirect = !approve;
|
|
|
|
|
|
if (approve) url = base + '/change';
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const info = { change, ownership, cancellation }[form.type].data,
|
|
|
|
|
|
req = info.pending;
|
|
|
|
|
|
if (kind === 'approve' || kind === 'reject') {
|
|
|
|
|
|
if (!req || req.status !== 'pending' || !info.canApprove) throw new Error('请重新读取后复核。');
|
|
|
|
|
|
if (
|
|
|
|
|
|
kind === 'approve' &&
|
|
|
|
|
|
(!req.canApprove || req.stale || req.order_revision !== order.revision || !data.verified)
|
|
|
|
|
|
)
|
|
|
|
|
|
throw new Error('订单已有变动,请重新读取后复核。');
|
|
|
|
|
|
url = queues.find(([type]) => type === form.type)[1] + '/' + encodeURIComponent(req.id) + '/' + kind;
|
|
|
|
|
|
payload = {
|
|
|
|
|
|
...(kind === 'approve' ? { revision: req.order_revision, verified: true } : {}),
|
|
|
|
|
|
note: data.note.trim(),
|
|
|
|
|
|
};
|
|
|
|
|
|
} else if (form.type === 'ownership') {
|
|
|
|
|
|
payload = { member: data.member, revision: order.revision, reason: data.reason.trim(), verified: true };
|
|
|
|
|
|
url = base + '/ownership';
|
|
|
|
|
|
} else {
|
|
|
|
|
|
payload = {
|
|
|
|
|
|
revision: order.revision,
|
|
|
|
|
|
reason: data.reason.trim(),
|
|
|
|
|
|
...(kind === 'direct' ? { date: info.directActionDate, verified: true } : {}),
|
|
|
|
|
|
};
|
|
|
|
|
|
url = base + (kind === 'direct' ? '/canceltoday' : '/cancellation');
|
|
|
|
|
|
}
|
|
|
|
|
|
if (data.legacyCountVerified) payload.legacyCountVerified = true;
|
|
|
|
|
|
}
|
2026-12-05 03:21:56 -08:00
|
|
|
|
const saving = client.mutate(url, payload);
|
|
|
|
|
|
submittedKey = client.pending()?.key;
|
|
|
|
|
|
await saving;
|
2026-12-04 22:43:40 -08:00
|
|
|
|
refreshDetail();
|
|
|
|
|
|
await refreshState();
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (await access.checkFailure(err)) return;
|
|
|
|
|
|
if (form.type === 'after') {
|
|
|
|
|
|
const forceApproval = err.status === 403 && submittedDirect && !client.pending();
|
|
|
|
|
|
const stale = !forceApproval && [403, 404, 409].includes(err.status);
|
|
|
|
|
|
if (forceApproval || stale) node.elements.verified.checked = false;
|
|
|
|
|
|
setUi(old => ({
|
|
|
|
|
|
...old,
|
|
|
|
|
|
...(forceApproval
|
|
|
|
|
|
? { forceApproval: true, verified: false }
|
|
|
|
|
|
: stale
|
|
|
|
|
|
? { stale: true, preview: null, verified: false }
|
|
|
|
|
|
: {}),
|
|
|
|
|
|
afterError: messageOf(err) + (forceApproval ? ' 输入已保留,重新核实后提交店主复核。' : ''),
|
|
|
|
|
|
}));
|
|
|
|
|
|
} else if (
|
|
|
|
|
|
form.type === 'action' &&
|
|
|
|
|
|
views.orderActionUsesChangeForm(kind) &&
|
2026-12-05 03:21:56 -08:00
|
|
|
|
([403, 404, 409].includes(err.status) || (submittedKey && submittedKey === client.pending()?.key))
|
2026-12-04 22:43:40 -08:00
|
|
|
|
) {
|
|
|
|
|
|
const forceApproval = err.status === 403 && submittedDirect && !client.pending();
|
2026-12-05 03:21:56 -08:00
|
|
|
|
const rejected = [403, 404, 409].includes(err.status);
|
|
|
|
|
|
if (rejected && node.elements.verified) node.elements.verified.checked = false;
|
2026-12-04 22:43:40 -08:00
|
|
|
|
setUi(old => ({
|
|
|
|
|
|
...old,
|
|
|
|
|
|
forceApproval,
|
2026-12-05 03:21:56 -08:00
|
|
|
|
pendingKey: submittedKey,
|
|
|
|
|
|
stale: !forceApproval && rejected,
|
2026-12-04 22:43:40 -08:00
|
|
|
|
actionError: messageOf(err) + (forceApproval ? ' 输入已保留,请重新核实后提交店主审批。' : ''),
|
|
|
|
|
|
}));
|
|
|
|
|
|
} else setError(messageOf(err));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (form?.type === 'after' && (ui.stale || client.pending() || client.busy))
|
|
|
|
|
|
panel = (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<fieldset disabled>{panel}</fieldset>
|
|
|
|
|
|
<button type="button" onClick={refreshDetail}>
|
|
|
|
|
|
重新读取订单
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button type="button" onClick={clearForm} disabled={client.busy}>
|
|
|
|
|
|
收起
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</>
|
|
|
|
|
|
);
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Dialog id="order-dialog" label="关闭订单" close={close}>
|
|
|
|
|
|
<div id="order-content" onClick={onClick} onChange={onChange} onSubmit={submit}>
|
|
|
|
|
|
{details.error ? (
|
|
|
|
|
|
<p role="alert">
|
|
|
|
|
|
{details.error} <button onClick={refreshDetail}>重新读取</button>
|
|
|
|
|
|
</p>
|
|
|
|
|
|
) : !order ? (
|
|
|
|
|
|
'正在读取订单…'
|
|
|
|
|
|
) : (
|
|
|
|
|
|
orderViews({ ...ctx, detailForm: panel, error: visibleError }).renderDetail()
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</Dialog>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function IndependentAfterSale({
|
|
|
|
|
|
client,
|
|
|
|
|
|
state,
|
|
|
|
|
|
session,
|
|
|
|
|
|
sale,
|
|
|
|
|
|
refreshState,
|
|
|
|
|
|
refreshBook,
|
|
|
|
|
|
openList,
|
|
|
|
|
|
close,
|
|
|
|
|
|
saved,
|
|
|
|
|
|
}) {
|
|
|
|
|
|
const { message } = AntApp.useApp();
|
|
|
|
|
|
const [review, setReview] = useState(sale.action === 'after-sale-change-open' ? sale.id : '');
|
|
|
|
|
|
const [kind, setKind] = useState('');
|
|
|
|
|
|
const [error, setError] = useState('');
|
|
|
|
|
|
const [invalid, setInvalid] = useState(false);
|
|
|
|
|
|
const [date, setDate] = useState(null);
|
|
|
|
|
|
const [savedRequest, setSavedRequest] = useState(null);
|
|
|
|
|
|
const [receipt, setReceipt] = useState('');
|
|
|
|
|
|
const [generation, reload] = useState(0);
|
|
|
|
|
|
const version = useRef(0);
|
|
|
|
|
|
useEffect(
|
|
|
|
|
|
() => () => {
|
|
|
|
|
|
version.current++;
|
|
|
|
|
|
},
|
|
|
|
|
|
[],
|
|
|
|
|
|
);
|
|
|
|
|
|
const id = review || sale.id;
|
|
|
|
|
|
const path = review
|
|
|
|
|
|
? '/api/after-sales/changes/' + encodeURIComponent(review)
|
|
|
|
|
|
: sale.id
|
|
|
|
|
|
? '/api/after-sales/' + encodeURIComponent(sale.id)
|
|
|
|
|
|
: '';
|
|
|
|
|
|
const views = orderViews({ state, session });
|
|
|
|
|
|
const read = useRead(
|
|
|
|
|
|
async () => {
|
|
|
|
|
|
if (!review) await refreshState();
|
|
|
|
|
|
const data = await client.api(path);
|
|
|
|
|
|
if (!review) return views.validateAfterSaleDetail(data, sale.id);
|
|
|
|
|
|
const request = views.validateAfterSaleRequest(data);
|
|
|
|
|
|
if (request.id !== review) throw new Error('申请编号不一致,请重新读取。');
|
|
|
|
|
|
return request;
|
|
|
|
|
|
},
|
|
|
|
|
|
path && state.user.id + '/' + path + '/' + generation,
|
|
|
|
|
|
);
|
|
|
|
|
|
const detail = read.value;
|
|
|
|
|
|
const request = review && (savedRequest || detail);
|
|
|
|
|
|
const approval =
|
|
|
|
|
|
!!detail?.permissions &&
|
|
|
|
|
|
(detail.permissions.requiresApproval ||
|
|
|
|
|
|
!detail.permissions.canDirectEdit ||
|
|
|
|
|
|
(state.user.role !== 'owner' &&
|
|
|
|
|
|
(detail.record.date !== state.today || (date ?? detail.record.date) !== state.today)));
|
|
|
|
|
|
const blocked = !!(invalid || client.busy || client.pending());
|
|
|
|
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
|
|
version.current++;
|
|
|
|
|
|
setKind('');
|
|
|
|
|
|
setError('');
|
|
|
|
|
|
setDate(null);
|
|
|
|
|
|
setSavedRequest(null);
|
|
|
|
|
|
setReceipt('');
|
|
|
|
|
|
setInvalid(false);
|
|
|
|
|
|
reload(value => value + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
function dismiss() {
|
|
|
|
|
|
version.current++;
|
|
|
|
|
|
close();
|
|
|
|
|
|
}
|
|
|
|
|
|
function fail(err) {
|
|
|
|
|
|
const text = messageOf(err).replace('不要重新开单', id ? '不要重复提交售后修改' : '不要重复新增售后');
|
|
|
|
|
|
if ([403, 404, 409].includes(err.status) && id) setInvalid(true);
|
|
|
|
|
|
setError(text);
|
|
|
|
|
|
message.error(text);
|
|
|
|
|
|
}
|
|
|
|
|
|
async function finish(url, input, result, at) {
|
|
|
|
|
|
if (client.user !== state.user.id) return;
|
|
|
|
|
|
const active = () => version.current === at && client.user === state.user.id;
|
|
|
|
|
|
if (url === '/api/after-sales') {
|
|
|
|
|
|
if (active()) saved(input.date);
|
|
|
|
|
|
else refreshBook();
|
|
|
|
|
|
message.success('售后已保存,已计入 ' + input.date + ' 的费用与利润');
|
|
|
|
|
|
await refreshState();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const update = url.endsWith('/update');
|
|
|
|
|
|
const applied = update && result.applied === true;
|
|
|
|
|
|
const text = update
|
|
|
|
|
|
? applied
|
|
|
|
|
|
? '售后修改已保存'
|
|
|
|
|
|
: '售后修改申请已提交,待店主审批'
|
|
|
|
|
|
: url.endsWith('/approve')
|
|
|
|
|
|
? '已同意售后修改'
|
|
|
|
|
|
: '已驳回售后修改申请';
|
|
|
|
|
|
if (active()) {
|
|
|
|
|
|
setKind('');
|
|
|
|
|
|
setError('');
|
|
|
|
|
|
setReceipt(text);
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
await refreshState();
|
|
|
|
|
|
if (client.user !== state.user.id) return;
|
|
|
|
|
|
if (active() && applied) saved(result.record?.record?.date || input.date);
|
|
|
|
|
|
else {
|
|
|
|
|
|
refreshBook();
|
|
|
|
|
|
if (active() && update) reset();
|
|
|
|
|
|
if (active() && !update) {
|
|
|
|
|
|
const finalRequest = views.validateAfterSaleRequest(result);
|
|
|
|
|
|
if (finalRequest.id !== review) throw new Error('申请编号不一致,请重新读取。');
|
|
|
|
|
|
setSavedRequest(finalRequest);
|
|
|
|
|
|
setReceipt('');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
message.success(text);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (active()) setError('结果已保存,刷新失败:' + messageOf(err) + '。请重新读取查看结果。');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
async function handle(event) {
|
|
|
|
|
|
const button = event.target.closest('[data-action]');
|
|
|
|
|
|
if (!button) return;
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
const { action, id: requestedId, kind: choice } = button.dataset;
|
|
|
|
|
|
if (action === 'close-after-sale' || action === 'after-sale-changes-close') return dismiss();
|
|
|
|
|
|
if (action === 'after-sale-changes-reload') {
|
|
|
|
|
|
version.current++;
|
|
|
|
|
|
openList();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'after-sale-change-open' || action === 'after-sale-edit') {
|
|
|
|
|
|
if (client.busy) return;
|
|
|
|
|
|
if (action === 'after-sale-change-open') setReview(requestedId);
|
|
|
|
|
|
reset();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'after-sale-review-decision') {
|
|
|
|
|
|
if (invalid || request?.status !== 'pending') return fail(new Error('请重新读取申请后再复核。'));
|
|
|
|
|
|
setKind(choice);
|
|
|
|
|
|
setError('');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'after-sale-review-cancel') {
|
|
|
|
|
|
setKind('');
|
|
|
|
|
|
setError('');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'retry') {
|
|
|
|
|
|
const at = version.current;
|
|
|
|
|
|
try {
|
|
|
|
|
|
const restored = await client.retryPending();
|
|
|
|
|
|
if (restored) await finish(restored.job.url, restored.job.data, restored.result, at);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (at === version.current) fail(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
async function submit(event) {
|
|
|
|
|
|
stop(event);
|
|
|
|
|
|
const node = event.target;
|
|
|
|
|
|
const values = readForm(node);
|
|
|
|
|
|
const at = version.current;
|
|
|
|
|
|
try {
|
|
|
|
|
|
let url, input;
|
|
|
|
|
|
if (review) {
|
|
|
|
|
|
if (state.user.role !== 'owner') throw new Error('请使用店主账号复核。');
|
|
|
|
|
|
if (blocked || request?.status !== 'pending' || node.dataset.id !== request.id || node.dataset.kind !== kind)
|
|
|
|
|
|
throw new Error('请重新读取申请后再复核。');
|
|
|
|
|
|
const note = values.note.trim();
|
|
|
|
|
|
if (kind === 'approve' && (!request.canApprove || request.stale))
|
|
|
|
|
|
throw new Error('原记录已有变动,请重新读取并核对。');
|
|
|
|
|
|
if (kind === 'reject' && !note) throw new Error('请填写驳回意见。');
|
|
|
|
|
|
if (note.length > 200) throw new Error('复核意见最多200字。');
|
|
|
|
|
|
if (kind === 'approve' && !values.verified) throw new Error('请核对修改前后内容并勾选确认。');
|
|
|
|
|
|
url = path + '/' + kind;
|
|
|
|
|
|
input = { revision: request.revision, note, ...(kind === 'approve' ? { verified: true } : {}) };
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (id && (blocked || !detail.permissions.canEdit || detail.pending || node.dataset.id !== id))
|
|
|
|
|
|
throw new Error('请重新读取售后记录后再修改。');
|
|
|
|
|
|
input = {
|
|
|
|
|
|
date: values.date,
|
|
|
|
|
|
project: values.project.trim(),
|
|
|
|
|
|
customer: values.customer.trim(),
|
|
|
|
|
|
amount: values.amount.trim(),
|
|
|
|
|
|
payer: values.payer,
|
|
|
|
|
|
};
|
|
|
|
|
|
if (!views.validOrderDate(input.date) || input.date > state.today)
|
|
|
|
|
|
throw new Error('售后日期请选择今天或之前的有效日期。');
|
|
|
|
|
|
if (!input.project || input.project.length > 100) throw new Error('售后项目请填写1~100字。');
|
|
|
|
|
|
if (!input.customer || input.customer.length > 60) throw new Error('客户请填写1~60字的称呼或代号。');
|
|
|
|
|
|
if (!views.teamMemberKeys().includes(input.payer)) throw new Error('请选择这笔售后由谁支付。');
|
|
|
|
|
|
Calc.cents(input.amount, '售后金额');
|
|
|
|
|
|
if (id) {
|
|
|
|
|
|
const reason = values.reason.trim();
|
|
|
|
|
|
if (reason.length > 200) throw new Error('修改说明最多200字。');
|
|
|
|
|
|
url = path + '/update';
|
|
|
|
|
|
input = { revision: detail.revision, ...input, reason };
|
|
|
|
|
|
} else {
|
|
|
|
|
|
url = '/api/after-sales';
|
|
|
|
|
|
if (views.teamMode()) input.member = values.member || views.currentMember();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
setError('');
|
|
|
|
|
|
const result = await client.mutate(url, input);
|
|
|
|
|
|
await finish(url, input, result, at);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (at !== version.current) return;
|
|
|
|
|
|
if ([403, 404, 409].includes(err.status) && node.elements.verified) node.elements.verified.checked = false;
|
|
|
|
|
|
fail(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
const reread = (
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
disabled={client.busy}
|
|
|
|
|
|
data-action={review ? 'after-sale-change-open' : 'after-sale-edit'}
|
|
|
|
|
|
data-id={id}
|
|
|
|
|
|
>
|
|
|
|
|
|
{review ? '重新读取申请并复核' : '重新读取售后'}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
);
|
|
|
|
|
|
const feedback = error && (
|
|
|
|
|
|
<div className="error" role="alert">
|
|
|
|
|
|
{error}{' '}
|
|
|
|
|
|
{id &&
|
|
|
|
|
|
(client.pending() ? (
|
|
|
|
|
|
<button type="button" data-action="retry" disabled={client.busy}>
|
|
|
|
|
|
恢复原操作
|
|
|
|
|
|
</button>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
reread
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
const ui = { approval, blocked, busy: client.busy, feedback };
|
|
|
|
|
|
const panel = orderViews({ state, session, ui });
|
|
|
|
|
|
let content;
|
|
|
|
|
|
if (receipt || (path && !detail)) {
|
|
|
|
|
|
content = (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<button
|
|
|
|
|
|
className="dialog-close"
|
|
|
|
|
|
data-action={review ? 'after-sale-changes-close' : 'close-after-sale'}
|
|
|
|
|
|
aria-label={review ? '关闭售后修改复核' : '关闭售后记录'}
|
|
|
|
|
|
>
|
|
|
|
|
|
×
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<h2>{receipt || (review ? '售后修改复核' : '修改售后')}</h2>
|
|
|
|
|
|
{read.error || error ? (
|
|
|
|
|
|
<div className="error" role="alert">
|
|
|
|
|
|
{read.error || error} {reread}
|
|
|
|
|
|
{review && <button data-action="after-sale-changes-reload">返回申请列表</button>}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<p role="status">{receipt ? '正在刷新…' : review ? '正在读取申请…' : '正在读取售后记录…'}</p>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</>
|
|
|
|
|
|
);
|
|
|
|
|
|
} else if (review) content = panel.independentReview(request, kind);
|
|
|
|
|
|
else content = id ? panel.editIndependentSale(detail) : panel.newIndependentSale();
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Dialog
|
|
|
|
|
|
id={review ? 'after-sale-changes-dialog' : 'after-sale-dialog'}
|
|
|
|
|
|
ownClose
|
|
|
|
|
|
label={review ? '售后修改复核' : '售后记录'}
|
|
|
|
|
|
close={dismiss}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div
|
|
|
|
|
|
onClick={handle}
|
|
|
|
|
|
onSubmit={submit}
|
|
|
|
|
|
onChange={event => {
|
|
|
|
|
|
if (event.target.name === 'date') setDate(event.target.value);
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
{content}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</Dialog>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|