2026-11-19 02:23:28 +08:00
|
|
|
|
'use strict';
|
2026-11-24 21:11:32 +08:00
|
|
|
|
// 新开单和整单修改共用;修改中的原流水身份、付款人和日期必须保留。
|
2026-12-04 00:53:53 +08:00
|
|
|
|
function orderCostConfig() {
|
|
|
|
|
|
return FeeEstimates.presets(typeof state === 'undefined' ? undefined : state?.shopSettings?.costPresets);
|
|
|
|
|
|
}
|
2026-11-24 21:11:32 +08:00
|
|
|
|
const orderCostCategories = { jd: 'shipping', yt: 'shipping', box: 'box', steel: 'braiding', silver: 'silverChain' };
|
2026-11-19 13:24:18 +08:00
|
|
|
|
function orderCostFeeCategory(item) {
|
|
|
|
|
|
if (item.key === 'shipping' || item.key === 'box') return item.key;
|
|
|
|
|
|
// 旧版 braiding 是编绳,只有明确写了钢绳才属于店内钢绳成本。
|
|
|
|
|
|
if (String(item.note || '').trim() === '编绳') return '';
|
|
|
|
|
|
return FeeEstimates.checkboxCategory(item.note) || FeeEstimates.checkboxCategory(item.sourceName);
|
|
|
|
|
|
}
|
|
|
|
|
|
function orderCostReplacementHint(items, category) {
|
2026-11-24 21:11:32 +08:00
|
|
|
|
if (items.some(item => item.funding && orderCostFeeCategory(item) === category))
|
|
|
|
|
|
return '已有报销关联费用,请在原费用行核对';
|
2026-12-04 00:53:53 +08:00
|
|
|
|
const count = items.filter(
|
|
|
|
|
|
item => !FeeEstimates.presetKind(item, { presets: orderCostConfig() }) && orderCostFeeCategory(item) === category,
|
|
|
|
|
|
).length;
|
2026-11-19 13:41:58 +08:00
|
|
|
|
return count
|
|
|
|
|
|
? '已有' +
|
|
|
|
|
|
{ shipping: '快递', box: '包装', braiding: '店内钢绳', silverChain: '家里银链' }[category] +
|
|
|
|
|
|
'费用' +
|
|
|
|
|
|
(count > 1 ? '(' + count + '项)' : '') +
|
|
|
|
|
|
',勾选后替换' +
|
|
|
|
|
|
(count > 1 ? '全部' : '')
|
|
|
|
|
|
: '';
|
2026-11-19 13:24:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
function orderCostLegacyRows() {
|
2026-11-24 21:11:32 +08:00
|
|
|
|
const root = $('#fee-rows'),
|
|
|
|
|
|
rows = [...(root?.querySelectorAll('.fee-row') || [])],
|
|
|
|
|
|
items = root && typeof readFeeRows === 'function' ? readFeeRows(root, false) : null;
|
|
|
|
|
|
return rows
|
|
|
|
|
|
.map((row, index) => ({
|
|
|
|
|
|
row,
|
|
|
|
|
|
item: items?.[index] || {
|
|
|
|
|
|
key: row.dataset.fee,
|
|
|
|
|
|
sourceName: row.dataset.source,
|
|
|
|
|
|
note: row.querySelector('[name="feeNote"]')?.value ?? row.querySelector('.fee-name')?.textContent,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
},
|
2026-11-24 21:11:32 +08:00
|
|
|
|
}))
|
|
|
|
|
|
.filter(({ row }) => !row.dataset.automaticFee);
|
2026-11-19 13:24:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
function syncOrderCostReplacementHints() {
|
|
|
|
|
|
const root = $('#order-cost-presets');
|
|
|
|
|
|
if (!root) return;
|
|
|
|
|
|
const items = orderCostLegacyRows().map(value => value.item);
|
|
|
|
|
|
for (const hint of root.querySelectorAll('[data-cost-replacement]'))
|
|
|
|
|
|
hint.textContent = orderCostReplacementHint(items, hint.dataset.costReplacement);
|
|
|
|
|
|
}
|
2026-11-24 21:11:32 +08:00
|
|
|
|
function orderCostPresetsHTML(items, { editing = false } = {}) {
|
2026-12-04 01:12:41 +08:00
|
|
|
|
if (FeeEstimates.customPresets(orderCostConfig())) return configuredCostPresetsHTML(items, { editing });
|
2026-12-04 00:53:53 +08:00
|
|
|
|
const selected = new Map(
|
|
|
|
|
|
items
|
|
|
|
|
|
.map(item => [FeeEstimates.presetKind(item, { recorded: editing, presets: orderCostConfig() }), item])
|
|
|
|
|
|
.filter(([kind]) => kind),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
),
|
2026-12-04 00:53:53 +08:00
|
|
|
|
originals = Object.fromEntries([...selected].map(([kind, item]) => [orderCostCategories[kind], item]));
|
|
|
|
|
|
const config = orderCostConfig(),
|
|
|
|
|
|
payerText = kind =>
|
|
|
|
|
|
config[kind].payer === 'order' ? '订单归属人付款' : e(teamMemberName(config[kind].payer)) + '付款';
|
2026-11-19 13:24:18 +08:00
|
|
|
|
const checked = kind => (selected.has(kind) ? ' checked' : '');
|
2026-11-19 02:23:28 +08:00
|
|
|
|
const box = selected.get('box'),
|
|
|
|
|
|
count = box ? box.note.match(/(([^)]*)个 ×/)[1] : '1';
|
2026-11-24 21:11:32 +08:00
|
|
|
|
const funded = category => items.some(item => item.funding && orderCostFeeCategory(item) === category);
|
|
|
|
|
|
const option = (kind, category, title, amount, payer) =>
|
|
|
|
|
|
`<label class="order-cost-option"><input type="checkbox" name="cost_${kind}"${checked(kind)}${funded(category) ? ' disabled' : ''}><span><strong>${title}</strong><span class="order-cost-price">${amount}</span><small>${payer}</small>${editing && originals[category]?.date ? `<small>原支付日期 ${e(originals[category].date)}</small>` : ''}<small data-cost-replacement="${category}">${e(orderCostReplacementHint(items, category))}</small></span></label>`;
|
2026-12-04 00:53:53 +08:00
|
|
|
|
return `<fieldset id="order-cost-presets" class="order-cost-presets" data-cost-editing="${editing}" data-cost-originals="${e(JSON.stringify(originals))}"><legend>常用成本 · 按实际发生勾选</legend><p class="small muted">快递二选一,也可不选;${editing ? '已有费用保留原支付日期,取消后保存会冲回对应成本。' : '取消勾选不记成本。'}</p><div class="order-cost-options">${option('jd', 'shipping', '京东快递', e(config.jd.amount) + '元', payerText('jd'))}${option('yt', 'shipping', '圆通快递', e(config.yt.amount) + '元', '<span data-cost-yt-payer>订单归属人付款</span>')}<div class="order-cost-box">${option('box', 'box', '盒子', e(config.box.amount) + '元/个', payerText('box'))}<label class="order-cost-quantity">盒子数量(个)<input type="number" name="cost_box_quantity" min="1" max="333333333" step="1" inputmode="numeric" value="${e(count)}"${!box || funded('box') ? ' disabled' : ' required'}><small data-cost-box-total>${box ? e(count) + '个 × ' + e(config.box.amount) + '元 = ' + e(box.amount) + '元' : '勾选后计费'}</small></label></div>${option('steel', 'braiding', '钢绳(店里)', e(config.steel.amount) + '元', payerText('steel'))}${option('silver', 'silverChain', '银链(家里)', e(config.silver.amount) + '元', payerText('silver'))}</div></fieldset>`;
|
2026-11-19 02:23:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
function syncOrderCostPresets(target) {
|
|
|
|
|
|
const root = $('#order-cost-presets');
|
|
|
|
|
|
if (!root) return;
|
2026-12-04 01:12:41 +08:00
|
|
|
|
if (root.dataset?.configured === 'true') return syncConfiguredCosts(root, target);
|
2026-11-19 02:23:28 +08:00
|
|
|
|
if (target?.checked && ['cost_jd', 'cost_yt'].includes(target.name))
|
|
|
|
|
|
root.querySelector('[name="' + (target.name === 'cost_jd' ? 'cost_yt' : 'cost_jd') + '"]').checked = false;
|
2026-11-24 21:11:32 +08:00
|
|
|
|
const category =
|
|
|
|
|
|
target?.name === 'cost_box_quantity'
|
|
|
|
|
|
? 'box'
|
|
|
|
|
|
: orderCostCategories[String(target?.name || '').replace(/^cost_/, '')],
|
|
|
|
|
|
feeKey = category === 'silverChain' ? 'other' : category;
|
|
|
|
|
|
const originals = JSON.parse(root.dataset?.costOriginals || '{}'),
|
|
|
|
|
|
changed = JSON.parse(root.dataset?.costChanged || '{}');
|
|
|
|
|
|
if (category) {
|
|
|
|
|
|
changed[category] = true;
|
|
|
|
|
|
root.dataset ??= {};
|
|
|
|
|
|
root.dataset.costChanged = JSON.stringify(changed);
|
2026-11-19 13:24:18 +08:00
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
if (
|
2026-11-24 21:11:32 +08:00
|
|
|
|
(target?.checked || (target?.name === 'cost_box_quantity' && root.querySelector('[name="cost_box"]').checked)) &&
|
2026-12-04 08:58:53 -08:00
|
|
|
|
category
|
|
|
|
|
|
)
|
2026-12-04 01:12:41 +08:00
|
|
|
|
for (const { row, item } of orderCostLegacyRows()) {
|
2026-11-24 21:11:32 +08:00
|
|
|
|
if (orderCostFeeCategory(item) === category) {
|
2026-12-04 01:12:41 +08:00
|
|
|
|
if (item.funding) continue;
|
|
|
|
|
|
if (!originals[category]) originals[category] = item;
|
|
|
|
|
|
row.remove();
|
2026-11-19 13:41:58 +08:00
|
|
|
|
} else if (item.key === feeKey) row.dataset.fee = 'manual:' + crypto.randomUUID();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-11-24 21:11:32 +08:00
|
|
|
|
if (root.dataset) root.dataset.costOriginals = JSON.stringify(originals);
|
2026-11-19 02:23:28 +08:00
|
|
|
|
root.querySelector('[name="cost_box_quantity"]').disabled =
|
|
|
|
|
|
!root.querySelector('[name="cost_box"]').checked || root.querySelector('[name="cost_box"]').disabled;
|
|
|
|
|
|
root.querySelector('[name="cost_box_quantity"]').required =
|
|
|
|
|
|
!root.querySelector('[name="cost_box_quantity"]').disabled;
|
2026-11-19 13:24:18 +08:00
|
|
|
|
syncOrderCostReplacementHints();
|
2026-11-19 02:23:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
function readOrderCostPresets() {
|
|
|
|
|
|
const root = $('#order-cost-presets');
|
|
|
|
|
|
if (!root) return [];
|
2026-12-04 01:12:41 +08:00
|
|
|
|
if (root.dataset?.configured === 'true') return readConfiguredCosts(root);
|
2026-11-19 13:24:18 +08:00
|
|
|
|
syncOrderCostReplacementHints();
|
2026-11-24 21:11:32 +08:00
|
|
|
|
const originals = JSON.parse(root.dataset?.costOriginals || '{}'),
|
|
|
|
|
|
changed = JSON.parse(root.dataset?.costChanged || '{}'),
|
|
|
|
|
|
editing = root.dataset?.costEditing === 'true';
|
|
|
|
|
|
const checked = name => {
|
|
|
|
|
|
const input = root.querySelector('[name="cost_' + name + '"]');
|
|
|
|
|
|
return (
|
|
|
|
|
|
input.checked &&
|
|
|
|
|
|
(!input.disabled || (editing && originals[orderCostCategories[name]] && !changed[orderCostCategories[name]]))
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
2026-12-04 00:53:53 +08:00
|
|
|
|
const config = orderCostConfig(),
|
|
|
|
|
|
member = orderFormMember(),
|
|
|
|
|
|
quantity = root.querySelector('[name="cost_box_quantity"]').value;
|
2026-11-19 13:41:58 +08:00
|
|
|
|
const selection = {
|
|
|
|
|
|
shipping: checked('jd') ? 'jd' : checked('yt') ? 'yt' : '',
|
|
|
|
|
|
box: checked('box'),
|
|
|
|
|
|
quantity,
|
|
|
|
|
|
steel: checked('steel'),
|
|
|
|
|
|
silver: checked('silver'),
|
|
|
|
|
|
};
|
2026-11-19 02:23:28 +08:00
|
|
|
|
const boxTotal = root.querySelector('[data-cost-box-total]');
|
|
|
|
|
|
let items;
|
2026-12-04 00:53:53 +08:00
|
|
|
|
try {
|
|
|
|
|
|
items = FeeEstimates.checkboxFees(selection, member, config);
|
|
|
|
|
|
boxTotal.textContent = selection.box
|
|
|
|
|
|
? FeeEstimates.boxCount(quantity) +
|
|
|
|
|
|
'个 × ' +
|
|
|
|
|
|
config.box.amount +
|
|
|
|
|
|
'元 = ' +
|
|
|
|
|
|
items.find(item => item.key === 'box').amount +
|
|
|
|
|
|
'元'
|
|
|
|
|
|
: '勾选后计费';
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
items = FeeEstimates.checkboxFees({ ...selection, box: false }, member, config);
|
|
|
|
|
|
items.push({
|
|
|
|
|
|
key: 'box',
|
|
|
|
|
|
note: `盒子(${quantity}个 × ${config.box.amount}元/个)`,
|
|
|
|
|
|
amount: '',
|
|
|
|
|
|
payer: config.box.payer === 'order' ? member : config.box.payer,
|
|
|
|
|
|
status: 'estimated',
|
|
|
|
|
|
edited: true,
|
|
|
|
|
|
});
|
|
|
|
|
|
boxTotal.textContent = '请填写正整数数量';
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-12-04 00:53:53 +08:00
|
|
|
|
if (editing)
|
|
|
|
|
|
items = items.map(item => {
|
|
|
|
|
|
const category = orderCostCategories[FeeEstimates.presetKind(item, { presets: orderCostConfig() })] || 'box',
|
|
|
|
|
|
source = originals[category];
|
|
|
|
|
|
return source ? (changed[category] ? { ...source, ...item } : { ...source }) : item;
|
|
|
|
|
|
});
|
2026-11-24 21:11:32 +08:00
|
|
|
|
const shipping = items.find(item => item.note === '圆通快递');
|
2026-12-04 00:53:53 +08:00
|
|
|
|
root.querySelector('[data-cost-yt-payer]').textContent =
|
|
|
|
|
|
teamMemberName(shipping?.payer || (config.yt.payer === 'order' ? member : config.yt.payer)) +
|
|
|
|
|
|
'付款' +
|
|
|
|
|
|
(shipping && editing && !changed.shipping ? '(原费用)' : config.yt.payer === 'order' ? '(订单归属人)' : '');
|
2026-11-19 02:23:28 +08:00
|
|
|
|
return items;
|
|
|
|
|
|
}
|
2026-12-04 01:12:41 +08:00
|
|
|
|
|
|
|
|
|
|
// 可配置目录沿用原费用流水键;变更配置后不能匹配的旧费用留在下方逐项核对。
|
|
|
|
|
|
function configuredCostCategory(kind) {
|
|
|
|
|
|
return orderCostCategories[kind] || kind;
|
|
|
|
|
|
}
|
|
|
|
|
|
function configuredCostItemCategory(item) {
|
|
|
|
|
|
if (String(item.key).startsWith('manual:')) {
|
|
|
|
|
|
const kind = 'custom_' + item.key.slice(7);
|
|
|
|
|
|
if (orderCostConfig()[kind]) return kind;
|
|
|
|
|
|
}
|
|
|
|
|
|
return orderCostFeeCategory(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
function configuredCostPresetsHTML(items, { editing = false } = {}) {
|
|
|
|
|
|
const config = FeeEstimates.presetCatalog(orderCostConfig()),
|
|
|
|
|
|
selected = new Map(
|
|
|
|
|
|
items
|
|
|
|
|
|
.map(item => [FeeEstimates.presetKind(item, { recorded: editing, presets: config }), item])
|
|
|
|
|
|
.filter(([kind]) => kind),
|
|
|
|
|
|
);
|
|
|
|
|
|
const originals = Object.fromEntries([...selected].map(([kind, item]) => [configuredCostCategory(kind), item]));
|
|
|
|
|
|
const cards = Object.entries(config)
|
|
|
|
|
|
.filter(([, row]) => row.enabled)
|
|
|
|
|
|
.map(([kind, row]) => {
|
|
|
|
|
|
const source = selected.get(kind),
|
|
|
|
|
|
category = configuredCostCategory(kind),
|
|
|
|
|
|
funded = items.some(item => item.funding && configuredCostItemCategory(item) === category),
|
|
|
|
|
|
count = row.byQuantity && source ? source.note.match(/(([^)]*)个 ×/)[1] : '1';
|
|
|
|
|
|
const old = items.filter(
|
|
|
|
|
|
item =>
|
|
|
|
|
|
!FeeEstimates.presetKind(item, { recorded: editing, presets: config }) &&
|
|
|
|
|
|
configuredCostItemCategory(item) === category,
|
|
|
|
|
|
).length;
|
|
|
|
|
|
return `<div class="order-cost-box"><label class="order-cost-option"><input type="checkbox" name="cost_${e(kind)}" data-configured-cost="${e(kind)}"${source ? ' checked' : ''}${funded ? ' disabled' : ''}><span><strong>${e(row.name)}</strong><span class="order-cost-price">${e(row.amount)}元${row.byQuantity ? '/个' : ''}</span><small>${row.payer === 'order' ? '订单归属人' : e(teamMemberName(row.payer))}付款</small>${editing && source?.date ? `<small>原支付日期 ${e(source.date)}</small>` : ''}${funded ? '<small>已有报销关联费用,请在原费用行核对</small>' : old ? '<small>已有同类费用,勾选后替换;请核对下方原记录</small>' : ''}</span></label>${row.byQuantity ? `<label class="order-cost-quantity">数量(个)<input type="number" name="cost_${e(kind)}_quantity" data-configured-quantity="${e(kind)}" min="1" max="333333333" step="1" inputmode="numeric" value="${e(count)}"${source && !funded ? ' required' : ' disabled'}><small data-configured-total="${e(kind)}">勾选后计费</small></label>` : ''}</div>`;
|
|
|
|
|
|
})
|
|
|
|
|
|
.join('');
|
|
|
|
|
|
return `<fieldset id="order-cost-presets" class="order-cost-presets" data-configured="true" data-cost-editing="${editing}" data-cost-originals="${e(JSON.stringify(originals))}"><legend>常用成本 · 按实际发生勾选</legend><p class="small muted">勾选后计入内部成本。快递项目二选一;${editing ? '原费用保留付款人和支付日期,取消后保存冲回成本。' : '取消勾选不记成本。'}</p><div class="order-cost-options">${cards || '<p>暂无启用的勾选成本,可在店铺配置中添加。</p>'}</div></fieldset>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function syncConfiguredCosts(root, target) {
|
|
|
|
|
|
const config = FeeEstimates.presetCatalog(orderCostConfig()),
|
|
|
|
|
|
kind = target?.dataset?.configuredCost || target?.dataset?.configuredQuantity,
|
|
|
|
|
|
category = configuredCostCategory(kind);
|
|
|
|
|
|
const originals = JSON.parse(root.dataset.costOriginals || '{}'),
|
|
|
|
|
|
changed = JSON.parse(root.dataset.costChanged || '{}');
|
|
|
|
|
|
if (kind && config[kind]) {
|
|
|
|
|
|
changed[category] = true;
|
|
|
|
|
|
const checked = root.querySelector(`[name="cost_${kind}"]`)?.checked;
|
|
|
|
|
|
if (checked && category === 'shipping')
|
|
|
|
|
|
for (const other of ['jd', 'yt'])
|
|
|
|
|
|
if (other !== kind) {
|
|
|
|
|
|
const checkbox = root.querySelector(`[name="cost_${other}"]`);
|
|
|
|
|
|
if (checkbox) checkbox.checked = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (checked)
|
|
|
|
|
|
for (const { row, item } of orderCostLegacyRows()) {
|
|
|
|
|
|
if (configuredCostItemCategory(item) === category) {
|
|
|
|
|
|
if (item.funding) continue;
|
|
|
|
|
|
if (!originals[category]) originals[category] = item;
|
|
|
|
|
|
row.remove();
|
|
|
|
|
|
} else if (item.key === FeeEstimates.presetKey(kind)) row.dataset.fee = 'manual:' + crypto.randomUUID();
|
|
|
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-12-04 01:12:41 +08:00
|
|
|
|
root.dataset.costOriginals = JSON.stringify(originals);
|
|
|
|
|
|
root.dataset.costChanged = JSON.stringify(changed);
|
|
|
|
|
|
for (const input of root.querySelectorAll('[data-configured-quantity]')) {
|
|
|
|
|
|
const checkbox = root.querySelector(`[name="cost_${input.dataset.configuredQuantity}"]`);
|
|
|
|
|
|
input.disabled = !checkbox?.checked || checkbox.disabled;
|
|
|
|
|
|
input.required = !input.disabled;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
function readConfiguredCosts(root) {
|
|
|
|
|
|
const config = FeeEstimates.presetCatalog(orderCostConfig()),
|
|
|
|
|
|
originals = JSON.parse(root.dataset.costOriginals || '{}'),
|
|
|
|
|
|
changed = JSON.parse(root.dataset.costChanged || '{}'),
|
|
|
|
|
|
editing = root.dataset.costEditing === 'true',
|
|
|
|
|
|
member = orderFormMember(),
|
|
|
|
|
|
items = [];
|
|
|
|
|
|
for (const [kind, row] of Object.entries(config)) {
|
|
|
|
|
|
const checkbox = root.querySelector(`[name="cost_${kind}"]`),
|
|
|
|
|
|
total = root.querySelector(`[data-configured-total="${kind}"]`),
|
|
|
|
|
|
category = configuredCostCategory(kind),
|
|
|
|
|
|
source = originals[category];
|
|
|
|
|
|
if (!row.enabled || !checkbox?.checked || (checkbox.disabled && !(editing && source && !changed[category]))) {
|
|
|
|
|
|
if (total) total.textContent = '勾选后计费';
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
const quantity = root.querySelector(`[name="cost_${kind}_quantity"]`)?.value ?? '1';
|
|
|
|
|
|
let item;
|
|
|
|
|
|
try {
|
|
|
|
|
|
item = FeeEstimates.presetFee(kind, quantity, member, config);
|
|
|
|
|
|
if (total) total.textContent = `${quantity}个 × ${row.amount}元 = ${item.amount}元`;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
item = {
|
|
|
|
|
|
key: FeeEstimates.presetKey(kind),
|
|
|
|
|
|
note: row.name,
|
|
|
|
|
|
amount: '',
|
|
|
|
|
|
payer: row.payer === 'order' ? member : row.payer,
|
|
|
|
|
|
status: 'estimated',
|
|
|
|
|
|
edited: true,
|
|
|
|
|
|
};
|
|
|
|
|
|
if (total) total.textContent = error.message;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-12-04 01:12:41 +08:00
|
|
|
|
items.push(editing && source ? (changed[category] ? { ...source, ...item } : { ...source }) : item);
|
|
|
|
|
|
}
|
|
|
|
|
|
return items;
|
|
|
|
|
|
}
|