'use strict'; // 新开单和整单修改共用;修改中的原流水身份、付款人和日期必须保留。 function orderCostConfig() { return FeeEstimates.presets(typeof state === 'undefined' ? undefined : state?.shopSettings?.costPresets); } const orderCostCategories = { jd: 'shipping', yt: 'shipping', box: 'box', steel: 'braiding', silver: 'silverChain' }; 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) { if (items.some(item => item.funding && orderCostFeeCategory(item) === category)) return '已有报销关联费用,请在原费用行核对'; const count = items.filter( item => !FeeEstimates.presetKind(item, { presets: orderCostConfig() }) && orderCostFeeCategory(item) === category, ).length; return count ? '已有' + { shipping: '快递', box: '包装', braiding: '店内钢绳', silverChain: '家里银链' }[category] + '费用' + (count > 1 ? '(' + count + '项)' : '') + ',勾选后替换' + (count > 1 ? '全部' : '') : ''; } function orderCostLegacyRows() { 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, }, })) .filter(({ row }) => !row.dataset.automaticFee); } 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); } function orderCostPresetsHTML(items, { editing = false } = {}) { if (FeeEstimates.customPresets(orderCostConfig())) return configuredCostPresetsHTML(items, { editing }); const selected = new Map( items .map(item => [FeeEstimates.presetKind(item, { recorded: editing, presets: orderCostConfig() }), item]) .filter(([kind]) => kind), ), originals = Object.fromEntries([...selected].map(([kind, item]) => [orderCostCategories[kind], item])); const config = orderCostConfig(), payerText = kind => config[kind].payer === 'order' ? '订单归属人付款' : e(teamMemberName(config[kind].payer)) + '付款'; const checked = kind => (selected.has(kind) ? ' checked' : ''); const box = selected.get('box'), count = box ? box.note.match(/(([^)]*)个 ×/)[1] : '1'; const funded = category => items.some(item => item.funding && orderCostFeeCategory(item) === category); const option = (kind, category, title, amount, payer) => ``; return `
`; } function syncOrderCostPresets(target) { const root = $('#order-cost-presets'); if (!root) return; if (root.dataset?.configured === 'true') return syncConfiguredCosts(root, target); if (target?.checked && ['cost_jd', 'cost_yt'].includes(target.name)) root.querySelector('[name="' + (target.name === 'cost_jd' ? 'cost_yt' : 'cost_jd') + '"]').checked = false; 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); } if ( (target?.checked || (target?.name === 'cost_box_quantity' && root.querySelector('[name="cost_box"]').checked)) && category ) for (const { row, item } of orderCostLegacyRows()) { if (orderCostFeeCategory(item) === category) { if (item.funding) continue; if (!originals[category]) originals[category] = item; row.remove(); } else if (item.key === feeKey) row.dataset.fee = 'manual:' + crypto.randomUUID(); } if (root.dataset) root.dataset.costOriginals = JSON.stringify(originals); 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; syncOrderCostReplacementHints(); } function readOrderCostPresets() { const root = $('#order-cost-presets'); if (!root) return []; if (root.dataset?.configured === 'true') return readConfiguredCosts(root); syncOrderCostReplacementHints(); 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]])) ); }; const config = orderCostConfig(), member = orderFormMember(), quantity = root.querySelector('[name="cost_box_quantity"]').value; const selection = { shipping: checked('jd') ? 'jd' : checked('yt') ? 'yt' : '', box: checked('box'), quantity, steel: checked('steel'), silver: checked('silver'), }; const boxTotal = root.querySelector('[data-cost-box-total]'); let items; 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 = '请填写正整数数量'; } 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; }); const shipping = items.find(item => item.note === '圆通快递'); 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' ? '(订单归属人)' : ''); return items; } // 可配置目录沿用原费用流水键;变更配置后不能匹配的旧费用留在下方逐项核对。 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 `