120 lines
4.8 KiB
Python
120 lines
4.8 KiB
Python
import uuid
|
|
|
|
from decimal import Decimal
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import Boolean, ForeignKey, Integer, Numeric, Text, Unicode, Uuid
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from ..db.base_class import reg
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from .account import Account
|
|
from .product_group import ProductGroup
|
|
from .stock_keeping_unit import StockKeepingUnit
|
|
|
|
|
|
@reg.mapped_as_dataclass(unsafe_hash=True)
|
|
class Product:
|
|
__tablename__ = "products"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, insert_default=uuid.uuid4)
|
|
code: Mapped[int] = mapped_column(Integer, unique=True, nullable=False)
|
|
name: Mapped[str] = mapped_column(Unicode, nullable=False, unique=True)
|
|
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
fraction_units: Mapped[str] = mapped_column(Unicode, nullable=False)
|
|
product_group_id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid,
|
|
ForeignKey("product_groups.id"),
|
|
nullable=False,
|
|
)
|
|
account_id: Mapped[uuid.UUID] = mapped_column(Uuid, ForeignKey("accounts.id"), nullable=False)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False)
|
|
is_fixture: Mapped[bool] = mapped_column(Boolean, nullable=False)
|
|
is_purchased: Mapped[bool] = mapped_column(Boolean, nullable=False)
|
|
is_sold: Mapped[bool] = mapped_column(Boolean, nullable=False)
|
|
|
|
skus: Mapped[list["StockKeepingUnit"]] = relationship("StockKeepingUnit", back_populates="product")
|
|
product_group: Mapped["ProductGroup"] = relationship("ProductGroup", back_populates="products")
|
|
account: Mapped["Account"] = relationship("Account", back_populates="products")
|
|
|
|
allergen: Mapped[str] = mapped_column(Text, nullable=False)
|
|
|
|
protein: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
carbohydrate: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
total_sugar: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
added_sugar: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
total_fat: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
saturated_fat: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
trans_fat: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
cholestrol: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
sodium: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
msnf: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
other_solids: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
total_solids: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
water: Mapped[Decimal] = mapped_column(Numeric(precision=15, scale=5), nullable=False)
|
|
|
|
def __init__(
|
|
self,
|
|
name: str,
|
|
description: str | None,
|
|
fraction_units: str,
|
|
product_group_id: uuid.UUID,
|
|
account_id: uuid.UUID,
|
|
is_active: bool,
|
|
is_purchased: bool,
|
|
is_sold: bool,
|
|
allergen: str = "",
|
|
protein: Decimal = 0,
|
|
carbohydrate: Decimal = 0,
|
|
total_sugar: Decimal = 0,
|
|
added_sugar: Decimal = 0,
|
|
total_fat: Decimal = 0,
|
|
saturated_fat: Decimal = 0,
|
|
trans_fat: Decimal = 0,
|
|
cholestrol: Decimal = 0,
|
|
sodium: Decimal = 0,
|
|
msnf: Decimal = 0,
|
|
other_solids: Decimal = 0,
|
|
total_solids: Decimal = 0,
|
|
water: Decimal = 0,
|
|
code: int | None = None,
|
|
id_: uuid.UUID | None = None,
|
|
is_fixture: bool | None = False,
|
|
) -> None:
|
|
if code is not None:
|
|
self.code = code
|
|
self.name = name
|
|
self.description = description
|
|
self.fraction_units = fraction_units
|
|
self.product_group_id = product_group_id
|
|
self.account_id = account_id
|
|
self.is_active = is_active
|
|
self.is_purchased = is_purchased
|
|
self.is_sold = is_sold
|
|
|
|
self.allergen = allergen
|
|
self.protein = protein
|
|
self.carbohydrate = carbohydrate
|
|
self.total_sugar = total_sugar
|
|
self.added_sugar = added_sugar
|
|
self.total_fat = total_fat
|
|
self.saturated_fat = saturated_fat
|
|
self.trans_fat = trans_fat
|
|
self.cholestrol = cholestrol
|
|
self.sodium = sodium
|
|
self.msnf = msnf
|
|
self.other_solids = other_solids
|
|
self.total_solids = total_solids
|
|
self.water = water
|
|
|
|
if id_ is not None:
|
|
self.id = id_
|
|
if is_fixture is not None:
|
|
self.is_fixture = is_fixture
|
|
|
|
@classmethod
|
|
def suspense(cls) -> uuid.UUID:
|
|
return uuid.UUID("aa79a643-9ddc-4790-ac7f-a41f9efb4c15")
|