import random
import string
from datetime import datetime, timedelta

from django.conf import settings
from django.contrib.auth.models import User, Group
from django.db import models
from django.utils import timezone
from jdatetime import time

from LiveStock.models import Cooperative, LiveStockRolseProduct
from authentication.models import (
    BaseModel,
    Address,
    UserProfile,
    SystemAddress,
    SystemUserProfile,
    BankCard,
    Province, ExternalTransaction
)
from authentication.models import UserMessage
from general_urls import base_user_gate_way_id
from notification.models import Notification
# from .KillHouse.views import KillHouseComplaintViewSet
from .ReportingPanel.models import SearchFields


class CheckState(BaseModel):
    poultry_request = models.ForeignKey(
        'PoultryRequest',
        on_delete=models.CASCADE,
        null=True,
        related_name="poultry_request"
    )
    notification = models.ForeignKey(
        Notification,
        on_delete=models.CASCADE,
        null=True,
        related_name="check_notification"
    )
    user_message = models.ForeignKey(
        UserMessage,
        on_delete=models.CASCADE,
        null=True,
        related_name="message_check"
    )
    city_operator_check = models.ForeignKey(
        'CityOperatorCheckRequest',
        on_delete=models.CASCADE,
        null=True,
        related_name="city_operator_check_state"
    )
    province_operator_check = models.ForeignKey(
        'ProvinceCheckOperatorRequest',
        on_delete=models.CASCADE,
        null=True,
        related_name='province_operator_check_state'
    )
    state = models.BooleanField(default=False)
    city_state = models.BooleanField(default=False)
    city_state_notification = models.BooleanField(default=False)
    city_state_modal = models.BooleanField(default=False)
    city_state_sms = models.BooleanField(default=False)
    province_state = models.BooleanField(default=False)
    province_state_notification = models.BooleanField(default=False)
    province_state_modal = models.BooleanField(default=False)
    province_state_sms = models.BooleanField(default=False)
    kill_house_state_notification = models.BooleanField(default=False)
    kill_house_state_modal = models.BooleanField(default=False)
    kill_house_state_sms = models.BooleanField(default=False)
    assignment_state_notification = models.BooleanField(default=False)
    assignment_state_modal = models.BooleanField(default=False)
    assignment_state_sms = models.BooleanField(default=False)
    main_check = models.CharField(max_length=50, null=True)


class Wallet(BaseModel):
    inventory = models.FloatField(default=0)
    state = models.CharField(max_length=120, default='active')
    province_name = models.CharField(max_length=50, null=True)
    id_number = models.IntegerField(default=1)

    def save(self, *args, **kwargs):
        super(Wallet, self).save(*args, **kwargs)


class OperatorLastTimeEnter(BaseModel):
    city_operator_last_time_check = models.DateTimeField(default=datetime.now())
    province_operator_last_time_check = models.DateTimeField(default=datetime.now())
    kill_house_last_time_check = models.DateTimeField(default=datetime.now())
    assignment_last_time_check = models.DateTimeField(default=datetime.now())
    operator = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        null=True,
        related_name="operator_last_time_check"
    )


# مدل مربوط به قیمت گذاری
class Pricing(BaseModel):
    # price = models.FloatField(null=True)
    operator = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        null=True,
        related_name="pricing_operator"
    )
    date = models.DateTimeField(null=True)
    live_chicken_price = models.FloatField(null=True)
    dead_chicken_price = models.FloatField(null=True)
    major_seller_price = models.FloatField(null=True)
    retail_seller_price = models.FloatField(null=True)
    floor_price = models.FloatField(null=True)
    ceiling_price = models.FloatField(null=True)
    role = models.CharField(max_length=20, null=True)
    lock = models.BooleanField(default=False)
    pricing_id_key = models.IntegerField(null=True)
    operator_id_foreign_key = models.IntegerField(null=True)

    # partial_seller_price = models.FloatField(null=True)

    def save(self, *args, **kwargs):
        super(Pricing, self).save(*args, **kwargs)


class ChainCompany(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="chain_company_user",
        null=True
    )

    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="chain_company_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="chain_company_wallet",
        null=True
    )
    name = models.CharField(max_length=200, null=True)
    city = models.CharField(max_length=200, null=True)
    province = models.CharField(max_length=200, null=True)
    postal_code = models.CharField(max_length=200, null=True)
    address = models.TextField(null=True)

    def save(self, *args, **kwargs):
        super(ChainCompany, self).save(*args, **kwargs)


class ChainCompanyTransaction(BaseModel):
    chain_company = models.ForeignKey(
        ChainCompany,
        on_delete=models.CASCADE,
        related_name="transaction_chain_company",
        null=True
    )
    amount = models.BigIntegerField(default=0)
    amount_with_tax = models.BigIntegerField(default=0)
    transaction = models.IntegerField(null=True)
    state = models.CharField(max_length=20, default='pending')
    union_share = models.BigIntegerField(default=0)
    company_share = models.BigIntegerField(default=0)
    guilds_share = models.BigIntegerField(default=0)
    city_share = models.BigIntegerField(default=0)
    wallet_share = models.BigIntegerField(default=0)
    other_share = models.BigIntegerField(default=0)
    pay_type = models.CharField(max_length=200, null=True)
    saleReferenceId = models.CharField(max_length=100, null=True)
    refId = models.CharField(max_length=100, null=True)
    orderId = models.CharField(max_length=100, null=True)
    cardHolderPan = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(ChainCompanyTransaction, self).save(*args, **kwargs)


# مدل مربوط به ثبت  مرغدار
class Poultry(BaseModel):
    owner = models.ForeignKey(
        'self',
        null=True,
        blank=True,
        related_name='poultry_owner',
        on_delete=models.CASCADE
    )
    owner_id_foreign_key = models.IntegerField(null=True)

    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="poultry_user",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)

    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="poultry_system_address",
        null=True
    )

    address_id_foreign_key = models.IntegerField(null=True)

    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="poultry_bank_info",
        null=True
    )

    has_chain_company = models.BooleanField(default=False)

    user_bank_id_foreign_key = models.IntegerField(null=True)
    city_operator = models.CharField(max_length=150, null=True)
    unit_name = models.CharField(max_length=100, null=True)
    password = models.CharField(max_length=100, null=True)
    gis_code = models.CharField(max_length=30, null=True)
    operating_licence_capacity = models.BigIntegerField(default=0)
    number_of_halls = models.IntegerField(default=0)
    tenant = models.BooleanField(default=False, null=True)
    has_tenant = models.BooleanField(default=False, null=True)
    person_type = models.CharField(max_length=10, null=True)
    economic_code = models.CharField(max_length=30, null=True)
    system_code = models.CharField(max_length=20, null=True)
    epidemiological_code = models.CharField(max_length=20, null=True)
    breeding_unique_id = models.CharField(max_length=20, null=True)
    total_capacity = models.BigIntegerField(default=0)
    licence_number = models.CharField(max_length=20, null=True)
    health_certificate_number = models.CharField(max_length=20, null=True)
    number_of_requests = models.BigIntegerField(default=0)
    hatching_date = models.DateTimeField(default=timezone.now())
    last_party_date = models.DateTimeField(default=timezone.now())
    number_of_incubators = models.BigIntegerField(default=0)
    herd_age_by_day = models.IntegerField(default=0)
    herd_age_by_week = models.IntegerField(default=0)
    number_of_party = models.IntegerField(default=0)
    communication_type = models.CharField(max_length=30, null=True)
    cooperative = models.CharField(max_length=50, null=True)
    date_of_register = models.DateTimeField(default=timezone.now())
    unit_status = models.CharField(max_length=20, null=True)
    active = models.BooleanField(default=True)
    identity_documents = models.JSONField(null=True)
    samasat_user_code = models.CharField(max_length=20, null=True)
    base_order = models.BigIntegerField(null=True)
    incubation_date = models.DateTimeField(null=True)
    wallet_amount = models.BigIntegerField(default=0)
    city = models.IntegerField(default=0)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="poultry_wallet",
        null=True
    )
    wallet_id_foreign_key = models.IntegerField(default=1)

    poultry_id_key = models.IntegerField(default=1)
    Lat = models.FloatField(null=True)
    Long = models.FloatField(null=True)
    date = models.DateTimeField(null=True)
    killing_ave_age = models.IntegerField(default=1)
    active_left_over = models.IntegerField(default=0)
    killing_ave_count = models.IntegerField(default=0)
    killing_ave_weight = models.IntegerField(default=0)
    killing_live_weight = models.IntegerField(default=0)
    killing_carcasses_weight = models.IntegerField(default=0)
    killing_loss_weight_percent = models.IntegerField(default=0)
    real_killing_ave_weight = models.FloatField(default=0)
    real_killing_live_weight = models.FloatField(default=0)
    real_killing_carcasses_weight = models.FloatField(default=0)
    real_killing_loss_weight_percent = models.FloatField(default=0)
    interest_license_id = models.CharField(max_length=100, null=True)
    order_limit = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(Poultry, self).save(*args, **kwargs)


# مدل مربوط به مستاجر  مرغدار
class PoultryTenant(BaseModel):
    poultry = models.ForeignKey(
        Poultry,
        on_delete=models.CASCADE,
        related_name="poultry_tenant",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="poultry_tenant_bank_info",
        null=True
    )
    fullname = models.CharField(max_length=150, null=True)
    first_name = models.CharField(max_length=200, null=True)
    last_name = models.CharField(max_length=200, null=True)
    natinal_id = models.CharField(max_length=10, null=True)
    mobile = models.CharField(max_length=11, null=True, default="")

    def save(self, *args, **kwargs):
        super(PoultryTenant, self).save(*args, **kwargs)


class PoultryAllowCityProvince(BaseModel):
    poultry = models.ForeignKey(
        Poultry,
        on_delete=models.CASCADE,
        related_name="poultry_allow",
        null=True
    )
    poultry_id_foreign_key = models.IntegerField(null=True)
    poultry_allow_city_province_id_key = models.IntegerField(null=True)
    city = models.BooleanField(default=False)
    province = models.BooleanField(default=False)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(PoultryAllowCityProvince, self).save(*args, **kwargs)


# مدل مربوط به ثبت جوجه ریزی مرغدار
class PoultryHatching(BaseModel):
    poultry = models.ForeignKey(
        Poultry,
        on_delete=models.CASCADE,
        related_name="poultry_hatching_user",
        null=True
    )
    chain_company = models.ForeignKey(
        ChainCompany,
        on_delete=models.CASCADE,
        related_name="poultry_hatching_chain_company",
        null=True
    )
    has_chain_company = models.BooleanField(default=False)
    poultry_id_foreign_key = models.IntegerField(null=True)
    poultry_hatching_id_key = models.IntegerField(null=True)
    quantity = models.BigIntegerField(default=0)
    losses = models.BigIntegerField(default=0)
    left_over = models.BigIntegerField(null=True)
    killed_quantity = models.BigIntegerField(default=0)
    extra_killed_quantity = models.BigIntegerField(default=0)
    governmental_killed_quantity = models.FloatField(default=0)
    governmental_quantity = models.FloatField(default=0)
    free_killed_quantity = models.FloatField(default=0)
    free_quantity = models.FloatField(default=0)
    chain_killed_quantity = models.FloatField(default=0)
    chain_killed_weight = models.FloatField(default=0)
    out_province_killed_weight = models.FloatField(default=0)
    out_province_killed_quantity = models.FloatField(default=0)
    export_killed_weight = models.FloatField(default=0)
    export_killed_quantity = models.FloatField(default=0)
    total_commitment = models.FloatField(default=0)
    commitment_type = models.CharField(max_length=20, default='govermental')
    total_commitment_quantity = models.FloatField(default=0)
    total_free_commitment_quantity = models.FloatField(default=0)
    total_free_commitment_weight = models.FloatField(default=0)
    total_killed_weight = models.FloatField(default=0)
    total_average_killed_weight = models.FloatField(default=0)
    request_left_over = models.BigIntegerField(default=0)
    hall = models.IntegerField(default=0)
    date = models.DateTimeField(null=True)
    predicate_date = models.DateTimeField(null=True)
    chicken_breed = models.CharField(max_length=200, null=True)
    period = models.BigIntegerField(default=0, null=True)
    allow_hatching = models.CharField(max_length=20, default='pending')
    state = models.CharField(max_length=50, default="pending")
    archive = models.BooleanField(default=False)
    violation = models.BooleanField(default=False)
    message = models.CharField(max_length=500, null=True)
    registrar = models.JSONField(null=True)
    breed = models.JSONField(null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    last_change = models.JSONField(null=True)
    chicken_age = models.IntegerField(default=1)
    now_age = models.IntegerField(default=1)
    latest_hatching_change = models.JSONField(null=True)
    violation_report = models.TextField(null=True)
    violation_message = models.TextField(null=True)
    violation_image = models.JSONField(null=True)
    violation_reporter = models.CharField(max_length=200, null=True)
    violation_report_date = models.DateTimeField(null=True)
    violation_report_editor = models.CharField(max_length=200, null=True)
    violation_report_edit_date = models.DateTimeField(null=True)
    total_losses = models.BigIntegerField(default=0)
    direct_losses = models.BigIntegerField(default=0)
    direct_losses_inputer = models.CharField(max_length=200, null=True)
    direct_losses_date = models.DateTimeField(null=True)
    direct_losses_editor = models.CharField(max_length=200, null=True)
    direct_losses_last_edit_date = models.DateTimeField(null=True)
    end_period_losses_inputer = models.CharField(max_length=200, null=True)
    end_period_losses_date = models.DateTimeField(null=True)
    end_period_losses_editor = models.CharField(max_length=200, null=True)
    end_period_losses_last_edit_date = models.DateTimeField(null=True)
    breeding_unique_id = models.CharField(max_length=20, null=True)
    licence_number = models.CharField(max_length=20, null=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)
    first_date_input_archive = models.DateTimeField(null=True)
    second_date_input_archive = models.DateTimeField(null=True)
    input_archiver = models.CharField(max_length=20, null=True)
    output_archive_date = models.DateTimeField(null=True)
    output_archiver = models.CharField(max_length=20, null=True)
    bar_difference_request_weight = models.FloatField(default=0)
    bar_difference_request_quantity = models.FloatField(default=0)
    total_disease_losses = models.BigIntegerField(default=0)  # مجموع تلفات ناشی از بیماری
    total_flock_destruction = models.BigIntegerField(default=0)  # مجموع معدوم‌سازی گله
    total_normal_flock_losses = models.BigIntegerField(default=0)  # مجموع تلفات عادی گله
    total_force_majeure_losses = models.BigIntegerField(default=0)  # مجموع تلفات ناشی از عوامل قهری و طبیعی
    total_fire_losses = models.BigIntegerField(default=0)  # مجموع تلفات ناشی از آتش‌سوزی
    health_certificate = models.CharField(max_length=100, null=True)
    samasat_discharge_percentage = models.IntegerField(default=0)
    PersonTypeName = models.CharField(max_length=200, null=True, blank=True)
    InteractTypeName = models.CharField(max_length=200, null=True, blank=True)
    UnionTypeName = models.CharField(max_length=200, null=True, blank=True)
    CertId = models.CharField(max_length=200, null=True, blank=True)
    increase_quantity = models.IntegerField(default=0)
    tenant_fullname = models.CharField(max_length=300, null=True, blank=True)
    tenant_national_code = models.CharField(max_length=50, null=True, blank=True)
    tenant_mobile = models.CharField(max_length=20, null=True, blank=True)
    tenant_city = models.CharField(max_length=200, null=True, blank=True)
    has_tenant = models.BooleanField(default=False)
    archive_date = models.DateTimeField(null=True)
    unknown = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        self.total_killed_weight = (
                (self.governmental_killed_quantity or 0)
                + (self.free_killed_quantity or 0)
                + (self.out_province_killed_weight or 0)
                + (self.chain_killed_weight or 0)
                + (self.export_killed_weight or 0)
                + (self.bar_difference_request_weight or 0)
        )

        managed_losses_total = 0
        management_model = globals().get('HatchingLossManagement')
        if management_model:
            management = management_model.get_active()
        else:
            management = None
        if management:
            for management_field, loss_field in management_model.LOSS_FIELD_MAPPING.items():
                if getattr(management, management_field, False):
                    managed_losses_total += getattr(self, loss_field, 0) or 0

        effective_losses = managed_losses_total if managed_losses_total else (self.losses or 0)
        self.total_losses = effective_losses + (self.direct_losses or 0)

        self.killed_quantity = (
                (self.governmental_quantity or 0)
                + (self.free_quantity or 0)
                + (self.out_province_killed_quantity or 0)
                + (self.chain_killed_quantity or 0)
                + (self.export_killed_quantity or 0)
                + (self.bar_difference_request_quantity or 0)
        )
        self.total_average_killed_weight = round(self.total_killed_weight / self.killed_quantity,
                                                 2) if self.killed_quantity > 0 else 0
        calculated_left_over = (
                (self.quantity or 0)
                + (self.increase_quantity or 0)
                - (self.killed_quantity or 0)
                - (self.total_losses or 0)
        )
        self.left_over = calculated_left_over if calculated_left_over > 0 else 0
        super(PoultryHatching, self).save(*args, **kwargs)


class EvacuationHatchingDetail(BaseModel):
    # گزارشات تلفات جوجه ریزی
    hatching = models.ForeignKey(
        PoultryHatching,
        on_delete=models.CASCADE,
        related_name="evacuation_details",
        null=True,
        blank=True
    )
    PartIdCode = models.CharField(max_length=200, null=True, blank=True)
    RequestId = models.CharField(max_length=200, null=True, blank=True)
    MoReportId = models.CharField(max_length=200, null=True, blank=True)
    ReportType = models.IntegerField(null=True, blank=True)
    ReportTypeString = models.CharField(max_length=255, null=True, blank=True)
    ReportDate = models.CharField(max_length=200, null=True, blank=True)
    ReportDateShamsi = models.CharField(max_length=200, null=True, blank=True)
    MoReason = models.TextField(null=True, blank=True)
    MoDate = models.CharField(max_length=200, null=True, blank=True)
    MoDateShamsi = models.CharField(max_length=200, null=True, blank=True)
    MoStartDay = models.IntegerField(null=True, blank=True)
    MoEndDay = models.IntegerField(null=True, blank=True)
    MoReportSubId = models.CharField(max_length=200, null=True, blank=True)
    ReportStatus = models.IntegerField(null=True, blank=True)
    GoodCount = models.IntegerField(null=True, blank=True)
    Message = models.TextField(null=True, blank=True)
    ErrorCode = models.IntegerField(null=True, blank=True)
    IsDeleted = models.BooleanField(default=False)
    RegDate = models.CharField(max_length=200, null=True, blank=True)
    RegDateShamsi = models.CharField(max_length=200, null=True, blank=True)
    RegDateShamsiWithTime = models.CharField(max_length=200, null=True, blank=True)
    RegDateShamsiOnlyTime = models.CharField(max_length=200, null=True, blank=True)
    ExternalId = models.CharField(max_length=200, null=True, blank=True)
    StringId = models.CharField(max_length=200, null=True, blank=True)
    IsPersisted = models.BooleanField(default=False)
    AllowInsert = models.BooleanField(default=False)
    AllowUpdate = models.BooleanField(default=False)
    ModalCss = models.CharField(max_length=255, null=True, blank=True)
    GridContainerParametersModel = models.CharField(max_length=255, null=True, blank=True)
    MenuUserAccess = models.CharField(max_length=255, null=True, blank=True)
    MenuUserAccessId = models.IntegerField(null=True, blank=True)
    LogTableName = models.CharField(max_length=255, null=True, blank=True)
    LogTableAlias = models.CharField(max_length=255, null=True, blank=True)
    PageTitle = models.CharField(max_length=255, null=True, blank=True)

    def save(self, *args, **kwargs):
        super(EvacuationHatchingDetail, self).save(*args, **kwargs)


class HatchingLossManagement(BaseModel):
    include_total_disease_losses = models.BooleanField(default=False)
    include_total_flock_destruction = models.BooleanField(default=False)
    include_total_normal_flock_losses = models.BooleanField(default=False)
    include_total_force_majeure_losses = models.BooleanField(default=False)
    include_total_fire_losses = models.BooleanField(default=False)

    LOSS_FIELD_MAPPING = {
        'include_total_disease_losses': 'total_disease_losses',
        'include_total_flock_destruction': 'total_flock_destruction',
        'include_total_normal_flock_losses': 'total_normal_flock_losses',
        'include_total_force_majeure_losses': 'total_force_majeure_losses',
        'include_total_fire_losses': 'total_fire_losses',
    }

    @classmethod
    def get_active(cls):
        return cls.objects.filter(trash=False).order_by('-modify_date').first()

    def as_dict(self):
        return {
            field: getattr(self, field, False)
            for field in self.LOSS_FIELD_MAPPING.keys()
        }


# مدل ثبت اپراتور شهرستان
class CityOperator(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="city_operator_user",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    city_operator_id_key = models.IntegerField(null=True)
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="city_system_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="city_operator_bank_info",
        null=True
    )
    wallet_amount = models.BigIntegerField(default=0)
    identity_documents = models.JSONField(null=True)
    phone = models.CharField(max_length=20, null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="city_wallet",
        null=True
    )
    unit_name = models.CharField(max_length=50, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(CityOperator, self).save(*args, **kwargs)


class OutProvincePoultryRequestBuyer(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="out_province_poultry_user",
        null=True
    )

    type = models.CharField(max_length=100, null=True)
    fullname = models.CharField(max_length=300, null=True)
    mobile = models.CharField(max_length=11, null=True)
    unit_name = models.CharField(max_length=300, null=True)
    city = models.CharField(max_length=300, null=True)
    province = models.CharField(max_length=300, null=True)
    kill_house_unique_id = models.CharField(max_length=100, null=True)
    active = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(OutProvincePoultryRequestBuyer, self).save(*args, **kwargs)


class Vet(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        null=True,
        related_name="user_profile_vet"
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="vet_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="vet_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="vet_wallet",
        null=True
    )

    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    vet_id_key = models.IntegerField(null=True)

    wallet_amount = models.BigIntegerField(default=0)
    identity_documents = models.JSONField(null=True)
    active = models.BooleanField(default=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(Vet, self).save(*args, **kwargs)


# مدل مربوط به ثبت درخواست مرغدار
class PoultryRequest(BaseModel):
    user = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        related_name="poultry_request",
        null=True
    )
    city_operator = models.ForeignKey(
        CityOperator,
        on_delete=models.CASCADE,
        related_name="poultry_request_city_operator",
        null=True
    )
    poultry = models.ForeignKey(
        Poultry,
        on_delete=models.CASCADE,
        related_name="poultry_request_poultry",
        null=True
    )
    hatching = models.ForeignKey(
        PoultryHatching,
        on_delete=models.CASCADE,
        related_name="poultry_request_hatching",
        null=True
    )
    out_province_poultry_request_buyer = models.ForeignKey(
        OutProvincePoultryRequestBuyer,
        on_delete=models.CASCADE,
        related_name="poultry_request_hatching",
        null=True
    )
    vet_farm = models.ForeignKey(
        Vet,
        on_delete=models.CASCADE,
        related_name="vet_poultry_request",
        null=True
    )
    city_operator_id_foreign_key = models.IntegerField(null=True)
    poultry_id_foreign_key = models.IntegerField(null=True)
    poultry_hatching_id_foreign_key = models.IntegerField(null=True)
    poultry_request_id_key = models.IntegerField(null=True)
    quantity = models.BigIntegerField(default=0)
    previous_quantity = models.BigIntegerField(default=0)
    remain_quantity = models.BigIntegerField(default=0)
    first_quantity = models.BigIntegerField(default=0)
    losses = models.BigIntegerField(default=0)
    send_date = models.DateTimeField(null=True)
    chicken_breed = models.CharField(max_length=200, null=True)
    financial_operation = models.CharField(max_length=100, null=True)
    Index_weight = models.FloatField(null=True)
    amount = models.FloatField(default=0)
    state = models.JSONField(default=dict, null=True)
    state_process = models.CharField(max_length=50, default="pending")
    province_state = models.CharField(max_length=50, default="pending")
    order_code = models.BigIntegerField(null=True)
    general_order_code = models.BigIntegerField(null=True)
    message = models.CharField(max_length=500, null=True)
    inspector = models.CharField(max_length=30, null=True)
    union = models.BooleanField(default=False)
    awaiting_payment = models.BooleanField(default=False)
    assignment = models.BooleanField(default=False)
    auction = models.BooleanField(default=False)
    fee = models.FloatField(default=0)
    final_state = models.CharField(max_length=50, default="pending")
    cell_type = models.JSONField(default=dict, null=True)
    kill_house_list = models.JSONField(null=True)
    registrar = models.JSONField(null=True)
    buyer = models.JSONField(null=True)
    out = models.BooleanField(default=False)
    export = models.BooleanField(default=False)
    free_sale_in_province = models.BooleanField(default=False)
    vet = models.BooleanField(default=False)
    archive = models.BooleanField(default=False)
    direct_buying = models.BooleanField(default=False)
    direct_buying_kill_place = models.CharField(max_length=100, null=True)
    direct_buying_buyer_info = models.JSONField(null=True)
    selling_without_city = models.BooleanField(default=False)
    # clearance_code = models.BigIntegerField(null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    quarantine_code = models.CharField(max_length=200, null=True)
    quarantine_quantity = models.IntegerField(default=0)
    quarantine_code_registrar = models.JSONField(null=True)

    out_province_driver_info = models.JSONField(null=True)
    out_province_request_canceller = models.JSONField(null=True)
    out_province_request_cancel = models.BooleanField(default=False)
    freezing = models.BooleanField(default=False)
    approved_price = models.BooleanField(default=False)
    wage = models.FloatField(default=0)
    total_wage_amount = models.BigIntegerField(default=0)
    union_share = models.BigIntegerField(default=0)
    union_share_percent = models.FloatField(default=0)
    company_share = models.BigIntegerField(default=0)
    company_share_percent = models.FloatField(default=0)
    guilds_share = models.BigIntegerField(default=0)
    guilds_share_percent = models.FloatField(default=0)
    city_share = models.BigIntegerField(default=0)
    city_share_percent = models.FloatField(default=0)
    wallet_share = models.BigIntegerField(default=0)
    wallet_share_percent = models.FloatField(default=0)
    other_share = models.BigIntegerField(default=0)
    other_share_percent = models.FloatField(default=0)
    archive_wage = models.BooleanField(default=False)
    wage_pay = models.BooleanField(default=False)
    has_wage = models.BooleanField(default=False)
    sms = models.BooleanField(default=False)
    payer_type = models.CharField(max_length=20, null=True)
    buyer_mobile = models.CharField(max_length=11, null=True)
    buyer_fullname = models.CharField(max_length=200, null=True)
    buyer_city = models.CharField(max_length=200, null=True)
    buyer_province = models.CharField(max_length=200, null=True)
    payer_fullname = models.CharField(max_length=200, null=True)
    agent = models.JSONField(null=True)
    hatching_left_over = models.IntegerField(default=0)
    killing_age = models.IntegerField(default=1)
    killer_kill_house_unit_name = models.CharField(max_length=300, null=True)
    killer_kill_house_city = models.CharField(max_length=300, null=True)
    killer_kill_house_province = models.CharField(max_length=300, null=True)
    kill_house_unique_id = models.CharField(max_length=100, null=True)
    payment_link = models.CharField(max_length=300, null=True)
    interface_number = models.CharField(max_length=11, null=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)
    price_confirmation = models.BooleanField(default=False)
    price_confirmation_code = models.CharField(max_length=10, null=True)
    input_price_confirmation_code = models.CharField(max_length=10, null=True)
    fine = models.BooleanField(default=False)
    fine_amount = models.BigIntegerField(default=0)
    fine_coefficient = models.FloatField(default=0)
    market = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(PoultryRequest, self).save(*args, **kwargs)


class PoultryRequestQuarantineCode(BaseModel):
    poultry_request = models.ForeignKey(
        PoultryRequest,
        on_delete=models.CASCADE,
        related_name="poultry_request_code",
        null=True
    )
    quarantine_code = models.CharField(max_length=200, null=True)
    traffic_code = models.CharField(max_length=200, null=True)
    registrar = models.CharField(max_length=200, null=True)
    register_date = models.DateTimeField(null=True)
    quarantine_quantity = models.IntegerField(default=0)
    system_quarantine_quantity = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(PoultryRequestQuarantineCode, self).save(*args, **kwargs)


# مدل مربوط به ثبت درخواست مرغدار
class TotalPoultryRequestQuantity(BaseModel):
    quantity = models.BigIntegerField(default=0)
    accepted_quantity = models.BigIntegerField(default=0)
    returned_quantity = models.BigIntegerField(default=0)
    first_quantity = models.BigIntegerField(default=0)
    city_accepted_quantity = models.BigIntegerField(default=0)
    allocated_quantity = models.BigIntegerField(default=0)
    direct_buying_quantity = models.BigIntegerField(default=0)
    number_of_poultry = models.BigIntegerField(default=0)
    rejected_quantity = models.BigIntegerField(default=0)
    waiting_quantity = models.BigIntegerField(default=0)
    number_of_kill_house = models.BigIntegerField(default=0)
    input_quantity = models.BigIntegerField(default=0)
    assignableQuantity = models.BigIntegerField(default=0)
    kill_requests_quantity = models.BigIntegerField(default=0)
    date = models.DateTimeField(default=datetime.now())
    age_up_45 = models.BigIntegerField(default=0, null=True)
    total_poultry_request_quantity_id_key = models.IntegerField(null=True)

    def save(self, *args, **kwargs):
        super(TotalPoultryRequestQuantity, self).save(*args, **kwargs)


# مدل مربوط به ثبت درخواست مزایده مرغدار
class PoultryRequestAuction(BaseModel):
    poultry_request = models.ForeignKey(
        PoultryRequest,
        on_delete=models.CASCADE,
        related_name="poultry_request_auction",
        null=True)
    pricing = models.ForeignKey(
        Pricing,
        on_delete=models.CASCADE,
        related_name="poultry_request_auction_pricing",
        null=True
    )
    fee = models.FloatField(default=0, null=True)
    hour = models.CharField(max_length=10, null=True)
    auction_date = models.DateTimeField(null=True)
    state = models.CharField(max_length=10, default='inactive')

    def save(self, *args, **kwargs):
        super(PoultryRequestAuction, self).save(*args, **kwargs)


# مدل مربوط به ثبت درخواست بورسی مرغدار
class PoultryRequestExchange(BaseModel):
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, related_name="poultry_request_exchange", null=True)
    quantity = models.BigIntegerField(default=0, null=True)
    send_date = models.DateTimeField(null=True)
    hatching_date = models.DateTimeField(null=True)
    chicken_breed = models.CharField(max_length=20, null=True)
    Index_weight = models.FloatField(null=True)
    fee = models.FloatField(null=True)
    order_code = models.IntegerField(null=True)
    longitude = models.FloatField(default=0)
    latitude = models.FloatField(default=0)
    address = models.TextField()
    age = models.IntegerField(default=0, null=True)
    inspector = models.CharField(max_length=30, null=True)
    state = models.CharField(max_length=15, default='pending')

    def save(self, *args, **kwargs):
        super(PoultryRequestExchange, self).save(*args, **kwargs)


# مدل ثبت اپراتور کشتارگاه
class KillHouseOperator(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="kill_house_operator_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="kill_house_system_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="kill_house_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="kill_house_wallet",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    kill_house_operator_id_key = models.IntegerField(null=True)
    wallet_amount = models.BigIntegerField(default=0)
    killer = models.BooleanField(default=False)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(KillHouseOperator, self).save(*args, **kwargs)


class Car(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="user_car",
        null=True
    )
    driver_name = models.CharField(max_length=500, null=True)
    driver_mobile = models.CharField(max_length=11, null=True)
    type_car = models.CharField(max_length=50, null=True)
    type = models.CharField(max_length=50, null=True)
    transport_type = models.CharField(max_length=50, null=True)
    pelak = models.CharField(max_length=200, null=True)
    capocity = models.CharField(max_length=50, null=True)
    weight_without_load = models.CharField(max_length=50, null=True)
    health_code = models.CharField(max_length=100, null=True)
    status = models.CharField(max_length=50, default="inactive")
    active = models.BooleanField(default=True)
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="car_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="car_wallet",
        null=True
    )
    wallet_amount = models.BigIntegerField(default=0)
    identity_documents = models.JSONField(null=True)
    phone = models.CharField(max_length=20, null=True)
    registrar = models.JSONField(null=True)
    active_state = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(Car, self).save(*args, **kwargs)


# مدل مربوط به ثبت کشتارگاه
class KillHouse(BaseModel):
    user = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        related_name="user_killhouse",
        null=True)
    userprofile = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        null=True,
        related_name="system_user_kill_house"
    )
    address = models.ForeignKey(
        Address,
        on_delete=models.CASCADE,
        related_name="killhouse_address",
        null=True)
    system_address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="killhouse_system_address",
        null=True)
    kill_house_operator = models.ForeignKey(
        KillHouseOperator,
        on_delete=models.CASCADE,
        related_name="system_kill_house_operator",
        null=True)

    cars = models.ManyToManyField(
        Car,
        related_name='kill_house_car',
        null=True, blank=True)

    system_address_id_foreign_key = models.IntegerField(null=True)
    kill_house_operator_id_foreign_key = models.IntegerField(null=True)
    kill_house_id_key = models.IntegerField(null=True)
    shift_work_from = models.CharField(max_length=50, null=True)
    shift_work_to = models.CharField(max_length=50, null=True)
    name = models.CharField(max_length=50, null=True)
    car = models.JSONField(default=dict, null=True)
    capacity = models.FloatField(default=0)
    killing_race = models.CharField(max_length=20, null=True)
    phone = models.CharField(max_length=20, null=True)
    alternate_number = models.CharField(max_length=20, null=True)
    killer = models.BooleanField(default=False)
    wallet_amount = models.BigIntegerField(default=0)
    identity_documents = models.JSONField(null=True)
    active = models.BooleanField(default=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    type = models.CharField(max_length=100, default='public')
    out_province = models.BooleanField(default=False)
    has_pos = models.BooleanField(default=False)
    show_exclusive = models.BooleanField(default=True)
    company_gateway_percent = models.IntegerField(default=0)
    union_gateway_percent = models.IntegerField(default=0)
    guilds_gateway_percent = models.IntegerField(default=0)
    other_gateway_percent = models.IntegerField(default=0)
    total_kill_capacity = models.FloatField(default=0)
    total_kill_capacity_percent = models.FloatField(default=0)
    extra_bar_kill_percent = models.FloatField(default=0)
    max_kill_limit = models.BooleanField(default=False)
    off = models.BigIntegerField(default=0)
    unique_identifier = models.CharField(max_length=20, null=True)
    share_debt_counting_wage = models.BooleanField(default=False)
    out_province_selling_limitation = models.BooleanField(default=False)
    out_province_selling_limitation_percent = models.IntegerField(default=0)
    in_province_selling_limitation = models.BooleanField(default=False)
    in_province_selling_limitation_percent = models.IntegerField(default=0)
    market_buying = models.BooleanField(default=False)
    market_buying_limitation = models.BooleanField(default=False)
    market_buying_capacity_percent_status = models.BooleanField(default=False)
    market_capacity_percent = models.FloatField(default=0)
    market_light_capacity = models.BooleanField(default=False)
    market_light_capacity_percent = models.FloatField(default=0)
    poultry_payment_limitation = models.BooleanField(default=False)
    quota = models.BooleanField(default=False)
    quota_max_kill_limit = models.BooleanField(default=False)
    quota_request = models.BooleanField(default=False)
    quota_custom = models.BooleanField(default=False)
    free_quota = models.FloatField(default=0)
    governmental_quota = models.FloatField(default=0)
    quota_custom_quantity = models.FloatField(default=0)
    out_province_governmental_selling_percent = models.FloatField(default=0)
    out_province_free_selling_percent = models.FloatField(default=0)
    out_province_free_buying_commitment_percent = models.FloatField(default=0)
    in_province_governmental_selling_percent = models.FloatField(default=0)
    in_province_free_selling_percent = models.FloatField(default=0)
    segmentation_governmental_percent = models.FloatField(default=0)
    segmentation_free_selling_percent = models.FloatField(default=0)
    cold_house_governmental_percent = models.FloatField(default=0)
    cold_house_free_percent = models.FloatField(default=0)
    governmental_selling_permission = models.BooleanField(default=False)
    free_selling_permission = models.BooleanField(default=False)
    free_sale_form_governmental_quota = models.BooleanField(default=False)
    free_sale_from_free_quota_in_province = models.BooleanField(default=False)
    total_input_warehouse_weight = models.FloatField(default=0)
    total_input_warehouse_governmental_weight = models.FloatField(default=0)
    total_input_warehouse_free_weight = models.FloatField(default=0)
    total_selling_warehouse_governmental_weight = models.FloatField(default=0)
    total_selling_warehouse_free_weight = models.FloatField(default=0)
    total_remain_warehouse_governmental_weight = models.FloatField(default=0)
    total_remain_warehouse_free_weight = models.FloatField(default=0)
    total_remain_warehouse_weight = models.FloatField(default=0)
    total_selling_in_province_governmental_weight = models.FloatField(default=0)
    total_selling_in_province_free_weight = models.FloatField(default=0)
    total_selling_out_province_governmental_weight = models.FloatField(default=0)
    total_selling_out_province_free_weight = models.FloatField(default=0)
    total_segmentation_governmental_weight = models.FloatField(default=0)
    total_segmentation_free_weight = models.FloatField(default=0)
    total_in_province_governmental_bars_weight = models.FloatField(default=0)
    total_in_province_free_bars_weight = models.FloatField(default=0)
    total_out_province_buying_bars_weight = models.FloatField(default=0)
    pos_allocated_weight = models.FloatField(default=0)
    pos_governmental_allocated_weight = models.FloatField(default=0)
    pos_free_allocated_weight = models.FloatField(default=0)
    total_selling_warehouse_weight = models.FloatField(default=0)
    total_segmentation_weight = models.FloatField(default=0)
    total_cold_house_weight = models.FloatField(default=0)
    total_cold_house_governmental_weight = models.FloatField(default=0)
    total_cold_house_free_weight = models.FloatField(default=0)
    ware_house_archive_weight = models.FloatField(default=0)
    ware_house_archive_governmental_weight = models.FloatField(default=0)
    ware_house_archive_free_weight = models.FloatField(default=0)
    total_commitment_governmental_weight = models.FloatField(default=0)
    total_commitment_free_weight = models.FloatField(default=0)
    total_in_province_governmental_bars_commitment_weight = models.FloatField(default=0)
    total_in_province_free_bars_commitment_weight = models.FloatField(default=0)
    total_out_province_buying_bars_commitment_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_governmental_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_free_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_governmental_remain_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_free_remain_weight = models.FloatField(default=0)
    total_commitment_selling_out_province_governmental_weight = models.FloatField(default=0)
    total_commitment_selling_out_province_free_weight = models.FloatField(default=0)
    total_commitment_cold_house_governmental_weight = models.FloatField(default=0)
    total_commitment_cold_house_free_weight = models.FloatField(default=0)
    total_commitment_segmentation_governmental_weight = models.FloatField(default=0)
    total_commitment_segmentation_free_weight = models.FloatField(default=0)
    maximum_load_volume_increase = models.FloatField(default=0)
    maximum_load_volume_reduction = models.FloatField(default=0)
    ware_house_remaining_percent_limitation = models.FloatField(default=0)
    ware_house_remaining_percent_limitation_status = models.BooleanField(default=False)
    ware_house_remaining_weight_limitation = models.FloatField(default=0)
    ware_house_remaining_weight_limitation_status = models.BooleanField(default=False)
    ware_house_remaining_weight_archive_percent = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        self.total_segmentation_weight = self.total_segmentation_governmental_weight + self.total_segmentation_free_weight
        self.total_cold_house_weight = self.total_cold_house_governmental_weight + self.total_cold_house_free_weight
        self.total_input_warehouse_governmental_weight = self.total_in_province_governmental_bars_weight
        self.total_input_warehouse_free_weight = self.total_in_province_free_bars_weight + self.total_out_province_buying_bars_weight
        self.total_input_warehouse_weight = self.total_input_warehouse_governmental_weight + self.total_input_warehouse_free_weight
        self.total_selling_warehouse_governmental_weight = self.total_selling_in_province_governmental_weight + self.total_selling_out_province_governmental_weight + self.total_segmentation_governmental_weight + self.total_cold_house_governmental_weight + self.pos_governmental_allocated_weight
        self.total_selling_warehouse_free_weight = self.total_selling_in_province_free_weight + self.total_selling_out_province_free_weight + self.total_segmentation_free_weight + self.total_cold_house_free_weight + self.pos_free_allocated_weight
        self.total_selling_warehouse_weight = self.total_selling_warehouse_governmental_weight + self.total_selling_warehouse_free_weight
        self.total_remain_warehouse_governmental_weight = (
                                                                  self.total_input_warehouse_governmental_weight - self.total_selling_warehouse_governmental_weight) - self.ware_house_archive_governmental_weight
        self.total_remain_warehouse_free_weight = (
                                                          self.total_input_warehouse_free_weight - self.total_selling_warehouse_free_weight) - self.ware_house_archive_free_weight
        self.total_remain_warehouse_weight = self.total_input_warehouse_weight - self.total_selling_warehouse_weight
        # self.total_commitment_governmental_weight = int((self.in_province_governmental_selling_percent/100) * self.total_input_warehouse_governmental_weight) if self.governmental_selling_permission else 0
        self.total_commitment_selling_in_province_governmental_weight = self.total_in_province_governmental_bars_commitment_weight
        self.total_commitment_selling_out_province_governmental_weight = int((
                                                                                     self.out_province_governmental_selling_percent / 100) * self.total_input_warehouse_governmental_weight) if self.governmental_selling_permission else 0
        self.total_commitment_cold_house_governmental_weight = int((
                                                                           self.cold_house_governmental_percent / 100) * self.total_input_warehouse_governmental_weight) if self.governmental_selling_permission else 0
        self.total_commitment_segmentation_governmental_weight = int((
                                                                             self.segmentation_governmental_percent / 100) * self.total_input_warehouse_governmental_weight) if self.governmental_selling_permission else 0
        # self.total_commitment_free_weight = int((self.in_province_governmental_selling_percent/100) * self.total_input_warehouse_governmental_weight) if self.governmental_selling_permission else 0

        self.total_commitment_selling_in_province_free_weight = self.total_in_province_free_bars_commitment_weight + self.total_out_province_buying_bars_commitment_weight
        self.total_commitment_selling_out_province_free_weight = int((
                                                                             self.out_province_free_selling_percent / 100) * self.total_input_warehouse_free_weight) if self.free_selling_permission else 0
        self.total_commitment_cold_house_free_weight = int((
                                                                   self.cold_house_free_percent / 100) * self.total_input_warehouse_free_weight) if self.free_selling_permission else 0
        self.total_commitment_segmentation_free_weight = int((
                                                                     self.segmentation_free_selling_percent / 100) * self.total_input_warehouse_free_weight) if self.free_selling_permission else 0
        self.total_commitment_selling_in_province_governmental_remain_weight = self.total_commitment_selling_in_province_governmental_weight - self.total_selling_in_province_governmental_weight if (
                                                                                                                                                                                                             self.total_commitment_selling_in_province_governmental_weight - self.total_selling_in_province_governmental_weight) > 0 else 0
        self.total_commitment_selling_in_province_free_remain_weight = self.total_commitment_selling_in_province_free_weight - self.total_selling_in_province_free_weight if (
                                                                                                                                                                                     self.total_commitment_selling_in_province_free_weight - self.total_selling_in_province_free_weight) > 0 else 0

        super(KillHouse, self).save(*args, **kwargs)


class ChainAllocation(BaseModel):
    poultry_hatching = models.ForeignKey(
        PoultryHatching,
        on_delete=models.CASCADE,
        related_name="poultry_hatching_chain_allocation",
        null=True
    )
    chain_company = models.ForeignKey(
        ChainCompany,
        on_delete=models.CASCADE,
        related_name="chain_company_allocation",
        null=True
    )
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="chain_company_allocation_kill_house",
        null=True
    )
    date = models.DateTimeField(null=True)
    state = models.CharField(max_length=50, default="pending")
    vet_farm_state = models.CharField(max_length=50, default="pending")
    quantity = models.BigIntegerField(default=0)
    weight = models.FloatField(default=0)
    index_weight = models.FloatField(default=0)
    quarantine_code = models.CharField(max_length=200, null=True)
    message = models.TextField(null=True)
    remover = models.JSONField(null=True)
    seconder = models.JSONField(null=True)
    registerer = models.JSONField(null=True)
    quarantine_code_registrar = models.JSONField(null=True)
    wage = models.FloatField(default=0)
    total_wage_amount = models.BigIntegerField(default=0)
    union_share = models.BigIntegerField(default=0)
    union_share_percent = models.FloatField(default=0)
    company_share = models.BigIntegerField(default=0)
    company_share_percent = models.FloatField(default=0)
    guilds_share = models.BigIntegerField(default=0)
    guilds_share_percent = models.FloatField(default=0)
    city_share = models.BigIntegerField(default=0)
    city_share_percent = models.FloatField(default=0)
    wallet_share = models.BigIntegerField(default=0)
    wallet_share_percent = models.FloatField(default=0)
    other_share = models.BigIntegerField(default=0)
    other_share_percent = models.FloatField(default=0)
    driver_name = models.CharField(max_length=100, null=True)
    driver_mobile = models.CharField(max_length=11, null=True)
    type_car = models.CharField(max_length=100, null=True)
    pelak = models.CharField(max_length=100, null=True)
    health_code = models.CharField(max_length=100, null=True)
    buyer_name = models.CharField(max_length=100, null=True)
    buyer_mobile = models.CharField(max_length=11, null=True)
    province = models.CharField(max_length=50, null=True)
    city = models.CharField(max_length=50, null=True)
    company_name = models.CharField(max_length=100, null=True)
    company_user_fullname = models.CharField(max_length=100, null=True)
    company_user_mobile = models.CharField(max_length=11, null=True)
    company_user_province = models.CharField(max_length=50, null=True)
    company_user_city = models.CharField(max_length=50, null=True)
    out_province = models.BooleanField(default=False)
    archive_wage = models.BooleanField(default=False)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(ChainAllocation, self).save(*args, **kwargs)


