2026-11-04 22:18:56 +08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
// 已保存回收/置换使用独立草稿;原开单草稿、身份和业务类型保持各自入口。
|
|
|
|
|
|
let recoveryEditEntry = null,
|
|
|
|
|
|
recoveryEditRequest = 0,
|
|
|
|
|
|
recoveryDetailEdit = null,
|
|
|
|
|
|
recoveryReviewEntry = null,
|
|
|
|
|
|
recoveryReviewRequest = 0,
|
|
|
|
|
|
recoveryChangesStatus = 'pending',
|
|
|
|
|
|
recoveryChangeDialogBound = null;
|
|
|
|
|
|
const recoveryEditDraftKey = id => `mm-recovery-edit-${recoveryUser()}-${id}`;
|
|
|
|
|
|
const recoveryChangeUser = () => (typeof orderDetailUser === 'function' ? orderDetailUser() : recoveryUser());
|
|
|
|
|
|
function clearRecoveryEditDetail() {
|
|
|
|
|
|
recoveryDetailEdit = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
function invalidateRecoveryChangeViews() {
|
|
|
|
|
|
++recoveryEditRequest;
|
|
|
|
|
|
++recoveryReviewRequest;
|
|
|
|
|
|
recoveryDetailEdit = null;
|
|
|
|
|
|
recoveryEditEntry = null;
|
|
|
|
|
|
recoveryReviewEntry = null;
|
|
|
|
|
|
$('#recovery-change-dialog')?.close();
|
|
|
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
|
function recoveryInputShape(input) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
input &&
|
|
|
|
|
|
Object.hasOwn(recoveryModeNames, input.mode) &&
|
|
|
|
|
|
validOrderDate(input.date) &&
|
|
|
|
|
|
typeof input.customer === 'string' &&
|
|
|
|
|
|
typeof input.salesperson === 'string' &&
|
|
|
|
|
|
teamMemberKeys().includes(input.member) &&
|
|
|
|
|
|
Array.isArray(input.old_lines) &&
|
|
|
|
|
|
input.old_lines.length > 0 &&
|
|
|
|
|
|
input.old_lines.every(
|
|
|
|
|
|
row => row && ['name', 'incoming_weight', 'melted_weight', 'purity'].every(key => typeof row[key] === 'string'),
|
|
|
|
|
|
) &&
|
|
|
|
|
|
(input.mode === 'direct' ||
|
|
|
|
|
|
(input.exchange && Array.isArray(input.exchange.charges) && Array.isArray(input.exchange.fee_items)))
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function validateRecoveryEditDetail(data, id) {
|
|
|
|
|
|
if (
|
|
|
|
|
|
!data ||
|
|
|
|
|
|
data.id !== id ||
|
|
|
|
|
|
data.record?.id !== id ||
|
|
|
|
|
|
!Number.isSafeInteger(data.revision) ||
|
|
|
|
|
|
data.revision < 1 ||
|
|
|
|
|
|
!['active', 'cancelled'].includes(data.status) ||
|
|
|
|
|
|
!recoveryInputShape(data.inputs) ||
|
|
|
|
|
|
!data.permissions ||
|
|
|
|
|
|
!['canEdit', 'canCancel', 'canDirect', 'requiresApproval'].every(
|
|
|
|
|
|
key => typeof data.permissions[key] === 'boolean',
|
|
|
|
|
|
) ||
|
|
|
|
|
|
!Array.isArray(data.blockedUsages) ||
|
|
|
|
|
|
!Array.isArray(data.history) ||
|
|
|
|
|
|
(state.user.role !== 'owner' && data.inputs.member !== currentMember())
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
throw new Error('回收更正资料未完整返回或不属于当前成员,请重新读取。');
|
|
|
|
|
|
if (data.record.member !== data.inputs.member || data.record.mode !== data.inputs.mode)
|
|
|
|
|
|
throw new Error('回收记录身份与修改资料不一致。');
|
|
|
|
|
|
if (data.pending && validateRecoveryChangeRequest(data.pending).recoveryId !== id)
|
|
|
|
|
|
throw new Error('回收申请不属于当前记录。');
|
|
|
|
|
|
if (data.history.some(row => validateRecoveryChangeRequest(row).recoveryId !== id))
|
|
|
|
|
|
throw new Error('回收更正历史不属于当前记录。');
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
function validateRecoveryChangeRequest(request) {
|
|
|
|
|
|
if (
|
|
|
|
|
|
!request ||
|
|
|
|
|
|
typeof request.id !== 'string' ||
|
|
|
|
|
|
!request.id ||
|
|
|
|
|
|
typeof request.recoveryId !== 'string' ||
|
|
|
|
|
|
!Number.isSafeInteger(request.revision) ||
|
|
|
|
|
|
request.revision < 1 ||
|
|
|
|
|
|
!Number.isSafeInteger(request.recoveryRevision) ||
|
|
|
|
|
|
request.recoveryRevision < 1 ||
|
|
|
|
|
|
!['update', 'cancel'].includes(request.action) ||
|
|
|
|
|
|
!['pending', 'approved', 'rejected'].includes(request.status) ||
|
|
|
|
|
|
!recoveryInputShape(request.before) ||
|
|
|
|
|
|
(request.action === 'update' && !recoveryInputShape(request.after)) ||
|
|
|
|
|
|
(request.action === 'cancel' && request.after !== null) ||
|
|
|
|
|
|
typeof request.canApprove !== 'boolean' ||
|
|
|
|
|
|
!request.summary ||
|
|
|
|
|
|
request.member !== request.before.member ||
|
|
|
|
|
|
(request.after && (request.after.member !== request.member || request.after.mode !== request.before.mode)) ||
|
|
|
|
|
|
(state.user.role !== 'owner' && request.member !== currentMember())
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
throw new Error('回收更正申请未完整返回或不属于当前成员,请重新读取。');
|
|
|
|
|
|
return request;
|
|
|
|
|
|
}
|
|
|
|
|
|
function recoveryDetailChangeActionsHTML(value) {
|
|
|
|
|
|
const info = recoveryDetailEdit;
|
|
|
|
|
|
if (!info || info.id !== value.id) return '';
|
|
|
|
|
|
return (
|
|
|
|
|
|
(info.permissions.canEdit ? button('修改', 'recovery-edit-open', '', `data-id="${e(value.id)}"`) : '') +
|
|
|
|
|
|
(info.permissions.canCancel
|
|
|
|
|
|
? button('取消这笔记录', 'recovery-edit-cancel', '', 'data-id="' + e(value.id) + '"')
|
|
|
|
|
|
: '')
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
function recoveryMaterialBatchHTML(batch) {
|
|
|
|
|
|
return batch
|
|
|
|
|
|
? `<div class="note"><strong>${e(teamMemberName(batch.member))}个人回收专用料</strong><p class="small">原回收 ${grams(batch.mg)}g · 已用/锁价预留 ${grams(batch.usedMg)}g · 剩余 ${grams(batch.remainingMg)}g。仅供本人所属订单先用,已有占用须先核对关联订单或锁价。</p></div>`
|
|
|
|
|
|
: '';
|
|
|
|
|
|
}
|
2026-11-05 00:04:01 +08:00
|
|
|
|
function recoveryCancellationMaterialHTML() {
|
|
|
|
|
|
return '<div class="note"><strong>取消后的关联用料</strong><p class="small">取消生效时,关联订单和锁价预留的用料会自动重新分配,公共料按可用量补足,并重算料费、利润和待结金额。公共料不足仍可取消,缺料的关联单显示“料费待补”,补足购料后自动核算。</p></div>';
|
|
|
|
|
|
}
|
|
|
|
|
|
function recoveryBlockedHTML(rows, editing = true) {
|
|
|
|
|
|
return rows.length
|
|
|
|
|
|
? `<div class="note"><strong>关联金料已用/锁价预留</strong><ul>${rows.map(row => `<li>${row.anonymous ? '其他成员用料' : e(row.number || '关联用料')} · ${Number.isSafeInteger(row.mg) ? grams(row.mg) + 'g' : '克重待核对'}${!row.anonymous && row.priceLockId ? ' ' + button('查看锁价单', 'recovery-change-open-lock', 'link-button', `data-id="${e(row.priceLockId)}"`) : !row.anonymous && row.orderId ? ' ' + button('查看订单', 'recovery-change-open-order', 'link-button', `data-id="${e(row.orderId)}"`) : ''}</li>`).join('')}</ul>${editing ? '<p class="small">修改时不能更换金料日期,也不能将金料减至低于已用/预留量。</p>' : ''}</div>`
|
|
|
|
|
|
: '';
|
|
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function recoveryEditPanelHTML(data) {
|
2026-11-14 01:16:35 +08:00
|
|
|
|
return `${data.status === 'cancelled' ? '<div class="note">本单已取消,原记录和更正历史保留。</div>' : ''}${data.permissions.reason ? `<p class="small muted">${e(data.permissions.reason)}</p>` : ''}${recoveryMaterialBatchHTML(data.record.materialBatch)}${recoveryBlockedHTML(data.blockedUsages)}${data.permissions.canCancel && data.blockedUsages.length ? recoveryCancellationMaterialHTML() : ''}${data.pending ? `<section class="panel recovery-change-pending"><h3>待店主复核</h3><p class="small muted">原记录仍生效,申请尚未记账。</p>${recoveryChangeComparisonHTML(data.pending)}${button(state.user.role === 'owner' ? '查看并复核' : '查看申请', 'recovery-change-open', '', `data-id="${e(data.pending.id)}"`)}</section>` : ''}${data.history.length ? `<details><summary>更正与取消记录(${data.history.length})</summary><ul class="timeline">${data.history.map(row => `<li>${e(recoveryChangeStatus(row))} · ${e(row.requesterName || '未提供')}${row.reviewerName ? ' · ' + e(row.reviewerName) + '复核' : ''}${row.note ? '<p>' + e(row.note) + '</p>' : ''}${button('查看前后内容', 'recovery-change-open', 'quiet', `data-id="${e(row.id)}"`)}</li>`).join('')}</ul></details>` : ''}`;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function recoveryEditDraft(input) {
|
2026-11-14 01:16:35 +08:00
|
|
|
|
if (input.exchange?.items) {
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const rows = input.exchange.items.map(row => recoveryEditDraft({ ...input, ...row, exchange: row.exchange })),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
first = rows[0],
|
2026-11-28 01:28:39 +08:00
|
|
|
|
x = input.exchange;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
return {
|
|
|
|
|
|
...first,
|
2026-11-28 01:28:39 +08:00
|
|
|
|
independent_products: rows.length > 1,
|
2026-11-27 15:48:37 +08:00
|
|
|
|
extra_products: rows.slice(1),
|
|
|
|
|
|
charges: x.charges,
|
|
|
|
|
|
charge_name: x.charges[0]?.name || '其他收费',
|
2026-11-04 22:18:56 +08:00
|
|
|
|
charge_amount: x.charges[0]?.amount || '0',
|
2026-11-27 15:48:37 +08:00
|
|
|
|
fee_items: x.fee_items,
|
|
|
|
|
|
fee_note: x.fee_items[0]?.note || '其他费用',
|
|
|
|
|
|
fee_amount: x.fee_items[0]?.amount || '0',
|
2026-11-04 22:18:56 +08:00
|
|
|
|
fee_payer: x.fee_items[0]?.payer || input.member,
|
|
|
|
|
|
discount: x.discount,
|
|
|
|
|
|
rounding_mode: x.rounding_mode,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
2026-11-27 15:48:37 +08:00
|
|
|
|
const first = input.old_lines[0],
|
|
|
|
|
|
x = input.exchange || {},
|
|
|
|
|
|
charge = x.charges?.[0],
|
|
|
|
|
|
fee = x.fee_items?.[0],
|
|
|
|
|
|
account = input.payment_account || '',
|
|
|
|
|
|
identity = accountIdentity(account),
|
|
|
|
|
|
recipient = identity ? memberAccountPrefix(identity.member) : recoveryPrefix(input.member),
|
|
|
|
|
|
method = identity?.channel || '微信';
|
|
|
|
|
|
return {
|
|
|
|
|
|
...recoveryDefaults(input.mode),
|
|
|
|
|
|
mode: input.mode,
|
|
|
|
|
|
surplus_mode: input.surplus_mode || 'buy',
|
2026-11-14 01:16:35 +08:00
|
|
|
|
surplus_memo: input.surplus_memo || '',
|
|
|
|
|
|
old_source: input.old_source || 'incoming',
|
|
|
|
|
|
deposit_id: input.deposit_id || '',
|
|
|
|
|
|
deposit_weight: input.deposit_weight || '',
|
|
|
|
|
|
date: input.date,
|
|
|
|
|
|
customer: input.customer,
|
|
|
|
|
|
salesperson: input.salesperson,
|
2026-11-26 01:15:41 +08:00
|
|
|
|
member: input.member,
|
|
|
|
|
|
note: input.note || '',
|
|
|
|
|
|
old_pocket: input.old_pocket ?? '回收旧料',
|
|
|
|
|
|
old_stock_count: input.old_stock_count ?? '',
|
|
|
|
|
|
old_name: first.name,
|
|
|
|
|
|
incoming_weight: first.incoming_weight,
|
|
|
|
|
|
melted_weight: first.melted_weight,
|
|
|
|
|
|
purity: first.purity,
|
|
|
|
|
|
old_lines: JSON.parse(JSON.stringify(input.old_lines)),
|
|
|
|
|
|
buy_rate: input.buy_rate,
|
|
|
|
|
|
buy_total: input.buy_total ?? '',
|
|
|
|
|
|
buy_total_manual: Object.hasOwn(input, 'buy_total'),
|
|
|
|
|
|
melted_manual: true,
|
|
|
|
|
|
depreciation_manual: Object.hasOwn(x, 'depreciation_weight'),
|
|
|
|
|
|
payer: input.payer,
|
|
|
|
|
|
payment_method: method,
|
|
|
|
|
|
payment_recipient: recipient,
|
|
|
|
|
|
payout_method: method === '信用卡' ? '微信' : method,
|
|
|
|
|
|
new_name: x.name || '足金999',
|
|
|
|
|
|
new_quantity: x.quantity || '',
|
|
|
|
|
|
pocket: x.pocket ?? state.pockets[0] ?? '',
|
|
|
|
|
|
stockCount: x.stockCount ?? '',
|
|
|
|
|
|
gold_price: x.gold_price || '0',
|
|
|
|
|
|
labor_mode: x.labor_mode || 'gram',
|
|
|
|
|
|
labor_rate: x.labor_rate || '0',
|
|
|
|
|
|
depreciation_weight: x.depreciation_weight ?? '',
|
|
|
|
|
|
depreciation_rate: x.depreciation_rate || '0',
|
|
|
|
|
|
charge_name: charge?.name || '其他收费',
|
|
|
|
|
|
charge_amount: charge?.amount || '0',
|
|
|
|
|
|
charges: JSON.parse(JSON.stringify(x.charges || [])),
|
|
|
|
|
|
discount: x.discount || '0',
|
|
|
|
|
|
rounding_mode: x.rounding_mode || 'cent',
|
|
|
|
|
|
material_source: x.cost_material?.mode === 'pool' ? 'pool' : 'manual',
|
|
|
|
|
|
material_pool_id: x.cost_material?.poolId || input.date,
|
|
|
|
|
|
material_rate: x.cost_material?.rate || '',
|
|
|
|
|
|
material_payer: x.cost_material_payer || input.member,
|
|
|
|
|
|
cost_labor_mode: x.cost_labor?.mode || 'total',
|
|
|
|
|
|
cost_labor_value: x.cost_labor?.mode === 'gram' ? x.cost_labor.rate : (x.cost_labor?.amount ?? ''),
|
|
|
|
|
|
cost_payer: x.cost_payer || input.member,
|
|
|
|
|
|
labor_payer: x.cost_labor_payer || x.cost_payer || input.member,
|
|
|
|
|
|
fee_note: fee?.note || '其他费用',
|
|
|
|
|
|
fee_amount: fee?.amount || '0',
|
|
|
|
|
|
fee_payer: fee?.payer || input.member,
|
|
|
|
|
|
fee_items: JSON.parse(JSON.stringify(x.fee_items || [])),
|
|
|
|
|
|
...(x.products
|
|
|
|
|
|
? { ...recoveryProductDraft(x.products[0]), extra_products: x.products.slice(1).map(recoveryProductDraft) }
|
|
|
|
|
|
: {}),
|
|
|
|
|
|
verified: false,
|
|
|
|
|
|
};
|
2026-11-04 22:18:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
function recoveryEditFormDraft(form, flat) {
|
|
|
|
|
|
const entry = recoveryEditEntry;
|
|
|
|
|
|
if (!entry) return flat;
|
|
|
|
|
|
const source = entry.draft,
|
|
|
|
|
|
read = (row, name) => recoveryText(row.querySelector('[name="' + name + '"]')?.value);
|
|
|
|
|
|
const extra = (kind, convert) =>
|
|
|
|
|
|
[...form.querySelectorAll('[data-recovery-edit-row="' + kind + '"]')].map(row =>
|
|
|
|
|
|
convert(row, Number(row.dataset.index)),
|
|
|
|
|
|
);
|
|
|
|
|
|
const old_lines = [
|
2026-12-04 08:58:53 -08:00
|
|
|
|
{
|
2026-11-04 22:18:56 +08:00
|
|
|
|
...(source.old_lines?.[0] || {}),
|
|
|
|
|
|
name: flat.old_name,
|
|
|
|
|
|
incoming_weight: flat.incoming_weight,
|
|
|
|
|
|
melted_weight: flat.melted_weight,
|
|
|
|
|
|
purity: flat.purity,
|
|
|
|
|
|
},
|
|
|
|
|
|
...extra('old', row => ({
|
|
|
|
|
|
name: read(row, 'extra_old_name'),
|
|
|
|
|
|
incoming_weight: read(row, 'extra_incoming_weight'),
|
|
|
|
|
|
melted_weight: read(row, 'extra_melted_weight'),
|
|
|
|
|
|
purity: read(row, 'extra_purity'),
|
|
|
|
|
|
})),
|
|
|
|
|
|
];
|
|
|
|
|
|
const charges = [
|
|
|
|
|
|
...(source.charges?.length || (flat.charge_amount !== '' && Number(flat.charge_amount) !== 0)
|
|
|
|
|
|
? [{ ...(source.charges?.[0] || {}), name: flat.charge_name, amount: flat.charge_amount }]
|
|
|
|
|
|
: []),
|
|
|
|
|
|
...extra('charge', (row, index) => ({
|
|
|
|
|
|
...source.charges?.[index],
|
|
|
|
|
|
name: read(row, 'extra_charge_name'),
|
|
|
|
|
|
amount: read(row, 'extra_charge_amount'),
|
|
|
|
|
|
})),
|
|
|
|
|
|
];
|
|
|
|
|
|
const fee_items = [
|
|
|
|
|
|
...(source.fee_items?.length || (flat.fee_amount !== '' && Number(flat.fee_amount) !== 0)
|
2026-12-04 08:58:53 -08:00
|
|
|
|
? [
|
|
|
|
|
|
{
|
2026-11-04 22:18:56 +08:00
|
|
|
|
...(source.fee_items?.[0] || { key: 'other' }),
|
|
|
|
|
|
note: flat.fee_note,
|
|
|
|
|
|
amount: flat.fee_amount,
|
|
|
|
|
|
payer: flat.fee_payer,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
|
|
|
|
|
]
|
2026-11-04 22:18:56 +08:00
|
|
|
|
: []),
|
|
|
|
|
|
...extra('fee', (row, index) => ({
|
|
|
|
|
|
...source.fee_items?.[index],
|
|
|
|
|
|
key: source.fee_items?.[index]?.key || 'manual:' + crypto.randomUUID(),
|
|
|
|
|
|
note: read(row, 'extra_fee_note'),
|
|
|
|
|
|
amount: read(row, 'extra_fee_amount'),
|
|
|
|
|
|
payer: read(row, 'extra_fee_payer'),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
})),
|
2026-11-24 21:56:05 +08:00
|
|
|
|
];
|
|
|
|
|
|
return {
|
|
|
|
|
|
...flat,
|
|
|
|
|
|
member: entry.data.inputs.member,
|
|
|
|
|
|
mode: entry.data.inputs.mode,
|
|
|
|
|
|
cost_payer: source.cost_payer,
|
|
|
|
|
|
...(flat.independent_products ? {} : { old_lines }),
|
|
|
|
|
|
charges,
|
|
|
|
|
|
fee_items,
|
|
|
|
|
|
};
|
2026-11-04 22:18:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
function rememberRecoveryEditDraft(form = $('#recovery-form')) {
|
|
|
|
|
|
const entry = recoveryEditEntry;
|
|
|
|
|
|
if (!entry || entry.user !== recoveryChangeUser() || !form || form.dataset?.recoveryEdit !== 'true') return;
|
|
|
|
|
|
entry.draft = recoveryFormDraft(form);
|
|
|
|
|
|
const reason = recoveryField(form, 'change_reason');
|
|
|
|
|
|
entry.reason = reason ? reason.value : entry.reason || '';
|
|
|
|
|
|
try {
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
|
|
recoveryEditDraftKey(entry.id),
|
|
|
|
|
|
JSON.stringify({ id: entry.id, revision: entry.revision, draft: entry.draft, reason: entry.reason }),
|
|
|
|
|
|
);
|
|
|
|
|
|
} catch {}
|
|
|
|
|
|
}
|
|
|
|
|
|
function recoveryEditRowsHTML(kind, rows) {
|
|
|
|
|
|
return rows
|
|
|
|
|
|
.slice(1)
|
|
|
|
|
|
.map((row, offset) => {
|
|
|
|
|
|
const index = offset + 1;
|
|
|
|
|
|
return `<div class="recovery-row recovery-edit-extra-row" data-recovery-edit-row="${kind}" data-index="${index}">${kind === 'old' ? field('旧料名称', 'extra_old_name', row.name, 'text', 'required maxlength="60"') + recoveryNumberField('来料重量(g)', 'extra_incoming_weight', row.incoming_weight, 3, 'required') + recoveryNumberField('熔后重量(g)', 'extra_melted_weight', row.melted_weight, 3, 'required') + recoveryNumberField('成色(%)', 'extra_purity', row.purity, 3, 'required max="100"') : kind === 'charge' ? field('收费名称', 'extra_charge_name', row.name, 'text', 'required maxlength="60"') + recoveryNumberField('收费金额(元)', 'extra_charge_amount', row.amount, 2, 'required') : field('费用名称', 'extra_fee_note', row.note, 'text', 'required maxlength="100"') + recoveryNumberField('费用金额(元)', 'extra_fee_amount', row.amount, 2, 'required') + choice('实际付款人', 'extra_fee_payer', teamMemberOptions(), row.payer)}${button('移除这项', 'recovery-edit-remove-row', 'quiet', `data-kind="${kind}" data-index="${index}"`)}</div>`;
|
|
|
|
|
|
})
|
|
|
|
|
|
.join('');
|
|
|
|
|
|
}
|
|
|
|
|
|
function recoveryEditingFormHTML(entry) {
|
|
|
|
|
|
const draft = entry.draft,
|
|
|
|
|
|
locked = `<label>归属成员<input value="${e(teamMemberName(entry.data.inputs.member))}" readonly><input name="member" type="hidden" value="${e(entry.data.inputs.member)}"></label>`;
|
2026-11-26 01:01:18 +08:00
|
|
|
|
let html = recoveryFormHTML(draft)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
.replace('<form id="recovery-form"', '<form id="recovery-form" data-recovery-edit="true"')
|
2026-12-04 08:58:53 -08:00
|
|
|
|
.replace(
|
2026-11-04 22:18:56 +08:00
|
|
|
|
/<div class="recovery-mode-switch"[\s\S]*?<\/div>/,
|
|
|
|
|
|
`<p class="note">${e(recoveryModeNames[draft.mode])} · 沿用原单号 ${e(entry.data.record.number)},本单业务类型保持不变。</p>`,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
.replace(recoveryMemberField(draft), locked)
|
|
|
|
|
|
.replace('保存后自动编号', '沿用原单号')
|
|
|
|
|
|
.replace(
|
|
|
|
|
|
'<p class="small muted recovery-formula" id="recovery-folded-formula"',
|
|
|
|
|
|
`${recoveryEditRowsHTML('old', draft.old_lines)}${button('+ 添加旧料', 'recovery-edit-add-row', 'quiet', 'data-kind="old"')}<p class="small muted recovery-formula" id="recovery-folded-formula"`,
|
|
|
|
|
|
);
|
|
|
|
|
|
if (draft.pocket && !activePocketKeys().includes(draft.pocket))
|
2026-11-24 21:56:05 +08:00
|
|
|
|
html = html.replace(
|
2026-11-04 22:18:56 +08:00
|
|
|
|
pocketChoice('出货货盘', 'pocket', draft.pocket, { blank: '外配(不扣店内库存)' }),
|
|
|
|
|
|
pocketChoice('出货货盘', 'pocket', draft.pocket, { blank: '外配(不扣店内库存)', historical: true }),
|
|
|
|
|
|
);
|
|
|
|
|
|
html = html.replace(
|
|
|
|
|
|
'</div></details></section><section class="panel recovery-buy-panel">',
|
|
|
|
|
|
`</div>${recoveryEditRowsHTML('charge', draft.charges)}${button('+ 添加收费', 'recovery-edit-add-row', 'quiet', 'data-kind="charge"')}</details></section><section class="panel recovery-buy-panel">`,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
2026-11-04 22:18:56 +08:00
|
|
|
|
html = html.replace(
|
|
|
|
|
|
'</div></details><p class="small recovery-formula" id="recovery-internal-preview"',
|
|
|
|
|
|
`</div>${recoveryEditRowsHTML('fee', draft.fee_items)}${button('+ 添加实际费用', 'recovery-edit-add-row', 'quiet', 'data-kind="fee"')}</details><p class="small recovery-formula" id="recovery-internal-preview"`,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (draft.independent_products)
|
|
|
|
|
|
html = html.replace(
|
|
|
|
|
|
'<p class="small" id="recovery-material-note">',
|
2026-11-24 21:56:05 +08:00
|
|
|
|
recoveryEditRowsHTML('charge', draft.charges) +
|
|
|
|
|
|
button('+ 添加收费', 'recovery-edit-add-row', 'quiet', 'data-kind="charge"') +
|
|
|
|
|
|
recoveryEditRowsHTML('fee', draft.fee_items) +
|
2026-11-04 22:18:56 +08:00
|
|
|
|
button('+ 添加实际费用', 'recovery-edit-add-row', 'quiet', 'data-kind="fee"') +
|
2026-11-24 21:56:05 +08:00
|
|
|
|
'<p class="small" id="recovery-material-note">',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
|
|
|
|
|
html = html
|
2026-11-04 22:18:56 +08:00
|
|
|
|
.replace(
|
|
|
|
|
|
'<section class="panel recovery-save-panel">',
|
|
|
|
|
|
`<section class="panel recovery-save-panel"><div class="note" id="recovery-edit-mode" aria-live="polite"></div>${legacyCountConfirmationHTML(entry.data.permissions)}${field('修改原因', 'change_reason', entry.reason || '', 'text', 'required maxlength="200"')}<div id="recovery-edit-conflict" aria-live="polite">${entry.invalid ? `<p class="error">记录已有变动,填写已保留,请核对最新记录。</p>${button('重新读取并核对', 'recovery-edit-reload', '', `data-id="${e(entry.id)}"`)}${entry.latest ? button('已核对最新记录,保留填写继续', 'recovery-edit-keep', '', 'data-id="' + e(entry.id) + '"') : ''}` : ''}</div>`,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-24 21:11:32 +08:00
|
|
|
|
.replace(
|
2026-11-04 22:18:56 +08:00
|
|
|
|
'<button type="submit" class="primary" id="recovery-save">',
|
|
|
|
|
|
`${button('取消修改', 'recovery-edit-close', 'quiet')}<button type="submit" class="primary" id="recovery-save">`,
|
|
|
|
|
|
);
|
|
|
|
|
|
return html;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function recoveryEditApproval(entry, date) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
!entry.data.permissions.canDirect ||
|
|
|
|
|
|
entry.data.permissions.requiresApproval ||
|
|
|
|
|
|
(state.user.role !== 'owner' && (entry.data.inputs.date !== state.today || date !== state.today))
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function syncRecoveryEditMode(form = $('#recovery-form')) {
|
|
|
|
|
|
const entry = recoveryEditEntry;
|
|
|
|
|
|
if (!entry || form?.dataset.recoveryEdit !== 'true') return;
|
|
|
|
|
|
const approval = recoveryEditApproval(entry, recoveryField(form, 'date')?.value),
|
|
|
|
|
|
submit = $('#recovery-save'),
|
|
|
|
|
|
note = $('#recovery-edit-mode');
|
|
|
|
|
|
if (submit) submit.textContent = approval ? '提交修改申请' : '保存修改';
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (note)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
note.textContent = approval
|
|
|
|
|
|
? '店主同意后才更新原记录;审批前收付款、购料和库存不变。'
|
2026-11-14 01:16:35 +08:00
|
|
|
|
: '保存后更正原记录,并同步关联收付款、购料和库存。';
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function renderRecoveryEdit() {
|
|
|
|
|
|
const entry = recoveryEditEntry;
|
|
|
|
|
|
if (!entry || entry.user !== recoveryChangeUser()) return;
|
|
|
|
|
|
$('#main').innerHTML =
|
|
|
|
|
|
heading(
|
|
|
|
|
|
'EDIT RECOVERY',
|
|
|
|
|
|
'修改' + recoveryModeNames[entry.data.inputs.mode],
|
|
|
|
|
|
e(entry.data.record.number),
|
|
|
|
|
|
button('取消修改', 'recovery-edit-close', 'quiet'),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) +
|
2026-11-04 22:18:56 +08:00
|
|
|
|
recoveryBlockedHTML(entry.data.blockedUsages) +
|
|
|
|
|
|
recoveryEditingFormHTML(entry);
|
2026-11-26 01:01:18 +08:00
|
|
|
|
syncRecoveryForm($('#recovery-form'), { quote: false });
|
|
|
|
|
|
recoveryPoolOptions($('#recovery-form'));
|
|
|
|
|
|
syncRecoveryEditMode();
|
2026-11-04 22:18:56 +08:00
|
|
|
|
applyRecoveryChangeRestrictions();
|
|
|
|
|
|
loadCustomerMaterials();
|
|
|
|
|
|
loadRecoveryPools($('#recovery-form'));
|
2026-11-26 01:01:18 +08:00
|
|
|
|
loadRecoveryQuote($('#recovery-form'));
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
async function openRecoveryEdit(id) {
|
|
|
|
|
|
if (busy || pending()) throw new Error('请先恢复未确认操作,再修改回收记录。');
|
|
|
|
|
|
const request = ++recoveryEditRequest,
|
|
|
|
|
|
user = recoveryChangeUser(),
|
|
|
|
|
|
sourceDialog = $('#recovery-dialog'),
|
|
|
|
|
|
sourceRequest = recoveryDetailRequest,
|
|
|
|
|
|
fromDetail = sourceDialog?.open;
|
|
|
|
|
|
if ($('#recovery-form')?.dataset.recoveryEdit !== 'true') rememberRecoveryDraft();
|
2026-11-24 21:56:05 +08:00
|
|
|
|
const response = await api('/api/recoveries/' + encodeURIComponent(id) + '/edit');
|
|
|
|
|
|
if (
|
|
|
|
|
|
request !== recoveryEditRequest ||
|
|
|
|
|
|
user !== recoveryChangeUser() ||
|
|
|
|
|
|
(fromDetail && (!sourceDialog.open || sourceRequest !== recoveryDetailRequest))
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
|
|
|
|
|
return;
|
2026-11-24 21:56:05 +08:00
|
|
|
|
const data = validateRecoveryEditDetail(response, id);
|
2026-11-08 15:47:07 +08:00
|
|
|
|
if (!data.permissions.canEdit || data.pending) throw new Error(data.permissions.reason || '这笔记录当前不能修改。');
|
|
|
|
|
|
let saved;
|
|
|
|
|
|
try {
|
|
|
|
|
|
saved = JSON.parse(localStorage.getItem(recoveryEditDraftKey(id)) || 'null');
|
|
|
|
|
|
} catch {}
|
|
|
|
|
|
const validSaved =
|
|
|
|
|
|
saved?.id === id && saved.draft?.mode === data.inputs.mode && saved.draft?.member === data.inputs.member;
|
|
|
|
|
|
if (validSaved && saved.draft.independent_products && !(data.inputs.exchange?.items?.length > 1)) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
localStorage.setItem(recoveryEditDraftKey(id) + '-before-shared-old', JSON.stringify(saved));
|
|
|
|
|
|
} catch {}
|
|
|
|
|
|
saved.draft = recoverySharedOldDraft(saved.draft);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 15:47:07 +08:00
|
|
|
|
closeRecoveryDetail();
|
|
|
|
|
|
tab = 'recoveries';
|
|
|
|
|
|
recoveryEditEntry = {
|
|
|
|
|
|
id,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
user,
|
|
|
|
|
|
data,
|
2026-11-08 15:47:07 +08:00
|
|
|
|
revision: validSaved ? saved.revision : data.revision,
|
|
|
|
|
|
draft: validSaved ? saved.draft : recoveryEditDraft(data.inputs),
|
|
|
|
|
|
reason: validSaved ? saved.reason || '' : '',
|
|
|
|
|
|
invalid: validSaved && saved.revision !== data.revision,
|
|
|
|
|
|
latest: null,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
};
|
2026-11-08 15:47:07 +08:00
|
|
|
|
recoveryEditEntry.draft.verified = false;
|
|
|
|
|
|
document.querySelectorAll('nav button').forEach(node => node.classList.toggle('active', node.dataset.tab === tab));
|
2026-11-04 22:18:56 +08:00
|
|
|
|
renderRecoveryEdit();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 15:47:07 +08:00
|
|
|
|
async function reloadRecoveryEdit() {
|
|
|
|
|
|
const entry = recoveryEditEntry;
|
|
|
|
|
|
if (!entry || busy || pending()) throw new Error('请先恢复原操作再重新读取。');
|
2026-11-04 22:18:56 +08:00
|
|
|
|
rememberRecoveryEditDraft();
|
2026-11-08 15:47:07 +08:00
|
|
|
|
const user = entry.user,
|
|
|
|
|
|
response = await api('/api/recoveries/' + encodeURIComponent(entry.id) + '/edit');
|
|
|
|
|
|
if (recoveryEditEntry !== entry || user !== recoveryChangeUser()) return;
|
|
|
|
|
|
const data = validateRecoveryEditDetail(response, entry.id);
|
|
|
|
|
|
entry.latest = data;
|
2026-11-04 22:18:56 +08:00
|
|
|
|
entry.invalid = true;
|
|
|
|
|
|
entry.draft.verified = false;
|
|
|
|
|
|
renderRecoveryEdit();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 15:47:07 +08:00
|
|
|
|
function applyRecoveryChangeRestrictions() {
|
|
|
|
|
|
const form = $('#recovery-form');
|
|
|
|
|
|
if (form?.dataset.recoveryEdit === 'true') {
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const blocked =
|
2026-11-08 15:47:07 +08:00
|
|
|
|
busy ||
|
2026-11-04 22:18:56 +08:00
|
|
|
|
!!pending() ||
|
|
|
|
|
|
!recoveryEditEntry ||
|
|
|
|
|
|
recoveryEditEntry.invalid ||
|
2026-11-08 15:47:07 +08:00
|
|
|
|
recoveryEditEntry.user !== recoveryChangeUser();
|
|
|
|
|
|
for (const control of form.querySelectorAll('input,select,textarea,button')) {
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const action = control.dataset.action;
|
2026-11-08 15:47:07 +08:00
|
|
|
|
control.disabled = ['recovery-edit-close', 'recovery-edit-reload', 'recovery-edit-keep'].includes(action)
|
|
|
|
|
|
? busy || (!!pending() && action !== 'recovery-edit-close')
|
|
|
|
|
|
: blocked || !!control.closest?.('[hidden]');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 15:47:07 +08:00
|
|
|
|
if (recoveryField(form, 'payment_method')?.value === '信用卡' && recoveryField(form, 'payment_recipient'))
|
2026-11-04 22:18:56 +08:00
|
|
|
|
recoveryField(form, 'payment_recipient').disabled = true;
|
2026-11-08 15:47:07 +08:00
|
|
|
|
syncRecoveryEditMode(form);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 15:47:07 +08:00
|
|
|
|
const review = $('#recovery-change-review-form') || $('#recovery-cancel-form');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (review)
|
2026-11-08 15:47:07 +08:00
|
|
|
|
for (const control of review.querySelectorAll('input,textarea,button'))
|
2026-11-04 22:18:56 +08:00
|
|
|
|
control.disabled =
|
2026-11-08 15:47:07 +08:00
|
|
|
|
control.dataset.action === 'recovery-changes-close'
|
2026-12-04 08:58:53 -08:00
|
|
|
|
? busy
|
2026-11-04 22:18:56 +08:00
|
|
|
|
: busy || !!pending() || !recoveryReviewEntry || recoveryReviewEntry.invalid;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 15:47:07 +08:00
|
|
|
|
async function submitRecoveryEdit(form) {
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const entry = recoveryEditEntry;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (
|
|
|
|
|
|
!entry ||
|
2026-11-04 22:18:56 +08:00
|
|
|
|
entry.invalid ||
|
|
|
|
|
|
entry.user !== recoveryChangeUser() ||
|
2026-11-08 15:47:07 +08:00
|
|
|
|
form !== $('#recovery-form') ||
|
|
|
|
|
|
!entry.data.permissions.canEdit ||
|
2026-11-04 22:18:56 +08:00
|
|
|
|
entry.data.pending
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-08 15:47:07 +08:00
|
|
|
|
throw new Error('请重新读取并核对回收记录后再修改。');
|
|
|
|
|
|
syncRecoveryForm(form, { quote: false });
|
2026-11-04 22:18:56 +08:00
|
|
|
|
rememberRecoveryEditDraft(form);
|
2026-11-08 15:47:07 +08:00
|
|
|
|
const input = validateRecoveryInput(recoveryInput(recoveryFormDraft(form))),
|
|
|
|
|
|
reason = recoveryText(recoveryField(form, 'change_reason')?.value);
|
|
|
|
|
|
if (!reason || reason.length > 200) throw new Error('修改原因请填写1~200字。');
|
|
|
|
|
|
if (input.mode !== entry.data.inputs.mode || input.member !== entry.data.inputs.member)
|
|
|
|
|
|
throw new Error('原记录类型与归属不能修改。');
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const payload = {
|
2026-12-04 08:58:53 -08:00
|
|
|
|
...input,
|
2026-11-08 15:47:07 +08:00
|
|
|
|
...requireLegacyCountConfirmation(form, entry.data.permissions),
|
2026-11-04 22:18:56 +08:00
|
|
|
|
revision: entry.revision,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
reason,
|
2026-11-04 22:18:56 +08:00
|
|
|
|
verified: true,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
2026-11-08 15:47:07 +08:00
|
|
|
|
url = '/api/recoveries/' + encodeURIComponent(entry.id) + '/update',
|
2026-11-04 22:18:56 +08:00
|
|
|
|
view = captureRecoveryChangeView();
|
2026-11-08 15:47:07 +08:00
|
|
|
|
$('#recovery-form-error').innerHTML = '';
|
2026-12-04 08:58:53 -08:00
|
|
|
|
try {
|
2026-11-04 22:18:56 +08:00
|
|
|
|
form.querySelectorAll('input,select,textarea').forEach(control => (control.disabled = true));
|
2026-11-08 15:47:07 +08:00
|
|
|
|
const result = await mutate(url, payload);
|
2026-11-04 22:18:56 +08:00
|
|
|
|
await finishRecoveryChangeSave(url, payload, result, view);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
} catch (err) {
|
2026-11-08 15:47:07 +08:00
|
|
|
|
if (view.user === recoveryChangeUser()) recoveryChangeError(err, { kind: 'update', id: entry.id });
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
|
function recoveryChangeStatus(row) {
|
2026-12-04 08:58:53 -08:00
|
|
|
|
return (
|
2026-11-08 15:47:07 +08:00
|
|
|
|
{ pending: '待店主复核', approved: row.action === 'cancel' ? '已同意取消' : '已同意修改', rejected: '已驳回' }[
|
2026-11-14 01:16:35 +08:00
|
|
|
|
row.status
|
2026-11-08 15:47:07 +08:00
|
|
|
|
] || '状态待核对'
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function recoveryCompareCell(value) {
|
2026-11-08 15:47:07 +08:00
|
|
|
|
return e(value == null || value === '' ? '未提供' : value).replace(/\n/g, '<br>');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 15:47:07 +08:00
|
|
|
|
function recoveryChangeComparisonHTML(raw) {
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const request = validateRecoveryChangeRequest(raw),
|
|
|
|
|
|
before = request.before,
|
|
|
|
|
|
after = request.after,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
pair = (title, a, b) =>
|
2026-11-08 15:47:07 +08:00
|
|
|
|
`<tr${String(a ?? '') !== String(b ?? '') ? ' class="order-change-modified"' : ''}><th scope="row">${e(title)}</th><td>${recoveryCompareCell(a)}</td><td>${recoveryCompareCell(b)}</td></tr>`,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
old = input =>
|
2026-11-04 22:18:56 +08:00
|
|
|
|
input?.old_lines
|
|
|
|
|
|
.map(row => `${row.name}:来料 ${row.incoming_weight}g,熔后 ${row.melted_weight}g,成色 ${row.purity}%`)
|
2026-11-14 01:16:35 +08:00
|
|
|
|
.join('\n'),
|
|
|
|
|
|
fees = input =>
|
|
|
|
|
|
input?.exchange?.fee_items
|
|
|
|
|
|
.map(row => `${row.note}:${row.amount}元 · ${teamMemberName(row.payer)}付款`)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
.join('\n') || '无',
|
2026-11-14 01:16:35 +08:00
|
|
|
|
charges = input => input?.exchange?.charges.map(row => `${row.name}:${row.amount}元`).join('\n') || '无';
|
|
|
|
|
|
const x = before.exchange || {},
|
|
|
|
|
|
y = after?.exchange || {},
|
|
|
|
|
|
material = value =>
|
|
|
|
|
|
value?.mode === 'pool' ? '料池 ' + value.poolId : value ? '手填 ' + value.rate + '元/克' : '无补金差重',
|
|
|
|
|
|
labor = value => (value ? (value.mode === 'gram' ? value.rate + '元/克' : value.amount + '元总工费') : '未提供');
|
|
|
|
|
|
let rows =
|
|
|
|
|
|
pair('业务日期', before.date, after?.date || '取消原记录') +
|
|
|
|
|
|
pair('客户', before.customer, after?.customer) +
|
|
|
|
|
|
pair('开单人', before.salesperson, after?.salesperson) +
|
|
|
|
|
|
pair('归属', teamMemberName(before.member), after ? teamMemberName(after.member) : '沿用原身份留档') +
|
|
|
|
|
|
pair('旧料逐项称重', old(before), after ? old(after) : '冲正原库存及关联购料') +
|
2026-12-04 08:58:53 -08:00
|
|
|
|
pair(
|
2026-11-14 01:16:35 +08:00
|
|
|
|
'旧料入库货盘',
|
2026-11-26 01:01:18 +08:00
|
|
|
|
pocketLabel(before.old_pocket ?? '回收旧料'),
|
|
|
|
|
|
after ? pocketLabel(after.old_pocket ?? '回收旧料') : '冲回原入库货盘',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) +
|
2026-11-26 01:01:18 +08:00
|
|
|
|
pair(
|
|
|
|
|
|
'旧料入库数量(对)',
|
|
|
|
|
|
stockCountText(before.old_stock_count == null ? null : Number(before.old_stock_count)),
|
2026-11-04 22:18:56 +08:00
|
|
|
|
after ? stockCountText(after.old_stock_count == null ? null : Number(after.old_stock_count)) : '冲回原入库数量',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) +
|
2026-11-04 22:18:56 +08:00
|
|
|
|
pair('回收单价(元/克)', before.buy_rate, after?.buy_rate) +
|
|
|
|
|
|
pair('回收结算额(元)', before.buy_total, after?.buy_total) +
|
2026-12-04 08:58:53 -08:00
|
|
|
|
pair(
|
2026-11-24 21:11:32 +08:00
|
|
|
|
'实际收付款账户',
|
2026-11-04 22:18:56 +08:00
|
|
|
|
accountDisplayName(before.payment_account),
|
2026-11-08 15:47:07 +08:00
|
|
|
|
after ? accountDisplayName(after.payment_account) : '冲正原收付记录',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) +
|
2026-11-08 15:47:07 +08:00
|
|
|
|
pair('付款人', teamMemberName(before.payer), after ? teamMemberName(after.payer) : '原付款流水保留') +
|
|
|
|
|
|
pair('备注', before.note, after?.note);
|
|
|
|
|
|
if (before.surplus_mode === 'deposit' || after?.surplus_mode === 'deposit')
|
|
|
|
|
|
rows += pair(
|
2026-11-26 01:15:41 +08:00
|
|
|
|
'余料处理',
|
2026-11-08 15:47:07 +08:00
|
|
|
|
before.surplus_mode === 'deposit' ? '寄存,暂不结算' : '按回收价结算',
|
|
|
|
|
|
after ? (after.surplus_mode === 'deposit' ? '寄存,暂不结算' : '按回收价结算') : '撤回本单寄存余料',
|
|
|
|
|
|
);
|
|
|
|
|
|
if (before.old_source === 'deposit' || after?.old_source === 'deposit')
|
|
|
|
|
|
rows += pair(
|
|
|
|
|
|
'使用客户寄存料',
|
|
|
|
|
|
before.old_source === 'deposit' ? before.deposit_id + ' · ' + before.deposit_weight + 'g' : '本次交来旧料',
|
|
|
|
|
|
after?.old_source === 'deposit'
|
|
|
|
|
|
? after.deposit_id + ' · ' + after.deposit_weight + 'g'
|
|
|
|
|
|
: after
|
2026-11-26 01:01:18 +08:00
|
|
|
|
? '本次交来旧料'
|
|
|
|
|
|
: '退回寄存账户',
|
2026-11-08 15:47:07 +08:00
|
|
|
|
);
|
|
|
|
|
|
if (before.exchange?.items || after?.exchange?.items) {
|
|
|
|
|
|
const items = v =>
|
|
|
|
|
|
(v?.exchange?.items || [])
|
|
|
|
|
|
.map(
|
2026-12-04 08:58:53 -08:00
|
|
|
|
(p, i) =>
|
|
|
|
|
|
i +
|
|
|
|
|
|
1 +
|
|
|
|
|
|
' · ' +
|
2026-11-08 15:47:07 +08:00
|
|
|
|
p.exchange.name +
|
|
|
|
|
|
(p.old_source === 'deposit' ? ' · 寄存记录 ' + p.deposit_id + ' 使用 ' + p.deposit_weight + 'g' : '') +
|
2026-11-26 01:15:41 +08:00
|
|
|
|
(p.surplus_mode === 'deposit' ? ' · 余料寄存,暂不结算' : '') +
|
2026-11-26 01:01:18 +08:00
|
|
|
|
' · 旧料 ' +
|
2026-11-08 15:47:07 +08:00
|
|
|
|
p.old_lines.map(r => r.melted_weight + 'g × ' + r.purity + '%').join('+') +
|
2026-12-04 08:58:53 -08:00
|
|
|
|
' 入' +
|
2026-11-08 15:47:07 +08:00
|
|
|
|
pocketLabel(p.old_pocket) +
|
|
|
|
|
|
' · 新货 ' +
|
|
|
|
|
|
p.exchange.quantity +
|
2026-12-04 08:58:53 -08:00
|
|
|
|
'g ' +
|
2026-11-08 15:47:07 +08:00
|
|
|
|
(pocketLabel(p.exchange.pocket) || '外配') +
|
|
|
|
|
|
' · 补金 ' +
|
|
|
|
|
|
p.exchange.gold_price +
|
2026-11-24 21:11:32 +08:00
|
|
|
|
'元/g · 折旧 ' +
|
2026-11-08 15:47:07 +08:00
|
|
|
|
p.exchange.depreciation_weight +
|
2026-12-04 08:58:53 -08:00
|
|
|
|
'g × ' +
|
2026-11-08 15:47:07 +08:00
|
|
|
|
p.exchange.depreciation_rate +
|
|
|
|
|
|
'元/g · 回收 ' +
|
|
|
|
|
|
p.buy_rate +
|
2026-12-04 08:58:53 -08:00
|
|
|
|
'元/g,' +
|
2026-11-08 15:47:07 +08:00
|
|
|
|
p.buy_total +
|
2026-11-24 21:11:32 +08:00
|
|
|
|
'元 · 料费 ' +
|
2026-11-08 15:47:07 +08:00
|
|
|
|
JSON.stringify(p.exchange.cost_material) +
|
2026-11-26 01:01:18 +08:00
|
|
|
|
' · 料费付款 ' +
|
2026-11-04 22:18:56 +08:00
|
|
|
|
teamMemberName(p.exchange.cost_material_payer) +
|
|
|
|
|
|
' · 进货工费 ' +
|
|
|
|
|
|
JSON.stringify(p.exchange.cost_labor) +
|
|
|
|
|
|
',' +
|
|
|
|
|
|
teamMemberName(p.exchange.cost_labor_payer),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
.join(';');
|
|
|
|
|
|
rows += pair('逐件置换完整资料', items(before), after ? items(after) : '全部冲回');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (before.exchange?.products || after?.exchange?.products) {
|
|
|
|
|
|
const items = x =>
|
|
|
|
|
|
(x?.products ?? (x ? [x] : []))
|
|
|
|
|
|
.map(
|
|
|
|
|
|
(p, i) =>
|
2026-12-04 08:58:53 -08:00
|
|
|
|
i +
|
|
|
|
|
|
1 +
|
|
|
|
|
|
' · ' +
|
2026-11-04 22:18:56 +08:00
|
|
|
|
p.name +
|
2026-12-04 08:58:53 -08:00
|
|
|
|
' · ' +
|
2026-11-04 22:18:56 +08:00
|
|
|
|
p.quantity +
|
|
|
|
|
|
'g · ' +
|
|
|
|
|
|
(pocketLabel(p.pocket) || '外配') +
|
|
|
|
|
|
' · 销售工费 ' +
|
|
|
|
|
|
p.labor_rate +
|
|
|
|
|
|
(p.labor_mode === 'total' ? '元' : '元/克') +
|
|
|
|
|
|
' · 进货工费 ' +
|
2026-11-24 21:11:32 +08:00
|
|
|
|
(p.cost_labor?.rate ?? p.cost_labor?.amount ?? '待补') +
|
2026-12-04 08:58:53 -08:00
|
|
|
|
' · ' +
|
2026-11-24 21:11:32 +08:00
|
|
|
|
teamMemberName(p.cost_labor_payer) +
|
|
|
|
|
|
(p.stockCount == null ? '' : ' · ' + p.stockCount + '对'),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-24 21:11:32 +08:00
|
|
|
|
.join(';');
|
|
|
|
|
|
rows += pair('逐件置换商品', items(before.exchange), after ? items(after.exchange) : '冲回全部新货出库');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-24 21:11:32 +08:00
|
|
|
|
if (before.mode === 'exchange')
|
2026-12-04 08:58:53 -08:00
|
|
|
|
rows +=
|
2026-11-24 21:11:32 +08:00
|
|
|
|
pair('置换新货', x.name, y.name) +
|
|
|
|
|
|
pair('新货克重(g)', x.quantity, y.quantity) +
|
|
|
|
|
|
pair('实际货盘', pocketLabel(x.pocket) || '外配', after ? pocketLabel(y.pocket) || '外配' : '冲回原出库货盘') +
|
|
|
|
|
|
pair(
|
|
|
|
|
|
'出库数量(对)',
|
|
|
|
|
|
stockCountText(x.stockCount == null ? null : Number(x.stockCount)),
|
|
|
|
|
|
stockCountText(y.stockCount == null ? null : Number(y.stockCount)),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) +
|
2026-11-24 21:11:32 +08:00
|
|
|
|
pair('销售金价(元/克)', x.gold_price, y.gold_price) +
|
|
|
|
|
|
pair(
|
|
|
|
|
|
'销售工费',
|
|
|
|
|
|
x.labor_rate + '元/' + (x.labor_mode === 'gram' ? '克' : '总额'),
|
|
|
|
|
|
after ? y.labor_rate + '元/' + (y.labor_mode === 'gram' ? '克' : '总额') : '取消',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) +
|
2026-11-24 21:11:32 +08:00
|
|
|
|
pair(
|
2026-11-26 01:15:41 +08:00
|
|
|
|
'折旧计费克重/单价',
|
|
|
|
|
|
x.depreciation_weight + 'g × ' + x.depreciation_rate + '元/克',
|
|
|
|
|
|
after ? y.depreciation_weight + 'g × ' + y.depreciation_rate + '元/克' : '取消',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) +
|
2026-11-26 01:15:41 +08:00
|
|
|
|
pair('附加收费', charges(before), after ? charges(after) : '取消') +
|
|
|
|
|
|
pair('优惠(元)', x.discount, y.discount) +
|
2026-12-04 08:58:53 -08:00
|
|
|
|
pair(
|
2026-11-26 01:15:41 +08:00
|
|
|
|
'取整方式',
|
|
|
|
|
|
x.rounding_mode === 'yuan' ? '四舍五入到元' : '保留到分',
|
|
|
|
|
|
after ? (y.rounding_mode === 'yuan' ? '四舍五入到元' : '保留到分') : '取消',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) +
|
2026-11-26 01:15:41 +08:00
|
|
|
|
pair('补金差重进货来源', material(x.cost_material), after ? material(y.cost_material) : '释放原耗用') +
|
2026-12-04 08:58:53 -08:00
|
|
|
|
pair(
|
2026-11-26 01:15:41 +08:00
|
|
|
|
'料费付款人',
|
|
|
|
|
|
teamMemberName(x.cost_material_payer),
|
2026-11-24 21:11:32 +08:00
|
|
|
|
after ? teamMemberName(y.cost_material_payer) : '原来源留档',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) +
|
2026-11-24 21:11:32 +08:00
|
|
|
|
pair('进货工费', labor(x.cost_labor), after ? labor(y.cost_labor) : '取消') +
|
|
|
|
|
|
pair(
|
|
|
|
|
|
'工费付款人',
|
|
|
|
|
|
teamMemberName(x.cost_labor_payer),
|
|
|
|
|
|
after ? teamMemberName(y.cost_labor_payer) : '原来源留档',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) +
|
2026-11-24 21:11:32 +08:00
|
|
|
|
pair('实际费用', fees(before), after ? fees(after) : '冲正原费用');
|
|
|
|
|
|
const a = request.summary.before || {},
|
|
|
|
|
|
b = request.summary.after || {};
|
|
|
|
|
|
rows += [
|
2026-11-04 22:18:56 +08:00
|
|
|
|
['实际收客户', 'cashReceipt'],
|
|
|
|
|
|
['实际付客户', 'cashPayment'],
|
|
|
|
|
|
['购料成本', 'purchaseAmount'],
|
|
|
|
|
|
['订单货品成本', 'goodsCost'],
|
|
|
|
|
|
['实际费用', 'directCost'],
|
|
|
|
|
|
['原账利润', 'profit'],
|
2026-12-04 08:58:53 -08:00
|
|
|
|
]
|
2026-11-04 22:18:56 +08:00
|
|
|
|
.map(([title, key]) =>
|
|
|
|
|
|
pair(
|
|
|
|
|
|
title,
|
|
|
|
|
|
a[key] == null ? '待核实' : '¥' + yuan(a[key]),
|
|
|
|
|
|
after ? (b[key] == null ? '待核实' : '¥' + yuan(b[key])) : '取消后冲正',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
),
|
|
|
|
|
|
)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
.join('');
|
2026-11-08 15:47:07 +08:00
|
|
|
|
return `${request.summary.legacyCountNotice ? '<p class="note">' + e(request.summary.legacyCountNotice) + (request.summary.legacyCountVerified ? '(申请人已确认)' : '') + '</p>' : ''}<div class="table-wrap recovery-change-comparison"><table class="order-change-table" aria-label="回收更正原值与申请值"><thead><tr><th>核对项目</th><th>原值</th><th>${request.action === 'cancel' ? '取消后' : '申请值'}</th></tr></thead><tbody>${rows}</tbody></table></div>${request.action === 'cancel' ? recoveryBlockedHTML(request.summary.blockedUsages || [], false) + recoveryCancellationMaterialHTML() : ''}`;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function recoveryChangesEntryHTML() {
|
|
|
|
|
|
return button(
|
|
|
|
|
|
state.user.role === 'owner' ? '回收更正复核(…)' : '回收更正申请(…)',
|
|
|
|
|
|
'recovery-changes-open',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
'',
|
2026-11-04 22:18:56 +08:00
|
|
|
|
'id="recovery-changes-entry"',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
2026-11-04 22:18:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
async function loadRecoveryChangeCount() {
|
|
|
|
|
|
const node = $('#recovery-changes-entry');
|
|
|
|
|
|
if (!node) return;
|
|
|
|
|
|
const user = recoveryChangeUser(),
|
|
|
|
|
|
title = state.user.role === 'owner' ? '回收更正复核' : '回收更正申请';
|
|
|
|
|
|
try {
|
|
|
|
|
|
const data = await api('/api/recovery-changes?status=pending');
|
|
|
|
|
|
if (!Number.isSafeInteger(data.counts?.pending) || data.counts.pending < 0) throw new Error('数量未完整返回');
|
|
|
|
|
|
if (node === $('#recovery-changes-entry') && user === recoveryChangeUser())
|
|
|
|
|
|
node.textContent = title + '(' + data.counts.pending + ')';
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
if (node === $('#recovery-changes-entry') && user === recoveryChangeUser())
|
|
|
|
|
|
node.textContent = title + '(读取失败)';
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function recoveryChangesDialog() {
|
|
|
|
|
|
let dialog = $('#recovery-change-dialog');
|
|
|
|
|
|
if (!dialog) {
|
|
|
|
|
|
dialog = document.createElement('dialog');
|
|
|
|
|
|
dialog.id = 'recovery-change-dialog';
|
|
|
|
|
|
dialog.setAttribute('aria-labelledby', 'recovery-change-title');
|
|
|
|
|
|
document.body.append(dialog);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (recoveryChangeDialogBound !== dialog) {
|
|
|
|
|
|
dialog.addEventListener('close', () => {
|
|
|
|
|
|
if (!dialog.open) {
|
|
|
|
|
|
++recoveryReviewRequest;
|
|
|
|
|
|
recoveryReviewEntry = null;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
});
|
|
|
|
|
|
recoveryChangeDialogBound = dialog;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
return dialog;
|
|
|
|
|
|
}
|
|
|
|
|
|
function closeRecoveryChanges() {
|
|
|
|
|
|
++recoveryReviewRequest;
|
|
|
|
|
|
recoveryReviewEntry = null;
|
|
|
|
|
|
$('#recovery-change-dialog')?.close();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
async function openRecoveryChanges() {
|
|
|
|
|
|
closeRecoveryDetail();
|
|
|
|
|
|
const dialog = recoveryChangesDialog(),
|
|
|
|
|
|
request = ++recoveryReviewRequest,
|
|
|
|
|
|
user = recoveryChangeUser();
|
|
|
|
|
|
recoveryReviewEntry = null;
|
|
|
|
|
|
dialog.innerHTML = `${button('关闭', 'recovery-changes-close', 'dialog-close', 'aria-label="关闭回收更正列表"')}<h2 id="recovery-change-title">回收更正${state.user.role === 'owner' ? '复核' : '申请'}</h2><div id="recovery-change-list">正在读取…</div>`;
|
|
|
|
|
|
if (!dialog.open) dialog.showModal();
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = await api('/api/recovery-changes?' + new URLSearchParams({ status: recoveryChangesStatus }));
|
|
|
|
|
|
if (request !== recoveryReviewRequest || user !== recoveryChangeUser() || !dialog.open) return;
|
|
|
|
|
|
if (
|
|
|
|
|
|
!Array.isArray(result.requests) ||
|
|
|
|
|
|
!['pending', 'approved', 'rejected'].every(
|
|
|
|
|
|
key => Number.isSafeInteger(result.counts?.[key]) && result.counts[key] >= 0,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
throw new Error('申请列表未完整返回');
|
|
|
|
|
|
const rows = result.requests.map(validateRecoveryChangeRequest);
|
|
|
|
|
|
$('#recovery-change-list').innerHTML = `<div class="change-list-tabs">${[
|
|
|
|
|
|
['pending', '待复核'],
|
|
|
|
|
|
['approved', '已同意'],
|
|
|
|
|
|
['rejected', '已驳回'],
|
|
|
|
|
|
['all', '全部'],
|
2026-12-04 08:58:53 -08:00
|
|
|
|
]
|
2026-11-04 22:18:56 +08:00
|
|
|
|
.map(([status, name]) =>
|
|
|
|
|
|
button(
|
|
|
|
|
|
name + (status === 'all' ? '' : '(' + result.counts[status] + ')'),
|
|
|
|
|
|
'recovery-changes-filter',
|
|
|
|
|
|
status === recoveryChangesStatus ? 'active' : '',
|
|
|
|
|
|
`data-status="${status}"`,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
),
|
|
|
|
|
|
)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
.join(
|
|
|
|
|
|
'',
|
|
|
|
|
|
)}</div><div class="table-wrap"><table><thead><tr><th>原记录</th><th>动作</th><th>申请人</th><th>状态</th><th>操作</th></tr></thead><tbody>${rows.map(row => `<tr><td>${e(row.before.date)} · ${e(row.before.customer)}</td><td>${row.action === 'cancel' ? '取消' : '修改'}</td><td>${e(row.requesterName)}</td><td>${e(recoveryChangeStatus(row))}${row.stale ? '<div class="small danger-text">资料已有变动</div>' : ''}</td><td>${button(state.user.role === 'owner' && row.status === 'pending' ? '查看并复核' : '查看申请', 'recovery-change-open', '', `data-id="${e(row.id)}"`)}</td></tr>`).join('') || '<tr><td colspan="5" class="muted">没有符合状态的申请。</td></tr>'}</tbody></table></div>`;
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (request === recoveryReviewRequest && user === recoveryChangeUser() && dialog.open)
|
|
|
|
|
|
$('#recovery-change-list').innerHTML =
|
|
|
|
|
|
`<div class="error">${e(err.message)}</div>${button('重新读取', 'recovery-changes-open')}`;
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
async function openRecoveryChange(id) {
|
|
|
|
|
|
if (busy) throw new Error('正在保存,请稍候。');
|
|
|
|
|
|
closeRecoveryDetail();
|
|
|
|
|
|
const dialog = recoveryChangesDialog(),
|
|
|
|
|
|
request = ++recoveryReviewRequest,
|
|
|
|
|
|
user = recoveryChangeUser();
|
|
|
|
|
|
recoveryReviewEntry = null;
|
|
|
|
|
|
dialog.innerHTML = `${button('关闭', 'recovery-changes-close', 'dialog-close', 'aria-label="关闭回收更正申请"')}<h2 id="recovery-change-title">回收更正申请</h2><p>正在读取…</p>`;
|
|
|
|
|
|
if (!dialog.open) dialog.showModal();
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await api('/api/recovery-changes/' + encodeURIComponent(id));
|
|
|
|
|
|
if (request !== recoveryReviewRequest || user !== recoveryChangeUser() || !dialog.open) return;
|
|
|
|
|
|
const row = validateRecoveryChangeRequest(response);
|
|
|
|
|
|
if (row.id !== id) throw new Error('申请编号不一致');
|
|
|
|
|
|
recoveryReviewEntry = { request: row, user, invalid: false, kind: null };
|
|
|
|
|
|
renderRecoveryChangeReview();
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (request === recoveryReviewRequest && user === recoveryChangeUser() && dialog.open)
|
|
|
|
|
|
dialog.innerHTML = `${button('关闭', 'recovery-changes-close', 'dialog-close')}<h2 id="recovery-change-title">申请未读到</h2><div class="error">${e(err.message)}</div>${button('重新读取申请', 'recovery-change-open', '', `data-id="${e(id)}"`)}`;
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function renderRecoveryChangeReview() {
|
|
|
|
|
|
const entry = recoveryReviewEntry;
|
|
|
|
|
|
if (!entry || entry.user !== recoveryChangeUser()) return;
|
|
|
|
|
|
const row = entry.request,
|
|
|
|
|
|
owner = state.user.role === 'owner' && row.status === 'pending';
|
|
|
|
|
|
recoveryChangesDialog().innerHTML = `${button('关闭', 'recovery-changes-close', 'dialog-close', 'aria-label="关闭回收更正申请"')}<h2 id="recovery-change-title">${row.action === 'cancel' ? '取消' : '修改'}回收/置换记录</h2>${button('返回申请列表', 'recovery-changes-open', 'quiet')}<p>${e(recoveryChangeStatus(row))} · ${e(row.requesterName)}提交${row.reviewerName ? ' · ' + e(row.reviewerName) + '复核' : ''}</p><p class="change-reason">申请原因:${e(row.reason)}</p>${row.note ? `<p class="change-reason">复核意见:${e(row.note)}</p>` : ''}<p class="small muted">原记录在批准前保持有效。更正关联收付款、购料、库存和利润;系统不会发起真实转账。</p>${recoveryChangeComparisonHTML(row)}${row.stale ? '<div class="error">关联资料已有变动,不能同意这份旧申请。可驳回后重新申请。</div>' : ''}<div id="recovery-change-review-actions">${owner ? `<div class="actions">${row.canApprove && !row.stale ? button('同意申请', 'recovery-change-decision', 'primary', 'data-kind="approve"') : ''}${button('驳回申请', 'recovery-change-decision', '', 'data-kind="reject"')}</div>` : ''}</div><div id="recovery-change-review-error" aria-live="polite"></div>`;
|
2026-11-14 01:16:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
function showRecoveryDecision(kind) {
|
|
|
|
|
|
const entry = recoveryReviewEntry,
|
|
|
|
|
|
row = entry?.request;
|
|
|
|
|
|
if (state.user.role !== 'owner') throw new Error('请使用店主账号复核。');
|
|
|
|
|
|
if (
|
|
|
|
|
|
!entry ||
|
|
|
|
|
|
entry.invalid ||
|
|
|
|
|
|
entry.user !== recoveryChangeUser() ||
|
|
|
|
|
|
row.status !== 'pending' ||
|
|
|
|
|
|
!['approve', 'reject'].includes(kind)
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-14 01:16:35 +08:00
|
|
|
|
throw new Error('请重新读取申请后核对。');
|
|
|
|
|
|
if (kind === 'approve' && (!row.canApprove || row.stale)) throw new Error('申请已有变动,请重新读取。');
|
|
|
|
|
|
entry.kind = kind;
|
|
|
|
|
|
$('#recovery-change-review-actions').innerHTML =
|
|
|
|
|
|
`<form id="recovery-change-review-form" data-kind="${kind}"><label>${kind === 'reject' ? '驳回意见' : '复核备注(选填)'}<textarea name="note" maxlength="200" rows="2" ${kind === 'reject' ? 'required' : ''}></textarea></label>${kind === 'approve' ? '<label class="check"><input type="checkbox" name="verified" required>已核对修改前后记录、收付款、购料、库存与利润。</label>' : ''}<div class="form-actions">${button('取消', 'recovery-change-review-cancel', 'quiet')}<button type="submit" class="primary">${kind === 'approve' ? '确认同意' : '确认驳回'}</button></div></form>`;
|
|
|
|
|
|
applyRecoveryChangeRestrictions();
|
|
|
|
|
|
}
|
|
|
|
|
|
async function submitRecoveryDecision(form) {
|
|
|
|
|
|
const entry = recoveryReviewEntry,
|
|
|
|
|
|
row = entry?.request,
|
|
|
|
|
|
kind = form.dataset.kind;
|
|
|
|
|
|
if (
|
|
|
|
|
|
state.user.role !== 'owner' ||
|
|
|
|
|
|
!entry ||
|
|
|
|
|
|
entry.invalid ||
|
|
|
|
|
|
entry.user !== recoveryChangeUser() ||
|
|
|
|
|
|
entry.kind !== kind ||
|
|
|
|
|
|
row.status !== 'pending' ||
|
|
|
|
|
|
form !== $('#recovery-change-review-form')
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-14 01:16:35 +08:00
|
|
|
|
throw new Error('请使用店主账号重新读取申请后复核。');
|
|
|
|
|
|
if (kind === 'approve' && (!row.canApprove || row.stale)) throw new Error('申请已有变动,请重新读取。');
|
|
|
|
|
|
const note = recoveryText(form.querySelector('[name="note"]')?.value);
|
|
|
|
|
|
if (note.length > 200 || (kind === 'reject' && !note)) throw new Error('驳回须填写意见,复核意见最多200字。');
|
|
|
|
|
|
if (kind === 'approve' && !form.querySelector('[name="verified"]')?.checked)
|
|
|
|
|
|
throw new Error('请核对申请内容并勾选确认。');
|
|
|
|
|
|
const input = { revision: row.revision, note, ...(kind === 'approve' ? { verified: true } : {}) },
|
|
|
|
|
|
url = '/api/recovery-changes/' + encodeURIComponent(row.id) + '/' + kind,
|
|
|
|
|
|
view = captureRecoveryChangeView();
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = await mutate(url, input);
|
|
|
|
|
|
await finishRecoveryChangeSave(url, input, result, view);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (view.user === recoveryChangeUser()) recoveryChangeError(err, { kind, id: row.id });
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
async function openRecoveryCancel(id) {
|
|
|
|
|
|
if (busy || pending()) throw new Error('请先恢复未确认的操作。');
|
|
|
|
|
|
closeRecoveryDetail();
|
|
|
|
|
|
const dialog = recoveryChangesDialog(),
|
|
|
|
|
|
request = ++recoveryReviewRequest,
|
|
|
|
|
|
user = recoveryChangeUser();
|
|
|
|
|
|
recoveryReviewEntry = null;
|
|
|
|
|
|
dialog.innerHTML = `${button('关闭', 'recovery-changes-close', 'dialog-close', 'aria-label="关闭取消回收记录"')}<h2 id="recovery-change-title">取消回收/置换记录</h2><p>正在读取…</p>`;
|
|
|
|
|
|
if (!dialog.open) dialog.showModal();
|
|
|
|
|
|
try {
|
2026-11-05 00:04:01 +08:00
|
|
|
|
const response = await api('/api/recoveries/' + encodeURIComponent(id) + '/edit');
|
|
|
|
|
|
if (request !== recoveryReviewRequest || user !== recoveryChangeUser() || !dialog.open) return;
|
|
|
|
|
|
const data = validateRecoveryEditDetail(response, id);
|
|
|
|
|
|
if (!data.permissions.canCancel || data.pending)
|
|
|
|
|
|
throw new Error(data.permissions.reason || '记录已取消或有待审批申请,当前不能取消。');
|
|
|
|
|
|
recoveryReviewEntry = { user, data, kind: 'cancel', invalid: false };
|
2026-11-08 15:47:07 +08:00
|
|
|
|
dialog.innerHTML = `${button('关闭', 'recovery-changes-close', 'dialog-close', 'aria-label="关闭取消回收记录"')}<h2 id="recovery-change-title">取消这笔${e(recoveryModeNames[data.inputs.mode])}记录</h2><p>${e(data.record.number)} · ${e(data.inputs.date)} · ${e(data.inputs.customer)}</p><p class="small muted">取消后冲正整套关联收付款、购料与库存,原身份和更正记录保留。系统不会发起真实退款。</p>${recoveryMaterialBatchHTML(data.record.materialBatch)}${recoveryBlockedHTML(data.blockedUsages, false)}${recoveryCancellationMaterialHTML()}<form id="recovery-cancel-form">${legacyCountConfirmationHTML(data.permissions)}<label>取消原因<textarea name="reason" required maxlength="200" rows="2"></textarea></label><label class="check"><input type="checkbox" name="verified" required>已核实这笔记录需要取消,并核对关联用料自动重算与实际收付款。</label><div id="recovery-cancel-error" aria-live="polite"></div><div class="form-actions">${button('返回', 'recovery-changes-close', 'quiet')}<button class="primary" type="submit">${data.permissions.requiresApproval ? '提交取消申请' : '确认取消记录'}</button></div></form>`;
|
2026-11-04 22:18:56 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (request === recoveryReviewRequest && user === recoveryChangeUser() && dialog.open)
|
|
|
|
|
|
dialog.innerHTML = `${button('关闭', 'recovery-changes-close', 'dialog-close')}<h2 id="recovery-change-title">暂不能取消</h2><div class="error">${e(err.message)}</div>${button('重新读取', 'recovery-edit-cancel', '', `data-id="${e(id)}"`)}`;
|
|
|
|
|
|
}
|
2026-11-08 15:47:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
async function submitRecoveryCancel(form) {
|
|
|
|
|
|
const entry = recoveryReviewEntry,
|
|
|
|
|
|
data = entry?.data;
|
|
|
|
|
|
if (
|
|
|
|
|
|
!entry ||
|
|
|
|
|
|
entry.invalid ||
|
|
|
|
|
|
entry.kind !== 'cancel' ||
|
|
|
|
|
|
entry.user !== recoveryChangeUser() ||
|
|
|
|
|
|
!data.permissions.canCancel ||
|
|
|
|
|
|
data.pending
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-08 15:47:07 +08:00
|
|
|
|
throw new Error('请重新读取回收记录后核对取消。');
|
|
|
|
|
|
const reason = recoveryText(form.querySelector('[name="reason"]')?.value);
|
|
|
|
|
|
if (!reason || reason.length > 200) throw new Error('取消原因请填写1~200字。');
|
|
|
|
|
|
if (!form.querySelector('[name="verified"]')?.checked) throw new Error('请核实取消记录及关联账务,并勾选确认。');
|
|
|
|
|
|
const input = {
|
|
|
|
|
|
revision: data.revision,
|
|
|
|
|
|
reason,
|
|
|
|
|
|
verified: true,
|
|
|
|
|
|
...requireLegacyCountConfirmation(form, data.permissions),
|
|
|
|
|
|
},
|
|
|
|
|
|
url = '/api/recoveries/' + encodeURIComponent(data.id) + '/cancel',
|
|
|
|
|
|
view = captureRecoveryChangeView();
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = await mutate(url, input);
|
|
|
|
|
|
await finishRecoveryChangeSave(url, input, result, view);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (view.user === recoveryChangeUser()) recoveryChangeError(err, { kind: 'cancel', id: data.id });
|
2026-11-04 22:18:56 +08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
function recoveryChangeOperation(url) {
|
|
|
|
|
|
let found = String(url).match(/^\/api\/recoveries\/([^/]+)\/(update|cancel)$/);
|
|
|
|
|
|
if (found) return { id: decodeURIComponent(found[1]), kind: found[2] };
|
|
|
|
|
|
found = String(url).match(/^\/api\/recovery-changes\/([^/]+)\/(approve|reject)$/);
|
|
|
|
|
|
return found ? { id: decodeURIComponent(found[1]), kind: found[2] } : null;
|
|
|
|
|
|
}
|
|
|
|
|
|
function captureRecoveryChangeView() {
|
|
|
|
|
|
return {
|
|
|
|
|
|
user: recoveryChangeUser(),
|
|
|
|
|
|
tab,
|
|
|
|
|
|
edit: recoveryEditEntry,
|
|
|
|
|
|
review: recoveryReviewEntry,
|
|
|
|
|
|
dialog: $('#recovery-change-dialog'),
|
|
|
|
|
|
version: recoveryReviewRequest,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
function recoveryChangeError(err, operation) {
|
|
|
|
|
|
const review = operation && operation.kind !== 'update',
|
|
|
|
|
|
entry = review ? recoveryReviewEntry : recoveryEditEntry;
|
|
|
|
|
|
if ([403, 404, 409].includes(err.status) && entry) {
|
|
|
|
|
|
entry.invalid = true;
|
|
|
|
|
|
const checked = review
|
|
|
|
|
|
? $('#recovery-change-review-form [name="verified"]') || $('#recovery-cancel-form [name="verified"]')
|
|
|
|
|
|
: recoveryField($('#recovery-form'), 'verified');
|
|
|
|
|
|
if (checked) checked.checked = false;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const target = $(
|
|
|
|
|
|
review
|
|
|
|
|
|
? entry?.kind === 'cancel'
|
|
|
|
|
|
? '#recovery-cancel-error'
|
|
|
|
|
|
: '#recovery-change-review-error'
|
|
|
|
|
|
: '#recovery-form-error',
|
|
|
|
|
|
);
|
|
|
|
|
|
if (target)
|
|
|
|
|
|
target.innerHTML = `<div class="error">${e(err.message)}${pending() ? ' ' + button('恢复原操作', 'retry', 'danger') : operation ? (review ? button('重新读取', operation.kind === 'cancel' ? 'recovery-edit-cancel' : 'recovery-change-open', '', `data-id="${e(operation.id)}"`) : button('重新读取并核对', 'recovery-edit-reload', '', `data-id="${e(operation.id)}"`)) : ''}</div>`;
|
|
|
|
|
|
if (!review && recoveryEditEntry) rememberRecoveryEditDraft();
|
|
|
|
|
|
applyRecoveryChangeRestrictions();
|
|
|
|
|
|
toast(err.message, true);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
async function finishRecoveryChangeSave(url, input, result, savedView) {
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const operation = recoveryChangeOperation(url),
|
|
|
|
|
|
view = savedView || captureRecoveryChangeView();
|
|
|
|
|
|
if (view.user !== recoveryChangeUser()) return;
|
|
|
|
|
|
const decision = ['approve', 'reject'].includes(operation.kind),
|
|
|
|
|
|
editActive = !!view.edit && view.edit === recoveryEditEntry,
|
|
|
|
|
|
reviewActive =
|
|
|
|
|
|
!!view.review &&
|
|
|
|
|
|
view.review === recoveryReviewEntry &&
|
|
|
|
|
|
view.dialog?.open &&
|
|
|
|
|
|
view.version === recoveryReviewRequest,
|
|
|
|
|
|
showDetail = operation.kind === 'update' ? editActive : operation.kind === 'cancel' && reviewActive,
|
|
|
|
|
|
message = decision
|
|
|
|
|
|
? operation.kind === 'approve'
|
|
|
|
|
|
? '已同意回收更正'
|
|
|
|
|
|
: '已驳回回收更正'
|
|
|
|
|
|
: result.applied
|
|
|
|
|
|
? operation.kind === 'cancel'
|
|
|
|
|
|
? '回收记录已取消'
|
|
|
|
|
|
: '回收记录已修改'
|
|
|
|
|
|
: '回收更正申请已提交,待店主复核',
|
|
|
|
|
|
recoveryId = decision ? result.recoveryId : result.id || operation.id;
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (operation.kind === 'update' && editActive) {
|
|
|
|
|
|
localStorage.removeItem(recoveryEditDraftKey(view.edit.id));
|
|
|
|
|
|
recoveryEditEntry = null;
|
|
|
|
|
|
++recoveryEditRequest;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (reviewActive) closeRecoveryChanges();
|
|
|
|
|
|
const editVersion = recoveryEditRequest,
|
|
|
|
|
|
reviewVersion = recoveryReviewRequest;
|
|
|
|
|
|
invalidateReport();
|
|
|
|
|
|
invalidateMaterialPools();
|
|
|
|
|
|
if (typeof invalidatePriceLockViews === 'function') invalidatePriceLockViews();
|
|
|
|
|
|
inventoryPage = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
await refresh(false, () => view.user === recoveryChangeUser());
|
|
|
|
|
|
if (view.user !== recoveryChangeUser()) return;
|
|
|
|
|
|
if (tab === 'report') await renderReport();
|
|
|
|
|
|
else if (tab === 'orders') await renderOrders();
|
|
|
|
|
|
else if (tab === 'recoveries') {
|
|
|
|
|
|
const filters = recoveryGetFilters(),
|
|
|
|
|
|
date =
|
|
|
|
|
|
result.recovery?.date ||
|
|
|
|
|
|
result.after?.date ||
|
|
|
|
|
|
result.before?.date ||
|
|
|
|
|
|
input.date ||
|
|
|
|
|
|
view.edit?.data.inputs.date ||
|
|
|
|
|
|
view.review?.data?.inputs.date;
|
|
|
|
|
|
if (date && (editActive || reviewActive) && view.tab === tab) Object.assign(filters, { from: date, date });
|
|
|
|
|
|
await renderRecoveries();
|
|
|
|
|
|
}
|
|
|
|
|
|
await loadRecoveryChangeCount();
|
|
|
|
|
|
if (
|
|
|
|
|
|
!decision &&
|
|
|
|
|
|
showDetail &&
|
|
|
|
|
|
view.tab === tab &&
|
|
|
|
|
|
view.user === recoveryChangeUser() &&
|
|
|
|
|
|
!recoveryEditEntry &&
|
|
|
|
|
|
editVersion === recoveryEditRequest &&
|
|
|
|
|
|
reviewVersion === recoveryReviewRequest
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-04 22:18:56 +08:00
|
|
|
|
await openRecovery(recoveryId, result.recovery);
|
|
|
|
|
|
toast(message);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (view.user === recoveryChangeUser()) toast(message + ',汇总刷新失败,请重新读取查看结果。', true);
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
document.addEventListener('input', event => {
|
|
|
|
|
|
const form = event.target.closest?.('#recovery-form');
|
|
|
|
|
|
if (form?.dataset.recoveryEdit === 'true') {
|
|
|
|
|
|
if (event.target.name !== 'verified' && recoveryField(form, 'verified'))
|
|
|
|
|
|
recoveryField(form, 'verified').checked = false;
|
|
|
|
|
|
syncRecoveryEditMode(form);
|
|
|
|
|
|
rememberRecoveryEditDraft(form);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
});
|
|
|
|
|
|
document.addEventListener('change', event => {
|
|
|
|
|
|
const form = event.target.closest?.('#recovery-form');
|
|
|
|
|
|
if (form?.dataset.recoveryEdit === 'true') {
|
|
|
|
|
|
if (event.target.name !== 'verified' && recoveryField(form, 'verified'))
|
|
|
|
|
|
recoveryField(form, 'verified').checked = false;
|
|
|
|
|
|
syncRecoveryEditMode(form);
|
|
|
|
|
|
rememberRecoveryEditDraft(form);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
});
|
|
|
|
|
|
document.addEventListener('click', async event => {
|
|
|
|
|
|
const node = event.target.closest?.('button'),
|
|
|
|
|
|
action = node?.dataset.action;
|
|
|
|
|
|
if (!action || node.disabled) return;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (action === 'recovery-edit-open') return await openRecoveryEdit(node.dataset.id);
|
|
|
|
|
|
if (action === 'recovery-edit-cancel') return await openRecoveryCancel(node.dataset.id);
|
|
|
|
|
|
if (action === 'recovery-edit-reload') return await reloadRecoveryEdit();
|
|
|
|
|
|
if (action === 'recovery-edit-keep') {
|
|
|
|
|
|
const entry = recoveryEditEntry;
|
|
|
|
|
|
if (!entry?.latest || !entry.latest.permissions.canEdit || entry.latest.pending)
|
|
|
|
|
|
throw new Error('请先读取最新可编辑记录。');
|
|
|
|
|
|
entry.data = entry.latest;
|
|
|
|
|
|
entry.revision = entry.latest.revision;
|
|
|
|
|
|
entry.latest = null;
|
|
|
|
|
|
entry.invalid = false;
|
|
|
|
|
|
entry.draft.verified = false;
|
|
|
|
|
|
renderRecoveryEdit();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'recovery-edit-close') {
|
|
|
|
|
|
rememberRecoveryEditDraft();
|
|
|
|
|
|
const id = recoveryEditEntry?.id;
|
|
|
|
|
|
recoveryEditEntry = null;
|
|
|
|
|
|
++recoveryEditRequest;
|
|
|
|
|
|
await renderRecoveries();
|
|
|
|
|
|
if (id) await openRecovery(id);
|
|
|
|
|
|
return;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (action === 'recovery-edit-add-row' || action === 'recovery-edit-remove-row') {
|
|
|
|
|
|
const entry = recoveryEditEntry;
|
|
|
|
|
|
if (!entry || entry.invalid) throw new Error('请重新读取回收记录。');
|
|
|
|
|
|
rememberRecoveryEditDraft();
|
|
|
|
|
|
const kind = node.dataset.kind,
|
|
|
|
|
|
key = { old: 'old_lines', charge: 'charges', fee: 'fee_items' }[kind];
|
|
|
|
|
|
if (!key) return;
|
|
|
|
|
|
const rows = entry.draft[key];
|
|
|
|
|
|
if (action === 'recovery-edit-remove-row') rows.splice(Number(node.dataset.index), 1);
|
|
|
|
|
|
else {
|
|
|
|
|
|
if (kind !== 'old' && !rows.length)
|
|
|
|
|
|
rows.push(
|
|
|
|
|
|
kind === 'charge'
|
|
|
|
|
|
? { name: entry.draft.charge_name, amount: entry.draft.charge_amount }
|
2026-12-04 08:58:53 -08:00
|
|
|
|
: {
|
2026-11-04 22:18:56 +08:00
|
|
|
|
key: 'other',
|
|
|
|
|
|
note: entry.draft.fee_note,
|
|
|
|
|
|
amount: entry.draft.fee_amount,
|
|
|
|
|
|
payer: entry.draft.fee_payer,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
2026-11-04 22:18:56 +08:00
|
|
|
|
);
|
|
|
|
|
|
rows.push(
|
|
|
|
|
|
kind === 'old'
|
|
|
|
|
|
? { name: 'Au旧料', incoming_weight: '', melted_weight: '', purity: '100' }
|
|
|
|
|
|
: kind === 'charge'
|
|
|
|
|
|
? { name: '其他收费', amount: '0' }
|
2026-12-04 08:58:53 -08:00
|
|
|
|
: {
|
2026-11-04 22:18:56 +08:00
|
|
|
|
key: 'manual:' + crypto.randomUUID(),
|
|
|
|
|
|
note: '其他费用',
|
|
|
|
|
|
amount: '0',
|
|
|
|
|
|
payer: entry.data.inputs.member,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
2026-11-04 22:18:56 +08:00
|
|
|
|
);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
entry.draft.verified = false;
|
|
|
|
|
|
renderRecoveryEdit();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'recovery-changes-open') return await openRecoveryChanges();
|
|
|
|
|
|
if (action === 'recovery-changes-close') return closeRecoveryChanges();
|
|
|
|
|
|
if (action === 'recovery-changes-filter') {
|
|
|
|
|
|
if (['pending', 'approved', 'rejected', 'all'].includes(node.dataset.status))
|
|
|
|
|
|
recoveryChangesStatus = node.dataset.status;
|
|
|
|
|
|
return await openRecoveryChanges();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (action === 'recovery-change-open') return await openRecoveryChange(node.dataset.id);
|
|
|
|
|
|
if (action === 'recovery-change-decision') return showRecoveryDecision(node.dataset.kind);
|
|
|
|
|
|
if (action === 'recovery-change-review-cancel') {
|
|
|
|
|
|
if (recoveryReviewEntry) recoveryReviewEntry.kind = null;
|
|
|
|
|
|
return renderRecoveryChangeReview();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (action === 'recovery-change-open-lock') {
|
|
|
|
|
|
closeRecoveryChanges();
|
|
|
|
|
|
closeRecoveryDetail();
|
|
|
|
|
|
return await openPriceLock(node.dataset.id);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
if (action === 'recovery-change-open-order') {
|
|
|
|
|
|
closeRecoveryChanges();
|
|
|
|
|
|
closeRecoveryDetail();
|
|
|
|
|
|
return await openOrder(node.dataset.id);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
toast(err.message, true);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
});
|
|
|
|
|
|
document.addEventListener('submit', async event => {
|
|
|
|
|
|
const form = event.target;
|
|
|
|
|
|
if (!['recovery-cancel-form', 'recovery-change-review-form'].includes(form.id)) return;
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (form.id === 'recovery-cancel-form') await submitRecoveryCancel(form);
|
|
|
|
|
|
else await submitRecoveryDecision(form);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
recoveryChangeError(err, {
|
|
|
|
|
|
kind: form.id === 'recovery-cancel-form' ? 'cancel' : form.dataset.kind,
|
|
|
|
|
|
id: recoveryReviewEntry?.request?.id || recoveryReviewEntry?.data?.id,
|
|
|
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-04 22:18:56 +08:00
|
|
|
|
});
|