""" backend/app/models/voucher_check.py 用途:立账凭证检核单票最新状态与批次明细 ORM。 依赖:models.base.Base、schedule_runs。 逻辑:runner/persistence 写入;query_service 与工作台 API 读取;增量跳过依赖 invoice_states。 """ 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)