class KillHouseWareHouse(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="ware_house_kill_house",
        null=True)
    bar_quantity = models.BigIntegerField(default=0)
    allocated_quantity = models.BigIntegerField(default=0)
    bar_live_weight = models.FloatField(default=0)
    number_of_carcasses = models.BigIntegerField(default=0)
    updated_number_of_carcasses = models.BigIntegerField(default=0)
    weight_of_carcasses = models.FloatField(default=0)
    updated_weight_of_carcasses = models.FloatField(default=0)
    ave_weight_of_carcasses = models.FloatField(default=0)
    update_ave_weight_of_carcasses = models.FloatField(default=0)
    free_bar_quantity = models.BigIntegerField(default=0)
    number_of_free_carcasses = models.BigIntegerField(default=0)
    free_weight_of_carcasses = models.FloatField(default=0)
    total_bar_quantity = models.BigIntegerField(default=0)
    total_number_of_carcasses = models.BigIntegerField(default=0)
    update_total_number_of_carcasses = models.BigIntegerField(default=0)
    final_total_number_of_carcasses = models.BigIntegerField(default=0)
    total_weight_of_carcasses = models.FloatField(default=0)
    update_total_weight_of_carcasses = models.FloatField(default=0)
    final_total_weight_of_carcasses = models.FloatField(default=0)
    allocated_total_number_of_carcasses = models.BigIntegerField(default=0)
    allocated_total_weight_of_carcasses = models.FloatField(default=0)
    remain_total_number_of_carcasses = models.BigIntegerField(default=0)
    remain_total_weight_of_carcasses = models.FloatField(default=0)
    pre_cold_number_of_carcasses_self_ware_house = models.BigIntegerField(default=0)
    pre_cold_weight_of_carcasses_self_ware_house = models.FloatField(default=0)
    pre_cold_number_of_carcasses_to_ware_house = models.BigIntegerField(default=0)
    pre_cold_weight_of_carcasses_to_ware_house = models.FloatField(default=0)
    freezing_quantity = models.BigIntegerField(default=0)
    freezing_weight = models.FloatField(default=0)
    free_sale_quantity = models.BigIntegerField(default=0)
    free_sale_weight = models.FloatField(default=0)
    final_registration = models.BooleanField(default=False)

    date = models.DateTimeField(null=True)

    def save(self, *args, **kwargs):
        super(KillHouseWareHouse, self).save(*args, **kwargs)


# مدل مربوط به ثبت کشتارگاه
class KillHouseAllowVet(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_allow",
        null=True)
    kill_house_id_foreign_key = models.IntegerField(null=True)
    kill_house_allow_vet_id_key = models.IntegerField(null=True)

    allow = models.BooleanField(default=False)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(KillHouseAllowVet, self).save(*args, **kwargs)


# مدل مربوط به ثبت کشتارگاه
class KillHousePercentage(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="percent_killhouse",
        null=True)
    kill_house_for_killer = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="killer_kill_house",
        null=True)

    kill_house_for_killer_id_foreign_key = models.IntegerField(null=True)
    kill_house_id_foreign_key = models.IntegerField(null=True)
    kill_house_percentage_id_key = models.IntegerField(null=True)
    percent = models.FloatField(default=0)
    last_guilds_update_date = models.CharField(max_length=100, null=True)
    guilds_quantity = models.IntegerField(default=0)
    guilds_weight = models.FloatField(default=0)
    quantity = models.BigIntegerField(default=0)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    change_data = models.JSONField(null=True)

    def save(self, *args, **kwargs):
        super(KillHousePercentage, self).save(*args, **kwargs)


# مدل مربوط به اضافه کردن راننده به کشتاگاه
class KillHouseDriver(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="user_driver",
        null=True
    )
    driver_name = models.CharField(max_length=500, null=True)
    driver_mobile = models.CharField(max_length=11, null=True)
    type_car = models.CharField(max_length=50, null=True)
    type = models.CharField(max_length=50, null=True)
    pelak = models.CharField(max_length=200, null=True)
    capocity = models.CharField(max_length=50, null=True)
    weight_without_load = models.CharField(max_length=50, null=True)
    health_code = models.CharField(max_length=100, null=True)
    status = models.CharField(max_length=50, default="inactive")
    active = models.BooleanField(default=True)
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="driver_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="driver_wallet",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    kill_house_driver_id_key = models.IntegerField(null=True)
    wallet_amount = models.BigIntegerField(default=0)
    identity_documents = models.JSONField(null=True)
    phone = models.CharField(max_length=20, null=True)
    registrar = models.JSONField(null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    active_state = models.BooleanField(default=True)
    archive = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(KillHouseDriver, self).save(*args, **kwargs)


# مدل مربوط به اضافه کردن ماشین به کشتاگاه
class KillHouseADDCAR(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_add_car",
        null=True
    )
    driver = models.ForeignKey(
        KillHouseDriver,
        on_delete=models.CASCADE,
        related_name="kill_house_add_car_driver",
        null=True
    )
    kill_house_id_foreign_key = models.IntegerField(null=True)
    driver_id_foreign_key = models.IntegerField(null=True)
    kill_house_add_car_id_key = models.IntegerField(null=True)
    status = models.CharField(max_length=50, default="inactive")
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    active_state = models.BooleanField(default=True)
    archive = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(KillHouseADDCAR, self).save(*args, **kwargs)


# مدل بررسی درخواست های شهرستان که شامل تایید یا رد درخواست مرغدار
class CityOperatorCheckRequest(BaseModel):
    city_operator = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        related_name="user_city_operator",
        null=True
    )
    city_operator_system = models.ForeignKey(
        CityOperator,
        on_delete=models.CASCADE,
        related_name="system_city_operator",
        null=True
    )
    poultry_request = models.ForeignKey(
        PoultryRequest,
        on_delete=models.CASCADE,
        related_name="poultry_check",
        null=True
    )
    city_operator_system_id_foreign_key = models.IntegerField(null=True)
    poultry_request_id_foreign_key = models.IntegerField(null=True)
    city_operator_check_request_id_key = models.IntegerField(null=True)
    state = models.CharField(max_length=20, null=True)
    message = models.CharField(max_length=500, null=True)
    allow_hatching = models.BooleanField(null=True)
    show_province = models.BooleanField(default=True)
    province_accept = models.BooleanField(default=False)
    province_state = models.CharField(max_length=20, default='pending')
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(CityOperatorCheckRequest, self).save(*args, **kwargs)


