from __future__ import annotations from datetime import datetime, timezone from sqlalchemy import DateTime, ForeignKey, Integer, Numeric, String, UniqueConstraint from sqlalchemy.orm import Mapped, mapped_column from app.core.db.common import CommonBase def _utcnow() -> datetime: return datetime.now(timezone.utc) class AccountingFinancialLedgerMapping(CommonBase): """Client-specific reporting override for a mirrored Tally ledger. The default classifier remains Tally-group driven. Rows in this table are only explicit overrides and therefore remain reusable across financial years. """ __tablename__ = "accounting_financial_ledger_mappings" __table_args__ = ( UniqueConstraint("tenant_id", "client_id", "ledger_name", name="uq_accounting_fs_mapping_client_ledger"), ) 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) client_id: Mapped[int] = mapped_column(ForeignKey("clients.id", ondelete="CASCADE"), nullable=False, index=True) ledger_name: Mapped[str] = mapped_column(String(255), nullable=False, index=True) statement_code: Mapped[str] = mapped_column(String(20), nullable=False, index=True) schedule_code: Mapped[str] = mapped_column(String(60), nullable=False, index=True) display_group: Mapped[str] = mapped_column(String(160), nullable=False, default="") 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), nullable=False, default=_utcnow) updated_at_utc: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=_utcnow, onupdate=_utcnow) class AccountingPartnershipProfitShare(CommonBase): """Client/FY partner profit-sharing configuration used only for finalisation.""" __tablename__ = "accounting_partnership_profit_shares" __table_args__ = ( UniqueConstraint( "tenant_id", "client_id", "financial_year", "partner_name", name="uq_accounting_partner_share_client_fy_name", ), ) 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) client_id: Mapped[int] = mapped_column(ForeignKey("clients.id", ondelete="CASCADE"), nullable=False, index=True) financial_year: Mapped[str] = mapped_column(String(7), nullable=False, index=True) partner_name: Mapped[str] = mapped_column(String(200), nullable=False) profit_share_percent: Mapped[float] = mapped_column(Numeric(7, 4), nullable=False, default=0) target_ledger_name: Mapped[str] = mapped_column(String(255), nullable=False, default="") 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), nullable=False, default=_utcnow) updated_at_utc: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=_utcnow, onupdate=_utcnow)