feat(invoice-check): 立账凭证优化

This commit is contained in:
sophie committed 2026-09-30 16:12:27 +08:00
1 parent 22b05c91a2
commit 425ed43649
7 files changed
+207 -65

No files matched your search

@@ -4,11 +4,13 @@ backend/app/services/voucher_check/notification.py
用途:立账凭证检核缺件通知、以及检核 / PO 生成排程的技术失败告警。
依赖:wecom_notify、notify_config_service、ScheduleTask.notify_user_ids_json、engine_persistence。
逻辑:缺件按文件类型接收人拆成每人一封,正文只含该人负责且本批缺失的类型;
卡片标题为「销售开票附件缺件」,不写业务方代号和批次号;
排程「启用通知」为总开关;技术失败仍走排程通知人;domain_notification_managed 避免通用排程重复通知。
"""
from __future__ import annotations
import re
import uuid
from datetime import datetime
from typing import Any
@@ -23,6 +25,46 @@ from app.services.voucher_check.engine_persistence import persist_notification
_TZ = ZoneInfo("Asia/Shanghai")
def _friendly_period_label(period_month: str) -> str:
"""
把 YYYY-MM 收成企微卡片上的中文年月。
核心逻辑:收件人看「2026年8月」,不看 2026-08;解析失败时保留原文。
"""
text = str(period_month or "").strip()
match = re.fullmatch(r"(\d{4})-(\d{2})", text)
if not match:
return text or "-"
return f"{int(match.group(1))}年{int(match.group(2))}月"
def _missing_type_line(item: dict[str, Any]) -> str:
"""一条缺件类型:名称、缺件份数、涉及发票张数。"""
name = str(item.get("name") or item.get("code") or "")
files = int(item.get("missing_file_count") or 0)
invoices = int(item.get("missing_invoice_count") or 0)
return f"{name}:缺 {files} 份,涉及 {invoices} 张发票"
def _missing_textcard(period_month: str, items: list[dict[str, Any]]) -> tuple[str, str]:
"""
组装销售开票附件缺件 textcard。
核心逻辑:用纯文本而不是 div 着色。企微 description 约 512 字节,
颜色标签会挤掉后面的缺件类型。
Returns:
(title, description)
"""
body = "\n".join(
[
_friendly_period_label(period_month),
"销售开票附件已检核,你负责的类型还有缺件:",
*[_missing_type_line(item) for item in items],
"请尽快补齐。完整清单见详情里的 Excel。",
]
)
return "销售开票附件缺件", body
def _users_by_ids(user_ids: list[int]) -> list[User]:
"""按 id 取出已绑定企微的用户,保持传入顺序。"""
if not user_ids:
@@ -84,7 +126,7 @@ def send_batch_missing_summary(
) -> str:
"""
按文件类型接收人发送缺件 textcard。
核心逻辑:每人一封,只列出该人负责且本批缺失的类型;无缺件或无人可发则写审计不发空消息。
核心逻辑:每人一封,只列出该人负责且本批缺失的类型;标题写明销售开票附件,不写业务方代号和批次号。无缺件或无人可发则写审计不发空消息。
Returns:
send_status:sent / skipped_no_missing / skipped_no_recipients / skipped_disabled / failed
@@ -177,19 +219,7 @@ def send_batch_missing_summary(
sent_ids: list[int] = []
for user in users:
items = plan["user_rules"].get(int(user.id)) or []
type_lines = [
f"{item['name']} {item['missing_file_count']} 份 / {item['missing_invoice_count']} 张 invoice"
for item in items
]
title = f"立账凭证缺件: {task.name}"
description = (
f"业务方 {profile_code}\n"
f"资料所属期间 {period_month}\n"
f"批次 {schedule_run.run_no}\n"
f"你负责的缺件:\n"
+ "\n".join(type_lines)
+ "\n明细以工作台 Excel 为准。"
)
title, description = _missing_textcard(period_month, items)
trace_id = uuid.uuid4().hex
jump_url = _build_jump_url(
"/workspace/accounting-voucher-check",