# مدل ثبت اپراتور استان
class ProvinceOperator(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="province_operator_user",
        null=True
    )

    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="province_system_address",
        null=True
    )
    user_bank_info = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="province_operator_bank_info",
                                       null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="province_wallet",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    province_operator_id_key = models.IntegerField(null=True)
    wallet_amount = models.BigIntegerField(default=0)
    identity_documents = models.JSONField(null=True)
    phone = models.CharField(max_length=20, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(ProvinceOperator, self).save(*args, **kwargs)


class ProvinceCheckOperatorOutRequest(BaseModel):
    province_operator_system = models.ForeignKey(
        ProvinceOperator,
        on_delete=models.CASCADE,
        related_name="system_province_operator_out",
        null=True
    )
    poultry_request = models.ForeignKey(
        PoultryRequest,
        on_delete=models.CASCADE,
        related_name="province_poultry_out_request",
        null=True
    )

    province_operator_system_id_foreign_key = models.IntegerField(null=True)
    poultry_request_id_foreign_key = models.IntegerField(null=True)
    province_operator_check_out_request_id_key = models.IntegerField(null=True)
    state = models.CharField(max_length=50, null=True)

    message = models.CharField(max_length=200, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(ProvinceCheckOperatorOutRequest, self).save(*args, **kwargs)


# مدل مربوط به بررسی درخواست های استان که شامل تایید یا رد درخواست های شهرستان
class ProvinceCheckOperatorRequest(BaseModel):
    province_operator = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        related_name="user_province_operator",
        null=True
    )

    province_operator_system = models.ForeignKey(
        ProvinceOperator,
        on_delete=models.CASCADE,
        related_name="system_province_operator",
        null=True
    )
    poultry_request = models.ForeignKey(
        PoultryRequest,
        on_delete=models.CASCADE,
        related_name="province_poultry_request",
        null=True
    )
    city_request_Poultry = models.ForeignKey(
        CityOperatorCheckRequest,
        on_delete=models.CASCADE,
        related_name="Poultry_city_request_to_province", null=True
    )
    province_operator_system_id_foreign_key = models.IntegerField(null=True)
    poultry_request_id_foreign_key = models.IntegerField(null=True)
    city_request_poultry_id_foreign_key = models.IntegerField(null=True)
    province_operator_check_request_id_key = models.IntegerField(null=True)
    state = models.CharField(max_length=50, null=True)
    quantity = models.BigIntegerField(default=0, null=True)
    fee = models.FloatField(null=True)
    total_weight_at_end = models.FloatField(default=0, null=True)
    total_cost_at_end = models.FloatField(null=True)
    paid_state = models.CharField(max_length=20, default="pending")
    province_show_kill_house = models.CharField(max_length=20, default="pending")
    factor_state = models.CharField(max_length=20, default="pending")
    message = models.CharField(max_length=200, null=True)
    financial_archive = models.CharField(max_length=20, default="pending")
    deposit_allocation_archive = models.CharField(max_length=20, default="pending")
    end_state = models.CharField(max_length=20, default='active')
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(ProvinceCheckOperatorRequest, self).save(*args, **kwargs)


# مدل مربوط به بررسی درخواست های استان که شامل تایید یا رد درخواست های شهرستان
class ProvinceRequestAction(BaseModel):
    poultry_request = models.ForeignKey(
        PoultryRequest,
        on_delete=models.CASCADE,
        related_name="province_request_auction_poultry",
        null=True
    )
    province_operator_system = models.ForeignKey(
        ProvinceOperator,
        on_delete=models.CASCADE,
        related_name="system_province_operator_auction",
        null=True
    )
    state = models.CharField(max_length=15, default='pending')

    def save(self, *args, **kwargs):
        super(ProvinceRequestAction, self).save(*args, **kwargs)


class ProvincePercentLeftOver(BaseModel):
    percent = models.FloatField(default=0, null=True)

    def save(self, *args, **kwargs):
        super(ProvincePercentLeftOver, self).save(*args, **kwargs)


class ShareOfAllocation(BaseModel):
    province = models.ForeignKey(
        Province,
        on_delete=models.CASCADE,
        related_name="share_province",
        null=True)
    province_id_foreign_key = models.IntegerField(null=True)
    share_of_allocation_id_key = models.IntegerField(null=True)
    central_union = models.FloatField(default=0, null=True)
    province_union = models.FloatField(default=0, null=True)
    city_union = models.FloatField(default=0, null=True)
    fanava = models.FloatField(default=0, null=True)
    central_control_system = models.FloatField(default=0, null=True)
    company = models.FloatField(default=0, null=True)
    vet = models.FloatField(default=0, null=True)
    total = models.FloatField(default=0, null=True)
    state = models.CharField(max_length=20, null=True)
    date = models.DateTimeField(null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        self.total = self.central_union + self.province_union + self.city_union + self.fanava + self.company
        super(ShareOfAllocation, self).save(*args, **kwargs)


# مدل مربوط به ثبت درخواست بورسی کشتارگاه
class KillHouseRequestExchange(BaseModel):
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, related_name="kill_house_request_exchange",
                             null=True)
    kill_house = models.ForeignKey(KillHouse, on_delete=models.CASCADE,
                                   related_name="kill_house_exchange_kill_house",
                                   null=True)
    quantity = models.BigIntegerField(default=0, null=True)
    send_date1 = models.DateTimeField(null=True)
    send_date2 = models.DateTimeField(null=True)
    chicken_breed = models.JSONField(default=[], null=True)
    Index_weight = models.FloatField(default=0, null=True)
    fee = models.FloatField(default=0, null=True)
    longitude = models.FloatField(default=0)
    latitude = models.FloatField(default=0)
    address = models.TextField()
    age1 = models.IntegerField(default=0, null=True)
    age2 = models.IntegerField(default=0, null=True)
    state = models.CharField(max_length=15, default='pending')

    def save(self, *args, **kwargs):
        super(KillHouseRequestExchange, self).save(*args, **kwargs)


# مدل مربوط به ثبت درخواست بورسی رزرو کشتارگاه
class KillHouseRequestExchangeReserve(BaseModel):
    kill_house = models.ForeignKey(KillHouse, on_delete=models.CASCADE,
                                   related_name="kill_house_request_exchange_kill_house",
                                   null=True)
    # poultry_exchange = models.ForeignKey(PoultryRequestExchange, on_delete=models.CASCADE,
    #                                      related_name="kill_house_request_exchange_poultry",
    #                                      null=True)
    # quantity = models.BigIntegerField(default=0, null=True)
    state = models.CharField(max_length=15, default='pending')

    def save(self, *args, **kwargs):
        super(KillHouseRequestExchangeReserve, self).save(*args, **kwargs)


# مدل مربوط به ثبت درخواست بورسی رزرو کشتارگاه
class KillHouseRequestAction(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_request_auction_kill_house",
        null=True)
    poultry_request = models.ForeignKey(
        PoultryRequest,
        on_delete=models.CASCADE,
        related_name="kill_house_request_auction_poultry",
        null=True)
    fee = models.FloatField(default=0, null=True)
    date = models.DateTimeField(null=True)

    state = models.CharField(max_length=15, default='pending')

    def save(self, *args, **kwargs):
        super(KillHouseRequestAction, self).save(*args, **kwargs)


# مدل مربوط به ثبت درخواست بورسی رزرو کشتارگاه
class KillHouseRequestActionWinner(BaseModel):
    kill_house_request_auction = models.ForeignKey(
        KillHouseRequestAction,
        on_delete=models.CASCADE,
        related_name="kill_house_request_auction_kill_house",
        null=True)
    fee = models.FloatField(default=0, null=True)
    quantity = models.BigIntegerField(default=0, null=True)

    def save(self, *args, **kwargs):
        super(KillHouseRequestActionWinner, self).save(*args, **kwargs)


# مدل مربوط به تایید یا رد درخواست مرغدار که رزرو شده توسط کشتارگاه
class PoultryRequestExchangeAccept(BaseModel):
    poultry_request_exchange = models.ForeignKey(PoultryRequestExchange, on_delete=models.CASCADE,
                                                 related_name="poultry_request_exchange_accept", null=True)
    kill_house_request_exchange_reserve = models.ForeignKey(KillHouseRequestExchangeReserve, on_delete=models.CASCADE,
                                                            related_name="poultry_request_exchange_kill_house_accept",
                                                            null=True)
    state = models.CharField(max_length=15, default='pending')
    message = models.CharField(max_length=200, null=True)

    def save(self, *args, **kwargs):
        super(PoultryRequestExchangeAccept, self).save(*args, **kwargs)


# مدل مربوط اضافه کردن ماشین به درخواست بورسی کشتارگاه
class KillHouseRequestExchangeAddCar(BaseModel):
    kill_house_reserve = models.ForeignKey(KillHouseRequestExchangeReserve, on_delete=models.CASCADE,
                                           related_name="kill_house_request_accept_reserve",
                                           null=True)
    cars = models.JSONField(default=dict, null=True)

    def save(self, *args, **kwargs):
        super(KillHouseRequestExchangeAddCar, self).save(*args, **kwargs)


# مدل مربوط به ثبت درخواست کشتار از سمت  کشتارگاه
class KillRequest(BaseModel):
    kill_house = models.ForeignKey(KillHouse, on_delete=models.CASCADE, related_name="kill", null=True)
    slaughter_house = models.ForeignKey(KillHouse, on_delete=models.CASCADE, related_name="slaughter_kill_req",
                                        null=True)
    kill_house_id_foreign_key = models.IntegerField(null=True)
    slaughter_house_id_foreign_key = models.IntegerField(null=True)
    kill_request_id_key = models.IntegerField(null=True)
    kill_capacity = models.IntegerField(null=True)
    previous_kill_capacity = models.IntegerField(default=0, null=True)
    remain_quantity_for_poultry = models.IntegerField(default=0)
    remain_quantity = models.IntegerField(null=True)
    recive_time = models.CharField(max_length=50, null=True)
    recive_date = models.DateTimeField(null=True)
    state = models.CharField(max_length=20, default='pending')
    province_state = models.CharField(max_length=20, default='pending')
    province_quantity = models.IntegerField(null=True)
    cars = models.JSONField(null=True, default=dict)
    buy_type = models.JSONField(default=dict, null=True)
    weight = models.CharField(max_length=200, null=True)
    weight_type = models.JSONField(default=dict, null=True)
    chicken_breed = models.CharField(max_length=200, null=True)
    old_chicken_breed = models.CharField(max_length=200, null=True)
    message = models.CharField(max_length=200, null=True)
    Index_weight = models.FloatField(null=True)
    first_average_weight = models.FloatField(default=0)
    second_average_weight = models.FloatField(default=0)
    factor_amount = models.FloatField(default=0)
    allocated_number = models.FloatField(default=0)
    factor_deposit = models.FloatField(default=0)
    debt = models.BooleanField(default=False)
    priority = models.BooleanField(default=False)
    debt_amount = models.FloatField(default=0)
    sms_payment = models.BooleanField(default=False)
    automatic = models.BooleanField(default=False)
    automatic_debt = models.BooleanField(default=False)
    payment_info = models.JSONField(null=True)
    registrar = models.JSONField(null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    poultry = models.ForeignKey(Poultry, on_delete=models.CASCADE, related_name="poultry_kill_req", null=True)
    poultry_hatching = models.ForeignKey(PoultryHatching, on_delete=models.CASCADE,
                                         related_name="poultry_hatching_kill_req", null=True)
    poultry_request = models.ForeignKey(PoultryRequest, on_delete=models.CASCADE,
                                        related_name="poultry_request_kill_req", null=True)
    free_direct_buying = models.BooleanField(default=False)
    direct_buying_state = models.CharField(max_length=20, default='pending')
    direct_buying_message = models.CharField(max_length=500, null=True)
    direct_buying_code = models.CharField(max_length=20, null=True)
    input_direct_buying_code = models.CharField(max_length=20, null=True)
    export_status = models.BooleanField(default=False)
    export_state = models.CharField(max_length=20, default='pending')
    export_message = models.CharField(max_length=500, null=True)
    export_code = models.CharField(max_length=20, null=True)
    input_export_code = models.CharField(max_length=20, null=True)
    export_country = models.CharField(max_length=100, null=True)
    accept_reject_date = models.DateTimeField(null=True)
    amount = models.FloatField(default=0)
    automatic_accept = models.BooleanField(default=False)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)
    price_confirmation = models.BooleanField(default=False)
    final_accept = models.BooleanField(default=False)
    poultry_mobile = models.CharField(max_length=20, null=True)
    payment_deadline_days = models.IntegerField(default=0)
    payment_deadline_date = models.DateTimeField(null=True)
    payment_deadline = models.BooleanField(default=False)
    extension_payment_deadline_days = models.IntegerField(default=0)
    extension_payment_deadline_date = models.DateTimeField(null=True)
    market = models.BooleanField(default=False)
    market_final_accept = models.BooleanField(default=False)
    market_state = models.CharField(max_length=20, default='pending')
    market_state_message = models.TextField(null=True)
    market_expire_date_time = models.DateTimeField(null=True)
    direct_buying_intermediary_mobile = models.CharField(max_length=20, null=True)
    market_code_status = models.BooleanField(default=False)
    market_code = models.CharField(max_length=20, null=True)
    input_market_code = models.CharField(max_length=20, null=True)
    expire_time_ten_minute = models.DateTimeField(null=True)
    freezing = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(KillRequest, self).save(*args, **kwargs)


class AutomaticKillRequest(BaseModel):
    active = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(AutomaticKillRequest, self).save(*args, **kwargs)


class KillRequestFactor(BaseModel):
    kill_request = models.ForeignKey(KillRequest, on_delete=models.CASCADE, related_name="factor_kill_req", null=True)
    kill_request_id_foreign_key = models.IntegerField(null=True)
    kill_request_factor_id_key = models.IntegerField(null=True)
    state = models.CharField(max_length=20, default='pending')
    amount = models.FloatField(default=0)
    minimum_amount = models.FloatField(default=0)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(KillRequestFactor, self).save(*args, **kwargs)


class KillRequestFactorPayment(BaseModel):
    kill_request_factor = models.ForeignKey(KillRequestFactor, on_delete=models.CASCADE, related_name="kill_req_factor",
                                            null=True)
    kill_request_factor_id_foreign_key = models.IntegerField(null=True)
    kill_request_factor_payment_id_key = models.IntegerField(null=True)
    state = models.CharField(max_length=20, default='pending')
    image = models.CharField(max_length=200, null=True)
    payment_code = models.BigIntegerField(default=0)
    amount = models.FloatField(default=0)
    remain_amount = models.FloatField(default=0)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(KillRequestFactorPayment, self).save(*args, **kwargs)


# مدل مربوط به تخصیص استان به کشتارگاه
class ProvinceKillRequest(BaseModel):
    killhouse_user = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_user",
        null=True
    )
    kill_request = models.ForeignKey(
        KillRequest,
        on_delete=models.CASCADE,
        related_name="kill_request",
        null=True
    )
    province_request = models.ForeignKey(
        ProvinceCheckOperatorRequest,
        on_delete=models.CASCADE,
        related_name="kill_house_province",
        null=True
    )

    killhouse_user_id_foreign_key = models.IntegerField(null=True)
    kill_request_id_foreign_key = models.IntegerField(null=True)
    province_request_id_foreign_key = models.IntegerField(null=True)
    province_kill_request_id_key = models.IntegerField(null=True)
    quantity = models.BigIntegerField(default=0)
    main_quantity = models.BigIntegerField(default=0)
    total_killed_quantity = models.BigIntegerField(default=0)
    first_car_allocated_quantity = models.BigIntegerField(default=0)
    extra_killed_quantity = models.BigIntegerField(default=0)
    total_killed_weight = models.FloatField(default=0)
    extra_killed_weight = models.FloatField(default=0)
    fee = models.FloatField(default=0, null=True)
    wage = models.FloatField(default=0, null=True)
    total_amount = models.FloatField(default=0, null=True)
    total_wage_amount = models.FloatField(default=0, null=True)
    prev_total_amount = models.FloatField(null=True)
    state = models.CharField(max_length=20, default='pending')
    vet_state = models.CharField(max_length=20, default='pending')
    payment_type = models.CharField(max_length=20, null=True)
    payment_dead_line = models.DateTimeField(null=True)
    message = models.CharField(max_length=200, null=True)
    clearance_code = models.BigIntegerField(null=True)
    automatic = models.BooleanField(default=False)
    dont_show_kill_house = models.BooleanField(default=False)
    reviewer = models.JSONField(null=True)
    total_amount_editor = models.JSONField(null=True)
    delete_message = models.CharField(max_length=200, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    wage_pay = models.BooleanField(default=False)
    depositor = models.JSONField(null=True)
    archive_by_province = models.BooleanField(default=False)
    archive_wage = models.BooleanField(default=False)
    return_to_province = models.BooleanField(default=False)
    archive_message = models.CharField(max_length=500, null=True)
    archiver = models.JSONField(null=True)
    return_archive_message = models.CharField(max_length=500, null=True)
    returner = models.JSONField(null=True)
    wallet_pay = models.BooleanField(default=False)
    union_share = models.BigIntegerField(default=0)
    union_share_percent = models.FloatField(default=0)
    company_share = models.BigIntegerField(default=0)
    company_share_percent = models.FloatField(default=0)
    guilds_share = models.BigIntegerField(default=0)
    guilds_share_percent = models.FloatField(default=0)
    city_share = models.BigIntegerField(default=0)
    city_share_percent = models.FloatField(default=0)
    wallet_share = models.BigIntegerField(default=0)
    wallet_share_percent = models.FloatField(default=0)
    other_share = models.BigIntegerField(default=0)
    other_share_percent = models.FloatField(default=0)
    kill_house_price = models.BigIntegerField(default=0)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)
    payment_deadline_days = models.IntegerField(default=0)
    payment_deadline_date = models.DateTimeField(null=True)
    payment_deadline = models.BooleanField(default=False)
    payment_deadline_archive = models.BooleanField(default=False)
    payment_deadline_state = models.CharField(max_length=50, default='pending')
    payment_deadline_checker_fullname = models.CharField(max_length=50, null=True)
    payment_deadline_checker_mobile = models.CharField(max_length=50, null=True)
    payment_deadline_check_date = models.DateTimeField(null=True)
    payment_deadline_archive_message = models.TextField(null=True)
    payment_deadline_supporter_message = models.TextField(null=True)
    final_accept = models.BooleanField(default=False)
    final_accept_registrar = models.CharField(max_length=200, null=True)
    final_accept_registrar_mobile = models.CharField(max_length=20, null=True)
    final_accept_date = models.DateTimeField(null=True)
    payment_deadline_amount = models.BigIntegerField(default=0)
    extension_payment_deadline_days = models.IntegerField(default=0)
    extension_payment_deadline_date = models.DateTimeField(null=True)
    fine = models.BooleanField(default=False)
    fine_amount = models.BigIntegerField(default=0)
    fine_coefficient = models.FloatField(default=0)
    direct_buying_fine = models.BooleanField(default=False)
    direct_buying_fine_amount = models.BigIntegerField(default=0)
    direct_buying_fine_coefficient = models.FloatField(default=0)
    direct_buying = models.BooleanField(default=False)
    market = models.BooleanField(default=False)
    warehouse = models.BooleanField(default=True)
    return_trash = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        self.extra_killed_quantity = self.total_killed_quantity - self.main_quantity if self.total_killed_quantity - self.main_quantity > 0 else 0

        self.quantity = self.main_quantity - self.first_car_allocated_quantity if self.main_quantity - self.first_car_allocated_quantity > 0 else 0
        super(ProvinceKillRequest, self).save(*args, **kwargs)


class BarDocumentStatus(BaseModel):
    title = models.CharField(max_length=500, null=True)
    description = models.TextField(null=True)
    sms = models.BooleanField(default=False)
    is_error = models.BooleanField(default=False)
    priority_id = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        if self.priority_id == 0:
            bar_documents_status = BarDocumentStatus.objects.filter(trash=False).order_by('priority_id').last()
            self.priority_id = bar_documents_status.priority_id + 1 if bar_documents_status else 1
        super(BarDocumentStatus, self).save(*args, **kwargs)


