feat(invoice-check): 立账凭证检核增加收件人配置并迁移到chat页面
This commit is contained in:
1 parent
18e957545c
commit
7b80b4c395
72 files changed
+2705
-263
No files matched your search
@@ -0,0 +1,300 @@
|
||||
"""
|
||||
backend/tests/voucher_check/test_notify_config.py
|
||||
|
||||
用途:按文件类型配置缺件接收人,以及把缺件类型拆成每人一封。
|
||||
依赖:pytest、Flask client、notify_config_service、notification.send_batch_missing_summary。
|
||||
逻辑:GET/PUT 仅 admin;未知规则码丢弃;发送时每人只看到自己负责且本批缺失的类型。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
from types import SimpleNamespace
|
||||
|
||||
from tests.conftest import parse_api_data, parse_api_error
|
||||
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, User
|
||||
from app.models.voucher_check import VoucherCheckNotification, VoucherCheckNotifyConfig, VoucherCheckRun
|
||||
from app.services.voucher_check.notify_config_service import (
|
||||
normalize_recipients_json,
|
||||
plan_missing_recipient_messages,
|
||||
)
|
||||
from app.services.voucher_check.notification import send_batch_missing_summary
|
||||
|
||||
|
||||
def _ensure_user(
|
||||
*,
|
||||
username: str,
|
||||
role: str = "user",
|
||||
wecom_userid: str | None = None,
|
||||
department: str | None = None,
|
||||
) -> User:
|
||||
"""
|
||||
测试用户幂等创建。
|
||||
|
||||
Args:
|
||||
username: 登录名。
|
||||
role: 角色,默认普通用户。
|
||||
wecom_userid: 企微 userid,有值即视为已绑定。
|
||||
department: 部门名,供候选人接口与选人弹窗分组。
|
||||
"""
|
||||
user = db.session.query(User).filter_by(username=username).first()
|
||||
if user:
|
||||
user.role = role
|
||||
user.status = "active"
|
||||
user.wecom_userid = wecom_userid
|
||||
if department is not None:
|
||||
user.department = department
|
||||
db.session.commit()
|
||||
return user
|
||||
user = User(
|
||||
username=username,
|
||||
email=f"{username}@example.com",
|
||||
role=role,
|
||||
status="active",
|
||||
real_name=username,
|
||||
wecom_userid=wecom_userid,
|
||||
department=department,
|
||||
)
|
||||
user.set_password("test")
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
def test_plan_merges_types_per_user_and_tracks_unmapped():
|
||||
"""同一人管多类时合并到一封;有缺件但无人接收的类型进入 unmapped。"""
|
||||
plan = plan_missing_recipient_messages(
|
||||
by_file_type={
|
||||
"po_order": {
|
||||
"rule_code": "po_order",
|
||||
"rule_name": "PO订单",
|
||||
"missing_file_count": 2,
|
||||
"missing_invoice_count": 1,
|
||||
},
|
||||
"tax_invoice": {
|
||||
"rule_code": "tax_invoice",
|
||||
"rule_name": "发票",
|
||||
"missing_file_count": 3,
|
||||
"missing_invoice_count": 2,
|
||||
},
|
||||
"bill_of_lading": {
|
||||
"rule_code": "bill_of_lading",
|
||||
"rule_name": "提单",
|
||||
"missing_file_count": 1,
|
||||
"missing_invoice_count": 1,
|
||||
},
|
||||
"customs_pack": {
|
||||
"rule_code": "customs_pack",
|
||||
"rule_name": "报关资料",
|
||||
"missing_file_count": 0,
|
||||
"missing_invoice_count": 0,
|
||||
},
|
||||
},
|
||||
recipients_by_rule={
|
||||
"po_order": [11, 12],
|
||||
"tax_invoice": [11],
|
||||
"bill_of_lading": [],
|
||||
},
|
||||
)
|
||||
# 关键断言:11 同时收到 PO 与发票;提单无人配置。
|
||||
assert plan["unmapped_codes"] == ["bill_of_lading"]
|
||||
assert [item["code"] for item in plan["user_rules"][11]] == ["po_order", "tax_invoice"]
|
||||
assert [item["code"] for item in plan["user_rules"][12]] == ["po_order"]
|
||||
|
||||
|
||||
def test_normalize_drops_unknown_rule_codes():
|
||||
"""提交里的未知规则码必须丢弃,不能写进配置表。"""
|
||||
cleaned = normalize_recipients_json(
|
||||
{"po_order": [1, 1, "2", 0, "x"], "unknown": [9]},
|
||||
profile_code="sales",
|
||||
)
|
||||
assert cleaned["po_order"] == [1, 2]
|
||||
assert "unknown" not in cleaned
|
||||
assert "tax_invoice" in cleaned
|
||||
|
||||
|
||||
def test_admin_can_get_and_save_notify_config(app, client, admin_headers):
|
||||
"""管理员可读取六行规则并保存接收人。"""
|
||||
with app.app_context():
|
||||
admin = _ensure_user(username="admin", role="admin")
|
||||
owner = _ensure_user(username="vc_po_owner", wecom_userid="wx-po", department="财务部")
|
||||
owner_id = owner.id
|
||||
admin_id = admin.id
|
||||
|
||||
listed = client.get(
|
||||
"/api/workspace/accounting-voucher-check/notify-config",
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert listed.status_code == 200
|
||||
payload = parse_api_data(listed.get_json())
|
||||
owner_row = next(row for row in payload["users"] if row["id"] == owner_id)
|
||||
# 关键断言:候选人带部门,供公共选人弹窗左侧分组。
|
||||
assert owner_row["department"] == "财务部"
|
||||
assert owner_row["wecom_bound"] is True
|
||||
codes = [row["code"] for row in payload["rules"]]
|
||||
# 关键断言:页面行来自销售 Profile 六类必检文件。
|
||||
assert codes == [
|
||||
"po_order",
|
||||
"customs_pack",
|
||||
"sales_release",
|
||||
"tax_invoice",
|
||||
"customs_declaration",
|
||||
"bill_of_lading",
|
||||
]
|
||||
|
||||
saved = client.put(
|
||||
"/api/workspace/accounting-voucher-check/notify-config",
|
||||
headers=admin_headers,
|
||||
json={
|
||||
"profile_code": "sales",
|
||||
"recipients": {"po_order": [owner_id], "unknown": [admin_id]},
|
||||
},
|
||||
)
|
||||
assert saved.status_code == 200
|
||||
saved_payload = parse_api_data(saved.get_json())
|
||||
po_row = next(row for row in saved_payload["rules"] if row["code"] == "po_order")
|
||||
assert po_row["user_ids"] == [owner_id]
|
||||
unknown_codes = [row["code"] for row in saved_payload["rules"] if row["code"] == "unknown"]
|
||||
assert unknown_codes == []
|
||||
|
||||
with app.app_context():
|
||||
row = db.session.query(VoucherCheckNotifyConfig).filter_by(profile_code="sales").one()
|
||||
assert row.recipients_json["po_order"] == [owner_id]
|
||||
assert "unknown" not in row.recipients_json
|
||||
|
||||
|
||||
def test_notify_config_rejects_non_admin_headers(app, client, user_headers):
|
||||
"""仅凭普通用户 header 不能改通知配置。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
|
||||
res = client.get("/api/workspace/accounting-voucher-check/notify-config", headers=user_headers)
|
||||
assert res.status_code == 403
|
||||
assert parse_api_error(res.get_json())["code"] in {
|
||||
"VOUCHER_CHECK.FORBIDDEN",
|
||||
"VOUCHER_CHECK.ADMIN_REQUIRED",
|
||||
}
|
||||
|
||||
|
||||
def test_send_batch_splits_one_card_per_user(app, monkeypatch):
|
||||
"""缺件通知按人拆信,正文只含该人负责的缺失类型。"""
|
||||
sent: list[tuple[str, str, str]] = []
|
||||
|
||||
def _fake_send(wecom_userid, title, description, jump_url):
|
||||
sent.append((wecom_userid, title, description))
|
||||
return {"errcode": 0}
|
||||
|
||||
monkeypatch.setattr("app.services.wecom_notify.send_wecom_textcard_message", _fake_send)
|
||||
monkeypatch.setattr(
|
||||
"app.services.wecom_notify._create_audit",
|
||||
lambda *args, **kwargs: SimpleNamespace(
|
||||
jump_url=None, status=None, error_code=None, error_message=None
|
||||
),
|
||||
)
|
||||
monkeypatch.setattr("app.services.wecom_notify._build_jump_url", lambda *args, **kwargs: "https://jump")
|
||||
|
||||
with app.app_context():
|
||||
app.config["WECOM_NOTIFY_ENABLED"] = True
|
||||
owner = _ensure_user(username="vc_notify_owner", role="admin", wecom_userid="wx-owner")
|
||||
po_user = _ensure_user(username="vc_po_recv", wecom_userid="wx-po-recv")
|
||||
bl_user = _ensure_user(username="vc_bl_recv", wecom_userid="wx-bl-recv")
|
||||
task = ScheduleTask(
|
||||
schedule_no="SCH-VC-NOTIFY-SPLIT",
|
||||
name="立账凭证检核",
|
||||
schedule_type="platform_job",
|
||||
platform_api_path="accounting-voucher-check",
|
||||
owner_user_id=owner.id,
|
||||
cron_expr="0 6 * * 2",
|
||||
notify_enabled=True,
|
||||
notify_user_ids_json=[],
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
schedule_run = ScheduleRun(
|
||||
schedule_task_id=task.id,
|
||||
run_no="RUN-VC-NOTIFY-SPLIT",
|
||||
trigger_type="cron",
|
||||
status="success",
|
||||
)
|
||||
db.session.add(schedule_run)
|
||||
db.session.flush()
|
||||
engine_run = VoucherCheckRun(
|
||||
id=1,
|
||||
schedule_run_id=schedule_run.id,
|
||||
run_no="VC-NOTIFY-SPLIT",
|
||||
profile_code="sales",
|
||||
profile_version="1",
|
||||
profile_snapshot_json={"profile_code": "sales"},
|
||||
period_month=date(2026, 8, 1),
|
||||
mode="incremental",
|
||||
status="missing",
|
||||
subject_count=4,
|
||||
missing_file_count=3,
|
||||
missing_invoice_count=2,
|
||||
)
|
||||
db.session.add(engine_run)
|
||||
db.session.flush()
|
||||
notify_row = (
|
||||
db.session.query(VoucherCheckNotifyConfig).filter_by(profile_code="sales").one_or_none()
|
||||
)
|
||||
if notify_row is None:
|
||||
notify_row = VoucherCheckNotifyConfig(profile_code="sales")
|
||||
db.session.add(notify_row)
|
||||
notify_row.recipients_json = {
|
||||
"po_order": [po_user.id],
|
||||
"bill_of_lading": [bl_user.id],
|
||||
}
|
||||
db.session.commit()
|
||||
status = send_batch_missing_summary(
|
||||
task=task,
|
||||
schedule_run=schedule_run,
|
||||
engine_run_id=engine_run.id,
|
||||
profile_code="sales",
|
||||
period_month="2026-08",
|
||||
stats={
|
||||
"subject_count": 4,
|
||||
"missing_file_count": 3,
|
||||
"missing_invoice_count": 2,
|
||||
"by_file_type": {
|
||||
"po_order": {
|
||||
"rule_code": "po_order",
|
||||
"rule_name": "PO订单",
|
||||
"missing_file_count": 2,
|
||||
"missing_invoice_count": 1,
|
||||
},
|
||||
"bill_of_lading": {
|
||||
"rule_code": "bill_of_lading",
|
||||
"rule_name": "提单",
|
||||
"missing_file_count": 1,
|
||||
"missing_invoice_count": 1,
|
||||
},
|
||||
"tax_invoice": {
|
||||
"rule_code": "tax_invoice",
|
||||
"rule_name": "发票",
|
||||
"missing_file_count": 1,
|
||||
"missing_invoice_count": 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
excel_filename="voucher_check_sales_2026-08.xlsx",
|
||||
enabled=True,
|
||||
)
|
||||
assert status == "sent"
|
||||
audit = db.session.query(VoucherCheckNotification).filter_by(run_id=engine_run.id).one()
|
||||
# 关键断言:仍是一批一条审计;skip_reason 记录未配置类型。
|
||||
assert audit.send_status == "sent"
|
||||
assert audit.skip_reason == "部分文件类型未配置接收人"
|
||||
assert set(audit.recipient_user_ids_json or []) == {po_user.id, bl_user.id}
|
||||
assert "tax_invoice" in (audit.summary_json or {}).get("unmapped_rule_codes", [])
|
||||
|
||||
wecom_ids = {item[0] for item in sent}
|
||||
assert wecom_ids == {"wx-po-recv", "wx-bl-recv"}
|
||||
po_body = next(item[2] for item in sent if item[0] == "wx-po-recv")
|
||||
bl_body = next(item[2] for item in sent if item[0] == "wx-bl-recv")
|
||||
assert "PO订单" in po_body
|
||||
assert "提单" not in po_body
|
||||
assert "发票" not in po_body
|
||||
assert "提单" in bl_body
|
||||
assert "PO订单" not in bl_body
|
||||
@@ -104,10 +104,10 @@ def _add_engine_run(
|
||||
return engine
|
||||
|
||||
|
||||
def test_workspace_latest_run_returns_queued_force_all(app, client, user_headers):
|
||||
def test_workspace_latest_run_returns_queued_force_all(app, client, admin_headers):
|
||||
"""入队中的强制重检必须显示 queued,而不是建批前失败口径。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
_add_schedule_run(
|
||||
run_no="RUN-LATEST-QUEUED",
|
||||
status="queued",
|
||||
@@ -117,7 +117,7 @@ def test_workspace_latest_run_returns_queued_force_all(app, client, user_headers
|
||||
|
||||
response = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs/latest",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = parse_api_data(response.get_json())
|
||||
@@ -129,10 +129,10 @@ def test_workspace_latest_run_returns_queued_force_all(app, client, user_headers
|
||||
assert payload["current_step"]["step"] == "queued"
|
||||
|
||||
|
||||
def test_workspace_latest_run_returns_completed_metrics(app, client, user_headers):
|
||||
def test_workspace_latest_run_returns_completed_metrics(app, client, admin_headers):
|
||||
"""终态缺件批次返回四指标与 100% 进度。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
sched = _add_schedule_run(
|
||||
run_no="RUN-LATEST-DONE",
|
||||
status="success",
|
||||
@@ -149,7 +149,7 @@ def test_workspace_latest_run_returns_completed_metrics(app, client, user_header
|
||||
|
||||
response = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs/latest",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = parse_api_data(response.get_json())
|
||||
@@ -165,10 +165,10 @@ def test_workspace_latest_run_returns_completed_metrics(app, client, user_header
|
||||
assert payload["missing_invoice_count"] == 6
|
||||
|
||||
|
||||
def test_workspace_latest_run_running_progress_ignores_list_period(app, client, user_headers):
|
||||
def test_workspace_latest_run_running_progress_ignores_list_period(app, client, admin_headers):
|
||||
"""执行中进度跟 runner 步骤走,且 latest 不吃列表 period。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
old = _add_schedule_run(
|
||||
run_no="RUN-LATEST-OLD-AUG",
|
||||
status="success",
|
||||
@@ -200,7 +200,7 @@ def test_workspace_latest_run_running_progress_ignores_list_period(app, client,
|
||||
|
||||
latest = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs/latest",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert latest.status_code == 200
|
||||
payload = parse_api_data(latest.get_json())
|
||||
@@ -213,7 +213,7 @@ def test_workspace_latest_run_running_progress_ignores_list_period(app, client,
|
||||
|
||||
filtered = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs?period=2026-08",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert filtered.status_code == 200
|
||||
nos = {item["run_no"] for item in parse_api_data(filtered.get_json())["items"]}
|
||||
@@ -222,10 +222,10 @@ def test_workspace_latest_run_running_progress_ignores_list_period(app, client,
|
||||
assert payload["run_no"] == "RUN-LATEST-RUNNING"
|
||||
|
||||
|
||||
def test_workspace_latest_run_pre_engine_failure(app, client, user_headers):
|
||||
def test_workspace_latest_run_pre_engine_failure(app, client, admin_headers):
|
||||
"""建批前失败仍作为最近一次检核,状态为技术失败。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
_add_schedule_run(
|
||||
run_no="RUN-LATEST-FAILED",
|
||||
status="failed",
|
||||
@@ -242,7 +242,7 @@ def test_workspace_latest_run_pre_engine_failure(app, client, user_headers):
|
||||
|
||||
response = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs/latest",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = parse_api_data(response.get_json())
|
||||
|
||||
@@ -42,7 +42,7 @@ def _ensure_user(*, username: str, role: str, real_name: str | None = None) -> U
|
||||
return user
|
||||
|
||||
|
||||
def test_workspace_lists_wecom_notifications(app, client, user_headers):
|
||||
def test_workspace_lists_wecom_notifications(app, client, admin_headers):
|
||||
"""企微发送记录列表应带出批次统计、接收人姓名,并按发送状态筛选。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="admin", role="admin")
|
||||
@@ -116,7 +116,7 @@ def test_workspace_lists_wecom_notifications(app, client, user_headers):
|
||||
|
||||
listed = client.get(
|
||||
"/api/workspace/accounting-voucher-check/notifications",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert listed.status_code == 200
|
||||
payload = parse_api_data(listed.get_json())
|
||||
@@ -132,7 +132,7 @@ def test_workspace_lists_wecom_notifications(app, client, user_headers):
|
||||
|
||||
filtered = client.get(
|
||||
"/api/workspace/accounting-voucher-check/notifications?period=2026-07&send_status=skipped_no_missing",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert filtered.status_code == 200
|
||||
filtered_payload = parse_api_data(filtered.get_json())
|
||||
|
||||
@@ -74,10 +74,10 @@ def test_parse_period_filter_year_and_month():
|
||||
assert exc.value.code == "VOUCHER_CHECK.CONFIG_INVALID"
|
||||
|
||||
|
||||
def test_workspace_lists_runs_by_year_or_month(app, client, user_headers):
|
||||
def test_workspace_lists_runs_by_year_or_month(app, client, admin_headers):
|
||||
"""任务列表 year 筛出同年多月,month 仍只返回单月。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
db.session.add(_engine_run(run_no="VC-202608-Y", period_month=date(2026, 8, 1)))
|
||||
db.session.add(_engine_run(run_no="VC-202607-Y", period_month=date(2026, 7, 1)))
|
||||
db.session.add(_engine_run(run_no="VC-202512-Y", period_month=date(2025, 12, 1)))
|
||||
@@ -85,7 +85,7 @@ def test_workspace_lists_runs_by_year_or_month(app, client, user_headers):
|
||||
|
||||
year_resp = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs?period=2026",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert year_resp.status_code == 200
|
||||
year_payload = parse_api_data(year_resp.get_json())
|
||||
@@ -97,7 +97,7 @@ def test_workspace_lists_runs_by_year_or_month(app, client, user_headers):
|
||||
|
||||
month_resp = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs?period=2026-08",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert month_resp.status_code == 200
|
||||
month_payload = parse_api_data(month_resp.get_json())
|
||||
@@ -137,10 +137,10 @@ def test_workspace_year_filter_includes_pre_engine_failure(app, client, admin_he
|
||||
assert "RUN-PREENGINE-YEAR" in nos
|
||||
|
||||
|
||||
def test_workspace_lists_notifications_by_year(app, client, user_headers):
|
||||
def test_workspace_lists_notifications_by_year(app, client, admin_headers):
|
||||
"""企微发送记录按年筛选应覆盖该年各月。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
august = _engine_run(run_no="VC-202608-N", period_month=date(2026, 8, 1))
|
||||
july = _engine_run(run_no="VC-202607-N", period_month=date(2026, 7, 1), status="completed")
|
||||
db.session.add_all([august, july])
|
||||
@@ -163,7 +163,7 @@ def test_workspace_lists_notifications_by_year(app, client, user_headers):
|
||||
|
||||
response = client.get(
|
||||
"/api/workspace/accounting-voucher-check/notifications?period=2026",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = parse_api_data(response.get_json())
|
||||
@@ -172,13 +172,13 @@ def test_workspace_lists_notifications_by_year(app, client, user_headers):
|
||||
assert "VC-202607-N" in nos
|
||||
|
||||
|
||||
def test_workspace_rejects_invalid_period(app, client, user_headers):
|
||||
def test_workspace_rejects_invalid_period(app, client, admin_headers):
|
||||
"""非法 period 返回配置错误,而不是空列表。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="user", role="user")
|
||||
_ensure_user(username="admin", role="admin")
|
||||
response = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs?period=2026-13",
|
||||
headers=user_headers,
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert response.status_code == 400
|
||||
error = parse_api_error(response.get_json())
|
||||
|
||||
Reference in new issue
Block a user