支持多商品锁价及待交付修改作废审批
This commit is contained in:
1 parent
1cbcdc728c
commit
337b2a709f
28 files changed
+1187
-344
No files matched your search
+23
-13
@@ -10,6 +10,8 @@ const {orderBook}=require('./order-book');
|
||||
const {reportByOrderDate,dailyFundsReceipts}=require('./daily-report');
|
||||
const DailyFunds=require('./daily-funds');
|
||||
const PriceLocks=require('./price-locks');
|
||||
const PriceLockChanges=require('./price-lock-changes');
|
||||
const LockLines=require('./price-lock-lines');
|
||||
const MaterialPools=require('./material-pools');
|
||||
const RollingMaterialPools=require('./rolling-material-pools');
|
||||
const MaterialPurchaseChanges=require('./material-purchase-changes');
|
||||
@@ -158,6 +160,7 @@ class Ledger {
|
||||
if(!snapshotColumns.includes('scheduled_for'))this.db.exec('ALTER TABLE inventory_snapshots ADD COLUMN scheduled_for TEXT');
|
||||
PriceLocks.initialize(this);
|
||||
MaterialPools.initialize(this);
|
||||
PriceLockChanges.initialize(this);
|
||||
OrderTemplates.initialize(this);
|
||||
OrderCancellations.initialize(this);
|
||||
OrderChanges.initialize(this);
|
||||
@@ -273,7 +276,7 @@ class Ledger {
|
||||
const o=this.rawOrder(id);
|
||||
Object.assign(o,OrderCancellations.projection(this,id),OrderChanges.projection(this,id),OrderOwnership.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);
|
||||
const priceLock=this.one('SELECT * 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));}
|
||||
o.expenses=this.rows('SELECT * FROM expenses WHERE order_id=? ORDER BY date,created',id);
|
||||
o.settlements=this.rows('SELECT * FROM settlements WHERE order_id=? ORDER BY date,created',id);
|
||||
@@ -292,23 +295,22 @@ class Ledger {
|
||||
const j=this.one('SELECT COALESCE(SUM(profit),0) AS profit FROM journal WHERE order_id=?',id);o.profit=o.materialCostPending&&o.status==='delivered'?null:j.profit;o.knownProfit=o.profit??0;
|
||||
o.ownerCost=o.status==='delivered'?Calc.orderCosts(o).byPayer.owner.goods:0;
|
||||
if(['delivered','returned','cancelled'].includes(o.status))o.ownerCost+=sum(o.expenses,e=>e.payer==='owner'?e.amount:0);
|
||||
if(priceLock&&!LockPools.find(this,priceLock.id)){
|
||||
const delivery=priceLock.delivery?JSON.parse(priceLock.delivery):null,sources=[];
|
||||
const leftover=['delivered','returned'].includes(o.status)&&priceLock.buy_payer==='assistant'?(delivery?.leftover_cost??0):0;
|
||||
if(leftover)sources.push({kind:'leftover',amount:leftover,quantity:delivery.leftover_mg,explanation:'助理垫付的剩余金料进入自有货盘,按原采购成本抵扣货款待结'});
|
||||
const returned=o.status==='returned'?Calc.orderCosts({...o,status:'delivered'}).byPayer.assistant.goods:0;
|
||||
if(priceLock){
|
||||
const pooled=new Set(LockPools.findAll(this,priceLock.id).map(row=>row.line_index)),manual=LockLines.lines(priceLock).filter(row=>!pooled.has(row.line_index)),sources=[];
|
||||
const leftoverRows=['delivered','returned'].includes(o.status)?manual.filter(row=>row.buy_payer==='assistant').map(row=>LockLines.deliveryAt(priceLock,row.line_index)).filter(Boolean):[],leftover=sum(leftoverRows,'leftover_cost');
|
||||
if(leftover)sources.push({kind:'leftover',amount:leftover,quantity:sum(leftoverRows,'leftover_mg'),explanation:'助理垫付的剩余金料进入自有货盘,按原采购成本抵扣货款待结'});
|
||||
const returned=o.status==='returned'&&manual.length?Calc.orderCosts({...o,lines:manual.map(row=>o.lines[row.line_index]),status:'delivered'}).byPayer.assistant.goods:0;
|
||||
if(returned)sources.push({kind:'returnedGoods',amount:returned,explanation:'退回自有货盘的商品由助理垫付的货品成本,抵扣货款待结'});
|
||||
o.assistantInventoryCredit=leftover+returned;o.assistantInventoryCreditSources=sources;
|
||||
o.assistantInventoryCreditExplanation=sources.map(source=>source.explanation).join(';');
|
||||
if(manual.length){o.assistantInventoryCredit=leftover+returned;o.assistantInventoryCreditSources=sources;o.assistantInventoryCreditExplanation=sources.map(source=>source.explanation).join(';');}
|
||||
}
|
||||
if((!priceLock||LockPools.find(this,priceLock.id))&&o.status==='returned'&&o.lines.some(line=>line.costMaterial?.mode==='pool')){
|
||||
const pooled=o.lines.filter(line=>line.costMaterial?.mode==='pool');
|
||||
if((!priceLock||LockPools.findAll(this,priceLock.id).length)&&o.status==='returned'&&o.lines.some(line=>line.costMaterial?.mode==='pool'||line.priceLockPool)){
|
||||
const pooled=o.lines.filter(line=>line.costMaterial?.mode==='pool'||line.priceLockPool);
|
||||
const retained=Calc.orderCosts({...o,lines:pooled,status:'delivered'}).rows.filter(row=>row.kind==='goods'&&row.payer==='assistant'&&!row.materialPoolId);
|
||||
const amount=sum(retained,'amount');
|
||||
if(amount){
|
||||
o.assistantInventoryCredit=amount;
|
||||
o.assistantInventoryCreditSources=[{kind:'returnedGoods',amount,explanation:'日购料关联商品已退回自有货盘,助理垫付的进货工费、配件和其他货品成本抵扣货款待结;料费由日购料剩余垫款单独抵扣'}];
|
||||
o.assistantInventoryCreditExplanation=o.assistantInventoryCreditSources[0].explanation;
|
||||
o.assistantInventoryCredit=(o.assistantInventoryCredit||0)+amount;
|
||||
o.assistantInventoryCreditSources=[...(o.assistantInventoryCreditSources||[]),{kind:'returnedGoods',amount,explanation:'日购料关联商品已退回自有货盘,助理垫付的进货工费、配件和其他货品成本抵扣货款待结;料费由日购料剩余垫款单独抵扣'}];
|
||||
o.assistantInventoryCreditExplanation=o.assistantInventoryCreditSources.map(source=>source.explanation).join(';');
|
||||
}
|
||||
}
|
||||
o.ownerDirect=sum(o.payments,p=>p.account.startsWith('麦麦')?(p.kind==='receipt'?p.amount:-p.amount):0);
|
||||
@@ -778,6 +780,14 @@ class Ledger {
|
||||
const paymentMethods=[...new Set((receiptAccounts.length?receiptAccounts:[o.payment_account]).map(account=>account.replace(/^(助理2|助理1|助理|麦麦)/,'')))];
|
||||
return {id:o.id,...(this.teamMode?{member:o.member}:{}),...(o.recovery_id?{recovery_id:o.recovery_id,recovery_mode:'exchange',nonCashOffset:o.nonCashOffset,cashPaid:o.cashPaid,netSettlement:o.netSettlement}:{}),number:o.number,date:o.date,customer:o.customer,salesperson:o.salesperson,note:o.note,paper_number:o.paper_number,status:o.status,total:o.total,rounding_mode:o.rounding_mode,rounding_adjustment:o.rounding_adjustment,discount:o.discount,received:o.received,refunded:o.refunded,netReceived:o.netReceived,balance:o.balance,paymentMethods:o.recovery_id&&!o.netReceived?[]:paymentMethods,lines:o.lines.map(l=>({name:l.name,code:l.code,unit:l.unit,quantity:l.quantity,sale:l.sale,baseSale:l.baseSale??l.sale,charges:(l.charges||[]).filter(c=>c.amount!==null).map(c=>({name:c.name,amount:c.amount})),priceNote:l.priceNote,...(l.pricingMode==='auto'?{pricingMode:'auto',goldPrice:l.goldPrice,laborRate:l.laborRate,laborMode:l.laborMode,pieces:l.pieces}:{})}))};
|
||||
}
|
||||
priceLockEdit(id,actor){return PriceLockChanges.detail(this,id,actor,priceLockEnv);}
|
||||
editPriceLock(id,input,actor){return PriceLockChanges.edit(this,id,input,actor,priceLockEnv);}
|
||||
cancelPriceLock(id,input,actor){return PriceLockChanges.cancel(this,id,input,actor,priceLockEnv);}
|
||||
requestPriceLockChange(id,input,actor){return PriceLockChanges.request(this,id,input,actor,priceLockEnv);}
|
||||
priceLockChanges(query={},actor){return PriceLockChanges.list(this,query,actor,priceLockEnv);}
|
||||
priceLockChange(id,actor){return PriceLockChanges.read(this,id,actor,priceLockEnv);}
|
||||
approvePriceLockChange(id,input,actor){return PriceLockChanges.approve(this,id,input,actor,priceLockEnv);}
|
||||
rejectPriceLockChange(id,input,actor){return PriceLockChanges.reject(this,id,input,actor,priceLockEnv);}
|
||||
createPriceLock(input,actor){return PriceLocks.create(this,input,actor,priceLockEnv);}
|
||||
priceLocks(query={}){return PriceLocks.list(this,query,priceLockEnv);}
|
||||
priceLockDetail(id){return PriceLocks.detail(this,id,priceLockEnv);}
|
||||
|
||||
Reference in new issue
Block a user