from typing import Self from django.db import models from django.core.validators import ( EmailValidator, MinLengthValidator, ) from django.utils.translation import gettext_lazy as _ from django.contrib.auth import get_user_model from core.utils.base_model import UUIDPrimaryKeyBaseModel from core.apps.banks.models import BankModel from core.apps.companies.validators.companies import ( iik_validator, iin_validator, name_validator, phone_validator, pinfl_validator, CompanyValidator, ) UserModel = get_user_model() class CompanyModel(UUIDPrimaryKeyBaseModel): name = models.CharField( _("name"), max_length=512, validators=[ name_validator, MinLengthValidator(3) ], null=False, blank=False ) pinfl_code = models.CharField( _("PINFL code"), validators=[ pinfl_validator, ], null=True, blank=True, ) iin_code = models.CharField( _("IIN code"), validators=[iin_validator], null=True, blank=True ) phone = models.CharField( _("Phone Number"), max_length=25, unique=True, validators=[ phone_validator, ], null=False, blank=False, ) email = models.CharField( _("Email Address"), max_length=255, validators=[ EmailValidator() ], unique=True, null=False, blank=False, ) iik_code = models.CharField( _("IIK code"), max_length=30, validators=[ iik_validator ], null=True, blank=True ) bank = models.ForeignKey( BankModel, on_delete=models.SET_NULL, null=True, blank=True ) legal_address = models.CharField( _("Legal Address"), max_length=512, validators=[ MinLengthValidator(10) ], null=True, blank=True ) real_address = models.CharField( _("Real Address"), max_length=512, validators=[ MinLengthValidator(10) ], null=True, blank=True ) logo = models.ImageField( _("Logo"), max_length=1024, null=True, blank=True ) signature_authority_document = models.FileField( _("Signature Authority Document"), max_length=1024, null=True, blank=True ) def __str__(self): return self.name @property def company_code(self) -> str: if self.pinfl_code is not None: return f"PINFL code: {self.pinfl_code}" else: return f"IIN code: {self.iin_code}" def clean(self): super().clean() validator = CompanyValidator(self) validator() @classmethod def _create_fake(cls) -> Self: return cls.objects.create( name="mock LLC", pinfl_code="12345678901234", iin_code="12345678901234", phone="+998901234567", email="mock@example.com", iik_code="UZS1234567890", legal_address="Some legal address, City, Country", real_address="Same as above", bank=BankModel._create_fake() # type: ignore ) class Meta: # type: ignore db_table = "companies" verbose_name = _("Company") verbose_name_plural = _("Companies") indexes = [ models.Index( fields=["phone"], name="companies_phone_inx" ), models.Index( fields=["email"], name="companies_email_inx" ), models.Index( fields=["iin_code"], name="companies_iin_code_inx" ), models.Index( fields=["pinfl_code"], name="companies_pinfl_code_inx" ), models.Index( fields=["name"], name="companies_name_inx" ), ]