2026-11-06 01:29:35 +08:00
|
|
|
'use strict';
|
|
|
|
|
// 月度业绩只重排原订单与售后利润,不生成结算、转账或新的利润分录。
|
|
|
|
|
const DailyProfit = require('./daily-profit');
|
2026-11-14 01:16:35 +08:00
|
|
|
const TeamMembers = require('./team-members'),
|
|
|
|
|
ShopSettings = require('./shop-settings');
|
|
|
|
|
function share(profit, percent) {
|
2026-11-06 01:29:35 +08:00
|
|
|
if (profit === null) return null;
|
2026-11-14 01:16:35 +08:00
|
|
|
if (!Number.isSafeInteger(profit) || !Number.isSafeInteger(percent) || percent < 0 || percent > 100)
|
|
|
|
|
throw new Error('月利润或分成比例无效');
|
2026-11-06 01:29:35 +08:00
|
|
|
const sign = profit < 0 ? -1n : 1n;
|
2026-11-14 01:16:35 +08:00
|
|
|
return Number(sign * ((BigInt(Math.abs(profit)) * BigInt(percent) + 50n) / 100n));
|
2026-11-06 01:29:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2026-11-14 01:16:35 +08:00
|
|
|
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;
|
2026-11-06 01:29:35 +08:00
|
|
|
return ledger.scope(
|
|
|
|
|
actor,
|
|
|
|
|
() => {
|
|
|
|
|
const book = ledger.orderBook({ from, date, status: 'all' });
|
2026-11-14 01:16:35 +08:00
|
|
|
const rows = TeamMembers.members(ledger)
|
|
|
|
|
.filter(row => row.member !== 'owner')
|
|
|
|
|
.map(({ member, label }) => {
|
2026-11-06 01:29:35 +08:00
|
|
|
const summary = DailyProfit.project(ledger, {
|
|
|
|
|
rows: book.rows,
|
|
|
|
|
groups: { [member]: { name: label } },
|
|
|
|
|
from,
|
|
|
|
|
date,
|
|
|
|
|
}).summary;
|
|
|
|
|
const profit = summary.incomplete ? null : summary.profit;
|
2026-11-14 01:16:35 +08:00
|
|
|
return { member, name: label, ...summary, profit, shareRate, estimatedShare: share(profit, shareRate) };
|
2026-11-06 01:29:35 +08:00
|
|
|
});
|
|
|
|
|
return { basis: 'orderDate', month, from, date, rows };
|
|
|
|
|
},
|
|
|
|
|
null,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { report };
|