# مدل ایحاد درخواست کشتار از سمت کشتارگاه با توجه به میزان تخصیص از سمت استان
class KillHouseRequest(BaseModel):
    killhouse_user = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_request_user",
        null=True
    )
    killer = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_request_killer",
        null=True
    )
    kill_request = models.ForeignKey(
        KillRequest,
        on_delete=models.CASCADE,
        related_name="kill_request_kill_house",
        null=True
    )
    province_request = models.ForeignKey(
        ProvinceCheckOperatorRequest,
        on_delete=models.CASCADE,
        related_name="kill_house_province_request",
        null=True
    )
    province_kill_request = models.ForeignKey(
        ProvinceKillRequest,
        on_delete=models.CASCADE,
        related_name="province_kill_house_request",
        null=True
    )
    kill_house_request_auction_winner = models.ForeignKey(
        KillHouseRequestActionWinner,
        on_delete=models.CASCADE,
        related_name="kill_house_request_winner",
        null=True
    )
    # driver = models.ForeignKey(
    #     KillHouseDriver,
    #     on_delete=models.CASCADE,
    #     related_name="kill_house_request_driver",
    #     null=True
    # )
    add_car = models.ForeignKey(
        KillHouseADDCAR,
        on_delete=models.CASCADE,
        related_name="kill_house_request_add_car",
        null=True
    )
    real_add_car = models.ForeignKey(
        KillHouseADDCAR,
        on_delete=models.CASCADE,
        related_name="kill_house_request_real_add_car",
        null=True
    )
    bar_document_status = models.ForeignKey(
        BarDocumentStatus,
        on_delete=models.CASCADE,
        related_name="kill_house_request_bar_document_status",
        null=True
    )
    input_warehouse = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_input_warehouse",
        null=True
    )
    killhouse_user_id_foreign_key = models.IntegerField(null=True)
    kill_request_id_foreign_key = models.IntegerField(null=True)
    province_request_id_foreign_key = models.IntegerField(null=True)
    province_kill_request_id_foreign_key = models.IntegerField(null=True)
    add_car_id_foreign_key = models.IntegerField(null=True)
    kill_house_request_id_key = models.IntegerField(null=True)
    capacity = models.BigIntegerField(default=0)
    quantity = models.BigIntegerField(default=0)
    bar_code = models.BigIntegerField(null=True)
    quarantine_quantity = models.BigIntegerField(null=True)
    quarantine_code_state = models.CharField(max_length=500, null=True)
    fee = models.FloatField(default=0, null=True)
    time = models.CharField(max_length=20, null=True)
    state = models.CharField(max_length=20, null=True)
    vet_state = models.CharField(max_length=20, default='pending')
    active_state = models.CharField(max_length=20, default='active')
    assignment_state_archive = models.CharField(max_length=20, default='pending')
    show_kill_house = models.CharField(max_length=20, default='pending')
    car = models.JSONField(default=dict, null=True)
    kill_house_message = models.TextField(max_length=1000, null=True)
    allocation_state = models.CharField(max_length=20, null=True)
    auction = models.BooleanField(default=False)
    role = models.JSONField(null=True)
    clearance_code = models.CharField(max_length=200, null=True)
    traffic_code = models.CharField(max_length=200, null=True)
    registrar_clearance_code = models.JSONField(null=True)
    editor_traffic_code = models.JSONField(null=True)
    bar_remover = models.JSONField(null=True)
    extra_killed_quantity = models.BigIntegerField(default=0)
    accepted_real_quantity = models.BigIntegerField(default=0)
    accepted_real_weight = models.FloatField(default=0)
    extra_killed_weight = models.FloatField(default=0)
    vet_accepted_real_quantity = models.BigIntegerField(default=0)
    vet_accepted_real_weight = models.FloatField(default=0)
    accepted_assignment_real_quantity = models.BigIntegerField(default=0)
    accepted_assignment_real_weight = models.FloatField(default=0)
    message = models.CharField(max_length=200, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    ware_house_confirmation = models.BooleanField(default=False)
    ware_house_accepted_real_quantity = models.BigIntegerField(default=0)
    ware_house_accepted_real_weight = models.FloatField(default=0)
    date_of_ware_house = models.CharField(max_length=200, null=True)
    freezing = models.BooleanField(default=False)
    archive_wage = models.BooleanField(default=False)
    weight_loss = models.FloatField(default=0)
    ware_house_input_type = models.CharField(max_length=100, null=True)
    document_status = models.CharField(max_length=500, null=True)
    aggregate_code = models.BigIntegerField(null=True)
    aggregate_status = models.BooleanField(default=False)
    calculate_status = models.BooleanField(default=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)
    entered_message = models.TextField(null=True)
    # فیلد های استعلام از قرنطینه
    inquiry_date = models.DateField(null=True)
    inquiry_origin = models.CharField(max_length=100, null=True)
    inquiry_destination = models.CharField(max_length=100, null=True)
    inquiry_driver = models.CharField(max_length=100, null=True)
    inquiry_pelak = models.CharField(max_length=100, null=True)
    settlement_type = models.CharField(max_length=100, null=True)
    price = models.IntegerField(default=0)
    description = models.TextField(null=True)
    bar_document_description = models.TextField(null=True)
    image = models.CharField(max_length=500, null=True)
    price_registerar = models.CharField(max_length=200, null=True)
    price_registerar_role = models.CharField(max_length=100, null=True)
    price_register_date = models.DateTimeField(null=True)
    price_editor = models.CharField(max_length=200, null=True)
    price_editor_role = models.CharField(max_length=100, null=True)
    price_editor_date = models.DateTimeField(null=True)
    non_receipt = models.BooleanField(default=False)
    non_receipt_return = models.BooleanField(default=False)
    non_receipt_return_message = models.TextField(null=True)
    non_receipt_message = models.TextField(null=True)
    main_non_receipt = models.BooleanField(default=False)
    non_receipt_state = models.CharField(max_length=20, default='pending')
    non_receipt_checker = models.CharField(max_length=200, null=True)
    non_receipt_checker_message = models.TextField(null=True)
    non_receipt_checker_mobile = models.CharField(max_length=20, null=True)
    non_receipt_check_date = models.DateTimeField(null=True)
    non_receipt_returner = models.CharField(max_length=200, null=True)
    non_receipt_returner_mobile = models.CharField(max_length=20, null=True)
    non_receipt_return_date = models.DateTimeField(null=True)
    fine = models.BooleanField(default=False)
    fine_amount = models.BigIntegerField(default=0)
    fine_coefficient = models.FloatField(default=0)
    document_number = models.CharField(max_length=50, null=True)
    company_document = models.BooleanField(default=False)
    warehouse = models.BooleanField(default=True)
    warehouse_commitment_weight = models.FloatField(default=0)
    return_trash = models.BooleanField(default=False)
    amount = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(KillHouseRequest, self).save(*args, **kwargs)


# مدل مربوط به تایید یا رد درخواست کشتار تخصیص داده شده به کشتارگاه توسط کشتارگاه
class KillHouseCheckRequest(BaseModel):
    province_kill_request = models.ForeignKey(
        ProvinceKillRequest,
        on_delete=models.CASCADE,
        related_name="province_kill_house_check",
        null=True
    )
    province_kill_request_id_foreign_key = models.IntegerField(null=True)
    kill_house_check_request_id_key = models.IntegerField(null=True)
    role = models.JSONField(null=True)
    state = models.CharField(max_length=20, null=True)
    message = models.CharField(max_length=200, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(KillHouseCheckRequest, self).save(*args, **kwargs)


class MostSearch(BaseModel):
    searched_items = models.CharField(max_length=200, null=True)
    count = models.IntegerField(default=0)


# مدل مربوط به ورود اطلاعات بار توسط کشتارگاه
class KillHouseAssignmentInformation(BaseModel):
    kill_house_check = models.ForeignKey(
        KillHouseCheckRequest,
        on_delete=models.CASCADE,
        related_name="kill_house_check_req",
        null=True
    )
    kill_house_request = models.ForeignKey(
        KillHouseRequest,
        on_delete=models.CASCADE,
        related_name="kill_house_request_assignment",
        null=True
    )

    kill_house_check_id_foreign_key = models.IntegerField(null=True)
    kill_house_request_id_foreign_key = models.IntegerField(null=True)
    kill_house_kill_house_assingment_information_id_key = models.IntegerField(null=True)
    car_weight_without_load = models.FloatField(default=0)
    car_weight_without_load_image = models.CharField(max_length=500, null=True)
    car_weight_with_load = models.FloatField(default=0)
    car_weight_with_load_image = models.CharField(max_length=500, null=True)
    message = models.CharField(max_length=200, null=True)
    net_weight = models.FloatField(default=0)
    exploited_carcass = models.FloatField(default=0)
    state = models.CharField(max_length=15, default='pending')
    poultry = models.BooleanField(default=False)
    city = models.BooleanField(default=False)
    province = models.BooleanField(default=False)
    unusual_casualties = models.BooleanField(default=False)
    real_quantity = models.IntegerField(default=0)
    importer = models.JSONField(default=dict, null=True)
    protest_time = models.DateTimeField(null=True)
    weight_withs_losses = models.FloatField(default=0, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        # self.net_weight = float(self.car_weight_with_load) - float(self.car_weight_without_load)
        super(KillHouseAssignmentInformation, self).save(*args, **kwargs)


# مدل مربوط به ورود اطلاعات بار توسط کشتارگاه
class KillHouseAssignmentImages(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_images",
        null=True
    )
    health_certificate_image = models.CharField(max_length=500, null=True)
    national_card_image = models.CharField(max_length=500, null=True)
    birth_certificate_image = models.CharField(max_length=500, null=True)
    police_clearance_image = models.CharField(max_length=500, null=True)
    no_addiction_image = models.CharField(max_length=500, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(KillHouseAssignmentImages, self).save(*args, **kwargs)


# مدل مربوط به ورود اطلاعات بار توسط مرغدار
class PoultryAssignmentInformation(BaseModel):
    poultry_request = models.ForeignKey(
        PoultryRequest,
        on_delete=models.CASCADE,
        related_name="poultry_assignment",
        null=True
    )
    car_weight_without_load = models.FloatField(default=0)
    car_weight_without_load_image = models.CharField(max_length=500, null=True)
    car_weight_with_load = models.FloatField(default=0)
    car_weight_with_load_image = models.CharField(max_length=500, null=True)
    net_weight = models.FloatField(default=0)
    state = models.CharField(max_length=15, default='pending')

    def save(self, *args, **kwargs):
        self.net_weight = float(self.car_weight_with_load) - float(self.car_weight_without_load)
        super(PoultryAssignmentInformation, self).save(*args, **kwargs)


# مدل مربوط به بررسی اطلاعات بار وارد شده
class ProvinceCheckInformation(BaseModel):
    kill_house_assignment = models.ForeignKey(
        KillHouseAssignmentInformation,
        on_delete=models.CASCADE,
        related_name="kill_house_assign",
        null=True
    )
    poultry_assignment = models.ForeignKey(
        PoultryAssignmentInformation,
        on_delete=models.CASCADE,
        related_name="poultry_assign",
        null=True
    )
    province_operator = models.ForeignKey(
        ProvinceOperator,
        on_delete=models.CASCADE,
        related_name="provinc_operator_check_info",
        null=True
    )
    kill_house_assignment_id_foreign_key = models.IntegerField(null=True)
    province_operator_id_foreign_key = models.IntegerField(null=True)
    province_check_information_id_key = models.IntegerField(null=True)
    state = models.CharField(max_length=20, null=True)
    message = models.CharField(max_length=200, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(ProvinceCheckInformation, self).save(*args, **kwargs)


# مدل ایجاد فاکتور توسط استان برای کشتارگاه
class ProvinceFactorToKillHouseForPoultry(BaseModel):
    province_check_info = models.ForeignKey(
        ProvinceCheckInformation,
        on_delete=models.CASCADE,
        related_name="province_info_poultry",
        null=True
    )
    province_check_req = models.ForeignKey(
        ProvinceCheckOperatorRequest,
        on_delete=models.CASCADE,
        related_name="province_check_poultry",
        null=True
    )

    total_weight = models.FloatField(default=0)
    weight = models.FloatField(default=0)
    factor_fee = models.FloatField(default=0)
    factor_bar_code = models.BigIntegerField(null=True)
    factor_date = models.DateTimeField(null=True)
    total_price = models.FloatField(default=0)
    today_price = models.FloatField(default=0)
    real_weight = models.FloatField(default=0)
    shares = models.JSONField(default=dict, null=True)
    paid_state = models.CharField(max_length=20, default='pending')
    pay_for = models.CharField(max_length=20, null=True)
    kill_house_factor_info = models.CharField(max_length=20, default='pending')
    bank = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="bank_card_poultry",
        null=True
    )
    sum_state = models.CharField(max_length=20, default='pending')
    out = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        # self.total_price = self.total_weight * self.factor_fee
        super(ProvinceFactorToKillHouseForPoultry, self).save(*args, **kwargs)


# مدل ایجاد فاکتور توسط استان برای کشتارگاه
class ProvinceFactorToKillHouse(BaseModel):
    province_check_info = models.ForeignKey(
        ProvinceCheckInformation,
        on_delete=models.CASCADE,
        related_name="province_info",
        null=True
    )
    poultry_factor = models.ForeignKey(
        ProvinceFactorToKillHouseForPoultry,
        on_delete=models.CASCADE,
        related_name="province_factor_poultry",
        null=True
    )
    province_check_req = models.ForeignKey(
        ProvinceCheckOperatorRequest,
        on_delete=models.CASCADE,
        related_name="province_check_province",
        null=True
    )

    total_weight = models.FloatField(default=0)
    weight = models.FloatField(default=0)
    factor_fee = models.FloatField(default=0)
    factor_bar_code = models.BigIntegerField(null=True)
    factor_date = models.DateTimeField(null=True)
    total_price = models.FloatField(default=0)
    province_input_amount = models.FloatField(default=0)
    province_input_amount_type = models.CharField(max_length=20, null=True)
    province_wage = models.FloatField(default=0)
    previous_amount = models.FloatField(default=0)
    decreasing_amount = models.FloatField(default=0)
    today_price = models.FloatField(default=0)
    kill_house_factor_payment_remain = models.FloatField(default=0)
    real_weight = models.FloatField(default=0)
    shares = models.JSONField(default=dict, null=True)
    paid_state = models.CharField(max_length=20, default='pending')
    pay_for = models.CharField(max_length=20, null=True)
    kill_house_factor_info = models.CharField(max_length=20, default='pending')
    reason = models.TextField(null=True)
    sum_state = models.CharField(max_length=20, default='pending')
    factor_person_type = models.CharField(max_length=20, default='pending')
    # bank = models.JSONField(default={'bank_info': 'بانک کشاورزی به نام اتحادیه تعاونی کشاورزی محمد رضا کمالوندی',
    #                                  'bank_card': '6037654323417678', 'bank_shaba': '56678876554433223445566'})
    bank = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="bank_card_province",
        null=True
    )
    province_check_info_id_foreign_key = models.IntegerField(null=True)
    province_check_req_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    province_factor_to_kill_house_id_key = models.IntegerField(null=True)
    out = models.BooleanField(default=False)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        # self.total_price = self.total_weight * self.factor_fee
        super(ProvinceFactorToKillHouse, self).save(*args, **kwargs)


class KillHouseFactorToPoultry(BaseModel):
    province_factor = models.ForeignKey(
        ProvinceFactorToKillHouseForPoultry,
        on_delete=models.CASCADE,
        related_name="province_factor_info_poultry",
        null=True
    )

    payment_code = models.CharField(max_length=100, null=True)
    state = models.CharField(max_length=20, default='pending')
    payment_state = models.CharField(max_length=50, null=True)
    payment_method = models.CharField(max_length=50, null=True)
    factor_image = models.CharField(max_length=500, null=True)
    message = models.CharField(max_length=500, null=True)
    importer = models.CharField(max_length=100, null=True)
    archive_state = models.CharField(max_length=50, default='pending')

    def save(self, *args, **kwargs):
        super(KillHouseFactorToPoultry, self).save(*args, **kwargs)


# مدل ایجاد فاکتور توسط کشتارگاه برای استان
class KillHouseFactorToProvince(BaseModel):
    province_factor = models.ForeignKey(
        ProvinceFactorToKillHouse,
        on_delete=models.CASCADE,
        related_name="province_factor_info",
        null=True
    )

    province_factor_id_foreign_key = models.IntegerField(null=True)
    kill_house_factor_to_province_id_key = models.IntegerField(null=True)

    payment_code = models.CharField(max_length=100, null=True)
    state = models.CharField(max_length=20, default='pending')
    payment_state = models.CharField(max_length=50, null=True)
    payment_method = models.CharField(max_length=50, null=True)
    factor_image = models.CharField(max_length=500, null=True)
    message = models.CharField(max_length=500, null=True)
    importer = models.CharField(max_length=100, null=True)
    archive_state = models.CharField(max_length=50, default='pending')
    first_payment = models.BooleanField(default=False)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(KillHouseFactorToProvince, self).save(*args, **kwargs)


# مدل بررسی فاکتور کشتارگاه توسط استان
class ProvinceCheckKillHouseFactor(BaseModel):
    kill_house_factor = models.ForeignKey(
        KillHouseFactorToProvince,
        on_delete=models.CASCADE,
        related_name="province_check_factor",
        null=True
    )
    kill_house_factor_poultry = models.ForeignKey(
        KillHouseFactorToPoultry,
        on_delete=models.CASCADE,
        related_name="province_check_factor_poultry",
        null=True
    )
    kill_house_factor_id_foreign_key = models.IntegerField(null=True)
    province_check_kill_house_factor_id_key = models.IntegerField(null=True)
    state = models.CharField(max_length=20, null=True)
    message = models.CharField(max_length=500, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(ProvinceCheckKillHouseFactor, self).save(*args, **kwargs)


class ProvinceImportKillHouseOutFactors(BaseModel):
    kill_house_factor = models.ForeignKey(
        ProvinceFactorToKillHouse,
        on_delete=models.CASCADE,
        related_name="province_check_factor_out",
        null=True
    )
    kill_house_factor_poultry = models.ForeignKey(
        ProvinceFactorToKillHouseForPoultry,
        on_delete=models.CASCADE,
        related_name="province_check_factor_poultry_out",
        null=True
    )
    province_check = models.ForeignKey(
        ProvinceCheckOperatorRequest,
        on_delete=models.CASCADE,
        related_name="province_check_request",
        null=True
    )
    image = models.CharField(max_length=500, null=True)
    amount = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(ProvinceImportKillHouseOutFactors, self).save(*args, **kwargs)


# مدل مریوط به تخصیصات مالی برای مرغدار و شهرستان و استان
class DepositAllocation(BaseModel):
    poultry = models.ForeignKey(
        PoultryRequest,
        on_delete=models.CASCADE,
        related_name="allocations_poultry",
        null=True
    )
    poultry_request_id_foreign_key = models.IntegerField(null=True)
    deposit_allocation_id_key = models.IntegerField(null=True)

    poultry_share = models.FloatField(default=0, null=True)
    poultry_share_payment = models.FloatField(default=0, null=True)
    poultry_share_payment_info = models.JSONField(default=list, null=True)
    city_share = models.FloatField(default=0, null=True)
    city_share_payment = models.FloatField(default=0, null=True)
    city_share_payment_info = models.JSONField(default=list, null=True)
    province_share = models.FloatField(default=0, null=True)
    province_share_payment = models.FloatField(default=0, null=True)
    province_share_payment_info = models.JSONField(default=list, null=True)
    poultry_bank = models.JSONField(default=dict, null=True)
    city_bank = models.JSONField(default=dict, null=True)
    poultry_image = models.CharField(max_length=200, null=True)
    city_image = models.CharField(max_length=200, null=True)
    company_share = models.FloatField(default=0, null=True)
    company_share_payment = models.FloatField(default=0, null=True)
    company_share_payment_info = models.JSONField(default=list, null=True)
    company_bank = models.JSONField(default=dict, null=True)
    central_union_share = models.FloatField(default=0, null=True)
    central_union_share_payment = models.FloatField(default=0, null=True)
    central_union_share_payment_info = models.JSONField(default=list, null=True)
    fanava_share = models.FloatField(default=0, null=True)
    fanava_share_payment = models.FloatField(default=0, null=True)
    fanava_share_payment_info = models.JSONField(default=list, null=True)

    deposit_allocation_state = models.CharField(max_length=20, default='pending')
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(DepositAllocation, self).save(*args, **kwargs)


class InspectorOperator(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="inspector_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="inspector_address",
        null=True
    )
    user_bank_info = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="province_inspector_bank_info",
                                       null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="inspector_wallet",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    inspector_operator_id_key = models.IntegerField(null=True)
    wallet_amount = models.BigIntegerField(default=0)
    active = models.BooleanField(default=True)
    identity_documents = models.JSONField(null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    id_number = models.IntegerField(default=1)

    def save(self, *args, **kwargs):
        super(InspectorOperator, self).save(*args, **kwargs)


# مدل مربوط به تایید یا رد پرونده توسط بازرس استان
class PovinceInspector(BaseModel):
    inspector_operator = models.ForeignKey(
        InspectorOperator,
        on_delete=models.CASCADE,
        related_name="inspector",
        null=True
    )
    poultry_request = models.ForeignKey(
        PoultryRequest,
        on_delete=models.CASCADE,
        related_name="poultry_request_inspector",
        null=True
    )
    inspector_operator_id_foreign_key = models.IntegerField(null=True)
    poultry_request_id_foreign_key = models.IntegerField(null=True)
    province_inspector_id_key = models.IntegerField(null=True)
    state = models.CharField(max_length=20, default='pending')
    message = models.CharField(max_length=500, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(PovinceInspector, self).save(*args, **kwargs)


class FunctionExecutor(BaseModel):
    func_name = models.CharField(max_length=200, null=True)
    time_type = models.CharField(max_length=100, null=True)
    cycle = models.IntegerField(default=0)
    compare_time = models.DateTimeField(default=datetime.now())


class ReportingFieldRoleLimitation(BaseModel):
    role_name = models.ForeignKey(
        Group,
        on_delete=models.CASCADE,
        null=True,
        related_name="report_role"
    )
    fields = models.CharField(max_length=200, null=True)

    def save(self, *args, **kwargs):
        super(ReportingFieldRoleLimitation, self).save(*args, **kwargs)


# مدل مربوط به آنالیز قیمت مرغ
class PriceAnalysis(models.Model):
    corn_percent = models.FloatField(default=0, null=True)
    corn_free_price = models.FloatField(default=0, null=True)
    corn_Ration_free_price = models.FloatField(default=0, null=True)
    corn_total_free_price = models.FloatField(default=0, null=True)
    Soy_free_price = models.FloatField(default=0, null=True)
    Soy_total_free_price = models.FloatField(default=0, null=True)
    Soy_Ration_free_price = models.FloatField(default=0, null=True)
    Soy_percent = models.FloatField(default=0, null=True)
    Oil_percent = models.FloatField(default=0, null=True)
    Oil_free_price = models.FloatField(default=0, null=True)
    Oil_total_free_price = models.FloatField(default=0, null=True)
    Oil_Ration_free_price = models.FloatField(default=0, null=True)
    Dicalcium_free_price = models.FloatField(default=0, null=True)
    Dicalcium_total_free_price = models.FloatField(default=0, null=True)
    Dicalcium_Ration_free_price = models.FloatField(default=0, null=True)
    Dicalcium_percent = models.FloatField(default=0, null=True)
    Carbonate_free_price = models.FloatField(default=0, null=True)
    Carbonate_total_free_price = models.FloatField(default=0, null=True)
    Carbonate_Ration_free_price = models.FloatField(default=0, null=True)
    Carbonate_percent = models.FloatField(default=0, null=True)
    mineral_supplement_percent = models.FloatField(default=0, null=True)
    mineral_Ration_supplement_free_price = models.FloatField(default=0, null=True)
    mineral_supplement_free_price = models.FloatField(default=0, null=True)
    mineral_supplement_total_free_price = models.FloatField(default=0, null=True)
    vitamin_supplement_free_price = models.FloatField(default=0, null=True)
    vitamin_supplement_total_free_price = models.FloatField(default=0, null=True)
    vitamin_supplement_percent = models.FloatField(default=0, null=True)
    vitamin_Ration_free_supplement_percent = models.FloatField(default=0, null=True)
    baking_soda_free_price = models.FloatField(default=0, null=True)
    baking_soda_total_free_price = models.FloatField(default=0, null=True)
    baking_Ration_soda_free_price = models.FloatField(default=0, null=True)
    baking_soda_percent = models.FloatField(default=0, null=True)
    Salt_Ration_free_price = models.FloatField(default=0, null=True)
    Salt_Ration_total_free_price = models.FloatField(default=0, null=True)
    Salt_free_total_price = models.FloatField(default=0, null=True)
    Salt_free_price = models.FloatField(default=0, null=True)
    Salt_percent = models.FloatField(default=0, null=True)
    metiunin_Ration_free_price = models.FloatField(default=0, null=True)
    metiunin_free_price = models.FloatField(default=0, null=True)
    metiunin_total_free_price = models.FloatField(default=0, null=True)
    metiunin_percent = models.FloatField(default=0, null=True)
    Lysine_Ration_free_price = models.FloatField(default=0, null=True)
    Lysine_free_price = models.FloatField(default=0, null=True)
    Lysine_total_free_price = models.FloatField(default=0, null=True)
    Lysine_percent = models.FloatField(default=0, null=True)
    threonine_free_price = models.FloatField(default=0, null=True)
    threonine_total_free_price = models.FloatField(default=0, null=True)
    threonine_Ration_free_price = models.FloatField(default=0, null=True)
    threonine_percent = models.FloatField(default=0, null=True)
    Antifungal_free_price = models.FloatField(default=0, null=True)
    Antifungal_total_free_price = models.FloatField(default=0, null=True)
    Antifungal_Ration_free_price = models.FloatField(default=0, null=True)
    Antifungal_percent = models.FloatField(default=0, null=True)
    inulin_free_price = models.FloatField(default=0, null=True)
    inulin_total_free_price = models.FloatField(default=0, null=True)
    inulin_Ration_free_price = models.FloatField(default=0, null=True)
    inulin_percent = models.FloatField(default=0, null=True)
    Fire_emulsion_free_price = models.FloatField(default=0, null=True)
    Fire_total_emulsion_free_price = models.FloatField(default=0, null=True)
    Fire_emulsion_Ration_free_price = models.FloatField(default=0, null=True)
    Fire_emulsion_percent = models.FloatField(default=0, null=True)
    Colleen_free_price = models.FloatField(default=0, null=True)
    Colleen_total_free_price = models.FloatField(default=0, null=True)
    Colleen_Ration_free_price = models.FloatField(default=0, null=True)
    Colleen_percent = models.FloatField(default=0, null=True)
    total_price = models.FloatField(default=0, null=True)
    total_Ration_free_price = models.FloatField(default=0, null=True)
    Vaccination_period = models.FloatField(default=0, null=True)
    Multivitamin_course = models.FloatField(default=0, null=True)
    period_chicken = models.FloatField(default=0, null=True)
    directors_office = models.FloatField(default=0, null=True)
    Water_gas_electricity_period = models.FloatField(default=0, null=True)
    Water_gas_the_current_cost_of_the_period_unit = models.FloatField(default=0, null=True)
    Course_repairs = models.FloatField(default=0, null=True)
    Course_loss = models.FloatField(default=0, null=True)
    motfar_ga_durah = models.FloatField(default=0, null=True)
    Paper_reel = models.FloatField(default=0, null=True)
    Course_disinfection = models.FloatField(default=0, null=True)
    Lamp = models.FloatField(default=0, null=True)
    total_percent = models.FloatField(default=0, null=True)
    total_ration = models.FloatField(default=0, null=True)
    chicken_price = models.FloatField(default=0, null=True)

    def save(self, *args, **kwargs):
        super(PriceAnalysis, self).save(*args, **kwargs)


# مدل مربوط به ثیت نماینده دامپزشک برای کشتارگاه


class KillHouseVet(BaseModel):
    vet = models.ForeignKey(
        Vet,
        on_delete=models.CASCADE,
        null=True,
        related_name="kill_house_vet_user"
    )
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        null=True,
        related_name="kill_house_vet"
    )
    vet_id_foreign_key = models.IntegerField(null=True)
    kill_house_id_foreign_key = models.IntegerField(null=True)
    kill_house_vet_id_key = models.IntegerField(null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(KillHouseVet, self).save(*args, **kwargs)


# مدل مربوط به تایید بار رسیده به کشتار گاه توسط نماینده دامپزشکی
class VetCheckRequest(BaseModel):
    kill_house_vet = models.ForeignKey(
        KillHouseVet,
        on_delete=models.CASCADE,
        null=True,
        related_name="kill_house_user_vet"
    )

    kill_house_request = models.ForeignKey(
        KillHouseRequest,
        on_delete=models.CASCADE,
        null=True,
        related_name="kill_house_request_vet"
    )
    kill_house_vet_id_foreign_key = models.IntegerField(null=True)
    kill_house_request_id_foreign_key = models.IntegerField(null=True)
    vet_check_request_id_key = models.IntegerField(null=True)

    state = models.CharField(max_length=20, null=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(VetCheckRequest, self).save(*args, **kwargs)


# مدل مربوط به ثبت  دامپزشک فارم
class VetFarm(BaseModel):
    vet = models.ForeignKey(
        Vet,
        on_delete=models.CASCADE,
        related_name="vet_farm_vet",
        null=True
    )
    poultry = models.ForeignKey(
        Poultry,
        on_delete=models.CASCADE,
        related_name="vet_farm_poultry",
        null=True
    )
    vet_id_foreign_key = models.IntegerField(null=True)
    poultry_id_foreign_key = models.IntegerField(null=True)
    vet_farm_id_key = models.IntegerField(null=True)

    hall = models.IntegerField(default=0)
    vetfarm_losses = models.BigIntegerField(default=0)
    state = models.CharField(max_length=20, default='pending')
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(VetFarm, self).save(*args, **kwargs)


# مدل مربوط به ثبت  دامپزشک فارم
class VetFarmInspection(BaseModel):
    vet_farm = models.ForeignKey(VetFarm,
                                 on_delete=models.CASCADE,
                                 related_name="vet_farm_inspection",
                                 null=True
                                 )
    poultry_hatching = models.ForeignKey(
        PoultryHatching,
        on_delete=models.CASCADE,
        related_name="vet_farm_inspection_poultry_hatching",
        null=True
    )

    vet_farm_id_foreign_key = models.IntegerField(null=True)
    poultry_hatching_id_foreign_key = models.IntegerField(null=True)
    vet_farm_inspection_id_key = models.IntegerField(null=True)

    hall = models.IntegerField(default=0)
    topic = models.CharField(max_length=500, null=True)
    description = models.TextField(null=True)
    Losses = models.BigIntegerField(default=0)
    image = models.JSONField(null=True)
    longitude = models.FloatField(default=0)
    latitude = models.FloatField(default=0)
    state = models.CharField(max_length=20, default='pending')
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(VetFarmInspection, self).save(*args, **kwargs)


# مدل مربوط ثبت درخواست مرغ منجمد
class WareHouseFactor(BaseModel):
    customer = models.CharField(max_length=50, null=True)
    national_code = models.CharField(max_length=20, null=True)
    letter_number = models.BigIntegerField(null=True)
    letter_number_jahad = models.BigIntegerField(null=True)
    date = models.DateTimeField(null=True)
    mobile = models.CharField(max_length=11, null=True)
    product_type = models.JSONField(default=dict, null=True)
    weight = models.FloatField(default=0, null=True)
    fee = models.FloatField(default=0, null=True)
    wage = models.FloatField(default=0, null=True)
    total_wage = models.FloatField(default=0, null=True)
    total_fee = models.FloatField(default=0, null=True)
    total_cost = models.FloatField(default=0, null=True)
    Complications = models.FloatField(default=0, null=True)
    tax = models.FloatField(default=0, null=True)
    discount = models.FloatField(default=0, null=True)
    name_of_bank_user = models.CharField(max_length=200, null=True)
    card = models.CharField(max_length=16, null=True)
    shaba = models.CharField(max_length=100, null=True)
    state = models.CharField(max_length=100, default='pending')
    account = models.CharField(max_length=100, null=True)
    mark = models.CharField(max_length=100, default='pending')
    considerations = models.TextField(null=True)
    first_operator = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        null=True,
        related_name="user_first_operator"
    )
    second_operator = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        null=True,
        related_name="user_second_operator"
    )

    def save(self, *args, **kwargs):
        super(WareHouseFactor, self).save(*args, **kwargs)


# مدل مربوط به اضافه کردن راننده به کشتاگاه
class DriverRequestCancel(BaseModel):
    Kill_house_add_car = models.ForeignKey(
        KillHouseADDCAR,
        on_delete=models.CASCADE,
        related_name="cancel_request_car",
        null=True
    )
    kill_house_request = models.ForeignKey(
        KillHouseRequest,
        on_delete=models.CASCADE,
        related_name="cancel_request",
        null=True
    )

    message = models.TextField(null=True)

    def save(self, *args, **kwargs):
        super(DriverRequestCancel, self).save(*args, **kwargs)


class PaymentDeadLine(BaseModel):
    days = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(PaymentDeadLine, self).save(*args, **kwargs)


class MonthlyProfitPercentage(BaseModel):
    days = models.IntegerField(default=0)
    percent = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(MonthlyProfitPercentage, self).save(*args, **kwargs)


class PercentageOfLosses(BaseModel):
    houre = models.IntegerField(default=0)
    percent = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(PercentageOfLosses, self).save(*args, **kwargs)


class KillHousePercentageOfLosses(BaseModel):
    percent = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(KillHousePercentageOfLosses, self).save(*args, **kwargs)


class KillHouseComplaint(BaseModel):
    bar = models.ForeignKey(
        KillHouseAssignmentInformation,
        on_delete=models.CASCADE,
        related_name="bar_information",
        null=True
    )
    title = models.CharField(max_length=200, null=True)
    description = models.TextField()
    image = models.JSONField(null=True)
    percent = models.FloatField(default=0)
    state = models.CharField(max_length=20, default='pending')
    message = models.CharField(max_length=20, null=True)
    registrar = models.JSONField(null=True)

    def save(self, *args, **kwargs):
        super(KillHouseComplaint, self).save(*args, **kwargs)


class CheckKillHouseComplaint(BaseModel):
    camplaint = models.ForeignKey(
        KillHouseComplaint,
        on_delete=models.CASCADE,
        related_name="check_complaint",
        null=True
    )
    state = models.CharField(max_length=20, null=True)
    message = models.CharField(max_length=20, null=True)

    def save(self, *args, **kwargs):
        super(CheckKillHouseComplaint, self).save(*args, **kwargs)


class Itransaction(BaseModel):
    wallet = models.ForeignKey(Wallet,
                               related_name="itranseaction_wallet",
                               on_delete=models.CASCADE,
                               null=True)
    transfer_amount = models.FloatField(null=True)
    transaction_type = models.CharField(max_length=20, null=True)
    payment_code = models.BigIntegerField(null=True, default=0)
    state = models.CharField(max_length=20, default='pending')
    make_location_state = models.CharField(max_length=20, null=True)
    file_id = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(Itransaction, self).save(*args, **kwargs)


class Debt(BaseModel):
    cost = models.IntegerField(null=True)
    description = models.TextField(null=True)
    image = models.CharField(max_length=300, null=True)
    itransaction = models.ForeignKey(Itransaction,
                                     related_name="debtor_itransaction",
                                     on_delete=models.CASCADE,
                                     null=True)

    def save(self, *args, **kwargs):
        super(Debt, self).save(*args, **kwargs)


class Penalty(BaseModel):
    cost = models.IntegerField(null=True)
    description = models.TextField(null=True)
    image = models.CharField(max_length=300, null=True)
    itransaction = models.ForeignKey(Itransaction,
                                     related_name="penalty_itransaction",
                                     on_delete=models.CASCADE,
                                     null=True)

    def save(self, *args, **kwargs):
        super(Penalty, self).save(*args, **kwargs)


class Deposit(BaseModel):
    cost = models.IntegerField(null=True)
    description = models.TextField(null=True)
    image = models.CharField(max_length=300, null=True)
    itransaction = models.ForeignKey(Itransaction,
                                     related_name="deposit_itransaction",
                                     on_delete=models.CASCADE,
                                     null=True)

    def save(self, *args, **kwargs):
        super(Deposit, self).save(*args, **kwargs)


class FinancialDocument(BaseModel):
    transaction_details = models.JSONField(null=True)
    type = models.CharField(max_length=30, null=True)
    cost = models.FloatField(null=True)
    description = models.TextField(null=True)
    payment_code = models.IntegerField(null=True)
    image = models.JSONField(null=True)
    wallet = models.ForeignKey(Wallet,
                               related_name="financial_wallet",
                               on_delete=models.CASCADE,
                               null=True)

    def save(self, *args, **kwargs):
        super(FinancialDocument, self).save(*args, **kwargs)


class FinancialTransaction(BaseModel):
    type = models.CharField(max_length=50, null=True)
    cost = models.IntegerField(null=True)
    description = models.TextField(null=True)
    image = models.JSONField(null=True)
    itransaction = models.ForeignKey(Itransaction,
                                     related_name="financial_transaction",
                                     on_delete=models.CASCADE,
                                     null=True)

    def save(self, *args, **kwargs):
        super(FinancialTransaction, self).save(*args, **kwargs)


class Admin(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        null=True,
        related_name="user_profile_admin"
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="admin_address",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    admin_id_key = models.IntegerField(null=True)
    identity_documents = models.JSONField(null=True)
    active = models.BooleanField(default=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(Admin, self).save(*args, **kwargs)


class CheckUnusualCasualties(BaseModel):
    state = models.CharField(max_length=50, null=True)
    message = models.TextField(null=True)
    complaint = models.ForeignKey(KillHouseComplaint,
                                  on_delete=models.CASCADE,
                                  null=True,
                                  related_name="losses"
                                  )
    role = models.JSONField(null=True)

    def save(self, *args, **kwargs):
        super(CheckUnusualCasualties, self).save(*args, **kwargs)


# مدل کاربر جهاد
class Jahad(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="jahad_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="jahad_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="jahad_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="jahad_wallet",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    jahad_id_key = models.IntegerField(null=True)
    identity_documents = models.JSONField(null=True)
    active = models.BooleanField(default=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(Jahad, self).save(*args, **kwargs)


class CityJahad(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="city_jahad_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="city_jahad_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="city_jahad_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="city_jahad_wallet",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    city_jahad_id_key = models.IntegerField(null=True)
    identity_documents = models.JSONField(null=True)
    active = models.BooleanField(default=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(CityJahad, self).save(*args, **kwargs)


class CityPoultry(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="city_poultry_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="city_poultry_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="city_poultry_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="city_poultry_wallet",
        null=True
    )

    def save(self, *args, **kwargs):
        super(CityPoultry, self).save(*args, **kwargs)


class ProteinGuild(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="protein_guild_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="protein_guild_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="protein_guild_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="protein_guild_wallet",
        null=True
    )

    def save(self, *args, **kwargs):
        super(ProteinGuild, self).save(*args, **kwargs)


# مدل کاربر استانداری
class ProvincialGovernment(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="government_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="government_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="government_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="government_wallet",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    provincial_government_id_key = models.IntegerField(null=True)
    identity_documents = models.JSONField(null=True)
    active = models.BooleanField(default=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(ProvincialGovernment, self).save(*args, **kwargs)


class VetSupervisor(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="supervisor_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="supervisor_address",
        null=True
    )
    user_bank_info = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="supervisor_bank_info",
                                       null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="supervisor_wallet",
        null=True
    )
    vet = models.ForeignKey(
        Vet,
        on_delete=models.CASCADE,
        related_name="vet_supervisor",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    vet_id_foreign_key = models.IntegerField(null=True)
    vet_superviser_id_key = models.IntegerField(null=True)
    wallet_amount = models.BigIntegerField(default=0)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(VetSupervisor, self).save(*args, **kwargs)


class CityVet(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="city_vet_user",
        null=True
    )
    vet = models.ForeignKey(
        Vet,
        on_delete=models.CASCADE,
        related_name="vet_city_vet",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="city_vet_address",
        null=True
    )
    user_bank_info = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="city_vet_bank_info",
                                       null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="city_vet_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(CityVet, self).save(*args, **kwargs)


class LiveStockSupport(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="live_stock_support_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="live_stock_support_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="live_stock_support_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="live_stock_support_wallet",
        null=True
    )
    identity_documents = models.JSONField(null=True)
    active = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(LiveStockSupport, self).save(*args, **kwargs)


class Commerce(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="commerce_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="commerce_address",
        null=True
    )
    user_bank_info = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="commerce_bank_info",
                                       null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="commerce_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(Commerce, self).save(*args, **kwargs)


class CityCommerce(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="city_commerce_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="city_commerce_address",
        null=True
    )
    user_bank_info = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="city_commerce_bank_info",
                                       null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="city_commerce_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)


class ProvinceSupervisor(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="province_supervisor_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="province_supervisor_address",
        null=True
    )
    user_bank_info = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="province_supervisor_bank_info",
                                       null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="province_supervisor_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)


class CitySupervisor(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="city_supervisor_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="city_supervisor_address",
        null=True
    )
    user_bank_info = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="city_supervisor_bank_info",
                                       null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="city_supervisor_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(CitySupervisor, self).save(*args, **kwargs)


class JahadInspector(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="jahad_inspector_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="jahad_inspector_address",
        null=True
    )
    user_bank_info = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="jahad_inspector_bank_info",
                                       null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="jahad_inspector_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(JahadInspector, self).save(*args, **kwargs)


class VetCheckAllocations(BaseModel):
    vet = models.ForeignKey(
        Vet,
        on_delete=models.CASCADE,
        related_name="vet_check",
        null=True
    )

    kill_house_request = models.ForeignKey(
        KillHouseRequest,
        on_delete=models.CASCADE,
        related_name="check_kill_house_request",
        null=True
    )
    vet_id_foreign_key = models.IntegerField(null=True)
    kill_house_request_id_foreign_key = models.IntegerField(null=True)
    vet_chick_allocations_id_key = models.IntegerField(null=True)

    #
    # province_kill_request = models.ForeignKey(
    #     ProvinceKillRequest,
    #     on_delete=models.CASCADE,
    #     related_name="check_province_kill_request",
    #     null=True
    # )
    # poultry_request = models.ForeignKey(
    #     PoultryRequest,
    #     on_delete=models.CASCADE,
    #     related_name="vet_check_poultry_request",
    #     null=True
    # )
    state = models.CharField(max_length=50, default='pending')
    reviewer = models.JSONField(null=True)

    def save(self, *args, **kwargs):
        super(VetCheckAllocations, self).save(*args, **kwargs)


class HourLimit(BaseModel):
    hour = models.IntegerField(null=True)
    active = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(HourLimit, self).save(*args, **kwargs)


class KillHouseHourLimit(BaseModel):
    hour = models.IntegerField(null=True)
    active = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(KillHouseHourLimit, self).save(*args, **kwargs)


class SmsLicense(BaseModel):
    hatching = models.BooleanField(default=False)
    poultry_request = models.BooleanField(default=False)
    city_approval = models.BooleanField(default=False)
    province_approval = models.BooleanField(default=False)
    kill_request = models.BooleanField(default=False)
    province_kill_request = models.BooleanField(default=False)
    farm_veterinarian_approval = models.BooleanField(default=False)
    kill_house_request = models.BooleanField(default=False)
    kill_house_house_veterinarian_approval = models.BooleanField(default=False)
    assingment_information = models.BooleanField(default=False)
    confirmation_of_assingment_information = models.BooleanField(default=False)
    invoicing = models.BooleanField(default=False)
    invoice_payment = models.BooleanField(default=False)
    invoice_payment_confirmation = models.BooleanField(default=False)
    inspector_approval = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(SmsLicense, self).save(*args, **kwargs)


class KillHouseCreditors(BaseModel):
    amount = models.FloatField(default=0)
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_creditors",
        null=True
    )

    def save(self, *args, **kwargs):
        super(KillHouseCreditors, self).save(*args, **kwargs)


class ProvinceAllowKillHouseRegisterCar(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="province_allow_kill_house_register",
        null=True
    )
    kill_house_id_foreign_key = models.IntegerField(null=True)
    province_allow_kill_house_register_car_id_key = models.IntegerField(null=True)
    allow = models.BooleanField(default=False)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(ProvinceAllowKillHouseRegisterCar, self).save(*args, **kwargs)


class ProvinceAllowPoultryChooseKillHouse(BaseModel):
    poultry = models.ForeignKey(
        Poultry,
        on_delete=models.CASCADE,
        related_name="province_allow_kill_house_register",
        null=True
    )
    poultry_id_foreign_key = models.IntegerField(null=True)
    province_allow_poultry_choose_kill_house_id_key = models.IntegerField(null=True)
    allow = models.BooleanField(default=False)
    mandatory = models.BooleanField(default=False)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(ProvinceAllowPoultryChooseKillHouse, self).save(*args, **kwargs)


class ProvinceAllowPoultryChooseKillHouseTotal(BaseModel):
    allow = models.BooleanField(default=False)
    mandatory = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(ProvinceAllowPoultryChooseKillHouseTotal, self).save(*args, **kwargs)


class FreeSaleWithinprovince(BaseModel):
    allow = models.BooleanField(default=False)
    type = models.CharField(max_length=20, null=True)
    weight = models.FloatField(default=0)
    percent = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(FreeSaleWithinprovince, self).save(*args, **kwargs)


class ProvinceAllowPoultrySellFree(BaseModel):
    poultry = models.ForeignKey(
        Poultry,
        on_delete=models.CASCADE,
        related_name="province_allow_poultry_sell_free",
        null=True
    )
    poultry_id_foreign_key = models.IntegerField(null=True)
    province_allow_poultry_sell_free_id_key = models.IntegerField(null=True)
    allow = models.BooleanField(default=False)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(ProvinceAllowPoultrySellFree, self).save(*args, **kwargs)


class ProvinceAllowPoultrySellFreeTotal(BaseModel):
    allow = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(ProvinceAllowPoultrySellFreeTotal, self).save(*args, **kwargs)


class ProvinceAllowKillHouseDirectBuying(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="province_allow_kill_house_direct_buying",
        null=True
    )
    allow = models.BooleanField(default=False)
    export_status = models.BooleanField(default=False)
    freezing_status = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(ProvinceAllowKillHouseDirectBuying, self).save(*args, **kwargs)


class ProvinceAllowKillHouseDirectBuyingTotal(BaseModel):
    allow = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(ProvinceAllowKillHouseDirectBuyingTotal, self).save(*args, **kwargs)


class ProvinceAllowKillHouseRegisterGuilds(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="province_allow_kill_house_register_guilds",
        null=True
    )
    allow = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(ProvinceAllowKillHouseRegisterGuilds, self).save(*args, **kwargs)


class ProvinceAllowKillHouseRegisterGuildsTotal(BaseModel):
    allow = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(ProvinceAllowKillHouseRegisterGuildsTotal, self).save(*args, **kwargs)


class KillHouseDailyQuota(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="percent_killhouse_daily_quota",
        null=True)
    kill_request = models.ForeignKey(
        KillRequest,
        on_delete=models.CASCADE,
        related_name="percent_kill_request_daily_quota",
        null=True)
    killer_kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="killer_kill_house_daily_quota",
        null=True)

    kill_house_id_foreign_key = models.IntegerField(null=True)
    kill_request_id_foreign_key = models.IntegerField(null=True)
    killer_kill_house_id_foreign_key = models.IntegerField(null=True)
    kill_house_daily_quota_id_key = models.IntegerField(null=True)
    percent = models.FloatField(default=0)
    quantity = models.BigIntegerField(default=0)
    remain_quantity = models.BigIntegerField(default=0)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(KillHouseDailyQuota, self).save(*args, **kwargs)


# class ProvinceAutomaticAllocation(BaseModel):
#     daily_quota = models.ForeignKey(
#         KillHouseDailyQuota,
#         on_delete=models.CASCADE,
#         related_name="percent_killhouse_daily_quota_province",
#         null=True)
#
#     poultry_request = models.ForeignKey(
#         PoultryRequest,
#         on_delete=models.CASCADE,
#         related_name="poultry_request_quantity_province",
#         null=True)
#     quantity = models.BigIntegerField(default=0)
#
#     def save(self, *args, **kwargs):
#         super(ProvinceAutomaticAllocation, self).save(*args, **kwargs)


class ProvinceAutoAllocation(BaseModel):
    daily_quota = models.ForeignKey(
        KillHouseDailyQuota,
        on_delete=models.CASCADE,
        related_name="percent_auto_killhouse_daily_quota_province",
        null=True)

    poultry_request = models.ForeignKey(
        PoultryRequest,
        on_delete=models.CASCADE,
        related_name="poultry_request_auto_quantity_province",
        null=True)
    province_kill_request = models.ForeignKey(
        ProvinceKillRequest,
        on_delete=models.CASCADE,
        related_name="province_kill_request_auto_quantity_province",
        null=True)

    daily_quota_id_foreign_key = models.IntegerField(null=True)
    poultry_request_id_foreign_key = models.IntegerField(null=True)
    province_kill_request_id_foreign_key = models.IntegerField(null=True)
    province_auto_allocation_id_key = models.IntegerField(null=True)

    unauthorized_number = models.BooleanField(default=False)

    quantity = models.BigIntegerField(default=0)
    allocation_order_code = models.BigIntegerField(default=0)
    state = models.CharField(max_length=50, default='temporary')
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(ProvinceAutoAllocation, self).save(*args, **kwargs)


class AutoAcceptProvinceKillRequest(BaseModel):
    allow = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(AutoAcceptProvinceKillRequest, self).save(*args, **kwargs)


class AutoMakeKillHouseRequest(BaseModel):
    allow = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(AutoMakeKillHouseRequest, self).save(*args, **kwargs)


class Hall(models.Model):
    poultry = models.ForeignKey(
        Poultry,
        on_delete=models.CASCADE,
        related_name="poultry_hall",
        null=True
    )

    hall_number = models.IntegerField(default=1)

    def save(self, *args, **kwargs):
        super(Hall, self).save(*args, **kwargs)


class PoultryHallProfile(models.Model):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="user_poultry_profile",
        null=True
    )
    hall = models.ForeignKey(
        Hall,
        on_delete=models.CASCADE,
        related_name="hall_poultry_profile",
        null=True
    )
    start_date = models.DateTimeField(null=True)
    end_date = models.DateTimeField(null=True)

    def save(self, *args, **kwargs):
        super(PoultryHallProfile, self).save(*args, **kwargs)


class TypeActivity(BaseModel):
    title = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(TypeActivity, self).save(*args, **kwargs)


class AreaActivity(BaseModel):
    title = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(AreaActivity, self).save(*args, **kwargs)


class GuildSteward(BaseModel):
    guild = models.ForeignKey('Guilds', on_delete=models.CASCADE, related_name='guild_to_stewards')
    steward = models.ForeignKey('Guilds', on_delete=models.CASCADE, related_name='steward_to_guilds')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('guild', 'steward')

    def save(self, *args, **kwargs):
        super(GuildSteward, self).save(*args, **kwargs)


class DistributionUserLevel(BaseModel):
    fa_title = models.CharField(max_length=200, null=True)
    en_title = models.CharField(max_length=200, null=True)

    def save(self, *args, **kwargs):
        super(DistributionUserLevel, self).save(*args, **kwargs)


class GuildsGeneralConfigs(BaseModel):
    active = models.BooleanField(default=False)
    activator = models.CharField(max_length=200, null=True)
    out_province_governmental_selling_percent = models.FloatField(default=0)
    out_province_free_selling_percent = models.FloatField(default=0)
    out_province_free_buying_commitment_percent = models.FloatField(default=0)
    in_province_governmental_selling_percent = models.FloatField(default=0)
    in_province_free_selling_percent = models.FloatField(default=0)
    segmentation_governmental_percent = models.FloatField(default=0)
    segmentation_free_selling_percent = models.FloatField(default=0)
    cold_house_governmental_percent = models.FloatField(default=0)
    cold_house_free_percent = models.FloatField(default=0)
    governmental_selling_permission = models.BooleanField(default=False)
    free_selling_permission = models.BooleanField(default=False)
    free_sale_form_governmental_quota = models.BooleanField(default=False)
    free_sale_from_free_quota_in_province = models.BooleanField(default=False)
    total_input_warehouse_weight = models.FloatField(default=0)
    total_input_warehouse_governmental_weight = models.FloatField(default=0)
    total_input_warehouse_free_weight = models.FloatField(default=0)
    total_selling_warehouse_governmental_weight = models.FloatField(default=0)
    total_selling_warehouse_free_weight = models.FloatField(default=0)
    total_remain_warehouse_governmental_weight = models.FloatField(default=0)
    total_remain_warehouse_free_weight = models.FloatField(default=0)
    total_remain_warehouse_weight = models.FloatField(default=0)
    total_selling_in_province_governmental_weight = models.FloatField(default=0)
    total_selling_in_province_free_weight = models.FloatField(default=0)
    total_selling_out_province_governmental_weight = models.FloatField(default=0)
    total_selling_out_province_free_weight = models.FloatField(default=0)
    total_segmentation_governmental_weight = models.FloatField(default=0)
    total_segmentation_free_weight = models.FloatField(default=0)
    total_in_province_governmental_bars_weight = models.FloatField(default=0)
    total_in_province_free_bars_weight = models.FloatField(default=0)
    total_out_province_buying_bars_weight = models.FloatField(default=0)
    total_selling_warehouse_weight = models.FloatField(default=0)
    total_segmentation_weight = models.FloatField(default=0)
    total_cold_house_weight = models.FloatField(default=0)
    total_cold_house_governmental_weight = models.FloatField(default=0)
    total_cold_house_free_weight = models.FloatField(default=0)
    total_commitment_governmental_weight = models.FloatField(default=0)
    total_commitment_free_weight = models.FloatField(default=0)
    total_in_province_governmental_bars_commitment_weight = models.FloatField(default=0)
    total_in_province_free_bars_commitment_weight = models.FloatField(default=0)
    total_out_province_buying_bars_commitment_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_governmental_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_free_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_governmental_remain_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_free_remain_weight = models.FloatField(default=0)
    total_commitment_selling_out_province_governmental_weight = models.FloatField(default=0)
    total_commitment_selling_out_province_free_weight = models.FloatField(default=0)
    total_commitment_cold_house_governmental_weight = models.FloatField(default=0)
    total_commitment_cold_house_free_weight = models.FloatField(default=0)
    total_commitment_segmentation_governmental_weight = models.FloatField(default=0)
    total_commitment_segmentation_free_weight = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(GuildsGeneralConfigs, self).save(*args, **kwargs)
class Steward(BaseModel):
    guilds = models.ForeignKey(
        'Guilds',
        on_delete=models.CASCADE,
        related_name="steward_guilds",
        null=True
    )
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="steward_user",
        null=True
    )
    kill_house = models.ManyToManyField(
        KillHouse,
        related_name='stewards_kill_houses',
        null=True, blank=True)
    name = models.CharField(max_length=200,null=True)
    active = models.BooleanField(default=True)
    allocation_limit = models.BigIntegerField(null=True)
    limitation_allocation = models.BooleanField(default=False)
    centers_allocation = models.JSONField(null=True)
    kill_house_register = models.BooleanField(default=False)
    province_accept_state = models.CharField(max_length=20, default='accepted')
    city = models.CharField(max_length=100, null=True)
    out_province_free_selling_percent = models.FloatField(default=0)
    out_province_free_buying_commitment_percent = models.FloatField(default=0)
    in_province_governmental_selling_percent = models.FloatField(default=0)
    in_province_free_selling_percent = models.FloatField(default=0)
    segmentation_governmental_percent = models.FloatField(default=0)
    segmentation_free_selling_percent = models.FloatField(default=0)
    cold_house_governmental_percent = models.FloatField(default=0)
    cold_house_free_percent = models.FloatField(default=0)
    governmental_selling_permission = models.BooleanField(default=False)
    free_selling_permission = models.BooleanField(default=False)
    free_sale_form_governmental_quota = models.BooleanField(default=False)
    free_sale_from_free_quota_in_province = models.BooleanField(default=False)
    total_input_warehouse_weight = models.FloatField(default=0)
    total_input_warehouse_governmental_weight = models.FloatField(default=0)
    total_input_warehouse_free_weight = models.FloatField(default=0)
    total_selling_warehouse_governmental_weight = models.FloatField(default=0)
    total_selling_warehouse_free_weight = models.FloatField(default=0)
    total_remain_warehouse_governmental_weight = models.FloatField(default=0)
    total_remain_warehouse_free_weight = models.FloatField(default=0)
    total_remain_warehouse_weight = models.FloatField(default=0)
    total_selling_in_province_governmental_weight = models.FloatField(default=0)
    total_selling_in_province_free_weight = models.FloatField(default=0)
    total_selling_out_province_governmental_weight = models.FloatField(default=0)
    total_selling_out_province_free_weight = models.FloatField(default=0)
    total_segmentation_governmental_weight = models.FloatField(default=0)
    total_segmentation_free_weight = models.FloatField(default=0)
    total_in_province_governmental_bars_weight = models.FloatField(default=0)
    total_in_province_free_bars_weight = models.FloatField(default=0)
    total_out_province_buying_bars_weight = models.FloatField(default=0)
    pos_allocated_weight = models.FloatField(default=0)
    pos_governmental_allocated_weight = models.FloatField(default=0)
    pos_free_allocated_weight = models.FloatField(default=0)
    total_selling_warehouse_weight = models.FloatField(default=0)
    total_segmentation_weight = models.FloatField(default=0)
    total_cold_house_weight = models.FloatField(default=0)
    total_cold_house_governmental_weight = models.FloatField(default=0)
    total_cold_house_free_weight = models.FloatField(default=0)
    ware_house_archive_weight = models.FloatField(default=0)
    ware_house_archive_governmental_weight = models.FloatField(default=0)
    ware_house_archive_free_weight = models.FloatField(default=0)
    total_commitment_governmental_weight = models.FloatField(default=0)
    total_commitment_free_weight = models.FloatField(default=0)
    total_in_province_governmental_bars_commitment_weight = models.FloatField(default=0)
    total_in_province_free_bars_commitment_weight = models.FloatField(default=0)
    total_out_province_buying_bars_commitment_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_governmental_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_free_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_governmental_remain_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_free_remain_weight = models.FloatField(default=0)
    total_commitment_selling_out_province_governmental_weight = models.FloatField(default=0)
    total_commitment_selling_out_province_free_weight = models.FloatField(default=0)
    total_commitment_cold_house_governmental_weight = models.FloatField(default=0)
    total_commitment_cold_house_free_weight = models.FloatField(default=0)
    total_commitment_segmentation_governmental_weight = models.FloatField(default=0)
    total_commitment_segmentation_free_weight = models.FloatField(default=0)
    license_type = models.CharField(max_length=100, null=True)  # نوع مجوز
    license_status = models.CharField(max_length=50, null=True)  # وضعیت مجوز
    license_issue_date = models.DateTimeField(null=True)  # تاریخ صدور مجوز
    license_expire_date = models.DateTimeField(null=True)  # تاریخ انقضا مجوز
    company_name = models.CharField(max_length=200, null=True)  # نام شرکت
    union_name = models.CharField(max_length=200, null=True)
    national_code = models.CharField(max_length=200, null=True)
    phone_number = models.CharField(max_length=200, null=True)
    company_identifier = models.CharField(max_length=100, null=True)  # شناسه شرکت
    is_foreign_national = models.BooleanField(default=False)  # آیا اتباع است
    has_partner = models.BooleanField(default=False)  # آیا شریک دارد
    has_inquiry = models.BooleanField(default=False)  # آیا استعلام دارد
    is_real_person = models.BooleanField(default=True)  # حقیقی یا حقوقی

    # province_accept = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        self.total_segmentation_weight = self.total_segmentation_governmental_weight + self.total_segmentation_free_weight
        self.total_cold_house_weight = self.total_cold_house_governmental_weight + self.total_cold_house_free_weight
        self.total_input_warehouse_governmental_weight = self.total_in_province_governmental_bars_weight
        self.total_input_warehouse_free_weight = self.total_in_province_free_bars_weight + self.total_out_province_buying_bars_weight
        self.total_input_warehouse_weight = self.total_input_warehouse_governmental_weight + self.total_input_warehouse_free_weight
        self.total_selling_warehouse_governmental_weight = self.total_selling_in_province_governmental_weight + self.total_selling_out_province_governmental_weight + self.total_segmentation_governmental_weight + self.total_cold_house_governmental_weight + self.pos_governmental_allocated_weight
        self.total_selling_warehouse_free_weight = self.total_selling_in_province_free_weight + self.total_selling_out_province_free_weight + self.total_segmentation_free_weight + self.total_cold_house_free_weight + self.pos_free_allocated_weight
        self.total_selling_warehouse_weight = self.total_selling_warehouse_governmental_weight + self.total_selling_warehouse_free_weight
        self.total_remain_warehouse_governmental_weight = (
                                                                  self.total_input_warehouse_governmental_weight - self.total_selling_warehouse_governmental_weight) - self.ware_house_archive_governmental_weight
        self.total_remain_warehouse_free_weight = (
                                                          self.total_input_warehouse_free_weight - self.total_selling_warehouse_free_weight) - self.ware_house_archive_free_weight
        self.total_remain_warehouse_weight = self.total_input_warehouse_weight - self.total_selling_warehouse_weight
        self.total_commitment_selling_in_province_governmental_weight = int(
            self.total_input_warehouse_governmental_weight * (self.in_province_governmental_selling_percent / 100))
        self.total_commitment_selling_out_province_governmental_weight = int((
                                                                                     self.out_province_governmental_selling_percent / 100) * self.total_input_warehouse_governmental_weight) if self.governmental_selling_permission else 0
        self.total_commitment_cold_house_governmental_weight = int((
                                                                           self.cold_house_governmental_percent / 100) * self.total_input_warehouse_governmental_weight) if self.governmental_selling_permission else 0
        self.total_commitment_segmentation_governmental_weight = int((
                                                                             self.segmentation_governmental_percent / 100) * self.total_input_warehouse_governmental_weight) if self.governmental_selling_permission else 0
        self.total_commitment_selling_in_province_free_weight = int(
            self.total_input_warehouse_free_weight * (self.in_province_free_selling_percent / 100))
        self.total_commitment_selling_out_province_free_weight = int((
                                                                             self.out_province_free_selling_percent / 100) * self.total_input_warehouse_free_weight) if self.free_selling_permission else 0
        self.total_commitment_cold_house_free_weight = int((
                                                                   self.cold_house_free_percent / 100) * self.total_input_warehouse_free_weight) if self.free_selling_permission else 0
        self.total_commitment_segmentation_free_weight = int((
                                                                     self.segmentation_free_selling_percent / 100) * self.total_input_warehouse_free_weight) if self.free_selling_permission else 0
        self.total_commitment_selling_in_province_governmental_remain_weight = self.total_commitment_selling_in_province_governmental_weight - self.total_selling_in_province_governmental_weight if (
                                                                                                                                                                                                             self.total_commitment_selling_in_province_governmental_weight - self.total_selling_in_province_governmental_weight) > 0 else 0
        self.total_commitment_selling_in_province_free_remain_weight = self.total_commitment_selling_in_province_free_weight - self.total_selling_in_province_free_weight if (
                                                                                                                                                                                     self.total_commitment_selling_in_province_free_weight - self.total_selling_in_province_free_weight) > 0 else 0
        super(Steward, self).save(*args, **kwargs)


class Guilds(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="guilds_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="guilds_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="guilds_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="guilds_wallet",
        null=True
    )
    guild_area_activity = models.ForeignKey(
        AreaActivity,
        on_delete=models.CASCADE,
        related_name="guilds_area_activity",
        null=True
    )
    guild_type_activity = models.ForeignKey(
        TypeActivity,
        on_delete=models.CASCADE,
        related_name="guilds_type_activity",
        null=True
    )
    cars = models.ManyToManyField(
        Car,
        related_name='guild_car',
        null=True, blank=True)
    kill_house = models.ManyToManyField(
        KillHouse,
        related_name='guild_kill_houses',
        null=True, blank=True)
    guilds_stewards = models.ManyToManyField(
        Steward,
        related_name='guilds_direct_stewards',
        null=True, blank=True)
    steward_kill_house = models.ManyToManyField(
        KillHouse,
        related_name='steward_kill_houses',
        null=True, blank=True)
    stewards = models.ManyToManyField(
        'self',
        through='GuildSteward',
        symmetrical=False,
        related_name='guild_stewards', blank=True)
    user_level = models.ManyToManyField(
        DistributionUserLevel,
        related_name='guild_user_level',
        null=True, blank=True)
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    provincial_government_id_key = models.IntegerField(null=True)
    identity_documents = models.JSONField(null=True)
    active = models.BooleanField(default=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    guilds_id = models.CharField(max_length=50, null=True)
    license_number = models.CharField(max_length=50, null=True)
    guilds_name = models.CharField(max_length=100, null=True)
    phone = models.CharField(max_length=100, null=True)
    type_activity = models.CharField(max_length=100, null=True)
    area_activity = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)
    steward = models.BooleanField(default=False)
    has_pos = models.BooleanField(default=False)
    centers_allocation = models.JSONField(null=True)
    kill_house_centers_allocation = models.JSONField(null=True)
    allocation_limit = models.BigIntegerField(null=True)
    limitation_allocation = models.BooleanField(default=False)
    # province_accept = models.BooleanField(default=True)
    registerar_role = models.CharField(max_length=50, null=True)
    registerar_fullname = models.CharField(max_length=100, null=True)
    registerar_mobile = models.CharField(max_length=11, null=True)
    kill_house_register = models.BooleanField(default=False)
    steward_register = models.BooleanField(default=False)
    guilds_room_register = models.BooleanField(default=False)
    pos_company_register = models.BooleanField(default=False)
    province_accept_state = models.CharField(max_length=20, default='accepted')
    province_message = models.CharField(max_length=500, null=True)
    condition = models.CharField(max_length=500, null=True)
    description_condition = models.TextField(null=True)
    steward_active = models.BooleanField(default=True)
    steward_allocation_limit = models.BigIntegerField(null=True)
    steward_limitation_allocation = models.BooleanField(default=False)
    license = models.BooleanField(default=False)
    license_form = models.CharField(max_length=500, null=True)
    license_file = models.CharField(max_length=500, null=True)
    reviewer_role = models.CharField(max_length=50, null=True)
    reviewer_fullname = models.CharField(max_length=100, null=True)
    reviewer_mobile = models.CharField(max_length=11, null=True)
    checker_message = models.TextField(null=True)
    final_accept = models.BooleanField(default=False)
    temporary_registration = models.BooleanField(default=False)
    role_activation = models.CharField(max_length=100, null=True)
    register_code = models.CharField(max_length=100, null=True)
    logged_register_code = models.CharField(max_length=100, null=True)
    active_register_code = models.BooleanField(default=False)
    expire_time_register_code = models.DateTimeField(null=True)
    register_date_register_code = models.DateTimeField(null=True)
    is_registered = models.BooleanField(default=False)
    out_province_governmental_selling_percent = models.FloatField(default=0)
    out_province_free_selling_percent = models.FloatField(default=0)
    out_province_free_buying_commitment_percent = models.FloatField(default=0)
    in_province_governmental_selling_percent = models.FloatField(default=0)
    in_province_free_selling_percent = models.FloatField(default=0)
    segmentation_governmental_percent = models.FloatField(default=0)
    segmentation_free_selling_percent = models.FloatField(default=0)
    cold_house_governmental_percent = models.FloatField(default=0)
    cold_house_free_percent = models.FloatField(default=0)
    governmental_selling_permission = models.BooleanField(default=False)
    free_selling_permission = models.BooleanField(default=False)
    free_sale_form_governmental_quota = models.BooleanField(default=False)
    free_sale_from_free_quota_in_province = models.BooleanField(default=False)
    total_input_warehouse_weight = models.FloatField(default=0)
    total_input_warehouse_governmental_weight = models.FloatField(default=0)
    total_input_warehouse_free_weight = models.FloatField(default=0)
    total_selling_warehouse_governmental_weight = models.FloatField(default=0)
    total_selling_warehouse_free_weight = models.FloatField(default=0)
    total_remain_warehouse_governmental_weight = models.FloatField(default=0)
    total_remain_warehouse_free_weight = models.FloatField(default=0)
    total_remain_warehouse_weight = models.FloatField(default=0)
    total_selling_in_province_governmental_weight = models.FloatField(default=0)
    total_selling_in_province_free_weight = models.FloatField(default=0)
    total_selling_out_province_governmental_weight = models.FloatField(default=0)
    total_selling_out_province_free_weight = models.FloatField(default=0)
    total_segmentation_governmental_weight = models.FloatField(default=0)
    total_segmentation_free_weight = models.FloatField(default=0)
    total_in_province_governmental_bars_weight = models.FloatField(default=0)
    total_in_province_free_bars_weight = models.FloatField(default=0)
    total_out_province_buying_bars_weight = models.FloatField(default=0)
    pos_allocated_weight = models.FloatField(default=0)
    pos_governmental_allocated_weight = models.FloatField(default=0)
    pos_free_allocated_weight = models.FloatField(default=0)
    total_selling_warehouse_weight = models.FloatField(default=0)
    total_segmentation_weight = models.FloatField(default=0)
    total_cold_house_weight = models.FloatField(default=0)
    total_cold_house_governmental_weight = models.FloatField(default=0)
    total_cold_house_free_weight = models.FloatField(default=0)
    ware_house_archive_weight = models.FloatField(default=0)
    ware_house_archive_governmental_weight = models.FloatField(default=0)
    ware_house_archive_free_weight = models.FloatField(default=0)
    total_commitment_governmental_weight = models.FloatField(default=0)
    total_commitment_free_weight = models.FloatField(default=0)
    total_in_province_governmental_bars_commitment_weight = models.FloatField(default=0)
    total_in_province_free_bars_commitment_weight = models.FloatField(default=0)
    total_out_province_buying_bars_commitment_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_governmental_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_free_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_governmental_remain_weight = models.FloatField(default=0)
    total_commitment_selling_in_province_free_remain_weight = models.FloatField(default=0)
    total_commitment_selling_out_province_governmental_weight = models.FloatField(default=0)
    total_commitment_selling_out_province_free_weight = models.FloatField(default=0)
    total_commitment_cold_house_governmental_weight = models.FloatField(default=0)
    total_commitment_cold_house_free_weight = models.FloatField(default=0)
    total_commitment_segmentation_governmental_weight = models.FloatField(default=0)
    total_commitment_segmentation_free_weight = models.FloatField(default=0)
    license_type = models.CharField(max_length=100, null=True)  # نوع مجوز
    license_status = models.CharField(max_length=50, null=True)  # وضعیت مجوز
    license_issue_date = models.DateTimeField(null=True)  # تاریخ صدور مجوز
    license_expire_date = models.DateTimeField(null=True)  # تاریخ انقضا مجوز
    company_name = models.CharField(max_length=200, null=True)  # نام شرکت
    union_name = models.CharField(max_length=200, null=True)
    national_code = models.CharField(max_length=200, null=True)
    phone_number = models.CharField(max_length=200, null=True)
    company_identifier = models.CharField(max_length=100, null=True)  # شناسه شرکت
    is_foreign_national = models.BooleanField(default=False)  # آیا اتباع است
    has_partner = models.BooleanField(default=False)  # آیا شریک دارد
    has_inquiry = models.BooleanField(default=False)  # آیا استعلام دارد
    is_real_person = models.BooleanField(default=True)  # حقیقی یا حقوقی

    def save(self, *args, **kwargs):
        self.total_segmentation_weight = self.total_segmentation_governmental_weight + self.total_segmentation_free_weight
        self.total_cold_house_weight = self.total_cold_house_governmental_weight + self.total_cold_house_free_weight
        self.total_input_warehouse_governmental_weight = self.total_in_province_governmental_bars_weight
        self.total_input_warehouse_free_weight = self.total_in_province_free_bars_weight + self.total_out_province_buying_bars_weight
        self.total_input_warehouse_weight = self.total_input_warehouse_governmental_weight + self.total_input_warehouse_free_weight
        self.total_selling_warehouse_governmental_weight = self.total_selling_in_province_governmental_weight + self.total_selling_out_province_governmental_weight + self.total_segmentation_governmental_weight + self.total_cold_house_governmental_weight + self.pos_governmental_allocated_weight
        self.total_selling_warehouse_free_weight = self.total_selling_in_province_free_weight + self.total_selling_out_province_free_weight + self.total_segmentation_free_weight + self.total_cold_house_free_weight + self.pos_free_allocated_weight
        self.total_selling_warehouse_weight = self.total_selling_warehouse_governmental_weight + self.total_selling_warehouse_free_weight
        self.total_remain_warehouse_governmental_weight = (
                                                                  self.total_input_warehouse_governmental_weight - self.total_selling_warehouse_governmental_weight) - self.ware_house_archive_governmental_weight
        self.total_remain_warehouse_free_weight = (
                                                          self.total_input_warehouse_free_weight - self.total_selling_warehouse_free_weight) - self.ware_house_archive_free_weight
        self.total_remain_warehouse_weight = self.total_input_warehouse_weight - self.total_selling_warehouse_weight
        self.total_commitment_selling_in_province_governmental_weight = int(
            self.total_input_warehouse_governmental_weight * (self.in_province_governmental_selling_percent / 100))
        self.total_commitment_selling_out_province_governmental_weight = int((
                                                                                     self.out_province_governmental_selling_percent / 100) * self.total_input_warehouse_governmental_weight) if self.governmental_selling_permission else 0
        self.total_commitment_cold_house_governmental_weight = int((
                                                                           self.cold_house_governmental_percent / 100) * self.total_input_warehouse_governmental_weight) if self.governmental_selling_permission else 0
        self.total_commitment_segmentation_governmental_weight = int((
                                                                             self.segmentation_governmental_percent / 100) * self.total_input_warehouse_governmental_weight) if self.governmental_selling_permission else 0
        self.total_commitment_selling_in_province_free_weight = int(
            self.total_input_warehouse_free_weight * (self.in_province_free_selling_percent / 100))
        self.total_commitment_selling_out_province_free_weight = int((
                                                                             self.out_province_free_selling_percent / 100) * self.total_input_warehouse_free_weight) if self.free_selling_permission else 0
        self.total_commitment_cold_house_free_weight = int((
                                                                   self.cold_house_free_percent / 100) * self.total_input_warehouse_free_weight) if self.free_selling_permission else 0
        self.total_commitment_segmentation_free_weight = int((
                                                                     self.segmentation_free_selling_percent / 100) * self.total_input_warehouse_free_weight) if self.free_selling_permission else 0
        self.total_commitment_selling_in_province_governmental_remain_weight = self.total_commitment_selling_in_province_governmental_weight - self.total_selling_in_province_governmental_weight if (
                                                                                                                                                                                                             self.total_commitment_selling_in_province_governmental_weight - self.total_selling_in_province_governmental_weight) > 0 else 0
        self.total_commitment_selling_in_province_free_remain_weight = self.total_commitment_selling_in_province_free_weight - self.total_selling_in_province_free_weight if (
                                                                                                                                                                                     self.total_commitment_selling_in_province_free_weight - self.total_selling_in_province_free_weight) > 0 else 0
        super(Guilds, self).save(*args, **kwargs)


class GuildLegalPersonDetail(BaseModel):
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="legal_person_details",
        null=True,
        blank=True,
    )
    status = models.BooleanField(default=False)
    status_code = models.IntegerField(default=0)
    api_log_id = models.CharField(max_length=100, null=True, blank=True)

    legal_person_info_dto_id = models.CharField(max_length=100, null=True, blank=True)
    parent_legal_person_id = models.CharField(max_length=100, null=True, blank=True)
    successful = models.BooleanField(default=False)
    message = models.TextField(null=True, blank=True)
    name = models.CharField(max_length=300, null=True, blank=True)
    national_code = models.CharField(max_length=20, null=True, blank=True)
    post_code = models.CharField(max_length=20, null=True, blank=True)
    address = models.TextField(null=True, blank=True)
    legal_person_type = models.CharField(max_length=100, null=True, blank=True)
    register_number = models.CharField(max_length=100, null=True, blank=True)
    register_unit = models.CharField(max_length=100, null=True, blank=True)
    register_date = models.CharField(max_length=100, null=True, blank=True)
    register_date_unix = models.BigIntegerField(default=0)
    residency = models.CharField(max_length=100, null=True, blank=True)
    state = models.CharField(max_length=100, null=True, blank=True)
    is_settle = models.BooleanField(default=False)
    settle_date = models.CharField(max_length=100, null=True, blank=True)
    is_break_up = models.BooleanField(default=False)
    break_up_date = models.CharField(max_length=100, null=True, blank=True)
    is_branch = models.BooleanField(default=False)
    branch_list = models.JSONField(null=True, blank=True)
    parent_legal_person_raw = models.JSONField(null=True, blank=True)
    establishment_date = models.CharField(max_length=100, null=True, blank=True)
    is_db_result = models.BooleanField(default=False)
    service_type = models.IntegerField(default=0)

    parent_name = models.CharField(max_length=300, null=True, blank=True)
    parent_national_code = models.CharField(max_length=20, null=True, blank=True)
    parent_post_code = models.CharField(max_length=20, null=True, blank=True)
    parent_address = models.TextField(null=True, blank=True)
    parent_legal_person_type = models.CharField(max_length=100, null=True, blank=True)
    parent_register_number = models.CharField(max_length=100, null=True, blank=True)
    parent_register_unit = models.CharField(max_length=100, null=True, blank=True)
    parent_register_date = models.CharField(max_length=100, null=True, blank=True)
    parent_register_date_unix = models.BigIntegerField(default=0)
    parent_residency = models.CharField(max_length=100, null=True, blank=True)
    parent_state = models.CharField(max_length=100, null=True, blank=True)
    parent_is_settle = models.BooleanField(default=False)
    parent_settle_date = models.CharField(max_length=100, null=True, blank=True)
    parent_is_break_up = models.BooleanField(default=False)
    parent_break_up_date = models.CharField(max_length=100, null=True, blank=True)
    parent_is_branch = models.BooleanField(default=False)
    parent_branch_list = models.JSONField(null=True, blank=True)
    parent_establishment_date = models.CharField(max_length=100, null=True, blank=True)
    parent_is_db_result = models.BooleanField(default=False)
    parent_service_type = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(GuildLegalPersonDetail, self).save(*args, **kwargs)


class GuildsWareHouse(BaseModel):
    guilds = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="ware_house_guilds",
        null=True)
    bar_quantity = models.BigIntegerField(default=0)
    allocated_quantity = models.BigIntegerField(default=0)
    number_of_carcasses = models.BigIntegerField(default=0)
    real_number_of_carcasses = models.BigIntegerField(default=0)
    weight_of_carcasses = models.FloatField(default=0)
    real_weight_of_carcasses = models.FloatField(default=0)
    ave_weight_of_carcasses = models.FloatField(default=0)
    allocated_total_number_of_carcasses = models.BigIntegerField(default=0)
    allocated_total_weight_of_carcasses = models.FloatField(default=0)
    remain_total_number_of_carcasses = models.BigIntegerField(default=0)
    remain_total_weight_of_carcasses = models.FloatField(default=0)
    final_registration = models.BooleanField(default=False)
    date = models.DateTimeField(null=True)

    def save(self, *args, **kwargs):
        self.remain_total_number_of_carcasses = (
                self.number_of_carcasses - self.allocated_total_number_of_carcasses) if self.real_number_of_carcasses == 0 else (
                self.real_number_of_carcasses - self.allocated_total_number_of_carcasses)
        self.remain_total_weight_of_carcasses = (
                self.weight_of_carcasses - self.allocated_total_weight_of_carcasses) if self.real_weight_of_carcasses == 0 else (
                self.real_weight_of_carcasses - self.allocated_total_weight_of_carcasses)
        super(GuildsWareHouse, self).save(*args, **kwargs)




class StewardWareHouse(BaseModel):
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="ware_house_steward",
        null=True)
    bar_quantity = models.BigIntegerField(default=0)
    allocated_quantity = models.BigIntegerField(default=0)
    number_of_carcasses = models.BigIntegerField(default=0)
    real_number_of_carcasses = models.BigIntegerField(default=0)
    weight_of_carcasses = models.FloatField(default=0)
    real_weight_of_carcasses = models.FloatField(default=0)
    free_bar_quantity = models.BigIntegerField(default=0)
    number_of_free_carcasses = models.BigIntegerField(default=0)
    weight_of_free_carcasses = models.FloatField(default=0)
    ave_weight_of_carcasses = models.FloatField(default=0)
    allocated_total_number_of_carcasses = models.BigIntegerField(default=0)
    allocated_total_weight_of_carcasses = models.FloatField(default=0)
    remain_total_number_of_carcasses = models.BigIntegerField(default=0)
    remain_total_weight_of_carcasses = models.FloatField(default=0)
    final_registration = models.BooleanField(default=False)
    free_sale_quantity = models.BigIntegerField(default=0)
    free_sale_weight = models.FloatField(default=0)

    date = models.DateTimeField(null=True)

    def save(self, *args, **kwargs):
        super(StewardWareHouse, self).save(*args, **kwargs)


class Dispenser(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="dispenser_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="dispenser_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="dispenser_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="dispenser_wallet",
        null=True
    )

    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="dispenser_kill_house",
        null=True
    )

    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="dispenser_guild",
        null=True
    )
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="dispenser_steward",
        null=True
    )
    dispenser_type = models.CharField(max_length=100, null=True)
    limitation_amount = models.FloatField(default=0)
    car = models.CharField(max_length=100, null=True)
    pelak = models.CharField(max_length=100, null=True)
    registrar = models.CharField(max_length=200, null=True)
    active = models.BooleanField(default=True)
    free_sale = models.BooleanField(default=False)
    free_guilds = models.BooleanField(default=False)
    free_stewards = models.BooleanField(default=False)
    in_use = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(Dispenser, self).save(*args, **kwargs)


class DispenserInformation(BaseModel):
    dispenser = models.ForeignKey(
        Dispenser,
        on_delete=models.CASCADE,
        related_name="dispenser_info_user",
        null=True
    )

    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="dispenser_info_kill_house",
        null=True
    )
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="dispenser_info_steward",
        null=True
    )
    fullname = models.CharField(max_length=200, null=True)
    national_id = models.CharField(max_length=200, null=True)
    first_name = models.CharField(max_length=200, null=True)
    last_name = models.CharField(max_length=200, null=True)
    mobile = models.CharField(max_length=11, null=True)
    city = models.CharField(max_length=300, null=True)
    province = models.CharField(max_length=300, null=True)
    active = models.BooleanField(default=True)
    limitation = models.BooleanField(default=False)
    free_limitation = models.BooleanField(default=False)
    governmental_limitation = models.BooleanField(default=False)
    governmental_limitation_weight = models.FloatField(default=0)
    free_limitation_weight = models.FloatField(default=0)
    total_selling_warehouse_governmental_weight = models.FloatField(default=0)
    total_selling_warehouse_free_weight = models.FloatField(default=0)
    total_selling_warehouse_weight = models.FloatField(default=0)
    def save(self, *args, **kwargs):
        super(DispenserInformation, self).save(*args, **kwargs)


class NewProduct(BaseModel):
    product_id = models.IntegerField(default=0)
    name = models.CharField(max_length=200, null=True)
    image = models.CharField(max_length=500, null=True)
    sale_type = models.CharField(max_length=100, null=True)
    sale_type_package_weight = models.FloatField(default=0)
    approved_price_status = models.BooleanField(default=False)
    approved_price = models.IntegerField(default=0)
    sale_limitation_status = models.BooleanField(default=False)
    sale_limitation_maximum_weight_status = models.BooleanField(default=False)
    sale_limitation_maximum_weight = models.FloatField(default=0)
    sale_limitation_maximum_package_status = models.BooleanField(default=False)
    sale_limitation_maximum_package = models.FloatField(default=0)
    sale_limitation_maximum_number_of_purchases_status = models.BooleanField(default=False)
    sale_limitation_maximum_number_of_purchases = models.FloatField(default=0)
    sale_limitation_maximum_number_of_purchases_type = models.CharField(max_length=100, null=True)
    sale_limitation_maximum_inquiry_status = models.BooleanField(default=False)
    sale_limitation_maximum_inquiry_type = models.CharField(max_length=100, null=True)
    Conventional_sales_status = models.BooleanField(default=False)
    Conventional_sales_weight = models.FloatField(default=0)
    Conventional_sales_package = models.FloatField(default=0)
    selling_more_than_inventory = models.BooleanField(default=False)
    parent = models.ForeignKey(
        'self',
        on_delete=models.CASCADE,
        related_name="child",
        null=True
    )

    def save(self, *args, **kwargs):
        super(NewProduct, self).save(*args, **kwargs)


class RolesProducts(BaseModel):
    parent_product = models.ForeignKey(
        NewProduct,
        on_delete=models.CASCADE,
        related_name="roles_products_new_product",
        null=True
    )
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="roles_products_kill_house",
        null=True
    )
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="roles_products_steward",
        null=True
    )
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="roles_products_guild",
        null=True
    )
    name = models.CharField(max_length=300, null=True)
    province_governmental_carcasses_quantity = models.BigIntegerField(default=0)
    province_governmental_carcasses_weight = models.BigIntegerField(default=0)
    province_free_carcasses_quantity = models.BigIntegerField(default=0)
    province_free_carcasses_weight = models.BigIntegerField(default=0)
    receive_governmental_carcasses_quantity = models.BigIntegerField(default=0)
    receive_governmental_carcasses_weight = models.BigIntegerField(default=0)
    receive_free_carcasses_quantity = models.BigIntegerField(default=0)
    receive_free_carcasses_weight = models.BigIntegerField(default=0)
    free_buying_carcasses_quantity = models.BigIntegerField(default=0)
    free_buying_carcasses_weight = models.BigIntegerField(default=0)
    total_governmental_carcasses_quantity = models.BigIntegerField(default=0)
    total_governmental_carcasses_weight = models.BigIntegerField(default=0)
    total_free_bars_carcasses_quantity = models.BigIntegerField(default=0)
    total_free_bars_carcasses_weight = models.BigIntegerField(default=0)
    weight_average = models.FloatField(default=0)
    total_carcasses_quantity = models.BigIntegerField(default=0)
    total_carcasses_weight = models.BigIntegerField(default=0)
    freezing_quantity = models.BigIntegerField(default=0)
    freezing_weight = models.BigIntegerField(default=0)
    loss_weight = models.BigIntegerField(default=0)
    out_province_allocated_quantity = models.BigIntegerField(default=0)
    out_province_allocated_weight = models.BigIntegerField(default=0)
    province_allocated_quantity = models.BigIntegerField(default=0)
    province_allocated_weight = models.BigIntegerField(default=0)
    province_governmental_allocated_weight = models.BigIntegerField(default=0)
    province_free_allocated_weight = models.BigIntegerField(default=0)
    real_allocated_quantity = models.BigIntegerField(default=0)
    real_allocated_weight = models.BigIntegerField(default=0)
    cold_house_allocated_weight = models.BigIntegerField(default=0)
    pos_allocated_weight = models.BigIntegerField(default=0)
    segmentation_weight = models.BigIntegerField(default=0)
    ware_house_archive_weight = models.BigIntegerField(default=0)
    total_remain_quantity = models.BigIntegerField(default=0)
    total_remain_weight = models.BigIntegerField(default=0)
    free_price = models.IntegerField(default=0)
    approved_price = models.IntegerField(default=0)
    approved_price_status = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        self.total_governmental_carcasses_quantity = self.province_governmental_carcasses_quantity + self.receive_governmental_carcasses_quantity
        self.total_governmental_carcasses_weight = self.province_governmental_carcasses_weight + self.receive_governmental_carcasses_weight
        self.total_free_bars_carcasses_quantity = self.province_free_carcasses_quantity + self.receive_free_carcasses_quantity + self.free_buying_carcasses_quantity
        self.total_free_bars_carcasses_weight = self.province_free_carcasses_weight + self.receive_free_carcasses_weight + self.free_buying_carcasses_weight
        self.total_carcasses_quantity = self.total_governmental_carcasses_quantity + self.total_free_bars_carcasses_quantity
        self.total_carcasses_weight = self.total_governmental_carcasses_weight + self.total_free_bars_carcasses_weight
        self.real_allocated_quantity = self.province_allocated_quantity + self.out_province_allocated_quantity + self.freezing_quantity
        self.real_allocated_weight = self.province_allocated_weight + self.out_province_allocated_weight + self.loss_weight + self.cold_house_allocated_weight + self.segmentation_weight + int(
            self.pos_allocated_weight / 1000)
        self.total_remain_quantity = self.total_carcasses_quantity - self.real_allocated_quantity
        self.total_remain_weight = (
                                           self.total_carcasses_weight - self.real_allocated_weight) - self.ware_house_archive_weight
        self.weight_average = round(self.total_carcasses_weight / self.total_carcasses_quantity,
                                    2) if self.total_carcasses_quantity > 0 else 0
        super(RolesProducts, self).save(*args, **kwargs)


