from datetime import datetime from decimal import Decimal from sqlalchemy import DateTime, ForeignKey, Integer, JSON, Numeric, String, Text from sqlalchemy.orm import Mapped, mapped_column, relationship from app.db.base import Base class ReceiptLineItem(Base): __tablename__ = "receipt_line_items" id: Mapped[int] = mapped_column(primary_key=True, index=True) document_id: Mapped[int] = mapped_column(ForeignKey("documents.id"), nullable=False, index=True) line_index: Mapped[int | None] = mapped_column(Integer, nullable=True) raw_description: Mapped[str] = mapped_column(Text, nullable=False) normalized_description: Mapped[str | None] = mapped_column(Text, nullable=True) quantity: Mapped[Decimal | None] = mapped_column(Numeric(12, 3), nullable=True) unit_price: Mapped[Decimal | None] = mapped_column(Numeric(12, 2), nullable=True) line_total: Mapped[Decimal | None] = mapped_column(Numeric(12, 2), nullable=True) item_category: Mapped[str | None] = mapped_column(String(64), nullable=True) confidence: Mapped[Decimal | None] = mapped_column(Numeric(5, 2), nullable=True) extra_json: Mapped[dict | None] = mapped_column(JSON, 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, ) document: Mapped["Document"] = relationship(back_populates="receipt_line_items")