63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
import datetime
|
|
import uuid
|
|
|
|
from decimal import Decimal
|
|
from typing import TYPE_CHECKING, List, Optional
|
|
|
|
from sqlalchemy import Column, Date, ForeignKey, Numeric, Unicode
|
|
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 .recipe_item import RecipeItem
|
|
from .stock_keeping_unit import StockKeepingUnit
|
|
|
|
|
|
class Recipe(Base):
|
|
__tablename__ = "recipes"
|
|
|
|
id: uuid.UUID = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
sku_id: uuid.UUID = Column("sku_id", UUID(as_uuid=True), ForeignKey("stock_keeping_units.id"), nullable=False)
|
|
|
|
recipe_yield: Decimal = Column("recipe_yield", Numeric(precision=15, scale=2), 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)
|
|
notes: str = Column("notes", Unicode(255))
|
|
|
|
valid_from: datetime.date = Column("valid_from", Date, nullable=False)
|
|
valid_till: datetime.date = Column("valid_till", Date, nullable=False)
|
|
|
|
items: List["RecipeItem"] = relationship("RecipeItem", back_populates="recipe")
|
|
sku: Mapped["StockKeepingUnit"] = relationship("StockKeepingUnit", back_populates="recipes")
|
|
|
|
def __init__(
|
|
self,
|
|
recipe_yield: Decimal,
|
|
cost_price: Decimal,
|
|
sale_price: Decimal,
|
|
valid_from: datetime.date,
|
|
valid_till: datetime.date,
|
|
notes: str = "",
|
|
sku_id: Optional[uuid.UUID] = None,
|
|
sku: Optional["StockKeepingUnit"] = None,
|
|
id_: Optional[uuid.UUID] = None,
|
|
) -> None:
|
|
if sku_id is not None:
|
|
self.sku_id = sku_id
|
|
self.recipe_yield = recipe_yield
|
|
self.cost_price = cost_price
|
|
self.sale_price = sale_price
|
|
self.valid_from = valid_from
|
|
self.valid_till = valid_till
|
|
self.notes = notes
|
|
if id_ is not None:
|
|
self.id = id_
|
|
if sku is not None:
|
|
self.sku_id = sku.id
|
|
self.sku = sku
|