增加订单退货退款售后、原成本回料及跨日损益核算
This commit is contained in:
1 parent
b6a0213a0c
commit
dab735423e
24 files changed
+1048
-82
No files matched your search
+12
-3
@@ -2,6 +2,7 @@
|
||||
// 三个经营款账户独立核算。新账只追加来源差额,旧二人结算绝不自动带入。
|
||||
const {randomUUID,createHash}=require('node:crypto');
|
||||
const LockPools=require('./price-lock-pools');
|
||||
const OrderAfterSaleReporting=require('./order-after-sale-reporting');
|
||||
const MEMBERS=['owner','assistant','assistant2'];
|
||||
const NAMES=Object.fromEntries(require('./team-members').MEMBERS.map(({member,label})=>[member,label]));
|
||||
const LIMIT=100000000000;
|
||||
@@ -74,9 +75,9 @@ function materialUsageObligations(ledger,snapshot,order,liable,env){
|
||||
}
|
||||
function orderSnapshot(ledger,id,env){
|
||||
const snapshot=empty(),liable=entityMember(ledger,'order',id);if(!liable)return snapshot;
|
||||
const order=ledger.rawOrder(id),lock=ledger.one('SELECT id FROM price_locks WHERE final_order_id=?',id);
|
||||
const raw=ledger.rawOrder(id),afterSale=OrderAfterSaleReporting.read(ledger,id),order=OrderAfterSaleReporting.originalOrder(ledger,raw),lock=ledger.one('SELECT id FROM price_locks WHERE final_order_id=?',id);
|
||||
const correctedExpenses=new Set(ledger.rows("SELECT target FROM corrections WHERE type='expense' AND target IN (SELECT id FROM expenses WHERE order_id=?)",id).map(row=>row.target));
|
||||
for(const payment of ledger.rows('SELECT * FROM payments WHERE order_id=? ORDER BY date,created,rowid',id))addPayment(snapshot,payment,liable,lock?payment.date:order.date);
|
||||
for(const payment of ledger.rows('SELECT * FROM payments WHERE order_id=? ORDER BY date,created,rowid',id))addPayment(snapshot,payment,liable,lock||OrderAfterSaleReporting.addedEntry(afterSale,payment,'payments')?payment.date:order.date);
|
||||
// 锁价的料费、初始收退款和手续费由原事件来源记一次。
|
||||
for(const expense of ledger.rows('SELECT * FROM expenses WHERE order_id=? ORDER BY date,created,rowid',id)){
|
||||
if(ledger.one('SELECT id FROM price_lock_events WHERE card_fee_expense_id=?',expense.id))continue;
|
||||
@@ -94,14 +95,22 @@ function orderSnapshot(ledger,id,env){
|
||||
if(component==='material'&&(line.costMaterial?.mode==='pool'||line.materialAllocations||lock))continue;
|
||||
if(amount==null){snapshot.pending.push({member:liable,project:'商品实际成本待补'});continue;}
|
||||
const payer=line.costPayers?.[component]??line.payer,key='goods:'+index+':'+component;
|
||||
if(component==='labor'&&OrderAfterSaleReporting.inheritedLabor(line)>0){
|
||||
// 库存工费在售后回收日撤销原耗用往来,再次销售时恢复耗用责任,没有第二次付款。
|
||||
obligation(snapshot,key,line.costDate??order.delivered_date??order.date,'advance','现货继承工费耗用',liable,payer,amount);continue;
|
||||
}
|
||||
const covered=fundingClaim(ledger,line.costFundings?.[component],{kind:'order',id,component:key,payer,liable,amount},env);
|
||||
addOutlay(snapshot,key,line.costDate??order.delivered_date??order.date,'advance','商品实际垫付',liable,payer,amount-covered);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 回料沿用原资金归属,只在退回当日撤销该笔金料往来;采购现金与历史耗用记录保留。
|
||||
for(const returned of ledger.rows('SELECT * FROM material_rolling_returns WHERE order_id=?',id))for(const part of JSON.parse(returned.allocations))
|
||||
if(!afterSale)for(const returned of ledger.rows('SELECT * FROM material_rolling_returns WHERE order_id=?',id))for(const part of JSON.parse(returned.allocations))
|
||||
obligation(snapshot,'material-return:'+returned.id+':'+part.payer,returned.date,'material_return','退货金料回池',part.payer,liable,part.amount);
|
||||
if(afterSale)for(const line of afterSale.summary.lines){
|
||||
for(const part of line.materialAllocations||[])obligation(snapshot,'after-sale-material:'+line.lineIndex+':'+part.payer,afterSale.date,'material_return','售后金料回池',part.payer,liable,part.amount);
|
||||
for(const part of line.restockedLaborAllocations||[])obligation(snapshot,'after-sale-labor:'+line.lineIndex+':'+part.payer,afterSale.date,'inventory_return','售后现货工费回库存',part.payer,liable,part.amount);
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
function priceLockSnapshot(ledger,id){
|
||||
|
||||
Reference in new issue
Block a user