支持多商品锁价及待交付修改作废审批

This commit is contained in:
summer committed 2026-11-08 02:22:58 +08:00
1 parent 1cbcdc728c
commit 337b2a709f
28 files changed
+1187 -344

No files matched your search

+12 -7
View File
@@ -1,5 +1,6 @@
'use strict';
// 审批依据保留原始来源,结果比较只保留业务值;没有池锁时沿用旧申请哈希。
const LockLines=require('./price-lock-lines');
const exists=(ledger,table)=>!!ledger.one("SELECT name FROM sqlite_master WHERE type='table' AND name=?",table);
const allocations=value=>(typeof value==='string'?JSON.parse(value):value??[]).map(({payer,amount})=>({payer,amount}));
function rows(ledger,{poolId=null,priceLockId=null}={}){
@@ -24,8 +25,8 @@ function effect(ledger,poolIds=null){
if(!reservations.length&&!returns.length)return {};
const stable=(key,value)=>['id','revision','created','updated','actor','source_sequence','source_id'].includes(key)?undefined:value;
return {priceLockPool:{
reservations:reservations.map(row=>({priceLockId:row.price_lock_id,poolId:row.pool_id,mg:row.mg,amount:row.amount,allocations:allocations(row.allocations),snapshot:JSON.parse(JSON.stringify(JSON.parse(row.snapshot),stable))})).sort((a,b)=>a.priceLockId.localeCompare(b.priceLockId)),
returns:returns.map(row=>({priceLockId:row.price_lock_id,date:row.date,mg:row.mg,amount:row.amount,allocations:allocations(row.allocations),originPoolId:row.origin_pool_id,...(row.kind!==undefined?{kind:row.kind}:{}),...(row.same_day!==undefined?{sameDay:row.same_day}:{})})).sort((a,b)=>a.priceLockId.localeCompare(b.priceLockId)||String(a.kind??'').localeCompare(String(b.kind??''))||a.date.localeCompare(b.date))
reservations:reservations.map(row=>({priceLockId:row.price_lock_id,lineIndex:row.line_index,poolId:row.pool_id,mg:row.mg,amount:row.amount,allocations:allocations(row.allocations),snapshot:JSON.parse(JSON.stringify(JSON.parse(row.snapshot),stable))})).sort((a,b)=>a.priceLockId.localeCompare(b.priceLockId)||a.lineIndex-b.lineIndex),
returns:returns.map(row=>({priceLockId:row.price_lock_id,lineIndex:row.line_index,date:row.date,mg:row.mg,amount:row.amount,allocations:allocations(row.allocations),originPoolId:row.origin_pool_id,...(row.kind!==undefined?{kind:row.kind}:{}),...(row.same_day!==undefined?{sameDay:row.same_day}:{})})).sort((a,b)=>a.priceLockId.localeCompare(b.priceLockId)||a.lineIndex-b.lineIndex||String(a.kind??'').localeCompare(String(b.kind??''))||a.date.localeCompare(b.date))
}};
}
function finance(ledger){return ledger.scope(null,()=>financeInternal(ledger),null);}
@@ -33,15 +34,19 @@ function financeInternal(ledger){
const state=rows(ledger),result=new Map();
for(const reservation of state.reservations){
const lock=ledger.one('SELECT * FROM price_locks WHERE id=?',reservation.price_lock_id);if(!lock)continue;
const delivery=lock.delivery?JSON.parse(lock.delivery):null,order=lock.final_order_id?ledger.rawOrder(lock.final_order_id):null;
const original=order?.lines.flatMap(line=>line.materialAllocations??[]).filter(part=>part.source==='original');
result.set(lock.id,{id:lock.id,number:lock.number,date:lock.date,customer:lock.customer,member:ledger.entityMember('price_lock',lock.id),
materialAmount:reservation.amount,usedMaterialAmount:original?.length?original.reduce((sum,part)=>sum+part.amount,0):reservation.amount,leftoverCost:delivery?(delivery.leftover_cost??null):0,allocations:allocations(reservation.allocations)});
const delivery=LockLines.deliveryAt(lock,reservation.line_index),order=lock.final_order_id?ledger.rawOrder(lock.final_order_id):null;
const original=order?.lines[reservation.line_index]?.materialAllocations?.filter(part=>part.source==='original');
const row={lineIndex:reservation.line_index,materialAmount:reservation.amount,usedMaterialAmount:original?.length?original.reduce((sum,part)=>sum+part.amount,0):reservation.amount,leftoverCost:delivery?(delivery.leftover_cost??null):0,allocations:allocations(reservation.allocations)};
const previous=result.get(lock.id);
if(previous){
previous.lines??=[{lineIndex:previous.lineIndex,materialAmount:previous.materialAmount,usedMaterialAmount:previous.usedMaterialAmount,leftoverCost:previous.leftoverCost,allocations:previous.allocations}];previous.lines.push(row);
for(const key of ['materialAmount','usedMaterialAmount','leftoverCost'])previous[key]=previous[key]===null||row[key]===null?null:previous[key]+row[key];previous.allocations=[...previous.allocations,...row.allocations];
}else result.set(lock.id,{id:lock.id,number:lock.number,date:lock.date,customer:lock.customer,member:ledger.entityMember('price_lock',lock.id),...row});
}
return result;
}
function changes(before,after){
const fields=['materialAmount','usedMaterialAmount','leftoverCost','allocations'];
const fields=['materialAmount','usedMaterialAmount','leftoverCost','allocations','lines'];
return [...before].flatMap(([id,previous])=>{const next=after.get(id);if(!next||fields.every(key=>JSON.stringify(previous[key])===JSON.stringify(next[key])))return [];
return [{id,number:previous.number,date:previous.date,customer:previous.customer,member:previous.member,before:Object.fromEntries(fields.map(key=>[key,previous[key]])),after:Object.fromEntries(fields.map(key=>[key,next[key]]))}];});
}