'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 `
常用成本 · 按实际发生勾选

快递二选一,也可不选;${editing?'已有费用保留原支付日期,取消后保存会冲回对应成本。':'取消勾选不记成本。'}

${option('jd','shipping','京东快递',e(config.jd.amount)+'元',payerText('jd'))}${option('yt','shipping','圆通快递',e(config.yt.amount)+'元','订单归属人付款')}
${option('box','box','盒子',e(config.box.amount)+'元/个',payerText('box'))}
${option('steel','braiding','钢绳(店里)',e(config.steel.amount)+'元',payerText('steel'))}${option('silver','silverChain','银链(家里)',e(config.silver.amount)+'元',payerText('silver'))}
`; } 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 `
${row.byQuantity?``:''}
`; }).join(''); return `
常用成本 · 按实际发生勾选

勾选后计入内部成本。快递项目二选一;${editing?'原费用保留付款人和支付日期,取消后保存冲回成本。':'取消勾选不记成本。'}

${cards||'

暂无启用的勾选成本,可在店铺配置中添加。

'}
`; } 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(); } } 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;} items.push(editing&&source?(changed[category]?{...source,...item}:{...source}):item); } return items; }