class OtherProducts(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="other_products_kill_house",
        null=True
    )
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="other_products_guild",
        null=True
    )
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="other_products_steward",
        null=True
    )
    name = models.CharField(max_length=300, null=True)
    unit = models.CharField(max_length=100, default='kg')
    targetunit = models.CharField(max_length=100, default='g')
    image = models.CharField(max_length=500, null=True)
    price = models.BigIntegerField(default=0)
    active = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(OtherProducts, self).save(*args, **kwargs)


class BaseOutProvinceCarcassesBuyer(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="base_buyer_user",
        null=True)
    fullname = models.CharField(max_length=200, null=True)
    national_id = models.CharField(max_length=200, null=True)
    first_name = models.CharField(max_length=200, null=True)
    last_name = models.CharField(max_length=200, null=True)
    mobile = models.CharField(max_length=11, null=True)
    unit_name = models.CharField(max_length=300, null=True)
    city = models.CharField(max_length=300, null=True)
    province = models.CharField(max_length=300, null=True)
    isRealPerson = models.BooleanField(
        default=True,
        verbose_name="حقیقی=True / حقوقی=False")
    status = models.BooleanField(default=False)
    statusCode = models.IntegerField(null=True, blank=True)
    legalPersonInfoDtoId = models.CharField(
        max_length=100,
        null=True,
        blank=True)
    parentLegalPersonId = models.CharField(
        max_length=100,
        null=True,
        blank=True)
    successful = models.BooleanField(default=False)
    message = models.TextField(null=True, blank=True)
    name = models.CharField(max_length=300, null=True, blank=True)
    nationalCode = models.CharField(
        max_length=20,
        null=True,
        blank=True)
    postCode = models.CharField(
        max_length=20,
        null=True,
        blank=True)
    address = models.TextField(null=True, blank=True)
    legalPersonType = models.CharField(
        max_length=100,
        null=True,
        blank=True)
    registerNumber = models.CharField(max_length=50, null=True, blank=True)
    registerUnit = models.CharField(max_length=100, null=True, blank=True)
    registerDate = models.CharField(max_length=50, null=True, blank=True)
    registerDateUnix = models.BigIntegerField(null=True, blank=True)
    residency = models.CharField(max_length=100, null=True, blank=True)
    state = models.CharField(max_length=100, null=True, blank=True)
    isSettle = models.BooleanField(default=False)
    settleDate = models.CharField(max_length=50, null=True, blank=True)
    isBreakUp = models.BooleanField(default=False)
    breakUpdate = models.CharField(max_length=50, null=True, blank=True)
    isBranch = models.BooleanField(default=False)
    branchList = models.TextField(null=True, blank=True)
    establishmentDate = models.CharField(max_length=50, null=True, blank=True)
    isDbResult = models.BooleanField(default=False)
    serviceType = models.IntegerField(null=True, blank=True)
    parentLegalPersonInfoDtoId = models.CharField(
        max_length=100,
        null=True,
        blank=True)
    parentLegalPersonParentId = models.CharField(
        max_length=100,
        null=True,
        blank=True)
    parentLegalPersonSuccessful = models.BooleanField(default=False)
    parentLegalPersonMessage = models.TextField(null=True, blank=True)
    parentLegalPersonName = models.CharField(
        max_length=300,
        null=True,
        blank=True)
    parentLegalPersonNationalCode = models.CharField(
        max_length=20,
        null=True,
        blank=True)
    parentLegalPersonPostCode = models.CharField(
        max_length=20,
        null=True,
        blank=True)
    parentLegalPersonAddress = models.TextField(null=True, blank=True)
    parentLegalPersonType = models.CharField(
        max_length=100,
        null=True,
        blank=True)
    parentLegalPersonRegisterNumber = models.CharField(
        max_length=50,
        null=True,
        blank=True)
    parentLegalPersonRegisterUnit = models.CharField(
        max_length=100,
        null=True,
        blank=True)
    parentLegalPersonRegisterDate = models.CharField(
        max_length=50,
        null=True,
        blank=True)
    parentLegalPersonRegisterDateUnix = models.BigIntegerField(
        null=True,
        blank=True)
    parentLegalPersonResidency = models.CharField(
        max_length=100,
        null=True,
        blank=True)
    parentLegalPersonState = models.CharField(
        max_length=100,
        null=True,
        blank=True)
    parentLegalPersonIsSettle = models.BooleanField(default=False)
    parentLegalPersonSettleDate = models.CharField(
        max_length=50,
        null=True,
        blank=True)
    parentLegalPersonIsBreakUp = models.BooleanField(default=False)
    parentLegalPersonBreakUpdate = models.CharField(
        max_length=50,
        null=True,
        blank=True)
    parentLegalPersonIsBranch = models.BooleanField(default=False)
    parentLegalPersonBranchList = models.TextField(null=True, blank=True)
    parentLegalPersonEstablishmentDate = models.CharField(
        max_length=50,
        null=True,
        blank=True)
    parentLegalPersonIsDbResult = models.BooleanField(default=False)
    parentLegalPersonServiceType = models.IntegerField(
        null=True,
        blank=True)
    apiLogId = models.CharField(max_length=100, null=True, blank=True)
    active = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        self.fullname = self.first_name + " " + self.last_name
        super(BaseOutProvinceCarcassesBuyer, self).save(*args, **kwargs)


