import datetime

from django.db.models import Q, Sum, F

from rest_framework import serializers

from deposit_percent import wage_percent, carcases_sell
from panel.models import ReportingFieldRoleLimitation, ProvinceKillRequest, KillHousePercentage, \
    KillHouseRequest, KillHouseFreeBarInformation, InternalTransaction, ChainAllocation, KillHouse, WageType, \
    PercentageOfWageType, KillHouseFreeSaleBarInformation, IranProvinces, IranCities, AgeNotificationPoultry, \
    CookieSamasat, HatchingLossManagement
from authentication.serializers import GroupSerializer
from .models import SearchFields


# class ReportingPanelSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = PoultryRequest


class ReportingFieldLimitationSerializer(serializers.ModelSerializer):
    role_name = GroupSerializer(required=False)

    class Meta:
        model = ReportingFieldRoleLimitation
        fields = [
            'role_name',
            'fields'
        ]

    def create(self, validated_data):
        return ReportingFieldRoleLimitation.objects.create(**validated_data)


class SearchFieldsSerializer(serializers.ModelSerializer):
    class Meta:
        model = SearchFields
        fields = ['type', 'field_name']


class HatchingLossManagementSerializer(serializers.ModelSerializer):
    class Meta:
        model = HatchingLossManagement
        fields = [
            'id',
            'include_total_disease_losses',
            'include_total_flock_destruction',
            'include_total_normal_flock_losses',
            'include_total_force_majeure_losses',
            'include_total_fire_losses'
        ]
        read_only_fields = ['id']


