fix: 合同付款预警优化

This commit is contained in:
sophie committed 2026-10-07 18:28:33 +08:00
1 parent c5b7cc662b
commit d57ecc62f9
3 files changed
+115 -6

No files matched your search

+3 -1
View File
@@ -26,11 +26,13 @@ HXOPS_PUBLIC_BASE_URL=https://ait.well-trust.com
AUTH_LOGIN_MODE=casdoor
# 必须与 CASDOOR_BASE_URL discovery 返回的 issuer 完全一致,不能填写 HXOPS 地址。
# 令牌 iss 仍是公网域名,不能改成 IP。证书恢复后把 CASDOOR_BASE_URL 改回 https://autht.well-trust.com。
AUTH_JWT_ISSUER=https://autht.well-trust.com
AUTH_JWT_AUDIENCE=758be5fcba2df784fdb4
AUTH_JWT_JWKS_URL=http://192.168.100.192:8870/.well-known/jwks
# Casdoor
CASDOOR_BASE_URL=https://autht.well-trust.com
# 临时绕开 autht.well-trust.com 过期证书。Casdoor 直连 8870,不经过 443。
CASDOOR_BASE_URL=http://192.168.100.192:8870
# 与 Casdoor 里 Application「hxops-prod」(或你当前生产应用)的 Client ID 一致
CASDOOR_CLIENT_ID=758be5fcba2df784fdb4
CASDOOR_CLIENT_SECRET=522161bea5bb4f90b63f77c41dc26d223f1244ab
@@ -5,6 +5,7 @@ backend/app/services/contract_payment/parse_service.py
依赖:标准库正则与日期处理;原文抽取由调用方传入,避免绑定某一种文件解析器。
逻辑:先识别固定字段和付款比例,再校验金额/比例/日期;无法确定的事实进入 needs_review,不猜测。
合同金额以正文抽出的正数为准;OA 的 0 与空值都不是有效总额。同一付款阶段的相同比例重复抽取不报重复。
增值税税率和「收到定金后 N 天」不计入付款比例。
"""
from __future__ import annotations
@@ -24,7 +25,12 @@ from app.services.procurement_contract.analyze import extract_text_from_bytes
_DATE_RE = re.compile(r"(20\d{2})[\s_]*[年./-][\s_]*(\d{1,2})[\s_]*[月./-][\s_]*(\d{1,2})[\s_]*日?")
_MONEY_RE = re.compile(r"(?:设备总价款\s*[((]?含税[))]?|人民币合同金额|合同金额|合同总价|含税总价)[::\s¥¥]*([\d,]+(?:\.\d+)?)", re.I)
_PERCENT_RE = re.compile(r"(预付款|定金|出货款|货款|验收款|质保款|保款|尾款)[^。;;\n]{0,140}?(\d+(?:\.\d+)?)\s*%", re.S)
_STAGE_LABEL_RE = re.compile(r"预付款|定金|出货款|货款|验收款|质保款|保款|尾款")
_PERCENT_VALUE_RE = re.compile(r"(\d+(?:\.\d+)?)\s*%")
_DEPOSIT_SCHEDULE_RE = re.compile(r"定金\s*后\s*\d+\s*(?:个)?(?:工作日|天|日)")
_SENTENCE_BREAK_RE = re.compile(r"[。;;\n]")
_TAX_PREFIX_RE = re.compile(r"(?:增值税|税率)\s*$")
_TAX_SUFFIX_RE = re.compile(r"\s*(?:的\s*)?增值税")
_DELIVERY_RE = re.compile(r"(?:交货期限|约定到货日期|交付期限|交货期)[::\s]*[^。;;\n]{0,60}?(20\d{2}[^\n。;;]*)")
_WARRANTY_RE = re.compile(r"(?:质保期|保修期|保固期|质保款[^。;;\n]{0,100}?)(?:满后|为|:|:)?\s*(\d+)\s*(年|个月|月|天)")
_WARRANTY_PAYMENT_RE = re.compile(
@@ -146,12 +152,82 @@ def unique_parse_issues(issues: list[Any] | None) -> list[str]:
return result
def _is_tax_percentage(body: str, number_start: int, percent_end: int) -> bool:
"""
判断百分号是增值税或税率,而不是付款比例。
核心逻辑:数字前紧挨「增值税」或「税率」,或百分号后紧挨「增值税」,都视为税率。
「含13%增值税」靠百分号后面的「增值税」识别;单独的「含30%」仍可当作付款比例。
Args:
body: 合同正文。
number_start: 百分数数字在正文中的起始下标。
percent_end: 百分号结束下标,不含其后的文字。
Returns:
该百分号是税率时返回 True。
"""
before = body[max(0, number_start - 16):number_start]
after = body[percent_end:percent_end + 16]
return _TAX_PREFIX_RE.search(before) is not None or _TAX_SUFFIX_RE.match(after) is not None
def _first_payment_percentage(body: str, label_end: int) -> re.Match[str] | None:
"""
在阶段词后的同一句、140 字内取第一个非税率百分号。
核心逻辑:税率百分号跳过并继续看,避免「含13%增值税」挡住同一句里的「总价款的30%」。
遇到句号、分号或换行即停止,窗口长度与原先一致。
Args:
body: 合同正文。
label_end: 阶段词结束下标。
Returns:
付款比例的正则匹配;窗口内没有可用百分号时返回 None。
"""
window_end = label_end + 140
sentence = _SENTENCE_BREAK_RE.search(body, label_end)
if sentence and sentence.start() < window_end:
window_end = sentence.start()
cursor = label_end
while cursor < window_end:
value_match = _PERCENT_VALUE_RE.search(body, cursor, window_end)
if value_match is None:
return None
if _is_tax_percentage(body, value_match.start(1), value_match.end()):
cursor = value_match.end()
continue
return value_match
return None
def _matched_payment_percentages(body: str) -> list[tuple[str, str]]:
"""
抽出每个付款阶段词对应的第一个付款比例。
核心逻辑:「收到定金后 N 天」是完成日期,不作为预付款锚点。
其余阶段词沿用原窗口,但丢掉增值税、税率百分号。
Args:
body: 已抽取的合同正文。
Returns:
(阶段词, 百分数字符串) 列表,按出现顺序。未命中付款比例的阶段词不出现。
"""
matched: list[tuple[str, str]] = []
for label_match in _STAGE_LABEL_RE.finditer(body):
label = label_match.group(0)
if label == "定金" and _DEPOSIT_SCHEDULE_RE.match(body, label_match.start()):
continue
value_match = _first_payment_percentage(body, label_match.end())
if value_match is None:
continue
matched.append((label, value_match.group(1)))
return matched
def _extract_payment_percentages(body: str) -> tuple[dict[str, Decimal], list[str]]:
"""
提取四阶段付款比例,并校验阶段完整性及比例合计。
核心逻辑:同一语义阶段只允许一个比例。正文里同一比例被抽到多次(表格/页眉重复)
视为同一事实,不报重复;只有同一阶段出现不同比例才阻断。四阶段须完整且合计 100%。
增值税税率和「收到定金后 N 天」不计入付款比例。
Args:
body: 已抽取的合同正文。
Returns:
@@ -159,8 +235,8 @@ def _extract_payment_percentages(body: str) -> tuple[dict[str, Decimal], list[st
"""
found: dict[str, list[Decimal]] = {node_type: [] for node_type in _PAYMENT_NODE_TYPES}
issues: list[str] = []
for match in _PERCENT_RE.finditer(body):
label, raw = match.groups()
matched = _matched_payment_percentages(body)
for label, raw in matched:
node_type = _PAYMENT_LABEL_TO_NODE_TYPE[label]
percentage = _parse_decimal(raw)
if percentage is None:
@@ -181,7 +257,6 @@ def _extract_payment_percentages(body: str) -> tuple[dict[str, Decimal], list[st
if missing:
issues.append(f"付款阶段比例无法识别:{'、'.join(_PAYMENT_NODE_LABELS[item] for item in missing)}")
try:
matched = [(m.group(1), m.group(2)) for m in _PERCENT_RE.finditer(body)]
snippet = ""
for label in ("质保款", "验收款", "尾款"):
idx = body.find(label)
+33 -1
View File
@@ -3,7 +3,7 @@ backend/tests/test_contract_payment_parse.py
用途:验证合同付款预警解析器、三轮预警升级、ERP 金额匹配、周汇总摘要及企微 Excel 收件人列。
依赖:contract_payment.parse_service、alert_service、erp_payment_service、supervisor_oa_service(三轮测试 mock)、openpyxl。
逻辑:覆盖确定性解析边界、pending 计数与下一次周五 18:00;三轮升级 mock OA 矩阵,不调用外部 OA、文档库或企业微信;Excel 用例只断言「收件人」列为真实姓名;失败原因用例只断言轨迹归类;OA 金额为 0 时断言正文正数回写台账;合同原文被清理后断言按附件 ID 重新下载;OA「公司」决定 ERP 账套,同一采购单号不能跨账套记成已付。
逻辑:覆盖确定性解析边界、pending 计数与下一次周五 18:00;三轮升级 mock OA 矩阵,不调用外部 OA、文档库或企业微信;Excel 用例只断言「收件人」列为真实姓名;失败原因用例只断言轨迹归类;OA 金额为 0 时断言正文正数回写台账;合同原文被清理后断言按附件 ID 重新下载;OA「公司」决定 ERP 账套,同一采购单号不能跨账套记成已付;增值税税率和「收到定金后 N 天」不计入付款比例。
"""
from datetime import date, datetime
@@ -443,6 +443,38 @@ def test_conflicting_shipment_percentages_report_duplicate_once():
assert not any(node["node_type"] == "shipment" for node in result["nodes"])
def test_vat_rate_beside_deposit_schedule_is_not_prepayment_percentage():
"""关键断言:完成日期「收到定金后135天」后的含13%增值税不算预付款,结算条款的定金30%仍保留。"""
result = parse_contract_text(
"设备定作合同 收到定金后 135天 合计(人民币大写)壹佰伍拾伍万元整(含13%增值税) "
"四、结算方式及期限:甲方支付乙方定金:¥465,000.00元(总价款的30%),"
"甲方支付乙方发货款:¥930,000.00元(总价款的60%),"
"验收款(总价款的10%)¥155,000.00元。"
"设备总价款(含税):1550000.00元"
)
percentages = {node["node_type"]: node["percentage"] for node in result["nodes"]}
assert percentages["prepayment"] == "30"
assert percentages["shipment"] == "60"
assert percentages["acceptance"] == "10"
assert "warranty" not in percentages
assert "预付款付款比例重复,需人工确认" not in result["issues"]
assert "付款阶段比例无法识别:质保款" in result["issues"]
def test_tax_rate_in_same_clause_does_not_hide_payment_percentage():
"""关键断言:同一句里先写含13%增值税、再写总价款30%时,预付款取30%,不取税率。"""
result = parse_contract_text(
"采购协议 合同号:HT-2026-006;合同总价:100,000元;"
"定金(含13%增值税)为总价款的30%;出货款40%;验收款20%;质保款10%;交货期:2026年09月01日。"
)
assert result["status"] == "parsed"
prepayment = next(item for item in result["nodes"] if item["node_type"] == "prepayment")
assert prepayment["percentage"] == "30"
assert "预付款付款比例重复,需人工确认" not in result["issues"]
def test_dms_resolve_year_folder_uses_configured_root(app, monkeypatch):
"""关键断言:DMS 先用根 FolderID 查年份目录,并返回目录记录 ID。"""
app.config.update(