class OutProvinceCarcassesBuyer(BaseModel):
    buyer = models.ForeignKey(
        BaseOutProvinceCarcassesBuyer,
        on_delete=models.CASCADE,
        related_name="base_buyer",
        null=True)
    type_activity = models.ForeignKey(
        TypeActivity,
        on_delete=models.CASCADE,
        related_name="buyer_activity",
        null=True)
    Kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_buyer",
        null=True)
    steward = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="steward_buyer",
        null=True)
    fullname = models.CharField(max_length=200, null=True)
    first_name = models.CharField(max_length=200, null=True)
    last_name = models.CharField(max_length=200, null=True)
    national_id = models.CharField(max_length=200, null=True)
    mobile = models.CharField(max_length=11, null=True)
    unit_name = models.CharField(max_length=300, null=True)
    city = models.CharField(max_length=300, null=True)
    province = models.CharField(max_length=300, null=True)
    active = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        self.fullname = self.first_name + " " + self.last_name
        super(OutProvinceCarcassesBuyer, self).save(*args, **kwargs)


class Representative(BaseModel):

    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_representative",
        null=True
    )
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="steward_representative",
        null=True
    )
    first_name = models.CharField(max_length=200, null=True)
    last_name = models.CharField(max_length=200, null=True)
    mobile = models.CharField(max_length=15, null=True)
    city = models.CharField(max_length=100, null=True)
    active = models.BooleanField(default=True)
    limitation = models.BooleanField(default=False)
    free_limitation = models.BooleanField(default=False)
    governmental_limitation = models.BooleanField(default=False)
    governmental_limitation_weight = models.FloatField(default=0)
    free_limitation_weight = models.FloatField(default=0)
    total_selling_warehouse_governmental_weight = models.FloatField(default=0)
    total_selling_warehouse_free_weight = models.FloatField(default=0)
    total_selling_warehouse_weight = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(Representative, self).save(*args, **kwargs)


class KillHouseFreeSaleBarInformation(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="free_sale_bar_info_kill_house",
        null=True)

    product = models.ForeignKey(
        RolesProducts,
        on_delete=models.CASCADE,
        related_name="free_sale_bar_info_kill_house_product",
        null=True)
    buyer = models.ForeignKey(
        OutProvinceCarcassesBuyer,
        on_delete=models.CASCADE,
        related_name="free_sale_bar_info_buyer",
        null=True)
    dispenser = models.ForeignKey(
        Dispenser,
        on_delete=models.CASCADE,
        related_name="free_sale_bar_info_dispenser",
        null=True
    )
    representative = models.ForeignKey(
        Representative,
        on_delete=models.CASCADE,
        related_name="free_sale_bar_info_representative",
        null=True
    )
    registerar_fullname = models.CharField(max_length=100, null=True)
    permission_type = models.CharField(max_length=100, null=True)
    status = models.CharField(max_length=300, default='pending')
    buyer_name = models.CharField(max_length=100, null=True)
    buyer_mobile = models.CharField(max_length=11, null=True)
    province = models.CharField(max_length=50, null=True)
    city = models.CharField(max_length=50, null=True)
    driver_name = models.CharField(max_length=100, null=True)
    driver_mobile = models.CharField(max_length=11, null=True)
    type_car = models.CharField(max_length=100, null=True)
    pelak = models.CharField(max_length=100, null=True)
    clearance_code = models.CharField(max_length=50, null=True)
    number_of_carcasses = models.BigIntegerField(default=0)
    weight_of_carcasses = models.FloatField(default=0)
    real_number_of_carcasses = models.BigIntegerField(default=0)
    real_weight_of_carcasses = models.FloatField(default=0)
    quarantine_weight_of_carcasses = models.FloatField(default=0)
    accepted_number_of_carcasses = models.BigIntegerField(default=0)
    accepted_weight_of_carcasses = models.FloatField(default=0)
    date = models.DateTimeField(null=True)
    wage = models.FloatField(default=0)
    archive_wage = models.BooleanField(default=False)
    total_wage_amount = models.BigIntegerField(default=0)
    union_share = models.BigIntegerField(default=0)
    union_share_percent = models.FloatField(default=0)
    company_share = models.BigIntegerField(default=0)
    company_share_percent = models.FloatField(default=0)
    guilds_share = models.BigIntegerField(default=0)
    guilds_share_percent = models.FloatField(default=0)
    city_share = models.BigIntegerField(default=0)
    city_share_percent = models.FloatField(default=0)
    wallet_share = models.BigIntegerField(default=0)
    wallet_share_percent = models.FloatField(default=0)
    other_share = models.BigIntegerField(default=0)
    other_share_percent = models.FloatField(default=0)
    calculate_status = models.BooleanField(default=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)
    sale_type = models.CharField(max_length=20, null=True)
    quota = models.CharField(max_length=20, null=True)
    warehouse = models.BooleanField(default=True)
    registration_code = models.IntegerField(null=True)
    logged_registration_code = models.IntegerField(null=True)
    expire_date_time_registration_code = models.DateTimeField(null=True)
    register_date_time_registration_code = models.DateTimeField(null=True)
    active_expire_date_time = models.BooleanField(default=False)
    system_registration_code = models.BooleanField(default=False)
    expire_time_ten_minute = models.DateTimeField(null=True)
    production_date = models.DateTimeField(null=True)
    distribution_type = models.CharField(max_length=20, null=True)
    amount = models.BigIntegerField(default=0)
    total_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(KillHouseFreeSaleBarInformation, self).save(*args, **kwargs)


class KillHouseFreeBarInformation(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="free_bar_info_kill_house",
        null=True)
    exclusive_killer = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="free_bar_info_exclusive_killer",
        null=True)
    public_killer = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="free_bar_info_public_killer",
        null=True)
    product = models.ForeignKey(
        RolesProducts,
        on_delete=models.CASCADE,
        related_name="free_bar_info_kill_house_product",
        null=True)
    input_warehouse = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="free_bar_input_warehouse",
        null=True
    )
    dispenser = models.ForeignKey(
        Dispenser,
        on_delete=models.CASCADE,
        related_name="free_bar_dispenser",
        null=True
    )
    representative = models.ForeignKey(
        Representative,
        on_delete=models.CASCADE,
        related_name="free_bar_representative",
        null=True
    )
    poultry_name = models.CharField(max_length=100, null=True)
    poultry_mobile = models.CharField(max_length=11, null=True)
    seller_name = models.CharField(max_length=100, null=True)
    seller_mobile = models.CharField(max_length=11, null=True)
    province = models.CharField(max_length=50, null=True)
    city = models.CharField(max_length=50, null=True)
    vet_farm_name = models.CharField(max_length=100, null=True)
    vet_farm_mobile = models.CharField(max_length=11, null=True)
    driver_name = models.CharField(max_length=100, null=True)
    driver_mobile = models.CharField(max_length=11, null=True)
    car = models.JSONField(null=True)
    clearance_code = models.JSONField(null=True)
    bar_clearance_code = models.CharField(max_length=100, null=True)
    quantity = models.BigIntegerField(default=0)
    number_of_carcasses = models.BigIntegerField(default=0)
    weight_of_carcasses = models.FloatField(default=0)
    kill_house_vet_quantity = models.BigIntegerField(default=0)
    kill_house_vet_weight = models.FloatField(default=0)
    kill_house_vet_state = models.CharField(max_length=50, default="pending")
    date_of_accept_reject = models.DateTimeField(null=True)
    acceptor_rejector = models.CharField(max_length=200, null=True)
    live_weight = models.FloatField(default=0)
    bar_image = models.CharField(max_length=200, null=True)
    buy_type = models.CharField(max_length=50, null=True)
    ware_house = models.BooleanField(default=False)
    date = models.DateTimeField(null=True)
    wage = models.FloatField(default=0)
    total_wage_amount = models.BigIntegerField(default=0)
    union_share = models.BigIntegerField(default=0)
    union_share_percent = models.FloatField(default=0)
    company_share = models.BigIntegerField(default=0)
    company_share_percent = models.FloatField(default=0)
    guilds_share = models.BigIntegerField(default=0)
    guilds_share_percent = models.FloatField(default=0)
    city_share = models.BigIntegerField(default=0)
    city_share_percent = models.FloatField(default=0)
    wallet_share = models.BigIntegerField(default=0)
    wallet_share_percent = models.FloatField(default=0)
    other_share = models.BigIntegerField(default=0)
    other_share_percent = models.FloatField(default=0)
    archive_wage = models.BooleanField(default=False)
    weight_loss = models.FloatField(default=0)
    calculate_status = models.BooleanField(default=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)
    entered_message = models.TextField(null=True)
    bar_code = models.BigIntegerField(null=True)
    register_type = models.CharField(max_length=250, default='manual')
    register_date = models.DateTimeField(null=True)
    warehouse = models.BooleanField(default=True)
    warehouse_commitment_weight = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(KillHouseFreeBarInformation, self).save(*args, **kwargs)


class StewardFreeBarInformation(BaseModel):
    steward = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="free_bar_info_steward",
        null=True)
    stewards = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="free_bar_info_stewards",
        null=True)
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="free_bar_info_guild",
        null=True)
    product = models.ForeignKey(
        RolesProducts,
        on_delete=models.CASCADE,
        related_name="free_bar_info_steward_product",
        null=True)
    dispenser = models.ForeignKey(
        Dispenser,
        on_delete=models.CASCADE,
        related_name="free_bar_info_steward_dispenser",
        null=True
    )
    representative = models.ForeignKey(
        Representative,
        on_delete=models.CASCADE,
        related_name="free_bar_info_steward_representative",
        null=True
    )
    kill_house_name = models.CharField(max_length=100, null=True)
    kill_house_mobile = models.CharField(max_length=11, null=True)
    kill_house_vet_name = models.CharField(max_length=100, null=True)
    kill_house_vet_mobile = models.CharField(max_length=11, null=True)
    province = models.CharField(max_length=50, null=True)
    city = models.CharField(max_length=50, null=True)
    driver_name = models.CharField(max_length=100, null=True)
    driver_mobile = models.CharField(max_length=11, null=True)
    car = models.CharField(max_length=100, null=True)
    pelak = models.CharField(max_length=100, null=True)
    number_of_carcasses = models.BigIntegerField(default=0)
    weight_of_carcasses = models.FloatField(default=0)
    bar_image = models.CharField(max_length=200, null=True)
    date = models.DateTimeField(null=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)
    warehouse = models.BooleanField(default=True)
    quota = models.CharField(max_length=20, null=True)

    def save(self, *args, **kwargs):
        super(StewardFreeBarInformation, self).save(*args, **kwargs)


class StewardFreeSaleBarInformation(BaseModel):
    steward = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="free_sale_bar_info_steward",
        null=True)
    stewards = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="free_sale_bar_info_stewards",
        null=True)
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="free_sale_bar_info_guild",
        null=True)

    product = models.ForeignKey(
        RolesProducts,
        on_delete=models.CASCADE,
        related_name="free_sale_bar_info_steward_product",
        null=True)
    buyer = models.ForeignKey(
        OutProvinceCarcassesBuyer,
        on_delete=models.CASCADE,
        related_name="steward_free_sale_bar_info_buyer",
        null=True)
    dispenser = models.ForeignKey(
        Dispenser,
        on_delete=models.CASCADE,
        related_name="steward_free_sale_bar_info_dispenser",
        null=True
    )
    representative = models.ForeignKey(
        Representative,
        on_delete=models.CASCADE,
        related_name="steward_free_sale_bar_info_representative",
        null=True
    )
    buyer_name = models.CharField(max_length=100, null=True)
    buyer_mobile = models.CharField(max_length=11, null=True)
    province = models.CharField(max_length=50, null=True)
    city = models.CharField(max_length=50, null=True)
    driver_name = models.CharField(max_length=100, null=True)
    driver_mobile = models.CharField(max_length=11, null=True)
    type_car = models.CharField(max_length=100, null=True)
    pelak = models.CharField(max_length=100, null=True)
    clearance_code = models.CharField(max_length=50, null=True)
    number_of_carcasses = models.BigIntegerField(default=0)
    weight_of_carcasses = models.FloatField(default=0)
    date = models.DateTimeField(null=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)
    warehouse = models.BooleanField(default=True)
    quota = models.CharField(max_length=20, null=True)
    sale_type = models.CharField(max_length=20, null=True)
    registration_code = models.IntegerField(null=True)
    logged_registration_code = models.IntegerField(null=True)
    expire_date_time_registration_code = models.DateTimeField(null=True)
    register_date_time_registration_code = models.DateTimeField(null=True)
    active_expire_date_time = models.BooleanField(default=False)
    system_registration_code = models.BooleanField(default=False)
    expire_time_ten_minute = models.DateTimeField(null=True)
    production_date = models.DateTimeField(null=True)
    distribution_type = models.CharField(max_length=20, null=True)

    def save(self, *args, **kwargs):
        super(StewardFreeSaleBarInformation, self).save(*args, **kwargs)


