新增锁价与每日购料管理

支持锁价交付、每日加权成本、购料修改与软删除,完善付款归属、结算及利润重算。

244项测试和隔离浏览器验收通过;记录尚未实施的剩料跨日结转与当天资金结清需求。
This commit is contained in:
summer committed 2026-10-21 01:32:49 +08:00
1 parent 42d75a4968
commit 20bf6e7d24
23 files changed
+2330 -101

No files matched your search

+49 -4
View File
@@ -102,10 +102,40 @@
}
return result;
};
const costMaterial=perGram('material',line.costMaterial,'进货料价','料费总成本','请选择每克料价或料费总成本','每克料价需要克重,按件商品请填写料费总成本');
let costMaterial;
if(line.costMaterial?.mode==='pool'){
if(line.unit!=='g'||isSilver(line.name))throw new Error('日购料成本只支持按克黄金商品');
const poolId=String(line.costMaterial.poolId??'');
if(!/^\d{4}-\d{2}-\d{2}$/.test(poolId))throw new Error('请选择日购料日期');
costMaterial={mode:'pool',poolId};
const previewAmount=line.materialPool?.amount;
parts.material=Number.isSafeInteger(previewAmount)&&previewAmount>=0?(previewAmount/100).toFixed(2):'';
}else costMaterial=perGram('material',line.costMaterial,'进货料价','料费总成本','请选择每克料价或料费总成本','每克料价需要克重,按件商品请填写料费总成本');
const costLabor=perGram('labor',line.costLabor,'进货克工费','进货总工费','请选择进货克工费或总工费','进货克工费需要克重,按件商品请填写总工费');
return {...costs(parts),costMaterial,costLabor};
}
// 先用实际总金额与总毫克计算,展示用均价不可参与成本分摊。
function poolMaterialAmount(totalAmount,totalMg,quantityMg){
for(const value of [totalAmount,totalMg,quantityMg])if(!Number.isSafeInteger(value)||value<0||value>100000000000)throw new Error('日购料金额或克重无效');
if(!totalMg)throw new Error('日购料尚未登记克重');
const amount=(BigInt(totalAmount)*BigInt(quantityMg)+BigInt(totalMg)/2n)/BigInt(totalMg);
if(amount>100000000000n)throw new Error('日购料成本超出支持范围');
return Number(amount);
}
function priceLockCost(input){
const locked=fixed(input.lockedQuantity,3,'锁定克重'),actual=fixed(input.quantity,3,'实际克重');
const originalRate=fixed(input.buyRate,2,'原进货料价');
if(locked<=0n||actual<=0n)throw new Error('锁定克重和实际克重必须大于0');
if(originalRate<=0n)throw new Error('原进货料价必须大于0');
const used=actual<locked?actual:locked,extra=actual>locked?actual-locked:0n;
const extraRate=extra?fixed(input.extraBuyRate,2,'补重进货料价'):0n;
if(extra&&extraRate<=0n)throw new Error('补重进货料价必须大于0');
const amount=(quantity,rate)=>(quantity*rate+500n)/1000n;
const originalPurchase=amount(locked,originalRate),originalUsedCost=amount(used,originalRate),extraCost=amount(extra,extraRate);
if([originalPurchase,originalUsedCost+extraCost].some(value=>value>100000000000n))throw new Error('锁价料费成本超出支持范围');
return {lockedMg:Number(locked),finalMg:Number(actual),originalUsedMg:Number(used),extraMg:Number(extra),leftoverMg:Number(locked-used),
originalPurchase:Number(originalPurchase),originalUsedCost:Number(originalUsedCost),extraCost:Number(extraCost),materialCost:Number(originalUsedCost+extraCost),leftoverCost:Number(originalPurchase-originalUsedCost)};
}
// 订单内部对账用,输入为账本已保存的整数分;预估费用与实际支出分开。
function orderCosts(order){
const empty=()=>({goods:0,goodsMissing:0,paidFees:0,pendingFees:0,pendingMissing:0,total:0,missing:0});
@@ -121,8 +151,23 @@
for(const [i,line] of order.lines.entries()){
const payers=costPayers(line.costPayers);
const parts=line.costParts?Object.entries(costLabels).map(([key,name])=>({key,name,amount:line.costParts[key]??null})):[{key:'total',name:'货品成本(原单未分项)',amount:line.cost??null}];
for(const part of parts)add({kind:'goods',name:part.name,source:`商品 ${i+1} · ${line.name}`,amount:part.amount,payer:payers?.[part.key]??line.payer,excluded:goodsExcluded,
calculation:part.key==='material'&&line.costMaterial?.mode==='gram'?{rate:line.costMaterial.rate,quantity:line.quantity}:part.key==='labor'&&line.costLabor?.mode==='gram'?{rate:line.costLabor.rate,quantity:line.quantity}:null});
for(const part of parts){
if(part.key==='material'&&line.costMaterial?.mode==='pool'){
const pool=line.materialPool,allocations=pool?.allocations;
if(part.amount===null||!allocations?.length){
add({kind:'goods',component:'material',name:part.name,source:`商品 ${i+1} · ${line.name} · ${line.costMaterial.poolId}日购料待补`,amount:null,payer:payers?.material??line.payer,excluded:goodsExcluded,materialPoolId:line.costMaterial.poolId,materialPoolRevision:pool?.revision,poolPending:true,calculation:null});
}else{
if(!Array.isArray(allocations)||allocations.some(item=>!['owner','assistant'].includes(item.payer)||!Number.isSafeInteger(item.amount)||item.amount<0)||allocations.reduce((total,item)=>total+item.amount,0)!==part.amount)throw new Error('日购料分项成本记录无效');
for(const item of allocations)add({kind:'goods',component:'material',name:part.name,source:`商品 ${i+1} · ${line.name} · ${line.costMaterial.poolId}日购料`,amount:item.amount,payer:item.payer,excluded:goodsExcluded,materialPoolId:line.costMaterial.poolId,materialPoolRevision:pool?.revision,calculation:{rate:pool.averageRate,quantity:line.quantity,weighted:true}});
}
}else if(part.key==='material'&&line.materialAllocations){
const allocations=line.materialAllocations;
if(!Array.isArray(allocations)||!allocations.length||allocations.some(item=>!['original','extra'].includes(item.source)||!['owner','assistant'].includes(item.payer)||!Number.isSafeInteger(item.amount)||item.amount<0||!Number.isSafeInteger(item.quantity)||item.quantity<=0||!Number.isSafeInteger(item.rate)||item.rate<=0)||allocations.reduce((total,item)=>total+item.amount,0)!==part.amount)throw new Error('锁价分项料费记录无效');
for(const item of allocations)add({kind:'goods',component:'material',name:part.name,source:`商品 ${i+1} · ${line.name} · ${item.source==='original'?'原锁价采购':'补重采购'}`,amount:item.amount,payer:item.payer,excluded:goodsExcluded,
priceLockSource:item.source,calculation:{rate:item.rate,quantity:item.quantity}});
}else add({kind:'goods',name:part.name,source:`商品 ${i+1} · ${line.name}`,amount:part.amount,payer:payers?.[part.key]??line.payer,excluded:goodsExcluded,
calculation:part.key==='material'&&line.costMaterial?.mode==='gram'?{rate:line.costMaterial.rate,quantity:line.quantity}:part.key==='labor'&&line.costLabor?.mode==='gram'?{rate:line.costLabor.rate,quantity:line.quantity}:null});
}
}
// 原费用与冲回行成对抵消,只列最新有效支出,保留原流水供追溯。
const corrected=new Set((order.corrections||[]).filter(c=>c.type==='expense').map(c=>c.target));
@@ -141,6 +186,6 @@
}
return {byPayer,total,rows,goodsExcluded,pendingCount:(order.pendingFees||[]).length};
}
const api={calculate,cents,isSilver,costs,costLabels,costPayers,charges,sale,orderTotal,purchaseCosts,orderCosts};
const api={calculate,cents,isSilver,costs,costLabels,costPayers,charges,sale,orderTotal,purchaseCosts,poolMaterialAmount,priceLockCost,orderCosts};
if(typeof module==='object'&&module.exports)module.exports=api;else root.LedgerCalc=api;
})(typeof globalThis==='object'?globalThis:this);