Files
syops/backend/app/services/voucher_check/adapters/base.py
T

114 lines
3.0 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/services/voucher_check/adapters/base.py
用途:立账凭证来源 Adapter 统一数据结构与协议。
依赖:无。
逻辑:引擎只消费这些类型;ERP/OA/DMS 细节留在各 Adapter 内。
"""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import date
from typing import Any, Protocol
@dataclass(frozen=True)
class SubjectRow:
"""应检 invoice 主体。"""
invoice_no: str
invoice_date: date | None
source_fingerprint: str
attributes: dict[str, Any] = field(default_factory=dict)
@dataclass(frozen=True)
class ScannedFile:
"""类型目录中命中的一个文件。"""
filename: str
item_id: str | None = None
folder_id: str | None = None
path: str | None = None
fingerprint: str | None = None
extra: dict[str, Any] = field(default_factory=dict)
@dataclass(frozen=True)
class ScanOutcome:
"""一条文件规则对一张 invoice 的扫描结果。"""
status: str # matched / missing / technical_error
file: ScannedFile | None = None
error_code: str | None = None
error_message: str | None = None
folder_id: str | None = None
folder_path: str | None = None
class SubjectAdapter(Protocol):
"""获取期间内应检主体。"""
source_type: str
def fetch(self, *, period_month: date, config: Any) -> list[SubjectRow]:
"""
拉取一个自然月的 invoice 清单。
【AI 触发时机】周任务或强制重检需要本期主体时调用。
"""
...
class DirectoryScanAdapter(Protocol):
"""扫描文档库类型目录。"""
source_type: str
def scan(
self,
*,
period_month: date,
invoice_no: str,
rule: dict[str, Any],
) -> ScanOutcome:
"""
在对应类型目录中按命名模板匹配文件。
【AI 触发时机】周任务对每张 invoice 的每条 directory_scan 规则调用。
"""
...
class AcquireAdapter(Protocol):
"""把外部单据打印后写入类型目录;PO 制式 PDF 见 po_order,ERP 取数见 erp_po_repository。"""
source_type: str
wired: bool
def fetch(self, *, period_month: date, subjects: list[SubjectRow]) -> list[ScannedFile]:
"""
获取并落入类型目录。
【AI 触发时机】仅当 Adapter.wired=True 且独立抓取走本方法时调用;周任务不调用。PO 独立抓取请用 ErpPoPrintAdapter.fetch_documents,不要走 fetch。
"""
...
class ArchiveAdapter(Protocol):
"""把已匹配文件复制到所有归档/{invoice}/。"""
wired: bool
def archive(
self,
*,
period_month: date,
invoice_no: str,
file: ScannedFile,
rule: dict[str, Any],
) -> ScannedFile:
"""
复制到归档子目录;目标已有同名文件则跳过覆盖。
【AI 触发时机】扫描命中且命名校验通过后调用。
"""
...