2026-10-22 17:06:32 +08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
// 已记录订单的更正使用独立草稿和接口,普通开单内容不受影响。
|
|
|
|
|
|
let orderEditEntry = null,
|
|
|
|
|
|
orderEditLoading = false,
|
|
|
|
|
|
orderEditRequest = 0;
|
|
|
|
|
|
const orderEditDraftKey = id => `mm-order-edit-${session.user.id}-${id}`;
|
|
|
|
|
|
const orderEditActiveKey = () => `mm-order-edit-active-${session.user.id}`;
|
|
|
|
|
|
function readOrderEditDraft(id) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const saved = JSON.parse(localStorage.getItem(orderEditDraftKey(id)) || 'null');
|
|
|
|
|
|
return saved?.id === id && Number.isSafeInteger(saved.revision) && saved.inputs && Array.isArray(saved.inputs.lines)
|
|
|
|
|
|
? saved
|
|
|
|
|
|
: null;
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
return null;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
function restoreOrderEditTab() {
|
|
|
|
|
|
const id = localStorage.getItem(orderEditActiveKey());
|
|
|
|
|
|
if (id && readOrderEditDraft(id)) {
|
|
|
|
|
|
tab = 'order-edit';
|
|
|
|
|
|
return id;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderEditForm() {
|
|
|
|
|
|
return $('#order-form[data-order-edit="true"]') || $('#order-edit-meta-form');
|
|
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function priceLockOrderEditable(entry = orderEditEntry) {
|
|
|
|
|
|
return !!(
|
|
|
|
|
|
entry?.financialEditable &&
|
|
|
|
|
|
entry?.priceLock &&
|
|
|
|
|
|
entry.order?.price_lock_id &&
|
|
|
|
|
|
entry.order.status === 'delivered'
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-12-04 02:09:22 +08:00
|
|
|
|
function priceLockCostInput(form, name, fallback = '') {
|
|
|
|
|
|
return form?.querySelector('[name="' + name + '"]' + (name === 'customer_type' ? ':checked' : ''))?.value ?? fallback;
|
|
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function priceLockEditAmount(value) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
return LedgerCalc.cents(String(value), '成本');
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
return String(value ?? '');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 02:22:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
function priceLockOrderEditMeta(entry, index) {
|
|
|
|
|
|
return entry.priceLock.lines?.[index] || entry.priceLock;
|
|
|
|
|
|
}
|
|
|
|
|
|
function priceLockOrderEditMaterial(input, index) {
|
|
|
|
|
|
return input.lines[index].price_lock_material || (input.lines.length === 1 ? input.price_lock_material : null);
|
|
|
|
|
|
}
|
|
|
|
|
|
function priceLockOrderEditExtra(input, index) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
input.lines[index].price_lock_extra_material || (input.lines.length === 1 ? input.price_lock_extra_material : null)
|
|
|
|
|
|
);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 02:22:58 +08:00
|
|
|
|
function collectMultiPriceLockOrderEdit(form, entry) {
|
|
|
|
|
|
const original = entry.inputs,
|
|
|
|
|
|
get = (name, fallback = '') => priceLockCostInput(form, name, fallback),
|
|
|
|
|
|
roots = [...form.querySelectorAll('[data-lock-edit-line]')];
|
|
|
|
|
|
const lines = original.lines.map((line, index) => {
|
|
|
|
|
|
const root = roots.find(row => row.dataset.lineId === line.lineId);
|
|
|
|
|
|
if (!root) throw new Error('商品成本表单未完整读到,请重新读取订单。');
|
|
|
|
|
|
const single = {
|
|
|
|
|
|
...entry,
|
|
|
|
|
|
inputs: {
|
|
|
|
|
|
...original,
|
|
|
|
|
|
lines: [line],
|
|
|
|
|
|
price_lock_material: priceLockOrderEditMaterial(original, index),
|
|
|
|
|
|
price_lock_extra_material: priceLockOrderEditExtra(original, index),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
2026-11-08 02:22:58 +08:00
|
|
|
|
};
|
|
|
|
|
|
const value = collectPriceLockOrderEdit(root, single);
|
|
|
|
|
|
return {
|
|
|
|
|
|
...value.lines[0],
|
|
|
|
|
|
lineId: line.lineId,
|
|
|
|
|
|
price_lock_material: value.price_lock_material,
|
|
|
|
|
|
...(value.price_lock_extra_material ? { price_lock_extra_material: value.price_lock_extra_material } : {}),
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
2026-12-04 02:03:31 +08:00
|
|
|
|
return {
|
|
|
|
|
|
...original,
|
|
|
|
|
|
date: get('date', original.date),
|
|
|
|
|
|
customer: get('customer', original.customer),
|
|
|
|
|
|
customer_type: get('customer_type', original.customer_type || 'unknown'),
|
|
|
|
|
|
salesperson: get('salesperson', original.salesperson),
|
|
|
|
|
|
note: get('note', original.note),
|
|
|
|
|
|
internal_note: get('internal_note', original.internal_note || ''),
|
|
|
|
|
|
fees_date: get('fees_date', original.fees_date),
|
|
|
|
|
|
fee_cost_mode: $('#fee-rows')?.dataset?.feeCostMode || original.fee_cost_mode,
|
|
|
|
|
|
lines,
|
|
|
|
|
|
fee_items: $('#fee-rows') ? readFeeRows() : original.fee_items,
|
|
|
|
|
|
};
|
2026-11-08 02:22:58 +08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function priceLockEditFinancialValue(input) {
|
2026-11-24 21:11:32 +08:00
|
|
|
|
if (input?.lines?.length > 1)
|
|
|
|
|
|
return {
|
|
|
|
|
|
date: input.date,
|
|
|
|
|
|
fees_date: input.fees_date,
|
|
|
|
|
|
fee_cost_mode: input.fee_cost_mode || 'legacy',
|
|
|
|
|
|
lines: input.lines.map((line, index) => ({
|
|
|
|
|
|
lineId: line.lineId,
|
|
|
|
|
|
...priceLockEditFinancialValue({
|
|
|
|
|
|
lines: [line],
|
|
|
|
|
|
price_lock_material: priceLockOrderEditMaterial(input, index),
|
|
|
|
|
|
price_lock_extra_material: priceLockOrderEditExtra(input, index),
|
|
|
|
|
|
fee_items: [],
|
|
|
|
|
|
}),
|
|
|
|
|
|
})),
|
|
|
|
|
|
fees: priceLockEditFinancialValue({ ...input, lines: [] }).fees,
|
|
|
|
|
|
};
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const line = input?.lines?.[0] || {},
|
|
|
|
|
|
material = input?.price_lock_material || {},
|
|
|
|
|
|
extra = input?.price_lock_extra_material;
|
2026-11-30 16:40:30 +08:00
|
|
|
|
return {
|
|
|
|
|
|
date: input?.date,
|
|
|
|
|
|
fees_date: input?.fees_date,
|
|
|
|
|
|
fee_cost_mode: input?.fee_cost_mode || 'legacy',
|
|
|
|
|
|
material:
|
|
|
|
|
|
material.source === 'pool'
|
|
|
|
|
|
? { source: 'pool' }
|
|
|
|
|
|
: { source: material.source, rate: priceLockEditAmount(material.rate), payer: material.payer },
|
|
|
|
|
|
extra: extra
|
2026-12-04 08:58:53 -08:00
|
|
|
|
? {
|
2026-11-30 16:40:30 +08:00
|
|
|
|
...(extra.source === 'pool' ? { source: 'pool' } : {}),
|
|
|
|
|
|
rate: priceLockEditAmount(extra.rate),
|
|
|
|
|
|
payer: extra.payer,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-30 16:40:30 +08:00
|
|
|
|
: null,
|
2026-11-04 22:18:56 +08:00
|
|
|
|
costs: {
|
|
|
|
|
|
labor: priceLockEditAmount(line.costParts?.labor),
|
|
|
|
|
|
accessories: priceLockEditAmount(line.costParts?.accessories),
|
|
|
|
|
|
other: priceLockEditAmount(line.costParts?.other),
|
|
|
|
|
|
payer: line.payer,
|
|
|
|
|
|
laborPayer: line.costPayers?.labor,
|
|
|
|
|
|
laborMode: line.costLabor?.mode || 'total',
|
|
|
|
|
|
...(line.costLabor?.mode === 'gram' ? { laborRate: priceLockEditAmount(line.costLabor.rate) } : {}),
|
|
|
|
|
|
},
|
|
|
|
|
|
fees: (input?.fee_items || [])
|
|
|
|
|
|
.map(fee => ({
|
|
|
|
|
|
key: fee.key,
|
|
|
|
|
|
expense_id: fee.expense_id || '',
|
|
|
|
|
|
date: fee.date || input.fees_date,
|
|
|
|
|
|
note: fee.note,
|
|
|
|
|
|
payer: fee.payer,
|
|
|
|
|
|
amount: priceLockEditAmount(fee.amount),
|
|
|
|
|
|
funding: fee.funding ?? null,
|
|
|
|
|
|
}))
|
|
|
|
|
|
.sort((a, b) => String(a.key).localeCompare(String(b.key))),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
function priceLockEditFinancialChanged(entry, input = entry?.inputs) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
JSON.stringify(priceLockEditFinancialValue(input)) !==
|
|
|
|
|
|
JSON.stringify(priceLockEditFinancialValue(entry?.originalInputs))
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
function collectPriceLockOrderEdit(form, entry = orderEditEntry) {
|
2026-11-08 02:22:58 +08:00
|
|
|
|
if (entry.inputs.lines.length > 1) return collectMultiPriceLockOrderEdit(form, entry);
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const original = entry.inputs,
|
|
|
|
|
|
line = original.lines[0],
|
|
|
|
|
|
get = (name, value = '') => priceLockCostInput(form, name, value),
|
|
|
|
|
|
laborMode = get('lock_cost_labor_mode', line.costLabor?.mode || 'total');
|
|
|
|
|
|
let labor = get('lock_cost_labor_total', line.costParts?.labor ?? '');
|
|
|
|
|
|
if (laborMode === 'gram') {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const cost = LedgerCalc.purchaseCosts({
|
|
|
|
|
|
unit: line.unit,
|
|
|
|
|
|
quantity: line.quantity,
|
|
|
|
|
|
costParts: { material: '0', labor: '0', accessories: '0', other: '0' },
|
|
|
|
|
|
costMaterial: { mode: 'total' },
|
|
|
|
|
|
costLabor: { mode: 'gram', rate: get('lock_cost_labor_rate', line.costLabor?.rate ?? '') },
|
|
|
|
|
|
});
|
|
|
|
|
|
labor = cost.parts.labor === null ? '' : (cost.parts.labor / 100).toFixed(2);
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
labor = '';
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const material = original.price_lock_material || {},
|
|
|
|
|
|
extra = original.price_lock_extra_material;
|
2026-12-04 02:03:31 +08:00
|
|
|
|
return {
|
|
|
|
|
|
...original,
|
|
|
|
|
|
date: get('date', original.date),
|
|
|
|
|
|
customer: get('customer', original.customer),
|
|
|
|
|
|
customer_type: get('customer_type', original.customer_type || 'unknown'),
|
|
|
|
|
|
salesperson: get('salesperson', original.salesperson),
|
|
|
|
|
|
note: get('note', original.note),
|
|
|
|
|
|
internal_note: get('internal_note', original.internal_note || ''),
|
|
|
|
|
|
fees_date: get('fees_date', original.fees_date),
|
2026-11-04 22:18:56 +08:00
|
|
|
|
price_lock_material: {
|
|
|
|
|
|
...material,
|
|
|
|
|
|
source: get('lock_material_source', material.source),
|
|
|
|
|
|
rate: get('lock_material_rate', material.rate),
|
|
|
|
|
|
payer: get('lock_material_payer', material.payer),
|
|
|
|
|
|
verified: form.querySelector('[name="lock_material_verified"]')?.checked ?? material.verified === true,
|
|
|
|
|
|
},
|
2026-11-30 16:40:30 +08:00
|
|
|
|
...(extra
|
2026-12-04 08:58:53 -08:00
|
|
|
|
? {
|
2026-11-30 16:40:30 +08:00
|
|
|
|
price_lock_extra_material: {
|
|
|
|
|
|
...extra,
|
|
|
|
|
|
source: get('lock_extra_source', extra.source || 'manual'),
|
|
|
|
|
|
rate: get('lock_extra_rate', extra.rate),
|
|
|
|
|
|
payer: get('lock_extra_payer', extra.payer),
|
|
|
|
|
|
verified: form.querySelector('[name="lock_extra_verified"]')?.checked ?? extra.verified === true,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
|
|
|
|
|
}
|
2026-11-30 16:40:30 +08:00
|
|
|
|
: {}),
|
2026-11-04 22:18:56 +08:00
|
|
|
|
lines: [
|
2026-12-04 08:58:53 -08:00
|
|
|
|
{
|
2026-11-04 22:18:56 +08:00
|
|
|
|
...line,
|
|
|
|
|
|
payer: get('lock_cost_payer', line.payer),
|
|
|
|
|
|
costLabor: {
|
|
|
|
|
|
mode: laborMode,
|
|
|
|
|
|
...(laborMode === 'gram' ? { rate: get('lock_cost_labor_rate', line.costLabor?.rate ?? '') } : {}),
|
|
|
|
|
|
},
|
|
|
|
|
|
costParts: {
|
|
|
|
|
|
...line.costParts,
|
|
|
|
|
|
labor,
|
|
|
|
|
|
accessories: get('lock_cost_accessories', line.costParts?.accessories ?? '0'),
|
|
|
|
|
|
other: get('lock_cost_other', line.costParts?.other ?? '0'),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
2026-11-04 22:18:56 +08:00
|
|
|
|
costPayers: { ...line.costPayers, labor: get('lock_cost_labor_payer', line.costPayers?.labor) },
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
2026-11-04 22:18:56 +08:00
|
|
|
|
],
|
2026-11-24 21:11:32 +08:00
|
|
|
|
fee_cost_mode: $('#fee-rows')?.dataset?.feeCostMode || original.fee_cost_mode,
|
|
|
|
|
|
fee_items: $('#fee-rows') ? readFeeRows() : original.fee_items,
|
|
|
|
|
|
};
|
2026-11-04 22:18:56 +08:00
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
function collectOrderEditInput() {
|
|
|
|
|
|
const form = orderEditForm();
|
|
|
|
|
|
if (!form || !orderEditEntry) return null;
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (priceLockOrderEditable()) return collectPriceLockOrderEdit(form);
|
2026-10-22 17:06:32 +08:00
|
|
|
|
if (form.id === 'order-form') return collectDraft();
|
|
|
|
|
|
const get = name => form.querySelector(`[name="${name}"]`).value;
|
2026-12-04 02:09:22 +08:00
|
|
|
|
return {
|
|
|
|
|
|
...orderEditEntry.inputs,
|
|
|
|
|
|
...(form.querySelector('[name="customer_type"]:checked')
|
|
|
|
|
|
? { customer_type: form.querySelector('[name="customer_type"]:checked').value }
|
|
|
|
|
|
: {}),
|
|
|
|
|
|
customer: get('customer'),
|
|
|
|
|
|
salesperson: get('salesperson'),
|
|
|
|
|
|
note: get('note'),
|
|
|
|
|
|
...(form.querySelector('[name="internal_note"]') ? { internal_note: get('internal_note') } : {}),
|
|
|
|
|
|
};
|
2026-10-22 17:06:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
function rememberOrderEditDraft(input = collectOrderEditInput()) {
|
|
|
|
|
|
const entry = orderEditEntry,
|
|
|
|
|
|
form = orderEditForm();
|
|
|
|
|
|
if (!entry || !input || entry.user !== session.user.id) return;
|
|
|
|
|
|
entry.inputs = {
|
|
|
|
|
|
...input,
|
|
|
|
|
|
id: entry.id,
|
|
|
|
|
|
revision: entry.revision,
|
|
|
|
|
|
paper_number: entry.order.paper_number || '',
|
|
|
|
|
|
paper_date: entry.order.paper_date || '',
|
|
|
|
|
|
fee_auto_initialized: true,
|
|
|
|
|
|
};
|
|
|
|
|
|
if (form) {
|
|
|
|
|
|
entry.reason = form.querySelector('[name="edit_reason"]').value;
|
|
|
|
|
|
entry.verified = form.querySelector('[name="edit_verified"]').checked;
|
|
|
|
|
|
}
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
|
|
orderEditDraftKey(entry.id),
|
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
|
id: entry.id,
|
|
|
|
|
|
revision: entry.revision,
|
|
|
|
|
|
inputs: entry.inputs,
|
|
|
|
|
|
reason: entry.reason || '',
|
|
|
|
|
|
verified: !!entry.verified,
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
localStorage.setItem(orderEditActiveKey(), entry.id);
|
|
|
|
|
|
}
|
|
|
|
|
|
function leaveOrderEdit() {
|
|
|
|
|
|
if (orderEditEntry) rememberOrderEditDraft();
|
|
|
|
|
|
++orderEditRequest;
|
|
|
|
|
|
orderEditEntry = null;
|
|
|
|
|
|
localStorage.removeItem(orderEditActiveKey());
|
|
|
|
|
|
}
|
|
|
|
|
|
function validateOrderEditResponse(id, result) {
|
|
|
|
|
|
if (result?.eligible !== true) throw new Error(result?.reason || '这张订单暂不能修改,请重新读取订单后核对。');
|
|
|
|
|
|
if (
|
|
|
|
|
|
result.order?.id !== id ||
|
|
|
|
|
|
!Number.isSafeInteger(result.revision) ||
|
|
|
|
|
|
!result.inputs ||
|
|
|
|
|
|
!Array.isArray(result.inputs.lines) ||
|
|
|
|
|
|
!Array.isArray(result.inputs.fee_items)
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-22 17:06:32 +08:00
|
|
|
|
throw new Error('订单修改资料未完整返回,请重新读取。');
|
2026-11-08 02:22:58 +08:00
|
|
|
|
if (result.financialEditable && result.order.price_lock_id) {
|
|
|
|
|
|
const lock = result.priceLock,
|
|
|
|
|
|
lines = result.inputs.lines,
|
|
|
|
|
|
metas = lines.length > 1 ? lock?.lines : [lock],
|
|
|
|
|
|
ids = new Set();
|
|
|
|
|
|
if (
|
|
|
|
|
|
!lock ||
|
|
|
|
|
|
lock.id !== result.order.price_lock_id ||
|
|
|
|
|
|
!validOrderDate(lock.date) ||
|
|
|
|
|
|
typeof lock.financeRequiresApproval !== 'boolean' ||
|
|
|
|
|
|
!Array.isArray(metas) ||
|
|
|
|
|
|
metas.length !== lines.length ||
|
|
|
|
|
|
!lines.length
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-08 02:22:58 +08:00
|
|
|
|
throw new Error('锁价日期与成本修改资料未完整返回,请重新读取。');
|
|
|
|
|
|
for (const [index, line] of lines.entries()) {
|
|
|
|
|
|
const meta = metas[index],
|
|
|
|
|
|
material = priceLockOrderEditMaterial(result.inputs, index),
|
|
|
|
|
|
extra = priceLockOrderEditExtra(result.inputs, index),
|
|
|
|
|
|
id = meta?.lineId || meta?.id;
|
|
|
|
|
|
if (
|
|
|
|
|
|
!meta ||
|
|
|
|
|
|
!['pool', 'manual'].includes(meta.materialSource) ||
|
|
|
|
|
|
!Number.isSafeInteger(meta.extraMg) ||
|
|
|
|
|
|
meta.extraMg < 0 ||
|
|
|
|
|
|
!material ||
|
|
|
|
|
|
!['pool', 'manual'].includes(material.source) ||
|
|
|
|
|
|
(meta.extraMg > 0 && !extra) ||
|
|
|
|
|
|
(lines.length > 1 && (!line.lineId || line.lineId !== id || ids.has(line.lineId)))
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-08 02:22:58 +08:00
|
|
|
|
throw new Error('锁价日期与成本修改资料未完整返回,请重新读取。');
|
|
|
|
|
|
ids.add(line.lineId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function orderEditNeedsApproval(entry, date = entry?.inputs?.date, input = entry?.inputs) {
|
2026-10-23 11:58:19 +08:00
|
|
|
|
if (session.user.role !== 'assistant') return false;
|
2026-11-04 22:18:56 +08:00
|
|
|
|
return (
|
|
|
|
|
|
!entry?.sameDayDirectEdit ||
|
|
|
|
|
|
!entry.directEditDate ||
|
|
|
|
|
|
entry.order.date !== entry.directEditDate ||
|
|
|
|
|
|
date !== entry.directEditDate ||
|
|
|
|
|
|
(priceLockOrderEditable(entry) &&
|
|
|
|
|
|
entry.priceLock.financeRequiresApproval &&
|
|
|
|
|
|
priceLockEditFinancialChanged(entry, input))
|
|
|
|
|
|
);
|
2026-10-23 11:58:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
function updateOrderEditApprovalMode() {
|
|
|
|
|
|
const entry = orderEditEntry;
|
|
|
|
|
|
if (!entry) return;
|
|
|
|
|
|
const form = orderEditForm(),
|
|
|
|
|
|
date = entry.financialEditable
|
|
|
|
|
|
? (form?.querySelector('[name="date"]')?.value ?? entry.inputs.date)
|
|
|
|
|
|
: entry.order.date;
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const input = priceLockOrderEditable(entry) && form ? collectPriceLockOrderEdit(form, entry) : entry.inputs;
|
|
|
|
|
|
entry.requiresApproval = orderEditNeedsApproval(entry, date, input);
|
2026-10-23 11:58:19 +08:00
|
|
|
|
const submit = form?.querySelector('[data-order-edit-submit]');
|
|
|
|
|
|
if (submit) submit.textContent = entry.requiresApproval ? '提交修改申请' : '保存修改';
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const notice = form?.querySelector('[data-order-edit-approval-note]');
|
|
|
|
|
|
if (notice) {
|
|
|
|
|
|
notice.hidden = session.user.role !== 'assistant';
|
|
|
|
|
|
notice.textContent = entry.requiresApproval
|
|
|
|
|
|
? priceLockOrderEditable(entry) &&
|
|
|
|
|
|
entry.priceLock.financeRequiresApproval &&
|
|
|
|
|
|
priceLockEditFinancialChanged(entry, input)
|
|
|
|
|
|
? entry.priceLock.approvalReason || entry.approvalReason || '涉及历史锁价购料或料池,请提交店主复核。'
|
|
|
|
|
|
: '提交的是修改申请。店主复核同意后才生效,填写内容已经保留。'
|
|
|
|
|
|
: '今天的订单可直接保存,改为其他日期须店主复核。';
|
|
|
|
|
|
}
|
2026-10-23 11:58:19 +08:00
|
|
|
|
const title = $('#order-edit-title');
|
|
|
|
|
|
if (title) title.textContent = entry.requiresApproval ? '申请修改订单' : '修改订单';
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const intro = $('#order-edit-save-intro');
|
|
|
|
|
|
if (intro)
|
|
|
|
|
|
intro.textContent = priceLockOrderEditable(entry)
|
|
|
|
|
|
? '更正交付日期与实际成本,原锁日期、销售金价、克重和收付款保留。'
|
|
|
|
|
|
: (entry.requiresApproval ? '店主复核同意后' : '保存后') +
|
|
|
|
|
|
',以本页成交合计作为核实后的客户实收金额,并重新核算成本、费用、库存和利润。本次沿用原单号 ' +
|
|
|
|
|
|
(entry.order.paper_number || entry.order.number) +
|
|
|
|
|
|
'。';
|
2026-10-23 11:58:19 +08:00
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
function useOrderEditResponse(id, result, saved) {
|
|
|
|
|
|
const source = saved?.inputs || result.inputs,
|
|
|
|
|
|
revision = saved?.revision ?? result.revision;
|
2026-11-04 22:18:56 +08:00
|
|
|
|
orderEditEntry = {
|
|
|
|
|
|
id,
|
|
|
|
|
|
user: session.user.id,
|
|
|
|
|
|
revision,
|
|
|
|
|
|
order: result.order,
|
|
|
|
|
|
originalInputs: JSON.parse(JSON.stringify(result.inputs)),
|
|
|
|
|
|
approvalReason: result.approvalReason || '',
|
|
|
|
|
|
paymentAccount: result.inputs.payment_account,
|
|
|
|
|
|
financialEditable: result.financialEditable === true,
|
|
|
|
|
|
directEditDate:
|
|
|
|
|
|
typeof result.directEditDate === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(result.directEditDate)
|
|
|
|
|
|
? result.directEditDate
|
|
|
|
|
|
: null,
|
|
|
|
|
|
sameDayDirectEdit: result.requiresApproval === false,
|
|
|
|
|
|
priceLock: result.priceLock || null,
|
|
|
|
|
|
inputs: {
|
|
|
|
|
|
...JSON.parse(JSON.stringify(source)),
|
|
|
|
|
|
...(result.inputs.price_lock_material
|
|
|
|
|
|
? { price_lock_material: source.price_lock_material || result.inputs.price_lock_material }
|
|
|
|
|
|
: {}),
|
|
|
|
|
|
...(result.inputs.price_lock_extra_material
|
|
|
|
|
|
? { price_lock_extra_material: source.price_lock_extra_material || result.inputs.price_lock_extra_material }
|
|
|
|
|
|
: {}),
|
|
|
|
|
|
internal_note: source.internal_note ?? result.inputs.internal_note ?? '',
|
|
|
|
|
|
id,
|
|
|
|
|
|
revision,
|
|
|
|
|
|
paper_number: result.order.paper_number || '',
|
|
|
|
|
|
paper_date: result.order.paper_date || '',
|
|
|
|
|
|
fee_auto_initialized: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
reason: saved?.reason || '',
|
|
|
|
|
|
verified: !!saved?.verified,
|
2026-10-22 17:06:32 +08:00
|
|
|
|
conflicted: revision !== result.revision,
|
|
|
|
|
|
latest: revision !== result.revision ? result : null,
|
|
|
|
|
|
message: result.reason || result.priceLock?.reason || '',
|
|
|
|
|
|
};
|
|
|
|
|
|
if (orderEditEntry.conflicted) orderEditEntry.verified = false;
|
2026-10-23 11:58:19 +08:00
|
|
|
|
orderEditEntry.requiresApproval = orderEditNeedsApproval(orderEditEntry);
|
2026-10-22 17:06:32 +08:00
|
|
|
|
return orderEditEntry;
|
|
|
|
|
|
}
|
|
|
|
|
|
async function startOrderEdit(id, source, restoring = false) {
|
|
|
|
|
|
if (busy || orderEditLoading || orderCopyLoading) throw new Error('正在处理订单,请稍候再修改。');
|
|
|
|
|
|
if (!restoring && pending()) throw new Error('请先点击顶部“恢复原操作”,确认上次提交结果后再修改订单。');
|
|
|
|
|
|
const user = session.user.id,
|
|
|
|
|
|
originTab = tab,
|
|
|
|
|
|
originDialog = source?.closest('dialog'),
|
|
|
|
|
|
request = ++orderEditRequest;
|
|
|
|
|
|
orderEditLoading = true;
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = validateOrderEditResponse(id, await api('/api/orders/' + encodeURIComponent(id) + '/edit'));
|
|
|
|
|
|
if (
|
|
|
|
|
|
request !== orderEditRequest ||
|
|
|
|
|
|
session.user.id !== user ||
|
|
|
|
|
|
tab !== originTab ||
|
|
|
|
|
|
(source && !source.isConnected) ||
|
|
|
|
|
|
(originDialog && !originDialog.open)
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-22 17:06:32 +08:00
|
|
|
|
return;
|
|
|
|
|
|
if (busy || (!restoring && pending())) throw new Error('已有提交正在处理或结果待确认,请恢复原操作后再修改。');
|
|
|
|
|
|
useOrderEditResponse(id, result, readOrderEditDraft(id));
|
|
|
|
|
|
localStorage.setItem(orderEditActiveKey(), id);
|
|
|
|
|
|
tab = 'order-edit';
|
|
|
|
|
|
currentOrder = null;
|
|
|
|
|
|
$('#order-dialog')?.close();
|
|
|
|
|
|
renderOrderEditForm();
|
|
|
|
|
|
document
|
|
|
|
|
|
.querySelectorAll('nav button')
|
|
|
|
|
|
.forEach(button => button.classList.toggle('active', button.dataset.tab === 'orders'));
|
|
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
|
|
orderEditForm()?.querySelector('[name="customer"]')?.focus();
|
2026-10-26 20:25:31 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (request !== orderEditRequest || session.user.id !== user || tab !== originTab) return;
|
|
|
|
|
|
if (typeof checkOrderOwnershipAfterEditError === 'function' && (await checkOrderOwnershipAfterEditError(err, id)))
|
|
|
|
|
|
return;
|
|
|
|
|
|
throw err;
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
orderEditLoading = false;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
async function renderOrderEdit() {
|
|
|
|
|
|
if (orderEditEntry && orderEditEntry.user === session.user.id) return renderOrderEditForm();
|
|
|
|
|
|
const id = localStorage.getItem(orderEditActiveKey());
|
|
|
|
|
|
if (!id) {
|
|
|
|
|
|
tab = 'orders';
|
|
|
|
|
|
return renderOrders();
|
|
|
|
|
|
}
|
|
|
|
|
|
$('#main').innerHTML =
|
|
|
|
|
|
heading('EDIT ORDER', '修改订单', '正在读取已记录的订单…') +
|
|
|
|
|
|
`<section class="panel"><div id="order-edit-load-status" aria-live="polite">编辑内容已暂存在本机。</div><div class="actions">${button('重新读取订单', 'reload-order-edit', '', `data-id="${e(id)}"`)}${button('返回订单列表', 'leave-order-edit', 'quiet')}</div></section>`;
|
|
|
|
|
|
try {
|
|
|
|
|
|
await startOrderEdit(id, null, true);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (tab === 'order-edit' && $('#order-edit-load-status'))
|
|
|
|
|
|
$('#order-edit-load-status').innerHTML =
|
|
|
|
|
|
`<div class="error">${e(err.name === 'AbortError' ? '订单读取超时,请重试。' : err.message)}</div>`;
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
function orderEditLatestHTML(result) {
|
|
|
|
|
|
if (!result?.order) return '<p>重新读取最新订单后,核对原记录再继续。</p>';
|
|
|
|
|
|
const order = result.order;
|
2026-11-03 01:15:41 +08:00
|
|
|
|
return `<details open><summary>当前已保存的订单</summary><p class="small">${e(order.date)} · ${e(order.customer || '未填客户称呼')} · 开单人 ${e(order.salesperson)} · ${e(accountDisplayName(order.payment_account))} · 成交 ${orderAmount(order.total, order.rounding_mode)}</p><div class="table-wrap"><table><thead><tr><th>商品</th><th>克重/数量</th><th>成交金额</th><th>货品成本</th></tr></thead><tbody>${order.lines.map(line => `<tr><td>${e(line.name)}</td><td>${line.unit === 'g' ? grams(line.quantity) + 'g' : e(line.quantity) + '件'}</td><td>¥${yuan(line.sale)}</td><td>${line.cost == null ? '待补' : '¥' + yuan(line.cost)}</td></tr>`).join('')}</tbody></table></div><p class="small">实际费用 ¥${yuan(order.directCost)};客户单备注:${e(order.note || '无')}</p>${orderInternalNoteHTML(order)}</details>`;
|
2026-10-22 17:06:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
function orderEditConflictHTML(entry) {
|
|
|
|
|
|
if (!entry.conflicted) return '';
|
|
|
|
|
|
return `<section class="panel" id="order-edit-conflict" role="status"><h2>订单已有新记录,请先核对</h2><p>你正在填写的内容已保留。核对下方当前订单与表单后,再决定继续使用哪份内容。</p>${orderEditLatestHTML(entry.latest)}<div class="actions">${entry.latest ? button('已核对,保留草稿继续修改', 'keep-order-edit', '', `data-id="${e(entry.id)}"`) : button('读取最新订单供核对', 'reload-order-edit', '', `data-id="${e(entry.id)}"`)}${button('改用当前订单内容', 'reset-order-edit', 'quiet', `data-id="${e(entry.id)}"`)}</div></section>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderEditReviewHTML(entry) {
|
2026-12-04 02:03:31 +08:00
|
|
|
|
return `<section class="panel"><div class="note" data-order-edit-approval-note ${session.user.role === 'assistant' ? '' : 'hidden'}>${entry.requiresApproval ? '提交的是修改申请。店主复核同意后才生效,待批期间仍按原订单计账。' : '今天的订单可直接保存,改为其他日期须店主复核。'}</div><label>修改原因<textarea name="edit_reason" required maxlength="200" rows="2" placeholder="${priceLockOrderEditable(entry) ? '例如:更正交付日期、进货工费或料费付款人' : entry.financialEditable ? '例如:核对实物后更正克重,或修正收款账户' : '例如:更正客户代号或开单人'}">${e(entry.reason || '')}</textarea></label><label class="check"><input type="checkbox" name="edit_verified" required ${entry.verified ? 'checked' : ''}>${priceLockOrderEditable(entry) ? '已核实交付日期、实际成本与付款人,原锁销售条件和收付款保持不变。' : entry.financialEditable ? '已核实本页成交金额等于客户实际已付金额,收款账户、商品、克重及成本与凭据一致。' : '已核实客户称呼、开单人及备注的本次更正。'}</label><p id="order-edit-save-status" class="small muted" role="status" aria-live="polite" hidden></p><div class="form-actions">${button('取消修改', 'cancel-order-edit', 'quiet')}<button class="primary" type="submit" data-order-edit-submit>${entry.requiresApproval ? '提交修改申请' : '保存修改'}</button></div><div id="order-error" aria-live="polite"></div></section>`;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
|
function orderEditPaymentNote(order) {
|
|
|
|
|
|
const corrected = new Set((order.corrections || []).filter(item => item.type === 'payment').map(item => item.target));
|
|
|
|
|
|
const accounts = new Set(
|
2026-10-22 17:06:32 +08:00
|
|
|
|
(order.payments || [])
|
2026-11-14 01:16:35 +08:00
|
|
|
|
.filter(
|
2026-12-04 08:58:53 -08:00
|
|
|
|
item =>
|
2026-11-14 01:16:35 +08:00
|
|
|
|
item.kind === 'receipt' &&
|
2026-10-22 17:06:32 +08:00
|
|
|
|
item.amount > 0 &&
|
2026-11-14 01:16:35 +08:00
|
|
|
|
!corrected.has(item.id) &&
|
|
|
|
|
|
!String(item.reference || '').startsWith('更正冲回:'),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-14 01:16:35 +08:00
|
|
|
|
.map(item => item.account),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
2026-11-14 01:16:35 +08:00
|
|
|
|
return accounts.size > 1
|
|
|
|
|
|
? '<p class="small muted">原单有多渠道收款。成交金额和收款账户不变时沿用原渠道分布,修改业务日期也会保留;修改金额或账户后,按所选账户及已核实的成交总额更正。</p>'
|
2026-12-04 08:58:53 -08:00
|
|
|
|
: '';
|
|
|
|
|
|
}
|
2026-12-04 02:03:31 +08:00
|
|
|
|
function multiPriceLockOrderEditHTML(entry) {
|
2026-10-23 11:58:19 +08:00
|
|
|
|
const draft = entry.inputs,
|
2026-11-04 22:18:56 +08:00
|
|
|
|
lock = entry.priceLock;
|
2026-12-04 02:03:31 +08:00
|
|
|
|
const cards = draft.lines
|
|
|
|
|
|
.map((line, index) => {
|
2026-11-08 02:22:58 +08:00
|
|
|
|
const meta = priceLockOrderEditMeta(entry, index),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
single = {
|
|
|
|
|
|
...entry,
|
2026-11-14 01:16:35 +08:00
|
|
|
|
inputs: {
|
2026-12-04 08:58:53 -08:00
|
|
|
|
...draft,
|
2026-11-08 02:22:58 +08:00
|
|
|
|
lines: [line],
|
2026-11-14 01:16:35 +08:00
|
|
|
|
price_lock_material: priceLockOrderEditMaterial(draft, index),
|
|
|
|
|
|
price_lock_extra_material: priceLockOrderEditExtra(draft, index),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
2026-11-08 02:22:58 +08:00
|
|
|
|
priceLock: { ...lock, ...meta },
|
|
|
|
|
|
order: { ...entry.order, lines: [entry.order.lines[index]] },
|
2026-12-04 08:58:53 -08:00
|
|
|
|
};
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const html = priceLockOrderEditHTML(single),
|
|
|
|
|
|
start = html.indexOf('<section class="panel internal price-lock-edit-costs">'),
|
|
|
|
|
|
end = html.indexOf('<section class="panel internal"><div class="panel-title"><h2>实际费用');
|
|
|
|
|
|
if (start < 0 || end < 0) throw new Error('商品成本编辑内容未完整生成。');
|
2026-12-04 02:03:31 +08:00
|
|
|
|
const fields = html
|
2026-11-08 02:22:58 +08:00
|
|
|
|
.slice(start, end)
|
2026-11-14 01:16:35 +08:00
|
|
|
|
.replace(/id="lock-edit-manual-material"/g, 'data-lock-edit-part="manual-material"')
|
|
|
|
|
|
.replace(/id="lock-edit-material-note"/g, 'data-lock-edit-part="material-note"');
|
|
|
|
|
|
return `<div class="lock-edit-product" data-lock-edit-line data-line-id="${e(line.lineId)}"><div class="panel lock-edit-product-heading"><h2>商品 ${index + 1} · ${e(line.name)}</h2><p class="small muted">${grams(entry.order.lines[index].quantity)}g · 原锁销售金价 ¥${yuan(entry.order.lines[index].goldPrice ?? 0)}/克 · 货盘 ${e(pocketLabel(entry.order.lines[index].pocket) || '未提供')}</p></div>${fields}</div>`;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
})
|
|
|
|
|
|
.join('');
|
2026-11-14 01:16:35 +08:00
|
|
|
|
return `<div class="note" id="order-edit-save-intro">更正交付日期与逐件实际成本,原锁日期、销售金价、克重和收付款保留。</div>${orderEditConflictHTML(entry)}<form id="order-edit-meta-form" data-price-lock-edit="true"><section class="panel"><div class="panel-title"><h2>交付与客户资料</h2><span class="small muted">原单号 ${e(entry.order.paper_number || entry.order.number)}</span></div><div class="grid three">${field('交付日期', 'date', draft.date, 'date', `required min="${e(lock.date)}" max="${e(state.today)}"`)}${field('客户称呼/代号(必填)', 'customer', draft.customer || '', 'text', 'required maxlength="60"')}${typeof customerTypeField === 'function' ? customerTypeField(draft.customer_type || 'unknown', false) : ''}${field('开单人(必填)', 'salesperson', draft.salesperson || '', 'text', 'required maxlength="30"')}</div><p class="small muted">原锁日期 ${e(lock.date)} · 共 ${draft.lines.length}件 · 成交 ${orderAmount(entry.order.total, entry.order.rounding_mode)}</p><label>客户单备注<textarea name="note" maxlength="240" rows="2">${e(draft.note || '')}</textarea></label>${orderInternalNoteField(draft.internal_note || '')}</section>${cards}${feeHTML(draft, { cardPreview: false })}<section class="panel"><h2>整单更正后成本预览</h2><div id="lock-edit-cost-preview" class="lock-edit-cost-preview" aria-live="polite"></div></section>${orderEditReviewHTML(entry)}</form>`;
|
2026-10-22 17:06:32 +08:00
|
|
|
|
}
|
2026-11-08 02:22:58 +08:00
|
|
|
|
function priceLockOrderEditHTML(entry) {
|
2026-11-08 03:30:58 +08:00
|
|
|
|
if (entry.inputs.lines.length > 1) return multiPriceLockOrderEditHTML(entry);
|
|
|
|
|
|
const draft = entry.inputs,
|
|
|
|
|
|
lock = entry.priceLock,
|
|
|
|
|
|
line = draft.lines[0],
|
|
|
|
|
|
material = draft.price_lock_material,
|
|
|
|
|
|
extra = draft.price_lock_extra_material,
|
|
|
|
|
|
raw = entry.order.lines[0],
|
2026-12-04 02:03:31 +08:00
|
|
|
|
sourceOptions =
|
2026-11-08 03:30:58 +08:00
|
|
|
|
lock.materialSource === 'pool'
|
|
|
|
|
|
? [['pool', '保留原锁料池'], ...(lock.canSwitchToManual ? [['manual', '改为手填原锁料价']] : [])]
|
2026-11-08 02:22:58 +08:00
|
|
|
|
: [['manual', '手填原锁料价']];
|
2026-12-04 02:03:31 +08:00
|
|
|
|
return `<div class="note" id="order-edit-save-intro">更正交付日期与实际成本,原锁日期、销售金价、克重和收付款保留。</div>${orderEditConflictHTML(entry)}<form id="order-edit-meta-form" data-price-lock-edit="true"><section class="panel"><div class="panel-title"><h2>交付与客户资料</h2><span class="small muted">原单号 ${e(entry.order.paper_number || entry.order.number)}</span></div><div class="grid three">${field('交付日期', 'date', draft.date, 'date', `required min="${e(lock.date)}" max="${e(state.today)}"`)}${field('客户称呼/代号(必填)', 'customer', draft.customer || '', 'text', 'required maxlength="60"')}${typeof customerTypeField === 'function' ? customerTypeField(draft.customer_type || 'unknown', false) : ''}${field('开单人(必填)', 'salesperson', draft.salesperson || '', 'text', 'required maxlength="30"')}</div><p class="small muted">原锁日期 ${e(lock.date)} · ${e(raw.name)} · ${grams(raw.quantity)}g · 原锁销售金价 ¥${yuan(raw.goldPrice ?? 0)}/克 · 成交 ${orderAmount(entry.order.total, entry.order.rounding_mode)}</p><label>客户单备注<textarea name="note" maxlength="240" rows="2">${e(draft.note || '')}</textarea></label>${orderInternalNoteField(draft.internal_note || '')}</section><section class="panel internal price-lock-edit-costs"><h2>原锁金料与补重</h2>${choice('原锁料费来源', 'lock_material_source', sourceOptions, material.source)}<p id="lock-edit-material-note" class="note" aria-live="polite"></p><div id="lock-edit-manual-material"><div class="grid two">${moneyField('原锁进货料价(元/克)', 'lock_material_rate', material.rate ?? '')}${choice('原锁料费实际付款人', 'lock_material_payer', [['', '请选择'], ...teamMemberOptions()], material.payer || '')}</div><label class="check"><input type="checkbox" name="lock_material_verified" ${material.verified ? 'checked' : ''}>已核实手填原锁料费和实际付款人。</label></div>${
|
|
|
|
|
|
extra
|
2026-11-04 22:18:56 +08:00
|
|
|
|
? `<div class="lock-edit-extra"><h3>补重金料 ${grams(lock.extraMg)}g</h3>${
|
|
|
|
|
|
raw.priceLockExtraPool
|
2026-12-04 08:58:53 -08:00
|
|
|
|
? choice(
|
2026-12-04 02:03:31 +08:00
|
|
|
|
'补重料费来源',
|
2026-11-08 02:22:58 +08:00
|
|
|
|
'lock_extra_source',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
[
|
2026-12-04 02:03:31 +08:00
|
|
|
|
['pool', '保留交付料池'],
|
2026-11-04 22:18:56 +08:00
|
|
|
|
['manual', '改为手填补重料价'],
|
|
|
|
|
|
],
|
|
|
|
|
|
extra.source,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-12-04 02:03:31 +08:00
|
|
|
|
: ''
|
|
|
|
|
|
}<div class="grid two">${moneyField('补重进货料价(元/克)', 'lock_extra_rate', extra.rate ?? '')}${choice('补重料费实际付款人', 'lock_extra_payer', [['', '请选择'], ...teamMemberOptions()], extra.payer || '')}</div><label class="check"><input type="checkbox" name="lock_extra_verified" ${extra.verified ? 'checked' : ''}>已核实补重料费和实际付款人。</label></div>`
|
|
|
|
|
|
: ''
|
|
|
|
|
|
}</section><section class="panel internal price-lock-edit-costs"><h2>交付进货成本</h2><div class="grid three">${choice(
|
|
|
|
|
|
'进货工费方式',
|
|
|
|
|
|
'lock_cost_labor_mode',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
[
|
2026-12-04 02:03:31 +08:00
|
|
|
|
['total', '总工费'],
|
|
|
|
|
|
['gram', '克工费'],
|
|
|
|
|
|
],
|
|
|
|
|
|
line.costLabor?.mode || 'total',
|
|
|
|
|
|
)}${moneyField('进货克工费(元/克)', 'lock_cost_labor_rate', line.costLabor?.rate ?? '')}${moneyField('进货总工费(元)', 'lock_cost_labor_total', line.costParts?.labor ?? '')}${choice('进货工费付款人', 'lock_cost_labor_payer', [['', '请选择'], ...teamMemberOptions()], line.costPayers?.labor || '')}${moneyField('配件成本(元)', 'lock_cost_accessories', line.costParts?.accessories ?? '0')}${moneyField('其他货品成本(元)', 'lock_cost_other', line.costParts?.other ?? '0')}${choice('配件及其他成本付款人', 'lock_cost_payer', [['', '请选择'], ...teamMemberOptions()], line.payer || '')}</div></section>${feeHTML(draft, { cardPreview: false })}<section class="panel"><h2>更正后成本预览</h2><div id="lock-edit-cost-preview" class="lock-edit-cost-preview" aria-live="polite"></div><p class="small muted">按本页成本预估,保存或复核同意后按原账更新利润。</p></section>${orderEditReviewHTML(entry)}</form>`;
|
2026-11-04 22:18:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
function priceLockEditGramCost(mg, rate) {
|
|
|
|
|
|
return LedgerCalc.purchaseCosts({
|
|
|
|
|
|
unit: 'g',
|
|
|
|
|
|
quantity: grams(mg),
|
|
|
|
|
|
costParts: { material: '0', labor: '0', accessories: '0', other: '0' },
|
|
|
|
|
|
costMaterial: { mode: 'gram', rate },
|
|
|
|
|
|
costLabor: { mode: 'total' },
|
|
|
|
|
|
}).parts.material;
|
|
|
|
|
|
}
|
|
|
|
|
|
function priceLockOrderEditPreview(input, entry = orderEditEntry) {
|
2026-11-08 02:22:58 +08:00
|
|
|
|
if (input.lines.length > 1) {
|
|
|
|
|
|
const costs = {
|
|
|
|
|
|
material: 0,
|
|
|
|
|
|
labor: 0,
|
|
|
|
|
|
accessories: 0,
|
|
|
|
|
|
other: 0,
|
|
|
|
|
|
fees: input.fee_items.reduce((sum, fee) => sum + LedgerCalc.cents(fee.amount, '实际费用'), 0),
|
|
|
|
|
|
};
|
|
|
|
|
|
input.lines.forEach((line, index) => {
|
|
|
|
|
|
const old = entry.originalInputs,
|
|
|
|
|
|
meta = priceLockOrderEditMeta(entry, index),
|
|
|
|
|
|
singleInput = {
|
|
|
|
|
|
...input,
|
|
|
|
|
|
lines: [line],
|
|
|
|
|
|
price_lock_material: priceLockOrderEditMaterial(input, index),
|
|
|
|
|
|
price_lock_extra_material: priceLockOrderEditExtra(input, index),
|
|
|
|
|
|
fee_items: [],
|
|
|
|
|
|
},
|
|
|
|
|
|
singleEntry = {
|
|
|
|
|
|
...entry,
|
|
|
|
|
|
originalInputs: {
|
|
|
|
|
|
...old,
|
|
|
|
|
|
lines: [old.lines[index]],
|
|
|
|
|
|
price_lock_material: priceLockOrderEditMaterial(old, index),
|
|
|
|
|
|
price_lock_extra_material: priceLockOrderEditExtra(old, index),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
2026-11-08 02:22:58 +08:00
|
|
|
|
priceLock: { ...entry.priceLock, ...meta },
|
|
|
|
|
|
order: { ...entry.order, lines: [entry.order.lines[index]] },
|
|
|
|
|
|
};
|
|
|
|
|
|
const value = priceLockOrderEditPreview(singleInput, singleEntry);
|
|
|
|
|
|
for (const key of ['material', 'labor', 'accessories', 'other']) costs[key] += value[key];
|
|
|
|
|
|
});
|
|
|
|
|
|
costs.total = Object.values(costs).reduce((sum, value) => sum + value, 0);
|
|
|
|
|
|
costs.profit = entry.order.total - costs.total;
|
|
|
|
|
|
return costs;
|
|
|
|
|
|
}
|
2026-11-30 16:40:30 +08:00
|
|
|
|
const original = entry.originalInputs,
|
|
|
|
|
|
line = input.lines[0],
|
|
|
|
|
|
extraMg = entry.priceLock.extraMg,
|
|
|
|
|
|
usedMg = entry.order.lines[0].quantity - extraMg,
|
|
|
|
|
|
oldExtra = extraMg
|
|
|
|
|
|
? original.price_lock_extra_material.source === 'pool'
|
|
|
|
|
|
? original.price_lock_extra_material.amount
|
|
|
|
|
|
: priceLockEditGramCost(extraMg, original.price_lock_extra_material.rate)
|
|
|
|
|
|
: 0;
|
|
|
|
|
|
const originalMaterial =
|
|
|
|
|
|
input.price_lock_material.source === 'pool'
|
|
|
|
|
|
? LedgerCalc.cents(original.lines[0].costParts.material, '原料费') - oldExtra
|
|
|
|
|
|
: priceLockEditGramCost(usedMg, input.price_lock_material.rate),
|
|
|
|
|
|
extra = extraMg
|
|
|
|
|
|
? input.price_lock_extra_material.source === 'pool'
|
|
|
|
|
|
? original.price_lock_extra_material.amount
|
|
|
|
|
|
: priceLockEditGramCost(extraMg, input.price_lock_extra_material.rate)
|
2026-12-04 08:58:53 -08:00
|
|
|
|
: 0;
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (!Number.isSafeInteger(originalMaterial) || originalMaterial < 0 || !Number.isSafeInteger(extra))
|
|
|
|
|
|
throw new Error('原锁料费尚未完整,请核对原料价与补重料价。');
|
|
|
|
|
|
const costs = {
|
|
|
|
|
|
material: originalMaterial + extra,
|
|
|
|
|
|
labor: LedgerCalc.cents(line.costParts.labor, '进货工费'),
|
|
|
|
|
|
accessories: LedgerCalc.cents(line.costParts.accessories, '配件成本'),
|
|
|
|
|
|
other: LedgerCalc.cents(line.costParts.other, '其他货品成本'),
|
|
|
|
|
|
fees: input.fee_items.reduce((total, fee) => total + LedgerCalc.cents(fee.amount, '实际费用'), 0),
|
|
|
|
|
|
};
|
|
|
|
|
|
costs.total = Object.values(costs).reduce((total, amount) => total + amount, 0);
|
|
|
|
|
|
costs.profit = entry.order.total - costs.total;
|
|
|
|
|
|
return costs;
|
|
|
|
|
|
}
|
2026-11-08 02:22:58 +08:00
|
|
|
|
function syncMultiPriceLockOrderEdit(changed = '', target = null) {
|
2026-11-30 16:40:30 +08:00
|
|
|
|
syncLockExtraEditControls(orderEditForm());
|
2026-11-08 02:22:58 +08:00
|
|
|
|
const entry = orderEditEntry,
|
|
|
|
|
|
form = orderEditForm(),
|
|
|
|
|
|
roots = [...form.querySelectorAll('[data-lock-edit-line]')];
|
|
|
|
|
|
roots.forEach((root, index) => {
|
|
|
|
|
|
const meta = priceLockOrderEditMeta(entry, index),
|
|
|
|
|
|
get = name => root.querySelector('[name="' + name + '"]'),
|
|
|
|
|
|
manual = get('lock_material_source').value === 'manual',
|
|
|
|
|
|
affected = !target || target.closest('[data-lock-edit-line]') === root;
|
|
|
|
|
|
if (affected && changed === 'lock_material_source' && manual && meta.materialSource === 'pool')
|
|
|
|
|
|
get('lock_material_payer').value = '';
|
|
|
|
|
|
if (affected && ['lock_material_source', 'lock_material_rate', 'lock_material_payer'].includes(changed))
|
|
|
|
|
|
get('lock_material_verified').checked = false;
|
|
|
|
|
|
if (affected && ['lock_extra_rate', 'lock_extra_payer'].includes(changed) && get('lock_extra_verified'))
|
|
|
|
|
|
get('lock_extra_verified').checked = false;
|
|
|
|
|
|
const manualRoot = root.querySelector('[data-lock-edit-part="manual-material"]');
|
|
|
|
|
|
manualRoot.hidden = !manual;
|
|
|
|
|
|
manualRoot.querySelectorAll('input,select').forEach(control => {
|
|
|
|
|
|
control.disabled = !manual;
|
|
|
|
|
|
control.required = manual;
|
|
|
|
|
|
});
|
|
|
|
|
|
const note = root.querySelector('[data-lock-edit-part="material-note"]');
|
|
|
|
|
|
note.textContent =
|
|
|
|
|
|
manual && meta.materialSource === 'pool'
|
|
|
|
|
|
? '改为手填后,释放本件原锁料池占用。请重新确认原锁料价与实际付款人。'
|
|
|
|
|
|
: manual
|
|
|
|
|
|
? '按核实后的本件原锁料价与付款人更正成本,原锁日期保持不变。'
|
|
|
|
|
|
: '保留本件原锁 ' + entry.priceLock.date + ' 的原料来源与成本。';
|
|
|
|
|
|
const gram = get('lock_cost_labor_mode').value === 'gram';
|
|
|
|
|
|
for (const [name, hidden] of [
|
|
|
|
|
|
['lock_cost_labor_rate', !gram],
|
|
|
|
|
|
['lock_cost_labor_total', false],
|
|
|
|
|
|
]) {
|
|
|
|
|
|
const control = get(name);
|
|
|
|
|
|
control.closest('label').hidden = hidden;
|
|
|
|
|
|
control.required = !hidden;
|
|
|
|
|
|
control.disabled = hidden;
|
|
|
|
|
|
if (name === 'lock_cost_labor_total') control.readOnly = gram;
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
});
|
2026-11-08 02:22:58 +08:00
|
|
|
|
if (changed && form.querySelector('[name="edit_verified"]'))
|
|
|
|
|
|
form.querySelector('[name="edit_verified"]').checked = false;
|
|
|
|
|
|
const input = collectMultiPriceLockOrderEdit(form, entry);
|
|
|
|
|
|
roots.forEach((root, index) => {
|
|
|
|
|
|
if (input.lines[index].costLabor.mode === 'gram')
|
|
|
|
|
|
root.querySelector('[name="lock_cost_labor_total"]').value = input.lines[index].costParts.labor;
|
|
|
|
|
|
});
|
|
|
|
|
|
const preview = $('#lock-edit-cost-preview');
|
|
|
|
|
|
if (preview) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const costs = priceLockOrderEditPreview(input);
|
|
|
|
|
|
preview.innerHTML = [
|
|
|
|
|
|
['料费', costs.material],
|
|
|
|
|
|
['进货工费', costs.labor],
|
|
|
|
|
|
['配件及其他', costs.accessories + costs.other],
|
|
|
|
|
|
['实际费用', costs.fees],
|
|
|
|
|
|
['成本合计', costs.total],
|
|
|
|
|
|
['利润预览', costs.profit],
|
2026-12-04 08:58:53 -08:00
|
|
|
|
]
|
2026-11-08 02:22:58 +08:00
|
|
|
|
.map(([name, amount]) => `<span>${e(name)} <strong>¥${yuan(amount)}</strong></span>`)
|
|
|
|
|
|
.join('');
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
preview.textContent = err.message;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-08 02:22:58 +08:00
|
|
|
|
updateOrderEditApprovalMode();
|
|
|
|
|
|
}
|
|
|
|
|
|
function syncPriceLockOrderEdit(changed = '', target = null) {
|
2026-11-30 16:40:30 +08:00
|
|
|
|
syncLockExtraEditControls(orderEditForm());
|
2026-11-08 02:22:58 +08:00
|
|
|
|
if (priceLockOrderEditable() && orderEditEntry.inputs.lines.length > 1)
|
|
|
|
|
|
return syncMultiPriceLockOrderEdit(changed, target);
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (!priceLockOrderEditable()) return;
|
|
|
|
|
|
const form = orderEditForm();
|
|
|
|
|
|
if (!form || form.dataset.priceLockEdit !== 'true') return;
|
|
|
|
|
|
const get = name => form.querySelector('[name="' + name + '"]'),
|
|
|
|
|
|
source = get('lock_material_source')?.value || orderEditEntry.inputs.price_lock_material.source,
|
|
|
|
|
|
manual = source === 'manual';
|
|
|
|
|
|
if (
|
|
|
|
|
|
changed === 'lock_material_source' &&
|
|
|
|
|
|
manual &&
|
|
|
|
|
|
orderEditEntry.priceLock.materialSource === 'pool' &&
|
|
|
|
|
|
get('lock_material_payer')
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
get('lock_material_payer').value = '';
|
|
|
|
|
|
if (
|
|
|
|
|
|
['lock_material_source', 'lock_material_rate', 'lock_material_payer'].includes(changed) &&
|
|
|
|
|
|
get('lock_material_verified')
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
get('lock_material_verified').checked = false;
|
|
|
|
|
|
if (['lock_extra_rate', 'lock_extra_payer'].includes(changed) && get('lock_extra_verified'))
|
|
|
|
|
|
get('lock_extra_verified').checked = false;
|
|
|
|
|
|
if (changed && get('edit_verified')) get('edit_verified').checked = false;
|
|
|
|
|
|
const manualRoot = $('#lock-edit-manual-material');
|
|
|
|
|
|
if (manualRoot) {
|
|
|
|
|
|
manualRoot.hidden = !manual;
|
|
|
|
|
|
manualRoot.querySelectorAll('input,select').forEach(control => {
|
|
|
|
|
|
control.disabled = !manual;
|
|
|
|
|
|
control.required = manual;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
const materialNote = $('#lock-edit-material-note');
|
|
|
|
|
|
if (materialNote)
|
|
|
|
|
|
materialNote.textContent =
|
|
|
|
|
|
manual && orderEditEntry.priceLock.materialSource === 'pool'
|
|
|
|
|
|
? '改为手填后,释放原锁料池占用。请重新确认原锁料价与实际付款人。'
|
|
|
|
|
|
: manual
|
|
|
|
|
|
? '按核实后的原锁料价与付款人更正成本,原锁日期保持不变。'
|
|
|
|
|
|
: '保留原锁 ' + orderEditEntry.priceLock.date + ' 的原料来源与成本;只改交付日期不会改用新日期料池。';
|
|
|
|
|
|
const sources =
|
|
|
|
|
|
orderEditEntry.order.lines[0].priceLockPool?.materialSources ||
|
|
|
|
|
|
orderEditEntry.order.lines[0].materialPool?.materialSources;
|
|
|
|
|
|
if (!manual && materialNote && sources?.length && typeof materialSourceSegmentsText === 'function')
|
|
|
|
|
|
materialNote.textContent += ' ' + materialSourceSegmentsText(sources) + '。';
|
|
|
|
|
|
const gram = get('lock_cost_labor_mode')?.value === 'gram';
|
|
|
|
|
|
for (const [name, hidden] of [
|
|
|
|
|
|
['lock_cost_labor_rate', !gram],
|
|
|
|
|
|
['lock_cost_labor_total', false],
|
|
|
|
|
|
]) {
|
|
|
|
|
|
const control = get(name);
|
|
|
|
|
|
if (control) {
|
|
|
|
|
|
const label = control.closest?.('label');
|
|
|
|
|
|
if (label) label.hidden = hidden;
|
|
|
|
|
|
control.required = !hidden;
|
|
|
|
|
|
control.disabled = hidden;
|
|
|
|
|
|
if (name === 'lock_cost_labor_total') control.readOnly = gram;
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const input = collectPriceLockOrderEdit(form);
|
|
|
|
|
|
if (gram && get('lock_cost_labor_total')) get('lock_cost_labor_total').value = input.lines[0].costParts.labor;
|
|
|
|
|
|
const preview = $('#lock-edit-cost-preview');
|
|
|
|
|
|
if (preview) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const costs = priceLockOrderEditPreview(input);
|
|
|
|
|
|
preview.innerHTML = [
|
|
|
|
|
|
['料费', costs.material],
|
|
|
|
|
|
['进货工费', costs.labor],
|
|
|
|
|
|
['配件及其他', costs.accessories + costs.other],
|
|
|
|
|
|
['实际费用', costs.fees],
|
|
|
|
|
|
['成本合计', costs.total],
|
|
|
|
|
|
['利润预览', costs.profit],
|
2026-12-04 08:58:53 -08:00
|
|
|
|
]
|
2026-11-04 22:18:56 +08:00
|
|
|
|
.map(([name, amount]) => `<span>${e(name)} <strong>¥${yuan(amount)}</strong></span>`)
|
|
|
|
|
|
.join('');
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
preview.textContent = err.message;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
updateOrderEditApprovalMode();
|
|
|
|
|
|
}
|
|
|
|
|
|
function validatePriceLockOrderEdit(input, entry = orderEditEntry) {
|
2026-11-08 02:22:58 +08:00
|
|
|
|
if (input.lines.length > 1) {
|
|
|
|
|
|
if (input.lines.length !== entry.order.lines.length) throw new Error('商品成本资料未完整读到,请重新读取订单。');
|
|
|
|
|
|
input.lines.forEach((line, index) => {
|
|
|
|
|
|
const meta = priceLockOrderEditMeta(entry, index);
|
|
|
|
|
|
if (line.lineId !== (meta.lineId || meta.id)) throw new Error('锁价商品身份已变化,请重新读取订单。');
|
|
|
|
|
|
validatePriceLockOrderEdit(
|
2026-12-04 08:58:53 -08:00
|
|
|
|
{
|
2026-11-08 02:22:58 +08:00
|
|
|
|
...input,
|
|
|
|
|
|
lines: [line],
|
|
|
|
|
|
price_lock_material: priceLockOrderEditMaterial(input, index),
|
|
|
|
|
|
price_lock_extra_material: priceLockOrderEditExtra(input, index),
|
|
|
|
|
|
fee_items: [],
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
2026-11-08 02:22:58 +08:00
|
|
|
|
{ ...entry, priceLock: { ...entry.priceLock, ...meta } },
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
2026-11-14 01:16:35 +08:00
|
|
|
|
for (const fee of input.fee_items) {
|
|
|
|
|
|
LedgerCalc.cents(fee.amount, '实际费用');
|
|
|
|
|
|
if (!teamMemberKeys().includes(fee.payer)) throw new Error('请选择每笔实际费用的付款人。');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
|
return {
|
|
|
|
|
|
...input,
|
|
|
|
|
|
...validateOrderIdentity({
|
|
|
|
|
|
customer: input.customer,
|
|
|
|
|
|
salesperson: input.salesperson,
|
|
|
|
|
|
note: input.note,
|
|
|
|
|
|
internal_note: input.internal_note,
|
|
|
|
|
|
}),
|
|
|
|
|
|
};
|
2026-11-08 02:22:58 +08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (!validOrderDate(input.date) || input.date < entry.priceLock.date || input.date > state.today)
|
|
|
|
|
|
throw new Error('交付日期请选择原锁日期至今天之间的有效日期。');
|
|
|
|
|
|
const material = input.price_lock_material,
|
|
|
|
|
|
extra = input.price_lock_extra_material,
|
|
|
|
|
|
line = input.lines[0];
|
|
|
|
|
|
if (
|
|
|
|
|
|
!material ||
|
|
|
|
|
|
!['pool', 'manual'].includes(material.source) ||
|
|
|
|
|
|
(material.source === 'pool' && entry.priceLock.materialSource !== 'pool') ||
|
2026-12-04 02:03:31 +08:00
|
|
|
|
(material.source === 'manual' && entry.priceLock.materialSource === 'pool' && !entry.priceLock.canSwitchToManual)
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-12-04 02:03:31 +08:00
|
|
|
|
throw new Error('原锁料费来源已变化,请重新读取锁价单。');
|
|
|
|
|
|
if (material.source === 'manual') {
|
|
|
|
|
|
LedgerCalc.cents(material.rate, '原锁进货料价');
|
|
|
|
|
|
if (!teamMemberKeys().includes(material.payer)) throw new Error('请选择原锁料费的实际付款人。');
|
|
|
|
|
|
if (material.verified !== true) throw new Error('请核实手填原锁料费和实际付款人,并勾选确认。');
|
2026-10-22 17:06:32 +08:00
|
|
|
|
}
|
2026-11-30 15:40:40 +08:00
|
|
|
|
if (entry.priceLock.extraMg > 0) {
|
|
|
|
|
|
if (!extra) throw new Error('请带入补重料费。');
|
|
|
|
|
|
LedgerCalc.cents(extra.rate, '补重进货料价');
|
|
|
|
|
|
if (!teamMemberKeys().includes(extra.payer)) throw new Error('请选择补重料费的实际付款人。');
|
2026-11-14 01:16:35 +08:00
|
|
|
|
if (extra.verified !== true) throw new Error('请核实补重料费和实际付款人,并勾选确认。');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-30 15:40:40 +08:00
|
|
|
|
if (!teamMemberKeys().includes(line.payer) || !teamMemberKeys().includes(line.costPayers?.labor))
|
2026-11-14 01:16:35 +08:00
|
|
|
|
throw new Error('请选择进货工费、配件及其他成本的实际付款人。');
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (!['gram', 'total'].includes(line.costLabor?.mode)) throw new Error('请选择进货工费方式。');
|
|
|
|
|
|
if (line.costLabor.mode === 'gram') LedgerCalc.cents(line.costLabor.rate, '进货克工费');
|
|
|
|
|
|
for (const [key, name] of [
|
|
|
|
|
|
['labor', '进货总工费'],
|
|
|
|
|
|
['accessories', '配件成本'],
|
|
|
|
|
|
['other', '其他货品成本'],
|
2026-12-04 08:58:53 -08:00
|
|
|
|
])
|
2026-11-04 22:18:56 +08:00
|
|
|
|
LedgerCalc.cents(line.costParts[key], name);
|
2026-11-14 01:16:35 +08:00
|
|
|
|
for (const fee of input.fee_items) {
|
|
|
|
|
|
LedgerCalc.cents(fee.amount, '实际费用');
|
|
|
|
|
|
if (!teamMemberKeys().includes(fee.payer)) throw new Error('请选择每笔实际费用的付款人。');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
|
|
|
...input,
|
2026-12-04 02:09:22 +08:00
|
|
|
|
...validateOrderIdentity({
|
2026-12-04 02:03:31 +08:00
|
|
|
|
customer: input.customer,
|
2026-11-04 22:18:56 +08:00
|
|
|
|
salesperson: input.salesperson,
|
2026-12-04 02:03:31 +08:00
|
|
|
|
note: input.note,
|
|
|
|
|
|
internal_note: input.internal_note,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
function renderOrderEditForm() {
|
|
|
|
|
|
const entry = orderEditEntry;
|
2026-10-23 11:58:19 +08:00
|
|
|
|
if (!entry) return;
|
2026-11-30 15:40:40 +08:00
|
|
|
|
entry.requiresApproval = orderEditNeedsApproval(entry);
|
2026-10-23 11:58:19 +08:00
|
|
|
|
const draft = entry.inputs,
|
2026-11-04 22:18:56 +08:00
|
|
|
|
order = entry.order;
|
2026-10-22 17:06:32 +08:00
|
|
|
|
const identity = `${e(order.paper_number || order.number)} · ${e(label[order.status] || order.status)}`;
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const intro = entry.financialEditable
|
|
|
|
|
|
? `${entry.requiresApproval ? '店主复核同意后' : '保存后'},以本页成交合计作为核实后的客户实收金额,并重新核算成本、费用、库存和利润。`
|
|
|
|
|
|
: '修改这张订单的客户称呼、开单人、客户单备注和内部备注。';
|
2026-10-23 11:58:19 +08:00
|
|
|
|
const headingHTML = heading(
|
|
|
|
|
|
'EDIT ORDER',
|
|
|
|
|
|
`<span id="order-edit-title">${entry.requiresApproval ? '申请修改订单' : '修改订单'}</span>`,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
identity,
|
2026-10-23 11:58:19 +08:00
|
|
|
|
button('取消修改', 'cancel-order-edit', 'quiet'),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (priceLockOrderEditable(entry)) {
|
|
|
|
|
|
$('#main').innerHTML = headingHTML + priceLockOrderEditHTML(entry);
|
2026-11-30 15:40:40 +08:00
|
|
|
|
syncPriceLockOrderEdit();
|
2026-11-04 22:18:56 +08:00
|
|
|
|
} else if (!entry.financialEditable) {
|
2026-12-04 02:03:31 +08:00
|
|
|
|
$('#main').innerHTML =
|
|
|
|
|
|
headingHTML +
|
2026-11-30 15:40:40 +08:00
|
|
|
|
`<div class="note">${e(entry.message || intro)}</div>` +
|
2026-12-04 02:03:31 +08:00
|
|
|
|
orderEditConflictHTML(entry) +
|
|
|
|
|
|
`<form id="order-edit-meta-form"><section class="panel"><div class="grid two">${field('客户称呼/代号(必填)', 'customer', draft.customer || '', 'text', 'required maxlength="60"')}${typeof customerTypeField === 'function' ? customerTypeField(draft.customer_type || 'unknown', false) : ''}${field('开单人(必填)', 'salesperson', draft.salesperson || '', 'text', 'required maxlength="30"')}<label>客户单备注<textarea name="note" maxlength="240" rows="3">${e(draft.note || '')}</textarea></label></div>${orderInternalNoteField(draft.internal_note || '')}<p class="small muted">原单号 ${e(order.paper_number || order.number)} · 业务日期 ${e(order.date)} · 成交 ${orderAmount(order.total, order.rounding_mode)}</p></section>${orderEditReviewHTML(entry)}</form>`;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
} else {
|
2026-12-04 02:03:31 +08:00
|
|
|
|
$('#main').innerHTML =
|
|
|
|
|
|
headingHTML +
|
|
|
|
|
|
`<div class="note" role="status" id="order-edit-save-intro">${e(intro)}本次沿用原单号 ${e(order.paper_number || order.number)}。</div>` +
|
|
|
|
|
|
orderEditConflictHTML(entry) +
|
2026-11-30 15:40:40 +08:00
|
|
|
|
`<form id="order-form" data-order-edit="true"><input type="hidden" name="rounding_mode" value="${e(draft.rounding_mode || 'cent')}"><input type="hidden" name="orderId" value="${e(entry.id)}"><input type="hidden" name="revision" value="${e(entry.revision)}"><section class="panel"><div class="panel-title"><h2>客户与单据</h2><span class="small muted">修改内容单独暂存,可刷新后继续</span></div><div class="grid four">${dateField('date', draft.date)}${field('原纸单编号', 'paper_number', order.paper_number || order.number, 'text', 'readonly')}${field('客户称呼/代号(必填)', 'customer', draft.customer || '', 'text', 'required maxlength="60"')}${typeof customerTypeField === 'function' ? customerTypeField(draft.customer_type || 'unknown', false) : ''}${field('开单人(必填)', 'salesperson', draft.salesperson || '', 'text', 'required maxlength="30"')}${accountFields(draft.payment_account)}</div><p class="small muted">选择客户实际付款的方式与收款人。修改业务日期会重新核算对应日期的订单与库存,原单号保留。</p></section><section class="panel"><div class="panel-title"><h2>商品明细</h2><span class="small muted">金价与工费分开填写</span></div><div id="lines">${draft.lines.map((line, index) => lineHTML(line, index + 1, { materialPool: true, historicalPocket: !!order.lines[index] && line.pocket === order.lines[index].pocket })).join('')}</div><div class="form-actions">${button('+ 添加商品', 'add-line')}<span class="small muted">填写本单最终实际交付的商品、克重与金额。</span></div></section>${feeHTML(draft)}<section class="panel"><div class="grid two">${moneyField('整单优惠/抹零(元)', 'discount', draft.discount ?? '0')}<label>客户单备注<textarea name="note" maxlength="240" rows="2">${e(draft.note || '')}</textarea></label></div>${orderInternalNoteField(draft.internal_note || '')}<div class="form-actions"><div><span class="small muted">更正后成交合计/客户实收</span><div id="create-total" class="summary-amount">待填写</div><div id="create-rounding-note" class="small muted order-rounding-note" aria-live="polite"></div></div></div></section>${orderEditReviewHTML(entry)}</form>`;
|
2026-10-22 17:06:32 +08:00
|
|
|
|
const paymentNote = orderEditPaymentNote(order);
|
|
|
|
|
|
if (paymentNote)
|
|
|
|
|
|
$('#order-form [name="payment_recipient"]').closest('.grid').insertAdjacentHTML('afterend', paymentNote);
|
2026-10-25 14:19:24 +08:00
|
|
|
|
updateTotal();
|
2026-11-30 15:40:40 +08:00
|
|
|
|
loadMaterialPoolChoices(true);
|
2026-10-25 14:19:24 +08:00
|
|
|
|
if (typeof loadReimbursementFundingOptions === 'function') loadReimbursementFundingOptions(true);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-30 15:40:40 +08:00
|
|
|
|
rememberOrderEditDraft();
|
|
|
|
|
|
applyOrderEditRestrictions();
|
|
|
|
|
|
loadOrderNoteTags();
|
2026-10-22 17:06:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
function applyOrderEditRestrictions() {
|
2026-10-23 11:58:19 +08:00
|
|
|
|
updateOrderEditApprovalMode();
|
2026-10-22 17:06:32 +08:00
|
|
|
|
const form = orderEditForm();
|
|
|
|
|
|
if (!form) return;
|
2026-10-23 09:49:36 +08:00
|
|
|
|
if (pending())
|
|
|
|
|
|
form.querySelectorAll('input,select,textarea,button').forEach(control => {
|
|
|
|
|
|
if (!['cancel-order-edit', 'retry'].includes(control.dataset.action)) control.disabled = true;
|
|
|
|
|
|
});
|
|
|
|
|
|
form.querySelectorAll('button[data-action="retry"],button[data-action="reload-order-edit"]').forEach(control => {
|
|
|
|
|
|
if (control.dataset?.action)
|
|
|
|
|
|
control.disabled = busy || (control.dataset.action === 'reload-order-edit' && !!pending());
|
2026-12-04 08:58:53 -08:00
|
|
|
|
});
|
2026-10-22 17:06:32 +08:00
|
|
|
|
const submit = form.querySelector('[data-order-edit-submit]');
|
|
|
|
|
|
if (submit) submit.disabled = busy || !!pending() || !!orderEditEntry?.conflicted;
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderEditPoolUsedMg(pool) {
|
|
|
|
|
|
if (!orderEditEntry || tab !== 'order-edit' || orderEditEntry.order.status !== 'delivered') return pool.usedMg;
|
|
|
|
|
|
const own = orderEditEntry.order.lines.reduce(
|
|
|
|
|
|
(sum, line) =>
|
|
|
|
|
|
sum + (line.costMaterial?.mode === 'pool' && line.costMaterial.poolId === pool.id ? line.quantity : 0),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
0,
|
2026-10-22 17:06:32 +08:00
|
|
|
|
);
|
|
|
|
|
|
return Math.max(0, pool.usedMg - own);
|
|
|
|
|
|
}
|
|
|
|
|
|
async function reloadOrderEdit(id, mode = 'review') {
|
|
|
|
|
|
if (busy || orderEditLoading || pending()) throw new Error('请先恢复原操作,再读取订单修改资料。');
|
|
|
|
|
|
if (!orderEditEntry) return startOrderEdit(id, null, true);
|
|
|
|
|
|
rememberOrderEditDraft();
|
|
|
|
|
|
const entry = orderEditEntry,
|
|
|
|
|
|
user = session.user.id;
|
|
|
|
|
|
if (mode === 'reset' && !window.confirm('改用当前已保存的订单内容?这张订单尚未保存的修改草稿将被替换。')) return;
|
|
|
|
|
|
orderEditLoading = true;
|
|
|
|
|
|
const request = ++orderEditRequest;
|
|
|
|
|
|
try {
|
|
|
|
|
|
const latest = validateOrderEditResponse(id, await api('/api/orders/' + encodeURIComponent(id) + '/edit'));
|
|
|
|
|
|
if (request !== orderEditRequest || orderEditEntry !== entry || session.user.id !== user || tab !== 'order-edit')
|
|
|
|
|
|
return;
|
|
|
|
|
|
if (mode === 'reset') useOrderEditResponse(id, latest, null);
|
|
|
|
|
|
else if (mode === 'keep' && entry.latest?.revision === latest.revision) {
|
2026-11-04 22:18:56 +08:00
|
|
|
|
entry.revision = latest.revision;
|
|
|
|
|
|
entry.order = latest.order;
|
|
|
|
|
|
entry.paymentAccount = latest.inputs.payment_account;
|
|
|
|
|
|
entry.financialEditable = latest.financialEditable === true;
|
|
|
|
|
|
entry.directEditDate = latest.directEditDate || null;
|
|
|
|
|
|
entry.sameDayDirectEdit = latest.requiresApproval === false;
|
|
|
|
|
|
entry.priceLock = latest.priceLock || null;
|
|
|
|
|
|
entry.originalInputs = JSON.parse(JSON.stringify(latest.inputs));
|
|
|
|
|
|
entry.approvalReason = latest.approvalReason || '';
|
|
|
|
|
|
entry.message = latest.reason || latest.priceLock?.reason || '';
|
|
|
|
|
|
entry.conflicted = false;
|
|
|
|
|
|
entry.latest = null;
|
|
|
|
|
|
entry.verified = false;
|
2026-10-22 17:06:32 +08:00
|
|
|
|
entry.inputs.revision = entry.revision;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
entry.latest = latest;
|
|
|
|
|
|
entry.conflicted = true;
|
|
|
|
|
|
entry.verified = false;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
renderOrderEditForm();
|
2026-10-26 20:25:31 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (request !== orderEditRequest || orderEditEntry !== entry || session.user.id !== user || tab !== 'order-edit')
|
|
|
|
|
|
return;
|
|
|
|
|
|
if (typeof checkOrderOwnershipAfterEditError === 'function' && (await checkOrderOwnershipAfterEditError(err, id)))
|
|
|
|
|
|
return;
|
|
|
|
|
|
throw err;
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
orderEditLoading = false;
|
2026-10-22 17:06:32 +08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
async function cancelOrderEdit() {
|
|
|
|
|
|
const id = orderEditEntry?.id || localStorage.getItem(orderEditActiveKey());
|
|
|
|
|
|
leaveOrderEdit();
|
|
|
|
|
|
tab = 'orders';
|
|
|
|
|
|
await refresh();
|
|
|
|
|
|
if (id) await openOrder(id);
|
|
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
async function orderEditError(err, id = orderEditEntry?.id) {
|
|
|
|
|
|
if (typeof checkOrderOwnershipAfterEditError === 'function' && (await checkOrderOwnershipAfterEditError(err, id)))
|
|
|
|
|
|
return;
|
2026-10-23 11:58:19 +08:00
|
|
|
|
if (err.status === 403 && session.user.role === 'assistant' && orderEditEntry?.id === id) {
|
|
|
|
|
|
orderEditEntry.sameDayDirectEdit = false;
|
|
|
|
|
|
orderEditEntry.verified = false;
|
|
|
|
|
|
const checked = orderEditForm()?.querySelector('[name="edit_verified"]');
|
|
|
|
|
|
if (checked) checked.checked = false;
|
|
|
|
|
|
err.message += '。填写内容已保留,请重新核对并勾选后提交修改申请。';
|
|
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
err.message = err.message.replace('不要重新开单', '不要重复保存修改');
|
|
|
|
|
|
toast(err.message, true);
|
|
|
|
|
|
if (orderEditEntry?.id !== id) return;
|
2026-10-23 09:49:36 +08:00
|
|
|
|
rememberOrderEditDraft();
|
|
|
|
|
|
if (err.status === 409) {
|
|
|
|
|
|
orderEditEntry.conflicted = true;
|
|
|
|
|
|
orderEditEntry.latest = null;
|
|
|
|
|
|
orderEditEntry.verified = false;
|
|
|
|
|
|
const checked = orderEditForm()?.querySelector('[name="edit_verified"]');
|
|
|
|
|
|
if (checked) checked.checked = false;
|
|
|
|
|
|
rememberOrderEditDraft();
|
|
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
const form = orderEditForm();
|
|
|
|
|
|
if (form && !pending()) {
|
|
|
|
|
|
form.querySelectorAll('input,select,textarea,button').forEach(control => (control.disabled = false));
|
|
|
|
|
|
if (form.id === 'order-form') updateTotal();
|
|
|
|
|
|
}
|
|
|
|
|
|
const target = $('#order-error');
|
|
|
|
|
|
if (target)
|
|
|
|
|
|
target.innerHTML = `<div class="error">${e(err.message)}${err.status === 409 ? ' ' + button('读取最新订单供核对', 'reload-order-edit', '', `data-id="${e(id)}"`) : pending() ? ' ' + button('恢复原操作', 'retry', 'danger') : ''}</div>`;
|
|
|
|
|
|
applyOrderEditRestrictions();
|
|
|
|
|
|
}
|
|
|
|
|
|
async function submitOrderEdit(form) {
|
|
|
|
|
|
const entry = orderEditEntry;
|
|
|
|
|
|
if (!entry || entry.user !== session.user.id || form !== orderEditForm())
|
|
|
|
|
|
throw new Error('请重新打开这张订单,再核对修改。');
|
|
|
|
|
|
rememberOrderEditDraft();
|
|
|
|
|
|
if (entry.conflicted) throw new Error('订单已有新记录,请先读取最新订单并核对,填写内容已经保留。');
|
|
|
|
|
|
const reason = form.querySelector('[name="edit_reason"]').value.trim(),
|
|
|
|
|
|
verified = form.querySelector('[name="edit_verified"]').checked;
|
|
|
|
|
|
if (!reason || reason.length > 200) throw new Error('请填写1~200字的修改原因。');
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (!verified)
|
|
|
|
|
|
throw new Error(
|
|
|
|
|
|
priceLockOrderEditable(entry)
|
|
|
|
|
|
? '请先核实交付日期、实际成本及付款人,并勾选确认。'
|
|
|
|
|
|
: entry.financialEditable
|
|
|
|
|
|
? '请先核实修改后成交金额等于客户实际已付金额,并勾选确认。'
|
|
|
|
|
|
: '请先核实本次资料更正,并勾选确认。',
|
|
|
|
|
|
);
|
2026-10-22 17:06:32 +08:00
|
|
|
|
let input;
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (priceLockOrderEditable(entry)) input = validatePriceLockOrderEdit(collectPriceLockOrderEdit(form), entry);
|
|
|
|
|
|
else if (entry.financialEditable) {
|
|
|
|
|
|
updateTotal();
|
|
|
|
|
|
try {
|
|
|
|
|
|
input = validateIssueDraft(collectDraft());
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
err.message = err.message.replace('开单前', '保存修改前');
|
|
|
|
|
|
throw err;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-12-04 02:09:22 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
const get = name => form.querySelector(`[name="${name}"]`).value;
|
|
|
|
|
|
input = {
|
|
|
|
|
|
...(form.querySelector('[name="customer_type"]')
|
|
|
|
|
|
? { customer_type: form.querySelector('[name="customer_type"]:checked')?.value || 'unknown' }
|
|
|
|
|
|
: {}),
|
|
|
|
|
|
...validateOrderIdentity({
|
|
|
|
|
|
customer: get('customer'),
|
|
|
|
|
|
salesperson: get('salesperson'),
|
|
|
|
|
|
note: get('note'),
|
|
|
|
|
|
...(form.querySelector('[name="internal_note"]') ? { internal_note: get('internal_note') } : {}),
|
|
|
|
|
|
}),
|
|
|
|
|
|
};
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
input = { ...input, revision: entry.revision, reason, verified: true };
|
|
|
|
|
|
$('#order-error').innerHTML = '';
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const requiresApproval = orderEditNeedsApproval(entry, input.date ?? entry.order.date, input);
|
|
|
|
|
|
entry.requiresApproval = requiresApproval;
|
2026-10-22 17:06:32 +08:00
|
|
|
|
try {
|
|
|
|
|
|
form.querySelectorAll('input,select,textarea').forEach(control => (control.disabled = true));
|
2026-10-23 11:58:19 +08:00
|
|
|
|
const url = '/api/orders/' + encodeURIComponent(entry.id) + (requiresApproval ? '/change' : '/edit');
|
|
|
|
|
|
const result = await mutate(url, input);
|
|
|
|
|
|
return requiresApproval
|
|
|
|
|
|
? await finishOrderChangeSave(url, input, result)
|
|
|
|
|
|
: await finishOrderEditSave(input, result, entry.id);
|
2026-10-26 20:25:31 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
return await orderEditError(err, entry.id);
|
2026-10-22 17:06:32 +08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
async function finishOrderEditSave(input, result, id) {
|
|
|
|
|
|
const orderId = result.id || id;
|
|
|
|
|
|
localStorage.removeItem(orderEditDraftKey(orderId));
|
|
|
|
|
|
if (localStorage.getItem(orderEditActiveKey()) === orderId) localStorage.removeItem(orderEditActiveKey());
|
|
|
|
|
|
++orderEditRequest;
|
|
|
|
|
|
orderEditEntry = null;
|
|
|
|
|
|
currentOrder = null;
|
|
|
|
|
|
$('#order-dialog')?.close();
|
|
|
|
|
|
invalidateMaterialPools();
|
|
|
|
|
|
invalidateReport();
|
|
|
|
|
|
invalidatePriceLockViews();
|
|
|
|
|
|
inventoryPage = null;
|
|
|
|
|
|
++orderBookRequest;
|
|
|
|
|
|
tab = 'orders';
|
|
|
|
|
|
const date = result.order?.date || input.date;
|
|
|
|
|
|
if (date) {
|
|
|
|
|
|
const filters = getOrderBookFilters();
|
|
|
|
|
|
Object.assign(filters, { mode: 'date', date, q: '', status: '' });
|
|
|
|
|
|
saveOrderBookFilters();
|
|
|
|
|
|
}
|
2026-11-02 18:55:10 +08:00
|
|
|
|
$('#main').innerHTML =
|
|
|
|
|
|
heading('ORDER UPDATED', '订单修改已保存', '已更新原订单,原单号保留。') +
|
|
|
|
|
|
`<section class="panel"><p id="order-edit-saved-status" aria-live="polite">修改已经保存,可以查看修改后的订单。</p><div class="actions">${button('查看修改后的订单', 'view-saved-order-edit', 'primary', `data-id="${e(orderId)}"`)}${button('重新读取订单', 'refresh')}</div></section>`;
|
2026-10-22 17:06:32 +08:00
|
|
|
|
document
|
|
|
|
|
|
.querySelectorAll('nav button')
|
|
|
|
|
|
.forEach(button => button.classList.toggle('active', button.dataset.tab === tab));
|
2026-11-02 18:55:10 +08:00
|
|
|
|
toast('修改已保存,原单号保留');
|
|
|
|
|
|
// POST 回执已经确认保存;后台更新状态不再串行等待列表与详情,也不覆盖成功页。
|
|
|
|
|
|
const status = $('#order-edit-saved-status'),
|
|
|
|
|
|
user = session.user.id,
|
|
|
|
|
|
request = orderEditRequest;
|
|
|
|
|
|
const active = () =>
|
|
|
|
|
|
session?.user?.id === user &&
|
|
|
|
|
|
request === orderEditRequest &&
|
|
|
|
|
|
tab === 'orders' &&
|
|
|
|
|
|
$('#order-edit-saved-status') === status;
|
|
|
|
|
|
void refresh(false, active).catch(() => {
|
|
|
|
|
|
if (active() && status) status.textContent = '订单修改已经保存,最新资料暂未读到。请重新读取原订单查看。';
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
async function viewSavedOrderEdit(id, source) {
|
|
|
|
|
|
const status = $('#order-edit-saved-status');
|
|
|
|
|
|
if (!status || busy || pending()) return;
|
|
|
|
|
|
const user = session?.user?.id,
|
|
|
|
|
|
request = ++orderEditRequest;
|
|
|
|
|
|
let element = status,
|
|
|
|
|
|
bookRequest = null;
|
|
|
|
|
|
const active = () =>
|
|
|
|
|
|
session?.user?.id === user &&
|
|
|
|
|
|
request === orderEditRequest &&
|
|
|
|
|
|
tab === 'orders' &&
|
|
|
|
|
|
(bookRequest === null
|
|
|
|
|
|
? $('#order-edit-saved-status') === element
|
|
|
|
|
|
: $('#order-list') === element && orderBookRequest === bookRequest);
|
|
|
|
|
|
source.disabled = true;
|
|
|
|
|
|
status.textContent = '订单修改已经保存,正在读取订单列表与详情…';
|
|
|
|
|
|
try {
|
|
|
|
|
|
await refresh(false, active);
|
|
|
|
|
|
if (!active()) return;
|
|
|
|
|
|
const reading = renderOrders();
|
|
|
|
|
|
element = $('#order-list');
|
|
|
|
|
|
bookRequest = orderBookRequest;
|
|
|
|
|
|
await reading;
|
|
|
|
|
|
if (!active()) return;
|
|
|
|
|
|
const list = orderBookDetailList;
|
|
|
|
|
|
if (!list || list.request !== bookRequest) throw new Error('订单列表暂未读到,请重新读取。');
|
|
|
|
|
|
const index = list.ids.indexOf(id),
|
|
|
|
|
|
opened = await openOrder(id, { navigation: index < 0 ? null : { list, index } });
|
|
|
|
|
|
if (opened !== false && index < 0) {
|
|
|
|
|
|
orderDetailListChanged = true;
|
|
|
|
|
|
updateOrderDetailNavigation();
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (!active()) return;
|
|
|
|
|
|
const message =
|
|
|
|
|
|
'订单修改已经保存,读取失败:' + (err.name === 'AbortError' ? '连接超时,请重新读取。' : err.message);
|
|
|
|
|
|
if ($('#order-edit-saved-status') === status) status.textContent = message;
|
|
|
|
|
|
toast(message, true);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
source.disabled = false;
|
2026-10-22 17:06:32 +08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 17:06:32 +08:00
|
|
|
|
function rememberOrderEditChange(event) {
|
|
|
|
|
|
const form = event.target.closest('#order-form[data-order-edit="true"],#order-edit-meta-form');
|
|
|
|
|
|
if (!form) return;
|
|
|
|
|
|
if (!['edit_verified', 'edit_reason'].includes(event.target.name))
|
|
|
|
|
|
form.querySelector('[name="edit_verified"]').checked = false;
|
2026-11-08 02:22:58 +08:00
|
|
|
|
if (priceLockOrderEditable()) {
|
|
|
|
|
|
syncPriceLockOrderEdit(
|
|
|
|
|
|
['edit_verified', 'edit_reason'].includes(event.target.name) ? '' : event.target.name,
|
|
|
|
|
|
event.target,
|
|
|
|
|
|
);
|
|
|
|
|
|
rememberOrderEditDraft();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-10-23 11:58:19 +08:00
|
|
|
|
if (event.target.name === 'date') updateOrderEditApprovalMode();
|
2026-10-22 17:06:32 +08:00
|
|
|
|
// 完整表单由原开单联动统一暂存;资料表单单独保存。
|
|
|
|
|
|
if (form.id === 'order-edit-meta-form') rememberOrderEditDraft();
|
|
|
|
|
|
}
|
|
|
|
|
|
document.addEventListener('input', rememberOrderEditChange);
|
|
|
|
|
|
document.addEventListener('change', rememberOrderEditChange);
|
2026-11-30 16:40:30 +08:00
|
|
|
|
|
|
|
|
|
|
function syncLockExtraEditControls(form) {
|
|
|
|
|
|
for (const select of form?.querySelectorAll('[name="lock_extra_source"]') || []) {
|
|
|
|
|
|
const area = select.closest('.lock-edit-extra'),
|
|
|
|
|
|
pooled = select.value === 'pool';
|
|
|
|
|
|
for (const name of ['lock_extra_rate', 'lock_extra_payer', 'lock_extra_verified']) {
|
|
|
|
|
|
const control = area.querySelector('[name="' + name + '"]');
|
|
|
|
|
|
if (control) {
|
|
|
|
|
|
control.disabled = pooled;
|
|
|
|
|
|
control.closest('label').hidden = pooled;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-30 16:40:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
let note = area.querySelector('[data-extra-pool-note]');
|
|
|
|
|
|
if (!note) {
|
|
|
|
|
|
note = document.createElement('p');
|
|
|
|
|
|
note.dataset.extraPoolNote = 'true';
|
|
|
|
|
|
note.className = 'note';
|
|
|
|
|
|
area.append(note);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-30 16:40:30 +08:00
|
|
|
|
note.hidden = !pooled;
|
|
|
|
|
|
note.textContent = '沿用交付料池的成本和付款来源,不重复登记付款;更改交付日期时按对应日期料池重新核算。';
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-30 16:40:30 +08:00
|
|
|
|
}
|