diff --git a/backend/app/services/voucher_check/notification.py b/backend/app/services/voucher_check/notification.py index 946c0343..cb2f08c9 100644 --- a/backend/app/services/voucher_check/notification.py +++ b/backend/app/services/voucher_check/notification.py @@ -5,6 +5,7 @@ backend/app/services/voucher_check/notification.py 依赖:wecom_notify、notify_config_service、ScheduleTask.notify_user_ids_json、engine_persistence。 逻辑:缺件按文件类型接收人拆成每人一封,正文只含该人负责且本批缺失的类型; 卡片标题为「销售开票附件缺件提醒」,不写业务方代号和批次号; + 卡片发出后紧跟本批检核结果 Excel,收件人不用进详情才知道缺哪张票; 排程「启用通知」为总开关;技术失败仍走排程通知人;domain_notification_managed 避免通用排程重复通知。 """ @@ -59,7 +60,7 @@ def _missing_textcard(period_month: str, items: list[dict[str, Any]]) -> tuple[s _friendly_period_label(period_month), "销售开票附件已检核,你负责的类型还有缺件:", *[_missing_type_line(item) for item in items], - "请尽快补齐。完整清单见详情里的 Excel。", + "请尽快补齐。缺件明细见下一条 Excel。", ] ) return "销售开票附件缺件提醒", body @@ -113,6 +114,36 @@ def _persist_skip( return send_status +def _send_excel_file(wecom_userid: str, media_id: str) -> bool: + """ + 把已上传的检核结果 Excel 发给一个收件人。 + 核心逻辑:企微文件消息与 textcard 是两条消息,失败只记日志,不回滚已发出的卡片。 + + Returns: + 企业微信 errcode 为 0 时为 True。 + """ + from app.services.integrations.wecom.notify import send_wecom_file_message + + try: + data = send_wecom_file_message(wecom_userid, media_id) + except Exception as exc: + current_app.logger.warning( + "[voucher-check] excel file failed user=%s err=%s", + wecom_userid, + exc, + ) + return False + if data.get("errcode") == 0: + return True + current_app.logger.warning( + "[voucher-check] excel file failed user=%s errcode=%s errmsg=%s", + wecom_userid, + data.get("errcode"), + data.get("errmsg"), + ) + return False + + def send_batch_missing_summary( *, task: ScheduleTask, @@ -122,11 +153,17 @@ def send_batch_missing_summary( period_month: str, stats: dict[str, Any], excel_filename: str | None, + excel_bytes: bytes | None = None, enabled: bool, ) -> str: """ - 按文件类型接收人发送缺件 textcard。 - 核心逻辑:每人一封,只列出该人负责且本批缺失的类型;标题写明销售开票附件,不写业务方代号和批次号。无缺件或无人可发则写审计不发空消息。 + 按文件类型接收人发送缺件 textcard,并附上本批检核结果 Excel。 + 核心逻辑:每人一封,只列出该人负责且本批缺失的类型;标题写明销售开票附件,不写业务方代号和批次号。 + Excel 全批共用一份,上传一次后随卡片发给每个收件人。附件失败不挡住卡片,原因写入审计。 + 无缺件或无人可发则写审计不发空消息。 + + Args: + excel_bytes: 与排程落库同一份检核结果。为空时只发卡片。 Returns: send_status:sent / skipped_no_missing / skipped_no_recipients / skipped_disabled / failed @@ -213,9 +250,20 @@ def send_batch_missing_summary( _build_jump_url, _create_audit, send_wecom_textcard_message, + upload_wecom_temp_media, ) + media_id = None + file_note = None + if excel_bytes and excel_filename: + try: + media_id = upload_wecom_temp_media(excel_bytes, excel_filename) + except Exception as exc: + file_note = "Excel 附件上传失败" + current_app.logger.warning("[voucher-check] excel upload failed: %s", exc) + ok_any = False + file_failed = False sent_ids: list[int] = [] for user in users: items = plan["user_rules"].get(int(user.id)) or [] @@ -234,6 +282,8 @@ def send_batch_missing_summary( audit.status = "sent" ok_any = True sent_ids.append(int(user.id)) + if media_id and not _send_excel_file(user.wecom_userid, media_id): + file_failed = True else: audit.error_code = str(data.get("errcode")) audit.error_message = str(data.get("errmsg", "unknown"))[:255] @@ -244,11 +294,16 @@ def send_batch_missing_summary( exc, ) - skip_reason = None + if file_failed and not file_note: + file_note = "部分 Excel 附件发送失败" + reasons: list[str] = [] if not ok_any: - skip_reason = "企业微信发送失败" + reasons.append("企业微信发送失败") elif plan["unmapped_codes"]: - skip_reason = "部分文件类型未配置接收人" + reasons.append("部分文件类型未配置接收人") + if file_note: + reasons.append(file_note) + skip_reason = ";".join(reasons) or None persist_notification( run_id=engine_run_id, excel_filename=excel_filename, diff --git a/backend/app/services/voucher_check/runner.py b/backend/app/services/voucher_check/runner.py index 812c90d0..5823e73d 100644 --- a/backend/app/services/voucher_check/runner.py +++ b/backend/app/services/voucher_check/runner.py @@ -4,7 +4,7 @@ backend/app/services/voucher_check/runner.py 用途:销售一期周批次编排(上月清单、目录扫描、按票归档、缺件聚合、Excel、企微汇总)。 依赖:profile_loader、adapters、dms_client、aggregator、engine_persistence、excel_rows、notification。 逻辑:不调用 erp_po_print.fetch / oa_sales_release.fetch;PO PDF 由 accounting-voucher-po-print(周二 05:00)、销货放行单由 accounting-voucher-oa-sales-release(周二 05:30)提前写入类型目录。 - 命中文件复制到所有归档,同名跳过仍记 archived。 + 命中文件复制到所有归档,同名跳过仍记 archived。缺件通知附带与排程落库相同的检核结果 Excel。 """ from __future__ import annotations @@ -28,7 +28,11 @@ from app.services.voucher_check.engine_persistence import ( persist_engine_results, ) from app.services.voucher_check.erp_repository import previous_period_bounds -from app.services.voucher_check.excel_rows import to_excel_invoice_row +from app.services.voucher_check.excel_rows import ( + VOUCHER_CHECK_HEADER_LABELS, + VOUCHER_CHECK_HEADER_ORDER, + to_excel_invoice_row, +) from app.services.voucher_check.notification import send_batch_missing_summary, send_failure_alert from app.services.voucher_check.profile_loader import ensure_published_profile, load_profile_definition from app.services.voucher_check.profiles.sales import required_file_rules @@ -369,8 +373,18 @@ def run_voucher_check_batch(task: ScheduleTask, run: ScheduleRun) -> ScheduleExe to_excel_invoice_row(item, period=period, executed_at=executed_at) for item in stats["excel_rows"] ] + from app.services.schedule.excel_service import export_schedule_result_excel - # [STEP 4] 按文件类型接收人拆成每人一封缺件通知 + # 与排程落库用同一导出,保证企微附件和事后下载是同一张表。 + excel_bytes, _, _ = export_schedule_result_excel( + run_no=run.run_no, + summary={"period": period}, + details=details, + header_order=list(VOUCHER_CHECK_HEADER_ORDER), + header_labels=VOUCHER_CHECK_HEADER_LABELS, + ) + + # [STEP 4] 按文件类型接收人拆成每人一封缺件通知,并附上本批 Excel notify_status = send_batch_missing_summary( task=task, schedule_run=run, @@ -379,6 +393,7 @@ def run_voucher_check_batch(task: ScheduleTask, run: ScheduleRun) -> ScheduleExe period_month=period, stats=stats, excel_filename=excel_filename, + excel_bytes=excel_bytes, enabled=config.notify_missing_enabled, ) step_logs.append( diff --git a/backend/tests/voucher_check/test_notify_config.py b/backend/tests/voucher_check/test_notify_config.py index 2f4be4f8..018919da 100644 --- a/backend/tests/voucher_check/test_notify_config.py +++ b/backend/tests/voucher_check/test_notify_config.py @@ -3,7 +3,7 @@ backend/tests/voucher_check/test_notify_config.py 用途:按文件类型配置缺件接收人,以及把缺件类型拆成每人一封。 依赖:pytest、Flask client、notify_config_service、notification.send_batch_missing_summary。 -逻辑:GET/PUT 仅 is_platform_admin;未知规则码丢弃;发送时每人只看到自己负责且本批缺失的类型。 +逻辑:GET/PUT 仅 is_platform_admin;未知规则码丢弃;发送时每人只看到自己负责且本批缺失的类型,并收到同一份 Excel。 """ from __future__ import annotations @@ -179,14 +179,26 @@ def test_notify_config_rejects_non_admin_headers(app, client, user_headers): def test_send_batch_splits_one_card_per_user(app, monkeypatch): - """缺件通知按人拆信,正文只含该人负责的缺失类型。""" + """缺件通知按人拆信,正文只含该人负责的缺失类型,并给每人附上同一份 Excel。""" sent: list[tuple[str, str, str]] = [] + uploaded: list[tuple[bytes, str]] = [] + files: list[tuple[str, str]] = [] def _fake_send(wecom_userid, title, description, jump_url): sent.append((wecom_userid, title, description)) return {"errcode": 0} + def _fake_upload(data, filename): + uploaded.append((data, filename)) + return "media-vc-1" + + def _fake_file(wecom_userid, media_id): + files.append((wecom_userid, media_id)) + return {"errcode": 0} + monkeypatch.setattr("app.services.integrations.wecom.notify.send_wecom_textcard_message", _fake_send) + monkeypatch.setattr("app.services.integrations.wecom.notify.upload_wecom_temp_media", _fake_upload) + monkeypatch.setattr("app.services.integrations.wecom.notify.send_wecom_file_message", _fake_file) monkeypatch.setattr( "app.services.integrations.wecom.notify._create_audit", lambda *args, **kwargs: SimpleNamespace( @@ -279,6 +291,7 @@ def test_send_batch_splits_one_card_per_user(app, monkeypatch): }, }, excel_filename="voucher_check_sales_2026-08.xlsx", + excel_bytes=b"voucher-xlsx", enabled=True, ) assert status == "sent" @@ -306,3 +319,7 @@ def test_send_batch_splits_one_card_per_user(app, monkeypatch): assert "发票:缺" not in po_body assert "提单" in bl_body assert "PO订单:缺" not in bl_body + # 关键断言:同一份 Excel 只上传一次,两个收件人都收到文件消息。 + assert uploaded == [(b"voucher-xlsx", "voucher_check_sales_2026-08.xlsx")] + assert files == [("wx-po-recv", "media-vc-1"), ("wx-bl-recv", "media-vc-1")] + assert "下一条 Excel" in po_body diff --git a/docs/features/accounting-voucher-check/demand.md b/docs/features/accounting-voucher-check/demand.md index 8efdce2b..0d7501f7 100644 --- a/docs/features/accounting-voucher-check/demand.md +++ b/docs/features/accounting-voucher-check/demand.md @@ -244,7 +244,7 @@ flowchart TD | -------- | ------------------------------------------------------------------------------------------------------- | | 触发 | 本批存在必检文件缺件 | | 聚合 | 审计仍是一个检核批次一条;企微按接收人拆信,不在正文逐票展开缺失明细 | -| 内容 | 标题为「销售开票附件缺件提醒」。正文为中文年月、该人负责且本批缺失的文件类型(缺多少份、涉及多少张发票),并提示到详情 Excel 补齐。不写业务方代号和批次号 | +| 内容 | 标题为「销售开票附件缺件提醒」。正文为中文年月、该人负责且本批缺失的文件类型(缺多少份、涉及多少张发票),并提示下一条消息是本批 Excel。不写业务方代号和批次号。卡片发出后另发本批检核结果 Excel 文件 | | 明细查看 | 全部缺件 `invoice`、缺失文件列表和处理状态以 Excel 与执行记录为准 | | 接收人 | 平台管理员在 Chat「通知配置」按文件类型从系统用户多选。未配置任何接收人时不发送空通知,并在发送记录标记未发送原因 | | 技术失败 | 不作为缺件通知;按排程通知人单独告警 | @@ -268,7 +268,7 @@ flowchart TD 1. 每周二任务仅处理上一个自然月,支持跨年期间计算和增量跳过。 2. 同一 `invoice` 缺少多个文件时,Excel 的“缺件文件总数”正确累计,“缺件 invoice 数”只计一次。 3. Excel 报告顶部可按资料所属期间选择年月,并单独展示执行时间;每个 `invoice` 一行,直接展示各必检文件为“有 / 缺件 / 未检核”和总体状态;全部必检文件均为“有”时状态为“完整”。 -4. 企业微信按文件类型接收人拆成每人一封,正文只含该人负责的缺件类型,并提供本批 Excel 下载入口;完整 `invoice` 明细不在消息正文展开。审计仍是每批一条。 +4. 企业微信按文件类型接收人拆成每人一封,正文只含该人负责的缺件类型;卡片之后发送本批 Excel 文件。完整 `invoice` 明细不在消息正文展开。审计仍是每批一条。 5. OA 关联字段不完整记录被直接丢弃,不进入异常、缺件、打印或归档结果。 6. 销售 invoice 清单、文档库(同一 LibraryID、销售独立根 FolderID、建文件夹/复制接口)、周任务星期二、按文件类型选人界面已确认;ERP PO 取数:发票 HXJM、采购单 TGHX,关联 `TRIM(oma99)=TRIM(pmm99)` 已接线(独立抓取,见技术设计 §3.4);OA 销货放行单取数已接线(E10 `ft_xqqgsq` + OpenAPI,独立排程周二 05:30),凭据只进环境变量。 7. 归档复制采用「同名跳过」:目标目录已有同名文件不覆盖、不报缺件。文档库自动加上的「(重命名)」改回原名,同一目录多出来的该后缀副本删除。 diff --git a/docs/features/accounting-voucher-check/technical-design.md b/docs/features/accounting-voucher-check/technical-design.md index 2637033c..18ca4105 100644 --- a/docs/features/accounting-voucher-check/technical-design.md +++ b/docs/features/accounting-voucher-check/technical-design.md @@ -318,7 +318,7 @@ ERP/OA 打印或目录扫描的成功文件先写入类型存放目录;完成 ### 6.2 通知聚合 -缺件通知按文件类型接收人拆信:同一人管多类时合并为一封,正文只含该人负责且本批缺失的类型。审计仍以 `run_id` 聚合为一条 `voucher_check_notifications`。接收人由 Chat「通知配置」(仅平台管理员)维护,存在 `voucher_check_notify_configs`,不写进 Profile。未配置则不发送并记录原因。排程「启用通知」与 `notify_missing_enabled` 为总开关;排程选人只用于技术失败告警,不混入缺件通知。 +缺件通知按文件类型接收人拆信:同一人管多类时合并为一封,正文只含该人负责且本批缺失的类型,卡片之后再发本批检核结果 Excel。审计仍以 `run_id` 聚合为一条 `voucher_check_notifications`。接收人由 Chat「通知配置」(仅平台管理员)维护,存在 `voucher_check_notify_configs`,不写进 Profile。未配置则不发送并记录原因。排程「启用通知」与 `notify_missing_enabled` 为总开关;排程选人只用于技术失败告警,不混入缺件通知。 ### 6.3 检核任务列表 diff --git a/frontend/src/features/kb/components/KbJobQueuePanel.vue b/frontend/src/features/kb/components/KbJobQueuePanel.vue index b56a6555..c7b1129e 100644 --- a/frontend/src/features/kb/components/KbJobQueuePanel.vue +++ b/frontend/src/features/kb/components/KbJobQueuePanel.vue @@ -1,12 +1,12 @@