常用成本增加银链家里18元由麦麦付款

This commit is contained in:
summer committed 2026-11-19 13:41:58 +08:00
1 parent 294c430884
commit 5777036743
3 files changed
+45 -20

No files matched your search

+35 -14
View File
@@ -9,11 +9,11 @@ const line=(extra={})=>({name:'纯银925',unit:'件',quantity:'1',pocket:'',paye
const input=(fee_items=[],extra={})=>({date,customer:'合成客户',salesperson:'合成开单人',payment_account:'麦麦微信',discount:'0',rounding_mode:'yuan',fee_cost_mode:'checkboxes',lines:[line()],fee_items,...extra});
function fixture(t){const ledger=new Ledger(':memory:');t.after(()=>ledger.close());const run=(data,fn,key=randomUUID())=>ledger.transact(key,actor,data,fn);const issue=(data,key=randomUUID())=>run(data,()=>ledger.issueOrder(data,actor),key);return {ledger,run,issue};}
test('36种选择与付款人组合固定8/6/3/7,两个盒子6元,不读取店铺旧默认',()=>{
for(const member of ['owner','assistant','assistant2'])for(const shipping of ['','jd','yt'])for(const box of [false,true])for(const steel of [false,true]){
const selected={shipping,box,quantity:'2',steel},fees=Fees.checkboxFees(selected,member),expected=(shipping==='jd'?8:shipping==='yt'?6:0)+(box?6:0)+(steel?7:0);
test('72种选择与付款人组合固定8/6/3/7/18,两个盒子6元,不读取店铺旧默认',()=>{
for(const member of ['owner','assistant','assistant2'])for(const shipping of ['','jd','yt'])for(const box of [false,true])for(const steel of [false,true])for(const silver of [false,true]){
const selected={shipping,box,quantity:'2',steel,silver},fees=Fees.checkboxFees(selected,member),expected=(shipping==='jd'?8:shipping==='yt'?6:0)+(box?6:0)+(steel?7:0)+(silver?18:0);
assert.equal(fees.reduce((total,row)=>total+Calc.cents(row.amount),0),expected*100);
assert.equal(fees.length,Number(!!shipping)+Number(box)+Number(steel));
assert.equal(fees.length,Number(!!shipping)+Number(box)+Number(steel)+Number(silver));
for(const row of fees)assert.equal(row.payer,shipping==='yt'&&row.key==='shipping'?member:'owner');
const sync=Fees.sync(fees,[line()],{checkboxCosts:true,addShipping:true,shopDefaults:{shipping:'99',packaging:'77',packagingPayer:'assistant2'}});
assert.deepEqual(sync,fees);assert.equal(Fees.sync([],[line()],{checkboxCosts:true,addShipping:true}).length,0);
@@ -21,8 +21,8 @@ test('36种选择与付款人组合固定8/6/3/7,两个盒子6元,不读取
for(const quantity of ['','0','-1','1.2','1e2','333333334'])assert.throws(()=>Fees.checkboxFees({box:true,quantity}),/正整数/);
});
test('客户收费保留原金额,未选及取消后不会重建三类成本;编绳和证书仍须核实',()=>{
const lines=[line({charges:[...charges(),{id:randomUUID(),name:'证书',amount:'10'},{id:randomUUID(),name:'编绳',amount:'55'}]})],before=JSON.stringify(lines);
test('客户收费保留原金额,未选及取消后不会重建勾选成本;编绳和证书仍须核实',()=>{
const lines=[line({charges:[...charges(),{id:randomUUID(),name:'银链(家里)',amount:'30'},{id:randomUUID(),name:'证书',amount:'10'},{id:randomUUID(),name:'编绳',amount:'55'}]})],before=JSON.stringify(lines);
const items=Fees.sync([],lines,{checkboxCosts:true});
assert.deepEqual(items.map(row=>[row.note,row.amount]),[['证书','4'],['编绳','']]);assert.equal(JSON.stringify(lines),before);
const old=[{key:'shipping',note:'快递费',amount:'11.23',payer:'assistant2',edited:true},{key:'charge:'+lines[0].charges[0].id,note:'包装',sourceName:'包装',amount:'4.50',payer:'assistant',edited:true}];
@@ -32,15 +32,16 @@ test('客户收费保留原金额,未选及取消后不会重建三类成本
test('新模式所有组合只入账一次,客户单不含勾选成本、付款人或模式',t=>{
const {ledger,issue}=fixture(t);
for(const member of ['owner','assistant','assistant2'])for(const shipping of ['','jd','yt'])for(const box of [false,true])for(const steel of [false,true]){
const fees=Fees.checkboxFees({shipping,box,quantity:'2',steel},member),data=input(fees),key=randomUUID(),first=issue(data,key);assert.deepEqual(issue(data,key),first);
for(const member of ['owner','assistant','assistant2'])for(const shipping of ['','jd','yt'])for(const box of [false,true])for(const steel of [false,true])for(const silver of [false,true]){
const fees=Fees.checkboxFees({shipping,box,quantity:'2',steel,silver},member),data=input(fees),key=randomUUID(),first=issue(data,key);assert.deepEqual(issue(data,key),first);
const order=ledger.detail(first.id),expected=fees.reduce((total,row)=>total+Calc.cents(row.amount),0);
assert.equal(order.fee_cost_mode,'checkboxes');assert.equal(order.total,13300);assert.equal(order.directCost,expected);assert.equal(order.profit,9300-expected);
assert.equal(order.expenses.filter(row=>row.amount>0).length,fees.length);assert.equal(order.expenses.reduce((total,row)=>total+row.amount,0),expected);assert.equal(order.fee_items.length,fees.length);
if(box)assert.ok(order.expenses.some(row=>row.note==='盒子(2个 × 3元/个)'&&row.amount===600&&row.payer==='owner'));
if(silver)assert.ok(order.expenses.some(row=>row.note==='银链(家里)'&&row.amount===1800&&row.payer==='owner'));
if(shipping==='yt')assert.equal(order.expenses.find(row=>row.note==='圆通快递').payer,member);
const publicOrder=ledger.publicOrder(first.id),json=JSON.stringify(publicOrder),html=receipt(publicOrder);
for(const marker of ['fee_items','fee_cost_mode','payer','盒子(2个','京东快递','圆通快递']){assert.ok(!json.includes(marker));assert.ok(!html.includes(marker));}
for(const marker of ['fee_items','fee_cost_mode','payer','盒子(2个','京东快递','圆通快递','银链(家里)']){assert.ok(!json.includes(marker));assert.ok(!html.includes(marker));}
}
});
@@ -52,7 +53,7 @@ test('新模式仍拒绝遗漏编绳和证书费用,不把未知成本当0',t=
test('新模式再打开编辑保留盒数单价和实际费用,无选择的收费项目不补造成本',t=>{
const {ledger,run,issue}=fixture(t);
for(const fees of [[],Fees.checkboxFees({shipping:'yt',box:true,quantity:'2',steel:true},'assistant2')]){
for(const fees of [[],Fees.checkboxFees({shipping:'yt',box:true,quantity:'2',steel:true,silver:true},'assistant2')]){
const {id}=issue(input(fees)),original=ledger.detail(id),before=ledger.rows('SELECT * FROM expenses WHERE order_id=?',id),info=ledger.orderEdit(id,actor);
assert.equal(info.inputs.fee_cost_mode,'checkboxes');assert.equal(info.inputs.fee_items.length,fees.length);
const synced=Fees.sync(info.inputs.fee_items,info.inputs.lines,{checkboxCosts:true});assert.deepEqual(synced,info.inputs.fee_items);
@@ -74,7 +75,7 @@ test('旧模式不自动切换,不套新费率,仍按原费用规则并完
});
test('模板反复保存套用保留勾选、盒数及旧值,取消后不因客户包装/快递收费重建',()=>{
const fees=Fees.checkboxFees({shipping:'yt',box:true,quantity:'2',steel:true},'assistant2');
const fees=Fees.checkboxFees({shipping:'yt',box:true,quantity:'2',steel:true,silver:true},'assistant2');
let draft=input(fees);
for(let i=0;i<3;i++){
const preset=Templates.fromDraft(draft);draft=Templates.apply(preset,{today:date});
@@ -88,8 +89,8 @@ test('模板反复保存套用保留勾选、盒数及旧值,取消后不因
});
function ui(items=[]){
const fields=Object.fromEntries(['jd','yt','box','steel'].map(name=>['cost_'+name,{name:'cost_'+name,checked:false,disabled:false}]));fields.cost_box_quantity={name:'cost_box_quantity',value:'1',disabled:true};
const hints=['shipping','shipping','box','braiding'].map(category=>({dataset:{costReplacement:category},textContent:''}));
const fields=Object.fromEntries(['jd','yt','box','steel','silver'].map(name=>['cost_'+name,{name:'cost_'+name,checked:false,disabled:false}]));fields.cost_box_quantity={name:'cost_box_quantity',value:'1',disabled:true};
const hints=['shipping','shipping','box','braiding','silverChain'].map(category=>({dataset:{costReplacement:category},textContent:''}));
const legacy=items.filter(item=>!Fees.presetKind(item)).map(item=>({item:{...item},dataset:{fee:item.key,source:item.sourceName||''},removed:false,remove(){this.removed=true;},querySelector(selector){return selector==='.fee-name'?{textContent:this.item.note}:null;}}));
for(const item of items){const kind=Fees.presetKind(item);if(kind){fields['cost_'+kind].checked=true;if(kind==='box'){fields.cost_box_quantity.value=item.note.match(/(([^)]*)个 ×/)[1];fields.cost_box_quantity.disabled=false;}}}
const payer={textContent:''},total={textContent:''},root={querySelectorAll:()=>hints,querySelector(selector){if(selector==='[data-cost-yt-payer]')return payer;if(selector==='[data-cost-box-total]')return total;return fields[selector.match(/name="([^"]+)"/)?.[1]];}},feeRoot={querySelectorAll:()=>legacy.filter(row=>!row.removed)};
@@ -105,7 +106,7 @@ test('界面快递互斥又能取消、圆通跟随归属、盒数正整数与
const html=h.html(h.read());assert.match(html,/name="cost_box" checked/);assert.match(html,/value="2"/);
h.fields.cost_box_quantity.value='0';const invalid=h.read();assert.equal(invalid[0].amount,'');assert.match(h.html(invalid),/value="0"/);assert.match(h.total.textContent,/正整数/);
h.fields.cost_box.checked=false;h.change('box');assert.deepEqual(h.read(),[]);assert.equal(h.fields.cost_box_quantity.disabled,true);
const oldHtml=h.html([{key:'shipping',note:'快递费',amount:'12.34',payer:'assistant2'}]);assert.doesNotMatch(oldHtml,/name="cost_(?:jd|yt|box|steel)"[^>]*disabled/);assert.match(oldHtml,/已有快递费用,勾选后替换/);
const oldHtml=h.html([{key:'shipping',note:'快递费',amount:'12.34',payer:'assistant2'}]);assert.doesNotMatch(oldHtml,/name="cost_(?:jd|yt|box|steel|silver)"[^>]*disabled/);assert.match(oldHtml,/已有快递费用,勾选后替换/);
});
test('旧模板费用先按原金额付款人保留,主动选择替换后切快递及取消均不双计',t=>{
const {ledger,issue}=fixture(t),old=[{key:'shipping',note:'快递费',amount:'12.34',payer:'assistant2',edited:true,status:'estimated'},{key:'box',note:'包装盒',amount:'4.50',payer:'assistant',edited:true,status:'estimated'}];
@@ -134,3 +135,23 @@ test('钢绳替换只处理明确钢绳,旧braiding编绳和任意其他费用
const saved=ledger.detail(issue(input(fees)).id);assert.equal(saved.directCost,1425);assert.equal(saved.expenses.length,3);
h.fields.cost_steel.checked=false;h.change('steel');assert.equal(h.fees().length,2);assert.equal(h.fees().find(row=>row.note==='编绳').amount,'5.25');
});
test('家里银链18元可回填取消,同名旧费用只替换一次,其他费用及编绳钢绳完整保留',t=>{
const {ledger,run,issue}=fixture(t),names=['银链','银链(家里)','银链(家里)'];
for(const name of names)assert.equal(Fees.checkboxCategory(name),'silverChain');
assert.equal(Fees.checkboxCategory('银链(外购)'),'');
const kept=[{key:'other',note:'其他实际成本',amount:'2.25',payer:'assistant2'},{key:'braiding',note:'编绳',amount:'5.25',payer:'assistant2'},{key:'manual:'+randomUUID(),note:'钢绳(店里)',amount:'9.75',payer:'assistant'},{key:'manual:'+randomUUID(),note:'银链(外购)',amount:'23',payer:'assistant'}];
const old=[...kept,...names.map(note=>({key:'manual:'+randomUUID(),note,amount:'20.75',payer:'assistant'}))],h=ui(old);
assert.deepEqual(h.fees(),old);assert.match(h.html(old),/已有家里银链费用(3项),勾选后替换全部/);
h.fields.cost_silver.checked=true;h.change('silver');const fees=h.fees();
assert.equal(fees.length,5);assert.equal(new Set(fees.map(row=>row.key)).size,5);
assert.deepEqual(fees.map(({note,amount,payer})=>({note,amount,payer})),[...kept,Fees.checkboxFees({silver:true})[0]].map(({note,amount,payer})=>({note,amount,payer})));
assert.match(fees.find(row=>row.note==='其他实际成本').key,/^manual:/);
const draft=Templates.apply(Templates.fromDraft(input(fees)),{today:date});draft.customer='合成银链客户';
const {id}=run(draft,()=>ledger.saveOrder(draft,actor)),stored=ledger.detail(id),restored=ui(stored.fee_items.map(row=>({...row,amount:String(row.amount/100)})));
assert.equal(restored.fields.cost_silver.checked,true);assert.match(restored.html(restored.fees()),/name="cost_silver" checked/);
const saved=ledger.detail(issue({...draft,id,revision:stored.revision,fee_items:restored.fees()}).id);
assert.equal(saved.directCost,5825);assert.equal(saved.expenses.length,5);assert.equal(saved.expenses.filter(row=>row.note==='银链(家里)'&&row.amount===1800&&row.payer==='owner').length,1);
restored.fields.cost_silver.checked=false;restored.change('silver');const cancelled=Templates.apply(Templates.fromDraft(input(restored.fees())),{today:date});
assert.equal(ui(cancelled.fee_items).fields.cost_silver.checked,false);assert.equal(cancelled.fee_items.length,4);
assert.equal(Fees.presetKind({...Fees.checkboxFees({silver:true})[0],expense_id:randomUUID()}),'');
});