支持直接修改已生成订单并重算成本库存与利润

This commit is contained in:
summer committed 2026-10-22 17:06:32 +08:00
1 parent 3bf9e8d82d
commit 34781b6cd1
14 files changed
+718 -31

No files matched your search

+34 -1
View File
@@ -213,6 +213,39 @@ function release(ledger,order,date,actor,env){
}
for(const id of pools)reconcile(ledger,id,date,actor,env);
}
// 整单修改复用原行的消耗身份;删除、重排、换池和手填成本都留前后事件。
function adjust(ledger,order,date,actor,env){
const existing=ledger.rows('SELECT * FROM material_pool_usages WHERE order_id=? ORDER BY line_index',order.id),affected=new Set();
const indices=new Set([...existing.map(row=>row.line_index),...order.lines.map((_,index)=>index)]);
for(const index of indices){
const previous=existing.find(row=>row.line_index===index),line=order.lines[index],pooled=line?.costMaterial?.mode==='pool';
if(!pooled){
if(previous?.active){
const next={...previous,active:0,revision:previous.revision+1};
ledger.run('UPDATE material_pool_usages SET active=0,revision=revision+1,updated=? WHERE id=?',env.now(),previous.id);
usageEvent(ledger,previous,usageValue(previous),usageValue(next),'整单修改释放',date,actor,env);affected.add(previous.pool_id);
}
continue;
}
if(line.materialAllocations)env.fail('锁价采购与日购料不能混用');
const id=poolId(line.costMaterial.poolId,env);if(id>order.date)env.fail('日购料日期不能晚于订单日期');
affected.add(id);if(previous?.active)affected.add(previous.pool_id);
if(previous){
const changed=!previous.active||previous.pool_id!==id||previous.mg!==line.quantity||previous.date!==date;
if(changed){
const next={...previous,pool_id:id,date,mg:line.quantity,active:1,revision:previous.revision+1};
ledger.run('UPDATE material_pool_usages SET pool_id=?,date=?,mg=?,active=1,revision=revision+1,updated=? WHERE id=?',id,date,line.quantity,env.now(),previous.id);
usageEvent(ledger,next,usageValue(previous),usageValue(next),'整单修改消耗',date,actor,env);
}
}else{
const row={id:randomUUID(),order_id:order.id,line_index:index,pool_id:id,date,mg:line.quantity,active:1,amount:null,owner_amount:null,assistant_amount:null,revision:1,created:env.now(),updated:env.now()};
ledger.run('INSERT INTO material_pool_usages VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)',...Object.values(row));
usageEvent(ledger,row,null,usageValue(row),'整单修改新增消耗',date,actor,env);
}
}
for(const id of affected)reconcile(ledger,id,date,actor,env,{skipJournalOrder:order.id});
return ledger.rawOrder(order.id);
}
function reassign(ledger,order,input,actor,env){return atomic(ledger,()=>{
activeActor(actor,env);
if(order.status!=='delivered')env.fail('只可为已交付订单调整日购料来源');
@@ -254,4 +287,4 @@ function reporting(ledger,query,env){
settlements:ledger.rows('SELECT * FROM material_pool_settlements WHERE date>=? AND date<=? ORDER BY date,created,rowid',from,to),
events:ledger.rows('SELECT * FROM material_pool_events WHERE date>=? AND date<=? ORDER BY date,created,rowid',from,to).map(row=>({...row,before_value:JSON.parse(row.before_value),after_value:JSON.parse(row.after_value)}))};
}
module.exports={initialize,prepareLine,readPool,list,purchase,purchaseDetail,editPurchase,deletePurchase,deliver,release,reassign,settle,reporting,settlement};
module.exports={initialize,prepareLine,readPool,list,purchase,purchaseDetail,editPurchase,deletePurchase,deliver,release,adjust,reassign,settle,reporting,settlement};