支持回收单修改取消、按归属人优先用料及锁价交付改单
This commit is contained in:
1 parent
0e146739bc
commit
19b1b0263c
41 files changed
+1814
-203
No files matched your search
@@ -27,6 +27,11 @@ function priceLockPoolPreview(date,quantity){
|
||||
const rows=known?materialPoolRows():[],pool=rows.find(value=>value.id===date);
|
||||
if(priceLockPoolError||!known)throw new Error(priceLockPoolError||'正在读取每日料池,请稍候。');
|
||||
if(!currentMaterialRolling())throw new Error('请先启用统一料池,再选择当日加权料价。');
|
||||
if(materialPoolHasPersonal(pool)){
|
||||
const form=$('#price-lock-create-form'),input=materialLockQuoteInput(form),quote=materialQuoteFor(form,input,'lock');if(!quote.result)throw new Error(quote.error||'正在按订单归属核对个人回收料与公共补足,请稍候。');
|
||||
const row=quote.result.entries.find(row=>row.lineIndex===0),balance=quote.result.pools.find(row=>row.poolId===date),mg=lockQuantityMg(quantity);if(!row||!balance)throw new Error('本次料费来源未完整返回,请刷新料池。');
|
||||
const remainingMg=balance.before?.eligibleRemainingMg??0,shortageMg=Math.max(0,mg-remainingMg);return {pool:{...pool,revision:balance.revision},mg,amount:row.amount,amounts:{0:row.amount},covered:row.amount!==null,remainingMg,shortageMg,allocations:row.allocations,materialSources:row.materialSources,balance};
|
||||
}
|
||||
if(!pool||!pool.totalMg)throw new Error(date+' 尚无可用材料。请先登记购料,再保存锁价。');
|
||||
if(pool.totalAmount==null)throw new Error('料池成本尚未补齐,请先核对材料记录,再保存锁价。');
|
||||
const mg=lockQuantityMg(quantity);
|
||||
@@ -56,11 +61,12 @@ function syncPriceLockMaterial(form){
|
||||
form.querySelector('[name="material_pool_date"]').value=date;rate.value='';estimate.textContent='待确定';payers.innerHTML='';poolRoot.dataset.revision='';poolRoot.dataset.ready='false';note.classList.remove('danger-text');
|
||||
try{
|
||||
if(date!==state.today)throw new Error('当日加权料池只支持今天锁价;补录其他日期请使用手动填写料价。');
|
||||
const preview=priceLockPoolPreview(date,quantity),{pool,amount,remainingMg,shortageMg,allocations}=preview;
|
||||
rate.value=pool.averageRate==null?'':(pool.averageRate/100).toFixed(2);poolRoot.dataset.revision=pool.revision;
|
||||
note.textContent=`${date} 料池 ${grams(pool.totalMg)}g,已用/预留 ${grams(preview.usedMg)}g,可用 ${grams(remainingMg)}g;本次预留 ${grams(preview.mg)}g。`;
|
||||
const preview=priceLockPoolPreview(date,quantity),{pool,amount,remainingMg,shortageMg,allocations,materialSources}=preview;
|
||||
rate.value=materialSources?.length?materialSources.map(source=>materialSourceName(source)+' '+(source.averageRate==null?'待补':(source.averageRate/100).toFixed(2))).join(';'):pool.averageRate==null?'':(pool.averageRate/100).toFixed(2);poolRoot.dataset.revision=pool.revision;
|
||||
note.textContent=preview.balance?`${date} · ${materialPayerName(materialFormMember(form))}先用本人回收料,公共补足;${materialQuoteBalanceText(preview.balance)}本次预留 ${grams(preview.mg)}g。`:`${date} 料池 ${grams(pool.totalMg)}g,已用/预留 ${grams(preview.usedMg)}g,可用 ${grams(remainingMg)}g;本次预留 ${grams(preview.mg)}g。`;
|
||||
if(shortageMg){note.textContent+=`还缺 ${grams(shortageMg)}g,请先补足购料后再保存锁价。`;note.classList.add('danger-text');return;}
|
||||
estimate.textContent='¥'+yuan(amount);payers.innerHTML='<strong>自动来源:</strong>'+priceLockAllocationHTML(allocations);poolRoot.dataset.ready='true';
|
||||
if(amount==null){note.textContent+='材料成本待补齐,请先核对后保存锁价。';note.classList.add('danger-text');return;}
|
||||
estimate.textContent='¥'+yuan(amount);payers.innerHTML=(materialSources?.length?materialSourceSegmentsHTML(materialSources):'')+'<strong>自动付款来源:</strong>'+priceLockAllocationHTML(allocations);poolRoot.dataset.ready='true';
|
||||
}catch(err){note.textContent=err.message;note.classList.add('danger-text');}
|
||||
}
|
||||
async function loadPriceLockPoolChoices(force=false){
|
||||
@@ -99,19 +105,33 @@ function priceLockCardFeesHTML(lock){
|
||||
function priceLockDialog(){let dialog=$('#price-lock-dialog');if(!dialog){dialog=document.createElement('dialog');dialog.id='price-lock-dialog';document.body.append(dialog);}return dialog;}
|
||||
function priceLockFacts(lock){
|
||||
const pool=priceLockUsesPool(lock),material=lock.materialPool;
|
||||
return `<div class="grid three price-lock-facts"><div>锁价日期<strong>${e(lock.date)}</strong></div><div>原锁克重<strong>${grams(lock.locked_mg)}g</strong></div><div>锁定销售金价<strong>¥${yuan(lock.sell_rate)}/克</strong></div><div>${lock.status==='delivered'?'锁价及交付阶段净收款':'客户实际已付净额'}<strong>¥${yuan(lock.netReceived)}</strong>${lock.status==='delivered'?'<small>后续收退款请查看销售单。</small>':''}</div><div>原锁料费 · ${pool?'当日加权料池':'手动独立购料'}<strong>¥${yuan(lock.material_amount)}</strong><small>${pool?`${e(material?.date||lock.material_pool_id||lock.date)}料池,加权均价约 ¥${yuan(material?.averageRate??lock.buy_rate)}/克`:`${e(priceLockPayerName(lock.buy_payer))}支付,进货料价 ¥${yuan(lock.buy_rate)}/克`}</small>${pool?`<small>${e(priceLockAllocationText(priceLockMaterialAllocations(lock)))}</small><small>${material?.frozen?'已冻结锁价日成本':'当天补料继续加权'},不重复记购料付款。</small>`:''}</div><div>状态<strong>${lock.status==='pending'?'待交付,不计利润':'已交付'}</strong></div></div>`+priceLockCardFeesHTML(lock);
|
||||
return `<div class="grid three price-lock-facts"><div>锁价日期<strong>${e(lock.date)}</strong></div><div>原锁克重<strong>${grams(lock.locked_mg)}g</strong></div><div>锁定销售金价<strong>¥${yuan(lock.sell_rate)}/克</strong></div><div>${lock.status==='delivered'?'锁价及交付阶段净收款':'客户实际已付净额'}<strong>¥${yuan(lock.netReceived)}</strong>${lock.status==='delivered'?'<small>后续收退款请查看销售单。</small>':''}</div><div>原锁料费 · ${pool?'当日加权料池':'手动独立购料'}<strong>¥${yuan(lock.material_amount)}</strong><small>${pool?material?.materialSources?.length?e(materialSourceSegmentsText(material.materialSources)):`${e(material?.date||lock.material_pool_id||lock.date)}料池,加权均价约 ¥${yuan(material?.averageRate??lock.buy_rate)}/克`:`${e(priceLockPayerName(lock.buy_payer))}支付,进货料价 ¥${yuan(lock.buy_rate)}/克`}</small>${pool?`<small>${e(priceLockAllocationText(priceLockMaterialAllocations(lock)))}</small><small>${material?.frozen?'已冻结锁价日成本':'当天补料继续加权'},不重复记购料付款。</small>`:''}</div><div>状态<strong>${lock.status==='pending'?'待交付,不计利润':'已交付'}</strong></div></div>`+priceLockCardFeesHTML(lock);
|
||||
}
|
||||
function priceLockSplitMaterialSources(pool,mg){
|
||||
let left=mg;const taken=[],rest=[];if(!Array.isArray(pool.materialSources)||pool.materialSources.reduce((sum,source)=>sum+source.quantity,0)!==pool.quantity||pool.materialSources.reduce((sum,source)=>sum+source.amount,0)!==pool.amount)throw new Error('原锁分来源克重或成本未完整读到,请重新读取。');
|
||||
for(const source of pool.materialSources){
|
||||
if(!['quantity','amount','basisAmount','basisMg','prefixMg','prefixAmount'].every(key=>Number.isSafeInteger(source[key])&&source[key]>=0)||!source.basisMg||!Array.isArray(source.allocations)||source.allocations.reduce((sum,part)=>sum+part.amount,0)!==source.amount)throw new Error('原锁分来源成本未完整读到,请重新读取。');
|
||||
const n=Math.min(left,source.quantity);left-=n;if(!n){rest.push({...source});continue;}
|
||||
if(n===source.quantity){taken.push({...source});continue;}
|
||||
const amount=LedgerCalc.poolMaterialAmount(source.basisAmount,source.basisMg,source.prefixMg+n)-source.prefixAmount,paid=[],unpaid=[];let money=amount;
|
||||
for(const part of source.allocations){const value=Math.min(money,part.amount);if(value)paid.push({...part,amount:value});if(part.amount>value)unpaid.push({...part,amount:part.amount-value});money-=value;}
|
||||
if(money||amount<0||amount>source.amount)throw new Error('原锁分来源分币未完整读到,请重新读取。');if(!amount&&!paid.length)paid.push({payer:source.allocations[0]?.payer||'owner',amount:0});
|
||||
const batches=[],remainingBatches=[];let weight=n;for(const batch of source.batches||[]){const value=Math.min(weight,batch.mg);if(value)batches.push({...batch,mg:value});if(batch.mg>value)remainingBatches.push({...batch,mg:batch.mg-value});weight-=value;}
|
||||
taken.push({...source,quantity:n,amount,allocations:paid,batches});rest.push({...source,quantity:source.quantity-n,amount:source.amount-amount,allocations:unpaid,batches:remainingBatches,prefixMg:source.prefixMg+n,prefixAmount:source.prefixAmount+amount});
|
||||
}
|
||||
if(left)throw new Error('实际耗料超过原锁来源克重。');return {taken,rest};
|
||||
}
|
||||
function priceLockDeliveryCost(lock,quantity,extraBuyRate){
|
||||
if(!priceLockUsesPool(lock))return LedgerCalc.priceLockCost({lockedQuantity:grams(lock.locked_mg),quantity,buyRate:(lock.buy_rate/100).toFixed(2),extraBuyRate});
|
||||
const pool=lock.materialPool,finalMg=lockQuantityMg(quantity),originalUsedMg=Math.min(finalMg,lock.locked_mg),extraMg=Math.max(0,finalMg-lock.locked_mg),leftoverMg=lock.locked_mg-originalUsedMg;
|
||||
if(!pool||!['basisAmount','basisMg','prefixMg','prefixAmount','amount'].every(key=>Number.isSafeInteger(pool[key])&&pool[key]>=0)||!pool.basisMg)throw new Error('原锁料池成本来源未完整读到,请重新读取锁价单。');
|
||||
const originalUsedCost=LedgerCalc.poolMaterialAmount(pool.basisAmount,pool.basisMg,pool.prefixMg+originalUsedMg)-pool.prefixAmount;
|
||||
const split=pool.materialSources?priceLockSplitMaterialSources(pool,originalUsedMg):null,originalUsedCost=split?split.taken.reduce((sum,source)=>sum+source.amount,0):LedgerCalc.poolMaterialAmount(pool.basisAmount,pool.basisMg,pool.prefixMg+originalUsedMg)-pool.prefixAmount;
|
||||
const extraRate=extraMg?LedgerCalc.cents(extraBuyRate,'补重进货料价'):0;if(extraMg&&!extraRate)throw new Error('补重进货料价必须大于0。');
|
||||
const extraCost=extraMg?Number((BigInt(extraMg)*BigInt(extraRate)+500n)/1000n):0;
|
||||
let left=originalUsedCost;const allocations=[];
|
||||
for(const part of priceLockMaterialAllocations(lock)){const amount=Math.min(left,part.amount);if(amount||!originalUsedCost&&!allocations.length)allocations.push({payer:part.payer,amount});left-=amount;}
|
||||
for(const part of split?split.taken.flatMap(source=>source.allocations):priceLockMaterialAllocations(lock)){const amount=Math.min(left,part.amount);if(amount||!originalUsedCost&&!allocations.length)allocations.push({payer:part.payer,amount});left-=amount;}
|
||||
if(left||originalUsedCost<0||originalUsedCost>pool.amount)throw new Error('原锁料池成本分摊不完整,请重新读取锁价单。');
|
||||
return {lockedMg:lock.locked_mg,finalMg,originalUsedMg,extraMg,leftoverMg,originalPurchase:pool.amount,originalUsedCost,extraCost,materialCost:originalUsedCost+extraCost,leftoverCost:pool.amount-originalUsedCost,allocations};
|
||||
return {lockedMg:lock.locked_mg,finalMg,originalUsedMg,extraMg,leftoverMg,originalPurchase:pool.amount,originalUsedCost,extraCost,materialCost:originalUsedCost+extraCost,leftoverCost:pool.amount-originalUsedCost,allocations,...(split?{materialSources:split.taken,leftoverMaterialSources:split.rest}:{})};
|
||||
}
|
||||
async function renderPriceLocks(){
|
||||
if(priceLockDeliveryId)return await renderPriceLockDelivery(priceLockDeliveryId);
|
||||
@@ -163,14 +183,14 @@ function syncPriceLockDelivery(){
|
||||
get('cost-material').readOnly=true;row.querySelector('.line-top button')?.remove();
|
||||
row.querySelector('.purchase-material .purchase-original').hidden=true;
|
||||
const poolMode=priceLockUsesPool(lock),formula=row.querySelector('.purchase-material .purchase-formula');
|
||||
formula.textContent=costs?poolMode?`原锁按 ${lock.material_pool_id||lock.date} 料池耗用 ${grams(costs.originalUsedMg)}g,料费 ¥${yuan(costs.originalUsedCost)}${costs.extraMg?';补重 '+grams(costs.extraMg)+'g × '+lockFormValue(form,'extra_rate')+'元/克,料费 ¥'+yuan(costs.extraCost):''};合计 ¥${yuan(costs.materialCost)}`:`原锁使用 ${grams(costs.originalUsedMg)}g × ${yuan(lock.buy_rate)}元/克${costs.extraMg?';补重 '+grams(costs.extraMg)+'g × '+lockFormValue(form,'extra_rate')+'元/克':''} = ${yuan(costs.materialCost)}元`:poolMode?'原锁料费沿用料池实际分币金额;有补重时,请填写补重进货料价。':'料费按原锁进货价计算;有补重时,请先填写补重进货料价。';
|
||||
formula.textContent=costs?poolMode?`${costs.materialSources?.length?materialSourceSegmentsText(costs.materialSources):'原锁按 '+(lock.material_pool_id||lock.date)+' 料池耗用 '+grams(costs.originalUsedMg)+'g,料费 ¥'+yuan(costs.originalUsedCost)}${costs.extraMg?';补重 '+grams(costs.extraMg)+'g × '+lockFormValue(form,'extra_rate')+'元/克,料费 ¥'+yuan(costs.extraCost):''};合计 ¥${yuan(costs.materialCost)}`:`原锁使用 ${grams(costs.originalUsedMg)}g × ${yuan(lock.buy_rate)}元/克${costs.extraMg?';补重 '+grams(costs.extraMg)+'g × '+lockFormValue(form,'extra_rate')+'元/克':''} = ${yuan(costs.materialCost)}元`:poolMode?'原锁料费沿用料池实际分币金额;有补重时,请填写补重进货料价。':'料费按原锁进货价计算;有补重时,请先填写补重进货料价。';
|
||||
if(poolMode){let payers=row.querySelector('.lock-pool-delivery-payers');if(!payers){formula.insertAdjacentHTML('afterend','<div class="lock-pool-delivery-payers price-lock-material-payers small"></div>');payers=row.querySelector('.lock-pool-delivery-payers');}payers.innerHTML='<strong>原锁耗用来源:</strong>'+priceLockAllocationHTML(costs?.allocations||priceLockMaterialAllocations(lock));}
|
||||
get('goldPrice').closest('label').firstChild.textContent='原锁销售金价(元/克,固定)';get('cost-material').closest('label').firstChild.textContent='实际耗用料费合计(元,自动)';get('cost-materialPayer').closest('label').firstChild.textContent='原锁料费付款人';get('cost-materialMode').querySelector('option[value="total"]').textContent='原锁+补重自动核算';get('pocket').required=true;get('pocket').querySelector('option[value=""]').textContent='请选择实际黄金货盘';
|
||||
get('cost-materialPayer').closest('label').hidden=poolMode;
|
||||
const originalStock=form.querySelector('[name="original_stock"]');originalStock.required=!poolMode;originalStock.disabled=poolMode;if(poolMode)originalStock.value='stocked';
|
||||
const feeDate=form.querySelector('[name="fees_date"]');feeDate.value=lockFormValue(form,'date');feeDate.readOnly=true;
|
||||
syncFeeRows();const d=collectPriceLockDelivery();
|
||||
$('#lock-material-breakdown').textContent=costs?`原锁${poolMode?'预留':'料费已付'} ¥${yuan(lock.material_amount)};本单耗用料费 ¥${yuan(costs.materialCost)}${costs.leftoverMg?';'+(poolMode?'解除预留回池 ':'余料 ')+grams(costs.leftoverMg)+'g,成本 ¥'+yuan(costs.leftoverCost):''}${poolMode?';已有购料付款不重复登记。':''}`:extra?'请填写补重料价,计算本次实际耗用成本。':'填写最终交付克重,自动计算实际耗料。';
|
||||
$('#lock-material-breakdown').textContent=costs?`原锁${poolMode?'预留':'料费已付'} ¥${yuan(lock.material_amount)};本单耗用料费 ¥${yuan(costs.materialCost)}${costs.leftoverMg?';'+(poolMode?'解除预留回池 ':'余料 ')+grams(costs.leftoverMg)+'g,成本 ¥'+yuan(costs.leftoverCost)+(costs.leftoverMaterialSources?.length?'('+materialSourceSegmentsText(costs.leftoverMaterialSources)+')':''):''}${poolMode?';已有购料付款不重复登记。':''}`:extra?'请填写补重料价,计算本次实际耗用成本。':'填写最终交付克重,自动计算实际耗料。';
|
||||
const original=d.inventory.original,pocket=d.inventory.pocket;
|
||||
$('#lock-stock-note').textContent=costs&&original&&pocket?`${poolMode?'原锁金料已在料池预留,不重复入库;':original==='receive'?'原锁 '+grams(lock.locked_mg)+'g 先入库;':'原锁金料此前已入库;'}${costs.extraMg?(d.inventory.extra==='receive'?'补重 '+grams(costs.extraMg)+'g 先入库;':d.inventory.extra==='stocked'?'补重金料此前已入库;':'请选择补重金料是否已入库;'):''}从「${pocket}」出库 ${grams(costs.finalMg)}g。${costs.leftoverMg?poolMode?'少重 '+grams(costs.leftoverMg)+'g 解除预留回到料池,实物留在货盘,不重复入库或扣料。':'余料 '+grams(costs.leftoverMg)+'g 留在该货盘,按原进货价计成本,不重复入库。':''}`:'选择实际货盘和金料入库情况后,显示本次库存动作。';
|
||||
let difference=null;
|
||||
@@ -216,7 +236,7 @@ async function submitPriceLockForm(form){
|
||||
lockQuantityMg(data.line.quantity);if(LedgerCalc.cents(data.line.goldPrice)<=0)throw new Error('销售金价须大于0。');
|
||||
if(data.material.source==='pool'){
|
||||
if(data.date!==state.today)throw new Error('当日加权料池只支持今天锁价;补录其他日期请使用手动填写料价。');
|
||||
const preview=priceLockPoolPreview(data.date,data.line.quantity);if(!preview.covered)throw new Error(`当日料池还缺 ${grams(preview.shortageMg)}g,请先补足购料后再保存锁价。`);
|
||||
const preview=priceLockPoolPreview(data.date,data.line.quantity);if(!preview.covered)throw new Error(preview.shortageMg?`当日料池还缺 ${grams(preview.shortageMg)}g,请先补足购料后再保存锁价。`:'材料成本待补齐,请先核对后保存锁价。');
|
||||
if(!data.material.revision||data.material.revision!==preview.pool.revision)throw new Error('请刷新料池,核对当前成本与自动付款来源后再保存。');
|
||||
}else if(LedgerCalc.cents(data.material.rate)<=0)throw new Error('原进货料价须大于0。');
|
||||
LedgerCalc.cents(data.payment.amount,'客户实际付款');LedgerCalc.orderTotal([LedgerCalc.sale(data.line).sale],LedgerCalc.cents(data.discount),'yuan');
|
||||
|
||||
Reference in new issue
Block a user