brewman/brewman/brewman/models/employee.py

72 lines
2.4 KiB
Python

import uuid
from datetime import date
from decimal import Decimal
from typing import TYPE_CHECKING
from sqlalchemy import Date, ForeignKey, Integer, Numeric, Unicode, Uuid, func, select
from sqlalchemy.orm import Mapped, Session, mapped_column, relationship
from ..db.base_class import reg
from .account_base import AccountBase
if TYPE_CHECKING:
from .attendance import Attendance
from .fingerprint import Fingerprint
@reg.mapped_as_dataclass(unsafe_hash=True)
class Employee(AccountBase):
__tablename__ = "employees"
__mapper_args__ = {"polymorphic_identity": "employees"} # type: ignore
id: Mapped[uuid.UUID] = mapped_column(Uuid, ForeignKey("accounts.id"), primary_key=True)
designation: Mapped[str] = mapped_column(Unicode, nullable=False)
salary: Mapped[int] = mapped_column(Integer, nullable=False)
points: Mapped[Decimal] = mapped_column(Numeric(precision=5, scale=2), nullable=False)
joining_date: Mapped[date] = mapped_column(Date, nullable=False)
leaving_date: Mapped[date | None] = mapped_column(Date, nullable=True)
attendances: Mapped[list["Attendance"]] = relationship("Attendance", back_populates="employee")
fingerprints: Mapped[list["Fingerprint"]] = relationship("Fingerprint", back_populates="employee")
def __init__(
self,
code: int | None,
name: str,
is_starred: bool,
is_active: bool,
cost_centre_id: uuid.UUID,
designation: str,
salary: int,
points: Decimal,
joining_date: date,
leaving_date: date | None = None,
) -> None:
self.designation = designation
self.salary = salary
self.points = points
self.joining_date = joining_date
self.leaving_date = leaving_date
super().__init__(
code=code if code is not None else 0,
name=name,
type_id=10,
is_starred=is_starred,
is_active=is_active,
is_reconcilable=False,
cost_centre_id=cost_centre_id,
)
def create(self, db: Session) -> "Employee":
self.code = db.execute(
select(func.coalesce(func.max(AccountBase.code), 0) + 1).filter(AccountBase.type_id == self.type_id)
).scalar_one()
self.name += f" ({str(self.code)})"
db.add(self)
return self
def can_delete(self, advanced_delete: bool) -> tuple[bool, str]:
return super().can_delete(advanced_delete)