62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
import uuid
|
|
|
|
from datetime import date
|
|
from decimal import Decimal
|
|
from typing import TYPE_CHECKING, List, Optional
|
|
|
|
from sqlalchemy import Column, Date, ForeignKey, Numeric
|
|
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 .inventory import Inventory
|
|
from .stock_keeping_unit import StockKeepingUnit
|
|
|
|
|
|
class Batch(Base):
|
|
__tablename__ = "batches"
|
|
|
|
id: uuid.UUID = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
name: date = Column("name", Date, nullable=False)
|
|
sku_id: uuid.UUID = Column("sku_id", UUID(as_uuid=True), ForeignKey("stock_keeping_units.id"), nullable=False)
|
|
quantity_remaining: Decimal = Column("quantity_remaining", Numeric(precision=15, scale=2), nullable=False)
|
|
rate: Decimal = Column("rate", Numeric(precision=15, scale=2), nullable=False)
|
|
tax: Decimal = Column("tax", Numeric(precision=15, scale=5), nullable=False)
|
|
discount: Decimal = Column("discount", Numeric(precision=15, scale=5), nullable=False)
|
|
|
|
inventories: List["Inventory"] = relationship("Inventory", back_populates="batch")
|
|
sku: Mapped["StockKeepingUnit"] = relationship("StockKeepingUnit", back_populates="batches")
|
|
|
|
def __init__(
|
|
self,
|
|
name: date,
|
|
quantity_remaining: Decimal,
|
|
rate: Decimal,
|
|
tax: Decimal,
|
|
discount: Decimal,
|
|
sku_id: Optional[uuid.UUID] = None,
|
|
sku: Optional["StockKeepingUnit"] = None,
|
|
):
|
|
self.name = name
|
|
if sku_id is not None and sku is None:
|
|
self.sku_id = sku_id
|
|
self.quantity_remaining = quantity_remaining
|
|
self.rate = rate
|
|
self.tax = tax
|
|
self.discount = discount
|
|
if sku is not None:
|
|
self.sku_id = sku.id
|
|
self.sku = sku
|
|
|
|
def amount(self) -> Decimal:
|
|
return self.quantity_remaining * self.rate * (1 + self.tax) * (1 - self.discount)
|
|
|
|
@classmethod
|
|
def suspense(cls) -> uuid.UUID:
|
|
return uuid.UUID("a955790e-93cf-493c-a816-c7d92b127383")
|