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

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

+9 -7
View File
@@ -104,9 +104,9 @@ function editAction(l,id,input,actor,env,options={}){
return {id,price_lock_id:id,status:'pending',revision:after.revision,lock:clone(after)};
}
function cancelAction(l,id,input,actor,env,options={}){
const lock=guard(l,id,input,actor,env,options),reason=env.text(input.reason,'取消原因',240);release(l,lock,actor,env);voidEvents(l,id,reason,env);
const lock=guard(l,id,input,actor,env,options),reason=env.text(input.reason,'取消原因',240),materialReturn=LockPools.cancelReservations(l,lock,actor,env);voidEvents(l,id,reason,env);
l.run("UPDATE price_locks SET status='cancelled',revision=revision+1,cancelled_at=?,cancelled_reason=?,updated=? WHERE id=?",env.now(),reason,env.now(),id);l.dirty(lock.date);
const after=PriceLocks.detail(l,id,env);l.audit('price-lock:'+id,'录错作废待交付锁价单',actor,{reason,before:lock,after,notice:'撤销本单有效收款、手填购料及信用卡手续费,释放每行料池预留;无退款、采购退款、库存或利润流水'});
const after=PriceLocks.detail(l,id,env);l.audit('price-lock:'+id,'录错作废待交付锁价单',actor,{reason,before:lock,after,materialReturn,notice:'撤销本单有效收款、手填购料及信用卡手续费;同日料池释放预留,跨天按原克重原成本回实际生效日料池,保留历史耗用成本;无新增现金或实物流水'});
return {id,price_lock_id:id,status:'cancelled',revision:after.revision,lock:clone(after)};
}
function apply(l,id,input,actor,env,action,options={}){return atomic(l,()=>{
@@ -121,16 +121,16 @@ function related(before,after){const fields=['goodsCost','profit','total','netRe
function businessEffect(l,id){return l.scope(null,()=>{
const lock=PriceLocks.detail(l,id,{fail:(message,status)=>{const error=new Error(message);error.status=status;throw error;},today:()=>''});
const clean=(key,value)=>['id','lineId','revision','created','updated','actor','voided_at','cancelled_at','source_id','source_sequence','price_lock_id','number'].includes(key)?undefined:value;
return JSON.parse(JSON.stringify({lock:{date:lock.date,status:lock.status,customer:lock.customer,salesperson:lock.salesperson,note:lock.note,lines:lock.lines.map(row=>({...row,delivery:undefined})),estimated_total:lock.estimated_total,discount:lock.discount,netReceived:lock.netReceived,cash_events:lock.cash_events,material_events:lock.material_events,credit_card_fee_total:lock.credit_card_fee_total},orders:[...orderFinance(l).values()].map(row=>({...row,lines:l.rawOrder(row.id).lines})),pools:l.rows('SELECT date,opening,snapshot FROM material_rolling_days ORDER BY date').map(row=>{const pool=row.snapshot?JSON.parse(row.snapshot):null,fields=['totalMg','totalAmount','usedMg','remainingMg','shortageMg','consumedCost','remainingCost','averageRate','status','byPayer','remainingAllocations','remainingMaterialGroups'];return {date:row.date,opening:JSON.parse(row.opening),snapshot:pool?Object.fromEntries(fields.map(key=>[key,pool[key]])):null};})},clean));
return JSON.parse(JSON.stringify({lock:{date:lock.date,status:lock.status,customer:lock.customer,salesperson:lock.salesperson,note:lock.note,lines:lock.lines.map(row=>({...row,delivery:undefined})),estimated_total:lock.estimated_total,discount:lock.discount,netReceived:lock.netReceived,cash_events:lock.cash_events,material_events:lock.material_events,credit_card_fee_total:lock.credit_card_fee_total,cancellationReturns:lock.cancellationReturns},orders:[...orderFinance(l).values()].map(row=>({...row,lines:l.rawOrder(row.id).lines})),pools:l.rows('SELECT date,opening,snapshot FROM material_rolling_days ORDER BY date').map(row=>{const pool=row.snapshot?JSON.parse(row.snapshot):null,fields=['totalMg','totalAmount','usedMg','remainingMg','shortageMg','consumedCost','remainingCost','averageRate','status','byPayer','remainingAllocations','remainingMaterialGroups'];return {date:row.date,opening:JSON.parse(row.opening),snapshot:pool?Object.fromEntries(fields.map(key=>[key,pool[key]])):null};})},clean));
},null);}
function preview(l,id,payload,actor,env,action){
function preview(l,id,payload,actor,env,action,options={}){
return l.scope(null,()=>{
const before=PriceLocks.detail(l,id,env),finance=orderFinance(l),lockFinance=LockApprovals.finance(l);l.db.exec('SAVEPOINT price_lock_change_preview');
try{const result=apply(l,id,payload,{...actor,role:'owner'},env,action),after=result.lock,fields=['date','customer','salesperson','note','lines','discount','estimated_total','netReceived','material_amount','status'];return {before,after,changes:fields.filter(key=>JSON.stringify(before[key])!==JSON.stringify(after[key])).map(field=>({field,before:before[field],after:after[field]})),relatedOrders:related(finance,orderFinance(l)),relatedPriceLocks:LockApprovals.changes(lockFinance,LockApprovals.finance(l)).filter(row=>row.id!==id),effectHash:hash(businessEffect(l,id))};}finally{l.db.exec('ROLLBACK TO price_lock_change_preview;RELEASE price_lock_change_preview');}
try{const result=apply(l,id,payload,{...actor,role:'owner'},env,action,options),after=result.lock,fields=['date','customer','salesperson','note','lines','discount','estimated_total','netReceived','material_amount','status'];return {before,after,...(action==='cancel'?{materialReturnDate:env.today()}:{}),changes:fields.filter(key=>JSON.stringify(before[key])!==JSON.stringify(after[key])).map(field=>({field,before:before[field],after:after[field]})),relatedOrders:related(finance,orderFinance(l)),relatedPriceLocks:LockApprovals.changes(lockFinance,LockApprovals.finance(l)).filter(row=>row.id!==id),effectHash:hash(businessEffect(l,id))};}finally{l.db.exec('ROLLBACK TO price_lock_change_preview;RELEASE price_lock_change_preview');}
},null);
}
function stale(l,row){return row.status==='pending'&&hash(snapshot(l,row.price_lock_id))!==row.snapshot_hash;}
function read(l,id,actor,env){active(actor,env);const row=l.one('SELECT * FROM price_lock_changes WHERE id=?',id);if(!row)env.fail('锁价修改申请不存在',404);l.assertAccess('price_lock',row.price_lock_id,actor);const value=JSON.parse(row.preview),visible=actor.role==='assistant'?value.relatedOrders.filter(item=>l.canAccess('order',item.id,actor)):value.relatedOrders;return {...row,id,requestId:id,revision:row.price_lock_revision,payload:JSON.parse(row.payload),snapshot:undefined,preview:undefined,effect_hash:undefined,snapshot_hash:undefined,...value,relatedOrders:visible,relatedPriceLocks:LockApprovals.visibleChanges(l,value.relatedPriceLocks,actor),stale:stale(l,row)};}
function read(l,id,actor,env){active(actor,env);const row=l.one('SELECT * FROM price_lock_changes WHERE id=?',id);if(!row)env.fail('锁价修改申请不存在',404);l.assertAccess('price_lock',row.price_lock_id,actor);const stored=JSON.parse(row.preview),current= row.action==='cancel'&&row.status==='pending'&&!stale(l,row)&&stored.materialReturnDate!==env.today()?preview(l,row.price_lock_id,JSON.parse(row.payload),actor,env,'cancel',{approvalId:id}):stored,{effectHash,...value}=current,visible=actor.role==='assistant'?value.relatedOrders.filter(item=>l.canAccess('order',item.id,actor)):value.relatedOrders;return {...row,id,requestId:id,revision:row.price_lock_revision,payload:JSON.parse(row.payload),snapshot:undefined,preview:undefined,effect_hash:undefined,snapshot_hash:undefined,...value,relatedOrders:visible,relatedPriceLocks:LockApprovals.visibleChanges(l,value.relatedPriceLocks,actor),stale:stale(l,row)};}
function request(l,id,input,actor,env){return atomic(l,()=>{
active(actor,env);const lock=getLock(l,id,actor,env),action=env.pick(input.action,['edit','cancel'],'锁价申请类型');if(lock.status!=='pending'||lock.final_order_id)env.fail('只有待交付锁价单可以修改或取消',409);if(pending(l,id))env.fail('本单已有待审批申请,请先由麦麦复核',409);
if(input.revision!==lock.revision)env.fail('锁价单已更新,请刷新后再申请',409);if(input.verified!==true)env.fail('请核实锁价修改资料');const reason=env.text(input.reason,'申请原因',240);
@@ -140,7 +140,9 @@ function request(l,id,input,actor,env){return atomic(l,()=>{
});}
function approve(l,id,input,actor,env){return atomic(l,()=>{
owner(actor,env);const row=l.one('SELECT * FROM price_lock_changes WHERE id=?',id);if(!row)env.fail('锁价修改申请不存在',404);l.assertAccess('price_lock',row.price_lock_id,actor);if(row.status!=='pending')env.fail('该申请已处理',409);if(input.verified!==true||input.revision!==row.price_lock_revision)env.fail('请核实当前申请版本后审批',409);if(stale(l,row))env.fail('申请依据已过期,请驳回后重新申请',409);
const result=apply(l,row.price_lock_id,JSON.parse(row.payload),actor,env,row.action,{approvalId:id});if(hash(businessEffect(l,row.price_lock_id))!==row.effect_hash)env.fail('本次执行与申请试算结果不一致,请重新申请',409);
const stored=JSON.parse(row.preview),refreshed=row.action==='cancel'&&stored.materialReturnDate!==env.today()?preview(l,row.price_lock_id,JSON.parse(row.payload),actor,env,'cancel',{approvalId:id}):null,expectedHash=refreshed?.effectHash??row.effect_hash;
const result=apply(l,row.price_lock_id,JSON.parse(row.payload),actor,env,row.action,{approvalId:id});if(hash(businessEffect(l,row.price_lock_id))!==expectedHash)env.fail('本次执行与申请试算结果不一致,请重新申请',409);
if(refreshed){const {effectHash,...view}=refreshed;l.run('UPDATE price_lock_changes SET preview=?,effect_hash=? WHERE id=?',JSON.stringify(view),effectHash,id);}
l.run("UPDATE price_lock_changes SET status='approved',reviewer_id=?,reviewer_name=?,reviewed=?,note=? WHERE id=?",actor.id,actor.name,env.now(),env.text(input.note,'审批备注',240,true),id);
l.audit('price-lock:'+row.price_lock_id,'同意'+(row.action==='cancel'?'取消':'修改')+'锁价单',actor,{requestId:id,reason:row.reason,result});return {id,requestId:id,price_lock_id:row.price_lock_id,request:read(l,id,actor,env),lock:result.lock};
});}