class ColdHouse(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="cold_house_kill_house",
        null=True
    )
    live_stock_support = models.ForeignKey(
        LiveStockSupport,
        on_delete=models.CASCADE,
        related_name="cold_house_live_stock_support",
        null=True
    )
    steward = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="steward_cold_house",
        null=True
    )

    name = models.CharField(max_length=500, null=True)
    province = models.CharField(max_length=200, null=True)
    city = models.CharField(max_length=200, null=True)
    address = models.CharField(max_length=500, null=True)
    total_quantity = models.BigIntegerField(default=0)
    total_weight = models.FloatField(default=0)
    total_input_weight = models.BigIntegerField(default=0)
    total_allocated_weight = models.BigIntegerField(default=0)
    total_remain_weight = models.BigIntegerField(default=0)
    active = models.BooleanField(default=True)
    broadcast = models.BooleanField(default=True)
    relocate = models.BooleanField(default=True)
    capacity = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        self.total_remain_weight = self.total_input_weight - self.total_allocated_weight
        super(ColdHouse, self).save(*args, **kwargs)


class StewardAllocation(BaseModel):
    ware_house = models.ForeignKey(
        KillHouseWareHouse,
        on_delete=models.CASCADE,
        related_name="ware_house_steward_allocation",
        null=True
    )
    steward_ware_house = models.ForeignKey(
        StewardWareHouse,
        on_delete=models.CASCADE,
        related_name="steward_ware_house_steward_guilds_allocation",
        null=True
    )
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_steward_allocation",
        null=True
    )
    steward = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="steward_steward_allocation",
        null=True
    )

    guilds = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="steward_allocation_guilds",
        null=True
    )
    to_steward = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="allocation_to_steward",
        null=True
    )
    stewards = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="allocation_to_stewards",
        null=True
    )
    to_stewards = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="to_allocation_to_stewards",
        null=True
    )
    to_guilds = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="allocation_to_guilds",
        null=True
    )
    to_kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="allocation_to_kill_house",
        null=True
    )

    to_cold_house = models.ForeignKey(
        ColdHouse,
        on_delete=models.CASCADE,
        related_name="allocation_to_cold_house",
        null=True
    )
    other_cold_house = models.ForeignKey(
        ColdHouse,
        on_delete=models.CASCADE,
        related_name="allocation_to_other_cold_house",
        null=True
    )

    car = models.ForeignKey(
        Car,
        on_delete=models.CASCADE,
        related_name="steward_allocation_car",
        null=True
    )
    dispenser = models.ForeignKey(
        Dispenser,
        on_delete=models.CASCADE,
        related_name="allocation_dispenser",
        null=True
    )
    representative = models.ForeignKey(
        Representative,
        on_delete=models.CASCADE,
        related_name="allocation_representative",
        null=True
    )
    product = models.ForeignKey(
        RolesProducts,
        on_delete=models.CASCADE,
        related_name="allocation_product",
        null=True
    )
    number_of_carcasses = models.BigIntegerField(default=0)
    real_number_of_carcasses = models.BigIntegerField(default=0)
    receiver_real_number_of_carcasses = models.BigIntegerField(default=0)
    weight_of_carcasses = models.FloatField(default=0)
    real_weight_of_carcasses = models.FloatField(default=0)
    receiver_real_weight_of_carcasses = models.FloatField(default=0)
    weight_loss_of_carcasses = models.FloatField(default=0)
    final_registration = models.BooleanField(default=False)
    sell_type = models.CharField(max_length=100, null=True)
    product_name = models.CharField(max_length=200, null=True)
    seller_type = models.CharField(max_length=100, null=True)
    type = models.CharField(max_length=100, null=True)
    sale_type = models.CharField(max_length=100, null=True)
    allocation_type = models.CharField(max_length=100, null=True)
    system_registration_code = models.BooleanField(default=False)
    registration_code = models.IntegerField(null=True)
    amount = models.BigIntegerField(default=0)
    total_amount = models.BigIntegerField(default=0)
    total_amount_paid = models.BigIntegerField(default=0)
    total_amount_remain = models.BigIntegerField(default=0)
    logged_registration_code = models.IntegerField(null=True)
    state = models.CharField(max_length=50, default='pending')
    receiver_state = models.CharField(max_length=50, default='pending')
    allocation_state = models.CharField(max_length=50, default='pending')
    date = models.DateTimeField(null=True)
    role = models.JSONField(null=True)
    steward_temp_key = models.IntegerField(null=True)
    approved_price_status = models.BooleanField(default=True)
    calculate_status = models.BooleanField(default=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)
    overhead = models.BooleanField(default=False)
    image = models.CharField(max_length=500, null=True)
    quota = models.CharField(max_length=20, null=True)
    warehouse = models.BooleanField(default=True)
    steward_warehouse = models.BooleanField(default=True)
    expire_date_time_registration_code = models.DateTimeField(null=True)
    register_date_time_registration_code = models.DateTimeField(null=True)
    active_expire_date_time = models.BooleanField(default=False)
    return_trash = models.BooleanField(default=False)
    return_trash_date = models.DateTimeField(null=True)
    returner_trash = models.CharField(max_length=200, null=True)
    interface_number = models.CharField(max_length=11, null=True)
    expire_time_ten_minute = models.DateTimeField(null=True)
    production_date = models.DateTimeField(null=True)
    distribution_type = models.CharField(max_length=20, null=True)
    Settlement_type = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        # allow = AllowRegisterCodeForStewardAllocation.objects.filter(trash=False, active=True).first()
        # if allow:
        # if allow and self.registration_code and self.expire_date_time_registration_code is None:
        #     self.expire_date_time_registration_code = datetime.now() + timedelta(minutes=int(allow.time))
        #     self.active_expire_date_time = True
        self.total_amount_remain = self.total_amount - self.total_amount_paid if self.total_amount - self.total_amount_paid > 0 else 0
        super(StewardAllocation, self).save(*args, **kwargs)


class AutomaticStewardAllocation(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_automatic_allocation",
        null=True
    )
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="automatic_steward_allocation",
        null=True
    )
    guilds = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="automatic_guilds_allocation",
        null=True
    )
    number_of_carcasses = models.BigIntegerField(default=0)
    real_number_of_carcasses = models.BigIntegerField(default=0)
    weight_of_carcasses = models.FloatField(default=0)
    real_weight_of_carcasses = models.FloatField(default=0)
    final_registration = models.BooleanField(default=False)
    registration_code = models.IntegerField(null=True)
    logged_registration_code = models.IntegerField(null=True)
    state = models.CharField(max_length=50, default='pending')
    date = models.DateTimeField(null=True)
    role = models.JSONField(null=True)

    def save(self, *args, **kwargs):
        super(AutomaticStewardAllocation, self).save(*args, **kwargs)


class ProvinceAllowKillHouseChooseStewardGuilds(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="allow_kill_house_steward_guilds",
        null=True
    )

    steward = models.BooleanField(default=False)
    guilds = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(ProvinceAllowKillHouseChooseStewardGuilds, self).save(*args, **kwargs)


class LastUpdate(models.Model):
    poultry_hatching = models.JSONField(null=True)
    update_date = models.DateTimeField(null=True)


class HatchingLossesPermission(BaseModel):
    percent = models.FloatField(default=0)
    allow = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(HatchingLossesPermission, self).save(*args, **kwargs)


class WagePayment(BaseModel):
    province_kill_request = models.JSONField(null=True)
    payment_type = models.CharField(max_length=50, null=True)
    type = models.CharField(max_length=100, null=True)
    total_amount = models.BigIntegerField(default=0)
    total_amount_with_tax = models.BigIntegerField(default=0)
    payer = models.JSONField(null=True)
    tracking_code = models.CharField(max_length=100, null=True)
    refId = models.CharField(max_length=100, null=True)
    orderId = models.CharField(max_length=100, null=True)
    authority = models.CharField(max_length=100, null=True)
    cardHolderPan = models.CharField(max_length=100, null=True)
    payer_info = models.CharField(max_length=200, null=True)
    state = models.CharField(max_length=20, default='pending')
    message = models.TextField(null=True)
    link_pay = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(WagePayment, self).save(*args, **kwargs)


class KillHousePurchaseRequest(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_purchase_request",
        null=True
    )
    allow = models.BooleanField(default=True)
    limitation = models.BooleanField(default=False)
    total_limitation = models.BooleanField(default=False)
    limitation_number = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(KillHousePurchaseRequest, self).save(*args, **kwargs)


class Observatory(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="observatory_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="observatory_address",
        null=True
    )
    user_bank_info = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="observatory_bank_info",
                                       null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="observatory_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(Observatory, self).save(*args, **kwargs)


class PosCompany(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="pos_company_user",
        null=True
    )
    name = models.CharField(max_length=100, null=True)
    active = models.BooleanField(default=True)
    en_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(PosCompany, self).save(*args, **kwargs)


class PosDeviceVersion(BaseModel):
    company = models.ForeignKey(
        PosCompany,
        on_delete=models.CASCADE,
        related_name="pos_version_company",
        null=True
    )
    name = models.CharField(max_length=200, null=True)
    code = models.IntegerField()
    description = models.TextField(null=True)
    enable = models.BooleanField(default=True)
    remove = models.BooleanField(default=False)
    link = models.TextField(null=True)
    checksum = models.TextField(null=True)

    def save(self, *args, **kwargs):
        super(PosDeviceVersion, self).save(*args, **kwargs)

    class Meta:
        unique_together = ('company', 'code')


class POSId(BaseModel):
    pos_id = models.CharField(max_length=12, null=True)
    info = models.JSONField(null=True)

    def save(self, *args, **kwargs):
        super(POSId, self).save(*args, **kwargs)




class POSMachine(BaseModel):
    pos_id = models.CharField(max_length=12, null=True)
    pos_unique_id = models.CharField(max_length=6, unique=True, null=True)
    token = models.CharField(max_length=36, null=True)
    receiver_number = models.CharField(max_length=100, null=True)
    terminal_number = models.CharField(max_length=100, null=True)
    active = models.BooleanField(default=True)
    multi_device = models.BooleanField(default=False)
    server_register = models.BooleanField(default=False)
    Lat = models.FloatField(null=True)
    Long = models.FloatField(null=True)
    company_name = models.CharField(max_length=200, null=True)
    last_check = models.DateTimeField(null=True)
    serial = models.TextField(default="")
    mac = models.CharField(max_length=50, null=True)
    password = models.CharField(max_length=10, null=True)
    ip = models.CharField(max_length=15, default='0.0.0.0')
    name = models.TextField(default="")
    sdk = models.TextField(default="")
    version = models.IntegerField(default=0)
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="user_pos",
        null=True
    )

    owner = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="owned_pos",
        null=True
    )

    current_user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.SET_NULL,
        null=True,
        related_name="current_pos"
    )
    current_dispenser = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.SET_NULL,
        null=True,
        related_name="current_dispenser_pos"
    )
    current_representative = models.ForeignKey(
        Representative,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="representative_pos"
    )
    pos_company = models.ForeignKey(
        PosCompany,
        on_delete=models.CASCADE,
        related_name="company_pos",
        null=True
    )
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_pos",
        null=True
    )
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="guild_pos",
        null=True
    )

    cooperative = models.ForeignKey(
        Cooperative,
        on_delete=models.CASCADE,
        related_name="cooperative_pos",
        null=True
    )
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="steward_pos",
        null=True
    )
    dispenser = models.ForeignKey(
        Dispenser,
        on_delete=models.CASCADE,
        related_name="dispenser_pos",
        null=True
    )

    def save(self, *args, **kwargs):
        super(POSMachine, self).save(*args, **kwargs)


class POSAccessLevel(BaseModel):
    pos = models.ForeignKey(POSMachine, on_delete=models.CASCADE, related_name='access_levels', null=True)
    name = models.CharField(max_length=100, null=True)
    in_province_sale = models.BooleanField(default=True)
    out_province_sale = models.BooleanField(default=True)
    cutting = models.BooleanField(default=True)
    freezing = models.BooleanField(default=True)
    warehouse = models.BooleanField(default=True)
    retail = models.BooleanField(default=True)
    active = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(POSAccessLevel, self).save(*args, **kwargs)


class POSAuditLog(models.Model):
    ACTION_CHOICES = (
        ('CHANGE_OWNER', 'تغییر مالک'),
        ('CHANGE_CURRENT_DISPENSER', 'تغییر توزیع کننده'),
        ('CHANGE_CURRENT_REPRESENTATIVE', 'تغییر نماینده'),
        ('ACCESSLEVEL_UPDATE', 'بروزرسانی سطح دسترسی'),
    )
    pos = models.ForeignKey(POSMachine, on_delete=models.CASCADE, related_name='audit_logs')
    action = models.CharField(max_length=50, choices=ACTION_CHOICES)
    performed = models.ForeignKey(SystemUserProfile, on_delete=models.SET_NULL, null=True)
    old_value = models.JSONField(null=True, blank=True)
    new_value = models.JSONField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    description = models.TextField(null=True, blank=True)

    def save(self, *args, **kwargs):
        super(POSAuditLog, self).save(*args, **kwargs)


class POSDeviceSession(BaseModel):
    pos = models.ForeignKey(
        POSMachine,
        on_delete=models.CASCADE,
        related_name="pos_device_session",
        null=True,
    )
    pos_unique_id = models.CharField(max_length=6, unique=True, null=True)
    version = models.IntegerField()
    mac = models.CharField(max_length=50)
    name = models.TextField(default="")
    sdk = models.TextField(default="")
    serial = models.TextField(default="")
    ip = models.CharField(max_length=15, default='0.0.0.0')
    password = models.CharField(max_length=10)
    session_create_date = models.DateTimeField(default=datetime.now())
    session_last_seen_date = models.DateTimeField(default=datetime.now())
    lng = models.FloatField(default=0)
    lot = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(POSDeviceSession, self).save(*args, **kwargs)


class POSTransactions(BaseModel):
    pos_machine = models.ForeignKey(
        POSMachine,
        on_delete=models.CASCADE,
        related_name="pos_machine_transaction",
        null=True
    )

    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="user_pos_transaction",
        null=True
    )
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_pos_transaction",
        null=True
    )
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="guild_pos_transaction",
        null=True
    )
    cooperative = models.ForeignKey(
        Cooperative,
        on_delete=models.CASCADE,
        related_name="cooperative_pos_transaction",
        null=True
    )
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="steward_pos_transaction",
        null=True
    )
    cart = models.CharField(max_length=20, null=True)
    mobile = models.CharField(max_length=20, null=True)
    terminal = models.CharField(max_length=100, null=True)
    posid = models.CharField(max_length=100, null=True)
    result = models.CharField(max_length=500, null=True)
    refnum = models.CharField(max_length=200, null=True)
    resnum = models.CharField(max_length=200, null=True)
    natcode = models.CharField(max_length=20, null=True)
    fullname = models.CharField(max_length=200, null=True)
    buy_date = models.BigIntegerField(null=True)
    date = models.BigIntegerField(null=True)
    product = models.JSONField(null=True)
    state = models.IntegerField(null=True)
    amount = models.FloatField(null=True)
    price = models.FloatField(null=True)
    lng = models.FloatField(null=True)
    lot = models.FloatField(null=True)
    checkout = models.BooleanField(null=True)
    registered = models.BooleanField(null=True)
    paid = models.BooleanField(null=True)

    def save(self, *args, **kwargs):
        super(POSTransactions, self).save(*args, **kwargs)


class AdditionalProducts(BaseModel):
    name = models.CharField(max_length=200, null=True)
    image = models.CharField(max_length=500, null=True)
    unit_of_measurement = models.CharField(max_length=20, null=True)
    selling_free_price = models.BooleanField(default=False)
    selling_approved_price = models.BooleanField(default=False)
    selling_more_than_inventory = models.BooleanField(default=False)
    selling_other_products = models.BooleanField(default=False)
    price = models.FloatField(default=0)
    priority = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(AdditionalProducts, self).save(*args, **kwargs)


class Product(BaseModel):
    name = models.CharField(max_length=200, null=True)
    image = models.CharField(max_length=500, null=True)
    unit_of_measurement = models.CharField(max_length=20, null=True)
    quantity = models.BigIntegerField(default=0)
    priority = models.IntegerField(default=0)
    sale_quantity = models.BigIntegerField(default=0)
    extra_sale_quantity = models.BigIntegerField(default=0)
    remain_quantity = models.BigIntegerField(default=0)
    weight = models.FloatField(default=0)
    sale_weight = models.FloatField(default=0)
    extra_sale_weight = models.FloatField(default=0)
    remain_weight = models.FloatField(default=0)
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_product",
        null=True
    )
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="guild_product",
        null=True
    )
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="steward_product",
        null=True
    )
    selling_free_price = models.BooleanField(default=False)
    selling_approved_price = models.BooleanField(default=False)
    selling_more_than_inventory = models.BooleanField(default=False)
    selling_other_products = models.BooleanField(default=False)
    general = models.BooleanField(default=False)
    show = models.BooleanField(default=False)
    price = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(Product, self).save(*args, **kwargs)


class PosVersion(BaseModel):
    build_id = models.BigIntegerField(default=0)
    latest_downloadlink = models.TextField()

    def save(self, *args, **kwargs):
        super(PosVersion, self).save(*args, **kwargs)


class PosItem(BaseModel):
    name = models.CharField(max_length=100, null=True)
    value = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(PosItem, self).save(*args, **kwargs)


class GuildRoom(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="guild_room_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="guild_room_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="guild_room_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="guild_room_wallet",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    identity_documents = models.JSONField(null=True)
    active = models.BooleanField(default=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(GuildRoom, self).save(*args, **kwargs)


class EvacuationPermit(BaseModel):
    type = models.CharField(max_length=100, default='optional')

    def save(self, *args, **kwargs):
        super(EvacuationPermit, self).save(*args, **kwargs)


class SellForFreezing(BaseModel):
    permission = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(SellForFreezing, self).save(*args, **kwargs)


class ColdHouseAllocations(BaseModel):
    cold_house = models.ForeignKey(
        ColdHouse,
        on_delete=models.CASCADE,
        related_name="cold_house_allocations_cold_house",
        null=True
    )
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="cold_house_allocations_kill_house",
        null=True
    )
    kill_house_request = models.ForeignKey(
        KillHouseRequest,
        on_delete=models.CASCADE,
        related_name="cold_house_allocations_kill_house_request",
        null=True
    )
    kill_house_ware_house = models.ForeignKey(
        KillHouseWareHouse,
        on_delete=models.CASCADE,
        related_name="cold_house_allocations_kill_house_ware_house",
        null=True
    )
    product = models.ForeignKey(
        RolesProducts,
        on_delete=models.CASCADE,
        related_name="cold_house_allocations_product",
        null=True
    )
    quantity = models.BigIntegerField(default=0)
    accepted_quantity = models.BigIntegerField(default=0)
    real_quantity = models.BigIntegerField(default=0)
    weight = models.FloatField(default=0)
    accepted_weight = models.FloatField(default=0)
    real_weight = models.FloatField(default=0)
    state = models.CharField(max_length=20, default='pending')
    allocation_type = models.CharField(max_length=50, null=True)
    reviewer = models.JSONField(null=True)
    date = models.DateTimeField(null=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(ColdHouseAllocations, self).save(*args, **kwargs)


class OperationLimitation(BaseModel):
    vet_farm_out_province_limitation = models.BooleanField(default=False)
    province_allocation_limitation = models.BooleanField(default=False)
    kill_house_allocation_limitation = models.BooleanField(default=False)
    kill_house_assignment_limitation = models.BooleanField(default=False)
    vet_check_kill_house_assignment_limitation = models.BooleanField(default=False)
    kill_house_input_bar_limitation = models.BooleanField(default=False)
    kill_house_steward_guild_allocation_limitation = models.BooleanField(default=False)
    kill_house_freezing_limitation = models.BooleanField(default=False)
    kill_house_free_sale_limitation = models.BooleanField(default=False)
    kill_house_free_buy_limitation = models.BooleanField(default=False)
    kill_house_vet_limitation = models.BooleanField(default=False)
    vet_farm_limitation = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(OperationLimitation, self).save(*args, **kwargs)


class ApprovedPrice(BaseModel):
    approved = models.BooleanField(default=False)
    lowest_price = models.FloatField(default=0)
    highest_price = models.FloatField(default=0)
    lowest_weight = models.FloatField(default=0)
    highest_weight = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(ApprovedPrice, self).save(*args, **kwargs)


class Announcements(BaseModel):
    active = models.BooleanField(default=False)
    description = models.TextField(max_length=2000, null=True)
    role = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(Announcements, self).save(*args, **kwargs)


class ChickenAgeRange(BaseModel):
    active = models.BooleanField(default=False)
    maximum = models.IntegerField(default=0)
    minimum = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(ChickenAgeRange, self).save(*args, **kwargs)


class TimeRange(BaseModel):
    time_range = models.JSONField(null=True)

    def save(self, *args, **kwargs):
        super(TimeRange, self).save(*args, **kwargs)


class SystemWallet(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="system_wallet_kill_house",
        null=True
    )
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="system_wallet_steward",
        null=True
    )
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="system_wallet_guild",
        null=True
    )
    balance = models.BigIntegerField(default=0)
    active = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(SystemWallet, self).save(*args, **kwargs)


class SuperAdmin(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="super_admin_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="super_admin_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="super_admin_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="super_admin_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(SuperAdmin, self).save(*args, **kwargs)


class PaymentGatewayPercentage(BaseModel):
    company = models.IntegerField(default=0)
    union = models.IntegerField(default=0)
    guilds = models.IntegerField(default=0)
    other = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(PaymentGatewayPercentage, self).save(*args, **kwargs)


class TotalPaymentGatewayPercentage(BaseModel):
    company = models.IntegerField(default=0)
    union = models.IntegerField(default=0)
    guilds = models.IntegerField(default=0)
    other = models.IntegerField(default=0)
    type = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(TotalPaymentGatewayPercentage, self).save(*args, **kwargs)


class SlaughterHouseTransaction(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="slaughter_house_transaction_kill_house",
        null=True
    )
    parent_kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="slaughter_house_transaction_parent_kill_house",
        null=True
    )
    amount = models.BigIntegerField(default=0)
    amount_with_tax = models.BigIntegerField(default=0)
    transaction = models.IntegerField(null=True)
    type = models.CharField(max_length=100, null=True)
    state = models.CharField(max_length=20, default='pending')
    union_share = models.BigIntegerField(default=0)
    company_share = models.BigIntegerField(default=0)
    guilds_share = models.BigIntegerField(default=0)
    city_share = models.BigIntegerField(default=0)
    wallet_share = models.BigIntegerField(default=0)
    other_share = models.BigIntegerField(default=0)
    pay_type = models.CharField(max_length=200, null=True)
    saleReferenceId = models.CharField(max_length=100, null=True)
    refId = models.CharField(max_length=100, null=True)
    orderId = models.CharField(max_length=100, null=True)
    cardHolderPan = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(SlaughterHouseTransaction, self).save(*args, **kwargs)


class ShareType(BaseModel):
    name = models.CharField(max_length=500, null=True)
    en_name = models.CharField(max_length=500, null=True)
    account_id = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(ShareType, self).save(*args, **kwargs)


class WageType(BaseModel):
    name = models.CharField(max_length=500, null=True)
    en_name = models.CharField(max_length=500, null=True)
    amount = models.BigIntegerField(default=0)
    status = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(WageType, self).save(*args, **kwargs)


class PercentageOfWageType(BaseModel):
    wage_type = models.ForeignKey(
        WageType,
        on_delete=models.CASCADE,
        related_name="wage_type_percentage",
        null=True
    )
    share_type = models.ForeignKey(
        ShareType,
        on_delete=models.CASCADE,
        related_name="share_type_percentage",
        null=True
    )
    percent = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(PercentageOfWageType, self).save(*args, **kwargs)


class SubSectorPercentageOfWageType(BaseModel):
    percentage_of_wage_type = models.ForeignKey(
        PercentageOfWageType,
        on_delete=models.CASCADE,
        related_name="sub_sector_wage_type",
        null=True
    )
    city = models.BooleanField(default=False)
    percent = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(SubSectorPercentageOfWageType, self).save(*args, **kwargs)


class TotalWageInformation(BaseModel):
    total_amount = models.BigIntegerField(default=0)
    total_unpaid_amount = models.BigIntegerField(default=0)
    total_paid_amount = models.BigIntegerField(default=0)
    total_weight = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(TotalWageInformation, self).save(*args, **kwargs)


class InternalTransaction(BaseModel):
    date = models.DateTimeField(auto_now_add=True)
    amount = models.BigIntegerField(default=0)
    amount_with_tax = models.BigIntegerField(default=0)
    status = models.CharField(max_length=100, default='pending')
    transaction_type = models.CharField(max_length=100, null=True)
    type = models.CharField(max_length=100, null=True)
    payer_type = models.CharField(max_length=100, null=True)
    message = models.TextField(null=True)
    saleReferenceId = models.CharField(max_length=100, null=True)
    refId = models.CharField(max_length=100, null=True)
    authority = models.CharField(max_length=100, null=True)
    orderId = models.CharField(max_length=100, null=True)
    cardHolderPan = models.CharField(max_length=100, null=True)
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        null=True,
        related_name="internal_transaction_kill_house_user"
    )
    parent_kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="internal_transaction_parent_kill_house",
        null=True
    )
    chain_company = models.ForeignKey(
        ChainCompany,
        on_delete=models.CASCADE,
        null=True,
        related_name="internal_transaction_chain_company_user"
    )
    poultry_request = models.ManyToManyField(
        PoultryRequest,
        related_name='poutry_request_transactions',
        null=True, blank=True)
    poultry = models.ForeignKey(
        Poultry,
        on_delete=models.CASCADE,
        null=True,
        related_name="internal_transaction_poultry"
    )
    out_province_poultry_request_buyer = models.ForeignKey(
        OutProvincePoultryRequestBuyer,
        on_delete=models.CASCADE,
        null=True,
        related_name="internal_transaction_out_province_poultry_request_buyer"
    )
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        null=True,
        related_name="internal_transaction_user"
    )

    union_share = models.BigIntegerField(default=0)
    union_account = models.CharField(max_length=50, null=True)
    company_share = models.BigIntegerField(default=0)
    company_account = models.CharField(max_length=50, null=True)
    guilds_share = models.BigIntegerField(default=0)
    guilds_account = models.CharField(max_length=50, null=True)
    city_share = models.BigIntegerField(default=0)
    city_account = models.CharField(max_length=50, null=True)
    wallet_share = models.BigIntegerField(default=0)
    wallet_account = models.CharField(max_length=50, null=True)
    other_share = models.BigIntegerField(default=0)
    other_account = models.CharField(max_length=50, null=True)
    link_pay = models.BooleanField(default=False)
    description = models.TextField(null=True)
    image = models.CharField(max_length=500, null=True)
    payer_fullname = models.CharField(max_length=200, null=True)
    payer_mobile = models.CharField(max_length=11, null=True)
    temporary_trash = models.BooleanField(default=False)
    temporary_deleted = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(InternalTransaction, self).save(*args, **kwargs)


class CityLivestock(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="city_livestock_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="city_livestock_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="city_livestock_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="city_livestock_wallet",
        null=True
    )
    user_id_foreign_key = models.IntegerField(null=True)
    address_id_foreign_key = models.IntegerField(null=True)
    user_bank_id_foreign_key = models.IntegerField(null=True)
    wallet_id_foreign_key = models.IntegerField(null=True)
    city_jahad_id_key = models.IntegerField(null=True)
    identity_documents = models.JSONField(null=True)
    active = models.BooleanField(default=True)
    city_number = models.IntegerField(default=0)
    city_name = models.CharField(max_length=100, null=True)
    province_number = models.IntegerField(default=0)
    province_name = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(CityLivestock, self).save(*args, **kwargs)


class ImprovingLivestock(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="improving_livestock_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="improving_livestock_address",
        null=True
    )
    user_bank_info = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="improving_livestock_bank_info",
                                       null=True)
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="improving_livestock_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(ImprovingLivestock, self).save(*args, **kwargs)


class ReportSubmissionTime(BaseModel):
    type = models.CharField(max_length=100, null=True)
    hour = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(ReportSubmissionTime, self).save(*args, **kwargs)


class Reports(BaseModel):
    title = models.CharField(max_length=500, null=True)
    end_point = models.CharField(max_length=50, null=True)
    active = models.BooleanField(default=False)
    description = models.TextField(null=True)

    def save(self, *args, **kwargs):
        super(Reports, self).save(*args, **kwargs)


