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

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

+18 -2
View File
@@ -40,7 +40,7 @@ function reservations(l,date){return l.rows(`SELECT r.*,s.sequence AS fifo_seque
return {...r,kind:'priceLock',member:l.entityMember('price_lock',r.price_lock_id),...(effective.materialSources?{materialSources:effective.materialSources}:{}),date,price_lock_id:r.price_lock_id,order_id:r.final_order_id,reserved_mg:r.mg,mg,...substitution(l,'priceLock',r.id,mg,r.mg),active:mg>0?1:0,amount:effective.amount,snapshot,allocations:effective.allocations,product:entry?.line.name??JSON.parse(r.line).name,...Object.fromEntries(PARTIES.map(p=>[p+'_amount',effective.amount===null?null:sum(effective.allocations.filter(a=>a.payer===p),'amount')]))};
});}
function combined(l,date,rows){return [...decorate(l,rows),...reservations(l,date)].sort((a,b)=>a.created<b.created?-1:a.created>b.created?1:a.fifo_sequence-b.fifo_sequence);}
function returns(l,date){return l.rows('SELECT r.*,p.number,p.customer,p.line,p.lines AS lock_lines FROM price_lock_pool_returns r JOIN price_locks p ON p.id=r.price_lock_id WHERE r.date=? ORDER BY r.created,r.rowid',date).map(r=>({...r,allocations:JSON.parse(r.allocations),...(Allocation.returnSources(l,'lock',r.id)?{materialSources:Allocation.returnSources(l,'lock',r.id)}:{}),product:LockLines.product({...r,id:r.price_lock_id,lines:r.lock_lines},r.line_index),sourceKind:'return',cashAmount:0,payer:JSON.parse(r.allocations)[0]?.payer??'owner',sameDay:!!r.same_day,note:r.kind==='leftover'?'锁价余料回池':'锁价退货回料',source_sequence:l.one("SELECT sequence FROM material_rolling_source_order WHERE kind='lock_return' AND source_id=?",r.id)?.sequence??0}));}
function returns(l,date){return l.rows('SELECT r.*,p.number,p.customer,p.line,p.lines AS lock_lines FROM price_lock_pool_returns r JOIN price_locks p ON p.id=r.price_lock_id WHERE r.date=? ORDER BY r.created,r.rowid',date).map(r=>({...r,allocations:JSON.parse(r.allocations),...(Allocation.returnSources(l,'lock',r.id)?{materialSources:Allocation.returnSources(l,'lock',r.id)}:{}),product:LockLines.product({...r,id:r.price_lock_id,lines:r.lock_lines},r.line_index),sourceKind:'return',cashAmount:0,payer:JSON.parse(r.allocations)[0]?.payer??'owner',sameDay:!!r.same_day,note:r.kind==='cancel'?'锁价作废回料(原成本)':r.kind==='leftover'?'锁价余料回池':'锁价退货回料',source_sequence:l.one("SELECT sequence FROM material_rolling_source_order WHERE kind='lock_return' AND source_id=?",r.id)?.sequence??0}));}
function fundingParts(purchases,start,amount){let skip=start,left=amount;const result=[];for(const p of purchases)for(const part of (p.allocations??[{payer:p.payer,amount:p.amount}])){if(skip>=part.amount){skip-=part.amount;continue;}const n=Math.min(left,part.amount-skip);if(n)result.push({payer:part.payer,amount:n});left-=n;skip=0;}if(left)throw new Error('锁价料池来源金额不足');return result;}
function allocate(pool,rows,allocatePayers){
if(Allocation.enabled(pool)||rows.some(row=>row.publicSubstitutionMg>0||row.reservedPublicSubstitutionMg>0))return Allocation.allocate({...pool,materialGroups:pool.materialGroups??Allocation.groups(pool.purchases)},rows);
@@ -83,6 +83,7 @@ function update(l,row,pool,actor,env,{skipJournalOrder=null}={}){
l.run('UPDATE price_locks SET revision=revision+1,updated=? WHERE id=?',env.now(),row.price_lock_id);
const lock=l.one('SELECT * FROM price_locks WHERE id=?',row.price_lock_id),lockedLines=LockLines.lines(lock),entry=lockedLines[row.line_index],lockedLine=entry.line;
entry.material_amount=snapshot.amount;entry.buy_rate=snapshot.averageRate;entry.material_source='pool';lockedLine.costParts={...lockedLine.costParts,material:snapshot.amount};lockedLine.cost=Object.values(lockedLine.costParts).some(v=>v===null)?null:sum(Object.values(lockedLine.costParts).map(amount=>({amount})),'amount');LockLines.save(l,lock,lockedLines);
if(lock.status==='cancelled'){const returned=l.one("SELECT * FROM price_lock_pool_returns WHERE price_lock_id=? AND line_index=? AND kind='cancel'",lock.id,row.line_index);if(returned)returnRow(l,{...lock,line_index:row.line_index},'cancel',returned.date,row.mg,snapshot.amount,snapshot.allocations,snapshot.poolId,actor,env,snapshot.materialSources??null);}
if(lock.final_order_id){
const order=l.rawOrder(lock.final_order_id),line=order.lines[row.line_index],deliveries=LockLines.deliveryLines(lock.delivery),delivery=deliveries[row.line_index],part=split(snapshot,delivery.original_used_mg),before=line.costParts.material,lineLock={...lock,line_index:row.line_index};
const extra=(line.materialAllocations??[]).filter(a=>a.source==='extra');line.materialAllocations=[...materialAllocations(snapshot,delivery.original_used_mg),...extra];line.priceLockPool={...snapshot,quantity:delivery.original_used_mg,amount:part.amount,allocations:part.allocations,...(part.materialSources?{materialSources:part.materialSources}:{}),priceLockId:lock.id,lineId:entry.id,line_index:row.line_index};
@@ -152,6 +153,21 @@ function releaseForOrderEdit(l,value,actor,env,options={}){return atomicEdit(l,(
for(const date of dates)l.dirty(date);
return {released:true,reservation,returns:rows,affectedDates:dates,earliestDate:dates[0]};
});}
// 跨天作废保留原日占用事实,以原成本在实际生效日回料;不倒改历史加权。
function cancelReservations(l,value,actor,env){return atomicEdit(l,()=>{
const lock=editableLock(l,value,actor,env),date=env.today(),rows=findAll(l,lock.id),cross=rows.filter(row=>row.pool_id<date),same=rows.filter(row=>row.pool_id===date);
if(rows.some(row=>row.pool_id>date))env.fail('锁价料池日期晚于作废日期,请先核对',409);
for(const row of cross){
if(!Number.isSafeInteger(row.amount)||row.amount<0||row.snapshot.amount!==row.amount||row.snapshot.quantity!==row.mg||sum(row.allocations,'amount')!==row.amount)env.fail('原锁价料费或付款来源待核实,不能按未知成本回池,请先核实原料费',409);
}
// 先建立当日结转,冻结的是原日预留仍有效时的剩料,避免回料重复结转。
const rolling=require('./rolling-material-pools');
if(cross.length&&rolling.enabled(l))rolling.reconcile(l,date,actor,env);
for(const row of cross)returnRow(l,{...lock,line_index:row.line_index},'cancel',date,row.mg,row.amount,row.allocations,row.pool_id,actor,env,row.snapshot.materialSources??null);
for(const row of same)releaseForOrderEdit(l,lock,actor,env,{lineIndex:row.line_index,preservePriceLockIds:[lock.id]});
if(cross.length)rebuildAffected(l,[date],actor,env,{preservePriceLockIds:[lock.id]});
return {date,returned:cross.map(row=>({lineIndex:row.line_index,mg:row.mg,amount:row.amount,originPoolId:row.pool_id,allocations:row.allocations})),releasedSameDay:same.map(row=>row.line_index)};
});}
// 仅挪动交付日时原锁定分厘和付款归属冻结;余料沿原回料身份移日,不能成为第二笔采购。
function moveDeliveryDate(l,value,nextDate,actor,env,{skipJournalOrder=null}={}){return atomicEdit(l,()=>{
const lock=editableLock(l,value,actor,env),reservations=findAll(l,lock.id);
@@ -172,4 +188,4 @@ function moveDeliveryDate(l,value,nextDate,actor,env,{skipJournalOrder=null}={})
for(const date of dates)l.dirty(date);
return {moved:true,affectedDates:dates,earliestDate:dates[0],delivery:updated};
});}
module.exports={initialize,sequence,decorate,find,findAll,detail,reservations,combined,returns,allocate,update,reserve,split,materialAllocations,prepareDelivery,finishDelivery,recordReturn,releaseForOrderEdit,moveDeliveryDate};
module.exports={initialize,sequence,decorate,find,findAll,detail,reservations,combined,returns,allocate,update,reserve,split,materialAllocations,prepareDelivery,finishDelivery,recordReturn,releaseForOrderEdit,cancelReservations,moveDeliveryDate};