Files
weiguang-jewelry-ledger/新账系统/price-lock-pool-approvals.js
T

49 lines
4.7 KiB
JavaScript
Raw Normal View History

'use strict';
// 审批依据保留原始来源,结果比较只保留业务值;没有池锁时沿用旧申请哈希。
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}={}){
if(!exists(ledger,'price_lock_pool_reservations'))return {reservations:[],returns:[]};
const reservations=ledger.rows('SELECT * FROM price_lock_pool_reservations'+(priceLockId?' WHERE price_lock_id=?':poolId?' WHERE pool_id=?':'')+' ORDER BY rowid',...(priceLockId?[priceLockId]:poolId?[poolId]:[]));
const returns=exists(ledger,'price_lock_pool_returns')?ledger.rows('SELECT * FROM price_lock_pool_returns'+(priceLockId?' WHERE price_lock_id=?':poolId?' WHERE origin_pool_id=? OR date=?':'')+' ORDER BY rowid',...(priceLockId?[priceLockId]:poolId?[poolId,poolId]:[])):[];
return {reservations,returns};
}
function snapshot(ledger,query={}){
const state=rows(ledger,query);if(!state.reservations.length&&!state.returns.length)return {};
if(query.poolId){const ids=[...new Set([...state.reservations,...state.returns].map(row=>row.price_lock_id))];state.locks=ids.map(id=>ledger.one('SELECT * FROM price_locks WHERE id=?',id));}
if(exists(ledger,'material_usage_order')){
const usageIds=new Set(state.reservations.map(row=>row.id)),orderUsageIds=new Set(query.poolId?ledger.rows('SELECT id FROM material_pool_usages WHERE pool_id=?',query.poolId).map(row=>row.id):[]);
state.usageOrder=ledger.rows('SELECT * FROM material_usage_order ORDER BY sequence').filter(row=>row.kind==='priceLock'?usageIds.has(row.source_id):row.kind==='order'&&orderUsageIds.has(row.source_id));
}
return {priceLockPool:state};
}
function effect(ledger,poolIds=null){
const source=poolIds?poolIds.map(poolId=>rows(ledger,{poolId})): [rows(ledger)];
const unique=values=>[...new Map(values.map(row=>[row.id,row])).values()];
const reservations=unique(source.flatMap(value=>value.reservations)),returns=unique(source.flatMap(value=>value.returns));
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))
}};
}
function finance(ledger){return ledger.scope(null,()=>financeInternal(ledger),null);}
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)});
}
return result;
}
function changes(before,after){
const fields=['materialAmount','usedMaterialAmount','leftoverCost','allocations'];
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]]))}];});
}
function visibleChanges(ledger,value,actor){return actor?.role==='assistant'?value.filter(row=>ledger.canAccess('price_lock',row.id,actor)):value;}
module.exports={snapshot,effect,finance,changes,visibleChanges};