取消已用回收单时将关联用料转入公共料池
This commit is contained in:
1 parent
19b1b0263c
commit
dec01a3dca
18 files changed
+380
-56
No files matched your search
@@ -11,8 +11,20 @@ function initialize(l){l.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS material_rolling_days(date TEXT PRIMARY KEY,opening TEXT NOT NULL,snapshot TEXT NOT NULL DEFAULT '',frozen INTEGER NOT NULL DEFAULT 0,revision INTEGER NOT NULL DEFAULT 1);
|
||||
CREATE TABLE IF NOT EXISTS material_rolling_legacy(pool_id TEXT PRIMARY KEY,snapshot TEXT NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS material_rolling_source_order(sequence INTEGER PRIMARY KEY AUTOINCREMENT,kind TEXT NOT NULL,source_id TEXT NOT NULL,UNIQUE(kind,source_id));
|
||||
CREATE TABLE IF NOT EXISTS material_rolling_returns(id TEXT PRIMARY KEY,order_id TEXT NOT NULL,line_index INTEGER NOT NULL,date TEXT NOT NULL,mg INTEGER NOT NULL,amount INTEGER NOT NULL,allocations TEXT NOT NULL,origin_pool_id TEXT NOT NULL,same_day INTEGER NOT NULL,actor TEXT NOT NULL,created TEXT NOT NULL,UNIQUE(order_id,line_index));
|
||||
`);}
|
||||
CREATE TABLE IF NOT EXISTS material_rolling_returns(id TEXT PRIMARY KEY,order_id TEXT NOT NULL,line_index INTEGER 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(order_id,line_index));
|
||||
`);
|
||||
// 撤销原回收后,历史退货回料也可能暂缺成本;保留原行及顺序,只放宽该金额字段。
|
||||
if(l.rows('PRAGMA table_info(material_rolling_returns)').find(r=>r.name==='amount')?.notnull){
|
||||
l.db.exec('SAVEPOINT rolling_return_pending_upgrade');try{
|
||||
const sql=l.one("SELECT sql FROM sqlite_master WHERE type='table' AND name='material_rolling_returns'").sql;
|
||||
const indexes=l.rows("SELECT sql FROM sqlite_master WHERE tbl_name='material_rolling_returns' AND type IN ('index','trigger') AND sql IS NOT NULL");
|
||||
l.db.exec(sql.replace(/CREATE TABLE\s+(?:"material_rolling_returns"|material_rolling_returns)/i,'CREATE TABLE material_rolling_returns_pending_upgrade').replace(/amount\s+INTEGER\s+NOT NULL/i,'amount INTEGER'));
|
||||
l.db.exec('INSERT INTO material_rolling_returns_pending_upgrade(rowid,id,order_id,line_index,date,mg,amount,allocations,origin_pool_id,same_day,actor,created) SELECT rowid,* FROM material_rolling_returns');
|
||||
l.db.exec('DROP TABLE material_rolling_returns;ALTER TABLE material_rolling_returns_pending_upgrade RENAME TO material_rolling_returns');for(const r of indexes)l.db.exec(r.sql);
|
||||
l.db.exec('RELEASE rolling_return_pending_upgrade');
|
||||
}catch(e){l.db.exec('ROLLBACK TO rolling_return_pending_upgrade');l.db.exec('RELEASE rolling_return_pending_upgrade');throw e;}
|
||||
}
|
||||
}
|
||||
function config(l){return l.meta(KEY);}
|
||||
function enabled(l){return !!config(l)?.enabled;}
|
||||
function isDay(l,date){const c=config(l);return !!c?.enabled&&date>=c.cutover;}
|
||||
@@ -22,6 +34,7 @@ function dependency(l){
|
||||
const tables=['material_purchases','material_purchase_shares','material_purchase_payment_origins','recovery_purchase_sources','material_pool_usages','material_usage_parties','material_rolling_days','material_rolling_returns','material_rolling_legacy','material_rolling_source_order'];
|
||||
if(l.one("SELECT name FROM sqlite_master WHERE type='table' AND name='recovery_material_batches'")&&l.one('SELECT purchase_id FROM recovery_material_batches LIMIT 1'))tables.push('recovery_material_batches','material_usage_sources','material_return_sources');
|
||||
if(l.one('SELECT id FROM price_lock_pool_reservations LIMIT 1'))tables.push('price_lock_pool_reservations','price_lock_pool_returns','price_locks','material_usage_order');
|
||||
if(l.one('SELECT source_id FROM material_public_substitutions LIMIT 1'))tables.push('material_public_substitutions');
|
||||
return hash({config:config(l),tables:tables.map(table=>[table,l.rows('SELECT * FROM '+table+' ORDER BY rowid')]),orders:l.rows('SELECT id,revision,status,lines FROM orders ORDER BY rowid')});
|
||||
}
|
||||
function parts(p){return (p.allocations??[{payer:p.payer,amount:p.amount}]).filter(a=>a.amount>0);}
|
||||
@@ -165,7 +178,7 @@ function reconcile(l,...args){return l.scope(null,()=>reconcileInternal(l,...arg
|
||||
function reconcileInternal(l,date,actor,env,{skipJournalOrder=null,preservePriceLockIds=[]}={}){
|
||||
assertDate(l,date,env);ensureDay(l,date,env);const pool=readSources(l,date,env),updates=new Map();
|
||||
const preserved=new Set(preservePriceLockIds);
|
||||
for(const u of allocations(pool,pool.usages.filter(u=>consumed(l,u,date)))){
|
||||
for(const u of allocations(pool,pool.usages.filter(u=>consumed(l,u,date)||u.kind==='priceLock'&&u.reservedPublicSubstitutionMg>0))){
|
||||
if(u.kind==='priceLock'){if(!preserved.has(u.price_lock_id))LockPools.update(l,u,pool,actor,env,{skipJournalOrder});continue;}
|
||||
if(!updates.has(u.order_id))updates.set(u.order_id,{order:l.rawOrder(u.order_id),delta:0,changed:false});const v=updates.get(u.order_id),line=v.order.lines[u.line_index];
|
||||
if(!line||line.costMaterial?.poolId!==date)env.fail('滚动料池与商品来源不一致',409);
|
||||
@@ -204,7 +217,8 @@ function list(l,query,env){
|
||||
function syncReturnSources(l,origin){
|
||||
for(const r of l.rows('SELECT * FROM material_rolling_returns WHERE origin_pool_id=?',origin)){
|
||||
const o=l.rawOrder(r.order_id),line=o.lines[r.line_index],amount=line.costParts?.material,allocations=line.materialPool?.allocations;
|
||||
if(!Number.isSafeInteger(amount)||!allocations||sum(allocations,'amount')!==amount)throw new Error('复核后的退货回料来源无法核对');
|
||||
const pending=amount===null&&line.materialPool?.status==='pending'&&line.materialPool?.materialSources?.some(s=>s.kind==='public'&&s.amount===null);
|
||||
if(!pending&&(!Number.isSafeInteger(amount)||!allocations||sum(allocations,'amount')!==amount))throw new Error('复核后的退货回料来源无法核对');
|
||||
l.run('UPDATE material_rolling_returns SET amount=?,allocations=? WHERE id=?',amount,JSON.stringify(allocations),r.id);Allocation.saveReturn(l,'order',r.id,line.materialPool?.materialSources);
|
||||
}
|
||||
}
|
||||
@@ -216,7 +230,7 @@ function repriceLegacyReturns(l,actor,env){
|
||||
const order=l.rawOrder(u.order_id),line=order.lines[u.line_index],old=line.costParts?.material,oldAlloc=line.materialPool?.allocations;applyLine(line,u,pool);const delta=(u.amount??0)-(old??0);
|
||||
l.run('UPDATE material_pool_usages SET amount=?,owner_amount=?,assistant_amount=?,revision=revision+1,updated=? WHERE id=?',u.amount,u.owner_amount,u.assistant_amount,env.now(),u.id);
|
||||
if(u.assistant2_amount!==undefined)l.run('INSERT INTO material_usage_parties VALUES (?,?) ON CONFLICT(usage_id) DO UPDATE SET assistant2_amount=excluded.assistant2_amount',u.id,u.assistant2_amount);else l.run('DELETE FROM material_usage_parties WHERE usage_id=?',u.id);
|
||||
l.run('UPDATE orders SET lines=? WHERE id=?',JSON.stringify(order.lines),order.id);
|
||||
l.run('UPDATE orders SET lines=? WHERE id=?',JSON.stringify(order.lines),order.id);Allocation.saveUsage(l,u.id,u.materialSources);
|
||||
if(delta||JSON.stringify(oldAlloc)!==JSON.stringify(line.materialPool.allocations)){
|
||||
if(delta){l.journal(order.id,order.delivered_date||order.date,'历史复核金料成本调整',0,delta,0);if(order.status==='returned')l.journal(order.id,order.returned_date,'历史复核退货成本冲回',0,-delta,0);}
|
||||
l.touch(order.id);l.dirty(order.date);l.audit(order.id,'历史复核退货来源重算',actor,{origin,goodsDelta:delta});
|
||||
|
||||
Reference in new issue
Block a user