import datetime from datetime import timedelta from django.db.models import Sum, Q, F, Count from rest_framework import serializers # import panel.KillHouse.serializers from authentication.models import SystemUserProfile from authentication.serializer.serializer import SystemUserProfileSerializer, BankCardSerializer, \ SystemUserProfileForAutoAllocationSerializer, SystemUserProfileForGuildSerializer, \ SystemUserProfileForPoultryLocSerializer, SystemUserProfileForInspectionSerializer from authentication.serializers import UserProfileSerializer, SystemAddressSerializer from deposit_id import wage_counting_type, new_out_selling_count_wage, before_out_buying_count_wage_amount from deposit_percent import wage_percent, carcases_sell from panel.KillHouse.helpers import get_finance_info, get_kill_house_finance_info, get_difference_carcasses_weight, \ get_difference_carcasses_weight_kill, market_kill_request_share_quantity from panel.models import \ KillHouse, \ KillHouseRequest, \ KillRequest, \ KillHouseCheckRequest, KillHouseAssignmentInformation, PoultryAssignmentInformation, \ ProvinceCheckOperatorRequest, KillHouseADDCAR, Vet, VetCheckRequest, ProvinceKillRequest, KillHouseRequestExchange, \ KillHouseRequestExchangeReserve, KillHouseRequestExchangeAddCar, PoultryRequestExchangeAccept, KillHouseDriver, \ KillHouseOperator, KillHouseRequestAction, KillHouseRequestActionWinner, DriverRequestCancel, KillHouseVet, \ KillHouseComplaint, CheckKillHouseComplaint, CheckUnusualCasualties, KillHouseAssignmentImages, KillRequestFactor, \ KillRequestFactorPayment, AutomaticKillRequest, VetFarm, KillHouseCreditors, KillHouseAllowVet, Poultry, \ ProvinceAllowKillHouseRegisterCar, KillHousePercentage, KillHouseDailyQuota, KillHouseFactorToProvince, \ KillHouseWareHouse, KillHouseFreeBarInformation, PoultryRequest, ShareOfAllocation, \ ProvinceAllowKillHouseDirectBuying, Pricing, KillHousePurchaseRequest, StewardAllocation, ProvinceOperator, \ CityOperatorCheckRequest, CityOperator, StewardWareHouse, Steward, PoultryHatching, KillHouseFreeSaleBarInformation, \ PaymentGatewayPercentage, SlaughterHouseTransaction, InternalTransaction, BarDocumentStatus, \ KillHouseStewardGuildRelation, OutProvinceCarcassesBuyer, TypeActivity, WageType, RolesProducts, ColdHouse, \ BarDifferenceRequest, PercentageOfWageType, PoultryRequestQuarantineCode, DirectBuyingPayment, PosSegmentation, \ ProductsTransactions, WarehouseArchive from panel.poultry.serializers import PoultryRequestExchangeSerializer, PoultryRequestSerializer, \ PoultryRequestForBarMangementSerializer, PoultrySerializer, PoultryForBarManagementSerializer, \ PoultryForPoultryRequestLetterSerializer, OutProvincePoultryRequestBuyerSerializer, PoultryHatchingSerializer, \ PoultryHatchingForBarDifferenceRequestSerializer, PoultryForPredictionSerializer, PoultryRequestForMarketSerializer, \ PoultryHatchingForLocSerializer class BarDocumentStatusSerializer(serializers.ModelSerializer): class Meta: model = BarDocumentStatus fields = ['id', 'key', 'title', 'sms', 'is_error', 'priority_id'] class KillHouseOperatorForAutoAllocationsSerializer(serializers.ModelSerializer): user = SystemUserProfileForAutoAllocationSerializer(read_only=True) class Meta: model = KillHouseOperator fields = ['user'] class KillHouseOperatorForColdHouseSerializer(serializers.ModelSerializer): user = SystemUserProfileForInspectionSerializer(read_only=True) class Meta: model = KillHouseOperator fields = ['user'] # سریالایزر مربوط به اپراتور شهرستان class KillHouseOperatorSerializer(serializers.ModelSerializer): user = SystemUserProfileSerializer(read_only=True) address = SystemAddressSerializer(read_only=True) user_bank_info = BankCardSerializer(required=False) class Meta: model = KillHouseOperator fields = '__all__' class KillHouseOperatorForKillHouseDriverSerializer(serializers.ModelSerializer): user = SystemUserProfileForAutoAllocationSerializer(read_only=True) class Meta: model = KillHouseOperator fields = ['user'] class KillHouseOperatorForAllUserSerializer(serializers.ModelSerializer): user_bank_info = BankCardSerializer(required=False) class Meta: model = KillHouseOperator fields = ['user_bank_info'] class KillHouseForKillHouseDriverSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForKillHouseDriverSerializer(read_only=True) class Meta: model = KillHouse fields = ['kill_house_operator', 'killer'] class VetForKillHouseVetSerializer(serializers.ModelSerializer): user = SystemUserProfileForAutoAllocationSerializer(read_only=True) class Meta: model = Vet fields = ['user'] class KillHouseVetForKillHouseSerializer(serializers.ModelSerializer): vet = VetForKillHouseVetSerializer(read_only=True) class Meta: model = KillHouseVet fields = ['vet'] class KillHouseForKillHouseVetKillHouseSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForKillHouseDriverSerializer(read_only=True) class Meta: model = KillHouse fields = ['kill_house_operator', 'name'] class KillHouseForAllUserSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForAllUserSerializer(read_only=True) system_address = SystemAddressSerializer(read_only=True) killers = serializers.SerializerMethodField('get_killers') kill_house_vet = serializers.SerializerMethodField('get_kill_house_vet') class Meta: model = KillHouse exclude = ( 'id', 'trash', 'created_by', 'modified_by', 'address', 'car', 'create_date', 'modify_date', 'killing_race', 'phone', 'shift_work_from', 'shift_work_to', 'user', 'userprofile', ) def get_killers(self, obj): killers = KillHousePercentage.objects.filter(kill_house_for_killer__exact=obj) return KillHousePercentageForKillerKillHousseSerializer(killers, many=True).data def get_kill_house_vet(self, obj): kill_house_vet = KillHouseVet.objects.filter(kill_house__exact=obj, trash=False) return KillHouseVetForKillHouseSerializer(kill_house_vet, many=True).data class KillHouseForAutoAllocationSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) class Meta: model = KillHouse fields = ['kill_house_operator', 'name', 'killer', 'key', 'maximum_load_volume_increase', 'maximum_load_volume_reduction'] class KillHouseForKillHouseGuildRelationSerializer(serializers.ModelSerializer): # kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) city = serializers.CharField(source='kill_house_operator.user.city.name', read_only=True) fullname = serializers.CharField(source='kill_house_operator.user.fullname', read_only=True) mobile = serializers.CharField(source='kill_house_operator.user.mobile', read_only=True) allocation_limit = serializers.SerializerMethodField('get_allocation_limit') class Meta: model = KillHouse fields = ['name', 'city', 'fullname', 'mobile', 'killer', 'key', 'allocation_limit'] def get_allocation_limit(self, obj): guild = self.context.get('guild') if guild: relation = KillHouseStewardGuildRelation.objects.filter(kill_house=obj, guild=guild).first() if relation: return { "allocation_limit": relation.allocation_limit, "allocation_type": relation.allocation_type } return None class KillHouseForKillHouseStewardRelationSerializer(serializers.ModelSerializer): # kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) city = serializers.CharField(source='kill_house_operator.user.city.name', read_only=True) fullname = serializers.CharField(source='kill_house_operator.user.fullname', read_only=True) mobile = serializers.CharField(source='kill_house_operator.user.mobile', read_only=True) allocation_limit = serializers.SerializerMethodField('get_allocation_limit') class Meta: model = KillHouse fields = ['name', 'city', 'fullname', 'mobile', 'killer', 'key', 'allocation_limit'] def get_allocation_limit(self, obj): guild = self.context.get('guild') if guild: relation = KillHouseStewardGuildRelation.objects.filter(kill_house=obj, steward=guild).first() if relation: return { "allocation_limit": relation.allocation_limit, "allocation_type": relation.allocation_type } return None class KillHouseForCommonlyUsedSerializer(serializers.ModelSerializer): city = serializers.CharField(source='kill_house_operator.user.city.name', read_only=True) fullname = serializers.CharField(source='kill_house_operator.user.fullname', read_only=True) mobile = serializers.CharField(source='kill_house_operator.user.mobile', read_only=True) class Meta: model = KillHouse fields = ['name', 'city', 'fullname', 'mobile', 'killer', 'key', 'market_capacity_percent', 'market_light_capacity', 'market_light_capacity_percent', 'market_buying', 'market_buying_limitation', 'total_kill_capacity', 'total_kill_capacity_percent', 'max_kill_limit', 'market_buying_capacity_percent_status', 'governmental_selling_permission', 'free_selling_permission', 'in_province_governmental_selling_percent', 'out_province_governmental_selling_percent', 'segmentation_governmental_percent', 'in_province_free_selling_percent', 'out_province_free_selling_percent', 'segmentation_free_selling_percent', 'cold_house_governmental_percent', 'cold_house_free_percent', 'free_sale_form_governmental_quota', 'out_province_free_buying_commitment_percent', 'free_sale_from_free_quota_in_province', ] class KillHouseForReturnBuyingSerializer(serializers.ModelSerializer): city = serializers.CharField(source='kill_house_operator.user.city.name', read_only=True) fullname = serializers.CharField(source='kill_house_operator.user.fullname', read_only=True) mobile = serializers.CharField(source='kill_house_operator.user.mobile', read_only=True) class Meta: model = KillHouse fields = ['name', 'city', 'fullname', 'mobile', 'killer', 'key'] class KillHouseForColdHouseAllocationSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'name', 'info'] def get_info(self, obj): date1 = self.context.get('date1') date2 = self.context.get('date2') if date1: cold_house_allocations = StewardAllocation.objects.filter( kill_house=obj, date__date__gte=date1, date__date__lte=date2, to_cold_house__isnull=False, receiver_state__in=('accepted', 'pending'), trash=False) else: cold_house_allocations = StewardAllocation.objects.filter( kill_house=obj, to_cold_house__isnull=False, receiver_state__in=('accepted', 'pending'), trash=False) self_allocations = cold_house_allocations.filter(kill_house=obj, to_cold_house__kill_house=obj) other_allocations = cold_house_allocations.filter(kill_house=obj).exclude(to_cold_house__kill_house=obj) cold_house_allocations_weight = \ cold_house_allocations.aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 self_allocations_weight = \ self_allocations.aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 other_allocations_weight = \ other_allocations.aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 result = { "total_allocations_count": cold_house_allocations.count(), "total_allocations_weight": cold_house_allocations_weight, "total_self_allocations_count": self_allocations.count(), "total_self_allocations_weight": self_allocations_weight, "total_other_allocations_count": other_allocations.count(), "total_other_allocations_weight": other_allocations_weight, } return result class KillHouseForSegmentationSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'name', 'info'] def get_info(self, obj): date1 = self.context.get('date1') date2 = self.context.get('date2') if date1: segmentations = PosSegmentation.objects.filter(kill_house=obj, trash=False, date__date__gte=date1, date__date__lte=date2) else: segmentations = PosSegmentation.objects.filter(kill_house=obj, trash=False) segmentations_aggregates = segmentations.aggregate( total_count=Count('id'), total_self_count=Count('id', filter=Q(to_guild__isnull=True)), total_other_count=Count('id', filter=Q(to_guild__isnull=False)), total_weight=Sum('weight'), total_self_weight=Sum('weight', filter=Q(to_guild__isnull=True)), total_other_weight=Sum('weight', filter=Q(to_guild__isnull=False)), ) result = { "total_count": segmentations_aggregates['total_count'] or 0, "total_weight": segmentations_aggregates['total_weight'] or 0, "total_self_count": segmentations_aggregates['total_self_count'] or 0, "total_self_weight": segmentations_aggregates['total_self_weight'] or 0, "total_other_count": segmentations_aggregates['total_other_count'] or 0, "total_other_weight": segmentations_aggregates['total_other_weight'] or 0 } return result # class TotalKillHouseRemainWeightViewSetSerializer(serializers.ModelSerializer): # info = serializers.SerializerMethodField('get_info') # # class Meta: # model = KillHouse # fields = ['key', 'name', 'info'] # # def get_info(self, obj): # product = RolesProducts.objects.filter(kill_house=obj, trash=False, name='مرغ گرم').first() # archives = WarehouseArchive.objects.filter(kill_house=obj, trash=False,warehouse=True) # today = datetime.datetime.now().date() # yesterday = today - timedelta(days=1) # two_days_ago = yesterday - timedelta(days=1) # date_list=[two_days_ago,yesterday,two_days_ago] # # all_dates = set() # # all_dates.update( # KillHouseRequest.objects.filter( # input_warehouse=obj, # ware_house_confirmation=True, trash=False, calculate_status=True, warehouse=True # ).values_list('kill_request__recive_date__date', flat=True) # ) # # all_dates.update( # KillHouseFreeBarInformation.objects.filter( # input_warehouse=obj, trash=False, calculate_status=True, warehouse=True # ).exclude(entered_message='ورود به انبار مجازی') # .values_list('date__date', flat=True) # ) # # all_dates.update( # StewardAllocation.objects.filter( # kill_house=obj, trash=False, calculate_status=True, warehouse=True, # receiver_state__in=('pending', 'accepted') # ).values_list('production_date__date', flat=True) # ) # # all_dates.update( # KillHouseFreeSaleBarInformation.objects.filter( # kill_house=obj, trash=False, calculate_status=True, warehouse=True # ).values_list('production_date__date', flat=True) # ) # # all_dates.update( # PosSegmentation.objects.filter( # kill_house=obj, trash=False, warehouse=True # ).values_list('production_date__date', flat=True) # ) # # all_dates.update( # ProductsTransactions.objects.filter( # product=product, transaction__paid=True, trash=False, warehouse=True # ).values_list('transaction__date__date', flat=True) # ) # # days = sorted([d for d in all_dates if d is not None]) # # if not days: # return None # # kill_house_requests = KillHouseRequest.objects.filter( # input_warehouse=obj, # ware_house_confirmation=True, trash=False, calculate_status=True, warehouse=True # ) # # kill_house_free_buying_bars = KillHouseFreeBarInformation.objects.filter( # input_warehouse=obj, trash=False, calculate_status=True, warehouse=True # ).exclude(entered_message='ورود به انبار مجازی') # # kill_house_allocations = StewardAllocation.objects.filter( # kill_house=obj, trash=False, calculate_status=True, warehouse=True, # receiver_state__in=('pending', 'accepted') # ) # # kill_house_free_sale_bars = KillHouseFreeSaleBarInformation.objects.filter( # kill_house=obj, trash=False, calculate_status=True, warehouse=True # ) # # segmentations = PosSegmentation.objects.filter( # kill_house=obj, trash=False, warehouse=True # ) # # transactions = ProductsTransactions.objects.filter( # product=product, transaction__paid=True, trash=False, warehouse=True # ) # # governmental_list = [] # free_list = [] # # for day in days: # kill_house_requests_info = kill_house_requests.aggregate( # total_kill_house_request_governmental_weight=Sum( # 'ware_house_accepted_real_weight', # filter=Q( # province_request__poultry_request__free_sale_in_province=False, # kill_request__recive_date__date=day # ) # ), # total_kill_house_request_free_weight=Sum( # 'ware_house_accepted_real_weight', # filter=Q( # province_request__poultry_request__free_sale_in_province=True, # kill_request__recive_date__date=day # ) # ), # ) # # kill_house_free_buying_bars_info = kill_house_free_buying_bars.aggregate( # total_kill_house_free_buying_bars_weight=Sum('weight_of_carcasses', filter=Q(date__date=day)) # ) # # kill_house_allocations_info = kill_house_allocations.aggregate( # total_kill_house_allocations_governmental_weight=Sum( # 'real_weight_of_carcasses', filter=Q(quota='governmental', production_date__date=day) # ), # total_kill_house_allocations_free_weight=Sum( # 'real_weight_of_carcasses', filter=Q(quota='free', production_date__date=day) # ), # ) # # kill_house_free_sale_bars_info = kill_house_free_sale_bars.aggregate( # total_kill_house_free_sale_bars_governmental_weight=Sum( # 'real_weight_of_carcasses', filter=Q(quota='governmental', production_date__date=day) # ), # total_kill_house_free_sale_bars_free_weight=Sum( # 'real_weight_of_carcasses', filter=Q(quota='free', production_date__date=day) # ), # ) # # segmentations_info = segmentations.aggregate( # segmentations_governmental_weight=Sum('weight', # filter=Q(quota='governmental', production_date__date=day)), # segmentations_free_weight=Sum('weight', filter=Q(quota='free', production_date__date=day)), # ) # # pos_allocated_weight_info = transactions.aggregate( # pos_governmental_allocated_weight=Sum('cur_weight', # filter=Q(price_approved=True, transaction__date__date=day)), # pos_free_allocated_weight=Sum('cur_weight', # filter=Q(price_approved=False, transaction__date__date=day)), # ) # archives_info = archives.aggregate( # total_archive_governmental_weight=Sum('weight', filter=Q(date__date=day, quota='governmental')), # total_archive_free_weight=Sum('weight', filter=Q(date__date=day, quota='free')), # ) # # total_kill_house_request_governmental_weight = kill_house_requests_info[ # 'total_kill_house_request_governmental_weight'] or 0 # total_kill_house_request_free_weight = kill_house_requests_info['total_kill_house_request_free_weight'] or 0 # total_kill_house_free_buying_bars_weight = kill_house_free_buying_bars_info[ # 'total_kill_house_free_buying_bars_weight'] or 0 # total_kill_house_allocations_governmental_weight = kill_house_allocations_info[ # 'total_kill_house_allocations_governmental_weight'] or 0 # total_kill_house_allocations_free_weight = kill_house_allocations_info[ # 'total_kill_house_allocations_free_weight'] or 0 # total_kill_house_free_sale_bars_governmental_weight = kill_house_free_sale_bars_info[ # 'total_kill_house_free_sale_bars_governmental_weight'] or 0 # total_kill_house_free_sale_bars_free_weight = kill_house_free_sale_bars_info[ # 'total_kill_house_free_sale_bars_free_weight'] or 0 # segmentations_governmental_weight = segmentations_info['segmentations_governmental_weight'] or 0 # segmentations_free_weight = segmentations_info['segmentations_free_weight'] or 0 # pos_governmental_allocated_weight = pos_allocated_weight_info['pos_governmental_allocated_weight'] or 0 # pos_free_allocated_weight = pos_allocated_weight_info['pos_free_allocated_weight'] or 0 # total_archive_governmental_weight = archives_info['total_archive_governmental_weight'] or 0 # total_archive_free_weight = archives_info['total_archive_free_weight'] or 0 # # total_governmental_input = total_kill_house_request_governmental_weight # total_free_input = total_kill_house_request_free_weight + total_kill_house_free_buying_bars_weight # total_governmental_output = ( # total_kill_house_allocations_governmental_weight + # total_kill_house_free_sale_bars_governmental_weight + # segmentations_governmental_weight + # int(pos_governmental_allocated_weight / 1000) # ) # total_free_output = ( # total_kill_house_allocations_free_weight + # total_kill_house_free_sale_bars_free_weight + # segmentations_free_weight + # int(pos_free_allocated_weight / 1000) # ) # governmental_amount = ( # total_governmental_input - total_governmental_output) - total_archive_governmental_weight # free_amount = (total_free_input - total_free_output) - total_archive_free_weight # governmental_data = { # 'day': day, # 'amount': governmental_amount, # } # free_data = { # 'day': day, # 'amount': free_amount, # } # if day not in date_list: # # if governmental_amount > 0: # governmental_list.append(governmental_data) # if free_amount > 0: # free_list.append(free_data) # # final_dict = { # 'governmental': governmental_list, # 'free': free_list # } # return final_dict class TotalKillHouseRemainWeightViewSetSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'name', 'info'] def get_info(self, obj): product = RolesProducts.objects.filter(kill_house=obj, trash=False, name='مرغ گرم').first() archives = WarehouseArchive.objects.filter(kill_house=obj, trash=False,warehouse=True) today = datetime.datetime.now().date() yesterday = today - timedelta(days=1) two_days_ago = yesterday - timedelta(days=1) date_list=[two_days_ago,yesterday,two_days_ago] all_dates = set() all_dates.update( KillHouseRequest.objects.filter( input_warehouse=obj, ware_house_confirmation=True, trash=False, calculate_status=True, warehouse=True ).values_list('kill_request__recive_date__date', flat=True) ) all_dates.update( KillHouseFreeBarInformation.objects.filter( input_warehouse=obj, trash=False, calculate_status=True, warehouse=True ).exclude(entered_message='ورود به انبار مجازی') .values_list('date__date', flat=True) ) all_dates.update( StewardAllocation.objects.filter( kill_house=obj, trash=False, calculate_status=True, warehouse=True, receiver_state__in=('pending', 'accepted') ).values_list('production_date__date', flat=True) ) all_dates.update( KillHouseFreeSaleBarInformation.objects.filter( kill_house=obj, trash=False, calculate_status=True, warehouse=True ).values_list('production_date__date', flat=True) ) all_dates.update( PosSegmentation.objects.filter( kill_house=obj, trash=False, warehouse=True ).values_list('production_date__date', flat=True) ) all_dates.update( ProductsTransactions.objects.filter( product=product, transaction__paid=True, trash=False, warehouse=True ).values_list('transaction__date__date', flat=True) ) days = sorted([d for d in all_dates if d is not None]) if not days: return None kill_house_requests = KillHouseRequest.objects.filter( input_warehouse=obj, ware_house_confirmation=True, trash=False, calculate_status=True, warehouse=True ) kill_house_free_buying_bars = KillHouseFreeBarInformation.objects.filter( input_warehouse=obj, trash=False, calculate_status=True, warehouse=True ).exclude(entered_message='ورود به انبار مجازی') kill_house_allocations = StewardAllocation.objects.filter( kill_house=obj, trash=False, calculate_status=True, warehouse=True, receiver_state__in=('pending', 'accepted') ) kill_house_free_sale_bars = KillHouseFreeSaleBarInformation.objects.filter( kill_house=obj, trash=False, calculate_status=True, warehouse=True ) segmentations = PosSegmentation.objects.filter( kill_house=obj, trash=False, warehouse=True ) transactions = ProductsTransactions.objects.filter( product=product, transaction__paid=True, trash=False, warehouse=True ) governmental_list = [] free_list = [] for day in days: kill_house_requests_info = kill_house_requests.aggregate( total_kill_house_request_governmental_weight=Sum( 'ware_house_accepted_real_weight', filter=Q( province_request__poultry_request__free_sale_in_province=False, kill_request__recive_date__date=day ) ), total_kill_house_request_free_weight=Sum( 'ware_house_accepted_real_weight', filter=Q( province_request__poultry_request__free_sale_in_province=True, kill_request__recive_date__date=day ) ), ) kill_house_free_buying_bars_info = kill_house_free_buying_bars.aggregate( total_kill_house_free_buying_bars_weight=Sum('weight_of_carcasses', filter=Q(date__date=day)) ) kill_house_allocations_info = kill_house_allocations.aggregate( total_kill_house_allocations_governmental_weight=Sum( 'real_weight_of_carcasses', filter=Q(quota='governmental', production_date__date=day) ), total_kill_house_allocations_free_weight=Sum( 'real_weight_of_carcasses', filter=Q(quota='free', production_date__date=day) ), ) kill_house_free_sale_bars_info = kill_house_free_sale_bars.aggregate( total_kill_house_free_sale_bars_governmental_weight=Sum( 'real_weight_of_carcasses', filter=Q(quota='governmental', production_date__date=day) ), total_kill_house_free_sale_bars_free_weight=Sum( 'real_weight_of_carcasses', filter=Q(quota='free', production_date__date=day) ), ) segmentations_info = segmentations.aggregate( segmentations_governmental_weight=Sum('weight', filter=Q(quota='governmental', production_date__date=day)), segmentations_free_weight=Sum('weight', filter=Q(quota='free', production_date__date=day)), ) pos_allocated_weight_info = transactions.aggregate( pos_governmental_allocated_weight=Sum('cur_weight', filter=Q(price_approved=True, transaction__date__date=day)), pos_free_allocated_weight=Sum('cur_weight', filter=Q(price_approved=False, transaction__date__date=day)), ) archives_info = archives.aggregate( total_archive_governmental_weight=Sum('weight', filter=Q(date__date=day, quota='governmental')), total_archive_free_weight=Sum('weight', filter=Q(date__date=day, quota='free')), ) total_kill_house_request_governmental_weight = kill_house_requests_info[ 'total_kill_house_request_governmental_weight'] or 0 total_kill_house_request_free_weight = kill_house_requests_info['total_kill_house_request_free_weight'] or 0 total_kill_house_free_buying_bars_weight = kill_house_free_buying_bars_info[ 'total_kill_house_free_buying_bars_weight'] or 0 total_kill_house_allocations_governmental_weight = kill_house_allocations_info[ 'total_kill_house_allocations_governmental_weight'] or 0 total_kill_house_allocations_free_weight = kill_house_allocations_info[ 'total_kill_house_allocations_free_weight'] or 0 total_kill_house_free_sale_bars_governmental_weight = kill_house_free_sale_bars_info[ 'total_kill_house_free_sale_bars_governmental_weight'] or 0 total_kill_house_free_sale_bars_free_weight = kill_house_free_sale_bars_info[ 'total_kill_house_free_sale_bars_free_weight'] or 0 segmentations_governmental_weight = segmentations_info['segmentations_governmental_weight'] or 0 segmentations_free_weight = segmentations_info['segmentations_free_weight'] or 0 pos_governmental_allocated_weight = pos_allocated_weight_info['pos_governmental_allocated_weight'] or 0 pos_free_allocated_weight = pos_allocated_weight_info['pos_free_allocated_weight'] or 0 total_archive_governmental_weight = archives_info['total_archive_governmental_weight'] or 0 total_archive_free_weight = archives_info['total_archive_free_weight'] or 0 total_governmental_input = total_kill_house_request_governmental_weight total_free_input = total_kill_house_request_free_weight + total_kill_house_free_buying_bars_weight total_governmental_output = ( total_kill_house_allocations_governmental_weight + total_kill_house_free_sale_bars_governmental_weight + segmentations_governmental_weight + int(pos_governmental_allocated_weight / 1000) ) total_free_output = ( total_kill_house_allocations_free_weight + total_kill_house_free_sale_bars_free_weight + segmentations_free_weight + int(pos_free_allocated_weight / 1000) ) governmental_amount = ( total_governmental_input - total_governmental_output) - total_archive_governmental_weight free_amount = (total_free_input - total_free_output) - total_archive_free_weight governmental_data = { 'day': day, 'amount': governmental_amount, } free_data = { 'day': day, 'amount': free_amount, } if day not in date_list: if governmental_amount > 0: governmental_list.append(governmental_data) if free_amount > 0: free_list.append(free_data) final_dict = { 'governmental': governmental_list, 'free': free_list } return final_dict class TotalKillHouseWarehouseArchiveDashboardSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'name', 'info'] def get_info(self, obj): date1 = self.context.get('date1') date2 = self.context.get('date2') if date1: archives = WarehouseArchive.objects.filter(trash=False, kill_house=obj, create_date__date__gte=date1, create_date__date__lte=date2) else: archives = WarehouseArchive.objects.filter(trash=False, kill_house=obj) archives_info = archives.aggregate( total_count=Count('id'), total_governmental_count=Count('id', filter=Q(quota='governmental')), total_free_count=Count('id', filter=Q(quota='free')), total_weight=Sum('weight'), total_governmental_weight=Sum('weight', filter=Q(quota='governmental')), total_free_weight=Sum('weight', filter=Q(quota='free')), ) total_count = archives_info['total_count'] or 0 total_governmental_count = archives_info['total_governmental_count'] or 0 total_free_count = archives_info['total_free_count'] or 0 total_weight = archives_info['total_weight'] or 0 total_governmental_weight = archives_info['total_governmental_weight'] or 0 total_free_weight = archives_info['total_free_weight'] or 0 result = { 'total_count': total_count, 'total_governmental_count': total_governmental_count, 'total_free_count': total_free_count, 'total_weight': total_weight, 'total_governmental_weight': total_governmental_weight, 'total_free_weight': total_free_weight, } return result class KillHouseComparativeInformationSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_ware_house_info') class Meta: model = KillHouse fields = ['key', 'name', 'info'] def _calculate_info(self, obj, date1=None, date2=None): product = RolesProducts.objects.filter(kill_house=obj, trash=False, name='مرغ گرم').first() if date1: bars_date1 = date1 bars_date2 = date2 # bars_date1 = date1 - timedelta(days=1) # bars_date2 = date2 - timedelta(days=1) province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), trash=False, temporary_trash=False, temporary_deleted=False, kill_request__recive_date__date__gte=bars_date1, kill_request__recive_date__date__lte=bars_date2) kill_house_requests = KillHouseRequest.objects.filter( killhouse_user=obj, non_receipt=False, trash=False, calculate_status=True, kill_request__recive_date__date__gte=bars_date1, kill_request__recive_date__date__lte=bars_date2 ) kill_house_free_bar_informations = KillHouseFreeBarInformation.objects.filter(Q(date__date__gte=date1, date__date__lte=date2, buy_type='carcass') | Q( create_date__date__gte=date1, create_date__date__lte=date2, buy_type='live'), kill_house=obj, trash=False, temporary_trash=False, temporary_deleted=False, calculate_status=True) kill_house_free_Sale_bar_informations = KillHouseFreeSaleBarInformation.objects.filter(kill_house=obj, date__date__gte=date1, date__date__lte=date2, trash=False, temporary_trash=False, temporary_deleted=False, calculate_status=True) kill_house_allocations = StewardAllocation.objects.filter(kill_house=obj, trash=False, receiver_state__in=('pending', 'accepted'), temporary_trash=False, temporary_deleted=False, to_cold_house__isnull=True, calculate_status=True, date__date__gte=date1, date__date__lte=date2) segmentations = PosSegmentation.objects.filter(kill_house=obj, trash=False, date__date__gte=date1, date__date__lte=date2) cold_house_allocations = StewardAllocation.objects.filter( kill_house=obj, date__date__gte=date1, date__date__lte=date2, to_cold_house__isnull=False, receiver_state__in=('accepted', 'pending'), trash=False) transactions = ProductsTransactions.objects.filter(product=product, transaction__paid=True, transaction__date__date__gte=date1, transaction__date__date__lte=date2, trash=False, warehouse=True) archives = WarehouseArchive.objects.filter(kill_house=obj,date__date__gte=date1,date__date__lte=date2, trash=False) else: province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), trash=False, temporary_trash=False, temporary_deleted=False) kill_house_requests = KillHouseRequest.objects.filter(killhouse_user=obj, trash=False, non_receipt=False, calculate_status=True) kill_house_free_bar_informations = KillHouseFreeBarInformation.objects.filter(kill_house=obj, trash=False, temporary_trash=False, temporary_deleted=False, calculate_status=True) kill_house_free_Sale_bar_informations = KillHouseFreeSaleBarInformation.objects.filter(kill_house=obj, trash=False, temporary_trash=False, temporary_deleted=False, calculate_status=True) kill_house_allocations = StewardAllocation.objects.filter(kill_house=obj, trash=False, receiver_state__in=('pending', 'accepted'), temporary_trash=False, temporary_deleted=False, to_cold_house__isnull=True, calculate_status=True) segmentations = PosSegmentation.objects.filter(kill_house=obj, trash=False) cold_house_allocations = StewardAllocation.objects.filter( kill_house=obj, to_cold_house__isnull=False, receiver_state__in=('accepted', 'pending'), trash=False) transactions = ProductsTransactions.objects.filter(product=product, transaction__paid=True, trash=False, warehouse=True) archives = WarehouseArchive.objects.filter(kill_house=obj, trash=False) province_kill_request_info = province_kill_requests.aggregate( total_province_kill_request_count=Count('id'), total_province_kill_request_quantity=Sum('total_killed_quantity'), total_province_kill_request_weight=Sum('total_killed_weight'), ) kill_house_request_info = kill_house_requests.aggregate( total_kill_house_request_quantity=Sum('accepted_real_quantity'), total_kill_house_request_governmental_quantity=Sum('accepted_real_quantity', filter=Q( province_request__poultry_request__free_sale_in_province=False)), total_kill_house_request_free_quantity=Sum('accepted_real_quantity', filter=Q( province_request__poultry_request__free_sale_in_province=True)), total_kill_house_request_warehouse_entered_quantity=Sum('accepted_real_quantity', filter=Q(ware_house_confirmation=True)), total_kill_house_request_warehouse_entered_weight=Sum('accepted_real_weight', filter=Q(ware_house_confirmation=True)), total_kill_house_request_warehouse_cacasses_entered_weight=Sum('ware_house_accepted_real_weight', filter=Q(ware_house_confirmation=True)), total_kill_house_request_warehouse_entered_governmental_weight=Sum('ware_house_accepted_real_weight', filter=Q(ware_house_confirmation=True, province_request__poultry_request__free_sale_in_province=False)), total_kill_house_request_warehouse_entered_free_weight=Sum('ware_house_accepted_real_weight', filter=Q(ware_house_confirmation=True, province_request__poultry_request__free_sale_in_province=True)), ) kill_house_free_bar_info = kill_house_free_bar_informations.aggregate( total_kill_house_free_bar_carcass_total_weight=Sum('weight_of_carcasses'), total_kill_house_free_bar_live_total_quantity=Sum('quantity', filter=Q(buy_type='live')), total_kill_house_entered_free_bar_live_total_weight=Sum('live_weight', filter=Q(buy_type='live', ware_house=True)), total_kill_house_entered_free_bar_live_carcasses_total_weight=Sum('weight_of_carcasses', filter=Q(buy_type='live', ware_house=True)), total_kill_house_entered_free_bar_live_total_quantity=Sum('quantity', filter=Q(buy_type='live', ware_house=True)), ) kill_house_free_Sale_bar_info = kill_house_free_Sale_bar_informations.aggregate( total_kill_house_free_Sale_bar_weight=Sum('real_weight_of_carcasses'), total_kill_house_free_Sale_bar_governmental_weight=Sum('real_weight_of_carcasses', filter=Q(quota='governmental')), total_kill_house_free_Sale_bar_free_weight=Sum('real_weight_of_carcasses', filter=Q(quota='free')), ) kill_house_allocations_info = kill_house_allocations.aggregate( total_kill_house_allocations_weight=Sum('real_weight_of_carcasses'), total_kill_house_allocations_governmental_weight=Sum('real_weight_of_carcasses', filter=Q(quota='governmental')), total_kill_house_allocations_free_weight=Sum('real_weight_of_carcasses', filter=Q(quota='free')), ) segmentations_aggregates_info = segmentations.aggregate( total_segmentations_weight=Sum('weight'), total_segmentations_governmental_weight=Sum('weight', filter=Q(quota='governmental')), total_segmentations_free_weight=Sum('weight', filter=Q(quota='free')), ) cold_house_allocations_info = cold_house_allocations.aggregate( total_cold_house_allocations_weight=Sum('real_weight_of_carcasses'), total_cold_house_allocations_governmental_weight=Sum('real_weight_of_carcasses', filter=Q(quota='governmental')), total_cold_house_allocations_free_weight=Sum('real_weight_of_carcasses', filter=Q(quota='free')), ) pos_allocated_weight_info = transactions.aggregate( pos_allocated_weight=Sum('cur_weight'), pos_governmental_allocated_weight=Sum('cur_weight', filter=Q(price_approved=True)), pos_free_allocated_weight=Sum('cur_weight', filter=Q(price_approved=False)), ) archives_info = archives.aggregate( total_archive_governmental_weight=Sum('weight', filter=Q(quota='governmental')), total_archive_free_weight=Sum('weight', filter=Q(quota='free')), ) return {**province_kill_request_info, **kill_house_request_info, **kill_house_free_bar_info, **kill_house_free_Sale_bar_info, **kill_house_allocations_info, **segmentations_aggregates_info,**pos_allocated_weight_info, **cold_house_allocations_info,**archives_info} def get_ware_house_info(self, obj): product_remain_weight = RolesProducts.objects.filter(kill_house=obj, trash=False).only( 'total_remain_weight').first() product_remain_weight = product_remain_weight.total_remain_weight if product_remain_weight else 0 date1 = self.context.get('date1') date2 = self.context.get('date2') rsi_data = self.context.get('rsi_data') kill_house_rsi_info = next( (obj_info for obj_info in rsi_data if obj_info["PartIdCode"] == obj.unique_identifier), None) if date1 and date2: date1 = datetime.datetime.strptime(str(date1), '%Y-%m-%d').date() date2 = datetime.datetime.strptime(str(date2), '%Y-%m-%d').date() total_info = self._calculate_info(obj, date1, date2) if date1 else self._calculate_info(obj) total_province_kill_requests_quantity = total_info['total_province_kill_request_quantity'] or 0 total_kill_house_request_governmental_quantity = total_info[ 'total_kill_house_request_governmental_quantity'] or 0 total_kill_house_request_free_quantity = total_info['total_kill_house_request_free_quantity'] or 0 total_kill_house_free_bar_live_total_quantity = total_info['total_kill_house_free_bar_live_total_quantity'] or 0 total_live_bars_quantity = total_kill_house_request_governmental_quantity + total_kill_house_request_free_quantity + total_kill_house_free_bar_live_total_quantity total_kill_house_request_warehouse_entered_quantity = total_info[ 'total_kill_house_request_warehouse_entered_quantity'] or 0 total_kill_house_entered_free_bar_live_total_quantity = total_info[ 'total_kill_house_entered_free_bar_live_total_quantity'] or 0 total_entered_warehouse_bars_quantity = total_kill_house_request_warehouse_entered_quantity + total_kill_house_entered_free_bar_live_total_quantity total_kill_house_request_warehouse_entered_weight = total_info[ 'total_kill_house_request_warehouse_entered_weight'] or 0 total_kill_house_entered_free_bar_live_total_weight = total_info[ 'total_kill_house_entered_free_bar_live_total_weight'] or 0 total_entered_warehouse_bars_weight = total_kill_house_request_warehouse_entered_weight + total_kill_house_entered_free_bar_live_total_weight total_kill_house_request_warehouse_cacasses_entered_weight = total_info[ 'total_kill_house_request_warehouse_cacasses_entered_weight'] or 0 total_kill_house_entered_free_bar_live_carcasses_total_weight = total_info[ 'total_kill_house_entered_free_bar_live_carcasses_total_weight'] or 0 total_kill_house_free_bar_carcass_total_weight = total_info[ 'total_kill_house_free_bar_carcass_total_weight'] or 0 total_kill_house_request_warehouse_entered_governmental_weight = total_info[ 'total_kill_house_request_warehouse_entered_governmental_weight'] or 0 total_kill_house_request_warehouse_entered_free_weight = total_info[ 'total_kill_house_request_warehouse_entered_free_weight'] or 0 total_kill_house_warehouse_cacasses_entered_weight = total_kill_house_request_warehouse_cacasses_entered_weight + total_kill_house_free_bar_carcass_total_weight total_kill_house_warehouse_govermental_cacasses_entered_weight = total_kill_house_request_warehouse_entered_governmental_weight total_kill_house_warehouse_free_cacasses_entered_weight = total_kill_house_request_warehouse_entered_free_weight + total_kill_house_entered_free_bar_live_carcasses_total_weight total_kill_house_free_Sale_bar_weight = total_info['total_kill_house_free_Sale_bar_weight'] or 0 total_kill_house_free_Sale_bar_governmental_weight = total_info[ 'total_kill_house_free_Sale_bar_governmental_weight'] or 0 total_kill_house_free_Sale_bar_free_weight = total_info['total_kill_house_free_Sale_bar_free_weight'] or 0 total_kill_house_allocations_weight = total_info['total_kill_house_allocations_weight'] or 0 total_kill_house_allocations_governmental_weight = total_info[ 'total_kill_house_allocations_governmental_weight'] or 0 total_kill_house_allocations_free_weight = total_info['total_kill_house_allocations_free_weight'] or 0 total_segmentations_weight = total_info['total_segmentations_weight'] or 0 total_segmentations_governmental_weight = total_info['total_segmentations_governmental_weight'] or 0 total_segmentations_free_weight = total_info['total_segmentations_free_weight'] or 0 total_cold_house_allocations_weight = total_info['total_cold_house_allocations_weight'] or 0 total_cold_house_allocations_governmental_weight = total_info[ 'total_cold_house_allocations_governmental_weight'] or 0 total_cold_house_allocations_free_weight = total_info['total_cold_house_allocations_free_weight'] or 0 pos_allocated_weight = total_info['pos_allocated_weight'] or 0 pos_governmental_allocated_weight = total_info['pos_governmental_allocated_weight'] or 0 pos_free_allocated_weight = total_info['pos_free_allocated_weight'] or 0 total_archive_governmental_weight = total_info['total_archive_governmental_weight'] or 0 total_archive_free_weight = total_info['total_archive_free_weight'] or 0 total_input_weight = total_kill_house_warehouse_cacasses_entered_weight total_governmental_input_weight = total_kill_house_request_warehouse_entered_governmental_weight total_free_input_weight = total_kill_house_request_warehouse_entered_free_weight + total_kill_house_free_bar_carcass_total_weight total_output_weight = total_kill_house_free_Sale_bar_weight + total_kill_house_allocations_weight + total_segmentations_weight + total_cold_house_allocations_weight total_output_governmental_weight = total_kill_house_free_Sale_bar_governmental_weight + total_kill_house_allocations_governmental_weight + total_segmentations_governmental_weight + total_cold_house_allocations_governmental_weight + int(pos_governmental_allocated_weight / 1000) total_output_free_weight = total_kill_house_free_Sale_bar_free_weight + total_kill_house_allocations_free_weight + total_segmentations_free_weight + total_cold_house_allocations_free_weight + int(pos_free_allocated_weight / 1000) # total_remain_weight = total_input_weight - total_output_weight total_remain_governmental_weight = (total_governmental_input_weight - total_output_governmental_weight) - total_archive_governmental_weight if ( total_governmental_input_weight - total_output_governmental_weight) - total_archive_governmental_weight> 0 else 0 last_total_remain_governmental_weight = ( (total_governmental_input_weight - total_output_governmental_weight) - total_archive_governmental_weight) * -1 if ( total_governmental_input_weight - total_output_governmental_weight) - total_archive_governmental_weight < 0 else 0 total_remain_free_weight = (total_free_input_weight - total_output_free_weight) - total_archive_free_weight if ( total_free_input_weight - total_output_free_weight) - total_archive_free_weight > 0 else 0 last_total_remain_free_weight = ((total_free_input_weight - total_output_free_weight) - total_archive_free_weight) * -1 if ( total_free_input_weight - total_output_free_weight) - total_archive_free_weight < 0 else 0 total_remain_weight = total_remain_governmental_weight + total_remain_free_weight last_total_remain_weight = last_total_remain_governmental_weight + last_total_remain_free_weight return { "kill_house_rsi_info": kill_house_rsi_info, "total_killing_quantity": total_province_kill_requests_quantity + total_kill_house_free_bar_live_total_quantity, "total_kill_house_request_governmental_quantity": total_kill_house_request_governmental_quantity, "total_kill_house_request_free_quantity": total_kill_house_request_free_quantity, "total_kill_house_free_bar_live_total_quantity": total_kill_house_free_bar_live_total_quantity, "total_live_bars_quantity": total_live_bars_quantity, "total_entered_warehouse_bars_quantity": total_entered_warehouse_bars_quantity, "total_entered_warehouse_bars_weight": total_entered_warehouse_bars_weight, "total_kill_house_request_warehouse_entered_quantity": total_kill_house_request_warehouse_entered_quantity, "total_kill_house_request_warehouse_entered_weight": total_kill_house_request_warehouse_entered_weight, "total_kill_house_warehouse_cacasses_entered_weight": total_kill_house_warehouse_cacasses_entered_weight, "total_kill_house_warehouse_govermental_cacasses_entered_weight": total_kill_house_warehouse_govermental_cacasses_entered_weight, "total_kill_house_warehouse_free_cacasses_entered_weight": total_kill_house_warehouse_free_cacasses_entered_weight, "total_kill_house_request_warehouse_entered_governmental_weight": total_kill_house_request_warehouse_entered_governmental_weight, "total_kill_house_request_warehouse_entered_free_weight": total_kill_house_request_warehouse_entered_free_weight, "total_output_weight": total_output_weight, "total_kill_house_allocations_weight": total_kill_house_allocations_weight, "total_kill_house_free_Sale_bar_weight": total_kill_house_free_Sale_bar_weight, "total_other_output_weight": total_segmentations_weight + total_cold_house_allocations_weight, "total_remain_weight": total_remain_weight, "total_remain_governmental_weight": total_remain_governmental_weight, "total_remain_free_weight": total_remain_free_weight, "last_total_remain_governmental_weight": last_total_remain_governmental_weight, "last_total_remain_free_weight": last_total_remain_free_weight, "last_total_remain_weight": last_total_remain_weight, "pos_allocated_weight": int(pos_allocated_weight / 1000), "pos_governmental_allocated_weight": int(pos_governmental_allocated_weight / 1000), "pos_free_allocated_weight": int(pos_free_allocated_weight / 1000), "total_archive_governmental_weight": total_archive_governmental_weight, "total_archive_free_weight": total_archive_free_weight, } class KillHouseMarketInfoSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'name', 'info'] def get_info(self, obj): date1 = self.context.get('date1') date2 = self.context.get('date2') kill_houses = self.context.get('kill_houses') if date1: kill_house_info = market_kill_request_share_quantity(obj, date1, date2, total_kill_houses=kill_houses) else: kill_house_info = market_kill_request_share_quantity(obj, total_kill_houses=kill_houses) result = { "total_poultry_requests_quantity": kill_house_info['total_poultry_requests_quantity'], "kill_house_today_share": kill_house_info['kill_house_today_share'], "market_light_share": kill_house_info['market_light_share'], "kill_house_market_kill_requests_quantity": kill_house_info['kill_house_market_kill_requests_quantity'], "kill_house_market_kill_requests_quantity_first": kill_house_info[ 'kill_house_market_kill_requests_quantity_first'], "kill_house_market_kill_requests_quantity_final": kill_house_info[ 'kill_house_market_kill_requests_quantity_final'], "kill_house_today_left_share": kill_house_info['kill_house_today_left_share'], "kill_house_market_kill_requests_quantity_weight": kill_house_info[ 'kill_house_market_kill_requests_quantity_weight'], "kill_house_market_kill_requests_quantity_first_weight": kill_house_info[ 'kill_house_market_kill_requests_quantity_first_weight'], "kill_house_market_kill_requests_quantity_final_weight": kill_house_info[ 'kill_house_market_kill_requests_quantity_final_weight'], "total_kill_house_market_kill_requests_quantity_agreement_light_weight": kill_house_info[ 'total_kill_house_market_kill_requests_quantity_agreement_light_weight'], "total_kill_house_market_kill_requests_quantity_light_weight": kill_house_info[ 'total_kill_house_market_kill_requests_quantity_light_weight'], "total_kill_house_market_kill_requests_quantity_heavy_weight": kill_house_info[ 'total_kill_house_market_kill_requests_quantity_heavy_weight'], } return result class KillHouseForKillHousePercentageSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) system_address = SystemAddressSerializer(read_only=True) class Meta: model = KillHouse fields = ['key', 'system_address', 'kill_house_operator', 'name', 'active', 'killer', 'type'] class KillHouseForTotalReportAutomaticStewardAllocationSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) quantity_info = serializers.SerializerMethodField('get_quantity_info') class Meta: model = KillHouse fields = ['name', 'killer', 'kill_house_operator', 'quantity_info'] def get_quantity_info(self, obj): from django.db.models import F, Sum, DecimalField date = datetime.datetime.strptime(str(self.context.get('request').GET['date']), '%Y-%m-%d').date() province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, state__in=('pending', 'accepted'), kill_request__recive_date__date=date ).select_related( 'province_request__poultry_request') allocations = StewardAllocation.objects.filter(kill_house=obj, date__date=date) total_quantity = province_kill_requests.aggregate(total_quantity=Sum('main_quantity')).get( 'total_quantity', 0) total_allocations = allocations.aggregate(total_quantity=Sum('real_number_of_carcasses')).get( 'total_quantity', 0) if total_quantity == None: total_quantity = 0 if total_allocations == None: total_allocations = 0 if total_quantity == None: total_quantity = 0 if total_allocations == None: total_allocations = 0 remain_quantity = total_quantity - total_allocations return { "total_quantity": total_quantity, "total_allocations": total_allocations, "remain_quantity": remain_quantity } class KillHouseWageForTotalDashbordSerializer(serializers.ModelSerializer): wage_info = serializers.SerializerMethodField('get_wage_info') class Meta: model = KillHouse fields = ['key', 'name', 'wage_info'] def get_wage_info(self, obj): province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, state__in=('pending', 'accepted'), return_to_province=False, trash=False) unpaid_province_kill_requests = province_kill_requests.filter( wage_pay=False, archive_by_province=False, return_to_province=False, trash=False) paid_province_kill_requests = province_kill_requests.filter( wage_pay=True, archive_by_province=False, return_to_province=False, trash=False) archive_province_kill_requests = province_kill_requests.filter( wage_pay=False, archive_by_province=True, return_to_province=False, trash=False) return { "total_count": len(province_kill_requests), "paid_count": len(paid_province_kill_requests), "unpaid_count": len(unpaid_province_kill_requests), "archive_count": len(archive_province_kill_requests), "total_quantity": province_kill_requests.aggregate(total=Sum('total_killed_quantity'))['total'] or 0, "total_weight": province_kill_requests.aggregate(total=Sum('total_killed_weight'))['total'] or 0, "total_wage": province_kill_requests.aggregate(total=Sum(F('total_killed_weight') * F('wage')))[ 'total'], "total_paid_quantity": paid_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0, "total_weight_paid": paid_province_kill_requests.aggregate(total=Sum('total_killed_weight'))['total'] or 0, "total_paid_wage": paid_province_kill_requests.aggregate(total=Sum(F('total_killed_weight') * F('wage')))[ 'total'] or 0, "total_unpaid_quantity": unpaid_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0, "total_weight_unpaid": unpaid_province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0, "total_unpaid_wage": unpaid_province_kill_requests.aggregate(total=Sum(F('total_killed_weight') * F('wage')))[ 'total'], "total_archive_quantity": archive_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0, "total_weight_archive": archive_province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0, "total_archive_wage": archive_province_kill_requests.aggregate(total=Sum(F('total_killed_weight') * F('wage')))[ 'total'], } class KillHouseForAutomaticStewardAllocationSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) system_address = SystemAddressSerializer(read_only=True) province_kill_request_information = serializers.SerializerMethodField('get_province_kill_request_information') date = serializers.SerializerMethodField('get_date') class Meta: model = KillHouse fields = ['key', 'system_address', 'kill_house_operator', 'name', 'active', 'killer', 'province_kill_request_information', 'date' ] def get_province_kill_request_information(self, obj): from datetime import datetime, timedelta date = datetime.strptime(str(self.context.get('request').GET['date']), '%Y-%m-%d').date() ware_house = KillHouseWareHouse.objects.filter(kill_house=obj, date__date=date).first() if not ware_house: ware_house_date = datetime(year=date.year, month=date.month, day=date.day, hour=1, minute=12, second=20) ware_house = KillHouseWareHouse( kill_house=obj, date=ware_house_date ) ware_house.save() total_number_of_carcasses_temp = ware_house.update_total_number_of_carcasses + ware_house.pre_cold_number_of_carcasses_to_ware_house if ware_house.update_total_number_of_carcasses > 0 else ware_house.total_number_of_carcasses + ware_house.pre_cold_number_of_carcasses_to_ware_house total_weight_of_carcasses_temp = ware_house.update_total_weight_of_carcasses + ware_house.pre_cold_weight_of_carcasses_to_ware_house if ware_house.update_total_weight_of_carcasses > 0 else ware_house.total_weight_of_carcasses + ware_house.pre_cold_weight_of_carcasses_to_ware_house return { "total_number_of_carcasses": total_number_of_carcasses_temp, "total_weight_of_carcasses": total_weight_of_carcasses_temp, "allocated_total_number_of_carcasses": ware_house.allocated_total_number_of_carcasses, "pre_cold_number_of_carcasses_self_ware_house": ware_house.pre_cold_number_of_carcasses_self_ware_house, "pre_cold_weight_of_carcasses_self_ware_house": ware_house.pre_cold_weight_of_carcasses_self_ware_house, "allocated_total_weight_of_carcasses": ware_house.allocated_total_weight_of_carcasses, "remain_total_number_of_carcasses": total_number_of_carcasses_temp - ware_house.allocated_total_number_of_carcasses if ware_house.pre_cold_number_of_carcasses_self_ware_house == 0 else 0, "remain_total_weight_of_carcasses": total_weight_of_carcasses_temp - ware_house.allocated_total_weight_of_carcasses if ware_house.pre_cold_weight_of_carcasses_to_ware_house == 0 else 0, } def get_date(self, obj): return datetime.datetime.strptime(str(self.context.get('request').GET['date']), '%Y-%m-%d').date() class KillHousePurchaseRequestSerializer(serializers.ModelSerializer): kill_house = KillHouseForKillHousePercentageSerializer(read_only=True) class Meta: model = KillHousePurchaseRequest fields = '__all__' class KillHouseforPurchaseRequestSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) system_address = SystemAddressSerializer(read_only=True) allow_purchase_request = serializers.SerializerMethodField('get_allow_purchase_request') wage_info = serializers.SerializerMethodField('get_wage_info') kill_house_for_killer = serializers.SerializerMethodField('get_kill_house_for_killer') class Meta: model = KillHouse fields = ['key', 'max_kill_limit', 'extra_bar_kill_percent', 'total_kill_capacity', 'kill_house_operator', 'name', 'killer', 'system_address', 'allow_purchase_request', 'wage_info', 'type', 'killer', 'kill_house_for_killer', 'out_province_selling_limitation', 'out_province_selling_limitation_percent', 'in_province_selling_limitation', 'in_province_selling_limitation_percent', 'quota', 'free_quota', 'governmental_quota', 'quota_max_kill_limit', 'quota_request', 'quota_custom', 'quota_custom_quantity', 'maximum_load_volume_increase', 'maximum_load_volume_reduction', 'ware_house_remaining_percent_limitation', 'ware_house_remaining_percent_limitation_status', 'ware_house_remaining_weight_limitation', 'ware_house_remaining_weight_limitation_status','ware_house_remaining_weight_archive_percent'] def get_allow_purchase_request(self, obj): allow = True limitation = False total_limitation = False limitation_number = 0 purchase_request = KillHousePurchaseRequest.objects.filter(kill_house=obj).first() if purchase_request: allow = purchase_request.allow limitation = purchase_request.limitation limitation_number = purchase_request.limitation_number total_limitation = purchase_request.total_limitation return { "allow": allow, "limitation": limitation, "limitation_number": limitation_number, "total_limitation": total_limitation, } # def get_wage_info(self, obj): # total_unpaid_wage = 0 # total_paid_wage = 0 # kill_house_kill_requests_wage = 0 # real_free_sale_wage = 0 # # role = self.context.get('request').GET['role'] # total_unpaid_wage = get_finance_info(obj)['total_price'] # slaughter_transactions = InternalTransaction.objects.filter(Q(kill_house=obj) | Q(parent_kill_house=obj), # status='completed', # trash=False) # # total_paid_wage += slaughter_transactions.aggregate(total=Sum('amount'))[ # 'total'] or 0 # # return { # "user_token": 'k' + obj.kill_house_operator.user.user_gate_way_id, # "total_amount": total_unpaid_wage - (total_paid_wage + obj.off) # } def get_wage_info(self, obj): total_unpaid_wage = 0 total_paid_wage = 0 kill_house_kill_requests_wage = 0 real_free_sale_wage = 0 # role = self.context.get('request').GET['role'] total_unpaid_wage = get_finance_info(obj)['total_price'] if obj.killer and obj.type == 'public': slaughter_transactions = InternalTransaction.objects.filter(kill_house=obj,parent_kill_house=obj, status='completed', trash=False) else: slaughter_transactions = InternalTransaction.objects.filter(Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', trash=False) total_paid_wage += slaughter_transactions.aggregate(total=Sum('amount'))[ 'total'] or 0 return { "user_token": 'k' + obj.kill_house_operator.user.user_gate_way_id, "total_amount": total_unpaid_wage - (total_paid_wage + obj.off) } def get_kill_house_for_killer(self, obj): kill_house_for_killer_info = None kill_house_for_killer = KillHousePercentage.objects.filter(kill_house=obj, kill_house_for_killer__isnull=False, kill_house__killer=True).first() if kill_house_for_killer: kill_house_for_killer_info = { "kill_house_name": kill_house_for_killer.kill_house_for_killer.name, "fullname": kill_house_for_killer.kill_house_for_killer.kill_house_operator.user.fullname, "mobile": kill_house_for_killer.kill_house_for_killer.kill_house_operator.user.mobile, } return kill_house_for_killer_info class KillHouseForKillingInformationDiscrepancyReportSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) class Meta: model = KillHouse fields = ['key', 'kill_house_operator', 'name', 'killer'] class PosKillHouseForKillingInformationDiscrepancyReportSerializer(serializers.ModelSerializer): class Meta: model = KillHouse fields = ['key', 'name'] class KillHouseForProvinceSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForColdHouseSerializer(read_only=True) class Meta: model = KillHouse fields = ['key', 'name', 'killer', 'kill_house_operator'] class KillHouseForProvinceWageSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'info'] def get_info(self, obj): total_quantity = 0 total_weight = 0 total_wage = 0 role = self.context.get('request').GET['role'] if role in ['KillHouse', 'SuperAdmin', 'AdminX', 'Supporter']: wage_type = 'total_wage_amount' province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, state__in=('pending', 'accepted'), return_to_province=False, archive_wage=False, trash=False).select_related( 'province_request__poultry_request') elif role == 'ProvinceOperator': wage_type = 'union_share' province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, state__in=('pending', 'accepted'), union_share__gt=0, return_to_province=False, archive_wage=False, trash=False).select_related( 'province_request__poultry_request') elif role == 'Company': wage_type = 'company_share' province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, state__in=('pending', 'accepted'), company_share__gt=0, return_to_province=False, archive_wage=False, trash=False).select_related( 'province_request__poultry_request') elif role == 'Guilds': wage_type = 'guilds_share' province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, state__in=('pending', 'accepted'), guilds_share__gt=0, return_to_province=False, archive_wage=False, trash=False).select_related( 'province_request__poultry_request') else: wage_type = 'wallet_share' total_quantity += province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 total_weight += province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 total_wage += province_kill_requests.aggregate(total=Sum(wage_type))[ 'total'] or 0 return { "killer": obj.killer, "kill_house_fullname": obj.kill_house_operator.user.fullname, "kill_house_name": obj.name, "kill_house_mobile": obj.kill_house_operator.user.mobile, "kill_house_city": obj.kill_house_operator.user.city.name, "total_count": len(province_kill_requests), "total_quantity": total_quantity, "total_weight": total_weight, "total_wage": total_wage, } class KillHouseForKillHouseRequestWageSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'info'] def get_info(self, obj): total_quantity = 0 total_weight = 0 role = self.context.get('request').GET['role'] if role == 'ProvinceOperator': kill_house_requests = KillHouseRequest.objects.filter(Q(killhouse_user=obj) | Q(killer=obj), province_kill_request__union_share__gt=0, archive_wage=False, trash=False) elif role == 'Company': kill_house_requests = KillHouseRequest.objects.filter(Q(killhouse_user=obj) | Q(killer=obj), province_kill_request__company_share__gt=0, archive_wage=False, trash=False) elif role == 'Guilds': kill_house_requests = KillHouseRequest.objects.filter(Q(killhouse_user=obj) | Q(killer=obj), province_kill_request__guilds_share__gt=0, archive_wage=False, trash=False) else: kill_house_requests = KillHouseRequest.objects.filter(Q(killhouse_user=obj) | Q(killer=obj), archive_wage=False, trash=False) total_quantity += kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))[ 'total'] or 0 total_weight += kill_house_requests.aggregate(total=Sum('accepted_real_weight'))[ 'total'] or 0 return { "killer": obj.killer, "kill_house_fullname": obj.kill_house_operator.user.fullname, "kill_house_name": obj.name, "kill_house_mobile": obj.kill_house_operator.user.mobile, "kill_house_city": obj.kill_house_operator.user.city.name, "total_count": len(kill_house_requests), "total_quantity": total_quantity, "total_weight": total_weight, } class KillHouseForNewWageInormationSerializer(serializers.ModelSerializer): wage_info = serializers.SerializerMethodField('get_wage_info') shares = serializers.SerializerMethodField('get_shares') class Meta: model = KillHouse fields = ['key', 'name', 'wage_info', 'shares'] # def get_wage_info(self, obj): # date1 = None # date2 = None # if self.context.get('request').GET.get('date1'): # 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() # total_paid_wage = 0 # if date1: # # finance_info = get_kill_house_finance_info(obj,date1,date2) # finance_info = get_finance_info(obj, date1, date2) # slaughter_transactions = InternalTransaction.objects.filter( # Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', date__date__gte=date1, # date__date__lte=date2, # trash=False) # else: # # finance_info = get_kill_house_finance_info(obj) # finance_info = get_finance_info(obj) # slaughter_transactions = InternalTransaction.objects.filter( # Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', # trash=False) # # total_paid_wage += slaughter_transactions.aggregate(total=Sum('amount'))[ # 'total'] or 0 # # return { # "wage_counting_type": wage_counting_type, # "total_wage": finance_info['total_price'], # "total_paid_wage": total_paid_wage, # "off": obj.off, # "total_unpaid_wage": finance_info['total_price'] - (total_paid_wage + obj.off), # "province_kill_requests_total_wage": finance_info['total_pure_province_carcasses_price'], # "province_kill_requests_total_weight": finance_info['total_pure_province_carcasses_weight'], # "free_bars_live_total_wage": finance_info['total_out_live_buying_province_carcasses_price'], # "free_bars_live_total_weight": finance_info['total_out_live_buying_province_carcasses_weight'], # "free_bars_carcases_total_wage": finance_info['total_out_carcasses_buying_province_carcasses_price'], # "free_bars_carcases_total_weight": finance_info['total_out_carcasses_buying_province_carcasses_weight'], # "free_bars_out_province_carcases_total_wage": finance_info['total_out_selling_province_carcasses_price'], # "free_bars_out_province_carcases_total_weight": finance_info['total_out_selling_province_carcasses_weight'], # "total_province_live_weight": finance_info['total_province_live_weight'], # "total_province_carcasses_weight": finance_info['total_province_carcasses_weight'], # "province_live_wage_amount": finance_info['province_live_wage_amount'], # "free_buying_live_weight_amount": finance_info['free_buying_live_weight_amount'], # "free_buying_carcesses_weight_amount": finance_info['free_buying_carcesses_weight_amount'], # "free_sell_carcesses_weight_amount": finance_info['free_sell_carcesses_weight_amount'], # "return_total_province_live_weight": finance_info['return_total_province_live_weight'], # "total_return_pure_province_carcasses_price": finance_info['total_return_pure_province_carcasses_price'], # } def get_wage_info(self, obj): date1 = None date2 = None if self.context.get('request').GET.get('date1'): 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() total_paid_wage = 0 if date1: # finance_info = get_kill_house_finance_info(obj,date1,date2) finance_info = get_finance_info(obj, date1, date2) if obj.killer and obj.type == 'public': slaughter_transactions = InternalTransaction.objects.filter( kill_house=obj,parent_kill_house=obj, status='completed', date__date__gte=date1, date__date__lte=date2, trash=False) else: slaughter_transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', date__date__gte=date1, date__date__lte=date2, trash=False) else: # finance_info = get_kill_house_finance_info(obj) finance_info = get_finance_info(obj) if obj.killer and obj.type == 'public': slaughter_transactions = InternalTransaction.objects.filter( kill_house=obj,parent_kill_house=obj, status='completed', trash=False) else: slaughter_transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', trash=False) total_paid_wage += slaughter_transactions.aggregate(total=Sum('amount'))[ 'total'] or 0 return { "wage_counting_type": wage_counting_type, "total_wage": finance_info['total_price'], "total_paid_wage": total_paid_wage, "off": obj.off, "total_unpaid_wage": finance_info['total_price'] - (total_paid_wage + obj.off), "province_kill_requests_total_wage": finance_info['total_pure_province_carcasses_price'], "province_kill_requests_total_weight": finance_info['total_pure_province_carcasses_weight'], "free_bars_live_total_wage": finance_info['total_out_live_buying_province_carcasses_price'], "free_bars_live_total_weight": finance_info['total_out_live_buying_province_carcasses_weight'], "free_bars_carcases_total_wage": finance_info['total_out_carcasses_buying_province_carcasses_price'], "free_bars_carcases_total_weight": finance_info['total_out_carcasses_buying_province_carcasses_weight'], "free_bars_out_province_carcases_total_wage": finance_info['total_out_selling_province_carcasses_price'], "free_bars_out_province_carcases_total_weight": finance_info['total_out_selling_province_carcasses_weight'], "total_province_live_weight": finance_info['total_province_live_weight'], "total_province_carcasses_weight": finance_info['total_province_carcasses_weight'], "province_live_wage_amount": finance_info['province_live_wage_amount'], "free_buying_live_weight_amount": finance_info['free_buying_live_weight_amount'], "free_buying_carcesses_weight_amount": finance_info['free_buying_carcesses_weight_amount'], "free_sell_carcesses_weight_amount": finance_info['free_sell_carcesses_weight_amount'], "return_total_province_live_weight": finance_info['return_total_province_live_weight'], "total_return_pure_province_carcasses_price": finance_info['total_return_pure_province_carcasses_price'], } # def get_shares(self, kill_house): # date1 = None # date2 = None # before_total_out_live_buying_province_carcasses_price = 0 # total_wage_type = WageType.objects.filter(trash=False) # free_buying_live_weight_wage_type = total_wage_type.filter(en_name='live-buy', trash=False).first() # free_buying_live_weight_wage_type_amount = free_buying_live_weight_wage_type.amount if free_buying_live_weight_wage_type.status == True else 0 # # if self.context.get('request').GET.get('date1'): # 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() # # finance_info = get_finance_info(kill_house, date1, date2) if date1 else get_finance_info(kill_house) # if date1: # slaughter_transactions = InternalTransaction.objects.filter( # Q(kill_house=kill_house) | Q(parent_kill_house=kill_house), status='completed', date__date__gte=date1, # date__date__lte=date2, # trash=False) # else: # slaughter_transactions = InternalTransaction.objects.filter( # Q(kill_house=kill_house) | Q(parent_kill_house=kill_house), status='completed', # trash=False) # # total_pure_province_carcasses_price = finance_info['total_pure_province_carcasses_price'] # return_total_pure_province_carcasses_price = finance_info['total_return_pure_province_carcasses_price'] # total_out_selling_province_carcasses_price = finance_info['total_out_selling_province_carcasses_price'] # difference_requests_price = finance_info['difference_requests_price'] # # if new_out_selling_count_wage: # total_out_live_buying_province_carcasses_price = int( # (finance_info[ # 'before_total_out_live_buying_province_carcasses_weight'] * before_out_buying_count_wage_amount) + ( # finance_info[ # 'after_total_out_live_buying_province_carcasses_weight'] * free_buying_live_weight_wage_type_amount)) # # before_total_out_live_buying_province_carcasses_price = finance_info[ # 'before_total_out_live_buying_province_carcasses_weight'] * before_out_buying_count_wage_amount # else: # total_out_live_buying_province_carcasses_price = finance_info[ # 'total_out_live_buying_province_carcasses_price'] # total_out_carcasses_buying_province_carcasses_price = finance_info[ # 'total_out_carcasses_buying_province_carcasses_price'] # total_price = finance_info['total_price'] # # 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 # company_province_kill_request_percent = percentages_wage_type.filter( # wage_type__en_name='province-kill-request', share_type__en_name='company').first().percent / 100 # guilds_province_kill_request_percent = percentages_wage_type.filter( # wage_type__en_name='province-kill-request', share_type__en_name='guilds').first().percent / 100 # other_province_kill_request_percent = percentages_wage_type.filter( # wage_type__en_name='province-kill-request', share_type__en_name='other').first().percent / 100 # # union_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', # share_type__en_name='union').first().percent / 100 # union_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', # share_type__en_name='union').first().percent / 100 # company_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', # share_type__en_name='company').first().percent / 100 # company_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', # share_type__en_name='company').first().percent / 100 # guilds_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', # share_type__en_name='guilds').first().percent / 100 # guilds_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', # share_type__en_name='guilds').first().percent / 100 # other_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', # share_type__en_name='other').first().percent / 100 # other_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', # share_type__en_name='other').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 # company_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', # share_type__en_name='company').first().percent / 100 # guilds_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', # share_type__en_name='guilds').first().percent / 100 # other_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', # share_type__en_name='other').first().percent / 100 # # guilds_difference_requests_price = difference_requests_price * guilds_province_kill_request_percent # other_difference_requests_price = difference_requests_price * other_province_kill_request_percent # guild_return_province_kill_request_wage = return_total_pure_province_carcasses_price * guilds_province_kill_request_percent # other_return_province_kill_request_wage = return_total_pure_province_carcasses_price * other_province_kill_request_percent # # union_province_kill_request_wage = total_pure_province_carcasses_price * union_province_kill_request_percent # return_union_province_kill_request_wage = return_total_pure_province_carcasses_price * union_province_kill_request_percent # union_free_buying_live_wage = ( # total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * union_free_buying_live_percent # union_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * union_free_buying_carcasses_percent # union_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * union_free_sell_carcasses_percent # union_total_wage = union_province_kill_request_wage + union_free_buying_live_wage + union_free_buying_carcasses_wage + union_free_sell_carcasses_wage + return_union_province_kill_request_wage # union_total_paid_wage = slaughter_transactions.aggregate(total=Sum('union_share'))['total'] or 0 # union_total_unpaid_wage = union_total_wage - union_total_paid_wage # company_province_kill_request_wage = ( # total_pure_province_carcasses_price * company_province_kill_request_percent) + ( # guilds_difference_requests_price + other_difference_requests_price) # return_company_province_kill_request_wage = ( # return_total_pure_province_carcasses_price * company_province_kill_request_percent) + ( # guild_return_province_kill_request_wage + other_return_province_kill_request_wage) # company_free_buying_live_wage = (( # total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * company_free_buying_live_percent) + before_total_out_live_buying_province_carcasses_price # company_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * company_free_buying_carcasses_percent # company_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * company_free_sell_carcasses_percent # company_total_wage = company_province_kill_request_wage + company_free_buying_live_wage + company_free_buying_carcasses_wage + company_free_sell_carcasses_wage + return_company_province_kill_request_wage # company_total_paid_wage = slaughter_transactions.aggregate(total=Sum('company_share'))['total'] or 0 # company_total_unpaid_wage = company_total_wage - company_total_paid_wage # # guilds_province_kill_request_wage = ( # total_pure_province_carcasses_price * guilds_province_kill_request_percent) - guilds_difference_requests_price # return_guilds_province_kill_request_wage = 0 # guilds_free_buying_live_wage = ( # total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * guilds_free_buying_live_percent # guilds_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * guilds_free_buying_carcasses_percent # guilds_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * guilds_free_sell_carcasses_percent # guilds_total_wage = guilds_province_kill_request_wage + guilds_free_buying_live_wage + guilds_free_buying_carcasses_wage + guilds_free_sell_carcasses_wage # guilds_total_paid_wage = slaughter_transactions.aggregate(total=Sum('guilds_share'))['total'] or 0 # guilds_total_unpaid_wage = guilds_total_wage - guilds_total_paid_wage # # other_province_kill_request_wage = ( # total_pure_province_carcasses_price * other_province_kill_request_percent) - other_difference_requests_price # return_other_province_kill_request_wage = 0 # other_free_buying_live_wage = ( # total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * other_free_buying_live_percent # other_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * other_free_buying_carcasses_percent # other_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * other_free_sell_carcasses_percent # other_total_wage = other_province_kill_request_wage + other_free_buying_live_wage + other_free_buying_carcasses_wage + other_free_sell_carcasses_wage # other_total_paid_wage = slaughter_transactions.aggregate(total=Sum('other_share'))['total'] or 0 # other_total_unpaid_wage = other_total_wage - other_total_paid_wage # return [ # { # "name": "اتحادیه", # "province_kill_request_wage": union_province_kill_request_wage, # "return_province_kill_request_wage": return_union_province_kill_request_wage, # "free_buying_live_wage": union_free_buying_live_wage, # "free_buying_carcasses_wage": union_free_buying_carcasses_wage, # "free_sell_carcasses_wage": union_free_sell_carcasses_wage, # "total_wage": union_total_wage, # "total_paid_wage": union_total_paid_wage, # "total_unpaid_wage": union_total_unpaid_wage, # }, # { # "name": "شرکت", # "province_kill_request_wage": company_province_kill_request_wage, # "return_province_kill_request_wage": return_company_province_kill_request_wage, # "free_buying_live_wage": company_free_buying_live_wage, # "free_buying_carcasses_wage": company_free_buying_carcasses_wage, # "free_sell_carcasses_wage": company_free_sell_carcasses_wage, # "total_wage": company_total_wage, # "total_paid_wage": company_total_paid_wage, # "total_unpaid_wage": company_total_unpaid_wage, # }, # { # "name": "صنف پروتئین", # "province_kill_request_wage": guilds_province_kill_request_wage, # "return_province_kill_request_wage": return_guilds_province_kill_request_wage, # "free_buying_live_wage": guilds_free_buying_live_wage, # "free_buying_carcasses_wage": guilds_free_buying_carcasses_wage, # "free_sell_carcasses_wage": guilds_free_sell_carcasses_wage, # "total_wage": guilds_total_wage, # "total_paid_wage": guilds_total_paid_wage, # "total_unpaid_wage": guilds_total_unpaid_wage, # }, # { # "name": "دامپزشک", # "province_kill_request_wage": other_province_kill_request_wage, # "return_province_kill_request_wage": return_other_province_kill_request_wage, # "free_buying_live_wage": other_free_buying_live_wage, # "free_buying_carcasses_wage": other_free_buying_carcasses_wage, # "free_sell_carcasses_wage": other_free_sell_carcasses_wage, # "total_wage": other_total_wage, # "total_paid_wage": other_total_paid_wage, # "total_unpaid_wage": other_total_unpaid_wage, # }] def get_shares(self, kill_house): date1 = None date2 = None before_total_out_live_buying_province_carcasses_price = 0 total_wage_type = WageType.objects.filter(trash=False) free_buying_live_weight_wage_type = total_wage_type.filter(en_name='live-buy', trash=False).first() free_buying_live_weight_wage_type_amount = free_buying_live_weight_wage_type.amount if free_buying_live_weight_wage_type.status == True else 0 if self.context.get('request').GET.get('date1'): 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() finance_info = get_finance_info(kill_house, date1, date2) if date1 else get_finance_info(kill_house) if date1: if kill_house.killer and kill_house.type == 'public': slaughter_transactions = InternalTransaction.objects.filter( ill_house=kill_house,parent_kill_house=kill_house, status='completed', date__date__gte=date1, date__date__lte=date2, trash=False) else: slaughter_transactions = InternalTransaction.objects.filter( Q(kill_house=kill_house) | Q(parent_kill_house=kill_house), status='completed', date__date__gte=date1, date__date__lte=date2, trash=False) else: if kill_house.killer and kill_house.type == 'public': slaughter_transactions = InternalTransaction.objects.filter( kill_house=kill_house,parent_kill_house=kill_house, status='completed', trash=False) else: slaughter_transactions = InternalTransaction.objects.filter( Q(kill_house=kill_house) | Q(parent_kill_house=kill_house), status='completed', trash=False) total_pure_province_carcasses_price = finance_info['total_pure_province_carcasses_price'] return_total_pure_province_carcasses_price = finance_info['total_return_pure_province_carcasses_price'] total_out_selling_province_carcasses_price = finance_info['total_out_selling_province_carcasses_price'] difference_requests_price = finance_info['difference_requests_price'] if new_out_selling_count_wage: total_out_live_buying_province_carcasses_price = int( (finance_info[ 'before_total_out_live_buying_province_carcasses_weight'] * before_out_buying_count_wage_amount) + ( finance_info[ 'after_total_out_live_buying_province_carcasses_weight'] * free_buying_live_weight_wage_type_amount)) before_total_out_live_buying_province_carcasses_price = finance_info[ 'before_total_out_live_buying_province_carcasses_weight'] * before_out_buying_count_wage_amount else: total_out_live_buying_province_carcasses_price = finance_info[ 'total_out_live_buying_province_carcasses_price'] total_out_carcasses_buying_province_carcasses_price = finance_info[ 'total_out_carcasses_buying_province_carcasses_price'] total_price = finance_info['total_price'] 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 company_province_kill_request_percent = percentages_wage_type.filter( wage_type__en_name='province-kill-request', share_type__en_name='company').first().percent / 100 guilds_province_kill_request_percent = percentages_wage_type.filter( wage_type__en_name='province-kill-request', share_type__en_name='guilds').first().percent / 100 other_province_kill_request_percent = percentages_wage_type.filter( wage_type__en_name='province-kill-request', share_type__en_name='other').first().percent / 100 union_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', share_type__en_name='union').first().percent / 100 union_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', share_type__en_name='union').first().percent / 100 company_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', share_type__en_name='company').first().percent / 100 company_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', share_type__en_name='company').first().percent / 100 guilds_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', share_type__en_name='guilds').first().percent / 100 guilds_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', share_type__en_name='guilds').first().percent / 100 other_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', share_type__en_name='other').first().percent / 100 other_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', share_type__en_name='other').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 company_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', share_type__en_name='company').first().percent / 100 guilds_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', share_type__en_name='guilds').first().percent / 100 other_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', share_type__en_name='other').first().percent / 100 guilds_difference_requests_price = difference_requests_price * guilds_province_kill_request_percent other_difference_requests_price = difference_requests_price * other_province_kill_request_percent guild_return_province_kill_request_wage = return_total_pure_province_carcasses_price * guilds_province_kill_request_percent other_return_province_kill_request_wage = return_total_pure_province_carcasses_price * other_province_kill_request_percent union_province_kill_request_wage = total_pure_province_carcasses_price * union_province_kill_request_percent return_union_province_kill_request_wage = return_total_pure_province_carcasses_price * union_province_kill_request_percent union_free_buying_live_wage = ( total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * union_free_buying_live_percent union_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * union_free_buying_carcasses_percent union_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * union_free_sell_carcasses_percent union_total_wage = union_province_kill_request_wage + union_free_buying_live_wage + union_free_buying_carcasses_wage + union_free_sell_carcasses_wage + return_union_province_kill_request_wage union_total_paid_wage = slaughter_transactions.aggregate(total=Sum('union_share'))['total'] or 0 union_total_unpaid_wage = union_total_wage - union_total_paid_wage company_province_kill_request_wage = ( total_pure_province_carcasses_price * company_province_kill_request_percent) + ( guilds_difference_requests_price + other_difference_requests_price) return_company_province_kill_request_wage = ( return_total_pure_province_carcasses_price * company_province_kill_request_percent) + ( guild_return_province_kill_request_wage + other_return_province_kill_request_wage) company_free_buying_live_wage = (( total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * company_free_buying_live_percent) + before_total_out_live_buying_province_carcasses_price company_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * company_free_buying_carcasses_percent company_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * company_free_sell_carcasses_percent company_total_wage = company_province_kill_request_wage + company_free_buying_live_wage + company_free_buying_carcasses_wage + company_free_sell_carcasses_wage + return_company_province_kill_request_wage company_total_paid_wage = slaughter_transactions.aggregate(total=Sum('company_share'))['total'] or 0 company_total_unpaid_wage = company_total_wage - company_total_paid_wage guilds_province_kill_request_wage = ( total_pure_province_carcasses_price * guilds_province_kill_request_percent) - guilds_difference_requests_price return_guilds_province_kill_request_wage = 0 guilds_free_buying_live_wage = ( total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * guilds_free_buying_live_percent guilds_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * guilds_free_buying_carcasses_percent guilds_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * guilds_free_sell_carcasses_percent guilds_total_wage = guilds_province_kill_request_wage + guilds_free_buying_live_wage + guilds_free_buying_carcasses_wage + guilds_free_sell_carcasses_wage guilds_total_paid_wage = slaughter_transactions.aggregate(total=Sum('guilds_share'))['total'] or 0 guilds_total_unpaid_wage = guilds_total_wage - guilds_total_paid_wage other_province_kill_request_wage = ( total_pure_province_carcasses_price * other_province_kill_request_percent) - other_difference_requests_price return_other_province_kill_request_wage = 0 other_free_buying_live_wage = ( total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * other_free_buying_live_percent other_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * other_free_buying_carcasses_percent other_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * other_free_sell_carcasses_percent other_total_wage = other_province_kill_request_wage + other_free_buying_live_wage + other_free_buying_carcasses_wage + other_free_sell_carcasses_wage other_total_paid_wage = slaughter_transactions.aggregate(total=Sum('other_share'))['total'] or 0 other_total_unpaid_wage = other_total_wage - other_total_paid_wage return [ { "name": "اتحادیه", "province_kill_request_wage": union_province_kill_request_wage, "return_province_kill_request_wage": return_union_province_kill_request_wage, "free_buying_live_wage": union_free_buying_live_wage, "free_buying_carcasses_wage": union_free_buying_carcasses_wage, "free_sell_carcasses_wage": union_free_sell_carcasses_wage, "total_wage": union_total_wage, "total_paid_wage": union_total_paid_wage, "total_unpaid_wage": union_total_unpaid_wage, }, { "name": "شرکت", "province_kill_request_wage": company_province_kill_request_wage, "return_province_kill_request_wage": return_company_province_kill_request_wage, "free_buying_live_wage": company_free_buying_live_wage, "free_buying_carcasses_wage": company_free_buying_carcasses_wage, "free_sell_carcasses_wage": company_free_sell_carcasses_wage, "total_wage": company_total_wage, "total_paid_wage": company_total_paid_wage, "total_unpaid_wage": company_total_unpaid_wage, }, { "name": "صنف پروتئین", "province_kill_request_wage": guilds_province_kill_request_wage, "return_province_kill_request_wage": return_guilds_province_kill_request_wage, "free_buying_live_wage": guilds_free_buying_live_wage, "free_buying_carcasses_wage": guilds_free_buying_carcasses_wage, "free_sell_carcasses_wage": guilds_free_sell_carcasses_wage, "total_wage": guilds_total_wage, "total_paid_wage": guilds_total_paid_wage, "total_unpaid_wage": guilds_total_unpaid_wage, }, { "name": "دامپزشک", "province_kill_request_wage": other_province_kill_request_wage, "return_province_kill_request_wage": return_other_province_kill_request_wage, "free_buying_live_wage": other_free_buying_live_wage, "free_buying_carcasses_wage": other_free_buying_carcasses_wage, "free_sell_carcasses_wage": other_free_sell_carcasses_wage, "total_wage": other_total_wage, "total_paid_wage": other_total_paid_wage, "total_unpaid_wage": other_total_unpaid_wage, }] class KillHouseColdHouseSerializer(serializers.ModelSerializer): kill_house = KillHouseForProvinceSerializer(read_only=True) class Meta: model = ColdHouse fields = ['key', 'kill_house', 'total_input_weight', 'total_allocated_weight', 'total_remain_weight', 'name', 'province', 'city', 'address', 'capacity', 'active', 'broadcast', 'relocate'] class KillHouseColdHousesSerializer(serializers.ModelSerializer): cold_houses = serializers.SerializerMethodField('get_cold_houses') class Meta: model = KillHouse fields = ['key', 'cold_houses'] def get_cold_houses(self, obj): cold_houses = ColdHouse.objects.filter(kill_house=obj, trash=False) serializer = KillHouseColdHouseSerializer(cold_houses, many=True) return serializer.data class KillHouseForColdHouseSerializer(serializers.ModelSerializer): cold_houses_info = serializers.SerializerMethodField('get_cold_houses_info') fullname = serializers.CharField(source='kill_house_operator.user.fullname', read_only=True) mobile = serializers.CharField(source='kill_house_operator.user.mobile', read_only=True) province = serializers.CharField(source='kill_house_operator.user.province.name', read_only=True) city = serializers.CharField(source='kill_house_operator.user.city.name', read_only=True) class Meta: model = KillHouse fields = ['key', 'name', 'fullname', 'mobile', 'cold_houses_info', 'province', 'city'] def get_cold_houses_info(self, obj): cold_houses = ColdHouse.objects.filter(kill_house=obj, trash=False) total_input_weight = cold_houses.aggregate(total=Sum('total_input_weight'))[ 'total'] or 0 total_allocated_weight = cold_houses.aggregate(total=Sum('total_allocated_weight'))[ 'total'] or 0 total_remain_weight = cold_houses.aggregate(total=Sum('total_remain_weight'))[ 'total'] or 0 return { "total_cold_houses": len(cold_houses), "total_input_weight": total_input_weight, "total_allocated_weight": total_allocated_weight, "total_remain_weight": total_remain_weight, } class ParentCompanyKillHouseForNewWageInormationSerializer(serializers.ModelSerializer): wage_info = serializers.SerializerMethodField('get_wage_info') class Meta: model = KillHouse fields = ['key', 'name', 'wage_info'] def get_wage_info(self, obj): total_paid_wage = 0 finance_info = get_kill_house_finance_info(obj) slaughter_transactions = InternalTransaction.objects.filter( kill_house=obj, status='completed', trash=False) total_paid_wage += slaughter_transactions.aggregate(total=Sum('amount'))[ 'total'] or 0 return { "wage_counting_type": wage_counting_type, "total_wage": finance_info['total_price'], "total_paid_wage": total_paid_wage, "off": obj.off, "total_unpaid_wage": finance_info['total_price'] - (total_paid_wage + obj.off), "province_kill_requests_total_wage": finance_info['total_pure_province_carcasses_price'], "province_kill_requests_total_weight": finance_info['total_pure_province_carcasses_weight'], "free_bars_live_total_wage": finance_info['total_out_live_buying_province_carcasses_price'], "free_bars_live_total_weight": finance_info['total_out_live_buying_province_carcasses_weight'], "free_bars_carcases_total_wage": finance_info['total_out_carcasses_buying_province_carcasses_price'], "free_bars_carcases_total_weight": finance_info['total_out_carcasses_buying_province_carcasses_weight'], "free_bars_out_province_carcases_total_wage": finance_info['total_out_selling_province_carcasses_price'], "free_bars_out_province_carcases_total_weight": finance_info['total_out_selling_province_carcasses_weight'], "total_province_live_weight": finance_info['total_province_live_weight'], "total_province_carcasses_weight": finance_info['total_province_carcasses_weight'], "province_live_wage_amount": finance_info['province_live_wage_amount'], "free_buying_live_weight_amount": finance_info['free_buying_live_weight_amount'], "free_buying_carcesses_weight_amount": finance_info['free_buying_carcesses_weight_amount'], "free_sell_carcesses_weight_amount": finance_info['free_sell_carcesses_weight_amount'], } class KillHouseForProvinceWareHouseDashboardSerializer(serializers.ModelSerializer): ware_house_info = serializers.SerializerMethodField('get_ware_house_info') kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) class Meta: model = KillHouse fields = ['key', 'name', 'killer', 'kill_house_operator', 'ware_house_info'] def _calculate_info(self, obj, date1=None, date2=None): product = RolesProducts.objects.filter(kill_house=obj, trash=False, name='مرغ گرم').first() if date1: bars_date1 = date1 bars_date2 = date2 # bars_date1 = date1 - timedelta(days=1) # bars_date2 = date1 - timedelta(days=1) province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0, trash=False, temporary_trash=False, temporary_deleted=False, kill_request__recive_date__date__gte=bars_date1, kill_request__recive_date__date__lte=bars_date2, warehouse=True) # kill_house_requests = KillHouseRequest.objects.filter( # Q(killhouse_user=obj) | Q(killer=obj), # trash=False, calculate_status=True, # archive_wage=False, # temporary_trash=False, # temporary_deleted=False, # kill_request__recive_date__date__gte=date1, # kill_request__recive_date__date__lte=date2 # ) kill_house_requests = KillHouseRequest.objects.filter( input_warehouse=obj, trash=False, calculate_status=True, kill_request__recive_date__date__gte=bars_date1, kill_request__recive_date__date__lte=bars_date2, warehouse=True ) # kill_house_requests = kill_house_requests.filter( # Q(killhouse_user=obj, killer__isnull=True) | Q(killhouse_user=obj, killer=obj) | Q( # killer=obj)) kill_house_free_bar_informations = KillHouseFreeBarInformation.objects.filter(Q(date__date__gte=date1, date__date__lte=date2, buy_type='carcass') | Q( create_date__date__gte=date1, create_date__date__lte=date2, buy_type='live'), input_warehouse=obj, trash=False, temporary_trash=False, temporary_deleted=False, calculate_status=True, warehouse=True).exclude( entered_message='ورود به انبار مجازی') kill_house_free_Sale_bar_informations = KillHouseFreeSaleBarInformation.objects.filter(kill_house=obj, date__date__gte=date1, date__date__lte=date2, trash=False, temporary_trash=False, temporary_deleted=False, calculate_status=True, warehouse=True) kill_house_allocations = StewardAllocation.objects.filter(kill_house=obj, trash=False, receiver_state__in=('pending', 'accepted'), temporary_trash=False, temporary_deleted=False, to_cold_house__isnull=True, calculate_status=True, date__date__gte=date1, date__date__lte=date2, warehouse=True) segmentations = PosSegmentation.objects.filter(kill_house=obj, trash=False, date__date__gte=date1, date__date__lte=date2, warehouse=True) cold_house_allocations = StewardAllocation.objects.filter( kill_house=obj, date__date__gte=date1, date__date__lte=date2, to_cold_house__isnull=False, receiver_state__in=('accepted', 'pending'), trash=False, warehouse=True) transactions = ProductsTransactions.objects.filter(product=product, transaction__paid=True, transaction__date__date__gte=date1, transaction__date__date__lte=date2, trash=False, warehouse=True) archives = WarehouseArchive.objects.filter(kill_house=obj,date__date__gte=date1,date__date__lte=date2, trash=False,warehouse=True) else: province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), temporary_trash=False, temporary_deleted=False, first_car_allocated_quantity=0, trash=False, warehouse=True) # kill_house_requests = KillHouseRequest.objects.filter( # Q(killhouse_user=obj) | Q(killer=obj), # temporary_trash=False, # temporary_deleted=False, # trash=False, calculate_status=True) kill_house_requests = KillHouseRequest.objects.filter( input_warehouse=obj, # Q(killhouse_user=obj)|Q(killer=obj), trash=False, calculate_status=True, warehouse=True ) # kill_house_requests = kill_house_requests.filter( # Q(killhouse_user=obj, killer__isnull=True) | Q(killhouse_user=obj, killer=obj) | Q( # killer=obj)) kill_house_free_bar_informations = KillHouseFreeBarInformation.objects.filter(input_warehouse=obj, trash=False, temporary_trash=False, temporary_deleted=False, calculate_status=True, warehouse=True).exclude( entered_message='ورود به انبار مجازی') kill_house_free_Sale_bar_informations = KillHouseFreeSaleBarInformation.objects.filter(kill_house=obj, trash=False, temporary_trash=False, temporary_deleted=False, calculate_status=True, warehouse=True) kill_house_allocations = StewardAllocation.objects.filter(kill_house=obj, trash=False, receiver_state__in=('pending', 'accepted'), temporary_trash=False, temporary_deleted=False, to_cold_house__isnull=True, calculate_status=True, warehouse=True) segmentations = PosSegmentation.objects.filter(kill_house=obj, trash=False, warehouse=True) cold_house_allocations = StewardAllocation.objects.filter( kill_house=obj, to_cold_house__isnull=False, receiver_state__in=('accepted', 'pending'), trash=False, warehouse=True) transactions = ProductsTransactions.objects.filter(product=product, trash=False, warehouse=True) archives = WarehouseArchive.objects.filter(kill_house=obj, trash=False,warehouse=True) province_kill_request_info = province_kill_requests.aggregate( total_province_kill_request_count=Count('id'), total_province_kill_request_quantity=Sum('total_killed_quantity'), total_province_kill_request_weight=Sum('total_killed_weight'), ) kill_house_request_info = kill_house_requests.aggregate( total_kill_house_request_count=Count('id'), total_kill_house_request_warehouse_entered_count=Count('id', filter=Q(ware_house_confirmation=True)), total_kill_house_request_warehouse_not_entered_count=Count('id', filter=Q(ware_house_confirmation=False)), total_kill_house_request_quantity=Sum('accepted_real_quantity'), total_kill_house_request_weight=Sum('accepted_real_weight'), total_kill_house_request_warehouse_not_entered_quantity=Sum('accepted_real_quantity', filter=Q(ware_house_confirmation=False)), total_kill_house_request_warehouse_not_entered_weight=Sum('accepted_real_weight', filter=Q(ware_house_confirmation=False)), kill_house_request_warehouse_weight=Sum('ware_house_accepted_real_weight', filter=Q(ware_house_confirmation=True)), kill_house_request_warehouse_governmental_weight=Sum('ware_house_accepted_real_weight', filter=Q(ware_house_confirmation=True, province_request__poultry_request__free_sale_in_province=False)), kill_house_request_warehouse_free_weight=Sum('ware_house_accepted_real_weight', filter=Q(ware_house_confirmation=True, province_request__poultry_request__free_sale_in_province=True)), ) kill_house_free_bar_info = kill_house_free_bar_informations.aggregate( total_kill_house_free_bar_live_count=Count('id', filter=Q(buy_type='live')), total_kill_house_free_bar_carcass_count=Count('id', filter=Q(buy_type='carcass')), total_kill_house_free_bar_warehouse_entered_count=Count('id', filter=Q(weight_of_carcasses__gt=0)), total_kill_house_free_bar_warehouse_not_entered_count=Count('id', filter=Q(weight_of_carcasses=0)), total_kill_house_free_bar_live_quantity=Sum('quantity', filter=Q(buy_type='live')), total_kill_house_free_bar_live_weight=Sum('live_weight', filter=Q(buy_type='live')), total_kill_house_free_bar_carcass_quantity=Sum('number_of_carcasses', filter=Q(buy_type='carcass')), total_kill_house_free_bar_carcass_weight=Sum('weight_of_carcasses', filter=Q(buy_type='carcass')), total_kill_house_free_bar_carcass_total_weight=Sum('weight_of_carcasses'), total_kill_house_free_bar_live_warehouse_not_entered_quantity=Sum('quantity', filter=Q(weight_of_carcasses=0)), total_kill_house_free_bar_warehouse_not_entered_weight=Sum('live_weight', filter=Q(weight_of_carcasses=0)), ) kill_house_free_Sale_bar_info = kill_house_free_Sale_bar_informations.aggregate( total_kill_house_free_Sale_bar_weight=Sum('real_weight_of_carcasses'), total_kill_house_free_Sale_bar_governmental_weight=Sum('real_weight_of_carcasses', filter=Q(quota='governmental')), total_kill_house_free_Sale_bar_free_weight=Sum('real_weight_of_carcasses', filter=Q(quota='free')), ) kill_house_allocations_info = kill_house_allocations.aggregate( total_kill_house_allocations_weight=Sum('real_weight_of_carcasses'), total_kill_house_allocations_governmental_weight=Sum('real_weight_of_carcasses', filter=Q(quota='governmental')), total_kill_house_allocations_free_weight=Sum('real_weight_of_carcasses', filter=Q(quota='free')), ) segmentations_aggregates_info = segmentations.aggregate( total_segmentations_weight=Sum('weight'), total_segmentations_governmental_weight=Sum('weight', filter=Q(quota='governmental')), total_segmentations_free_weight=Sum('weight', filter=Q(quota='free')), ) cold_house_allocations_info = cold_house_allocations.aggregate( total_cold_house_allocations_weight=Sum('real_weight_of_carcasses'), total_cold_house_allocations_governmental_weight=Sum('real_weight_of_carcasses', filter=Q(quota='governmental')), total_cold_house_allocations_free_weight=Sum('real_weight_of_carcasses', filter=Q(quota='free')), ) pos_allocated_weight_info = transactions.aggregate( pos_allocated_weight=Sum('cur_weight'), pos_governmental_allocated_weight=Sum('cur_weight', filter=Q(price_approved=True)), pos_free_allocated_weight=Sum('cur_weight', filter=Q(price_approved=False)), ) archives_info = archives.aggregate( total_archive_governmental_weight=Sum('weight', filter=Q(quota='governmental')), total_archive_free_weight=Sum('weight', filter=Q(quota='free')), ) return {**province_kill_request_info, **kill_house_request_info, **kill_house_free_bar_info, **kill_house_free_Sale_bar_info, **kill_house_allocations_info, **segmentations_aggregates_info, **cold_house_allocations_info, **pos_allocated_weight_info, **archives_info} def get_ware_house_info(self, obj): product_remain_weight = RolesProducts.objects.filter(kill_house=obj, trash=False).only( 'total_remain_weight').first() product_remain_weight = product_remain_weight.total_remain_weight if product_remain_weight else 0 request = self.context.get('request') date1 = request.GET.get('date1', None) date2 = request.GET.get('date2', None) if date1 and date2: date1 = datetime.datetime.strptime(str(date1), '%Y-%m-%d').date() date2 = datetime.datetime.strptime(str(date2), '%Y-%m-%d').date() total_info = self._calculate_info(obj, date1, date2) if date1 else self._calculate_info(obj) total_kill_house_requests_quantity = total_info['total_kill_house_request_quantity'] if total_info[ 'total_kill_house_request_quantity'] else 0 total_kill_house_free_bar_quantity = total_info['total_kill_house_free_bar_live_quantity'] if total_info[ 'total_kill_house_free_bar_live_quantity'] else 0 total_kill_house_free_bar_carcasses = total_info['total_kill_house_free_bar_carcass_quantity'] if total_info[ 'total_kill_house_free_bar_carcass_quantity'] else 0 total_kill_house_requests_weight = total_info['total_kill_house_request_weight'] if total_info[ 'total_kill_house_request_weight'] else 0 total_kill_house_free_bar_weight = total_info['total_kill_house_free_bar_live_weight'] if total_info[ 'total_kill_house_free_bar_live_weight'] else 0 total_kill_house_free_bar_carcasses_weight = total_info['total_kill_house_free_bar_carcass_weight'] if \ total_info['total_kill_house_free_bar_carcass_weight'] else 0 total_province_bars = total_info['total_kill_house_request_count'] if total_info[ 'total_kill_house_request_count'] else 0 kill_house_free_bar_informations_live = total_info['total_kill_house_free_bar_live_count'] if total_info[ 'total_kill_house_free_bar_live_count'] else 0 kill_house_free_bar_informations_carcasses = total_info['total_kill_house_free_bar_carcass_count'] if \ total_info['total_kill_house_free_bar_carcass_count'] else 0 total_entered_kill_house_requests_carcasses_weight = total_info['kill_house_request_warehouse_weight'] if \ total_info['kill_house_request_warehouse_weight'] else 0 total_entered_kill_house_free_bar_carcasses_weight = total_info[ 'total_kill_house_free_bar_carcass_total_weight'] if total_info[ 'total_kill_house_free_bar_carcass_total_weight'] else 0 entered_kill_house_request = total_info['total_kill_house_request_warehouse_entered_count'] if total_info[ 'total_kill_house_request_warehouse_entered_count'] else 0 entered_free_buying_bars = total_info['total_kill_house_free_bar_warehouse_entered_count'] if total_info[ 'total_kill_house_free_bar_warehouse_entered_count'] else 0 not_entered_kill_house_request = total_info['total_kill_house_request_warehouse_not_entered_count'] if \ total_info['total_kill_house_request_warehouse_not_entered_count'] else 0 not_entered_free_buying_bars = total_info['total_kill_house_free_bar_warehouse_not_entered_count'] if \ total_info['total_kill_house_free_bar_warehouse_not_entered_count'] else 0 not_entered_kill_house_request_quantity = total_info[ 'total_kill_house_request_warehouse_not_entered_quantity'] if total_info[ 'total_kill_house_request_warehouse_not_entered_quantity'] else 0 not_entered_free_buying_bars_quantity = total_info[ 'total_kill_house_free_bar_live_warehouse_not_entered_quantity'] if total_info[ 'total_kill_house_free_bar_live_warehouse_not_entered_quantity'] else 0 not_entered_kill_house_request_weight = total_info['total_kill_house_request_warehouse_not_entered_weight'] if \ total_info['total_kill_house_request_warehouse_not_entered_weight'] else 0 not_entered_free_buying_bars_weight = total_info['total_kill_house_free_bar_warehouse_not_entered_weight'] if \ total_info['total_kill_house_free_bar_warehouse_not_entered_weight'] else 0 total_kill_house_allocations_weight = total_info['total_kill_house_allocations_weight'] if \ total_info['total_kill_house_allocations_weight'] else 0 total_kill_house_free_Sale_bar_weight = total_info['total_kill_house_free_Sale_bar_weight'] if \ total_info['total_kill_house_free_Sale_bar_weight'] else 0 total_kill_house_segmentations_weight = total_info['total_segmentations_weight'] if \ total_info['total_segmentations_weight'] else 0 total_kill_house_cold_house_allocations_weight = total_info['total_cold_house_allocations_weight'] if \ total_info['total_cold_house_allocations_weight'] else 0 kill_house_request_warehouse_governmental_weight = total_info[ 'kill_house_request_warehouse_governmental_weight'] if \ total_info['kill_house_request_warehouse_governmental_weight'] else 0 kill_house_request_warehouse_free_weight = total_info['kill_house_request_warehouse_free_weight'] if \ total_info['kill_house_request_warehouse_free_weight'] else 0 total_kill_house_free_Sale_bar_governmental_weight = total_info[ 'total_kill_house_free_Sale_bar_governmental_weight'] if \ total_info['total_kill_house_free_Sale_bar_governmental_weight'] else 0 total_kill_house_free_Sale_bar_free_weight = total_info['total_kill_house_free_Sale_bar_free_weight'] if \ total_info['total_kill_house_free_Sale_bar_free_weight'] else 0 total_kill_house_allocations_governmental_weight = total_info[ 'total_kill_house_allocations_governmental_weight'] if \ total_info['total_kill_house_allocations_governmental_weight'] else 0 total_kill_house_allocations_free_weight = total_info['total_kill_house_allocations_free_weight'] if \ total_info['total_kill_house_allocations_free_weight'] else 0 total_segmentations_governmental_weight = total_info['total_segmentations_governmental_weight'] if \ total_info['total_segmentations_governmental_weight'] else 0 total_segmentations_free_weight = total_info['total_segmentations_free_weight'] if \ total_info['total_segmentations_free_weight'] else 0 total_cold_house_allocations_governmental_weight = total_info[ 'total_cold_house_allocations_governmental_weight'] if \ total_info['total_cold_house_allocations_governmental_weight'] else 0 total_cold_house_allocations_free_weight = total_info['total_cold_house_allocations_free_weight'] if \ total_info['total_cold_house_allocations_free_weight'] else 0 pos_allocated_weight = total_info['pos_allocated_weight'] or 0 pos_governmental_allocated_weight = total_info['pos_governmental_allocated_weight'] or 0 pos_free_allocated_weight = total_info['pos_free_allocated_weight'] or 0 total_archive_governmental_weight = total_info['total_archive_governmental_weight'] or 0 total_archive_free_weight = total_info['total_archive_free_weight'] or 0 total_governmental_input_weight = kill_house_request_warehouse_governmental_weight total_free_input_weight = kill_house_request_warehouse_free_weight + total_entered_kill_house_free_bar_carcasses_weight total_governmental_output_weight = total_kill_house_free_Sale_bar_governmental_weight + total_kill_house_allocations_governmental_weight + total_segmentations_governmental_weight + total_cold_house_allocations_governmental_weight + int(pos_governmental_allocated_weight / 1000) total_free_output_weight = total_kill_house_free_Sale_bar_free_weight + total_kill_house_allocations_free_weight + total_segmentations_free_weight + total_cold_house_allocations_free_weight + int(pos_free_allocated_weight / 1000) total_governmental_remain_weight = (total_governmental_input_weight - total_governmental_output_weight) - total_archive_governmental_weight if ( total_governmental_input_weight - total_governmental_output_weight) - total_archive_governmental_weight > 0 else 0 last_total_governmental_remain_weight = ( (total_governmental_input_weight - total_governmental_output_weight) - total_archive_governmental_weight) * -1 if ( total_governmental_input_weight - total_governmental_output_weight) - total_archive_governmental_weight < 0 else 0 total_free_remain_weight = (total_free_input_weight - total_free_output_weight) - total_archive_free_weight if ( total_free_input_weight - total_free_output_weight) - total_archive_free_weight > 0 else 0 last_total_free_remain_weight = ((total_free_input_weight - total_free_output_weight) - total_archive_free_weight) * -1 if ( total_free_input_weight - total_free_output_weight) - total_archive_free_weight < 0 else 0 last_total_remain_weight = last_total_governmental_remain_weight + last_total_free_remain_weight warehouse_total_weight = total_kill_house_requests_weight + total_kill_house_free_bar_weight + total_kill_house_free_bar_carcasses_weight warehouse_total_entered_carcasses_weight = total_entered_kill_house_requests_carcasses_weight + total_entered_kill_house_free_bar_carcasses_weight difference_weight = warehouse_total_entered_carcasses_weight - ( total_kill_house_allocations_weight + total_kill_house_free_Sale_bar_weight + total_kill_house_segmentations_weight + total_kill_house_cold_house_allocations_weight) final_difference_weight = difference_weight if difference_weight > 0 else (-1) * difference_weight total_sell = total_kill_house_allocations_weight + total_kill_house_free_Sale_bar_weight + total_kill_house_segmentations_weight + total_kill_house_cold_house_allocations_weight return { "product_remain_weight": product_remain_weight, "previous_product_remain_weight": int(product_remain_weight + final_difference_weight) if date1 else 0, "total_sell": total_sell, "total_province_bars": total_province_bars, "total_province_bars_quantity": total_info['total_kill_house_request_quantity'] if total_info[ 'total_kill_house_request_quantity'] else 0, "total_province_bars_weight": total_info['total_kill_house_request_weight'] if total_info[ 'total_kill_house_request_weight'] else 0, "total_kill_house_free_bar": total_info['total_kill_house_free_bar_live_count'] if total_info[ 'total_kill_house_free_bar_live_count'] else 0, "total_kill_house_free_live_bar_quantity": total_info['total_kill_house_free_bar_live_quantity'] if total_info['total_kill_house_free_bar_live_quantity'] else 0, "total_kill_house_free_live_bar_weight": total_info['total_kill_house_free_bar_live_weight'] if total_info[ 'total_kill_house_free_bar_live_weight'] else 0, "total_kill_house_free_carcasses_bar": total_info['total_kill_house_free_bar_carcass_count'] if total_info[ 'total_kill_house_free_bar_carcass_count'] else 0, "total_kill_house_free_bar_carcasses": total_info['total_kill_house_free_bar_carcass_quantity'] if total_info['total_kill_house_free_bar_carcass_quantity'] else 0, "total_kill_house_free_bar_carcasses_weight": total_info['total_kill_house_free_bar_carcass_weight'] if total_info['total_kill_house_free_bar_carcass_weight'] else 0, "province_kill_requests": total_info['total_province_kill_request_count'] if total_info[ 'total_province_kill_request_count'] else 0, "province_kill_requests_quantity": total_info['total_province_kill_request_quantity'] if total_info[ 'total_province_kill_request_quantity'] else 0, "province_kill_requests_weight": total_info['total_province_kill_request_weight'] if total_info[ 'total_province_kill_request_weight'] else 0, "total_bars": total_province_bars + kill_house_free_bar_informations_live + kill_house_free_bar_informations_carcasses, "warehouse_total_quantity": total_kill_house_requests_quantity + total_kill_house_free_bar_quantity + total_kill_house_free_bar_carcasses, "warehouse_total_weight": warehouse_total_weight, "warehouse_total_entered_bars": entered_kill_house_request + entered_free_buying_bars, "warehouse_total_entered_carcasses_weight": warehouse_total_entered_carcasses_weight, "warehouse_total_not_entered_bars": not_entered_kill_house_request + not_entered_free_buying_bars, "warehouse_total_not_entered_bars_quantity": not_entered_kill_house_request_quantity + not_entered_free_buying_bars_quantity, "warehouse_total_not_entered_bars_weight": not_entered_kill_house_request_weight + not_entered_free_buying_bars_weight, "total_kill_house_allocations_weight": total_info['total_kill_house_allocations_weight'] if total_info[ 'total_kill_house_allocations_weight'] else 0, "total_kill_house_free_sale__bar_carcasses_weight": total_info['total_kill_house_free_Sale_bar_weight'] if total_info['total_kill_house_free_Sale_bar_weight'] else 0, "total_kill_house_segmentations_weight": total_kill_house_segmentations_weight, "total_kill_house_cold_house_allocations_weight": total_kill_house_cold_house_allocations_weight, "total_governmental_input_weight": total_governmental_input_weight, "total_free_input_weight": total_free_input_weight, "total_governmental_output_weight": total_governmental_output_weight, "total_free_output_weight": total_free_output_weight, "total_governmental_remain_weight": total_governmental_remain_weight, "total_free_remain_weight": total_free_remain_weight, "last_total_governmental_remain_weight": last_total_governmental_remain_weight, "last_total_free_remain_weight": last_total_free_remain_weight, "last_total_remain_weight": last_total_remain_weight, "pos_allocated_weight": int(pos_allocated_weight / 1000), "pos_governmental_allocated_weight": int(pos_governmental_allocated_weight / 1000), "pos_free_allocated_weight": int(pos_free_allocated_weight / 1000), "total_archive_governmental_weight": total_archive_governmental_weight, "total_archive_free_weight": total_archive_free_weight, } class KillHouseForFreeBarWageSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'info'] def get_info(self, obj): total_weight = 0 total_wage = 0 total_quantity = 0 type = self.context.get('request').GET['type'] role = self.context.get('request').GET['role'] if role in ['KillHouse', 'SuperAdmin', 'AdminX', 'Supporter']: wage_type = 'total_wage_amount' free_bars = KillHouseFreeBarInformation.objects.filter(kill_house=obj, archive_wage=False, buy_type=type, trash=False) elif role == 'ProvinceOperator': wage_type = 'union_share' free_bars = KillHouseFreeBarInformation.objects.filter(kill_house=obj, archive_wage=False, buy_type=type, union_share__gt=0, trash=False) elif role == 'Company': wage_type = 'company_share' free_bars = KillHouseFreeBarInformation.objects.filter(kill_house=obj, archive_wage=False, buy_type=type, company_share__gt=0, trash=False) elif role == 'Guilds': wage_type = 'guilds_share' free_bars = KillHouseFreeBarInformation.objects.filter(kill_house=obj, archive_wage=False, buy_type=type, guilds_share__gt=0, trash=False) else: wage_type = 'wallet_share' if type == 'live': total_weight += free_bars.aggregate(total=Sum('live_weight'))[ 'total'] or 0 total_quantity += free_bars.aggregate(total=Sum('quantity'))[ 'total'] or 0 total_wage += free_bars.aggregate(total=Sum(wage_type))[ 'total'] or 0 else: total_weight += free_bars.aggregate(total=Sum('weight_of_carcasses'))[ 'total'] or 0 total_quantity += free_bars.aggregate(total=Sum('number_of_carcasses'))[ 'total'] or 0 total_wage += free_bars.aggregate(total=Sum(wage_type))[ 'total'] or 0 return { "killer": obj.killer, "kill_house_fullname": obj.kill_house_operator.user.fullname, "kill_house_name": obj.name, "kill_house_mobile": obj.kill_house_operator.user.mobile, "kill_house_city": obj.kill_house_operator.user.city.name, "total_count": len(free_bars), "total_weight": total_weight, "total_quantity": total_quantity, "total_wage": total_wage, } class KillHouseForProvinceWageWithDateSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'info'] def get_info(self, obj): total_quantity = 0 total_weight = 0 total_wage = 0 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() role = self.context.get('request').GET['role'] if role in ['KillHouse', 'SuperAdmin', 'AdminX', 'Supporter']: wage_type = 'total_wage_amount' province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, state__in=('pending', 'accepted'), return_to_province=False, archive_wage=False, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, trash=False).select_related( 'province_request__poultry_request') elif role == 'ProvinceOperator': wage_type = 'union_share' province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, state__in=('pending', 'accepted'), union_share__gt=0, return_to_province=False, archive_wage=False, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, trash=False).select_related( 'province_request__poultry_request') elif role == 'Company': wage_type = 'company_share' province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, state__in=('pending', 'accepted'), company_share__gt=0, return_to_province=False, archive_wage=False, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, trash=False).select_related( 'province_request__poultry_request') elif role == 'Guilds': wage_type = 'guilds_share' province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, state__in=('pending', 'accepted'), guilds_share__gt=0, return_to_province=False, archive_wage=False, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, trash=False).select_related( 'province_request__poultry_request') else: wage_type = 'wallet_share' total_quantity += province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 total_weight += province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 total_wage += province_kill_requests.aggregate(total=Sum(wage_type))[ 'total'] or 0 return { "killer": obj.killer, "kill_house_fullname": obj.kill_house_operator.user.fullname, "kill_house_name": obj.name, "kill_house_mobile": obj.kill_house_operator.user.mobile, "kill_house_city": obj.kill_house_operator.user.city.name, "total_count": len(province_kill_requests), "total_quantity": total_quantity, "total_weight": total_weight, "total_wage": total_wage, } class KillHouseForKillHouseRequestWageWithDateSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'info'] def get_info(self, obj): total_quantity = 0 total_weight = 0 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() # role = self.context.get('request').GET['role'] kill_house_requests = KillHouseRequest.objects.filter(Q(killhouse_user=obj) | Q(killer=obj), kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, archive_wage=False, trash=False) total_quantity += kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))[ 'total'] or 0 total_weight += kill_house_requests.aggregate(total=Sum('accepted_real_weight'))[ 'total'] or 0 return { "killer": obj.killer, "kill_house_fullname": obj.kill_house_operator.user.fullname, "kill_house_name": obj.name, "kill_house_mobile": obj.kill_house_operator.user.mobile, "kill_house_city": obj.kill_house_operator.user.city.name, "total_count": len(kill_house_requests), "total_quantity": total_quantity, "total_weight": total_weight, } # class KillHouseForProvinceWageWithDateSerializer(serializers.ModelSerializer): # info = serializers.SerializerMethodField('get_info') # # class Meta: # model = KillHouse # fields = ['key', 'info'] # # def get_info(self, obj): # total_count = 0 # paid_count = 0 # unpaid_count = 0 # archive_count = 0 # total_quantity = 0 # total_weight_unpaid = 0 # total_weight_archive = 0 # total_weight_paid = 0 # total_unpaid_wage = 0 # total_archive_wage = 0 # total_paid_wage = 0 # total_paid_quantity = 0 # total_unpaid_quantity = 0 # total_archive_quantity = 0 # total_quantity_sale_free = 0 # total_quantity_sale_government = 0 # total_weight_sale_free = 0 # total_wage = 0 # total_weight_sale_government = 0 # 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() # role = self.context.get('request').GET['role'] # province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, # state__in=('pending', 'accepted'), # return_to_province=False, # kill_request__recive_date__date__gte=date1, # kill_request__recive_date__date__lte=date2, # trash=False).select_related( # 'province_request__poultry_request') # # province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, # # state__in=('pending', 'accepted'), # # return_to_province=False, # # trash=False).select_related( # # 'province_request__poultry_request') # total_count = len(province_kill_requests) # total_quantity = province_kill_requests.aggregate(total_quantity=Sum('total_killed_quantity')).get( # 'total_quantity', 0) # # for province_kill_request in province_kill_requests: # if province_kill_request.wage_pay == True: # paid_count += 1 # total_paid_quantity += province_kill_request.total_killed_quantity # total_paid_wage += province_kill_request.total_killed_weight * province_kill_request.wage # # total_paid_wage += int(province_kill_request.depositor['total_amount']) # total_weight_paid += province_kill_request.total_killed_weight # elif province_kill_request.wage_pay == False and province_kill_request.archive_by_province == False: # unpaid_count += 1 # total_unpaid_quantity += province_kill_request.total_killed_quantity # total_unpaid_wage += province_kill_request.total_killed_weight * province_kill_request.wage # total_weight_unpaid += province_kill_request.total_killed_weight # # else: # archive_count += 1 # total_archive_quantity += province_kill_request.total_killed_quantity # total_archive_wage += province_kill_request.total_killed_weight * province_kill_request.wage # total_weight_archive += province_kill_request.total_killed_weight # if province_kill_request.province_request.poultry_request.free_sale_in_province == True: # total_quantity_sale_free += province_kill_request.total_killed_quantity # total_weight_sale_free += province_kill_request.total_killed_weight # # else: # total_quantity_sale_government += province_kill_request.total_killed_quantity # total_weight_sale_government += province_kill_request.total_killed_weight # total_wage = total_paid_wage + total_unpaid_wage # # gate_way_percentage = PaymentGatewayPercentage.objects.get(trash=False) # union = gate_way_percentage.union / 100 if gate_way_percentage.union > 0 else 0 # # if role == 'ProvinceOperator': # total_wage = (total_paid_wage + total_unpaid_wage) * union # total_paid_wage = total_paid_wage * union # total_unpaid_wage = total_unpaid_wage * union # total_archive_wage = total_archive_wage * union # # return { # "killer": obj.killer, # "kill_house_fullname": obj.kill_house_operator.user.fullname, # "kill_house_name": obj.name, # "kill_house_mobile": obj.kill_house_operator.user.mobile, # "kill_house_city": obj.kill_house_operator.user.city.name, # "total_count": total_count, # "paid_count": paid_count, # "unpaid_count": unpaid_count, # "archive_count": archive_count, # "total_quantity": total_quantity, # "total_weight": int(total_weight_paid + total_weight_unpaid), # "total_weight_paid": int(total_weight_paid), # "total_weight_archive": int(total_weight_archive), # "total_weight_unpaid": int(total_weight_unpaid), # "total_paid_wage": total_paid_wage, # "total_unpaid_wage": total_unpaid_wage, # "total_archive_wage": total_archive_wage, # # "total_wage": total_paid_wage + total_unpaid_wage, # "total_wage": total_wage, # "total_unpaid_quantity": total_unpaid_quantity, # "total_paid_quantity": total_paid_quantity, # "total_archive_quantity": total_archive_quantity, # "total_quantity_sale_free": total_quantity_sale_free, # "total_weight_sale_free": total_weight_sale_free, # "total_quantity_sale_government": total_quantity_sale_government, # "total_weight_sale_government": total_weight_sale_government, # # } class KillHouseForTotalProvinceWageTransactionWithDAteSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'info'] def get_info(self, obj): total_paid_wage = 0 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() role = self.context.get('request').GET['role'] if role in ['KillHouse', 'AdminX', 'Supporter']: wage_type = 'amount' transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), date__date__gte=date1, date__date__lte=date2, status='completed', trash=False).order_by('id') elif role == 'SuperAdmin': wage_type = 'amount' transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), date__date__gte=date1, date__date__lte=date2, status='completed', trash=False).exclude(union_share=0, company_share=0, guilds_share=0).order_by('id') elif role == 'ProvinceOperator': wage_type = 'union_share' transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', date__date__gte=date1, date__date__lte=date2, union_share__gt=0, trash=False).order_by('id') elif role == 'Company': wage_type = 'company_share' transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', company_share__gt=0, date__date__gte=date1, date__date__lte=date2, trash=False).order_by('id') elif role == 'Guilds': wage_type = 'guilds_share' transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', guilds_share__gt=0, date__date__gte=date1, date__date__lte=date2, trash=False).order_by('id') else: wage_type = 'wallet_share' total_paid_wage += transactions.aggregate(total=Sum(wage_type))[ 'total'] or 0 if role == 'SuperAdmin': total_paid_wage -= transactions.aggregate(total=Sum('other_share'))[ 'total'] or 0 return { "killer": obj.killer, "kill_house_fullname": obj.kill_house_operator.user.fullname, "kill_house_name": obj.name, "kill_house_mobile": obj.kill_house_operator.user.mobile, "kill_house_city": obj.kill_house_operator.user.city.name, "total_paid_count": len(transactions), "total_paid_wage": total_paid_wage, } class KillHouseForTotalProvinceWageTransactionSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'info'] def get_info(self, obj): total_paid_wage = 0 role = self.context.get('request').GET['role'] # transactions = InternalTransaction.objects.filter( # Q(kill_house=obj) | Q(parent_kill_house=obj), # status='completed', # trash=False).order_by('id') if role in ['KillHouse', 'AdminX', 'Supporter']: wage_type = 'amount' transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', trash=False).order_by('id') elif role == 'SuperAdmin': wage_type = 'amount' transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', trash=False).exclude(union_share=0, company_share=0, guilds_share=0).order_by('id') elif role == 'ProvinceOperator': wage_type = 'union_share' transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', union_share__gt=0, trash=False).order_by('id') elif role == 'Company': wage_type = 'company_share' transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', company_share__gt=0, trash=False).order_by('id') elif role == 'Guilds': wage_type = 'guilds_share' transactions = InternalTransaction.objects.filter( Q(kill_house=obj) | Q(parent_kill_house=obj), status='completed', guilds_share__gt=0, trash=False).order_by('id') else: wage_type = 'wallet_share' total_paid_wage += transactions.aggregate(total=Sum(wage_type))[ 'total'] or 0 if role == 'SuperAdmin': total_paid_wage -= transactions.aggregate(total=Sum('other_share'))[ 'total'] or 0 return { "killer": obj.killer, "kill_house_fullname": obj.kill_house_operator.user.fullname, "kill_house_name": obj.name, "kill_house_mobile": obj.kill_house_operator.user.mobile, "kill_house_city": obj.kill_house_operator.user.city.name, "total_paid_count": len(transactions), "total_paid_wage": total_paid_wage, } class KillHouseForFreeBarWageWithDateSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'info'] def get_info(self, obj): total_weight = 0 total_wage = 0 total_quantity = 0 type = self.context.get('request').GET['type'] role = self.context.get('request').GET['role'] if role in ['KillHouse', 'SuperAdmin', 'AdminX', 'Supporter']: wage_type = 'total_wage_amount' elif role == 'ProvinceOperator': wage_type = 'union_share' elif role == 'Company': wage_type = 'company_share' elif role == 'Company': wage_type = 'company_share' else: wage_type = 'wallet_share' 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() free_bars = KillHouseFreeBarInformation.objects.filter(kill_house=obj, date__date__gte=date1, date__date__lte=date2, archive_wage=False, trash=False) if type == 'live': total_weight += free_bars.aggregate(total=Sum('live_weight'))[ 'total'] or 0 total_quantity += free_bars.aggregate(total=Sum('quantity'))[ 'total'] or 0 total_wage += free_bars.aggregate(total=Sum(wage_type))[ 'total'] or 0 else: total_weight += free_bars.aggregate(total=Sum('weight_of_carcasses'))[ 'total'] or 0 total_quantity += free_bars.aggregate(total=Sum('number_of_carcasses'))[ 'total'] or 0 total_wage += free_bars.aggregate(total=Sum(wage_type))[ 'total'] or 0 return { "killer": obj.killer, "kill_house_fullname": obj.kill_house_operator.user.fullname, "kill_house_name": obj.name, "kill_house_mobile": obj.kill_house_operator.user.mobile, "kill_house_city": obj.kill_house_operator.user.city.name, "total_count": len(free_bars), "total_weight": total_weight, "total_quantity": total_quantity, "total_wage": total_wage, } class KillHouseForFreeSaleBarInformationViewSetWithDateSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'name', 'kill_house_operator', 'info'] def get_info(self, obj): role = self.context.get('request').GET['role'] 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() free_sale_bars = KillHouseFreeSaleBarInformation.objects.filter(kill_house=obj, date__date__gte=date1, date__date__lte=date2, trash=False) buyers = OutProvinceCarcassesBuyer.objects.filter(Kill_house=obj, trash=False) total_carcasses = free_sale_bars.aggregate(total=Sum('number_of_carcasses'))[ 'total'] or 0 total_weight = free_sale_bars.aggregate(total=Sum('weight_of_carcasses'))[ 'total'] or 0 return { "number_of_buyers": len(buyers), "total_number_of_carcasses": total_carcasses, "total_weight_of_carcasses": total_weight, } class KillHouseForFreeSaleBarInformationViewSetSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) info = serializers.SerializerMethodField('get_info') class Meta: model = KillHouse fields = ['key', 'name', 'kill_house_operator', 'info'] def get_info(self, obj): free_sale_bars = KillHouseFreeSaleBarInformation.objects.filter(kill_house=obj, trash=False) buyers = OutProvinceCarcassesBuyer.objects.filter(Kill_house=obj, trash=False) total_carcasses = free_sale_bars.aggregate(total=Sum('number_of_carcasses'))[ 'total'] or 0 total_weight = free_sale_bars.aggregate(total=Sum('weight_of_carcasses'))[ 'total'] or 0 return { "number_of_buyers": len(buyers), "total_number_of_carcasses": total_carcasses, "total_weight_of_carcasses": total_weight, } class GeneralKillHouseSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForKillHouseDriverSerializer(read_only=True) class Meta: model = KillHouse fields = ['key', 'name', 'killer', 'kill_house_operator', 'union_gateway_percent', 'company_gateway_percent', 'guilds_gateway_percent', 'other_gateway_percent'] class PosGeneralKillHouseSerializer(serializers.ModelSerializer): class Meta: model = KillHouse fields = ['key', 'name', 'killer'] # سریالایزر مربوط به کشتارگاه class KillHouseSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorSerializer(read_only=True) system_address = SystemAddressSerializer(read_only=True) allow_state = serializers.SerializerMethodField('get_register_state') allow_direct_buying = serializers.SerializerMethodField('get_allow_direct_buying') kill_place = serializers.SerializerMethodField('get_kill_place') debt = serializers.SerializerMethodField('get_debt') # kill_request_quantity = serializers.SerializerMethodField('get_kill_request_remain_quantity') class Meta: model = KillHouse exclude = ( 'id', 'trash', 'created_by', 'modified_by', ) def get_kill_place(self, instance): place = None percentage = KillHousePercentage.objects.filter(kill_house=instance) if percentage.count() > 0: percentage = percentage.last() if percentage.kill_house_for_killer != None: place = percentage.kill_house_for_killer.name else: place = percentage.kill_house.name return place def get_allow_direct_buying(self, obj): allow_state = False allow = ProvinceAllowKillHouseDirectBuying.objects.filter(kill_house=obj) if allow.count() > 0: allow_state = allow.last().allow return allow_state def get_register_state(self, instance): allow_state = None allow = ProvinceAllowKillHouseRegisterCar.objects.filter(kill_house=instance) if allow.count() > 0: allow_state = allow.last().allow return allow_state def get_debt(self, instance): debt = False if KillHouseFactorToProvince.objects.filter( province_factor__province_check_info__kill_house_assignment__kill_house_request__killhouse_user=instance, state='pending').exists(): debt = True return debt # def get_kill_request_remain_quantity(self, obj): # quantity_sum = 0 # kill_req_key = None # now = datetime.datetime.now().date() # kill_request = KillRequest.objects.filter(kill_house=obj, recive_date__year=now.year, # recive_date__month=now.month, recive_date__day=now.day, # trash=False).last() # # if kill_request: # quantity_sum = kill_request.remain_quantity_for_poultry # kill_req_key = kill_request.key # # return { # "quantity_sum": quantity_sum, # "kill_req_key": kill_req_key, # } class PosKillHouseSerializer(serializers.ModelSerializer): shop_name = serializers.CharField(source='name', read_only=True) fullname = serializers.CharField(source='kill_house_operator.user.fullname', read_only=True) mobile = serializers.CharField(source='kill_house_operator.user.mobile', read_only=True) class Meta: model = KillHouse fields = ['key', 'shop_name', 'fullname', 'mobile'] class PspKillHouseSerializer(serializers.ModelSerializer): unit_name = serializers.CharField(source='name', read_only=True) fullname = serializers.CharField(source='kill_house_operator.user.fullname', read_only=True) city = serializers.CharField(source='kill_house_operator.user.city.name', read_only=True) mobile = serializers.CharField(source='kill_house_operator.user.mobile', read_only=True) class Meta: model = KillHouse fields = ['key', 'unit_name', 'fullname', 'mobile','city','killer'] class distributionKillHouseSerializer(serializers.ModelSerializer): shop_name = serializers.CharField(source='name', read_only=True) fullname = serializers.CharField(source='kill_house_operator.user.fullname', read_only=True) mobile = serializers.CharField(source='kill_house_operator.user.mobile', read_only=True) shop_type = serializers.SerializerMethodField('get_type') class Meta: model = KillHouse fields = ['key', 'shop_name', 'fullname', 'mobile', 'shop_type'] def get_type(self, obj): obj_type = 'کشتارکن' if obj.killer else 'کشتارگاه' return obj_type class KillHouseAllowVetSerializer(serializers.ModelSerializer): kill_house = KillHouseSerializer(read_only=True) class Meta: model = KillHouseAllowVet fields = '__all__' class KillHousePercentageSerializer(serializers.ModelSerializer): kill_house = KillHouseForKillHousePercentageSerializer(read_only=True) kill_house_for_killer = KillHouseForProvinceSerializer(read_only=True) kill_house_vet = serializers.SerializerMethodField('get_kill_house_vet') # kill_house = KillHouseSerializer(read_only=True) # kill_house_for_killer = KillHouseSerializer(read_only=True) class Meta: model = KillHousePercentage fields = ['key', 'guilds_quantity', 'guilds_weight', 'kill_house', 'kill_house_for_killer', 'percent', 'last_guilds_update_date', 'kill_house_vet'] # fields = '__all__' def get_kill_house_vet(self, obj): if obj.kill_house_for_killer != None: kill_house_vet = KillHouseVet.objects.filter(kill_house=obj.kill_house_for_killer, trash=False).last() else: kill_house_vet = KillHouseVet.objects.filter(kill_house=obj.kill_house, trash=False).last() if kill_house_vet != None: serializer = KillHouseVetForKillHousePercentageSerializer(kill_house_vet) return serializer.data return None class KillHousePercentageForKillerKillHousseSerializer(serializers.ModelSerializer): kill_house = KillHouseForAutoAllocationSerializer(read_only=True) # kill_house_for_killer = KillHouseForAutoAllocationSerializer(read_only=True) class Meta: model = KillHousePercentage fields = ['kill_house'] # fields = ['kill_house_for_killer'] class KillHouseADDCARForKillHouseDriverSerializer(serializers.ModelSerializer): kill_house = KillHouseForKillHouseDriverSerializer(read_only=True) class Meta: model = KillHouseADDCAR fields = ['kill_house'] class KillHouseDriverForAllUserSerializer(serializers.ModelSerializer): user_bank_info = BankCardSerializer(read_only=True, required=False) byers = serializers.SerializerMethodField('get_buyers') class Meta: model = KillHouseDriver exclude = ( 'id', 'trash', 'created_by', 'modified_by', 'create_date', 'modify_date', 'status', 'wallet', 'wallet_amount', 'registrar', 'user', 'weight_without_load', ) def get_buyers(self, obj): cars = KillHouseADDCAR.objects.filter(driver__exact=obj) return KillHouseADDCARForKillHouseDriverSerializer(cars, many=True).data # سریالایزر مربوط به اضافه کردن راننده به کشتارگاه class KillHouseDriverSerializer(serializers.ModelSerializer): user = SystemUserProfileSerializer(read_only=True, required=False) user_bank_info = BankCardSerializer(read_only=True, required=False) first_name_last_name = serializers.SerializerMethodField('get_first_name_last_name') class Meta: model = KillHouseDriver fields = '__all__' extra_kwargs = { 'capocity': {'required': False}, 'weight_without_load': {'required': False} } def get_first_name_last_name(self, instance): user_dict = {} user = SystemUserProfile.objects.get( key=instance.user.key) user_dict = { "first_name": user.first_name, "last_name": user.last_name, } return user_dict class KillHouseDriverForBarManagementSerializer(serializers.ModelSerializer): class Meta: model = KillHouseDriver fields = ['driver_name', 'driver_mobile', 'type_car', 'pelak', 'health_code'] # سریالایزر مربوط به اضافه کردن خودرو به کشتارگاه class KillHouseADDCARSerializer(serializers.ModelSerializer): kill_house = KillHouseSerializer(read_only=True) driver = KillHouseDriverSerializer(read_only=True) class Meta: model = KillHouseADDCAR exclude = ( 'id', 'trash', 'created_by', 'modified_by', ) class KillHouseADDCARForBarManagementSerializer(serializers.ModelSerializer): driver = KillHouseDriverForBarManagementSerializer(read_only=True) class Meta: model = KillHouseADDCAR fields = ['driver'] class VetForBarManagementSerializer(serializers.ModelSerializer): user = SystemUserProfileForAutoAllocationSerializer(read_only=True) class Meta: model = Vet fields = ['user'] class VetFarmForBarManagementSerializer(serializers.ModelSerializer): vet = VetForBarManagementSerializer(read_only=True) class Meta: model = VetFarm fields = ['vet'] # سریالایزر مربوط به ثبت نماینده کشتارگاه class VetSerializer(serializers.ModelSerializer): user = SystemUserProfileSerializer(read_only=True) address = SystemAddressSerializer(read_only=True) user_bank_info = BankCardSerializer(read_only=True) farms = serializers.SerializerMethodField('get_farms') kill_houses = serializers.SerializerMethodField('get_kill_houses') class Meta: model = Vet exclude = ( 'id', 'create_date', 'modify_date', 'created_by', 'modified_by', ) def get_farms(self, instance): farms_list = [] farms = VetFarm.objects.filter(vet=instance, trash=False).select_related('poultry', 'vet') if farms.count() > 0: for farm in farms: internal_dict = { "poultry_name": farm.poultry.unit_name, "poultry_full_name": farm.poultry.user.fullname, "poultry_mobile": farm.poultry.user.mobile, "poultry_hall": farm.hall, "vet_id": farm.vet.id, "vet_farm_key": farm.key, } farms_list.append(internal_dict) return farms_list def get_kill_houses(self, instance): kill_houses_list = [] kill_house_vets = KillHouseVet.objects.filter(vet=instance, trash=False).select_related( 'kill_house__kill_house_operator__user') if kill_house_vets.count() > 0: for kill_house_vet in kill_house_vets: internal_dict = { "Kill_house_key": kill_house_vet.kill_house.key, "Kill_house_name": kill_house_vet.kill_house.name, "Kill_house_user_full_name": kill_house_vet.kill_house.kill_house_operator.user.fullname, "Kill_house_user_mobile": kill_house_vet.kill_house.kill_house_operator.user.mobile, } kill_houses_list.append(internal_dict) return kill_houses_list class KillHouseVetSerializer(serializers.ModelSerializer): vet = VetSerializer(read_only=True) kill_house = KillHouseSerializer(read_only=True) class Meta: model = KillHouseVet fields = '__all__' class VetForKillRequestSerializer(serializers.ModelSerializer): user = SystemUserProfileForAutoAllocationSerializer(read_only=True) class Meta: model = Vet fields = ['user'] class KillHouseVetForKillRequestSerializer(serializers.ModelSerializer): vet = VetForKillRequestSerializer(read_only=True) kill_house = KillHouseForAutoAllocationSerializer(read_only=True) class Meta: model = KillHouseVet fields = '__all__' class KillHouseVetForKillHousePercentageSerializer(serializers.ModelSerializer): vet = VetForKillRequestSerializer(read_only=True) class Meta: model = KillHouseVet fields = ['vet'] class KillRequestForBarManagementSerializer(serializers.ModelSerializer): class Meta: model = KillRequest fields = ['recive_date'] # سریالایزر مربوط به ثبت درخواست کشتار توسط کشتارگاه class KillRequestSerializer(serializers.ModelSerializer): kill_house = KillHouseForAutoAllocationSerializer(read_only=True) poultry = PoultrySerializer(read_only=True) slaughter_house = KillHouseForAutoAllocationSerializer(read_only=True) # kill_house = KillHouseSerializer(read_only=True) # slaughter_house = KillHouseSerializer(read_only=True) kill_house_vet = serializers.SerializerMethodField('get_kill_house_vet') number_of_allocated = serializers.SerializerMethodField('get_number_of_allocated') poultry_hatching = PoultryHatchingSerializer(read_only=True) # kill_house_vet = serializers.SerializerMethodField('get_kill_house_vet') # number_of_allocated = serializers.SerializerMethodField('get_number_of_allocated') class Meta: model = KillRequest fields = '__all__' # def get_kill_house_vet(self, instance): # vet = None # kill_house_vet = KillHouseVet.objects.filter(kill_house=instance.kill_house, trash=False) # if kill_house_vet.count() > 0: # kill_house_vet = kill_house_vet.last() # vet = { # "kill_house_vet_full_name": kill_house_vet.vet.user.fullname, # "kill_house_vet_mobile": kill_house_vet.vet.user.mobile, # } # # return vet def get_kill_house_vet(self, obj): kill_house_vet = KillHouseVet.objects.filter(kill_house__exact=obj.kill_house, trash=False).last() if kill_house_vet: return KillHouseVetForKillRequestSerializer(kill_house_vet).data else: return None # # def get_number_of_allocated(self, instance): # quantity = 0 # province_kill_request = ProvinceKillRequest.objects.filter(kill_request=instance, # state__in=('pending', 'accepted'), # trash=False) # if province_kill_request.count() > 0: # quantity = province_kill_request.count() # # return quantity def get_number_of_allocated(self, instance): return instance.kill_capacity - instance.remain_quantity class KillHouseDailyQuotaSerializer(serializers.ModelSerializer): kill_house = KillHouseSerializer(read_only=True) killer_kill_house = KillHouseSerializer(read_only=True) kill_request = KillRequestSerializer(read_only=True) class Meta: model = KillHouseDailyQuota fields = '__all__' class AutomaticKillRequestSerializer(serializers.ModelSerializer): class Meta: model = AutomaticKillRequest fields = '__all__' class KillRequestFactorSerializer(serializers.ModelSerializer): kill_request = KillRequestSerializer(read_only=True) class Meta: model = KillRequestFactor fields = '__all__' class KillRequestFactorPaymentSerializer(serializers.ModelSerializer): kill_request_factor = KillRequestFactorSerializer(read_only=True) class Meta: model = KillRequestFactorPayment fields = '__all__' class KillHouseRequestForColdHouseAllocationsSerializer(serializers.ModelSerializer): kill_house_request_info = serializers.SerializerMethodField('get_kill_house_request_info') class Meta: model = KillHouseRequest fields = ['key', 'kill_house_request_info'] def get_kill_house_request_info(self, obj): return { "poultry_fullname": obj.province_request.poultry_request.poultry.user.fullname, "poultry_mobile": obj.province_request.poultry_request.poultry.user.mobile, "poultry_name": obj.province_request.poultry_request.poultry.unit_name, "kill_house_name": obj.killhouse_user.name, "kill_house_fullname": obj.killhouse_user.kill_house_operator.user.fullname, "kill_house_mobile": obj.killhouse_user.kill_house_operator.user.mobile, "quantity": obj.ware_house_accepted_real_quantity, "weight": obj.ware_house_accepted_real_weight, } # سریالایزر مربوط به ثبت درخواست کشتار بعد از تخصیص استان class KillHouseRequestSerializer(serializers.ModelSerializer): killhouse_user = KillHouseSerializer(read_only=True) kill_request = KillRequestSerializer(read_only=True) add_car = KillHouseADDCARSerializer(read_only=True) poultry = serializers.SerializerMethodField('get_poultry') class Meta: model = KillHouseRequest fields = '__all__' def get_poultry(self, instance): poultry = Poultry.objects.get( key=instance.province_kill_request.province_request.poultry_request.poultry.key) poultry_dict = { "poultry_name": poultry.unit_name, "poultry_mobile": poultry.user.mobile, "poultry_address": poultry.address.address, "poultry_city": poultry.address.city.name, "poultry_province": poultry.address.province.name, } return poultry_dict class KillHouseRequestForDiffrentBarInfoSerializer(serializers.ModelSerializer): killhouse_user = KillHouseForAutoAllocationSerializer(read_only=True) kill_request = KillRequestForBarManagementSerializer(read_only=True) add_car = KillHouseADDCARForBarManagementSerializer(read_only=True) weight_info = serializers.SerializerMethodField('get_weight_info') class Meta: model = KillHouseRequest exclude = ( 'id', 'create_date', 'modify_date', # 'trash', 'created_by', 'modified_by', 'killhouse_user_id_foreign_key', 'kill_request_id_foreign_key', 'province_request_id_foreign_key', 'province_kill_request_id_foreign_key', 'add_car_id_foreign_key', 'kill_house_request_id_key', 'capacity', 'kill_house_request_auction_winner', 'city_number', 'city_name', 'province_number', 'province_name', 'province_request', 'province_kill_request', ) def get_weight_info(self, obj): index_weight = 0 weight = 0 assignment_info = KillHouseAssignmentInformation.objects.filter( kill_house_request=obj).last() assingment_quantity = assignment_info.real_quantity assingment_weight = assignment_info.net_weight kill_house_vet_quantity = obj.vet_accepted_real_quantity kill_house_vet_weight = obj.vet_accepted_real_weight quantity = obj.quantity weight = int(obj.quantity * obj.province_request.poultry_request.Index_weight) return {"assingment_quantity": assingment_quantity, "assingment_weight": assingment_weight, "kill_house_vet_quantity": kill_house_vet_quantity, "kill_house_vet_weight": kill_house_vet_weight, "quantity": quantity, "weight": weight, } class KillHouseRequestForBarManagementSerializer(serializers.ModelSerializer): killhouse_user = KillHouseForAutoAllocationSerializer(read_only=True) killer = KillHouseForAutoAllocationSerializer(read_only=True) bar_document_status = BarDocumentStatusSerializer(read_only=True) kill_request = KillRequestForBarManagementSerializer(read_only=True) add_car = KillHouseADDCARForBarManagementSerializer(read_only=True) real_add_car = KillHouseADDCARForBarManagementSerializer(read_only=True) poultry_request = serializers.SerializerMethodField('get_poultry_request') kill_place = serializers.SerializerMethodField('get_kill_place') vet_farm = serializers.SerializerMethodField('get_vet_farm') kill_house_vet = serializers.SerializerMethodField('get_kill_house_vet') weight_info = serializers.SerializerMethodField('get_weight_info') assignment_info = serializers.SerializerMethodField('get_assignment_info') final_bar_state = serializers.SerializerMethodField('get_final_bar_state') class Meta: model = KillHouseRequest exclude = ( 'id', # 'create_date', # 'modify_date', # 'trash', 'created_by', 'modified_by', 'killhouse_user_id_foreign_key', 'kill_request_id_foreign_key', 'province_request_id_foreign_key', 'province_kill_request_id_foreign_key', 'add_car_id_foreign_key', 'kill_house_request_id_key', 'capacity', 'kill_house_request_auction_winner', 'city_number', 'city_name', 'province_number', 'province_name', 'province_request', 'province_kill_request', ) def get_poultry_request(self, obj): poultry_request = PoultryRequest.objects.get( key=obj.province_kill_request.province_request.poultry_request.key) serializer = PoultryRequestForBarMangementSerializer(poultry_request) return serializer.data def get_kill_place(self, obj): kill_place = obj.kill_request.slaughter_house.name if obj.kill_request.slaughter_house != None else obj.kill_request.kill_house.name return kill_place def get_vet_farm(self, obj): vet_farm = VetFarm.objects.filter( poultry=obj.province_kill_request.province_request.poultry_request.poultry).last() serializer = VetFarmForBarManagementSerializer(vet_farm) return serializer.data def get_kill_house_vet(self, obj): data = None kill_request = KillRequest.objects.get(key=obj.kill_request.key) if kill_request.slaughter_house != None: kill_house_vet = KillHouseVet.objects.filter(kill_house=kill_request.slaughter_house).select_related( 'vet__user').first() else: kill_house_vet = KillHouseVet.objects.filter(kill_house=kill_request.kill_house).select_related( 'vet__user').first() if kill_house_vet: data = { "fullname": kill_house_vet.vet.user.fullname, "mobile": kill_house_vet.vet.user.mobile, } return data def get_weight_info(self, obj): first_index_weight = round((obj.quantity * obj.province_request.poultry_request.Index_weight) / obj.quantity, 1) if obj.quantity > 0 else 0 first_weight = int(obj.quantity * obj.province_request.poultry_request.Index_weight) final_index_weight = round(obj.accepted_real_weight / obj.accepted_real_quantity, 1) if obj.accepted_real_quantity > 0 else 0 kill_house_price = obj.province_kill_request.kill_house_price weight_loss = obj.accepted_real_weight - obj.ware_house_accepted_real_weight if obj.ware_house_accepted_real_weight \ < obj.accepted_real_weight and obj.ware_house_accepted_real_weight != 0 else 0 total_weight_loss = (weight_loss / obj.accepted_real_weight) * 100 if weight_loss != 0 else 0 initial_to_quarantine_ratio = round((obj.quarantine_quantity / obj.quantity) * 100, 1) if obj.quantity > 0 and obj.quarantine_quantity else 0 quarantine_to_final_ratio = round((obj.quarantine_quantity / obj.accepted_real_quantity) * 100, 1) if obj.accepted_real_quantity > 0 and obj.quarantine_quantity else 0 return {"index_weight": first_index_weight, "weight": first_weight, "final_index_weight": final_index_weight, "kill_house_price": kill_house_price, "weight_loss": round(total_weight_loss, 1), "input_loss": obj.weight_loss, "initial_to_quarantine_ratio": initial_to_quarantine_ratio, "quarantine_to_final_ratio": quarantine_to_final_ratio, } def get_assignment_info(self, obj): assignment_state = False state = None assignment_key = None net_weight = 0 real_quantity = 0 car_weight_without_load = 0 car_weight_with_load = 0 image_with_bar = image_without_bar = None assignment = KillHouseAssignmentInformation.objects.filter(trash=False, kill_house_request=obj).first() if assignment: assignment_state = True image_without_bar = assignment.car_weight_without_load_image image_with_bar = assignment.car_weight_with_load_image car_weight_without_load = assignment.car_weight_without_load car_weight_with_load = assignment.car_weight_with_load net_weight = assignment.net_weight real_quantity = assignment.real_quantity state = assignment.state assignment_key = assignment.key return {"assignment_state": assignment_state, "image_without_bar": image_without_bar, "image_with_bar": image_with_bar, "car_weight_without_load": car_weight_without_load, "car_weight_with_load": car_weight_with_load, "net_weight": net_weight, "state": state, "real_quantity": real_quantity, "assignment_key": assignment_key } def get_final_bar_state(self, obj): final_state = 'در انتظار ورود اطلاعات' if obj.ware_house_confirmation and obj.assignment_state_archive == 'True': final_state = 'تکمیل شده' elif obj.assignment_state_archive == 'True' and not obj.ware_house_confirmation: final_state = 'در انتظار ورود به انبار' elif obj.assignment_state_archive == 'pending' and obj.ware_house_confirmation: final_state = 'در انتظار ورود اطلاعات بار' else: final_state = 'در انتظار ورود اطلاعات' return final_state class KillHouseRequestForInputBarsSerializer(serializers.ModelSerializer): killhouse_user = KillHouseForAutoAllocationSerializer(read_only=True) killer = KillHouseForAutoAllocationSerializer(read_only=True) add_car = KillHouseADDCARForBarManagementSerializer(read_only=True) poultry_request = serializers.SerializerMethodField('get_poultry_request') weight_info = serializers.SerializerMethodField('get_weight_info') class Meta: model = KillHouseRequest exclude = ( 'id', # 'create_date', 'modify_date', # 'trash', 'created_by', 'modified_by', 'killhouse_user_id_foreign_key', 'kill_request_id_foreign_key', 'province_request_id_foreign_key', 'province_kill_request_id_foreign_key', 'add_car_id_foreign_key', 'kill_house_request_id_key', 'capacity', 'kill_house_request_auction_winner', 'city_number', 'city_name', 'province_number', 'province_name', 'province_request', 'province_kill_request', ) def get_poultry_request(self, obj): poultry_request = PoultryRequest.objects.get( key=obj.province_kill_request.province_request.poultry_request.key) return { "poultry_req_order_code": poultry_request.order_code, "poultry_name": poultry_request.poultry.unit_name, "poultry_user_name": poultry_request.poultry.user.fullname, "poultry_mobile": poultry_request.poultry.user.mobile, "poultry_city": poultry_request.poultry.address.city.name, "chicken_breed": poultry_request.chicken_breed, "date": poultry_request.send_date, "freezing": poultry_request.freezing, "export": poultry_request.export, "free_sale_in_province": poultry_request.free_sale_in_province, "direct_buying": poultry_request.direct_buying, } # def get_vet_info(self, obj): # vet_farm_name = None # vet_farm_mobile = None # kill_house_vet_name = None # kill_house_vet_mobile = None # vet_farm = VetFarm.objects.filter( # poultry=obj.province_kill_request.province_request.poultry_request.poultry).last() # # if vet_farm: # vet_farm_name = vet_farm.vet.user.mobile # vet_farm_mobile = vet_farm.vet.user.fullname # kill_request = KillRequest.objects.get(key=obj.kill_request.key, trash=False) # if kill_request.slaughter_house != None: # kill_house_vet = KillHouseVet.objects.filter(kill_house=kill_request.slaughter_house).select_related( # 'vet__user').first() # if kill_house_vet: # kill_house_vet_name=kill_house_vet.vet.user.fullname # kill_house_vet_mobile=kill_house_vet.vet.user.mobile # else: # kill_house_vet = KillHouseVet.objects.filter(kill_house=kill_request.kill_house).select_related( # 'vet__user').first() # if kill_house_vet: # kill_house_vet_name=kill_house_vet.vet.user.fullname # kill_house_vet_mobile=kill_house_vet.vet.user.mobilee # data = { # "vet_farm_name": vet_farm_name, # "vet_farm_mobile": vet_farm_mobile, # "kill_house_vet_name": kill_house_vet_name, # "kill_house_vet_mobile": kill_house_vet_mobile, # } # return data def get_weight_info(self, obj): if obj.ware_house_confirmation == True: state = 'ورود به انبار' elif obj.assignment_state_archive == 'pending': state = 'در انتظار تکمیل اطلاعات' else: state = 'تکمیل اطلاعات' first_index_weight = round((obj.quantity * obj.province_request.poultry_request.Index_weight) / obj.quantity, 1) first_weight = int(obj.quantity * obj.province_request.poultry_request.Index_weight) final_index_weight = round(obj.accepted_real_weight / obj.accepted_real_quantity, 1) kill_house_price = obj.province_kill_request.kill_house_price weight_loss = obj.accepted_real_weight - obj.ware_house_accepted_real_weight if obj.ware_house_accepted_real_weight \ < obj.accepted_real_weight and obj.ware_house_accepted_real_weight != 0 else 0 total_weight_loss = (weight_loss / obj.accepted_real_weight) * 100 if weight_loss != 0 else 0 return {"index_weight": first_index_weight, "weight": first_weight, "final_index_weight": final_index_weight, "kill_house_price": kill_house_price, "weight_loss": round(total_weight_loss, 1), "input_loss": obj.weight_loss, "state": state, } # سریالایزر مربوط به تایید یا رد توسط کشتارگاه نسبت به تخصیص استان به کشتارگاه class KillHouseCheckRequestSerializer(serializers.ModelSerializer): kill_house_request = KillHouseRequestSerializer(read_only=True) class Meta: model = KillHouseCheckRequest fields = '__all__' # سریالایزر مربوط به ثبت اطلاعات بار توسط کشتارگاه class KillHouseRequestForCompletedLoadsSerializer(serializers.ModelSerializer): killhouse_user = KillHouseForAutoAllocationSerializer(read_only=True) add_car = KillHouseADDCARForBarManagementSerializer(read_only=True) real_add_car = KillHouseADDCARForBarManagementSerializer(read_only=True) kill_place = serializers.SerializerMethodField('get_kill_place') kill_house_vet = serializers.SerializerMethodField('get_kill_house_vet') first_weight = serializers.SerializerMethodField('get_first_weight') payment_remain_amount = serializers.SerializerMethodField('get_payment_remain_amount') class Meta: model = KillHouseRequest fields = ['key', 'killhouse_user', 'add_car', 'real_add_car', 'kill_place', 'quantity', 'bar_code', 'assignment_state_archive', 'clearance_code', 'traffic_code', 'accepted_real_quantity', 'accepted_real_weight', 'vet_state', 'kill_house_vet', 'first_weight', 'payment_remain_amount'] def get_kill_place(self, obj): kill_place = obj.kill_request.slaughter_house.name if obj.kill_request.slaughter_house != None else obj.kill_request.kill_house.name return kill_place def get_kill_house_vet(self, obj): kill_house_vet_fullname = None kill_house_vet_mobile = None vet_check_date = None if obj.vet_state == 'accepted': vet_check = VetCheckRequest.objects.filter(kill_house_request=obj).only('kill_house_request', 'create_date').first() vet_check_date = vet_check.create_date kill_house = obj.kill_request.slaughter_house if obj.kill_request.slaughter_house != None else obj.kill_request.kill_house kill_house_vet = KillHouseVet.objects.filter(kill_house=kill_house, trash=False).select_related( 'vet__user').first() if kill_house_vet: kill_house_vet_fullname = kill_house_vet.vet.user.fullname kill_house_vet_mobile = kill_house_vet.vet.user.mobile return { "kill_house_vet_fullname": kill_house_vet_fullname, "kill_house_vet_mobile": kill_house_vet_mobile, "vet_check_date": vet_check_date, } def get_first_weight(self, obj): weight = obj.province_kill_request.province_request.poultry_request.Index_weight * obj.quantity return weight def get_payment_remain_amount(self, obj): kill_house_payment = KillRequestFactorPayment.objects.filter( kill_request_factor__kill_request=obj.kill_request) if kill_house_payment.count() > 0: kill_request_payment_remain_amount = kill_house_payment.last().remain_amount else: kill_request_payment_remain_amount = 0 return { "kill_request_payment_remain_amount": kill_request_payment_remain_amount, "sms_payment": obj.kill_request.sms_payment, "buy_type": obj.province_kill_request.payment_type, } class KillHouseAssignmentInformationForCompletedLoadsSerializer(serializers.ModelSerializer): kill_house_request = KillHouseRequestForCompletedLoadsSerializer(read_only=True) poultry_request = serializers.SerializerMethodField('get_poultry_request') banks_information = serializers.SerializerMethodField('get_banks_information') class Meta: model = KillHouseAssignmentInformation fields = ['kill_house_request', 'poultry_request', 'banks_information', 'car_weight_without_load', 'car_weight_without_load_image', 'car_weight_with_load', 'car_weight_with_load_image', 'net_weight', 'state', 'real_quantity', 'protest_time'] def get_poultry_request(self, obj): poultry_request = PoultryRequest.objects.get( key=obj.kill_house_request.province_kill_request.province_request.city_request_Poultry.poultry_request.key) return { "poultry_province": poultry_request.poultry.user.province.name, "poultry_city": poultry_request.poultry.user.city.name, "order_code": poultry_request.order_code, "poultry_name": poultry_request.poultry.user.fullname, "unit_name": poultry_request.poultry.unit_name, "poultry_mobile": poultry_request.poultry.user.mobile, "poultry_request_quantity": poultry_request.quantity, "poultry_request_id": poultry_request.id, "age": (datetime.datetime.now() - poultry_request.hatching.date).days + 1, "index_weight": poultry_request.Index_weight, "chicken_breed": poultry_request.chicken_breed, "create_date": poultry_request.create_date, "send_date": poultry_request.send_date, "hatching_date": poultry_request.hatching.date, "cell_type": poultry_request.cell_type, "losses": poultry_request.hatching.losses, } def get_banks_information(self, obj): poultry_name_of_bank_user = None poultry_bank_name = None poultry_card = None poultry_account = None poultry_shaba = None province_name_of_bank_user = None province_bank_name = None province_card = None province_account = None province_shaba = None allocation = ShareOfAllocation.objects.all() if allocation.count() > 0: allocation = allocation.last().total else: allocation = 0 poultry = Poultry.objects.get( key=obj.kill_house_request.province_request.poultry_request.poultry.key, trash=False) if poultry.user_bank_info != None: poultry_name_of_bank_user = poultry.user_bank_info.name_of_bank_user poultry_bank_name = poultry.user_bank_info.bank_name poultry_card = poultry.user_bank_info.card poultry_account = poultry.user_bank_info.account poultry_shaba = poultry.user_bank_info.shaba operator = ProvinceOperator.objects.filter(trash=False) if operator.last().user_bank_info != None: operator = operator.last() province_name_of_bank_user = operator.user_bank_info.name_of_bank_user province_bank_name = operator.user_bank_info.bank_name province_card = operator.user_bank_info.shaba province_account = operator.user_bank_info.account province_shaba = operator.user_bank_info.shaba return { "poultry_name_of_bank_user": poultry_name_of_bank_user, "poultry_bank_name": poultry_bank_name, "poultry_card": poultry_card, "poultry_account": poultry_account, "poultry_shaba": poultry_shaba, "province_name_of_bank_user": province_name_of_bank_user, "province_bank_name": province_bank_name, "province_card": province_card, "province_account": province_account, "province_shaba": province_shaba, "province_wage": allocation, } class KillHouseAssignmentInformationSerializer(serializers.ModelSerializer): kill_house_request = KillHouseRequestSerializer(read_only=True) class Meta: model = KillHouseAssignmentInformation fields = '__all__' class KillHouseAssignmentInformationForAggregateLoadsSerializer(serializers.ModelSerializer): class Meta: model = KillHouseAssignmentInformation fields = ['key', 'car_weight_without_load', 'car_weight_without_load_image', 'car_weight_with_load', 'car_weight_with_load_image', 'net_weight', 'real_quantity'] class KillHouseRequestForAggregateLoadsSerializer(serializers.ModelSerializer): killhouse_user = KillHouseForAutoAllocationSerializer(read_only=True) killer = KillHouseForAutoAllocationSerializer(read_only=True) add_car = KillHouseADDCARForBarManagementSerializer(read_only=True) poultry_request = serializers.SerializerMethodField('get_poultry_request') kill_place = serializers.SerializerMethodField('get_kill_place') assingment_information = serializers.SerializerMethodField('get_assingment_information') first_weight = serializers.SerializerMethodField('get_first_weight') class Meta: model = KillHouseRequest fields = ['key', 'killhouse_user', 'add_car', 'poultry_request', 'kill_place', 'assingment_information', 'quantity', 'bar_code', 'assignment_state_archive', 'clearance_code', 'traffic_code', 'accepted_real_quantity', 'accepted_real_weight', 'vet_state', 'first_weight', 'killer'] def get_poultry_request(self, obj): poultry_request = PoultryRequest.objects.get( key=obj.province_kill_request.province_request.poultry_request.key) serializer = PoultryRequestForBarMangementSerializer(poultry_request) return serializer.data def get_kill_place(self, obj): kill_place = obj.kill_request.slaughter_house.name if obj.kill_request.slaughter_house != None else obj.kill_request.kill_house.name return kill_place def get_assingment_information(self, obj): assignment = KillHouseAssignmentInformation.objects.filter(kill_house_request=obj).first() serializer = KillHouseAssignmentInformationForAggregateLoadsSerializer(assignment) return serializer.data def get_first_weight(self, obj): weight = obj.province_kill_request.province_request.poultry_request.Index_weight * obj.quantity return weight class KillHouseAssignmentImagesSerializer(serializers.ModelSerializer): kill_house = KillHouseSerializer(read_only=True) class Meta: model = KillHouseAssignmentImages fields = '__all__' class VetForAllUserSerializer(serializers.ModelSerializer): address = SystemAddressSerializer(read_only=True) user_bank_info = BankCardSerializer(read_only=True) class Meta: model = Vet fields = ['address', 'user_bank_info', 'identity_documents', 'active'] class KillHouseVetForKillHouseVetSerializer(serializers.ModelSerializer): kill_house = KillHouseForKillHouseVetKillHouseSerializer(read_only=True) class Meta: model = KillHouseVet fields = ['kill_house'] class KillHouseVetForAllUserSerializer(serializers.ModelSerializer): vet = VetForAllUserSerializer(read_only=True) # kill_house = KillHouseForAllUserSerializer(read_only=True) kill_houses = serializers.SerializerMethodField('get_kill_houses') class Meta: model = KillHouseVet fields = ['vet', 'kill_houses'] def get_kill_houses(self, obj): kill_houses = KillHouseVet.objects.filter(key=obj.key) return KillHouseVetForKillHouseVetSerializer(kill_houses, many=True).data # سریالایزر مربوط به ثبت اطلاعات بار توسط کشتارگاه class VetCheckRequestSerializer(serializers.ModelSerializer): class Meta: model = VetCheckRequest fields = '__all__' class ProvinceKillRequestForAutoAllocationSerializer(serializers.ModelSerializer): killhouse_user = KillHouseForAutoAllocationSerializer(read_only=True) class Meta: model = ProvinceKillRequest fields = ['killhouse_user', 'main_quantity'] # سریالایزر مربوط به تخصیص استان به کشتارگاه class ProvinceKillRequestSerializer(serializers.ModelSerializer): killhouse_user = KillHouseSerializer(read_only=True) kill_request = KillRequestSerializer(read_only=True) class Meta: model = ProvinceKillRequest fields = '__all__' class ProvinceKillRequestProvinceWageSerializer(serializers.ModelSerializer): province_request = serializers.SerializerMethodField('get_province_request') kill_house_requests = serializers.SerializerMethodField('get_kill_house_request') class Meta: model = ProvinceKillRequest fields = ['province_request', 'kill_house_requests', 'wage_pay', 'depositor', 'total_amount_editor', 'archive_by_province', 'archive_message', 'archiver'] def get_province_request(self, obj): role = self.context.get('request').GET['role'] kill_place = obj.kill_request.slaughter_house.name if obj.kill_request.slaughter_house != None else obj.kill_request.kill_house.name kill_house_user_full_name = obj.killhouse_user.kill_house_operator.user.fullname kill_house_user_mobile = obj.killhouse_user.kill_house_operator.user.mobile killer = obj.killhouse_user.killer province_request = ProvinceCheckOperatorRequest.objects.filter(key=obj.province_request.key, trash=False).select_related( 'poultry_request__poultry__user', 'poultry_request__poultry__address__city', 'poultry_request').last() if role in ['KillHouse', 'SuperAdmin', 'AdminX', 'Supporter']: total_amount = obj.total_wage_amount elif role == 'ProvinceOperator': total_amount = obj.union_share elif role == 'Company': total_amount = obj.company_share elif role == 'Company': total_amount = obj.guilds_share else: total_amount = obj.total_wage_amount return { "order_code": province_request.poultry_request.order_code, "poultry_fullname": province_request.poultry_request.poultry.user.fullname, "poultry_mobile": province_request.poultry_request.poultry.user.mobile, "poultry_city": province_request.poultry_request.poultry.address.city.name, "breed": province_request.poultry_request.chicken_breed, "index_weight": province_request.poultry_request.Index_weight, "send_date": province_request.poultry_request.send_date, "wage": obj.wage, "killer": killer, "kill_place": kill_place, "kill_house_user_full_name": kill_house_user_full_name, "kill_house_user_mobile": kill_house_user_mobile, "province_kill_request_quantity": obj.total_killed_quantity, "province_kill_request_key": obj.key, "province_kill_request_total_weight": obj.total_killed_weight, "total_amount": total_amount, "prev_total_amount": obj.prev_total_amount, # "prev_total_amount": wage * (obj.main_quantity * province_request.poultry_request.Index_weight) "free_sale": province_request.poultry_request.free_sale_in_province, } def get_kill_house_request(self, obj): kill_house_request = KillHouseRequest.objects.filter(province_kill_request=obj, trash=False) serializer = KillHouseRequestForBarManagementSerializer(kill_house_request, many=True) return serializer.data class KillRequestForDirectBuyingSerializer(serializers.ModelSerializer): kill_house = KillHouseForCommonlyUsedSerializer(read_only=True) slaughter_house = KillHouseForCommonlyUsedSerializer(read_only=True) poultry = PoultryForPredictionSerializer(read_only=True) poultry_request = PoultryRequestForMarketSerializer(read_only=True) # kill_house_vet = serializers.SerializerMethodField('get_kill_house_vet') class Meta: model = KillRequest fields = ['key', 'create_date', 'recive_date', 'free_direct_buying', 'chicken_breed', 'kill_house', 'slaughter_house', 'poultry', 'poultry_request', 'payment_deadline_date', 'payment_deadline_days', 'kill_capacity', 'Index_weight', 'amount', 'market_final_accept', 'market_state', 'recive_time', 'market_code_status', 'input_market_code', 'market_code','market_state_message'] # def get_kill_house_vet(self, obj): # kill_house_vet = KillHouseVet.objects.filter(kill_house__exact=obj.kill_house, trash=False).last() # if kill_house_vet: # return KillHouseVetForKillRequestSerializer(kill_house_vet).data # else: # return None class ProvinceKillRequestForDirectBuyingSerializer(serializers.ModelSerializer): kill_request = KillRequestForDirectBuyingSerializer(read_only=True) general_info = serializers.SerializerMethodField('get_general_info') class Meta: model = ProvinceKillRequest fields = ['key', 'kill_request', 'total_killed_quantity', 'total_killed_weight', 'general_info', 'payment_deadline_days', 'payment_deadline_date', 'payment_deadline', 'payment_deadline_archive', 'payment_deadline_state', 'payment_deadline_checker_fullname', 'payment_deadline_checker_mobile', 'payment_deadline_check_date', 'payment_deadline_archive_message', 'final_accept', 'extension_payment_deadline_days', 'payment_deadline_amount', 'extension_payment_deadline_days', 'extension_payment_deadline_date'] def get_general_info(self, obj): age = ((obj.kill_request.recive_date.date()) - (obj.kill_request.poultry_hatching.date.date())).days + 1 total_amount = int(obj.kill_request.amount * obj.total_killed_weight) payments = DirectBuyingPayment.objects.filter(province_kill_request=obj, trash=False) total_paid_amount = payments.aggregate(total=Sum('amount'))['total'] or 0 return { "age": age, "total_amount": total_amount, "Index_weight": round((obj.total_killed_weight / obj.total_killed_quantity), 2), "total_paid_amount": total_paid_amount } class ProvinceKillRequestForKillingInformationDiscrepancyReportSerializer(serializers.ModelSerializer): killhouse_user = KillHouseForKillingInformationDiscrepancyReportSerializer(read_only=True) total_info = serializers.SerializerMethodField('get_total_info') class Meta: model = ProvinceKillRequest fields = ['killhouse_user', 'total_info'] def get_total_info(self, obj): province_kill_request_quantity = obj.main_quantity province_kill_request_weight = int(obj.main_quantity * obj.province_request.poultry_request.Index_weight) province_kill_request_remain_quantity = obj.quantity province_kill_request_remain_weight = int(obj.quantity * obj.province_request.poultry_request.Index_weight) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_kill_request=obj).annotate( total_quantity=Sum('accepted_real_quantity'), total_weight=Sum('accepted_real_weight'), ) kill_house_request_quantity = kill_house_requests.aggregate(total=Sum('total_quantity'))['total'] kill_house_request_weight = kill_house_requests.aggregate(total=Sum('total_weight'))['total'] return { "province_kill_request_quantity": province_kill_request_quantity, "province_kill_request_weight": province_kill_request_weight, "province_kill_request_remain_quantity": province_kill_request_remain_quantity, "province_kill_request_remain_weight": province_kill_request_remain_weight, "kill_house_requests_count": kill_house_requests.count(), "kill_house_request_quantity": kill_house_request_quantity if kill_house_request_quantity != None else 0, "kill_house_request_weight": kill_house_request_weight if kill_house_request_weight != None else 0, "operator_fullname": obj.killhouse_user.kill_house_operator.user.fullname, "operator_mobile": obj.killhouse_user.kill_house_operator.user.mobile, "role": "KillHouse", } # سریالایزر مربوط به ثبت درخواست بورسی کشتارگاه class KillHouseRequestExchangeSerializer(serializers.ModelSerializer): user = UserProfileSerializer(read_only=True) class Meta: model = KillHouseRequestExchange fields = '__all__' # سریالایزر مربوط به ثبت درخواست رزرو بورسی کشتارگاه class KillHouseRequestExchangeReserveSerializer(serializers.ModelSerializer): poultry_exchange = PoultryRequestExchangeSerializer(read_only=True) class Meta: model = KillHouseRequestExchangeReserve fields = '__all__' # سریالایزر مربوط به ثبت درخواست رزرو بورسی کشتارگاه class KillHouseRequestActionSerializer(serializers.ModelSerializer): poultry_request = PoultryRequestSerializer(read_only=True) kill_house = KillHouseSerializer(read_only=True) winner = serializers.SerializerMethodField('get_winner') class Meta: model = KillHouseRequestAction fields = '__all__' def get_winner(self, instance): winners = KillHouseRequestActionWinner.objects.filter(kill_house_request_auction=instance) if winners.count() > 0: winners = winners.last() internal_dict = { "key": winners.key, "fee": winners.fee, "quantity": winners.quantity } return internal_dict # سریالایزر مربوط به ثبت درخواست رزرو بورسی کشتارگاه class KillHouseRequestActionWinnerSerializer(serializers.ModelSerializer): kill_house_request_auction = KillHouseRequestActionSerializer(read_only=True) class Meta: model = KillHouseRequestActionWinner fields = '__all__' # سریالایزر مربوط به اضافه کردن خودرو به درخواست بورسی کشتارگاه class KillHouseRequestExchangeAddCarSerializer(serializers.ModelSerializer): kill_house_reserve = KillHouseRequestExchangeReserveSerializer(read_only=True) class Meta: model = KillHouseRequestExchangeAddCar fields = '__all__' # سریالایزر مربوط به تایید یا رد درخواست بورسی کشتارگاه توسط مرغدار class PoultryRequestExchangeAcceptSerializer(serializers.ModelSerializer): poultry_request_exchange = PoultryRequestExchangeSerializer(read_only=True) kill_house_request_exchange_reserve = KillHouseRequestExchangeReserveSerializer(read_only=True) class Meta: model = PoultryRequestExchangeAccept fields = '__all__' class DriverRequestCancelSerializer(serializers.ModelSerializer): Kill_house_add_car = KillHouseADDCARSerializer(read_only=True) kill_house_request = KillHouseRequestSerializer(read_only=True) class Meta: model = DriverRequestCancel fields = '__all__' class KillHouseComplaintSerializer(serializers.ModelSerializer): bar = KillHouseAssignmentInformationSerializer(read_only=True) class Meta: model = KillHouseComplaint fields = '__all__' class CheckKillHouseComplaintSerializer(serializers.ModelSerializer): complaint = KillHouseComplaintSerializer(read_only=True) class Meta: model = CheckKillHouseComplaint fields = '__all__' class CheckUnusualCasualtiesSerializer(serializers.ModelSerializer): killHousecomplaint = KillHouseComplaintSerializer(read_only=True) class Meta: model = CheckUnusualCasualties fields = '__all__' class KillHouseCreditorsSerializer(serializers.ModelSerializer): kill_house = KillHouseSerializer(read_only=True) class Meta: model = KillHouseCreditors fields = '__all__' class KillHouseWareHouseForDailyBroadCastOInDetailsSerializer(serializers.ModelSerializer): informations = serializers.SerializerMethodField('get_informations') class Meta: model = KillHouse fields = ['informations', 'key'] def get_informations(self, obj): 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() kill_house_ware_houses = KillHouseWareHouse.objects.filter( Q(weight_of_carcasses__gt=0) | Q(free_weight_of_carcasses__gt=0) | Q( pre_cold_weight_of_carcasses_to_ware_house__gt=0), date__date__gte=date1, date__date__lte=date2, kill_house=obj, trash=False) steward_allocations = StewardAllocation.objects.filter( Q(type='manual') | Q(type='auto', system_registration_code=True), ware_house__in=kill_house_ware_houses, receiver_state__in=('pending', 'accepted'), trash=False).order_by( 'id') guild_allocations = steward_allocations.filter(guilds__isnull=False, trash=False) number_of_guild = guild_allocations.values('guilds').distinct().count() if guild_allocations else 0 steward_allocations_for_number_of_steward = steward_allocations.filter(steward__isnull=False, trash=False) number_of_steward = steward_allocations_for_number_of_steward.values( 'steward').distinct().count() if steward_allocations_for_number_of_steward else 0 total_pre_cold_quantity = \ kill_house_ware_houses.aggregate(total=Sum('pre_cold_number_of_carcasses_to_ware_house'))[ 'total'] total_pre_cold_weight = \ kill_house_ware_houses.aggregate(total=Sum('pre_cold_weight_of_carcasses_to_ware_house'))[ 'total'] total_number_of_free_carcasses = kill_house_ware_houses.aggregate(total=Sum('number_of_free_carcasses'))[ 'total'] total_free_weight_of_carcasses = kill_house_ware_houses.aggregate(total=Sum('free_weight_of_carcasses'))[ 'total'] total_number_of_carcasses = kill_house_ware_houses.aggregate(total=Sum('number_of_carcasses'))[ 'total'] total_weight_of_carcasses = kill_house_ware_houses.aggregate(total=Sum('weight_of_carcasses'))[ 'total'] final_total_number_of_carcasses = \ kill_house_ware_houses.aggregate(total=Sum('final_total_number_of_carcasses'))[ 'total'] final_total_weight_of_carcasses = \ kill_house_ware_houses.aggregate(total=Sum('final_total_weight_of_carcasses'))[ 'total'] total_remain_quantity = \ kill_house_ware_houses.aggregate(total=Sum('remain_total_number_of_carcasses'))[ 'total'] total_remain_weight = \ kill_house_ware_houses.aggregate(total=Sum('remain_total_weight_of_carcasses'))[ 'total'] total_allocated_quantity = \ steward_allocations.aggregate(total=Sum('real_number_of_carcasses'))[ 'total'] total_allocated_weight = \ steward_allocations.aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] total_accepted_allocated_quantity = \ steward_allocations.aggregate(total=Sum('receiver_real_number_of_carcasses'))[ 'total'] total_accepted_allocated_weight = \ steward_allocations.aggregate(total=Sum('receiver_real_weight_of_carcasses'))[ 'total'] return { "buyers": obj.name, "city": obj.kill_house_operator.user.city.name, "incoming_quantity_of_cold_house": 0, "incoming_weight_of_cold_house": 0, "total_pre_cold_quantity": total_pre_cold_quantity if total_pre_cold_quantity != None else 0, "total_pre_cold_weight": total_pre_cold_weight if total_pre_cold_weight != None else 0, "total_number_of_free_carcasses": total_number_of_free_carcasses if total_number_of_free_carcasses != None else 0, "total_free_weight_of_carcasses": total_free_weight_of_carcasses if total_free_weight_of_carcasses != None else 0, "total_number_of_carcasses": total_number_of_carcasses if total_number_of_carcasses != None else 0, "total_weight_of_carcasses": total_weight_of_carcasses if total_weight_of_carcasses != None else 0, "final_total_number_of_carcasses": final_total_number_of_carcasses if final_total_number_of_carcasses != None else 0, "final_total_weight_of_carcasses": final_total_weight_of_carcasses if final_total_weight_of_carcasses != None else 0, "total_allocated_quantity": total_allocated_quantity if total_allocated_quantity != None else 0, "total_allocated_weight": total_allocated_weight if total_allocated_weight != None else 0, "total_accepted_allocated_quantity": total_accepted_allocated_quantity if total_accepted_allocated_quantity != None else 0, "total_accepted_allocated_weight": total_accepted_allocated_weight if total_accepted_allocated_weight != None else 0, "total_remain_quantity": total_remain_quantity if total_remain_quantity != None else 0, "total_remain_weight": total_remain_weight if total_remain_weight != None else 0, "number_of_guild": number_of_guild, "number_of_steward": number_of_steward, } class KillHouseWareHouseSerializer(serializers.ModelSerializer): kill_house = KillHouseForAutoAllocationSerializer(read_only=True) class Meta: model = KillHouseWareHouse fields = '__all__' class KillHouseFreeBarInformationSerializer(serializers.ModelSerializer): kill_house = KillHouseForAutoAllocationSerializer(read_only=True) exclusive_killer = KillHouseForAutoAllocationSerializer(read_only=True) class Meta: model = KillHouseFreeBarInformation fields = '__all__' class KillHouseFreeBarInformationForWageTotalSerializer(serializers.ModelSerializer): kill_house = KillHouseForAutoAllocationSerializer(read_only=True) total_amount = serializers.SerializerMethodField('get_info') class Meta: model = KillHouseFreeBarInformation fields = '__all__' def get_info(self, obj): role = self.context.get('request').GET['role'] if role in ['KillHouse', 'SuperAdmin', 'AdminX', 'Supporter']: amount = obj.total_wage_amount elif role == 'ProvinceOperator': amount = obj.union_share elif role == 'Company': amount = obj.company_share elif role == 'Guilds': amount = obj.guilds_share else: amount = obj.total_wage_amount return amount class TypeActivityForKillHouseBuyerSerializer(serializers.ModelSerializer): class Meta: model = TypeActivity fields = ['key', 'title'] class OutProvinceCarcassesBuyerForKillHouseSerializer(serializers.ModelSerializer): type_activity = TypeActivityForKillHouseBuyerSerializer(read_only=True) Kill_house = KillHouseForAutoAllocationSerializer(read_only=True) class Meta: model = OutProvinceCarcassesBuyer exclude = ( 'id', 'create_date', 'modify_date', 'created_by', 'modified_by' ) class KillHouseFreeSaleBarInformationSerializer(serializers.ModelSerializer): kill_house = KillHouseForAutoAllocationSerializer(read_only=True) buyer = OutProvinceCarcassesBuyerForKillHouseSerializer(read_only=True) class Meta: model = KillHouseFreeSaleBarInformation fields = '__all__' class ProvinceKillRequestForLetterVetSerializer(serializers.ModelSerializer): killhouse_user = KillHouseForAutoAllocationSerializer(read_only=True) info = serializers.SerializerMethodField('get_info') class Meta: model = ProvinceKillRequest fields = ['killhouse_user', 'info'] def get_info(self, obj): if obj.kill_request.slaughter_house == None: kill_place = obj.kill_request.kill_house.name else: kill_place = obj.kill_request.slaughter_house.name return { "kill_place": kill_place, "quantity": obj.main_quantity, "weight": obj.main_quantity * obj.province_request.poultry_request.Index_weight, } class PoultryRequestoutProvinceSerializer(serializers.ModelSerializer): poultry = PoultryForPoultryRequestLetterSerializer(read_only=True) out_province_poultry_request_buyer = OutProvincePoultryRequestBuyerSerializer(read_only=True) vet_farm_name = serializers.CharField(source='vet_farm.user.fullname', read_only=True) vet_farm_mobile = serializers.CharField(source='vet_farm.user.mobile', read_only=True) hatching = serializers.SerializerMethodField('get_hatching') out_state = serializers.SerializerMethodField('get_out_state') total_system_quarantine_quantity = serializers.SerializerMethodField('get_total_system_quarantine_quantity') class Meta: model = PoultryRequest fields = ['key', 'out_state', 'hatching', 'create_date', 'poultry', 'order_code', 'quantity', 'remain_quantity', 'send_date', 'chicken_breed', 'financial_operation', 'Index_weight', 'amount', 'state_process', 'province_state', 'order_code', 'registrar', 'buyer', 'out', 'quarantine_code', 'final_state', 'out_province_request_cancel', 'out_province_request_canceller', 'out_province_driver_info', 'hatching_left_over', 'freezing', 'total_wage_amount', 'wage_pay', 'has_wage', 'payer_type', 'payer_fullname', 'out_province_poultry_request_buyer', 'buyer_mobile', 'buyer_fullname', 'buyer_city', 'buyer_province', 'agent', 'killer_kill_house_unit_name', 'killer_kill_house_city', 'killer_kill_house_province', 'kill_house_unique_id', 'payment_link', 'quarantine_quantity', 'total_system_quarantine_quantity','vet_farm_name','vet_farm_mobile','interface_number'] def get_hatching(self, obj): # if ProvinceKillRequest.objects.filter( # province_request__city_request_Poultry__poultry_request=obj, # state='accepted').exists(): # age = (obj.send_date - obj.hatching.date).days + 1 # else: # age = (datetime.datetime.now() - obj.hatching.date).days + 1 age = (obj.send_date - obj.hatching.date).days + 1 return { "age": age, "left_over": obj.hatching.left_over, "city": obj.poultry.address.city.name, "province": obj.poultry.address.province.name, "hatching_quantity": obj.hatching.quantity, } def get_out_state(self, obj): state = None if obj.province_state == 'rejected': state = 'رد شده' elif obj.province_state == 'pending' and obj.out_province_request_cancel == False: state = 'در انتظار تایید' elif obj.province_state == 'accepted' and obj.has_wage == True and obj.wage_pay == False and obj.out_province_request_cancel == False and obj.out_province_request_cancel == False: state = 'در انتظار پرداخت' elif obj.province_state == 'accepted' and obj.has_wage == False and obj.wage_pay == False and obj.out_province_request_cancel == False and obj.out_province_request_cancel == False: state = 'تایید شده' elif obj.province_state == 'accepted' and obj.has_wage == True and obj.wage_pay == True and obj.out_province_request_cancel == False and obj.out_province_request_cancel == False: state = 'پرداخت شده' else: state = 'لغو شده' return state def get_total_system_quarantine_quantity(self, obj): code = PoultryRequestQuarantineCode.objects.filter(trash=False, poultry_request=obj).aggregate( total=Sum('system_quarantine_quantity'))['total'] or 0 return code class PoultryRequestLetterTotalSerializer(serializers.ModelSerializer): poultry = PoultryForPoultryRequestLetterSerializer(read_only=True) class Meta: model = PoultryRequest fields = ['poultry', 'order_code'] class PoultryRequestLetterForProvinceVetSerializer(serializers.ModelSerializer): poultry = PoultryForBarManagementSerializer(read_only=True) hatching = serializers.SerializerMethodField('get_hatching') province_kill_requests = serializers.SerializerMethodField('get_province_kill_requests') class Meta: model = PoultryRequest fields = ['key', 'id', 'poultry', 'order_code', 'send_date', 'province_kill_requests', 'quantity', 'Index_weight', 'hatching'] def get_hatching(self, obj): vet_farm = VetFarm.objects.filter(trash=False, poultry=obj.poultry).first() pricing = Pricing.objects.filter(trash=False).last() vet_farm_name = vet_farm.vet.user.fullname if vet_farm else None vet_farm_mobile = vet_farm.vet.user.mobile if vet_farm else None age = (datetime.datetime.now() - obj.hatching.date).days + 1 return { "vet_farm_name": vet_farm_name, "vet_farm_mobile": vet_farm_mobile, "age": age, "hatching_quantity": obj.hatching.quantity, "price": pricing.live_chicken_price if pricing else None, } def get_province_kill_requests(self, obj): province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request=obj) serializer = ProvinceKillRequestForLetterVetSerializer(province_kill_requests, many=True) return serializer.data class KillHouseRequestForTotalInformationSerializer(serializers.ModelSerializer): add_car = KillHouseADDCARForBarManagementSerializer(read_only=True) killhouse_user = KillHouseForAutoAllocationSerializer(read_only=True) kill_place = serializers.SerializerMethodField('get_kill_place') vet_farm = serializers.SerializerMethodField('get_vet_farm') kill_house_vet = serializers.SerializerMethodField('get_kill_house_vet') weight_info = serializers.SerializerMethodField('get_weight_info') class Meta: model = KillHouseRequest exclude = ( 'id', 'create_date', 'modify_date', # 'trash', 'created_by', 'modified_by', 'killhouse_user_id_foreign_key', 'kill_request_id_foreign_key', 'province_request_id_foreign_key', 'province_kill_request_id_foreign_key', 'add_car_id_foreign_key', 'kill_house_request_id_key', 'capacity', 'kill_house_request_auction_winner', 'city_number', 'city_name', 'province_number', 'province_name', 'province_request', 'province_kill_request', ) def get_kill_place(self, obj): kill_place = obj.kill_request.slaughter_house.name if obj.kill_request.slaughter_house != None else obj.kill_request.kill_house.name return kill_place def get_vet_farm(self, obj): vet_farm = VetFarm.objects.filter( poultry=obj.province_kill_request.province_request.poultry_request.poultry).last() serializer = VetFarmForBarManagementSerializer(vet_farm) return serializer.data def get_kill_house_vet(self, obj): data = None kill_request = KillRequest.objects.get(key=obj.kill_request.key, trash=False) if kill_request.slaughter_house != None: kill_house_vet = KillHouseVet.objects.filter(kill_house=kill_request.slaughter_house).select_related( 'vet__user').first() else: kill_house_vet = KillHouseVet.objects.filter(kill_house=kill_request.kill_house).select_related( 'vet__user').first() if kill_house_vet: data = { "fullname": kill_house_vet.vet.user.fullname, "mobile": kill_house_vet.vet.user.mobile, } return data def get_weight_info(self, obj): return { "weight": int(obj.quantity * obj.province_request.poultry_request.Index_weight), "index_weight": round(int(obj.quantity * obj.province_request.poultry_request.Index_weight) / obj.quantity, 2), } class KillHouseRequestForTotalInformationInTableSerializer(serializers.ModelSerializer): add_car = KillHouseADDCARForBarManagementSerializer(read_only=True) killhouse_user = KillHouseForAutoAllocationSerializer(read_only=True) kill_place = serializers.SerializerMethodField('get_kill_place') vet_farm = serializers.SerializerMethodField('get_vet_farm') kill_house_vet = serializers.SerializerMethodField('get_kill_house_vet') weight_info = serializers.SerializerMethodField('get_weight_info') vet_check = serializers.SerializerMethodField('get_vet_check') assignment = serializers.SerializerMethodField('get_assignment') class Meta: model = KillHouseRequest exclude = ( 'id', 'create_date', 'modify_date', # 'trash', 'created_by', 'modified_by', 'killhouse_user_id_foreign_key', 'kill_request_id_foreign_key', 'province_request_id_foreign_key', 'province_kill_request_id_foreign_key', 'add_car_id_foreign_key', 'kill_house_request_id_key', 'capacity', 'kill_house_request_auction_winner', 'city_number', 'city_name', 'province_number', 'province_name', 'province_request', 'province_kill_request', ) def get_kill_place(self, obj): kill_place = obj.kill_request.slaughter_house.name if obj.kill_request.slaughter_house != None else obj.kill_request.kill_house.name return kill_place def get_vet_farm(self, obj): vet_farm = VetFarm.objects.filter( poultry=obj.province_kill_request.province_request.poultry_request.poultry).last() serializer = VetFarmForBarManagementSerializer(vet_farm) return serializer.data def get_kill_house_vet(self, obj): data = None kill_request = KillRequest.objects.get(key=obj.kill_request.key, trash=False) if kill_request.slaughter_house != None: kill_house_vet = KillHouseVet.objects.filter(kill_house=kill_request.slaughter_house).select_related( 'vet__user').first() else: kill_house_vet = KillHouseVet.objects.filter(kill_house=kill_request.kill_house).select_related( 'vet__user').first() if kill_house_vet: data = { "fullname": kill_house_vet.vet.user.fullname, "mobile": kill_house_vet.vet.user.mobile, } return data def get_weight_info(self, obj): return { "weight": int(obj.quantity * obj.province_request.poultry_request.Index_weight), "index_weight": round(int(obj.quantity * obj.province_request.poultry_request.Index_weight) / obj.quantity, 2), } def get_vet_check(self, obj): try: vet_check = VetCheckRequest.objects.get(kill_house_request=obj) vet_state = { "date": vet_check.create_date, "state": vet_check.state, "kill_house_vet_fullname": vet_check.vet.user.fullname, "kill_house_vet_mobile": vet_check.vet.user.mobile, } except: vet_state = None return vet_state def get_assignment(self, obj): try: assignment = KillHouseAssignmentInformation.objects.get(kill_house_request=obj) assignment_info = { "net_weight": assignment.net_weight, "real_quantity": assignment.real_quantity, "car_weight_without_load_image": assignment.car_weight_without_load_image, "car_weight_with_load_image": assignment.car_weight_with_load_image, "state": assignment.state, } except: assignment_info = None return assignment_info class ProvinceKillRequestForTotalInformationSerializer(serializers.ModelSerializer): killhouse_user = KillHouseForAutoAllocationSerializer(read_only=True) weight_info = serializers.SerializerMethodField('get_weight_info') kill_place = serializers.SerializerMethodField('get_kill_place') class Meta: model = ProvinceKillRequest fields = ['killhouse_user', 'quantity', 'kill_place', 'main_quantity', 'total_killed_quantity', 'first_car_allocated_quantity', 'total_killed_weight', 'automatic', 'weight_info', 'state'] def get_weight_info(self, obj): province_kill_request_quantity = obj.main_quantity province_kill_request_weight = int(obj.main_quantity * obj.province_request.poultry_request.Index_weight) province_kill_request_index_weight = round((province_kill_request_weight / province_kill_request_quantity), 2) return { "province_kill_request_quantity": province_kill_request_quantity, "province_kill_request_weight": province_kill_request_weight, "province_kill_request_index_weight": province_kill_request_index_weight, } def get_kill_place(self, obj): kill_place = obj.kill_request.slaughter_house.name if obj.kill_request.slaughter_house != None else obj.kill_request.kill_house.name return kill_place class ProvinceKillRequestForTotalInformationInTableSerializer(serializers.ModelSerializer): killhouse_user = KillHouseForAutoAllocationSerializer(read_only=True) weight_info = serializers.SerializerMethodField('get_weight_info') kill_place = serializers.SerializerMethodField('get_kill_place') kill_house_requests = serializers.SerializerMethodField('get_kill_house_requests') class Meta: model = ProvinceKillRequest fields = ['killhouse_user', 'quantity', 'kill_place', 'main_quantity', 'total_killed_quantity', 'first_car_allocated_quantity', 'total_killed_weight', 'automatic', 'kill_house_requests', 'weight_info', 'state'] def get_weight_info(self, obj): province_kill_request_quantity = obj.main_quantity province_kill_request_weight = int(obj.main_quantity * obj.province_request.poultry_request.Index_weight) province_kill_request_index_weight = round((province_kill_request_weight / province_kill_request_quantity), 2) return { "province_kill_request_quantity": province_kill_request_quantity, "province_kill_request_weight": province_kill_request_weight, "province_kill_request_index_weight": province_kill_request_index_weight, } def get_kill_place(self, obj): kill_place = obj.kill_request.slaughter_house.name if obj.kill_request.slaughter_house != None else obj.kill_request.kill_house.name return kill_place def get_kill_house_requests(self, obj): kill_house_requests = KillHouseRequest.objects.filter(province_kill_request=obj) kill_house_requests_serializer = KillHouseRequestForTotalInformationInTableSerializer(kill_house_requests, many=True) return kill_house_requests_serializer.data class PoultryRequestForTotalInformationSerializer(serializers.ModelSerializer): poultry = PoultryForBarManagementSerializer(read_only=True) hatching = serializers.SerializerMethodField('get_hatching') city_state = serializers.SerializerMethodField('get_city_state') province_state = serializers.SerializerMethodField('get_province_state') province_kill_requests = serializers.SerializerMethodField('get_province_kill_requests') kill_house_requests = serializers.SerializerMethodField('get_kill_house_requests') class Meta: model = PoultryRequest fields = ['key', 'create_date', 'poultry', 'quantity', 'send_date', 'direct_buying', 'union', 'state_process', 'free_sale_in_province', 'hatching', 'province_state', 'amount', 'financial_operation', 'chicken_breed', 'order_code', 'city_state', 'province_state', 'Index_weight', 'province_kill_requests', 'kill_house_requests', 'freezing', 'out', 'out_province_driver_info', 'out_province_request_canceller', 'out_province_request_cancel', 'buyer'] def get_city_state(self, obj): try: if obj.out == True: city_operator = CityOperator.objects.get(key=obj.city_operator.key, trash=False) return { "city_operator_fullname": city_operator.user.fullname, "city_operator_mobile": city_operator.user.mobile, "state": 'accept' if obj.state_process == 'accepted' else 'reject', "poultry": obj.poultry.unit_name, "city": obj.poultry.address.city.name, "province": obj.poultry.address.province.name, "date": obj.send_date.date(), } else: city = CityOperatorCheckRequest.objects.get(trash=False, poultry_request=obj) return { "city_operator_fullname": city.city_operator_system.user.fullname, "city_operator_mobile": city.city_operator_system.user.mobile, "state": city.state, "poultry": city.poultry_request.poultry.unit_name, "city": city.poultry_request.poultry.address.city.name, "province": city.poultry_request.poultry.address.province.name, "date": city.create_date.date(), } except: city_operator = CityOperator.objects.get(key=obj.city_operator.key, trash=False) return { "city_operator_fullname": city_operator.user.fullname, "city_operator_mobile": city_operator.user.mobile, "state": 'pending', "poultry": obj.poultry.unit_name, "city": obj.poultry.address.city.name, "province": obj.poultry.address.province.name, "date": None, } def get_hatching(self, obj): if ProvinceKillRequest.objects.filter( province_request__city_request_Poultry__poultry_request=obj, state='accepted').exists(): age2 = (obj.send_date - obj.hatching.date).days + 1 else: age2 = (datetime.datetime.now() - obj.hatching.date).days + 1 price = Pricing.objects.filter(trash=False).last() price = price.live_chicken_price if price else None try: vet_farm = VetFarm.objects.get(poultry=obj.poultry, trash=False) vet_farm_info = { "vet_farm_fullname": vet_farm.vet.user.fullname, "vet_farm_mobile": vet_farm.vet.user.mobile } except: vet_farm_info = None return { "quantity": obj.hatching.quantity, "left_over": obj.hatching.left_over, "age": age2, "price": price, "vet_farm_info": vet_farm_info, "weight": int(obj.quantity * obj.Index_weight) } def get_province_state(self, obj): try: if obj.out == True: province_operator = ProvinceOperator.objects.get(user__role__name='ProvinceOperator', trash=False) return { "province_operator_fullname": province_operator.user.fullname, "province_operator_mobile": province_operator.user.mobile, "state": 'accept' if obj.province_state == 'accepted' else 'reject', "poultry": obj.poultry.unit_name, "city": obj.poultry.address.city.name, "province": obj.poultry.address.province.name, "date": obj.send_date.date(), } else: province = ProvinceCheckOperatorRequest.objects.get(trash=False, poultry_request=obj) return { "province_operator_fullname": province.province_operator_system.user.fullname, "province_operator_mobile": province.province_operator_system.user.mobile, "state": province.state, "poultry": province.poultry_request.poultry.unit_name, "city": province.poultry_request.poultry.address.city.name, "province": province.poultry_request.poultry.address.province.name, "date": province.create_date.date(), } except: province_operator = ProvinceOperator.objects.get(user__role__name='ProvinceOperator', trash=False) return { "province_operator_fullname": province_operator.user.fullname, "province_operator_mobile": province_operator.user.mobile, "state": 'pending', "poultry": obj.poultry.unit_name, "city": obj.poultry.address.city.name, "province": obj.poultry.address.province.name, "date": None, } def get_province_kill_requests(self, obj): province_kill_requests = ProvinceKillRequest.objects.filter(state__in=('pending', 'accepted'), trash=False, province_request__poultry_request__key=obj.key) province_kill_request_serializer = ProvinceKillRequestForTotalInformationSerializer(province_kill_requests, many=True) poultry_request_quantity = obj.quantity poultry_request_remain_quantity = obj.remain_quantity allocated_quantity = province_kill_requests.aggregate(total=Sum('main_quantity'))['total'] return { "province_kill_request_serializer": province_kill_request_serializer.data, "province_kill_requests_total_info": { "poultry_request_quantity": poultry_request_quantity, "poultry_request_remain_quantity": poultry_request_remain_quantity, "allocated_quantity": allocated_quantity if allocated_quantity != None else 0, } } def get_kill_house_requests(self, obj): province_kill_requests = ProvinceKillRequest.objects.filter(state__in=('pending', 'accepted'), trash=False, province_request__poultry_request__key=obj.key) allocated_quantity = province_kill_requests.aggregate(total=Sum('main_quantity'))['total'] remain_quantity = province_kill_requests.aggregate(total=Sum('quantity'))['total'] kill_house_requests = KillHouseRequest.objects.filter( (Q(trash=False) | Q(trash=True, bar_remover__isnull=False)), province_request__poultry_request=obj) kill_house_request_quantity = kill_house_requests.aggregate(total=Sum('quantity'))['total'] kill_house_request_vet_check = kill_house_requests.filter( (Q(trash=False, vet_state='accepted', assignment_state_archive='pending') | Q(trash=False, vet_state='pending', assignment_state_archive='True'))) kill_house_request_assignment = kill_house_requests.filter(trash=False, assignment_state_archive='True') kill_house_requests_serializer = KillHouseRequestForTotalInformationSerializer(kill_house_requests, many=True) kill_house_request_vet_check_serializer = KillHouseRequestForTotalInformationSerializer( kill_house_request_vet_check, many=True) kill_house_request_assignment_serializer = KillHouseRequestForTotalInformationSerializer( kill_house_request_assignment, many=True) return { "allocated_quantity": allocated_quantity, "remain_quantity": remain_quantity, "kill_house_request_quantity": kill_house_request_quantity if kill_house_request_quantity != None else 0, "number_of_bars": len(kill_house_requests), "vet_accepted_number_of_bars": len(kill_house_request_vet_check), "vet_remain_number_of_bars": len(kill_house_requests) - len(kill_house_request_vet_check), "kill_house_request_assignment": len(kill_house_request_assignment), "assignment_remain_number_of_bars": len(kill_house_requests) - len(kill_house_request_assignment), "kill_house_requests_serializer": kill_house_requests_serializer.data, "kill_house_request_vet_check_serializer": kill_house_request_vet_check_serializer.data, "kill_house_request_assignment_serializer": kill_house_request_assignment_serializer.data, } class PoultryRequestForTotalInformationInTableSerializer(serializers.ModelSerializer): poultry = PoultryForBarManagementSerializer(read_only=True) hatching = serializers.SerializerMethodField('get_hatching') city_state = serializers.SerializerMethodField('get_city_state') province_state = serializers.SerializerMethodField('get_province_state') province_kill_requests = serializers.SerializerMethodField('get_province_kill_requests') class Meta: model = PoultryRequest fields = ['key', 'create_date', 'poultry', 'quantity', 'send_date', 'direct_buying', 'union', 'state_process', 'free_sale_in_province', 'hatching', 'province_state', 'amount', 'financial_operation', 'chicken_breed', 'order_code', 'city_state', 'province_state', 'Index_weight', 'province_kill_requests'] def get_city_state(self, obj): try: city = CityOperatorCheckRequest.objects.get(trash=False, poultry_request=obj) return { "city_operator_fullname": city.city_operator_system.user.fullname, "city_operator_mobile": city.city_operator_system.user.mobile, "state": city.state, "poultry": city.poultry_request.poultry.unit_name, "city": city.poultry_request.poultry.address.city.name, "province": city.poultry_request.poultry.address.province.name, "date": city.create_date.date(), } except: city_operator = CityOperator.objects.get(key=obj.city_operator.key, trash=False) return { "city_operator_fullname": city_operator.user.fullname, "city_operator_mobile": city_operator.user.mobile, "state": 'pending', "poultry": obj.poultry.unit_name, "city": obj.poultry.address.city.name, "province": obj.poultry.address.province.name, "date": None, } def get_hatching(self, obj): if ProvinceKillRequest.objects.filter( province_request__city_request_Poultry__poultry_request=obj, state='accepted').exists(): age2 = (obj.send_date - obj.hatching.date).days + 1 else: age2 = (datetime.datetime.now() - obj.hatching.date).days + 1 price = Pricing.objects.filter(trash=False).last() price = price.live_chicken_price if price else None try: vet_farm = VetFarm.objects.get(poultry=obj.poultry, trash=False) vet_farm_info = { "vet_farm_fullname": vet_farm.vet.user.fullname, "vet_farm_mobile": vet_farm.vet.user.mobile } except: vet_farm_info = None return { "quantity": obj.hatching.quantity, "left_over": obj.hatching.left_over, "age": age2, "price": price, "vet_farm_info": vet_farm_info, "weight": int(obj.quantity * obj.Index_weight) } def get_province_state(self, obj): try: province = ProvinceCheckOperatorRequest.objects.get(trash=False, poultry_request=obj) return { "province_operator_fullname": province.province_operator_system.user.fullname, "province_operator_mobile": province.province_operator_system.user.mobile, "state": province.state, "poultry": province.poultry_request.poultry.unit_name, "city": province.poultry_request.poultry.address.city.name, "province": province.poultry_request.poultry.address.province.name, "date": province.create_date.date(), } except: province_operator = ProvinceOperator.objects.get(user__role__name='ProvinceOperator', trash=False) return { "province_operator_fullname": province_operator.user.fullname, "province_operator_mobile": province_operator.user.mobile, "state": 'pending', "poultry": obj.poultry.unit_name, "city": obj.poultry.address.city.name, "province": obj.poultry.address.province.name, "date": None, } def get_province_kill_requests(self, obj): province_kill_requests = ProvinceKillRequest.objects.filter(state__in=('pending', 'accepted'), trash=False, province_request__poultry_request__key=obj.key) province_kill_request_serializer = ProvinceKillRequestForTotalInformationInTableSerializer( province_kill_requests, many=True) return province_kill_request_serializer.data class SlaughterHouseTransactionSerializer(serializers.ModelSerializer): kill_house = KillHouseForAutoAllocationSerializer(read_only=True) parent_kill_house = KillHouseForAutoAllocationSerializer(read_only=True) class Meta: model = SlaughterHouseTransaction fields = '__all__' class KillHouseWageSerializer(serializers.ModelSerializer): wage_info = serializers.SerializerMethodField('get_wage_info') class Meta: model = KillHouse fields = ['key', 'name', 'wage_info'] def get_wage_info(self, obj): total_weight = 0 total_wage = 0 total_paid_wage = 0 total_unpaid_wage = 0 kill_house_requests = KillHouseRequest.objects.filter(Q(killer=obj) | Q(killhouse_user=obj), trash=False) total_wage += \ kill_house_requests.aggregate(total=Sum(F('accepted_real_weight') * F('province_kill_request__wage')))[ 'total'] or 0 total_weight += kill_house_requests.aggregate(total=Sum('accepted_real_weight'))[ 'total'] or 0 province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj, first_car_allocated_quantity=0, state__in=('pending', 'accepted'), trash=False, return_to_province=False, archive_by_province=False) total_wage += province_kill_requests.aggregate(total=Sum(F('total_killed_weight') * F('wage')))[ 'total'] or 0 total_weight += province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 transactions = SlaughterHouseTransaction.objects.filter(Q(kill_house=obj) | Q(parent_kill_house=obj), state='completed', trash=False) total_paid_wage = transactions.aggregate(total=Sum('amount'))[ 'total'] or 0 return { "total_weight": total_weight, "total_wage": total_wage, "total_paid_wage": total_paid_wage, "total_unpaid_wage": total_wage - total_paid_wage, } class TotalWageInformationExclusiveKillerSerializer(serializers.ModelSerializer): wage_info = serializers.SerializerMethodField('get_wage_info') class Meta: model = KillHouse fields = ['name', 'key', 'wage_info'] # def get_wage_info(self, obj): # before_total_out_live_buying_province_carcasses_price = 0 # total_wage_type = WageType.objects.filter(trash=False) # free_buying_live_weight_wage_type = total_wage_type.filter(en_name='live-buy', trash=False).first() # free_buying_live_weight_wage_type_amount = free_buying_live_weight_wage_type.amount if free_buying_live_weight_wage_type.status == True else 0 # finance_info = get_kill_house_finance_info(obj) # slaughter_transactions = InternalTransaction.objects.filter( # kill_house=obj, status='completed', # trash=False) # # total_paid_wage = slaughter_transactions.aggregate(total=Sum('amount'))[ # 'total'] or 0 # # total_pure_province_carcasses_price = finance_info['total_pure_province_carcasses_price'] # return_total_pure_province_carcasses_price = finance_info['total_return_pure_province_carcasses_price'] # total_out_selling_province_carcasses_price = finance_info['total_out_selling_province_carcasses_price'] # difference_requests_price = finance_info['difference_requests_price'] # # if new_out_selling_count_wage: # total_out_live_buying_province_carcasses_price = int( # (finance_info[ # 'before_total_out_live_buying_province_carcasses_weight'] * before_out_buying_count_wage_amount) + ( # finance_info[ # 'after_total_out_live_buying_province_carcasses_weight'] * free_buying_live_weight_wage_type_amount)) # # before_total_out_live_buying_province_carcasses_price = finance_info[ # 'before_total_out_live_buying_province_carcasses_weight'] * before_out_buying_count_wage_amount # else: # total_out_live_buying_province_carcasses_price = finance_info[ # 'total_out_live_buying_province_carcasses_price'] # total_out_carcasses_buying_province_carcasses_price = finance_info[ # 'total_out_carcasses_buying_province_carcasses_price'] # total_price = finance_info['total_price'] # # # total_pure_province_carcasses_price = finance_info['total_pure_province_carcasses_price'] # # total_out_selling_province_carcasses_price = finance_info['total_out_selling_province_carcasses_price'] # # total_out_live_buying_province_carcasses_price = finance_info['total_out_live_buying_province_carcasses_price'] # # total_out_carcasses_buying_province_carcasses_price = finance_info[ # # 'total_out_carcasses_buying_province_carcasses_price'] # # total_price = finance_info['total_price'] # # 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 # company_province_kill_request_percent = percentages_wage_type.filter( # wage_type__en_name='province-kill-request', share_type__en_name='company').first().percent / 100 # guilds_province_kill_request_percent = percentages_wage_type.filter( # wage_type__en_name='province-kill-request', share_type__en_name='guilds').first().percent / 100 # other_province_kill_request_percent = percentages_wage_type.filter( # wage_type__en_name='province-kill-request', share_type__en_name='other').first().percent / 100 # # union_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', # share_type__en_name='union').first().percent / 100 # union_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', # share_type__en_name='union').first().percent / 100 # company_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', # share_type__en_name='company').first().percent / 100 # company_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', # share_type__en_name='company').first().percent / 100 # guilds_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', # share_type__en_name='guilds').first().percent / 100 # guilds_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', # share_type__en_name='guilds').first().percent / 100 # other_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', # share_type__en_name='other').first().percent / 100 # other_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', # share_type__en_name='other').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 # company_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', # share_type__en_name='company').first().percent / 100 # guilds_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', # share_type__en_name='guilds').first().percent / 100 # other_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', # share_type__en_name='other').first().percent / 100 # # guilds_difference_requests_price = difference_requests_price * guilds_province_kill_request_percent # other_difference_requests_price = difference_requests_price * other_province_kill_request_percent # guild_return_province_kill_request_wage = return_total_pure_province_carcasses_price * guilds_province_kill_request_percent # other_return_province_kill_request_wage = return_total_pure_province_carcasses_price * other_province_kill_request_percent # # union_province_kill_request_wage = total_pure_province_carcasses_price * union_province_kill_request_percent # return_union_province_kill_request_wage = return_total_pure_province_carcasses_price * union_province_kill_request_percent # union_free_buying_live_wage = ( # total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * union_free_buying_live_percent # union_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * union_free_buying_carcasses_percent # union_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * union_free_sell_carcasses_percent # union_total_wage = union_province_kill_request_wage + union_free_buying_live_wage + union_free_buying_carcasses_wage + union_free_sell_carcasses_wage + return_union_province_kill_request_wage # union_total_paid_wage = slaughter_transactions.aggregate(total=Sum('union_share'))['total'] or 0 # union_total_unpaid_wage = union_total_wage - union_total_paid_wage # company_province_kill_request_wage = ( # total_pure_province_carcasses_price * company_province_kill_request_percent) + ( # guilds_difference_requests_price + other_difference_requests_price) # return_company_province_kill_request_wage = ( # return_total_pure_province_carcasses_price * company_province_kill_request_percent) + ( # guild_return_province_kill_request_wage + other_return_province_kill_request_wage) # company_free_buying_live_wage = (( # total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * company_free_buying_live_percent) + before_total_out_live_buying_province_carcasses_price # company_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * company_free_buying_carcasses_percent # company_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * company_free_sell_carcasses_percent # company_total_wage = company_province_kill_request_wage + company_free_buying_live_wage + company_free_buying_carcasses_wage + company_free_sell_carcasses_wage + return_company_province_kill_request_wage # company_total_paid_wage = slaughter_transactions.aggregate(total=Sum('company_share'))['total'] or 0 # company_total_unpaid_wage = company_total_wage - company_total_paid_wage # # guilds_province_kill_request_wage = ( # total_pure_province_carcasses_price * guilds_province_kill_request_percent) - guilds_difference_requests_price # return_guilds_province_kill_request_wage = 0 # guilds_free_buying_live_wage = ( # total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * guilds_free_buying_live_percent # guilds_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * guilds_free_buying_carcasses_percent # guilds_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * guilds_free_sell_carcasses_percent # guilds_total_wage = guilds_province_kill_request_wage + guilds_free_buying_live_wage + guilds_free_buying_carcasses_wage + guilds_free_sell_carcasses_wage # guilds_total_paid_wage = slaughter_transactions.aggregate(total=Sum('guilds_share'))['total'] or 0 # guilds_total_unpaid_wage = guilds_total_wage - guilds_total_paid_wage # # other_province_kill_request_wage = ( # total_pure_province_carcasses_price * other_province_kill_request_percent) - other_difference_requests_price # return_other_province_kill_request_wage = 0 # other_free_buying_live_wage = ( # total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * other_free_buying_live_percent # other_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * other_free_buying_carcasses_percent # other_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * other_free_sell_carcasses_percent # other_total_wage = other_province_kill_request_wage + other_free_buying_live_wage + other_free_buying_carcasses_wage + other_free_sell_carcasses_wage # other_total_paid_wage = slaughter_transactions.aggregate(total=Sum('other_share'))['total'] or 0 # other_total_unpaid_wage = other_total_wage - other_total_paid_wage # # shares = [ # { # "name": "اتحادیه", # "province_kill_request_wage": union_province_kill_request_wage, # "return_province_kill_request_wage": return_union_province_kill_request_wage, # "free_buying_live_wage": union_free_buying_live_wage, # "free_buying_carcasses_wage": union_free_buying_carcasses_wage, # "free_sell_carcasses_wage": union_free_sell_carcasses_wage, # "total_wage": union_total_wage, # "total_paid_wage": union_total_paid_wage, # "total_unpaid_wage": union_total_unpaid_wage, # }, # { # "name": "شرکت", # "province_kill_request_wage": company_province_kill_request_wage, # "return_province_kill_request_wage": return_company_province_kill_request_wage, # "free_buying_live_wage": company_free_buying_live_wage, # "free_buying_carcasses_wage": company_free_buying_carcasses_wage, # "free_sell_carcasses_wage": company_free_sell_carcasses_wage, # "total_wage": company_total_wage, # "total_paid_wage": company_total_paid_wage, # "total_unpaid_wage": company_total_unpaid_wage, # }, # { # "name": "صنف پروتئین", # "province_kill_request_wage": guilds_province_kill_request_wage, # "return_province_kill_request_wage": return_guilds_province_kill_request_wage, # "free_buying_live_wage": guilds_free_buying_live_wage, # "free_buying_carcasses_wage": guilds_free_buying_carcasses_wage, # "free_sell_carcasses_wage": guilds_free_sell_carcasses_wage, # "total_wage": guilds_total_wage, # "total_paid_wage": guilds_total_paid_wage, # "total_unpaid_wage": guilds_total_unpaid_wage, # }, # { # "name": "دامپزشک", # "province_kill_request_wage": other_province_kill_request_wage, # "return_province_kill_request_wage": return_other_province_kill_request_wage, # "free_buying_live_wage": other_free_buying_live_wage, # "free_buying_carcasses_wage": other_free_buying_carcasses_wage, # "free_sell_carcasses_wage": other_free_sell_carcasses_wage, # "total_wage": other_total_wage, # "total_paid_wage": other_total_paid_wage, # "total_unpaid_wage": other_total_unpaid_wage, # }] # # return { # "wage_counting_type": wage_counting_type, # "total_wage": finance_info['total_price'], # "total_paid_wage": total_paid_wage, # "off": obj.off, # "total_unpaid_wage": finance_info['total_price'] - (total_paid_wage + obj.off), # "province_kill_requests_total_wage": finance_info['total_pure_province_carcasses_price'], # "province_kill_requests_total_weight": finance_info['total_pure_province_carcasses_weight'], # "free_bars_live_total_wage": finance_info['total_out_live_buying_province_carcasses_price'], # "free_bars_live_total_weight": finance_info['total_out_live_buying_province_carcasses_weight'], # "free_bars_carcases_total_wage": finance_info['total_out_carcasses_buying_province_carcasses_price'], # "free_bars_carcases_total_weight": finance_info['total_out_carcasses_buying_province_carcasses_weight'], # "free_bars_out_province_carcases_total_wage": finance_info['total_out_selling_province_carcasses_price'], # "free_bars_out_province_carcases_total_weight": finance_info['total_out_selling_province_carcasses_weight'], # "total_province_live_weight": finance_info['total_province_live_weight'], # "total_province_carcasses_weight": finance_info['total_province_carcasses_weight'], # "province_live_wage_amount": finance_info['province_live_wage_amount'], # "free_buying_live_weight_amount": finance_info['free_buying_live_weight_amount'], # "free_buying_carcesses_weight_amount": finance_info['free_buying_carcesses_weight_amount'], # "free_sell_carcesses_weight_amount": finance_info['free_sell_carcesses_weight_amount'], # "user_token": 'k' + obj.kill_house_operator.user.user_gate_way_id, # "shares": shares, # "return_total_province_live_weight": finance_info['return_total_province_live_weight'], # "total_return_pure_province_carcasses_price": finance_info['total_return_pure_province_carcasses_price'], # # } def get_wage_info(self, obj): before_total_out_live_buying_province_carcasses_price = 0 total_wage_type = WageType.objects.filter(trash=False) free_buying_live_weight_wage_type = total_wage_type.filter(en_name='live-buy', trash=False).first() free_buying_live_weight_wage_type_amount = free_buying_live_weight_wage_type.amount if free_buying_live_weight_wage_type.status == True else 0 finance_info = get_kill_house_finance_info(obj) if obj.killer and obj.type == 'public': slaughter_transactions = InternalTransaction.objects.filter( kill_house=obj,parent_kill_house=obj, status='completed', trash=False) else: slaughter_transactions = InternalTransaction.objects.filter( kill_house=obj, status='completed', trash=False) total_paid_wage = slaughter_transactions.aggregate(total=Sum('amount'))[ 'total'] or 0 total_pure_province_carcasses_price = finance_info['total_pure_province_carcasses_price'] return_total_pure_province_carcasses_price = finance_info['total_return_pure_province_carcasses_price'] total_out_selling_province_carcasses_price = finance_info['total_out_selling_province_carcasses_price'] difference_requests_price = finance_info['difference_requests_price'] if new_out_selling_count_wage: total_out_live_buying_province_carcasses_price = int( (finance_info[ 'before_total_out_live_buying_province_carcasses_weight'] * before_out_buying_count_wage_amount) + ( finance_info[ 'after_total_out_live_buying_province_carcasses_weight'] * free_buying_live_weight_wage_type_amount)) before_total_out_live_buying_province_carcasses_price = finance_info[ 'before_total_out_live_buying_province_carcasses_weight'] * before_out_buying_count_wage_amount else: total_out_live_buying_province_carcasses_price = finance_info[ 'total_out_live_buying_province_carcasses_price'] total_out_carcasses_buying_province_carcasses_price = finance_info[ 'total_out_carcasses_buying_province_carcasses_price'] total_price = finance_info['total_price'] # total_pure_province_carcasses_price = finance_info['total_pure_province_carcasses_price'] # total_out_selling_province_carcasses_price = finance_info['total_out_selling_province_carcasses_price'] # total_out_live_buying_province_carcasses_price = finance_info['total_out_live_buying_province_carcasses_price'] # total_out_carcasses_buying_province_carcasses_price = finance_info[ # 'total_out_carcasses_buying_province_carcasses_price'] # total_price = finance_info['total_price'] 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 company_province_kill_request_percent = percentages_wage_type.filter( wage_type__en_name='province-kill-request', share_type__en_name='company').first().percent / 100 guilds_province_kill_request_percent = percentages_wage_type.filter( wage_type__en_name='province-kill-request', share_type__en_name='guilds').first().percent / 100 other_province_kill_request_percent = percentages_wage_type.filter( wage_type__en_name='province-kill-request', share_type__en_name='other').first().percent / 100 union_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', share_type__en_name='union').first().percent / 100 union_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', share_type__en_name='union').first().percent / 100 company_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', share_type__en_name='company').first().percent / 100 company_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', share_type__en_name='company').first().percent / 100 guilds_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', share_type__en_name='guilds').first().percent / 100 guilds_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', share_type__en_name='guilds').first().percent / 100 other_free_buying_live_percent = percentages_wage_type.filter(wage_type__en_name='live-buy', share_type__en_name='other').first().percent / 100 other_free_buying_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-buy', share_type__en_name='other').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 company_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', share_type__en_name='company').first().percent / 100 guilds_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', share_type__en_name='guilds').first().percent / 100 other_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell', share_type__en_name='other').first().percent / 100 guilds_difference_requests_price = difference_requests_price * guilds_province_kill_request_percent other_difference_requests_price = difference_requests_price * other_province_kill_request_percent guild_return_province_kill_request_wage = return_total_pure_province_carcasses_price * guilds_province_kill_request_percent other_return_province_kill_request_wage = return_total_pure_province_carcasses_price * other_province_kill_request_percent union_province_kill_request_wage = total_pure_province_carcasses_price * union_province_kill_request_percent return_union_province_kill_request_wage = return_total_pure_province_carcasses_price * union_province_kill_request_percent union_free_buying_live_wage = ( total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * union_free_buying_live_percent union_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * union_free_buying_carcasses_percent union_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * union_free_sell_carcasses_percent union_total_wage = union_province_kill_request_wage + union_free_buying_live_wage + union_free_buying_carcasses_wage + union_free_sell_carcasses_wage + return_union_province_kill_request_wage union_total_paid_wage = slaughter_transactions.aggregate(total=Sum('union_share'))['total'] or 0 union_total_unpaid_wage = union_total_wage - union_total_paid_wage company_province_kill_request_wage = ( total_pure_province_carcasses_price * company_province_kill_request_percent) + ( guilds_difference_requests_price + other_difference_requests_price) return_company_province_kill_request_wage = ( return_total_pure_province_carcasses_price * company_province_kill_request_percent) + ( guild_return_province_kill_request_wage + other_return_province_kill_request_wage) company_free_buying_live_wage = (( total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * company_free_buying_live_percent) + before_total_out_live_buying_province_carcasses_price company_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * company_free_buying_carcasses_percent company_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * company_free_sell_carcasses_percent company_total_wage = company_province_kill_request_wage + company_free_buying_live_wage + company_free_buying_carcasses_wage + company_free_sell_carcasses_wage + return_company_province_kill_request_wage company_total_paid_wage = slaughter_transactions.aggregate(total=Sum('company_share'))['total'] or 0 company_total_unpaid_wage = company_total_wage - company_total_paid_wage guilds_province_kill_request_wage = ( total_pure_province_carcasses_price * guilds_province_kill_request_percent) - guilds_difference_requests_price return_guilds_province_kill_request_wage = 0 guilds_free_buying_live_wage = ( total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * guilds_free_buying_live_percent guilds_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * guilds_free_buying_carcasses_percent guilds_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * guilds_free_sell_carcasses_percent guilds_total_wage = guilds_province_kill_request_wage + guilds_free_buying_live_wage + guilds_free_buying_carcasses_wage + guilds_free_sell_carcasses_wage guilds_total_paid_wage = slaughter_transactions.aggregate(total=Sum('guilds_share'))['total'] or 0 guilds_total_unpaid_wage = guilds_total_wage - guilds_total_paid_wage other_province_kill_request_wage = ( total_pure_province_carcasses_price * other_province_kill_request_percent) - other_difference_requests_price return_other_province_kill_request_wage = 0 other_free_buying_live_wage = ( total_out_live_buying_province_carcasses_price - before_total_out_live_buying_province_carcasses_price) * other_free_buying_live_percent other_free_buying_carcasses_wage = total_out_carcasses_buying_province_carcasses_price * other_free_buying_carcasses_percent other_free_sell_carcasses_wage = total_out_selling_province_carcasses_price * other_free_sell_carcasses_percent other_total_wage = other_province_kill_request_wage + other_free_buying_live_wage + other_free_buying_carcasses_wage + other_free_sell_carcasses_wage other_total_paid_wage = slaughter_transactions.aggregate(total=Sum('other_share'))['total'] or 0 other_total_unpaid_wage = other_total_wage - other_total_paid_wage shares = [ { "name": "اتحادیه", "province_kill_request_wage": union_province_kill_request_wage, "return_province_kill_request_wage": return_union_province_kill_request_wage, "free_buying_live_wage": union_free_buying_live_wage, "free_buying_carcasses_wage": union_free_buying_carcasses_wage, "free_sell_carcasses_wage": union_free_sell_carcasses_wage, "total_wage": union_total_wage, "total_paid_wage": union_total_paid_wage, "total_unpaid_wage": union_total_unpaid_wage, }, { "name": "شرکت", "province_kill_request_wage": company_province_kill_request_wage, "return_province_kill_request_wage": return_company_province_kill_request_wage, "free_buying_live_wage": company_free_buying_live_wage, "free_buying_carcasses_wage": company_free_buying_carcasses_wage, "free_sell_carcasses_wage": company_free_sell_carcasses_wage, "total_wage": company_total_wage, "total_paid_wage": company_total_paid_wage, "total_unpaid_wage": company_total_unpaid_wage, }, { "name": "صنف پروتئین", "province_kill_request_wage": guilds_province_kill_request_wage, "return_province_kill_request_wage": return_guilds_province_kill_request_wage, "free_buying_live_wage": guilds_free_buying_live_wage, "free_buying_carcasses_wage": guilds_free_buying_carcasses_wage, "free_sell_carcasses_wage": guilds_free_sell_carcasses_wage, "total_wage": guilds_total_wage, "total_paid_wage": guilds_total_paid_wage, "total_unpaid_wage": guilds_total_unpaid_wage, }, { "name": "دامپزشک", "province_kill_request_wage": other_province_kill_request_wage, "return_province_kill_request_wage": return_other_province_kill_request_wage, "free_buying_live_wage": other_free_buying_live_wage, "free_buying_carcasses_wage": other_free_buying_carcasses_wage, "free_sell_carcasses_wage": other_free_sell_carcasses_wage, "total_wage": other_total_wage, "total_paid_wage": other_total_paid_wage, "total_unpaid_wage": other_total_unpaid_wage, }] return { "wage_counting_type": wage_counting_type, "total_wage": finance_info['total_price'], "total_paid_wage": total_paid_wage, "off": obj.off, "total_unpaid_wage": finance_info['total_price'] - (total_paid_wage + obj.off), "province_kill_requests_total_wage": finance_info['total_pure_province_carcasses_price'], "province_kill_requests_total_weight": finance_info['total_pure_province_carcasses_weight'], "free_bars_live_total_wage": finance_info['total_out_live_buying_province_carcasses_price'], "free_bars_live_total_weight": finance_info['total_out_live_buying_province_carcasses_weight'], "free_bars_carcases_total_wage": finance_info['total_out_carcasses_buying_province_carcasses_price'], "free_bars_carcases_total_weight": finance_info['total_out_carcasses_buying_province_carcasses_weight'], "free_bars_out_province_carcases_total_wage": finance_info['total_out_selling_province_carcasses_price'], "free_bars_out_province_carcases_total_weight": finance_info['total_out_selling_province_carcasses_weight'], "total_province_live_weight": finance_info['total_province_live_weight'], "total_province_carcasses_weight": finance_info['total_province_carcasses_weight'], "province_live_wage_amount": finance_info['province_live_wage_amount'], "free_buying_live_weight_amount": finance_info['free_buying_live_weight_amount'], "free_buying_carcesses_weight_amount": finance_info['free_buying_carcesses_weight_amount'], "free_sell_carcesses_weight_amount": finance_info['free_sell_carcesses_weight_amount'], "user_token": 'k' + obj.kill_house_operator.user.user_gate_way_id, "shares": shares, "return_total_province_live_weight": finance_info['return_total_province_live_weight'], "total_return_pure_province_carcasses_price": finance_info['total_return_pure_province_carcasses_price'], } class TotalWageInformationKillHouseExclusiveKillerSerializer(serializers.ModelSerializer): wage_info = serializers.SerializerMethodField('get_wage_info') class Meta: model = KillHouse fields = ['name', 'key', 'wage_info'] def get_wage_info(self, obj): # kill_house_ids = KillHouseRequest.objects.filter(Q(killhouse_user=obj) | Q(killer=obj), archive_wage=False, # trash=False # ).values_list('killer', flat=True).distinct() kill_house_ids = KillHousePercentage.objects.filter( Q(kill_house=obj) | Q(kill_house_for_killer=obj, kill_house__type='exclusive'), trash=False).values_list( 'kill_house', flat=True) kill_houses = KillHouse.objects.filter(id__in=kill_house_ids) kill_house_srz = TotalWageInformationExclusiveKillerSerializer(kill_houses, many=True, context={'kill_house': obj}) return kill_house_srz.data class ProvinceRequestForKillHouseLetterSerialize(serializers.ModelSerializer): wage_info = serializers.SerializerMethodField('get_wage_info') model = ProvinceKillRequest fields = ['id', 'wage_info'] def get_wage_info(self, obj): age = (datetime.datetime.now() - obj.province_request.poultry_request.hatching.date).days + 1 poultry = { 'poultry': obj.province_request.poultry_request.poultry.unit_name, 'poultry_mobile': obj.province_request.poultry_request.poultry.user.mobile, 'poultry_city': obj.province_request.poultry_request.poultry.address.city.name, 'quantity': obj.main_quantity, 'Index_weight': obj.province_request.poultry_request.Index_weight, 'chicken_age': age, } return poultry class KillHouseLetterForProvinceSerializer(serializers.ModelSerializer): province_kill_request = serializers.SerializerMethodField() total_quantity = serializers.SerializerMethodField() kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) class Meta: model = KillHouse fields = ['key', 'killer', 'name', 'kill_house_operator', 'province_kill_request', 'total_quantity'] def get_province_kill_request(self, obj): date = self.context.get('date') kill_requests = KillHouseRequest.objects.filter( Q(killer=obj) | Q(killhouse_user=obj), trash=False, kill_request__recive_date__date=date ).select_related( 'province_request__poultry_request__poultry', 'province_request__poultry_request__hatching', 'province_request__poultry_request__poultry__address', 'add_car__driver' ) total_quantity = kill_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get('total_quantity', 0) self.total_quantity = total_quantity poultries = [ { 'poultry': kill.province_request.poultry_request.poultry.unit_name, 'poultry_mobile': kill.province_request.poultry_request.poultry.user.mobile, 'poultry_city': kill.province_request.poultry_request.poultry.address.city.name, 'quantity': kill.accepted_real_quantity, 'Index_weight': kill.province_request.poultry_request.Index_weight, 'chicken_age': (datetime.datetime.now() - kill.province_request.poultry_request.hatching.date).days + 1, 'weight': kill.accepted_real_weight, 'car_type': kill.add_car.driver.type_car, 'driver_name': kill.add_car.driver.driver_name, 'traffic_code': kill.traffic_code, 'clearance_code': kill.clearance_code, 'pelak': kill.add_car.driver.pelak, } for kill in kill_requests ] return poultries def get_total_quantity(self, obj): return getattr(self, 'total_quantity', 0) class KillHousetestSerializer(serializers.ModelSerializer): info = serializers.SerializerMethodField('get_province_kill_request') class Meta: model = KillHouse fields = ['key', 'name', 'info'] def get_province_kill_request(self, obj): total_weight = 0 total_wage = 0 total_paid_wage = 0 total_unpaid_wage = 0 union_total_weight = 0 union_total_wage = 0 union_total_paid_wage = 0 union_total_unpaid_wage = 0 union_province_kill_request_total_wage = 0 freezing_union_province_kill_request_total_wage = 0 union_kill_house_free_live_bar_total_wage = 0 union_kill_house_free_carcases_bar_total_wage = 0 union_chain_allocation_total_wage = 0 union_province_kill_request_total_weight = 0 freezing_union_province_kill_request_total_weight = 0 union_kill_house_free_live_bar_total_weight = 0 union_kill_house_free_carcases_bar_total_weight = 0 union_chain_allocation_total_weight = 0 company_total_weight = 0 company_total_wage = 0 company_total_paid_wage = 0 company_total_unpaid_wage = 0 company_province_kill_request_total_wage = 0 freezing_company_province_kill_request_total_wage = 0 company_kill_house_free_live_bar_total_wage = 0 company_kill_house_free_carcases_bar_total_wage = 0 company_chain_allocation_total_wage = 0 company_province_kill_request_total_weight = 0 freezing_company_province_kill_request_total_weight = 0 company_kill_house_free_live_bar_total_weight = 0 company_kill_house_free_carcases_bar_total_weight = 0 company_chain_allocation_total_weight = 0 guilds_total_weight = 0 guilds_total_wage = 0 guilds_total_paid_wage = 0 guilds_total_unpaid_wage = 0 guilds_province_kill_request_total_wage = 0 freezing_guilds_province_kill_request_total_wage = 0 guilds_kill_house_free_live_bar_total_wage = 0 guilds_kill_house_free_carcases_bar_total_wage = 0 guilds_chain_allocation_total_wage = 0 guilds_province_kill_request_total_weight = 0 freezing_guilds_province_kill_request_total_weight = 0 guilds_kill_house_free_live_bar_total_weight = 0 guilds_kill_house_free_carcases_bar_total_weight = 0 guilds_chain_allocation_total_weight = 0 province_kill_request_total_wage = 0 province_kill_request_total_weight = 0 freezing_province_kill_requests_total_wage = 0 freezing_province_kill_requests_total_weight = 0 chain_total_wage = 0 chain_total_weight = 0 free_bars_live_total_wage = 0 free_bars_live_total_weight = 0 free_bars_carcases_total_wage = 0 free_bars_carcases_total_weight = 0 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) freezing_province_kill_requests = province_kill_requests.filter( province_request__poultry_request__freezing=True) 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)), # killhouse_user=kill_house, archive_wage=False, trash=False ) kill_house_requests = kill_house_requests.filter(Q(killer=obj) | Q(killhouse_user=obj)).exclude( province_kill_request__union_share=0, province_kill_request__company_share=0, province_kill_request__guilds_share=0) # kill_house_requests = KillHouseRequest.objects.filter( # Q(Q(killhouse_user=obj) & Q(killer=obj)) | Q( # Q(killhouse_user=obj) & Q(killer__isnull=True)) | Q( # Q(killhouse_user=obj) | Q(killer=obj)), archive_wage=False, # trash=False # ).exclude(province_kill_request__union_share=0, province_kill_request__company_share=0, # province_kill_request__guilds_share=0) freezing_kill_house_requests = kill_house_requests.filter( province_kill_request__province_request__poultry_request__freezing=True) free_bars = KillHouseFreeBarInformation.objects.filter(kill_house=obj, archive_wage=False, trash=False) free_bars_live = free_bars.filter(buy_type='live', trash=False) free_bars_carcases = free_bars.filter(buy_type='carcass', trash=False) province_kill_requests_total_wage = \ province_kill_requests.filter(province_request__poultry_request__freezing=False).aggregate( total=Sum('total_wage_amount'))[ 'total'] or 0 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 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 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 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 slaughter_transactions = InternalTransaction.objects.filter( kill_house=obj, status='completed', trash=False) 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 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_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 += \ slaughter_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_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 += \ slaughter_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_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 += \ slaughter_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 return { "total_weight": total_weight, "total_wage": total_wage, "total_paid_wage": total_paid_wage, "total_unpaid_wage": total_wage - total_paid_wage, "province_kill_requests_total_wage": province_kill_requests_total_wage, "province_kill_requests_total_weight": province_kill_requests_total_weight, "freezing_province_kill_requests_total_wage": freezing_province_kill_requests_total_wage, "freezing_province_kill_requests_total_weight": freezing_province_kill_requests_total_weight, "free_bars_live_total_wage": free_bars_live_total_wage, "free_bars_live_total_weight": free_bars_live_total_weight, "free_bars_carcases_total_wage": free_bars_carcases_total_wage, "free_bars_carcases_total_weight": free_bars_carcases_total_weight, "shares": { "union_total_weight": union_total_weight, "union_total_wage": union_total_wage, "union_total_paid_wage": union_total_paid_wage, "union_total_unpaid_wage": union_total_unpaid_wage, "union_province_kill_request_total_wage": union_province_kill_request_total_wage, "union_kill_house_free_live_bar_total_wage": union_kill_house_free_live_bar_total_wage, "union_kill_house_free_carcases_bar_total_wage": union_kill_house_free_carcases_bar_total_wage, "union_chain_allocation_total_wage": union_chain_allocation_total_wage, "union_province_kill_request_total_weight": union_province_kill_request_total_weight, "union_kill_house_free_live_bar_total_weight": union_kill_house_free_live_bar_total_weight, "union_kill_house_free_carcases_bar_total_weight": union_kill_house_free_carcases_bar_total_weight, "union_chain_allocation_total_weight": union_chain_allocation_total_weight, "freezing_union_province_kill_request_total_wage": freezing_union_province_kill_request_total_wage, "freezing_union_province_kill_request_total_weight": freezing_union_province_kill_request_total_weight, "company_total_weight": company_total_weight, "company_total_wage": company_total_wage, "company_total_paid_wage": company_total_paid_wage, "company_total_unpaid_wage": company_total_unpaid_wage, "company_province_kill_request_total_wage": company_province_kill_request_total_wage, "company_kill_house_free_live_bar_total_wage": company_kill_house_free_live_bar_total_wage, "company_kill_house_free_carcases_bar_total_wage": company_kill_house_free_carcases_bar_total_wage, "company_chain_allocation_total_wage": company_chain_allocation_total_wage, "company_province_kill_request_total_weight": company_province_kill_request_total_weight, "company_kill_house_free_live_bar_total_weight": company_kill_house_free_live_bar_total_weight, "company_kill_house_free_carcases_bar_total_weight": company_kill_house_free_carcases_bar_total_weight, "company_chain_allocation_total_weight": company_chain_allocation_total_weight, "freezing_company_province_kill_request_total_wage": freezing_company_province_kill_request_total_wage, "freezing_company_province_kill_request_total_weight": freezing_company_province_kill_request_total_weight, "guilds_total_weight": guilds_total_weight, "guilds_total_wage": guilds_total_wage, "guilds_total_paid_wage": guilds_total_paid_wage, "guilds_total_unpaid_wage": guilds_total_unpaid_wage, "guilds_province_kill_request_total_wage": guilds_province_kill_request_total_wage, "guilds_kill_house_free_live_bar_total_wage": guilds_kill_house_free_live_bar_total_wage, "guilds_kill_house_free_carcases_bar_total_wage": guilds_kill_house_free_carcases_bar_total_wage, "guilds_chain_allocation_total_wage": guilds_chain_allocation_total_wage, "guilds_province_kill_request_total_weight": guilds_province_kill_request_total_weight, "guilds_kill_house_free_live_bar_total_weight": guilds_kill_house_free_live_bar_total_weight, "guilds_kill_house_free_carcases_bar_total_weight": guilds_kill_house_free_carcases_bar_total_weight, "guilds_chain_allocation_total_weight": guilds_chain_allocation_total_weight, "freezing_guilds_province_kill_request_total_wage": freezing_guilds_province_kill_request_total_wage, "freezing_guilds_province_kill_request_total_weight": freezing_guilds_province_kill_request_total_weight, } } class VetKillHouseSerializer(serializers.ModelSerializer): user = SystemUserProfileSerializer(read_only=True) address = SystemAddressSerializer(read_only=True) user_bank_info = BankCardSerializer(read_only=True) kill_houses = serializers.SerializerMethodField('get_kill_houses') class Meta: model = Vet exclude = ( 'id', 'create_date', 'modify_date', 'created_by', 'modified_by', ) def get_kill_houses(self, instance): kill_houses_list = [] kill_house_vets = KillHouseVet.objects.filter(vet=instance, trash=False).select_related( 'kill_house__kill_house_operator__user') if kill_house_vets.count() > 0: for kill_house_vet in kill_house_vets: internal_dict = { "Kill_house_key": kill_house_vet.kill_house.key, "Kill_house_name": kill_house_vet.kill_house.name, "Kill_house_user_full_name": kill_house_vet.kill_house.kill_house_operator.user.fullname, "Kill_house_user_mobile": kill_house_vet.kill_house.kill_house_operator.user.mobile, } kill_houses_list.append(internal_dict) return kill_houses_list class VetFarmListSerializer(serializers.ModelSerializer): user = SystemUserProfileSerializer(read_only=True) address = SystemAddressSerializer(read_only=True) user_bank_info = BankCardSerializer(read_only=True) farms = serializers.SerializerMethodField('get_farms') class Meta: model = Vet exclude = ( 'id', 'create_date', 'modify_date', 'created_by', 'modified_by', ) def get_farms(self, instance): farms_list = [] farms = VetFarm.objects.filter(vet=instance, trash=False).select_related('poultry', 'vet') if farms.count() > 0: for farm in farms: internal_dict = { "poultry_name": farm.poultry.unit_name, "poultry_full_name": farm.poultry.user.fullname, "poultry_mobile": farm.poultry.user.mobile, "poultry_hall": farm.hall, "vet_id": farm.vet.id, "vet_farm_key": farm.key, } farms_list.append(internal_dict) return farms_list class KillHouseOperatorForBarDifferenceRequestSerializer(serializers.ModelSerializer): user = SystemUserProfileForGuildSerializer(read_only=True) class Meta: model = KillHouseOperator fields = ['user'] class KillHouseForBarDifferenceRequestSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForBarDifferenceRequestSerializer(read_only=True) class Meta: model = KillHouse fields = ['kill_house_operator', 'name', 'killer', 'key'] class BarDifferenceRequestSerializer(serializers.ModelSerializer): hatching = PoultryHatchingForBarDifferenceRequestSerializer(read_only=True) kill_house = KillHouseForBarDifferenceRequestSerializer(read_only=True) bar_info = serializers.SerializerMethodField('get_bar_info') class Meta: model = BarDifferenceRequest fields = '__all__' def get_bar_info(self, obj): total_quantity = 0 first_total_quantity = 0 difference_quantity = 0 total_weight = 0 Province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=obj.kill_house, trash=False, state__in=('pending', 'accepted'), return_to_province=False, province_request__poultry_request__hatching=obj.hatching) first_total_quantity = \ Province_kill_requests.aggregate(total=Sum('main_quantity'))[ 'total'] or 0 total_quantity = \ Province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 total_weight = \ Province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 difference_quantity = first_total_quantity - total_quantity # kill_house_requests = KillHouseRequest.objects.filter(Q(killhouse_user=obj.kill_house) | Q(killer=obj.kill_house),trash=False, # province_request__poultry_request__hatching=obj.hatching) # # # total_quantity +=\ # kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))[ # 'total'] or 0 # total_weight +=\ # kill_house_requests.aggregate(total=Sum('accepted_real_weight'))[ # 'total'] or 0 result = { "first_total_quantity": first_total_quantity, "total_quantity": total_quantity, "difference_quantity": difference_quantity, "total_weight": total_weight, } return result class KillHouseOperatorForHatchingDetailSerializer(serializers.ModelSerializer): user = SystemUserProfileForPoultryLocSerializer(read_only=True) address = SystemAddressSerializer(read_only=True) class Meta: model = KillHouseOperator fields = ['user', 'address'] class KillHouseForHatchingDetailSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForHatchingDetailSerializer(read_only=True) class Meta: model = KillHouse fields = ['kill_house_operator', 'name'] class KillHouseRequestForHatchingDetailSerializer(serializers.ModelSerializer): buyer = KillHouseForHatchingDetailSerializer(source='killhouse_user', read_only=True) date = serializers.DateTimeField(source='kill_request.recive_date', read_only=True) killer_info = KillHouseForHatchingDetailSerializer(source='killer', read_only=True) product = serializers.CharField(default='مرغ گرم', read_only=True) status = serializers.SerializerMethodField() poultry_request = serializers.SerializerMethodField() class Meta: model = KillHouseRequest fields = [ 'bar_code', 'date', 'buyer', 'killer_info', 'product', 'clearance_code', 'accepted_real_quantity', 'accepted_real_weight', 'quarantine_quantity', 'status', 'ware_house_accepted_real_quantity', 'ware_house_accepted_real_weight', 'poultry_request' ] read_only_fields = fields def get_status(self, obj): if obj.ware_house_confirmation == True: status = 'ورود به انبار' elif obj.assignment_state_archive == 'True' and obj.ware_house_confirmation == False: status = 'در انتظار ورود به انبار' elif obj.assignment_state_archive == 'True': status = 'تخلیه شده' else: status = 'درانتظار تخلیه' return status def get_poultry_request(self, obj): return PoultryRequestForBarMangementSerializer(obj.province_kill_request.province_request.poultry_request).data class KillHouseForPerformanceDashboardSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForBarDifferenceRequestSerializer(read_only=True) killing_info = serializers.SerializerMethodField('get_killing_info') class Meta: model = KillHouse fields = ['key', 'name', 'kill_house_operator', 'killer', 'killing_info'] def get_killing_info(self, obj): date1 = self.context.get('date1') date2 = self.context.get('date2') province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, killhouse_user=obj, state__in=('pending', 'accepted'), return_to_province=False, archive_wage=False, temporary_trash=False, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, first_car_allocated_quantity=0) kill_house_requests = KillHouseRequest.objects.filter(Q(killer=obj) | Q(killhouse_user=obj), kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, calculate_status=True, temporary_trash=False, trash=False) kill_house_free_bar = KillHouseFreeBarInformation.objects.filter(kill_house=obj, calculate_status=True, buy_type='live', create_date__date__gte=date1, create_date__date__lte=date2, temporary_trash=False, trash=False) province_kill_requests_aggregates = province_kill_requests.aggregate( total_count=Count('id'), total_weight=Sum('total_killed_weight'), total_quantity=Sum('total_killed_quantity') ) kill_house_requests_aggregates = kill_house_requests.aggregate( total_count=Count('id'), total_weight=Sum('accepted_real_weight'), total_quantity=Sum('accepted_real_quantity') ) kill_house_requests_ware_house_true = kill_house_requests.filter(ware_house_confirmation=True).aggregate( total_count=Count('id'), total_weight=Sum('accepted_real_weight'), total_quantity=Sum('accepted_real_quantity') ) kill_house_requests_ware_house_false = kill_house_requests.filter(ware_house_confirmation=False).aggregate( total_count=Count('id'), total_weight=Sum('accepted_real_weight'), total_quantity=Sum('accepted_real_quantity') ) kill_house_free_bar_aggregates = kill_house_free_bar.aggregate( total_count=Count('id'), total_weight=Sum('live_weight'), total_quantity=Sum('quantity') ) total_weight = (province_kill_requests_aggregates['total_weight'] or 0) + ( kill_house_requests_aggregates['total_weight'] or 0) + \ (kill_house_free_bar_aggregates['total_weight'] or 0) total_quantity = (province_kill_requests_aggregates['total_quantity'] or 0) + \ (kill_house_requests_aggregates['total_quantity'] or 0) + \ (kill_house_free_bar_aggregates['total_quantity'] or 0) total_avg_weight = round(total_weight / total_quantity, 1) if total_quantity > 0 else 0 return { "province_kill_requests_count": province_kill_requests_aggregates['total_count'] or 0, "province_kill_requests_quantity": province_kill_requests_aggregates['total_quantity'] or 0, "province_kill_requests_weight": province_kill_requests_aggregates['total_weight'] or 0, "kill_house_requests_count": kill_house_requests_aggregates['total_count'] or 0, "kill_house_requests_quantity": kill_house_requests_aggregates['total_quantity'] or 0, "kill_house_requests_weight": kill_house_requests_aggregates['total_weight'] or 0, "kill_house_free_bar_count": kill_house_free_bar_aggregates['total_count'] or 0, "kill_house_free_bar_quantity": kill_house_free_bar_aggregates['total_quantity'] or 0, "kill_house_free_bar_weight": kill_house_free_bar_aggregates['total_weight'] or 0, "total_quantity": total_quantity, "total_weight": total_weight, "total_avg_weight": total_avg_weight, "total_count": province_kill_requests_aggregates['total_count'] or 0 + kill_house_requests_aggregates[ 'total_count'] or 0 + kill_house_free_bar_aggregates['total_count'] or 0, "kill_house_requests_ware_house_true_quantity": kill_house_requests_ware_house_true['total_quantity'] or 0, "kill_house_requests_ware_house_true_weight": kill_house_requests_ware_house_true['total_weight'] or 0, "kill_house_requests_ware_house_false_quantity": kill_house_requests_ware_house_false[ 'total_quantity'] or 0, "kill_house_requests_ware_house_false_weight": kill_house_requests_ware_house_false['total_weight'] or 0, } class DirectBuyingPaymentSerializer(serializers.ModelSerializer): class Meta: model = DirectBuyingPayment fields = ['key', 'amount', 'image', 'date', 'payment_deadline_supporter_message'] class KillHouseForDirectBuyingSerializer(serializers.ModelSerializer): kill_house_operator = KillHouseOperatorForAutoAllocationsSerializer(read_only=True) class Meta: model = KillHouse fields = ['name', 'kill_house_operator'] class KillRequestForDirectBuyingTrueSerializer(serializers.ModelSerializer): kill_house = KillHouseForDirectBuyingSerializer(read_only=True) slaughter_house = KillHouseForDirectBuyingSerializer(read_only=True) poultry = PoultryForPredictionSerializer(read_only=True) poultry_request = PoultryRequestForMarketSerializer(read_only=True) poultry_hatching = PoultryHatchingForLocSerializer(read_only=True) class Meta: model = KillRequest fields = ['key', 'create_date', 'recive_date', 'free_direct_buying', 'chicken_breed', 'kill_house', 'slaughter_house', 'poultry', 'poultry_request', 'payment_deadline_date', 'payment_deadline_days', 'kill_capacity', 'Index_weight', 'amount', 'market_final_accept', 'market_state', 'recive_time', 'market_code_status', 'market_code', 'input_market_code', 'direct_buying_state', 'free_direct_buying', 'poultry_hatching', 'automatic_accept', 'input_direct_buying_code'] class ReturnProvinceKillRequestSerializer(serializers.ModelSerializer): killhouse_user = KillHouseForReturnBuyingSerializer(read_only=True) poultry_unit_name = serializers.CharField(source='province_request.poultry_request.poultry.unit_name', read_only=True) poultry_fullname = serializers.CharField(source='province_request.poultry_request.poultry.user.fullname', read_only=True) poultry_mobile = serializers.CharField(source='province_request.poultry_request.poultry.user.mobile', read_only=True) poultry_city = serializers.CharField(source='province_request.poultry_request.poultry.user.city.name', read_only=True) killing_date = serializers.CharField(source='province_request.poultry_request.send_date', read_only=True) order_code = serializers.CharField(source='province_request.poultry_request.order_code', read_only=True) poultry_request_quantity = serializers.CharField(source='province_request.poultry_request.quantity', read_only=True) poultry_amount = serializers.CharField(source='province_request.poultry_request.amount', read_only=True) class Meta: model = ProvinceKillRequest fields = '__all__'