2026-10-22 04:21:26 +08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
let orderTemplateRows = null,
|
|
|
|
|
|
orderTemplateUser = '',
|
|
|
|
|
|
orderTemplateSelection = '',
|
|
|
|
|
|
orderTemplateRequest = 0,
|
|
|
|
|
|
orderTemplateApplying = false,
|
|
|
|
|
|
orderTemplateEntry = null;
|
|
|
|
|
|
function resetOrderTemplates() {
|
|
|
|
|
|
orderTemplateRows = null;
|
|
|
|
|
|
orderTemplateUser = '';
|
|
|
|
|
|
orderTemplateSelection = '';
|
|
|
|
|
|
++orderTemplateRequest;
|
|
|
|
|
|
closeOrderTemplateDialog();
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderTemplateControls() {
|
|
|
|
|
|
const rows = orderTemplateUser === session.user.id ? orderTemplateRows : null;
|
|
|
|
|
|
return `<div class="order-template-controls"><label>选择模板<select id="order-template-select" ${rows ? '' : 'disabled'}><option value="">${rows ? '选择后自动填入预设' : '正在读取模板…'}</option>${(rows || []).map(row => `<option value="${e(row.id)}" ${row.id === orderTemplateSelection ? 'selected' : ''}>${e(row.name)}</option>`).join('')}</select></label><div class="actions">${button('保存为新模板', 'save-order-template')}${button('更新所选模板', 'update-order-template', 'quiet', orderTemplateSelection ? '' : 'disabled')}${button('删除模板', 'delete-order-template', 'quiet', orderTemplateSelection ? '' : 'disabled')}</div></div>`;
|
|
|
|
|
|
}
|
2026-11-19 02:23:28 +08:00
|
|
|
|
function orderTemplatesPanelHTML() {
|
2026-10-22 04:21:26 +08:00
|
|
|
|
if (orderTemplateUser !== session.user.id) {
|
|
|
|
|
|
orderTemplateRows = null;
|
|
|
|
|
|
orderTemplateSelection = '';
|
|
|
|
|
|
orderTemplateUser = session.user.id;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-12-04 01:43:44 +08:00
|
|
|
|
return `<details class="panel order-template-panel entry-tool" id="order-template-panel"><summary>开单模板<span class="small muted">选择或保存常用模板</span></summary><div class="entry-tool-body"><div id="order-template-controls">${orderTemplateControls()}</div><p class="small muted order-template-hint">用于普通开单,可以保存未填完的表单。客户称呼和内部备注不存入模板,套用时留空;业务日期和费用日期用今天;每日料价随本次订单日期读取。</p><div class="small" id="order-template-status" aria-live="polite"></div></div></details>`;
|
2026-10-22 04:21:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
function orderTemplateStatus(message, error = false) {
|
|
|
|
|
|
const node = $('#order-template-status');
|
|
|
|
|
|
if (node) {
|
|
|
|
|
|
node.textContent = message;
|
|
|
|
|
|
node.className = 'small' + (error ? ' danger-text' : ' muted');
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 04:21:26 +08:00
|
|
|
|
async function loadOrderTemplates() {
|
|
|
|
|
|
const panel = $('#order-template-panel'),
|
|
|
|
|
|
user = session.user.id,
|
|
|
|
|
|
request = ++orderTemplateRequest;
|
|
|
|
|
|
if (!panel) return false;
|
|
|
|
|
|
try {
|
2026-10-25 14:19:24 +08:00
|
|
|
|
const result = await api(
|
|
|
|
|
|
'/api/order-templates' +
|
|
|
|
|
|
(teamMode() && state.user.role === 'owner' ? '?' + new URLSearchParams({ member: orderFormMember() }) : ''),
|
|
|
|
|
|
);
|
|
|
|
|
|
if (!Array.isArray(result.rows)) throw new Error('模板列表未完整返回,请重试。');
|
2026-10-22 04:21:26 +08:00
|
|
|
|
if (request !== orderTemplateRequest || user !== session.user.id || panel !== $('#order-template-panel'))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
orderTemplateRows = result.rows;
|
|
|
|
|
|
orderTemplateUser = user;
|
|
|
|
|
|
if (!result.rows.some(row => row.id === orderTemplateSelection)) orderTemplateSelection = '';
|
|
|
|
|
|
$('#order-template-controls').innerHTML = orderTemplateControls();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (request === orderTemplateRequest && user === session.user.id && panel === $('#order-template-panel')) {
|
|
|
|
|
|
orderTemplateStatus('模板尚未读到:' + (err.name === 'AbortError' ? '读取超时' : err.message), true);
|
|
|
|
|
|
$('#order-template-controls').innerHTML =
|
|
|
|
|
|
orderTemplateControls() + button('重新读取模板', 'reload-order-templates', 'quiet');
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderTemplateHasDraft(draft, today) {
|
2026-11-19 02:23:28 +08:00
|
|
|
|
return (
|
|
|
|
|
|
OrderCopy.hasDraftContent(draft, { today, salesperson: defaultSalesperson() }) ||
|
|
|
|
|
|
!!orderTemplateSelection ||
|
|
|
|
|
|
(draft.fees_date && draft.fees_date !== today) ||
|
|
|
|
|
|
draft.lines.some(
|
|
|
|
|
|
line =>
|
|
|
|
|
|
line.pricingMode === 'manual' ||
|
|
|
|
|
|
(line.laborMode && line.laborMode !== 'gram') ||
|
|
|
|
|
|
(line.pieces != null && String(line.pieces) !== '1') ||
|
|
|
|
|
|
line.costLabor?.mode === 'gram' ||
|
|
|
|
|
|
line.costMaterial?.mode === 'total',
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-11-19 02:23:28 +08:00
|
|
|
|
);
|
2026-10-22 04:21:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
async function applyOrderTemplate(id, select) {
|
|
|
|
|
|
if (busy || orderCopyLoading || orderTemplateApplying) throw new Error('正在处理订单,请稍候再套用模板。');
|
|
|
|
|
|
if (pending()) throw new Error('请先恢复上次未确认的操作,再套用模板。');
|
2026-10-22 17:06:32 +08:00
|
|
|
|
const user = session.user.id,
|
|
|
|
|
|
form = $('#order-form');
|
|
|
|
|
|
if (form?.dataset.orderEdit === 'true') throw new Error('订单修改中不能套用模板,请先保存或取消修改。');
|
|
|
|
|
|
if (!form) throw new Error('请在普通开单页套用模板。');
|
|
|
|
|
|
orderTemplateApplying = true;
|
2026-10-22 04:21:26 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const [template, latest] = await Promise.all([
|
|
|
|
|
|
api('/api/order-templates/' + encodeURIComponent(id)),
|
|
|
|
|
|
api('/api/session'),
|
|
|
|
|
|
]);
|
|
|
|
|
|
if (session.user.id !== user || latest.user?.id !== user) throw new Error('登录账号已变化,请刷新后再套用模板。');
|
|
|
|
|
|
if (form !== $('#order-form') || !select.isConnected || select.value !== id) return;
|
|
|
|
|
|
if (busy || pending()) throw new Error('已有提交正在处理或结果待确认,请恢复原操作后再套用模板。');
|
2026-12-04 01:08:08 +08:00
|
|
|
|
const draft = OrderTemplates.apply(template.preset, {
|
|
|
|
|
|
today: latest.today,
|
|
|
|
|
|
salesperson: defaultSalesperson(),
|
|
|
|
|
|
pocketKeys: [
|
|
|
|
|
|
...new Set([...activePocketKeys(), ...(template.preset.lines || []).map(line => line.pocket).filter(Boolean)]),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
],
|
2026-12-04 01:08:08 +08:00
|
|
|
|
});
|
|
|
|
|
|
draft.salesperson = defaultSalesperson();
|
|
|
|
|
|
if (teamMode()) draft.member = template.member || orderFormMember();
|
2026-10-22 04:21:26 +08:00
|
|
|
|
if (
|
|
|
|
|
|
orderTemplateHasDraft(collectDraft(), latest.today) &&
|
|
|
|
|
|
!window.confirm('当前还有未保存的开单内容。套用模板会替换这份草稿,客户称呼将留空、日期改为今天,是否继续?')
|
2026-12-04 08:58:53 -08:00
|
|
|
|
)
|
2026-10-22 04:21:26 +08:00
|
|
|
|
return;
|
|
|
|
|
|
localStorage.setItem('mm-draft-' + user, JSON.stringify(draft));
|
|
|
|
|
|
editDraft = null;
|
|
|
|
|
|
state.today = latest.today;
|
|
|
|
|
|
orderTemplateSelection = id;
|
|
|
|
|
|
const row = orderTemplateRows?.find(row => row.id === id);
|
|
|
|
|
|
if (row) Object.assign(row, { name: template.name, revision: template.revision, updated: template.updated });
|
|
|
|
|
|
renderCreate();
|
|
|
|
|
|
$('#order-form [name="customer"]')?.focus();
|
|
|
|
|
|
orderTemplateStatus('已套用「' + template.name + '」,请核对本次克重、报价和费用后生成单据。');
|
|
|
|
|
|
toast('模板已填入,生成销售单据后才记账');
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
orderTemplateApplying = false;
|
|
|
|
|
|
if (select.isConnected) select.value = orderTemplateSelection;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 04:21:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
function orderTemplateDialog() {
|
|
|
|
|
|
let dialog = $('#order-template-dialog');
|
|
|
|
|
|
if (!dialog) {
|
|
|
|
|
|
dialog = document.createElement('dialog');
|
|
|
|
|
|
dialog.id = 'order-template-dialog';
|
|
|
|
|
|
dialog.setAttribute('aria-labelledby', 'order-template-title');
|
|
|
|
|
|
dialog.addEventListener('cancel', ev => {
|
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
|
closeOrderTemplateDialog();
|
|
|
|
|
|
});
|
|
|
|
|
|
document.body.append(dialog);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 04:21:26 +08:00
|
|
|
|
return dialog;
|
|
|
|
|
|
}
|
|
|
|
|
|
function closeOrderTemplateDialog() {
|
|
|
|
|
|
const dialog = $('#order-template-dialog');
|
|
|
|
|
|
if (dialog) {
|
|
|
|
|
|
dialog.close();
|
|
|
|
|
|
dialog.replaceChildren();
|
|
|
|
|
|
}
|
|
|
|
|
|
orderTemplateEntry = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
function showOrderTemplateDialog(kind) {
|
|
|
|
|
|
if (busy || pending() || orderTemplateApplying) throw new Error('请先确认当前操作结果,再保存模板。');
|
|
|
|
|
|
const form = $('#order-form');
|
|
|
|
|
|
if (form?.dataset.orderEdit === 'true') throw new Error('订单修改中不能保存开单模板,请先保存或取消修改。');
|
2026-10-22 17:06:32 +08:00
|
|
|
|
if (!form) throw new Error('请在普通开单页保存模板。');
|
2026-10-22 04:21:26 +08:00
|
|
|
|
const row = kind === 'update' ? orderTemplateRows?.find(row => row.id === orderTemplateSelection) : null;
|
|
|
|
|
|
if (kind === 'update' && !row) throw new Error('请先选择需要更新的模板。');
|
|
|
|
|
|
orderTemplateEntry = { kind, id: row?.id, revision: row?.revision, user: session.user.id, form };
|
|
|
|
|
|
const dialog = orderTemplateDialog();
|
|
|
|
|
|
dialog.innerHTML = `<button class="dialog-close" data-action="close-order-template" aria-label="关闭模板">×</button><h2 id="order-template-title">${row ? '更新模板' : '保存为新模板'}</h2><p class="small muted">将当前普通开单表单保存为预设,允许金额和克重留空。客户、内部备注、日期及订单编号不存入模板;本次正在填写的内容会保留。</p><form id="order-template-form">${field('模板名称', 'template_name', row?.name || '', 'text', 'required maxlength="60" placeholder="例如:足金手链常用报价"')}<div class="form-actions"><button class="primary" type="submit">${row ? '保存更新' : '保存模板'}</button>${button('取消', 'close-order-template', 'quiet')}</div><div id="order-template-error" aria-live="polite"></div></form>`;
|
|
|
|
|
|
dialog.showModal();
|
|
|
|
|
|
dialog.querySelector('input').focus();
|
|
|
|
|
|
}
|
|
|
|
|
|
async function finishOrderTemplateSave(url, input, result) {
|
|
|
|
|
|
closeOrderTemplateDialog();
|
|
|
|
|
|
const deleted = url.endsWith('/delete');
|
|
|
|
|
|
orderTemplateSelection = deleted ? '' : result.id;
|
|
|
|
|
|
const loaded = await loadOrderTemplates();
|
|
|
|
|
|
orderTemplateStatus(
|
|
|
|
|
|
(deleted ? '模板已删除' : '已保存模板「' + (result.name || input.name) + '」') +
|
|
|
|
|
|
(loaded ? '。' : ';列表暂未读到,可重新读取,当前表单已保留。'),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
2026-10-22 04:21:26 +08:00
|
|
|
|
toast(deleted ? '模板已删除,当前开单内容保留' : '模板已保存,当前开单内容保留');
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderTemplateError(error) {
|
2026-10-28 18:41:14 +08:00
|
|
|
|
const message = error.message.replace('不要重新开单', '不要重复保存模板');
|
2026-10-22 04:21:26 +08:00
|
|
|
|
toast(message, true);
|
|
|
|
|
|
if ($('#order-template-error'))
|
|
|
|
|
|
$('#order-template-error').innerHTML =
|
|
|
|
|
|
`<div class="error">${e(message)}${[404, 409].includes(error.status) ? ' ' + button('关闭并刷新模板列表', 'reload-order-templates', 'quiet') : ''}</div>`;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
else {
|
2026-10-22 04:21:26 +08:00
|
|
|
|
orderTemplateStatus(message, true);
|
|
|
|
|
|
if ([404, 409].includes(error.status))
|
|
|
|
|
|
$('#order-template-status')?.insertAdjacentHTML(
|
|
|
|
|
|
'beforeend',
|
|
|
|
|
|
' ' + button('刷新模板列表', 'reload-order-templates', 'quiet'),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-22 04:21:26 +08:00
|
|
|
|
async function submitOrderTemplate(form) {
|
|
|
|
|
|
const entry = orderTemplateEntry;
|
|
|
|
|
|
if (!entry || entry.user !== session.user.id || entry.form !== $('#order-form'))
|
|
|
|
|
|
throw new Error('开单表单已变化,请关闭后重新保存模板。');
|
2026-11-08 03:30:58 +08:00
|
|
|
|
updateTotal();
|
|
|
|
|
|
const input = {
|
|
|
|
|
|
...(teamMode() ? { member: orderFormMember() } : {}),
|
|
|
|
|
|
name: form.elements.template_name.value.trim(),
|
|
|
|
|
|
preset: OrderTemplates.fromDraft(collectDraft(), { pocketKeys: activePocketKeys() }),
|
|
|
|
|
|
...(entry.kind === 'update' ? { revision: entry.revision } : {}),
|
|
|
|
|
|
};
|
2026-10-22 04:21:26 +08:00
|
|
|
|
if (!input.name || input.name.length > 60) throw new Error('模板名称请填写1~60字。');
|
|
|
|
|
|
const url = '/api/order-templates' + (entry.kind === 'update' ? '/' + encodeURIComponent(entry.id) + '/update' : '');
|
|
|
|
|
|
$('#order-template-error').innerHTML = '';
|
|
|
|
|
|
const result = await mutate(url, input);
|
|
|
|
|
|
return await finishOrderTemplateSave(url, input, result);
|
|
|
|
|
|
}
|
|
|
|
|
|
async function deleteSelectedOrderTemplate() {
|
|
|
|
|
|
if (busy || pending() || orderTemplateApplying) throw new Error('请先确认当前操作结果,再删除模板。');
|
|
|
|
|
|
const row = orderTemplateRows?.find(row => row.id === orderTemplateSelection);
|
|
|
|
|
|
if (!row) throw new Error('请先选择需要删除的模板。');
|
|
|
|
|
|
if (!window.confirm('删除模板「' + row.name + '」?当前开单内容和已保存订单会保留。')) return;
|
|
|
|
|
|
const url = '/api/order-templates/' + encodeURIComponent(row.id) + '/delete',
|
|
|
|
|
|
input = { revision: row.revision };
|
|
|
|
|
|
const result = await mutate(url, input);
|
|
|
|
|
|
return await finishOrderTemplateSave(url, input, result);
|
|
|
|
|
|
}
|
|
|
|
|
|
document.addEventListener('change', async ev => {
|
|
|
|
|
|
if (ev.target.id !== 'order-template-select') return;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (!ev.target.value) {
|
|
|
|
|
|
orderTemplateSelection = '';
|
|
|
|
|
|
$('#order-template-controls').innerHTML = orderTemplateControls();
|
|
|
|
|
|
orderTemplateStatus('当前开单内容保留。');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
await applyOrderTemplate(ev.target.value, ev.target);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
ev.target.value = orderTemplateSelection;
|
|
|
|
|
|
orderTemplateError(err);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 04:21:26 +08:00
|
|
|
|
});
|
|
|
|
|
|
document.addEventListener('click', async ev => {
|
|
|
|
|
|
const action = ev.target.closest('button')?.dataset.action;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (action === 'save-order-template') showOrderTemplateDialog('new');
|
|
|
|
|
|
if (action === 'update-order-template') showOrderTemplateDialog('update');
|
|
|
|
|
|
if (action === 'close-order-template') closeOrderTemplateDialog();
|
|
|
|
|
|
if (action === 'reload-order-templates') {
|
|
|
|
|
|
closeOrderTemplateDialog();
|
|
|
|
|
|
await loadOrderTemplates();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (action === 'delete-order-template') await deleteSelectedOrderTemplate();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
} catch (err) {
|
2026-10-22 04:21:26 +08:00
|
|
|
|
orderTemplateError(err);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2026-10-22 04:21:26 +08:00
|
|
|
|
document.addEventListener('submit', async ev => {
|
|
|
|
|
|
if (ev.target.id !== 'order-template-form') return;
|
|
|
|
|
|
ev.preventDefault();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
try {
|
2026-10-22 04:21:26 +08:00
|
|
|
|
await submitOrderTemplate(ev.target);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
orderTemplateError(err);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 04:21:26 +08:00
|
|
|
|
});
|