from __future__ import annotations from datetime import datetime, timezone from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Text, UniqueConstraint from sqlalchemy.orm import Mapped, mapped_column from app.core.db.common import CommonBase class AccountingNature(CommonBase): """Firm-scoped canonical accounting nature used by the intelligence engine. Phase 4 intentionally stores accounting meaning separately from any client's Tally ledger name. Later mapping/learning phases can link a nature code to client-specific ledgers without changing this taxonomy. """ __tablename__ = "accounting_natures" __table_args__ = ( UniqueConstraint("tenant_id", "code", name="uq_accounting_natures_tenant_code"), ) id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) tenant_id: Mapped[int] = mapped_column(ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True) code: Mapped[str] = mapped_column(String(80), nullable=False, index=True) name: Mapped[str] = mapped_column(String(160), nullable=False, index=True) parent_code: Mapped[str | None] = mapped_column(String(80), nullable=True, index=True) classification_group: Mapped[str] = mapped_column(String(40), nullable=False, index=True) capital_revenue: Mapped[str] = mapped_column(String(20), nullable=False, default="revenue", index=True) description: Mapped[str | None] = mapped_column(Text, nullable=True) sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=100) is_posting_nature: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, index=True) is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, index=True) is_system_defined: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True) created_by_user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True) updated_by_user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True) created_at_utc: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), nullable=False) updated_at_utc: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc), nullable=False)