Files
weiguang-jewelry-ledger/新账系统/public/order-cost-presets-ui.js
T

28 lines
3.5 KiB
JavaScript
Raw Normal View History

'use strict';
// 仅供普通新开单使用。历史费用仍在原费用行中按原金额、付款人显示。
function orderCostPresetsHTML(items){
const selected=new Map(items.map(item=>[FeeEstimates.presetKind(item),item]).filter(([kind])=>kind));
const blocked=new Set(items.filter(item=>!FeeEstimates.presetKind(item)).map(item=>['shipping','box','braiding'].includes(item.key)?item.key:FeeEstimates.checkboxCategory(item.sourceName||item.note)).filter(Boolean));
const disabled=category=>blocked.has(category)?' disabled':'',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)}${disabled(category)}><span><strong>${title}</strong><span class="order-cost-price">${amount}</span><small>${payer}${blocked.has(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||blocked.has('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;
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;
}
function readOrderCostPresets(){
const root=$('#order-cost-presets');if(!root)return [];
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;
}