first commit
This commit is contained in:
535
databases/sql_models/company/company.py
Normal file
535
databases/sql_models/company/company.py
Normal file
@@ -0,0 +1,535 @@
|
||||
from fastapi.exceptions import HTTPException
|
||||
from databases.sql_models.core_mixin import CrudCollection
|
||||
|
||||
from sqlalchemy import String, Integer, Boolean, ForeignKey, Index
|
||||
from sqlalchemy.orm import mapped_column
|
||||
|
||||
from api_configs import RelationAccess
|
||||
from application.shared_classes import SelectAction, Explanation
|
||||
from validations import InsertCompany, UpdateCompany
|
||||
from validations.company import MatchCompany2Company
|
||||
from valids.auth.token_validations import EmployeeTokenObject
|
||||
|
||||
|
||||
class RelationshipDutyCompany(CrudCollection):
|
||||
"""
|
||||
CompanyRelationship class based on declarative_base and CrudCollection via session
|
||||
Company -> Sub Company -> Sub-Sub Company
|
||||
|
||||
if owner_id == parent_id: can manipulate data of any record
|
||||
else: Read-Only
|
||||
duty_id = if relationship_type == base An organization / not operational / no responsible person
|
||||
|
||||
relationship = company_id filter -> Action filter(company_id) relationship_type = Organization
|
||||
relationship = company_id filter -> Action filter(company_id) relationship_type = Commercial
|
||||
"""
|
||||
|
||||
__tablename__ = "relationship_duty_company"
|
||||
__exclude__fields__ = []
|
||||
__access_by__ = RelationAccess.SuperAccessList
|
||||
|
||||
owner_id = mapped_column(ForeignKey("companies.id"), nullable=False) # 1
|
||||
duties_id = mapped_column(
|
||||
ForeignKey("duties.id"), nullable=False
|
||||
) # duty -> (n)employee Evyos LTD
|
||||
|
||||
member_id = mapped_column(ForeignKey("companies.id"), nullable=False) # 2, 3, 4
|
||||
parent_id = mapped_column(ForeignKey("companies.id"), nullable=True) # None
|
||||
|
||||
relationship_type = mapped_column(
|
||||
String, nullable=True, server_default="Commercial"
|
||||
) # Commercial, Organization # Bulk
|
||||
child_count = mapped_column(Integer) # 0
|
||||
show_only = mapped_column(Boolean, server_default="0")
|
||||
|
||||
# related_company: Mapped[List["Companies"]] = relationship(
|
||||
# "Companies",
|
||||
# back_populates="related_companies",
|
||||
# foreign_keys=[related_company_id],
|
||||
# )
|
||||
|
||||
@classmethod
|
||||
def match_company_to_company_commercial(cls, data: MatchCompany2Company, token):
|
||||
from database_sql_models import Companies, Duties
|
||||
|
||||
token_duties_id, token_company_id = token.get("duty_id"), token.get(
|
||||
"company_id"
|
||||
)
|
||||
send_duties, list_match_company_id = Duties.find_one(uu_id=data.duty_uu_id), []
|
||||
send_user_duties = Duties.find_one(
|
||||
duties_id=send_duties.id, company_id=token_duties_id
|
||||
)
|
||||
if not send_user_duties:
|
||||
raise Exception(
|
||||
"Send Duty is not found in company. Please check duty uuid and try again."
|
||||
)
|
||||
|
||||
for company_uu_id in list(data.match_company_uu_id):
|
||||
company = Companies.find_one(uu_id=company_uu_id)
|
||||
bulk_company = RelationshipDutyCompany.find_one(
|
||||
owner_id=token_company_id,
|
||||
relationship_type="Bulk",
|
||||
member_id=company.id,
|
||||
)
|
||||
if not bulk_company:
|
||||
raise Exception(
|
||||
f"Bulk Company is not found in company. "
|
||||
f"Please check company uuid {bulk_company.uu_id} and try again."
|
||||
)
|
||||
list_match_company_id.append(bulk_company)
|
||||
|
||||
for match_company_id in list_match_company_id:
|
||||
RelationshipDutyCompany.find_or_create(
|
||||
owner_id=token_company_id,
|
||||
duties_id=send_user_duties.id,
|
||||
member_id=match_company_id.id,
|
||||
parent_id=match_company_id.parent_id,
|
||||
relationship_type="Commercial",
|
||||
show_only=False,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def match_company_to_company_organization(cls, data: MatchCompany2Company, token):
|
||||
from database_sql_models import Companies, Duties
|
||||
|
||||
token_duties_id, token_company_id = token.get("duty_id"), token.get(
|
||||
"company_id"
|
||||
)
|
||||
send_duties, list_match_company_id = Duties.find_one(uu_id=data.duty_uu_id), []
|
||||
send_user_duties = Duties.find_one(
|
||||
duties_id=send_duties.id, company_id=token_duties_id
|
||||
)
|
||||
if not send_user_duties:
|
||||
raise Exception(
|
||||
"Send Duty is not found in company. Please check duty uuid and try again."
|
||||
)
|
||||
|
||||
for company_uu_id in list(data.match_company_uu_id):
|
||||
company = Companies.find_one(uu_id=company_uu_id)
|
||||
bulk_company = RelationshipDutyCompany.find_one(
|
||||
owner_id=token_company_id,
|
||||
relationship_type="Bulk",
|
||||
member_id=company.id,
|
||||
)
|
||||
if not bulk_company:
|
||||
raise Exception(
|
||||
f"Bulk Company is not found in company. "
|
||||
f"Please check company uuid {bulk_company.uu_id} and try again."
|
||||
)
|
||||
list_match_company_id.append(bulk_company)
|
||||
|
||||
for match_company_id in list_match_company_id:
|
||||
Duties.init_a_company_default_duties(company_id=match_company_id.id)
|
||||
RelationshipDutyCompany.find_or_create(
|
||||
owner_id=token_company_id,
|
||||
duties_id=send_user_duties.id,
|
||||
member_id=match_company_id.id,
|
||||
parent_id=match_company_id.parent_id,
|
||||
relationship_type="Organization",
|
||||
show_only=False,
|
||||
)
|
||||
|
||||
__table_args__ = (
|
||||
Index(
|
||||
"_company_relationship_ndx_01",
|
||||
duties_id,
|
||||
owner_id,
|
||||
member_id,
|
||||
relationship_type,
|
||||
unique=True,
|
||||
),
|
||||
{"comment": "Company Relationship Information"},
|
||||
)
|
||||
|
||||
|
||||
class AbstractCompany:
|
||||
"""
|
||||
Abstract and explanation of Company class for end-user guide
|
||||
"""
|
||||
|
||||
formal_name = Explanation(
|
||||
explanation="Devletin resmi kayıtlarında bulunan şirket ünvanıdır.",
|
||||
usage="Devletin resmi kayıtlarında bulunan şirket adı istendiğinde kullanılır.",
|
||||
alias="Resmi Ünvan",
|
||||
example=["X Şirketi LTD", "Y Şirketi A.Ş."],
|
||||
)
|
||||
company_type = Explanation(
|
||||
explanation="Şirketin türüdür.",
|
||||
usage="Şirketin türü istendiğinde kullanılır.",
|
||||
alias="Şirket Türü",
|
||||
example=[
|
||||
"Şahıs",
|
||||
"Limited",
|
||||
"Anonim",
|
||||
"Kolektif",
|
||||
"Komandit",
|
||||
"Kooperatif",
|
||||
"Serbest Meslek",
|
||||
"Adi Ortaklık",
|
||||
],
|
||||
)
|
||||
commercial_type = Explanation(
|
||||
explanation="Şirketin ticari türüdür.",
|
||||
usage="Şirketin ticari türü istendiğinde kullanılır.",
|
||||
alias="Ticari Tür",
|
||||
example=["Tüzel", "Birey"],
|
||||
)
|
||||
tax_no = Explanation(
|
||||
explanation="Şirketin vergi numarasıdır.",
|
||||
usage="Şirketin vergi numarası istendiğinde kullanılır.",
|
||||
alias="Vergi No",
|
||||
example=["1234567890"],
|
||||
)
|
||||
public_name = Explanation(
|
||||
explanation="Şirketin kamuoyunda bilinen adıdır.",
|
||||
usage="Şirketin kamuoyunda bilinen adı istendiğinde kullanılır.",
|
||||
alias="Piyasada Bilinen Adı",
|
||||
example=["X Şirketi", "Y Şirketi"],
|
||||
)
|
||||
company_tag = Explanation(
|
||||
explanation="Şirketin takma adı veya etiketidir.",
|
||||
usage="Şirketin yöneticisin karar verdiği takma adı veya etiketi istendiğinde kullanılır.",
|
||||
alias="Şirket Etiketi veya Takma Adı",
|
||||
example=["X", "Y"],
|
||||
)
|
||||
default_lang_type = Explanation(
|
||||
explanation="Şirketin varsayılan dil türüdür.",
|
||||
usage="Şirketin varsayılan dil türü istendiğinde kullanılır.",
|
||||
alias="Şirketin Dil Türü",
|
||||
example=["TR", "EN"],
|
||||
)
|
||||
default_money_type = Explanation(
|
||||
explanation="Şirketin varsayılan para birimi türüdür.",
|
||||
usage="Şirketin varsayılan para birimi türü istendiğinde kullanılır.",
|
||||
alias="Şirketin Para Birimi Türü",
|
||||
example=["TL", "USD", "EUR"],
|
||||
)
|
||||
is_commercial = Explanation(
|
||||
explanation="Şirketin ticari olup olmadığını belirtir.",
|
||||
usage="Şirketin ticari olup olmadığını applikasyonun anlaması için kullanılır.",
|
||||
condition=lambda commercial_type: True if commercial_type == "Şahıs" else False,
|
||||
alias="Şirket Ticari mi?",
|
||||
)
|
||||
is_blacklist = Explanation(
|
||||
explanation="Şirketin kara listeye alınıp alınmadığını belirtir.",
|
||||
usage="Şirketin kara listeye alınıp alınmadığını applikasyonun anlaması için kullanılır.",
|
||||
alias="Kara Listeye alınsın mı?",
|
||||
example=[True, False],
|
||||
)
|
||||
parent_id = Explanation(
|
||||
explanation="Şirketin sorumlu olduğu şirketin ID'sidir.",
|
||||
usage="Şirketin sorumlu olduğu şirketin ID'si istendiğinde kullanılır.",
|
||||
alias="Sorumlu Şirket",
|
||||
example=[
|
||||
"Bir şirketin sorumlu şirketi hangisi olduğunu bulmak için kullanılır.",
|
||||
],
|
||||
)
|
||||
workplace_no = Explanation(
|
||||
explanation="Şirketin iş yeri numarasıdır.",
|
||||
usage="Şirketin iş yeri numarası istendiğinde kullanılır.",
|
||||
alias="İş Yeri No",
|
||||
example=["1234567890"],
|
||||
)
|
||||
official_address_id = Explanation(
|
||||
explanation="Şirketin resmi adresidi.",
|
||||
usage="Şirketin resmi adresinin ne olduğunu bulmak için kullanılır.",
|
||||
alias="Resmi Adres",
|
||||
example=[
|
||||
"Bu şirketin adresi nedir sorusuna cevap vermek için kullanılır.",
|
||||
],
|
||||
)
|
||||
top_responsible_company_id = Explanation(
|
||||
explanation="Şirketin en üst sorumlu şirketin ID'sidir.",
|
||||
usage="Şirketin en üst sorumlu şirketin hangisi olduğunu bulmak için kullanılır.",
|
||||
alias="Ana Yetkili Şirket",
|
||||
example=[
|
||||
"Bölge veya ülke genelinde en üst sorumlu şirketin hangisi olduğunu belirtmek için kullanılır.",
|
||||
],
|
||||
)
|
||||
buildings = Explanation(
|
||||
explanation="Şirketin sahip olduğu binaların listesidir.",
|
||||
usage="Şirketin sahip olduğu binaların listesini bulmak için kullanılır.",
|
||||
alias="Sorumlu olduğu binalar Binalar",
|
||||
example=[
|
||||
"Şirketin sahip olduğu binaların listesini bulmak için kullanılır.",
|
||||
],
|
||||
)
|
||||
|
||||
def wag_create_company(self):
|
||||
"""
|
||||
Er kişiye wag_create_company fonksiyonu = fieldları manipule edebilir?
|
||||
78 ile oluşturulan bir user için wag_create_company fonksiyonu = fieldları manipule edebilir?
|
||||
"""
|
||||
return {
|
||||
"commercial_type": self.commercial_type,
|
||||
"formal_name": self.formal_name,
|
||||
"public_name": self.public_name,
|
||||
"company_type": self.company_type,
|
||||
"tax_no": self.tax_no,
|
||||
"workplace_no": self.workplace_no,
|
||||
"company_tag": self.company_tag,
|
||||
"default_lang_type": self.default_lang_type,
|
||||
"default_money_type": self.default_money_type,
|
||||
"official_address_id": self.official_address_id,
|
||||
}
|
||||
|
||||
def wag_update_company(self):
|
||||
return {
|
||||
"commercial_type": self.commercial_type,
|
||||
"formal_name": self.formal_name,
|
||||
"public_name": self.public_name,
|
||||
"company_type": self.company_type,
|
||||
"tax_no": self.tax_no,
|
||||
"workplace_no": self.workplace_no,
|
||||
"company_tag": self.company_tag,
|
||||
"default_lang_type": self.default_lang_type,
|
||||
"default_money_type": self.default_money_type,
|
||||
"official_address_id": self.official_address_id,
|
||||
}
|
||||
|
||||
|
||||
class Companies(CrudCollection, SelectAction):
|
||||
"""
|
||||
Company class based on declarative_base and CrudCollection via session
|
||||
formal_name = Government register name by offical
|
||||
public_name = Public registered name by User
|
||||
nick_name = Search by nickname, commercial_type = Tüzel veya birey
|
||||
"""
|
||||
|
||||
__tablename__ = "companies"
|
||||
|
||||
__exclude__fields__ = ["is_blacklist", "is_commercial"]
|
||||
__access_by__ = []
|
||||
__many__table__ = RelationshipDutyCompany
|
||||
__explain__ = AbstractCompany()
|
||||
|
||||
formal_name = mapped_column(String, nullable=False, comment="Formal Name")
|
||||
company_type = mapped_column(String, nullable=False, comment="Company Type")
|
||||
commercial_type = mapped_column(String, nullable=False, comment="Commercial Type")
|
||||
tax_no = mapped_column(
|
||||
String, index=True, unique=True, nullable=False, comment="Tax No"
|
||||
)
|
||||
|
||||
public_name = mapped_column(String, comment="Public Name of a company")
|
||||
company_tag = mapped_column(String, comment="Company Tag")
|
||||
default_lang_type = mapped_column(String, server_default="TR")
|
||||
default_money_type = mapped_column(String, server_default="TL")
|
||||
is_commercial = mapped_column(Boolean, server_default="False")
|
||||
is_blacklist = mapped_column(Boolean, server_default="False")
|
||||
parent_id = mapped_column(Integer, nullable=True)
|
||||
workplace_no = mapped_column(String, nullable=True)
|
||||
|
||||
official_address_id = mapped_column(ForeignKey("addresses.id"))
|
||||
official_address_uu_id = mapped_column(
|
||||
String, nullable=True, comment="Official Address UUID"
|
||||
)
|
||||
top_responsible_company_id = mapped_column(
|
||||
ForeignKey("companies.id"), nullable=True
|
||||
)
|
||||
top_responsible_company_uu_id = mapped_column(
|
||||
String, nullable=True, comment="Top Responsible Company UUID"
|
||||
)
|
||||
|
||||
# buildings: Mapped[List["Build"]] = relationship(
|
||||
# "Build",
|
||||
# back_populates="companies",
|
||||
# foreign_keys="Build.company_id",
|
||||
# )
|
||||
|
||||
__table_args__ = (
|
||||
Index("_company_ndx_01", tax_no, unique=True),
|
||||
Index("_company_ndx_02", formal_name, public_name),
|
||||
{"comment": "Company Information"},
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def create_action(cls, data: InsertCompany, token: EmployeeTokenObject):
|
||||
from database_sql_models import Addresses, Duties
|
||||
|
||||
data_dict = data.model_dump()
|
||||
if cls.find_one(tax_no=str(data.tax_no).strip()):
|
||||
raise Exception(
|
||||
"Company already exists. Please ask supervisor to make company visible for your duty."
|
||||
)
|
||||
|
||||
official_address = Addresses.find_one(uu_id=data.official_address_uu_id)
|
||||
if not official_address:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Official address is not found. Please check address uuid and try again.",
|
||||
)
|
||||
|
||||
bulk_duties = Duties.get_bulk_duties_of_a_company(
|
||||
company_id=token.selected_company.company_id
|
||||
)
|
||||
|
||||
data_dict["parent_id"] = token.selected_company.company_id
|
||||
data_dict["official_address_id"] = official_address.id
|
||||
data_dict["official_address_uu_id"] = str(official_address.uu_id)
|
||||
data_dict["top_responsible_company_id"] = token.selected_company.company_id
|
||||
data_dict["top_responsible_company_uu_id"] = (
|
||||
token.selected_company.company_uu_id
|
||||
)
|
||||
|
||||
company_created = cls.find_or_create(**data_dict)
|
||||
if not company_created.is_found:
|
||||
RelationshipDutyCompany.find_or_create(
|
||||
owner_id=token.selected_company.company_id,
|
||||
duties_id=bulk_duties.id,
|
||||
member_id=company_created.id,
|
||||
parent_id=company_created.parent_id,
|
||||
relationship_type="Bulk",
|
||||
show_only=False,
|
||||
)
|
||||
if (
|
||||
not str(token.get("priority_code")) == "78"
|
||||
): # Company based configuration will be applied
|
||||
user_duties = Duties.find_one(
|
||||
duties_id=token.selected_company.duty_id,
|
||||
company_id=token.selected_company.company_id,
|
||||
)
|
||||
RelationshipDutyCompany.find_or_create(
|
||||
owner_id=token.selected_company.company_id,
|
||||
duties_id=user_duties.id,
|
||||
member_id=company_created.id,
|
||||
parent_id=company_created.parent_id,
|
||||
relationship_type="Commercial",
|
||||
show_only=False,
|
||||
)
|
||||
return company_created
|
||||
|
||||
@classmethod
|
||||
def update_action(cls, data: UpdateCompany, token):
|
||||
from database_sql_models import Addresses
|
||||
|
||||
data_dict = data.excluded_dump()
|
||||
duty_id = token.get("duty_id")
|
||||
company_id = token.get("company_id")
|
||||
if data.official_address_uu_id:
|
||||
official_address = Addresses.find_one(uu_id=data.official_address_uu_id)
|
||||
data_dict["official_address_id"] = official_address.id
|
||||
del data_dict["official_address_uu_id"], data_dict["company_uu_id"]
|
||||
company_to_update = cls.select_action(
|
||||
duty_id=duty_id,
|
||||
filter_expr=[
|
||||
cls.uu_id == data.company_uu_id,
|
||||
RelationshipDutyCompany.parent_id == company_id,
|
||||
],
|
||||
)
|
||||
return company_to_update.update(**data_dict)
|
||||
|
||||
# parent_id = mapped_column(ForeignKey("companies.id"))
|
||||
# if data.parent_uu_id:
|
||||
# company = Companies.find_one(uu_id=data.parent_uu_id)
|
||||
# data_dict["parent_id"] = company.id
|
||||
# def is_access_valid(self, endpoint_ext: str):
|
||||
# try:
|
||||
# if (
|
||||
# not arrow.get(self.stop_date)
|
||||
# > arrow.utcnow()
|
||||
# > arrow.get(self.start_date)
|
||||
# ):
|
||||
# message = f"Kullanıcı yetkileri süresi dolmuştur. {self.endpoint_name} için supervisor ile görüşünüz."
|
||||
# SystemLogs.create_log(
|
||||
# log_type="ERROR",
|
||||
# log_code="ACCESS_EXPIRED",
|
||||
# log_action=self.__tablename__,
|
||||
# log_message=message,
|
||||
# )
|
||||
# return False
|
||||
# except Exception as e:
|
||||
# SystemLogs.create_log(
|
||||
# log_type="ERROR",
|
||||
# log_code="ACCESS_EXPIRED",
|
||||
# log_action=self.__tablename__,
|
||||
# log_message=e,
|
||||
# )
|
||||
# return False
|
||||
#
|
||||
# access_dict = {
|
||||
# "LIST": self.access_read,
|
||||
# "INSERT": self.access_write,
|
||||
# "UPDATE": self.access_update,
|
||||
# "DELETE": self.access_delete,
|
||||
# "ACTIVE": self.access_update,
|
||||
# "PRINT": self.report_print,
|
||||
# "EXPORT": self.report_export,
|
||||
# }
|
||||
# return access_dict.get(endpoint_ext.upper(), False)
|
||||
|
||||
# official_address: Mapped[List["Address"]] = relationship(
|
||||
# "Address",
|
||||
# back_populates="official_companies",
|
||||
# foreign_keys=[official_address_id],
|
||||
# )
|
||||
#
|
||||
# emails: Mapped[List["UsersEmails"]] = relationship(
|
||||
# "UsersEmails", back_populates="companies", foreign_keys="UsersEmails.company_id"
|
||||
# )
|
||||
# phones: Mapped[List["UsersPhones"]] = relationship(
|
||||
# "UsersPhones", back_populates="company", foreign_keys="UsersPhones.company_id"
|
||||
# )
|
||||
# buildings: Mapped[List["Build"]] = relationship(
|
||||
# "Build",
|
||||
# back_populates="companies",
|
||||
# foreign_keys="Build.company_id",
|
||||
# )
|
||||
# response_buildings: Mapped[List["Build"]] = relationship(
|
||||
# "Build",
|
||||
# back_populates="response_companies",
|
||||
# foreign_keys="Build.response_company_id",
|
||||
# )
|
||||
# departments: Mapped[List["CompanyDepartments"]] = relationship(
|
||||
# "CompanyDepartments",
|
||||
# back_populates="company",
|
||||
# foreign_keys="CompanyDepartments.company_id",
|
||||
# )
|
||||
# budget_records: Mapped[List["CompanyBudgetRecords"]] = relationship(
|
||||
# "CompanyBudgetRecords",
|
||||
# back_populates="companies",
|
||||
# foreign_keys="CompanyBudgetRecords.company_id",
|
||||
# )
|
||||
# send_budget_records: Mapped[List["CompanyBudgetRecords"]] = relationship(
|
||||
# "CompanyBudgetRecords",
|
||||
# back_populates="send_companies",
|
||||
# foreign_keys="CompanyBudgetRecords.send_company_id",
|
||||
# )
|
||||
# decision_books: Mapped[List["BuildDecisionBook"]] = relationship(
|
||||
# "BuildDecisionBook",
|
||||
# back_populates="companies",
|
||||
# foreign_keys="BuildDecisionBook.resp_company_id",
|
||||
# )
|
||||
# decision_book_projects: Mapped[List["BuildDecisionBookProjects"]] = relationship(
|
||||
# "BuildDecisionBookProjects",
|
||||
# back_populates="companies",
|
||||
# foreign_keys="BuildDecisionBookProjects.resp_company_id",
|
||||
# )
|
||||
# decision_book_legal: Mapped["BuildDecisionBookLegal"] = relationship(
|
||||
# "BuildDecisionBookLegal",
|
||||
# back_populates="attorney_companies",
|
||||
# foreign_keys="BuildDecisionBookLegal.resp_attorney_company",
|
||||
# )
|
||||
#
|
||||
# company_account_books: Mapped["AccountBooks"] = relationship(
|
||||
# "AccountBooks",
|
||||
# back_populates="company",
|
||||
# foreign_keys="AccountBooks.company_id",
|
||||
# )
|
||||
# branch_account_books: Mapped["AccountBooks"] = relationship(
|
||||
# "AccountBooks",
|
||||
# back_populates="branch",
|
||||
# foreign_keys="AccountBooks.branch_id",
|
||||
# )
|
||||
# account_codes: Mapped["AccountCodes"] = relationship(
|
||||
# "AccountCodes", back_populates="company", foreign_keys="AccountCodes.company_id"
|
||||
# )
|
||||
# search_iban_description: Mapped["BuildIbanDescription"] = relationship(
|
||||
# "BuildIbanDescription",
|
||||
# back_populates="company",
|
||||
# foreign_keys="BuildIbanDescription.company_id",
|
||||
# )
|
||||
# related_companies: Mapped[List["CompanyRelationship"]] = relationship(
|
||||
# "CompanyRelationship",
|
||||
# back_populates="related_company",
|
||||
# foreign_keys="CompanyRelationship.related_company_id",
|
||||
# )
|
||||
206
databases/sql_models/company/department.py
Normal file
206
databases/sql_models/company/department.py
Normal file
@@ -0,0 +1,206 @@
|
||||
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"
|
||||
# )
|
||||
120
databases/sql_models/company/employee.py
Normal file
120
databases/sql_models/company/employee.py
Normal file
@@ -0,0 +1,120 @@
|
||||
from sqlalchemy import (
|
||||
String,
|
||||
ForeignKey,
|
||||
Index,
|
||||
Numeric,
|
||||
)
|
||||
from sqlalchemy.orm import mapped_column
|
||||
from databases.sql_models.core_mixin import CrudCollection
|
||||
|
||||
from validations import InsertCompanyEmployees
|
||||
|
||||
|
||||
class Staff(CrudCollection):
|
||||
|
||||
__tablename__ = "staff"
|
||||
__exclude__fields__ = []
|
||||
|
||||
staff_description = mapped_column(
|
||||
String, server_default="", comment="Staff Description"
|
||||
)
|
||||
staff_name = mapped_column(String, nullable=False, comment="Staff Name")
|
||||
staff_code = mapped_column(String, nullable=False, comment="Staff Code")
|
||||
|
||||
duties_id = mapped_column(ForeignKey("duties.id"), nullable=False)
|
||||
duties_uu_id = mapped_column(String, nullable=False, comment="Duty UUID")
|
||||
|
||||
# people: Mapped["People"] = relationship(
|
||||
# "People", back_populates="employees", foreign_keys=[people_id], uselist=True
|
||||
# )
|
||||
# duty: Mapped["CompanyDuty"] = relationship(
|
||||
# "CompanyDuty", back_populates="employees", foreign_keys=[duty_id]
|
||||
# )
|
||||
|
||||
@classmethod
|
||||
def create_action(cls, data: InsertCompanyEmployees):
|
||||
from database_sql_models import Duties
|
||||
|
||||
data_dict = data.model_dump()
|
||||
if duty := Duties.find_one(uu_id=data.duty_uu_id):
|
||||
data_dict["duty_id"] = duty.id
|
||||
# if person := People.find_one(uu_id=data.person_uu_id):
|
||||
# data_dict["people_id"] = person.id
|
||||
if data.start_date:
|
||||
data_dict["expiry_starts"] = data.start_date
|
||||
if data.stop_date:
|
||||
data_dict["expiry_ends"] = data.stop_date
|
||||
# del data_dict["duty_uu_id"], data_dict["person_uu_id"]
|
||||
del data_dict["start_date"], data_dict["stop_date"], data_dict["duty_uu_id"]
|
||||
return cls.find_or_create(**data_dict)
|
||||
|
||||
__table_args__ = ({"comment": "Staff Information"},)
|
||||
|
||||
|
||||
class Employees(CrudCollection):
|
||||
|
||||
__tablename__ = "employees"
|
||||
__exclude__fields__ = []
|
||||
|
||||
staff_id = mapped_column(ForeignKey("staff.id"))
|
||||
staff_uu_id = mapped_column(String, nullable=False, comment="Staff UUID")
|
||||
people_id = mapped_column(ForeignKey("people.id"), nullable=True)
|
||||
people_uu_id = mapped_column(String, nullable=True, comment="People UUID")
|
||||
|
||||
__table_args__ = (
|
||||
Index("employees_ndx_00", people_id, staff_id, unique=True),
|
||||
{"comment": "Employee Person Information"},
|
||||
)
|
||||
|
||||
|
||||
class EmployeeHistory(CrudCollection):
|
||||
|
||||
__tablename__ = "employee_history"
|
||||
__exclude__fields__ = []
|
||||
|
||||
staff_id = mapped_column(ForeignKey("staff.id"), nullable=False, comment="Staff ID")
|
||||
staff_uu_id = mapped_column(String, nullable=False, comment="Staff UUID")
|
||||
people_id = mapped_column(
|
||||
ForeignKey("people.id"), nullable=False, comment="People ID"
|
||||
)
|
||||
people_uu_id = mapped_column(String, nullable=False, comment="People UUID")
|
||||
|
||||
__table_args__ = (
|
||||
Index("_employee_history_ndx_00", people_id, staff_id),
|
||||
{"comment": "Employee History Information"},
|
||||
)
|
||||
|
||||
|
||||
class EmployeesSalaries(CrudCollection):
|
||||
|
||||
__tablename__ = "employee_salaries"
|
||||
__exclude__fields__ = []
|
||||
|
||||
gross_salary = mapped_column(Numeric(20, 6), nullable=False, comment="Gross Salary")
|
||||
net_salary = mapped_column(Numeric(20, 6), nullable=False, comment="Net Salary")
|
||||
|
||||
people_id = mapped_column(ForeignKey("people.id"), nullable=False)
|
||||
people_uu_id = mapped_column(String, nullable=False, comment="People UUID")
|
||||
|
||||
# people: Mapped["People"] = relationship(
|
||||
# "People", back_populates="employee_salaries", foreign_keys=[people_id]
|
||||
# )
|
||||
|
||||
__table_args__ = (
|
||||
Index("_employee_salaries_ndx_00", people_id, CrudCollection.expiry_starts),
|
||||
{"comment": "Employee Salaries Information"},
|
||||
)
|
||||
|
||||
|
||||
# class Events2Employees(CrudCollection):
|
||||
#
|
||||
# __tablename__ = "events2employees"
|
||||
# __exclude__fields__ = []
|
||||
#
|
||||
# event_id = mapped_column(ForeignKey("events.id"), nullable=False)
|
||||
# employees_id = mapped_column(ForeignKey("employees.id"), nullable=False)
|
||||
#
|
||||
# __table_args__ = (
|
||||
# Index("_events2employees_ndx_00", event_id, employees_id),
|
||||
# {"comment": "Events2Employees Information"},
|
||||
# )
|
||||
Reference in New Issue
Block a user