57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
# models.py – SQLModel (Pydantic + SQLAlchemy) pour toutes les tables
|
||
from decimal import Decimal
|
||
from typing import Optional, List
|
||
from sqlmodel import Field, SQLModel, Relationship, Session, create_engine, select
|
||
|
||
class User(SQLModel, table=True):
|
||
id: Optional[int] = Field(primary_key=True)
|
||
email: str = Field(unique=True, index=True)
|
||
hashed_pw: str
|
||
is_admin: bool = False
|
||
|
||
class Product(SQLModel, table=True):
|
||
id: Optional[int] = Field(primary_key=True)
|
||
sku: str = Field(unique=True, index=True)
|
||
name: str
|
||
price: Decimal = Field(decimal_places=2)
|
||
qty_in_stock: int = 0
|
||
|
||
class Supplier(SQLModel, table=True):
|
||
id: Optional[int] = Field(primary_key=True)
|
||
name: str
|
||
contact: str
|
||
|
||
class PurchaseOrder(SQLModel, table=True):
|
||
id: Optional[int] = Field(primary_key=True)
|
||
supplier_id: int = Field(foreign_key="supplier.id")
|
||
created_at: dt.datetime = Field(default_factory=dt.datetime.utcnow)
|
||
|
||
class POLine(SQLModel, table=True):
|
||
id: Optional[int] = Field(primary_key=True)
|
||
po_id: int = Field(foreign_key="purchaseorder.id")
|
||
product_id: int = Field(foreign_key="product.id")
|
||
qty: int
|
||
unit_cost: Decimal = Field(decimal_places=2)
|
||
|
||
class Customer(SQLModel, table=True):
|
||
id: Optional[int] = Field(primary_key=True)
|
||
name: str
|
||
email: str
|
||
|
||
class SaleInvoice(SQLModel, table=True):
|
||
id: Optional[int] = Field(primary_key=True)
|
||
customer_id: int = Field(foreign_key="customer.id")
|
||
created_at: dt.datetime = Field(default_factory=dt.datetime.utcnow)
|
||
total: Decimal = Field(decimal_places=2)
|
||
|
||
class SaleLine(SQLModel, table=True):
|
||
id: Optional[int] = Field(primary_key=True)
|
||
invoice_id: int = Field(foreign_key="saleinvoice.id")
|
||
product_id: int = Field(foreign_key="product.id")
|
||
qty: int
|
||
unit_price: Decimal = Field(decimal_places=2)
|
||
|
||
def get_session():
|
||
engine = create_engine(settings.DB_URL, echo=False)
|
||
SQLModel.metadata.create_all(engine)
|
||
with Session(engine) as s: yield s |