class DetailsGeneraWageSerializer(serializers.ModelSerializer):
    info = serializers.SerializerMethodField('get_info')

    class Meta:
        model = KillHouse
        fields = ['id', 'info']

    def get_info(self, obj):
        real_free_sale_wage = 0
        union_real_free_sale_wage = 0
        company_real_free_sale_wage = 0
        guilds_real_free_sale_wage = 0
        other_real_free_sale_wage = 0
        total_check_wage = 0
        union_wage_percent = 0
        company_wage_percent = 0
        guilds_wage_percent = 0
        other_wage_percent = 0
        wage_type = WageType.objects.filter(en_name='province-kill-request').first()
        total_check_wage = wage_type.amount
        percentages_wage_type = PercentageOfWageType.objects.filter(wage_type=wage_type, percent__gt=0, trash=False)
        for percentage_wage_type in percentages_wage_type:
            if percentage_wage_type.share_type.en_name == 'union':
                union_wage_percent = percentage_wage_type.percent / 100
            elif percentage_wage_type.share_type.en_name == 'company':
                company_wage_percent = percentage_wage_type.percent / 100

            elif percentage_wage_type.share_type.en_name == 'guilds':
                guilds_wage_percent = percentage_wage_type.percent / 100
            elif percentage_wage_type.share_type.en_name in ['city', 'wallet']:
                pass
            else:
                other_wage_percent = percentage_wage_type.percent / 100

        free_sales = KillHouseFreeSaleBarInformation.objects.filter(trash=False)
        free_sales_weight = free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        free_sales_wage = free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0

        free_sales = KillHouseFreeSaleBarInformation.objects.filter(trash=False)
        free_sales_weight = free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        free_sales_wage = free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0
        union_free_sales = free_sales.filter(union_share__gt=0, trash=False)
        union_free_sales_weight = union_free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        union_free_sales_wage = union_free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0

        company_free_sales = free_sales.filter(company_share__gt=0, trash=False)
        company_free_sales_weight = company_free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        company_free_sales_wage = company_free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0

        guilds_free_sales = free_sales.filter(guilds_share__gt=0, trash=False)
        guilds_free_sales_weight = guilds_free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        guilds_free_sales_wage = guilds_free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0

        other_free_sales = free_sales.filter(other_share__gt=0, trash=False)
        other_free_sales_weight = other_free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        other_free_sales_wage = other_free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0
        if carcases_sell == True:
            real_free_sale_wage = (free_sales_weight * total_check_wage) - free_sales_wage if (free_sales_weight * total_check_wage) - free_sales_wage > 0 else 0

            union_real_free_sale_wage = (union_free_sales_weight * union_wage_percent) - union_free_sales_wage if (union_free_sales_weight * union_wage_percent) - union_free_sales_wage > 0 else 0

            company_real_free_sale_wage = (company_free_sales_weight * company_wage_percent) - company_free_sales_wage if (company_free_sales_weight * company_wage_percent) - company_free_sales_wage > 0 else 0


            guilds_real_free_sale_wage = (guilds_free_sales_weight * guilds_wage_percent) - guilds_free_sales_wage if (guilds_free_sales_weight * guilds_wage_percent) - guilds_free_sales_wage > 0 else 0

            other_real_free_sale_wage = (other_free_sales_weight * other_wage_percent) - other_free_sales_wage if (other_free_sales_weight * other_wage_percent) - other_free_sales_wage > 0 else 0

        if obj.killer == True:
            killer = True
        else:
            killer = False
        if obj.killer == True and obj.type == 'exclusive':
            parent = KillHousePercentage.objects.get(kill_house=obj, trash=False)
            killer_type = f'کشتارکن اختصاصی {parent.kill_house_for_killer.name}'
        elif obj.killer == True and obj.type == 'public':
            killer_type = 'کشتارکن عمومی'
        else:
            killer_type = 'کشتارگاه'
        role = self.context.get('request').GET['role']

        if self.context.get('request').GET['date1'] != 'null':
            date1 = datetime.datetime.strptime(str(self.context.get('request').GET['date1']),
                                               '%Y-%m-%d').date()
            date2 = datetime.datetime.strptime(str(self.context.get('request').GET['date2']),
                                               '%Y-%m-%d').date()
            province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj,
                                                                        state__in=('pending', 'accepted'),
                                                                        trash=False,
                                                                        return_to_province=False,
                                                                        first_car_allocated_quantity=0,
                                                                        archive_wage=False,
                                                                        kill_request__recive_date__date__gte=date1,
                                                                        kill_request__recive_date__date__lte=date2
                                                                        ).exclude(union_share=0, company_share=0,
                                                                                  guilds_share=0).only(
                'total_killed_weight', 'union_share', 'company_share',
                'guilds_share', 'total_wage_amount', 'total_killed_quantity')
            freezing_province_kill_requests = province_kill_requests.filter(
                province_request__poultry_request__freezing=True).only('total_killed_weight', 'union_share',
                                                                       'company_share',
                                                                       'guilds_share', 'total_wage_amount',
                                                                       'total_killed_quantity')

            kill_house_requests = KillHouseRequest.objects.filter(
                Q(Q(killer=obj) & Q(killhouse_user=obj)) | Q(
                    Q(killer__isnull=True) & Q(killhouse_user=obj)) | Q(
                    Q(killer__isnull=True) | Q(killer=obj)),
                archive_wage=False,
                trash=False, kill_request__recive_date__date__gte=date1,
                kill_request__recive_date__date__lte=date2
            ).select_related('province_kill_request').only('accepted_real_weight',
                                                           'province_kill_request__union_share_percent',
                                                           'province_kill_request__wage', 'accepted_real_weight',
                                                           'province_kill_request__guilds_share_percent',
                                                           'province_kill_request__company_share_percent',
                                                           'accepted_real_quantity', 'accepted_real_quantity')

            kill_house_requests = kill_house_requests.filter(Q(killer=obj) | Q(killhouse_user=obj)).select_related(
                'province_kill_request').only('accepted_real_weight', 'province_kill_request__union_share_percent',
                                              'province_kill_request__wage', 'accepted_real_weight',
                                              'province_kill_request__guilds_share_percent',
                                              'province_kill_request__company_share_percent', 'accepted_real_quantity')

            freezing_kill_house_requests = kill_house_requests.filter(
                province_kill_request__province_request__poultry_request__freezing=True).select_related(
                'province_kill_request').only('accepted_real_weight', 'province_kill_request__union_share_percent',
                                              'province_kill_request__wage', 'accepted_real_weight',
                                              'province_kill_request__guilds_share_percent',
                                              'province_kill_request__company_share_percent', 'accepted_real_quantity')

            free_bars = KillHouseFreeBarInformation.objects.filter(date__date__gte=date1,
                                                                   date__date__lte=date2, kill_house=obj,
                                                                   archive_wage=False,
                                                                   trash=False).only('live_weight', 'union_share',
                                                                                     'weight_of_carcasses',
                                                                                     'company_share',
                                                                                     'guilds_share',
                                                                                     'total_wage_amount')
            free_bars_live = free_bars.filter(buy_type='live', trash=False).only('live_weight', 'union_share',
                                                                                 'weight_of_carcasses',
                                                                                 'company_share',
                                                                                 'guilds_share',
                                                                                 'total_wage_amount')
            free_bars_carcases = free_bars.filter(buy_type='carcass', trash=False).only('live_weight', 'union_share',
                                                                                        'weight_of_carcasses',
                                                                                        'company_share',
                                                                                        'guilds_share',
                                                                                        'total_wage_amount'
                                                                                        )

            slaughter_transactions = InternalTransaction.objects.filter(date__date__gte=date1,
                                                                        date__date__lte=date2,
                                                                        kill_house=obj, status='completed',
                                                                        trash=False).only('union_share',
                                                                                          'company_share',
                                                                                          'guilds_share', 'amount')

        else:
            province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj,
                                                                        state__in=('pending', 'accepted'),
                                                                        trash=False,
                                                                        return_to_province=False,
                                                                        first_car_allocated_quantity=0,
                                                                        archive_wage=False,
                                                                        ).exclude(union_share=0, company_share=0,
                                                                                  guilds_share=0).only(
                'total_killed_weight', 'union_share', 'company_share',
                'guilds_share', 'total_wage_amount', 'total_killed_quantity')
            freezing_province_kill_requests = province_kill_requests.filter(
                province_request__poultry_request__freezing=True).only('total_killed_weight', 'union_share',
                                                                       'company_share',
                                                                       'guilds_share', 'total_wage_amount',
                                                                       'total_killed_quantity')

            kill_house_requests = KillHouseRequest.objects.filter(
                Q(Q(killer=obj) & Q(killhouse_user=obj)) | Q(
                    Q(killer__isnull=True) & Q(killhouse_user=obj)) | Q(
                    Q(killer__isnull=True) | Q(killer=obj)),
                archive_wage=False,
                trash=False
            ).select_related('province_kill_request').only('accepted_real_weight',
                                                           'province_kill_request__union_share_percent',
                                                           'province_kill_request__wage', 'accepted_real_weight',
                                                           'province_kill_request__guilds_share_percent',
                                                           'province_kill_request__company_share_percent',
                                                           'accepted_real_quantity', 'accepted_real_quantity')
            kill_house_requests = kill_house_requests.filter(Q(killer=obj) | Q(killhouse_user=obj)).select_related(
                'province_kill_request').only(
                'accepted_real_weight', 'province_kill_request__union_share_percent',
                'province_kill_request__wage', 'accepted_real_weight', 'province_kill_request__guilds_share_percent',
                'province_kill_request__company_share_percent', 'accepted_real_quantity')

            freezing_kill_house_requests = kill_house_requests.filter(
                province_kill_request__province_request__poultry_request__freezing=True).select_related(
                'province_kill_request').only('accepted_real_weight', 'province_kill_request__union_share_percent',
                                              'province_kill_request__wage', 'accepted_real_weight',
                                              'province_kill_request__guilds_share_percent',
                                              'province_kill_request__company_share_percent', 'accepted_real_quantity')

            free_bars = KillHouseFreeBarInformation.objects.filter(kill_house=obj, archive_wage=False,
                                                                   trash=False).only('live_weight', 'union_share',
                                                                                     'weight_of_carcasses',
                                                                                     'company_share',
                                                                                     'guilds_share',
                                                                                     'total_wage_amount')
            free_bars_live = free_bars.filter(buy_type='live', trash=False).only('live_weight', 'union_share',
                                                                                 'weight_of_carcasses',
                                                                                 'company_share',
                                                                                 'guilds_share',
                                                                                 'total_wage_amount')
            free_bars_carcases = free_bars.filter(buy_type='carcass', trash=False).only('live_weight', 'union_share',
                                                                                        'weight_of_carcasses',
                                                                                        'company_share',
                                                                                        'guilds_share',
                                                                                        'total_wage_amount'
                                                                                        )
            slaughter_transactions = InternalTransaction.objects.filter(
                kill_house=obj, status='completed',
                trash=False).only('union_share', 'company_share', 'guilds_share', 'amount')

        province_kill_requests_total_wage = \
            province_kill_requests.filter(province_request__poultry_request__freezing=False).aggregate(
                total=Sum('total_wage_amount'))[
                'total'] or 0
        if wage_percent > 0:
            province_kill_requests_total_wage -= province_kill_requests_total_wage * wage_percent
        freezing_province_kill_request_total_wage = \
            freezing_province_kill_requests.aggregate(total=Sum('total_wage_amount'))['total'] or 0

        kill_house_reqest_total_wage = \
            kill_house_requests.filter(
                province_kill_request__province_request__poultry_request__freezing=False).aggregate(
                total=Sum(F('accepted_real_weight') * F('province_kill_request__wage')))[
                'total'] or 0
        if wage_percent > 0:
            kill_house_reqest_total_wage -= kill_house_reqest_total_wage * wage_percent
        freezing_kill_house_reqest_total_wage = \
            freezing_kill_house_requests.aggregate(
                total=Sum(F('accepted_real_weight') * F('province_kill_request__wage')))[
                'total'] or 0

        free_bars_live_total_wage = free_bars_live.aggregate(total=Sum('total_wage_amount'))[
                                        'total'] or 0

        free_bars_carcases_total_wage = free_bars_carcases.aggregate(total=Sum('total_wage_amount'))[
                                            'total'] or 0
        total_wage = free_bars_carcases_total_wage + kill_house_reqest_total_wage + \
                     freezing_kill_house_reqest_total_wage + province_kill_requests_total_wage + \
                     freezing_province_kill_request_total_wage + free_bars_live_total_wage

        total_paid_wage = slaughter_transactions.aggregate(total=Sum('amount'))[
                              'total'] or 0

        union_province_kill_request_total_wage = \
            province_kill_requests.filter(union_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('union_share'))[
                'total'] or 0
        union_province_kill_request_kill_house_requests_total_wage = \
        kill_house_requests.filter(province_kill_request__union_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__union_share_percent') / 100)))[
            'total'] or 0
        if wage_percent > 0:
            union_province_kill_request_kill_house_requests_total_wage -= union_province_kill_request_kill_house_requests_total_wage * wage_percent
        union_province_kill_request_total_wage += union_province_kill_request_kill_house_requests_total_wage

        freezing_union_province_kill_request_total_wage = \
            freezing_province_kill_requests.filter(union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0
        freezing_union_province_kill_request_total_wage += \
            freezing_kill_house_requests.filter(province_kill_request__union_share__gt=0).aggregate(total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__union_share_percent') / 100)))[
                'total'] or 0

        union_kill_house_free_live_bar_total_wage = \
            free_bars.filter(buy_type='live', union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0

        union_kill_house_free_carcases_bar_total_wage = \
            free_bars.filter(buy_type='carcass', union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0
        union_total_paid_wage = \
            slaughter_transactions.filter(union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0
        union_total_wage = union_province_kill_request_total_wage + freezing_union_province_kill_request_total_wage + union_kill_house_free_live_bar_total_wage + union_kill_house_free_carcases_bar_total_wage
        union_total_unpaid_wage = union_total_wage - union_total_paid_wage

        company_province_kill_request_total_wage = \
            province_kill_requests.filter(company_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('company_share'))[
                'total'] or 0
        if wage_percent > 0:
            company_province_kill_request_total_wage -= company_province_kill_request_total_wage * wage_percent
        company_province_kill_request_kill_house_requests_total_wage = \
        kill_house_requests.filter(province_kill_request__company_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__company_share_percent') / 100)))[
            'total'] or 0
        if wage_percent > 0:
            company_province_kill_request_kill_house_requests_total_wage -= company_province_kill_request_kill_house_requests_total_wage * wage_percent

        company_province_kill_request_total_wage += company_province_kill_request_kill_house_requests_total_wage
        freezing_company_province_kill_request_total_wage = \
            freezing_province_kill_requests.filter(company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0
        freezing_company_province_kill_request_total_wage += \
            freezing_kill_house_requests.filter(province_kill_request__company_share__gt=0).aggregate(total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__company_share_percent') / 100)))[
                'total'] or 0

        company_kill_house_free_live_bar_total_wage = \
            free_bars.filter(buy_type='live', company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0

        company_kill_house_free_carcases_bar_total_wage = \
            free_bars.filter(buy_type='carcass', company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0
        company_total_paid_wage = \
            slaughter_transactions.filter(company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0
        company_total_wage = company_province_kill_request_total_wage + freezing_company_province_kill_request_total_wage + company_kill_house_free_live_bar_total_wage + company_kill_house_free_carcases_bar_total_wage
        company_total_unpaid_wage = company_total_wage - company_total_paid_wage

        guilds_province_kill_request_total_wage = \
            province_kill_requests.filter(guilds_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('guilds_share'))[
                'total'] or 0
        if wage_percent > 0:
            guilds_province_kill_request_total_wage -= guilds_province_kill_request_total_wage * wage_percent

        guilds_province_kill_request_kill_house_requests_total_wage = \
        kill_house_requests.filter(province_kill_request__guilds_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__guilds_share_percent') / 100)))[
            'total'] or 0

        if wage_percent > 0:
            guilds_province_kill_request_kill_house_requests_total_wage -= guilds_province_kill_request_kill_house_requests_total_wage * wage_percent

        guilds_province_kill_request_total_wage += guilds_province_kill_request_kill_house_requests_total_wage
        freezing_guilds_province_kill_request_total_wage = \
            freezing_province_kill_requests.filter(guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0
        freezing_guilds_province_kill_request_total_wage += \
            freezing_kill_house_requests.filter(province_kill_request__guilds_share__gt=0).aggregate(total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__guilds_share_percent') / 100)))[
                'total'] or 0

        guilds_kill_house_free_live_bar_total_wage = \
            free_bars.filter(buy_type='live', guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0

        guilds_kill_house_free_carcases_bar_total_wage = \
            free_bars.filter(buy_type='carcass', guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0
        guilds_total_paid_wage = \
            slaughter_transactions.filter(guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0

        accepted_real_weight = \
            kill_house_requests.aggregate(total=Sum('accepted_real_weight'))[
                'total'] or 0
        if wage_percent > 0:
            accepted_real_weight -= accepted_real_weight * wage_percent
        total_killed_weights = \
            province_kill_requests.aggregate(total=Sum('total_killed_weight'))[
                'total'] or 0

        if wage_percent > 0:
            total_killed_weights -= total_killed_weights * wage_percent
        total_killed_quantitys = \
            province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[
                'total'] or 0
        accepted_real_quantity = \
            kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))[
                'total'] or 0

        guilds_total_wage = guilds_province_kill_request_total_wage + freezing_guilds_province_kill_request_total_wage + guilds_kill_house_free_live_bar_total_wage + guilds_kill_house_free_carcases_bar_total_wage
        guilds_total_unpaid_wage = guilds_total_wage - guilds_total_paid_wage

        other_province_kill_request_total_wage = \
            province_kill_requests.filter(other_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('other_share'))[
                'total'] or 0
        if wage_percent > 0:
            other_province_kill_request_total_wage -= other_province_kill_request_total_wage * wage_percent

        other_province_kill_request_kill_house_requests_total_wage = \
        kill_house_requests.filter(province_kill_request__other_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__other_share_percent') / 100)))[
            'total'] or 0
        if wage_percent > 0:
            other_province_kill_request_kill_house_requests_total_wage -= other_province_kill_request_kill_house_requests_total_wage * wage_percent
        other_province_kill_request_total_wage += other_province_kill_request_kill_house_requests_total_wage
        freezing_other_province_kill_request_total_wage = \
            freezing_province_kill_requests.filter(other_share__gt=0).aggregate(total=Sum('other_share'))[
                'total'] or 0
        freezing_other_province_kill_request_total_wage += \
            freezing_kill_house_requests.filter(province_kill_request__other_share__gt=0).aggregate(total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__other_share_percent') / 100)))[
                'total'] or 0

        other_kill_house_free_live_bar_total_wage = \
            free_bars.filter(buy_type='live', other_share__gt=0).aggregate(total=Sum('other_share'))[
                'total'] or 0

        other_kill_house_free_carcases_bar_total_wage = \
            free_bars.filter(buy_type='carcass', other_share__gt=0).aggregate(total=Sum('other_share'))[
                'total'] or 0
        other_total_paid_wage = \
            slaughter_transactions.filter(other_share__gt=0).aggregate(total=Sum('other_share'))[
                'total'] or 0
        other_total_wage = other_province_kill_request_total_wage + freezing_other_province_kill_request_total_wage + other_kill_house_free_live_bar_total_wage + other_kill_house_free_carcases_bar_total_wage
        other_total_unpaid_wage = other_total_wage - other_total_paid_wage
        not_paied = total_wage - total_paid_wage
        values_list3 = {
            "type": killer_type,
            "killer": killer,
            "kill_house_name": obj.name,
            "fullname": obj.kill_house_operator.user.fullname,
            "mobile": obj.kill_house_operator.user.mobile,
            "city": obj.kill_house_operator.user.city.name,
            "len_province_kill_requests": len(province_kill_requests),
            "total_killed_quantity": total_killed_quantitys,
            "total_killed_weight": total_killed_weights,
            "len_kill_house_requests": len(kill_house_requests),
            "accepted_real_quantity": accepted_real_quantity,
            "accepted_real_weight": accepted_real_weight,
            "union_total_wage": union_total_wage - union_real_free_sale_wage if (union_total_wage - union_real_free_sale_wage) > 0 else 0,
            "union_total_unpaid_wage": union_total_unpaid_wage,
            "union_total_paid_wage": union_total_paid_wage,
        }

        if role == 'ProvinceOperator':
            pass
        elif role == 'AdminX':
            values_list3.update({
                "off": obj.off,
                "len_slaughter_transactions": len(slaughter_transactions),
                "total_wage": total_wage - real_free_sale_wage if (total_wage - real_free_sale_wage) > 0 else 0 ,
                "company_total_wage": company_total_wage - company_real_free_sale_wage if (company_total_wage - company_real_free_sale_wage) > 0 else 0,
                "union_total_wage": union_total_wage - union_real_free_sale_wage if (union_total_wage - union_real_free_sale_wage) >0 else 0,
                "guilds_total_wage": guilds_total_wage - guilds_real_free_sale_wage if (guilds_total_wage - guilds_real_free_sale_wage) > 0 else 0,
                "other_total_wage": other_total_wage - other_real_free_sale_wage if (other_total_wage - other_real_free_sale_wage) > 0 else 0,
                "not_paied": not_paied - obj.off,
                "company_total_unpaid_wage": company_total_unpaid_wage,
                "union_total_unpaid_wage": union_total_unpaid_wage,
                "guilds_total_unpaid_wage": guilds_total_unpaid_wage,
                "other_total_unpaid_wage": other_total_unpaid_wage,
                "total_paid_wage": total_paid_wage,
                "union_total_paid_wage": union_total_paid_wage,
                "company_total_paid_wage": company_total_paid_wage,
                "guilds_total_paid_wage": guilds_total_paid_wage,
                "other_total_paid_wage": other_total_paid_wage,
            })
        else:
            values_list3.update({
                "off": obj.off,
                "total_wage": total_wage - real_free_sale_wage if (total_wage - real_free_sale_wage) > 0 else 0 ,
                "company_total_wage": company_total_wage - company_real_free_sale_wage if (company_total_wage - company_real_free_sale_wage) > 0 else 0,
                "len_slaughter_transactions": len(slaughter_transactions),
                "union_total_wage": union_total_wage - union_real_free_sale_wage if (union_total_wage - union_real_free_sale_wage) >0 else 0,
                "guilds_total_wage": guilds_total_wage - guilds_real_free_sale_wage if (guilds_total_wage - guilds_real_free_sale_wage) > 0 else 0,
                # "not_paied": not_paied - other_total_unpaid_wage,
                "not_paied": not_paied - obj.off,
                "company_total_unpaid_wage": company_total_unpaid_wage,
                "union_total_unpaid_wage": union_total_unpaid_wage,
                "guilds_total_unpaid_wage": guilds_total_unpaid_wage,
                "total_paid_wage": total_paid_wage ,
                "company_total_paid_wage": company_total_paid_wage,
                "union_total_paid_wage": union_total_paid_wage,
                "guilds_total_paid_wage": guilds_total_paid_wage,
            })

        return values_list3


class DashboardDetailsGeneraWageSerializer(serializers.ModelSerializer):
    info = serializers.SerializerMethodField('get_info')

    class Meta:
        model = KillHouse
        fields = ['id', 'info']

    def get_info(self, obj):
        role = self.context.get('request').GET['role']
        total_weight = 0
        total_wage = 0
        total_paid_wage = 0
        union_total_paid_wage = 0

        union_chain_allocation_total_wage = 0

        company_total_paid_wage = 0

        company_chain_allocation_total_wage = 0

        guilds_total_paid_wage = 0

        guilds_chain_allocation_total_wage = 0

        other_total_weight = 0
        other_total_wage = 0
        other_total_paid_wage = 0
        other_chain_allocation_total_wage = 0

        other_chain_allocation_total_weight = 0
        real_free_sale_wage = 0
        union_real_free_sale_wage = 0
        company_real_free_sale_wage = 0
        guilds_real_free_sale_wage = 0
        other_real_free_sale_wage = 0
        total_check_wage = 0
        union_wage_percent = 0
        company_wage_percent = 0
        guilds_wage_percent = 0
        other_wage_percent = 0
        wage_type = WageType.objects.filter(en_name='province-kill-request').first()
        total_check_wage = wage_type.amount
        percentages_wage_type = PercentageOfWageType.objects.filter(wage_type=wage_type, percent__gt=0, trash=False)
        for percentage_wage_type in percentages_wage_type:
            if percentage_wage_type.share_type.en_name == 'union':
                union_wage_percent = percentage_wage_type.percent / 100
            elif percentage_wage_type.share_type.en_name == 'company':
                company_wage_percent = percentage_wage_type.percent / 100

            elif percentage_wage_type.share_type.en_name == 'guilds':
                guilds_wage_percent = percentage_wage_type.percent / 100
            elif percentage_wage_type.share_type.en_name in ['city', 'wallet']:
                pass
            else:
                other_wage_percent = percentage_wage_type.percent / 100

        free_sales = KillHouseFreeSaleBarInformation.objects.filter(trash=False)
        free_sales_weight = free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        free_sales_wage = free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0

        free_sales = KillHouseFreeSaleBarInformation.objects.filter(trash=False)
        free_sales_weight = free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        free_sales_wage = free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0
        union_free_sales = free_sales.filter(union_share__gt=0, trash=False)
        union_free_sales_weight = union_free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        union_free_sales_wage = union_free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0

        company_free_sales = free_sales.filter(company_share__gt=0, trash=False)
        company_free_sales_weight = company_free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        company_free_sales_wage = company_free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0

        guilds_free_sales = free_sales.filter(guilds_share__gt=0, trash=False)
        guilds_free_sales_weight = guilds_free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        guilds_free_sales_wage = guilds_free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0

        other_free_sales = free_sales.filter(other_share__gt=0, trash=False)
        other_free_sales_weight = other_free_sales.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0

        other_free_sales_wage = other_free_sales.aggregate(total=Sum('total_wage_amount'))['total'] or 0
        if carcases_sell == True:
            real_free_sale_wage = (free_sales_weight * total_check_wage) - free_sales_wage if (free_sales_weight * total_check_wage) - free_sales_wage > 0 else 0

            union_real_free_sale_wage = (union_free_sales_weight * union_wage_percent) - union_free_sales_wage if (union_free_sales_weight * union_wage_percent) - union_free_sales_wage > 0 else 0

            company_real_free_sale_wage = (company_free_sales_weight * company_wage_percent) - company_free_sales_wage if (company_free_sales_weight * company_wage_percent) - company_free_sales_wage > 0 else 0


            guilds_real_free_sale_wage = (guilds_free_sales_weight * guilds_wage_percent) - guilds_free_sales_wage if (guilds_free_sales_weight * guilds_wage_percent) - guilds_free_sales_wage > 0 else 0

            other_real_free_sale_wage = (other_free_sales_weight * other_wage_percent) - other_free_sales_wage if (other_free_sales_weight * other_wage_percent) - other_free_sales_wage > 0 else 0


        if self.context.get('request').GET['date1'] != 'null':
            date1 = datetime.datetime.strptime(str(self.context.get('request').GET['date1']),
                                               '%Y-%m-%d').date()
            date2 = datetime.datetime.strptime(str(self.context.get('request').GET['date2']),
                                               '%Y-%m-%d').date()
            province_kill_requests = ProvinceKillRequest.objects.filter(
                state__in=('pending', 'accepted'),
                trash=False,
                return_to_province=False,
                first_car_allocated_quantity=0,
                archive_wage=False, kill_request__recive_date__date__gte=date1,
                kill_request__recive_date__date__lte=date2
            ).exclude(union_share=0, company_share=0,
                      guilds_share=0).only('total_killed_weight', 'union_share', 'company_share',
                                           'guilds_share', 'total_wage_amount', 'total_killed_quantity')
            freezing_province_kill_requests = province_kill_requests.filter(
                province_request__poultry_request__freezing=True).only('total_killed_weight', 'union_share',
                                                                       'company_share',
                                                                       'guilds_share', 'total_wage_amount',
                                                                       'total_killed_quantity')

            kill_house_requests = KillHouseRequest.objects.filter(
                archive_wage=False,
                trash=False, kill_request__recive_date__date__gte=date1,
                kill_request__recive_date__date__lte=date2
            ).select_related('province_kill_request').only('accepted_real_weight',
                                                           'province_kill_request__union_share_percent',
                                                           'province_kill_request__wage', 'accepted_real_weight',
                                                           'province_kill_request__guilds_share_percent',
                                                           'province_kill_request__company_share_percent',
                                                           'accepted_real_quantity').select_related(
                'province_kill_request').only('accepted_real_weight', 'province_kill_request__union_share_percent',
                                              'province_kill_request__wage', 'accepted_real_weight',
                                              'province_kill_request__guilds_share_percent',
                                              'province_kill_request__company_share_percent', 'accepted_real_quantity',
                                              'accepted_real_quantity')

            freezing_kill_house_requests = kill_house_requests.filter(
                province_kill_request__province_request__poultry_request__freezing=True).select_related(
                'province_kill_request').only('accepted_real_weight', 'province_kill_request__union_share_percent',
                                              'province_kill_request__wage', 'accepted_real_weight',
                                              'province_kill_request__guilds_share_percent',
                                              'province_kill_request__company_share_percent', 'accepted_real_quantity')

            free_bars = KillHouseFreeBarInformation.objects.filter(date__date__gte=date1,
                                                                   date__date__lte=date2, archive_wage=False,
                                                                   trash=False).only('live_weight', 'union_share',
                                                                                     'weight_of_carcasses',
                                                                                     'company_share',
                                                                                     'guilds_share',
                                                                                     'total_wage_amount')
            free_bars_live = free_bars.filter(buy_type='live', trash=False).only('live_weight', 'union_share',
                                                                                 'weight_of_carcasses',
                                                                                 'company_share',
                                                                                 'guilds_share',
                                                                                 'total_wage_amount')
            free_bars_carcases = free_bars.filter(buy_type='carcass', trash=False).only('live_weight', 'union_share',
                                                                                        'weight_of_carcasses',
                                                                                        'company_share',
                                                                                        'guilds_share',
                                                                                        'total_wage_amount'
                                                                                        )

            slaughter_transactions = InternalTransaction.objects.filter(date__date__gte=date1,
                                                                        date__date__lte=date2,
                                                                        status='completed', kill_house__isnull=False,
                                                                        trash=False).only('union_share',
                                                                                          'company_share',
                                                                                          'guilds_share', 'amount')


        else:
            province_kill_requests = ProvinceKillRequest.objects.filter(
                state__in=('pending', 'accepted'),
                trash=False,
                return_to_province=False,
                first_car_allocated_quantity=0,
                archive_wage=False,
            ).exclude(union_share=0, company_share=0,
                      guilds_share=0).only(
                'total_killed_weight', 'union_share', 'company_share',
                'guilds_share', 'total_wage_amount', 'total_killed_quantity')
            freezing_province_kill_requests = province_kill_requests.filter(
                province_request__poultry_request__freezing=True).only('total_killed_weight', 'union_share',
                                                                       'company_share',
                                                                       'guilds_share', 'total_wage_amount',
                                                                       'total_killed_quantity')

            kill_house_requests = KillHouseRequest.objects.filter(
                archive_wage=False,
                trash=False
            ).select_related('province_kill_request').only(
                'accepted_real_weight', 'province_kill_request__union_share_percent',
                'province_kill_request__wage', 'accepted_real_weight', 'province_kill_request__guilds_share_percent',
                'province_kill_request__company_share_percent', 'accepted_real_quantity').only('accepted_real_weight',
                                                                                               'province_kill_request__union_share_percent',
                                                                                               'province_kill_request__wage',
                                                                                               'accepted_real_weight',
                                                                                               'province_kill_request__guilds_share_percent',
                                                                                               'province_kill_request__company_share_percent',
                                                                                               'accepted_real_quantity',
                                                                                               'accepted_real_quantity')

            freezing_kill_house_requests = kill_house_requests.filter(
                province_kill_request__province_request__poultry_request__freezing=True).select_related(
                'province_kill_request').only('accepted_real_weight', 'province_kill_request__union_share_percent',
                                              'province_kill_request__wage', 'accepted_real_weight',
                                              'province_kill_request__guilds_share_percent',
                                              'province_kill_request__company_share_percent', 'accepted_real_quantity')

            free_bars = KillHouseFreeBarInformation.objects.filter(archive_wage=False,
                                                                   trash=False).only('live_weight', 'union_share',
                                                                                     'weight_of_carcasses',
                                                                                     'company_share',
                                                                                     'guilds_share',
                                                                                     'total_wage_amount')
            free_bars_live = free_bars.filter(buy_type='live', trash=False).only('live_weight', 'union_share',
                                                                                 'weight_of_carcasses',
                                                                                 'company_share',
                                                                                 'guilds_share',
                                                                                 'total_wage_amount')
            free_bars_carcases = free_bars.filter(buy_type='carcass', trash=False).only('live_weight', 'union_share',
                                                                                        'weight_of_carcasses',
                                                                                        'company_share',
                                                                                        'guilds_share',
                                                                                        'total_wage_amount'
                                                                                        )
            slaughter_transactions = InternalTransaction.objects.filter(
                kill_house__isnull=False, status='completed',
                trash=False).only('union_share', 'company_share', 'guilds_share', 'amount')

        province_kill_requests_total_wage = \
            province_kill_requests.filter(province_request__poultry_request__freezing=False).aggregate(
                total=Sum('total_wage_amount'))[
                'total'] or 0
        if wage_percent > 0:
            province_kill_requests_total_wage -= province_kill_requests_total_wage * wage_percent
        freezing_province_kill_request_total_wage = \
            freezing_province_kill_requests.aggregate(total=Sum('total_wage_amount'))['total'] or 0

        total_wage += province_kill_requests_total_wage + freezing_province_kill_request_total_wage
        province_kill_requests_total_weight = \
            province_kill_requests.filter(province_request__poultry_request__freezing=False).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        if wage_percent > 0:
            province_kill_requests_total_weight -= province_kill_requests_total_weight * wage_percent
        freezing_province_kill_request_total_weight = \
            freezing_province_kill_requests.aggregate(total=Sum('total_killed_weight'))['total'] or 0
        total_weight += province_kill_requests_total_weight + freezing_province_kill_request_total_weight
        kill_house_reqest_total_wage = \
            kill_house_requests.filter(
                province_kill_request__province_request__poultry_request__freezing=False).aggregate(
                total=Sum(F('accepted_real_weight') * F('province_kill_request__wage')))[
                'total'] or 0
        if wage_percent > 0:
            kill_house_reqest_total_wage -= kill_house_reqest_total_wage * wage_percent
        freezing_kill_house_reqest_total_wage = \
            freezing_kill_house_requests.aggregate(
                total=Sum(F('accepted_real_weight') * F('province_kill_request__wage')))[
                'total'] or 0
        total_wage += kill_house_reqest_total_wage + freezing_kill_house_reqest_total_wage

        kill_house_reqest_total_weight = kill_house_requests.filter(
            province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum('accepted_real_weight'))[
                                             'total'] or 0

        if wage_percent > 0:
            kill_house_reqest_total_weight -= kill_house_reqest_total_weight * wage_percent
        freezing_kill_house_reqest_total_weight = \
            freezing_kill_house_requests.aggregate(total=Sum('accepted_real_weight'))[
                'total'] or 0

        total_weight += kill_house_reqest_total_weight + freezing_kill_house_reqest_total_weight

        free_bars_live_total_wage = free_bars_live.aggregate(total=Sum('total_wage_amount'))[
                                        'total'] or 0
        total_wage += free_bars_live_total_wage
        free_bars_live_total_weight = free_bars_live.aggregate(total=Sum('live_weight'))[
                                          'total'] or 0
        total_weight += free_bars_live_total_weight
        free_bars_carcases_total_wage = free_bars_carcases.aggregate(total=Sum('total_wage_amount'))[
                                            'total'] or 0
        total_wage += free_bars_carcases_total_wage
        free_bars_carcases_total_weight = free_bars_carcases.aggregate(total=Sum('weight_of_carcasses'))[
                                              'total'] or 0
        total_weight += free_bars_carcases_total_weight

        total_paid_wage += slaughter_transactions.aggregate(total=Sum('amount'))[
                               'total'] or 0
        union_province_kill_request_total_weight = \
            province_kill_requests.filter(union_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        if wage_percent > 0:
            union_province_kill_request_total_weight -= union_province_kill_request_total_weight * wage_percent
        freezing_union_province_kill_request_total_weight = \
            freezing_province_kill_requests.filter(union_share__gt=0).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        union_province_kill_request_kill_house_requests_total_weight = \
        kill_house_requests.filter(province_kill_request__union_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum('accepted_real_weight'))[
            'total'] or 0
        if wage_percent > 0:
            union_province_kill_request_kill_house_requests_total_weight -= union_province_kill_request_kill_house_requests_total_weight * wage_percent
        union_province_kill_request_total_weight += union_province_kill_request_kill_house_requests_total_weight

        freezing_union_province_kill_request_total_weight += \
            freezing_kill_house_requests.filter(province_kill_request__union_share__gt=0).aggregate(
                total=Sum('accepted_real_weight'))[
                'total'] or 0

        union_province_kill_request_total_wage = \
            province_kill_requests.filter(union_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('union_share'))[
                'total'] or 0
        if wage_percent > 0:
            union_province_kill_request_total_wage -= union_province_kill_request_total_wage * wage_percent

        union_province_kill_request_kill_house_requests_total_wage = \
        kill_house_requests.filter(province_kill_request__union_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__union_share_percent') / 100)))[
            'total'] or 0
        if wage_percent > 0:
            union_province_kill_request_kill_house_requests_total_wage -= union_province_kill_request_kill_house_requests_total_wage * wage_percent
        union_province_kill_request_total_wage += union_province_kill_request_kill_house_requests_total_wage

        freezing_union_province_kill_request_total_wage = \
            freezing_province_kill_requests.filter(union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0
        freezing_union_province_kill_request_total_wage += \
            freezing_kill_house_requests.filter(province_kill_request__union_share__gt=0).aggregate(total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__union_share_percent') / 100)))[
                'total'] or 0

        union_kill_house_free_live_bar_total_wage = \
            free_bars.filter(buy_type='live', union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0

        union_kill_house_free_carcases_bar_total_wage = \
            free_bars.filter(buy_type='carcass', union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0
        union_total_paid_wage += \
            slaughter_transactions.filter(union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0
        union_total_wage = union_province_kill_request_total_wage + freezing_union_province_kill_request_total_wage + union_chain_allocation_total_wage + union_kill_house_free_live_bar_total_wage + union_kill_house_free_carcases_bar_total_wage
        union_total_unpaid_wage = union_total_wage - union_total_paid_wage

        company_province_kill_request_total_weight = \
            province_kill_requests.filter(company_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0

        if wage_percent > 0:
            company_province_kill_request_total_weight -= company_province_kill_request_total_weight * wage_percent

        company_province_kill_request_kill_house_requests_total_weight = \
        kill_house_requests.filter(province_kill_request__company_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum('accepted_real_weight'))[
            'total'] or 0
        if wage_percent > 0:
            company_province_kill_request_kill_house_requests_total_weight -= company_province_kill_request_kill_house_requests_total_weight * wage_percent
        company_province_kill_request_total_weight += company_province_kill_request_kill_house_requests_total_weight
        freezing_company_province_kill_request_total_weight = \
            freezing_province_kill_requests.filter(company_share__gt=0).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        freezing_company_province_kill_request_total_weight += \
            freezing_kill_house_requests.filter(province_kill_request__company_share__gt=0).aggregate(
                total=Sum('accepted_real_weight'))[
                'total'] or 0
        company_province_kill_request_total_wage = \
            province_kill_requests.filter(company_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('company_share'))[
                'total'] or 0
        if wage_percent > 0:
            company_province_kill_request_total_wage -= company_province_kill_request_total_wage * wage_percent

        company_province_kill_request_kill_house_requests_total_wage = \
        kill_house_requests.filter(province_kill_request__company_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__company_share_percent') / 100)))[
            'total'] or 0
        if wage_percent > 0:
            company_province_kill_request_kill_house_requests_total_wage -= company_province_kill_request_kill_house_requests_total_wage * wage_percent
        company_province_kill_request_total_wage += company_province_kill_request_kill_house_requests_total_wage
        freezing_company_province_kill_request_total_wage = \
            freezing_province_kill_requests.filter(company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0
        freezing_company_province_kill_request_total_wage += \
            freezing_kill_house_requests.filter(province_kill_request__company_share__gt=0).aggregate(total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__company_share_percent') / 100)))[
                'total'] or 0

        company_kill_house_free_live_bar_total_wage = \
            free_bars.filter(buy_type='live', company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0

        company_kill_house_free_carcases_bar_total_wage = \
            free_bars.filter(buy_type='carcass', company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0
        company_total_paid_wage += \
            slaughter_transactions.filter(company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0
        company_total_wage = company_province_kill_request_total_wage + freezing_company_province_kill_request_total_wage + company_chain_allocation_total_wage + company_kill_house_free_live_bar_total_wage + company_kill_house_free_carcases_bar_total_wage
        company_total_unpaid_wage = company_total_wage - company_total_paid_wage

        guilds_province_kill_request_total_weight = \
            province_kill_requests.filter(guilds_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        if wage_percent > 0:
            guilds_province_kill_request_total_weight -= guilds_province_kill_request_total_weight * wage_percent

        guilds_province_kill_request_kil_house_requests_total_weight = \
        kill_house_requests.filter(province_kill_request__guilds_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum('accepted_real_weight'))[
            'total'] or 0

        if wage_percent > 0:
            guilds_province_kill_request_kil_house_requests_total_weight -= guilds_province_kill_request_kil_house_requests_total_weight * wage_percent

        guilds_province_kill_request_total_weight += guilds_province_kill_request_kil_house_requests_total_weight

        freezing_guilds_province_kill_request_total_weight = \
            freezing_province_kill_requests.filter(guilds_share__gt=0).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        freezing_guilds_province_kill_request_total_weight += \
            freezing_kill_house_requests.filter(province_kill_request__guilds_share__gt=0).aggregate(
                total=Sum('accepted_real_weight'))[
                'total'] or 0

        guilds_province_kill_request_total_wage = \
            province_kill_requests.filter(guilds_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('guilds_share'))[
                'total'] or 0
        if wage_percent > 0:
            guilds_province_kill_request_total_wage -= guilds_province_kill_request_total_wage * wage_percent
        guilds_province_kill_request_kill_house_requests_total_wage = \
        kill_house_requests.filter(province_kill_request__guilds_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__guilds_share_percent') / 100)))[
            'total'] or 0
        if wage_percent > 0:
            guilds_province_kill_request_kill_house_requests_total_wage -= guilds_province_kill_request_kill_house_requests_total_wage * wage_percent
        guilds_province_kill_request_total_wage += guilds_province_kill_request_kill_house_requests_total_wage
        freezing_guilds_province_kill_request_total_wage = \
            freezing_province_kill_requests.filter(guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0
        freezing_guilds_province_kill_request_total_wage += \
            freezing_kill_house_requests.filter(province_kill_request__guilds_share__gt=0).aggregate(total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__guilds_share_percent') / 100)))[
                'total'] or 0

        guilds_kill_house_free_live_bar_total_wage = \
            free_bars.filter(buy_type='live', guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0

        guilds_kill_house_free_carcases_bar_total_wage = \
            free_bars.filter(buy_type='carcass', guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0
        guilds_total_paid_wage += \
            slaughter_transactions.filter(guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0

        accepted_real_weight = \
            kill_house_requests.aggregate(total=Sum('accepted_real_weight'))[
                'total'] or 0
        total_killed_weights = \
            province_kill_requests.aggregate(total=Sum('total_killed_weight'))[
                'total'] or 0
        total_killed_quantitys = \
            province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[
                'total'] or 0
        accepted_real_quantity = \
            kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))[
                'total'] or 0

        guilds_total_wage = guilds_province_kill_request_total_wage + freezing_guilds_province_kill_request_total_wage + guilds_chain_allocation_total_wage + guilds_kill_house_free_live_bar_total_wage + guilds_kill_house_free_carcases_bar_total_wage
        guilds_total_unpaid_wage = guilds_total_wage - guilds_total_paid_wage

        other_province_kill_request_total_weight = \
            province_kill_requests.filter(other_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0

        if wage_percent > 0:
            other_province_kill_request_total_weight -= other_province_kill_request_total_weight * wage_percent

        other_province_kill_request_kill_house_requests_total_weight = \
        kill_house_requests.filter(province_kill_request__other_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum('accepted_real_weight'))[
            'total'] or 0

        if wage_percent > 0:
            other_province_kill_request_kill_house_requests_total_weight -= other_province_kill_request_kill_house_requests_total_weight * wage_percent

        other_province_kill_request_total_weight += other_province_kill_request_kill_house_requests_total_weight

        freezing_other_province_kill_request_total_weight = \
            freezing_province_kill_requests.filter(other_share__gt=0).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        freezing_other_province_kill_request_total_weight += \
            freezing_kill_house_requests.filter(province_kill_request__other_share__gt=0).aggregate(
                total=Sum('accepted_real_weight'))[
                'total'] or 0
        other_province_kill_request_total_wage = \
            province_kill_requests.filter(other_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('other_share'))[
                'total'] or 0
        if wage_percent > 0:
            other_province_kill_request_total_wage -= other_province_kill_request_total_wage * wage_percent

        other_province_kill_request_kill_house_requests_total_wage = \
        kill_house_requests.filter(province_kill_request__other_share__gt=0,
                                   province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__other_share_percent') / 100)))[
            'total'] or 0
        if wage_percent > 0:
            other_province_kill_request_kill_house_requests_total_wage -= other_province_kill_request_kill_house_requests_total_wage * wage_percent
        other_province_kill_request_total_wage += other_province_kill_request_kill_house_requests_total_wage
        freezing_other_province_kill_request_total_wage = \
            freezing_province_kill_requests.filter(other_share__gt=0).aggregate(total=Sum('other_share'))[
                'total'] or 0
        freezing_other_province_kill_request_total_wage += \
            freezing_kill_house_requests.filter(province_kill_request__other_share__gt=0).aggregate(total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__other_share_percent') / 100)))[
                'total'] or 0
        other_kill_house_free_live_bar_total_weight = \
            free_bars.filter(buy_type='live', other_share__gt=0).aggregate(total=Sum('live_weight'))[
                'total'] or 0
        other_kill_house_free_live_bar_total_wage = \
            free_bars.filter(buy_type='live', other_share__gt=0).aggregate(total=Sum('other_share'))[
                'total'] or 0

        other_kill_house_free_carcases_bar_total_weight = \
            free_bars.filter(buy_type='carcass', other_share__gt=0).aggregate(
                total=Sum('weight_of_carcasses'))[
                'total'] or 0
        other_kill_house_free_carcases_bar_total_wage = \
            free_bars.filter(buy_type='carcass', other_share__gt=0).aggregate(total=Sum('other_share'))[
                'total'] or 0
        other_total_paid_wage += \
            slaughter_transactions.filter(other_share__gt=0).aggregate(total=Sum('other_share'))[
                'total'] or 0

        other_total_weight = other_province_kill_request_total_weight + freezing_other_province_kill_request_total_weight + other_chain_allocation_total_weight + other_kill_house_free_live_bar_total_weight + other_kill_house_free_carcases_bar_total_weight
        other_total_wage = other_province_kill_request_total_wage + freezing_other_province_kill_request_total_wage + other_chain_allocation_total_wage + other_kill_house_free_live_bar_total_wage + other_kill_house_free_carcases_bar_total_wage
        other_total_unpaid_wage = other_total_wage - other_total_paid_wage
        kill_houses = KillHouse.objects.filter(trash=False,
                                               id__in=province_kill_requests.values_list('killhouse_user', flat=True))
        total_off = kill_houses.aggregate(total=Sum('off'))[
                        'total'] or 0

        not_paied = total_wage - total_paid_wage

        if role == 'ProvinceOperator':
            values_list3 = {
                "len_province_kill_requests": len(province_kill_requests),
                "total_killed_quantity": total_killed_quantitys,
                "total_killed_weight": total_killed_weights,
                "len_kill_house_requests": len(kill_house_requests),
                "accepted_real_quantity": accepted_real_quantity,
                "accepted_real_weight": accepted_real_weight,
                "union_total_wage": union_total_wage - union_real_free_sale_wage if (union_total_wage - union_real_free_sale_wage) > 0 else 0,
                "union_total_unpaid_wage": union_total_unpaid_wage,
                "union_total_paid_wage": union_total_paid_wage,

            }
            return values_list3
        elif role == 'AdminX':
            values_list3 = {
                "len_province_kill_requests": len(province_kill_requests),
                "total_killed_quantity": total_killed_quantitys,
                "total_killed_weight": total_killed_weights,
                "len_kill_house_requests": len(kill_house_requests),
                "accepted_real_quantity": accepted_real_quantity,
                "accepted_real_weight": accepted_real_weight,
                "len_slaughter_transactions": len(slaughter_transactions),
                "total_wage": total_wage - real_free_sale_wage if (total_wage - real_free_sale_wage) > 0 else 0,
                "company_total_wage": company_total_wage - company_real_free_sale_wage if (company_total_wage - company_real_free_sale_wage) > 0 else 0 ,
                "union_total_wage": union_total_wage - union_real_free_sale_wage if (union_total_wage - union_real_free_sale_wage) > 0 else 0,
                "guilds_total_wage": guilds_total_wage - guilds_real_free_sale_wage if (guilds_total_wage - guilds_real_free_sale_wage) > 0 else 0,
                "other_total_wage": other_total_wage - other_real_free_sale_wage if (other_total_wage - other_real_free_sale_wage) > 0 else 0,
                "not_paied": not_paied - total_off,
                "company_total_unpaid_wage": company_total_unpaid_wage,
                "union_total_unpaid_wage": union_total_unpaid_wage,
                "guilds_total_unpaid_wage": guilds_total_unpaid_wage,
                "other_total_unpaid_wage": other_total_unpaid_wage,
                "total_paid_wage": total_paid_wage,
                "union_total_paid_wage": union_total_paid_wage,
                "company_total_paid_wage": company_total_paid_wage,
                "guilds_total_paid_wage": guilds_total_paid_wage,
                "other_total_paid_wage": other_total_paid_wage,
                "total_off": total_off,

            }
            return values_list3
        else:
            values_list3 = {
                "len_province_kill_requests": len(province_kill_requests),
                "total_killed_quantity": total_killed_quantitys,
                "total_killed_weight": total_killed_weights,
                "len_kill_house_requests": len(kill_house_requests),
                "accepted_real_quantity": accepted_real_quantity,
                "accepted_real_weight": accepted_real_weight,
                "len_slaughter_transactions": len(slaughter_transactions),
                "total_wage": total_wage - real_free_sale_wage if (total_wage - real_free_sale_wage) > 0 else 0,
                "company_total_wage": company_total_wage - company_real_free_sale_wage if (company_total_wage - company_real_free_sale_wage) > 0 else 0,
                "union_total_wage": union_total_wage - union_real_free_sale_wage if (union_total_wage - union_real_free_sale_wage) > 0 else 0,
                "guilds_total_wage": guilds_total_wage - guilds_real_free_sale_wage if (guilds_total_wage - guilds_real_free_sale_wage) > 0 else 0,
                "not_paied": not_paied - total_off,
                "company_total_unpaid_wage": company_total_unpaid_wage,
                "union_total_unpaid_wage": union_total_unpaid_wage,
                "guilds_total_unpaid_wage": guilds_total_unpaid_wage,
                "total_paid_wage": total_paid_wage,
                "union_total_paid_wage": union_total_paid_wage,
                "company_total_paid_wage": company_total_paid_wage,
                "guilds_total_paid_wage": guilds_total_paid_wage,
                "total_off": total_off,

            }
            return values_list3


class NewDetailsGeneraWageSerializer(serializers.ModelSerializer):
    info = serializers.SerializerMethodField('get_info')

    class Meta:
        model = KillHouse
        fields = ['id', 'info']

    def get_info(self, obj):
        request = self.context.get('request')
        date1, date2 = self.parse_dates(request)
        role = request.GET.get('role')

        province_kill_requests_filters = self.get_province_kill_requests_filters(obj, date1, date2)
        kill_house_requests_filters = self.get_kill_house_requests_filters(obj, date1, date2)
        free_bars_filters = self.get_free_bars_filters(obj, date1, date2)
        slaughter_transactions_filters = self.get_slaughter_transactions_filters(obj, date1, date2)
        province_kill_requests = self.filter_province_kill_requests(province_kill_requests_filters)
        kill_house_requests = self.filter_kill_house_requests(kill_house_requests_filters, obj)
        free_bars = self.filter_free_bars(free_bars_filters)
        slaughter_transactions = self.filter_slaughter_transactions(slaughter_transactions_filters)

        wage_data = self.calculate_wage_data(obj, province_kill_requests, kill_house_requests, free_bars,
                                             slaughter_transactions)
        basic_info = self.get_basic_info(obj, province_kill_requests, kill_house_requests, free_bars,
                                         slaughter_transactions, wage_data)

        return self.prepare_info(role, obj, basic_info, slaughter_transactions, wage_data)

    def parse_dates(self, request):
        date1 = request.GET.get('date1')
        date2 = request.GET.get('date2')

        if date1 != 'null' and date2 != 'null':
            date1 = datetime.datetime.strptime(date1, '%Y-%m-%d').date()
            date2 = datetime.datetime.strptime(date2, '%Y-%m-%d').date()
        else:
            date1 = date2 = None
        return date1, date2

    def get_province_kill_requests_filters(self, obj, date1, date2):
        filters = {
            'killhouse_user': obj,
            'state__in': ('pending', 'accepted'),
            'trash': False,
            'return_to_province': False,
            'first_car_allocated_quantity': 0,
            'archive_wage': False
        }
        if date1 and date2:
            filters.update({
                'kill_request__recive_date__date__gte': date1,
                'kill_request__recive_date__date__lte': date2,
            })
        return filters

    def get_kill_house_requests_filters(self, obj, date1, date2):
        filters = Q(
            (Q(killer=obj) & Q(killhouse_user=obj)) |
            Q(killer__isnull=True, killhouse_user=obj) |
            Q(killer__isnull=True) | Q(killer=obj)
        )
        filters &= Q(archive_wage=False, trash=False)
        if date1 and date2:
            filters &= Q(kill_request__recive_date__date__gte=date1)
            filters &= Q(kill_request__recive_date__date__lte=date2)
        return filters

    def get_free_bars_filters(self, obj, date1, date2):
        filters = {
            'kill_house': obj,
            'trash': False,
            'archive_wage': False
        }
        if date1 and date2:
            filters.update({
                'date__date__gte': date1,
                'date__date__lte': date2,
            })
        return filters

    def get_slaughter_transactions_filters(self, obj, date1, date2):
        filters = {
            'kill_house': obj,
            'trash': False,
            'status': 'completed',
        }
        if date1 and date2:
            filters.update({
                'date__date__gte': date1,
                'date__date__lte': date2,
            })
        return filters

    def filter_province_kill_requests(self, filters):
        return ProvinceKillRequest.objects.filter(**filters).exclude(
            union_share=0, company_share=0, guilds_share=0
        ).only(
            'total_killed_weight', 'union_share', 'company_share',
            'guilds_share', 'total_wage_amount', 'total_killed_quantity'
        )

    def filter_kill_house_requests(self, filters, obj):
        return KillHouseRequest.objects.filter(filters).select_related(
            'province_kill_request'
        ).only(
            'accepted_real_weight', 'province_kill_request__union_share_percent',
            'province_kill_request__wage', 'province_kill_request__guilds_share_percent',
            'province_kill_request__company_share_percent', 'accepted_real_quantity'
        ).filter(
            Q(killer=obj) | Q(killhouse_user=obj)
        )

    def filter_free_bars(self, filters):
        return KillHouseFreeBarInformation.objects.filter(**filters).only(
            'live_weight', 'union_share', 'weight_of_carcasses',
            'company_share', 'guilds_share', 'total_wage_amount'
        )

    def filter_slaughter_transactions(self, filters):
        return InternalTransaction.objects.filter(**filters).only(
            'union_share', 'company_share', 'guilds_share', 'amount'
        )

    def calculate_wage_data(self, obj, province_kill_requests, kill_house_requests, free_bars, slaughter_transactions):
        def sum_field(queryset, field):
            return queryset.aggregate(total=Sum(field))['total'] or 0

        def calculate_wage(queryset, weight_field, percent_field):
            return queryset.aggregate(
                total=Sum(F(weight_field) * F(percent_field) / 100)
            )['total'] or 0

        wage_data = {
            'province_kill_requests_total_wage': sum_field(province_kill_requests, 'total_wage_amount'),
            'kill_house_requests_total_wage': sum_field(kill_house_requests,
                                                        F('accepted_real_weight') * F('province_kill_request__wage')),
            'free_bars_live_total_wage': sum_field(free_bars.filter(buy_type='live'), 'total_wage_amount'),
            'free_bars_carcases_total_wage': sum_field(free_bars.filter(buy_type='carcass'), 'total_wage_amount'),
            'total_paid_wage': sum_field(slaughter_transactions, 'amount'),
            'union_total_paid_wage': sum_field(slaughter_transactions, 'union_share'),
            'company_total_paid_wage': sum_field(slaughter_transactions, 'company_share'),
            'guilds_total_paid_wage': sum_field(slaughter_transactions, 'guilds_share'),
            'union_total_wage': calculate_wage(kill_house_requests, 'accepted_real_weight',
                                               'province_kill_request__union_share_percent') + sum_field(
                province_kill_requests, 'union_share') + sum_field(free_bars, 'union_share'),
            'company_total_wage': calculate_wage(kill_house_requests, 'accepted_real_weight',
                                                 'province_kill_request__company_share_percent') + sum_field(
                province_kill_requests, 'company_share') + sum_field(free_bars, 'company_share'),
            'guilds_total_wage': calculate_wage(kill_house_requests, 'accepted_real_weight',
                                                'province_kill_request__guilds_share_percent') + sum_field(
                province_kill_requests, 'guilds_share') + sum_field(free_bars, 'guilds_share'),
        }

        wage_data['total_wage'] = sum(wage_data[key] for key in wage_data if
                                      key in ['province_kill_requests_total_wage', 'kill_house_requests_total_wage',
                                              'free_bars_live_total_wage', 'free_bars_carcases_total_wage'])
        wage_data['not_paied'] = wage_data['total_wage'] - (wage_data['total_paid_wage'] + obj.off)
        wage_data['union_total_unpaid_wage'] = wage_data['union_total_wage'] - wage_data['union_total_paid_wage']
        wage_data['company_total_unpaid_wage'] = wage_data['company_total_wage'] - wage_data['company_total_paid_wage']
        wage_data['guilds_total_unpaid_wage'] = wage_data['guilds_total_wage'] - wage_data['guilds_total_paid_wage']

        return wage_data

    def get_basic_info(self, obj, province_kill_requests, kill_house_requests, free_bars, slaughter_transactions,
                       wage_data):
        def sum_field(queryset, field):
            return queryset.aggregate(total=Sum(field))['total'] or 0

        return {
            "type": self.get_killer_type(obj),
            "killer": obj.killer,
            "kill_house_name": obj.name,
            "fullname": obj.kill_house_operator.user.fullname,
            "mobile": obj.kill_house_operator.user.mobile,
            "city": obj.kill_house_operator.user.city.name,
            "len_province_kill_requests": len(province_kill_requests),
            "total_killed_quantity": sum_field(province_kill_requests, 'total_killed_quantity'),
            "total_killed_weight": sum_field(province_kill_requests, 'total_killed_weight'),
            "len_kill_house_requests": len(kill_house_requests),
            "len_slaughter_transactions": len(slaughter_transactions),
            "accepted_real_quantity": sum_field(kill_house_requests, 'accepted_real_quantity'),
            "accepted_real_weight": sum_field(kill_house_requests, 'accepted_real_weight'),
            "free_bar_live_quantity": sum_field(free_bars.filter(buy_type='live'), 'quantity'),
            "free_bar_live_weight": sum_field(free_bars.filter(buy_type='live'), 'live_weight'),
            "free_bar_carcasses_quantity": sum_field(free_bars.filter(buy_type='carcass'), 'number_of_carcasses'),
            "free_bar_carcasses_weight": sum_field(free_bars.filter(buy_type='carcass'), 'weight_of_carcasses'),
            "union_total_wage": wage_data['union_total_wage'],
            "union_total_unpaid_wage": wage_data['union_total_unpaid_wage'],
            "union_total_paid_wage": wage_data['total_paid_wage'],
        }

    def get_killer_type(self, obj):
        if obj.killer:
            if obj.type == 'exclusive':
                parent = KillHousePercentage.objects.get(kill_house=obj, trash=False)
                return f'کشتارکن اختصاصی {parent.kill_house_for_killer.name}'
            elif obj.type == 'public':
                return 'کشتارکن عمومی'
        return 'کشتارگاه'

    def prepare_info(self, role, obj, basic_info, slaughter_transactions, wage_data):
        # def sum_field(queryset, field):
        #     return queryset.aggregate(total=Sum(field))['total'] or 0

        if role == 'ProvinceOperator':
            return basic_info
        elif role == 'AdminX':
            admin_info = {
                "off": obj.off,
                # "free_bars_live_weight": sum_field(
                #     KillHouseFreeBarInformation.objects.filter(kill_house=obj, trash=False, buy_type='live'),
                #     'live_weight'),
                # "free_bars_carcasses_weight": sum_field(
                #     KillHouseFreeBarInformation.objects.filter(kill_house=obj, trash=False, buy_type='carcass'),
                #     'weight_of_carcasses'),
                **wage_data
            }
            return {**basic_info, **admin_info}
        return {}


class GenaeralWageSerailizer(serializers.ModelSerializer):
    general = serializers.SerializerMethodField('get_general')

    class Meta:
        model = ProvinceKillRequest
        fields = ['key', 'general']

    def get_general(self, province_kill_requests):
        type = 'total_wage_amount'
        share_type = 'amount'

        total_weight = 0
        total_wage = 0
        total_paid_wage = 0
        union_total_paid_wage = 0
        company_total_paid_wage = 0
        guilds_total_paid_wage = 0
        freezing_province_kill_requests = province_kill_requests.filter(
            province_request__poultry_request__freezing=True).only('total_killed_weight', 'union_share',
                                                                   'company_share',
                                                                   'total_killed_weight', 'guilds_share')

        kill_house_requests = KillHouseRequest.objects.filter(trash=False, archive_wage=False).exclude(
            province_kill_request__union_share=0, province_kill_request__company_share=0,
            province_kill_request__guilds_share=0).only('accepted_real_weight',
                                                        'province_kill_request__union_share_percent',
                                                        'province_kill_request__wage', 'accepted_real_weight',
                                                        'province_kill_request__guilds_share_percent',
                                                        'province_kill_request__company_share_percent')
        freezing_kill_house_requests = kill_house_requests.filter(
            province_kill_request__province_request__poultry_request__freezing=True)

        chain_allocations = ChainAllocation.objects.filter(trash=False, state='accepted').only('weight', 'union_share',
                                                                                               'company_share',
                                                                                               'guilds_share',
                                                                                               )
        free_bars = KillHouseFreeBarInformation.objects.filter(archive_wage=False, trash=False).only('live_weight',
                                                                                                     'union_share',
                                                                                                     'weight_of_carcasses',
                                                                                                     'company_share',
                                                                                                     'guilds_share',
                                                                                                     )
        total_slaughter_transactions = InternalTransaction.objects.filter(payer_type='kill_house',
                                                                          status='completed',
                                                                          trash=False).only('union_share',
                                                                                            'company_share',
                                                                                            'guilds_share')
        total_chain_company_transactions = InternalTransaction.objects.filter(payer_type='chain_company',
                                                                              status='completed', trash=False).only(
            'union_share', 'company_share', 'guilds_share')

        union_province_kill_request_total_weight = \
            province_kill_requests.filter(union_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        freezing_union_province_kill_request_total_weight = \
            freezing_province_kill_requests.filter(union_share__gt=0).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        union_province_kill_request_total_weight += \
            kill_house_requests.filter(province_kill_request__union_share__gt=0,
                                       province_kill_request__province_request__poultry_request__freezing=False).aggregate(
                total=Sum('accepted_real_weight'))[
                'total'] or 0

        freezing_union_province_kill_request_total_weight += \
            freezing_kill_house_requests.filter(province_kill_request__union_share__gt=0).aggregate(
                total=Sum('accepted_real_weight'))[
                'total'] or 0

        union_province_kill_request_total_wage = \
            province_kill_requests.filter(union_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('union_share'))[
                'total'] or 0
        union_province_kill_request_total_wage += \
            kill_house_requests.filter(province_kill_request__union_share__gt=0,
                                       province_kill_request__province_request__poultry_request__freezing=False).aggregate(
                total=Sum(
                    F('accepted_real_weight') * F('province_kill_request__wage') * (
                            F('province_kill_request__union_share_percent') / 100)))[
                'total'] or 0

        freezing_union_province_kill_request_total_wage = \
            freezing_province_kill_requests.filter(union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0
        freezing_union_province_kill_request_total_wage += \
            freezing_kill_house_requests.filter(province_kill_request__union_share__gt=0).aggregate(total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__union_share_percent') / 100)))[
                'total'] or 0
        union_chain_allocation_total_weight = \
            chain_allocations.filter(union_share__gt=0).aggregate(total=Sum('weight'))[
                'total'] or 0
        union_chain_allocation_total_wage = \
            chain_allocations.filter(union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0
        union_kill_house_free_live_bar_total_weight = \
            free_bars.filter(buy_type='live', union_share__gt=0).aggregate(total=Sum('live_weight'))[
                'total'] or 0
        union_kill_house_free_live_bar_total_wage = \
            free_bars.filter(buy_type='live', union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0

        union_kill_house_free_carcases_bar_total_weight = \
            free_bars.filter(buy_type='carcass', union_share__gt=0).aggregate(total=Sum('weight_of_carcasses'))[
                'total'] or 0
        union_kill_house_free_carcases_bar_total_wage = \
            free_bars.filter(buy_type='carcass', union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0
        union_total_paid_wage += \
            total_slaughter_transactions.filter(union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0
        union_total_paid_wage += \
            total_chain_company_transactions.filter(union_share__gt=0).aggregate(total=Sum('union_share'))[
                'total'] or 0
        union_total_weight = union_province_kill_request_total_weight + freezing_union_province_kill_request_total_weight + union_chain_allocation_total_weight + union_kill_house_free_live_bar_total_weight + union_kill_house_free_carcases_bar_total_weight
        union_total_wage = union_province_kill_request_total_wage + freezing_union_province_kill_request_total_wage + union_chain_allocation_total_wage + union_kill_house_free_live_bar_total_wage + union_kill_house_free_carcases_bar_total_wage
        union_total_unpaid_wage = union_total_wage - union_total_paid_wage

        company_province_kill_request_total_weight = \
            province_kill_requests.filter(company_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        company_province_kill_request_total_weight += \
            kill_house_requests.filter(province_kill_request__company_share__gt=0,
                                       province_kill_request__province_request__poultry_request__freezing=False).aggregate(
                total=Sum('accepted_real_weight'))[
                'total'] or 0
        freezing_company_province_kill_request_total_weight = \
            freezing_province_kill_requests.filter(company_share__gt=0).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        freezing_company_province_kill_request_total_weight += \
            freezing_kill_house_requests.filter(province_kill_request__company_share__gt=0).aggregate(
                total=Sum('accepted_real_weight'))[
                'total'] or 0
        company_province_kill_request_total_wage = \
            province_kill_requests.filter(company_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('company_share'))[
                'total'] or 0
        company_province_kill_request_total_wage += \
            kill_house_requests.filter(province_kill_request__company_share__gt=0,
                                       province_kill_request__province_request__poultry_request__freezing=False).aggregate(
                total=Sum(
                    F('accepted_real_weight') * F('province_kill_request__wage') * (
                            F('province_kill_request__company_share_percent') / 100)))[
                'total'] or 0
        freezing_company_province_kill_request_total_wage = \
            freezing_province_kill_requests.filter(company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0
        freezing_company_province_kill_request_total_wage += \
            freezing_kill_house_requests.filter(province_kill_request__company_share__gt=0).aggregate(total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__company_share_percent') / 100)))[
                'total'] or 0
        company_chain_allocation_total_weight = \
            chain_allocations.filter(company_share__gt=0).aggregate(total=Sum('weight'))[
                'total'] or 0
        company_chain_allocation_total_wage = \
            chain_allocations.filter(company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0
        company_kill_house_free_live_bar_total_weight = \
            free_bars.filter(buy_type='live', company_share__gt=0).aggregate(total=Sum('live_weight'))[
                'total'] or 0
        company_kill_house_free_live_bar_total_wage = \
            free_bars.filter(buy_type='live', company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0

        company_kill_house_free_carcases_bar_total_weight = \
            free_bars.filter(buy_type='carcass', company_share__gt=0).aggregate(
                total=Sum('weight_of_carcasses'))[
                'total'] or 0
        company_kill_house_free_carcases_bar_total_wage = \
            free_bars.filter(buy_type='carcass', company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0
        company_total_paid_wage += \
            total_slaughter_transactions.filter(company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0
        company_total_paid_wage += \
            total_chain_company_transactions.filter(company_share__gt=0).aggregate(total=Sum('company_share'))[
                'total'] or 0
        company_total_weight = company_province_kill_request_total_weight + freezing_company_province_kill_request_total_weight + company_chain_allocation_total_weight + company_kill_house_free_live_bar_total_weight + company_kill_house_free_carcases_bar_total_weight
        company_total_wage = company_province_kill_request_total_wage + freezing_company_province_kill_request_total_wage + company_chain_allocation_total_wage + company_kill_house_free_live_bar_total_wage + company_kill_house_free_carcases_bar_total_wage
        company_total_unpaid_wage = company_total_wage - company_total_paid_wage

        guilds_province_kill_request_total_weight = \
            province_kill_requests.filter(guilds_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        guilds_province_kill_request_total_weight += \
            kill_house_requests.filter(province_kill_request__guilds_share__gt=0,
                                       province_kill_request__province_request__poultry_request__freezing=False).aggregate(
                total=Sum('accepted_real_weight'))[
                'total'] or 0

        freezing_guilds_province_kill_request_total_weight = \
            freezing_province_kill_requests.filter(guilds_share__gt=0).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        freezing_guilds_province_kill_request_total_weight += \
            freezing_kill_house_requests.filter(province_kill_request__guilds_share__gt=0).aggregate(
                total=Sum('accepted_real_weight'))[
                'total'] or 0
        guilds_province_kill_request_total_wage = \
            province_kill_requests.filter(guilds_share__gt=0,
                                          province_request__poultry_request__freezing=False).aggregate(
                total=Sum('guilds_share'))[
                'total'] or 0
        guilds_province_kill_request_total_wage += \
            kill_house_requests.filter(province_kill_request__guilds_share__gt=0,
                                       province_kill_request__province_request__poultry_request__freezing=False).aggregate(
                total=Sum(
                    F('accepted_real_weight') * F('province_kill_request__wage') * (
                            F('province_kill_request__guilds_share_percent') / 100)))[
                'total'] or 0
        freezing_guilds_province_kill_request_total_wage = \
            freezing_province_kill_requests.filter(guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0
        freezing_guilds_province_kill_request_total_wage += \
            freezing_kill_house_requests.filter(province_kill_request__guilds_share__gt=0).aggregate(total=Sum(
                F('accepted_real_weight') * F('province_kill_request__wage') * (
                        F('province_kill_request__guilds_share_percent') / 100)))[
                'total'] or 0
        guilds_chain_allocation_total_weight = \
            chain_allocations.filter(guilds_share__gt=0).aggregate(total=Sum('weight'))[
                'total'] or 0
        guilds_chain_allocation_total_wage = \
            chain_allocations.filter(guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0
        guilds_kill_house_free_live_bar_total_weight = \
            free_bars.filter(buy_type='live', guilds_share__gt=0).aggregate(total=Sum('live_weight'))[
                'total'] or 0
        guilds_kill_house_free_live_bar_total_wage = \
            free_bars.filter(buy_type='live', guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0

        guilds_kill_house_free_carcases_bar_total_weight = \
            free_bars.filter(buy_type='carcass', guilds_share__gt=0).aggregate(
                total=Sum('weight_of_carcasses'))[
                'total'] or 0
        guilds_kill_house_free_carcases_bar_total_wage = \
            free_bars.filter(buy_type='carcass', guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0
        guilds_total_paid_wage += \
            total_slaughter_transactions.filter(guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0
        guilds_total_paid_wage += \
            total_chain_company_transactions.filter(guilds_share__gt=0).aggregate(total=Sum('guilds_share'))[
                'total'] or 0
        guilds_total_weight = guilds_province_kill_request_total_weight + freezing_guilds_province_kill_request_total_weight + guilds_chain_allocation_total_weight + guilds_kill_house_free_live_bar_total_weight + guilds_kill_house_free_carcases_bar_total_weight
        guilds_total_wage = guilds_province_kill_request_total_wage + freezing_guilds_province_kill_request_total_wage + guilds_chain_allocation_total_wage + guilds_kill_house_free_live_bar_total_wage + guilds_kill_house_free_carcases_bar_total_wage
        guilds_total_unpaid_wage = guilds_total_wage - guilds_total_paid_wage

        province_kill_requests_total_wage = \
            province_kill_requests.filter(province_request__poultry_request__freezing=False).aggregate(
                total=Sum(type))[
                'total'] or 0
        province_kill_requests_total_wage += \
            kill_house_requests.filter(
                province_kill_request__province_request__poultry_request__freezing=False).aggregate(
                total=Sum(F('accepted_real_weight') * F('province_kill_request__wage')))[
                'total'] or 0

        freezing_province_kill_requests_total_wage = freezing_province_kill_requests.aggregate(total=Sum(type))[
                                                         'total'] or 0
        freezing_province_kill_requests_total_wage += \
            freezing_kill_house_requests.aggregate(
                total=Sum(F('accepted_real_weight') * F('province_kill_request__wage')))[
                'total'] or 0

        free_bars_live = free_bars.filter(buy_type='live', trash=False).only('live_weight', 'union_share',
                                                                             'weight_of_carcasses',
                                                                             'company_share',
                                                                             'guilds_share',
                                                                             )
        free_bars_carcases = free_bars.filter(buy_type='carcass', trash=False).only('live_weight', 'union_share',
                                                                                    'weight_of_carcasses',
                                                                                    'company_share',
                                                                                    'guilds_share',
                                                                                    )

        total_wage += province_kill_requests_total_wage + freezing_province_kill_requests_total_wage

        province_kill_requests_total_weight = \
            province_kill_requests.filter(province_request__poultry_request__freezing=False).aggregate(
                total=Sum('total_killed_weight'))[
                'total'] or 0
        province_kill_requests_total_weight += kill_house_requests.filter(
            province_kill_request__province_request__poultry_request__freezing=False).aggregate(
            total=Sum('accepted_real_weight'))[
                                                   'total'] or 0

        freezing_province_kill_requests_total_weight = \
            freezing_province_kill_requests.aggregate(total=Sum('total_killed_weight'))[
                'total'] or 0
        freezing_province_kill_requests_total_weight += \
            freezing_kill_house_requests.aggregate(total=Sum('accepted_real_weight'))[
                'total'] or 0
        total_weight += province_kill_requests_total_weight + freezing_province_kill_requests_total_weight

        chain_allocations_total_wage = chain_allocations.aggregate(total=Sum(type))[
                                           'total'] or 0
        total_wage += chain_allocations_total_wage
        chain_allocations_total_weight = chain_allocations.aggregate(total=Sum('weight'))[
                                             'total'] or 0
        total_weight += chain_allocations_total_weight
        free_bars_live_total_wage = free_bars_live.aggregate(total=Sum(type))[
                                        'total'] or 0
        total_wage += free_bars_live_total_wage
        free_bars_live_total_weight = free_bars_live.aggregate(total=Sum('live_weight'))[
                                          'total'] or 0
        total_weight += free_bars_live_total_weight
        free_bars_carcases_total_wage = free_bars_carcases.aggregate(total=Sum(type))[
                                            'total'] or 0
        total_wage += free_bars_carcases_total_wage
        free_bars_carcases_total_weight = free_bars_carcases.aggregate(total=Sum('weight_of_carcasses'))[
                                              'total'] or 0
        total_weight += free_bars_carcases_total_weight
        slaughter_transactions = InternalTransaction.objects.filter(payer_type='kill_house', status='completed',
                                                                    trash=False)

        total_paid_wage += slaughter_transactions.aggregate(total=Sum(share_type))[
                               'total'] or 0
        chain_company_transactions = InternalTransaction.objects.filter(payer_type='chain_company',
                                                                        status='completed', trash=False)
        total_paid_wage += chain_company_transactions.aggregate(total=Sum(share_type))[
                               'total'] or 0

        values_list = {
            "total_weight": total_weight,
            "total_wage": total_wage,
            "union_total_wage": union_total_wage,
            "company_total_wage": company_total_wage,
            "guilds_total_wage": guilds_total_wage,
            "total_paid_wage": total_paid_wage,
            "union_total_paid_wage": union_total_paid_wage,
            "company_total_paid_wage": company_total_paid_wage,
            "guilds_total_paid_wage": guilds_total_paid_wage,
            "total_wage - total_paid_wage": total_wage - total_paid_wage,
            "union_total_unpaid_wage": union_total_unpaid_wage,
            "company_total_unpaid_wage": company_total_unpaid_wage,
            "guilds_total_unpaid_wage": guilds_total_unpaid_wage,

        }

        values_list2 = {
            "province_kill_requests_total_weight": province_kill_requests_total_weight,
            "province_kill_requests_total_wage": province_kill_requests_total_wage,
            "freezing_province_kill_requests_total_weight": freezing_province_kill_requests_total_weight,
            "freezing_province_kill_requests_total_wage": freezing_province_kill_requests_total_wage,
            "chain_allocations_total_weight": chain_allocations_total_weight,
            "chain_allocations_total_wage": chain_allocations_total_wage,
            "free_bars_live_total_weight": free_bars_live_total_weight,
            "free_bars_live_total_wage": free_bars_live_total_wage,
            "free_bars_carcases_total_weight": free_bars_carcases_total_weight,
            "free_bars_carcases_total_wage": free_bars_carcases_total_wage,

        }

        values_union = {
            "union_province_kill_request_total_weight": union_province_kill_request_total_weight,
            "union_province_kill_request_total_wage": union_province_kill_request_total_wage,
            "freezing_union_province_kill_request_total_weight": freezing_union_province_kill_request_total_weight,
            "freezing_union_province_kill_request_total_wage": freezing_union_province_kill_request_total_wage,
            "union_chain_allocation_total_weight": union_chain_allocation_total_weight,
            "union_chain_allocation_total_wage": union_chain_allocation_total_wage,
            "union_kill_house_free_live_bar_total_weight": union_kill_house_free_live_bar_total_weight,
            "union_kill_house_free_live_bar_total_wage": union_kill_house_free_live_bar_total_wage,
            "union_kill_house_free_carcases_bar_total_weight": union_kill_house_free_carcases_bar_total_weight,
            "union_kill_house_free_carcases_bar_total_wage": union_kill_house_free_carcases_bar_total_wage,
            "union_total_wage": union_total_wage,
            "union_total_paid_wage": union_total_paid_wage,
            "union_total_unpaid_wage": union_total_unpaid_wage,

        }

        values_company = {
            "company_province_kill_request_total_weight": company_province_kill_request_total_weight,
            "company_province_kill_request_total_wage": company_province_kill_request_total_wage,
            "freezing_company_province_kill_request_total_weight": freezing_company_province_kill_request_total_weight,
            "freezing_company_province_kill_request_total_wage": freezing_company_province_kill_request_total_wage,
            "company_chain_allocation_total_weight": company_chain_allocation_total_weight,
            "company_chain_allocation_total_wage": company_chain_allocation_total_wage,
            "company_kill_house_free_live_bar_total_weight": company_kill_house_free_live_bar_total_weight,
            "company_kill_house_free_live_bar_total_wage": company_kill_house_free_live_bar_total_wage,
            "company_kill_house_free_carcases_bar_total_weight": company_kill_house_free_carcases_bar_total_weight,
            "company_kill_house_free_carcases_bar_total_wage": company_kill_house_free_carcases_bar_total_wage,
            "company_total_wage": company_total_wage,
            "company_total_paid_wage": company_total_paid_wage,
            "company_total_unpaid_wage": company_total_unpaid_wage,

        }

        values_guild = {
            "guilds_province_kill_request_total_weight": guilds_province_kill_request_total_weight,
            "guilds_province_kill_request_total_wage": guilds_province_kill_request_total_wage,
            "freezing_guilds_province_kill_request_total_weight": freezing_guilds_province_kill_request_total_weight,
            "freezing_guilds_province_kill_request_total_wage": freezing_guilds_province_kill_request_total_wage,
            "guilds_chain_allocation_total_weight": guilds_chain_allocation_total_weight,
            "guilds_chain_allocation_total_wage": guilds_chain_allocation_total_wage,
            "guilds_kill_house_free_live_bar_total_weight": guilds_kill_house_free_live_bar_total_weight,
            "guilds_kill_house_free_live_bar_total_wage": guilds_kill_house_free_live_bar_total_wage,
            "guilds_kill_house_free_carcases_bar_total_weight": guilds_kill_house_free_carcases_bar_total_weight,
            "guilds_kill_house_free_carcases_bar_total_wage": guilds_kill_house_free_carcases_bar_total_wage,
            "guilds_total_wage": guilds_total_wage,
            "guilds_total_paid_wage": guilds_total_paid_wage,
            "guilds_total_unpaid_wage": guilds_total_unpaid_wage,

        }

        dict1 = {
            "general": values_list,
            'detail': values_list2,
            'union': values_union,
            'company': values_company,
            'guild': values_guild
        }
        return dict1


class IranProvinceSerializer(serializers.ModelSerializer):
    class Meta:
        model = IranProvinces
        fields = ['id', 'name']

class IranCitiesSerializer(serializers.ModelSerializer):
    class Meta:
        model = IranCities
        fields = ['id', 'name']


class AgeNotificationPoultrySerilizer(serializers.ModelSerializer):
    class Meta:
        model = AgeNotificationPoultry
        fields = '__all__'


class CookieSamasatSerilizer(serializers.ModelSerializer):
    class Meta:
        model = CookieSamasat
        fields = '__all__'
