保存同行基础版:店铺配置与员工管理
This commit is contained in:
1 parent
ecb8d6dec0
commit
b7b987064b
84 files changed
+1884
-573
No files matched your search
@@ -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));
|
||||
@@ -37,7 +38,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)[r.line_index],entry=LockLines.lines({...r,id:r.price_lock_id,lines:r.lock_lines})[r.line_index];
|
||||
let mg=r.mg;if(delivery?.date===date)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},r.line_index),sourceKind:'return',cashAmount:0,payer:JSON.parse(r.allocations)[0]?.payer??'owner',sameDay:!!r.same_day,note: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}));}
|
||||
@@ -66,7 +67,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){
|
||||
|
||||
Reference in new issue
Block a user