53 lines
4.9 KiB
JavaScript
53 lines
4.9 KiB
JavaScript
'use strict';
|
||
// 仅供普通新开单使用。历史费用仍在原费用行中按原金额、付款人显示。
|
||
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){
|
||
const count=items.filter(item=>!FeeEstimates.presetKind(item)&&orderCostFeeCategory(item)===category).length;
|
||
return count?'已有'+({shipping:'快递',box:'包装',braiding:'店内钢绳'})[category]+'费用'+(count>1?'('+count+'项)':'')+',勾选后替换'+(count>1?'全部':''):'';
|
||
}
|
||
function orderCostLegacyRows(){
|
||
return [...($('#fee-rows')?.querySelectorAll('.fee-row')||[])].filter(row=>!row.dataset.automaticFee).map(row=>({row,item:{key:row.dataset.fee,sourceName:row.dataset.source,note:row.querySelector('[name="feeNote"]')?.value??row.querySelector('.fee-name')?.textContent}}));
|
||
}
|
||
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){
|
||
const selected=new Map(items.map(item=>[FeeEstimates.presetKind(item),item]).filter(([kind])=>kind));
|
||
const checked=kind=>selected.has(kind)?' checked':'';
|
||
const box=selected.get('box'),count=box?box.note.match(/(([^)]*)个 ×/)[1]:'1';
|
||
const option=(kind,category,title,amount,payer)=>`<label class="order-cost-option"><input type="checkbox" name="cost_${kind}"${checked(kind)}><span><strong>${title}</strong><span class="order-cost-price">${amount}</span><small>${payer}</small><small data-cost-replacement="${category}">${e(orderCostReplacementHint(items,category))}</small></span></label>`;
|
||
return `<fieldset id="order-cost-presets" class="order-cost-presets"><legend>常用成本 · 按实际发生勾选</legend><p class="small muted">快递二选一,也可不选;取消勾选不记成本。</p><div class="order-cost-options">${option('jd','shipping','京东快递','8元','麦麦付款')}${option('yt','shipping','圆通快递','6元','<span data-cost-yt-payer>订单归属人付款</span>')}<div class="order-cost-box">${option('box','box','盒子','3元/个','麦麦付款')}<label class="order-cost-quantity">盒子数量(个)<input type="number" name="cost_box_quantity" min="1" max="333333333" step="1" inputmode="numeric" value="${e(count)}"${!box?' disabled':' required'}><small data-cost-box-total>${box?e(count)+'个 × 3元 = '+e(box.amount)+'元':'勾选后计费'}</small></label></div>${option('steel','braiding','钢绳(店里)','7元','麦麦付款')}</div></fieldset>`;
|
||
}
|
||
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={cost_jd:'shipping',cost_yt:'shipping',cost_box:'box',cost_steel:'braiding'}[target?.name];
|
||
if(target?.checked&&category)for(const {row,item} of orderCostLegacyRows()){
|
||
if(orderCostFeeCategory(item)===category)row.remove();
|
||
else if(item.key===category)row.dataset.fee='manual:'+crypto.randomUUID();
|
||
}
|
||
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 checked=name=>{const input=root.querySelector('[name="cost_'+name+'"]');return input.checked&&!input.disabled;};
|
||
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')};
|
||
root.querySelector('[data-cost-yt-payer]').textContent=teamMemberName(member)+'付款(订单归属人)';
|
||
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='请填写正整数数量';}
|
||
return items;
|
||
}
|