Files
syops/backend/app/models/voucher_check.py
T
2026-09-07 11:13:33 +08:00

268 lines
12 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/models/voucher_check.py
用途:立账凭证检核 ORM。含旧一票一状态表,以及销售一期单文件结果引擎表。
依赖:models.base.Base、schedule_runs。
逻辑:旧 runner 写 invoice_states/run_items;新引擎写 profiles/runs/subjects/file_results/notifications。
"""
from __future__ import annotations
from datetime import date, datetime
from typing import Optional
from sqlalchemy import (
BigInteger,
Boolean,
Date,
DateTime,
ForeignKey,
Index,
JSON,
String,
Text,
UniqueConstraint,
)
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base
class VoucherCheckInvoiceState(Base):
"""账套+月份+发票号的最新材料齐全状态(增量跳过真相源)。"""
__tablename__ = "voucher_check_invoice_states"
__table_args__ = (
UniqueConstraint(
"scope_code",
"period_month",
"invoice_no",
name="uq_voucher_check_invoice_states_scope_period_invoice",
),
Index("ix_voucher_check_invoice_states_period_status", "period_month", "status"),
Index("ix_voucher_check_invoice_states_invoice_no", "invoice_no"),
Index("ix_voucher_check_invoice_states_notified_on", "last_missing_notified_on"),
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
scope_code: Mapped[str] = mapped_column(String(32), nullable=False)
period_month: Mapped[date] = mapped_column(Date, nullable=False)
invoice_no: Mapped[str] = mapped_column(String(128), nullable=False)
invoice_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
status: Mapped[str] = mapped_column(String(24), nullable=False)
folder_path: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True)
folder_exists: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
material_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
matched_files_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
missing_materials_json: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
last_actual_run_id: Mapped[Optional[int]] = mapped_column(
BigInteger,
ForeignKey("schedule_runs.id", ondelete="SET NULL"),
nullable=True,
)
last_checked_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
last_seen_erp_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
last_missing_notified_on: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
updated_at: Mapped[datetime] = mapped_column(
DateTime,
default=datetime.utcnow,
onupdate=datetime.utcnow,
nullable=False,
)
class VoucherCheckRunItem(Base):
"""单次 ScheduleRun 的发票检核快照(含跳过项,供审计与 Excel)。"""
__tablename__ = "voucher_check_run_items"
__table_args__ = (
UniqueConstraint(
"schedule_run_id",
"invoice_no",
name="uq_voucher_check_run_items_run_invoice",
),
Index("ix_voucher_check_run_items_run_result", "schedule_run_id", "result"),
Index("ix_voucher_check_run_items_period_result", "period_month", "result"),
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
schedule_run_id: Mapped[int] = mapped_column(
BigInteger,
ForeignKey("schedule_runs.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
invoice_state_id: Mapped[Optional[int]] = mapped_column(
BigInteger,
ForeignKey("voucher_check_invoice_states.id", ondelete="SET NULL"),
nullable=True,
)
scope_code: Mapped[str] = mapped_column(String(32), nullable=False)
period_month: Mapped[date] = mapped_column(Date, nullable=False)
invoice_no: Mapped[str] = mapped_column(String(128), nullable=False)
invoice_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
result: Mapped[str] = mapped_column(String(24), nullable=False)
folder_path: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True)
folder_exists: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
material_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
matched_files_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
missing_materials_json: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
error_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
checked_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
class VoucherCheckProfile(Base):
"""已发布 Profile 版本;批次绑定不可变快照,运行时不以本表热更新覆盖。"""
__tablename__ = "voucher_check_profiles"
__table_args__ = (
UniqueConstraint("profile_code", "version", name="uq_voucher_check_profiles_code_version"),
Index("ix_voucher_check_profiles_status", "status"),
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
profile_code: Mapped[str] = mapped_column(String(32), nullable=False)
version: Mapped[str] = mapped_column(String(32), nullable=False)
definition_json: Mapped[dict] = mapped_column(JSON, nullable=False)
status: Mapped[str] = mapped_column(String(24), nullable=False, default="published")
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
updated_at: Mapped[datetime] = mapped_column(
DateTime,
default=datetime.utcnow,
onupdate=datetime.utcnow,
nullable=False,
)
class VoucherCheckRun(Base):
"""一次检核批次;列表统计取本表快照,不在查询时重算单文件结果。"""
__tablename__ = "voucher_check_runs"
__table_args__ = (
Index("ix_voucher_check_runs_period_profile", "period_month", "profile_code"),
Index("ix_voucher_check_runs_status", "status"),
Index("ix_voucher_check_runs_finished_at", "finished_at"),
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
schedule_run_id: Mapped[Optional[int]] = mapped_column(
BigInteger,
ForeignKey("schedule_runs.id", ondelete="SET NULL"),
nullable=True,
index=True,
)
run_no: Mapped[str] = mapped_column(String(64), nullable=False)
profile_code: Mapped[str] = mapped_column(String(32), nullable=False)
profile_version: Mapped[str] = mapped_column(String(32), nullable=False)
profile_snapshot_json: Mapped[dict] = mapped_column(JSON, nullable=False)
period_month: Mapped[date] = mapped_column(Date, nullable=False)
mode: Mapped[str] = mapped_column(String(24), nullable=False, default="incremental")
status: Mapped[str] = mapped_column(String(24), nullable=False, default="running")
subject_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0)
archived_file_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0)
skipped_file_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0)
missing_file_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0)
missing_invoice_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0)
technical_error_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0)
staged_file_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0)
stats_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
started_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
finished_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
class VoucherCheckSubject(Base):
"""批次内一张销售 invoice 主体。"""
__tablename__ = "voucher_check_subjects"
__table_args__ = (
UniqueConstraint("run_id", "invoice_no", name="uq_voucher_check_subjects_run_invoice"),
Index("ix_voucher_check_subjects_invoice_no", "invoice_no"),
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
run_id: Mapped[int] = mapped_column(
BigInteger,
ForeignKey("voucher_check_runs.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
invoice_no: Mapped[str] = mapped_column(String(128), nullable=False)
invoice_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
source_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
attributes_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
class VoucherCheckFileResult(Base):
"""invoice × 文件规则 的单文件结果,是缺件统计与 Excel 的真相源。"""
__tablename__ = "voucher_check_file_results"
__table_args__ = (
UniqueConstraint(
"run_id",
"invoice_no",
"rule_code",
name="uq_voucher_check_file_results_run_invoice_rule",
),
Index("ix_voucher_check_file_results_status", "run_id", "status"),
Index("ix_voucher_check_file_results_invoice", "invoice_no"),
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
run_id: Mapped[int] = mapped_column(
BigInteger,
ForeignKey("voucher_check_runs.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
subject_id: Mapped[Optional[int]] = mapped_column(
BigInteger,
ForeignKey("voucher_check_subjects.id", ondelete="SET NULL"),
nullable=True,
)
invoice_no: Mapped[str] = mapped_column(String(128), nullable=False)
rule_code: Mapped[str] = mapped_column(String(64), nullable=False)
rule_name: Mapped[str] = mapped_column(String(128), nullable=False)
source_type: Mapped[str] = mapped_column(String(32), nullable=False)
expected_name: Mapped[Optional[str]] = mapped_column(String(512), nullable=True)
matched_name: Mapped[Optional[str]] = mapped_column(String(512), nullable=True)
source_folder_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
source_item_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
archive_folder_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
archive_item_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
source_path: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True)
archive_path: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True)
status: Mapped[str] = mapped_column(String(24), nullable=False)
missing_reason: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
error_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
source_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
checked_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
class VoucherCheckNotification(Base):
"""一个检核批次对应一条企微汇总通知审计。"""
__tablename__ = "voucher_check_notifications"
__table_args__ = (Index("ix_voucher_check_notifications_status", "send_status"),)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
run_id: Mapped[int] = mapped_column(
BigInteger,
ForeignKey("voucher_check_runs.id", ondelete="CASCADE"),
nullable=False,
unique=True,
)
excel_filename: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
recipient_user_ids_json: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
send_status: Mapped[str] = mapped_column(String(32), nullable=False)
skip_reason: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
summary_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
sent_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)