来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
80 lines
4.1 KiB
TypeScript
80 lines
4.1 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import { describe, expect, it } from 'vitest';
|
||
import { miscShareForCustomerDay } from '../src/services/customerBudgetAlertService';
|
||
|
||
/**
|
||
* 成本/销售预警「单个客户对应时间段的所有成本」口径钉子(开发者 2026-09-22 定稿)。
|
||
*
|
||
* 公式:
|
||
* 单个客户对应时间段的所有成本
|
||
* = ① 该客户商品成本总额(**订单行口径**:order_items.quantity × unit_cost)
|
||
* + ② 固定成本**本月总金额**(「固定成本」界面口径)÷ 本月天数 ÷ 当前启用客户数
|
||
* + ③ 该客户配送员产生的其他成本 ÷ **该员当天实际配送客户数**
|
||
*
|
||
* 三条硬规则(本次修订):
|
||
* - **独立自采入库不计入**:它是给加工商品算的,领料后成本价会回写订单行 unit_cost,
|
||
* 已在①体现;再按客户均摊即重复计算。
|
||
* - ② 的分子取 `fixed_costs` 本月合计(不是账本里含其它间接行的"期间固定"池)。
|
||
* - ③ 严格按当天实际配送客户数:当天该客户无配送记录 → 0(= 当天没下单、没配送),
|
||
* 不再用"该员的绑定客户数"兜底。
|
||
*/
|
||
const read = (rel: string) => readFileSync(new URL(`../../${rel}`, import.meta.url), 'utf8');
|
||
|
||
describe('成本/销售预警:单个客户对应时间段的所有成本', () => {
|
||
const src = read('server/src/services/customerBudgetAlertService.ts');
|
||
|
||
it('① 商品成本用订单行口径(quantity × unit_cost),不再用 cost_details 账本汇总', () => {
|
||
expect(src).toContain('COALESCE(oi.quantity, 0)::numeric * COALESCE(oi.unit_cost, 0)::numeric');
|
||
// 列表的 sku_cost 查询里不应再出现 cost_details
|
||
const skuBlock = src.slice(src.indexOf('AS sku_cost') - 800, src.indexOf('AS sku_cost') + 100);
|
||
expect(skuBlock).not.toContain('cost_details');
|
||
});
|
||
|
||
it('② 固定成本取 fixed_costs 本月总金额 ÷ 本月天数 ÷ 当前启用客户数', () => {
|
||
expect(src).toContain('FROM fixed_costs');
|
||
expect(src).toContain('COALESCE(actual_amount, budget_amount, 0)');
|
||
expect(src).toContain('COALESCE(status, 10) >= 20');
|
||
expect(src).toContain('daysInMonth(ym)');
|
||
expect(src).toContain('fixedShare += shareAmount(dailyFixed, enabledCount)');
|
||
});
|
||
|
||
it('独立自采入库不计入(去掉 spi 项)', () => {
|
||
expect(src).not.toContain('self_purchase_stock_inbounds');
|
||
expect(src).not.toContain('spiByDay');
|
||
expect(src).not.toContain("'spi'");
|
||
});
|
||
|
||
it('③ 严格按当天实际配送客户数:无配送记录即 0(无绑定客户数兜底)', () => {
|
||
const lookup = {
|
||
personByCustDay: new Map<string, string>(),
|
||
stopsByDayPerson: new Map<string, number>(),
|
||
miscByDayPerson: new Map<string, number>([['2026-09-16|P1', 300]]),
|
||
};
|
||
// 当天该客户没有配送记录 → 0(即使该员当天有杂费)
|
||
expect(miscShareForCustomerDay('C1', '2026-09-16', lookup)).toBe(0);
|
||
|
||
lookup.personByCustDay.set('C1|2026-09-16', 'P1');
|
||
lookup.stopsByDayPerson.set('2026-09-16|P1', 3);
|
||
expect(miscShareForCustomerDay('C1', '2026-09-16', lookup)).toBe(100);
|
||
|
||
// 有配送记录但该员当天没有停靠数(数据不全)→ 0,而不是回退到绑定客户数
|
||
lookup.stopsByDayPerson.set('2026-09-16|P1', 0);
|
||
expect(miscShareForCustomerDay('C1', '2026-09-16', lookup)).toBe(0);
|
||
|
||
// 源码里不应再有绑定客户数兜底
|
||
expect(src).not.toContain('bindCountByPerson');
|
||
});
|
||
|
||
it('三语文案同步:不再出现「独立入库 / independent inbound / inbound mandiri」口径', () => {
|
||
for (const loc of ['zh-CN', 'en', 'id']) {
|
||
const i18n = read(`src/i18n/locales/${loc}/finance.ts`);
|
||
const intro = i18n.slice(i18n.indexOf("'cost.alert.intro'"), i18n.indexOf('\n', i18n.indexOf("'cost.alert.intro'")));
|
||
expect(intro, loc).not.toMatch(/独立入库|independent inbound|inbound mandiri/);
|
||
const fixedWord = loc === 'zh-CN' ? '固定成本' : loc === 'id' ? 'tetap' : 'fixed';
|
||
expect(intro, loc).toContain(fixedWord);
|
||
}
|
||
const zh = read('src/i18n/locales/zh-CN/finance.ts');
|
||
expect(zh).toContain('单个客户对应时间段的所有成本');
|
||
});
|
||
});
|