Files
syops/backend/app/api/workspace/voucher_check.py
T

204 lines
7.4 KiB
Python
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.
"""
backend/app/api/workspace/voucher_check.py
用途:立账凭证检核查询、强制重检与通知配置 API(任务列表 / 最近一次检核 / 批次明细 / 企微发送记录 / 按类型接收人)。
依赖:auth.jwt、response_utils envelope、Agent 授权、voucher_check.query_service、notify_config_service。
逻辑:Chat Agent 与旧工作台查询共用;GET runs/latest 不跟筛选;GET notifications 为企微发送记录;
查询要求 is_platform_admin 或持有 `agent_code=voucher_check` 的 Agent 授权;POST runs 与通知配置仅平台管理员。
"""
from __future__ import annotations
from flask import Blueprint, request
from app.core.auth.jwt import get_current_user
from app.core.errors import ForbiddenError, UnauthorizedError, ValidationError
from app.core.response import ok
from app.models import Agent
from app.services.voucher_check import notify_config_service, query_service
workspace_voucher_check_bp = Blueprint("workspace_voucher_check", __name__)
VOUCHER_CHECK_AGENT_CODE = "voucher_check"
def _require_user():
"""
返回当前登录用户。
Returns:
已认证用户。
Raises:
UnauthorizedError: 请求没有有效登录身份。
"""
user = get_current_user()
if not user:
raise UnauthorizedError()
return user
def _user_has_voucher_check_agent(user) -> bool:
"""
判断用户是否持有启用中的立账凭证检核 Agent。
核心逻辑:按 agent_code 查找启用实例,再核对 user_agent_permissions;平台管理员不走此判断。
Args:
user: 已认证用户。
Returns:
已绑定启用中的 voucher_check Agent 时为 True。
"""
agent = Agent.query.filter_by(agent_code=VOUCHER_CHECK_AGENT_CODE, is_active=True).first()
return bool(agent is not None and agent in user.agents)
def _require_voucher_check_access():
"""
确保当前用户已登录且具备立账凭证检核访问权限。
核心逻辑:is_platform_admin 放行;普通用户必须被授予启用中的 `agent_code=voucher_check` Agent。
无权限时返回业务 403,而不是静默空数据。
Returns:
已认证且具备立账凭证 Agent 授权(或平台管理员)的用户。
Raises:
UnauthorizedError: 请求没有有效登录身份。
ForbiddenError: 已登录但没有立账凭证 Agent 授权。
"""
user = _require_user()
if user.is_platform_admin() or _user_has_voucher_check_agent(user):
return user
raise ForbiddenError(
"Voucher check agent permission required",
code="VOUCHER_CHECK.FORBIDDEN",
)
def _require_admin():
"""
确保当前用户既有立账凭证访问权,又是平台管理员。
核心逻辑:持有 Agent 的普通用户可查检核任务与发送记录;强制重检与通知配置仅 is_platform_admin。
Returns:
已认证的管理员用户。
Raises:
UnauthorizedError: 请求没有有效登录身份。
ForbiddenError: 无立账凭证访问权,或已授权但不是管理员。
"""
user = _require_voucher_check_access()
if not user.is_platform_admin():
raise ForbiddenError(
"Admin privilege required for voucher check jobs",
code="VOUCHER_CHECK.ADMIN_REQUIRED",
)
return user
@workspace_voucher_check_bp.route("/accounting-voucher-check/runs", methods=["GET"])
def workspace_voucher_runs():
"""
分页查看检核任务列表。
核心逻辑:委托 query_service.list_runs;period 为 YYYY 或 YYYY-MM,可再按业务方/状态筛选。
"""
_require_voucher_check_access()
data = query_service.list_runs(
period=request.args.get("period"),
profile_code=request.args.get("profile_code"),
status=request.args.get("status"),
page=request.args.get("page", default=1, type=int) or 1,
page_size=request.args.get("page_size", default=50, type=int) or 50,
)
return ok(data)
@workspace_voucher_check_bp.route("/accounting-voucher-check/runs", methods=["POST"])
def workspace_voucher_force_run():
"""
强制重检上一个自然月(不走增量跳过)。
核心逻辑:body.mode 仅接受 force_all;入队默认周批排程,由 schedule worker 执行。
"""
user = _require_admin()
payload = request.get_json(silent=True) or {}
mode = str(payload.get("mode") or "force_all").strip().lower()
if mode != "force_all":
raise ValidationError(
"工作台仅支持 force_all",
code="VOUCHER_CHECK.CONFIG_INVALID",
details={"mode": mode},
)
data = query_service.enqueue_force_all_run(triggered_by_user_id=getattr(user, "id", None))
return ok(data)
@workspace_voucher_check_bp.route("/accounting-voucher-check/runs/latest", methods=["GET"])
def workspace_voucher_latest_run():
"""
读取最近一次立账凭证检核批次(含排队/执行中)。
核心逻辑:委托 query_service.get_latest_run;不跟列表 period/status;无批次时 data 为 null。
"""
_require_voucher_check_access()
return ok(query_service.get_latest_run())
@workspace_voucher_check_bp.route("/accounting-voucher-check/runs/<int:run_id>/items", methods=["GET"])
def workspace_voucher_run_items(run_id: int):
"""
分页查看批次明细。
核心逻辑:委托 query_service.list_run_items,可按 result 过滤,并返回 by_file_type。
"""
_require_voucher_check_access()
data = query_service.list_run_items(
run_id=run_id,
result=request.args.get("result"),
page=request.args.get("page", default=1, type=int) or 1,
page_size=request.args.get("page_size", default=50, type=int) or 50,
)
return ok(data)
@workspace_voucher_check_bp.route("/accounting-voucher-check/notifications", methods=["GET"])
def workspace_voucher_notifications():
"""
分页查看企微缺件通知发送记录。
核心逻辑:委托 query_service.list_notifications;period 为 YYYY 或 YYYY-MM,可再按发送状态筛选。
"""
_require_voucher_check_access()
data = query_service.list_notifications(
period=request.args.get("period"),
send_status=request.args.get("send_status"),
page=request.args.get("page", default=1, type=int) or 1,
page_size=request.args.get("page_size", default=50, type=int) or 50,
)
return ok(data)
@workspace_voucher_check_bp.route("/accounting-voucher-check/notify-config", methods=["GET"])
def workspace_voucher_notify_config_get():
"""
读取按文件类型的企微接收人配置。
核心逻辑:仅 is_platform_admin;规则来自销售 Profile,接收人来自独立配置表。
"""
_require_admin()
return ok(notify_config_service.get_notify_config(profile_code=request.args.get("profile_code")))
@workspace_voucher_check_bp.route("/accounting-voucher-check/notify-config", methods=["PUT"])
def workspace_voucher_notify_config_put():
"""
保存按文件类型的企微接收人。
核心逻辑:仅 is_platform_admin;未知规则码丢弃;未绑企微的人可保存但发送时跳过。
"""
user = _require_admin()
payload = request.get_json(silent=True) or {}
data = notify_config_service.save_notify_config(
profile_code=payload.get("profile_code") or request.args.get("profile_code"),
recipients=payload.get("recipients"),
updated_by=getattr(user, "id", None),
)
return ok(data)