增加订单退货退款售后、原成本回料及跨日损益核算

This commit is contained in:
summer committed 2026-11-10 02:54:58 +08:00
1 parent b6a0213a0c
commit dab735423e
24 files changed
+1048 -82

No files matched your search

+7 -2
View File
@@ -47,8 +47,13 @@ function blockedUsages(l,purchaseId){
if(!purchaseSource(l,purchaseId))return null;
const result=new Map();
const add=(key,row)=>{if(!row.mg)return;const previous=result.get(key);result.set(key,previous?{...previous,mg:previous.mg+row.mg}:row);};
if(exists(l,'material_usage_sources'))for(const row of l.rows('SELECT u.order_id,o.number,s.sources FROM material_usage_sources s JOIN material_pool_usages u ON u.id=s.usage_id JOIN orders o ON o.id=u.order_id WHERE u.active=1 AND instr(s.sources,?)>0 ORDER BY u.created,u.rowid',purchaseId))
add('order:'+row.order_id,{orderId:row.order_id,number:row.number,mg:batches(row.sources).get(purchaseId)||0});
if(exists(l,'material_usage_sources'))for(const row of l.rows('SELECT u.order_id,u.line_index,o.number,s.sources FROM material_usage_sources s JOIN material_pool_usages u ON u.id=s.usage_id JOIN orders o ON o.id=u.order_id WHERE u.active=1 AND instr(s.sources,?)>0 ORDER BY u.created,u.rowid',purchaseId)){
let mg=batches(row.sources).get(purchaseId)||0;
// 一体售后保留原耗用以锁定历史成本;实物已回流的批次不能仍作为第二份已用量。
if(exists(l,'order_after_sales')&&l.one('SELECT order_id FROM order_after_sales WHERE order_id=?',row.order_id))for(const returned of l.rows("SELECT s.sources FROM material_return_sources s JOIN material_rolling_returns r ON s.return_kind='order' AND s.return_id=r.id WHERE r.order_id=? AND r.line_index=?",row.order_id,row.line_index))mg-=batches(returned.sources).get(purchaseId)||0;
if(mg<0)fail('售后退回批次超过原实际耗用,请先核对原单与回料');
add('order:'+row.order_id,{orderId:row.order_id,number:row.number,mg});
}
if(exists(l,'price_lock_pool_reservations'))for(const row of l.rows('SELECT r.id AS reservation_id,r.line_index,r.snapshot,p.id,p.final_order_id,p.number FROM price_lock_pool_reservations r JOIN price_locks p ON p.id=r.price_lock_id WHERE instr(r.snapshot,?)>0 ORDER BY r.created,r.rowid',purchaseId))
add('lock:'+row.id,{orderId:row.final_order_id??null,number:row.number,mg:lockBatches(l,row.id,JSON.parse(row.snapshot),row.line_index).get(purchaseId)||0,priceLockId:row.id});
return [...result.values()];