class ReportsUsers(BaseModel):
    active = models.BooleanField(default=True)
    firstname = models.CharField(max_length=100, null=True)
    lastname = models.CharField(max_length=100, null=True)
    fullname = models.CharField(max_length=100, null=True)
    city = models.CharField(max_length=100, null=True)
    position = models.CharField(max_length=100, null=True)
    mobile = models.CharField(max_length=11, null=True)
    user_token = models.CharField(max_length=4, null=True, unique=True)

    def save(self, *args, **kwargs):

        self.fullname = self.firstname + " " + self.lastname
        if self.user_token is None:
            while (True):
                res = ''.join(random.choices(string.ascii_lowercase + string.digits, k=3))
                res = base_user_gate_way_id + res
                if not ReportsUsers.objects.filter(user_token=res).exists():
                    break
            self.user_token = res
        super(ReportsUsers, self).save(*args, **kwargs)


class UserReports(BaseModel):
    active = models.BooleanField(default=True)
    user = models.ForeignKey(
        ReportsUsers,
        on_delete=models.CASCADE,
        related_name="user_reports_user",
        null=True
    )
    report = models.ForeignKey(
        Reports,
        on_delete=models.CASCADE,
        related_name="report_user_reports",
        null=True
    )

    def save(self, *args, **kwargs):
        super(UserReports, self).save(*args, **kwargs)


class MovingTextDashboardStatus(BaseModel):
    active = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(MovingTextDashboardStatus, self).save(*args, **kwargs)


class MovingTextRole(BaseModel):
    role = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(MovingTextRole, self).save(*args, **kwargs)


class MovingText(BaseModel):
    moving_text = models.TextField(null=True)
    active = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(MovingText, self).save(*args, **kwargs)


class MovingTextWithRole(BaseModel):
    moving_text = models.ForeignKey(
        MovingText,
        on_delete=models.CASCADE,
        related_name="role_moving_text",
        null=True
    )
    role = models.ForeignKey(
        MovingTextRole,
        on_delete=models.CASCADE,
        related_name="moving_text_role",
        null=True
    )
    active = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(MovingTextWithRole, self).save(*args, **kwargs)


class AdminX(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="admin_x_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="admin_x_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="admin_x_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="admin_x_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(AdminX, self).save(*args, **kwargs)


class Supporter(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="supporter_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="supporter_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="supporter_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="supporter_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(Supporter, self).save(*args, **kwargs)


class ZarinPalAccounts(BaseModel):
    name = models.CharField(max_length=50, null=True)
    en_name = models.CharField(max_length=50, null=True)
    account = models.CharField(max_length=50, null=True)

    def save(self, *args, **kwargs):
        super(ZarinPalAccounts, self).save(*args, **kwargs)


class PoultryExport(BaseModel):
    allow = models.BooleanField(default=False)
    limitation_status = models.BooleanField(default=False)
    limitation = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(PoultryExport, self).save(*args, **kwargs)


class PoultryOutProvinceRequest(BaseModel):
    allow = models.BooleanField(default=False)
    limitation_status = models.BooleanField(default=False)
    limitation = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(PoultryOutProvinceRequest, self).save(*args, **kwargs)


class VetFarmAggregatePermission(BaseModel):
    allow = models.BooleanField(default=False)
    limitation = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(VetFarmAggregatePermission, self).save(*args, **kwargs)


class KillHouseBarLimitation(BaseModel):
    allow = models.BooleanField(default=False)
    limitation = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(KillHouseBarLimitation, self).save(*args, **kwargs)


class KillHouseStewardGuildRelation(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_relation",
        null=True
    )
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="guild_relation",
        null=True
    )
    steward = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="steward_relation",
        null=True
    )

    allocation_limit = models.IntegerField(default=0)
    allocation_type = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(KillHouseStewardGuildRelation, self).save(*args, **kwargs)


class OutOfProvinceSellingCarcassesPermission(BaseModel):
    active = models.BooleanField(default=False)
    type = models.CharField(max_length=100, null=True)
    percent = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(OutOfProvinceSellingCarcassesPermission, self).save(*args, **kwargs)


class ProductPricingType(BaseModel):
    province = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(ProductPricingType, self).save(*args, **kwargs)


class KillHousePricePermission(BaseModel):
    allow = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(KillHousePricePermission, self).save(*args, **kwargs)


class IranProvinces(models.Model):
    name = models.CharField(max_length=250)
    slug = models.CharField(max_length=250)
    tel_prefix = models.CharField(max_length=3, null=True)


class IranCities(models.Model):
    name = models.CharField(max_length=250)
    slug = models.CharField(max_length=250)
    province_id = models.ForeignKey(IranProvinces, on_delete=models.CASCADE)


class AutomaticDirectBuyingPermission(models.Model):
    allow = models.BooleanField(default=False)
    start_time = models.TimeField(null=True)
    end_time = models.TimeField(null=True)

    def save(self, *args, **kwargs):
        super(AutomaticDirectBuyingPermission, self).save(*args, **kwargs)


class AgeNotificationPoultry(BaseModel):
    message = models.TextField()
    poultry_age = models.CharField(max_length=2)
    losses_percent = models.IntegerField(null=True)

    def save(self, *args, **kwargs):
        super(AgeNotificationPoultry, self).save(*args, **kwargs)


class PosMachineTransactions(BaseModel):
    pos = models.ForeignKey(
        POSMachine,
        on_delete=models.CASCADE,
        related_name="roles_products_pos_transactions",
        null=True
    )

    product = models.ForeignKey(
        RolesProducts,
        on_delete=models.CASCADE,
        related_name="roles_products_pos_transactions",
        null=True
    )
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="transaction_kill_house",
        null=True
    )
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="transaction_guild",
        null=True
    )
    check_id = models.CharField(max_length=100, null=True, unique=True)
    mobile = models.CharField(max_length=100, null=True, blank=True)
    natcode = models.CharField(max_length=100, null=True, blank=True)
    fullname = models.CharField(max_length=100, null=True, blank=True)
    date = models.DateTimeField(null=True)
    pos_date = models.BigIntegerField(default=0)
    price = models.BigIntegerField(default=0)
    price_paid = models.BigIntegerField(default=0)
    price_type = models.CharField(max_length=50, null=True, blank=True)
    paid = models.BooleanField(default=False)
    state = models.IntegerField(default=0)
    posProvider = models.CharField(max_length=100, null=True)
    result = models.CharField(max_length=1000, null=True)
    refnum = models.CharField(max_length=100, null=True, blank=True)
    terminal = models.CharField(max_length=100, null=True, blank=True)
    cart = models.CharField(max_length=100, null=True, blank=True)
    lng = models.FloatField(default=0)
    lot = models.FloatField(default=0)
    # additional = models.CharField(max_length=200, null=True, blank=True)
    additional = models.TextField(null=True, blank=True)
    current_price = models.IntegerField(default=0)
    checkout = models.BooleanField(default=False)
    weight = models.IntegerField(default=0)
    hasTry = models.BooleanField(default=False)
    live_stock = models.BooleanField(default=False)
    warehouse = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(PosMachineTransactions, self).save(*args, **kwargs)


class ProductsTransactions(BaseModel):
    product = models.ForeignKey(
        RolesProducts,
        on_delete=models.CASCADE,
        related_name="products_transactions_product",
        null=True
    )
    other_product = models.ForeignKey(
        OtherProducts,
        on_delete=models.CASCADE,
        related_name="products_transactions_other_product",
        null=True
    )
    transaction = models.ForeignKey(
        PosMachineTransactions,
        on_delete=models.CASCADE,
        related_name="transaction_products_transactions",
        null=True
    )
    live_stack_products = models.ForeignKey(
        LiveStockRolseProduct,
        on_delete=models.CASCADE,
        related_name="products_transactions_live_stack_products",
        null=True
    )
    price = models.BigIntegerField(default=0)
    cur_price = models.BigIntegerField(default=0)
    cur_weight = models.BigIntegerField(default=0)
    total_price = models.BigIntegerField(default=0)
    price_approved = models.BooleanField(default=False)
    name = models.CharField(max_length=300, null=True)
    image = models.CharField(max_length=500, null=True)
    unit = models.CharField(max_length=10, null=True)
    targetunit = models.CharField(max_length=10, null=True)
    warehouse = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(ProductsTransactions, self).save(*args, **kwargs)


class PosAllocationTransactions(BaseModel):
    pos = models.ForeignKey(
        POSMachine,
        on_delete=models.CASCADE,
        related_name="steward_allocation_pos_transactions",
        null=True
    )

    allocation = models.ForeignKey(
        StewardAllocation,
        on_delete=models.CASCADE,
        related_name="steward_allocation_transactions",
        null=True
    )
    check_id = models.CharField(max_length=100, null=True)
    date = models.DateTimeField(null=True)
    price = models.BigIntegerField(default=0)
    paid = models.BooleanField(default=False)
    state = models.IntegerField(default=0)
    posProvider = models.CharField(max_length=100, null=True)
    result = models.TextField(null=True)
    refnum = models.CharField(max_length=100, null=True, blank=True)
    terminal = models.CharField(max_length=100, null=True, blank=True)
    cart = models.CharField(max_length=100, null=True, blank=True)
    lng = models.FloatField(default=0)
    lot = models.FloatField(default=0)
    additional = models.CharField(max_length=200, null=True, blank=True)

    def save(self, *args, **kwargs):
        super(PosAllocationTransactions, self).save(*args, **kwargs)


class BroadcastPrice(BaseModel):
    active = models.BooleanField(default=False)
    kill_house_price = models.FloatField(default=0)
    steward_price = models.FloatField(default=0)
    guild_price = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(BroadcastPrice, self).save(*args, **kwargs)


class OutProvinceSaleLimitation(BaseModel):
    active = models.BooleanField(default=False)
    kill_house = models.FloatField(default=0)
    steward = models.FloatField(default=0)
    guild = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(OutProvinceSaleLimitation, self).save(*args, **kwargs)


class ParentCompany(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="parent_company_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="parent_company_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="parent_company_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="parent_company_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(ParentCompany, self).save(*args, **kwargs)


class CityGuild(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="city_guild_user",
        null=True
    )
    address = models.ForeignKey(
        SystemAddress,
        on_delete=models.CASCADE,
        related_name="city_guild_address",
        null=True
    )
    user_bank_info = models.ForeignKey(
        BankCard,
        on_delete=models.CASCADE,
        related_name="city_guild_bank_info",
        null=True
    )
    wallet = models.ForeignKey(
        Wallet,
        on_delete=models.CASCADE,
        related_name="city_guild_wallet",
        null=True
    )

    wallet_amount = models.BigIntegerField(default=0)
    unit_name = models.CharField(max_length=200, null=True)

    def save(self, *args, **kwargs):
        super(CityGuild, self).save(*args, **kwargs)


class SubSectorTransactions(BaseModel):
    city_operator = models.ForeignKey(
        CityOperator,
        on_delete=models.CASCADE,
        related_name="city_operator_transactions",
        null=True
    )
    vet = models.ForeignKey(
        Vet,
        on_delete=models.CASCADE,
        related_name="vet_transactions",
        null=True
    )
    city_guild = models.ForeignKey(
        CityGuild,
        on_delete=models.CASCADE,
        related_name="city_guild_transactions",
        null=True
    )
    date = models.DateTimeField(null=True)
    from_account = models.CharField(max_length=200, null=True)
    to_account = models.CharField(max_length=200, null=True)
    type = models.CharField(max_length=50, null=True)
    amount = models.BigIntegerField(default=0)
    image = models.CharField(max_length=500, null=True)

    def save(self, *args, **kwargs):
        super(SubSectorTransactions, self).save(*args, **kwargs)


class PosSegmentation(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_segmentation",
        null=True
    )
    steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="steward_segmentation",
        null=True
    )
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="guild_segmentation",
        null=True
    )
    product = models.ForeignKey(
        RolesProducts,
        on_delete=models.CASCADE,
        related_name="product_segmentation",
        null=True
    )

    to_guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="to_guild_segmentation",
        null=True
    )
    to_steward = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="to_steward_segmentation",
        null=True
    )
    dispenser = models.ForeignKey(
        Dispenser,
        on_delete=models.CASCADE,
        related_name="segmentation_dispenser",
        null=True
    )
    representative = models.ForeignKey(
        Representative,
        on_delete=models.CASCADE,
        related_name="segmentation_representative",
        null=True
    )
    date = models.DateTimeField(null=True)
    weight = models.BigIntegerField(default=0)
    sale_type = models.CharField(max_length=20, null=True)
    quota = models.CharField(max_length=20, null=True)
    warehouse = models.BooleanField(default=True)
    production_date = models.DateTimeField(null=True)
    distribution_type = models.CharField(max_length=20, null=True)
    amount = models.BigIntegerField(default=0)
    total_amount = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(PosSegmentation, self).save(*args, **kwargs)


class PoultryPrediction(BaseModel):
    poultry = models.ForeignKey(
        Poultry,
        on_delete=models.CASCADE,
        related_name="prediction_poultry",
        null=True
    )
    date = models.DateTimeField(null=True)
    killing_ave_age = models.IntegerField(default=1)
    active_left_over = models.IntegerField(default=0)
    killing_ave_count = models.IntegerField(default=0)
    killing_ave_weight = models.IntegerField(default=0)
    killing_loss_weight_percent = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(PoultryPrediction, self).save(*args, **kwargs)


class BarDifferenceRequest(BaseModel):
    hatching = models.ForeignKey(
        PoultryHatching,
        on_delete=models.CASCADE,
        related_name="difference_hatching",
        null=True
    )
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="difference_kill_house",
        null=True
    )
    state = models.CharField(max_length=20, default='pending')
    register_fullname = models.CharField(max_length=200, null=True)
    register_mobile = models.CharField(max_length=200, null=True)
    register_message = models.TextField(null=True)
    quantity = models.IntegerField(default=0)
    real_quantity = models.IntegerField(default=0)
    weight = models.IntegerField(default=0)
    real_weight = models.IntegerField(default=0)
    violation_image = models.JSONField(null=True)
    acceptor_image = models.JSONField(null=True)
    acceptor_fullname = models.CharField(max_length=200, null=True)
    acceptor_mobile = models.CharField(max_length=200, null=True)
    acceptor_date = models.DateTimeField(null=True)
    acceptor_message = models.TextField(null=True)

    def save(self, *args, **kwargs):
        super(BarDifferenceRequest, self).save(*args, **kwargs)


class CookieSamasat(models.Model):
    cookie = models.TextField(null=True)
    table_name = models.CharField(max_length=100, null=True, blank=True)

    def save(self, *args, **kwargs):
        super(CookieSamasat, self).save(*args, **kwargs)


class LiveChickenTransportDetails(BaseModel):
    kill_house = models.ForeignKey(KillHouse, on_delete=models.CASCADE, null=True)
    hatching = models.ForeignKey(PoultryHatching, on_delete=models.CASCADE, null=True)
    quarantine_code = models.CharField(max_length=100, null=True)
    rejester_date = models.DateTimeField(null=True)
    quantity = models.IntegerField(null=True)
    state = models.CharField(max_length=100, null=True)
    product_name = models.CharField(max_length=200, null=True)
    kill_house_unique_identifier = models.CharField(max_length=100, null=True)
    kill_house_name = models.CharField(max_length=100, null=True)
    status_registration_date = models.DateTimeField(null=True)

    def save(self, *args, **kwargs):
        super(LiveChickenTransportDetails, self).save(*args, **kwargs)


class RequestLimitation(BaseModel):
    limitation = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(RequestLimitation, self).save(*args, **kwargs)


class PriceConfirmation(BaseModel):
    poultry_status = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(PriceConfirmation, self).save(*args, **kwargs)


class ArchiveWageInfo(BaseModel):
    start_year = models.DateTimeField(null=True)
    end_year = models.DateTimeField(null=True)
    total_wage = models.BigIntegerField(default=0)
    total_paid_wage = models.BigIntegerField(default=0)
    total_company_paid_wage = models.BigIntegerField(default=0)
    total_union_paid_wage = models.BigIntegerField(default=0)
    total_guild_paid_wage = models.BigIntegerField(default=0)
    total_vet_paid_wage = models.BigIntegerField(default=0)

    def save(self, *args, **kwargs):
        super(ArchiveWageInfo, self).save(*args, **kwargs)


class CompanyBeneficiaryAccount(BaseModel):
    name = models.CharField(max_length=200, null=True)
    shaba = models.CharField(max_length=200, null=True)
    percent = models.BigIntegerField(default=0)
    in_province = models.BooleanField(default=False)
    out_province = models.BooleanField(default=False)
    unique_code = models.IntegerField(null=True)

    def save(self, *args, **kwargs):
        if self.unique_code is None:
            last_uniq = CompanyBeneficiaryAccount.objects.filter().last().unique_code
            self.unique_code = last_uniq + 1
        super(CompanyBeneficiaryAccount, self).save(*args, **kwargs)


class HatchingIncreaseRequest(BaseModel):
    hatching = models.ForeignKey(PoultryHatching, on_delete=models.CASCADE,
                                 related_name="increase_hatching",
                                 null=True)

    hatching_quantity = models.IntegerField(null=True)
    quantity = models.IntegerField(null=True)
    hatching_kill_quantity = models.IntegerField(null=True)
    hatching_left_over = models.IntegerField(null=True)
    hatching_losses = models.IntegerField(null=True)
    message = models.TextField(null=True)
    registerer_name = models.CharField(max_length=250, null=True)
    registerer_role = models.CharField(max_length=50, null=True)
    registerer_mobile = models.CharField(max_length=20, null=True)
    date = models.DateTimeField(auto_now_add=True)

    def save(self, *args, **kwargs):
        super(HatchingIncreaseRequest, self).save(*args, **kwargs)


class ChickenCommissionPrices(BaseModel):
    kill_house_price = models.IntegerField(default=15000)
    wholesaler_price = models.IntegerField(default=20000)
    retailer_price = models.IntegerField(default=40000)
    chicken_average_price = models.IntegerField(default=0)
    province_kill_request_amount = models.IntegerField(default=0)
    poultry_request_amount = models.IntegerField(default=0)
    kill_request_amount = models.IntegerField(default=0)
    date = models.DateTimeField(null=True)

    def save(self, *args, **kwargs):
        super(ChickenCommissionPrices, self).save(*args, **kwargs)


class CommonlyUsed(BaseModel):
    kill_house = models.ForeignKey(KillHouse, on_delete=models.CASCADE,
                                   related_name="commonly_used_kill_house",
                                   null=True)

    guild = models.ForeignKey(Guilds, on_delete=models.CASCADE,
                              related_name="commonly_used_guild",
                              null=True)

    steward = models.ForeignKey(Guilds, on_delete=models.CASCADE,
                                related_name="commonly_used_steward",
                                null=True)
    priority = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(CommonlyUsed, self).save(*args, **kwargs)


class PercentageDropLimitation(BaseModel):
    kill_house_limit_percent_down = models.IntegerField(default=0)
    kill_house_limit_percent_up = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(PercentageDropLimitation, self).save(*args, **kwargs)


class UploadImageLimitation(BaseModel):
    kill_house_allocation = models.BooleanField(default=False)
    kill_house_free_sale = models.BooleanField(default=False)
    steward_allocation = models.BooleanField(default=False)
    steward_free_sale = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(UploadImageLimitation, self).save(*args, **kwargs)


class ApkInfo(BaseModel):
    info = models.JSONField(null=True)
    download_link = models.CharField(max_length=700, null=True, blank=True)

    def save(self, *args, **kwargs):
        super(ApkInfo, self).save(*args, **kwargs)


class DirectBuyingVerification(BaseModel):
    poultry_code_mandatory = models.BooleanField(default=False)
    payment_deadline = models.BooleanField(default=False)
    payment_deadline_days = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        super(DirectBuyingVerification, self).save(*args, **kwargs)


class DirectBuyingPayment(BaseModel):
    province_kill_request = models.ForeignKey(ProvinceKillRequest, on_delete=models.CASCADE,
                                              related_name="direct_buying_province_kill_request",
                                              null=True)
    amount = models.BigIntegerField(default=0)
    image = models.CharField(max_length=700, null=True)
    date = models.DateTimeField(null=True)
    payment_registrar = models.CharField(max_length=200, null=True)
    payment_registrar_mobile = models.CharField(max_length=20, null=True)
    payment_deadline_supporter_message = models.TextField(null=True)

    def save(self, *args, **kwargs):
        super(DirectBuyingPayment, self).save(*args, **kwargs)


class FinePermission(BaseModel):
    out_poultry_request = models.BooleanField(default=False)
    out_poultry_request_start_time = models.TimeField(null=True)
    out_poultry_request_end_time = models.TimeField(null=True)
    out_poultry_request_fine_coefficient = models.FloatField(default=0)
    direct_buying = models.BooleanField(default=False)
    direct_buying_start_time = models.TimeField(null=True)
    direct_buying_end_time = models.TimeField(null=True)
    direct_buying_fine_coefficient = models.FloatField(default=0)
    in_province_allocations = models.BooleanField(default=False)
    in_province_allocations_fine_coefficient = models.FloatField(default=0)
    in_province_bars = models.BooleanField(default=False)
    in_province_bars_fine_coefficient = models.FloatField(default=0)
    name = models.CharField(max_length=200, null=True)
    start_time = models.TimeField(null=True)
    end_time = models.TimeField(null=True)
    fine_coefficient = models.FloatField(default=0)
    fine = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(FinePermission, self).save(*args, **kwargs)


class ManagementSendSms(BaseModel):
    name = models.CharField(max_length=100, null=True)
    username = models.CharField(max_length=100, null=True)
    password = models.CharField(max_length=100, null=True)


class ShowMarketRequest(BaseModel):
    allow = models.BooleanField(default=False)
    start_time = models.TimeField(null=True)
    end_time = models.TimeField(null=True)

    def save(self, *args, **kwargs):
        super(ShowMarketRequest, self).save(*args, **kwargs)


class ManagementHatchingAgeRange(BaseModel):
    from_age = models.SmallIntegerField(default=0, max_length=3)
    to_age = models.SmallIntegerField(default=0, max_length=3)
    from_weight = models.FloatField(default=0)
    to_weight = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(ManagementHatchingAgeRange, self).save(*args, **kwargs)


class IndexWeightCategory(BaseModel):
    name = models.CharField(max_length=100, null=True)
    min_value = models.FloatField(default=0)
    max_value = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(IndexWeightCategory, self).save(*args, **kwargs)


class PoultryScience(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="poultry_science_user",
        null=True
    )
    poultry = models.ManyToManyField(
        Poultry,
        related_name="poultry_entries",
    )
    engineering_code = models.CharField(max_length=50, null=True, blank=True)

    def save(self, *args, **kwargs):
        super(PoultryScience, self).save(*args, **kwargs)


class PoultryScienceReport(BaseModel):
    poultry_science = models.ForeignKey(
        PoultryScience,
        on_delete=models.CASCADE,
        related_name="poultry_science_reporter",
        null=True
    )
    hatching = models.ForeignKey(
        PoultryHatching,
        on_delete=models.CASCADE,
        related_name="poultry_science_hatching",
        null=True
    )
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="poultry_science_report_user",
        null=True
    )
    date = models.DateTimeField(null=True)
    image = models.JSONField(null=True)
    lat = models.FloatField(null=True)
    log = models.FloatField(null=True)
    reporter_fullname = models.CharField(null=True, max_length=50)
    reporter_mobile = models.CharField(null=True, max_length=11)
    reporter_role = models.CharField(null=True, max_length=50)
    state = models.CharField(default='pending', max_length=50)
    real_quantity_ai = models.IntegerField(null=True)
    message_ai = models.CharField(null=True, max_length=250)
    real_quantity = models.IntegerField(null=True)
    message = models.CharField(null=True, max_length=250)
    message_registerer_fullname = models.CharField(null=True, max_length=50)
    message_registerer_mobile = models.CharField(null=True, max_length=11)
    message_registerer_role = models.CharField(null=True, max_length=50)
    report_information = models.JSONField(null=True)
    report_id = models.IntegerField(null=True)
    chicken_age = models.IntegerField(null=True)

    def save(self, *args, **kwargs):
        if self.report_id is None:
            last = PoultryScienceReport.objects.filter().last()
            if last:
                last_report = last.report_id
            else:
                last_report = 1000
            self.report_id = last_report + 1
        super(PoultryScienceReport, self).save(*args, **kwargs)


class TokenEitaaForEachVet(models.Model):
    token = models.CharField(max_length=250)
    vet_fullname = models.CharField(max_length=50)
    vet_mobile = models.CharField(max_length=11)
    group_name = models.CharField(max_length=250)

    def save(self, *args, **kwargs):
        super(TokenEitaaForEachVet, self).save(*args, **kwargs)


class UserLoginLog(models.Model):
    user_profile = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="login_logs_user"
    )
    headers = models.JSONField(null=True, blank=True)
    ip_address = models.CharField(max_length=500, null=True, blank=True)
    user_agent = models.CharField(max_length=500, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def save(self, *args, **kwargs):
        super(UserLoginLog, self).save(*args, **kwargs)


class AllowRegisterCodeForStewardAllocation(BaseModel):
    active = models.BooleanField(default=False)
    time = models.CharField(max_length=50, null=True)

    def save(self, *args, **kwargs):
        super(AllowRegisterCodeForStewardAllocation, self).save(*args, **kwargs)


class AllowRegisterCodeForGuilds(BaseModel):
    active = models.BooleanField(default=False)
    time = models.CharField(max_length=50, null=True)
    has_time = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(AllowRegisterCodeForGuilds, self).save(*args, **kwargs)


class StewardAppLogin(BaseModel):
    steward = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="steward_login",
        null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    device_name = models.CharField(max_length=200, null=True)

    def save(self, *args, **kwargs):
        super(StewardAppLogin, self).save(*args, **kwargs)


class StewardRequest(BaseModel):
    steward = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="steward_request",
        null=True)
    date = models.DateTimeField(null=True)
    quantity = models.IntegerField(default=0)
    weight = models.IntegerField(default=0)
    state = models.CharField(default='pending', max_length=50)
    registerer = models.CharField(null=True, max_length=250)
    registerer_mobile = models.CharField(null=True, max_length=250)
    registerer_role = models.CharField(null=True, max_length=250)

    def save(self, *args, **kwargs):
        super(StewardRequest, self).save(*args, **kwargs)


class RestrictionCarcassDistribution(BaseModel):
    allow = models.BooleanField(default=False)
    time = models.TimeField(null=True)
    distribution_type = models.CharField(max_length=50, default='KillHouse')
    out = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(RestrictionCarcassDistribution, self).save(*args, **kwargs)


class AllowRegisterCodeForKillHouseFreeSaleBarInformation(BaseModel):
    active = models.BooleanField(default=False)
    time = models.CharField(max_length=50, null=True)

    def save(self, *args, **kwargs):
        super(AllowRegisterCodeForKillHouseFreeSaleBarInformation, self).save(*args, **kwargs)


class AllowRegisterCodeForStewardFreeSaleBarInformation(BaseModel):
    active = models.BooleanField(default=False)
    time = models.CharField(max_length=50, null=True)

    def save(self, *args, **kwargs):
        super(AllowRegisterCodeForStewardFreeSaleBarInformation, self).save(*args, **kwargs)


class LimitationForDirectPurchaseAndBarInformation(BaseModel):
    active = models.BooleanField(default=False)
    start_time = models.TimeField(null=True, blank=True)
    end_time = models.TimeField(null=True, blank=True)
    allow_buying = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(LimitationForDirectPurchaseAndBarInformation, self).save(*args, **kwargs)


class WarehouseArchive(BaseModel):
    kill_house = models.ForeignKey(
        KillHouse,
        on_delete=models.CASCADE,
        related_name="kill_house_archive",
        null=True)
    steward = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="steward_archive",
        null=True)
    stewards = models.ForeignKey(
        Steward,
        on_delete=models.CASCADE,
        related_name="stewards_archive",
        null=True)
    guild = models.ForeignKey(
        Guilds,
        on_delete=models.CASCADE,
        related_name="guild_archive",
        null=True)
    date = models.DateTimeField(null=True)
    weight = models.FloatField(default=0)
    image = models.CharField(max_length=500, null=True)
    quota = models.CharField(max_length=20, null=True)
    description = models.TextField(null=True)
    registerer = models.CharField(null=True, max_length=250)
    registerer_mobile = models.CharField(null=True, max_length=250)
    registerer_role = models.CharField(null=True, max_length=250)
    warehouse = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(WarehouseArchive, self).save(*args, **kwargs)


class SmsRecipient(BaseModel):
    phone_number = models.CharField(max_length=11, unique=True)
    name = models.CharField(max_length=100, null=True)
    is_active = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        super(SmsRecipient, self).save(*args, **kwargs)


class PosDeviceSetting(BaseModel):
    pos_machin = models.CharField(max_length=100, null=True)
    owner = models.TextField(null=True)
    serial = models.TextField(null=True)
    key_id = models.TextField(null=True)
    key_value = models.TextField(null=True)
    last_modify = models.DateTimeField(null=True)

    def save(self, *args, **kwargs):
        super(PosDeviceSetting, self).save(*args, **kwargs)


class MarketDailyLimitation(BaseModel):
    quantity = models.IntegerField(default=0)
    active = models.BooleanField(default=False)
    register_role = models.CharField(max_length=50, null=True)
    register_fullname = models.CharField(max_length=100, null=True)
    register_mobile = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(MarketDailyLimitation, self).save(*args, **kwargs)


class HatchingArchivePercent(BaseModel):
    active = models.BooleanField(default=False)
    percent = models.FloatField(default=0)

    def save(self, *args, **kwargs):
        super(HatchingArchivePercent, self).save(*args, **kwargs)



class RequestLog(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        null=True,
        blank=True,
        on_delete=models.SET_NULL
    )
    method = models.CharField(max_length=10)
    path = models.TextField()
    query_params = models.JSONField(null=True, blank=True)
    body = models.JSONField(null=True, blank=True)
    headers = models.JSONField(null=True, blank=True)

    status_code = models.PositiveIntegerField(null=True)
    ip_address = models.GenericIPAddressField(null=True, blank=True)

    duration = models.FloatField(help_text="Request duration in seconds")
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.method} {self.path} ({self.status_code})"