2026-10-23 00:38:04 +08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
// 取消是录错作废,申请与复核均沿用原操作号,不产生真实退款。
|
|
|
|
|
|
let orderCancellationDetail = null,
|
|
|
|
|
|
orderCancellationFormEntry = null;
|
|
|
|
|
|
let orderCancellationCountRequest = 0,
|
|
|
|
|
|
orderCancellationListRequest = 0,
|
|
|
|
|
|
orderCancellationListStatus = 'pending';
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const orderCancellationStatus = { pending: '待店主复核', approved: '已同意取消', rejected: '已驳回' };
|
2026-10-23 00:38:04 +08:00
|
|
|
|
function orderCancellationUser() {
|
|
|
|
|
|
return session?.user?.id || '';
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderCancellationTime(value) {
|
|
|
|
|
|
const date = new Date(value);
|
|
|
|
|
|
return Number.isNaN(date.getTime()) ? '时间未提供' : date.toLocaleString('zh-CN');
|
|
|
|
|
|
}
|
|
|
|
|
|
function validateOrderCancellationRequest(request, orderId) {
|
|
|
|
|
|
const summary = request?.summary,
|
|
|
|
|
|
nullable = value => value === null || Number.isSafeInteger(value);
|
|
|
|
|
|
if (
|
|
|
|
|
|
!request ||
|
|
|
|
|
|
typeof request.id !== 'string' ||
|
|
|
|
|
|
!request.id ||
|
|
|
|
|
|
typeof request.order_id !== 'string' ||
|
|
|
|
|
|
(orderId && request.order_id !== orderId) ||
|
|
|
|
|
|
!Object.hasOwn(orderCancellationStatus, request.status) ||
|
|
|
|
|
|
!Number.isSafeInteger(request.order_revision) ||
|
|
|
|
|
|
typeof request.reason !== 'string' ||
|
|
|
|
|
|
typeof request.requester_name !== 'string' ||
|
|
|
|
|
|
typeof request.created !== 'string' ||
|
|
|
|
|
|
typeof request.stale !== 'boolean' ||
|
|
|
|
|
|
typeof request.canApprove !== 'boolean' ||
|
|
|
|
|
|
!summary ||
|
|
|
|
|
|
!['total', 'netReceived', 'directCost'].every(key => Number.isSafeInteger(summary[key])) ||
|
|
|
|
|
|
!['goodsCost', 'profit', 'settlementDue'].every(key => nullable(summary[key])) ||
|
|
|
|
|
|
!Array.isArray(summary.inventory) ||
|
|
|
|
|
|
!summary.inventory.every(
|
|
|
|
|
|
item => typeof item.date === 'string' && typeof item.pocket === 'string' && Number.isSafeInteger(item.mg),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2026-10-23 00:38:04 +08:00
|
|
|
|
throw new Error('取消申请资料未完整返回,请重新读取订单后复核。');
|
|
|
|
|
|
return request;
|
|
|
|
|
|
}
|
|
|
|
|
|
function useOrderCancellationDetail(order, result, error) {
|
|
|
|
|
|
orderCancellationFormEntry = null;
|
|
|
|
|
|
const entry = { id: order.id, user: orderCancellationUser(), revision: order.revision, data: null, error: '' };
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (error) throw error;
|
|
|
|
|
|
if (
|
|
|
|
|
|
!result ||
|
|
|
|
|
|
typeof result.eligible !== 'boolean' ||
|
|
|
|
|
|
typeof result.canApprove !== 'boolean' ||
|
|
|
|
|
|
!Number.isSafeInteger(result.revision) ||
|
|
|
|
|
|
!Array.isArray(result.requests) ||
|
|
|
|
|
|
(result.pending !== null && typeof result.pending !== 'object')
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-23 00:38:04 +08:00
|
|
|
|
throw new Error('取消复核资料尚未完整返回,请重新读取。');
|
|
|
|
|
|
if (result.revision !== order.revision) throw new Error('订单已有新记录,请重新读取订单后复核取消申请。');
|
|
|
|
|
|
result.requests.forEach(request => validateOrderCancellationRequest(request, order.id));
|
|
|
|
|
|
if (result.pending) validateOrderCancellationRequest(result.pending, order.id);
|
|
|
|
|
|
entry.data = result;
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
entry.error = err.name === 'AbortError' ? '取消申请读取超时,请重新读取。' : err.message;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-23 00:38:04 +08:00
|
|
|
|
orderCancellationDetail = entry;
|
|
|
|
|
|
return entry;
|
|
|
|
|
|
}
|
|
|
|
|
|
function currentOrderCancellation(order = currentOrder) {
|
|
|
|
|
|
return order &&
|
|
|
|
|
|
orderCancellationDetail?.id === order.id &&
|
|
|
|
|
|
orderCancellationDetail.user === orderCancellationUser() &&
|
|
|
|
|
|
orderCancellationDetail.revision === order.revision
|
|
|
|
|
|
? orderCancellationDetail
|
|
|
|
|
|
: null;
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderCancellationPending(order) {
|
|
|
|
|
|
return currentOrderCancellation(order)?.data?.pending || order.cancellationPending || null;
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderCancellationApproved(order) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
order.cancellationApproved ||
|
|
|
|
|
|
currentOrderCancellation(order)?.data?.requests.find(request => request.status === 'approved') ||
|
|
|
|
|
|
null
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
2026-10-23 00:38:04 +08:00
|
|
|
|
}
|
2026-10-23 11:58:19 +08:00
|
|
|
|
function orderCancellationCanDirect(order = currentOrder) {
|
|
|
|
|
|
const data = currentOrderCancellation(order)?.data;
|
|
|
|
|
|
if (
|
|
|
|
|
|
!data?.eligible ||
|
|
|
|
|
|
data.pending ||
|
|
|
|
|
|
data.directToday !== true ||
|
|
|
|
|
|
!/^\d{4}-\d{2}-\d{2}$/.test(data.directActionDate || '') ||
|
|
|
|
|
|
order.date !== data.directActionDate ||
|
|
|
|
|
|
order.price_lock_id ||
|
|
|
|
|
|
!['draft', 'confirmed', 'delivered'].includes(order.status)
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-23 11:58:19 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
try {
|
|
|
|
|
|
validateOrderCancellationSummary(data.summary);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderCancellationRecordStatus(request) {
|
|
|
|
|
|
return request.direct === true ? '当天直接取消' : orderCancellationStatus[request.status];
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-23 00:38:04 +08:00
|
|
|
|
function orderCancellationActionHTML(order) {
|
|
|
|
|
|
const entry = currentOrderCancellation(order),
|
|
|
|
|
|
data = entry?.data;
|
2026-10-26 20:25:31 +08:00
|
|
|
|
if (typeof orderOwnershipPending === 'function' ? orderOwnershipPending(order) : order.ownershipPending) return '';
|
2026-10-23 09:49:36 +08:00
|
|
|
|
if (
|
|
|
|
|
|
order.price_lock_id ||
|
|
|
|
|
|
!['draft', 'confirmed', 'delivered'].includes(order.status) ||
|
|
|
|
|
|
orderCancellationPending(order) ||
|
|
|
|
|
|
(typeof orderChangePending === 'function' ? orderChangePending(order) : order.changePending) ||
|
|
|
|
|
|
!data?.eligible
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-23 09:49:36 +08:00
|
|
|
|
return '';
|
2026-10-23 11:58:19 +08:00
|
|
|
|
return button(
|
|
|
|
|
|
orderCancellationCanDirect(order) ? '取消订单' : '申请取消',
|
|
|
|
|
|
'request-order-cancellation',
|
|
|
|
|
|
'danger',
|
|
|
|
|
|
`data-id="${e(order.id)}"`,
|
|
|
|
|
|
);
|
2026-10-23 00:38:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
function orderCancellationHistoryHTML(request) {
|
2026-10-23 11:58:19 +08:00
|
|
|
|
if (request.direct === true)
|
|
|
|
|
|
return `<li><div class="cancellation-record-title"><strong>当天直接取消</strong></div><div>经办人:${e(request.operator_name || request.requester_name)} · ${e(orderCancellationTime(request.executedAt || request.created))}</div><p class="cancellation-reason">${e(request.reason)}</p>${request.note ? `<p class="small cancellation-reason">处理备注:${e(request.note)}</p>` : ''}</li>`;
|
2026-10-23 00:38:04 +08:00
|
|
|
|
return `<li><div class="cancellation-record-title"><strong>${e(orderCancellationStatus[request.status])}</strong>${request.stale && request.status === 'pending' ? '<span class="badge danger">订单已有变动</span>' : ''}</div><div>申请人:${e(request.requester_name)} · ${e(orderCancellationTime(request.created))}</div><p class="cancellation-reason">${e(request.reason)}</p>${request.status !== 'pending' ? `<div class="small muted">复核人:${e(request.reviewer_name || '未提供')} · ${e(orderCancellationTime(request.reviewed))}${request.note ? `<p class="cancellation-reason">${request.status === 'rejected' ? '驳回意见' : '复核备注'}:${e(request.note)}</p>` : ''}</div>` : ''}</li>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderCancellationDetailHTML(order) {
|
|
|
|
|
|
if (order.price_lock_id) return '';
|
|
|
|
|
|
const entry = currentOrderCancellation(order),
|
|
|
|
|
|
data = entry?.data,
|
|
|
|
|
|
request = orderCancellationPending(order),
|
|
|
|
|
|
approved = orderCancellationApproved(order);
|
|
|
|
|
|
if (!data)
|
|
|
|
|
|
return `<section class="panel order-cancellation-panel"><h2>取消申请与复核</h2><p class="${entry?.error ? 'danger-text' : 'muted'}" role="status">${e(entry?.error || '取消申请资料尚未读取,请先重新读取订单。')}</p>${button('重新读取订单', 'reload-order-cancellation', '', `data-id="${e(order.id)}"`)}</section>`;
|
|
|
|
|
|
if (!request && !data.requests.length) return '';
|
|
|
|
|
|
const canReview = state.user.role === 'owner' && data.canApprove && request?.status === 'pending';
|
2026-11-14 01:16:35 +08:00
|
|
|
|
return `<section class="panel order-cancellation-panel" id="order-cancellation-panel"><div class="panel-title"><h2>${approved ? '已取消 · 录错作废' : '取消申请与复核'}</h2>${request ? '<span class="badge warn">待店主复核</span>' : ''}</div>${request ? `<p>申请期间仍按原订单计账,尚未取消。复核完成前暂停本单财务修改和退货。</p>${request.stale ? '<div class="error" role="status">订单在申请后已有变动,原申请不能同意。请店主驳回后重新申请,再按最新资料核对。</div>' : ''}${canReview ? `<div class="actions">${!request.stale ? button('同意取消', 'approve-order-cancellation', 'danger', `data-id="${e(request.id)}"`) : ''}${button('驳回', 'reject-order-cancellation', '', `data-id="${e(request.id)}"`)}</div>` : '<p class="small muted">由店主账号查看撤销摘要并复核。</p>'}` : ''}${approved ? '<p>本单为录错作废:销售、成本、收款及费用已撤销,原有库存出入已冲回。原单、原流水与处理记录保留;本次没有登记真实退货或发起退款。</p><p class="small muted">双方已登记的实际交款保留,请继续核对下方待结金额。</p>' : ''}<details ${request ? 'open' : ''}><summary>取消申请与处理记录(${data.requests.length})</summary><ul class="timeline cancellation-history">${data.requests.map(orderCancellationHistoryHTML).join('')}</ul></details></section>`;
|
2026-10-23 11:58:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
function validateOrderCancellationSummary(summary) {
|
|
|
|
|
|
if (
|
|
|
|
|
|
!summary ||
|
|
|
|
|
|
!['total', 'netReceived', 'directCost'].every(key => Number.isSafeInteger(summary[key])) ||
|
|
|
|
|
|
!['goodsCost', 'profit', 'settlementDue'].every(
|
|
|
|
|
|
key => summary[key] === null || Number.isSafeInteger(summary[key]),
|
|
|
|
|
|
) ||
|
|
|
|
|
|
!Array.isArray(summary.inventory) ||
|
|
|
|
|
|
!summary.inventory.every(
|
|
|
|
|
|
item => typeof item.date === 'string' && typeof item.pocket === 'string' && Number.isSafeInteger(item.mg),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2026-10-23 11:58:19 +08:00
|
|
|
|
throw new Error('取消撤销摘要未完整返回,请重新读取订单。');
|
|
|
|
|
|
return summary;
|
2026-10-23 00:38:04 +08:00
|
|
|
|
}
|
2026-10-23 11:58:19 +08:00
|
|
|
|
function orderCancellationSummaryHTML(request) {
|
|
|
|
|
|
return orderCancellationSummaryDataHTML(validateOrderCancellationRequest(request).summary);
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderCancellationSummaryDataHTML(value) {
|
|
|
|
|
|
const summary = validateOrderCancellationSummary(value),
|
|
|
|
|
|
amount = value => (value === null ? '待补齐' : '¥' + yuan(value));
|
2026-11-08 15:47:07 +08:00
|
|
|
|
return `<div class="note">本次按“录错作废”撤销本单记账,不是实际退货退款。原单和冲回明细保留,不会向客户付款。</div><dl class="cancellation-summary"><div><dt>撤销原成交金额</dt><dd>${amount(summary.total)}</dd></div><div><dt>撤销收款净额</dt><dd>${amount(summary.netReceived)}</dd></div><div><dt>撤销货品成本</dt><dd>${amount(summary.goodsCost)}</dd></div><div><dt>撤销已记费用</dt><dd>${amount(summary.directCost)}</dd></div><div><dt>撤销原记账利润</dt><dd>${amount(summary.profit)}</dd></div><div><dt>当前双方待结净额</dt><dd>${amount(summary.settlementDue)}</dd></div></dl><h3>库存冲回摘要</h3>${summary.legacyCountNotice ? '<p class="note">' + e(summary.legacyCountNotice) + '</p>' : ''}${summary.inventory.length ? `<div class="table-wrap"><table aria-label="取消后库存冲回摘要"><thead><tr><th>原日期</th><th>货盘</th><th class="number">恢复/冲回克重</th></tr></thead><tbody>${summary.inventory.map(item => `<tr><td>${e(item.date)}</td><td>${e(pocketLabel(item.pocket))}</td><td class="number">${item.mg > 0 ? '+' : ''}${grams(item.mg)}g${inventoryCountHTML(item, { signed: true })}</td></tr>`).join('')}</tbody></table></div>` : '<p class="small muted">本单没有需要冲回的库存出入。</p>'}<p class="small muted cancellation-summary-note">按原日期、原货盘冲回库存。收款与费用撤回后仍能查看原记录和冲回行;双方实际交款保留,不会自动抹掉或转账。日购料真实采购付款保留,本单耗用将释放并重算相关成本与待结金额。</p>`;
|
2026-10-23 00:38:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
function requireOrderCancellationDetail() {
|
|
|
|
|
|
if (busy) throw new Error('正在保存,请稍候。');
|
|
|
|
|
|
if (pending()) throw new Error('请先点击顶部“恢复原操作”,确认上次提交结果后再处理取消申请。');
|
|
|
|
|
|
const order = currentOrder,
|
|
|
|
|
|
entry = currentOrderCancellation(order);
|
|
|
|
|
|
if (!order || !entry?.data || entry.error) throw new Error('请重新读取订单并核对取消申请。');
|
|
|
|
|
|
if (order.price_lock_id) throw new Error('锁价单暂不支持取消。');
|
2026-11-14 01:16:35 +08:00
|
|
|
|
if (typeof orderOwnershipPending === 'function' ? orderOwnershipPending(order) : order.ownershipPending)
|
|
|
|
|
|
throw new Error('归属申请待店主复核,请处理完成后再申请取消。');
|
|
|
|
|
|
if (typeof orderChangePending === 'function' ? orderChangePending(order) : order.changePending)
|
|
|
|
|
|
throw new Error('修改申请待店主复核,请复核完成后再申请取消。');
|
2026-10-23 00:38:04 +08:00
|
|
|
|
return { order, entry, data: entry.data };
|
|
|
|
|
|
}
|
|
|
|
|
|
function showOrderCancellationForm(kind) {
|
|
|
|
|
|
const { order, data } = requireOrderCancellationDetail(),
|
|
|
|
|
|
request = data.pending;
|
2026-10-23 11:58:19 +08:00
|
|
|
|
if (kind === 'request' && orderCancellationCanDirect(order)) kind = 'direct';
|
|
|
|
|
|
if (
|
|
|
|
|
|
['request', 'direct'].includes(kind) &&
|
|
|
|
|
|
(!data.eligible || request || !['draft', 'confirmed', 'delivered'].includes(order.status))
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-23 11:58:19 +08:00
|
|
|
|
throw new Error(data.reason || '这张订单当前不能申请取消。');
|
|
|
|
|
|
if (kind === 'direct' && !orderCancellationCanDirect(order)) throw new Error('当前不能直接取消,请重新读取订单。');
|
2026-11-14 01:16:35 +08:00
|
|
|
|
if (
|
|
|
|
|
|
!['request', 'direct'].includes(kind) &&
|
|
|
|
|
|
(state.user.role !== 'owner' || !data.canApprove || request?.status !== 'pending')
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-14 01:16:35 +08:00
|
|
|
|
throw new Error('取消申请由店主账号复核。');
|
2026-10-23 00:38:04 +08:00
|
|
|
|
if (kind === 'approve' && (!request.canApprove || request.stale || request.order_revision !== order.revision))
|
|
|
|
|
|
throw new Error('订单已有变动,请重新读取后复核;过期申请需驳回后重新申请。');
|
2026-10-23 11:58:19 +08:00
|
|
|
|
orderCancellationFormEntry = {
|
|
|
|
|
|
kind,
|
|
|
|
|
|
id: order.id,
|
|
|
|
|
|
user: orderCancellationUser(),
|
|
|
|
|
|
revision: order.revision,
|
|
|
|
|
|
requireVerified: kind === 'direct',
|
|
|
|
|
|
requestId: request?.id || '',
|
|
|
|
|
|
requestRevision: request?.order_revision,
|
|
|
|
|
|
};
|
2026-12-04 09:02:52 -08:00
|
|
|
|
let html, title, submit;
|
2026-10-23 11:58:19 +08:00
|
|
|
|
if (kind === 'direct') {
|
|
|
|
|
|
title = '取消今天的订单';
|
|
|
|
|
|
submit = '核实并取消订单';
|
|
|
|
|
|
html = `<div class="note" id="order-cancellation-mode">这张订单是今天开的,核实后直接按录错作废,无需另行审批。业务日期:${e(data.directActionDate)}。本入口不办理真实退货或退款。</div><input type="hidden" name="date" value="${e(data.directActionDate)}">${orderCancellationSummaryDataHTML(data.summary)}<label>取消原因<textarea name="reason" rows="3" required maxlength="200" placeholder="写清这张单录错的原因,例如:同一笔销售重复录入。"></textarea></label><label class="check"><input type="checkbox" name="verified" required>已核实这是录错作废,并确认撤销上述记账和冲回库存;本次不登记真实退货退款。</label>`;
|
|
|
|
|
|
} else if (kind === 'request') {
|
2026-10-23 00:38:04 +08:00
|
|
|
|
title = '申请取消这张订单';
|
|
|
|
|
|
submit = '提交取消申请';
|
2026-11-14 01:16:35 +08:00
|
|
|
|
html =
|
|
|
|
|
|
'<p>仅用于开错、重复录入等需要整单作废的登记错误。提交后仍按原单计账,店主同意后才取消、恢复库存并撤销本单销售、成本、收款和费用记账。</p><p class="small muted">真实退货请从“整单退货”办理;这里不会发起真实退款。</p><label>取消原因<textarea name="reason" rows="3" required maxlength="200" placeholder="写清这张单录错的原因,例如:同一笔销售重复录入。"></textarea></label>';
|
2026-10-23 00:38:04 +08:00
|
|
|
|
} else if (kind === 'approve') {
|
|
|
|
|
|
title = '复核并同意取消';
|
|
|
|
|
|
submit = '同意取消并作废';
|
|
|
|
|
|
html = `<p>申请人:${e(request.requester_name)} · ${e(orderCancellationTime(request.created))}</p><p class="cancellation-reason">取消原因:${e(request.reason)}</p>${orderCancellationSummaryHTML(request)}<label>复核备注(选填)<textarea name="note" maxlength="200" rows="2"></textarea></label><label class="check"><input type="checkbox" name="verified" required>已核实这是录错作废,并确认撤销上述记账和冲回库存;本次不登记真实退货退款。</label>`;
|
|
|
|
|
|
} else if (kind === 'reject') {
|
|
|
|
|
|
title = '驳回取消申请';
|
|
|
|
|
|
submit = '确认驳回';
|
|
|
|
|
|
html = `<p class="cancellation-reason">取消原因:${e(request.reason)}</p><p class="small muted">驳回后原订单继续计账,申请人可核对后重新申请。</p><label>驳回意见<textarea name="note" rows="3" required maxlength="200" placeholder="写清需要补充或重新核对的地方。"></textarea></label>`;
|
|
|
|
|
|
} else throw new Error('取消操作无效,请重新读取订单。');
|
2026-11-08 15:47:07 +08:00
|
|
|
|
if (kind !== 'reject')
|
|
|
|
|
|
html += legacyCountConfirmationHTML(
|
|
|
|
|
|
kind === 'approve' ? request.summary : data.requiresLegacyCountVerified ? data : data.summary,
|
|
|
|
|
|
);
|
2026-10-23 11:58:19 +08:00
|
|
|
|
$('#detail-error').innerHTML = '';
|
|
|
|
|
|
$('#detail-form').innerHTML =
|
|
|
|
|
|
`<form class="panel order-cancellation-form" id="order-cancellation-form" data-kind="${kind}"><h2>${title}</h2>${html}<div id="order-cancellation-error" aria-live="polite"></div><div class="form-actions"><button type="submit" data-order-cancellation-submit class="${['approve', 'direct'].includes(kind) ? 'danger' : 'primary'}">${submit}</button>${button('收起', 'close-order-cancellation-form', 'quiet')}</div></form>`;
|
2026-10-23 00:38:04 +08:00
|
|
|
|
$('#detail-form').scrollIntoView({ block: 'nearest', behavior: 'smooth' });
|
|
|
|
|
|
$('#order-cancellation-form textarea')?.focus();
|
|
|
|
|
|
}
|
|
|
|
|
|
function closeOrderCancellationForm() {
|
|
|
|
|
|
orderCancellationFormEntry = null;
|
|
|
|
|
|
if ($('#order-cancellation-form')) $('#detail-form').innerHTML = '';
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderCancellationOperation(url) {
|
2026-10-23 11:58:19 +08:00
|
|
|
|
const application = url.match(/^\/api\/orders\/([^/]+)\/(cancellation|canceltoday)$/),
|
|
|
|
|
|
decision = url.match(/^\/api\/order-cancellations\/([^/]+)\/(approve|reject)$/);
|
|
|
|
|
|
return application
|
|
|
|
|
|
? { kind: application[2] === 'canceltoday' ? 'direct' : 'request', orderId: decodeURIComponent(application[1]) }
|
|
|
|
|
|
: decision
|
|
|
|
|
|
? { kind: decision[2], requestId: decodeURIComponent(decision[1]) }
|
|
|
|
|
|
: null;
|
2026-10-23 00:38:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
function applyOrderCancellationRestrictions() {
|
|
|
|
|
|
const form = $('#order-cancellation-form');
|
|
|
|
|
|
if (!form) return;
|
|
|
|
|
|
const blocked = busy || !!pending() || !orderCancellationFormEntry;
|
|
|
|
|
|
form.querySelectorAll('input,textarea,button').forEach(control => {
|
|
|
|
|
|
const action = control.dataset.action;
|
|
|
|
|
|
control.disabled =
|
|
|
|
|
|
action === 'close-order-cancellation-form'
|
|
|
|
|
|
? false
|
|
|
|
|
|
: ['reload-order-cancellation', 'retry'].includes(action)
|
|
|
|
|
|
? busy
|
|
|
|
|
|
: blocked;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderCancellationError(err) {
|
|
|
|
|
|
err.message = err.message.replace('不要重新开单', '不要重复提交取消申请或复核');
|
|
|
|
|
|
const entry = orderCancellationFormEntry,
|
|
|
|
|
|
id = entry?.id || currentOrder?.id;
|
2026-10-23 11:58:19 +08:00
|
|
|
|
if (err.status === 403 && entry?.kind === 'direct' && entry.id === currentOrder?.id && !pending()) {
|
|
|
|
|
|
entry.kind = 'request';
|
|
|
|
|
|
if (orderCancellationDetail?.data)
|
|
|
|
|
|
orderCancellationDetail.data = { ...orderCancellationDetail.data, directToday: false };
|
|
|
|
|
|
const form = $('#order-cancellation-form'),
|
|
|
|
|
|
checkbox = form?.querySelector('[name="verified"]');
|
|
|
|
|
|
if (checkbox) checkbox.checked = false;
|
|
|
|
|
|
if (form) {
|
|
|
|
|
|
form.dataset.kind = 'request';
|
|
|
|
|
|
const title = form.querySelector('h2'),
|
|
|
|
|
|
submit = form.querySelector('[data-order-cancellation-submit]');
|
|
|
|
|
|
if (title) title.textContent = '申请取消这张订单';
|
|
|
|
|
|
if (submit) submit.textContent = '提交取消申请';
|
|
|
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const mode = $('#order-cancellation-mode');
|
|
|
|
|
|
if (mode) mode.textContent = '当前不能直接取消。输入已保留,重新核实后提交取消申请,店主同意后才按录错作废。';
|
|
|
|
|
|
const message = err.message + ' 输入已保留,请重新核实后提交店主审批。',
|
|
|
|
|
|
target = $('#order-cancellation-error') || $('#detail-error');
|
|
|
|
|
|
if (target) target.innerHTML = `<div class="error">${e(message)}</div>`;
|
2026-10-23 11:58:19 +08:00
|
|
|
|
applyOrderCancellationRestrictions();
|
|
|
|
|
|
toast(message, true);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-10-23 00:38:04 +08:00
|
|
|
|
if (err.status === 409 || err.status === 403 || err.status === 404) {
|
|
|
|
|
|
orderCancellationFormEntry = null;
|
|
|
|
|
|
if (orderCancellationDetail)
|
|
|
|
|
|
orderCancellationDetail = {
|
|
|
|
|
|
...orderCancellationDetail,
|
|
|
|
|
|
data: null,
|
|
|
|
|
|
error: '取消申请或订单已有新记录,请重新读取后复核。',
|
|
|
|
|
|
};
|
|
|
|
|
|
const checkbox = $('#order-cancellation-form [name="verified"]');
|
|
|
|
|
|
if (checkbox) checkbox.checked = false;
|
|
|
|
|
|
if ($('#order-cancellation-panel'))
|
|
|
|
|
|
$('#order-cancellation-panel').innerHTML =
|
|
|
|
|
|
'<h2>请重新读取后复核</h2><p>取消申请或订单已有变动,当前页面不能继续复核。</p>';
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-23 00:38:04 +08:00
|
|
|
|
const target = $('#order-cancellation-error') || $('#detail-error');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (target)
|
2026-10-23 00:38:04 +08:00
|
|
|
|
target.innerHTML = `<div class="error">${e(err.message)}${pending() ? ' ' + button('恢复原操作', 'retry', 'danger') : id ? ' ' + button('重新读取订单并复核', 'reload-order-cancellation', '', `data-id="${e(id)}"`) : ''}</div>`;
|
|
|
|
|
|
applyOrderCancellationRestrictions();
|
|
|
|
|
|
toast(err.message, true);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-23 00:38:04 +08:00
|
|
|
|
async function submitOrderCancellation(form) {
|
|
|
|
|
|
const { order, data } = requireOrderCancellationDetail(),
|
|
|
|
|
|
entry = orderCancellationFormEntry;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (
|
2026-10-23 00:38:04 +08:00
|
|
|
|
form !== $('#order-cancellation-form') ||
|
2026-12-04 08:58:53 -08:00
|
|
|
|
!entry ||
|
2026-10-23 00:38:04 +08:00
|
|
|
|
entry.id !== order.id ||
|
|
|
|
|
|
entry.user !== orderCancellationUser() ||
|
|
|
|
|
|
entry.revision !== order.revision
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-23 00:38:04 +08:00
|
|
|
|
throw new Error('请重新读取订单后核对取消申请。');
|
|
|
|
|
|
const get = name => form.querySelector(`[name="${name}"]`),
|
|
|
|
|
|
text = name => String(get(name)?.value || '').trim();
|
|
|
|
|
|
let input, url;
|
2026-10-23 11:58:19 +08:00
|
|
|
|
if (['request', 'direct'].includes(entry.kind)) {
|
2026-10-23 00:38:04 +08:00
|
|
|
|
if (!data.eligible || data.pending) throw new Error('这张订单当前不能重复申请取消。');
|
|
|
|
|
|
const reason = text('reason');
|
|
|
|
|
|
if (!reason || reason.length > 200) throw new Error('取消原因请填写1~200字。');
|
2026-10-23 11:58:19 +08:00
|
|
|
|
const direct = entry.kind === 'direct';
|
|
|
|
|
|
if (direct && !orderCancellationCanDirect(order)) throw new Error('当前不能直接取消,请重新读取订单。');
|
|
|
|
|
|
if (entry.requireVerified && !get('verified')?.checked)
|
2026-10-23 00:38:04 +08:00
|
|
|
|
throw new Error('请先核对撤销金额、成本和库存,并勾选录错作废确认。');
|
2026-10-23 11:58:19 +08:00
|
|
|
|
input = { revision: entry.revision, reason, ...(direct ? { date: data.directActionDate, verified: true } : {}) };
|
|
|
|
|
|
url = '/api/orders/' + encodeURIComponent(order.id) + '/' + (direct ? 'canceltoday' : 'cancellation');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
} else {
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const request = data.pending;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (
|
2026-11-14 01:16:35 +08:00
|
|
|
|
state.user.role !== 'owner' ||
|
|
|
|
|
|
!data.canApprove ||
|
|
|
|
|
|
request?.status !== 'pending' ||
|
|
|
|
|
|
request.id !== entry.requestId
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-14 01:16:35 +08:00
|
|
|
|
throw new Error('取消申请由店主账号复核,请重新读取。');
|
2026-10-23 00:38:04 +08:00
|
|
|
|
const note = text('note');
|
|
|
|
|
|
if (note.length > 200 || (entry.kind === 'reject' && !note))
|
|
|
|
|
|
throw new Error(entry.kind === 'reject' ? '驳回意见请填写1~200字。' : '复核备注最多200字。');
|
|
|
|
|
|
if (entry.kind === 'approve') {
|
|
|
|
|
|
if (!request.canApprove || request.stale || entry.requestRevision !== order.revision)
|
|
|
|
|
|
throw new Error('订单已有变动,请重新读取后复核,不能同意旧申请。');
|
|
|
|
|
|
if (!get('verified')?.checked) throw new Error('请先核对撤销金额、成本和库存,并勾选录错作废确认。');
|
|
|
|
|
|
input = { revision: entry.requestRevision, verified: true, note };
|
|
|
|
|
|
} else input = { note };
|
|
|
|
|
|
url = '/api/order-cancellations/' + encodeURIComponent(entry.requestId) + '/' + entry.kind;
|
|
|
|
|
|
}
|
2026-10-23 11:58:19 +08:00
|
|
|
|
if (entry.kind !== 'reject')
|
|
|
|
|
|
Object.assign(
|
|
|
|
|
|
input,
|
|
|
|
|
|
requireLegacyCountConfirmation(
|
|
|
|
|
|
form,
|
2026-11-14 01:16:35 +08:00
|
|
|
|
entry.kind === 'approve' ? data.pending.summary : data.requiresLegacyCountVerified ? data : data.summary,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
),
|
|
|
|
|
|
);
|
2026-11-14 01:16:35 +08:00
|
|
|
|
$('#order-cancellation-error').innerHTML = '';
|
|
|
|
|
|
const detailContext = captureOrderDetailReload(order.id);
|
2026-10-23 00:38:04 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const result = await mutate(url, input);
|
|
|
|
|
|
return await finishOrderCancellationSave(url, input, result, detailContext);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
orderCancellationError(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-23 11:58:19 +08:00
|
|
|
|
async function finishOrderCancellationSave(
|
|
|
|
|
|
url,
|
|
|
|
|
|
input,
|
|
|
|
|
|
result,
|
|
|
|
|
|
detailContext = captureOrderDetailReload(result.id || result.order?.id),
|
|
|
|
|
|
) {
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const operation = orderCancellationOperation(url),
|
|
|
|
|
|
id = result.id || result.order?.id || operation?.orderId,
|
|
|
|
|
|
title =
|
|
|
|
|
|
operation?.kind === 'direct'
|
|
|
|
|
|
? '当天直接取消,原订单已作废'
|
|
|
|
|
|
: operation?.kind === 'approve'
|
|
|
|
|
|
? '已同意取消,原订单已作废'
|
|
|
|
|
|
: operation?.kind === 'reject'
|
|
|
|
|
|
? '取消申请已驳回'
|
|
|
|
|
|
: '取消申请已提交,等待店主复核';
|
2026-10-23 00:38:04 +08:00
|
|
|
|
orderCancellationFormEntry = null;
|
|
|
|
|
|
orderCancellationDetail = null;
|
|
|
|
|
|
currentOrder = null;
|
|
|
|
|
|
invalidateMaterialPools();
|
|
|
|
|
|
invalidateReport();
|
|
|
|
|
|
invalidatePriceLockViews();
|
|
|
|
|
|
inventoryPage = null;
|
|
|
|
|
|
++orderBookRequest;
|
2026-10-23 11:58:19 +08:00
|
|
|
|
if (detailContext) detailContext = { ...detailContext, bookRequest: orderBookRequest };
|
|
|
|
|
|
if ($('#order-content'))
|
|
|
|
|
|
$('#order-content').innerHTML =
|
|
|
|
|
|
`<div class="order-header"><h2>${title}</h2><p>操作已经保存。${operation?.kind === 'request' ? '复核期间仍按原单计账。' : ['approve', 'direct'].includes(operation?.kind) ? '没有发起真实退款,原单和处理记录已保留。' : '原订单继续计账。'}</p><div id="order-cancellation-saved-status" aria-live="polite"></div><div class="actions">${button('重新读取原订单', 'reload-order-cancellation', 'primary', `data-id="${e(id)}"`)}</div></div>`;
|
|
|
|
|
|
try {
|
|
|
|
|
|
await refreshOrderDetailAfterSave(id, detailContext);
|
|
|
|
|
|
toast(title);
|
2026-10-23 00:38:04 +08:00
|
|
|
|
} catch {
|
|
|
|
|
|
if ($('#order-cancellation-saved-status'))
|
|
|
|
|
|
$('#order-cancellation-saved-status').textContent = '保存已成功,最新资料暂未读到。请重新读取原订单查看。';
|
|
|
|
|
|
toast('操作已经保存,请重新读取原订单查看,不要重复提交。', true);
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-23 00:38:04 +08:00
|
|
|
|
function validateOrderCancellationList(result) {
|
|
|
|
|
|
if (
|
|
|
|
|
|
!result ||
|
|
|
|
|
|
!Array.isArray(result.requests) ||
|
|
|
|
|
|
typeof result.canApprove !== 'boolean' ||
|
|
|
|
|
|
!['pending', 'approved', 'rejected'].every(
|
|
|
|
|
|
key => Number.isSafeInteger(result.counts?.[key]) && result.counts[key] >= 0,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2026-10-23 00:38:04 +08:00
|
|
|
|
throw new Error('取消复核列表未完整返回,请重新读取。');
|
|
|
|
|
|
result.requests.forEach(request => validateOrderCancellationRequest(request));
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
async function loadOrderCancellationCount() {
|
|
|
|
|
|
const request = ++orderCancellationCountRequest,
|
|
|
|
|
|
user = orderCancellationUser();
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = validateOrderCancellationList(await api('/api/order-cancellations?status=pending'));
|
|
|
|
|
|
const entry = $('#order-cancellations-entry');
|
|
|
|
|
|
if (request !== orderCancellationCountRequest || user !== orderCancellationUser() || tab !== 'orders' || !entry)
|
|
|
|
|
|
return;
|
|
|
|
|
|
entry.textContent = '待取消复核(' + result.counts.pending + ')';
|
|
|
|
|
|
entry.classList.toggle('danger', result.counts.pending > 0);
|
|
|
|
|
|
entry.title = '所有日期的取消申请,不受订单筛选影响';
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
const entry = $('#order-cancellations-entry');
|
|
|
|
|
|
if (request === orderCancellationCountRequest && user === orderCancellationUser() && tab === 'orders' && entry) {
|
|
|
|
|
|
entry.textContent = '待取消复核(读取失败)';
|
|
|
|
|
|
entry.title = '点击重新读取全部日期的取消申请';
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-23 00:38:04 +08:00
|
|
|
|
function orderCancellationListHTML(result) {
|
|
|
|
|
|
const owner = state.user.role === 'owner' && result.canApprove;
|
2026-10-23 11:58:19 +08:00
|
|
|
|
return `<div class="cancellation-list-tabs" role="group" aria-label="取消申请状态">${[
|
|
|
|
|
|
['pending', '待复核'],
|
|
|
|
|
|
['approved', '已取消'],
|
|
|
|
|
|
['rejected', '已驳回'],
|
|
|
|
|
|
['all', '全部'],
|
2026-12-04 08:58:53 -08:00
|
|
|
|
]
|
2026-10-23 11:58:19 +08:00
|
|
|
|
.map(([value, name]) =>
|
|
|
|
|
|
button(
|
|
|
|
|
|
name + (value === 'all' ? '' : '(' + result.counts[value] + ')'),
|
|
|
|
|
|
'filter-order-cancellations',
|
|
|
|
|
|
orderCancellationListStatus === value ? 'primary' : '',
|
|
|
|
|
|
`data-status="${value}" aria-pressed="${orderCancellationListStatus === value}"`,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
),
|
|
|
|
|
|
)
|
2026-10-23 11:58:19 +08:00
|
|
|
|
.join(
|
|
|
|
|
|
'',
|
|
|
|
|
|
)}</div>${result.requests.length ? `<div class="table-wrap"><table class="cancellation-list-table" aria-label="全部日期的取消申请"><thead><tr><th>原订单/日期</th><th>经办/申请人及时间</th><th>取消原因</th><th>状态/处理记录</th><th>查看</th></tr></thead><tbody>${result.requests.map(request => `<tr><td><strong>${e(request.order_number)}</strong><div class="small muted">${e(request.order_date)}</div><div class="small">原成交 ¥${yuan(request.summary.total)}</div></td><td>${request.direct === true ? '经办人:' : ''}${e(request.operator_name || request.requester_name)}<div class="small muted">${e(orderCancellationTime(request.executedAt || request.created))}</div></td><td class="cancellation-reason">${e(request.reason)}</td><td>${e(orderCancellationRecordStatus(request))}${request.stale && request.status === 'pending' ? '<div class="small danger-text">原单已有变动,需驳回后重新申请</div>' : ''}${request.note ? `<div class="small cancellation-reason">${e(request.note)}</div>` : ''}${request.reviewer_name ? `<div class="small muted">${e(request.reviewer_name)} · ${e(orderCancellationTime(request.reviewed))}</div>` : ''}</td><td>${button(owner && request.status === 'pending' ? '查看并复核' : '查看订单', 'open-cancellation-order', '', `data-id="${e(request.order_id)}"`)}</td></tr>`).join('')}</tbody></table></div>` : '<p class="empty">全部日期均无此状态的取消申请。</p>'}`;
|
2026-10-23 00:38:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
async function openOrderCancellations(status = 'pending') {
|
|
|
|
|
|
orderCancellationListStatus = Object.hasOwn(orderCancellationStatus, status) || status === 'all' ? status : 'pending';
|
|
|
|
|
|
let dialog = $('#order-cancellations-dialog');
|
|
|
|
|
|
if (!dialog) {
|
|
|
|
|
|
dialog = document.createElement('dialog');
|
|
|
|
|
|
dialog.id = 'order-cancellations-dialog';
|
|
|
|
|
|
dialog.setAttribute('aria-labelledby', 'order-cancellations-title');
|
|
|
|
|
|
dialog.addEventListener('cancel', () => {
|
|
|
|
|
|
++orderCancellationListRequest;
|
|
|
|
|
|
});
|
|
|
|
|
|
document.body.append(dialog);
|
|
|
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
|
dialog.innerHTML = `<button class="dialog-close" data-action="close-order-cancellations" aria-label="关闭取消申请列表">×</button><h2 id="order-cancellations-title">取消申请与复核</h2><p class="small muted">全部日期,不受订单页的日期、搜索或状态筛选影响。申请期间仍按原订单计账;店主同意后才按录错作废。当天直接取消的记录单独标明经办人。</p><div id="order-cancellations-list" aria-live="polite" aria-busy="true">正在读取…</div>`;
|
2026-10-23 00:38:04 +08:00
|
|
|
|
if (!dialog.open) dialog.showModal();
|
|
|
|
|
|
return await loadOrderCancellations();
|
|
|
|
|
|
}
|
|
|
|
|
|
async function loadOrderCancellations() {
|
|
|
|
|
|
const dialog = $('#order-cancellations-dialog'),
|
|
|
|
|
|
target = $('#order-cancellations-list');
|
|
|
|
|
|
if (!dialog?.open || !target) return;
|
|
|
|
|
|
const request = ++orderCancellationListRequest,
|
|
|
|
|
|
user = orderCancellationUser(),
|
|
|
|
|
|
status = orderCancellationListStatus;
|
|
|
|
|
|
target.setAttribute('aria-busy', 'true');
|
|
|
|
|
|
target.innerHTML = '正在读取全部日期的取消申请…';
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = validateOrderCancellationList(
|
|
|
|
|
|
await api('/api/order-cancellations?' + new URLSearchParams({ status })),
|
|
|
|
|
|
);
|
|
|
|
|
|
if (
|
|
|
|
|
|
request !== orderCancellationListRequest ||
|
|
|
|
|
|
user !== orderCancellationUser() ||
|
|
|
|
|
|
!dialog.open ||
|
|
|
|
|
|
status !== orderCancellationListStatus
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-23 00:38:04 +08:00
|
|
|
|
return;
|
|
|
|
|
|
target.innerHTML = orderCancellationListHTML(result);
|
|
|
|
|
|
target.setAttribute('aria-busy', 'false');
|
|
|
|
|
|
const entry = $('#order-cancellations-entry');
|
|
|
|
|
|
if (entry) entry.textContent = '待取消复核(' + result.counts.pending + ')';
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (request !== orderCancellationListRequest || user !== orderCancellationUser() || !dialog.open) return;
|
|
|
|
|
|
target.setAttribute('aria-busy', 'false');
|
|
|
|
|
|
target.innerHTML = `<div class="error">${e(err.name === 'AbortError' ? '读取超时,请重新读取。' : err.message)} ${button('重新读取', 'retry-order-cancellations')}</div>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
function closeOrderCancellations() {
|
|
|
|
|
|
++orderCancellationListRequest;
|
|
|
|
|
|
$('#order-cancellations-dialog')?.close();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-23 00:38:04 +08:00
|
|
|
|
function invalidateOrderCancellationViews() {
|
|
|
|
|
|
++orderCancellationCountRequest;
|
|
|
|
|
|
closeOrderCancellations();
|
|
|
|
|
|
orderCancellationDetail = null;
|
|
|
|
|
|
orderCancellationFormEntry = null;
|
|
|
|
|
|
}
|