Files
SupplierSystem/server/src/services/financeNetProfitFormula.ts
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库: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 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

93 lines
3.3 KiB
TypeScript
Raw 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.
/**
* ============================================================================
* 财务概览 · 净利润公式(锁定)
* ============================================================================
*
* 本文件是净利润计算的**唯一权威**。修改口径只允许改这里,并同步:
* - docs/design/20260809-lock-net-profit-formula.md
* - docs/design/20260809-net-profit-fixed-cost-day-alloc.md
* - server/tests/financeNetProfitFormula.test.ts
*
* 禁止在 marginQueryService / 前端 / 其他服务内联另一套净利润算术。
*
* 公式:
* 净利润 = 销售额 − 商品成本 − 期间固定 − 期间其他
* 期间固定:含 reference_type=fixed_cost 的按天分摊(含挂订单),按入账日入区间。
*/
/** 公式版本 ID:契约测试与文档引用;改公式必须改 ID 与测试 */
export const NET_PROFIT_FORMULA_ID = 'finance_overview_net_profit_v20260810' as const;
/** 对人展示的固定文案(勿在各页面各自改写);商品侧=账本采购(一套账定稿) */
export const NET_PROFIT_FORMULA_LABEL =
'销售额 − 采购成本 − 期间固定 − 期间其他' as const;
/**
* cost_details 上「期间固定」谓词(别名必须为 cd)。
* 含 fixed_cost 挂单分摊;排除 free_overhead(走期间其他)。
*/
export const PERIOD_FIXED_COST_SQL_PREDICATE = `(
cd.reference_type = 'fixed_cost'
OR (
(cd.order_code IS NULL OR TRIM(cd.order_code) = '')
AND cd.cost_type IS DISTINCT FROM 'purchase'
AND cd.cost_type IS DISTINCT FROM 'free_overhead'
AND cd.cost_type IS DISTINCT FROM 'adjustment'
AND cd.cost_type IS DISTINCT FROM 'refund'
AND (cd.reference_type IS NULL OR cd.reference_type IS DISTINCT FROM 'fixed_cost')
)
)` as const;
export type FinanceNetProfitInput = {
/** 销售额(订单当前预估) */
revenue: number;
/** 采购成本(cost_details 账本,一套账权威) */
productCost: number;
/** 期间固定(见 PERIOD_FIXED_COST_SQL_PREDICATE) */
periodFixedCost: number;
/** 期间其他(free_overhead) */
periodFreeCost: number;
};
export type FinanceNetProfitResult = {
formulaId: typeof NET_PROFIT_FORMULA_ID;
formulaLabel: typeof NET_PROFIT_FORMULA_LABEL;
grossProfit: number;
netProfit: number;
netProfitMarginPct: number;
};
function n(value: unknown): number {
const x = Number(value);
return Number.isFinite(x) ? x : 0;
}
function round2(value: number): number {
return Math.round((value + Number.EPSILON) * 100) / 100;
}
/**
* 计算商品毛利与净利润。算术只允许出现在本函数内。
*/
export function computeFinanceNetProfit(input: FinanceNetProfitInput): FinanceNetProfitResult {
const revenue = n(input.revenue);
const productCost = n(input.productCost);
const periodFixedCost = n(input.periodFixedCost);
const periodFreeCost = n(input.periodFreeCost);
const grossProfit = revenue - productCost;
const netProfit = revenue - productCost - periodFixedCost - periodFreeCost;
const netProfitMarginPct = revenue > 0 ? round2((netProfit / revenue) * 100) : 0;
return {
formulaId: NET_PROFIT_FORMULA_ID,
formulaLabel: NET_PROFIT_FORMULA_LABEL,
grossProfit: round2(grossProfit),
netProfit: round2(netProfit),
netProfitMarginPct,
};
}
/** 冻结导出,降低运行时被挂属性篡改的风险 */
Object.freeze(computeFinanceNetProfit);