2026-10-26 20:25:31 +08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
// 订单归属单独更正,不借用改单表单改变实际收付款或货品成本。
|
|
|
|
|
|
let orderOwnershipDetail = null,
|
|
|
|
|
|
orderOwnershipFormEntry = null;
|
|
|
|
|
|
let orderOwnershipCountRequest = 0,
|
|
|
|
|
|
orderOwnershipListRequest = 0,
|
|
|
|
|
|
orderOwnershipListStatus = 'pending',
|
|
|
|
|
|
orderOwnershipAccessRequest = 0;
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const orderOwnershipStatus = { pending: '待店主复核', approved: '已修改归属', rejected: '已驳回' };
|
2026-10-26 20:25:31 +08:00
|
|
|
|
function orderOwnershipUser() {
|
|
|
|
|
|
return session?.user?.id || '';
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderOwnershipTime(value) {
|
|
|
|
|
|
const date = new Date(value);
|
|
|
|
|
|
return Number.isNaN(date.getTime()) ? '时间未提供' : date.toLocaleString('zh-CN');
|
|
|
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
|
function validOrderOwnershipMember(value) {
|
|
|
|
|
|
return value && teamMemberKeys().includes(value.member) && typeof value.label === 'string' && !!value.label;
|
|
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
function validateOrderOwnershipRequest(request, orderId) {
|
|
|
|
|
|
if (
|
|
|
|
|
|
!request ||
|
|
|
|
|
|
typeof request.id !== 'string' ||
|
|
|
|
|
|
!request.id ||
|
|
|
|
|
|
typeof request.order_id !== 'string' ||
|
|
|
|
|
|
!request.order_id ||
|
|
|
|
|
|
(orderId && request.order_id !== orderId) ||
|
|
|
|
|
|
!Object.hasOwn(orderOwnershipStatus, 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' ||
|
|
|
|
|
|
!validOrderOwnershipMember(request.before) ||
|
|
|
|
|
|
!validOrderOwnershipMember(request.after) ||
|
|
|
|
|
|
request.before.member === request.after.member
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-26 20:25:31 +08:00
|
|
|
|
throw new Error('归属修改资料未完整返回,请重新读取订单后复核。');
|
|
|
|
|
|
return request;
|
|
|
|
|
|
}
|
|
|
|
|
|
function useOrderOwnershipDetail(order, result, error) {
|
|
|
|
|
|
orderOwnershipFormEntry = null;
|
|
|
|
|
|
const entry = { id: order.id, user: orderOwnershipUser(), revision: order.revision, data: null, error: '' };
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (error) throw error;
|
|
|
|
|
|
if (
|
|
|
|
|
|
!result ||
|
|
|
|
|
|
typeof result.eligible !== 'boolean' ||
|
|
|
|
|
|
typeof result.canApprove !== 'boolean' ||
|
|
|
|
|
|
typeof result.requiresApproval !== 'boolean' ||
|
|
|
|
|
|
typeof result.canDirect !== 'boolean' ||
|
|
|
|
|
|
!Number.isSafeInteger(result.revision) ||
|
2026-11-14 01:16:35 +08:00
|
|
|
|
!teamMemberKeys().includes(result.member) ||
|
|
|
|
|
|
!Array.isArray(result.members) ||
|
|
|
|
|
|
!result.members.length ||
|
|
|
|
|
|
!result.members.every(validOrderOwnershipMember) ||
|
|
|
|
|
|
new Set(result.members.map(member => member.member)).size !== result.members.length ||
|
2026-10-26 20:25:31 +08:00
|
|
|
|
!result.members.some(member => member.member === result.member) ||
|
|
|
|
|
|
!Array.isArray(result.requests) ||
|
|
|
|
|
|
(result.pending !== null && typeof result.pending !== 'object')
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-26 20:25:31 +08:00
|
|
|
|
throw new Error('订单归属资料尚未完整返回,请重新读取。');
|
|
|
|
|
|
if (result.revision !== order.revision || (order.member && result.member !== order.member))
|
|
|
|
|
|
throw new Error('订单已有新记录,请重新读取后核对归属。');
|
|
|
|
|
|
result.requests.forEach(request => validateOrderOwnershipRequest(request, order.id));
|
|
|
|
|
|
if (result.pending) validateOrderOwnershipRequest(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-26 20:25:31 +08:00
|
|
|
|
orderOwnershipDetail = entry;
|
|
|
|
|
|
return entry;
|
|
|
|
|
|
}
|
|
|
|
|
|
function currentOrderOwnership(order = currentOrder) {
|
|
|
|
|
|
return order &&
|
|
|
|
|
|
orderOwnershipDetail?.id === order.id &&
|
|
|
|
|
|
orderOwnershipDetail.user === orderOwnershipUser() &&
|
|
|
|
|
|
orderOwnershipDetail.revision === order.revision
|
|
|
|
|
|
? orderOwnershipDetail
|
|
|
|
|
|
: null;
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderOwnershipPending(order = currentOrder) {
|
|
|
|
|
|
return currentOrderOwnership(order)?.data?.pending || order?.ownershipPending || null;
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderOwnershipOtherPending(order) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
(typeof orderChangePending === 'function' ? orderChangePending(order) : order.changePending) ||
|
|
|
|
|
|
(typeof orderCancellationPending === 'function' ? orderCancellationPending(order) : order.cancellationPending)
|
|
|
|
|
|
);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
function orderOwnershipActionHTML(order) {
|
|
|
|
|
|
const data = currentOrderOwnership(order)?.data;
|
|
|
|
|
|
if (!data?.eligible || data.pending || orderOwnershipPending(order) || orderOwnershipOtherPending(order)) return '';
|
|
|
|
|
|
const direct = state.user.role === 'owner' && data.canDirect;
|
|
|
|
|
|
return button(
|
|
|
|
|
|
direct ? '修改归属' : '申请修改归属',
|
|
|
|
|
|
'order-ownership-request',
|
|
|
|
|
|
'order-ownership-trigger',
|
|
|
|
|
|
`data-id="${e(order.id)}"`,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderOwnershipPairHTML(request) {
|
|
|
|
|
|
validateOrderOwnershipRequest(request);
|
|
|
|
|
|
return `<div class="order-ownership-pair" aria-label="订单归属原值与新值"><div><small>原归属</small><strong>${e(request.before.label)}</strong></div><span aria-hidden="true">→</span><div><small>${request.status === 'pending' ? '申请归属' : '本次新归属'}</small><strong>${e(request.after.label)}</strong></div></div>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderOwnershipEffectHTML(before) {
|
|
|
|
|
|
return `<p class="small muted">修改后,订单的经营款与业绩按新归属统计。原单号、实际收退款账户、成本付款人及库存记录保留。</p>${before !== 'owner' ? '<p class="small order-ownership-access-note">生效后,本单将从原归属助理的列表移出,原助理不能再查看或修改本单。</p>' : ''}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderOwnershipHistoryHTML(request) {
|
|
|
|
|
|
return `<li><div class="change-record-title"><strong>${e(orderOwnershipStatus[request.status])}</strong>${request.stale && request.status === 'pending' ? '<span class="badge danger">订单已有变动</span>' : ''}</div>${orderOwnershipPairHTML(request)}<div>申请人:${e(request.requester_name)} · ${e(orderOwnershipTime(request.created))}</div><p class="change-reason">修改原因:${e(request.reason)}</p>${request.status !== 'pending' ? `<div class="small muted">处理人:${e(request.reviewer_name || request.requester_name)} · ${e(orderOwnershipTime(request.reviewed || request.created))}</div>${request.note ? `<p class="change-reason">${request.status === 'rejected' ? '驳回意见' : '复核备注'}:${e(request.note)}</p>` : ''}` : ''}</li>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderOwnershipDetailHTML(order) {
|
|
|
|
|
|
const entry = currentOrderOwnership(order),
|
|
|
|
|
|
data = entry?.data,
|
|
|
|
|
|
request = orderOwnershipPending(order);
|
|
|
|
|
|
if (!data)
|
|
|
|
|
|
return `<section class="panel order-ownership-panel"><h2>订单归属</h2><p class="${entry?.error ? 'danger-text' : 'muted'}" role="status">${e(entry?.error || '订单归属资料尚未读取。')}</p>${button('重新读取归属资料', 'order-ownership-reload', '', `data-id="${e(order.id)}"`)}</section>`;
|
|
|
|
|
|
if (!request && !data.requests.length)
|
|
|
|
|
|
return !data.eligible && data.reason
|
|
|
|
|
|
? `<p class="small muted order-ownership-unavailable">修改归属:${e(data.reason)}</p>`
|
|
|
|
|
|
: '';
|
|
|
|
|
|
const canReview = state.user.role === 'owner' && data.canApprove && request?.status === 'pending',
|
|
|
|
|
|
approved = data.requests.find(item => item.status === 'approved' && item.after.member === data.member);
|
2026-11-14 01:16:35 +08:00
|
|
|
|
return `<section class="panel order-ownership-panel" id="order-ownership-panel"><div class="panel-title"><h2>订单归属修改</h2>${request ? '<span class="badge warn">归属待复核</span>' : approved ? '<span class="badge">归属已修改</span>' : ''}</div>${!request && approved ? `${orderOwnershipPairHTML(approved)}<p class="small muted">${e(approved.reviewer_name || approved.requester_name)} · ${e(orderOwnershipTime(approved.reviewed || approved.created))} · 原因:${e(approved.reason)}</p>` : ''}${request ? `${orderOwnershipPairHTML(request)}<p>申请尚未生效,仍按原归属计账。助理修改归属均须店主复核,今天的订单也一样;复核完成前暂停本单其他修改、记账和取消申请。</p>${orderOwnershipEffectHTML(request.before.member)}${request.stale ? '<div class="error" role="status">订单在申请后已有变动,不能同意旧申请。请店主驳回后按最新订单重新申请。</div>' : ''}${canReview ? `<div class="actions">${!request.stale && request.canApprove ? button('核对并同意归属', 'order-ownership-approve', 'primary', `data-id="${e(request.id)}"`) : ''}${button('驳回归属申请', 'order-ownership-reject', '', `data-id="${e(request.id)}"`)}</div>` : '<p class="small muted">等待店主账号核对原归属、新归属和修改原因。</p>'}` : ''}<details ${request ? 'open' : ''}><summary>归属修改与复核记录(${data.requests.length})</summary><ul class="timeline change-history">${data.requests.map(orderOwnershipHistoryHTML).join('')}</ul></details></section>`;
|
2026-10-26 20:25:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
function requireOrderOwnershipDetail() {
|
|
|
|
|
|
if (busy) throw new Error('正在保存,请稍候。');
|
|
|
|
|
|
if (pending()) throw new Error('请先点击顶部“恢复原操作”,确认上次提交结果后再修改归属。');
|
|
|
|
|
|
const order = currentOrder,
|
|
|
|
|
|
entry = currentOrderOwnership(order);
|
|
|
|
|
|
if (!order || !entry?.data || entry.error) throw new Error('请重新读取订单并核对归属。');
|
|
|
|
|
|
if (orderOwnershipOtherPending(order)) throw new Error('本单已有待复核的修改或取消申请,请处理完成后再修改归属。');
|
|
|
|
|
|
return { order, entry, data: entry.data };
|
|
|
|
|
|
}
|
|
|
|
|
|
function showOrderOwnershipForm(kind = 'request') {
|
|
|
|
|
|
const { order, data } = requireOrderOwnershipDetail(),
|
|
|
|
|
|
request = data.pending;
|
|
|
|
|
|
if (!['request', 'approve', 'reject'].includes(kind)) throw new Error('归属修改操作无效,请重新读取。');
|
|
|
|
|
|
if (kind === 'request' && (!data.eligible || request)) throw new Error(data.reason || '本单当前不能重复修改归属。');
|
|
|
|
|
|
if (kind !== 'request' && (state.user.role !== 'owner' || !data.canApprove || request?.status !== 'pending'))
|
|
|
|
|
|
throw new Error('归属申请由店主账号复核。');
|
2026-11-14 01:16:35 +08:00
|
|
|
|
if (kind === 'approve' && (!request.canApprove || request.stale || request.order_revision !== order.revision))
|
|
|
|
|
|
throw new Error('订单已有变动,请驳回旧申请后按最新订单重新核对。');
|
2026-10-26 20:25:31 +08:00
|
|
|
|
const direct = kind === 'request' && state.user.role === 'owner' && data.canDirect;
|
|
|
|
|
|
if (kind === 'request' && state.user.role === 'owner' && !direct)
|
|
|
|
|
|
throw new Error(data.reason || '当前不能直接修改归属,请重新读取订单。');
|
|
|
|
|
|
orderOwnershipFormEntry = {
|
|
|
|
|
|
kind,
|
|
|
|
|
|
id: order.id,
|
|
|
|
|
|
user: orderOwnershipUser(),
|
|
|
|
|
|
revision: order.revision,
|
|
|
|
|
|
direct,
|
|
|
|
|
|
requestId: request?.id,
|
|
|
|
|
|
requestRevision: request?.order_revision,
|
|
|
|
|
|
};
|
2026-12-04 09:02:52 -08:00
|
|
|
|
let title, html, submit;
|
2026-10-26 20:25:31 +08:00
|
|
|
|
if (kind === 'request') {
|
|
|
|
|
|
title = direct ? '修改订单归属' : '申请修改订单归属';
|
|
|
|
|
|
submit = direct ? '确认修改归属' : '提交归属申请';
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const before = data.members.find(member => member.member === data.member),
|
|
|
|
|
|
members = data.members.filter(
|
|
|
|
|
|
member => member.member !== data.member && member.enabled !== false && teamMemberEnabled(member.member),
|
|
|
|
|
|
);
|
|
|
|
|
|
html = `<div class="note">${direct ? '店主核对后直接生效。' : '提交后等待店主复核,今天的订单也须审批;批准前继续按原归属计账。'}</div><div class="grid two"><label>当前归属<input value="${e(before.label)}" readonly></label><label>修改为<select name="member" required>${opts([['', '请选择新归属'], ...members.map(member => [member.member, member.label])], '')}</select></label></div><p class="order-ownership-selection" id="order-ownership-selection" aria-live="polite">${e(before.label)} → 请选择新归属</p>${orderOwnershipEffectHTML(data.member)}<label>修改原因<textarea name="reason" required maxlength="200" rows="3" placeholder="写清原归属录错的原因及正确归属。"></textarea></label><label class="check"><input type="checkbox" name="verified" required>已核对原归属、新归属和修改原因,确认本次归属${direct ? '更正' : '申请'}。</label>`;
|
2026-10-26 20:25:31 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
title = kind === 'approve' ? '复核并同意修改归属' : '驳回归属申请';
|
|
|
|
|
|
submit = kind === 'approve' ? '同意并修改归属' : '确认驳回';
|
|
|
|
|
|
html = `${orderOwnershipPairHTML(request)}<p>申请人:${e(request.requester_name)} · ${e(orderOwnershipTime(request.created))}</p><p class="change-reason">修改原因:${e(request.reason)}</p>${kind === 'approve' ? `${orderOwnershipEffectHTML(request.before.member)}<label>复核备注(选填)<textarea name="note" maxlength="200" rows="2"></textarea></label><label class="check"><input type="checkbox" name="verified" required>已核对原归属、新归属和原因,同意后按新归属统计,并更新原助理的查看权限。</label>` : '<p class="small muted">驳回后保留原归属,申请人可核对后重新申请。</p><label>驳回意见<textarea name="note" required maxlength="200" rows="3" placeholder="写清需要重新核对的地方。"></textarea></label>'}`;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
$('#detail-error').innerHTML = '';
|
|
|
|
|
|
$('#detail-form').innerHTML =
|
|
|
|
|
|
`<form id="order-ownership-form" class="panel order-ownership-form" data-kind="${kind}"><h2>${title}</h2>${html}<div id="order-ownership-error" aria-live="polite"></div><div class="form-actions"><button type="submit" class="primary">${submit}</button>${button('收起', 'order-ownership-close-form', 'quiet')}</div></form>`;
|
|
|
|
|
|
applyOrderOwnershipRestrictions();
|
|
|
|
|
|
$('#detail-form').scrollIntoView({ block: 'nearest', behavior: 'smooth' });
|
|
|
|
|
|
$('#order-ownership-form [name="member"]')?.focus();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
function closeOrderOwnershipForm() {
|
|
|
|
|
|
orderOwnershipFormEntry = null;
|
|
|
|
|
|
if ($('#order-ownership-form')) $('#detail-form').innerHTML = '';
|
|
|
|
|
|
}
|
|
|
|
|
|
function syncOrderOwnershipSelection(target) {
|
|
|
|
|
|
const form = $('#order-ownership-form'),
|
|
|
|
|
|
entry = orderOwnershipFormEntry,
|
|
|
|
|
|
data = currentOrderOwnership()?.data;
|
|
|
|
|
|
if (!form || !entry || !data || target.name !== 'member') return;
|
|
|
|
|
|
const before = data.members.find(member => member.member === data.member),
|
|
|
|
|
|
after = data.members.find(member => member.member === target.value),
|
|
|
|
|
|
preview = $('#order-ownership-selection');
|
|
|
|
|
|
if (preview) preview.textContent = before.label + ' → ' + (after?.label || '请选择新归属');
|
|
|
|
|
|
const verified = form.querySelector('[name="verified"]');
|
|
|
|
|
|
if (verified) verified.checked = false;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
function orderOwnershipOperation(url) {
|
|
|
|
|
|
const application = url.match(/^\/api\/orders\/([^/]+)\/ownership$/),
|
|
|
|
|
|
decision = url.match(/^\/api\/order-ownership-changes\/([^/]+)\/(approve|reject)$/);
|
|
|
|
|
|
return application
|
|
|
|
|
|
? { kind: 'request', orderId: decodeURIComponent(application[1]) }
|
|
|
|
|
|
: decision
|
|
|
|
|
|
? { kind: decision[2], requestId: decodeURIComponent(decision[1]) }
|
|
|
|
|
|
: null;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
function applyOrderOwnershipRestrictions() {
|
|
|
|
|
|
const form = $('#order-ownership-form');
|
|
|
|
|
|
if (!form) return;
|
|
|
|
|
|
const blocked = busy || !!pending() || !orderOwnershipFormEntry;
|
|
|
|
|
|
form.querySelectorAll('input,select,textarea,button').forEach(control => {
|
|
|
|
|
|
const action = control.dataset.action;
|
|
|
|
|
|
control.disabled =
|
|
|
|
|
|
action === 'order-ownership-close-form'
|
2026-12-04 08:58:53 -08:00
|
|
|
|
? false
|
2026-10-26 20:25:31 +08:00
|
|
|
|
: ['order-ownership-reload', 'retry'].includes(action)
|
|
|
|
|
|
? busy
|
|
|
|
|
|
: blocked;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
async function orderOwnershipError(err, operation = null, viewContext = null) {
|
|
|
|
|
|
if (viewContext && !orderOwnershipViewCurrent(viewContext)) return;
|
|
|
|
|
|
err.message = err.message.replace('不要重新开单', '不要重复提交归属修改或复核');
|
|
|
|
|
|
const id = orderOwnershipFormEntry?.id || currentOrder?.id || operation?.orderId;
|
|
|
|
|
|
if (id && [403, 404].includes(err.status) && (await handleOrderOwnershipAccessError(err, id))) return;
|
|
|
|
|
|
if ([403, 404, 409].includes(err.status)) {
|
|
|
|
|
|
orderOwnershipFormEntry = null;
|
|
|
|
|
|
if (orderOwnershipDetail)
|
|
|
|
|
|
orderOwnershipDetail = {
|
|
|
|
|
|
...orderOwnershipDetail,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
data: null,
|
2026-10-26 20:25:31 +08:00
|
|
|
|
error: '订单或归属申请已有变动,请重新读取后核对。',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
};
|
2026-10-26 20:25:31 +08:00
|
|
|
|
const verified = $('#order-ownership-form [name="verified"]');
|
|
|
|
|
|
if (verified) verified.checked = false;
|
|
|
|
|
|
if ($('#order-ownership-panel'))
|
|
|
|
|
|
$('#order-ownership-panel').innerHTML =
|
|
|
|
|
|
'<h2>请重新读取归属资料</h2><p>订单或归属申请已有变动,当前页面不能继续复核。</p>';
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
const target = $('#order-ownership-error') || $('#detail-error');
|
|
|
|
|
|
if (target)
|
|
|
|
|
|
target.innerHTML = `<div class="error">${e(err.message)}${pending() ? ' ' + button('恢复原操作', 'retry', 'danger') : id ? ' ' + button('重新读取订单并核对归属', 'order-ownership-reload', '', `data-id="${e(id)}"`) : ''}</div>`;
|
|
|
|
|
|
applyOrderOwnershipRestrictions();
|
|
|
|
|
|
toast(err.message, true);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
async function submitOrderOwnership(form) {
|
|
|
|
|
|
const { order, data } = requireOrderOwnershipDetail(),
|
|
|
|
|
|
entry = orderOwnershipFormEntry;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (
|
2026-10-26 20:25:31 +08:00
|
|
|
|
form !== $('#order-ownership-form') ||
|
2026-12-04 08:58:53 -08:00
|
|
|
|
!entry ||
|
2026-10-26 20:25:31 +08:00
|
|
|
|
entry.id !== order.id ||
|
|
|
|
|
|
entry.user !== orderOwnershipUser() ||
|
|
|
|
|
|
entry.revision !== order.revision
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-26 20:25:31 +08:00
|
|
|
|
throw new Error('请重新读取订单后核对归属。');
|
|
|
|
|
|
let url, input;
|
|
|
|
|
|
if (entry.kind === 'request') {
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (
|
2026-10-26 20:25:31 +08:00
|
|
|
|
!data.eligible ||
|
|
|
|
|
|
data.pending ||
|
|
|
|
|
|
(state.user.role === 'owner' && (!data.canDirect || !entry.direct)) ||
|
|
|
|
|
|
(state.user.role !== 'owner' && !data.requiresApproval)
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-26 20:25:31 +08:00
|
|
|
|
throw new Error(data.reason || '归属修改资格已有变动,请重新读取。');
|
|
|
|
|
|
const member = String(form.querySelector('[name="member"]')?.value || ''),
|
|
|
|
|
|
reason = String(form.querySelector('[name="reason"]')?.value || '').trim();
|
2026-11-14 01:16:35 +08:00
|
|
|
|
if (
|
|
|
|
|
|
!data.members.some(item => item.member === member && item.enabled !== false) ||
|
|
|
|
|
|
!teamMemberEnabled(member) ||
|
|
|
|
|
|
member === data.member
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-14 01:16:35 +08:00
|
|
|
|
throw new Error('请选择与原归属不同的新归属。');
|
2026-10-26 20:25:31 +08:00
|
|
|
|
if (!reason || reason.length > 200) throw new Error('修改原因请填写1~200字。');
|
|
|
|
|
|
input = { member, revision: entry.revision, reason, verified: true };
|
|
|
|
|
|
url = '/api/orders/' + encodeURIComponent(order.id) + '/ownership';
|
|
|
|
|
|
} else {
|
|
|
|
|
|
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-26 20:25:31 +08:00
|
|
|
|
const note = String(form.querySelector('[name="note"]')?.value || '').trim();
|
|
|
|
|
|
if (note.length > 200 || (entry.kind === 'reject' && !note))
|
|
|
|
|
|
throw new Error(entry.kind === 'reject' ? '驳回意见请填写1~200字。' : '复核备注最多200字。');
|
|
|
|
|
|
if (entry.kind === 'approve' && (!request.canApprove || request.stale || entry.requestRevision !== order.revision))
|
|
|
|
|
|
throw new Error('订单已有变动,不能同意旧的归属申请。');
|
|
|
|
|
|
input = entry.kind === 'approve' ? { revision: entry.requestRevision, verified: true, note } : { note };
|
|
|
|
|
|
url = '/api/order-ownership-changes/' + encodeURIComponent(request.id) + '/' + entry.kind;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (entry.kind !== 'reject' && !form.querySelector('[name="verified"]')?.checked)
|
|
|
|
|
|
throw new Error('请核对原归属、新归属和修改原因,并勾选确认。');
|
|
|
|
|
|
$('#order-ownership-error').innerHTML = '';
|
|
|
|
|
|
const detailContext = captureOrderDetailReload(order.id),
|
|
|
|
|
|
viewContext = captureOrderOwnershipView(order.id);
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = await mutate(url, input);
|
|
|
|
|
|
return await finishOrderOwnershipSave(url, input, result, detailContext, viewContext);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
return await orderOwnershipError(err, null, viewContext);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
function captureOrderOwnershipView(id = currentOrder?.id) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
id,
|
|
|
|
|
|
user: orderOwnershipUser(),
|
|
|
|
|
|
tab,
|
|
|
|
|
|
request: orderDetailRequest,
|
|
|
|
|
|
dialog: $('#order-dialog'),
|
|
|
|
|
|
wasOpen: !!$('#order-dialog')?.open,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderOwnershipViewCurrent(context) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
context.user === orderOwnershipUser() &&
|
|
|
|
|
|
context.tab === tab &&
|
|
|
|
|
|
context.request === orderDetailRequest &&
|
|
|
|
|
|
context.dialog === $('#order-dialog') &&
|
|
|
|
|
|
(!context.wasOpen || context.dialog.open)
|
|
|
|
|
|
);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
async function finishOrderOwnershipSave(
|
|
|
|
|
|
url,
|
|
|
|
|
|
input,
|
|
|
|
|
|
result,
|
|
|
|
|
|
detailContext = captureOrderDetailReload(result.id || result.order?.id),
|
|
|
|
|
|
viewContext = null,
|
|
|
|
|
|
) {
|
|
|
|
|
|
if (viewContext && !orderOwnershipViewCurrent(viewContext)) {
|
|
|
|
|
|
const active = () => viewContext.user === orderOwnershipUser() && viewContext.tab === tab;
|
|
|
|
|
|
if (active())
|
|
|
|
|
|
try {
|
|
|
|
|
|
await refresh(false, active);
|
|
|
|
|
|
if (active() && tab === 'orders') await loadOrderBook();
|
|
|
|
|
|
} catch {}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const operation = orderOwnershipOperation(url),
|
|
|
|
|
|
id = result.id || result.order?.id || operation?.orderId;
|
|
|
|
|
|
if (!id) throw new Error('归属操作已经保存,请刷新订单列表查看。');
|
2026-11-14 01:16:35 +08:00
|
|
|
|
const title =
|
|
|
|
|
|
operation?.kind === 'reject'
|
|
|
|
|
|
? '归属申请已驳回'
|
|
|
|
|
|
: result.applied || operation?.kind === 'approve'
|
|
|
|
|
|
? '订单归属已修改'
|
|
|
|
|
|
: '归属申请已提交,等待店主复核';
|
2026-10-26 20:25:31 +08:00
|
|
|
|
orderOwnershipFormEntry = null;
|
|
|
|
|
|
orderOwnershipDetail = null;
|
|
|
|
|
|
currentOrder = null;
|
|
|
|
|
|
if (typeof invalidateOrderChangeViews === 'function') invalidateOrderChangeViews();
|
|
|
|
|
|
if (typeof invalidateOrderCancellationViews === 'function') invalidateOrderCancellationViews();
|
|
|
|
|
|
invalidateMaterialPools();
|
|
|
|
|
|
invalidateReport();
|
|
|
|
|
|
invalidatePriceLockViews();
|
|
|
|
|
|
inventoryPage = null;
|
|
|
|
|
|
++orderBookRequest;
|
|
|
|
|
|
if (detailContext) detailContext = { ...detailContext, bookRequest: orderBookRequest };
|
|
|
|
|
|
if ($('#order-content'))
|
|
|
|
|
|
$('#order-content').innerHTML =
|
|
|
|
|
|
`<div class="order-header"><h2>${e(title)}</h2><p>操作已经保存,原单号和处理记录保留。</p><div id="order-ownership-saved-status" aria-live="polite"></div>${button('重新读取订单', 'order-ownership-reload', 'primary', `data-id="${e(id)}"`)}</div>`;
|
|
|
|
|
|
try {
|
|
|
|
|
|
const refreshed = await refreshOrderDetailAfterSave(id, detailContext);
|
|
|
|
|
|
if (refreshed !== false) toast(title);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (await handleOrderOwnershipAccessError(err, id)) return;
|
|
|
|
|
|
if ($('#order-ownership-saved-status'))
|
|
|
|
|
|
$('#order-ownership-saved-status').textContent = '保存已成功,最新资料暂未读到。请重新读取订单,不要重复提交。';
|
|
|
|
|
|
toast('归属操作已保存,请重新读取订单,不要重复提交。', true);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
if (tab === 'orders') await loadOrderOwnershipCount();
|
|
|
|
|
|
}
|
|
|
|
|
|
function validateOrderOwnershipList(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-26 20:25:31 +08:00
|
|
|
|
throw new Error('归属复核列表未完整返回,请重新读取。');
|
|
|
|
|
|
result.requests.forEach(request => validateOrderOwnershipRequest(request));
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
async function loadOrderOwnershipCount() {
|
|
|
|
|
|
const request = ++orderOwnershipCountRequest,
|
|
|
|
|
|
user = orderOwnershipUser();
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = validateOrderOwnershipList(await api('/api/order-ownership-changes?status=pending')),
|
|
|
|
|
|
target = $('#order-ownership-entry');
|
|
|
|
|
|
if (request !== orderOwnershipCountRequest || user !== orderOwnershipUser() || tab !== 'orders' || !target) return;
|
|
|
|
|
|
target.textContent = '归属复核(' + result.counts.pending + ')';
|
|
|
|
|
|
target.classList.toggle('danger', result.counts.pending > 0);
|
|
|
|
|
|
target.title = '全部日期的订单归属申请';
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
const target = $('#order-ownership-entry');
|
|
|
|
|
|
if (request === orderOwnershipCountRequest && user === orderOwnershipUser() && tab === 'orders' && target) {
|
|
|
|
|
|
target.textContent = '归属复核(读取失败)';
|
|
|
|
|
|
target.title = '点击重新读取全部日期的归属申请';
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
function orderOwnershipListHTML(result) {
|
|
|
|
|
|
const owner = state.user.role === 'owner' && result.canApprove;
|
|
|
|
|
|
return `<div class="change-list-tabs" role="group" aria-label="归属申请状态">${[
|
|
|
|
|
|
['pending', '待复核'],
|
|
|
|
|
|
['approved', '已修改'],
|
|
|
|
|
|
['rejected', '已驳回'],
|
|
|
|
|
|
['all', '全部'],
|
2026-12-04 08:58:53 -08:00
|
|
|
|
]
|
2026-10-26 20:25:31 +08:00
|
|
|
|
.map(([status, name]) =>
|
|
|
|
|
|
button(
|
|
|
|
|
|
name + (status === 'all' ? '' : '(' + result.counts[status] + ')'),
|
|
|
|
|
|
'order-ownership-filter',
|
|
|
|
|
|
orderOwnershipListStatus === status ? 'primary' : '',
|
|
|
|
|
|
`data-status="${status}" aria-pressed="${orderOwnershipListStatus === status}"`,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
),
|
|
|
|
|
|
)
|
2026-10-26 20:25:31 +08:00
|
|
|
|
.join(
|
|
|
|
|
|
'',
|
|
|
|
|
|
)}</div>${result.requests.length ? `<div class="table-wrap"><table class="change-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 class="order-ownership-list-customer">${e(request.order_customer || '未填客户称呼')}</strong><div class="small muted">${e(request.order_number)} · ${e(request.order_date)}</div></td><td>${orderOwnershipPairHTML(request)}</td><td>${e(request.requester_name)}<div class="small muted">${e(orderOwnershipTime(request.created))}</div><p class="change-reason">${e(request.reason)}</p></td><td>${e(orderOwnershipStatus[request.status])}${request.stale && request.status === 'pending' ? '<div class="small danger-text">订单已有变动,需驳回后重申</div>' : ''}${request.note ? `<p class="small change-reason">${e(request.note)}</p>` : ''}${request.reviewer_name ? `<div class="small muted">${e(request.reviewer_name)} · ${e(orderOwnershipTime(request.reviewed))}</div>` : ''}</td><td>${button(owner && request.status === 'pending' ? '查看并复核' : '查看订单', 'order-ownership-open-order', '', `data-id="${e(request.order_id)}"`)}</td></tr>`).join('')}</tbody></table></div>` : '<p class="empty">全部日期均无此状态的归属申请。</p>'}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
async function openOrderOwnerships(status = 'pending') {
|
|
|
|
|
|
orderOwnershipListStatus = Object.hasOwn(orderOwnershipStatus, status) || status === 'all' ? status : 'pending';
|
|
|
|
|
|
let dialog = $('#order-ownership-dialog');
|
|
|
|
|
|
if (!dialog) {
|
|
|
|
|
|
dialog = document.createElement('dialog');
|
|
|
|
|
|
dialog.id = 'order-ownership-dialog';
|
|
|
|
|
|
dialog.setAttribute('aria-labelledby', 'order-ownership-title');
|
|
|
|
|
|
dialog.addEventListener('cancel', () => {
|
|
|
|
|
|
++orderOwnershipListRequest;
|
|
|
|
|
|
});
|
|
|
|
|
|
document.body.append(dialog);
|
|
|
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
|
dialog.innerHTML = `<button class="dialog-close" data-action="order-ownership-close-list" aria-label="关闭归属复核列表">×</button><h2 id="order-ownership-title">订单归属申请与复核</h2><p class="small muted">全部日期,不受订单页筛选影响。${e(teamMemberName('owner'))}可直接更正归属;助理的归属申请均须店主同意后生效。</p><div id="order-ownership-list" aria-live="polite" aria-busy="true">正在读取…</div>`;
|
|
|
|
|
|
if (!dialog.open) dialog.showModal();
|
|
|
|
|
|
return await loadOrderOwnerships();
|
2026-10-26 20:25:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
async function loadOrderOwnerships() {
|
|
|
|
|
|
const dialog = $('#order-ownership-dialog'),
|
|
|
|
|
|
target = $('#order-ownership-list');
|
|
|
|
|
|
if (!dialog?.open || !target) return;
|
|
|
|
|
|
const request = ++orderOwnershipListRequest,
|
|
|
|
|
|
user = orderOwnershipUser(),
|
|
|
|
|
|
status = orderOwnershipListStatus;
|
|
|
|
|
|
target.setAttribute('aria-busy', 'true');
|
|
|
|
|
|
target.innerHTML = '正在读取全部日期的归属申请…';
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = validateOrderOwnershipList(
|
|
|
|
|
|
await api('/api/order-ownership-changes?' + new URLSearchParams({ status })),
|
|
|
|
|
|
);
|
|
|
|
|
|
if (
|
|
|
|
|
|
request !== orderOwnershipListRequest ||
|
|
|
|
|
|
user !== orderOwnershipUser() ||
|
|
|
|
|
|
!dialog.open ||
|
|
|
|
|
|
status !== orderOwnershipListStatus
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-26 20:25:31 +08:00
|
|
|
|
return;
|
|
|
|
|
|
target.innerHTML = orderOwnershipListHTML(result);
|
|
|
|
|
|
target.setAttribute('aria-busy', 'false');
|
|
|
|
|
|
const entry = $('#order-ownership-entry');
|
|
|
|
|
|
if (entry) entry.textContent = '归属复核(' + result.counts.pending + ')';
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (request !== orderOwnershipListRequest || user !== orderOwnershipUser() || !dialog.open) return;
|
|
|
|
|
|
target.setAttribute('aria-busy', 'false');
|
|
|
|
|
|
target.innerHTML = `<div class="error">${e(err.name === 'AbortError' ? '读取超时,请重新读取。' : err.message)} ${button('重新读取', 'order-ownership-retry-list')}</div>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
function closeOrderOwnerships() {
|
|
|
|
|
|
++orderOwnershipListRequest;
|
|
|
|
|
|
$('#order-ownership-dialog')?.close();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
async function orderOwnershipAction(action, button) {
|
|
|
|
|
|
if (action === 'order-ownership-open-list') return await openOrderOwnerships();
|
|
|
|
|
|
if (action === 'order-ownership-close-list') {
|
|
|
|
|
|
closeOrderOwnerships();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'order-ownership-filter') {
|
|
|
|
|
|
orderOwnershipListStatus =
|
|
|
|
|
|
Object.hasOwn(orderOwnershipStatus, button.dataset.status) || button.dataset.status === 'all'
|
|
|
|
|
|
? button.dataset.status
|
|
|
|
|
|
: 'pending';
|
|
|
|
|
|
return await loadOrderOwnerships();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
if (action === 'order-ownership-retry-list') return await loadOrderOwnerships();
|
|
|
|
|
|
if (action === 'order-ownership-open-order') {
|
|
|
|
|
|
closeOrderOwnerships();
|
|
|
|
|
|
return await openOrder(button.dataset.id);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'order-ownership-reload')
|
|
|
|
|
|
return orderDetailRetry?.savedContext && orderDetailRetry.id === button.dataset.id
|
|
|
|
|
|
? await retryOrderDetail()
|
|
|
|
|
|
: await refreshOrderDetailAfterSave(button.dataset.id);
|
|
|
|
|
|
if (action === 'order-ownership-close-form') {
|
|
|
|
|
|
closeOrderOwnershipForm();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'order-ownership-request') {
|
|
|
|
|
|
if (currentOrder?.id !== button.dataset.id) throw new Error('当前订单已有变化,请重新读取。');
|
|
|
|
|
|
return showOrderOwnershipForm();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'order-ownership-approve' || action === 'order-ownership-reject') {
|
|
|
|
|
|
if (currentOrderOwnership()?.data?.pending?.id !== button.dataset.id)
|
|
|
|
|
|
throw new Error('归属申请已有变化,请重新读取。');
|
|
|
|
|
|
return showOrderOwnershipForm(action === 'order-ownership-approve' ? 'approve' : 'reject');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
function invalidateOrderOwnershipViews() {
|
|
|
|
|
|
++orderOwnershipCountRequest;
|
|
|
|
|
|
++orderOwnershipAccessRequest;
|
|
|
|
|
|
closeOrderOwnerships();
|
|
|
|
|
|
orderOwnershipDetail = null;
|
|
|
|
|
|
orderOwnershipFormEntry = null;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
async function handleOrderOwnershipAccessError(err, id) {
|
|
|
|
|
|
if (![403, 404].includes(err.status) || state?.user?.role !== 'assistant') return false;
|
|
|
|
|
|
const user = orderOwnershipUser(),
|
|
|
|
|
|
fromEdit = tab === 'order-edit';
|
|
|
|
|
|
invalidateOrderOwnershipViews();
|
|
|
|
|
|
invalidateOrderDetailView(true);
|
|
|
|
|
|
invalidateOrderBookDetailList();
|
|
|
|
|
|
if (typeof orderEditDraftKey === 'function') localStorage.removeItem(orderEditDraftKey(id));
|
|
|
|
|
|
if (typeof orderEditActiveKey === 'function' && localStorage.getItem(orderEditActiveKey()) === id)
|
|
|
|
|
|
localStorage.removeItem(orderEditActiveKey());
|
|
|
|
|
|
if (typeof orderEditEntry !== 'undefined' && orderEditEntry?.id === id) {
|
|
|
|
|
|
++orderEditRequest;
|
|
|
|
|
|
orderEditEntry = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (fromEdit) {
|
|
|
|
|
|
tab = 'orders';
|
|
|
|
|
|
if ($('#main')) $('#main').innerHTML = '<p class="muted">正在更新可查看的订单…</p>';
|
|
|
|
|
|
document
|
|
|
|
|
|
.querySelectorAll('nav button')
|
|
|
|
|
|
.forEach(button => button.classList.toggle('active', button.dataset.tab === tab));
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
if (typeof invalidateOrderChangeViews === 'function') invalidateOrderChangeViews();
|
|
|
|
|
|
if (typeof invalidateOrderCancellationViews === 'function') invalidateOrderCancellationViews();
|
|
|
|
|
|
if ($('#order-content')) $('#order-content').innerHTML = '';
|
|
|
|
|
|
if ($('#order-list')) $('#order-list').innerHTML = '<p class="muted">正在更新可查看的订单…</p>';
|
|
|
|
|
|
const active = () => user === orderOwnershipUser();
|
|
|
|
|
|
try {
|
|
|
|
|
|
await refresh(false, active);
|
|
|
|
|
|
if (active() && tab === 'orders') {
|
|
|
|
|
|
if (fromEdit) await renderOrders();
|
|
|
|
|
|
else await loadOrderBook();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
} catch {
|
|
|
|
|
|
if (active() && tab === 'orders' && $('#order-list'))
|
|
|
|
|
|
orderBookError('当前账号已不能查看这张订单。请刷新订单列表。');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
if (active()) toast('订单归属或权限已更新,本账号已不能查看这张订单。');
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
async function checkOrderOwnershipAfterEditError(err, id) {
|
|
|
|
|
|
if (!id || ![403, 404].includes(err.status) || state?.user?.role !== 'assistant') return false;
|
|
|
|
|
|
const user = orderOwnershipUser(),
|
|
|
|
|
|
originTab = tab,
|
|
|
|
|
|
entry = typeof orderEditEntry !== 'undefined' ? orderEditEntry : null,
|
|
|
|
|
|
request = typeof orderEditRequest !== 'undefined' ? orderEditRequest : null;
|
|
|
|
|
|
if (entry && (entry.id !== id || entry.user !== user)) return true;
|
|
|
|
|
|
const active = () =>
|
|
|
|
|
|
user === orderOwnershipUser() &&
|
|
|
|
|
|
originTab === tab &&
|
|
|
|
|
|
(typeof orderEditEntry === 'undefined' || orderEditEntry === entry) &&
|
|
|
|
|
|
(typeof orderEditRequest === 'undefined' || orderEditRequest === request);
|
|
|
|
|
|
try {
|
|
|
|
|
|
await api('/api/orders/' + encodeURIComponent(id) + '/ownership');
|
|
|
|
|
|
return !active();
|
|
|
|
|
|
} catch (accessError) {
|
|
|
|
|
|
return !active() || (await handleOrderOwnershipAccessError(accessError, id));
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
async function recheckOrderOwnershipAccess() {
|
|
|
|
|
|
const editing = tab === 'order-edit' && typeof orderEditEntry !== 'undefined' ? orderEditEntry : null,
|
|
|
|
|
|
order = currentOrder || editing?.order;
|
|
|
|
|
|
if (
|
|
|
|
|
|
!order ||
|
|
|
|
|
|
state?.user?.role !== 'assistant' ||
|
|
|
|
|
|
(!editing && !$('#order-dialog')?.open) ||
|
|
|
|
|
|
busy ||
|
|
|
|
|
|
pending() ||
|
|
|
|
|
|
orderDetailLoad
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-26 20:25:31 +08:00
|
|
|
|
return;
|
|
|
|
|
|
const request = ++orderOwnershipAccessRequest,
|
|
|
|
|
|
user = orderOwnershipUser();
|
|
|
|
|
|
try {
|
|
|
|
|
|
await api('/api/orders/' + encodeURIComponent(order.id) + '/ownership');
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (
|
|
|
|
|
|
request === orderOwnershipAccessRequest &&
|
|
|
|
|
|
user === orderOwnershipUser() &&
|
|
|
|
|
|
(currentOrder?.id === order.id || (editing && orderEditEntry === editing))
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-26 20:25:31 +08:00
|
|
|
|
await handleOrderOwnershipAccessError(err, order.id);
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-26 20:25:31 +08:00
|
|
|
|
window.addEventListener?.('focus', () => {
|
|
|
|
|
|
recheckOrderOwnershipAccess();
|
|
|
|
|
|
});
|
|
|
|
|
|
document.addEventListener('visibilitychange', () => {
|
|
|
|
|
|
if (document.visibilityState === 'visible') recheckOrderOwnershipAccess();
|
|
|
|
|
|
});
|