'use strict'; // 新开单和整单修改共用;修改中的原流水身份、付款人和日期必须保留。 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)&&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}={}){ const selected=new Map(items.map(item=>[FeeEstimates.presetKind(item,{recorded:editing}),item]).filter(([kind])=>kind)),originals=Object.fromEntries([...selected].map(([kind,item])=>[orderCostCategories[kind],item])); 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','京东快递','8元','麦麦付款')}${option('yt','shipping','圆通快递','6元','订单归属人付款')}
${option('box','box','盒子','3元/个','麦麦付款')}
${option('steel','braiding','钢绳(店里)','7元','麦麦付款')}${option('silver','silverChain','银链(家里)','18元','麦麦付款')}
`; } function syncOrderCostPresets(target){ const root=$('#order-cost-presets');if(!root)return; 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 []; 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 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);boxTotal.textContent=selection.box?FeeEstimates.boxCount(quantity)+'个 × 3元 = '+FeeEstimates.boxCount(quantity)*3+'元':'勾选后计费';} catch{items=FeeEstimates.checkboxFees({...selection,box:false},member);items.push({key:'box',note:`盒子(${quantity}个 × 3元/个)`,amount:'',payer:'owner',status:'estimated',edited:true});boxTotal.textContent='请填写正整数数量';} if(editing)items=items.map(item=>{const category=orderCostCategories[FeeEstimates.presetKind(item)]||'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||member)+'付款'+(shipping&&editing&&!changed.shipping?'(原费用)':'(订单归属人)'); return items; }