79 lines
3.3 KiB
Python
79 lines
3.3 KiB
Python
import datetime
|
|
import uuid
|
|
|
|
from typing import TYPE_CHECKING, List, Optional
|
|
|
|
from sqlalchemy import Boolean, Column, Date, DateTime, Enum, ForeignKey, Unicode
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import Mapped, relationship
|
|
|
|
from .meta import Base
|
|
from .voucher_type import VoucherType
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
# if the target of the relationship is in another module
|
|
# that cannot normally be imported at runtime
|
|
from .employee_benefit import EmployeeBenefit
|
|
from .incentive import Incentive
|
|
from .inventory import Inventory
|
|
from .journal import Journal
|
|
from .user import User
|
|
|
|
|
|
class Voucher(Base):
|
|
__tablename__ = "vouchers"
|
|
|
|
id: uuid.UUID = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
date: datetime.date = Column("date", Date, nullable=False, index=True)
|
|
narration: str = Column("narration", Unicode(1000), nullable=False)
|
|
is_reconciled: bool = Column("is_reconciled", Boolean, nullable=False)
|
|
reconcile_date: datetime.date = Column("reconcile_date", Date, nullable=False)
|
|
is_starred: bool = Column("is_starred", Boolean, nullable=False)
|
|
creation_date: datetime.datetime = Column("creation_date", DateTime(), nullable=False)
|
|
last_edit_date: datetime.datetime = Column("last_edit_date", DateTime(), nullable=False)
|
|
voucher_type: VoucherType = Column("voucher_type", Enum(VoucherType), nullable=False)
|
|
user_id: uuid.UUID = Column("user_id", UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
|
|
posted: bool = Column("is_posted", Boolean, nullable=False)
|
|
poster_id: uuid.UUID = Column("poster_id", UUID(as_uuid=True), ForeignKey("users.id"))
|
|
|
|
user: Mapped["User"] = relationship("User", primaryjoin="User.id==Voucher.user_id")
|
|
poster: Mapped["User"] = relationship("User", primaryjoin="User.id==Voucher.poster_id")
|
|
|
|
journals: List["Journal"] = relationship("Journal", cascade="delete, delete-orphan", back_populates="voucher")
|
|
|
|
inventories: List["Inventory"] = relationship(
|
|
"Inventory", cascade="delete, delete-orphan", back_populates="voucher"
|
|
)
|
|
employee_benefits: List["EmployeeBenefit"] = relationship(
|
|
"EmployeeBenefit", cascade="delete, delete-orphan", backref="voucher"
|
|
)
|
|
incentives: List["Incentive"] = relationship("Incentive", cascade="delete, delete-orphan", backref="voucher")
|
|
|
|
def __init__(
|
|
self,
|
|
date: datetime.date,
|
|
voucher_type: VoucherType,
|
|
user_id: uuid.UUID,
|
|
narration: str = "",
|
|
is_starred: bool = False,
|
|
posted: bool = False,
|
|
is_reconciled: bool = False,
|
|
reconcile_date: Optional[datetime.date] = None,
|
|
poster_id: Optional[uuid.UUID] = None,
|
|
creation_date: Optional[datetime.datetime] = None,
|
|
last_edit_date: Optional[datetime.datetime] = None,
|
|
):
|
|
self.date = date
|
|
self.is_reconciled = is_reconciled
|
|
self.reconcile_date = reconcile_date or date
|
|
self.is_starred = is_starred if is_starred is not None else False
|
|
self.narration = narration
|
|
self.posted = posted
|
|
self.creation_date = creation_date or datetime.datetime.utcnow()
|
|
self.last_edit_date = last_edit_date or datetime.datetime.utcnow()
|
|
self.voucher_type = voucher_type
|
|
self.user_id = user_id
|
|
if poster_id is not None:
|
|
self.poster_id = poster_id
|