import datetime from io import BytesIO import jdatetime import openpyxl from django.db.models import Sum, Q, F from openpyxl import Workbook from openpyxl.styles import PatternFill, Alignment, Font from openpyxl.utils import get_column_letter from django.http import HttpResponse, QueryDict from panel.KillHouse.serializers import BarDifferenceRequestSerializer from panel.convert_date import convert_to_shamsi from panel.filterset import PoultryHatchingFilterSet, PoultryManageFilterSet, BarDifferenceRequestFilterSet, \ PoultryRequestDirectBuyingFilterSet from panel.helper import build_query from panel.helper_excel import shamsi_date, create_header, excel_description, create_header_freez, create_value, \ add_chart, convert_str_to_date from panel.models import Poultry, PoultryHatching, CityOperator, VetFarm, PoultryRequest, KillHouseRequest, \ ProvinceKillRequest, KillHouseAssignmentInformation, KillHouseVet, VetCheckRequest, BarDifferenceRequest, KillHouse, \ FreeSaleWithinprovince, ShowMarketRequest from authentication.models import ( SystemUserProfile, ) from panel.poultry.helpers import poultry_prediction_helper from panel.poultry.serializers import PoultryHatchingForPredictionSerializer, PoultryRequestForDirectBuyingSerializer def management_poultry(request): filterset_class = PoultryManageFilterSet poultreis = Poultry.objects.filter(trash=False).select_related('user', 'address__province', 'address__city') if 'role' in request.GET: user = SystemUserProfile.objects.get(key=request.GET['key']) if request.GET['role'] == 'CityOperator': city_operator = CityOperator.objects.get(trash=False, user=user) poultrys = poultreis.filter(poultry__city_operator=city_operator.unit_name) elif request.GET['role'] in ['CityJahad', 'CityPoultry']: poultrys = poultreis.filter(poultry__user__city=user.city) else: poultrys = Poultry.objects.filter(trash=False).select_related('user', 'address__province', 'address__city') else: poultrys = poultreis value = request.GET.get('value') search = request.GET.get('search') if value and search == 'filter': if value != 'undefined' and value.strip(): poultrys = poultreis.filter( build_query(filterset_class, value) ) excel_options = [ 'ردیف', 'نام واحد', 'مالک', 'تلفن', 'شناسه یکتا', 'کد اپیدمیولوژیک', 'کد بهداشتی', 'تعداد سالن', 'ظرفیت فارم', 'استان/شهر', 'تعاونی', 'دامپزشک فارم/موبایل', 'جوجه ریزی فعال', 'تعداد دوره جوجه ریزی', 'مجموع جوجه ریزی', 'میانگین جوجه ریزی', 'تعداد درخواست کشتار', 'حجم درخواست کشتار', 'تعداد بار', 'حجم بار', 'وزن بار', 'میانگین وزن', 'تعداد بار مستند', 'حجم بار مستند', 'وزن بار مستند', 'میانگین وزنی بار مستند', 'تعداد بار خارج استان', 'حجم بار خارج استان', 'وزن تقریبی بار', 'میانگین وزن', 'تعداد بار ورودی به انبار', 'حجم ورودی به انبار', 'وزن ورودی به انبار', 'میانگین درصد افت', ] output = BytesIO() workbook = Workbook() worksheet = workbook.active worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) blue_fill = PatternFill(start_color="1E487B", fill_type="solid") cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') for col_num, option in enumerate(excel_options, 1): col_letter = get_column_letter(col_num) cell = worksheet.cell(row=5, column=col_num, value=option) cell.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) cell.fill = blue_fill cell.font = Font(size=10, bold=True, color='D9FFFFFF') worksheet.column_dimensions[col_letter].width = 20.01 worksheet.row_dimensions[5].height = 18 worksheet.freeze_panes = worksheet['A6'] max_col = worksheet.max_column range_str = f'A5:{get_column_letter(max_col)}{worksheet.max_row}' worksheet.auto_filter.ref = range_str header_list2 = [ 'تعداد فارم ها', 'تعداد کل دوره جوجه ریزی ', 'میانگین کل دوره جوجه ریزی فارم ها ', 'حجم کل جوجه ریزی ', 'جوجه ریزی های فعال', 'حجم جوجه ریزی فعال ', 'حجم مانده در سالن فعال', 'تعداد کل درخواست کشتار', 'حجم کل درخواست کشتار', 'تعداد کل بار', 'حجم کل بار', 'وزن کل بار', 'میانگین کل وزن', 'تعداد کل بار مستند', 'حجم کل بار مستند', 'وزن کل بار مستند', 'میانگین وزنی کل بار مستند', 'تعداد کل بار خارج استان', 'حجم کل بار خارج استان', 'وزن کل تقریبی بار خارج استان', 'میانگین وزن کل بار خارج استان', 'تعداد بار ورودی به انبار', 'حجم ورودی به انبار', 'وزن ورودی به انبار', 'میانگین درصد افت', ] for col_num, option in enumerate(header_list2, 3): cell = worksheet.cell(row=2, column=col_num, value=option) cell.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) cell.fill = PatternFill(start_color="00B050", fill_type="solid") cell.font = Font(size=9, bold=True, color='D9FFFFFF') worksheet.row_dimensions[2].height = 20.8 l = 4 m = 1 all_hatching_pending = 0 all_total_quantity = 0 all_poultry_request = 0 all_kill_request = 0 all_kill_request_quantity = 0 all_kill_request_weight = 0 all_kill_request_has_assigment = 0 all_kill_request_quantity_assigment = 0 all_kill_request_weight_assigment = 0 all_total_quantity_poultry_hatching_pending = 0 all_total_left_over_poultry_hatching_pending = 0 all_total_quantity_poultry_request = 0 all_len_out_poultry_request = 0 all_total_out_quantity_poultry_request = 0 all_total_out_weight_poultry_request = 0 all_period = 0 len_werhouse_enter = 0 all_werhouse_enter_quantity = 0 all_werhouse_enter_weight = 0 all_all_weight_loss = 0 for poultry in poultrys: user_mobile = poultry.user.mobile if poultry.user else '-' user_name = poultry.user.fullname if poultry.user else '-' city_operator = poultry.city_operator if poultry.city_operator else '-' health_certificate_number = poultry.health_certificate_number if poultry.health_certificate_number else '-' epidemiological_code = poultry.epidemiological_code if poultry.epidemiological_code else '-' vet_farm = VetFarm.objects.filter(poultry=poultry, trash=False).select_related('vet__user').last() vet_name = '-' mobile_vet = '-' if vet_farm: vet_name = vet_farm.vet.user.fullname mobile_vet = vet_farm.vet.user.mobile poultry_hatching = PoultryHatching.objects.filter(poultry=poultry, trash=False).only('quantity', 'left_over' ) poultry_hatching_pending = poultry_hatching.filter(state='pending', allow_hatching='pending', archive=False).last() if poultry_hatching_pending: all_hatching_pending += 1 hatching = 'ندارد' if not poultry_hatching_pending else f'دارد' period = 0 if poultry_hatching_pending: period = poultry_hatching_pending.period else: if poultry_hatching: period = poultry_hatching.last().period all_period += period poultry_hatching_pending1 = poultry_hatching.filter(state='pending', allow_hatching='pending', archive=False) total_quantity = \ poultry_hatching.aggregate(total=Sum('quantity'))[ 'total'] or 0 total_quantity_poultry_hatching_pending = \ poultry_hatching_pending1.aggregate(total=Sum('quantity'))[ 'total'] or 0 total_left_over_poultry_hatching_pending = \ poultry_hatching_pending1.aggregate(total=Sum('left_over'))[ 'total'] or 0 all_total_quantity += total_quantity all_total_quantity_poultry_hatching_pending += total_quantity_poultry_hatching_pending all_total_left_over_poultry_hatching_pending += total_left_over_poultry_hatching_pending poultry_request = PoultryRequest.objects.filter(trash=False, poultry=poultry, state_process__in=('accepted', 'pending'), province_state__in=('accepted', 'pending')).only('quantity', 'Index_weight') kill_request = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__in=poultry_request).only( 'accepted_real_quantity', 'accepted_real_weight') out_poultry_request = poultry_request.filter(out=True, out_province_request_cancel=False).only( 'quantity', 'Index_weight' ) total_out_quantity_poultry_request = \ out_poultry_request.aggregate(total=Sum('quantity'))[ 'total'] or 0 total_out_weight_poultry_request = \ out_poultry_request.aggregate(total=Sum(F('quantity') * F('Index_weight')))[ 'total'] or 0 all_total_out_quantity_poultry_request += total_out_quantity_poultry_request all_total_out_weight_poultry_request += total_out_weight_poultry_request all_len_out_poultry_request += len(out_poultry_request) total_quantity_poultry_request = \ poultry_request.aggregate(total=Sum('quantity'))[ 'total'] or 0 all_total_quantity_poultry_request += total_quantity_poultry_request all_poultry_request += len(poultry_request) all_kill_request += len(kill_request) kill_request_quantity = kill_request.aggregate( total=Sum('accepted_real_quantity'))[ 'total'] or 0 all_kill_request_quantity += kill_request_quantity kill_request_weight = kill_request.aggregate( total=Sum('accepted_real_weight'))[ 'total'] or 0 all_kill_request_weight += int(kill_request_weight) kill_request_has_assigment = kill_request.filter(assignment_state_archive='True').only('accepted_real_quantity', 'accepted_real_weight') all_kill_request_has_assigment += len(kill_request_has_assigment) kill_request_quantity_assigment = kill_request_has_assigment.aggregate( total=Sum('accepted_real_quantity'))[ 'total'] or 0 all_kill_request_quantity_assigment += kill_request_quantity_assigment kill_request_weight_assigment = kill_request_has_assigment.aggregate( total=Sum('accepted_real_weight'))[ 'total'] or 0 all_kill_request_weight_assigment += int(kill_request_weight_assigment) werhouse_enter = kill_request.filter(ware_house_confirmation=True).only('ware_house_accepted_real_quantity', 'ware_house_accepted_real_weight', 'weight_loss') werhouse_enter_quantity = werhouse_enter.aggregate( total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 werhouse_enter_weight = werhouse_enter.aggregate( total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 all_weight_loss = werhouse_enter.aggregate( total_quantity=Sum('weight_loss')).get( 'total_quantity', 0) or 0 all_all_weight_loss += all_weight_loss all_weight_loss = round(int(all_weight_loss) / len(werhouse_enter), 2) if len(werhouse_enter) > 0 else 0 len_werhouse_enter += len(werhouse_enter) all_werhouse_enter_quantity += werhouse_enter_quantity all_werhouse_enter_weight += int(werhouse_enter_weight) province = poultry.user.province.name if poultry.user else '-' city = poultry.user.city.name if poultry.user else '-' list1 = [ m, poultry.unit_name, user_name, user_mobile, poultry.breeding_unique_id, epidemiological_code, health_certificate_number, poultry.number_of_halls, poultry.total_capacity, province + '/' + city, city_operator, vet_name + '/' + mobile_vet, hatching, period, total_quantity, round(total_quantity / period) if period > 0 else 0, len(poultry_request), total_quantity_poultry_request, len(kill_request), kill_request_quantity, int(kill_request_weight), round(kill_request_weight / kill_request_quantity, 1) if kill_request_weight > 0 else 0, len(kill_request_has_assigment), kill_request_quantity_assigment, int(kill_request_weight_assigment), round(kill_request_weight_assigment / kill_request_quantity_assigment, 1) if kill_request_weight_assigment > 0 else 0, len(out_poultry_request), total_out_quantity_poultry_request, int(total_out_weight_poultry_request), str(round(total_out_weight_poultry_request / total_out_quantity_poultry_request, 1)) if total_out_quantity_poultry_request > 0 else 0, len(werhouse_enter), werhouse_enter_quantity, int(werhouse_enter_weight), f'%{all_weight_loss}', ] l += 1 for item in range(len(list1)): cell = worksheet.cell(row=l + 1, column=item + 1, value=list1[item]) value = list1[item] # Check if the value is a number before formatting if isinstance(value, (int, float)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) m += 1 row_1 = m - 1 value_header_list = [ m - 1, all_period, round(all_period / row_1) if m > 1 else 0, all_total_quantity, all_hatching_pending, all_total_quantity_poultry_hatching_pending, all_total_left_over_poultry_hatching_pending, all_poultry_request, all_total_quantity_poultry_request, all_kill_request, all_kill_request_quantity, all_kill_request_weight, (round(all_kill_request_weight / all_kill_request_quantity, 1)) if all_kill_request_quantity > 0 else 0, all_kill_request_has_assigment, all_kill_request_quantity_assigment, all_kill_request_weight_assigment, str(round(all_kill_request_weight_assigment / all_kill_request_quantity_assigment, 1)) if all_kill_request_quantity_assigment > 0 else 0, all_len_out_poultry_request, all_total_out_quantity_poultry_request, int(all_total_out_weight_poultry_request), str(round(all_total_out_weight_poultry_request / all_total_out_quantity_poultry_request, 1)) if all_total_out_quantity_poultry_request > 0 else 0, len_werhouse_enter, all_werhouse_enter_quantity, all_werhouse_enter_weight, f'%{round(int(all_all_weight_loss) / len_werhouse_enter, 2) if len_werhouse_enter > 0 else 0}', ] for item in range(len(value_header_list)): cell = worksheet.cell(row=3, column=item + 3, value=value_header_list[item]) value = value_header_list[item] # Check if the value is a number before formatting if isinstance(value, (int, float)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center') cell.font = Font(size=10, bold=True) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', all_total_quantity, '', all_poultry_request, all_total_quantity_poultry_request, all_kill_request, all_kill_request_quantity, all_kill_request_weight, '', all_kill_request_has_assigment, all_kill_request_quantity_assigment, all_kill_request_weight_assigment, '', all_len_out_poultry_request, all_total_out_quantity_poultry_request, int(all_total_out_weight_poultry_request), str(round(all_total_out_weight_poultry_request / all_total_out_quantity_poultry_request, 1)) if all_total_out_quantity_poultry_request > 0 else 0, len_werhouse_enter, all_werhouse_enter_quantity, all_werhouse_enter_weight, f'%{round(int(all_all_weight_loss) / len_werhouse_enter, 2) if len_werhouse_enter > 0 else 0}', ] for item in range(len(list2)): cell = worksheet.cell(row=l + 3, column=item + 1, value=list2[item]) value = list2[item] if isinstance(value, (int)): cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center') cell.font = Font(size=10, bold=True) cell.font = Font(bold=True) cell.fill = PatternFill(start_color="00B050", fill_type="solid") workbook.save(output) output.seek(0) response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ 'Content-Disposition'] = f'attachment; filename="مدیریت مرغداران.xlsx"'.encode( 'utf-8') response.write(output.getvalue()) return response def hatching_excel(request): filterset_class = PoultryHatchingFilterSet unknown = True if 'unknown' in request.GET else False poultry_hatch = PoultryHatching.objects.filter(archive=False, allow_hatching='pending', trash=False,unknown=unknown).select_related('poultry', 'poultry__user').order_by( '-chicken_age') filterset_fields = [ 'poultry__user__first_name', 'poultry__user__last_name', 'poultry__user__mobile', 'poultry__unit_name', 'poultry__address__city__name', 'chicken_age', 'poultry__breeding_unique_id', ] if 'role' in request.GET: user = SystemUserProfile.objects.get(key=request.GET['key']) if request.GET['role'] in ['CityJahad', 'CityPoultry']: filtered_poultry_hatch = poultry_hatch.filter(poultry__address__city=user.city) elif request.GET['role'] == 'CityOperator': city_operator = CityOperator.objects.get(user=user) filtered_poultry_hatch = poultry_hatch.filter(poultry__city_operator=city_operator.unit_name) elif request.GET['role'] == 'ProvinceOperator': if 'chain' in request.GET: filtered_poultry_hatch = poultry_hatch.filter(has_chain_company=True) else: filtered_poultry_hatch = poultry_hatch else: filtered_poultry_hatch = poultry_hatch else: filtered_poultry_hatch = poultry_hatch if 'age2' in request.GET: if int(request.GET.get('age2')) > 0: age1 = int(request.GET.get('age1')) age2 = int(request.GET.get('age2')) filtered_poultry_hatch = filtered_poultry_hatch.filter(trash=False, chicken_age__gte=age1, chicken_age__lte=age2) else: filtered_poultry_hatch = filtered_poultry_hatch if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() date1 = datetime.datetime.now().date() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'تلفن مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیشبینی تاریخ کشتار', 'نژاد', 'سن', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک(قطعه)', 'تلفات اتحادیه(قطعه)', 'تلفات کل(قطعه)', 'زنجیره/شرکت', 'دامپزشک فارم', 'تلفن دامپزشک فارم', 'کد سیستمی واحد', 'حجم کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'درصد مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'وزن تعهد آزاد(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'حجم فروش به خارج از استان(قطعه)', 'حجم فروش به خارج از استان(کیلوگرم)', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'تایید تخلیه در سماصط', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بارهای تحویلی', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ گزارش', ] from_date_1 = shamsi_date(date1) output = BytesIO() workbook = Workbook() worksheet = workbook.active worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) blue_fill = PatternFill(start_color="1E487B", fill_type="solid") cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') red_font = Font(color="C00000", bold=True) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 poultry_ids = poultry_hatch.values_list('poultry', flat=True).distinct() vet_farms = VetFarm.objects.filter(poultry__in=poultry_ids, trash=False).select_related('vet__user') vet_farm_mapping = { vet_farm.poultry: (vet_farm.vet.user.fullname, vet_farm.vet.user.mobile) for vet_farm in vet_farms } l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching) governmental_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=False) governmental_province_kill_requests_quantity = \ governmental_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 free_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=True) free_province_kill_requests_quantity = \ free_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 out_poultry_requests = PoultryRequest.objects.filter(trash=False, out=True, state_process='accepted', province_state='accepted', out_province_request_cancel=False, final_state='archive', hatching=poultry_hatching) out_poultry_requests_quantity = out_poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 free_sale_province = FreeSaleWithinprovince.objects.filter(trash=False).first() if free_sale_province.allow == False: left_total_free_commitment_quantity = poultry_hatching.left_over elif poultry_hatching.total_free_commitment_quantity == 0: left_total_free_commitment_quantity = 0 else: if poultry_hatching.total_commitment_quantity == 0: left_total_free_commitment_quantity = poultry_hatching.left_over else: left_total_free_commitment_quantity = poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity) if ( poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity)) > 0 else 0 # return { # "governmental_allocated_quantity": governmental_province_kill_requests_quantity, # "total_commitment_quantity": obj.total_commitment_quantity, # "free_allocated_quantity": free_province_kill_requests_quantity + out_poultry_requests_quantity, # "total_free_commitment_quantity": obj.total_free_commitment_quantity, # "left_total_free_commitment_quantity": left_total_free_commitment_quantity, # # } vet_farm_id = poultry_hatching.poultry vet_farm_name, vet_farm_mobile = vet_farm_mapping.get(vet_farm_id, ('-', '-')) all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) predicate_date = jdatetime.date.fromgregorian( day=poultry_hatching.predicate_date.day, month=poultry_hatching.predicate_date.month, year=poultry_hatching.predicate_date.year ) if poultry_hatching.predicate_date else '-' date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 wothout_bar = province_kill_requests.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0) province_kill_requests_quantity_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_quantity'))['total'] or 0 province_kill_requests_weight_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_weight'))['total'] or 0 active_kill = 'ندارد' count_of_request = 0 province_kill_request_active = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='accepted') if province_kill_request_active.count() > 0: count_of_request = province_kill_request_active.count() if ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='pending').exists(): active_kill = 'دارد' list1 = [ m, farm_state, poultry_hatching.licence_number, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.CertId, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.user.mobile, poultry_hatching.InteractTypeName, poultry_hatching.PersonTypeName, poultry_hatching.UnionTypeName, f'{poultry_hatching.poultry.user.city.name if poultry_hatching.poultry.user.city else "-"} ' f'/ {poultry_hatching.poultry.city_operator if poultry_hatching.poultry.city_operator else "-"}', poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.poultry.killing_ave_age, str(predicate_date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.increase_quantity, poultry_hatching.losses, poultry_hatching.direct_losses, poultry_hatching.total_losses, chain_company, # poultry_hatching.poultry.city_operator, # # # vet_farm_name, vet_farm_mobile, poultry_hatching.poultry.system_code, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, f'{int(poultry_hatching.left_over * 100 / poultry_hatching.quantity)}%', left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.total_free_commitment_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(wothout_bar), province_kill_requests_quantity_wothout_bar, province_kill_requests_weight_wothout_bar, poultry_hatching.samasat_discharge_percentage, active_kill, count_of_request, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, poultry_hatching.chain_killed_quantity, poultry_hatching.chain_killed_weight, poultry_hatching.export_killed_quantity, poultry_hatching.export_killed_weight, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) all_increase_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('increase_quantity')).get( 'total_quantity', 0) all_total_free_commitment_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_free_commitment_quantity')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 chain_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_weight')).get( 'total_quantity', 0) or 0 chain_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_quantity')).get( 'total_quantity', 0) or 0 extra_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_quantity')).get( 'total_quantity', 0) or 0 export_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_weight')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', # وضعیت '', # شماره مجوز جوجه ریزی '', # شناسه یکتا '', # مجوز بهداشتی جوجه ریزی '', # نام فارم '', # مرغدار '', # بهره برداری '', # مالکیت '', # ارتباط '', # شهر/تعاونی '', # دامپزشک فارم '', # سالن '', # دوره جوجه ریزی '', # تاریخ ثبت جوجه ریزی '', # تاریخ جوجه ریزی '', # میانگین سن کشتار '', # پیش بینی تاریخ کشتار '', # نژاد '', # سن ورود به بایگانی '', # تاریخ ورود به بایگانی '', # سن فعلی all_quantity, # حجم جوجه ریزی '', # حجم افزایشی all_losses, all_direct_losses, all_total_losses, '', '', '', '', all_killed_quantity, all_total_killed_weight, all_left_over, '', all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_total_free_commitment_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, '', '', '', '', '', '', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') workbook.save(output) output.seek(0) response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ 'Content-Disposition'] = f'attachment; filename="جوجه ریزی.xlsx"'.encode( 'utf-8') response.write(output.getvalue()) return response def get_vet_farm_name(poultry): """Get vet farm name for poultry""" try: from panel.models import VetFarm vet_farm = VetFarm.objects.filter(poultry=poultry, trash=False).first() if vet_farm and vet_farm.vet: return vet_farm.vet.user.fullname except: pass return '-' def calculate_predicted_kill_date(poultry_hatching): """Calculate predicted kill date based on age and average kill age""" try: if poultry_hatching.poultry.killing_ave_age and poultry_hatching.chicken_age: remaining_days = poultry_hatching.poultry.killing_ave_age - poultry_hatching.chicken_age if remaining_days > 0: predicted_date = poultry_hatching.date + datetime.timedelta(days=remaining_days) return convert_to_shamsi(year=predicted_date.year, month=predicted_date.month, day=predicted_date.day) except: pass return '-' def calculate_current_age(poultry_hatching): """Calculate current age of chickens""" try: if poultry_hatching.date: current_date = datetime.datetime.now().date() age = (current_date - poultry_hatching.date.date()).days return age except: pass return poultry_hatching.chicken_age def calculate_governmental_killed_weight(poultry_hatching): """Calculate governmental killed weight""" try: if poultry_hatching.governmental_killed_quantity and poultry_hatching.total_average_killed_weight: return round(poultry_hatching.governmental_killed_quantity * poultry_hatching.total_average_killed_weight, 2) except: pass return 0 def calculate_free_killed_weight(poultry_hatching): """Calculate free killed weight""" try: if poultry_hatching.free_killed_quantity and poultry_hatching.total_average_killed_weight: return round(poultry_hatching.free_killed_quantity * poultry_hatching.total_average_killed_weight, 2) except: pass return 0 def calculate_governmental_commitment_weight(poultry_hatching): """Calculate governmental commitment weight""" try: if poultry_hatching.governmental_quantity and poultry_hatching.total_average_killed_weight: return round(poultry_hatching.governmental_quantity * poultry_hatching.total_average_killed_weight, 2) except: pass return 0 def calculate_unloaded_allocations(poultry_hatching): """Calculate unloaded allocations count""" try: from panel.models import ProvinceKillRequest unloaded = ProvinceKillRequest.objects.filter( trash=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching, first_car_allocated_quantity=0, return_to_province=False ).count() return unloaded except: pass return 0 def calculate_unloaded_allocations_quantity(poultry_hatching): """Calculate unloaded allocations quantity""" try: from panel.models import ProvinceKillRequest from django.db.models import Sum quantity = ProvinceKillRequest.objects.filter( trash=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching, first_car_allocated_quantity=0, return_to_province=False ).aggregate(total=Sum('total_killed_quantity'))['total'] or 0 return quantity except: pass return 0 def calculate_unloaded_allocations_weight(poultry_hatching): """Calculate unloaded allocations weight""" try: from panel.models import ProvinceKillRequest from django.db.models import Sum weight = ProvinceKillRequest.objects.filter( trash=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching, first_car_allocated_quantity=0, return_to_province=False ).aggregate(total=Sum('total_killed_weight'))['total'] or 0 return weight except: pass return 0 def calculate_radar_discharge_confirmation(poultry_hatching): """Calculate radar discharge confirmation""" # This would need to be implemented based on your specific radar system return '-' def calculate_active_kill_count(poultry_hatching): """Calculate active kill count""" try: from panel.models import PoultryRequest active_count = PoultryRequest.objects.filter( trash=False, state_process__in=('pending', 'accepted'), province_state__in=('pending', 'accepted'), out_province_request_cancel=False, hatching=poultry_hatching ).count() return active_count except: pass return 0 def calculate_kill_request_count(poultry_hatching): """Calculate total kill request count""" try: from panel.models import ProvinceKillRequest, KillHouseRequest province_count = ProvinceKillRequest.objects.filter( trash=False, province_request__poultry_request__hatching=poultry_hatching ).count() kill_house_count = KillHouseRequest.objects.filter( trash=False, province_request__poultry_request__hatching=poultry_hatching ).count() return province_count + kill_house_count except: pass return 0 def archive_hatching_excel(request): filterset_class = PoultryHatchingFilterSet filterset_fields = [ 'poultry__user__first_name', 'poultry__user__last_name', 'poultry__user__mobile', 'poultry__unit_name', 'poultry__address__city__name', 'chicken_age', 'poultry__breeding_unique_id', ] filtered_poultry_hatch = PoultryHatching.objects.filter( Q(allow_hatching=True, state='complete') | Q(archive=True), trash=False ).select_related('poultry', 'poultry__user').order_by('-create_date') if 'date1' in request.GET: date1 = datetime.datetime.strptime(request.GET['date1'], '%Y-%m-%d').date() date2 = datetime.datetime.strptime(request.GET['date2'], '%Y-%m-%d').date() filtered_poultry_hatch = filtered_poultry_hatch.filter(date__date__gte=date1, date__date__lte=date2) user = SystemUserProfile.objects.get(key=request.GET['key']) if 'chain' in request.GET: filtered_poultry_hatch = filtered_poultry_hatch.filter(has_chain_company=True) if request.GET['role'] in ['CityJahad', 'CityPoultry']: filtered_poultry_hatch = filtered_poultry_hatch.filter(poultry__address__city=user.city) elif request.GET['role'] == 'CityOperator': city_operator = CityOperator.objects.get(user=user) filtered_poultry_hatch = filtered_poultry_hatch.filter(poultry__city_operator=city_operator.unit_name) else: filtered_poultry_hatch = filtered_poultry_hatch if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'دامپزشک فارم', 'سالن', 'دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیش بینی تاریخ کشتار', 'نژاد', 'سن ورود به بایگانی', 'تاریخ ورود به بایگانی', 'سن فعلی', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک', 'تلفات اتحادیه', 'تلفات کل', 'حجم تعهد دولتی', 'حجم تعهد آزاد', 'حجم کشتار دولتی', 'وزن کشتار دولتی', 'حجم کشتار آزاد', 'وزن کشتار شده آزاد', 'حجم فروش به خارج استان', 'وزن فروش به خارج استان', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'حجم کشتار شده', 'حجم مانده در سالن', 'تلفات', 'کشتار شده', 'باقی مانده در سالن', 'تایید تخلیه رصدیار', 'تایید تخلیه در سماصط', 'وزن تعهد دولتی', 'وزن کشتار دولتی', 'وزن کشتار آزاد', 'میانگین وزن کشتار', 'وزن کل کشتار شده', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بارها', 'حجم بارها', 'وزن بارها', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'بارهای ورودی به انبار', 'حجم لاشه های انبار', 'وزن لاشه های انبار', 'درصد افت بارها', 'آخرین تغییر', 'سازنده جوجه ریزی' ] date1 = datetime.datetime.now().date() from_date_1 = shamsi_date(date1) output = BytesIO() workbook = Workbook() worksheet = workbook.active worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = convert_to_shamsi(year=poultry_hatching.create_date.year, month=poultry_hatching.create_date.month, day=poultry_hatching.create_date.day, ) date = convert_to_shamsi( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = poultry_hatching.chicken_age creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' list1 = [ m, # ردیف farm_state, # وضعیت poultry_hatching.licence_number, # شماره مجوز جوجه ریزی poultry_hatching.poultry.breeding_unique_id, # شناسه یکتا poultry_hatching.poultry.health_certificate_number if poultry_hatching.poultry.health_certificate_number else '-', # مجوز بهداشتی جوجه ریزی poultry_hatching.poultry.unit_name, # نام فارم poultry_hatching.poultry.user.fullname, # مرغدار poultry_hatching.poultry.person_type if poultry_hatching.poultry.person_type else '-', # بهره برداری 'مالک' if not poultry_hatching.poultry.has_tenant else 'مستاجر', # مالکیت poultry_hatching.poultry.communication_type if poultry_hatching.poultry.communication_type else '-', # ارتباط f"{poultry_hatching.poultry.user.city.name}/{poultry_hatching.poultry.city_operator}" if poultry_hatching.poultry.city_operator else poultry_hatching.poultry.user.city.name, # شهر/تعاونی get_vet_farm_name(poultry_hatching.poultry), # دامپزشک فارم poultry_hatching.hall, # سالن poultry_hatching.period, # دوره جوجه ریزی str(create_date), # تاریخ ثبت جوجه ریزی str(date), # تاریخ جوجه ریزی poultry_hatching.poultry.killing_ave_age, # میانگین سن کشتار calculate_predicted_kill_date(poultry_hatching), # پیش بینی تاریخ کشتار poultry_hatching.chicken_breed, # نژاد poultry_hatching.chicken_age, # سن ورود به بایگانی convert_to_shamsi(year=poultry_hatching.archive_date.year, month=poultry_hatching.archive_date.month, day=poultry_hatching.archive_date.day) if poultry_hatching.archive_date else '-', # تاریخ ورود به بایگانی calculate_current_age(poultry_hatching), # سن فعلی poultry_hatching.quantity, # حجم جوجه ریزی poultry_hatching.increase_quantity if hasattr(poultry_hatching, 'increase_quantity') else 0, # حجم افزایشی poultry_hatching.losses, # تلفات دامپزشک poultry_hatching.direct_losses, # تلفات اتحادیه poultry_hatching.total_losses, # تلفات کل poultry_hatching.total_commitment, # حجم تعهد دولتی poultry_hatching.free_quantity, # حجم تعهد آزاد poultry_hatching.governmental_killed_quantity, # حجم کشتار دولتی calculate_governmental_killed_weight(poultry_hatching), # وزن کشتار دولتی poultry_hatching.free_killed_quantity, # حجم کشتار آزاد calculate_free_killed_weight(poultry_hatching), # وزن کشتار شده آزاد poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else 0, # حجم فروش به خارج استان poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else 0, # وزن فروش به خارج استان calculate_unloaded_allocations(poultry_hatching), # تخصیصات بدون بار calculate_unloaded_allocations_quantity(poultry_hatching), # حجم تخصیصات بدون بار calculate_unloaded_allocations_weight(poultry_hatching), # وزن تخصیصات بدون بار poultry_hatching.killed_quantity, # حجم کشتار شده poultry_hatching.left_over, # حجم مانده در سالن poultry_hatching.total_losses, # تلفات poultry_hatching.killed_quantity, # کشتار شده poultry_hatching.left_over, # باقی مانده در سالن calculate_radar_discharge_confirmation(poultry_hatching), # تایید تخلیه رصدیار poultry_hatching.samasat_discharge_percentage if hasattr(poultry_hatching, 'samasat_discharge_percentage') else 0, # تایید تخلیه در سماصط calculate_governmental_commitment_weight(poultry_hatching), # وزن تعهد دولتی calculate_governmental_killed_weight(poultry_hatching), # وزن کشتار دولتی calculate_free_killed_weight(poultry_hatching), # وزن کشتار آزاد str(poultry_hatching.total_average_killed_weight), # میانگین وزن کشتار poultry_hatching.total_killed_weight, # وزن کل کشتار شده calculate_active_kill_count(poultry_hatching), # تعداد کشتار فعال calculate_kill_request_count(poultry_hatching), # تعداد درخواست کشتار len(kill_house_requests), # تعداد بارها first_quantity, # حجم بارها first_weight, # وزن بارها accepted_real_quantity_final, # حجم بارهای تحویلی accepted_real_wight_final, # وزن بارهای تحویلی poultry_hatching.chain_killed_quantity if hasattr(poultry_hatching, 'chain_killed_quantity') else 0, # حجم زنجیره poultry_hatching.chain_killed_weight if hasattr(poultry_hatching, 'chain_killed_weight') else 0, # وزن زنجیره poultry_hatching.export_killed_quantity if hasattr(poultry_hatching, 'export_killed_quantity') else 0, # حجم صادرات poultry_hatching.export_killed_weight if hasattr(poultry_hatching, 'export_killed_weight') else 0, # وزن صادرات len(ware_house_bars), # بارهای ورودی به انبار ware_house_bars_quantity, # حجم لاشه های انبار ware_house_bars_weight, # وزن لاشه های انبار str(round(ware_house_bars_weight_lose / len(ware_house_bars), 2) if ware_house_bars else 0), # درصد افت بارها shamsi_date(convert_str_to_date(poultry_hatching.last_change.get('date', ''))) if poultry_hatching.last_change and poultry_hatching.last_change.get('date') else '', # آخرین تغییر creator, # سازنده جوجه ریزی ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_losses, all_direct_losses, all_total_losses, all_killed_quantity, all_total_killed_weight, all_left_over, all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') workbook.save(output) output.seek(0) response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ 'Content-Disposition'] = f'attachment; filename=" جوجه ریزی بایگانی شده.xlsx"'.encode( 'utf-8') response.write(output.getvalue()) return response def hatching_age_range_excel(request): filtered_poultry_hatchs = PoultryHatching.objects.filter(state__in=('pending', 'complete'), archive=False, allow_hatching='pending', trash=False).order_by('-create_date').select_related( 'poultry') if 'role' in request.GET: user = SystemUserProfile.objects.get(key=request.GET['key']) if request.GET['role'] == 'CityOperator': city_operator = CityOperator.objects.get(trash=False, user=user) filtered_poultry_hatch = filtered_poultry_hatchs.filter(poultry__city_operator=city_operator.unit_name) elif request.GET['role'] in ['CityJahad', 'CityPoultry']: filtered_poultry_hatch = filtered_poultry_hatchs.filter(poultry__user__city=user.city) else: filtered_poultry_hatch = filtered_poultry_hatchs else: filtered_poultry_hatch = filtered_poultry_hatchs excel_options = [ 'ردیف', 'نام فارم', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'نژاد', 'سن', 'تعداد جوجه ریزی', 'تلفات دوره', 'کشتار شده', 'مانده در سالن', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] output = BytesIO() workbook = Workbook() worksheet = workbook.active worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) blue_fill = PatternFill(start_color="1E487B", fill_type="solid") cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') header_list = [ 'تعداد کل فارم ها', 'مجموع جوجه ریزی', 'مجموع کشتار شده', 'مجموع مانده در سالن', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] for col_num, option in enumerate(header_list, 3): col_letter = get_column_letter(col_num) cell = worksheet.cell(row=2, column=col_num, value=option) cell.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) cell.fill = PatternFill(start_color="31869B", fill_type="solid") cell.font = Font(size=10, bold=True, color='FFFFFF') worksheet.row_dimensions[2].height = 30 cell.border = openpyxl.styles.Border( left=openpyxl.styles.Side(style='thin'), right=openpyxl.styles.Side(style='thin'), top=openpyxl.styles.Side(style='thin'), bottom=openpyxl.styles.Side(style='thin') ) for col_num, option in enumerate(excel_options, 1): col_letter = get_column_letter(col_num) cell = worksheet.cell(row=5, column=col_num, value=option) cell.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) cell.fill = blue_fill cell.font = Font(size=10, bold=True, color='D9FFFFFF') worksheet.column_dimensions[col_letter].width = 20.01 worksheet.row_dimensions[1].height = 18 worksheet.freeze_panes = worksheet['A6'] max_col = worksheet.max_column range_str = f'A5:{get_column_letter(max_col)}{worksheet.max_row}' worksheet.auto_filter.ref = range_str l = 4 m = 1 for poultry_hatching in filtered_poultry_hatch: l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) poultry_requests = PoultryRequest.objects.filter(hatching=poultry_hatching, trash=False).only('quantity') age = (datetime.datetime.now().date() - poultry_hatching.date.date()).days + 1 quantity_sum = 0 for poultry_request in poultry_requests: if poultry_request and hasattr(poultry_request, 'quantity'): quantity_sum += poultry_request.quantity kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 list1 = [ m, poultry_hatching.poultry.unit_name, poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.losses, poultry_hatching.killed_quantity, poultry_hatching.left_over, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] for item in range(len(list1)): cell = worksheet.cell(row=l + 1, column=item + 1, value=list1[item]) cell.alignment = Alignment(horizontal='center') value = list1[item] # Check if the value is a number before formatting if isinstance(value, (int, float)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values m += 1 total_poultry_hatchings_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity') or 0 total_poultry_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity') or 0 total_poultry_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity') or 0 kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 value_list = [ m - 1, total_poultry_hatchings_quantity, total_poultry_killed_quantity, total_poultry_left_over, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), ] for item in range(len(value_list)): cell = worksheet.cell(row=3, column=item + 3, value=value_list[item]) cell.alignment = Alignment(horizontal='center') value = value_list[item] # Check if the value is a number before formatting if isinstance(value, (int, float)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values workbook.save(output) output.seek(0) response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ 'Content-Disposition'] = f'attachment; filename="موجودی جوجه ریزی (مانده در سالن) بر اساس بازه سنی.xlsx"'.encode( 'utf-8') response.write(output.getvalue()) return response def poultry_monitoring_excel(request): poultreis = Poultry.objects.filter(trash=False, key=request.GET['key']).select_related('user', 'address__province', 'address__city').first() hatchings = PoultryHatching.objects.filter(trash=False, poultry=poultreis).order_by('id') output = BytesIO() workbook = Workbook() worksheet = workbook.active workbook.remove(worksheet) sheet_name = ['اطلاعات کلی', 'مدیریت بارها'] for sheet_names in sheet_name: worksheet = workbook.create_sheet(sheet_names) if sheet_names == 'اطلاعات کلی': excel_options = [ ' جوجه ریزی مرحله', 'نام واحد', 'نام مالک', 'تاریخ جوجه ریزی', 'تعداد جوجه ریزی', 'نود درصد جوجه ریزی', 'مجموع تلفات', 'مانده در سالن', 'متوسط سن کشتار', 'تعداد ثبت سفارش', 'تعداد درخواست کشتار (قطعه)', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد تخصیصی استان به خریدار(قطعه)', 'وزن تخصیصی استان به خریدار(کیلوگرم)', 'درصد کشتار نسبت به نود درصد', 'مجموع تخصیصی به ماشین(قطعه)', 'مجموع وزن تخصیصی به ماشین(کیلوگرم)', 'تعداد تخلیه شده دامپزشک', 'وزن تخلیه شده دامپزشک', 'تعداد نهایی در کشتارگاه', 'وزن نهایی در کشتار گاه', 'تعداد بار های وارد شده در انبار', 'مجموع تعداد لاشه', 'مجموع وزن لاشه', 'میانگین درصد افت لاشه', 'حجم کشتار', 'وزن کشتار', 'عملکرد فارم', ] worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) blue_fill = PatternFill(start_color="1E487B", fill_type="solid") cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') for col_num, option in enumerate(excel_options, 3): col_letter = get_column_letter(col_num) cell = worksheet.cell(row=3, column=col_num, value=option) cell.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) cell.fill = PatternFill(start_color="31869B", fill_type="solid") cell.font = Font(size=9, bold=True, color='FFFFFF') worksheet.row_dimensions[3].height = 36 worksheet.column_dimensions[col_letter].width = 14 now = datetime.datetime.now().date() now_date = jdatetime.date.fromgregorian( year=now.year, month=now.month, day=now.day ).strftime('%Y-%m-%d') reversed_date = reversed(now_date.split("-")) separate = "-" date_now = separate.join(reversed_date) row_list2 = len(hatchings) + 10 row_city = len(hatchings) + 8 province = poultreis.user.province.name worksheet[ f'F1'] = f'گزارش پایش اطلاعاتی {poultreis.unit_name} سامانه رصدیار استان {province} در تاریخ{date_now} ' worksheet[f'F1'].font = Font(color="C00000", bold=True, size=12) worksheet[f'F1'].alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) merge_range1 = f'F1:I2' worksheet.merge_cells(merge_range1) l = 4 all_quantity_of_car1 = 0 average_age_list1 = [] all_weight1 = [] all_weight_of_car1 = 0 all_main_quantity1 = 0 sum_all_quantity_vet_kill1 = 0 sum_all_wight_vet_kill1 = 0 sum_net_weight1 = 0 sum_real_quantity_assigment1 = 0 sum_accepted_real_quantity1 = 0 sum_accepted_real_weight1 = 0 all_loses1 = 0 all_all_quantity_of_request1 = 0 len_werhouse_enter1 = 0 all_werhouse_enter_quantity1 = 0 all_werhouse_enter_weight1 = 0 all_all_weight_loss1 = 0 for hatching in hatchings: final_left_over = [] all_quantity_of_car = 0 average_age_list = [] all_percent = 0 m = 1 all_weight = [] all_quantity_of_car_vet_state_accepted = 0 request_weight = [] all_request_weight = [] all_free_sale_in_province_true = 0 all_kill_province_request_poultry_request_Index_weight = [] all_weight_of_car = 0 all_weight_of_car_vet_state_accepted = 0 all_main_quantity = 0 sum_quantity_qarantineh = 0 sum_all_quantity_vet_kill = 0 sum_all_wight_vet_kill = 0 sum_net_weight = 0 sum_real_quantity_assigment = 0 sum_accepted_real_quantity = 0 sum_accepted_real_weight = 0 all_free_sale_in_province_false = 0 all_loses = 0 all_province_quantity = 0 quantity = hatching.quantity ninety_percent = hatching.quantity all_len_werhouse_enter = 0 all_werhouse_enter_quantity = 0 all_werhouse_enter_weight = 0 all_all_weight_loss = 0 killed = 0 hatch_date = jdatetime.date.fromgregorian( day=hatching.date.day, month=hatching.date.month, year=hatching.date.year ) poultry_requests = PoultryRequest.objects.filter(out_province_request_cancel=False, trash=False, hatching=hatching, archive=False, state_process__in=('accepted', 'pending')).order_by( 'id') werhouse_enter1 = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=hatching, ware_house_confirmation=True) werhouse_enter_quantity1 = werhouse_enter1.aggregate( total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 werhouse_enter_weight1 = werhouse_enter1.aggregate( total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 all_weight_loss2 = werhouse_enter1.aggregate( total_quantity=Sum('weight_loss')).get( 'total_quantity', 0) or 0 all_all_weight_loss1 += all_weight_loss2 all_weight_loss1 = round(int(all_weight_loss2) / len(werhouse_enter1), 2) if len( werhouse_enter1) > 0 else 0 len_werhouse_enter1 += len(werhouse_enter1) all_werhouse_enter_quantity1 += werhouse_enter_quantity1 all_werhouse_enter_weight1 += int(werhouse_enter_weight1) excel_options = [ 'ردیف', 'کد سفارش', 'تاریخ ثبت درخواست', 'تاریخ کشتار', 'سن کشتار', 'نوع درخواست', 'فروش', 'تعداد درخواست کشتار', 'میانگین وزنی درخواست', 'وزن کل درخواست ', 'ماهیت خریدار', 'خریدار', 'تلفن خریدار', 'تعداد تخصیصی استان به خریدار(قطعه)', 'وزن تخصیصی استان به خریدار', 'مانده در سالن', ' تعداد بار', 'تعداد بار قطعه', 'وزن بار', 'تعداد قطعه وارد شده در قرنطینه', 'تعداد تخلیه شده دامپزشک', 'وزن بار تخلیه شده(کیلوگرم)دامپزشک', 'تعداد نهایی در کشتارگاه', 'وزن نهایی در کشتار گاه', 'تعداد بار ورود به انبار', 'حجم بار ورود به انبار', 'وزن بار ورود به انبار', 'درصد افت', 'حجم کشتار', 'وزن کشتار', ] if poultry_requests: worksheet[f'E{row_list2 - 2}'] = f'اطلاعات جوجه ریزی {hatching.period}' worksheet[f'E{row_list2 - 2}'].font = Font(color="C00000", bold=True, size=12) worksheet[f'E{row_list2 - 2}'].alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) merge_range1 = f'E{row_list2 - 2}:I{row_list2 - 1}' worksheet.merge_cells(merge_range1) for col_num, option in enumerate(excel_options, 1): col_letter = get_column_letter(col_num) cell = worksheet.cell(row=row_list2, column=col_num, value=option) cell.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) cell.fill = blue_fill cell.font = Font(size=10, bold=True, color='D9FFFFFF') worksheet.column_dimensions[col_letter].width = 20 worksheet.row_dimensions[row_list2].height = 36 for poultry_request in poultry_requests: all_loses += poultry_request.hatching.losses all_loses1 += poultry_request.hatching.losses kill_request = ProvinceKillRequest.objects.filter( province_request__poultry_request=poultry_request, trash=False, state__in=('pending', 'accepted'), return_to_province=False).select_related( 'killhouse_user', 'province_request__poultry_request').order_by('-create_date') car_date = jdatetime.date.fromgregorian( day=poultry_request.send_date.day, month=poultry_request.send_date.month, year=poultry_request.send_date.year ) create_date = jdatetime.date.fromgregorian( day=poultry_request.create_date.day, month=poultry_request.create_date.month, year=poultry_request.create_date.year ) average_age = (poultry_request.send_date.date() - hatching.date.date()).days + 1 average_age_list.append(average_age) average_age_list1.append(average_age) if poultry_request.direct_buying == True: type = 'خرید مستقیم' else: type = 'اتحادیه' free_sale_in_province_false = 0 Index_weight_sale_in_province_false = 0 free_sale_in_province_true = 0 all_province_quantity += poultry_request.quantity if poultry_request.out == True: sale_type = 'خارج از استان' elif poultry_request.free_sale_in_province == False: free_sale_in_province_false = poultry_request.quantity all_free_sale_in_province_false += free_sale_in_province_false Index_weight_sale_in_province_false = poultry_request.Index_weight sale_type = 'دولتی' else: sale_type = 'آزاد' free_sale_in_province_true = poultry_request.quantity all_free_sale_in_province_true += free_sale_in_province_true all_request_weight.append(poultry_request.Index_weight * poultry_request.quantity) list1 = [ m, str(poultry_request.order_code), str(create_date), str(car_date), average_age, type, sale_type, poultry_request.quantity, poultry_request.Index_weight, round(poultry_request.quantity * poultry_request.Index_weight, 2), # poultry_request.quantity, ] m += 1 for item in range(len(list1)): cell = worksheet.cell(row=row_list2 + 1, column=item + 1, value=list1[item]) value = list1[item] # Check if the value is a number before formatting if isinstance(value, (int, float)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center', vertical='center') cell.border = openpyxl.styles.Border( left=openpyxl.styles.Side(style='thin'), right=openpyxl.styles.Side(style='thin'), top=openpyxl.styles.Side(style='thin'), bottom=openpyxl.styles.Side(style='thin') ) # worksheet.column_dimensions[get_column_letter(item + 1)].width = 17.01 worksheet.row_dimensions[row_list2 + 1].height = 21 if m % 2 == 0: cell.fill = PatternFill(start_color="DAE1F6", fill_type="solid") # l += 1 if kill_request.exists(): o = row_list2 + 1 if len(kill_request) > 1: s = len(kill_request) - 1 for col in range(ord('A'), ord('J') + 1): # rng = chr(col) + '7:{}'.format(r) rng = f'{chr(col)}{o}:{chr(col)}{o + s}' worksheet.merge_cells(rng) worksheet[chr(col) + f'{o}'].alignment = Alignment(horizontal='center', vertical='center') for kill in kill_request: all_main_quantity += kill.main_quantity all_main_quantity1 += kill.main_quantity state_s = '' if kill.state == 'pending': state_s = 'درانتظار تایید' elif kill.state == 'accepted': state_s = ' تایید شده' quantity_of_car_vet_state_accepted = 0 weight_of_car_vet_state_accepted = 0 quantity_of_car = 0 weight_of_car = 0 kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_kill_request=kill).order_by( 'id').only('create_date', 'vet_state') all_net_weight = 0 all_real_quantity_assigment = 0 werhouse_enter = kill_house_requests.filter(ware_house_confirmation=True).only( 'ware_house_accepted_real_quantity', 'ware_house_accepted_real_weight', 'weight_loss') len_werhouse_enter = len(werhouse_enter) werhouse_enter_quantity = werhouse_enter.aggregate( total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 werhouse_enter_weight = werhouse_enter.aggregate( total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 all_weight_loss = werhouse_enter.aggregate( total_quantity=Sum('weight_loss')).get( 'total_quantity', 0) or 0 all_len_werhouse_enter += len_werhouse_enter all_werhouse_enter_quantity += werhouse_enter_quantity all_werhouse_enter_weight += werhouse_enter_weight all_all_weight_loss += all_weight_loss all_weight_loss = round(int(all_weight_loss) / len(werhouse_enter), 2) if len( werhouse_enter) > 0 else 0 if kill_house_requests: for kill_house_request in kill_house_requests: assignment = KillHouseAssignmentInformation.objects.filter( kill_house_request=kill_house_request, trash=False).only('net_weight', 'real_quantity').first() if assignment: all_net_weight += assignment.net_weight if assignment.net_weight != None else 0 all_real_quantity_assigment += assignment.real_quantity if assignment.real_quantity != None else 0 quantity_of_car += kill_house_request.quantity all_quantity_of_car += kill_house_request.quantity all_quantity_of_car1 += kill_house_request.quantity weight_of_car += int( kill_house_request.quantity * kill_house_request.province_request.poultry_request.Index_weight) if kill_house_request.vet_state == 'accepted': quantity_of_car_vet_state_accepted += kill_house_request.quantity weight_of_car_vet_state_accepted += kill_house_request.accepted_real_weight all_quantity_of_car_vet_state_accepted += kill_house_request.quantity killed += kill.main_quantity all_quantity_qarantineh = kill_house_requests.aggregate( total_quantity=Sum('quarantine_quantity')).get( 'total_quantity', 0) all_quantity_vet_kill = kill_house_requests.aggregate( total_quantity=Sum('vet_accepted_real_quantity')).get( 'total_quantity', 0) all_weight_vet_kill = kill_house_requests.aggregate( total_quantity=Sum('vet_accepted_real_weight')).get( 'total_quantity', 0) all_accepted_real_quantity = kill_house_requests.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) all_accepted_real_weight = kill_house_requests.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity', 0) sum_all_wight_vet_kill += all_weight_vet_kill if all_weight_vet_kill != None else 0 sum_all_wight_vet_kill1 += all_weight_vet_kill if all_weight_vet_kill != None else 0 sum_all_quantity_vet_kill += all_quantity_vet_kill if all_quantity_vet_kill != None else 0 sum_all_quantity_vet_kill1 += all_quantity_vet_kill if all_quantity_vet_kill != None else 0 sum_quantity_qarantineh += all_quantity_qarantineh if all_quantity_qarantineh != None else 0 sum_accepted_real_quantity += all_accepted_real_quantity if all_accepted_real_quantity != None else 0 sum_accepted_real_quantity1 += all_accepted_real_quantity if all_accepted_real_quantity != None else 0 sum_accepted_real_weight += all_accepted_real_weight if all_accepted_real_weight != None else 0 sum_accepted_real_weight1 += all_accepted_real_weight if all_accepted_real_weight != None else 0 left_over = (quantity - killed) final_left_over.append(left_over) percent_after_assigment = (quantity_of_car_vet_state_accepted * 100) / quantity all_percent += percent_after_assigment all_weight.append( kill.main_quantity * kill.province_request.poultry_request.Index_weight) all_weight1.append( kill.main_quantity * kill.province_request.poultry_request.Index_weight) killer = 'کشتارکن' if kill.killhouse_user.killer == True else 'کشتارگاه' request_weight.append(Index_weight_sale_in_province_false) all_kill_province_request_poultry_request_Index_weight.append( kill.province_request.poultry_request.Index_weight) all_weight_of_car_vet_state_accepted += weight_of_car_vet_state_accepted all_weight_of_car += weight_of_car all_weight_of_car1 += weight_of_car list1 = [ killer, kill.killhouse_user.name, kill.killhouse_user.kill_house_operator.user.mobile, kill.main_quantity, kill.province_request.poultry_request.Index_weight, left_over, len(kill_house_requests) if kill_house_requests else 0, quantity_of_car, weight_of_car, all_quantity_qarantineh if all_quantity_qarantineh else '-', all_quantity_vet_kill if all_quantity_vet_kill else '-', all_weight_vet_kill if all_weight_vet_kill else '-', all_real_quantity_assigment if all_real_quantity_assigment > 0 else '-', all_net_weight if all_net_weight > 0 else '-', len_werhouse_enter, werhouse_enter_quantity, int(werhouse_enter_weight), f'%{all_weight_loss}', all_accepted_real_quantity if all_accepted_real_quantity else '-', all_accepted_real_weight if all_accepted_real_weight else '-', ] for item in range(len(list1)): cell = worksheet.cell(row=row_list2 + 1, column=item + 11, value=list1[item]) value = list1[item] # Check if the value is a number before formatting if isinstance(value, (int, float)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center', vertical='center') cell.border = openpyxl.styles.Border( left=openpyxl.styles.Side(style='thin'), right=openpyxl.styles.Side(style='thin'), top=openpyxl.styles.Side(style='thin'), bottom=openpyxl.styles.Side(style='thin') ) # worksheet.column_dimensions[get_column_letter(item + 11)].width = 17.01 # worksheet.column_dimensions[get_column_letter(item + 11)].height = 20.01 if m % 2 == 0: cell.fill = PatternFill(start_color="DAE1F6", fill_type="solid") row_list2 += 1 sum_net_weight += all_net_weight sum_net_weight1 += all_net_weight sum_real_quantity_assigment += all_real_quantity_assigment sum_real_quantity_assigment1 += all_real_quantity_assigment else: if poultry_request.out == True: left_over = (quantity - killed) list1 = [ 'خارج از استان', poultry_request.buyer_fullname, poultry_request.buyer_mobile, poultry_request.quantity, int(poultry_request.Index_weight), left_over, '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', poultry_request.quantity, int(poultry_request.quantity * poultry_request.Index_weight), ] for item in range(len(list1)): cell = worksheet.cell(row=row_list2 + 1, column=item + 11, value=list1[item]) value = list1[item] # Check if the value is a number before formatting if isinstance(value, (int, float)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center', vertical='center') cell.border = openpyxl.styles.Border( left=openpyxl.styles.Side(style='thin'), right=openpyxl.styles.Side(style='thin'), top=openpyxl.styles.Side(style='thin'), bottom=openpyxl.styles.Side(style='thin') ) # worksheet.column_dimensions[get_column_letter(item + 11)].width = 17.01 # worksheet.column_dimensions[get_column_letter(item + 11)].height = 20.01 if m % 2 == 0: cell.fill = PatternFill(start_color="DAE1F6", fill_type="solid") sum_accepted_real_weight += int( poultry_request.quantity * poultry_request.Index_weight) sum_accepted_real_weight1 += int( poultry_request.quantity * poultry_request.Index_weight) sum_accepted_real_quantity += poultry_request.quantity sum_accepted_real_quantity1 += poultry_request.quantity else: list1 = [ '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', ] for item in range(len(list1)): cell = worksheet.cell(row=row_list2 + 1, column=item + 11, value=list1[item]) value = list1[item] # Check if the value is a number before formatting if isinstance(value, (int, float)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center', vertical='center') cell.border = openpyxl.styles.Border( left=openpyxl.styles.Side(style='thin'), right=openpyxl.styles.Side(style='thin'), top=openpyxl.styles.Side(style='thin'), bottom=openpyxl.styles.Side(style='thin') ) # worksheet.column_dimensions[get_column_letter(item + 11)].width = 17.01 # worksheet.column_dimensions[get_column_letter(item + 11)].height = 20.01 if m % 2 == 0: cell.fill = PatternFill(start_color="DAE1F6", fill_type="solid") row_list2 += 1 try: dd = round(sum(all_kill_province_request_poultry_request_Index_weight) / len( all_kill_province_request_poultry_request_Index_weight)) except: dd = 0 list2 = [ 'مجموع==>', '', '', '', f'', '', '', int(all_province_quantity), '', int(sum(all_request_weight)), '', '', '', int(all_main_quantity), int(dd), f'', '', int(all_quantity_of_car), int(all_weight_of_car), int(sum_quantity_qarantineh), int(sum_all_quantity_vet_kill), int(sum_all_wight_vet_kill), int(sum_real_quantity_assigment), int(sum_net_weight), all_len_werhouse_enter, all_werhouse_enter_quantity, all_werhouse_enter_weight, f'%{round(int(all_all_weight_loss) / all_len_werhouse_enter, 2) if all_len_werhouse_enter > 0 else 0}', int(sum_accepted_real_quantity), int(sum_accepted_real_weight), ] for item in range(len(list2)): cell = worksheet.cell(row=row_list2 + 2, column=item + 1, value=list2[item]) value = list2[item] # Check if the value is a number before formatting if isinstance(value, (int, float)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center') cell.font = Font(bold=True) cell.fill = PatternFill(start_color="00B050", fill_type="solid") row_list2 += 8 row_city += 6 try: avrage_age1 = round((sum(average_age_list) / len(average_age_list))) except: avrage_age1 = 0 all_quantity_of_request = poultry_requests.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity') or 0 row_header = 1 all_all_quantity_of_request1 += all_quantity_of_request value_header_list = [ hatching.period, hatching.poultry.unit_name, hatching.poultry.user.fullname, str(hatch_date), quantity, ninety_percent, all_loses, hatching.left_over, avrage_age1, len(poultry_requests), all_quantity_of_request, hatching.out_province_killed_quantity, hatching.out_province_killed_weight, all_main_quantity, sum(all_weight), round((hatching.killed_quantity * 100) / ninety_percent), all_quantity_of_car, all_weight_of_car, sum_all_quantity_vet_kill, sum_all_wight_vet_kill, sum_real_quantity_assigment, int(sum_net_weight), len(werhouse_enter1), werhouse_enter_quantity1, werhouse_enter_weight1, f'%{round(all_weight_loss2 / len(werhouse_enter1), 2) if len(werhouse_enter1) > 0 else 0}', sum_accepted_real_quantity, sum_accepted_real_weight, f'%{int((sum_accepted_real_quantity * 100) / quantity if quantity > 0 else 0)}', ] for item in range(len(value_header_list)): cell = worksheet.cell(row=l, column=item + 3, value=value_header_list[item]) value = value_header_list[item] # Check if the value is a number before formatting if isinstance(value, (int, float)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center', vertical='center') cell.border = openpyxl.styles.Border( left=openpyxl.styles.Side(style='thin'), right=openpyxl.styles.Side(style='thin'), top=openpyxl.styles.Side(style='thin'), bottom=openpyxl.styles.Side(style='thin') ) # worksheet.column_dimensions[get_column_letter(item + 11)].width = 17.01 # worksheet.column_dimensions[get_column_letter(item + 11)].height = 20.01 l += 1 row_header += 1 else: all_left_over = hatchings.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity') or 0 all_quantity = hatchings.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity') or 0 out_province_killed_quantity = hatchings.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity') or 0 out_province_killed_weight = hatchings.aggregate(total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity') or 0 ninety_percent = (all_quantity * 90) / 100 value_header_list = [ 'مجموع==>', '', '', '', all_quantity, ninety_percent, all_loses1, all_left_over, '', '', all_all_quantity_of_request1, out_province_killed_quantity, out_province_killed_weight, all_main_quantity1, sum(all_weight1), '', all_quantity_of_car1, all_weight_of_car1, sum_all_quantity_vet_kill1, sum_all_wight_vet_kill1, sum_real_quantity_assigment1, int(sum_net_weight1), len_werhouse_enter1, all_werhouse_enter_quantity1, all_werhouse_enter_weight1, '', sum_accepted_real_quantity1, sum_accepted_real_weight1, '', ] nn = len(hatchings) + 4 for item in range(len(value_header_list)): cell = worksheet.cell(row=nn, column=item + 3, value=value_header_list[item]) value = value_header_list[item] # Check if the value is a number before formatting if isinstance(value, (int, float)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center') cell.font = Font(bold=True) cell.fill = PatternFill(start_color="00B050", fill_type="solid") cell.border = openpyxl.styles.Border( left=openpyxl.styles.Side(style='thin'), right=openpyxl.styles.Side(style='thin'), top=openpyxl.styles.Side(style='thin'), bottom=openpyxl.styles.Side(style='thin') ) else: filtered_kill_request = KillHouseRequest.objects.filter( trash=False, province_request__poultry_request__poultry=poultreis ).order_by( '-create_date').select_related( 'killhouse_user', 'province_request__poultry_request__poultry__user', 'province_request__poultry_request', 'province_request__poultry_request__poultry', 'add_car__driver', 'killhouse_user__system_address__city', 'kill_request__slaughter_house').only('ware_house_confirmation', 'document_status', 'ware_house_accepted_real_quantity', 'ware_house_accepted_real_weight', 'weight_loss', 'message', 'province_request__poultry_request__freezing', 'province_request__poultry_request', 'traffic_code', 'assignment_state_archive', 'province_request__poultry_request__hatching__chicken_age', 'killhouse_user__kill_house_operator__user__province__name', 'killhouse_user', 'killhouse_user__name', 'province_request__poultry_request__amount', 'killhouse_user__kill_house_operator__user__mobile', 'killhouse_user__system_address__city__name', 'killhouse_user__killer', 'kill_request__slaughter_house', 'kill_request__slaughter_house__name', 'province_request__poultry_request__poultry', 'province_request__poultry_request__order_code', 'province_request__poultry_request__chicken_breed', 'province_request__poultry_request__poultry__user__city__name', 'province_request__poultry_request__poultry__unit_name', 'province_request__poultry_request__poultry__user__mobile', 'province_request__poultry_request__send_date', 'province_kill_request__province_request__poultry_request__Index_weight', 'add_car__driver__driver_name', 'add_car__driver__driver_mobile', 'add_car__driver__type_car', 'add_car__driver__health_code', 'key', 'clearance_code', 'quantity', 'bar_code', 'accepted_real_weight', 'accepted_real_quantity', 'vet_state', 'vet_accepted_real_quantity', 'vet_accepted_real_weight', 'quarantine_quantity', 'province_kill_request__province_request__poultry_request__free_sale_in_province', 'province_kill_request__province_request__poultry_request__union', 'province_kill_request__province_request__poultry_request__direct_buying', 'quarantine_code_state', 'province_request__poultry_request__send_date', 'province_request__poultry_request__hatching__date', 'killer__kill_house_operator__user__mobile', 'killer__name', 'killhouse_user__type', 'killer').values('document_status', 'ware_house_confirmation', 'ware_house_accepted_real_quantity', 'ware_house_accepted_real_weight', 'weight_loss', 'message', 'province_request__poultry_request__freezing', 'province_request__poultry_request', 'killhouse_user', 'killhouse_user__name', 'killhouse_user__kill_house_operator__user__mobile', 'killhouse_user__system_address__city__name', 'killhouse_user__killer', 'kill_request__slaughter_house', 'kill_request__slaughter_house__name', 'province_request__poultry_request__poultry', 'province_request__poultry_request__order_code', 'province_request__poultry_request__chicken_breed', 'province_request__poultry_request__poultry__user__city__name', 'province_request__poultry_request__poultry__unit_name', 'province_request__poultry_request__poultry__user__mobile', 'province_request__poultry_request__send_date', 'add_car__driver__driver_name', 'add_car__driver__driver_mobile', 'add_car__driver__type_car', 'add_car__driver__health_code', 'key', 'province_kill_request__province_request__poultry_request__Index_weight', 'clearance_code', 'quantity', 'bar_code', 'accepted_real_weight', 'accepted_real_quantity', 'vet_state', 'vet_accepted_real_quantity', 'vet_accepted_real_weight', 'quarantine_quantity', 'killhouse_user__kill_house_operator__user__province__name', 'province_request__poultry_request__hatching__chicken_age', 'province_request__poultry_request__amount', 'assignment_state_archive', 'traffic_code', 'province_kill_request__province_request__poultry_request__free_sale_in_province', 'province_kill_request__province_request__poultry_request__union', 'province_kill_request__province_request__poultry_request__direct_buying', 'quarantine_code_state', 'province_request__poultry_request__send_date', 'province_request__poultry_request__hatching__date', 'killer__kill_house_operator__user__mobile', 'killer__name', 'killhouse_user__type', 'killer', ) filtered_kill_reqs = filtered_kill_request poultry_request = (PoultryRequest.objects.filter(trash=False, pk__in=filtered_kill_reqs.values( 'province_request__poultry_request')) .only('quantity', 'Index_weight')).annotate( total_quantity=Sum('quantity'), total_weight=Sum(F('quantity') * F('Index_weight')) ) total_requests_quantity = poultry_request.aggregate(total=Sum('total_quantity'))['total'] or 0 total_requests_weight = poultry_request.aggregate(total=Sum('total_weight'))['total'] or 0 excel_options = [ 'ردیف', 'کد بار', ' تاریخ کشتار', 'کشتار', 'کدسفارش مرغدار', 'مرغدار', 'تلفن مرغدار', ' شهر مرغدار', 'فروش', 'سن مرغ', 'قیمت پیشنهادی مرغدار(ریال)', ' نژاد', 'دامپزشک فارم', 'تلفن دامپزشک فارم', ' ماهیت خریدار', 'خریدار', ' تلفن خریدار', ' آدرس ', 'کشتارکن اختصاصی', 'تلفن کشتارکن اختصاصی', ' محل کشتار ', 'نوع تخصیص', 'دامپزشک کشتارگاه ', ' تلفن دامپزشک کشتارگاه ', 'راننده', 'موبایل راننده', 'نوع خودرو', 'کد بهداشتی حمل و نقل', 'تعداد قطعه بار ', 'وزن بار', 'کد رهگیری سامانه قرنطینه', 'تعداد قطعه وارد شده در قرنطینه', 'وضعیت تخلیه', 'تاریخ تخلیه (کشتارگاه)', 'تعداد تخلیه شده دامپزشک', 'وزن بار تخلیه شده(کیلوگرم)دامپزشک', 'تعداد نهایی در کشتارگاه', 'وزن نهایی در کشتار گاه', 'ملاک قطعه کشتار شده', 'ملاک وزن کشتار شده', 'وضعیت', 'وضعیت سند', 'وضعیت ورود انبار', 'تعداد لاشه', 'وزن لاشه', 'درصد افت', ] worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) blue_fill = PatternFill(start_color="1E487B", fill_type="solid") cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') red_font = Font(color="C00000", bold=True) for col_num, option in enumerate(excel_options, 1): col_letter = get_column_letter(col_num) cell = worksheet.cell(row=6, column=col_num, value=option) cell.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) cell.fill = blue_fill cell.font = Font(size=10, bold=True, color='D9FFFFFF') if len(option) > worksheet.column_dimensions[col_letter].width: worksheet.column_dimensions[col_letter].width = len(option) + 3 # تنظیم ارتفاع سطر # ارتفاع سطر را برابر با ارتفاع آخرین سطر قرار می‌دهیم worksheet.row_dimensions[6].height = 19 worksheet.freeze_panes = worksheet['A7'] max_col = worksheet.max_column range_str = f'A6:{get_column_letter(max_col)}{worksheet.max_row}' worksheet.auto_filter.ref = range_str header_list = [ 'تعداد بارهای ایجاد شده', 'تعداد بارهای دارای کشتارکن اختصاصی', 'مجموع تعداد قطعه بارها', 'مجموع وزن بارها', 'تعداد بارهای دارای کد قرنطینه', 'تعداد بار احراز شده از قرنطینه', 'مجموع تعداد قطعه احراز شده از قرنطینه', 'تعداد تخلیه شده دامپزشک', 'مجموع تعداد تخلیه شده دامپزشک', 'مجموع وزن تخلیه شده دامپزشک', 'تعداد بارهای تکمیل شده', 'مجموع تعداد نهایی در کشتارگاه', 'مجموع وزن نهایی در کشتار گاه', 'ملاک قطعه کشتار شده', 'ملاک وزن کشتار شده', 'تعداد بار های وارد شده در انبار', 'مجموع تعداد لاشه', 'مجموع وزن لاشه', 'میانگین درصد افت لاشه', ] for col_num, option in enumerate(header_list, 9): cell = worksheet.cell(row=2, column=col_num, value=option) cell.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) cell.fill = PatternFill(start_color="0070C0", fill_type="solid") cell.font = Font(size=9, bold=True, color='D9FFFFFF') worksheet.row_dimensions[2].height = 20.8 header_list2 = [ 'تعداد درخواست مرغداران', ' مجموع تعداد قطعه درخواست مرغداران', ' مجموع وزن درخواست مرغداران', ] for col_num, option in enumerate(header_list2, 6): cell = worksheet.cell(row=2, column=col_num, value=option) cell.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True) cell.fill = PatternFill(start_color="00B050", fill_type="solid") cell.font = Font(size=9, bold=True, color='D9FFFFFF') worksheet.row_dimensions[2].height = 20.8 worksheet['B1'] = f'گزارش بارهای ایجاد شده در فرآیند کشتار مرغ گوشتی' if filtered_kill_reqs.exists(): name = filtered_kill_reqs.first()['killhouse_user__kill_house_operator__user__province__name'] worksheet['B2'] = f'استان {name}' # worksheet['B3'] = f'از تاریخ:({from_date_1}) تا تاریخ:({to_date_1})' worksheet['B3'].alignment = Alignment(horizontal='center', vertical='center') worksheet['B1'].alignment = Alignment(horizontal='center', vertical='center') worksheet['B2'].alignment = Alignment(horizontal='center', vertical='center') merge_range1 = 'B1:D1' merge_range2 = 'B2:D2' merge_range = 'B3:D3' worksheet.merge_cells(merge_range1) worksheet.merge_cells(merge_range) worksheet.merge_cells(merge_range2) worksheet['B1'].font = red_font worksheet['B3'].font = Font(size=11) worksheet['B2'].font = Font(size=10, bold=True, color="C00000") l = 5 m = 1 all_quantity = 0 all_real_quantity = 0 all_net_weighte = 0 all_weighte = 0 all_vet_state_accepted = len( filtered_kill_reqs.filter(vet_state='accepted')) bar_complete = filtered_kill_reqs.filter(assignment_state_archive='True') accepted_real_quantity = filtered_kill_reqs.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 accepted_real_wight = filtered_kill_reqs.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 all_kill_request_quantity = filtered_kill_reqs.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity') or 0 all_vet_accepted_real_quantity = filtered_kill_reqs.aggregate( total_quantity=Sum('vet_accepted_real_quantity')).get( 'total_quantity') or 0 all_vet_accepted_real_weight = filtered_kill_reqs.aggregate( total_quantity=Sum('vet_accepted_real_weight')).get( 'total_quantity') or 0 killer_exclusive = 0 all_assignment_state_archive_state = 0 len_weight_loss = 0 all_state_ware_house_confirmation = 0 if filtered_kill_reqs: for kill in filtered_kill_reqs: if (kill['assignment_state_archive'] == 'True' and kill['vet_state'] == 'pending') or kill[ 'vet_state'] == 'accepted': state = 'تخلیه شده' else: state = 'درانتظار تخلیه' l += 1 date_of_inner_bar = '-' vet_farm = VetFarm.objects.filter( poultry=kill.get('province_request__poultry_request__poultry'), trash=False).select_related('vet__user').only('vet__user__fullname', 'vet__user__mobile').values( 'vet__user__fullname', 'vet__user__mobile').first() vet_farm_name = vet_farm.get('vet__user__fullname') if vet_farm else '-' vet_farm_mobile = vet_farm.get('vet__user__mobile') if vet_farm else '-' kil_house_vet = KillHouseVet.objects.filter(kill_house=kill.get('killhouse_user'), trash=False).select_related( 'vet__user').only('vet__user__fullname', 'vet__user__mobile').values('vet__user__fullname', 'vet__user__mobile').first() vet_checks = VetCheckRequest.objects.filter(trash=False, kill_house_request__key=kill.get('key')).only( 'create_date').values( 'create_date').first() kill_house_vet_name = kil_house_vet.get('vet__user__fullname') if kil_house_vet else '-' kill_house_vet_mobile = kil_house_vet.get('vet__user__mobile') if kil_house_vet else '-' code = kill.get('clearance_code') if kill.get('clearance_code') else '-' if kill.get('quantity'): quantity = kill.get('quantity') else: quantity = '-' send_date = kill.get('province_request__poultry_request__send_date') date_of_poultry_request = jdatetime.date.fromgregorian( day=send_date.day, month=send_date.month, year=send_date.year ) killers = 'کشتارکن' name_killer_exclusive = '-' mobile_killer_exclusive = '-' if kill.get('killhouse_user__killer') == False: killers = 'کشتارگاه' if kill.get('killer') != None: name_killer_exclusive = kill['killer__name'] mobile_killer_exclusive = str(kill['killer__kill_house_operator__user__mobile']) killer_exclusive += 1 assignment = KillHouseAssignmentInformation.objects.filter( kill_house_request__key=kill.get('key'), trash=False).only('create_date', 'net_weight', 'real_quantity').values( 'net_weight', 'real_quantity', 'create_date').first() if assignment: net_weighte = int(assignment.get('net_weight')) real_quantity = assignment.get('real_quantity') vet_check_date = assignment.get('create_date') date_of_inner_bar = convert_to_shamsi( datetime=vet_check_date ) else: net_weighte = '-' real_quantity = '-' if vet_checks: vet_check_date = vet_checks.get('create_date') date_of_inner_bar = convert_to_shamsi( datetime=vet_check_date ) if kill.get('kill_request__slaughter_house') is not None: kill_place = kill.get('kill_request__slaughter_house__name') else: kill_place = '-' vet_quantity = kill['vet_accepted_real_quantity'] if kill['vet_accepted_real_quantity'] else '-' vet_wight = int(kill['vet_accepted_real_weight']) if kill['vet_accepted_real_weight'] else '-' weight = int(kill.get('quantity') * kill.get( 'province_kill_request__province_request__poultry_request__Index_weight')) quarantine_quantity = '-' if kill['quarantine_quantity'] != None: quarantine_quantity = kill['quarantine_quantity'] else: if kill['quarantine_code_state'] == 'contradiction': quarantine_quantity = 'مغایرت کد رهگیری' elif kill['quarantine_code_state'] == 'noclearance': quarantine_quantity = 'فاقد کد رهگیری' elif kill['quarantine_code_state'] == 'notconfirmed': quarantine_quantity = 'عدم تایید راهداری' sale_type = 'دولتی' if kill[ 'province_kill_request__province_request__poultry_request__free_sale_in_province'] == False else 'آزاد' if kill['province_kill_request__province_request__poultry_request__union'] == True: type = 'فروش آزاد' elif kill['province_kill_request__province_request__poultry_request__direct_buying'] == True: type = 'خرید مستقیم' else: type = 'اتحادیه' age = (kill.get('province_request__poultry_request__send_date').date() - kill.get('province_request__poultry_request__hatching__date').date()).days + 1 freez_state = 'منجمد' if kill['province_request__poultry_request__freezing'] == True else 'عادی' if kill['message'] is not None: state_delete = 'بار حذف شده' else: state_delete = 'فعال' all_quantity += quantity all_weighte += weight all_real_quantity += real_quantity if real_quantity != '-' else 0 all_net_weighte += net_weighte if net_weighte != '-' else 0 ware_house_accepted_real_quantity = kill['ware_house_accepted_real_quantity'] ware_house_accepted_real_weight = kill['ware_house_accepted_real_weight'] weight_loss = round(kill['weight_loss'], 2) if kill['weight_loss'] > 0 else 0 if weight_loss > 0: len_weight_loss += 1 if kill['ware_house_confirmation'] == True: state_ware_house_confirmation = 'ورود به انبار' all_state_ware_house_confirmation += 1 else: state_ware_house_confirmation = '-' list1 = [ m, str(kill.get('bar_code')), str(date_of_poultry_request), freez_state, str(kill.get('province_request__poultry_request__order_code')), kill.get('province_request__poultry_request__poultry__unit_name'), str(kill.get('province_request__poultry_request__poultry__user__mobile')), kill.get('province_request__poultry_request__poultry__user__city__name'), sale_type, age, kill.get('province_request__poultry_request__amount') if kill.get( 'province_request__poultry_request__amount') else '-', kill.get('province_request__poultry_request__chicken_breed'), vet_farm_name, vet_farm_mobile, killers, kill.get('killhouse_user__name'), kill.get('killhouse_user__kill_house_operator__user__mobile'), kill.get('killhouse_user__system_address__city__name'), name_killer_exclusive, mobile_killer_exclusive, kill_place, type, kill_house_vet_name, kill_house_vet_mobile, kill.get('add_car__driver__driver_name'), kill.get('add_car__driver__driver_mobile'), kill.get('add_car__driver__type_car'), str(kill.get('traffic_code')), quantity, weight, code, quarantine_quantity, state, str(date_of_inner_bar), vet_quantity, vet_wight, real_quantity, net_weighte, kill['accepted_real_quantity'], int(kill['accepted_real_weight']), state_delete, kill['document_status'] if kill['document_status'] else '-', state_ware_house_confirmation, ware_house_accepted_real_quantity, int(ware_house_accepted_real_weight), f'{weight_loss}%', ] m += 1 for item in range(len(list1)): cell = worksheet.cell(row=l + 1, column=item + 1, value=list1[item]) value = list1[item] if isinstance(value, (int)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values if list1[40] == 'بار حذف شده': cell.fill = PatternFill(start_color="FCDFDC", fill_type="solid") worksheet.row_dimensions[l + 1].height = 20 cell.alignment = Alignment(horizontal='center', vertical='center') has_code = len(filtered_kill_reqs.filter(clearance_code__isnull=False)) all_quarantine_quantity = filtered_kill_reqs.aggregate(total_quantity=Sum('quarantine_quantity')).get( 'total_quantity', 0) has_qarantine = len(filtered_kill_reqs.filter(quarantine_quantity__isnull=False)) all_ware_house_accepted_real_quantity = filtered_kill_reqs.aggregate( total_quantity=Sum('ware_house_accepted_real_quantity')).get( 'total_quantity') or 0 all_ware_house_accepted_real_weight = filtered_kill_reqs.aggregate( total_quantity=Sum('ware_house_accepted_real_weight')).get( 'total_quantity', 0) or 0 all_weight_loss = filtered_kill_reqs.aggregate( total_quantity=Sum('weight_loss')).get( 'total_quantity', 0) or 0 all_weight_loss = round(int(all_weight_loss) / len_weight_loss, 2) if len_weight_loss > 0 else 0 value_header_list = [ len(filtered_kill_reqs), killer_exclusive, all_quantity, int(all_weighte), has_code, has_qarantine, all_quarantine_quantity if all_quarantine_quantity != None else 0, all_vet_state_accepted, all_vet_accepted_real_quantity, int(all_vet_accepted_real_weight), len(bar_complete), all_real_quantity, int(all_net_weighte), accepted_real_quantity if accepted_real_quantity != None else 0, int(accepted_real_wight) if accepted_real_wight != None else 0, all_state_ware_house_confirmation, all_ware_house_accepted_real_quantity, int(all_ware_house_accepted_real_weight), f'%{all_weight_loss}' ] for item in range(len(value_header_list)): cell = worksheet.cell(row=3, column=item + 9, value=value_header_list[item]) value = value_header_list[item] # Check if the value is a number before formatting if isinstance(value, (int)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center') cell.font = Font(size=10, bold=True) value_header_list2 = [ len(poultry_request), total_requests_quantity, int(total_requests_weight) ] for item in range(len(value_header_list2)): cell = worksheet.cell(row=3, column=item + 6, value=value_header_list2[item]) value = value_header_list2[item] # Check if the value is a number before formatting if isinstance(value, (int)): if value != 0: cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center') cell.font = Font(size=10, bold=True) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_kill_request_quantity, int(all_weighte), '', all_quarantine_quantity if all_quarantine_quantity != None else 0, '', '', all_vet_accepted_real_quantity, int(all_vet_accepted_real_weight), all_real_quantity, int(all_net_weighte), accepted_real_quantity if accepted_real_quantity != None else 0, int(accepted_real_wight) if accepted_real_wight != None else 0, '', '', '', all_ware_house_accepted_real_quantity, int(all_ware_house_accepted_real_weight), f'%{all_weight_loss}' ] for item in range(len(list2)): cell = worksheet.cell(row=l + 3, column=item + 1, value=list2[item]) value = list2[item] if isinstance(value, (int)): cell.number_format = '#,###' # Apply general number format else: cell.value = value # Keep as text for other values cell.alignment = Alignment(horizontal='center') cell.font = Font(size=10, bold=True) cell.font = Font(bold=True) cell.fill = PatternFill(start_color="00B050", fill_type="solid") name = poultreis.unit_name workbook.save(output) output.seek(0) response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ 'Content-Disposition'] = f'attachment; filename="پایش کلی اطلاعاتی مرغدار {name}.xlsx"'.encode( 'utf-8') response.write(output.getvalue()) return response def poultry_hatching_prediction_chart_excel(request): consumption_limit_type = request.GET.get('consumption_limit_type') consumption_limit = request.GET.get('consumption_limit') killing_age_external_type = request.GET.get('killing_age_external_type') killing_age_internal_type = request.GET.get('killing_age_internal_type') killing_age = request.GET.get('killing_age') time_frame_type = request.GET.get('time_frame_type') date1 = request.GET.get('date1') date2 = request.GET.get('date2') date_list = [] final_list = [] poultries = Poultry.objects.filter(killing_ave_age__gt=1, trash=False) if killing_age_external_type == 'poultry_ave_killing_age': if killing_age_internal_type == 'poultry': if time_frame_type == 'all': hatchings = PoultryHatching.objects.filter(trash=False, state='pending', archive=False, temporary_trash=False, temporary_deleted=False, predicate_date__isnull=False) date_list = hatchings.values_list('predicate_date', flat=True).distinct() for date in date_list: n_hatchings = hatchings.filter(predicate_date=date) poultries = Poultry.objects.filter(pk__in=n_hatchings.values_list('poultry', flat=True)) ages = poultries.values_list('killing_ave_age', flat=True).distinct() index_weight = poultries.aggregate(total=Sum('real_killing_ave_weight'))[ 'total'] or 0 quantity = n_hatchings.aggregate(total=Sum('left_over'))[ 'total'] or 0 result = { "date": date, "quantity": quantity, "poultry": len(poultries), "age": ages, "hatchings": n_hatchings.values_list('id', flat=True), "weight": int(quantity * index_weight) if consumption_limit_type in ( 'quantity', 'live_weight') else int((quantity * index_weight) * 0.75), } final_list.append(result) else: date1 = datetime.datetime.strptime(str(request.GET['date1']), '%Y-%m-%d').date() date2 = datetime.datetime.strptime(str(request.GET['date2']), '%Y-%m-%d').date() hatchings = PoultryHatching.objects.filter(trash=False, state='pending', archive=False, temporary_trash=False, temporary_deleted=False, predicate_date__date__gte=date1, predicate_date__date__lte=date2) # poultries = poultries.filter(date__gte=date1,date__lte=date2).order_by('date') date_list = poultries.values_list('date', flat=True).distinct() for date in date_list: n_hatchings = hatchings.filter(predicate_date__date=date) poultries = Poultry.objects.filter(pk__in=n_hatchings.values_list('poultry', flat=True)) ages = poultries.values_list('killing_ave_age', flat=True).distinct() index_weight = poultries.aggregate(total=Sum('real_killing_ave_weight'))[ 'total'] or 0 quantity = n_hatchings.aggregate(total=Sum('left_over'))[ 'total'] or 0 result = { "date": date, "quantity": quantity, "poultry": len(poultries.filter(date=date)), "age": ages, "hatchings": n_hatchings.values_list('id', flat=True), "weight": int(quantity * index_weight) if consumption_limit_type in ( 'quantity', 'live_weight') else int((quantity * index_weight) * 0.75), } final_list.append(result) else: sum_of_ages = poultries.aggregate(total=Sum('killing_ave_age'))[ 'total'] or 0 ave_killing_age = int(sum_of_ages / len(poultries)) if time_frame_type == 'all': final_list = poultry_prediction_helper(ave_killing_age, consumption_limit_type) else: date1 = datetime.datetime.strptime(str(request.GET['date1']), '%Y-%m-%d').date() date2 = datetime.datetime.strptime(str(request.GET['date2']), '%Y-%m-%d').date() final_list = poultry_prediction_helper(ave_killing_age, consumption_limit_type, date1, date2) else: if killing_age_internal_type == 'poultry': if time_frame_type == 'all': final_list = poultry_prediction_helper(int(killing_age), consumption_limit_type) # poultries = poultries.filter(killing_ave_age=int(killing_age)) # date_list = poultries.values_list('date', flat=True).distinct() # for date in date_list: # index_weight = poultries.filter(date=date).aggregate(total=Sum('killing_ave_weight'))[ # 'total'] or 0 # quantity = poultries.filter(date=date).aggregate(total=Sum('active_left_over'))[ # 'total'] or 0 # # result = { # "date": date, # "quantity": quantity, # "poultry": len(poultries.filter(date=date)), # "age": int(killing_age), # "wight": quantity * index_weight, # } # final_list.append(result) else: date1 = datetime.datetime.strptime(str(request.GET['date1']), '%Y-%m-%d').date() date2 = datetime.datetime.strptime(str(request.GET['date2']), '%Y-%m-%d').date() final_list = poultry_prediction_helper(int(killing_age), consumption_limit_type, date1, date2) # poultries = poultries.filter(killing_ave_age=int(killing_age),date__gte=date1,date__lte=date2) # date_list = poultries.values_list('date', flat=True) # for date in date_list: # index_weight = poultries.filter(date=date).aggregate(total=Sum('killing_ave_weight'))[ # 'total'] or 0 # quantity = poultries.filter(date=date).aggregate(total=Sum('active_left_over'))[ # 'total'] or 0 # # result = { # "date": date, # "quantity": quantity, # "poultry": len(poultries.filter(date=date)), # "age": int(killing_age), # "wight": quantity * index_weight, # } # final_list.append(result) else: ave_killing_age = int(killing_age) if time_frame_type == 'all': final_list = poultry_prediction_helper(ave_killing_age, consumption_limit_type) else: date1 = datetime.datetime.strptime(str(request.GET['date1']), '%Y-%m-%d').date() date2 = datetime.datetime.strptime(str(request.GET['date2']), '%Y-%m-%d').date() final_list = poultry_prediction_helper(ave_killing_age, consumption_limit_type, date1, date2) excel_options = [ 'ردیف', 'تاریخ', 'تعداد مرغداران', 'حجم(قطعه)', 'وزن(کیلوگرم)', 'سن', ] output = BytesIO() workbook = Workbook() worksheet = workbook.active worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') header_list = [ 'تعداد مرغداران', 'مجموع حجم', 'مجموع وزن', ] create_header(worksheet, header_list, 4, 2, height=20) if "date1" in request.GET: date1 = datetime.datetime.strptime(str(request.GET['date1']), '%Y-%m-%d').date() date2 = datetime.datetime.strptime(str(request.GET['date2']), '%Y-%m-%d').date() excel_description(worksheet, 'B1', f'پیشبینی موجودی از تاریخ {shamsi_date(date1)} تا تاریخ {shamsi_date(date2)}', color='red', row2='C3') else: excel_description(worksheet, 'B1', f'پیشبینی موجودی', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) l = 5 m = 1 if final_list: for data in final_list: if type(data['age']) is str or type(data['age']) is int: age = data['age'] else: if len(data['age']) > 1: age = '-'.join(map(str, data['age'])) elif len(data['age']) == 1: age = str(data['age'][0]) else: age = '' if type(data['date']) is str: try: str_date = datetime.datetime.strptime(str(data['date']), "%Y-%m-%dT%H:%M:%S").date() except: str_date = datetime.datetime.strptime(str(data['date']), '%Y-%m-%d').date() date = convert_to_shamsi(date=str_date) elif data['date'] is None: date = '-' else: date = convert_to_shamsi(date=data['date']) list1 = [ m, date, data['poultry'], data['quantity'], data['weight'], age, ] m += 1 l += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', width=15) poultry = sum(item['poultry'] for item in final_list) quantity = sum(item['quantity'] for item in final_list) weight = sum(item['weight'] for item in final_list) value_header_list = [ poultry, quantity, weight, ] create_value(worksheet, value_header_list, 3, 4) list2 = [ 'مجموع==>', '', poultry, quantity, weight, '', ] create_value(worksheet, list2, l + 3, 1, color='green') if consumption_limit_type == 'quantity': data_columns = 4 y_axis_title = 'حجم موجودی (قطعه)' else: data_columns = 5 y_axis_title = "وزن موجودی (کیلوگرم)" add_chart( worksheet=worksheet, chart_type='line', data_columns=data_columns, category_column=2, start_row=7, end_row=l + 1, chart_position="H7", chart_title="داده های تجمیعی", x_axis_title="تاریخ", y_axis_title=y_axis_title ) workbook.save(output) output.seek(0) response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ 'Content-Disposition'] = f'attachment; filename="پیشبینی موجودی.xlsx"'.encode( 'utf-8') response.write(output.getvalue()) return response def hatching_report_from_age_excel(request): input_age1 = int(request.GET['age1']) input_age2 = int(request.GET['age2']) filtered_poultry_hatch = PoultryHatching.objects.filter(chicken_age__gte=input_age1, chicken_age__lte=input_age2, archive=False, allow_hatching='pending', trash=False).select_related('poultry', 'poultry__user').order_by( 'create_date') user = SystemUserProfile.objects.get(key=request.GET['key']) if 'chain' in request.GET: filtered_poultry_hatch = filtered_poultry_hatch.filter(has_chain_company=True) if request.GET['role'] in ['CityJahad', 'CityPoultry']: filtered_poultry_hatch = filtered_poultry_hatch.filter(poultry__address__city=user.city) elif request.GET['role'] == 'CityOperator': city_operator = CityOperator.objects.get(user=user) filtered_poultry_hatch = filtered_poultry_hatch.filter(poultry__city_operator=city_operator.unit_name) else: filtered_poultry_hatch = filtered_poultry_hatch excel_options = [ 'ردیف', 'وضعیت فارم', 'زنجیره/شرکت', 'نام و خانوادگی', 'نام فارم', 'موبایل', 'شهرستان', 'کد یکتا', 'کد سیستمی واحد', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'نژاد', 'سن', 'تعداد جوجه ریزی', 'تلفات دوره', 'تعداد کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'تعداد کشتار خارج از استان(قطعه)', 'وزن کشتار خارج از استان(کیلوگرم)', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ ثبت گزارش', ] date1 = datetime.datetime.now().date() from_date = jdatetime.date.fromgregorian( year=date1.year, month=date1.month, day=date1.day ).strftime('%Y-%m-%d') reversed_date = reversed(from_date.split("-")) separate = "-" from_date_1 = separate.join(reversed_date) output = BytesIO() workbook = Workbook() worksheet = workbook.active worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) blue_fill = PatternFill(start_color="1E487B", fill_type="solid") cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') red_font = Font(color="C00000", bold=True) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(وجود فارم فعال دیگر)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' list1 = [ m, farm_state, chain_company, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.mobile, poultry_hatching.poultry.user.city.name, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.poultry.system_code, poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.losses, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_losses, all_killed_quantity, all_total_killed_weight, all_left_over, all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') workbook.save(output) output.seek(0) response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ 'Content-Disposition'] = f'attachment; filename="جوجه ریزی بر اساس بازه {input_age1} تا {input_age2} روز.xlsx"'.encode( 'utf-8') response.write(output.getvalue()) return response def poultry_hatching_prediction_excel(request): filterset_class = PoultryHatchingFilterSet filterset_fields = [ 'poultry__user__first_name', 'poultry__user__last_name', 'poultry__user__mobile', 'poultry__unit_name', 'poultry__address__city__name', 'chicken_age', 'poultry__breeding_unique_id', ] user = SystemUserProfile.objects.get(key=request.GET['key'], trash=False) if 'hatching_list' in request.GET: poultry_hatchings = PoultryHatching.objects.filter(id__in=(request.GET['hatching_list']).split(',')).order_by( 'date') else: if request.GET['role'] == 'CityOperator': city_operator = CityOperator.objects.get(user=user, trash=False) poultry_hatchings = PoultryHatching.objects.filter(archive=False, allow_hatching='pending', state='pending', poultry__city_operator=city_operator.unit_name, trash=False).order_by('date') elif request.GET['role'] in ['CityJahad', 'CityPoultry', 'CityVet']: poultry_hatchings = PoultryHatching.objects.filter(archive=False, allow_hatching='pending', state='pending', poultry__address__city=user.city, trash=False).order_by('date') else: poultry_hatchings = PoultryHatching.objects.filter(archive=False, allow_hatching='pending', state='pending', trash=False).order_by('date') if 'search' in request.GET: hatching_list = [] if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=poultry_hatchings ) ).filter(): ps = filterset_class(data=query, queryset=poultry_hatchings) hatching_list = ps.filter() poultry_hatchings = [] if len(hatching_list) == 0 else hatching_list excel_options = [ 'ردیف', 'نام واحد', 'مالک', 'موبایل', 'شهر', 'میانگین سن کشتار', 'میانگین وزن کشتار', 'دوره', 'تاریخ جوجه ریزی', 'حجم جوجه ریزی', 'سن', 'نژاد', 'حجم کشتار شده', 'وزن کشتار شده', 'تخصیص بدون بار', 'حجم تخصیص بدون بار', 'وزن تخصیص بدون بار', 'مانده سالن(حجم)', 'مانده سالن(وزن زنده)', 'مانده سالن(وزن لاشه)', ] date1 = datetime.datetime.now().date() from_date = jdatetime.date.fromgregorian( year=date1.year, month=date1.month, day=date1.day ).strftime('%Y-%m-%d') reversed_date = reversed(from_date.split("-")) separate = "-" from_date_1 = separate.join(reversed_date) output = BytesIO() workbook = Workbook() worksheet = workbook.active worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') header_list = [ '', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'جزئیات پایش اطلاعات تاریخ {from_date_1}', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) l = 5 m = 1 serializer = PoultryHatchingForPredictionSerializer(poultry_hatchings, many=True).data if serializer: for data in serializer: datetime_object = datetime.datetime.strptime(data['date'], "%Y-%m-%dT%H:%M:%S") list1 = [ m, data['poultry']['unit_name'], data['poultry']['fullname'], data['poultry']['mobile'], data['poultry']['city'], data['general_info']['ave_age'], data['general_info']['ave_weight'], data['period'], convert_to_shamsi(date=datetime_object), data['quantity'], data['general_info']['age'], data['chicken_breed'], data['general_info']['total_quantity'], data['general_info']['total_weight'], data['general_info']['province_kill_requests'], data['general_info']['province_kill_requests_quantity'], data['general_info']['province_kill_requests_weight'], data['left_over'], round(data['left_over'] * data['poultry']['killing_ave_weight']), round(data['left_over'] * data['poultry']['killing_ave_weight'] * 0.75), ] m += 1 l += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin') quantity = sum( item['quantity'] for item in serializer) total_quantity = sum( item['general_info'].get('total_quantity', 0) for item in serializer) total_weight = sum( item['general_info'].get('total_weight', 0) for item in serializer) province_kill_requests = sum( item['general_info'].get('province_kill_requests', 0) for item in serializer) province_kill_requests_quantity = sum( item['general_info'].get('province_kill_requests_quantity', 0) for item in serializer) province_kill_requests_weight = sum( item['general_info'].get('province_kill_requests_weight', 0) for item in serializer) killing_ave_weight = sum( item['poultry'].get('killing_ave_weight', 0) for item in serializer) killing_ave_weight = killing_ave_weight / len(serializer) if serializer else 0 left_over = sum( item['left_over'] for item in serializer) value_header_list = [ ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', quantity, '', '', total_quantity, total_weight, province_kill_requests, province_kill_requests_quantity, province_kill_requests_weight, left_over, round(left_over * killing_ave_weight), round(left_over * killing_ave_weight * 0.75), ] create_value(worksheet, list2, l + 3, 1, color='green') workbook.save(output) output.seek(0) response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ 'Content-Disposition'] = f'attachment; filename="جزئیات پایش اطلاعات تاریخ {from_date_1}.xlsx"'.encode( 'utf-8') response.write(output.getvalue()) return response def bar_difference_reques_excel(request): filterset_class = BarDifferenceRequestFilterSet filterset_fields = [ 'kill_house__kill_house_operator__user__fullname', 'kill_house__kill_house_operator__user__mobile', 'hatching__poultry__user__fullname', 'hatching__poultry__user__mobile' ] user = SystemUserProfile.objects.get(key=request.GET['key'], trash=False) if request.GET['role'] == 'KillHouse': kill_house = KillHouse.objects.filter(kill_house_operator__user=user, trash=False).select_related( 'system_address__province').first() if request.GET['state'] == 'pending': state = 'جدید' bar_requests = BarDifferenceRequest.objects.filter(kill_house=kill_house, trash=False, state='pending').order_by('id') else: state = 'بایگانی شده' bar_requests = BarDifferenceRequest.objects.filter(kill_house=kill_house, trash=False, state__in=('accepted', 'rejected')).order_by('id') else: if request.GET['state'] == 'pending': state = 'جدید' bar_requests = BarDifferenceRequest.objects.filter(trash=False, state='pending').order_by('id') else: bar_requests = BarDifferenceRequest.objects.filter(trash=False, state__in=('accepted', 'rejected')).order_by('id') state = 'بایگانی شده' if 'search' in request.GET: bar_requests_list = [] if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=bar_requests ) ).filter(): ps = filterset_class(data=query, queryset=bar_requests) bar_requests_list = ps.filter() bar_requests = [] if len(bar_requests_list) == 0 else bar_requests_list excel_options = [ 'ردیف', 'وضعیت', 'ثبت کننده', 'تاریخ ثبت', 'مرغدار', 'کشتارگاه', 'حجم', 'حجم کم شده از سالن مرغدار', 'وزن تقریبی کشتار(کیلوگرم)', 'حجم سفارشات دریافتی توسط کشتارگاه', 'اختلاف کشتار(حجم)', ] date1 = datetime.datetime.now().date() from_date = jdatetime.date.fromgregorian( year=date1.year, month=date1.month, day=date1.day ).strftime('%Y-%m-%d') reversed_date = reversed(from_date.split("-")) separate = "-" from_date_1 = separate.join(reversed_date) output = BytesIO() workbook = Workbook() worksheet = workbook.active worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') header_list = [ 'حجم', 'حجم کم شده از سالن مرغدار', 'وزن تقریبی کشتار(کیلوگرم)', 'حجم سفارشات دریافتی توسط کشتارگاه', 'اختلاف کشتار(حجم)', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'اختلاف کشتار {state}', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) l = 5 m = 1 serializer = BarDifferenceRequestSerializer(bar_requests, many=True).data if serializer: for data in serializer: datetime_object = datetime.datetime.strptime(data['create_date'], "%Y-%m-%dT%H:%M:%S.%f") if data['state'] == 'pending': status = 'در انتظار تایید' elif data['state'] == 'accepted': status = 'تایید شده' else: status = 'رد شده' list1 = [ m, status, data['register_fullname'], str(convert_to_shamsi(date=datetime_object.date())), data['hatching']['poultry']['unit_name'] + '(' + data['hatching']['poultry']['user']['mobile'] + ')', data['kill_house']['name'] + '(' + data['kill_house']['kill_house_operator']['user']['mobile'] + ')', data['quantity'], data['bar_info']['total_quantity'], data['bar_info']['total_weight'], data['bar_info']['first_total_quantity'], data['bar_info']['difference_quantity'], ] m += 1 l += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin') quantity = sum( item['quantity'] for item in serializer) total_quantity = sum( item['bar_info'].get('total_quantity', 0) for item in serializer) total_weight = sum( item['bar_info'].get('total_weight', 0) for item in serializer) first_total_quantity = sum( item['bar_info'].get('first_total_quantity', 0) for item in serializer) difference_quantity = sum( item['bar_info'].get('difference_quantity', 0) for item in serializer) value_header_list = [ quantity, total_quantity, total_weight, first_total_quantity, difference_quantity, ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', quantity, total_quantity, total_weight, first_total_quantity, difference_quantity, ] create_value(worksheet, list2, l + 3, 1, color='green') workbook.save(output) output.seek(0) response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ 'Content-Disposition'] = f'attachment; filename="اختلاف کشتار {state}.xlsx"'.encode( 'utf-8') response.write(output.getvalue()) return response def direct_buying_poultry_requests(request): filterset_class = PoultryRequestDirectBuyingFilterSet role = request.GET.get('role') now = datetime.datetime.now() show_market = ShowMarketRequest.objects.filter(trash=False, allow=True).first() now_time = now.time().replace(second=0, microsecond=0) if show_market and role == 'KillHouse': if show_market.start_time < \ now_time < show_market.end_time: queryset = PoultryRequest.objects.filter( send_date__date=now.date(), state_process='accepted', province_state='accepted', temporary_trash=False, trash=False, out=False, final_state='pending', market=True, ) else: queryset = [] else: queryset = PoultryRequest.objects.filter( send_date__date=now.date(), state_process='accepted', province_state='accepted', temporary_trash=False, trash=False, out=False, final_state='pending', market=True, ) breed = request.GET.get('breed') city = request.GET.get('city') remain = request.GET.get('remain') min_amount = request.GET.get('min_amount') max_amount = request.GET.get('max_amount') weight = request.GET.get('weight') min_age = request.GET.get('min_age') max_age = request.GET.get('max_age') value = request.GET.get('value') search = request.GET.get('search') if breed: queryset = queryset.filter(chicken_breed__in=breed.split(',')) if city: queryset = queryset.filter(poultry__user__city__name__in=city.split(',')) if remain == 'true': queryset = queryset.filter(remain_quantity__gt=0) if min_amount: queryset = queryset.filter(amount__gte=min_amount) if max_amount: queryset = queryset.filter(amount__lte=max_amount) if weight: if weight == 'سبک': queryset = queryset.filter(Index_weight__gte=2, Index_weight__lt=2.7) elif weight == 'متوسط': queryset = queryset.filter(Index_weight__gte=2.7, Index_weight__lt=3) elif weight == 'سنگین': queryset = queryset.filter(Index_weight__gte=3) if min_age: queryset = queryset.filter(hatching__chicken_age__gte=min_age) if max_age: queryset = queryset.filter(hatching__chicken_age__lte=max_age) if value and search == 'filter' and value.strip() and value != 'undefined': queryset = queryset.filter(build_query(filterset_class, value)) excel_options = [ "ردیف", "نام و نام خانوادگی", "تلفن", "استان", "شهر", "نژاد", "سن جوجه", "میانگین وزنی (کیلوگرم)", "تاریخ کشتار", "تعداد قطعه", "مانده قابل خرید", "فروش رفته", "مبلغ هر کیلو (ریال)", ] output = BytesIO() workbook = Workbook() worksheet = workbook.active worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') header_list = [ 'تعداد قطعه', 'مانده قابل خرید', 'فروش رفته', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'لیست اعلام کشتار های مرغداران', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) l = 5 m = 1 serializer = PoultryRequestForDirectBuyingSerializer(queryset, many=True).data if serializer: for data in serializer: if type(data['send_date']) is str: try: str_date = datetime.datetime.strptime(str(data['send_date']), "%Y-%m-%dT%H:%M:%S").date() except: str_date = datetime.datetime.strptime(str(data['send_date']), '%Y-%m-%d').date() date = shamsi_date(str_date, in_value=True) elif data['send_date'] is None: date = '-' else: date = shamsi_date(data['send_date'], in_value=True) list1 = [ m, data['poultry']['unit_name'], data['poultry']['mobile'], data['poultry']['province'], data['poultry']['city'], data['chicken_breed'], data['age']['age'], str(data['Index_weight']), str(date), data['quantity'], data['remain_quantity'], data['total_allocated'], data['amount'], ] m += 1 l += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin') quantity = sum( item['quantity'] for item in serializer) remain_quantity = sum( item['remain_quantity'] for item in serializer) total_allocated = sum( item['total_allocated'] for item in serializer) value_header_list = [ quantity, remain_quantity, total_allocated, ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', quantity, remain_quantity, total_allocated, '', ] create_value(worksheet, list2, l + 3, 1, color='green') workbook.save(output) output.seek(0) response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ 'Content-Disposition'] = f'attachment; filename="لیست اعلام کشتار های مرغداران.xlsx"'.encode( 'utf-8') response.write(output.getvalue()) return response def hatching_for_every_age_range(request): filterset_class = PoultryHatchingFilterSet poultry_hatch = PoultryHatching.objects.filter(archive=False, allow_hatching='pending', trash=False).select_related('poultry', 'poultry__user').order_by( '-chicken_age') filterset_fields = [ 'poultry__user__first_name', 'poultry__user__last_name', 'poultry__user__mobile', 'poultry__unit_name', 'poultry__address__city__name', 'chicken_age', 'poultry__breeding_unique_id', ] date1 = datetime.datetime.now().date() output = BytesIO() workbook = Workbook() worksheet = workbook.active workbook.remove(worksheet) sheet_name = [ "کمتر از 35 روز", "بین 35 تا 40 روز", "بین 40 تا 45 روز", "بین 45 تا 50 روز", "بین 50 تا 55 روز", "بین 55 تا 60 روز", "بین 60 تا 65 روز", "بین 65 تا 70 روز", "بیش از 70 روز" ] for sheet_names in sheet_name: worksheet = workbook.create_sheet(sheet_names) worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) if sheet_names == 'کمتر از 35 روز': filtered_poultry_hatch =poultry_hatch.filter(chicken_age__lt=35).select_related('poultry', 'poultry__user').order_by( '-chicken_age') if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() date1 = datetime.datetime.now().date() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'تلفن مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیشبینی تاریخ کشتار', 'نژاد', 'سن', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک(قطعه)', 'تلفات اتحادیه(قطعه)', 'تلفات کل(قطعه)', 'زنجیره/شرکت', 'دامپزشک فارم', 'تلفن دامپزشک فارم', 'کد سیستمی واحد', 'حجم کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'درصد مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'وزن تعهد آزاد(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'حجم فروش به خارج از استان(قطعه)', 'حجم فروش به خارج از استان(کیلوگرم)', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'تایید تخلیه در سماصط', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بارهای تحویلی', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ گزارش', ] from_date_1 = shamsi_date(date1) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 poultry_ids = poultry_hatch.values_list('poultry', flat=True).distinct() vet_farms = VetFarm.objects.filter(poultry__in=poultry_ids, trash=False).select_related('vet__user') vet_farm_mapping = { vet_farm.poultry: (vet_farm.vet.user.fullname, vet_farm.vet.user.mobile) for vet_farm in vet_farms } l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching) governmental_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=False) governmental_province_kill_requests_quantity = \ governmental_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 free_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=True) free_province_kill_requests_quantity = \ free_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 out_poultry_requests = PoultryRequest.objects.filter(trash=False, out=True, state_process='accepted', province_state='accepted', out_province_request_cancel=False, final_state='archive', hatching=poultry_hatching) out_poultry_requests_quantity = out_poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 free_sale_province = FreeSaleWithinprovince.objects.filter(trash=False).first() if free_sale_province.allow == False: left_total_free_commitment_quantity = poultry_hatching.left_over elif poultry_hatching.total_free_commitment_quantity == 0: left_total_free_commitment_quantity = 0 else: if poultry_hatching.total_commitment_quantity == 0: left_total_free_commitment_quantity = poultry_hatching.left_over else: left_total_free_commitment_quantity = poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity) if ( poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity)) > 0 else 0 # return { # "governmental_allocated_quantity": governmental_province_kill_requests_quantity, # "total_commitment_quantity": obj.total_commitment_quantity, # "free_allocated_quantity": free_province_kill_requests_quantity + out_poultry_requests_quantity, # "total_free_commitment_quantity": obj.total_free_commitment_quantity, # "left_total_free_commitment_quantity": left_total_free_commitment_quantity, # # } vet_farm_id = poultry_hatching.poultry vet_farm_name, vet_farm_mobile = vet_farm_mapping.get(vet_farm_id, ('-', '-')) all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) predicate_date = jdatetime.date.fromgregorian( day=poultry_hatching.predicate_date.day, month=poultry_hatching.predicate_date.month, year=poultry_hatching.predicate_date.year ) if poultry_hatching.predicate_date else '-' date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 wothout_bar = province_kill_requests.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0) province_kill_requests_quantity_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_quantity'))['total'] or 0 province_kill_requests_weight_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_weight'))['total'] or 0 active_kill = 'ندارد' count_of_request = 0 province_kill_request_active = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='accepted') if province_kill_request_active.count() > 0: count_of_request = province_kill_request_active.count() if ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='pending').exists(): active_kill = 'دارد' list1 = [ m, farm_state, poultry_hatching.licence_number, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.CertId, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.user.mobile, poultry_hatching.InteractTypeName, poultry_hatching.PersonTypeName, poultry_hatching.UnionTypeName, f'{poultry_hatching.poultry.user.city.name if poultry_hatching.poultry.user.city else "-"} ' f'/ {poultry_hatching.poultry.city_operator if poultry_hatching.poultry.city_operator else "-"}', poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.poultry.killing_ave_age, str(predicate_date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.increase_quantity, poultry_hatching.losses, poultry_hatching.direct_losses, poultry_hatching.total_losses, chain_company, # poultry_hatching.poultry.city_operator, # # # vet_farm_name, vet_farm_mobile, poultry_hatching.poultry.system_code, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, f'{int(poultry_hatching.left_over * 100 / poultry_hatching.quantity)}%', left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.total_free_commitment_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(wothout_bar), province_kill_requests_quantity_wothout_bar, province_kill_requests_weight_wothout_bar, poultry_hatching.samasat_discharge_percentage, active_kill, count_of_request, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, poultry_hatching.chain_killed_quantity, poultry_hatching.chain_killed_weight, poultry_hatching.export_killed_quantity, poultry_hatching.export_killed_weight, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) all_increase_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('increase_quantity')).get( 'total_quantity', 0) all_total_free_commitment_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_free_commitment_quantity')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 chain_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_weight')).get( 'total_quantity', 0) or 0 chain_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_quantity')).get( 'total_quantity', 0) or 0 extra_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_quantity')).get( 'total_quantity', 0) or 0 export_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_weight')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_increase_quantity, all_losses, all_direct_losses, all_total_losses, '', '', '', '', all_killed_quantity, all_total_killed_weight, all_left_over, '', all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_total_free_commitment_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, '', '', '', '', '', '', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') elif sheet_names == 'بین 35 تا 40 روز': filtered_poultry_hatch =poultry_hatch.filter(chicken_age__range=(35, 39)).select_related('poultry', 'poultry__user').order_by( '-chicken_age') if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() date1 = datetime.datetime.now().date() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'تلفن مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیشبینی تاریخ کشتار', 'نژاد', 'سن', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک(قطعه)', 'تلفات اتحادیه(قطعه)', 'تلفات کل(قطعه)', 'زنجیره/شرکت', 'دامپزشک فارم', 'تلفن دامپزشک فارم', 'کد سیستمی واحد', 'حجم کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'درصد مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'وزن تعهد آزاد(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'حجم فروش به خارج از استان(قطعه)', 'حجم فروش به خارج از استان(کیلوگرم)', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'تایید تخلیه در سماصط', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بارهای تحویلی', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ گزارش', ] from_date_1 = shamsi_date(date1) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 poultry_ids = poultry_hatch.values_list('poultry', flat=True).distinct() vet_farms = VetFarm.objects.filter(poultry__in=poultry_ids, trash=False).select_related('vet__user') vet_farm_mapping = { vet_farm.poultry: (vet_farm.vet.user.fullname, vet_farm.vet.user.mobile) for vet_farm in vet_farms } l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching) governmental_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=False) governmental_province_kill_requests_quantity = \ governmental_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 free_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=True) free_province_kill_requests_quantity = \ free_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 out_poultry_requests = PoultryRequest.objects.filter(trash=False, out=True, state_process='accepted', province_state='accepted', out_province_request_cancel=False, final_state='archive', hatching=poultry_hatching) out_poultry_requests_quantity = out_poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 free_sale_province = FreeSaleWithinprovince.objects.filter(trash=False).first() if free_sale_province.allow == False: left_total_free_commitment_quantity = poultry_hatching.left_over elif poultry_hatching.total_free_commitment_quantity == 0: left_total_free_commitment_quantity = 0 else: if poultry_hatching.total_commitment_quantity == 0: left_total_free_commitment_quantity = poultry_hatching.left_over else: left_total_free_commitment_quantity = poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity) if ( poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity)) > 0 else 0 # return { # "governmental_allocated_quantity": governmental_province_kill_requests_quantity, # "total_commitment_quantity": obj.total_commitment_quantity, # "free_allocated_quantity": free_province_kill_requests_quantity + out_poultry_requests_quantity, # "total_free_commitment_quantity": obj.total_free_commitment_quantity, # "left_total_free_commitment_quantity": left_total_free_commitment_quantity, # # } vet_farm_id = poultry_hatching.poultry vet_farm_name, vet_farm_mobile = vet_farm_mapping.get(vet_farm_id, ('-', '-')) all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) predicate_date = jdatetime.date.fromgregorian( day=poultry_hatching.predicate_date.day, month=poultry_hatching.predicate_date.month, year=poultry_hatching.predicate_date.year ) if poultry_hatching.predicate_date else '-' date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 wothout_bar = province_kill_requests.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0) province_kill_requests_quantity_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_quantity'))['total'] or 0 province_kill_requests_weight_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_weight'))['total'] or 0 active_kill = 'ندارد' count_of_request = 0 province_kill_request_active = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='accepted') if province_kill_request_active.count() > 0: count_of_request = province_kill_request_active.count() if ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='pending').exists(): active_kill = 'دارد' list1 = [ m, farm_state, poultry_hatching.licence_number, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.CertId, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.user.mobile, poultry_hatching.InteractTypeName, poultry_hatching.PersonTypeName, poultry_hatching.UnionTypeName, f'{poultry_hatching.poultry.user.city.name if poultry_hatching.poultry.user.city else "-"} ' f'/ {poultry_hatching.poultry.city_operator if poultry_hatching.poultry.city_operator else "-"}', poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.poultry.killing_ave_age, str(predicate_date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.increase_quantity, poultry_hatching.losses, poultry_hatching.direct_losses, poultry_hatching.total_losses, chain_company, # poultry_hatching.poultry.city_operator, # # # vet_farm_name, vet_farm_mobile, poultry_hatching.poultry.system_code, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, f'{int(poultry_hatching.left_over * 100 / poultry_hatching.quantity)}%', left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.total_free_commitment_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(wothout_bar), province_kill_requests_quantity_wothout_bar, province_kill_requests_weight_wothout_bar, poultry_hatching.samasat_discharge_percentage, active_kill, count_of_request, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, poultry_hatching.chain_killed_quantity, poultry_hatching.chain_killed_weight, poultry_hatching.export_killed_quantity, poultry_hatching.export_killed_weight, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) all_increase_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('increase_quantity')).get( 'total_quantity', 0) all_total_free_commitment_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_free_commitment_quantity')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 chain_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_weight')).get( 'total_quantity', 0) or 0 chain_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_quantity')).get( 'total_quantity', 0) or 0 extra_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_quantity')).get( 'total_quantity', 0) or 0 export_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_weight')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_increase_quantity, all_losses, all_direct_losses, all_total_losses, '', '', '', '', all_killed_quantity, all_total_killed_weight, all_left_over, '', all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_total_free_commitment_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, '', '', '', '', '', '', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') elif sheet_names == 'بین 40 تا 45 روز': filtered_poultry_hatch =poultry_hatch.filter(chicken_age__range=(40, 44)).select_related('poultry', 'poultry__user').order_by( '-chicken_age') if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() date1 = datetime.datetime.now().date() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'تلفن مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیشبینی تاریخ کشتار', 'نژاد', 'سن', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک(قطعه)', 'تلفات اتحادیه(قطعه)', 'تلفات کل(قطعه)', 'زنجیره/شرکت', 'دامپزشک فارم', 'تلفن دامپزشک فارم', 'کد سیستمی واحد', 'حجم کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'درصد مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'وزن تعهد آزاد(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'حجم فروش به خارج از استان(قطعه)', 'حجم فروش به خارج از استان(کیلوگرم)', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'تایید تخلیه در سماصط', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بارهای تحویلی', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ گزارش', ] from_date_1 = shamsi_date(date1) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 poultry_ids = poultry_hatch.values_list('poultry', flat=True).distinct() vet_farms = VetFarm.objects.filter(poultry__in=poultry_ids, trash=False).select_related('vet__user') vet_farm_mapping = { vet_farm.poultry: (vet_farm.vet.user.fullname, vet_farm.vet.user.mobile) for vet_farm in vet_farms } l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching) governmental_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=False) governmental_province_kill_requests_quantity = \ governmental_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 free_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=True) free_province_kill_requests_quantity = \ free_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 out_poultry_requests = PoultryRequest.objects.filter(trash=False, out=True, state_process='accepted', province_state='accepted', out_province_request_cancel=False, final_state='archive', hatching=poultry_hatching) out_poultry_requests_quantity = out_poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 free_sale_province = FreeSaleWithinprovince.objects.filter(trash=False).first() if free_sale_province.allow == False: left_total_free_commitment_quantity = poultry_hatching.left_over elif poultry_hatching.total_free_commitment_quantity == 0: left_total_free_commitment_quantity = 0 else: if poultry_hatching.total_commitment_quantity == 0: left_total_free_commitment_quantity = poultry_hatching.left_over else: left_total_free_commitment_quantity = poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity) if ( poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity)) > 0 else 0 # return { # "governmental_allocated_quantity": governmental_province_kill_requests_quantity, # "total_commitment_quantity": obj.total_commitment_quantity, # "free_allocated_quantity": free_province_kill_requests_quantity + out_poultry_requests_quantity, # "total_free_commitment_quantity": obj.total_free_commitment_quantity, # "left_total_free_commitment_quantity": left_total_free_commitment_quantity, # # } vet_farm_id = poultry_hatching.poultry vet_farm_name, vet_farm_mobile = vet_farm_mapping.get(vet_farm_id, ('-', '-')) all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) predicate_date = jdatetime.date.fromgregorian( day=poultry_hatching.predicate_date.day, month=poultry_hatching.predicate_date.month, year=poultry_hatching.predicate_date.year ) if poultry_hatching.predicate_date else '-' date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 wothout_bar = province_kill_requests.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0) province_kill_requests_quantity_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_quantity'))['total'] or 0 province_kill_requests_weight_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_weight'))['total'] or 0 active_kill = 'ندارد' count_of_request = 0 province_kill_request_active = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='accepted') if province_kill_request_active.count() > 0: count_of_request = province_kill_request_active.count() if ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='pending').exists(): active_kill = 'دارد' list1 = [ m, farm_state, poultry_hatching.licence_number, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.CertId, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.user.mobile, poultry_hatching.InteractTypeName, poultry_hatching.PersonTypeName, poultry_hatching.UnionTypeName, f'{poultry_hatching.poultry.user.city.name if poultry_hatching.poultry.user.city else "-"} ' f'/ {poultry_hatching.poultry.city_operator if poultry_hatching.poultry.city_operator else "-"}', poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.poultry.killing_ave_age, str(predicate_date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.increase_quantity, poultry_hatching.losses, poultry_hatching.direct_losses, poultry_hatching.total_losses, chain_company, # poultry_hatching.poultry.city_operator, # # # vet_farm_name, vet_farm_mobile, poultry_hatching.poultry.system_code, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, f'{int(poultry_hatching.left_over * 100 / poultry_hatching.quantity)}%', left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.total_free_commitment_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(wothout_bar), province_kill_requests_quantity_wothout_bar, province_kill_requests_weight_wothout_bar, poultry_hatching.samasat_discharge_percentage, active_kill, count_of_request, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, poultry_hatching.chain_killed_quantity, poultry_hatching.chain_killed_weight, poultry_hatching.export_killed_quantity, poultry_hatching.export_killed_weight, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) all_increase_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('increase_quantity')).get( 'total_quantity', 0) all_total_free_commitment_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_free_commitment_quantity')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 chain_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_weight')).get( 'total_quantity', 0) or 0 chain_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_quantity')).get( 'total_quantity', 0) or 0 extra_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_quantity')).get( 'total_quantity', 0) or 0 export_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_weight')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_increase_quantity, all_losses, all_direct_losses, all_total_losses, '', '', '', '', all_killed_quantity, all_total_killed_weight, all_left_over, '', all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_total_free_commitment_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, '', '', '', '', '', '', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') elif sheet_names == 'بین 45 تا 50 روز': filtered_poultry_hatch =poultry_hatch.filter(chicken_age__range=(45, 49)).select_related('poultry', 'poultry__user').order_by( '-chicken_age') if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() date1 = datetime.datetime.now().date() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'تلفن مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیشبینی تاریخ کشتار', 'نژاد', 'سن', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک(قطعه)', 'تلفات اتحادیه(قطعه)', 'تلفات کل(قطعه)', 'زنجیره/شرکت', 'دامپزشک فارم', 'تلفن دامپزشک فارم', 'کد سیستمی واحد', 'حجم کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'درصد مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'وزن تعهد آزاد(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'حجم فروش به خارج از استان(قطعه)', 'حجم فروش به خارج از استان(کیلوگرم)', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'تایید تخلیه در سماصط', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بارهای تحویلی', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ گزارش', ] from_date_1 = shamsi_date(date1) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 poultry_ids = poultry_hatch.values_list('poultry', flat=True).distinct() vet_farms = VetFarm.objects.filter(poultry__in=poultry_ids, trash=False).select_related('vet__user') vet_farm_mapping = { vet_farm.poultry: (vet_farm.vet.user.fullname, vet_farm.vet.user.mobile) for vet_farm in vet_farms } l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching) governmental_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=False) governmental_province_kill_requests_quantity = \ governmental_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 free_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=True) free_province_kill_requests_quantity = \ free_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 out_poultry_requests = PoultryRequest.objects.filter(trash=False, out=True, state_process='accepted', province_state='accepted', out_province_request_cancel=False, final_state='archive', hatching=poultry_hatching) out_poultry_requests_quantity = out_poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 free_sale_province = FreeSaleWithinprovince.objects.filter(trash=False).first() if free_sale_province.allow == False: left_total_free_commitment_quantity = poultry_hatching.left_over elif poultry_hatching.total_free_commitment_quantity == 0: left_total_free_commitment_quantity = 0 else: if poultry_hatching.total_commitment_quantity == 0: left_total_free_commitment_quantity = poultry_hatching.left_over else: left_total_free_commitment_quantity = poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity) if ( poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity)) > 0 else 0 # return { # "governmental_allocated_quantity": governmental_province_kill_requests_quantity, # "total_commitment_quantity": obj.total_commitment_quantity, # "free_allocated_quantity": free_province_kill_requests_quantity + out_poultry_requests_quantity, # "total_free_commitment_quantity": obj.total_free_commitment_quantity, # "left_total_free_commitment_quantity": left_total_free_commitment_quantity, # # } vet_farm_id = poultry_hatching.poultry vet_farm_name, vet_farm_mobile = vet_farm_mapping.get(vet_farm_id, ('-', '-')) all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) predicate_date = jdatetime.date.fromgregorian( day=poultry_hatching.predicate_date.day, month=poultry_hatching.predicate_date.month, year=poultry_hatching.predicate_date.year ) if poultry_hatching.predicate_date else '-' date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 wothout_bar = province_kill_requests.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0) province_kill_requests_quantity_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_quantity'))['total'] or 0 province_kill_requests_weight_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_weight'))['total'] or 0 active_kill = 'ندارد' count_of_request = 0 province_kill_request_active = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='accepted') if province_kill_request_active.count() > 0: count_of_request = province_kill_request_active.count() if ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='pending').exists(): active_kill = 'دارد' list1 = [ m, farm_state, poultry_hatching.licence_number, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.CertId, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.user.mobile, poultry_hatching.InteractTypeName, poultry_hatching.PersonTypeName, poultry_hatching.UnionTypeName, f'{poultry_hatching.poultry.user.city.name if poultry_hatching.poultry.user.city else "-"} ' f'/ {poultry_hatching.poultry.city_operator if poultry_hatching.poultry.city_operator else "-"}', poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.poultry.killing_ave_age, str(predicate_date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.increase_quantity, poultry_hatching.losses, poultry_hatching.direct_losses, poultry_hatching.total_losses, chain_company, # poultry_hatching.poultry.city_operator, # # # vet_farm_name, vet_farm_mobile, poultry_hatching.poultry.system_code, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, f'{int(poultry_hatching.left_over * 100 / poultry_hatching.quantity)}%', left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.total_free_commitment_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(wothout_bar), province_kill_requests_quantity_wothout_bar, province_kill_requests_weight_wothout_bar, poultry_hatching.samasat_discharge_percentage, active_kill, count_of_request, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, poultry_hatching.chain_killed_quantity, poultry_hatching.chain_killed_weight, poultry_hatching.export_killed_quantity, poultry_hatching.export_killed_weight, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) all_increase_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('increase_quantity')).get( 'total_quantity', 0) all_total_free_commitment_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_free_commitment_quantity')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 chain_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_weight')).get( 'total_quantity', 0) or 0 chain_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_quantity')).get( 'total_quantity', 0) or 0 extra_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_quantity')).get( 'total_quantity', 0) or 0 export_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_weight')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_increase_quantity, all_losses, all_direct_losses, all_total_losses, '', '', '', '', all_killed_quantity, all_total_killed_weight, all_left_over, '', all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_total_free_commitment_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, '', '', '', '', '', '', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') elif sheet_names == 'بین 45 تا 50 روز': filtered_poultry_hatch =poultry_hatch.filter(chicken_age__range=(45, 49)).select_related('poultry', 'poultry__user').order_by( '-chicken_age') if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() date1 = datetime.datetime.now().date() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'تلفن مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیشبینی تاریخ کشتار', 'نژاد', 'سن', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک(قطعه)', 'تلفات اتحادیه(قطعه)', 'تلفات کل(قطعه)', 'زنجیره/شرکت', 'دامپزشک فارم', 'تلفن دامپزشک فارم', 'کد سیستمی واحد', 'حجم کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'درصد مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'وزن تعهد آزاد(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'حجم فروش به خارج از استان(قطعه)', 'حجم فروش به خارج از استان(کیلوگرم)', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'تایید تخلیه در سماصط', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بارهای تحویلی', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ گزارش', ] from_date_1 = shamsi_date(date1) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 poultry_ids = poultry_hatch.values_list('poultry', flat=True).distinct() vet_farms = VetFarm.objects.filter(poultry__in=poultry_ids, trash=False).select_related('vet__user') vet_farm_mapping = { vet_farm.poultry: (vet_farm.vet.user.fullname, vet_farm.vet.user.mobile) for vet_farm in vet_farms } l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching) governmental_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=False) governmental_province_kill_requests_quantity = \ governmental_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 free_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=True) free_province_kill_requests_quantity = \ free_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 out_poultry_requests = PoultryRequest.objects.filter(trash=False, out=True, state_process='accepted', province_state='accepted', out_province_request_cancel=False, final_state='archive', hatching=poultry_hatching) out_poultry_requests_quantity = out_poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 free_sale_province = FreeSaleWithinprovince.objects.filter(trash=False).first() if free_sale_province.allow == False: left_total_free_commitment_quantity = poultry_hatching.left_over elif poultry_hatching.total_free_commitment_quantity == 0: left_total_free_commitment_quantity = 0 else: if poultry_hatching.total_commitment_quantity == 0: left_total_free_commitment_quantity = poultry_hatching.left_over else: left_total_free_commitment_quantity = poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity) if ( poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity)) > 0 else 0 # return { # "governmental_allocated_quantity": governmental_province_kill_requests_quantity, # "total_commitment_quantity": obj.total_commitment_quantity, # "free_allocated_quantity": free_province_kill_requests_quantity + out_poultry_requests_quantity, # "total_free_commitment_quantity": obj.total_free_commitment_quantity, # "left_total_free_commitment_quantity": left_total_free_commitment_quantity, # # } vet_farm_id = poultry_hatching.poultry vet_farm_name, vet_farm_mobile = vet_farm_mapping.get(vet_farm_id, ('-', '-')) all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) predicate_date = jdatetime.date.fromgregorian( day=poultry_hatching.predicate_date.day, month=poultry_hatching.predicate_date.month, year=poultry_hatching.predicate_date.year ) if poultry_hatching.predicate_date else '-' date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 wothout_bar = province_kill_requests.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0) province_kill_requests_quantity_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_quantity'))['total'] or 0 province_kill_requests_weight_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_weight'))['total'] or 0 active_kill = 'ندارد' count_of_request = 0 province_kill_request_active = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='accepted') if province_kill_request_active.count() > 0: count_of_request = province_kill_request_active.count() if ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='pending').exists(): active_kill = 'دارد' list1 = [ m, farm_state, poultry_hatching.licence_number, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.CertId, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.user.mobile, poultry_hatching.InteractTypeName, poultry_hatching.PersonTypeName, poultry_hatching.UnionTypeName, f'{poultry_hatching.poultry.user.city.name if poultry_hatching.poultry.user.city else "-"} ' f'/ {poultry_hatching.poultry.city_operator if poultry_hatching.poultry.city_operator else "-"}', poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.poultry.killing_ave_age, str(predicate_date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.increase_quantity, poultry_hatching.losses, poultry_hatching.direct_losses, poultry_hatching.total_losses, chain_company, # poultry_hatching.poultry.city_operator, # # # vet_farm_name, vet_farm_mobile, poultry_hatching.poultry.system_code, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, f'{int(poultry_hatching.left_over * 100 / poultry_hatching.quantity)}%', left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.total_free_commitment_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(wothout_bar), province_kill_requests_quantity_wothout_bar, province_kill_requests_weight_wothout_bar, poultry_hatching.samasat_discharge_percentage, active_kill, count_of_request, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, poultry_hatching.chain_killed_quantity, poultry_hatching.chain_killed_weight, poultry_hatching.export_killed_quantity, poultry_hatching.export_killed_weight, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) all_increase_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('increase_quantity')).get( 'total_quantity', 0) all_total_free_commitment_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_free_commitment_quantity')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 chain_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_weight')).get( 'total_quantity', 0) or 0 chain_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_quantity')).get( 'total_quantity', 0) or 0 extra_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_quantity')).get( 'total_quantity', 0) or 0 export_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_weight')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_increase_quantity, all_losses, all_direct_losses, all_total_losses, '', '', '', '', all_killed_quantity, all_total_killed_weight, all_left_over, '', all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_total_free_commitment_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, '', '', '', '', '', '', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') elif sheet_names == 'بین 50 تا 55 روز': filtered_poultry_hatch = poultry_hatch.filter(chicken_age__range=(50, 54)).select_related('poultry', 'poultry__user').order_by( '-chicken_age') if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() date1 = datetime.datetime.now().date() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'تلفن مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیشبینی تاریخ کشتار', 'نژاد', 'سن', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک(قطعه)', 'تلفات اتحادیه(قطعه)', 'تلفات کل(قطعه)', 'زنجیره/شرکت', 'دامپزشک فارم', 'تلفن دامپزشک فارم', 'کد سیستمی واحد', 'حجم کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'درصد مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'وزن تعهد آزاد(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'حجم فروش به خارج از استان(قطعه)', 'حجم فروش به خارج از استان(کیلوگرم)', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'تایید تخلیه در سماصط', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بارهای تحویلی', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ گزارش', ] from_date_1 = shamsi_date(date1) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 poultry_ids = poultry_hatch.values_list('poultry', flat=True).distinct() vet_farms = VetFarm.objects.filter(poultry__in=poultry_ids, trash=False).select_related('vet__user') vet_farm_mapping = { vet_farm.poultry: (vet_farm.vet.user.fullname, vet_farm.vet.user.mobile) for vet_farm in vet_farms } l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching) governmental_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=False) governmental_province_kill_requests_quantity = \ governmental_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 free_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=True) free_province_kill_requests_quantity = \ free_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 out_poultry_requests = PoultryRequest.objects.filter(trash=False, out=True, state_process='accepted', province_state='accepted', out_province_request_cancel=False, final_state='archive', hatching=poultry_hatching) out_poultry_requests_quantity = out_poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 free_sale_province = FreeSaleWithinprovince.objects.filter(trash=False).first() if free_sale_province.allow == False: left_total_free_commitment_quantity = poultry_hatching.left_over elif poultry_hatching.total_free_commitment_quantity == 0: left_total_free_commitment_quantity = 0 else: if poultry_hatching.total_commitment_quantity == 0: left_total_free_commitment_quantity = poultry_hatching.left_over else: left_total_free_commitment_quantity = poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity) if ( poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity)) > 0 else 0 # return { # "governmental_allocated_quantity": governmental_province_kill_requests_quantity, # "total_commitment_quantity": obj.total_commitment_quantity, # "free_allocated_quantity": free_province_kill_requests_quantity + out_poultry_requests_quantity, # "total_free_commitment_quantity": obj.total_free_commitment_quantity, # "left_total_free_commitment_quantity": left_total_free_commitment_quantity, # # } vet_farm_id = poultry_hatching.poultry vet_farm_name, vet_farm_mobile = vet_farm_mapping.get(vet_farm_id, ('-', '-')) all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) predicate_date = jdatetime.date.fromgregorian( day=poultry_hatching.predicate_date.day, month=poultry_hatching.predicate_date.month, year=poultry_hatching.predicate_date.year ) if poultry_hatching.predicate_date else '-' date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 wothout_bar = province_kill_requests.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0) province_kill_requests_quantity_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_quantity'))['total'] or 0 province_kill_requests_weight_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_weight'))['total'] or 0 active_kill = 'ندارد' count_of_request = 0 province_kill_request_active = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='accepted') if province_kill_request_active.count() > 0: count_of_request = province_kill_request_active.count() if ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='pending').exists(): active_kill = 'دارد' list1 = [ m, farm_state, poultry_hatching.licence_number, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.CertId, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.user.mobile, poultry_hatching.InteractTypeName, poultry_hatching.PersonTypeName, poultry_hatching.UnionTypeName, f'{poultry_hatching.poultry.user.city.name if poultry_hatching.poultry.user.city else "-"} ' f'/ {poultry_hatching.poultry.city_operator if poultry_hatching.poultry.city_operator else "-"}', poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.poultry.killing_ave_age, str(predicate_date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.increase_quantity, poultry_hatching.losses, poultry_hatching.direct_losses, poultry_hatching.total_losses, chain_company, # poultry_hatching.poultry.city_operator, # # # vet_farm_name, vet_farm_mobile, poultry_hatching.poultry.system_code, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, f'{int(poultry_hatching.left_over * 100 / poultry_hatching.quantity)}%', left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.total_free_commitment_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(wothout_bar), province_kill_requests_quantity_wothout_bar, province_kill_requests_weight_wothout_bar, poultry_hatching.samasat_discharge_percentage, active_kill, count_of_request, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, poultry_hatching.chain_killed_quantity, poultry_hatching.chain_killed_weight, poultry_hatching.export_killed_quantity, poultry_hatching.export_killed_weight, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) all_increase_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('increase_quantity')).get( 'total_quantity', 0) all_total_free_commitment_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_free_commitment_quantity')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 chain_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_weight')).get( 'total_quantity', 0) or 0 chain_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_quantity')).get( 'total_quantity', 0) or 0 extra_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_quantity')).get( 'total_quantity', 0) or 0 export_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_weight')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_increase_quantity, all_losses, all_direct_losses, all_total_losses, '', '', '', '', all_killed_quantity, all_total_killed_weight, all_left_over, '', all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_total_free_commitment_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, '', '', '', '', '', '', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') elif sheet_names == 'بین 55 تا 60 روز': filtered_poultry_hatch = poultry_hatch.filter(chicken_age__range=(55, 59)).select_related('poultry', 'poultry__user').order_by( '-chicken_age') if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() date1 = datetime.datetime.now().date() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'تلفن مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیشبینی تاریخ کشتار', 'نژاد', 'سن', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک(قطعه)', 'تلفات اتحادیه(قطعه)', 'تلفات کل(قطعه)', 'زنجیره/شرکت', 'دامپزشک فارم', 'تلفن دامپزشک فارم', 'کد سیستمی واحد', 'حجم کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'درصد مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'وزن تعهد آزاد(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'حجم فروش به خارج از استان(قطعه)', 'حجم فروش به خارج از استان(کیلوگرم)', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'تایید تخلیه در سماصط', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بارهای تحویلی', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ گزارش', ] from_date_1 = shamsi_date(date1) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 poultry_ids = poultry_hatch.values_list('poultry', flat=True).distinct() vet_farms = VetFarm.objects.filter(poultry__in=poultry_ids, trash=False).select_related('vet__user') vet_farm_mapping = { vet_farm.poultry: (vet_farm.vet.user.fullname, vet_farm.vet.user.mobile) for vet_farm in vet_farms } l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching) governmental_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=False) governmental_province_kill_requests_quantity = \ governmental_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 free_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=True) free_province_kill_requests_quantity = \ free_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 out_poultry_requests = PoultryRequest.objects.filter(trash=False, out=True, state_process='accepted', province_state='accepted', out_province_request_cancel=False, final_state='archive', hatching=poultry_hatching) out_poultry_requests_quantity = out_poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 free_sale_province = FreeSaleWithinprovince.objects.filter(trash=False).first() if free_sale_province.allow == False: left_total_free_commitment_quantity = poultry_hatching.left_over elif poultry_hatching.total_free_commitment_quantity == 0: left_total_free_commitment_quantity = 0 else: if poultry_hatching.total_commitment_quantity == 0: left_total_free_commitment_quantity = poultry_hatching.left_over else: left_total_free_commitment_quantity = poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity) if ( poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity)) > 0 else 0 # return { # "governmental_allocated_quantity": governmental_province_kill_requests_quantity, # "total_commitment_quantity": obj.total_commitment_quantity, # "free_allocated_quantity": free_province_kill_requests_quantity + out_poultry_requests_quantity, # "total_free_commitment_quantity": obj.total_free_commitment_quantity, # "left_total_free_commitment_quantity": left_total_free_commitment_quantity, # # } vet_farm_id = poultry_hatching.poultry vet_farm_name, vet_farm_mobile = vet_farm_mapping.get(vet_farm_id, ('-', '-')) all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) predicate_date = jdatetime.date.fromgregorian( day=poultry_hatching.predicate_date.day, month=poultry_hatching.predicate_date.month, year=poultry_hatching.predicate_date.year ) if poultry_hatching.predicate_date else '-' date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 wothout_bar = province_kill_requests.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0) province_kill_requests_quantity_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_quantity'))['total'] or 0 province_kill_requests_weight_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_weight'))['total'] or 0 active_kill = 'ندارد' count_of_request = 0 province_kill_request_active = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='accepted') if province_kill_request_active.count() > 0: count_of_request = province_kill_request_active.count() if ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='pending').exists(): active_kill = 'دارد' list1 = [ m, farm_state, poultry_hatching.licence_number, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.CertId, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.user.mobile, poultry_hatching.InteractTypeName, poultry_hatching.PersonTypeName, poultry_hatching.UnionTypeName, f'{poultry_hatching.poultry.user.city.name if poultry_hatching.poultry.user.city else "-"} ' f'/ {poultry_hatching.poultry.city_operator if poultry_hatching.poultry.city_operator else "-"}', poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.poultry.killing_ave_age, str(predicate_date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.increase_quantity, poultry_hatching.losses, poultry_hatching.direct_losses, poultry_hatching.total_losses, chain_company, # poultry_hatching.poultry.city_operator, # # # vet_farm_name, vet_farm_mobile, poultry_hatching.poultry.system_code, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, f'{int(poultry_hatching.left_over * 100 / poultry_hatching.quantity)}%', left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.total_free_commitment_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(wothout_bar), province_kill_requests_quantity_wothout_bar, province_kill_requests_weight_wothout_bar, poultry_hatching.samasat_discharge_percentage, active_kill, count_of_request, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, poultry_hatching.chain_killed_quantity, poultry_hatching.chain_killed_weight, poultry_hatching.export_killed_quantity, poultry_hatching.export_killed_weight, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) all_increase_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('increase_quantity')).get( 'total_quantity', 0) all_total_free_commitment_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_free_commitment_quantity')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 chain_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_weight')).get( 'total_quantity', 0) or 0 chain_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_quantity')).get( 'total_quantity', 0) or 0 extra_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_quantity')).get( 'total_quantity', 0) or 0 export_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_weight')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_increase_quantity, all_losses, all_direct_losses, all_total_losses, '', '', '', '', all_killed_quantity, all_total_killed_weight, all_left_over, '', all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_total_free_commitment_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, '', '', '', '', '', '', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') elif sheet_names == 'بین 60 تا 65 روز': filtered_poultry_hatch = poultry_hatch.filter(chicken_age__range=(60, 64)).select_related('poultry', 'poultry__user').order_by( '-chicken_age') if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() date1 = datetime.datetime.now().date() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'تلفن مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیشبینی تاریخ کشتار', 'نژاد', 'سن', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک(قطعه)', 'تلفات اتحادیه(قطعه)', 'تلفات کل(قطعه)', 'زنجیره/شرکت', 'دامپزشک فارم', 'تلفن دامپزشک فارم', 'کد سیستمی واحد', 'حجم کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'درصد مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'وزن تعهد آزاد(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'حجم فروش به خارج از استان(قطعه)', 'حجم فروش به خارج از استان(کیلوگرم)', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'تایید تخلیه در سماصط', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بارهای تحویلی', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ گزارش', ] from_date_1 = shamsi_date(date1) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 poultry_ids = poultry_hatch.values_list('poultry', flat=True).distinct() vet_farms = VetFarm.objects.filter(poultry__in=poultry_ids, trash=False).select_related('vet__user') vet_farm_mapping = { vet_farm.poultry: (vet_farm.vet.user.fullname, vet_farm.vet.user.mobile) for vet_farm in vet_farms } l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching) governmental_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=False) governmental_province_kill_requests_quantity = \ governmental_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 free_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=True) free_province_kill_requests_quantity = \ free_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 out_poultry_requests = PoultryRequest.objects.filter(trash=False, out=True, state_process='accepted', province_state='accepted', out_province_request_cancel=False, final_state='archive', hatching=poultry_hatching) out_poultry_requests_quantity = out_poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 free_sale_province = FreeSaleWithinprovince.objects.filter(trash=False).first() if free_sale_province.allow == False: left_total_free_commitment_quantity = poultry_hatching.left_over elif poultry_hatching.total_free_commitment_quantity == 0: left_total_free_commitment_quantity = 0 else: if poultry_hatching.total_commitment_quantity == 0: left_total_free_commitment_quantity = poultry_hatching.left_over else: left_total_free_commitment_quantity = poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity) if ( poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity)) > 0 else 0 # return { # "governmental_allocated_quantity": governmental_province_kill_requests_quantity, # "total_commitment_quantity": obj.total_commitment_quantity, # "free_allocated_quantity": free_province_kill_requests_quantity + out_poultry_requests_quantity, # "total_free_commitment_quantity": obj.total_free_commitment_quantity, # "left_total_free_commitment_quantity": left_total_free_commitment_quantity, # # } vet_farm_id = poultry_hatching.poultry vet_farm_name, vet_farm_mobile = vet_farm_mapping.get(vet_farm_id, ('-', '-')) all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) predicate_date = jdatetime.date.fromgregorian( day=poultry_hatching.predicate_date.day, month=poultry_hatching.predicate_date.month, year=poultry_hatching.predicate_date.year ) if poultry_hatching.predicate_date else '-' date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 wothout_bar = province_kill_requests.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0) province_kill_requests_quantity_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_quantity'))['total'] or 0 province_kill_requests_weight_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_weight'))['total'] or 0 active_kill = 'ندارد' count_of_request = 0 province_kill_request_active = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='accepted') if province_kill_request_active.count() > 0: count_of_request = province_kill_request_active.count() if ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='pending').exists(): active_kill = 'دارد' list1 = [ m, farm_state, poultry_hatching.licence_number, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.CertId, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.user.mobile, poultry_hatching.InteractTypeName, poultry_hatching.PersonTypeName, poultry_hatching.UnionTypeName, f'{poultry_hatching.poultry.user.city.name if poultry_hatching.poultry.user.city else "-"} ' f'/ {poultry_hatching.poultry.city_operator if poultry_hatching.poultry.city_operator else "-"}', poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.poultry.killing_ave_age, str(predicate_date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.increase_quantity, poultry_hatching.losses, poultry_hatching.direct_losses, poultry_hatching.total_losses, chain_company, # poultry_hatching.poultry.city_operator, # # # vet_farm_name, vet_farm_mobile, poultry_hatching.poultry.system_code, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, f'{int(poultry_hatching.left_over * 100 / poultry_hatching.quantity)}%', left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.total_free_commitment_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(wothout_bar), province_kill_requests_quantity_wothout_bar, province_kill_requests_weight_wothout_bar, poultry_hatching.samasat_discharge_percentage, active_kill, count_of_request, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, poultry_hatching.chain_killed_quantity, poultry_hatching.chain_killed_weight, poultry_hatching.export_killed_quantity, poultry_hatching.export_killed_weight, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) all_increase_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('increase_quantity')).get( 'total_quantity', 0) all_total_free_commitment_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_free_commitment_quantity')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 chain_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_weight')).get( 'total_quantity', 0) or 0 chain_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_quantity')).get( 'total_quantity', 0) or 0 extra_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_quantity')).get( 'total_quantity', 0) or 0 export_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_weight')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_increase_quantity, all_losses, all_direct_losses, all_total_losses, '', '', '', '', all_killed_quantity, all_total_killed_weight, all_left_over, '', all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_total_free_commitment_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, '', '', '', '', '', '', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') elif sheet_names == 'بین 65 تا 70 روز': filtered_poultry_hatch = poultry_hatch.filter(chicken_age__range=(65, 70)).select_related('poultry', 'poultry__user').order_by( '-chicken_age') if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() date1 = datetime.datetime.now().date() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'تلفن مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیشبینی تاریخ کشتار', 'نژاد', 'سن', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک(قطعه)', 'تلفات اتحادیه(قطعه)', 'تلفات کل(قطعه)', 'زنجیره/شرکت', 'دامپزشک فارم', 'تلفن دامپزشک فارم', 'کد سیستمی واحد', 'حجم کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'درصد مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'وزن تعهد آزاد(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'حجم فروش به خارج از استان(قطعه)', 'حجم فروش به خارج از استان(کیلوگرم)', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'تایید تخلیه در سماصط', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بارهای تحویلی', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ گزارش', ] from_date_1 = shamsi_date(date1) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 poultry_ids = poultry_hatch.values_list('poultry', flat=True).distinct() vet_farms = VetFarm.objects.filter(poultry__in=poultry_ids, trash=False).select_related('vet__user') vet_farm_mapping = { vet_farm.poultry: (vet_farm.vet.user.fullname, vet_farm.vet.user.mobile) for vet_farm in vet_farms } l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching) governmental_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=False) governmental_province_kill_requests_quantity = \ governmental_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 free_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=True) free_province_kill_requests_quantity = \ free_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 out_poultry_requests = PoultryRequest.objects.filter(trash=False, out=True, state_process='accepted', province_state='accepted', out_province_request_cancel=False, final_state='archive', hatching=poultry_hatching) out_poultry_requests_quantity = out_poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 free_sale_province = FreeSaleWithinprovince.objects.filter(trash=False).first() if free_sale_province.allow == False: left_total_free_commitment_quantity = poultry_hatching.left_over elif poultry_hatching.total_free_commitment_quantity == 0: left_total_free_commitment_quantity = 0 else: if poultry_hatching.total_commitment_quantity == 0: left_total_free_commitment_quantity = poultry_hatching.left_over else: left_total_free_commitment_quantity = poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity) if ( poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity)) > 0 else 0 # return { # "governmental_allocated_quantity": governmental_province_kill_requests_quantity, # "total_commitment_quantity": obj.total_commitment_quantity, # "free_allocated_quantity": free_province_kill_requests_quantity + out_poultry_requests_quantity, # "total_free_commitment_quantity": obj.total_free_commitment_quantity, # "left_total_free_commitment_quantity": left_total_free_commitment_quantity, # # } vet_farm_id = poultry_hatching.poultry vet_farm_name, vet_farm_mobile = vet_farm_mapping.get(vet_farm_id, ('-', '-')) all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) predicate_date = jdatetime.date.fromgregorian( day=poultry_hatching.predicate_date.day, month=poultry_hatching.predicate_date.month, year=poultry_hatching.predicate_date.year ) if poultry_hatching.predicate_date else '-' date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 wothout_bar = province_kill_requests.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0) province_kill_requests_quantity_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_quantity'))['total'] or 0 province_kill_requests_weight_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_weight'))['total'] or 0 active_kill = 'ندارد' count_of_request = 0 province_kill_request_active = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='accepted') if province_kill_request_active.count() > 0: count_of_request = province_kill_request_active.count() if ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='pending').exists(): active_kill = 'دارد' list1 = [ m, farm_state, poultry_hatching.licence_number, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.CertId, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.user.mobile, poultry_hatching.InteractTypeName, poultry_hatching.PersonTypeName, poultry_hatching.UnionTypeName, f'{poultry_hatching.poultry.user.city.name if poultry_hatching.poultry.user.city else "-"} ' f'/ {poultry_hatching.poultry.city_operator if poultry_hatching.poultry.city_operator else "-"}', poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.poultry.killing_ave_age, str(predicate_date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.increase_quantity, poultry_hatching.losses, poultry_hatching.direct_losses, poultry_hatching.total_losses, chain_company, # poultry_hatching.poultry.city_operator, # # # vet_farm_name, vet_farm_mobile, poultry_hatching.poultry.system_code, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, f'{int(poultry_hatching.left_over * 100 / poultry_hatching.quantity)}%', left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.total_free_commitment_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(wothout_bar), province_kill_requests_quantity_wothout_bar, province_kill_requests_weight_wothout_bar, poultry_hatching.samasat_discharge_percentage, active_kill, count_of_request, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, poultry_hatching.chain_killed_quantity, poultry_hatching.chain_killed_weight, poultry_hatching.export_killed_quantity, poultry_hatching.export_killed_weight, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) all_increase_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('increase_quantity')).get( 'total_quantity', 0) all_total_free_commitment_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_free_commitment_quantity')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 chain_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_weight')).get( 'total_quantity', 0) or 0 chain_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_quantity')).get( 'total_quantity', 0) or 0 extra_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_quantity')).get( 'total_quantity', 0) or 0 export_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_weight')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_increase_quantity, all_losses, all_direct_losses, all_total_losses, '', '', '', '', all_killed_quantity, all_total_killed_weight, all_left_over, '', all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_total_free_commitment_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, '', '', '', '', '', '', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') elif sheet_names == 'بیش از 70 روز': filtered_poultry_hatch = poultry_hatch.filter(chicken_age__gt=70).select_related('poultry', 'poultry__user').order_by( '-chicken_age') if 'search' in request.GET: if request.GET['search'] == 'filter': if request.GET['value'] != "" and request.GET['value'] != 'undefined': for item in filterset_fields: query = QueryDict('{0}__contains={1}'.format(item, request.GET['value'])) if (filterset_class( data=query, queryset=filtered_poultry_hatch ) ).filter(): ps = filterset_class(data=query, queryset=filtered_poultry_hatch) filtered_poultry_hatch = ps.filter() date1 = datetime.datetime.now().date() excel_options = [ 'ردیف', 'وضعیت', 'شماره مجوز جوجه ریزی', 'شناسه یکتا', 'مجوز بهداشتی جوجه ریزی', 'نام فارم', 'مرغدار', 'تلفن مرغدار', 'بهره برداری', 'مالکیت', 'ارتباط', 'شهر/تعاونی', 'سالن', ' دوره جوجه ریزی', 'تاریخ ثبت جوجه ریزی', 'تاریخ جوجه ریزی', 'میانگین سن کشتار', 'پیشبینی تاریخ کشتار', 'نژاد', 'سن', 'حجم جوجه ریزی', 'حجم افزایشی', 'تلفات دامپزشک(قطعه)', 'تلفات اتحادیه(قطعه)', 'تلفات کل(قطعه)', 'زنجیره/شرکت', 'دامپزشک فارم', 'تلفن دامپزشک فارم', 'کد سیستمی واحد', 'حجم کشتار شده(قطعه)', 'وزن کل کشتارشده(کیلوگرم)', 'مانده در سالن', 'درصد مانده در سالن', 'نود درصد جوجه ریزی', 'وزن تعهد دولتی(کیلوگرم)', 'تعداد کشتار دولتی(قطعه)', 'وزن کشتار دولتی(کیلوگرم)', 'وزن تعهد آزاد(کیلوگرم)', 'تعداد کشتار آزاد(قطعه)', 'وزن کشتار آزاد(کیلوگرم)', 'میانگین وزن کشتار(کیلوگرم)', 'سازنده جوجه ریزی', 'کد اپیدمیولوژیک', 'حجم فروش به خارج از استان(قطعه)', 'حجم فروش به خارج از استان(کیلوگرم)', 'تخصیصات بدون بار', 'حجم تخصیصات بدون بار', 'وزن تخصیصات بدون بار', 'تایید تخلیه در سماصط', 'تعداد کشتار فعال', 'تعداد درخواست کشتار', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بارهای تحویلی', 'حجم بارهای تحویلی', 'وزن بارهای تحویلی', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', 'ثبت کننده گزارش', 'متن گزارش', 'تاریخ گزارش', ] from_date_1 = shamsi_date(date1) header_list = [ 'تعداد فارم فعال', 'تعداد فارم دارای زنجیره فعال', 'مجموع جوجه ریزی', 'مجموع تلفات دامپزشک(قطعه)', 'مجموع تلفات اتحادیه(قطعه)', 'مجموع تلفات کل(قطعه)', 'مجموع قطعه کشتار شده', 'مجموع وزن کل کشتارشده', 'مانده در سالن(قطعه)', 'مانده در سالن از نود درصد(قطعه)', 'کمترین سن', 'بیشترین سن', 'مجموع وزن تعهد دولتی', ' مجموع قطعه کشتار دولتی', ' مجموع وزن کشتار دولتی', ' مجموع قطعه کشتار آزاد', ' مجموع وزن کشتار آزاد', 'مجموع تعداد کشتار خارج از استان', 'مجموع وزن کشتار خارج از استان', 'تعداد فارم های متخلف', 'تعداد بار ایجاد شده', 'حجم بار ایجاد شده', 'وزن بار ایجاد شده', 'تعداد بار کشتار شده', 'حجم بار کشتار شده', 'وزن بار کشتار شده', 'حجم زنجیره', 'وزن زنجیره', 'حجم صادرات', 'وزن صادرات', 'تعداد بار ورودی به انبار', 'حجم لاشه ها', 'وزن لاشه ها', 'درصد افت بارها', ] create_header(worksheet, header_list, 5, 2, height=20) excel_description(worksheet, 'B1', f'این گزارش در مورخ {from_date_1} صادر شده است.', color='red', row2='C3') create_header_freez(worksheet, excel_options, 1, 6, 7, height=22) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 poultry_ids = poultry_hatch.values_list('poultry', flat=True).distinct() vet_farms = VetFarm.objects.filter(poultry__in=poultry_ids, trash=False).select_related('vet__user') vet_farm_mapping = { vet_farm.poultry: (vet_farm.vet.user.fullname, vet_farm.vet.user.mobile) for vet_farm in vet_farms } l = 5 m = 1 all_poultry_hatching_quantity = 0 all_poultry_hatching_killed_quantity = 0 all_poultry_hatching_left_over = 0 min_list = [] all_left_over_ninty_percent = 0 violation = 0 all_chain_company = 0 for poultry_hatching in filtered_poultry_hatch: province_kill_requests = ProvinceKillRequest.objects.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), province_request__poultry_request__hatching=poultry_hatching) governmental_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=False) governmental_province_kill_requests_quantity = \ governmental_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 free_province_kill_requests = province_kill_requests.filter( Q(province_request__poultry_request__direct_buying=False) | Q( province_request__poultry_request__direct_buying=True), province_request__poultry_request__free_sale_in_province=True) free_province_kill_requests_quantity = \ free_province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[ 'total'] or 0 out_poultry_requests = PoultryRequest.objects.filter(trash=False, out=True, state_process='accepted', province_state='accepted', out_province_request_cancel=False, final_state='archive', hatching=poultry_hatching) out_poultry_requests_quantity = out_poultry_requests.aggregate(total=Sum('quantity'))[ 'total'] or 0 free_sale_province = FreeSaleWithinprovince.objects.filter(trash=False).first() if free_sale_province.allow == False: left_total_free_commitment_quantity = poultry_hatching.left_over elif poultry_hatching.total_free_commitment_quantity == 0: left_total_free_commitment_quantity = 0 else: if poultry_hatching.total_commitment_quantity == 0: left_total_free_commitment_quantity = poultry_hatching.left_over else: left_total_free_commitment_quantity = poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity) if ( poultry_hatching.total_free_commitment_quantity - ( free_province_kill_requests_quantity + out_poultry_requests_quantity)) > 0 else 0 # return { # "governmental_allocated_quantity": governmental_province_kill_requests_quantity, # "total_commitment_quantity": obj.total_commitment_quantity, # "free_allocated_quantity": free_province_kill_requests_quantity + out_poultry_requests_quantity, # "total_free_commitment_quantity": obj.total_free_commitment_quantity, # "left_total_free_commitment_quantity": left_total_free_commitment_quantity, # # } vet_farm_id = poultry_hatching.poultry vet_farm_name, vet_farm_mobile = vet_farm_mapping.get(vet_farm_id, ('-', '-')) all_poultry_hatching_quantity += poultry_hatching.quantity all_poultry_hatching_killed_quantity += poultry_hatching.killed_quantity all_poultry_hatching_left_over += poultry_hatching.left_over if poultry_hatching.chicken_age not in min_list: min_list.append(poultry_hatching.chicken_age) l += 1 create_date = jdatetime.date.fromgregorian( day=poultry_hatching.create_date.day, month=poultry_hatching.create_date.month, year=poultry_hatching.create_date.year ) date = jdatetime.date.fromgregorian( day=poultry_hatching.date.day, month=poultry_hatching.date.month, year=poultry_hatching.date.year ) predicate_date = jdatetime.date.fromgregorian( day=poultry_hatching.predicate_date.day, month=poultry_hatching.predicate_date.month, year=poultry_hatching.predicate_date.year ) if poultry_hatching.predicate_date else '-' date1 = datetime.datetime.strptime(str(poultry_hatching.date), '%Y-%m-%d %H:%M:%S') age = (datetime.datetime.now() - date1).days + 1 creator = '-' if poultry_hatching.registrar: if poultry_hatching.registrar['fullname'] != '': creator = poultry_hatching.registrar['fullname'] else: creator = 'پنجره واحد' left_over_ninty_percent = ((poultry_hatching.quantity * 90) / 100) all_left_over_ninty_percent += left_over_ninty_percent farm_state = 'عادی' if poultry_hatching.violation == True: farm_state = 'متخلف' + ' ' + '(مانده در سالن بیش از حد مجاز)' violation += 1 if poultry_hatching.chain_company: chain_company = poultry_hatching.chain_company.name all_chain_company += 1 else: chain_company = '-' ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) violation_report_date = shamsi_date( poultry_hatching.violation_report_date.date()) if poultry_hatching.violation_report_date else '-' kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 wothout_bar = province_kill_requests.filter(trash=False, return_to_province=False, state__in=('pending', 'accepted'), first_car_allocated_quantity=0) province_kill_requests_quantity_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_quantity'))['total'] or 0 province_kill_requests_weight_wothout_bar = \ wothout_bar.aggregate(total=Sum('total_killed_weight'))['total'] or 0 active_kill = 'ندارد' count_of_request = 0 province_kill_request_active = ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='accepted') if province_kill_request_active.count() > 0: count_of_request = province_kill_request_active.count() if ProvinceKillRequest.objects.filter(trash=False, province_request__poultry_request__hatching=poultry_hatching, state='pending').exists(): active_kill = 'دارد' list1 = [ m, farm_state, poultry_hatching.licence_number, poultry_hatching.poultry.breeding_unique_id, poultry_hatching.CertId, poultry_hatching.poultry.unit_name, poultry_hatching.poultry.user.fullname, poultry_hatching.poultry.user.mobile, poultry_hatching.InteractTypeName, poultry_hatching.PersonTypeName, poultry_hatching.UnionTypeName, f'{poultry_hatching.poultry.user.city.name if poultry_hatching.poultry.user.city else "-"} ' f'/ {poultry_hatching.poultry.city_operator if poultry_hatching.poultry.city_operator else "-"}', poultry_hatching.hall, poultry_hatching.period, str(create_date), str(date), poultry_hatching.poultry.killing_ave_age, str(predicate_date), poultry_hatching.chicken_breed, age, poultry_hatching.quantity, poultry_hatching.increase_quantity, poultry_hatching.losses, poultry_hatching.direct_losses, poultry_hatching.total_losses, chain_company, # poultry_hatching.poultry.city_operator, # # # vet_farm_name, vet_farm_mobile, poultry_hatching.poultry.system_code, poultry_hatching.killed_quantity, poultry_hatching.total_killed_weight, poultry_hatching.left_over, f'{int(poultry_hatching.left_over * 100 / poultry_hatching.quantity)}%', left_over_ninty_percent, poultry_hatching.total_commitment, poultry_hatching.governmental_quantity, poultry_hatching.governmental_killed_quantity, poultry_hatching.total_free_commitment_quantity, poultry_hatching.free_quantity, poultry_hatching.free_killed_quantity, str(poultry_hatching.total_average_killed_weight), creator, poultry_hatching.poultry.epidemiological_code if poultry_hatching.poultry.epidemiological_code else '-', poultry_hatching.out_province_killed_quantity if poultry_hatching.out_province_killed_quantity > 0 else '-', poultry_hatching.out_province_killed_weight if poultry_hatching.out_province_killed_weight > 0 else '-', len(wothout_bar), province_kill_requests_quantity_wothout_bar, province_kill_requests_weight_wothout_bar, poultry_hatching.samasat_discharge_percentage, active_kill, count_of_request, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, poultry_hatching.chain_killed_quantity, poultry_hatching.chain_killed_weight, poultry_hatching.export_killed_quantity, poultry_hatching.export_killed_weight, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), poultry_hatching.violation_reporter if poultry_hatching.violation_reporter else '-', poultry_hatching.violation_report if poultry_hatching.violation_report else '-', str(violation_report_date), ] m += 1 create_value(worksheet, list1, l + 1, 1, border_style='thin', different_cell=1, different_value='متخلف' + ' ' + '(وجود فارم فعال دیگر)') min_list = sorted(min_list) all_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('quantity')).get( 'total_quantity', 0) all_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('losses')).get( 'total_quantity', 0) all_direct_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('direct_losses')).get( 'total_quantity', 0) all_total_losses = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_losses')).get( 'total_quantity', 0) all_killed_quantity = filtered_poultry_hatch.aggregate(total_quantity=Sum('killed_quantity')).get( 'total_quantity', 0) all_left_over = filtered_poultry_hatch.aggregate(total_quantity=Sum('left_over')).get( 'total_quantity', 0) all_total_commitment = filtered_poultry_hatch.aggregate(total_quantity=Sum('total_commitment')).get( 'total_quantity', 0) all_governmental_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_killed_quantity')).get( 'total_quantity', 0) all_free_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_killed_quantity')).get( 'total_quantity', 0) all_total_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_killed_weight')).get( 'total_quantity', 0) all_governmental_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('governmental_quantity')).get( 'total_quantity', 0) all_free_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('free_quantity')).get( 'total_quantity', 0) all_out_province_killed_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_quantity')).get( 'total_quantity', 0) all_out_province_killed_weight = filtered_poultry_hatch.aggregate( total_quantity=Sum('out_province_killed_weight')).get( 'total_quantity', 0) all_increase_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('increase_quantity')).get( 'total_quantity', 0) all_total_free_commitment_quantity = filtered_poultry_hatch.aggregate( total_quantity=Sum('total_free_commitment_quantity')).get( 'total_quantity', 0) kill_house_requests = KillHouseRequest.objects.filter(trash=False, province_request__poultry_request__hatching__in=filtered_poultry_hatch) first_quantity = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity') or 0 first_weight = kill_house_requests.aggregate(total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 ware_house_bars = kill_house_requests.filter(ware_house_confirmation=True) ware_house_bars_quantity = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_quantity'))[ 'total'] or 0 ware_house_bars_weight = ware_house_bars.aggregate(total=Sum('ware_house_accepted_real_weight'))[ 'total'] or 0 ware_house_bars_weight_lose = ware_house_bars.aggregate(total=Sum('weight_loss'))[ 'total'] or 0 bar_complete_with_kill_house = kill_house_requests.filter( assignment_state_archive='True') accepted_real_wight_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_weight')).get( 'total_quantity') or 0 accepted_real_quantity_final = bar_complete_with_kill_house.aggregate( total_quantity=Sum('accepted_real_quantity')).get( 'total_quantity', 0) or 0 chain_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_weight')).get( 'total_quantity', 0) or 0 chain_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('chain_killed_quantity')).get( 'total_quantity', 0) or 0 extra_killed_quantity_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_quantity')).get( 'total_quantity', 0) or 0 export_killed_weight_final = filtered_poultry_hatch.aggregate( total_quantity=Sum('export_killed_weight')).get( 'total_quantity', 0) or 0 value_header_list = [ len(filtered_poultry_hatch), all_chain_company, all_poultry_hatching_quantity, all_losses, all_direct_losses, all_total_losses, all_poultry_hatching_killed_quantity, all_total_killed_weight, all_poultry_hatching_left_over, all_left_over_ninty_percent, min_list[0] if len(min_list) > 0 else '-', min_list[len(min_list) - 1] if len(min_list) > 0 else '-', all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_free_quantity, all_free_killed_quantity, all_out_province_killed_quantity, all_out_province_killed_weight, violation, len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0) ] create_value(worksheet, value_header_list, 3, 5) list2 = [ 'مجموع==>', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', all_quantity, all_increase_quantity, all_losses, all_direct_losses, all_total_losses, '', '', '', '', all_killed_quantity, all_total_killed_weight, all_left_over, '', all_left_over_ninty_percent, all_total_commitment, all_governmental_quantity, all_governmental_killed_quantity, all_total_free_commitment_quantity, all_free_quantity, all_free_killed_quantity, '', '', '', all_out_province_killed_quantity, all_out_province_killed_weight, '', '', '', '', '', '', len(kill_house_requests), first_quantity, first_weight, len(bar_complete_with_kill_house), accepted_real_quantity_final, accepted_real_wight_final, chain_killed_quantity_final, chain_killed_weight_final, extra_killed_quantity_final, export_killed_weight_final, len(ware_house_bars), ware_house_bars_quantity, ware_house_bars_weight, str(round(ware_house_bars_weight_lose / len( ware_house_bars), 2) if ware_house_bars else 0), '', '', '', ] create_value(worksheet, list2, l + 3, 1, color='green') workbook.save(output) output.seek(0) response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ 'Content-Disposition'] = f'attachment; filename="پایش کلی اطلاعاتی مرغداران با بازه سنی.xlsx"'.encode( 'utf-8') response.write(output.getvalue()) return response