235 lines
8.9 KiB
Python
235 lines
8.9 KiB
Python
"""
|
||
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 为企微发送记录;
|
||
批次明细可按 result 与 invoice_no 筛选。企微记录里的 Excel 走本模块下载,不走排程通知人权限。
|
||
查询要求 is_platform_admin 或持有 `agent_code=voucher_check` 的 Agent 授权;POST runs 与通知配置仅平台管理员。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from io import BytesIO
|
||
|
||
from flask import Blueprint, request, send_file
|
||
|
||
from app.core.auth.jwt import get_current_user
|
||
from app.core.errors import ForbiddenError, NotFoundError, UnauthorizedError, ValidationError
|
||
from app.core.response import ok
|
||
from app.models import Agent, ScheduleRun, VoucherCheckRun
|
||
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 与 invoice_no 过滤,并返回 by_file_type。
|
||
"""
|
||
_require_voucher_check_access()
|
||
data = query_service.list_run_items(
|
||
run_id=run_id,
|
||
result=request.args.get("result"),
|
||
invoice_no=request.args.get("invoice_no"),
|
||
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/schedule-runs/<int:schedule_run_id>/excel",
|
||
methods=["GET"],
|
||
)
|
||
def workspace_voucher_schedule_excel(schedule_run_id: int):
|
||
"""
|
||
下载企微发送记录对应的检核结果 Excel。
|
||
核心逻辑:能看发送记录的人即可下载。只放行已挂到立账凭证批次上的 ScheduleRun,不按排程负责人或通知人拦截。
|
||
|
||
Raises:
|
||
NotFoundError: 该排程记录不是立账凭证批次,或还没有生成 Excel。
|
||
"""
|
||
_require_voucher_check_access()
|
||
engine = VoucherCheckRun.query.filter_by(schedule_run_id=schedule_run_id).first()
|
||
if engine is None:
|
||
raise NotFoundError("立账凭证检核批次不存在", code="VOUCHER_CHECK.RUN_NOT_FOUND")
|
||
run = ScheduleRun.query.get(schedule_run_id)
|
||
if run is None or not run.excel_file_data:
|
||
raise NotFoundError("检核结果 Excel 不存在", code="VOUCHER_CHECK.EXCEL_NOT_FOUND")
|
||
return send_file(
|
||
BytesIO(run.excel_file_data),
|
||
as_attachment=True,
|
||
download_name=run.excel_file_name or "voucher_check.xlsx",
|
||
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||
)
|
||
|
||
|
||
@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)
|