57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
import datetime
|
|
import uuid
|
|
|
|
from decimal import Decimal
|
|
from typing import TYPE_CHECKING, Optional
|
|
|
|
from sqlalchemy import Column, Date, ForeignKey, Numeric, 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 .cost_centre import CostCentre
|
|
from .stock_keeping_unit import StockKeepingUnit
|
|
|
|
|
|
class ClosingStock(Base):
|
|
__tablename__ = "closing_stocks"
|
|
__table_args__ = (UniqueConstraint("date", "cost_centre_id", "sku_id"),)
|
|
id: uuid.UUID = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
date_: datetime.date = Column("date", Date, nullable=False)
|
|
cost_centre_id: uuid.UUID = Column(
|
|
"cost_centre_id",
|
|
UUID(as_uuid=True),
|
|
ForeignKey("cost_centres.id"),
|
|
nullable=False,
|
|
)
|
|
sku_id: uuid.UUID = Column("sku_id", UUID(as_uuid=True), ForeignKey("stock_keeping_units.id"), nullable=False)
|
|
quantity: Decimal = Column("quantity", Numeric(precision=15, scale=2), nullable=False)
|
|
|
|
cost_centre: Mapped["CostCentre"] = relationship("CostCentre")
|
|
sku: Mapped["StockKeepingUnit"] = relationship("StockKeepingUnit")
|
|
|
|
def __init__(
|
|
self,
|
|
date_: datetime.date,
|
|
cost_centre_id: uuid.UUID,
|
|
quantity: Decimal,
|
|
sku_id: Optional[uuid.UUID] = None,
|
|
sku: Optional["StockKeepingUnit"] = None,
|
|
id_: Optional[uuid.UUID] = None,
|
|
):
|
|
self.date_ = date_
|
|
self.cost_centre_id = cost_centre_id
|
|
self.quantity = quantity
|
|
if sku_id is not None:
|
|
self.sku_id = sku_id
|
|
if sku is not None:
|
|
self.sku = sku
|
|
self.sku_id = sku.id
|
|
if id_ is not None:
|
|
self.id = id_
|