完善订单成本编辑与多商品置换,修正锁价跨天作废回池

This commit is contained in:
summer committed 2026-11-24 21:11:32 +08:00
1 parent a3262a455d
commit 2e4e7dad55
25 files changed
+430 -109

No files matched your search

@@ -22,6 +22,45 @@ function fixture(t){
const contents=(l,tables=l.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").map(r=>r.name))=>JSON.stringify(tables.map(table=>[table,l.rows('SELECT * FROM "'+table+'"')]));
const financial=['recoveries','material_purchases','recovery_purchase_sources','material_pool_usages','payments','expenses','journal','movements','team_fund_sources','team_fund_entries','team_fund_cash','closes','inventory_snapshots'];
test('旧料可入找金和自定义盘,已耗用个人料只换实物去向不改采购、耗用或付款',t=>{
const f=fixture(t),r=f.create(direct({old_pocket:'找金',old_lines:old('10','99')})),inv=key=>f.l.inventory().find(row=>row.pocket===key);
assert.equal(r.oldPocket,'找金');assert.equal(inv('找金').mg,9900);assert.equal(inv('回收旧料').mg,0);assert.equal(f.l.pocket('找金').self,false);
const use={date:D,member:'assistant',customer:'合成用料客户',salesperson:'合成开单人',payment_account:'麦麦微信',lines:[{name:'足金999',unit:'g',quantity:'5',pocket:'',payer:'owner',pricingMode:'manual',sale:'5000',costParts:{material:'',labor:'0',accessories:'0',other:'0'},costMaterial:{mode:'pool',poolId:D}}],fee_items:[]};
f.run(use,()=>f.l.issueOrder(use,f.actors.owner));const pocket=f.run({},()=>f.l.createPocket({name:'合成回收存放盘'},f.actors.owner)).pocket;
const tables=['orders','material_purchases','recovery_purchase_sources','material_pool_usages','payments','expenses','journal','team_fund_entries','team_fund_cash'],before=contents(f.l,tables),pool=f.l.materialPoolDetail(D).personalPools.assistant;
assert.equal(pool.usedMg,5000);f.update(r.id,{old_pocket:pocket.key});assert.equal(inv('找金').mg,0);assert.equal(inv(pocket.key).mg,9900);assert.equal(contents(f.l,tables),before);assert.equal(f.l.materialPoolDetail(D).personalPools.assistant.remainingMg,pool.remainingMg);
assert.equal(f.info(r.id).inputs.old_pocket,pocket.key);assert.equal(f.info(r.id).history[0].before.old_pocket,'找金');assert.equal(f.info(r.id).history[0].after.old_pocket,pocket.key);
f.cancel(r.id);assert.equal(inv(pocket.key).mg,0);assert.equal(inv('找金').mg,0);assert.equal(inv('回收旧料').mg,0);
});
test('置换旧料入库与新货出库独立,改旧料盘不重记财务,取消两侧回到原库存',t=>{
const f=fixture(t);f.stock();const r=f.create(exchange({old_pocket:'找金'})),inv=key=>f.l.inventory().find(row=>row.pocket===key).mg;
assert.equal(inv('找金'),20000);assert.equal(inv('现货'),70000);assert.equal(inv('回收旧料'),0);
const tables=['orders','material_purchases','recovery_purchase_sources','material_pool_usages','payments','expenses','journal','team_fund_entries','team_fund_cash'],before=contents(f.l,tables);
f.update(r.id,{old_pocket:'金料'});assert.equal(inv('找金'),0);assert.equal(inv('金料'),20000);assert.equal(inv('现货'),70000);assert.equal(contents(f.l,tables),before);
f.cancel(r.id);assert.equal(inv('金料'),0);assert.equal(inv('找金'),0);assert.equal(inv('现货'),100000);
});
test('历史缺字段仍属回收旧料且只读不迁移,停用原盘可沿用,历史助理改盘和取消须审批',t=>{
const f=fixture(t),r=f.create(direct({old_lines:old('10','99')})),row=f.l.one('SELECT data FROM recoveries WHERE id=?',r.id),legacy=JSON.parse(row.data);delete legacy.oldPocket;
f.l.run('UPDATE recoveries SET data=? WHERE id=?',JSON.stringify(legacy),r.id);const saved=f.l.one('SELECT data FROM recoveries WHERE id=?',r.id).data;
assert.equal(f.l.recovery(r.id,f.actors.owner).oldPocket,'回收旧料');assert.equal(f.info(r.id).inputs.old_pocket,'回收旧料');assert.equal(f.l.one('SELECT data FROM recoveries WHERE id=?',r.id).data,saved);
f.run({},()=>f.l.updatePocket('回收旧料',{revision:f.l.pocket('回收旧料').revision,active:false},f.actors.owner));
assert.throws(()=>f.create(),/停用/);assert.throws(()=>f.create(direct({old_pocket:'不存在'})),/货盘/);const movements=f.l.rows('SELECT * FROM movements');f.update(r.id,{customer:'只改客户'});assert.deepEqual(f.l.rows('SELECT * FROM movements'),movements);
const applied=f.update(r.id,{old_pocket:'找金'},'assistant');assert.equal(applied.pending,true);assert.equal(f.l.inventory().find(row=>row.pocket==='回收旧料').mg,9900);f.approve(applied.request);assert.equal(f.l.inventory().find(row=>row.pocket==='回收旧料').mg,0);assert.equal(f.l.inventory().find(row=>row.pocket==='找金').mg,9900);
const cancel=f.cancel(r.id,{},'assistant');assert.equal(cancel.pending,true);assert.equal(f.l.inventory().find(row=>row.pocket==='找金').mg,9900);f.approve(cancel.request);assert.equal(f.l.inventory().find(row=>row.pocket==='找金').mg,0);
});
test('计对旧料盘独立校验入库数量,修改冲回原对数,置换两侧数量分别记录和取消',t=>{
t.mock.timers.enable({apis:['Date'],now:Date.parse(D+'T04:00:00Z')});const f=fixture(t),pocket=f.run({},()=>f.l.createPocket({name:'合成按对回收盘',countUnit:'pair',initialCount:0,countVerified:true},f.actors.owner)).pocket,inv=()=>f.l.inventory().find(row=>row.pocket===pocket.key);
for(const old_stock_count of [undefined,null,'',0,1.5,'2.0']){const before=contents(f.l);assert.throws(()=>f.create(direct({old_pocket:pocket.key,old_stock_count})),/数量|整数/);assert.equal(contents(f.l),before);}
assert.throws(()=>f.create(direct({date:'2026-10-19',old_pocket:pocket.key,old_stock_count:1})),/盘点基准/);
const r=f.create(direct({old_pocket:pocket.key,old_stock_count:3}));assert.equal(inv().mg,10000);assert.equal(inv().count,3);assert.equal(f.info(r.id).inputs.old_stock_count,3);
const purchase=f.l.one('SELECT * FROM material_purchases WHERE id=?',r.purchase_id);f.update(r.id,{old_stock_count:2});assert.equal(inv().count,2);assert.deepEqual(f.l.one('SELECT * FROM material_purchases WHERE id=?',r.purchase_id),purchase);
f.update(r.id,{old_pocket:'找金',old_stock_count:''});assert.equal(inv().count,0);assert.equal(inv().mg,0);f.cancel(r.id);assert.equal(f.l.inventory().find(row=>row.pocket==='找金').mg,0);
const swap=f.create(exchange({old_lines:old('10'),old_pocket:pocket.key,old_stock_count:3},{quantity:'5',pocket:pocket.key,stockCount:2}));assert.equal(inv().mg,5000);assert.equal(inv().count,1);assert.equal(swap.oldStockCount,3);assert.equal(swap.newProduct.stockCount,2);f.cancel(swap.id);assert.equal(inv().mg,0);assert.equal(inv().count,0);
});
test('直接回收原位改重量、金额、日期和付款人,库存与现金仅保留新有效值,原身份和流水保留',t=>{
const f=fixture(t),r=f.create(direct({old_lines:old('10','99'),buy_total:'8999.91'})),original=f.l.one('SELECT * FROM recoveries WHERE id=?',r.id),movement=f.l.one('SELECT * FROM movements WHERE group_id=?',original.inventory_group);
for(const date of [D,D2])f.run({date},()=>f.l.closeDay(date,f.actors.owner,'orderDate'));
@@ -132,3 +171,35 @@ test('HTTP回收专用更正保留会话、CSRF、幂等与审批权限,旧普
const request=(await call('assistant',route,{...input,revision:2,date:D,buy_total:'9200'})).body.request;assert.equal(request.status,'pending');assert.equal((await call('assistant2','/api/recovery-changes/'+request.id)).status,404);assert.equal((await call('assistant','/api/recovery-changes/'+request.id+'/approve',{revision:1,verified:true})).status,403);
const approval=await call('owner','/api/recovery-changes/'+request.id+'/approve',{revision:1,verified:true});assert.equal(approval.status,200);assert.equal(approval.body.status,'approved');assert.equal((await call('owner','/api/recovery-changes/'+request.id+'/approve',{revision:1,verified:true})).status,409);
});
test('外配置换新货不扣库存,多商品分别出库、计成本,修改取消完整冲回',t=>{
const f=fixture(t);f.stock();
const products=[{name:'足金手链',quantity:'12',pocket:'现货',labor_mode:'gram',labor_rate:'20',cost_labor:{mode:'gram',rate:'5'},cost_labor_payer:'owner'},{name:'足金外配镯',quantity:'18',pocket:'',labor_mode:'gram',labor_rate:'30',cost_labor:{mode:'total',amount:'90'},cost_labor_payer:'assistant2'}];
const r=f.create(exchange({}, {products})),order=f.l.detail(r.order_id),inv=key=>f.l.inventory().find(row=>row.pocket===key).mg;
assert.equal(r.newMg,30000);assert.equal(r.differenceMg,10000);assert.equal(r.receivable,1098000);assert.equal(r.materialCost,900000);assert.equal(r.laborCost,15000);assert.equal(r.profit,183000);
assert.equal(order.lines.length,2);assert.equal(order.lines[1].pocket,'');assert.equal(order.lines[1].costPayers.labor,'assistant2');assert.equal(inv('现货'),88000);assert.equal(inv('回收旧料'),20000);assert.equal(f.l.rows("SELECT * FROM movements WHERE pocket='' ").length,0);
assert.equal(order.lines.reduce((n,row)=>n+row.recoveryMaterialMg,0),10000);assert.equal(order.lines.reduce((n,row)=>n+row.costParts.material,0),900000);
const inputs=f.info(r.id).inputs;assert.equal(inputs.exchange.products.length,2);assert.equal(inputs.exchange.products[1].pocket,'');
const edited={...inputs.exchange,products:inputs.exchange.products.map((p,i)=>i?{...p,quantity:'8'}:p)};
f.update(r.id,{exchange:edited});let q=f.info(r.id).record;assert.equal(q.newMg,20000);assert.equal(q.materialCost,0);assert.equal(inv('现货'),88000);assert.equal(inv('回收旧料'),20000);
f.cancel(r.id);assert.equal(inv('现货'),100000);assert.equal(inv('回收旧料'),0);assert.equal(f.l.detail(r.order_id).profit,0);
});
test('外配单件可保存修改取消;缺失货盘和无效盘仍拒绝,后续商品库存不足整单回滚',t=>{
const f=fixture(t),r=f.create(exchange({}, {pocket:''}));assert.equal(f.l.detail(r.order_id).lines[0].pocket,'');
f.update(r.id,{note:'外配更正'});f.cancel(r.id);assert.equal(f.l.inventory().find(row=>row.pocket==='回收旧料').mg,0);
for(const pocket of [undefined,null,'不存在'])assert.throws(()=>f.create(exchange({}, {pocket})),/货盘/);
const products=[{name:'足金外配',quantity:'10',pocket:'',labor_rate:'10',cost_labor:{mode:'total',amount:'0'},cost_labor_payer:'owner'},{name:'足金现货',quantity:'20',pocket:'现货',labor_rate:'10',cost_labor:{mode:'total',amount:'0'},cost_labor_payer:'owner'}];
const before=contents(f.l);assert.throws(()=>f.create(exchange({}, {products})),/库存不足/);assert.equal(contents(f.l),before);
});
test('多商品差重合计只耗用一次料池,余料只生成一笔购料;客户打印不带内部字段',t=>{
const f=fixture(t);f.stock();f.run({},()=>f.l.createMaterialPurchase({date:D,grams:'20',total:'18000',payer:'owner'},f.actors.owner));
const products=[{name:'足金A',quantity:'10.001',pocket:'现货',labor_rate:'10',cost_labor:{mode:'total',amount:'0'},cost_labor_payer:'owner'},{name:'足金B',quantity:'19.999',pocket:'',labor_rate:'20',cost_labor:{mode:'total',amount:'0'},cost_labor_payer:'assistant'}];
const r=f.create(exchange({}, {products,cost_material:{mode:'pool',poolId:D}}));
assert.equal(f.l.materialPoolDetail(D).usedMg,10000);assert.equal(f.l.detail(r.order_id).goodsCost,900000);
const dto=require('../recoveries').publicDetail(f.l,r.id,f.actors.owner,f.l.businessEnv());assert.equal(dto.new_lines.length,2);assert.doesNotMatch(JSON.stringify(dto),/costLabor|laborPayer|pocket|materialCost/);
const html=require('../recovery-printing').exchangeCustomerPrintDocument(dto);assert.match(html,/足金A/);assert.match(html,/足金B/);assert.doesNotMatch(html,/costLabor|laborPayer/);
f.cancel(r.id);assert.equal(f.l.materialPoolDetail(D).usedMg,0);
const surplus=f.create(exchange({old_lines:old('40'),buy_rate:'850'}, {products}));assert.equal(surplus.purchaseMg,10000);assert.equal(surplus.purchaseAmount,850000);assert.equal(f.l.rows('SELECT * FROM recovery_purchase_sources WHERE recovery_id=?',surplus.id).length,1);
});