32 lines
1.8 KiB
JavaScript
32 lines
1.8 KiB
JavaScript
'use strict';
|
|
// 月度业绩只重排原订单与售后利润,不生成结算、转账或新的利润分录。
|
|
const DailyProfit=require('./daily-profit');
|
|
const {MEMBERS}=require('./team-members'),ShopSettings=require('./shop-settings');
|
|
function share(profit,percent){
|
|
if(profit===null)return null;
|
|
if(!Number.isSafeInteger(profit)||!Number.isSafeInteger(percent)||percent<0||percent>100)throw new Error('月利润或分成比例无效');
|
|
const sign=profit<0?-1n:1n;
|
|
return Number(sign*((BigInt(Math.abs(profit))*BigInt(percent)+50n)/100n));
|
|
}
|
|
|
|
function report(ledger,query={},actor=ledger.actor,env){
|
|
if(actor?.role!=='owner')env.fail('团队账目仅麦麦账号可查看',403);
|
|
const today=env.today(),month=String(query.month??today.slice(0,7));
|
|
if(!/^\d{4}-(0[1-9]|1[0-2])$/.test(month)||month<'2000-01'||month>today.slice(0,7))env.fail('请选择2000年1月至本月之间的有效月份');
|
|
const from=month+'-01',[year,number]=month.split('-').map(Number);
|
|
const monthEnd=new Date(Date.UTC(year,number,0)).toISOString().slice(0,10),date=monthEnd<today?monthEnd:today;
|
|
const history=ShopSettings.read(ledger).profitShareHistory??[{month:'2000-01',percent:30}];
|
|
const shareRate=[...history].filter(row=>row.month<=month).sort((a,b)=>a.month.localeCompare(b.month)).at(-1)?.percent??30;
|
|
return ledger.scope(actor,()=>{
|
|
const book=ledger.orderBook({from,date,status:'all'});
|
|
const rows=MEMBERS.filter(row=>row.member!=='owner').map(({member,label})=>{
|
|
const summary=DailyProfit.project(ledger,{rows:book.rows,groups:{[member]:{name:label}},from,date}).summary;
|
|
const profit=summary.incomplete?null:summary.profit;
|
|
return {member,name:label,...summary,profit,shareRate,estimatedShare:share(profit,shareRate)};
|
|
});
|
|
return {basis:'orderDate',month,from,date,rows};
|
|
},null);
|
|
}
|
|
|
|
module.exports={report};
|