普通订单取消须麦麦审批并按录错作废冲回
This commit is contained in:
1 parent
46530c29b8
commit
27a11e9b35
28 files changed
+759
-60
No files matched your search
+15
-5
@@ -11,6 +11,7 @@ const PriceLocks=require('./price-locks');
|
||||
const MaterialPools=require('./material-pools');
|
||||
const OrderTemplates=require('./order-templates');
|
||||
const OrderEdit=require('./order-edit');
|
||||
const OrderCancellations=require('./order-cancellations');
|
||||
const FEE_LABELS={shipping:'快递费',box:'包装盒',certificate:'证书',braiding:'编绳',sticker:'手机贴',other:'其他直接费用'};
|
||||
const POCKETS = ['现货','金料','找金','回收旧料','君宝汇','臻金汇','金金','定制工厂'];
|
||||
// 历史货盘仍参与原单、退货和完整库存核算;新登记只使用当前五个货盘。
|
||||
@@ -85,6 +86,7 @@ class Ledger {
|
||||
PriceLocks.initialize(this);
|
||||
MaterialPools.initialize(this);
|
||||
OrderTemplates.initialize(this);
|
||||
OrderCancellations.initialize(this);
|
||||
if(filename!==':memory:') fs.chmodSync(filename,0o600);
|
||||
}
|
||||
rows(sql,...args){return this.db.prepare(sql).all(...args);}
|
||||
@@ -113,8 +115,16 @@ class Ledger {
|
||||
rawOrder(id){const o=this.one('SELECT * FROM orders WHERE id=?',id);if(!o)fail('订单不存在',404);o.lines=JSON.parse(o.lines);o.fee_items=JSON.parse(o.fee_items);return o;}
|
||||
orderEdit(id,actor){return OrderEdit.detail(this,id,actor,priceLockEnv);}
|
||||
editOrder(id,input,actor){return OrderEdit.edit(this,id,input,actor,priceLockEnv);}
|
||||
orderCancellation(id,actor){return OrderCancellations.detail(this,id,actor,priceLockEnv);}
|
||||
orderCancellations(query={},actor){return OrderCancellations.list(this,query,actor,priceLockEnv);}
|
||||
orderCancellationRequest(id,actor){return OrderCancellations.read(this,id,actor,priceLockEnv);}
|
||||
requestOrderCancellation(id,input,actor){return OrderCancellations.request(this,id,input,actor,priceLockEnv);}
|
||||
approveOrderCancellation(id,input,actor){return OrderCancellations.approve(this,id,input,actor,priceLockEnv);}
|
||||
rejectOrderCancellation(id,input,actor){return OrderCancellations.reject(this,id,input,actor,priceLockEnv);}
|
||||
guardOrderCancellation(id,options){return OrderCancellations.guard(this,id,options,priceLockEnv);}
|
||||
detail(id){
|
||||
const o=this.rawOrder(id);
|
||||
Object.assign(o,OrderCancellations.projection(this,id));
|
||||
o.payments=this.rows('SELECT * FROM payments WHERE order_id=? ORDER BY date,created',id);
|
||||
const priceLock=this.one('SELECT id,buy_payer,delivery FROM price_locks WHERE final_order_id=?',id);
|
||||
if(priceLock){o.price_lock_id=priceLock.id;o.payments.push(...PriceLocks.inheritedPayments(this,id));o.payments.sort((a,b)=>a.date.localeCompare(b.date)||a.created.localeCompare(b.created));}
|
||||
@@ -216,6 +226,7 @@ class Ledger {
|
||||
saveOrder(input,actor){
|
||||
let id=input.id;
|
||||
const prior=id?this.rawOrder(id):null;
|
||||
if(prior)this.guardOrderCancellation(id);
|
||||
if(prior){if(prior.status!=='draft')fail('已确认订单不能直接改写。未交付可取消重开,已交付请办理退货',409);if(prior.revision!==input.revision)fail('订单已被更新,请重新打开',409);}
|
||||
const date=day(input.date), lines=this.validateLines(input.lines,prior?.lines,date), discount=money(input.discount||'0','整单优惠');
|
||||
const roundingMode=input.rounding_mode===undefined?(prior?.rounding_mode??'yuan'):input.rounding_mode;
|
||||
@@ -256,6 +267,7 @@ class Ledger {
|
||||
// 调用方用同一事务生成订单、收全款、记录实际费用并交付;任一步失败全部回滚。
|
||||
// 旧定金单仍沿用原动作,仅经明确确认 complete 后完成交付,不自动补收款。
|
||||
const prior=input.id?this.rawOrder(input.id):null;
|
||||
if(prior)this.guardOrderCancellation(prior.id);
|
||||
if(prior&&prior.status!=='draft')fail('已确认订单不能直接改写。未交付可取消重开,已交付请办理退货',409);
|
||||
const lines=this.validateLines(input.lines,prior?.lines,day(input.date));
|
||||
const fees=input.fee_items??prior?.fee_items.map(f=>({...f,amount:f.amount===null?'':(f.amount/100).toFixed(2)}))??[];
|
||||
@@ -335,6 +347,8 @@ class Ledger {
|
||||
journal(id,date,kind,sales,goods,expense){this.run('INSERT INTO journal VALUES (?,?,?,?,?,?,?,?,?)',randomUUID(),id,date,kind,sales,goods,expense,sales-goods-expense,now());this.dirty(date);}
|
||||
action(id,action,input,actor){
|
||||
const o=this.detail(id);const date=day(input.date||today(),o.date);
|
||||
if(action==='cancel')fail('取消订单须先提交取消申请,并由麦麦账号审批;旧取消接口不能直接作废订单',409);
|
||||
this.guardOrderCancellation(id,{action,type:input.type});
|
||||
// 整单更正后旧错日期可能已全额冲零;审计行仍保留,但不能阻挡后续真实业务。
|
||||
const financialDate=this.one('SELECT MAX(date) AS date FROM (SELECT date FROM journal WHERE order_id=? GROUP BY date HAVING SUM(sales)<>0 OR SUM(goods)<>0 OR SUM(expense)<>0)',id)?.date;
|
||||
if(['return','expense','refund','settlement','correct'].includes(action)&&financialDate&&date<financialDate)fail(`该动作日期不能早于本单最近记账日${financialDate}`);
|
||||
@@ -410,10 +424,6 @@ class Ledger {
|
||||
const group=randomUUID();for(const l of o.lines)if(l.pocket)this.move({group,order:id,date,pocket:l.pocket,mg:-l.quantity,kind:'sale',note:o.number},actor);
|
||||
const knownGoods=sum(delivered.lines,line=>line.cost??sum(Object.values(line.costParts||{}).map(amount=>({amount:amount??0})),'amount'));
|
||||
this.journal(id,date,'交付',o.total,knownGoods,o.directCost);
|
||||
} else if(action==='cancel'){
|
||||
if(!['draft','confirmed'].includes(o.status))fail('已交付订单请登记退货');
|
||||
if(o.payments.some(p=>p.date>date)||o.expenses.some(e=>e.date>date))fail('取消日期不能早于本单已登记流水日期');
|
||||
this.run("UPDATE orders SET status='cancelled' WHERE id=?",id);this.journal(id,date,'取消预订',0,0,o.directCost);text(input.reason,'取消原因',200);
|
||||
} else if(action==='return'){
|
||||
if(o.status!=='delivered')fail('只有已交付订单可以整单退货',409);if(input.verified!==true)fail('请先确认实物已收回或退回供应商');
|
||||
text(input.reason,'退货原因',200);this.run("UPDATE orders SET status='returned',returned_date=? WHERE id=?",date,id);
|
||||
@@ -569,7 +579,7 @@ class Ledger {
|
||||
const knownProfit=sum(j.filter(row=>!pendingIds.has(row.order_id)),'profit')-sum(afterSales,'amount'),knownSettlementDue=sum(orders,o=>o.knownSettlementDue)+poolSettlement.knownSettlementDue;
|
||||
return {date,from,sales:sum(j,'sales'),goods:pendingMaterialOrders.length?null:sum(j,'goods'),knownGoods:sum(j,'goods'),expense:sum(j,'expense')+sum(afterSales,'amount'),profit:pendingMaterialOrders.length?null:knownProfit,knownProfit,materialCostPending:pendingMaterialOrders.length>0,pendingMaterialCount:pendingMaterialOrders.length,
|
||||
materialPoolActivity:poolActivity,materialPoolSettlement:poolSettlement,
|
||||
delivered:orders.filter(o=>o.delivered_date>=from&&o.delivered_date<=date).length,returns:j.filter(r=>r.kind==='整单退货').length,accounts,payments,settlements:settles,journal:j.map(row=>pendingIds.has(row.order_id)?{...row,goods:null,profit:null,materialCostPending:true}:row),afterSales,afterSaleCount:afterSales.length,inventory:this.inventory(date),warnings,unsettled:orders.filter(o=>o.settlementDue!==0).map(o=>({id:o.id,number:o.number,date:o.delivered_date||o.date,status:o.status,costDue:o.costDue,profitDue:o.profitDue,ownerDirect:o.ownerDirect,total:o.settlementDue,materialCostPending:o.materialCostPending})),settlementDue:hasPendingSettlement?null:sum(orders,'settlementDue')+poolSettlement.settlementDue,knownSettlementDue,customerBalance:sum(orders,o=>o.status==='confirmed'?o.balance:0),close:close?{date:close.date,actor:close.actor,created:close.created,stale:!!close.stale}:null};
|
||||
delivered:orders.filter(o=>!o.cancellationApproved&&o.delivered_date>=from&&o.delivered_date<=date).length,returns:j.filter(r=>r.kind==='整单退货').length,accounts,payments,settlements:settles,journal:j.map(row=>pendingIds.has(row.order_id)?{...row,goods:null,profit:null,materialCostPending:true}:row),afterSales,afterSaleCount:afterSales.length,inventory:this.inventory(date),warnings,unsettled:orders.filter(o=>o.settlementDue!==0).map(o=>({id:o.id,number:o.number,date:o.delivered_date||o.date,status:o.status,costDue:o.costDue,profitDue:o.profitDue,ownerDirect:o.ownerDirect,total:o.settlementDue,materialCostPending:o.materialCostPending})),settlementDue:hasPendingSettlement?null:sum(orders,'settlementDue')+poolSettlement.settlementDue,knownSettlementDue,customerBalance:sum(orders,o=>o.status==='confirmed'?o.balance:0),close:close?{date:close.date,actor:close.actor,created:close.created,stale:!!close.stale}:null};
|
||||
}
|
||||
closeDay(date,actor,basis){
|
||||
owner(actor);if(basis!==undefined&&basis!=='orderDate')fail('对账日期口径无效');
|
||||
|
||||
Reference in new issue
Block a user