2026-11-08 02:22:58 +08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
// 待交付修改和录错作废使用独立草稿;只有服务器确认后才替换原账。
|
|
|
|
|
|
let priceLockEditEntry = null,
|
|
|
|
|
|
priceLockPendingRequest = 0,
|
|
|
|
|
|
priceLockPendingAction = null,
|
|
|
|
|
|
priceLockChangeEntry = null,
|
|
|
|
|
|
priceLockChangesRequest = 0,
|
|
|
|
|
|
priceLockChangesStatus = 'pending',
|
|
|
|
|
|
priceLockListStatus = 'pending';
|
|
|
|
|
|
const priceLockEditDraftKey = id => `mm-price-lock-edit-${session.user.id}-${id}`;
|
|
|
|
|
|
const priceLockStatusName = status =>
|
|
|
|
|
|
({ pending: '待交付,不计利润', delivered: '已交付', cancelled: '已作废' })[status] || '状态待核对';
|
|
|
|
|
|
function priceLockEditAllowsPoolDate(form, date) {
|
|
|
|
|
|
return editingPendingPriceLock(form) && date === priceLockEditEntry.lock.date;
|
|
|
|
|
|
}
|
|
|
|
|
|
function priceLockPendingButtonsHTML(lock) {
|
|
|
|
|
|
return ['owner', 'assistant'].includes(session.user.role)
|
|
|
|
|
|
? button('修改', 'edit-price-lock', 'quiet', `data-id="${e(lock.id)}"`) +
|
|
|
|
|
|
button('作废', 'cancel-price-lock', 'quiet', `data-id="${e(lock.id)}"`)
|
|
|
|
|
|
: '';
|
|
|
|
|
|
}
|
|
|
|
|
|
function priceLockRelatedOrdersHTML(rows) {
|
|
|
|
|
|
const money = value => (value === null ? '待核算' : Number.isSafeInteger(value) ? '¥' + yuan(value) : '未提供');
|
|
|
|
|
|
return `<section class="lock-related-orders"><h3>受影响的销售单(${rows.length}张)</h3><div class="table-wrap"><table><thead><tr><th>订单</th><th>货品成本:当前 → 生效后</th><th>利润:当前 → 生效后</th></tr></thead><tbody>${rows.map(row => `<tr><td>${e(row.date)} · ${e(row.number || row.id)}</td><td>${money(row.before?.goodsCost)} → ${money(row.after?.goodsCost)}</td><td>${money(row.before?.profit)} → ${money(row.after?.profit)}</td></tr>`).join('')}</tbody></table></div></section>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function editingPendingPriceLock(form = $('#price-lock-create-form')) {
|
|
|
|
|
|
return !!(
|
|
|
|
|
|
priceLockEditEntry &&
|
|
|
|
|
|
form?.dataset.priceLockEditId === priceLockEditEntry.id &&
|
|
|
|
|
|
priceLockEditEntry.user === session.user.id
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
function priceLockPendingNeedsApproval(entry, input = entry.inputs) {
|
|
|
|
|
|
return session.user.role === 'assistant' && (entry.requiresApproval !== false || input.date !== state.today);
|
|
|
|
|
|
}
|
|
|
|
|
|
function priceLockPendingEditReviewHTML(entry) {
|
|
|
|
|
|
const approval = priceLockPendingNeedsApproval(entry);
|
2026-11-14 01:16:35 +08:00
|
|
|
|
return `<section class="panel lock-pending-review"><p class="note" data-lock-edit-notice>${approval ? '提交修改申请,店主复核同意后生效;待批期间沿用原锁价记账。' : '保存后按核实后的整单资料更正锁价记录、收款与原锁料费。'}</p>${entry.conflicted ? `<div class="error">原锁价单已有新记录,已填写内容已保留。请重新读取当前资料后核对。${button('使用当前锁价资料', 'reset-lock-edit', 'quiet', `data-id="${e(entry.id)}"`)}</div>` : ''}<label>修改原因<textarea name="pending_edit_reason" required maxlength="200" rows="2">${e(entry.reason || '')}</textarea></label><label class="check"><input type="checkbox" name="pending_edit_verified" required>已核实整单商品、原锁金料、客户实际已付款与账户,确认本次更正。</label><div class="actions">${button('退出修改', 'leave-lock-edit', 'quiet')}</div></section>`;
|
2026-11-08 02:22:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
function syncPendingPriceLockEditDraft(data, form) {
|
|
|
|
|
|
if (!editingPendingPriceLock(form)) return false;
|
|
|
|
|
|
const entry = priceLockEditEntry,
|
|
|
|
|
|
signature = JSON.stringify(data);
|
|
|
|
|
|
if (entry.signature && entry.signature !== signature) {
|
|
|
|
|
|
const check = form.querySelector('[name="pending_edit_verified"]');
|
|
|
|
|
|
if (check) check.checked = false;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 02:22:58 +08:00
|
|
|
|
entry.signature = signature;
|
|
|
|
|
|
entry.inputs = JSON.parse(JSON.stringify(data));
|
|
|
|
|
|
entry.reason = lockFormValue(form, 'pending_edit_reason');
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
|
|
priceLockEditDraftKey(entry.id),
|
|
|
|
|
|
JSON.stringify({ id: entry.id, revision: entry.revision, inputs: entry.inputs, reason: entry.reason }),
|
|
|
|
|
|
);
|
|
|
|
|
|
const approval = priceLockPendingNeedsApproval(entry, data),
|
|
|
|
|
|
submit = form.querySelector('[data-lock-create-submit]'),
|
|
|
|
|
|
notice = form.querySelector('[data-lock-edit-notice]');
|
|
|
|
|
|
if (submit) {
|
|
|
|
|
|
submit.textContent = approval ? '提交修改申请' : '保存锁价修改';
|
|
|
|
|
|
submit.disabled = entry.conflicted || !!pending();
|
|
|
|
|
|
}
|
2026-11-14 01:16:35 +08:00
|
|
|
|
if (notice)
|
|
|
|
|
|
notice.textContent = approval
|
|
|
|
|
|
? '提交修改申请,店主复核同意后生效;待批期间沿用原锁价记账。'
|
|
|
|
|
|
: '保存后按核实后的整单资料更正锁价记录、收款与原锁料费。';
|
|
|
|
|
|
return true;
|
2026-11-08 02:22:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
async function startPriceLockEdit(id, reset = false) {
|
|
|
|
|
|
if (pending()) throw new Error('请先恢复上次未确认的操作,再修改锁价单。');
|
|
|
|
|
|
const request = ++priceLockPendingRequest,
|
|
|
|
|
|
user = session.user.id,
|
|
|
|
|
|
originTab = tab,
|
|
|
|
|
|
result = await api('/api/price-locks/' + encodeURIComponent(id) + '/edit-context');
|
|
|
|
|
|
if (request !== priceLockPendingRequest || session.user.id !== user || tab !== originTab) return;
|
|
|
|
|
|
if (
|
|
|
|
|
|
result?.eligible !== true ||
|
|
|
|
|
|
result.lock?.id !== id ||
|
|
|
|
|
|
result.lock?.status !== 'pending' ||
|
|
|
|
|
|
!Number.isSafeInteger(result.revision) ||
|
|
|
|
|
|
!Array.isArray(result.inputs?.lines) ||
|
|
|
|
|
|
!result.inputs.lines.length ||
|
|
|
|
|
|
typeof result.requiresApproval !== 'boolean'
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-08 02:22:58 +08:00
|
|
|
|
throw new Error(result.reason || '锁价修改资料未完整返回,请重新读取。');
|
|
|
|
|
|
const saved = reset ? null : priceLockReadDraft(priceLockEditDraftKey(id)),
|
|
|
|
|
|
conflicted = !!saved && saved.revision !== result.revision;
|
|
|
|
|
|
priceLockEditEntry = {
|
|
|
|
|
|
id,
|
|
|
|
|
|
user,
|
|
|
|
|
|
revision: saved?.revision ?? result.revision,
|
|
|
|
|
|
requiresApproval: result.requiresApproval,
|
|
|
|
|
|
lock: result.lock,
|
|
|
|
|
|
inputs: JSON.parse(JSON.stringify(saved?.inputs || result.inputs)),
|
|
|
|
|
|
reason: saved?.reason || '',
|
|
|
|
|
|
conflicted,
|
|
|
|
|
|
};
|
|
|
|
|
|
$('#price-lock-dialog')?.close();
|
|
|
|
|
|
createMode = 'lock';
|
|
|
|
|
|
tab = 'create';
|
|
|
|
|
|
renderPriceLockCreate();
|
|
|
|
|
|
document
|
|
|
|
|
|
.querySelectorAll('nav button')
|
|
|
|
|
|
.forEach(button => button.classList.toggle('active', button.dataset.tab === tab));
|
|
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
function leavePendingPriceLockEdit() {
|
|
|
|
|
|
priceLockEditEntry = null;
|
|
|
|
|
|
++priceLockPendingRequest;
|
|
|
|
|
|
priceLockDeliveryId = '';
|
|
|
|
|
|
tab = 'price-locks';
|
|
|
|
|
|
return renderPriceLocks();
|
|
|
|
|
|
}
|
|
|
|
|
|
function pendingPriceLockEditPayload(data, form) {
|
|
|
|
|
|
const entry = priceLockEditEntry;
|
|
|
|
|
|
if (!editingPendingPriceLock(form) || entry.conflicted)
|
|
|
|
|
|
throw new Error('锁价单已有新记录,请重新读取当前资料后再修改。');
|
|
|
|
|
|
const reason = lockFormValue(form, 'pending_edit_reason').trim();
|
|
|
|
|
|
if (!reason) throw new Error('请填写修改原因。');
|
|
|
|
|
|
if (!form.querySelector('[name="pending_edit_verified"]')?.checked) throw new Error('请核实整单更正资料并勾选确认。');
|
|
|
|
|
|
const approval = priceLockPendingNeedsApproval(entry, data);
|
|
|
|
|
|
return {
|
|
|
|
|
|
url: '/api/price-locks/' + encodeURIComponent(entry.id) + (approval ? '/change-requests' : '/edit'),
|
|
|
|
|
|
data: { ...data, revision: entry.revision, reason, verified: true, ...(approval ? { action: 'edit' } : {}) },
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
async function openPriceLockCancel(id) {
|
|
|
|
|
|
if (pending()) throw new Error('请先恢复上次未确认的操作,再作废锁价单。');
|
|
|
|
|
|
const request = ++priceLockPendingRequest,
|
|
|
|
|
|
user = session.user.id,
|
|
|
|
|
|
dialog = priceLockDialog();
|
|
|
|
|
|
dialog.innerHTML = `<button class="dialog-close" data-action="close-price-lock" aria-label="关闭">×</button><p>正在读取锁价记录…</p>`;
|
|
|
|
|
|
if (!dialog.open) dialog.showModal();
|
|
|
|
|
|
const result = await api('/api/price-locks/' + encodeURIComponent(id) + '/edit-context');
|
|
|
|
|
|
if (request !== priceLockPendingRequest || session.user.id !== user || !dialog.open) return;
|
|
|
|
|
|
if (
|
|
|
|
|
|
result?.eligible !== true ||
|
|
|
|
|
|
result.lock?.id !== id ||
|
|
|
|
|
|
result.lock.status !== 'pending' ||
|
|
|
|
|
|
!Number.isSafeInteger(result.revision) ||
|
|
|
|
|
|
typeof result.requiresApproval !== 'boolean'
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-08 02:22:58 +08:00
|
|
|
|
throw new Error(result.reason || '这张锁价单暂不能作废,请重新读取。');
|
|
|
|
|
|
priceLockPendingAction = {
|
|
|
|
|
|
id,
|
|
|
|
|
|
user,
|
|
|
|
|
|
revision: result.revision,
|
|
|
|
|
|
requiresApproval: result.requiresApproval,
|
|
|
|
|
|
inputs: result.inputs,
|
|
|
|
|
|
lock: result.lock,
|
|
|
|
|
|
};
|
|
|
|
|
|
const approval = priceLockPendingNeedsApproval(priceLockPendingAction);
|
2026-12-04 00:53:53 +08:00
|
|
|
|
dialog.innerHTML = `<button class="dialog-close" data-action="close-price-lock" aria-label="关闭">×</button><div class="order-header"><div class="eyebrow">VOID PRICE LOCK</div><h2>${approval ? '申请作废' : '作废'} · ${e(result.lock.customer)}</h2></div>${priceLockFacts(result.lock)}<div class="note"><strong>录错作废</strong><p>生效后撤销本单有效收款、手填购料与信用卡手续费记账,同日释放料池预留;跨天按原锁定克重和原成本回到实际作废日料池,参与当天加权,历史订单成本不变。保留原单和作废记录。适用于录错单据。</p><p>本操作不登记真实客户退款,也不登记实物退料。</p></div><form id="price-lock-cancel-form"><label>作废原因<textarea name="reason" required maxlength="200" rows="2"></textarea></label><label class="check"><input name="verified" type="checkbox" required>已核实这是一张录错的待交付锁价单,确认整单作废。</label>${approval ? '<p class="note">提交申请后,店主复核同意才撤销记账;待批期间原锁价单保持有效。</p>' : ''}<div class="actions">${button('返回锁价详情', 'open-price-lock', 'quiet', `data-id="${e(id)}"`)}<button class="danger" type="submit">${approval ? '提交作废申请' : '确认整单作废'}</button></div><div id="price-lock-action-error" aria-live="polite"></div></form>`;
|
2026-11-08 02:22:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
async function submitPriceLockCancel(form) {
|
|
|
|
|
|
const entry = priceLockPendingAction;
|
|
|
|
|
|
if (!entry || entry.user !== session.user.id) throw new Error('请重新读取锁价单后再作废。');
|
|
|
|
|
|
const reason = lockFormValue(form, 'reason').trim();
|
|
|
|
|
|
if (!reason) throw new Error('请填写作废原因。');
|
|
|
|
|
|
if (!form.querySelector('[name="verified"]')?.checked) throw new Error('请核实录错单据并勾选整单作废。');
|
|
|
|
|
|
const approval = priceLockPendingNeedsApproval(entry),
|
|
|
|
|
|
url = '/api/price-locks/' + encodeURIComponent(entry.id) + (approval ? '/change-requests' : '/cancel'),
|
|
|
|
|
|
data = { revision: entry.revision, reason, verified: true, ...(approval ? { action: 'cancel' } : {}) },
|
|
|
|
|
|
result = await mutate(url, data);
|
|
|
|
|
|
await finishPendingPriceLockSave(url, data, result);
|
|
|
|
|
|
}
|
|
|
|
|
|
function priceLockPendingOperation(url) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
/^\/api\/price-locks\/[^/]+\/(?:edit|cancel|change-requests)$/.test(url) ||
|
|
|
|
|
|
/^\/api\/price-lock-changes\/[^/]+\/(?:approve|reject)$/.test(url)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
async function finishPendingPriceLockSave(url, data, result) {
|
|
|
|
|
|
const request = url.endsWith('/change-requests'),
|
|
|
|
|
|
review = /\/price-lock-changes\//.test(url),
|
|
|
|
|
|
id = result.price_lock_id || result.request?.price_lock_id || result.lock?.id || url.split('/')[3];
|
|
|
|
|
|
if (!request && !review) localStorage.removeItem(priceLockEditDraftKey(id));
|
|
|
|
|
|
if (request && data.action === 'edit') localStorage.removeItem(priceLockEditDraftKey(id));
|
|
|
|
|
|
priceLockEditEntry = null;
|
|
|
|
|
|
priceLockPendingAction = null;
|
|
|
|
|
|
priceLockChangeEntry = null;
|
|
|
|
|
|
priceLockDeliveryId = '';
|
|
|
|
|
|
++priceLockPendingRequest;
|
|
|
|
|
|
++priceLockChangesRequest;
|
|
|
|
|
|
invalidatePriceLockViews();
|
|
|
|
|
|
if (typeof invalidateMaterialPools === 'function') invalidateMaterialPools();
|
|
|
|
|
|
if (typeof invalidateReport === 'function') invalidateReport();
|
|
|
|
|
|
$('#price-lock-dialog')?.close();
|
|
|
|
|
|
$('#price-lock-changes-dialog')?.close();
|
|
|
|
|
|
tab = 'price-locks';
|
|
|
|
|
|
const title = request
|
|
|
|
|
|
? '锁价申请已提交'
|
|
|
|
|
|
: review
|
|
|
|
|
|
? url.endsWith('/approve')
|
|
|
|
|
|
? '锁价申请已批准'
|
|
|
|
|
|
: '锁价申请已驳回'
|
|
|
|
|
|
: url.endsWith('/cancel')
|
|
|
|
|
|
? '锁价单已作废'
|
|
|
|
|
|
: '锁价修改已保存';
|
2026-11-14 01:16:35 +08:00
|
|
|
|
$('#main').innerHTML =
|
|
|
|
|
|
heading(
|
|
|
|
|
|
'PRICE LOCK SAVED',
|
|
|
|
|
|
title,
|
|
|
|
|
|
request
|
|
|
|
|
|
? '原锁价单在店主复核同意后更新;待批期间按原记录记账。'
|
|
|
|
|
|
: review
|
|
|
|
|
|
? '复核结果已经保存,可重新查看申请与锁价单。'
|
|
|
|
|
|
: '原单号保留,相关有效记账已按本次核实结果更新。',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
) +
|
2026-11-14 01:16:35 +08:00
|
|
|
|
`<section class="panel"><div class="actions">${button('查看锁价单', 'open-price-lock', 'primary', `data-id="${e(id)}"`)}${button('返回待交付', 'refresh-price-locks')}${button('查看修改与作废申请', 'price-lock-changes-open')}</div></section>`;
|
2026-11-08 02:22:58 +08:00
|
|
|
|
document
|
|
|
|
|
|
.querySelectorAll('nav button')
|
|
|
|
|
|
.forEach(button => button.classList.toggle('active', button.dataset.tab === tab));
|
|
|
|
|
|
toast(title);
|
|
|
|
|
|
}
|
|
|
|
|
|
function priceLockPendingError(error, form) {
|
|
|
|
|
|
if (form?.dataset.priceLockEditId && priceLockEditEntry && error.status === 409) {
|
|
|
|
|
|
priceLockEditEntry.conflicted = true;
|
|
|
|
|
|
const submit = form.querySelector('[data-lock-create-submit]');
|
|
|
|
|
|
if (submit) submit.disabled = true;
|
|
|
|
|
|
form.querySelector('[name="pending_edit_verified"]') &&
|
|
|
|
|
|
(form.querySelector('[name="pending_edit_verified"]').checked = false);
|
|
|
|
|
|
}
|
|
|
|
|
|
const root = form?.id === 'price-lock-create-form' ? $('#price-lock-error') : $('#price-lock-action-error');
|
|
|
|
|
|
if (root)
|
|
|
|
|
|
root.innerHTML = `<div class="error">${e(error.message)}${form?.dataset.priceLockEditId && error.status === 409 ? ' ' + button('使用当前锁价资料', 'reset-lock-edit', 'quiet', `data-id="${e(form.dataset.priceLockEditId)}"`) : ''}${pending() ? ' ' + button('恢复原操作', 'retry', 'danger') : ''}</div>`;
|
|
|
|
|
|
toast(error.message, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
function priceLockChangesDialog() {
|
|
|
|
|
|
let dialog = $('#price-lock-changes-dialog');
|
|
|
|
|
|
if (!dialog) {
|
|
|
|
|
|
dialog = document.createElement('dialog');
|
|
|
|
|
|
dialog.id = 'price-lock-changes-dialog';
|
|
|
|
|
|
document.body.append(dialog);
|
|
|
|
|
|
}
|
|
|
|
|
|
return dialog;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 02:22:58 +08:00
|
|
|
|
async function openPriceLockChanges() {
|
|
|
|
|
|
const dialog = priceLockChangesDialog();
|
|
|
|
|
|
dialog.innerHTML = `<button class="dialog-close" data-action="price-lock-changes-close" aria-label="关闭">×</button><h2>锁价修改与作废申请</h2><div class="actions">${button('待复核', 'price-lock-changes-filter', priceLockChangesStatus === 'pending' ? 'primary' : '', 'data-status="pending"')}${button('全部记录', 'price-lock-changes-filter', priceLockChangesStatus === 'all' ? 'primary' : '', 'data-status="all"')}${button('刷新', 'price-lock-changes-reload')}</div><div id="price-lock-changes-content" aria-live="polite">正在读取申请…</div>`;
|
|
|
|
|
|
if (!dialog.open) dialog.showModal();
|
|
|
|
|
|
return loadPriceLockChanges();
|
|
|
|
|
|
}
|
|
|
|
|
|
async function loadPriceLockChanges() {
|
|
|
|
|
|
const request = ++priceLockChangesRequest,
|
|
|
|
|
|
user = session.user.id,
|
|
|
|
|
|
root = $('#price-lock-changes-content'),
|
|
|
|
|
|
dialog = $('#price-lock-changes-dialog');
|
|
|
|
|
|
if (!root) return;
|
2026-11-14 01:16:35 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const result = await api('/api/price-lock-changes?status=' + priceLockChangesStatus);
|
|
|
|
|
|
if (
|
|
|
|
|
|
request !== priceLockChangesRequest ||
|
|
|
|
|
|
user !== session.user.id ||
|
|
|
|
|
|
!dialog.open ||
|
|
|
|
|
|
root !== $('#price-lock-changes-content')
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-14 01:16:35 +08:00
|
|
|
|
return;
|
|
|
|
|
|
if (!Array.isArray(result.rows)) throw new Error('锁价申请未完整返回,请重新读取。');
|
|
|
|
|
|
root.innerHTML = result.rows.length
|
|
|
|
|
|
? `<div class="table-wrap"><table class="price-lock-change-list"><thead><tr><th>原锁价单/客户</th><th>申请</th><th>申请人/日期</th><th>状态</th><th>操作</th></tr></thead><tbody>${result.rows.map(row => `<tr><td>${e(row.number || row.before?.number || '锁价单')}<div class="small muted">${e(row.customer || row.before?.customer || '')}</div></td><td>${row.action === 'cancel' ? '录错作废' : '修改整单'}<div class="small muted">${e(row.reason || '')}</div></td><td>${e(row.requester_name || row.requester || '')}<div class="small muted">${e(row.created || '')}</div></td><td>${e({ pending: '待店主复核', approved: '已批准', rejected: '已驳回' }[row.status] || row.status)}</td><td>${button('查看申请', 'price-lock-change-open', '', `data-id="${e(row.requestId || row.id)}"`)}</td></tr>`).join('')}</tbody></table></div>`
|
|
|
|
|
|
: '<p class="empty">暂无符合条件的申请。</p>';
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
if (request === priceLockChangesRequest && root === $('#price-lock-changes-content'))
|
|
|
|
|
|
root.innerHTML = `<div class="error">${e(error.message)} ${button('重新读取', 'price-lock-changes-reload')}</div>`;
|
2026-11-08 02:22:58 +08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 02:22:58 +08:00
|
|
|
|
function priceLockChangeSnapshotHTML(snapshot, title) {
|
|
|
|
|
|
if (!snapshot) return `<div><h3>${e(title)}</h3><p>未提供</p></div>`;
|
|
|
|
|
|
const value = snapshot.lock || snapshot,
|
|
|
|
|
|
rows = value.lines || [],
|
|
|
|
|
|
inputRows = rows[0] && !rows[0].line;
|
|
|
|
|
|
if (!inputRows)
|
|
|
|
|
|
return `<div class="lock-change-snapshot"><h3>${e(title)}</h3><p>${e(value.date || '')} · ${e(value.customer || '')} · 开单人 ${e(value.salesperson || '未提供')}</p>${priceLockItemsHTML(value)}<p>预计成交 ${Number.isSafeInteger(value.estimated_total) ? '¥' + yuan(value.estimated_total) : '未提供'} · 已收净额 ${Number.isSafeInteger(value.netReceived) ? '¥' + yuan(value.netReceived) : '未提供'} · 整单优惠 ${Number.isSafeInteger(value.discount) ? '¥' + yuan(value.discount) : '未提供'}</p>${value.note ? `<p>客户备注:${e(value.note)}</p>` : ''}</div>`;
|
|
|
|
|
|
return `<div class="lock-change-snapshot"><h3>${e(title)}</h3><p>${e(value.date || '')} · ${e(value.customer || '')} · 开单人 ${e(value.salesperson || '未提供')}</p><div class="table-wrap"><table><thead><tr><th>商品</th><th>锁重/售价</th><th>销售工费/附加项目</th><th>原锁料费/付款人</th></tr></thead><tbody>${rows.map(row => `<tr><td>${e(row.name || '未提供')}</td><td>${e(row.quantity || '未提供')}g<br>¥${e(row.goldPrice ?? '未提供')}/克</td><td>${row.laborMode === 'piece' ? '件工费' : '克工费'} ¥${e(row.laborRate ?? '未提供')}${row.laborMode === 'piece' ? ' × ' + e(row.pieces || 1) + '件' : '/克'}${(row.charges || []).map(charge => '<br>' + e(charge.name) + ' ¥' + e(charge.amount)).join('')}</td><td>${row.material?.source === 'pool' ? '当日加权料池' : `手填 ¥${e(row.material?.rate ?? '未提供')}/克<br>${e(priceLockPayerName(row.material?.payer))}`}</td></tr>`).join('')}</tbody></table></div><p>客户实际付款 ¥${e(value.payment?.amount ?? '未提供')} · ${e(accountDisplayName(value.payment?.account || ''))} · 整单优惠 ¥${e(value.discount ?? '未提供')}</p>${value.note ? `<p>客户备注:${e(value.note)}</p>` : ''}</div>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
async function openPriceLockChange(id) {
|
|
|
|
|
|
const request = ++priceLockChangesRequest,
|
|
|
|
|
|
user = session.user.id,
|
|
|
|
|
|
dialog = priceLockChangesDialog();
|
|
|
|
|
|
dialog.innerHTML = `<button class="dialog-close" data-action="price-lock-changes-close" aria-label="关闭">×</button><div id="price-lock-change-loading">正在读取锁价申请…</div>`;
|
|
|
|
|
|
if (!dialog.open) dialog.showModal();
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = await api('/api/price-lock-changes/' + encodeURIComponent(id));
|
|
|
|
|
|
if (request !== priceLockChangesRequest || user !== session.user.id || !dialog.open) return;
|
|
|
|
|
|
if (
|
|
|
|
|
|
(result.requestId || result.id) !== id ||
|
|
|
|
|
|
!['edit', 'cancel'].includes(result.action) ||
|
|
|
|
|
|
!Array.isArray(result.changes) ||
|
|
|
|
|
|
!result.before
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-08 02:22:58 +08:00
|
|
|
|
throw new Error('锁价申请详情未完整返回,请重新读取。');
|
|
|
|
|
|
priceLockChangeEntry = result;
|
|
|
|
|
|
const canReview = session.user.role === 'owner' && result.status === 'pending' && !result.stale;
|
2026-11-24 21:11:32 +08:00
|
|
|
|
dialog.innerHTML = `<button class="dialog-close" data-action="price-lock-changes-close" aria-label="关闭">×</button><div class="eyebrow">PRICE LOCK REVIEW</div><h2>${result.action === 'cancel' ? '录错作废' : '整单修改'}申请</h2><p>${e(result.requester_name || '')} · ${e(result.created || '')} · ${e({ pending: '待复核', approved: '已批准', rejected: '已驳回' }[result.status] || result.status)}</p><p>申请原因:${e(result.reason || '未提供')}</p>${result.stale ? '<div class="error">原单或关联料池已有变动,本次申请不能直接批准。请驳回后重新核实提交。</div>' : ''}${priceLockChangeSnapshotHTML(result.before, '当前原单')}${result.action === 'edit' ? priceLockChangeSnapshotHTML(result.after, '申请修改后') : '<div class="note">批准后整单作废,撤销有效收款、手填购料及信用卡手续费记账,同日释放预留;跨天按原克重原成本回到批准当天料池,历史订单成本不变。保留原单与作废记录,不登记真实退款或实物退料。</div>'}${result.relatedPriceLocks?.length && typeof relatedPriceLocksHTML === 'function' ? relatedPriceLocksHTML(result.relatedPriceLocks) : ''}${result.relatedOrders?.length ? priceLockRelatedOrdersHTML(result.relatedOrders) : ''}${result.note ? `<p>复核备注:${e(result.note)}</p>` : ''}<div class="actions">${button('返回申请列表', 'price-lock-changes-open', 'quiet')}${button('查看原锁价单', 'price-lock-change-lock', '', `data-id="${e(result.price_lock_id)}"`)}</div>${session.user.role === 'owner' && result.status === 'pending' ? `<form id="price-lock-review-form"><label>复核备注<textarea name="review_note" maxlength="240" rows="2" placeholder="驳回时必填"></textarea></label>${canReview ? '<label class="check"><input type="checkbox" name="verified">已逐件核对商品、收款、购料与关联影响,确认批准本次申请。</label>' : ''}<div class="actions">${canReview ? '<button class="primary" name="decision" value="approve" type="submit">批准申请</button>' : ''}<button class="danger" name="decision" value="reject" type="submit">驳回申请</button></div><div id="price-lock-action-error" aria-live="polite"></div></form>` : ''}`;
|
2026-11-08 02:22:58 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
if (request === priceLockChangesRequest && dialog.open)
|
|
|
|
|
|
dialog.innerHTML = `<button class="dialog-close" data-action="price-lock-changes-close" aria-label="关闭">×</button><div class="error">${e(error.message)}</div>${button('重新读取申请', 'price-lock-change-open', '', `data-id="${e(id)}"`)}`;
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 02:22:58 +08:00
|
|
|
|
async function submitPriceLockReview(form, submitter) {
|
|
|
|
|
|
const entry = priceLockChangeEntry,
|
|
|
|
|
|
decision = submitter?.value;
|
|
|
|
|
|
if (
|
|
|
|
|
|
!entry ||
|
|
|
|
|
|
session.user.role !== 'owner' ||
|
|
|
|
|
|
entry.status !== 'pending' ||
|
|
|
|
|
|
!['approve', 'reject'].includes(decision)
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-08 02:22:58 +08:00
|
|
|
|
throw new Error('请重新读取待复核申请。');
|
|
|
|
|
|
const note = lockFormValue(form, 'review_note').trim();
|
|
|
|
|
|
let data;
|
|
|
|
|
|
if (decision === 'approve') {
|
|
|
|
|
|
if (entry.stale) throw new Error('原单或关联料池已有变化,请驳回后重新提交。');
|
|
|
|
|
|
if (!form.querySelector('[name="verified"]')?.checked) throw new Error('请核实全部变更与关联影响并勾选确认。');
|
|
|
|
|
|
data = { revision: entry.price_lock_revision ?? entry.revision, verified: true, ...(note ? { note } : {}) };
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (!note) throw new Error('请填写驳回原因。');
|
|
|
|
|
|
data = { note };
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 02:22:58 +08:00
|
|
|
|
const url = '/api/price-lock-changes/' + encodeURIComponent(entry.requestId || entry.id) + '/' + decision,
|
|
|
|
|
|
result = await mutate(url, data);
|
|
|
|
|
|
await finishPendingPriceLockSave(url, data, { ...result, price_lock_id: entry.price_lock_id });
|
|
|
|
|
|
}
|
|
|
|
|
|
async function priceLockPendingActionClick(action, buttonElement) {
|
|
|
|
|
|
if (action === 'edit-price-lock') return startPriceLockEdit(buttonElement.dataset.id);
|
|
|
|
|
|
if (action === 'cancel-price-lock') return openPriceLockCancel(buttonElement.dataset.id);
|
|
|
|
|
|
if (action === 'leave-lock-edit') return leavePendingPriceLockEdit();
|
|
|
|
|
|
if (action === 'reset-lock-edit') {
|
|
|
|
|
|
if (window.confirm('使用当前锁价资料会替换这份未保存的修改内容,确认重新读取?'))
|
|
|
|
|
|
return startPriceLockEdit(buttonElement.dataset.id, true);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'price-lock-list-status') {
|
|
|
|
|
|
priceLockListStatus = buttonElement.dataset.status;
|
|
|
|
|
|
return renderPriceLocks();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-08 02:22:58 +08:00
|
|
|
|
if (action === 'price-lock-changes-open') return openPriceLockChanges();
|
|
|
|
|
|
if (action === 'price-lock-changes-reload') return loadPriceLockChanges();
|
|
|
|
|
|
if (action === 'price-lock-changes-filter') {
|
|
|
|
|
|
priceLockChangesStatus = buttonElement.dataset.status;
|
|
|
|
|
|
return openPriceLockChanges();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'price-lock-changes-close') {
|
|
|
|
|
|
++priceLockChangesRequest;
|
|
|
|
|
|
$('#price-lock-changes-dialog')?.close();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'price-lock-change-open') return openPriceLockChange(buttonElement.dataset.id);
|
|
|
|
|
|
if (action === 'price-lock-change-lock') {
|
|
|
|
|
|
$('#price-lock-changes-dialog')?.close();
|
|
|
|
|
|
return openPriceLock(buttonElement.dataset.id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|