from datetime import date, datetime from decimal import Decimal from sqlalchemy import BigInteger, Date, DateTime, ForeignKey, Integer, Numeric, String, text from sqlalchemy.orm import Mapped, mapped_column, relationship from .database import Base class Supplier(Base): __tablename__ = "md_supplier" id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) supplier_code: Mapped[str] = mapped_column(String(32), unique=True) supplier_name: Mapped[str] = mapped_column(String(128)) contact_person: Mapped[str | None] = mapped_column(String(64)) contact_phone: Mapped[str | None] = mapped_column(String(32)) address: Mapped[str | None] = mapped_column(String(255)) status: Mapped[str] = mapped_column(String(16), default="ACTIVE") remark: Mapped[str | None] = mapped_column(String(255)) created_at: Mapped[datetime] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP")) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=text("CURRENT_TIMESTAMP"), server_onupdate=text("CURRENT_TIMESTAMP") ) class Customer(Base): __tablename__ = "md_customer" id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) customer_code: Mapped[str] = mapped_column(String(32), unique=True) customer_name: Mapped[str] = mapped_column(String(128)) contact_person: Mapped[str | None] = mapped_column(String(64)) contact_phone: Mapped[str | None] = mapped_column(String(32)) address: Mapped[str | None] = mapped_column(String(255)) status: Mapped[str] = mapped_column(String(16), default="ACTIVE") remark: Mapped[str | None] = mapped_column(String(255)) created_at: Mapped[datetime] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP")) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=text("CURRENT_TIMESTAMP"), server_onupdate=text("CURRENT_TIMESTAMP") ) class Sku(Base): __tablename__ = "md_sku" id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) sku_code: Mapped[str] = mapped_column(String(32), unique=True) sku_name: Mapped[str] = mapped_column(String(128)) category: Mapped[str | None] = mapped_column(String(64)) spec: Mapped[str | None] = mapped_column(String(128)) unit: Mapped[str] = mapped_column(String(16), default="件") purchase_price: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=Decimal("0")) sale_price: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=Decimal("0")) safety_stock: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=Decimal("0")) status: Mapped[str] = mapped_column(String(16), default="ACTIVE") remark: Mapped[str | None] = mapped_column(String(255)) created_at: Mapped[datetime] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP")) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=text("CURRENT_TIMESTAMP"), server_onupdate=text("CURRENT_TIMESTAMP") ) class AliasMapping(Base): __tablename__ = "mdm_alias_mapping" id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) entity_type: Mapped[str] = mapped_column(String(16)) entity_id: Mapped[int] = mapped_column(BigInteger) alias: Mapped[str] = mapped_column(String(128)) source_system: Mapped[str] = mapped_column(String(64)) remark: Mapped[str | None] = mapped_column(String(255)) created_at: Mapped[datetime] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP")) class PurchaseOrder(Base): __tablename__ = "po_order" id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) order_no: Mapped[str] = mapped_column(String(32), unique=True) supplier_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("md_supplier.id")) order_date: Mapped[date] = mapped_column(Date) status: Mapped[str] = mapped_column(String(16), default="DRAFT") total_amount: Mapped[Decimal] = mapped_column(Numeric(14, 2), default=Decimal("0")) handler: Mapped[str] = mapped_column(String(64), default="demo") remark: Mapped[str | None] = mapped_column(String(255)) received_at: Mapped[datetime | None] = mapped_column(DateTime) created_at: Mapped[datetime] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP")) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=text("CURRENT_TIMESTAMP"), server_onupdate=text("CURRENT_TIMESTAMP") ) supplier: Mapped[Supplier] = relationship(lazy="joined") items: Mapped[list["PurchaseOrderItem"]] = relationship( cascade="all, delete-orphan", order_by="PurchaseOrderItem.line_no", lazy="selectin" ) class PurchaseOrderItem(Base): __tablename__ = "po_order_item" id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) order_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("po_order.id")) line_no: Mapped[int] = mapped_column(Integer) sku_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("md_sku.id")) qty: Mapped[Decimal] = mapped_column(Numeric(12, 2)) price: Mapped[Decimal] = mapped_column(Numeric(12, 2)) amount: Mapped[Decimal] = mapped_column(Numeric(14, 2)) sku: Mapped[Sku] = relationship(lazy="joined") class SalesOrder(Base): __tablename__ = "so_order" id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) order_no: Mapped[str] = mapped_column(String(32), unique=True) customer_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("md_customer.id")) order_date: Mapped[date] = mapped_column(Date) status: Mapped[str] = mapped_column(String(16), default="DRAFT") total_amount: Mapped[Decimal] = mapped_column(Numeric(14, 2), default=Decimal("0")) handler: Mapped[str] = mapped_column(String(64), default="demo") remark: Mapped[str | None] = mapped_column(String(255)) shipped_at: Mapped[datetime | None] = mapped_column(DateTime) created_at: Mapped[datetime] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP")) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=text("CURRENT_TIMESTAMP"), server_onupdate=text("CURRENT_TIMESTAMP") ) customer: Mapped[Customer] = relationship(lazy="joined") items: Mapped[list["SalesOrderItem"]] = relationship( cascade="all, delete-orphan", order_by="SalesOrderItem.line_no", lazy="selectin" ) class SalesOrderItem(Base): __tablename__ = "so_order_item" id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) order_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("so_order.id")) line_no: Mapped[int] = mapped_column(Integer) sku_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("md_sku.id")) qty: Mapped[Decimal] = mapped_column(Numeric(12, 2)) price: Mapped[Decimal] = mapped_column(Numeric(12, 2)) amount: Mapped[Decimal] = mapped_column(Numeric(14, 2)) sku: Mapped[Sku] = relationship(lazy="joined") class Stock(Base): __tablename__ = "inv_stock" id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) sku_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("md_sku.id"), unique=True) qty_on_hand: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=Decimal("0")) updated_at: Mapped[datetime] = mapped_column( DateTime, server_default=text("CURRENT_TIMESTAMP"), server_onupdate=text("CURRENT_TIMESTAMP") ) sku: Mapped[Sku] = relationship(lazy="joined") class StockFlow(Base): __tablename__ = "inv_stock_flow" id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) sku_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("md_sku.id")) biz_type: Mapped[str] = mapped_column(String(16)) biz_order_no: Mapped[str | None] = mapped_column(String(32)) qty_delta: Mapped[Decimal] = mapped_column(Numeric(12, 2)) qty_after: Mapped[Decimal] = mapped_column(Numeric(12, 2)) remark: Mapped[str | None] = mapped_column(String(255)) created_at: Mapped[datetime] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP")) sku: Mapped[Sku] = relationship(lazy="joined")