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

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

+33 -1
View File
@@ -122,6 +122,38 @@
if(amount>100000000000n)throw new Error('日购料成本超出支持范围');
return Number(amount);
}
// 同池先按完整耗用顺序累计取整,再取相邻差额;编辑沿用原行身份,新增行追加。
function poolMaterialPreview(pool,entries,{orderId=null,allUsages=pool.usages||[]}={}){
const valid=value=>Number.isSafeInteger(value)&&value>=0&&value<=100000000000;
if(!valid(pool.totalAmount)||!valid(pool.totalMg)||!Array.isArray(entries)||!Array.isArray(pool.usages)||!Array.isArray(allUsages))throw new Error('日购料预估记录无效,请刷新料价');
const indices=new Set();
for(const entry of entries){
if(!Number.isSafeInteger(entry.lineIndex)||entry.lineIndex<0||indices.has(entry.lineIndex)||!valid(entry.mg)||!entry.mg)throw new Error('日购料预估克重或商品行无效');
indices.add(entry.lineIndex);
}
const originals=orderId?allUsages.filter(usage=>usage.order_id===orderId):[];
const usages=pool.usages.filter(usage=>usage.active&&usage.order_id!==orderId).map(usage=>({...usage}));
const usedMg=usages.reduce((total,usage)=>total+usage.mg,0),wantedMg=entries.reduce((total,entry)=>total+entry.mg,0);
const covered=pool.totalMg>0&&usedMg+wantedMg<=pool.totalMg,amounts=Object.fromEntries(entries.map(entry=>[entry.lineIndex,null]));
for(const entry of entries){
const original=originals.find(usage=>usage.line_index===entry.lineIndex);
usages.push({...original,mg:entry.mg,previewIndex:entry.lineIndex,appended:!original});
}
for(const usage of usages){
if(!valid(usage.mg)||!usage.mg||!usage.appended&&(!Number.isSafeInteger(usage.fifo_sequence)||usage.fifo_sequence<1||typeof usage.created!=='string'||!usage.created))throw new Error('日购料耗用顺序尚未读到,请刷新料价');
}
usages.sort((a,b)=>Number(!!a.appended)-Number(!!b.appended)||(a.appended?a.previewIndex-b.previewIndex:a.created<b.created?-1:a.created>b.created?1:a.fifo_sequence-b.fifo_sequence));
if(covered){
let cumulativeMg=0,previousAmount=0;
for(const usage of usages){
cumulativeMg+=usage.mg;
const cumulativeAmount=poolMaterialAmount(pool.totalAmount,pool.totalMg,cumulativeMg);
if(usage.previewIndex!==undefined)amounts[usage.previewIndex]=cumulativeAmount-previousAmount;
previousAmount=cumulativeAmount;
}
}
return {amounts,usedMg,wantedMg,covered};
}
function priceLockCost(input){
const locked=fixed(input.lockedQuantity,3,'锁定克重'),actual=fixed(input.quantity,3,'实际克重');
const originalRate=fixed(input.buyRate,2,'原进货料价');
@@ -186,6 +218,6 @@
}
return {byPayer,total,rows,goodsExcluded,pendingCount:(order.pendingFees||[]).length};
}
const api={calculate,cents,isSilver,costs,costLabels,costPayers,charges,sale,orderTotal,purchaseCosts,poolMaterialAmount,priceLockCost,orderCosts};
const api={calculate,cents,isSilver,costs,costLabels,costPayers,charges,sale,orderTotal,purchaseCosts,poolMaterialAmount,poolMaterialPreview,priceLockCost,orderCosts};
if(typeof module==='object'&&module.exports)module.exports=api;else root.LedgerCalc=api;
})(typeof globalThis==='object'?globalThis:this);