Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backend/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ def is_provider_configured(self, provider_id: str) -> bool:
def get_active_llm_provider(self) -> Optional[str]:
"""Get the canonical active LLM provider based on configuration."""
if self.llm_provider:
return resolve_provider_id(self.llm_provider)
resolved = resolve_provider_id(self.llm_provider)
if resolved in ("ocr_only", "none", "off", "disabled"):
return None
return resolved

# Auto-detect based on available API keys (preserve historical priority order first)
priority_order = (
Expand Down
37 changes: 37 additions & 0 deletions backend/app/database.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from sqlalchemy.orm import DeclarativeBase

Expand All @@ -16,6 +17,42 @@ class Base(DeclarativeBase):
pass


_INVOICE_MIGRATION_COLUMNS: tuple[tuple[str, str, str], ...] = (
("reimbursable_amount", "NUMERIC(12, 2)", "NUMERIC(12, 2)"),
("reimbursement_note", "TEXT", "TEXT"),
("field_bboxes", "JSON", "JSON"),
)


def _ensure_invoice_columns_sync(sync_conn) -> None:
"""Idempotently add backward-compatible nullable columns to existing invoices tables."""
dialect = getattr(getattr(sync_conn, "dialect", None), "name", "")
if dialect == "postgresql":
for col_name, pg_type, _sqlite_type in _INVOICE_MIGRATION_COLUMNS:
sync_conn.execute(
text(f"ALTER TABLE invoices ADD COLUMN IF NOT EXISTS {col_name} {pg_type}")
)
elif dialect == "sqlite":
rows = sync_conn.execute(text("PRAGMA table_info(invoices)")).fetchall()
existing = {row[1] for row in rows}
for col_name, _pg_type, sqlite_type in _INVOICE_MIGRATION_COLUMNS:
if col_name not in existing:
sync_conn.execute(
text(f"ALTER TABLE invoices ADD COLUMN {col_name} {sqlite_type}")
)


async def init_db(target_engine=None) -> None:
"""Create missing tables and idempotently migrate new nullable columns."""
from app.models import invoice as _invoice # noqa: F401
from app.models import reimbursement_set as _reimbursement_set # noqa: F401

eng = target_engine or engine
async with eng.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
await conn.run_sync(_ensure_invoice_columns_sync)


async def get_db():
async with async_session_maker() as session:
try:
Expand Down
6 changes: 2 additions & 4 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ async def rate_limit_handler(request: Request, exc: RateLimitExceeded):

@app.on_event("startup")
async def startup():
from app.database import engine, Base
from app.models import reimbursement_set as _reimbursement_set # noqa: F401
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
from app.database import init_db
await init_db()


def mount_spa(target_app: FastAPI, static_dir: Path) -> None:
Expand Down
7 changes: 6 additions & 1 deletion backend/app/models/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from enum import Enum
from sqlalchemy import (
Column, Integer, String, DateTime, Date, Numeric,
Text, LargeBinary, ForeignKey, Enum as SQLEnum
Text, LargeBinary, ForeignKey, Enum as SQLEnum, JSON
)
from sqlalchemy.orm import relationship

Expand Down Expand Up @@ -52,6 +52,11 @@ class Invoice(Base):
tax_rate = Column(String(20), nullable=True) # 税率
tax_amount = Column(Numeric(12, 2), nullable=True) # 税额

# Reimbursement adjustment & spatial proofreading bounding boxes (backward-compatible nullable)
reimbursable_amount = Column(Numeric(12, 2), nullable=True) # 核定实报金额
reimbursement_note = Column(Text, nullable=True) # 实报核减说明
field_bboxes = Column(JSON, nullable=True) # 字段归一化坐标框 {field: [x0, y0, x1, y1]}

# Status management
status = Column(SQLEnum(InvoiceStatus), default=InvoiceStatus.PENDING, nullable=False)
owner = Column(String(100), nullable=True) # 归属人
Expand Down
Loading
Loading