Files
SupplierSystem/server/src/services/statementScopeSql.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

53 lines
2.8 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.
/**
* 结算单「有有效订单归属」谓词(唯一实现,2026-09-22)。
*
* 背景:2026-07-28 上线前那批试运行订单被整体清除后,留下了只有表头金额、
* 没有任何明细行的**无源空壳结算单**(供应商 93 张 / ¥72,157.87,客户 70 张 / ¥107,149.13,BR002 实测)。
* 它们既不能被核验,也不能被发票池占用 —— 结算页的列表本来就靠「挂到有效订单行」把它们过滤掉了,
* 但**财务概览应付/应收合计、看板待确认单数、应收账龄催收**等汇总此前没有同一条件,
* 于是这些无源金额仍在被统计。
*
* 本谓词 = 结算页列表的同一口径(去掉日期区间那一段):
* - 有至少一条 `statement_lines` 明细挂到「存在且未软删」的订单;或
* - 客户一单一结:`statement_no` 本身就是订单号且该订单存在。
*
* 性能:`statement_lines(statement_no, statement_type)` 与 `orders(order_code)` 均有索引,
* 全部是列等值连接(不要改写成 `a.id::text = b.xxx` 这种不可索引形式,2026-09-22 出过 350s 级事故)。
*
* 设计:docs/design/20260922-supplier-statement-payable-caliber.md
*/
export function statementHasLiveOrderLinkSql(alias: string, type: 'customer' | 'supplier'): string {
const lineLink = `EXISTS (
SELECT 1 FROM statement_lines sl
JOIN orders o ON o.order_code = sl.order_code AND o.deleted_at IS NULL
WHERE sl.statement_no = ${alias}.statement_no
AND sl.statement_type = '${type}'
)`;
if (type === 'supplier') return lineLink;
return `(${lineLink}
OR EXISTS (
SELECT 1 FROM orders o2
WHERE o2.order_code = ${alias}.statement_no AND o2.deleted_at IS NULL
))`;
}
/**
* 明细行「计入结算金额」的口径(唯一实现,生成 / 重算 / 列表 / 明细接口共用):
*
* 未作废 且(无订单号 或 订单存在且未软删)
*
* 为什么必须有后半句:订单被软删(`orders.deleted_at` 非空)后,它的结算明细仍留在 `statement_lines` 里。
* 重算路径一直排除这些行,而 2026-09-22 发现**列表页求和漏了这条**,导致页面「应付总额」虚高
* (实测 BR002:14 行 / ¥1,628.50,5 张单,订单 40C9E70A9D 等已软删)。
* `getStatementLines`(GET 明细时会顺手重算表头)同样漏了,会把虚高值写回表头。
*
* ⚠️ 别改成 `o.id::text = ...` 之类不可索引的形式(2026-09-22 出过 350s 级线上事故)。
*/
export function statementLineCountedSql(alias = 'sl'): string {
return `COALESCE(${alias}.is_voided, false) = false
AND (
${alias}.order_code IS NULL
OR EXISTS (SELECT 1 FROM orders o WHERE o.order_code = ${alias}.order_code AND o.deleted_at IS NULL)
)`;
}