remodeling table models and migration to postgres
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
from sqlmodel import SQLModel
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class UserRole(str, Enum):
|
||||
ADMIN = "admin"
|
||||
WRITE = "write"
|
||||
READ_ONLY = "read_only"
|
||||
|
||||
|
||||
class TransactionType(str, Enum):
|
||||
SALE = "sell"
|
||||
PURCHASE = "buy"
|
||||
CREDIT = "credit"
|
||||
|
||||
|
||||
class TransactionStatus(str, Enum):
|
||||
UNPAID = "unpaid"
|
||||
PARTIALLY_PAID = "partially_paid"
|
||||
PAID = "paid"
|
||||
CANCELLED = 'cancelled'
|
||||
|
||||
|
||||
class PartnerType(str, Enum):
|
||||
CLIENT = "client"
|
||||
SUPPLIER = "supplier"
|
||||
@@ -18,86 +18,100 @@ from datetime import datetime
|
||||
from sqlalchemy import Column, DateTime, func, Enum as SQLEnum
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
from base import UserRole, PartnerType, TransactionType, TransactionStatus
|
||||
|
||||
|
||||
class TradeType(str, Enum):
|
||||
BUY = "Buy"
|
||||
SELL = "Sell"
|
||||
class User(SQLModel, table=True):
|
||||
"""
|
||||
User table mapping, api response validation and serialisation
|
||||
"""
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
username: str = Field(nullable=False,unique=True, max_length=100)
|
||||
role: UserRole = Field(nullable=True, default=PartnerType.CLIENT)
|
||||
password_hash: str = Field(nullable=False)
|
||||
|
||||
|
||||
class Client(SQLModel, table=True):
|
||||
class Partner(SQLModel, table=True):
|
||||
"""Clients table mapping, api response validation and serialisation"""
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
tin_number: int = Field(nullable=False, unique=True)
|
||||
names: str = Field(max_length=100, nullable=False)
|
||||
phone_number: str = Field(max_length=10, nullable=False)
|
||||
|
||||
|
||||
class Supplier(SQLModel, table=True):
|
||||
"""Supplier table mapping, api response validation and serialisation"""
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
tin_number: int = Field(nullable=False, unique=True)
|
||||
names: str = Field(max_length=100, nullable=False)
|
||||
phone_number: str = Field(max_length=10, nullable=False)
|
||||
type: PartnerType = Field(nullable=False, default=PartnerType.CLIENT)
|
||||
phone_number: str = Field(max_length=10, nullable=True)
|
||||
|
||||
|
||||
class Product(SQLModel, table=True):
|
||||
"""Products table mapping, api response validation and serialisation
|
||||
|
||||
NOTE: purchase price should update every time a supplier credits us goods
|
||||
and price has changed
|
||||
NOTE: Every time a product's purchase price changes, it should be updated
|
||||
here as well
|
||||
"""
|
||||
__table_args__ = (UniqueConstraint("product_code"),)
|
||||
__table_args__ = (UniqueConstraint("product_code"))
|
||||
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
product_code: str = Field(max_length=10, nullable=False)
|
||||
product_code: str = Field(max_length=10, unique=True, nullable=False)
|
||||
product_name: str = Field(max_length=20, nullable=False, unique=True)
|
||||
purchase_price: int = Field(nullable=False)
|
||||
date_modified: datetime = Field(
|
||||
default=None,
|
||||
sa_column=Column(DateTime,
|
||||
sa_column=Column(DateTime(timezone=True),
|
||||
server_default=func.now(),
|
||||
server_onupdate=func.now())
|
||||
)
|
||||
|
||||
|
||||
class Transaction(SQLModel, table=True):
|
||||
"""
|
||||
Transaction table mapping, api response validation and serialisation
|
||||
|
||||
Include both business events to/from suppliers and to/from clients
|
||||
"""
|
||||
__tablename__: str = "transactions"
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
partner_id: Optional[int] = Field(nullable=False, foreign_key="partner.id")
|
||||
transcation_type: TransactionType = Field(
|
||||
sa_column=Column(SQLEnum(TransactionType), nullable=False)
|
||||
)
|
||||
transaction_status: TransactionStatus
|
||||
created_on: datetime = Field(
|
||||
default=None,
|
||||
sa_column=Column(DateTime(timezone=True), server_default=func.now())
|
||||
)
|
||||
updated_on: datetime = Field(
|
||||
default=None,
|
||||
sa_column=Column(
|
||||
DateTime(timezone=True),
|
||||
onupdate=func.now(),
|
||||
server_default=func.now()
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class Transaction_items(SQLModel, table=True):
|
||||
"""
|
||||
Transaction table mapping, api response validation and serialisation
|
||||
Includes transactions details from transactions
|
||||
"""
|
||||
|
||||
|
||||
class Payment(SQLModel, table=True):
|
||||
"""
|
||||
Payments table mapping, api response validation and serialisation
|
||||
|
||||
Include both payments to suppliers and from clients
|
||||
|
||||
"""
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
payment_type: TradeType = Field(
|
||||
sa_column=Column(SQLEnum(TradeType), nullable=False)
|
||||
)
|
||||
product_code: str = Field(nullable=False, foreign_key="product.product_code")
|
||||
client_id: Optional[int] = Field(nullable=False, foreign_key="client.id")
|
||||
|
||||
supplier_id: Optional[int] = Field(nullable=False, foreign_key="supplier.id")
|
||||
amount: int = Field(nullable=False)
|
||||
payment_method: str = Field(max_length=24, nullable=False)
|
||||
date: datetime = Field(
|
||||
default=None,
|
||||
sa_column=Column(DateTime, server_default=func.now())
|
||||
)
|
||||
|
||||
|
||||
class Credit(SQLModel, table=True):
|
||||
class Credit_accounts(SQLModel, table=True):
|
||||
"""Credit table mapping, api response validation and serialisation
|
||||
|
||||
Include both credit from suppliers and to clients
|
||||
"""
|
||||
__tablename__: str = "credit_accounts"
|
||||
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
transcation_type: TradeType = Field(
|
||||
sa_column=Column(SQLEnum(TradeType), nullable=False)
|
||||
)
|
||||
product_code: str = Field(nullable=False, foreign_key="product.product_code")
|
||||
client_id: Optional[int] = Field(nullable=False, foreign_key="client.id")
|
||||
supplier_id: Optional[int] = Field(nullable=False, foreign_key="supplier.id")
|
||||
client_id: Optional[int] = Field(nullable=True, foreign_key="client.id")
|
||||
supplier_id: Optional[int] = Field(nullable=True, foreign_key="supplier.id")
|
||||
qty: int = Field(nullable=False)
|
||||
amount: int = Field(nullable=False)
|
||||
date: datetime = Field(
|
||||
default=None,
|
||||
sa_column=Column(DateTime, server_default=func.now())
|
||||
sa_column=Column(DateTime(timezone=True), server_default=func.now())
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user