完善订单成本编辑与多商品置换,修正锁价跨天作废回池
This commit is contained in:
1 parent
a3262a455d
commit
2e4e7dad55
25 files changed
+430
-109
No files matched your search
+21
-14
@@ -102,7 +102,7 @@ function movements(l,row,after){
|
||||
if(matching)Object.assign(matching,{stockCount:-count.stockCount,countEffectiveDate:count.date});
|
||||
else deltas.push({...count,mg:0,stockCount:-count.stockCount,countEffectiveDate:count.date,kind:'recovery_change_reverse'});
|
||||
}
|
||||
if(after){deltas.push({date:after.date,pocket:'回收旧料',mg:after.oldMg,kind:'recovery_change_old'});if(after.mode==='exchange')deltas.push({date:after.date,pocket:after.newProduct.pocket,mg:-after.newMg,stockCount:after.newProduct.stockCount==null?null:-after.newProduct.stockCount,kind:'recovery_change_sale'});}
|
||||
if(after){deltas.push({date:after.date,pocket:after.oldPocket??'回收旧料',mg:after.oldMg,stockCount:after.oldStockCount??null,kind:'recovery_change_old'});if(after.mode==='exchange')for(const product of Recoveries.products(after))if(product.pocket)deltas.push({date:after.date,pocket:product.pocket,mg:-product.quantity,stockCount:product.stockCount==null?null:-product.stockCount,kind:'recovery_change_sale'});}
|
||||
return deltas;
|
||||
}
|
||||
function validateInventory(l,deltas,env){
|
||||
@@ -148,8 +148,7 @@ function appendOrder(l,row,after,actor,reason,env){
|
||||
reverseRows(l,id,'payments',actor,reason,env);reverseRows(l,id,'expenses',actor,reason,env);
|
||||
if(!after){l.run("UPDATE orders SET status='cancelled',fee_items='[]',reviewed_revision=0 WHERE id=?",id);Material.release(l,l.rawOrder(id),row.date,actor,env,{kind:'回收记录取消释放'});return;}
|
||||
const input=Recoveries.orderInput(after),previous=l.rawOrder(id),lines=l.validateLines(input.lines,previous.lines,after.date,{orderId:id,member:row.member}),line=lines[0];
|
||||
line.recoveryMaterialMg=after.differenceMg;line.recovery_id=row.id;line.costMaterial=after.exchange.costMaterial;line.costLabor=after.exchange.costLabor;
|
||||
if(line.costMaterial.mode==='pool')Material.prepareLines(l,lines,after.date,env,{orderId:id,member:row.member});
|
||||
Recoveries.applyOrderMetadata(l,lines,after,row.id,env,id);
|
||||
l.run("UPDATE orders SET date=?,customer=?,salesperson=?,note=?,lines=?,discount=?,total=?,rounding_mode=?,payment_account=?,fees_date=?,delivered_date=?,fee_items='[]',reviewed_revision=0 WHERE id=?",after.date,after.customer,after.salesperson,after.note,JSON.stringify(lines),after.discount,after.receivable,after.rounding_mode,after.payment_account,after.date,after.date,id);
|
||||
const fees=[];for(const fee of after.exchange.fee_items){const expenseId=fee.amount?randomUUID():null;if(expenseId)l.run('INSERT INTO expenses VALUES (?,?,?,?,?,?,?,?)',expenseId,id,after.date,fee.amount,fee.payer,fee.note,actor.name,env.now());fees.push({...fee,status:'recorded',...(expenseId?{expense_id:expenseId}:{})});}
|
||||
l.run('UPDATE orders SET fee_items=? WHERE id=?',JSON.stringify(fees),id);
|
||||
@@ -162,29 +161,33 @@ function replaceJournal(l,row,after){
|
||||
l.touch(id);
|
||||
}
|
||||
function financialInput(value){const {customer,salesperson,note,...rest}=value;return rest;}
|
||||
function inventoryOnlyInput(value){const {old_pocket,old_stock_count,...rest}=financialInput(value);return rest;}
|
||||
function metadataChangeOnly(row,after){return !!after&&hash(financialInput(Recoveries.inputFor(JSON.parse(row.data))))===hash(financialInput(Recoveries.inputFor(after)));}
|
||||
function legacyCountInfo(l,row){
|
||||
const q=JSON.parse(row.data),product=q.mode==='exchange'?q.newProduct:null;
|
||||
return Counts.legacyInfo(l,product&&product.stockCount==null?[{id:'',pocket:product.pocket,mg:q.newMg}]:[]);
|
||||
const q=JSON.parse(row.data);
|
||||
return Counts.legacyInfo(l,[...Recoveries.products(q).filter(product=>product.stockCount==null).map(product=>({id:'',pocket:product.pocket,mg:product.quantity})),...(q.oldStockCount==null?[{id:'',pocket:q.oldPocket??'回收旧料',mg:q.oldMg}]:[])]);
|
||||
}
|
||||
function verifyLegacyCount(l,row,after,input,env){const info=legacyCountInfo(l,row);if(info.requiresLegacyCountVerified&&!metadataChangeOnly(row,after)&&input?.legacyCountVerified!==true)env.fail('请核对并确认:'+info.legacyCountNotice);}
|
||||
function execute(l,row,after,actor,reason,env,input={}){return Rolling.withHistorical(l,()=>l.scope(null,()=>{
|
||||
const before=snapshot(l,row),old=JSON.parse(row.data),metadataOnly=!!after&&hash(financialInput(Recoveries.inputFor(old)))===hash(financialInput(Recoveries.inputFor(after)));
|
||||
const inventoryOnly=!!after&&!metadataOnly&&hash(inventoryOnlyInput(Recoveries.inputFor(old)))===hash(inventoryOnlyInput(Recoveries.inputFor(after)));
|
||||
verifyLegacyCount(l,row,after,input,env);
|
||||
const deltas=metadataOnly?[]:movements(l,row,after);validateInventory(l,deltas,env);
|
||||
const changed=l.run('UPDATE recoveries SET date=?,customer=?,salesperson=?,note=?,old_lines=?,data=?,status=?,cancelled_at=?,updated=?,revision=revision+1 WHERE id=? AND revision=?',after?.date??row.date,after?.customer??row.customer,after?.salesperson??row.salesperson,after?.note??row.note,JSON.stringify(after?.old_lines??old.old_lines),JSON.stringify(metadataOnly?{...old,customer:after.customer,salesperson:after.salesperson,note:after.note}:after??old),after?'active':'cancelled',after?'':env.now(),env.now(),row.id,row.revision);
|
||||
if(changed.changes!==1)env.fail('回收记录已有其他更正,请刷新',409);
|
||||
if(metadataOnly){if(row.order_id){l.run('UPDATE orders SET customer=?,salesperson=?,note=?,reviewed_revision=0 WHERE id=?',after.customer,after.salesperson,after.note,row.order_id);l.touch(row.order_id);}}
|
||||
else{
|
||||
if(!after&&row.purchase_id)materialHook()?.cancelPurchase?.(l,row.purchase_id);
|
||||
appendOrder(l,row,after,actor,reason,env);const pools=purchaseChange(l,row,after,actor,env);
|
||||
if(after&&row.order_id){const order=l.rawOrder(row.order_id);Material.adjust(l,{...order,lines:order.lines.map(line=>line.costMaterial?.mode==='pool'?{...line,quantity:line.costMaterial.consumptionMg??line.quantity}:line)},after.date,actor,env);}
|
||||
for(const pool of pools)Material.reconcile(l,pool,env.today(),actor,env,{skipJournalOrder:row.order_id});
|
||||
if(Rolling.enabled(l))Rolling.rebuildFrom(l,[row.date,after?.date,...pools].filter(Boolean).sort()[0],actor,env);
|
||||
if(!inventoryOnly){
|
||||
if(!after&&row.purchase_id)materialHook()?.cancelPurchase?.(l,row.purchase_id);
|
||||
appendOrder(l,row,after,actor,reason,env);const pools=purchaseChange(l,row,after,actor,env);
|
||||
if(after&&row.order_id){const order=l.rawOrder(row.order_id);Material.adjust(l,{...order,lines:order.lines.map(line=>line.costMaterial?.mode==='pool'?{...line,quantity:line.costMaterial.consumptionMg??line.quantity}:line)},after.date,actor,env);}
|
||||
for(const pool of pools)Material.reconcile(l,pool,env.today(),actor,env,{skipJournalOrder:row.order_id});
|
||||
if(Rolling.enabled(l))Rolling.rebuildFrom(l,[row.date,after?.date,...pools].filter(Boolean).sort()[0],actor,env);
|
||||
}
|
||||
for(const movement of deltas){const id=randomUUID();l.run('INSERT INTO movements VALUES (?,?,?,?,?,?,?,?,?,?,?)',id,row.inventory_group,row.order_id,movement.date,movement.pocket,movement.mg,movement.kind,'回收更正:'+row.number+';'+reason,null,actor.name,env.now());l.attachMovementCount(id,movement.stockCount,{effectiveDate:movement.countEffectiveDate??movement.date});l.assignMember('movement',id,row.member);}
|
||||
l.validateCountInventory();
|
||||
replaceJournal(l,row,after);
|
||||
const hook=materialHook();hook?.recoveryUpdated?.(l,row.id,actor,env);
|
||||
if(!inventoryOnly){replaceJournal(l,row,after);const hook=materialHook();hook?.recoveryUpdated?.(l,row.id,actor,env);}
|
||||
else if(row.order_id&&(old.customer!==after.customer||old.salesperson!==after.salesperson||old.note!==after.note)){l.run('UPDATE orders SET customer=?,salesperson=?,note=?,reviewed_revision=0 WHERE id=?',after.customer,after.salesperson,after.note,row.order_id);l.touch(row.order_id);}
|
||||
}
|
||||
if(metadataOnly)l.run('UPDATE closes SET stale=1 WHERE date IN (?,?)',row.date,after.date);
|
||||
else l.dirtyInventory([row.date,after?.date,...before.movements.map(r=>r.date)].filter(Boolean).sort()[0]);
|
||||
@@ -198,9 +201,13 @@ function normalized(l,row,input,actor,env){
|
||||
if(Object.hasOwn(input,'member')&&input.member!==row.member)env.fail('已有回收记录的归属不能修改',403);
|
||||
// 历史试算只读;日期许可仍在change/approve决定,不能把试算许可当成落账权限。
|
||||
const previous=JSON.parse(row.data),after=Rolling.withHistorical(l,()=>Recoveries.quote(l,{...input,member:row.member},actor,env,{creating:true,orderId:row.order_id,existingRecovery:previous}));
|
||||
if(previous.mode==='exchange'&&previous.newProduct?.stockCount==null&&l.pocket(previous.newProduct.pocket)?.countUnit==='pair'){
|
||||
for(const [index,product] of Recoveries.products(previous).entries())if(product.stockCount==null&&l.pocket(product.pocket)?.countUnit==='pair'){
|
||||
const physical=product=>JSON.stringify([product.name,product.quantity,product.pocket,product.stockCount??null]);
|
||||
if(physical(previous.newProduct)!==physical(after.newProduct))env.fail('启用前已交付的置换新货没有原始对数,只能更正资料和财务;成品变化请先核对实物,再登记今天的数量调整,不能补旧单对数重扣盘点基准');
|
||||
if(physical(product)!==physical(Recoveries.products(after)[index]??{}))env.fail('启用前已交付的置换新货没有原始对数,只能更正资料和财务;成品变化请先核对实物,再登记今天的数量调整,不能补旧单对数重扣盘点基准');
|
||||
}
|
||||
if(previous.oldStockCount==null&&l.pocket(previous.oldPocket??'回收旧料')?.countUnit==='pair'){
|
||||
const physical=value=>JSON.stringify([value.oldPocket??'回收旧料',value.oldMg,value.oldStockCount??null]);
|
||||
if(physical(previous)!==physical(after))env.fail('启用前的回收旧料没有原始对数,只能更正资料和财务;实物变化请先核对后登记今天的库存数量调整,不能补旧单对数重扣盘点基准');
|
||||
}
|
||||
return after;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user