整合公共店铺配置与动态员工管理,保留最新订单功能及历史账兼容

This commit is contained in:
summer committed 2026-12-04 00:53:53 +08:00
commit b87f774386
84 files changed
+1622 -715

No files matched your search

+4 -3
View File
@@ -1,7 +1,8 @@
'use strict';
// 锁价预留只占用已有料池;不生成采购、现金、销售或实物流水。
const {randomUUID}=require('node:crypto'),Calc=require('./public/calculations'),Allocation=require('./material-pool-allocation'),LockLines=require('./price-lock-lines');
const PARTIES=['owner','assistant','assistant2'],sum=(rows,key)=>rows.reduce((n,r)=>n+(r[key]??0),0);
const Amounts=require('./material-member-amounts');
const sum=(rows,key)=>rows.reduce((n,r)=>n+(r[key]??0),0);
function initialize(l){l.db.exec(`
CREATE TABLE IF NOT EXISTS price_lock_pool_reservations(id TEXT PRIMARY KEY,price_lock_id TEXT NOT NULL REFERENCES price_locks(id),line_index INTEGER NOT NULL DEFAULT 0,pool_id TEXT NOT NULL,mg INTEGER NOT NULL CHECK(mg>0),amount INTEGER,allocations TEXT NOT NULL DEFAULT '[]',snapshot TEXT NOT NULL DEFAULT '{}',revision INTEGER NOT NULL DEFAULT 1,created TEXT NOT NULL,updated TEXT NOT NULL,UNIQUE(price_lock_id,line_index));
CREATE TABLE IF NOT EXISTS price_lock_pool_returns(id TEXT PRIMARY KEY,price_lock_id TEXT NOT NULL REFERENCES price_locks(id),line_index INTEGER NOT NULL DEFAULT 0,kind TEXT NOT NULL,date TEXT NOT NULL,mg INTEGER NOT NULL,amount INTEGER,allocations TEXT NOT NULL,origin_pool_id TEXT NOT NULL,same_day INTEGER NOT NULL,actor TEXT NOT NULL,created TEXT NOT NULL,UNIQUE(price_lock_id,line_index,kind));
@@ -39,7 +40,7 @@ function reservations(l,date){return l.rows(`SELECT r.*,s.sequence AS fifo_seque
const snapshot=JSON.parse(r.snapshot),delivery=LockLines.deliveryLines(r.delivery)[itemIndex(r.line_index)],entry=LockLines.lines({...r,id:r.price_lock_id,lines:r.lock_lines})[itemIndex(r.line_index)];
let mg=r.mg;if(delivery?.date===date)mg=r.line_index<0?delivery.extra_mg:delivery.original_used_mg;if(r.order_status==='returned'&&r.returned_date===date)mg=0;
const effective=snapshot.amount===undefined?{amount:null,allocations:[]}:mg===r.mg?{amount:r.amount,allocations:JSON.parse(r.allocations),...(snapshot.materialSources?{materialSources:snapshot.materialSources}:{})}:split(snapshot,mg);
return {...r,kind:'priceLock',member:l.entityMember('price_lock',r.price_lock_id),...(effective.materialSources?{materialSources:effective.materialSources}:{}),date,price_lock_id:r.price_lock_id,order_id:r.final_order_id,reserved_mg:r.mg,mg,...substitution(l,'priceLock',r.id,mg,r.mg),active:mg>0?1:0,amount:effective.amount,snapshot,allocations:effective.allocations,product:entry?.line.name??JSON.parse(r.line).name,...Object.fromEntries(PARTIES.map(p=>[p+'_amount',effective.amount===null?null:sum(effective.allocations.filter(a=>a.payer===p),'amount')]))};
return {...r,kind:'priceLock',member:l.entityMember('price_lock',r.price_lock_id),...(effective.materialSources?{materialSources:effective.materialSources}:{}),date,price_lock_id:r.price_lock_id,order_id:r.final_order_id,reserved_mg:r.mg,mg,...substitution(l,'priceLock',r.id,mg,r.mg),active:mg>0?1:0,amount:effective.amount,snapshot,allocations:effective.allocations,product:entry?.line.name??JSON.parse(r.line).name,...Object.fromEntries(Amounts.keys(effective.allocations).map(p=>[p+'_amount',effective.amount===null?null:sum(effective.allocations.filter(a=>a.payer===p),'amount')]))};
});}
function combined(l,date,rows){return [...decorate(l,rows),...reservations(l,date)].sort((a,b)=>a.created<b.created?-1:a.created>b.created?1:a.fifo_sequence-b.fifo_sequence);}
function returns(l,date){return l.rows('SELECT r.*,p.number,p.customer,p.line,p.lines AS lock_lines FROM price_lock_pool_returns r JOIN price_locks p ON p.id=r.price_lock_id WHERE r.date=? ORDER BY r.created,r.rowid',date).map(r=>({...r,allocations:JSON.parse(r.allocations),...(Allocation.returnSources(l,'lock',r.id)?{materialSources:Allocation.returnSources(l,'lock',r.id)}:{}),product:LockLines.product({...r,id:r.price_lock_id,lines:r.lock_lines},itemIndex(r.line_index)),sourceKind:'return',cashAmount:0,payer:JSON.parse(r.allocations)[0]?.payer??'owner',sameDay:!!r.same_day,note:r.kind==='cancel'?'锁价作废回料(原成本)':r.kind==='leftover'?'锁价余料回池':'锁价退货回料',source_sequence:l.one("SELECT sequence FROM material_rolling_source_order WHERE kind='lock_return' AND source_id=?",r.id)?.sequence??0}));}
@@ -68,7 +69,7 @@ function allocate(pool,rows,allocatePayers){
}
lockSnapshot={poolId:pool.id,date:pool.date,quantity:row.reserved_mg,amount:full,averageRate:pool.averageRate,allocations:parts,status:full===null?'pending':'allocated',basisAmount:pool.totalAmount,basisMg:pool.totalMg,prefixMg,prefixAmount,rolling:!!pool.rolling};
}
previous=cumulative??0;const result={...row,amount,...paid,...(lockSnapshot?{lockSnapshot}:{})};if(paid.assistant2_amount===undefined)delete result.assistant2_amount;return result;
previous=cumulative??0;const result={...row,amount,...paid,...(lockSnapshot?{lockSnapshot}:{})};for(const key of Object.keys(result))if(key.endsWith('_amount')&&Amounts.isKey(key.slice(0,-7))&&!Object.hasOwn(paid,key))delete result[key];if(!paid.allocations&&row.kind!=='priceLock')delete result.allocations;return result;
});
}
function returnRow(l,lock,kind,date,mg,amount,allocations,origin,actor,env,materialSources=null){