取消已用回收单时将关联用料转入公共料池

This commit is contained in:
summer committed 2026-11-05 00:04:01 +08:00
1 parent 19b1b0263c
commit dec01a3dca
18 files changed
+380 -56

No files matched your search

+9 -4
View File
@@ -9,7 +9,11 @@ function initialize(l){l.db.exec(`
INSERT INTO material_usage_order(kind,source_id) SELECT 'order',u.id FROM material_pool_usages u WHERE NOT EXISTS(SELECT 1 FROM material_usage_order s WHERE s.kind='order' AND s.source_id=u.id) ORDER BY u.created,u.rowid;
`);}
function sequence(l,kind,id){if(!l.one('SELECT sequence FROM material_usage_order WHERE kind=? AND source_id=?',kind,id))l.run('INSERT INTO material_usage_order(kind,source_id) VALUES (?,?)',kind,id);return l.one('SELECT sequence FROM material_usage_order WHERE kind=? AND source_id=?',kind,id).sequence;}
function decorate(l,rows){return rows.map(r=>({...r,fifo_sequence:l.one("SELECT sequence FROM material_usage_order WHERE kind='order' AND source_id=?",r.id)?.sequence??r.fifo_sequence}));}
function substitution(l,kind,id,quantity,reserved){
const ranges=Allocation.publicSubstitutionRanges(l,kind,id);
return ranges.length?{publicSubstitutionRanges:ranges,publicSubstitutionMg:Allocation.publicSubstitutionMg(l,kind,id,quantity),...(reserved===undefined?{}:{reservedPublicSubstitutionMg:Allocation.publicSubstitutionMg(l,kind,id,reserved)})}:{};
}
function decorate(l,rows){return rows.map(r=>({...r,fifo_sequence:l.one("SELECT sequence FROM material_usage_order WHERE kind='order' AND source_id=?",r.id)?.sequence??r.fifo_sequence,...substitution(l,'order',r.id,r.mg)}));}
function find(l,id){const r=l.one('SELECT * FROM price_lock_pool_reservations WHERE price_lock_id=?',id);return r?{...r,allocations:JSON.parse(r.allocations),snapshot:JSON.parse(r.snapshot)}:null;}
function splitParts(parts,amount){let left=amount;const taken=[],rest=[];for(const part of parts){const n=Math.min(left,part.amount);if(n)taken.push({payer:part.payer,amount:n});if(part.amount>n)rest.push({payer:part.payer,amount:part.amount-n});left-=n;}if(left)throw new Error('锁价料池付款归属不足');if(!amount&&!taken.length)taken.push({payer:parts[0]?.payer??'owner',amount:0});return {taken,rest};}
function split(snapshot,used){
@@ -23,13 +27,13 @@ function reservations(l,date){return l.rows(`SELECT r.*,s.sequence AS fifo_seque
const snapshot=JSON.parse(r.snapshot),delivery=r.delivery?JSON.parse(r.delivery):null;
let mg=r.mg;if(delivery?.date===date)mg=delivery.original_used_mg;if(r.order_status==='returned'&&r.returned_date===date)mg=0;
const effective=snapshot.amount===undefined?{amount:null,allocations:[]}:mg===r.mg?{amount:r.amount,allocations:JSON.parse(r.allocations),...(snapshot.materialSources?{materialSources:snapshot.materialSources}:{})}:split(snapshot,mg);
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,line_index:0,reserved_mg:r.mg,mg,active:mg>0?1:0,amount:effective.amount,snapshot,allocations:effective.allocations,product: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')]))};
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,line_index:0,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: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 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:JSON.parse(r.line).name,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 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))return Allocation.allocate(pool,rows);
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);
const missing=pool.totalAmount===null||sum(rows,'mg')>pool.totalMg;let mg=0,previous=0;
return rows.map(row=>{
const prefixMg=mg,prefixAmount=previous;mg+=row.mg;const cumulative=missing?null:Calc.poolMaterialAmount(pool.totalAmount,pool.totalMg,mg),amount=cumulative===null?null:cumulative-previous;
@@ -59,7 +63,8 @@ function returnRow(l,lock,kind,date,mg,amount,allocations,origin,actor,env,mater
if(!mg)return;const old=l.one('SELECT * FROM price_lock_pool_returns WHERE price_lock_id=? AND kind=?',lock.id,kind),id=old?.id??randomUUID();
if(old)l.run('UPDATE price_lock_pool_returns SET date=?,mg=?,amount=?,allocations=?,origin_pool_id=?,same_day=? WHERE id=?',date,mg,amount,JSON.stringify(allocations),origin,origin===date?1:0,id);
else {l.run('INSERT INTO price_lock_pool_returns VALUES (?,?,?,?,?,?,?,?,?,?,?)',id,lock.id,kind,date,mg,amount,JSON.stringify(allocations),origin,origin===date?1:0,actor.name,env.now());require('./rolling-material-pools').registerSource(l,'lock_return',id);}
Allocation.saveReturn(l,'lock',id,materialSources);
if(materialSources)Allocation.saveReturn(l,'lock',id,materialSources);
else l.run("DELETE FROM material_return_sources WHERE return_kind='lock' AND return_id=?",id);
}
function materialAllocations(snapshot,used){const part=split(snapshot,used);if(part.materialSources)return part.materialSources.flatMap(s=>s.allocations.map(p=>({source:'original',quantity:s.quantity,rate:s.averageRate??snapshot.averageRate,amount:p.amount,payer:p.payer,weighted:true,poolId:s.poolId??snapshot.poolId,materialKind:s.kind,materialOwner:s.member})));return part.amount===null?[]:part.allocations.map(p=>({source:'original',quantity:used,rate:snapshot.averageRate,amount:p.amount,payer:p.payer,weighted:true,poolId:snapshot.poolId}));}
function update(l,row,pool,actor,env,{skipJournalOrder=null}={}){