from itertools import chain from django.db.models import Sum, Q, F from deposit_id import wage_counting_type from panel.KillHouse.helpers import get_difference_carcasses_weight from panel.models import KillHouse, ProvinceKillRequest, KillHouseRequest, PoultryHatching, PoultryRequest, WageType, \ PercentageOfWageType, SubSectorTransactions, SubSectorPercentageOfWageType, Poultry, KillHouseFreeBarInformation, \ KillHouseFreeSaleBarInformation, BarDifferenceRequest def get_percent_for_city_sub_sector_finance_info(): poultries = Poultry.objects.filter(trash=False, city_operator__isnull=False).order_by('id') hatchings = PoultryHatching.objects.filter(poultry__in=poultries, killed_quantity__gt=0, trash=False).order_by( 'poultry') province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, archive_wage=False, state__in=('pending', 'accepted'), temporary_trash=False, temporary_deleted=False, province_request__poultry_request__hatching__in=hatchings) kill_houses = KillHouse.objects.filter(pk__in=province_kill_requests.values_list('killhouse_user', flat=True)) total_province_live_weight = province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 kill_house_free_bar_info = KillHouseFreeBarInformation.objects.filter(kill_house__in=kill_houses, archive_wage=False, calculate_status=True, trash=False) kill_house_free_sale_bar_info = KillHouseFreeSaleBarInformation.objects.filter( kill_house__in=kill_houses, archive_wage=False, calculate_status=True, trash=False) total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0 total_province_carcasses_weight = total_province_live_weight * 0.75 total_out_carcasses_buying_for_pure_province_carcasses_weight = \ kill_house_free_bar_info.aggregate(total=Sum('weight_of_carcasses'))['total'] or 0 out_selling_out_carcasses_buying_difference = total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight if ( total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight) > 0 else 0 total_pure_province_carcasses_weight = total_province_carcasses_weight - out_selling_out_carcasses_buying_difference internal_percent = ( total_pure_province_carcasses_weight / total_province_carcasses_weight) * 100 if total_province_carcasses_weight > 0 else 0 external_percent = 100 - internal_percent return {"internal_percent": internal_percent, "external_percent": external_percent} # def get_city_sub_sector_finance_info(poultries,operator): # total_wage_type = WageType.objects.filter(trash=False) # province_live_wage_amount = total_wage_type.filter(en_name='province-kill-request', trash=False).first().amount # free_sell_carcesses_wage_amount = total_wage_type.filter(en_name='carcasse-sell', trash=False).first().amount # out_poultry_request_amount = total_wage_type.filter(en_name='poultry-sell-out-province', trash=False).first().amount # # percentages_wage_type = PercentageOfWageType.objects.filter(trash=False) # union_province_kill_request_percent = percentages_wage_type.filter( # wage_type__en_name='province-kill-request', share_type__en_name='union').first().percent / 100 # union_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', # share_type__en_name='union').first().percent / 100 # # union_out_poultry_request_percent = percentages_wage_type.filter(wage_type__en_name='poultry-sell-out-province', # share_type__en_name='union').first().percent / 100 # city_percent_province_kill_request= SubSectorPercentageOfWageType.objects.filter(percentage_of_wage_type__wage_type__en_name='province-kill-request').first().percent / 100 # city_percent_carcasse_sell= SubSectorPercentageOfWageType.objects.filter(percentage_of_wage_type__wage_type__en_name='carcasse-sell').first().percent / 100 # city_percent_out_poultry_request= SubSectorPercentageOfWageType.objects.filter(percentage_of_wage_type__wage_type__en_name='poultry-sell-out-province').first().percent / 100 # union_province_kill_request_final_amount = union_province_kill_request_percent * province_live_wage_amount # union_carcasse_sell_final_amount = union_free_sell_carcasses_percent * free_sell_carcesses_wage_amount # union_out_poultry_request_amount = union_out_poultry_request_percent * out_poultry_request_amount # city_province_kill_request_final_amount = union_province_kill_request_final_amount * city_percent_province_kill_request # city_carcasse_sell_final_amount = union_carcasse_sell_final_amount * city_percent_carcasse_sell # city_out_poultry_request_final_amount = union_out_poultry_request_amount * city_percent_out_poultry_request # # # # hatchings = PoultryHatching.objects.filter(poultry__in=poultries,killed_quantity__gt=0, trash=False).order_by('poultry') # # hatching_quantity = hatchings.aggregate(total=Sum('quantity'))[ # 'total'] or 0 # poultry_requests = PoultryRequest.objects.filter(trash=False, state_process__in=('pending', 'accepted'), # province_state__in=('pending', 'accepted'), out=True, # out_province_request_cancel=False, temporary_trash=False, # temporary_deleted=False, hatching__in=hatchings) # out_province_poultry_request_quantity = poultry_requests.aggregate(total=Sum('quantity'))[ # 'total'] or 0 # out_province_poultry_request_weight = poultry_requests.aggregate(total=Sum(F('quantity') * F('Index_weight')))[ # 'total'] or 0 # # # # province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, # archive_wage=False, # state__in=('pending', 'accepted'), # temporary_trash=False, temporary_deleted=False, # # first_car_allocated_quantity=0, # province_request__poultry_request__hatching__in=hatchings) # # total_province_live_weight = province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ # 'total'] or 0 # total_province_live_quantity = province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ # 'total'] or 0 # total_province_carcasses_weight = total_province_live_weight * 0.75 # weight_percent = get_percent_for_city_sub_sector_finance_info() # total_pure_internal_province_carcasses_weight = (weight_percent['internal_percent']/100) * total_province_carcasses_weight # total_pure_external_province_carcasses_weight = (weight_percent['external_percent']/100) * total_province_carcasses_weight # total_pure_internal_province_carcasses_amount = total_pure_internal_province_carcasses_weight * city_province_kill_request_final_amount # total_pure_external_province_carcasses_amount = total_pure_external_province_carcasses_weight * city_carcasse_sell_final_amount # out_province_poultry_request_amount = out_province_poultry_request_weight * city_out_poultry_request_final_amount # total_wage_amount = total_pure_internal_province_carcasses_amount + total_pure_external_province_carcasses_amount + out_province_poultry_request_amount # total_killed_quantity = total_province_live_quantity + out_province_poultry_request_quantity # hatching_killing_percent = ((total_killed_quantity / hatching_quantity) if hatching_quantity > 0 else 0) * 100 # city_operator_deposit = SubSectorTransactions.objects.filter(trash=False,city_operator=operator) # city_deposit = city_operator_deposit.aggregate(total=Sum('amount'))['total'] or 0 # total_remain_wage_amount = total_wage_amount - city_deposit # # result = { # "poultries": len(poultries), # "hatchings": len(hatchings), # "hatchings_quantity": hatching_quantity, # "total_province_kill_requests_quantity": total_province_live_quantity, # "total_province_kill_requests_weight": total_province_live_weight, # "total_province_carcasses_weight": total_province_carcasses_weight, # "total_pure_internal_province_carcasses_weight": total_pure_internal_province_carcasses_weight, # "total_pure_external_province_carcasses_weight": total_pure_external_province_carcasses_weight, # "out_province_poultry_request_quantity": out_province_poultry_request_quantity, # "out_province_poultry_request_weight": out_province_poultry_request_weight, # "total_killed_quantity": total_killed_quantity, # "hatching_killing_percent": hatching_killing_percent, # "out_province_poultry_request_amount": out_province_poultry_request_amount, # "total_pure_internal_province_carcasses_amount": total_pure_internal_province_carcasses_amount, # "total_pure_external_province_carcasses_amount": total_pure_external_province_carcasses_amount, # "total_wage_amount": total_wage_amount, # "number_of_city_deposit": len(city_operator_deposit), # "city_deposit": city_deposit, # "total_remain_wage_amount": total_remain_wage_amount, # } # return result def get_city_sub_sector_finance_info(poultries, operator,date1,date2): total_wage_type = WageType.objects.filter(trash=False) province_live_wage_amount = total_wage_type.filter(en_name='province-kill-request', trash=False).first().amount free_sell_carcasses_wage_amount = total_wage_type.filter(en_name='carcasse-sell', trash=False).first().amount out_poultry_request_wage_amount = total_wage_type.filter(en_name='poultry-sell-out-province', trash=False).first().amount kill_houses = KillHouse.objects.filter(out_province=False, trash=False).order_by('id') hatchings = PoultryHatching.objects.filter(poultry__in=poultries, killed_quantity__gt=0, trash=False).order_by( 'poultry') if date1 is not None: total_province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user__in=kill_houses, trash=False, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2).order_by( 'id') total_kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user__in=kill_houses) & Q(killer__in=kill_houses)) | Q( Q(killhouse_user__in=kill_houses) & Q(killer__isnull=True)) | Q( Q(killhouse_user__in=kill_houses) | Q(killer__in=kill_houses)), archive_wage=False, trash=False, calculate_status=True, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2 ) poultry_requests = PoultryRequest.objects.filter(trash=False, state_process__in=('pending', 'accepted'), province_state__in=('pending', 'accepted'), out=True, out_province_request_cancel=False, temporary_trash=False, has_wage=True, wage_pay=True, temporary_deleted=False, hatching__in=hatchings, send_date__date__gte=date1, send_date__date__lte=date2) city_operator_deposit = SubSectorTransactions.objects.filter(trash=False, city_operator=operator, date__date__gte=date1, date__date__lte=date2) difference_requests = BarDifferenceRequest.objects.filter(kill_house__in=kill_houses, trash=False, state='accepted', acceptor_date__date__gte=date1, acceptor_date__date__lte=date2) else: poultry_requests = PoultryRequest.objects.filter(trash=False, state_process__in=('pending', 'accepted'), province_state__in=('pending', 'accepted'), out=True, out_province_request_cancel=False, temporary_trash=False, has_wage=True, wage_pay=True, poultry__in=poultries, temporary_deleted=False) total_province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user__in=kill_houses, trash=False, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0).order_by('id') total_kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user__in=kill_houses) & Q(killer__in=kill_houses)) | Q( Q(killhouse_user__in=kill_houses) & Q(killer__isnull=True)) | Q( Q(killhouse_user__in=kill_houses) | Q(killer__in=kill_houses)), archive_wage=False, trash=False, calculate_status=True ) difference_requests = BarDifferenceRequest.objects.filter(kill_house__in=kill_houses, trash=False, state='accepted') city_operator_deposit = SubSectorTransactions.objects.filter(trash=False, city_operator=operator) total_province_live_weight = total_province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 total_province_live_quantity = total_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 total_province_live_weight += total_kill_house_requests.aggregate(total=Sum('accepted_real_weight'))['total'] or 0 total_province_live_quantity += total_kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))[ 'total'] or 0 total_province_live_weight += \ difference_requests.aggregate(total=Sum('weight'))['total'] or 0 total_province_carcasses_weight = total_province_live_weight * 0.75 total_pure_province_carcasses_weight = total_province_carcasses_weight - get_difference_carcasses_weight( kill_houses,date1,date2) different_percent = (total_pure_province_carcasses_weight / total_province_carcasses_weight) if total_province_carcasses_weight > 0 else 0 province_kill_requests = total_province_kill_requests.filter( province_request__poultry_request__hatching__in=hatchings).order_by('id') kill_house_requests = total_kill_house_requests.filter( province_request__poultry_request__hatching__in=hatchings).order_by('id') province_kill_requests_kill_houses = KillHouse.objects.filter(pk__in=province_kill_requests.values_list('killhouse_user',flat=True)).order_by('id') hatching_list1 = hatchings.filter( pk__in=province_kill_requests.values_list('province_request__poultry_request__hatching', flat=True), poultry__in=poultries, killed_quantity__gt=0, trash=False).values_list('id', flat=True) hatching_list2 = hatchings.filter( pk__in=kill_house_requests.values_list('province_request__poultry_request__hatching', flat=True), poultry__in=poultries, killed_quantity__gt=0, trash=False).values_list('id', flat=True) hatching_list3 = hatchings.filter(pk__in=poultry_requests.values_list('hatching', flat=True), poultry__in=poultries, killed_quantity__gt=0, trash=False).values_list('id', flat=True) total_hatching_list = chain(hatching_list1, hatching_list2, hatching_list3) hatchings = hatchings.filter(pk__in=total_hatching_list) hatching_quantity = hatchings.aggregate(total=Sum('quantity'))[ 'total'] or 0 poultry_requests = poultry_requests.filter(hatching__id__in=hatching_list3) out_province_poultry_request_quantity = poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 out_province_poultry_request_weight = poultry_requests.aggregate(total=Sum(F('quantity') * F('Index_weight')))[ 'total'] or 0 province_live_weight = province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 province_live_quantity = province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 province_live_weight += kill_house_requests.aggregate(total=Sum('accepted_real_weight'))['total'] or 0 province_live_quantity += kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))['total'] or 0 province_live_weight += \ difference_requests.filter(kill_house__in=province_kill_requests_kill_houses).aggregate(total=Sum('weight'))['total'] or 0 province_carcasses_weight = province_live_weight * 0.75 internal_total_pure_province_carcasses_weight = province_carcasses_weight * different_percent external_total_pure_province_carcasses_weight = province_carcasses_weight - internal_total_pure_province_carcasses_weight # total_pure_province_carcasses_price = internal_total_pure_province_carcasses_weight * province_live_wage_amount # total_out_selling_province_carcasses_price = external_total_pure_province_carcasses_weight * free_sell_carcesses_weight_amount percentages_wage_type = PercentageOfWageType.objects.filter(trash=False) union_province_kill_request_percent = percentages_wage_type.filter( wage_type__en_name='province-kill-request', share_type__en_name='union').first().percent / 100 union_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', share_type__en_name='union').first().percent / 100 union_out_poultry_request_percent = percentages_wage_type.filter(wage_type__en_name='poultry-sell-out-province', share_type__en_name='union').first().percent / 100 city_percent_province_kill_request = SubSectorPercentageOfWageType.objects.filter( percentage_of_wage_type__wage_type__en_name='province-kill-request').first().percent / 100 city_percent_carcasse_sell = SubSectorPercentageOfWageType.objects.filter( percentage_of_wage_type__wage_type__en_name='carcasse-sell').first().percent / 100 city_percent_out_poultry_request = SubSectorPercentageOfWageType.objects.filter( percentage_of_wage_type__wage_type__en_name='poultry-sell-out-province').first().percent / 100 union_province_kill_request_final_amount = union_province_kill_request_percent * province_live_wage_amount union_carcasse_sell_final_amount = union_free_sell_carcasses_percent * free_sell_carcasses_wage_amount union_out_poultry_request_amount = union_out_poultry_request_percent * out_poultry_request_wage_amount city_province_kill_request_final_amount = union_province_kill_request_final_amount * city_percent_province_kill_request city_carcasse_sell_final_amount = union_carcasse_sell_final_amount * city_percent_carcasse_sell city_out_poultry_request_final_amount = union_out_poultry_request_amount * city_percent_out_poultry_request total_pure_internal_province_carcasses_amount = internal_total_pure_province_carcasses_weight * city_province_kill_request_final_amount total_pure_external_province_carcasses_amount = external_total_pure_province_carcasses_weight * city_carcasse_sell_final_amount out_province_poultry_request_amount = out_province_poultry_request_weight * city_out_poultry_request_final_amount total_wage_amount = total_pure_internal_province_carcasses_amount + total_pure_external_province_carcasses_amount + out_province_poultry_request_amount total_killed_quantity = province_live_quantity + out_province_poultry_request_quantity hatching_killing_percent = ((total_killed_quantity / hatching_quantity) if hatching_quantity > 0 else 0) * 100 city_deposit = city_operator_deposit.aggregate(total=Sum('amount'))['total'] or 0 total_remain_wage_amount = total_wage_amount - city_deposit result = { "poultries": len(poultries), "hatchings": len(hatchings), "hatchings_quantity": hatching_quantity, "total_province_kill_requests_quantity": province_live_quantity, "total_province_kill_requests_weight": province_live_weight, "total_province_carcasses_weight": province_carcasses_weight, "total_pure_internal_province_carcasses_weight": internal_total_pure_province_carcasses_weight, "total_pure_external_province_carcasses_weight": external_total_pure_province_carcasses_weight, "out_province_poultry_request_quantity": out_province_poultry_request_quantity, "out_province_poultry_request_weight": out_province_poultry_request_weight, "total_killed_quantity": total_killed_quantity, "hatching_killing_percent": hatching_killing_percent, "out_province_poultry_request_amount": out_province_poultry_request_amount, "total_pure_internal_province_carcasses_amount": total_pure_internal_province_carcasses_amount, "total_pure_external_province_carcasses_amount": total_pure_external_province_carcasses_amount, "total_wage_amount": total_wage_amount, "number_of_city_deposit": len(city_operator_deposit), "city_deposit": city_deposit, "total_remain_wage_amount": total_remain_wage_amount, } return result def get_city_sub_sector_finance_info_with_date(poultries, operator,date1,date2): total_wage_type = WageType.objects.filter(trash=False) province_live_wage_amount = total_wage_type.filter(en_name='province-kill-request', trash=False).first().amount free_sell_carcasses_wage_amount = total_wage_type.filter(en_name='carcasse-sell', trash=False).first().amount out_poultry_request_wage_amount = total_wage_type.filter(en_name='poultry-sell-out-province', trash=False).first().amount kill_houses = KillHouse.objects.filter(out_province=False, trash=False).order_by('id') total_province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user__in=kill_houses, trash=False, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0,kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2).order_by('id') total_kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user__in=kill_houses) & Q(killer__in=kill_houses)) | Q( Q(killhouse_user__in=kill_houses) & Q(killer__isnull=True)) | Q( Q(killhouse_user__in=kill_houses) | Q(killer__in=kill_houses)), archive_wage=False, trash=False, calculate_status=True,kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2 ) total_province_live_weight = total_province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 total_province_live_quantity = total_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 total_province_live_weight += total_kill_house_requests.aggregate(total=Sum('accepted_real_weight'))['total'] or 0 total_province_live_quantity += total_kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))[ 'total'] or 0 difference_requests = BarDifferenceRequest.objects.filter(kill_house__in=kill_houses, trash=False, state='accepted',acceptor_date__date__gte=date1,acceptor_date__date__lte=date2) total_province_live_weight += \ difference_requests.aggregate(total=Sum('weight'))['total'] or 0 total_province_carcasses_weight = total_province_live_weight * 0.75 total_pure_province_carcasses_weight = total_province_carcasses_weight - get_difference_carcasses_weight( kill_houses) different_percent = total_pure_province_carcasses_weight / total_province_carcasses_weight hatchings = PoultryHatching.objects.filter(poultry__in=poultries, killed_quantity__gt=0, trash=False).order_by( 'poultry') province_kill_requests = total_province_kill_requests.filter( province_request__poultry_request__hatching__in=hatchings).order_by('id') kill_house_requests = total_kill_house_requests.filter( province_request__poultry_request__hatching__in=hatchings).order_by('id') poultry_requests = PoultryRequest.objects.filter(trash=False, state_process__in=('pending', 'accepted'), province_state__in=('pending', 'accepted'), out=True, out_province_request_cancel=False, temporary_trash=False, has_wage=True, wage_pay=True, temporary_deleted=False, hatching__in=hatchings,send_date__date__gte=date1, send_date__date__lte=date2) province_kill_requests_kill_houses = KillHouse.objects.filter(pk__in=province_kill_requests.values_list('killhouse_user',flat=True)).order_by('id') hatching_list1 = hatchings.filter( pk__in=province_kill_requests.values_list('province_request__poultry_request__hatching', flat=True), poultry__in=poultries, killed_quantity__gt=0, trash=False).values_list('id', flat=True) hatching_list2 = hatchings.filter( pk__in=kill_house_requests.values_list('province_request__poultry_request__hatching', flat=True), poultry__in=poultries, killed_quantity__gt=0, trash=False).values_list('id', flat=True) hatching_list3 = hatchings.filter(pk__in=poultry_requests.values_list('hatching', flat=True), poultry__in=poultries, killed_quantity__gt=0, trash=False).values_list('id', flat=True) total_hatching_list = chain(hatching_list1, hatching_list2, hatching_list3) hatchings = hatchings.filter(pk__in=total_hatching_list) hatching_quantity = hatchings.aggregate(total=Sum('quantity'))[ 'total'] or 0 poultry_requests = poultry_requests.filter(hatching__id__in=hatching_list3) out_province_poultry_request_quantity = poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 out_province_poultry_request_weight = poultry_requests.aggregate(total=Sum(F('quantity') * F('Index_weight')))[ 'total'] or 0 province_live_weight = province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 province_live_quantity = province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 province_live_weight += kill_house_requests.aggregate(total=Sum('accepted_real_weight'))['total'] or 0 province_live_quantity += kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))['total'] or 0 province_live_weight += \ difference_requests.filter(kill_house__in=province_kill_requests_kill_houses).aggregate(total=Sum('weight'))['total'] or 0 province_carcasses_weight = province_live_weight * 0.75 internal_total_pure_province_carcasses_weight = province_carcasses_weight * different_percent external_total_pure_province_carcasses_weight = province_carcasses_weight - internal_total_pure_province_carcasses_weight # total_pure_province_carcasses_price = internal_total_pure_province_carcasses_weight * province_live_wage_amount # total_out_selling_province_carcasses_price = external_total_pure_province_carcasses_weight * free_sell_carcesses_weight_amount percentages_wage_type = PercentageOfWageType.objects.filter(trash=False) union_province_kill_request_percent = percentages_wage_type.filter( wage_type__en_name='province-kill-request', share_type__en_name='union').first().percent / 100 union_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', share_type__en_name='union').first().percent / 100 union_out_poultry_request_percent = percentages_wage_type.filter(wage_type__en_name='poultry-sell-out-province', share_type__en_name='union').first().percent / 100 city_percent_province_kill_request = SubSectorPercentageOfWageType.objects.filter( percentage_of_wage_type__wage_type__en_name='province-kill-request').first().percent / 100 city_percent_carcasse_sell = SubSectorPercentageOfWageType.objects.filter( percentage_of_wage_type__wage_type__en_name='carcasse-sell').first().percent / 100 city_percent_out_poultry_request = SubSectorPercentageOfWageType.objects.filter( percentage_of_wage_type__wage_type__en_name='poultry-sell-out-province').first().percent / 100 union_province_kill_request_final_amount = union_province_kill_request_percent * province_live_wage_amount union_carcasse_sell_final_amount = union_free_sell_carcasses_percent * free_sell_carcasses_wage_amount union_out_poultry_request_amount = union_out_poultry_request_percent * out_poultry_request_wage_amount city_province_kill_request_final_amount = union_province_kill_request_final_amount * city_percent_province_kill_request city_carcasse_sell_final_amount = union_carcasse_sell_final_amount * city_percent_carcasse_sell city_out_poultry_request_final_amount = union_out_poultry_request_amount * city_percent_out_poultry_request total_pure_internal_province_carcasses_amount = internal_total_pure_province_carcasses_weight * city_province_kill_request_final_amount total_pure_external_province_carcasses_amount = external_total_pure_province_carcasses_weight * city_carcasse_sell_final_amount out_province_poultry_request_amount = out_province_poultry_request_weight * city_out_poultry_request_final_amount total_wage_amount = total_pure_internal_province_carcasses_amount + total_pure_external_province_carcasses_amount + out_province_poultry_request_amount total_killed_quantity = province_live_quantity + out_province_poultry_request_quantity hatching_killing_percent = ((total_killed_quantity / hatching_quantity) if hatching_quantity > 0 else 0) * 100 city_operator_deposit = SubSectorTransactions.objects.filter(trash=False, city_operator=operator,date__date__gte=date1,date__date__lte=date2) city_deposit = city_operator_deposit.aggregate(total=Sum('amount'))['total'] or 0 total_remain_wage_amount = total_wage_amount - city_deposit result = { "poultries": len(poultries), "hatchings": len(hatchings), "hatchings_quantity": hatching_quantity, "total_province_kill_requests_quantity": province_live_quantity, "total_province_kill_requests_weight": province_live_weight, "total_province_carcasses_weight": province_carcasses_weight, "total_pure_internal_province_carcasses_weight": internal_total_pure_province_carcasses_weight, "total_pure_external_province_carcasses_weight": external_total_pure_province_carcasses_weight, "out_province_poultry_request_quantity": out_province_poultry_request_quantity, "out_province_poultry_request_weight": out_province_poultry_request_weight, "total_killed_quantity": total_killed_quantity, "hatching_killing_percent": hatching_killing_percent, "out_province_poultry_request_amount": out_province_poultry_request_amount, "total_pure_internal_province_carcasses_amount": total_pure_internal_province_carcasses_amount, "total_pure_external_province_carcasses_amount": total_pure_external_province_carcasses_amount, "total_wage_amount": total_wage_amount, "number_of_city_deposit": len(city_operator_deposit), "city_deposit": city_deposit, "total_remain_wage_amount": total_remain_wage_amount, } return result