from sqlalchemy import String, Integer, ForeignKey, Index, Boolean from sqlalchemy.orm import mapped_column from databases.sql_models.core_mixin import CrudCollection class Departments(CrudCollection): __tablename__ = "departments" __exclude__fields__ = [] parent_department_id = mapped_column(Integer, server_default="0") department_code = mapped_column( String(16), nullable=False, index=True, comment="Department Code" ) department_name = mapped_column( String(128), nullable=False, comment="Department Name" ) department_description = mapped_column(String, server_default="") company_id = mapped_column(ForeignKey("companies.id"), nullable=False) company_uu_id = mapped_column(String, nullable=False, comment="Company UUID") # @classmethod # def create_action(cls, data: DepartmentsPydantic, token): # data_dict = data.model_dump() # data_dict["company_id"] = token.selected_company.company_id # return cls.find_or_create(**data_dict) __table_args__ = {"comment": "Departments Information"} class Duty(CrudCollection): __tablename__ = "duty" __exclude__fields__ = [] duty_name = mapped_column(String, unique=True, nullable=False, comment="Duty Name") duty_code = mapped_column(String, nullable=False, comment="Duty Code") duty_description = mapped_column(String, comment="Duty Description") # @classmethod # def create_action(cls, data: InsertCompanyDuty, token): # # if not cls.__is_super__: # # raise HTTPException( # # status_code=401, detail="You are not authorized to create a duty." # # ) # data_dict = data.model_dump() # # return cls.find_or_create(**data_dict) __table_args__ = ({"comment": "Duty Information"},) class Duties(CrudCollection): __tablename__ = "duties" __exclude__fields__ = [] users_default_duty = mapped_column( ForeignKey("duty.id"), nullable=True, comment="Default Duty for Users" ) company_id = mapped_column(Integer) company_uu_id = mapped_column(String, nullable=False, comment="Company UUID") duties_id = mapped_column(ForeignKey("duty.id"), nullable=False) duties_uu_id = mapped_column(String, nullable=False, comment="Duty UUID") department_id = mapped_column( ForeignKey("departments.id"), nullable=False, comment="Department ID" ) department_uu_id = mapped_column(String, nullable=False, comment="Department UUID") # priority_id = mapped_column(ForeignKey("priority.id"), nullable=True) management_duty = mapped_column( Boolean, server_default="0" ) # is this a prime Company Duty ??? @classmethod def init_a_company_default_duties(cls, company_id, company_uu_id): __default_init__ = ["Execution Office", "IT Department"] active_row = dict( is_confirmed=True, active=True, deleted=False, is_notification_send=True ) execution = Departments.find_or_create( department_name="Execution Office", department_code="EO001", company_id=company_id, company_uu_id=str(company_uu_id), **active_row, ) it_dept = Departments.find_or_create( department_name="IT Department", department_code="ITD001", company_id=company_id, company_uu_id=str(company_uu_id), **active_row, ) bm_duty = Duty.find_or_create( duty_name="Business Manager", duty_code="BM0001", duty_description="Business Manager", **active_row, ) it_duty = Duty.find_or_create( duty_name="IT Manager", duty_code="IT0001", duty_description="IT Manager", **active_row, ) bulk_duty = Duty.find_or_create( duty_name="BULK", duty_code="BULK", duty_description="BULK RECORDS OF THE COMPANY", **active_row, ) occu_duty = Duty.find_or_create( duty_name="OCCUPANT", duty_code="OCCUPANT", duty_description="OCCUPANT RECORDS OF THE COMPANY", **active_row, ) cls.find_or_create( company_id=company_id, company_uu_id=str(company_uu_id), duties_id=bm_duty.id, duties_uu_id=str(bm_duty.uu_id), department_id=execution.id, **active_row, ) cls.find_or_create( company_id=company_id, company_uu_id=str(company_uu_id), duties_id=it_duty.id, duties_uu_id=str(it_duty.uu_id), department_id=it_dept.id, department_uu_id=str(it_dept.uu_id), **active_row, ) cls.find_or_create( company_id=company_id, company_uu_id=str(company_uu_id), duties_id=bulk_duty.id, duties_uu_id=str(bulk_duty.uu_id), department_id=execution.id, department_uu_id=str(execution.uu_id), **active_row, ) cls.find_or_create( company_id=company_id, company_uu_id=str(company_uu_id), duties_id=occu_duty.id, duties_uu_id=str(occu_duty.uu_id), department_id=execution.id, department_uu_id=str(execution.uu_id), **active_row, ) @classmethod def get_bulk_duties_of_a_company(cls, company_id): if bulk_duties := Duties.find_one( duties_id=Duty.find_one(duty_code="BULK").id, company_id=company_id ): return bulk_duties raise Exception("Bulk Duty not found. Please contact with supervisor.") # @classmethod # def create_action(cls, data: InsertCompanyDuty): # data_dict = data.model_dump() # if department := Departments.find_one(uu_id=data.department_uu_id): # data_dict["department_id"] = department.id # del data_dict["department_uu_id"] # return cls.find_or_create(**data_dict) __table_args__ = ( Index("duty_ndx_00", company_id, duties_id, department_id, unique=True), {"comment": "Duty & Company & Department Information"}, ) # department: Mapped[List["CompanyDepartments"]] = relationship( # "CompanyDepartments", back_populates="duties", foreign_keys=[department_id] # ) # employees: Mapped[List["CompanyEmployees"]] = relationship( # "CompanyEmployees", # back_populates="duty", # foreign_keys="CompanyEmployees.duty_id", # ) # duty_app: Mapped["CompanyDutyApp"] = relationship( # "CompanyDutyApp", back_populates="duties", foreign_keys="CompanyDutyApp.company_duty_id" # ) # def get_language_of_duty(self, lang): # if erp_text := ErpText.find_one(lang=lang, text_code=self.duty_code): # return erp_text.text_name, erp_text.text_description # return None, None # company: Mapped["Companies"] = relationship( # "Company", back_populates="departments", foreign_keys=[company_id] # ) # duties: Mapped[List["CompanyDuty"]] = relationship( # "CompanyDuty", # back_populates="department", # foreign_keys="CompanyDuty.department_id", # ) # app_item: Mapped["AppItems"] = relationship( # "AppItems", back_populates="department", foreign_keys="AppItems.department_id" # )