日购料垫款按采购先后扣减并迁移历史归属

This commit is contained in:
summer committed 2026-10-23 00:14:29 +08:00
1 parent 34781b6cd1
commit 499b84a7a6
13 files changed
+608 -90

No files matched your search

+7 -4
View File
@@ -152,9 +152,9 @@ class Ledger {
o.sync=this.one('SELECT revision,done_revision,error,updated FROM sync WHERE entity=?','order:'+id)||null;
return o;
}
validateLines(lines,historicalLines=[],businessDate=null){
validateLines(lines,historicalLines=[],businessDate=null,{orderId=null}={}){
if(!Array.isArray(lines)||!lines.length||lines.length>60)fail('每单需要1~60行商品');
return lines.map((l,i)=>{
const prepared=lines.map((l,i)=>{
const unit=pick(l.unit,['g','件'],'单位');const qty=unit==='g'?weight(l.quantity,`第${i+1}行克重`):fixed(l.quantity,1,'件数')/10;
if(qty<=0||(unit==='件'&&!Number.isInteger(qty)))fail('克重必须大于0,件数必须为正整数');
const pocket=l.pocket?pick(l.pocket,POCKETS,'货盘'):'';if(pocket&&unit!=='g')fail('货盘库存按克重管理,按件商品请选外配/不扣黄金');
@@ -169,8 +169,9 @@ class Ledger {
const costPayers=Calc.costPayers(l.costPayers===undefined?historicalLines?.[i]?.costPayers:l.costPayers);
const cost=breakdown?breakdown.total:(l.cost===''||l.cost==null?null:money(l.cost,'货品成本'));
const priceNote=pricing?`金价 ${(pricing.goldPrice/100).toFixed(2)}元/克 + 工费 ${(pricing.laborRate/100).toFixed(2)}元/${pricing.laborMode==='gram'?'克':`件 × ${pricing.pieces}件`}`:text(l.priceNote,'客户单价说明',100,true);
return MaterialPools.prepareLine(this,{name,code:text(l.code,'商品编号',30,true),unit,quantity:qty,pocket,payer:pick(l.payer,['owner','assistant'],'成本垫资人'),...(costPayers?{costPayers}:{}),cost,...(breakdown?{costParts:breakdown.parts,costMaterial:breakdown.costMaterial,costLabor:breakdown.costLabor}:{}),sale:priced.sale,baseSale:priced.baseSale,charges:priced.charges,pricingMode:mode,...(pricing?{goldPrice:pricing.goldPrice,laborRate:pricing.laborRate,laborMode:pricing.laborMode,pieces:pricing.pieces}:{}),priceNote},businessDate,priceLockEnv);
return {name,code:text(l.code,'商品编号',30,true),unit,quantity:qty,pocket,payer:pick(l.payer,['owner','assistant'],'成本垫资人'),...(costPayers?{costPayers}:{}),cost,...(breakdown?{costParts:breakdown.parts,costMaterial:breakdown.costMaterial,costLabor:breakdown.costLabor}:{}),sale:priced.sale,baseSale:priced.baseSale,charges:priced.charges,pricingMode:mode,...(pricing?{goldPrice:pricing.goldPrice,laborRate:pricing.laborRate,laborMode:pricing.laborMode,pieces:pricing.pieces}:{}),priceNote};
});
return MaterialPools.prepareLines(this,prepared,businessDate,priceLockEnv,{orderId});
}
paperNumber(date,allocate=false){
day(date);const prefix=date.replaceAll('-','');let seq=this.meta('paper-sequence',0);
@@ -370,11 +371,11 @@ class Ledger {
const result=Calc.purchaseCosts({name:l.name,costParts:value.parts||value,costMaterial:value.costMaterial,costLabor:value.costLabor,unit:l.unit,quantity:l.unit==='g'?(l.quantity/1000).toFixed(3):l.quantity});if(result.total===null&&result.costMaterial.mode!=='pool')fail('请补齐每项成本;没有的项目填0');
l.costParts=result.parts;l.cost=result.total;l.costMaterial=result.costMaterial;l.costLabor=result.costLabor;l.payer=payer;if(costPayers)l.costPayers=costPayers;
if(l.costMaterial.mode==='pool'){
MaterialPools.prepareLine(this,l,o.date,priceLockEnv);
if(['labor','accessories','other'].some(key=>l.costParts[key]==null))fail('请补齐每项成本;没有的项目填0');
}else delete l.materialPool;
}else{if(l.costParts)fail('这行已拆分成本,请逐项填写');l.cost=money(value,'货品成本');}
});
MaterialPools.prepareLines(this,o.lines,o.date,priceLockEnv,{orderId:id});
this.run('UPDATE orders SET lines=? WHERE id=?',JSON.stringify(o.lines),id);
} else if(action==='complete'){
if(o.status!=='confirmed')fail('只有待交付订单可以完成交付',409);
@@ -475,6 +476,8 @@ class Ledger {
deliverPriceLock(id,input,actor){return PriceLocks.deliver(this,id,input,actor,priceLockEnv);}
priceLockReporting(query={}){return PriceLocks.reporting(this,query,priceLockEnv);}
createMaterialPurchase(input,actor){return MaterialPools.purchase(this,input,actor,priceLockEnv);}
materialPoolAllocationStatus(){return MaterialPools.allocationStatus(this);}
migrateMaterialPoolAllocation(actor,options={}){return MaterialPools.migrateAllocation(this,actor,priceLockEnv,options);}
materialPurchaseDetail(id){return MaterialPools.purchaseDetail(this,id,priceLockEnv);}
editMaterialPurchase(id,input,actor){return MaterialPools.editPurchase(this,id,input,actor,priceLockEnv);}
deleteMaterialPurchase(id,input,actor){return MaterialPools.deletePurchase(this,id,input,actor,priceLockEnv);}