import datetime import jdatetime import requests import uuid from django.db.models import Sum, Q, Case, When, F, FloatField from django.http import HttpResponse from rest_framework.response import Response from deposit_id import wage_counting_type, province_id, out_selling_ignore, new_out_selling_count_wage, \ new_out_selling_count_wage_date, before_out_buying_count_wage_amount, market_code_state from general_urls import base_url_for_sms_report from panel.models import KillHouseRequest, KillHouseFreeBarInformation, ColdHouseAllocations, \ KillHouseFreeSaleBarInformation, StewardAllocation, ProvinceKillRequest, WageType, InternalTransaction, KillHouse, \ BarDifferenceRequest, PercentageOfWageType, RolesProducts, KillHouseWareHouse, KillRequest, \ ProvinceCheckOperatorRequest, PoultryRequest, KillHousePercentage, IndexWeightCategory, PosSegmentation, \ WarehouseArchive from ticket.models import TicketSupport, MessageSupport def kill_house_requests_product_warehousing(product): # kill_house_requests = KillHouseRequest.objects.filter(killhouse_user=product.kill_house,input_warehouse=product.kill_house, # ware_house_confirmation=True, trash=False, # calculate_status=True,warehouse=True) kill_house_requests = KillHouseRequest.objects.filter(input_warehouse=product.kill_house, ware_house_confirmation=True, trash=False, calculate_status=True, warehouse=True) governmental_kill_house_requests = kill_house_requests.filter( province_request__poultry_request__free_sale_in_province=False) free_kill_house_requests = kill_house_requests.filter(province_request__poultry_request__free_sale_in_province=True) # Q(province_request__poultry_request__free_sale_in_province=True) | Q( # province_request__poultry_request__direct_buying=True)) product.province_governmental_carcasses_quantity = \ governmental_kill_house_requests.aggregate(total=Sum('ware_house_accepted_real_quantity'))['total'] or 0 product.province_governmental_carcasses_weight = \ governmental_kill_house_requests.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 product.province_free_carcasses_quantity = \ free_kill_house_requests.aggregate(total=Sum('ware_house_accepted_real_quantity'))['total'] or 0 product.province_free_carcasses_weight = \ free_kill_house_requests.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 product.save() governmental_commitment_weight = \ governmental_kill_house_requests.aggregate(total=Sum('warehouse_commitment_weight'))[ 'total'] or 0 free_commitment_weight = \ free_kill_house_requests.aggregate(total=Sum('warehouse_commitment_weight'))['total'] or 0 product.kill_house.total_in_province_governmental_bars_weight = product.province_governmental_carcasses_weight product.kill_house.total_in_province_free_bars_weight = product.province_free_carcasses_weight product.kill_house.total_in_province_governmental_bars_commitment_weight = governmental_commitment_weight product.kill_house.total_in_province_free_bars_commitment_weight = free_commitment_weight product.kill_house.save() def kill_house_free_buying_product_warehousing(product): # kill_house_free_buying_bars = KillHouseFreeBarInformation.objects.filter(kill_house=product.kill_house, trash=False, # calculate_status=True,warehouse=True,input_warehouse=product.kill_house) \ # .exclude(entered_message='ورود به انبار مجازی') kill_house_free_buying_bars = KillHouseFreeBarInformation.objects.filter(trash=False, calculate_status=True, warehouse=True, input_warehouse=product.kill_house) \ .exclude(entered_message='ورود به انبار مجازی') product.free_buying_carcasses_quantity = \ kill_house_free_buying_bars.aggregate(total=Sum('number_of_carcasses'))['total'] or 0 product.free_buying_carcasses_weight = \ kill_house_free_buying_bars.aggregate(total=Sum('weight_of_carcasses'))[ 'total'] or 0 product.save() free_commitment_weight = \ kill_house_free_buying_bars.aggregate(total=Sum('warehouse_commitment_weight'))[ 'total'] or 0 product.kill_house.total_out_province_buying_bars_weight = product.free_buying_carcasses_weight product.kill_house.total_out_province_buying_bars_commitment_weight = free_commitment_weight product.kill_house.save() def kill_house_archive_warehousing(kill_house): product = RolesProducts.objects.filter(kill_house=kill_house, trash=False, name='مرغ گرم').first() archives = WarehouseArchive.objects.filter(kill_house=kill_house, trash=False,warehouse=True) archives_info = archives.aggregate( archives_weight=Sum('weight'), archives_governmental_weight=Sum('weight', filter=Q(quota='governmental')), archives_free_weight=Sum('weight', filter=Q(quota='free')), ) archives_weight = archives_info['archives_weight'] or 0 archives_governmental_weight = archives_info['archives_governmental_weight'] or 0 archives_free_weight = archives_info['archives_free_weight'] or 0 product.ware_house_archive_weight = archives_weight product.save() kill_house.ware_house_archive_governmental_weight = archives_governmental_weight kill_house.ware_house_archive_free_weight = archives_free_weight kill_house.save() def kill_house_cold_house_allocations_product_warehousing(product): cold_house_allocations = ColdHouseAllocations.objects.filter(kill_house=product.kill_house, state__in=('pending', 'accepted'), trash=False) product.freezing_quantity = cold_house_allocations.aggregate(total=Sum('real_quantity'))[ 'total'] or 0 product.freezing_weight = cold_house_allocations.aggregate(total=Sum('real_weight'))[ 'total'] or 0 product.save() def kill_house_cold_house_allocations(cold_house): cold_house_allocations = StewardAllocation.objects.filter(kill_house=cold_house.kill_house, to_cold_house=cold_house, receiver_state='accepted', trash=False, warehouse=True) cold_house.total_input_weight = cold_house_allocations.aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 cold_house.save() cold_house.kill_house.total_cold_house_governmental_weight = \ cold_house_allocations.filter(quota='governmental').aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 cold_house.kill_house.total_cold_house_free_weight = \ cold_house_allocations.filter(quota='free').aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 cold_house.kill_house.save() def cold_house_warehousing(cold_house): cold_house_allocations = StewardAllocation.objects.filter( Q(to_cold_house=cold_house) | Q(other_cold_house=cold_house), receiver_state__in=('accepted', 'pending'), trash=False, warehouse=True) input_allocations = cold_house_allocations.filter( Q(kill_house__isnull=False, to_cold_house=cold_house) | Q( kill_house__isnull=True, other_cold_house=cold_house), receiver_state='accepted') output_allocations = cold_house_allocations.filter( Q(to_steward__isnull=False) | Q(to_guilds__isnull=False) | Q(other_cold_house__isnull=False), kill_house__isnull=True, to_cold_house=cold_house) cold_house.total_input_weight = input_allocations.aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 cold_house.total_allocated_weight = output_allocations.aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 cold_house.save() cold_house.kill_house.total_cold_house_governmental_weight = \ output_allocations.filter(quota='governmental').aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 cold_house.kill_house.total_cold_house_free_weight = \ output_allocations.filter(quota='free').aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 cold_house.kill_house.save() def kill_house_allocations_product_warehousing(product): kill_house_allocations = StewardAllocation.objects.filter( Q(kill_house=product.kill_house) | Q(to_kill_house=product.kill_house), trash=False, calculate_status=True, warehouse=True) kill_house_allocated = kill_house_allocations.filter(kill_house__isnull=False, to_cold_house__isnull=True) kill_house_governmental_allocated = kill_house_allocations.filter(kill_house__isnull=False, to_cold_house__isnull=True, quota='governmental') kill_house_free_allocated = kill_house_allocations.filter(kill_house__isnull=False, to_cold_house__isnull=True, quota='free') kill_house_allocated_from = kill_house_allocations.filter(kill_house__isnull=True, to_cold_house__isnull=True) governmental_kill_house_allocated_from = kill_house_allocated_from.filter(approved_price_status=True) free_kill_house_allocated_from = kill_house_allocated_from.filter(approved_price_status=False) cold_house_allocations = kill_house_allocations.filter(to_cold_house__isnull=False, receiver_state__in=('pending', 'accepted')) product.province_allocated_quantity = \ kill_house_allocated.filter(receiver_state__in=('pending', 'accepted')).aggregate( total=Sum('real_number_of_carcasses'))[ 'total'] or 0 product.province_allocated_weight = \ kill_house_allocated.filter(receiver_state__in=('pending', 'accepted')).aggregate( total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 product.province_governmental_allocated_weight = \ kill_house_governmental_allocated.filter(receiver_state__in=('pending', 'accepted')).aggregate( total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 product.province_free_allocated_weight = \ kill_house_free_allocated.filter(receiver_state__in=('pending', 'accepted')).aggregate( total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 product.receive_governmental_carcasses_quantity = \ governmental_kill_house_allocated_from.filter(receiver_state='accepted').aggregate( total=Sum('receiver_real_number_of_carcasses'))[ 'total'] or 0 product.receive_governmental_carcasses_weight = \ governmental_kill_house_allocated_from.filter(receiver_state='accepted').aggregate( total=Sum('receiver_real_weight_of_carcasses'))[ 'total'] or 0 product.receive_free_carcasses_quantity = \ free_kill_house_allocated_from.filter(receiver_state='accepted').aggregate( total=Sum('receiver_real_number_of_carcasses'))[ 'total'] or 0 product.receive_free_carcasses_weight = \ free_kill_house_allocated_from.filter(receiver_state='accepted').aggregate( total=Sum('receiver_real_weight_of_carcasses'))[ 'total'] or 0 product.cold_house_allocated_weight = cold_house_allocations.aggregate( total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 product.save() product.kill_house.total_selling_in_province_governmental_weight = product.province_governmental_allocated_weight product.kill_house.total_selling_in_province_free_weight = product.province_free_allocated_weight product.kill_house.save() def kill_house_free_sale_product_warehousing(product): kill_house_free_sale_bars = KillHouseFreeSaleBarInformation.objects.filter(kill_house=product.kill_house, trash=False, calculate_status=True, warehouse=True) product.out_province_allocated_quantity = \ kill_house_free_sale_bars.aggregate(total=Sum('real_number_of_carcasses'))[ 'total'] or 0 product.out_province_allocated_weight = kill_house_free_sale_bars.aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 # return ({"len(1)":product.out_province_allocated_quantity,"2":product.out_province_allocated_weight}) product.save() product.kill_house.total_selling_out_province_governmental_weight = \ kill_house_free_sale_bars.filter(quota='governmental').aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 product.kill_house.total_selling_out_province_free_weight = \ kill_house_free_sale_bars.filter(quota='free').aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 product.kill_house.save() def get_new_wage_for_free_buying(kill_house_free_sale_bar_info): final_date = '2025-05-28' after_total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.filter(date__date__gt=new_out_selling_count_wage_date, quarantine_weight_of_carcasses=0).aggregate( total=Sum('real_weight_of_carcasses'))['total'] or 0 after_new_total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.filter(date__date__gt=new_out_selling_count_wage_date, quarantine_weight_of_carcasses__gt=0).aggregate( total_weight=Sum( Case( When(real_weight_of_carcasses__lt=F('quarantine_weight_of_carcasses'), then=F('real_weight_of_carcasses')), default=F('quarantine_weight_of_carcasses'), output_field=FloatField() ) ) ) # before_total_out_selling_province_carcasses_weight = \ # kill_house_free_sale_bar_info.filter(date__date__lte=final_date).filter(date__date__lte=new_out_selling_count_wage_date, # quarantine_weight_of_carcasses=0).aggregate( # total=Sum('real_weight_of_carcasses'))['total'] or 0 before_total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.filter(date__date__lte=final_date).aggregate( total=Sum('real_weight_of_carcasses'))['total'] or 0 # before_new_total_out_selling_province_carcasses_weight = \ # kill_house_free_sale_bar_info.filter(date__date__gt=final_date).filter(date__date__lte=new_out_selling_count_wage_date, # quarantine_weight_of_carcasses__gt=0).aggregate( # total_weight=Sum( # Case( # When(real_weight_of_carcasses__lt=F('quarantine_weight_of_carcasses'), # then=F('real_weight_of_carcasses')), # default=F('quarantine_weight_of_carcasses'), # output_field=FloatField() # ) # ) # ) before_new_total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.filter(date__date__gte=final_date, date__date__lt=new_out_selling_count_wage_date, quarantine_weight_of_carcasses__gt=0).aggregate( total_weight=Sum( Case( When(real_weight_of_carcasses__lt=F('quarantine_weight_of_carcasses'), then=F('real_weight_of_carcasses')), default=F('quarantine_weight_of_carcasses'), output_field=FloatField() ) ) ) before_new_out_selling = before_new_total_out_selling_province_carcasses_weight['total_weight'] or 0 before_total_out_selling_province_carcasses_weight += before_new_out_selling after_new_out_selling = after_new_total_out_selling_province_carcasses_weight['total_weight'] or 0 after_total_out_selling_province_carcasses_weight += after_new_out_selling return { "before_new_out_selling": before_new_out_selling, "before_total_out_selling_province_carcasses_weight": before_total_out_selling_province_carcasses_weight, "after_new_out_selling": after_new_out_selling, "after_total_out_selling_province_carcasses_weight": after_total_out_selling_province_carcasses_weight, } # def get_finance_info(kill_house, date1=None, date2=None): # before_total_out_live_buying_province_carcasses_weight = 0 # after_total_out_live_buying_province_carcasses_weight = 0 # before_new_out_selling = 0 # after_new_out_selling = 0 # extra_company_amount = 0 # total_wage_type = WageType.objects.filter(trash=False) # province_live_wage_type = total_wage_type.filter(en_name='province-kill-request', trash=False).first() # free_buying_live_weight_wage_type = total_wage_type.filter(en_name='live-buy', trash=False).first() # free_buying_carcesses_weight_wage_type = total_wage_type.filter(en_name='carcasse-buy', trash=False).first() # free_sell_carcesses_weight_wage_type = total_wage_type.filter(en_name='carcasse-sell', trash=False).first() # province_live_wage_type_amount = province_live_wage_type.amount if province_live_wage_type.status == True else 0 # 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 # free_buying_carcesses_weight_wage_type_amount = free_buying_carcesses_weight_wage_type.amount if free_buying_carcesses_weight_wage_type.status == True else 0 # free_sell_carcesses_weight_wage_type_amount = free_sell_carcesses_weight_wage_type.amount if free_sell_carcesses_weight_wage_type.status == True else 0 # # if date1: # province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=False, # archive_wage=False, return_to_province=False, # kill_request__recive_date__date__gte=date1, # kill_request__recive_date__date__lte=date2, # state__in=('pending', 'accepted'), # first_car_allocated_quantity=0).order_by('id') # # return_province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=True, # return_trash=True, # archive_wage=False, return_to_province=False, # kill_request__recive_date__date__gte=date1, # kill_request__recive_date__date__lte=date2, # state__in=('pending', 'accepted'), # first_car_allocated_quantity=0).order_by( # 'id') # kill_house_requests = KillHouseRequest.objects.filter( # Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( # Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( # Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, # kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, # trash=False, calculate_status=True # ) # # return_kill_house_requests = KillHouseRequest.objects.filter( # Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( # Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( # Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, # kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, # trash=True, return_trash=True, calculate_status=True # ) # kill_house_free_bar_info = KillHouseFreeBarInformation.objects.filter( # Q(kill_house=kill_house) | Q(exclusive_killer=kill_house), # archive_wage=False, # calculate_status=True, date__date__gte=date1, date__date__lte=date2, # trash=False) # kill_house_free_sale_bar_info = KillHouseFreeSaleBarInformation.objects.filter( # kill_house=kill_house, # archive_wage=False, # calculate_status=True, date__date__gte=date1, date__date__lte=date2, # trash=False) # slaughter_transactions = InternalTransaction.objects.filter( # kill_house=kill_house, status='completed', date__date__gte=date1, date__date__lte=date2, # trash=False) # difference_requests = BarDifferenceRequest.objects.filter(kill_house=kill_house, trash=False, state='accepted', # create_date__date__gte=date1, # create_date__date__lte=date2) # else: # # province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=False, # archive_wage=False, return_to_province=False, # state__in=('pending', 'accepted'), # first_car_allocated_quantity=0).order_by('id') # # return_province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=True, # return_trash=True, # archive_wage=False, return_to_province=False, # state__in=('pending', 'accepted'), # first_car_allocated_quantity=0).order_by( # 'id') # kill_house_requests = KillHouseRequest.objects.filter( # Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( # Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( # Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, # trash=False, calculate_status=True # ) # return_kill_house_requests = KillHouseRequest.objects.filter( # Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( # Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( # Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, # trash=True, return_trash=True, calculate_status=True # ) # kill_house_free_bar_info = KillHouseFreeBarInformation.objects.filter( # Q(kill_house=kill_house) | Q(exclusive_killer=kill_house), # archive_wage=False, # calculate_status=True, # trash=False) # kill_house_free_sale_bar_info = KillHouseFreeSaleBarInformation.objects.filter( # kill_house=kill_house, # archive_wage=False, # calculate_status=True, # trash=False) # slaughter_transactions = InternalTransaction.objects.filter( # kill_house=kill_house, status='completed', # trash=False) # difference_requests = BarDifferenceRequest.objects.filter(kill_house=kill_house, trash=False, state='accepted') # # total_paid_wage = slaughter_transactions.aggregate(total=Sum('amount'))[ # 'total'] or 0 # total_province_live_weight = province_kill_requests.aggregate(total=Sum('total_killed_weight'))['total'] or 0 # total_province_live_weight += kill_house_requests.aggregate(total=Sum('accepted_real_weight'))['total'] or 0 # total_province_live_weight += \ # difference_requests.aggregate(total=Sum('weight'))['total'] or 0 # difference_requests_weight = \ # difference_requests.aggregate(total=Sum('weight'))['total'] or 0 # return_total_province_live_weight = return_province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ # 'total'] or 0 # return_total_province_live_weight += return_kill_house_requests.aggregate(total=Sum('accepted_real_weight'))[ # 'total'] or 0 # # final_date = '2025-05-28' # total_out_selling_province_carcasses_weight = \ # kill_house_free_sale_bar_info.filter(date__date__lte=final_date).aggregate( # total=Sum('real_weight_of_carcasses'))['total'] or 0 # new_total_out_selling_province_carcasses_weight = \ # kill_house_free_sale_bar_info.filter(date__date__gt=final_date, # quarantine_weight_of_carcasses__gt=0).aggregate( # total_weight=Sum( # Case( # When(real_weight_of_carcasses__lt=F('quarantine_weight_of_carcasses'), # then=F('real_weight_of_carcasses')), # default=F('quarantine_weight_of_carcasses'), # output_field=FloatField() # ) # ) # ) # new_out_selling = new_total_out_selling_province_carcasses_weight['total_weight'] or 0 # # total_out_selling_province_carcasses_weight += new_out_selling # # if wage_counting_type == 'live': # total_province_carcasses_weight = total_province_live_weight # total_pure_province_carcasses_weight = total_province_carcasses_weight # else: # return_total_province_live_weight = int(return_total_province_live_weight * 0.75) # total_province_carcasses_weight = total_province_live_weight * 0.75 # difference_requests_weight = difference_requests_weight * 0.75 # total_out_carcasses_buying_for_pure_province_carcasses_weight = \ # kill_house_free_bar_info.aggregate(total=Sum('weight_of_carcasses'))['total'] or 0 # out_selling_out_carcasses_buying_difference = total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight if ( # total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight) > 0 else 0 # total_pure_province_carcasses_weight = total_province_carcasses_weight - out_selling_out_carcasses_buying_difference # total_out_live_buying_province_carcasses_weight = \ # kill_house_free_bar_info.filter(buy_type='live').aggregate(total=Sum('live_weight'))['total'] or 0 # real_total_out_live_buying_province_carcasses_weight = total_out_live_buying_province_carcasses_weight # if new_out_selling_count_wage: # if out_selling_ignore: # before_total_out_live_buying_province_carcasses_weight = \ # kill_house_free_bar_info.filter(create_date__date__lt=new_out_selling_count_wage_date, # buy_type='live').aggregate(total=Sum('live_weight'))['total'] or 0 # after_total_out_live_buying_province_carcasses_weight = \ # kill_house_free_bar_info.filter(create_date__date__gte=new_out_selling_count_wage_date, # buy_type='live').aggregate(total=Sum('live_weight'))['total'] or 0 # get_new_wage = get_new_wage_for_free_buying(kill_house_free_sale_bar_info) # # before_new_out_selling = get_new_wage['before_new_out_selling'] # after_new_out_selling = get_new_wage['after_new_out_selling'] # max_amount = before_total_out_live_buying_province_carcasses_weight * 0.80 # if max_amount <= before_new_out_selling: # before_total_out_live_buying_province_carcasses_weight -= max_amount # else: # before_total_out_live_buying_province_carcasses_weight -= before_new_out_selling # # if after_total_out_live_buying_province_carcasses_weight > after_new_out_selling: # extra_company_amount = int(after_new_out_selling * extra_company_amount) # after_total_out_live_buying_province_carcasses_weight -= after_new_out_selling # # else: # extra_company_amount = int(after_total_out_live_buying_province_carcasses_weight * extra_company_amount) # # after_total_out_live_buying_province_carcasses_weight = 0 # # total_out_live_buying_province_carcasses_weight = before_total_out_live_buying_province_carcasses_weight + after_total_out_live_buying_province_carcasses_weight # # else: # if out_selling_ignore: # max_amount = total_out_live_buying_province_carcasses_weight * 0.80 # if max_amount <= new_out_selling: # total_out_live_buying_province_carcasses_weight -= max_amount # else: # total_out_live_buying_province_carcasses_weight -= new_out_selling # # # else: # # total_out_live_buying_province_carcasses_weight -= total_out_selling_province_quarantine_carcasses_weight # total_out_carcasses_buying_province_carcasses_weight = \ # kill_house_free_bar_info.filter(buy_type='carcass').aggregate(total=Sum('weight_of_carcasses'))['total'] or 0 # # total_pure_province_carcasses_price = total_pure_province_carcasses_weight * province_live_wage_type_amount # difference_requests_price = difference_requests_weight * province_live_wage_type_amount # total_return_pure_province_carcasses_price = return_total_province_live_weight * province_live_wage_type_amount # total_out_selling_province_carcasses_price = total_out_selling_province_carcasses_weight * free_sell_carcesses_weight_wage_type_amount # if new_out_selling_count_wage: # total_out_live_buying_province_carcasses_price = int( # (before_total_out_live_buying_province_carcasses_weight * before_out_buying_count_wage_amount) + ( # after_total_out_live_buying_province_carcasses_weight * free_buying_live_weight_wage_type_amount)) # else: # total_out_live_buying_province_carcasses_price = total_out_live_buying_province_carcasses_weight * free_buying_live_weight_wage_type_amount # # total_out_carcasses_buying_province_carcasses_price = total_out_carcasses_buying_province_carcasses_weight * free_buying_carcesses_weight_wage_type_amount # total_price = total_pure_province_carcasses_price + total_out_selling_province_carcasses_price + total_out_live_buying_province_carcasses_price + total_out_carcasses_buying_province_carcasses_price + extra_company_amount + total_return_pure_province_carcasses_price # # return { # "total_province_live_weight": total_province_live_weight, # "total_province_carcasses_weight": total_province_carcasses_weight, # "total_out_selling_province_carcasses_weight": total_out_selling_province_carcasses_weight, # "total_pure_province_carcasses_weight": total_pure_province_carcasses_weight, # "total_pure_province_carcasses_price": total_pure_province_carcasses_price, # "total_out_selling_province_carcasses_price": total_out_selling_province_carcasses_price, # "total_out_carcasses_buying_province_carcasses_weight": total_out_carcasses_buying_province_carcasses_weight, # "total_out_carcasses_buying_province_carcasses_price": total_out_carcasses_buying_province_carcasses_price, # "total_out_live_buying_province_carcasses_weight": real_total_out_live_buying_province_carcasses_weight, # "total_out_live_buying_province_carcasses_price": total_out_live_buying_province_carcasses_price, # "total_paid_wage": total_paid_wage, # "total_price": total_price, # "province_live_wage_amount": province_live_wage_type_amount, # "free_buying_live_weight_amount": free_buying_live_weight_wage_type_amount, # "free_buying_carcesses_weight_amount": free_buying_carcesses_weight_wage_type_amount, # "free_sell_carcesses_weight_amount": free_sell_carcesses_weight_wage_type_amount, # "extra_company_amount": extra_company_amount, # "before_total_out_live_buying_province_carcasses_weight": before_total_out_live_buying_province_carcasses_weight, # "after_total_out_live_buying_province_carcasses_weight": after_total_out_live_buying_province_carcasses_weight, # "before_new_out_selling": before_new_out_selling, # "after_new_out_selling": after_new_out_selling, # "return_total_province_live_weight": return_total_province_live_weight, # "total_return_pure_province_carcasses_price": total_return_pure_province_carcasses_price, # "difference_requests_weight": difference_requests_weight, # "difference_requests_price": difference_requests_price, # # } def get_finance_info(kill_house, date1=None, date2=None): before_total_out_live_buying_province_carcasses_weight = 0 after_total_out_live_buying_province_carcasses_weight = 0 before_new_out_selling = 0 after_new_out_selling = 0 extra_company_amount = 0 total_wage_type = WageType.objects.filter(trash=False) province_live_wage_type = total_wage_type.filter(en_name='province-kill-request', trash=False).first() free_buying_live_weight_wage_type = total_wage_type.filter(en_name='live-buy', trash=False).first() free_buying_carcesses_weight_wage_type = total_wage_type.filter(en_name='carcasse-buy', trash=False).first() free_sell_carcesses_weight_wage_type = total_wage_type.filter(en_name='carcasse-sell', trash=False).first() province_live_wage_type_amount = province_live_wage_type.amount if province_live_wage_type.status == True else 0 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 free_buying_carcesses_weight_wage_type_amount = free_buying_carcesses_weight_wage_type.amount if free_buying_carcesses_weight_wage_type.status == True else 0 free_sell_carcesses_weight_wage_type_amount = free_sell_carcesses_weight_wage_type.amount if free_sell_carcesses_weight_wage_type.status == True else 0 if date1: province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=False, archive_wage=False, return_to_province=False, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, state__in=('pending', 'accepted'), first_car_allocated_quantity=0).order_by('id') return_province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=True, return_trash=True, archive_wage=False, return_to_province=False, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, state__in=('pending', 'accepted'), first_car_allocated_quantity=0).order_by( 'id') if kill_house.killer and kill_house.type == 'public': kill_house_requests = KillHouseRequest.objects.filter( killhouse_user=kill_house, archive_wage=False, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, trash=False, calculate_status=True ) return_kill_house_requests = KillHouseRequest.objects.filter( killhouse_user=kill_house, archive_wage=False, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, trash=True, return_trash=True, calculate_status=True ) else: kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, trash=False, calculate_status=True ) return_kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, trash=True, return_trash=True, calculate_status=True ) kill_house_free_bar_info = KillHouseFreeBarInformation.objects.filter( Q(kill_house=kill_house) | Q(exclusive_killer=kill_house), archive_wage=False, calculate_status=True, date__date__gte=date1, date__date__lte=date2, trash=False) kill_house_free_sale_bar_info = KillHouseFreeSaleBarInformation.objects.filter( kill_house=kill_house, archive_wage=False, calculate_status=True, date__date__gte=date1, date__date__lte=date2, trash=False) slaughter_transactions = InternalTransaction.objects.filter( kill_house=kill_house, status='completed', date__date__gte=date1, date__date__lte=date2, trash=False) difference_requests = BarDifferenceRequest.objects.filter(kill_house=kill_house, trash=False, state='accepted', create_date__date__gte=date1, create_date__date__lte=date2) else: province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=False, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0).order_by('id') return_province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=True, return_trash=True, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0).order_by( 'id') if kill_house.killer and kill_house.type == 'public': kill_house_requests = KillHouseRequest.objects.filter( killhouse_user=kill_house, archive_wage=False, trash=False, calculate_status=True ) return_kill_house_requests = KillHouseRequest.objects.filter( killhouse_user=kill_house, archive_wage=False, trash=True, return_trash=True, calculate_status=True ) else: kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, trash=False, calculate_status=True ) return_kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, trash=True, return_trash=True, calculate_status=True ) kill_house_free_bar_info = KillHouseFreeBarInformation.objects.filter( Q(kill_house=kill_house) | Q(exclusive_killer=kill_house), archive_wage=False, calculate_status=True, trash=False) kill_house_free_sale_bar_info = KillHouseFreeSaleBarInformation.objects.filter( kill_house=kill_house, archive_wage=False, calculate_status=True, trash=False) slaughter_transactions = InternalTransaction.objects.filter( kill_house=kill_house, status='completed', trash=False) difference_requests = BarDifferenceRequest.objects.filter(kill_house=kill_house, trash=False, state='accepted') total_paid_wage = slaughter_transactions.aggregate(total=Sum('amount'))[ 'total'] or 0 total_province_live_weight = province_kill_requests.aggregate(total=Sum('total_killed_weight'))['total'] or 0 total_province_live_weight += kill_house_requests.aggregate(total=Sum('accepted_real_weight'))['total'] or 0 total_province_live_weight += \ difference_requests.aggregate(total=Sum('weight'))['total'] or 0 difference_requests_weight = \ difference_requests.aggregate(total=Sum('weight'))['total'] or 0 return_total_province_live_weight = return_province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 return_total_province_live_weight += return_kill_house_requests.aggregate(total=Sum('accepted_real_weight'))[ 'total'] or 0 final_date = '2025-05-28' total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.filter(date__date__lte=final_date).aggregate( total=Sum('real_weight_of_carcasses'))['total'] or 0 new_total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.filter(date__date__gt=final_date, quarantine_weight_of_carcasses__gt=0).aggregate( total_weight=Sum( Case( When(real_weight_of_carcasses__lt=F('quarantine_weight_of_carcasses'), then=F('real_weight_of_carcasses')), default=F('quarantine_weight_of_carcasses'), output_field=FloatField() ) ) ) new_out_selling = new_total_out_selling_province_carcasses_weight['total_weight'] or 0 total_out_selling_province_carcasses_weight += new_out_selling if wage_counting_type == 'live': total_province_carcasses_weight = total_province_live_weight total_pure_province_carcasses_weight = total_province_carcasses_weight else: return_total_province_live_weight = int(return_total_province_live_weight * 0.75) total_province_carcasses_weight = total_province_live_weight * 0.75 difference_requests_weight = difference_requests_weight * 0.75 total_out_carcasses_buying_for_pure_province_carcasses_weight = \ kill_house_free_bar_info.aggregate(total=Sum('weight_of_carcasses'))['total'] or 0 out_selling_out_carcasses_buying_difference = total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight if ( total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight) > 0 else 0 total_pure_province_carcasses_weight = total_province_carcasses_weight - out_selling_out_carcasses_buying_difference total_out_live_buying_province_carcasses_weight = \ kill_house_free_bar_info.filter(buy_type='live').aggregate(total=Sum('live_weight'))['total'] or 0 real_total_out_live_buying_province_carcasses_weight = total_out_live_buying_province_carcasses_weight if new_out_selling_count_wage: if out_selling_ignore: before_total_out_live_buying_province_carcasses_weight = \ kill_house_free_bar_info.filter(create_date__date__lt=new_out_selling_count_wage_date, buy_type='live').aggregate(total=Sum('live_weight'))['total'] or 0 after_total_out_live_buying_province_carcasses_weight = \ kill_house_free_bar_info.filter(create_date__date__gte=new_out_selling_count_wage_date, buy_type='live').aggregate(total=Sum('live_weight'))['total'] or 0 get_new_wage = get_new_wage_for_free_buying(kill_house_free_sale_bar_info) before_new_out_selling = get_new_wage['before_new_out_selling'] after_new_out_selling = get_new_wage['after_new_out_selling'] max_amount = before_total_out_live_buying_province_carcasses_weight * 0.80 if max_amount <= before_new_out_selling: before_total_out_live_buying_province_carcasses_weight -= max_amount else: before_total_out_live_buying_province_carcasses_weight -= before_new_out_selling if after_total_out_live_buying_province_carcasses_weight > after_new_out_selling: extra_company_amount = int(after_new_out_selling * extra_company_amount) after_total_out_live_buying_province_carcasses_weight -= after_new_out_selling else: extra_company_amount = int(after_total_out_live_buying_province_carcasses_weight * extra_company_amount) after_total_out_live_buying_province_carcasses_weight = 0 total_out_live_buying_province_carcasses_weight = before_total_out_live_buying_province_carcasses_weight + after_total_out_live_buying_province_carcasses_weight else: if out_selling_ignore: max_amount = total_out_live_buying_province_carcasses_weight * 0.80 if max_amount <= new_out_selling: total_out_live_buying_province_carcasses_weight -= max_amount else: total_out_live_buying_province_carcasses_weight -= new_out_selling # else: # total_out_live_buying_province_carcasses_weight -= total_out_selling_province_quarantine_carcasses_weight total_out_carcasses_buying_province_carcasses_weight = \ kill_house_free_bar_info.filter(buy_type='carcass').aggregate(total=Sum('weight_of_carcasses'))['total'] or 0 total_pure_province_carcasses_price = total_pure_province_carcasses_weight * province_live_wage_type_amount difference_requests_price = difference_requests_weight * province_live_wage_type_amount total_return_pure_province_carcasses_price = return_total_province_live_weight * province_live_wage_type_amount total_out_selling_province_carcasses_price = total_out_selling_province_carcasses_weight * free_sell_carcesses_weight_wage_type_amount if new_out_selling_count_wage: total_out_live_buying_province_carcasses_price = int( (before_total_out_live_buying_province_carcasses_weight * before_out_buying_count_wage_amount) + ( after_total_out_live_buying_province_carcasses_weight * free_buying_live_weight_wage_type_amount)) else: total_out_live_buying_province_carcasses_price = total_out_live_buying_province_carcasses_weight * free_buying_live_weight_wage_type_amount total_out_carcasses_buying_province_carcasses_price = total_out_carcasses_buying_province_carcasses_weight * free_buying_carcesses_weight_wage_type_amount total_price = total_pure_province_carcasses_price + total_out_selling_province_carcasses_price + total_out_live_buying_province_carcasses_price + total_out_carcasses_buying_province_carcasses_price + extra_company_amount + total_return_pure_province_carcasses_price return { "total_province_live_weight": total_province_live_weight, "total_province_carcasses_weight": total_province_carcasses_weight, "total_out_selling_province_carcasses_weight": total_out_selling_province_carcasses_weight, "total_pure_province_carcasses_weight": total_pure_province_carcasses_weight, "total_pure_province_carcasses_price": total_pure_province_carcasses_price, "total_out_selling_province_carcasses_price": total_out_selling_province_carcasses_price, "total_out_carcasses_buying_province_carcasses_weight": total_out_carcasses_buying_province_carcasses_weight, "total_out_carcasses_buying_province_carcasses_price": total_out_carcasses_buying_province_carcasses_price, "total_out_live_buying_province_carcasses_weight": real_total_out_live_buying_province_carcasses_weight, "total_out_live_buying_province_carcasses_price": total_out_live_buying_province_carcasses_price, "total_paid_wage": total_paid_wage, "total_price": total_price, "province_live_wage_amount": province_live_wage_type_amount, "free_buying_live_weight_amount": free_buying_live_weight_wage_type_amount, "free_buying_carcesses_weight_amount": free_buying_carcesses_weight_wage_type_amount, "free_sell_carcesses_weight_amount": free_sell_carcesses_weight_wage_type_amount, "extra_company_amount": extra_company_amount, "before_total_out_live_buying_province_carcasses_weight": before_total_out_live_buying_province_carcasses_weight, "after_total_out_live_buying_province_carcasses_weight": after_total_out_live_buying_province_carcasses_weight, "before_new_out_selling": before_new_out_selling, "after_new_out_selling": after_new_out_selling, "return_total_province_live_weight": return_total_province_live_weight, "total_return_pure_province_carcasses_price": total_return_pure_province_carcasses_price, "difference_requests_weight": difference_requests_weight, "difference_requests_price": difference_requests_price, } def get_kill_house_finance_info(kill_house, date1=None, date2=None): before_total_out_live_buying_province_carcasses_weight = 0 after_total_out_live_buying_province_carcasses_weight = 0 before_new_out_selling = 0 after_new_out_selling = 0 extra_company_amount = 0 total_wage_type = WageType.objects.filter(trash=False) province_live_wage_type = total_wage_type.filter(en_name='province-kill-request', trash=False).first() free_buying_live_weight_wage_type = total_wage_type.filter(en_name='live-buy', trash=False).first() free_buying_carcesses_weight_wage_type = total_wage_type.filter(en_name='carcasse-buy', trash=False).first() free_sell_carcesses_weight_wage_type = total_wage_type.filter(en_name='carcasse-sell', trash=False).first() province_live_wage_type_amount = province_live_wage_type.amount if province_live_wage_type.status == True else 0 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 free_buying_carcesses_weight_wage_type_amount = free_buying_carcesses_weight_wage_type.amount if free_buying_carcesses_weight_wage_type.status == True else 0 free_sell_carcesses_weight_wage_type_amount = free_sell_carcesses_weight_wage_type.amount if free_sell_carcesses_weight_wage_type.status == True else 0 if date1: province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=False, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, first_car_allocated_quantity=0).order_by('id') return_province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=True, return_trash=True, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, first_car_allocated_quantity=0).order_by( 'id') kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, trash=False, calculate_status=True, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, ) kill_house_requests = kill_house_requests.filter( Q(killhouse_user=kill_house, killer__isnull=True) | Q(killhouse_user=kill_house, killer=kill_house) | Q( killer=kill_house)) return_kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, trash=True, return_trash=True, calculate_status=True, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, ) return_kill_house_requests = return_kill_house_requests.filter( Q(killhouse_user=kill_house, killer__isnull=True) | Q(killhouse_user=kill_house, killer=kill_house) | Q( killer=kill_house)) kill_house_free_bar_info = KillHouseFreeBarInformation.objects.filter( Q(kill_house=kill_house, exclusive_killer__isnull=True) | Q(exclusive_killer=kill_house), archive_wage=False, calculate_status=True, create_date__date__gte=date1, create_date__date__lte=date2, trash=False) kill_house_free_sale_bar_info = KillHouseFreeSaleBarInformation.objects.filter( kill_house=kill_house, archive_wage=False, calculate_status=True, date__date__gte=date1, date__date__lte=date2, trash=False) slaughter_transactions = InternalTransaction.objects.filter( kill_house=kill_house, status='completed', date__date__gte=date1, date__date__lte=date2, trash=False) difference_requests = BarDifferenceRequest.objects.filter(kill_house=kill_house, trash=False, state='accepted', create_date__date__gte=date1, create_date__date__lte=date2, ) else: province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=False, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0).order_by('id') return_province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user=kill_house, trash=True, return_trash=True, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0).order_by( 'id') kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, trash=False, calculate_status=True ) kill_house_requests = kill_house_requests.filter( Q(killhouse_user=kill_house, killer__isnull=True) | Q(killhouse_user=kill_house, killer=kill_house) | Q( killer=kill_house)) return_kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user=kill_house) & Q(killer=kill_house)) | Q( Q(killhouse_user=kill_house) & Q(killer__isnull=True)) | Q( Q(killhouse_user=kill_house) | Q(killer=kill_house)), archive_wage=False, trash=True, return_trash=True, calculate_status=True ) return_kill_house_requests = return_kill_house_requests.filter( Q(killhouse_user=kill_house, killer__isnull=True) | Q(killhouse_user=kill_house, killer=kill_house) | Q( killer=kill_house)) kill_house_free_bar_info = KillHouseFreeBarInformation.objects.filter( Q(kill_house=kill_house, exclusive_killer__isnull=True) | Q(exclusive_killer=kill_house), archive_wage=False, calculate_status=True, trash=False) kill_house_free_sale_bar_info = KillHouseFreeSaleBarInformation.objects.filter( kill_house=kill_house, archive_wage=False, calculate_status=True, trash=False) slaughter_transactions = InternalTransaction.objects.filter( kill_house=kill_house, status='completed', trash=False) difference_requests = BarDifferenceRequest.objects.filter(kill_house=kill_house, trash=False, state='accepted') total_paid_wage = slaughter_transactions.aggregate(total=Sum('amount'))[ 'total'] or 0 total_province_live_weight = province_kill_requests.aggregate(total=Sum('total_killed_weight'))['total'] or 0 total_province_live_weight += kill_house_requests.aggregate(total=Sum('accepted_real_weight'))['total'] or 0 # total_out_selling_province_carcasses_weight = \ # kill_house_free_sale_bar_info.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0 # sum_quarantine_carcasses_weight = \ # kill_house_free_sale_bar_info.filter(quarantine_weight_of_carcasses__gt=0).aggregate( # total_weight=Sum( # Case( # When(real_weight_of_carcasses__lt=F('quarantine_weight_of_carcasses'), # then=F('real_weight_of_carcasses')), # default=F('quarantine_weight_of_carcasses'), # output_field=FloatField() # ) # ) # ) # total_out_selling_province_quarantine_carcasses_weight = sum_quarantine_carcasses_weight['total_weight'] or 0 total_province_live_weight += \ difference_requests.aggregate(total=Sum('weight'))['total'] or 0 difference_requests_weight = \ difference_requests.aggregate(total=Sum('weight'))['total'] or 0 return_total_province_live_weight = return_province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 return_total_province_live_weight += return_kill_house_requests.aggregate(total=Sum('accepted_real_weight'))[ 'total'] or 0 final_date = '2025-05-28' total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.filter(date__date__lte=final_date).aggregate( total=Sum('real_weight_of_carcasses'))['total'] or 0 new_total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.filter(date__date__gt=final_date, quarantine_weight_of_carcasses__gt=0).aggregate( total_weight=Sum( Case( When(real_weight_of_carcasses__lt=F('quarantine_weight_of_carcasses'), then=F('real_weight_of_carcasses')), default=F('quarantine_weight_of_carcasses'), output_field=FloatField() ) ) ) new_out_selling = new_total_out_selling_province_carcasses_weight['total_weight'] or 0 total_out_selling_province_carcasses_weight += new_out_selling if wage_counting_type == 'live': total_province_carcasses_weight = total_province_live_weight total_pure_province_carcasses_weight = total_province_carcasses_weight else: return_total_province_live_weight = int(return_total_province_live_weight * 0.75) total_province_carcasses_weight = total_province_live_weight * 0.75 difference_requests_weight = difference_requests_weight * 0.75 total_out_carcasses_buying_for_pure_province_carcasses_weight = \ kill_house_free_bar_info.aggregate(total=Sum('weight_of_carcasses'))['total'] or 0 out_selling_out_carcasses_buying_difference = total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight if ( total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight) > 0 else 0 total_pure_province_carcasses_weight = total_province_carcasses_weight - out_selling_out_carcasses_buying_difference total_out_live_buying_province_carcasses_weight = \ kill_house_free_bar_info.filter(buy_type='live').aggregate(total=Sum('live_weight'))['total'] or 0 real_total_out_live_buying_province_carcasses_weight = total_out_live_buying_province_carcasses_weight if new_out_selling_count_wage: if out_selling_ignore: before_total_out_live_buying_province_carcasses_weight = \ kill_house_free_bar_info.filter(create_date__date__lt=new_out_selling_count_wage_date, buy_type='live').aggregate(total=Sum('live_weight'))['total'] or 0 after_total_out_live_buying_province_carcasses_weight = \ kill_house_free_bar_info.filter(create_date__date__gte=new_out_selling_count_wage_date, buy_type='live').aggregate(total=Sum('live_weight'))['total'] or 0 get_new_wage = get_new_wage_for_free_buying(kill_house_free_sale_bar_info) before_new_out_selling = get_new_wage['before_new_out_selling'] after_new_out_selling = get_new_wage['after_new_out_selling'] max_amount = before_total_out_live_buying_province_carcasses_weight * 0.80 if max_amount <= before_new_out_selling: before_total_out_live_buying_province_carcasses_weight -= max_amount else: before_total_out_live_buying_province_carcasses_weight -= before_new_out_selling if after_total_out_live_buying_province_carcasses_weight > after_new_out_selling: extra_company_amount = int(after_new_out_selling * extra_company_amount) after_total_out_live_buying_province_carcasses_weight -= after_new_out_selling else: extra_company_amount = int(after_total_out_live_buying_province_carcasses_weight * extra_company_amount) after_total_out_live_buying_province_carcasses_weight = 0 total_out_live_buying_province_carcasses_weight = before_total_out_live_buying_province_carcasses_weight + after_total_out_live_buying_province_carcasses_weight else: if out_selling_ignore: max_amount = total_out_live_buying_province_carcasses_weight * 0.80 if max_amount <= new_out_selling: total_out_live_buying_province_carcasses_weight -= max_amount else: total_out_live_buying_province_carcasses_weight -= new_out_selling # else: # total_out_live_buying_province_carcasses_weight -= total_out_selling_province_quarantine_carcasses_weight total_out_carcasses_buying_province_carcasses_weight = \ kill_house_free_bar_info.filter(buy_type='carcass').aggregate(total=Sum('weight_of_carcasses'))['total'] or 0 total_pure_province_carcasses_price = total_pure_province_carcasses_weight * province_live_wage_type_amount difference_requests_price = difference_requests_weight * province_live_wage_type_amount total_return_pure_province_carcasses_price = return_total_province_live_weight * province_live_wage_type_amount total_out_selling_province_carcasses_price = total_out_selling_province_carcasses_weight * free_sell_carcesses_weight_wage_type_amount if new_out_selling_count_wage: total_out_live_buying_province_carcasses_price = int( (before_total_out_live_buying_province_carcasses_weight * before_out_buying_count_wage_amount) + ( after_total_out_live_buying_province_carcasses_weight * free_buying_live_weight_wage_type_amount)) else: total_out_live_buying_province_carcasses_price = total_out_live_buying_province_carcasses_weight * free_buying_live_weight_wage_type_amount total_out_carcasses_buying_province_carcasses_price = total_out_carcasses_buying_province_carcasses_weight * free_buying_carcesses_weight_wage_type_amount total_price = total_pure_province_carcasses_price + total_out_selling_province_carcasses_price + total_out_live_buying_province_carcasses_price + total_out_carcasses_buying_province_carcasses_price + extra_company_amount + total_return_pure_province_carcasses_price return { "total_province_live_weight": total_province_live_weight, "total_province_carcasses_weight": total_province_carcasses_weight, "total_out_selling_province_carcasses_weight": total_out_selling_province_carcasses_weight, "total_pure_province_carcasses_weight": total_pure_province_carcasses_weight, "total_pure_province_carcasses_price": total_pure_province_carcasses_price, "total_out_selling_province_carcasses_price": total_out_selling_province_carcasses_price, "total_out_carcasses_buying_province_carcasses_weight": total_out_carcasses_buying_province_carcasses_weight, "total_out_carcasses_buying_province_carcasses_price": total_out_carcasses_buying_province_carcasses_price, "total_out_live_buying_province_carcasses_weight": real_total_out_live_buying_province_carcasses_weight, "total_out_live_buying_province_carcasses_price": total_out_live_buying_province_carcasses_price, "total_paid_wage": total_paid_wage, "total_price": total_price, "province_live_wage_amount": province_live_wage_type_amount, "free_buying_live_weight_amount": free_buying_live_weight_wage_type_amount, "free_buying_carcesses_weight_amount": free_buying_carcesses_weight_wage_type_amount, "free_sell_carcesses_weight_amount": free_sell_carcesses_weight_wage_type_amount, "extra_company_amount": extra_company_amount, "before_total_out_live_buying_province_carcasses_weight": before_total_out_live_buying_province_carcasses_weight, "after_total_out_live_buying_province_carcasses_weight": after_total_out_live_buying_province_carcasses_weight, "before_new_out_selling": before_new_out_selling, "after_new_out_selling": after_new_out_selling, "return_total_province_live_weight": return_total_province_live_weight, "total_return_pure_province_carcasses_price": total_return_pure_province_carcasses_price, "difference_requests_weight": difference_requests_weight, "difference_requests_price": difference_requests_price, } def get_difference_carcasses_weight(kill_houses, date1=None, date2=None): difference_weight = 0 for kill_house in kill_houses: if date1: kill_house_free_bar_info = KillHouseFreeBarInformation.objects.filter(kill_house=kill_house, archive_wage=False, calculate_status=True, trash=False, date__date__gte=date1, date__date__lte=date2) kill_house_free_sale_bar_info = KillHouseFreeSaleBarInformation.objects.filter( kill_house=kill_house, archive_wage=False, calculate_status=True, trash=False, date__date__gte=date1, date__date__lte=date2) else: kill_house_free_bar_info = KillHouseFreeBarInformation.objects.filter(kill_house=kill_house, archive_wage=False, calculate_status=True, trash=False) kill_house_free_sale_bar_info = KillHouseFreeSaleBarInformation.objects.filter( kill_house=kill_house, archive_wage=False, calculate_status=True, trash=False) total_out_carcasses_buying_for_pure_province_carcasses_weight = \ kill_house_free_bar_info.aggregate(total=Sum('weight_of_carcasses'))['total'] or 0 # total_out_selling_province_carcasses_weight = \ # kill_house_free_sale_bar_info.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0 final_date = '2025-05-28' total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.filter(date__date__lte=final_date).aggregate( total=Sum('real_weight_of_carcasses'))['total'] or 0 new_total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.filter(date__date__gt=final_date, quarantine_weight_of_carcasses__gt=0).aggregate( total_weight=Sum( Case( When(real_weight_of_carcasses__lt=F('quarantine_weight_of_carcasses'), then=F('real_weight_of_carcasses')), default=F('quarantine_weight_of_carcasses'), output_field=FloatField() ) ) ) total_out_selling_province_carcasses_weight += new_total_out_selling_province_carcasses_weight[ 'total_weight'] or 0 out_selling_out_carcasses_buying_difference = total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight if ( total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight) > 0 else 0 difference_weight += out_selling_out_carcasses_buying_difference return difference_weight def get_difference_carcasses_percent(date1=None, date2=None): different_percent = 0 kill_houses = KillHouse.objects.filter(out_province=False, trash=False).order_by('id') if date1: total_province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user__in=kill_houses, trash=False, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, first_car_allocated_quantity=0).order_by('id') total_kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user__in=kill_houses) & Q(killer__in=kill_houses)) | Q( Q(killhouse_user__in=kill_houses) & Q(killer__isnull=True)) | Q( Q(killhouse_user__in=kill_houses) | Q(killer__in=kill_houses)), archive_wage=False, trash=False, calculate_status=True, kill_request__recive_date__date__gte=date1, kill_request__recive_date__date__lte=date2, ) else: total_province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user__in=kill_houses, trash=False, archive_wage=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0).order_by('id') total_kill_house_requests = KillHouseRequest.objects.filter( Q(Q(killhouse_user__in=kill_houses) & Q(killer__in=kill_houses)) | Q( Q(killhouse_user__in=kill_houses) & Q(killer__isnull=True)) | Q( Q(killhouse_user__in=kill_houses) | Q(killer__in=kill_houses)), archive_wage=False, trash=False, calculate_status=True ) total_province_live_weight = total_province_kill_requests.aggregate(total=Sum('total_killed_weight'))[ 'total'] or 0 total_province_live_quantity = total_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 total_province_live_weight += total_kill_house_requests.aggregate(total=Sum('accepted_real_weight'))['total'] or 0 total_province_live_quantity += total_kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))[ 'total'] or 0 difference_requests = BarDifferenceRequest.objects.filter(kill_house__in=kill_houses, trash=False, state='accepted') total_province_live_weight += \ difference_requests.aggregate(total=Sum('weight'))['total'] or 0 total_province_carcasses_weight = total_province_live_weight * 0.75 total_pure_province_carcasses_weight = total_province_carcasses_weight - get_difference_carcasses_weight( kill_houses, date1, date2) different_percent = total_pure_province_carcasses_weight / total_province_carcasses_weight return different_percent def generate_unique_bar_code(): shamsi_year = jdatetime.date.fromgregorian(year=datetime.datetime.now().year, month=1, day=1).year last_digit = str(shamsi_year)[-1] while True: code = int(province_id + last_digit + str(uuid.uuid4().int)[:5]) if not KillHouseFreeBarInformation.objects.filter(bar_code=code, trash=False).exists(): return code def send_ticket_for_bar_difference_request(user, kill_house, quantity, poultry): new_ticket = TicketSupport( user=user, title="پیام سیستمی:درخواست رسیدگی اختلاف کشتار", status='open', read_only=True, type_ticket='single', parent=None, last_message='Admin', ) new_ticket.save() new_ticket.to_user.add(kill_house) message = 'با سلام و احترام این پیام از طرف سامانه میباشد.' \ '\n' \ f'کاربر گرامی برای شما حجم {quantity} قطعه اختلاف کشتار از مرغدار {poultry} ثبت گردیده لطفا جهت تعیین تکلیف به بخش مدیریت بار قسمت اختلاف کشتار مراجعه نمایید.باتشکر سامانه رصدیار ' msg = MessageSupport( ticket=new_ticket, message=message, created_by=user, sender='Admin' ) msg.save() def get_difference_carcasses_weight_kill(kill_house, date1=None, date2=None): if date1: kill_house_free_bar_info = KillHouseFreeBarInformation.objects.filter(kill_house=kill_house, archive_wage=False, date__date__gte=date1, date__date__lte=date2, calculate_status=True, trash=False) kill_house_free_sale_bar_info = KillHouseFreeSaleBarInformation.objects.filter( kill_house=kill_house, archive_wage=False, calculate_status=True, date__date__gte=date1, date__date__lte=date2, trash=False) else: kill_house_free_bar_info = KillHouseFreeBarInformation.objects.filter(kill_house=kill_house, archive_wage=False, calculate_status=True, trash=False) kill_house_free_sale_bar_info = KillHouseFreeSaleBarInformation.objects.filter( kill_house=kill_house, archive_wage=False, calculate_status=True, trash=False) total_out_carcasses_buying_for_pure_province_carcasses_weight = \ kill_house_free_bar_info.aggregate(total=Sum('weight_of_carcasses'))['total'] or 0 total_out_selling_province_carcasses_weight = \ kill_house_free_sale_bar_info.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0 out_selling_out_carcasses_buying_difference = total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight if ( total_out_selling_province_carcasses_weight - total_out_carcasses_buying_for_pure_province_carcasses_weight) > 0 else 0 difference_weight = out_selling_out_carcasses_buying_difference return difference_weight def get_calculate_shares(kill_house): 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 wage_info = get_finance_info(kill_house) slaughter_transactions = InternalTransaction.objects.filter( Q(kill_house=kill_house) | Q(parent_kill_house=kill_house), status='completed', trash=False) total_paid_wage = slaughter_transactions.aggregate(total=Sum('amount'))[ 'total'] or 0 # total_pure_province_carcasses_price = wage_info['total_pure_province_carcasses_price'] + wage_info[ # 'total_return_pure_province_carcasses_price'] total_pure_province_carcasses_price = wage_info['total_pure_province_carcasses_price'] total_out_selling_province_carcasses_price = wage_info['total_out_selling_province_carcasses_price'] difference_requests_price = wage_info['difference_requests_price'] total_return_pure_province_carcasses_price = wage_info['total_return_pure_province_carcasses_price'] if new_out_selling_count_wage: total_out_live_buying_province_carcasses_price = int( (wage_info[ 'before_total_out_live_buying_province_carcasses_weight'] * before_out_buying_count_wage_amount) + ( wage_info[ 'after_total_out_live_buying_province_carcasses_weight'] * free_buying_live_weight_wage_type_amount)) before_total_out_live_buying_province_carcasses_price = wage_info[ 'before_total_out_live_buying_province_carcasses_weight'] * before_out_buying_count_wage_amount else: total_out_live_buying_province_carcasses_price = wage_info[ 'total_out_live_buying_province_carcasses_price'] total_out_carcasses_buying_province_carcasses_price = wage_info[ 'total_out_carcasses_buying_province_carcasses_price'] total_price = wage_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 union_province_kill_request_wage = total_pure_province_carcasses_price * union_province_kill_request_percent return_union_province_kill_request_wage = total_return_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 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 = total_return_pure_province_carcasses_price * guilds_province_kill_request_percent other_return_province_kill_request_wage = total_return_pure_province_carcasses_price * other_province_kill_request_percent # company_province_kill_request_wage = ( # total_pure_province_carcasses_price * company_province_kill_request_percent) + ( # guilds_difference_requests_price + other_difference_requests_price + guild_return_province_kill_request_wage + other_return_province_kill_request_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 = ( total_return_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 + guild_return_province_kill_request_wage) 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 + other_return_province_kill_request_wage) 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, "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, "total_unpaid": total_price - total_paid_wage, }, { "name": "شرکت", "province_kill_request_wage": 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, "total_unpaid": total_price - total_paid_wage, }, { "name": "صنف پروتئین", "province_kill_request_wage": 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, "total_unpaid": total_price - total_paid_wage, }, { "name": "دامپزشک", "province_kill_request_wage": 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, "total_unpaid": total_price - total_paid_wage, }] def get_kill_house_percent(kill_house): shares = get_calculate_shares(kill_house) union = shares[0]['total_unpaid_wage'] if shares[0]['total_unpaid_wage'] > 0 else 0 company = shares[1]['total_unpaid_wage'] if shares[1]['total_unpaid_wage'] > 0 else 0 guilds = shares[2]['total_unpaid_wage'] if shares[2]['total_unpaid_wage'] > 0 else 0 vet = shares[3]['total_unpaid_wage'] if shares[3]['total_unpaid_wage'] > 0 else 0 # if union >= company: # return None # else: total = union + company + guilds + vet if total == 0 : return { "union": union, "company": company, "guilds": guilds, "vet": vet, "total_percent": total, } union = int((union / total) * 100) company = int((company / total) * 100) guilds = int((guilds / total) * 100) vet = int((vet / total) * 100) total_percent = union + company + guilds + vet if total_percent < 100: if base_url_for_sms_report == 'ma': if union >= company: union += 100 - total_percent else: company += 100 - total_percent else: company += 100 - total_percent return { "union": union, "company": company, "guilds": guilds, "vet": vet, "total_percent": total_percent, } # def create_kill_house_free_bar(request): # kill = KillHouse.objects.filter(trash=False, unique_identifier__isnull=False).order_by('id').values_list( # 'unique_identifier', flat=True) # kill_house_list = { # "kill_house": list(kill) # } # response = requests.post('https://rsibackend.rasadyar.com/app/get_bar_info/', data=kill_house_list) # try: # bars = response.json() # except ValueError: # return # if not isinstance(bars, list): # return # for bar in bars: # try: # if not isinstance(bar, dict): # continue # des_part_id_code = bar.get('DesPartIdCode') # if not des_part_id_code: # continue # kill_house = KillHouse.objects.filter(trash=False, unique_identifier=des_part_id_code).first() # if not kill_house: # continue # if KillHouseFreeBarInformation.objects.filter(trash=False, bar_clearance_code=bar.get('TrackingCode')).exists(): # continue # product = RolesProducts.objects.filter(kill_house=kill_house, trash=False, name='مرغ گرم').first() # formatted_str = str(bar.get('Date')).replace('T', ' ').replace('Z', '') # date = datetime.datetime.strptime(formatted_str, '%Y-%m-%d %H:%M:%S').date() # date = datetime.datetime(year=date.year, month=date.month, day=date.day, hour=10, # minute=1, # second=1) # kill_house_free_bar_info = KillHouseFreeBarInformation( # kill_house=kill_house, # product=product, # ) # kill_house_free_bar_info.driver_name = 'سیستمی' # kill_house_free_bar_info.driver_mobile = '0' # kill_house_free_bar_info.input_warehouse = kill_house # kill_house_free_bar_info.poultry_name = bar.get('hatching', {}).get('poultry', {}).get('UnitName') # kill_house_free_bar_info.poultry_mobile = bar.get('hatching', {}).get('poultry', {}).get('Mobile') # kill_house_free_bar_info.province = bar.get('hatching', {}).get('poultry', {}).get('Province') # kill_house_free_bar_info.city = bar.get('hatching', {}).get('poultry', {}).get('City') # kill_house_free_bar_info.bar_clearance_code = bar.get('TrackingCode') # kill_house_free_bar_info.quantity = bar.get('GoodAmount') # kill_house_free_bar_info.live_weight = int(int(bar.get('GoodAmount')) * 2.5) # kill_house_free_bar_info.buy_type = 'live' # kill_house_free_bar_info.car = '0' # kill_house_free_bar_info.register_type = 'automatic' # kill_house_free_bar_info.date = datetime.datetime.now() # kill_house_free_bar_info.bar_code = generate_unique_bar_code() # kill_house_free_bar_info.save() # wage = 0 # type = 'live-buy' if kill_house_free_bar_info.buy_type == 'live' else 'carcasse-buy' # wage_type = WageType.objects.filter(en_name=type, trash=False).first() # if wage_type and wage_type.status is True: # wage = wage_type.amount # kill_house_free_bar_info.wage = wage # kill_house_free_bar_info.total_wage_amount = wage * int( # kill_house_free_bar_info.live_weight) if kill_house_free_bar_info.buy_type == 'live' else wage * int( # kill_house_free_bar_info.weight_of_carcasses) # kill_house_free_bar_info.save() # kill_house_free_bar_info.create_date = date # kill_house_free_bar_info.register_date = datetime.datetime.now() # kill_house_free_bar_info.save() # kill_house_free_buying_product_warehousing(product) # percentages_wage_type = PercentageOfWageType.objects.filter(wage_type=wage_type, percent__gt=0, # trash=False) # if wage_type and wage_type.status is True and percentages_wage_type: # for percentage_wage_type in percentages_wage_type: # if percentage_wage_type.share_type.en_name == 'union': # kill_house_free_bar_info.union_share = int( # (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) # kill_house_free_bar_info.union_share_percent = percentage_wage_type.percent # kill_house_free_bar_info.save() # elif percentage_wage_type.share_type.en_name == 'company': # kill_house_free_bar_info.company_share = int( # (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) # kill_house_free_bar_info.company_share_percent = percentage_wage_type.percent # kill_house_free_bar_info.save() # elif percentage_wage_type.share_type.en_name == 'guilds': # kill_house_free_bar_info.guilds_share = int( # (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) # kill_house_free_bar_info.guilds_share_percent = percentage_wage_type.percent # kill_house_free_bar_info.save() # elif percentage_wage_type.share_type.en_name == 'city': # kill_house_free_bar_info.city_share = int( # (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) # kill_house_free_bar_info.city_share_percent = percentage_wage_type.percent # kill_house_free_bar_info.save() # elif percentage_wage_type.share_type.en_name == 'wallet': # kill_house_free_bar_info.wallet_share = int( # (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) # kill_house_free_bar_info.wallet_share_percent = percentage_wage_type.percent # kill_house_free_bar_info.save() # else: # kill_house_free_bar_info.other_share = int( # (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) # kill_house_free_bar_info.other_share_percent = percentage_wage_type.percent # kill_house_free_bar_info.save() # except Exception: # continue # return HttpResponse('ok') def create_kill_house_free_bar(request): kill = KillHouse.objects.filter(trash=False, unique_identifier__isnull=False).order_by('id').values_list( 'unique_identifier', flat=True) kill_house_list = { "kill_house": list(kill) } response = requests.post('https://rsibackend.rasadyar.com/app/get_bar_info/', data=kill_house_list) try: bars = response.json() except ValueError: return if not isinstance(bars, list): return for bar in bars: try: if not isinstance(bar, dict): continue des_part_id_code = bar.get('jihadi_destination') if not des_part_id_code: continue kill_house = KillHouse.objects.filter(trash=False, unique_identifier=des_part_id_code).first() if not kill_house: continue if KillHouseFreeBarInformation.objects.filter(trash=False, bar_clearance_code=bar.get('tracking')).exists(): continue product = RolesProducts.objects.filter(kill_house=kill_house, trash=False, name='مرغ گرم').first() formatted_str = str(bar.get('date')).replace('T', ' ').replace('Z', '') date = datetime.datetime.strptime(formatted_str, '%Y-%m-%d').date() date = datetime.datetime(year=date.year, month=date.month, day=date.day, hour=10, minute=1, second=1) kill_house_free_bar_info = KillHouseFreeBarInformation( kill_house=kill_house, product=product, ) kill_house_free_bar_info.driver_name = 'سیستمی' kill_house_free_bar_info.driver_mobile = '0' kill_house_free_bar_info.input_warehouse = kill_house kill_house_free_bar_info.poultry_name = bar.get('hatching', {}).get('poultry', {}).get('UnitName') kill_house_free_bar_info.poultry_mobile = bar.get('hatching', {}).get('poultry', {}).get('Mobile') kill_house_free_bar_info.province = bar.get('hatching', {}).get('poultry', {}).get('Province') kill_house_free_bar_info.city = bar.get('hatching', {}).get('poultry', {}).get('City') kill_house_free_bar_info.bar_clearance_code = bar.get('tracking') kill_house_free_bar_info.quantity = bar.get('quantity') kill_house_free_bar_info.live_weight = int(int(bar.get('quantity')) * 2.5) kill_house_free_bar_info.buy_type = 'live' kill_house_free_bar_info.car = '0' kill_house_free_bar_info.register_type = 'automatic' kill_house_free_bar_info.date = datetime.datetime.now() kill_house_free_bar_info.bar_code = generate_unique_bar_code() kill_house_free_bar_info.save() wage = 0 type = 'live-buy' if kill_house_free_bar_info.buy_type == 'live' else 'carcasse-buy' wage_type = WageType.objects.filter(en_name=type, trash=False).first() if wage_type and wage_type.status is True: wage = wage_type.amount kill_house_free_bar_info.wage = wage kill_house_free_bar_info.total_wage_amount = wage * int( kill_house_free_bar_info.live_weight) if kill_house_free_bar_info.buy_type == 'live' else wage * int( kill_house_free_bar_info.weight_of_carcasses) kill_house_free_bar_info.save() kill_house_free_bar_info.create_date = date kill_house_free_bar_info.register_date = datetime.datetime.now() kill_house_free_bar_info.save() kill_house_free_buying_product_warehousing(product) percentages_wage_type = PercentageOfWageType.objects.filter(wage_type=wage_type, percent__gt=0, trash=False) if wage_type and wage_type.status is True and percentages_wage_type: for percentage_wage_type in percentages_wage_type: if percentage_wage_type.share_type.en_name == 'union': kill_house_free_bar_info.union_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.union_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() elif percentage_wage_type.share_type.en_name == 'company': kill_house_free_bar_info.company_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.company_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() elif percentage_wage_type.share_type.en_name == 'guilds': kill_house_free_bar_info.guilds_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.guilds_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() elif percentage_wage_type.share_type.en_name == 'city': kill_house_free_bar_info.city_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.city_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() elif percentage_wage_type.share_type.en_name == 'wallet': kill_house_free_bar_info.wallet_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.wallet_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() else: kill_house_free_bar_info.other_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.other_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() except Exception: continue return HttpResponse('ok') def create_kill_house_free_bar_cron(): kill = KillHouse.objects.filter(trash=False, unique_identifier__isnull=False).order_by('id').values_list( 'unique_identifier', flat=True) kill_house_list = { "kill_house": list(kill) } response = requests.post('https://rsibackend.rasadyar.com/app/get_bar_info/', data=kill_house_list) try: bars = response.json() except ValueError: return if not isinstance(bars, list): return for bar in bars: try: if not isinstance(bar, dict): continue des_part_id_code = bar.get('jihadi_destination') if not des_part_id_code: continue kill_house = KillHouse.objects.filter(trash=False, unique_identifier=des_part_id_code).first() if not kill_house: continue if KillHouseFreeBarInformation.objects.filter(trash=False, bar_clearance_code=bar.get('tracking')).exists(): continue product = RolesProducts.objects.filter(kill_house=kill_house, trash=False, name='مرغ گرم').first() formatted_str = str(bar.get('date')).replace('T', ' ').replace('Z', '') date = datetime.datetime.strptime(formatted_str, '%Y-%m-%d').date() date = datetime.datetime(year=date.year, month=date.month, day=date.day, hour=10, minute=1, second=1) kill_house_free_bar_info = KillHouseFreeBarInformation( kill_house=kill_house, product=product, ) kill_house_free_bar_info.driver_name = 'سیستمی' kill_house_free_bar_info.driver_mobile = '0' kill_house_free_bar_info.input_warehouse = kill_house kill_house_free_bar_info.poultry_name = bar.get('hatching', {}).get('poultry', {}).get('UnitName') kill_house_free_bar_info.poultry_mobile = bar.get('hatching', {}).get('poultry', {}).get('Mobile') kill_house_free_bar_info.province = bar.get('hatching', {}).get('poultry', {}).get('Province') kill_house_free_bar_info.city = bar.get('hatching', {}).get('poultry', {}).get('City') kill_house_free_bar_info.bar_clearance_code = bar.get('tracking') kill_house_free_bar_info.quantity = bar.get('quantity') kill_house_free_bar_info.live_weight = int(int(bar.get('quantity')) * 2.5) kill_house_free_bar_info.buy_type = 'live' kill_house_free_bar_info.car = '0' kill_house_free_bar_info.register_type = 'automatic' kill_house_free_bar_info.date = datetime.datetime.now() kill_house_free_bar_info.bar_code = generate_unique_bar_code() kill_house_free_bar_info.save() wage = 0 type = 'live-buy' if kill_house_free_bar_info.buy_type == 'live' else 'carcasse-buy' wage_type = WageType.objects.filter(en_name=type, trash=False).first() if wage_type and wage_type.status is True: wage = wage_type.amount kill_house_free_bar_info.wage = wage kill_house_free_bar_info.total_wage_amount = wage * int( kill_house_free_bar_info.live_weight) if kill_house_free_bar_info.buy_type == 'live' else wage * int( kill_house_free_bar_info.weight_of_carcasses) kill_house_free_bar_info.save() kill_house_free_bar_info.create_date = date kill_house_free_bar_info.register_date = datetime.datetime.now() kill_house_free_bar_info.save() kill_house_free_buying_product_warehousing(product) percentages_wage_type = PercentageOfWageType.objects.filter(wage_type=wage_type, percent__gt=0, trash=False) if wage_type and wage_type.status is True and percentages_wage_type: for percentage_wage_type in percentages_wage_type: if percentage_wage_type.share_type.en_name == 'union': kill_house_free_bar_info.union_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.union_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() elif percentage_wage_type.share_type.en_name == 'company': kill_house_free_bar_info.company_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.company_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() elif percentage_wage_type.share_type.en_name == 'guilds': kill_house_free_bar_info.guilds_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.guilds_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() elif percentage_wage_type.share_type.en_name == 'city': kill_house_free_bar_info.city_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.city_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() elif percentage_wage_type.share_type.en_name == 'wallet': kill_house_free_bar_info.wallet_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.wallet_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() else: kill_house_free_bar_info.other_share = int( (percentage_wage_type.percent / 100) * kill_house_free_bar_info.total_wage_amount) kill_house_free_bar_info.other_share_percent = percentage_wage_type.percent kill_house_free_bar_info.save() except Exception: continue def market_poultry_request_remain_quantity(poultry_request): province_check = ProvinceCheckOperatorRequest.objects.get(poultry_request=poultry_request) province_kill_reqs = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request=poultry_request, market=False, return_to_province=False, state__in=('pending', 'accepted'), temporary_trash=False).only('total_killed_quantity') \ .aggregate(total=Sum('total_killed_quantity')) kill_requests = KillRequest.objects.filter(trash=False, market=True, market_state__in=('pending', 'accepted'), poultry_request=poultry_request).only('kill_capacity') \ .aggregate(total=Sum('kill_capacity')) new_remain = poultry_request.quantity - ((kill_requests['total'] or 0) + (province_kill_reqs['total'] or 0)) new_quantity = poultry_request.quantity - ((kill_requests['total'] or 0) + (province_kill_reqs['total'] or 0)) poultry_request.remain_quantity = new_remain if new_remain > 0 else 0 poultry_request.save() province_check.quantity = new_quantity if new_quantity > 0 else 0 province_check.save() # def market_calculate_light_weight(kill_house, total_poultry_requests_quantity, # total_poultry_requests_quantity_light_weight): # real_total_light_weight_quantity = 0 # kill_house_ids = KillHousePercentage.objects.filter(kill_house__type='exclusive', trash=False).values_list( # 'kill_house', flat=True) # kill_houses = KillHouse.objects.filter(out_province=False, market_buying=True, market_light_capacity=True, # trash=False).exclude( # id__in=kill_house_ids).order_by('id', 'killer') # for kill_house_obj in kill_houses: # kill_house_share = int((kill_house_obj.market_capacity_percent / 100) * total_poultry_requests_quantity) # kill_house_light_weight_quantity = int(kill_house_share * (kill_house_obj.market_light_capacity_percent / 100)) # real_total_light_weight_quantity += kill_house_light_weight_quantity # kill_house_share = 0 # kill_house_light_weight_quantity = 0 # # kill_house_share = int((kill_house.market_capacity_percent / 100) * total_poultry_requests_quantity) # # kill_house_light_weight_quantity = int(kill_house_share * (kill_house.market_light_capacity_percent / 100)) # # kill_house_real_percent = int(( # kill_house_light_weight_quantity / real_total_light_weight_quantity) * 100) if real_total_light_weight_quantity > 0 else 0 # # kill_house_real_light_weight = int((kill_house_real_percent / 100) * total_poultry_requests_quantity_light_weight) # # return kill_house_real_light_weight # # # def market_kill_request_share_quantity(kill_house, date1=None, date2=None): # if kill_house.market_buying: # now = datetime.datetime.now().date() # market_light_capacity = kill_house.market_light_capacity # market_light_capacity_percent = kill_house.market_light_capacity_percent # market_light_share = 0 # # index_weight_category = IndexWeightCategory.objects.filter(trash=False).order_by('id') # light = index_weight_category.filter(name='سبک').first() # heavy = index_weight_category.filter(name='سنگین').first() # # if date1: # poultry_requests = PoultryRequest.objects.filter( # send_date__date__gte=date1, # send_date__date__lte=date2, # state_process='accepted', # province_state='accepted', # temporary_trash=False, # trash=False, # out=False, # final_state__in=('pending', 'accepted'), # market=True, # ) # # kill_house_market_kill_requests = KillRequest.objects.filter(trash=False, recive_date__date__gte=date1, # recive_date__date__lte=date2, # kill_house=kill_house, market=True, # market_state__in=('pending', 'accepted')) # else: # poultry_requests = PoultryRequest.objects.filter( # send_date__date=now, # state_process='accepted', # province_state='accepted', # temporary_trash=False, # trash=False, # out=False, # final_state__in=('pending', 'accepted'), # market=True, # ) # # kill_house_market_kill_requests = KillRequest.objects.filter(trash=False, recive_date__date=now, # kill_house=kill_house, market=True, # market_state__in=('pending', 'accepted')) # # total_poultry_requests_quantity = poultry_requests.aggregate(total=Sum('quantity'))['total'] or 0 # # total_poultry_requests_quantity_light_weight = \ # poultry_requests.filter(Index_weight__gte=light.min_value, Index_weight__lt=light.max_value).aggregate( # total=Sum('quantity'))['total'] or 0 # total_remain_poultry_requests_quantity_light_weight = \ # poultry_requests.filter(Index_weight__gte=light.min_value, Index_weight__lt=light.max_value).aggregate( # total=Sum('remain_quantity'))['total'] or 0 # # light_real_quantity = market_calculate_light_weight(kill_house, total_poultry_requests_quantity, # total_poultry_requests_quantity_light_weight) # # if kill_house.market_buying_limitation: # if kill_house.market_buying_capacity_percent_status: # kill_house_today_share = int( # (kill_house.market_capacity_percent / 100) * total_poultry_requests_quantity) # # else: # kill_house_today_share = int( # kill_house.total_kill_capacity * (kill_house.total_kill_capacity_percent / 100)) # # # # else: # kill_house_today_share = total_poultry_requests_quantity # # kill_house_market_kill_requests_quantity = \ # kill_house_market_kill_requests.aggregate(total=Sum('kill_capacity'))[ # 'total'] or 0 # kill_house_market_kill_requests_quantity_weight = \ # kill_house_market_kill_requests.aggregate(total=Sum(F('kill_capacity') * F('Index_weight')))[ # 'total'] or 0 # kill_house_market_kill_requests_quantity_first = \ # kill_house_market_kill_requests.filter(market_final_accept=False).aggregate(total=Sum('kill_capacity'))[ # 'total'] or 0 # # kill_house_market_kill_requests_quantity_first_weight = \ # kill_house_market_kill_requests.filter(market_final_accept=False).aggregate( # total=Sum(F('kill_capacity') * F('Index_weight')))[ # 'total'] or 0 # kill_house_market_kill_requests_quantity_final = \ # kill_house_market_kill_requests.filter(market_final_accept=True).aggregate(total=Sum('kill_capacity'))[ # 'total'] or 0 # kill_house_market_kill_requests_quantity_final_weight = \ # kill_house_market_kill_requests.filter(market_final_accept=True).aggregate( # total=Sum(F('kill_capacity') * F('Index_weight')))[ # 'total'] or 0 # # total_kill_house_market_kill_requests_quantity_agreement_light_weight = \ # kill_house_market_kill_requests.filter(Index_weight__lt=light.min_value).aggregate( # total=Sum('kill_capacity'))[ # 'total'] or 0 # # total_kill_house_market_kill_requests_quantity_light_weight = \ # kill_house_market_kill_requests.filter(Index_weight__gte=light.min_value, # Index_weight__lt=light.max_value).aggregate( # total=Sum('kill_capacity'))[ # 'total'] or 0 # # total_kill_house_market_kill_requests_quantity_heavy_weight = \ # kill_house_market_kill_requests.filter(Index_weight__gte=heavy.min_value, # Index_weight__lte=heavy.max_value).aggregate( # total=Sum('kill_capacity'))[ # 'total'] or 0 # # if market_code_state: # kill_house_market_kill_requests_quantity_light_weight = \ # kill_house_market_kill_requests.filter(market_final_accept=True, market_code_status=True, # input_market_code__isnull=False, # Index_weight__lt=light.max_value).aggregate( # total=Sum('kill_capacity'))[ # 'total'] or 0 # else: # kill_house_market_kill_requests_quantity_light_weight = \ # kill_house_market_kill_requests.filter(market_final_accept=True, # Index_weight__lt=light.max_value).aggregate( # total=Sum('kill_capacity'))[ # 'total'] or 0 # # if market_light_capacity: # # market_light_share = int(kill_house_today_share * (market_light_capacity_percent / 100)) # market_light_share = light_real_quantity # # kill_house_today_left_share = kill_house_today_share - kill_house_market_kill_requests_quantity if ( # kill_house_today_share - kill_house_market_kill_requests_quantity) > 0 else 0 # # else: # total_poultry_requests_quantity = 0 # total_poultry_requests_quantity_light_weight = 0 # total_remain_poultry_requests_quantity_light_weight = 0 # kill_house_today_share = 0 # market_light_share = 0 # kill_house_market_kill_requests_quantity_light_weight = 0 # kill_house_market_kill_requests_quantity = 0 # kill_house_today_left_share = 0 # kill_house_market_kill_requests_quantity_first = 0 # kill_house_market_kill_requests_quantity_final = 0 # light_real_quantity = 0 # kill_house_market_kill_requests_quantity_weight = 0 # kill_house_market_kill_requests_quantity_first_weight = 0 # kill_house_market_kill_requests_quantity_final_weight = 0 # total_kill_house_market_kill_requests_quantity_agreement_light_weight = 0 # total_kill_house_market_kill_requests_quantity_light_weight = 0 # total_kill_house_market_kill_requests_quantity_heavy_weight = 0 # # result = { # "total_poultry_requests_quantity": total_poultry_requests_quantity, # "total_poultry_requests_quantity_light_weight": total_poultry_requests_quantity_light_weight, # "total_remain_poultry_requests_quantity_light_weight": total_remain_poultry_requests_quantity_light_weight, # "kill_house_today_share": kill_house_today_share, # "market_light_share": market_light_share, # "kill_house_market_kill_requests_quantity_light_weight": kill_house_market_kill_requests_quantity_light_weight, # "kill_house_market_kill_requests_quantity": kill_house_market_kill_requests_quantity, # "kill_house_market_kill_requests_quantity_first": kill_house_market_kill_requests_quantity_first, # "kill_house_market_kill_requests_quantity_final": kill_house_market_kill_requests_quantity_final, # "kill_house_today_left_share": kill_house_today_left_share, # "light_real_quantity": light_real_quantity, # "kill_house_market_kill_requests_quantity_weight": kill_house_market_kill_requests_quantity_weight, # "kill_house_market_kill_requests_quantity_first_weight": kill_house_market_kill_requests_quantity_first_weight, # "kill_house_market_kill_requests_quantity_final_weight": kill_house_market_kill_requests_quantity_final_weight, # "total_kill_house_market_kill_requests_quantity_agreement_light_weight": total_kill_house_market_kill_requests_quantity_agreement_light_weight, # "total_kill_house_market_kill_requests_quantity_light_weight": total_kill_house_market_kill_requests_quantity_light_weight, # "total_kill_house_market_kill_requests_quantity_heavy_weight": total_kill_house_market_kill_requests_quantity_heavy_weight, # } # # return result def market_calculate_light_weight(kill_house, total_kill_houses, total_poultry_requests_quantity, total_poultry_requests_quantity_light_weight): shares = total_kill_houses.annotate( kill_share=F('market_capacity_percent') * total_poultry_requests_quantity / 100.0, light_share=F('market_capacity_percent') * total_poultry_requests_quantity / 100.0 * F( 'market_light_capacity_percent') / 100.0 ).values_list('id', 'light_share') real_total_light_weight_quantity = sum([ls[1] for ls in shares]) kill_house_light_share = next((ls[1] for ls in shares if ls[0] == kill_house.id), 0) kill_house_real_percent = int(( kill_house_light_share / real_total_light_weight_quantity) * 100) if real_total_light_weight_quantity > 0 else 0 kill_house_real_light_weight = int((kill_house_real_percent / 100) * total_poultry_requests_quantity_light_weight) return kill_house_real_light_weight # def market_kill_request_share_quantity(kill_house, date1=None, date2=None, total_kill_houses=None): # result = { # "total_poultry_requests_quantity": 0, # "total_poultry_requests_quantity_light_weight": 0, # "total_remain_poultry_requests_quantity_light_weight": 0, # "kill_house_today_share": 0, # "market_light_share": 0, # "kill_house_market_kill_requests_quantity_light_weight": 0, # "kill_house_market_kill_requests_quantity": 0, # "kill_house_market_kill_requests_quantity_first": 0, # "kill_house_market_kill_requests_quantity_final": 0, # "kill_house_today_left_share": 0, # "light_real_quantity": 0, # "kill_house_market_kill_requests_quantity_weight": 0, # "kill_house_market_kill_requests_quantity_first_weight": 0, # "kill_house_market_kill_requests_quantity_final_weight": 0, # "total_kill_house_market_kill_requests_quantity_agreement_light_weight": 0, # "total_kill_house_market_kill_requests_quantity_light_weight": 0, # "total_kill_house_market_kill_requests_quantity_heavy_weight": 0, # } # # if not kill_house.market_buying: # return result # # now = datetime.datetime.now().date() # date_filter = Q(send_date__date=now) # kill_request_date_filter = Q(recive_date__date=now) # # if date1: # date_filter = Q(send_date__date__gte=date1, send_date__date__lte=date2) # kill_request_date_filter = Q(recive_date__date__gte=date1, recive_date__date__lte=date2) # # weights = IndexWeightCategory.objects.filter(trash=False) # light = weights.filter(name='سبک').first() # heavy = weights.filter(name='سنگین').first() # # poultry_requests = PoultryRequest.objects.filter( # date_filter, # state_process='accepted', # province_state='accepted', # temporary_trash=False, # trash=False, # out=False, # final_state__in=('pending', 'accepted'), # market=True, # ) # # total_poultry_requests_quantity = poultry_requests.aggregate(total=Sum('quantity'))['total'] or 0 # total_light_quantity = poultry_requests.filter( # Index_weight__gte=light.min_value, # Index_weight__lt=light.max_value # ).aggregate(total=Sum('quantity'))['total'] or 0 # total_remain_light_quantity = poultry_requests.filter( # Index_weight__gte=light.min_value, # Index_weight__lt=light.max_value # ).aggregate(total=Sum('remain_quantity'))['total'] or 0 # # kill_requests = KillRequest.objects.filter( # kill_request_date_filter, # trash=False, # kill_house=kill_house, # market=True, # market_state__in=('pending', 'accepted') # ) # # kill_house_today_share = total_poultry_requests_quantity # if kill_house.market_buying_limitation: # if kill_house.market_buying_capacity_percent_status: # kill_house_today_share = int(kill_house.market_capacity_percent / 100 * total_poultry_requests_quantity) # else: # kill_house_today_share = int(kill_house.total_kill_capacity * kill_house.total_kill_capacity_percent / 100) # # agg = kill_requests.aggregate( # total_kill_capacity=Sum('kill_capacity') or 0, # total_weight=Sum(F('kill_capacity') * F('Index_weight')) or 0, # total_first=Sum('kill_capacity', filter=Q(market_final_accept=False)) or 0, # total_first_weight=Sum(F('kill_capacity') * F('Index_weight'), filter=Q(market_final_accept=False)) or 0, # total_final=Sum('kill_capacity', filter=Q(market_final_accept=True)) or 0, # total_final_weight=Sum(F('kill_capacity') * F('Index_weight'), filter=Q(market_final_accept=True)) or 0, # total_agreement_light=Sum('kill_capacity', filter=Q(Index_weight__lt=light.min_value)) or 0, # total_light=Sum('kill_capacity', # filter=Q(Index_weight__gte=light.min_value, Index_weight__lt=light.max_value)) or 0, # total_heavy=Sum('kill_capacity', # filter=Q(Index_weight__gte=heavy.min_value, Index_weight__lte=heavy.max_value)) or 0, # ) # # light_real_quantity = market_calculate_light_weight(kill_house, total_kill_houses, total_poultry_requests_quantity, # total_light_quantity) # market_light_share = light_real_quantity if kill_house.market_light_capacity else 0 # kill_house_today_left_share = max(0, kill_house_today_share - (agg['total_kill_capacity'] or 0)) # # result.update({ # "total_poultry_requests_quantity": total_poultry_requests_quantity or 0, # "total_poultry_requests_quantity_light_weight": total_light_quantity or 0, # "total_remain_poultry_requests_quantity_light_weight": total_remain_light_quantity or 0, # "kill_house_today_share": kill_house_today_share or 0, # "market_light_share": market_light_share or 0, # "kill_house_market_kill_requests_quantity_light_weight": agg['total_light'] or 0, # "kill_house_market_kill_requests_quantity": agg['total_kill_capacity'] or 0, # "kill_house_market_kill_requests_quantity_first": agg['total_first'] or 0, # "kill_house_market_kill_requests_quantity_final": agg['total_final'] or 0, # "kill_house_today_left_share": kill_house_today_left_share or 0, # "light_real_quantity": light_real_quantity or 0, # "kill_house_market_kill_requests_quantity_weight": agg['total_weight'] or 0, # "kill_house_market_kill_requests_quantity_first_weight": agg['total_first_weight'] or 0, # "kill_house_market_kill_requests_quantity_final_weight": agg['total_final_weight'] or 0, # "total_kill_house_market_kill_requests_quantity_agreement_light_weight": agg['total_agreement_light'] or 0, # "total_kill_house_market_kill_requests_quantity_light_weight": agg['total_light'] or 0, # "total_kill_house_market_kill_requests_quantity_heavy_weight": agg['total_heavy'] or 0, # }) # # return result def market_kill_request_share_quantity( kill_house, date1=None, date2=None, total_kill_houses=None, weights=None, precomputed_poultry=None ): result = { "total_poultry_requests_quantity": 0, "total_poultry_requests_quantity_light_weight": 0, "total_remain_poultry_requests_quantity_light_weight": 0, "kill_house_today_share": 0, "market_light_share": 0, "kill_house_market_kill_requests_quantity_light_weight": 0, "kill_house_market_kill_requests_quantity": 0, "kill_house_market_kill_requests_quantity_first": 0, "kill_house_market_kill_requests_quantity_final": 0, "kill_house_today_left_share": 0, "light_real_quantity": 0, "kill_house_market_kill_requests_quantity_weight": 0, "kill_house_market_kill_requests_quantity_first_weight": 0, "kill_house_market_kill_requests_quantity_final_weight": 0, "total_kill_house_market_kill_requests_quantity_agreement_light_weight": 0, "total_kill_house_market_kill_requests_quantity_light_weight": 0, "total_kill_house_market_kill_requests_quantity_heavy_weight": 0, } if not kill_house.market_buying: return result now = datetime.date.today() date_filter = Q(send_date__date=now) kill_request_date_filter = Q(recive_date__date=now) if date1: date_filter = Q(send_date__date__gte=date1, send_date__date__lte=date2) kill_request_date_filter = Q(recive_date__date__gte=date1, recive_date__date__lte=date2) if weights is None: weights = IndexWeightCategory.objects.filter(trash=False) light = weights.filter(name='سبک').first() heavy = weights.filter(name='سنگین').first() poultry_requests = PoultryRequest.objects.filter( date_filter, state_process='accepted', province_state='accepted', temporary_trash=False, trash=False, out=False, final_state__in=('pending', 'accepted'), market=True, ) poultry_agg = poultry_requests.aggregate( total=Sum('quantity'), total_light=Sum('quantity', filter=Q(Index_weight__gte=light.min_value, Index_weight__lt=light.max_value)), total_remain_light=Sum('remain_quantity', filter=Q(Index_weight__gte=light.min_value, Index_weight__lt=light.max_value)), ) total_poultry_requests_quantity = poultry_agg['total'] or 0 total_light_quantity = poultry_agg['total_light'] or 0 total_remain_light_quantity = poultry_agg['total_remain_light'] or 0 kill_requests = KillRequest.objects.filter( kill_request_date_filter, trash=False, kill_house=kill_house, market=True, market_state__in=('pending', 'accepted') ) agg = kill_requests.aggregate( total_kill_capacity=Sum('kill_capacity'), total_weight=Sum(F('kill_capacity') * F('Index_weight')), total_first=Sum('kill_capacity', filter=Q(market_final_accept=False)), total_first_weight=Sum(F('kill_capacity') * F('Index_weight'), filter=Q(market_final_accept=False)), total_final=Sum('kill_capacity', filter=Q(market_final_accept=True)), total_final_weight=Sum(F('kill_capacity') * F('Index_weight'), filter=Q(market_final_accept=True)), total_agreement_light=Sum('kill_capacity', filter=Q(Index_weight__lt=light.min_value)), total_light=Sum('kill_capacity', filter=Q(Index_weight__gte=light.min_value, Index_weight__lt=light.max_value)), total_heavy=Sum('kill_capacity', filter=Q(Index_weight__gte=heavy.min_value, Index_weight__lte=heavy.max_value)), ) kill_house_today_share = total_poultry_requests_quantity if kill_house.market_buying_limitation: if kill_house.market_buying_capacity_percent_status: kill_house_today_share = int(kill_house.market_capacity_percent / 100 * total_poultry_requests_quantity) else: kill_house_today_share = int(kill_house.total_kill_capacity * kill_house.total_kill_capacity_percent / 100) light_real_quantity = market_calculate_light_weight( kill_house, total_kill_houses, total_poultry_requests_quantity, total_light_quantity ) market_light_share = light_real_quantity if kill_house.market_light_capacity else 0 kill_house_today_left_share = max(0, kill_house_today_share - (agg['total_kill_capacity'] or 0)) result.update({ "total_poultry_requests_quantity": total_poultry_requests_quantity, "total_poultry_requests_quantity_light_weight": total_light_quantity, "total_remain_poultry_requests_quantity_light_weight": total_remain_light_quantity, "kill_house_today_share": kill_house_today_share, "market_light_share": market_light_share, "kill_house_market_kill_requests_quantity_light_weight": agg['total_light'] or 0, "kill_house_market_kill_requests_quantity": agg['total_kill_capacity'] or 0, "kill_house_market_kill_requests_quantity_first": agg['total_first'] or 0, "kill_house_market_kill_requests_quantity_final": agg['total_final'] or 0, "kill_house_today_left_share": kill_house_today_left_share, "light_real_quantity": light_real_quantity, "kill_house_market_kill_requests_quantity_weight": agg['total_weight'] or 0, "kill_house_market_kill_requests_quantity_first_weight": agg['total_first_weight'] or 0, "kill_house_market_kill_requests_quantity_final_weight": agg['total_final_weight'] or 0, "total_kill_house_market_kill_requests_quantity_agreement_light_weight": agg['total_agreement_light'] or 0, "total_kill_house_market_kill_requests_quantity_light_weight": agg['total_light'] or 0, "total_kill_house_market_kill_requests_quantity_heavy_weight": agg['total_heavy'] or 0, }) return result def calculate_governmental_quota(kill_house): kill_house_governmental_share = 0 if kill_house.quota: now = datetime.datetime.now().date() if kill_house.quota_max_kill_limit: kill_house_governmental_share = int((kill_house.governmental_quota / 100) * kill_house.total_kill_capacity) elif kill_house.quota_request: poultry_requests = PoultryRequest.objects.filter( send_date__date=now, state_process='accepted', province_state='accepted', temporary_trash=False, trash=False, out=False, free_sale_in_province=False, final_state__in=('pending', 'accepted') ) poultry_requests_quantity = poultry_requests.aggregate(total=Sum('remain_quantity'))['total'] or 0 kill_house_governmental_share = int((kill_house.governmental_quota / 100) * poultry_requests_quantity) else: if kill_house.quota_custom: kill_house_governmental_share = kill_house.quota_custom_quantity province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, temporary_trash=False, killhouse_user=kill_house, return_to_province=False, state__in=('pending', 'accepted'), kill_request__recive_date__date=now) kill_house_market_kill_requests = KillRequest.objects.filter(trash=False, recive_date__date=now, kill_house=kill_house, market=True, market_state__in=('pending', 'accepted')) province_kill_requests_governmental_quantity = \ province_kill_requests.filter(province_request__poultry_request__free_sale_in_province=False).aggregate( total=Sum('total_killed_quantity'))['total'] or 0 kill_house_market_kill_requests_governmental_quantity = \ kill_house_market_kill_requests.filter(poultry_request__free_sale_in_province=False).aggregate( total=Sum('kill_capacity'))['total'] or 0 total_governmental_quantity = province_kill_requests_governmental_quantity + kill_house_market_kill_requests_governmental_quantity if total_governmental_quantity < kill_house_governmental_share: return 'not_allowed' return None # def check_kill_house_remain_limitation_weight(kill_house): # yesterday = datetime.datetime.now() - datetime.timedelta(days=1) # today = datetime.datetime.now().date() # kill_house_requests = KillHouseRequest.objects.filter(input_warehouse=kill_house, # province_request__poultry_request__free_sale_in_province=False, # kill_request__recive_date__date=yesterday.date(), # ware_house_confirmation=True, trash=False, # calculate_status=True, warehouse=True) # # kill_house_allocations = StewardAllocation.objects.filter( # kill_house=kill_house, trash=False, calculate_status=True, warehouse=True, system_registration_code=True, # receiver_state__in=('pending', 'accepted'), date__date=today, quota='governmental') # # kill_house_free_sale_bars = KillHouseFreeSaleBarInformation.objects.filter(kill_house=kill_house, # quota='governmental', # date__date=today, trash=False, # calculate_status=True, warehouse=True) # segmentations = PosSegmentation.objects.filter(kill_house=kill_house, date__date=today, trash=False, warehouse=True, # quota='governmental') # # kill_house_requests_weight = kill_house_requests.aggregate(total=Sum('ware_house_accepted_real_weight'))[ # 'total'] or 0 # kill_house_allocations_weight = \ # kill_house_allocations.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0 # kill_house_free_sale_bars_weight = kill_house_free_sale_bars.aggregate(total=Sum('real_weight_of_carcasses'))[ # 'total'] or 0 # # segmentation_weight = \ # segmentations.aggregate(total=Sum('weight'))[ # 'total'] or 0 # # total_input = kill_house_requests_weight # total_output = kill_house_allocations_weight + kill_house_free_sale_bars_weight + segmentation_weight # total_remain = total_input - total_output # # if total_remain <= 0: # return True # else: # if kill_house.ware_house_remaining_percent_limitation_status: # if (total_input * (kill_house.ware_house_remaining_percent_limitation / 100)) > total_remain: # return True # else: # return False # else: # return True def check_kill_house_remain_limitation_weight(kill_house): production_date = (datetime.datetime.now() - datetime.timedelta(days=3)).date() kill_house_requests = KillHouseRequest.objects.filter(input_warehouse=kill_house, province_request__poultry_request__free_sale_in_province=False, kill_request__recive_date__date=production_date, ware_house_confirmation=True, trash=False, calculate_status=True, warehouse=True) kill_house_allocations = StewardAllocation.objects.filter( kill_house=kill_house, trash=False, calculate_status=True, warehouse=True, system_registration_code=True, receiver_state__in=('pending', 'accepted'), production_date__date=production_date, quota='governmental') kill_house_free_sale_bars = KillHouseFreeSaleBarInformation.objects.filter(kill_house=kill_house, quota='governmental', production_date__date=production_date, trash=False, calculate_status=True, warehouse=True) segmentations = PosSegmentation.objects.filter(kill_house=kill_house, production_date__date=production_date, trash=False, warehouse=True, quota='governmental') kill_house_requests_weight = kill_house_requests.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 kill_house_allocations_weight = \ kill_house_allocations.aggregate(total=Sum('real_weight_of_carcasses'))['total'] or 0 kill_house_free_sale_bars_weight = kill_house_free_sale_bars.aggregate(total=Sum('real_weight_of_carcasses'))[ 'total'] or 0 segmentation_weight = \ segmentations.aggregate(total=Sum('weight'))[ 'total'] or 0 archives = WarehouseArchive.objects.filter(kill_house=kill_house,date__date=production_date,quota='governmental', trash=False) archives_governmental_weight = \ archives.aggregate(total=Sum('weight'))[ 'total'] or 0 total_input = kill_house_requests_weight total_output = kill_house_allocations_weight + kill_house_free_sale_bars_weight + segmentation_weight + archives_governmental_weight total_remain = total_input - total_output if total_remain <= 0: return True else: if kill_house.ware_house_remaining_percent_limitation_status: if (total_input * (kill_house.ware_house_remaining_percent_limitation / 100)) > total_remain: return True else: return False else: return True