126 lines
4.3 KiB
Python
126 lines
4.3 KiB
Python
import uuid
|
|
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING, List, Optional
|
|
|
|
from sqlalchemy import (
|
|
DateTime,
|
|
Enum,
|
|
ForeignKey,
|
|
Integer,
|
|
Unicode,
|
|
UniqueConstraint,
|
|
text,
|
|
)
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from ..db.base_class import reg
|
|
from .kot import Kot
|
|
from .voucher_type import VoucherType
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from .customer import Customer
|
|
from .food_table import FoodTable
|
|
from .overview import Overview
|
|
from .reprint import Reprint
|
|
from .settlement import Settlement
|
|
from .user import User
|
|
|
|
|
|
@reg.mapped_as_dataclass(unsafe_hash=True)
|
|
class Voucher:
|
|
__tablename__ = "vouchers"
|
|
__table_args__ = (UniqueConstraint("bill_id", "voucher_type"),)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
"id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), insert_default=uuid.uuid4
|
|
)
|
|
date: Mapped[datetime] = mapped_column("date", DateTime, nullable=False, index=True)
|
|
pax: Mapped[int] = mapped_column("pax", Integer, nullable=False)
|
|
bill_id: Mapped[Optional[int]] = mapped_column("bill_id", Integer)
|
|
kot_id: Mapped[uuid.UUID] = mapped_column("kot_id", Integer, nullable=False, unique=True)
|
|
creation_date: Mapped[datetime] = mapped_column("creation_date", DateTime(), nullable=False)
|
|
last_edit_date: Mapped[datetime] = mapped_column("last_edit_date", DateTime(), nullable=False)
|
|
food_table_id: Mapped[uuid.UUID] = mapped_column(
|
|
"food_table_id",
|
|
UUID(as_uuid=True),
|
|
ForeignKey("food_tables.id"),
|
|
nullable=False,
|
|
)
|
|
customer_id: Mapped[uuid.UUID] = mapped_column(
|
|
"customer_id", UUID(as_uuid=True), ForeignKey("customers.id"), nullable=True
|
|
)
|
|
narration: Mapped[str] = mapped_column("narration", Unicode(1000), nullable=True)
|
|
reason: Mapped[str] = mapped_column("reason", Unicode(255), nullable=True)
|
|
voucher_type: Mapped[VoucherType] = mapped_column("voucher_type", Enum(VoucherType), nullable=False)
|
|
user_id: Mapped[uuid.UUID] = mapped_column("user_id", UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
|
|
|
|
user: Mapped["User"] = relationship("User", backref="vouchers")
|
|
food_table: Mapped["FoodTable"] = relationship("FoodTable", backref="vouchers")
|
|
customer: Mapped["Customer"] = relationship("Customer", backref="vouchers")
|
|
|
|
kots: Mapped[List["Kot"]] = relationship(
|
|
"Kot",
|
|
back_populates="voucher",
|
|
cascade="delete, delete-orphan",
|
|
cascade_backrefs=False,
|
|
order_by=Kot.code,
|
|
)
|
|
settlements: Mapped[List["Settlement"]] = relationship(
|
|
"Settlement",
|
|
backref="voucher",
|
|
cascade="delete, delete-orphan",
|
|
cascade_backrefs=False,
|
|
)
|
|
reprints: Mapped[List["Reprint"]] = relationship(
|
|
"Reprint",
|
|
back_populates="voucher",
|
|
cascade="delete, delete-orphan",
|
|
cascade_backrefs=False,
|
|
)
|
|
status: Mapped[Optional["Overview"]] = relationship("Overview", back_populates="voucher", uselist=False)
|
|
|
|
def __init__(
|
|
self,
|
|
date,
|
|
pax,
|
|
bill_id,
|
|
kot_id,
|
|
food_table_id,
|
|
customer_id,
|
|
voucher_type,
|
|
user_id,
|
|
):
|
|
self.date = date
|
|
self.pax = pax
|
|
self.creation_date = date
|
|
self.last_edit_date = date
|
|
self.bill_id = bill_id
|
|
self.kot_id = kot_id
|
|
self.food_table_id = food_table_id
|
|
self.customer_id = customer_id
|
|
self.narration = None
|
|
self.voucher_type = voucher_type
|
|
self.user_id = user_id
|
|
|
|
@property
|
|
def full_bill_id(self):
|
|
if self.voucher_type == VoucherType.KOT:
|
|
return "K-" + str(self.kot_id)
|
|
if self.voucher_type == VoucherType.NO_CHARGE:
|
|
return "NC-" + str(self.bill_id)
|
|
if self.voucher_type == VoucherType.STAFF:
|
|
return "ST-" + str(self.bill_id)
|
|
if self.voucher_type == VoucherType.REGULAR_BILL:
|
|
return str(self.bill_id // 10000) + "-" + str(self.bill_id % 10000)
|
|
if self.voucher_type == VoucherType.VOID:
|
|
return "K-" + str(self.kot_id)
|
|
else:
|
|
raise Exception
|
|
|
|
@property
|
|
def amount(self):
|
|
return round(sum(i.amount for k in self.kots for i in k.inventories), 2)
|