brewman/brewman/brewman/models/employee.py

73 lines
2.5 KiB
Python

import uuid
from datetime import date
from decimal import Decimal
from typing import TYPE_CHECKING, List, Optional, Tuple
from sqlalchemy import Column, Date, ForeignKey, Integer, Numeric, Unicode, func, select
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Session, relationship
from .account_base import AccountBase
if TYPE_CHECKING:
# if the target of the relationship is in another module
# that cannot normally be imported at runtime
from .attendance import Attendance
from .fingerprint import Fingerprint
class Employee(AccountBase):
__tablename__ = "employees"
__mapper_args__ = {"polymorphic_identity": "employees"} # type: ignore
id: uuid.UUID = Column("id", UUID(as_uuid=True), ForeignKey("accounts.id"), primary_key=True)
designation: str = Column("designation", Unicode(255), nullable=False)
salary: int = Column("salary", Integer, nullable=False)
points: Decimal = Column("points", Numeric(precision=5, scale=2), nullable=False)
joining_date: date = Column("joining_date", Date, nullable=False)
leaving_date: Optional[date] = Column("leaving_date", Date, nullable=True)
attendances: List["Attendance"] = relationship("Attendance", back_populates="employee")
fingerprints: List["Fingerprint"] = relationship("Fingerprint", back_populates="employee")
def __init__(
self,
code: Optional[int],
name: str,
is_starred: bool,
is_active: bool,
cost_centre_id: uuid.UUID,
designation: str,
salary: int,
points: Decimal,
joining_date: date,
leaving_date: Optional[date] = 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(Employee, self).can_delete(advanced_delete)