brewman/brewman/brewman/models/stock_keeping_unit.py

59 lines
2.2 KiB
Python

import uuid
from decimal import Decimal
from typing import TYPE_CHECKING, List, Optional
from sqlalchemy import Column, ForeignKey, Numeric, Unicode, UniqueConstraint
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, relationship
from .meta import Base
if TYPE_CHECKING:
# if the target of the relationship is in another module
# that cannot normally be imported at runtime
from .batch import Batch
from .product import Product
from .recipe import Recipe
class StockKeepingUnit(Base):
__tablename__ = "stock_keeping_units"
__table_args__ = (UniqueConstraint("product_id", "units"),)
id: uuid.UUID = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
product_id: uuid.UUID = Column("product_id", UUID(as_uuid=True), ForeignKey("products.id"), nullable=False)
units: str = Column("units", Unicode(255), nullable=False)
fraction: Decimal = Column("fraction", Numeric(precision=15, scale=5), nullable=False)
product_yield: Decimal = Column("product_yield", Numeric(precision=15, scale=5), nullable=False)
cost_price: Decimal = Column("cost_price", Numeric(precision=15, scale=2), nullable=False)
sale_price: Decimal = Column("sale_price", Numeric(precision=15, scale=2), nullable=False)
product: Mapped["Product"] = relationship("Product", back_populates="skus")
batches: List["Batch"] = relationship("Batch", back_populates="sku")
recipes: List["Recipe"] = relationship("Recipe", back_populates="sku")
def __init__(
self,
units: str,
fraction: Decimal,
product_yield: Decimal,
cost_price: Decimal,
sale_price: Decimal,
product_id: Optional[uuid.UUID] = None,
product: Optional["Product"] = None,
id_: Optional[uuid.UUID] = None,
) -> None:
if product_id is not None:
self.product_id = product_id
self.units = units
self.fraction = fraction
self.product_yield = product_yield
self.cost_price = cost_price
self.sale_price = sale_price
if id_ is not None:
self.id = id_
if product is not None:
self.product = product