Files

44 lines
6.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
// 每件置换独立计算旧料、差重、单价及来源;仅收付款、优惠和取整整单汇总。
const Calc=require('./public/calculations'),Material=require('./material-pools');
const sum=(rows,key)=>rows.reduce((n,row)=>n+row[key],0);
function quote(l,input,actor,env,options,R){
if(options.creating){env.text(input.customer,'客户称呼/代号',60);env.text(input.salesperson,'开单人',30);}
const items=input.exchange.items;
if(!Array.isArray(items)||!items.length||items.length>60)env.fail('请填写1~60件独立置换商品');
const parts=items.map((item,i)=>{
if(!item||typeof item!=='object'||!item.exchange||item.exchange.items||item.exchange.products||(item.old_source!=='deposit'&&(!Array.isArray(item.old_lines)||item.old_lines.length!==1)))env.fail('第'+(i+1)+'件置换资料无效');
// 内部单件预览不产生收付款,信用卡手续费只按整单实收计算。
return R.quote(l,{...input,...item,old_source:item.old_source,deposit_id:item.deposit_id,deposit_weight:item.deposit_weight,old_lines:item.old_lines,old_pocket:item.old_pocket??'回收旧料',old_stock_count:item.old_stock_count,buy_rate:item.buy_rate,buy_total:item.buy_total,surplus_mode:item.surplus_mode,surplus_memo:item.surplus_memo,mode:'exchange',date:input.date,customer:input.customer,salesperson:input.salesperson,member:input.member,note:input.note,payer:input.payer,payment_account:input.payment_account?.endsWith('信用卡')?'麦麦微信':input.payment_account,exchange:{...item.exchange,charges:[],fee_items:[],discount:'0',rounding_mode:'cent'}},actor,env,{...options,creating:false,existingRecovery:options.existingRecovery?.exchangeItems?.[i]??null});
});
if(options.creating&&parts.some(p=>p.laborCost===null||p.differenceMg&&!p.exchange.costMaterial.rate&&p.exchange.costMaterial.mode!=='pool'))env.fail('请逐件补齐进货工费和差重料费');
// 必须一次试算所有用料,避免多件分别试算时重复使用同一笔剩料。
const probes=parts.map(p=>({name:p.newProduct.name,unit:'g',quantity:p.newMg,cost:0,costParts:{material:p.materialCost,labor:p.laborCost,accessories:0,other:0},costMaterial:p.exchange.costMaterial}));
Material.prepareLines(l,probes,parts[0].date,env,{orderId:options.orderId,member:parts[0].member});
for(const [i,p] of parts.entries())if(p.exchange.costMaterial.mode==='pool'){
p.materialCost=probes[i].costParts.material;p.goodsCost=p.materialCost===null||p.laborCost===null?null:p.materialCost+p.laborCost;
if(probes[i].materialPool?.materialSources)p.exchange.costMaterial.materialSources=probes[i].materialPool.materialSources;
}
const first=parts[0],q={...first,exchangeItems:parts,newProducts:parts.map(p=>({...p.newProduct,laborMode:p.laborMode,laborRate:p.laborRate,laborAmount:p.laborAmount,costLabor:p.exchange.costLabor,laborCost:p.laborCost,laborPayer:p.exchange.costPayers.labor})),old_lines:parts.flatMap(p=>p.old_lines),payment_account:input.payment_account??first.payment_account};
delete q.customerDeposit;delete q.surplusMode;delete q.surplusMemo;
if(parts.some(p=>p.depositMg))q.depositMg=parts.reduce((n,p)=>n+(p.depositMg||0),0);else delete q.depositMg;
for(const key of ['oldMg','newMg','matchedMg','differenceMg','surplusMg','depreciationMg','goldAmount','laborAmount','depreciationAmount','purchaseMg','purchaseAmount','calculatedPurchaseAmount'])q[key]=sum(parts,key);
for(const key of ['materialCost','laborCost','goodsCost'])q[key]=parts.some(p=>p[key]===null)?null:sum(parts,key);
q.charges=Calc.charges(input.exchange.charges??[]).items;if(q.charges.some(p=>p.amount===null))env.fail('请补齐其他收费');q.chargesAmount=sum(q.charges,'amount');
q.discount=env.money(input.exchange.discount??'0','整单优惠');q.rounding_mode=env.pick(input.exchange.rounding_mode??'yuan',['cent','yuan'],'取整方式');
const subtotal=q.goldAmount+q.laborAmount+q.depreciationAmount+q.chargesAmount;if(q.discount>subtotal)env.fail('整单优惠不能超过置换费用');
const total=subtotal===q.discount?{total:0,rounding_adjustment:0}:Calc.orderTotal([subtotal],q.discount,q.rounding_mode);q.receivable=total.total;q.rounding_adjustment=total.rounding_adjustment;
const fee_items=l.validateFees((input.exchange.fee_items??[]).map(f=>({...f,status:'estimated'})));if(fee_items.some(f=>f.amount===null))env.fail('请补齐实际费用');
q.exchange={...first.exchange,fee_items};q.netSettlement=q.receivable-q.purchaseAmount;q.cashReceipt=Math.max(0,q.netSettlement);q.cashPayment=Math.max(0,-q.netSettlement);q.offsetAmount=Math.min(q.receivable,q.purchaseAmount);
q.payment_account=env.paymentAccount(q.payment_account);if(q.cashPayment&&q.payment_account.endsWith('信用卡'))env.fail('回收付款不支持信用卡');
if(options.creating&&q.cashPayment&&require('./team-funds').accountMember(q.payment_account)!==q.payer)env.fail('实际付款账户须与回收付款人一致');
q.cardFee=q.cashReceipt&&q.payment_account.endsWith('信用卡')?env.cardFee(q.cashReceipt,q.credit_card_rate_permille):0;q.directCost=sum(fee_items,'amount')+q.cardFee;q.profit=q.goodsCost===null?null:q.receivable-q.goodsCost-q.directCost;
q.actualBuyRate=q.purchaseMg?Number((BigInt(q.purchaseAmount)*1000n+BigInt(q.purchaseMg)/2n)/BigInt(q.purchaseMg)):null;
q.paymentMethods=q.cashReceipt||q.cashPayment?[q.payment_account.replace(/^(助理2|助理1|助理|麦麦)/,'')]:[];
for(const key of ['oldMg','newMg','differenceMg','purchaseAmount','receivable','directCost','goodsCost','netSettlement'])if(q[key]!==null&&(!Number.isSafeInteger(q[key])||Math.abs(q[key])>100000000000))env.fail('置换汇总超出范围');
require('./customer-materials').validate(l,q,env,{orderId:options.orderId});
return q;
}
function publicItems(q){const fields=['old_lines','oldMg','newMg','matchedMg','differenceMg','surplusMg','depositMg','surplusMode','goldRate','laborRate','laborMode','depreciationMg','depreciationRate','buyRate','goldAmount','laborAmount','depreciationAmount','calculatedPurchaseAmount','purchaseAmount'];return q.exchangeItems.map(p=>({...Object.fromEntries(fields.map(k=>[k,p[k]])),newProduct:{name:p.newProduct.name,quantity:p.newMg}}));}
module.exports={quote,publicItems};