2799 lines
115 KiB
Python
2799 lines
115 KiB
Python
# import datetime
|
||
import datetime
|
||
from io import BytesIO
|
||
|
||
import openpyxl
|
||
import requests
|
||
from django.db.models import Sum, Count, Q, F
|
||
from django.http import HttpResponse
|
||
from django.views.decorators.csrf import csrf_exempt
|
||
from openpyxl import Workbook
|
||
from openpyxl.styles import Alignment
|
||
from rest_framework.decorators import api_view, permission_classes
|
||
from rest_framework.permissions import AllowAny
|
||
|
||
from app.filtersets import TransportingDetailFilterSet, KillHouseFilterSet, HatchingsFilterSet, GuildsFilterSet, \
|
||
TransportCarcassDetailFilterSet, AllProductsTransportFilterSet
|
||
from app.helper_excel import create_header, create_header_freez, shamsi_date, excel_description, create_value, \
|
||
convert_str_to_date
|
||
from app.models import TransportingDetail, KillHouse, Hatching, TransportCarcassDetail, Guilds, AllProductsTransport
|
||
from app.serializers import TransportingDetailSerializer, HatchingDetailSerializer, \
|
||
StewardForTransportCarcassSerializer, KillHouseForTransportCarcassSerializer, TransportCarcassDetailSerializer, \
|
||
GuildsForTransportCarcassSerializer, AllProductsTransportSerializer, AllProductsTransportCustomSerializer
|
||
from helpers import build_query
|
||
from app.helper import get_hatching_permit_code
|
||
|
||
def transporting_detail_excel(request):
|
||
filterset_class = TransportingDetailFilterSet
|
||
filters = {}
|
||
PartIdCode = request.GET['PartIdCode']
|
||
RequestCode = request.GET.get('RequestCode')
|
||
date1 = request.GET.get('date1')
|
||
date2 = request.GET.get('date2')
|
||
city = request.GET.get('city')
|
||
province = request.GET.get('province')
|
||
if province == 'undefined':
|
||
province = None
|
||
if date1 and date2:
|
||
date1 = datetime.datetime.strptime(str(request.GET['date1']), '%Y-%m-%d').date()
|
||
|
||
date2 = datetime.datetime.strptime(str(request.GET['date2']), '%Y-%m-%d').date()
|
||
filters['Date__date__gte'] = date1
|
||
filters['Date__date__lte'] = date2
|
||
if city:
|
||
filters['City__icontains'] = city
|
||
|
||
if province:
|
||
filters['Province__icontains'] = province
|
||
|
||
if RequestCode:
|
||
filters['hatching__RequestCode'] = RequestCode
|
||
|
||
search = request.GET.get('search')
|
||
value = request.GET.get('value')
|
||
transports = TransportingDetail.objects.filter(**filters, DesPartIdCode=PartIdCode).order_by("-Date")
|
||
|
||
if search:
|
||
if search != 'undefined' and search.strip():
|
||
transports = transports.filter(
|
||
build_query(filterset_class.Meta.fields, value)
|
||
)
|
||
|
||
serializer = TransportingDetailSerializer(transports, many=True).data
|
||
|
||
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')
|
||
|
||
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()
|
||
from_date_1 = shamsi_date(date1)
|
||
to_date_1 = shamsi_date(date2)
|
||
worksheet['A3'] = f'از تاریخ:({from_date_1}) تا تاریخ:({to_date_1})'
|
||
if serializer:
|
||
name = serializer[1]['DesUnitName']
|
||
else:
|
||
name = ''
|
||
excel_description(worksheet, 'A5', f'گزارش اطلاعات بار استان کشتارگاه {name}', color='red', row2='C5')
|
||
|
||
header_list2 = [
|
||
'تعداد بار',
|
||
'حجم بار',
|
||
'میانگین سن کشتار',
|
||
'میانگین سن کشتار',
|
||
'تعداد بار داخل استان',
|
||
'حجم بار داخل استان',
|
||
'درصد داخل استان',
|
||
'تعداد بار خارج استان',
|
||
'حجم بار خارج استان',
|
||
'درصد خارج استان',
|
||
|
||
]
|
||
create_header(worksheet, header_list2, 5, 2, height=20.8, border_style='thin')
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
l = 5
|
||
m = 1
|
||
if serializer:
|
||
for data in serializer:
|
||
date = datetime.datetime.strptime(str(data['Date']), '%Y-%m-%dT%H:%M:%SZ')
|
||
destination = 'داخل استان' if data['Out'] == False else 'خارج استان'
|
||
list1 = [
|
||
m,
|
||
data.get('TrackingCode', '-'),
|
||
str(shamsi_date(date, in_value=True)),
|
||
data.get('DesUnitName', '-'),
|
||
data.get('DesPartIdCode', '-'),
|
||
data.get('Province', '-'),
|
||
data.get('City', '-'),
|
||
data.get('GoodAmount', '-'),
|
||
data.get('TrackingStatusDescription', '-'),
|
||
destination,
|
||
data.get('SourceUnitName', '-'),
|
||
(data['hatching'] or {}).get('hatching', {}).get('PartIdCode', '-'),
|
||
(data['hatching'] or {}).get('hatching', {}).get('RequestCode', '-'),
|
||
(data['hatching'] or {}).get('hatching', {}).get('poultry', {}).get('Province', '-'),
|
||
(data['hatching'] or {}).get('hatching', {}).get('poultry', {}).get('City', '-'),
|
||
(data['hatching'] or {}).get('Age', '-'),
|
||
(data['hatching'] or {}).get('hatching', {}).get('PedigreeName', '-'),
|
||
|
||
]
|
||
m += 1
|
||
|
||
l += 1
|
||
create_value(worksheet, list1, l + 1, 1)
|
||
header_data = requests.get(
|
||
f'https://rsibackend.rasadyar.com/app/transporting-dashboard/?search=&value={value}&province={province}&PartIdCode={PartIdCode}').json()
|
||
if header_data:
|
||
value_header_list2 = [
|
||
header_data['bar_count'],
|
||
header_data['bar_quantity'],
|
||
header_data['input_bar_count'],
|
||
int(header_data['total_bar_killing_age']),
|
||
header_data['input_bar_count'],
|
||
header_data['input_bar_quantity'],
|
||
str(header_data['input_bar_percent']),
|
||
header_data['output_bar'],
|
||
header_data['output_bar_quantity'],
|
||
str(header_data['output_bar_percent']),
|
||
|
||
]
|
||
|
||
create_value(worksheet, value_header_list2, 3, 5, border_style='thin')
|
||
total_quantity = sum(
|
||
item['GoodAmount'] for item in serializer)
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
total_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="گزارش اطلاعات بار کشتارگاه.xlsx"'.encode(
|
||
'utf-8')
|
||
response.write(output.getvalue())
|
||
return response
|
||
|
||
|
||
def total_killhouse_excel(request):
|
||
filterset_class = KillHouseFilterSet
|
||
search = request.GET.get('search')
|
||
date1 = request.GET.get('date1')
|
||
date2 = request.GET.get('date2')
|
||
filters = {"trash": False}
|
||
if province := request.GET.get('province'):
|
||
filters['Province'] = province
|
||
|
||
if city := request.GET.get('city'):
|
||
filters['City'] = city
|
||
|
||
if kill_houses_name := request.GET.get('name'):
|
||
filters['UnitName'] = kill_houses_name
|
||
|
||
kill_houses = KillHouse.objects.filter(**filters).order_by('id')
|
||
|
||
if search:
|
||
if search != 'undefined' and search.strip():
|
||
kill_houses = kill_houses.filter(
|
||
build_query(filterset_class.Meta.fields, search)
|
||
)
|
||
|
||
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')
|
||
|
||
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()
|
||
from_date_1 = shamsi_date(date1)
|
||
to_date_1 = shamsi_date(date2)
|
||
worksheet['A3'] = f'از تاریخ:({from_date_1}) تا تاریخ:({to_date_1})'
|
||
excel_description(worksheet, 'A5', f'پایش کشتارگاه ها', color='red', row2='C5')
|
||
|
||
header_list2 = [
|
||
'تعداد کشتارگاه ها',
|
||
'تعداد بار',
|
||
'حجم بار',
|
||
'تعداد بار داخل استان',
|
||
'حجم بار داخل استان',
|
||
'درصد بار داخل استان',
|
||
'تعداد بار خارج استان',
|
||
'حجم بار خارج استان',
|
||
'درصد بار خارج استان',
|
||
|
||
]
|
||
create_header(worksheet, header_list2, 5, 2, height=20.8, border_style='thin')
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
l = 5
|
||
m = 1
|
||
if kill_houses:
|
||
part_id_codes = kill_houses.values_list('PartIdCode', flat=True)
|
||
all_bars = TransportingDetail.objects.filter(DesPartIdCode__in=part_id_codes,trash=False).only('Out', "GoodAmount")
|
||
|
||
all_products_transport = AllProductsTransport.objects.filter(
|
||
jihadi_destination__in=part_id_codes,
|
||
trash=False,
|
||
product='مرغ زنده -جهت كشتار'
|
||
).only('out', 'quantity')
|
||
|
||
if date1:
|
||
date1 = datetime.datetime.strptime(str(date1), '%Y-%m-%d').date()
|
||
date2 = datetime.datetime.strptime(str(date2), '%Y-%m-%d').date()
|
||
all_bars = all_bars.filter(Date__date__gte=date1, Date__date__lte=date2)
|
||
all_products_transport = all_products_transport.filter(
|
||
Q(
|
||
date__gte=date1,
|
||
date__lte=date2,
|
||
date__isnull=False
|
||
) |
|
||
Q(
|
||
unloading_date__gte=date1,
|
||
unloading_date__lte=date2,
|
||
date__isnull=True
|
||
)
|
||
)
|
||
|
||
for kill_house in kill_houses:
|
||
bars = all_bars.filter(DesPartIdCode=kill_house.PartIdCode, trash=False).only('GoodAmount',
|
||
'Out')
|
||
|
||
aggregation_bars = bars.aggregate(
|
||
total=Sum('GoodAmount'),
|
||
input_total=Sum('GoodAmount', filter=Q(Out=False)),
|
||
output_total=Sum('GoodAmount', filter=Q(Out=True)),
|
||
input_count=Count('id', filter=Q(Out=False)),
|
||
output_count=Count('id', filter=Q(Out=True)),
|
||
total_count=Count('id')
|
||
)
|
||
|
||
all_products_for_killhouse = all_products_transport.filter(jihadi_destination=kill_house.PartIdCode)
|
||
aggregation_all_products = all_products_for_killhouse.aggregate(
|
||
total=Sum('quantity'),
|
||
input_total=Sum('quantity', filter=Q(out=False)),
|
||
output_total=Sum('quantity', filter=Q(out=True)),
|
||
input_count=Count('id', filter=Q(out=False)),
|
||
output_count=Count('id', filter=Q(out=True)),
|
||
total_count=Count('id')
|
||
)
|
||
|
||
total_count = (aggregation_bars['total_count'] or 0) + (aggregation_all_products['total_count'] or 0)
|
||
total_bars_quantity = (aggregation_bars['total'] or 0) + (aggregation_all_products['total'] or 0)
|
||
total_input_bars_quantity = (aggregation_bars['input_total'] or 0) + (aggregation_all_products['input_total'] or 0)
|
||
total_output_bars_quantity = (aggregation_bars['output_total'] or 0) + (aggregation_all_products['output_total'] or 0)
|
||
input_bars_count = (aggregation_bars['input_count'] or 0) + (aggregation_all_products['input_count'] or 0)
|
||
output_bars_count = (aggregation_bars['output_count'] or 0) + (aggregation_all_products['output_count'] or 0)
|
||
|
||
if total_count > 0:
|
||
total_input_bars_percent = round((input_bars_count / total_count) * 100, 1)
|
||
total_output_bars_percent = round((output_bars_count / total_count) * 100, 1)
|
||
else:
|
||
total_input_bars_percent = 0
|
||
total_output_bars_percent = 0
|
||
|
||
list1 = [
|
||
m,
|
||
kill_house.UnitName,
|
||
kill_house.PartIdCode,
|
||
kill_house.Province,
|
||
kill_house.City,
|
||
total_count,
|
||
total_bars_quantity,
|
||
input_bars_count,
|
||
total_input_bars_quantity,
|
||
total_input_bars_percent,
|
||
output_bars_count,
|
||
total_output_bars_quantity,
|
||
total_output_bars_percent,
|
||
|
||
]
|
||
m += 1
|
||
|
||
l += 1
|
||
create_value(worksheet, list1, l + 1, 1)
|
||
|
||
aggregation1_bars = all_bars.aggregate(
|
||
total=Sum('GoodAmount'),
|
||
input_total=Sum('GoodAmount', filter=Q(Out=False)),
|
||
output_total=Sum('GoodAmount', filter=Q(Out=True)),
|
||
input_count=Count('id', filter=Q(Out=False)),
|
||
output_count=Count('id', filter=Q(Out=True)),
|
||
total_count=Count('id')
|
||
)
|
||
|
||
aggregation1_all_products = all_products_transport.aggregate(
|
||
total=Sum('quantity'),
|
||
input_total=Sum('quantity', filter=Q(out=False)),
|
||
output_total=Sum('quantity', filter=Q(out=True)),
|
||
input_count=Count('id', filter=Q(out=False)),
|
||
output_count=Count('id', filter=Q(out=True)),
|
||
total_count=Count('id')
|
||
)
|
||
|
||
total_count = (aggregation1_bars['total_count'] or 0) + (aggregation1_all_products['total_count'] or 0)
|
||
total_bars_quantity = (aggregation1_bars['total'] or 0) + (aggregation1_all_products['total'] or 0)
|
||
total_input_bars_quantity = (aggregation1_bars['input_total'] or 0) + (aggregation1_all_products['input_total'] or 0)
|
||
total_output_bars_quantity = (aggregation1_bars['output_total'] or 0) + (aggregation1_all_products['output_total'] or 0)
|
||
input_bars_count = (aggregation1_bars['input_count'] or 0) + (aggregation1_all_products['input_count'] or 0)
|
||
output_bars_count = (aggregation1_bars['output_count'] or 0) + (aggregation1_all_products['output_count'] or 0)
|
||
|
||
if total_count > 0:
|
||
total_input_bars_percent = round((input_bars_count / total_count) * 100, 1)
|
||
total_output_bars_percent = round((output_bars_count / total_count) * 100, 1)
|
||
else:
|
||
total_input_bars_percent = 0
|
||
total_output_bars_percent = 0
|
||
value_header_list2 = [
|
||
kill_houses.count(),
|
||
total_count,
|
||
total_bars_quantity,
|
||
input_bars_count,
|
||
total_input_bars_quantity,
|
||
total_input_bars_percent,
|
||
output_bars_count,
|
||
total_output_bars_quantity,
|
||
total_output_bars_percent,
|
||
|
||
]
|
||
|
||
create_value(worksheet, value_header_list2, 3, 5, border_style='thin')
|
||
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
total_count,
|
||
total_bars_quantity,
|
||
input_bars_count,
|
||
total_input_bars_quantity,
|
||
total_input_bars_percent,
|
||
output_bars_count,
|
||
total_output_bars_quantity,
|
||
total_output_bars_percent,
|
||
|
||
]
|
||
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_excel(request):
|
||
filterset_class = HatchingsFilterSet
|
||
output = BytesIO()
|
||
workbook = Workbook()
|
||
worksheet = workbook.active
|
||
workbook.remove(worksheet)
|
||
sheet_list = [
|
||
'فعال',
|
||
'بایگانی'
|
||
]
|
||
header_list = [
|
||
'تعداد دوره جوجه ریزی',
|
||
'حجم کل جوجه ریزی',
|
||
'تلفات',
|
||
'کشتار شده',
|
||
'میانگین سن کشتار',
|
||
'تعداد بار ها',
|
||
'تعداد جوجه ریزی فعال',
|
||
'حجم جوجه ریزی فعال',
|
||
'کمترین سن',
|
||
'بیشترین سن',
|
||
'مانده در سالن',
|
||
]
|
||
|
||
excel_options = [
|
||
'ردیف',
|
||
'استان',
|
||
'شهرستان',
|
||
'نام واحد',
|
||
|
||
'نام مالک',
|
||
'شماره مجوز',
|
||
'شماره گواهی بهداشتی',
|
||
'ظرفیت',
|
||
'تاریخ جوجه ریزی',
|
||
'سن گله',
|
||
'تعداد جوجه ریزی',
|
||
'دوره جوجه ریزی',
|
||
'مجموع تلفات',
|
||
'درصد جوجه ریزی به مجوز',
|
||
'مانده در سالن',
|
||
'میانگین سن کشتار',
|
||
'تعداد بارها',
|
||
'حجم بارها',
|
||
|
||
]
|
||
filters = {}
|
||
system_code = request.GET.get('system_code')
|
||
city = request.GET.get('city')
|
||
province = request.GET.get('province')
|
||
age = request.GET.get('age')
|
||
killing_age = request.GET.get('killing_age')
|
||
date1 = request.GET.get('date1')
|
||
date2 = request.GET.get('date2')
|
||
if date1 and date2:
|
||
date1 = datetime.datetime.strptime(str(request.GET['date1']), '%Y-%m-%d').date()
|
||
|
||
date2 = datetime.datetime.strptime(str(request.GET['date2']), '%Y-%m-%d').date()
|
||
filters['Date__date__gte'] = date1
|
||
filters['Date__date__lte'] = date2
|
||
|
||
if killing_age:
|
||
filters['KillingAve'] = int(killing_age)
|
||
|
||
if age:
|
||
filters['Age__exact'] = age
|
||
if city:
|
||
filters['CityName__icontains'] = city
|
||
if province:
|
||
filters['ProvinceName__icontains'] = province
|
||
hatchings = Hatching.objects.filter(**filters, SystemCode=system_code)
|
||
|
||
# hatchings = hatchings.filter(Age__gt=70)
|
||
|
||
header = requests.get(f'https://rsibackend.rasadyar.com/app/hatchings-dashboard/?system_code={system_code}').json()
|
||
value_header_list = [
|
||
header.get('total_hatching_count', ""), # تعداد دوره جوجه ریزی
|
||
header.get('total_hatching_quantity', ""), # حجم کل جوجه ریزی
|
||
header.get('total_hatching_evacuation', ""), # تلفات
|
||
header.get('total_hatching_killing_quantity', ""), # کشتار شده
|
||
header.get('total_hatching_killing_age', ""), # میانگین سن کشتار
|
||
header.get('total_hatching_bars', ""), # تعداد بار ها
|
||
header.get('total_active_hatching_count', ""), # تعداد جوجه ریزی فعال
|
||
header.get('total_active_hatching_quantity', ""), # حجم جوجه ریزی فعال
|
||
header.get('least_age', ""), # کمترین سن
|
||
header.get('most_age', ""), # بیشترین سن
|
||
header.get('total_hatching_left_over', "") # مانده در سالن
|
||
]
|
||
|
||
for sheet_name in sheet_list:
|
||
worksheet = workbook.create_sheet(sheet_name)
|
||
if sheet_name == 'فعال':
|
||
worksheet.sheet_view.rightToLeft = True
|
||
worksheet.insert_rows(1)
|
||
cell = worksheet.cell(row=1, column=1)
|
||
cell.alignment = Alignment(horizontal='center', vertical='center')
|
||
|
||
create_header(worksheet, header_list, 4, 2, height=25, width=25, border_style='thin', color='C00000')
|
||
hatchings_active = hatchings.filter(Age__lte=70)
|
||
serializer = HatchingDetailSerializer(hatchings_active, many=True).data
|
||
if serializer:
|
||
name = serializer[0]['poultry']['UnitName']
|
||
else:
|
||
name = ''
|
||
excel_description(worksheet, 'A1', f'پایش فارم فعال {name}', size=11, color='red', row2='C1')
|
||
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
|
||
l = 7
|
||
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
|
||
for data in serializer:
|
||
poultry = data.get('poultry', {})
|
||
info = data.get('info', {})
|
||
date = datetime.datetime.strptime(str(data['Date']), '%Y-%m-%dT%H:%M:%SZ')
|
||
list1 = [
|
||
m, # ردیف
|
||
poultry.get('Province', ''), # استان
|
||
poultry.get('City', ''), # شهرستان
|
||
poultry.get('UnitName', ''), # نام واحد
|
||
f"{poultry.get('FirstName', '')} {poultry.get('LastName', '')}".strip(), # نام مالک
|
||
data.get('RequestCode', ''), # شماره مجوز
|
||
data.get('CertId', ''), # شماره گواهی بهداشتی
|
||
data.get('CapacityFemale', ''), # ظرفیت
|
||
str(shamsi_date(date, in_value=True)),
|
||
data.get('Age', ''), # سن گله
|
||
data.get('ChickCountSum', ''), # تعداد جوجه ریزی
|
||
data.get('Period', ''), # دوره جوجه ریزی
|
||
data.get('Evacuation', ''), # مجموع تلفات
|
||
info.get('percent_hatching_license', ''), # درصد جوجه ریزی به مجوز
|
||
data.get('LeftOver', ''), # مانده در سالن
|
||
info.get('average_slaughter_age', ''), # میانگین سن کشتار
|
||
info.get('number_loads', ''), # تعداد بارها
|
||
info.get('load_volume', ''), # حجم بارها
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
if serializer:
|
||
total_capacity = sum(item.get('CapacityFemale', 0) for item in serializer)
|
||
|
||
total_chick_count = sum(item.get('ChickCountSum', 0) for item in serializer)
|
||
|
||
total_evacuation = sum(item.get('Evacuation', 0) for item in serializer)
|
||
|
||
total_left_over = sum(item.get('LeftOver', 0) for item in serializer)
|
||
|
||
total_load_volume = sum(item.get('info', {}).get('load_volume', 0) for item in serializer)
|
||
|
||
total_number_loads = sum(item.get('info', {}).get('number_loads', 0) for item in serializer)
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
total_capacity,
|
||
'',
|
||
'',
|
||
total_chick_count,
|
||
'',
|
||
total_evacuation,
|
||
'',
|
||
total_left_over,
|
||
'',
|
||
total_number_loads,
|
||
total_load_volume,
|
||
|
||
]
|
||
create_value(worksheet, list2, l + 3, 1, color='green')
|
||
else:
|
||
worksheet.sheet_view.rightToLeft = True
|
||
worksheet.insert_rows(1)
|
||
cell = worksheet.cell(row=1, column=1)
|
||
cell.alignment = Alignment(horizontal='center', vertical='center')
|
||
|
||
create_header(worksheet, header_list, 4, 2, height=25, width=25, border_style='thin', color='C00000')
|
||
hatchings1 = hatchings.filter(Age__gte=70)
|
||
serializer1 = HatchingDetailSerializer(hatchings1, many=True).data
|
||
if serializer1:
|
||
name = serializer1[0]['poultry']['UnitName']
|
||
else:
|
||
name = ''
|
||
excel_description(worksheet, 'A1', f'پایش فارم بایگانی {name}', size=11, color='red', row2='C1')
|
||
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
|
||
l = 7
|
||
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
|
||
for data in serializer1:
|
||
poultry = data.get('poultry', {})
|
||
info = data.get('info', {})
|
||
|
||
date = datetime.datetime.strptime(str(data['Date']), '%Y-%m-%dT%H:%M:%SZ')
|
||
list1 = [
|
||
m, # ردیف
|
||
poultry.get('Province', ''), # استان
|
||
poultry.get('City', ''), # شهرستان
|
||
poultry.get('UnitName', ''), # نام واحد
|
||
f"{poultry.get('FirstName', '')} {poultry.get('LastName', '')}".strip(), # نام مالک
|
||
data.get('RequestCode', ''), # شماره مجوز
|
||
data.get('CertId', ''), # شماره گواهی بهداشتی
|
||
data.get('CapacityFemale', ''), # ظرفیت
|
||
str(shamsi_date(date, in_value=True)),
|
||
data.get('Age', ''), # سن گله
|
||
data.get('ChickCountSum', ''), # تعداد جوجه ریزی
|
||
data.get('Period', ''), # دوره جوجه ریزی
|
||
data.get('Evacuation', ''), # مجموع تلفات
|
||
info.get('percent_hatching_license', ''), # درصد جوجه ریزی به مجوز
|
||
data.get('LeftOver', ''), # مانده در سالن
|
||
info.get('average_slaughter_age', ''), # میانگین سن کشتار
|
||
info.get('number_loads', ''), # تعداد بارها
|
||
info.get('load_volume', ''), # حجم بارها
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
if serializer1:
|
||
total_capacity = sum(item.get('CapacityFemale', 0) for item in serializer1)
|
||
|
||
total_chick_count = sum(item.get('ChickCountSum', 0) for item in serializer1)
|
||
|
||
total_evacuation = sum(item.get('Evacuation', 0) for item in serializer1)
|
||
|
||
total_left_over = sum(item.get('LeftOver', 0) for item in serializer1)
|
||
|
||
total_load_volume = sum(item.get('info', {}).get('load_volume', 0) for item in serializer1)
|
||
|
||
total_number_loads = sum(item.get('info', {}).get('number_loads', 0) for item in serializer1)
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
total_capacity,
|
||
'',
|
||
'',
|
||
total_chick_count,
|
||
'',
|
||
total_evacuation,
|
||
'',
|
||
total_left_over,
|
||
'',
|
||
total_number_loads,
|
||
total_load_volume,
|
||
|
||
]
|
||
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 all_hatching_excel(request):
|
||
filterset_class = HatchingsFilterSet
|
||
output = BytesIO()
|
||
workbook = Workbook()
|
||
worksheet = workbook.active
|
||
workbook.remove(worksheet)
|
||
sheet_list = [
|
||
'فعال',
|
||
'بایگانی'
|
||
]
|
||
header_list = [
|
||
"تعداد دوره جوجه ریزی",
|
||
"حجم کل جوجه ریزی",
|
||
"تلفات",
|
||
"درصد تلفات",
|
||
"کشتار شده",
|
||
"درصد کشتار شده",
|
||
"میانگین سن کشتار",
|
||
"تعداد بارها",
|
||
"کمترین سن",
|
||
"بیشترین سن",
|
||
"مانده در سالن",
|
||
"درصد مانده در سالن نسبت به جوجه ریزی"
|
||
]
|
||
|
||
header_list2 = [
|
||
"تعداد دوره جوجه ریزی",
|
||
"حجم کل جوجه ریزی",
|
||
"تلفات",
|
||
"درصد تلفات",
|
||
"کشتار شده",
|
||
"درصد کشتار شده",
|
||
"میانگین سن کشتار",
|
||
"تعداد بارها",
|
||
"حجم جوجه ریزی فعال",
|
||
"کمترین سن",
|
||
"بیشترین سن",
|
||
"مانده در سالن",
|
||
"درصد مانده در سالن",
|
||
"مانده در سالن آماده به کشتار",
|
||
"درصد مانده در سالن آماده به کشتار",
|
||
]
|
||
|
||
excel_options = [
|
||
'ردیف',
|
||
'استان',
|
||
'شهرستان',
|
||
'نام واحد',
|
||
|
||
'نام مالک',
|
||
'شماره مجوز',
|
||
'شماره گواهی بهداشتی',
|
||
'ظرفیت',
|
||
'تاریخ جوجه ریزی',
|
||
'سن گله',
|
||
'تعداد جوجه ریزی',
|
||
'دوره جوجه ریزی',
|
||
'مجموع تلفات',
|
||
'درصد جوجه ریزی به مجوز',
|
||
'مانده در سالن',
|
||
'میانگین سن کشتار',
|
||
'تعداد بارها',
|
||
'حجم بارها',
|
||
|
||
]
|
||
filters = {}
|
||
city = request.GET.get('city')
|
||
province = request.GET.get('province')
|
||
age = request.GET.get('age')
|
||
killing_age = request.GET.get('killing_age')
|
||
date1 = request.GET.get('date1')
|
||
date2 = request.GET.get('date2')
|
||
filter_and_search='?'
|
||
if date1 and date2:
|
||
date1 = datetime.datetime.strptime(str(request.GET['date1']), '%Y-%m-%d').date()
|
||
|
||
date2 = datetime.datetime.strptime(str(request.GET['date2']), '%Y-%m-%d').date()
|
||
filters['Date__date__gte'] = date1
|
||
filters['Date__date__lte'] = date2
|
||
filter_and_search+=f'date1={date1}&date2={date2}'
|
||
|
||
if killing_age:
|
||
filters['KillingAve'] = int(killing_age)
|
||
|
||
if age:
|
||
filters['Age__exact'] = age
|
||
if city:
|
||
filters['CityName__icontains'] = city
|
||
if province:
|
||
filters['ProvinceName__icontains'] = province
|
||
filter_and_search+=f'&province={province}'
|
||
hatchings = Hatching.objects.filter(**filters)
|
||
|
||
# hatchings = hatchings.filter(Age__gt=70)
|
||
|
||
header = requests.get(f'https://rsibackend.rasadyar.com/app/hatchings-dashboard/{filter_and_search}').json()
|
||
value_header_list = [
|
||
header.get('total_hatching_count', 0),
|
||
header.get('total_hatching_quantity', 0),
|
||
header.get('total_hatching_evacuation', 0),
|
||
header.get('total_hatching_evacuation', 0),
|
||
header.get('total_hatching_killing_quantity', 0),
|
||
header.get('total_hatching_evacuation_percent', 0),
|
||
header.get('total_hatching_killing_age', 0),
|
||
header.get('total_hatching_bars', 0),
|
||
header.get('least_age', 0),
|
||
header.get('most_age', 0),
|
||
header.get('total_hatching_left_over', 0),
|
||
header.get('total_hatching_left_over_percent', 0),
|
||
]
|
||
|
||
value_header_list2 = [
|
||
header.get('total_active_hatching_count', 0),
|
||
header.get('total_hatching_quantity', 0),
|
||
header.get('total_active_hatching_evacuation', 0),
|
||
header.get('total_active_hatching_evacuation_percent', 0),
|
||
header.get('total_active_hatching_killing_quantity', 0),
|
||
header.get('total_active_hatching_killing_quantity_percent', 0),
|
||
header.get('total_active_hatching_killing_age', 0),
|
||
header.get('total_active_hatching_bars', 0),
|
||
header.get('total_active_hatching_quantity', 0),
|
||
header.get('least_age', 0),
|
||
header.get('most_age', 0),
|
||
header.get('total_active_hatching_left_over', 0),
|
||
header.get('total_active_hatching_left_over_percent', 0),
|
||
header.get('total_ready_active_hatching_left_over', 0),
|
||
header.get('total_ready_hatching_left_over_percent', 0),
|
||
]
|
||
|
||
for sheet_name in sheet_list:
|
||
worksheet = workbook.create_sheet(sheet_name)
|
||
if sheet_name == 'فعال':
|
||
worksheet.sheet_view.rightToLeft = True
|
||
worksheet.insert_rows(1)
|
||
cell = worksheet.cell(row=1, column=1)
|
||
cell.alignment = Alignment(horizontal='center', vertical='center')
|
||
create_header(worksheet, header_list, 4, 2, height=25, width=25, border_style='thin', color='C00000')
|
||
|
||
create_header(worksheet, header_list2, 4, 5, height=25, width=25, border_style='thin', color='C00000')
|
||
|
||
hatchings_active = hatchings.filter(Age__lte=70)
|
||
serializer = HatchingDetailSerializer(hatchings_active, many=True).data
|
||
if date1:
|
||
info = 'از تاریخ {0} تا تاریخ {1}'.format(shamsi_date(date1), shamsi_date(date2))
|
||
else:
|
||
info = ''
|
||
excel_description(worksheet, 'A1', f'پایش فارم فعال {info}', size=11, color='red', row2='C2')
|
||
excel_description(worksheet, 'E1', f'خلاصه اطلاعات کل جوجه ریزی ها', size=11, color='red', row2='G1')
|
||
excel_description(worksheet, 'E4', f'خلاصه اطلاعات جوجه ریزی های فعال', size=11, color='red', row2='G4')
|
||
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 8, 9, 20)
|
||
|
||
l = 9
|
||
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
create_value(worksheet, value_header_list2, 6, 4, border_style='thin')
|
||
|
||
for data in serializer:
|
||
poultry = data.get('poultry') or {}
|
||
info = data.get('info') or {}
|
||
date = datetime.datetime.strptime(str(data['Date']), '%Y-%m-%dT%H:%M:%SZ')
|
||
list1 = [
|
||
m,
|
||
poultry.get('Province') or '',
|
||
poultry.get('City') or '',
|
||
poultry.get('UnitName') or '',
|
||
f"{poultry.get('FirstName') or ''} {poultry.get('LastName') or ''}".strip(),
|
||
data.get('RequestCode') or '',
|
||
data.get('CertId') or '',
|
||
data.get('CapacityFemale') or '',
|
||
str(shamsi_date(date, in_value=True)),
|
||
data.get('Age') or '',
|
||
data.get('ChickCountSum') or '',
|
||
data.get('Period') or '',
|
||
data.get('Evacuation') or '',
|
||
info.get('percent_hatching_license') or '',
|
||
data.get('LeftOver') or '',
|
||
info.get('average_slaughter_age') or '',
|
||
info.get('number_loads') or '',
|
||
info.get('load_volume') or '',
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
if serializer:
|
||
total_capacity = sum(item.get('CapacityFemale', 0) for item in serializer)
|
||
|
||
total_chick_count = sum(item.get('ChickCountSum', 0) for item in serializer)
|
||
|
||
total_evacuation = sum(item.get('Evacuation', 0) for item in serializer)
|
||
|
||
total_left_over = sum(item.get('LeftOver', 0) for item in serializer)
|
||
|
||
total_load_volume = sum(item.get('info', {}).get('load_volume', 0) for item in serializer)
|
||
|
||
total_number_loads = sum(item.get('info', {}).get('number_loads', 0) for item in serializer)
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
total_capacity,
|
||
'',
|
||
'',
|
||
total_chick_count,
|
||
'',
|
||
total_evacuation,
|
||
'',
|
||
total_left_over,
|
||
'',
|
||
total_number_loads,
|
||
total_load_volume,
|
||
|
||
]
|
||
create_value(worksheet, list2, l + 3, 1, color='green')
|
||
else:
|
||
worksheet.sheet_view.rightToLeft = True
|
||
worksheet.insert_rows(1)
|
||
cell = worksheet.cell(row=1, column=1)
|
||
cell.alignment = Alignment(horizontal='center', vertical='center')
|
||
|
||
create_header(worksheet, header_list, 4, 2, height=25, width=25, border_style='thin', color='C00000')
|
||
|
||
hatchings1 = hatchings.filter(Age__gte=70)
|
||
serializer1 = HatchingDetailSerializer(hatchings1, many=True).data
|
||
if date1:
|
||
info = 'از تاریخ {0} تا تاریخ {1}'.format(shamsi_date(date1), shamsi_date(date2))
|
||
else:
|
||
info = ''
|
||
excel_description(worksheet, 'A1', f'پایش فارم بایگانی {info}', size=11, color='red', row2='C2')
|
||
excel_description(worksheet, 'E1', f'خلاصه اطلاعات کل جوجه ریزی ها', size=11, color='red', row2='G1')
|
||
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
|
||
l = 7
|
||
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
|
||
for data in serializer1:
|
||
poultry = data.get('poultry') or {}
|
||
info = data.get('info') or {}
|
||
|
||
date = datetime.datetime.strptime(str(data['Date']), '%Y-%m-%dT%H:%M:%SZ')
|
||
list1 = [
|
||
m,
|
||
poultry.get('Province') or '',
|
||
poultry.get('City') or '',
|
||
poultry.get('UnitName') or '',
|
||
f"{poultry.get('FirstName') or ''} {poultry.get('LastName') or ''}".strip(),
|
||
data.get('RequestCode') or '',
|
||
data.get('CertId') or '',
|
||
data.get('CapacityFemale') or '',
|
||
str(shamsi_date(date, in_value=True)),
|
||
data.get('Age') or '',
|
||
data.get('ChickCountSum') or '',
|
||
data.get('Period') or '',
|
||
data.get('Evacuation') or '',
|
||
info.get('percent_hatching_license') or '',
|
||
data.get('LeftOver') or '',
|
||
info.get('average_slaughter_age') or '',
|
||
info.get('number_loads') or '',
|
||
info.get('load_volume') or '',
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
if serializer1:
|
||
total_capacity = sum(item.get('CapacityFemale', 0) for item in serializer1)
|
||
|
||
total_chick_count = sum(item.get('ChickCountSum', 0) for item in serializer1)
|
||
|
||
total_evacuation = sum(item.get('Evacuation', 0) for item in serializer1)
|
||
|
||
total_left_over = sum(item.get('LeftOver', 0) for item in serializer1)
|
||
|
||
total_load_volume = sum(item.get('info', {}).get('load_volume', 0) for item in serializer1)
|
||
|
||
total_number_loads = sum(item.get('info', {}).get('number_loads', 0) for item in serializer1)
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
total_capacity,
|
||
'',
|
||
'',
|
||
total_chick_count,
|
||
'',
|
||
total_evacuation,
|
||
'',
|
||
total_left_over,
|
||
'',
|
||
total_number_loads,
|
||
total_load_volume,
|
||
|
||
]
|
||
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 all_send_different_bar_excel(request):
|
||
filterset_class = TransportingDetailFilterSet
|
||
filters = {}
|
||
PartIdCode = request.GET.get('PartIdCode')
|
||
RequestCode = request.GET.get('RequestCode')
|
||
date1 = request.GET.get('date1')
|
||
date2 = request.GET.get('date2')
|
||
city = request.GET.get('city')
|
||
province = request.GET.get('province')
|
||
if province == 'undefined':
|
||
province = None
|
||
if date1 and date2:
|
||
date1 = datetime.datetime.strptime(str(request.GET['date1']), '%Y-%m-%d').date()
|
||
|
||
date2 = datetime.datetime.strptime(str(request.GET['date2']), '%Y-%m-%d').date()
|
||
filters['Date__date__gte'] = date1
|
||
filters['Date__date__lte'] = date2
|
||
if city:
|
||
filters['City__icontains'] = city
|
||
|
||
if province:
|
||
filters['hatching__poultry__Province__icontains'] = province
|
||
|
||
if PartIdCode:
|
||
filters['DesPartIdCode'] = PartIdCode
|
||
|
||
if RequestCode:
|
||
filters['hatching__RequestCode'] = RequestCode
|
||
|
||
search = request.GET.get('search')
|
||
transports = TransportingDetail.objects.filter(**filters, trash=False).order_by("-Date")
|
||
|
||
query_all_products = AllProductsTransport.objects.filter(
|
||
trash=False,
|
||
product='مرغ زنده -جهت كشتار'
|
||
)
|
||
|
||
|
||
if PartIdCode:
|
||
query_all_products = query_all_products.filter(jihadi_destination=PartIdCode)
|
||
|
||
if RequestCode:
|
||
query_all_products = query_all_products.filter(hatching__RequestCode=RequestCode)
|
||
|
||
if date1 and date2:
|
||
query_all_products = query_all_products.filter(
|
||
date__gte=date1,
|
||
date__lte=date2,
|
||
date__isnull=False
|
||
)
|
||
|
||
if city:
|
||
query_all_products = query_all_products.filter(destination_city__icontains=city)
|
||
|
||
if province:
|
||
query_all_products = query_all_products.filter(destination_province__icontains=province)
|
||
|
||
if search:
|
||
if search != 'undefined' and search.strip():
|
||
transports = transports.filter(
|
||
build_query(filterset_class.Meta.fields, search)
|
||
)
|
||
query_all_products = query_all_products.filter(
|
||
build_query(AllProductsTransportFilterSet.Meta.fields, search)
|
||
)
|
||
|
||
ser_data_transports = TransportingDetailSerializer(transports, many=True).data
|
||
|
||
all_products_list = list(query_all_products)
|
||
ser_data_all_products = []
|
||
for obj in all_products_list:
|
||
serializer = AllProductsTransportSerializer(obj)
|
||
data = serializer.data
|
||
date_value = obj.date if obj.date else obj.unloading_date
|
||
if date_value:
|
||
data['Date'] = datetime.datetime.combine(date_value, datetime.time.min) if isinstance(date_value, datetime.date) else date_value
|
||
else:
|
||
data['Date'] = None
|
||
data['DesPartIdCode'] = obj.jihadi_destination
|
||
data['TrackingCode'] = obj.tracking
|
||
data['GoodAmount'] = obj.quantity
|
||
data['DesUnitName'] = obj.destination
|
||
data['Province'] = obj.destination_province
|
||
data['City'] = obj.destination_city
|
||
data['Out'] = obj.out
|
||
if hasattr(obj, 'hatching') and obj.hatching:
|
||
data['hatching'] = {
|
||
'poultry': {
|
||
'UnitName': getattr(obj.hatching.poultry, 'UnitName', '') if hasattr(obj.hatching, 'poultry') and obj.hatching.poultry else '',
|
||
'PartIdCode': getattr(obj.hatching.poultry, 'PartIdCode', '') if hasattr(obj.hatching, 'poultry') and obj.hatching.poultry else '',
|
||
'Mobile': getattr(obj.hatching.poultry, 'Mobile', '') if hasattr(obj.hatching, 'poultry') and obj.hatching.poultry else '',
|
||
'City': getattr(obj.hatching.poultry, 'City', '') if hasattr(obj.hatching, 'poultry') and obj.hatching.poultry else '',
|
||
},
|
||
'RequestCode': getattr(obj.hatching, 'RequestCode', '') if obj.hatching else '',
|
||
}
|
||
else:
|
||
data['hatching'] = {
|
||
'poultry': {
|
||
'UnitName': '',
|
||
'PartIdCode': '',
|
||
'Mobile': '',
|
||
'City': '',
|
||
},
|
||
'RequestCode': '',
|
||
}
|
||
data['TrackingStatusDescription'] = ''
|
||
data['Age'] = ''
|
||
ser_data_all_products.append(data)
|
||
|
||
def get_sort_date(data):
|
||
if data.get('Date'):
|
||
if isinstance(data['Date'], datetime.datetime):
|
||
return data['Date'].date()
|
||
elif isinstance(data['Date'], datetime.date):
|
||
return data['Date']
|
||
return datetime.date.min
|
||
|
||
combined_data = []
|
||
for data in ser_data_transports:
|
||
combined_data.append((get_sort_date(data), data))
|
||
for data in ser_data_all_products:
|
||
combined_data.append((get_sort_date(data), data))
|
||
|
||
combined_data.sort(key=lambda x: x[0], reverse=True)
|
||
ser_data = [data for _, data in combined_data]
|
||
output = BytesIO()
|
||
workbook = Workbook()
|
||
worksheet = workbook.active
|
||
workbook.remove(worksheet)
|
||
sheet_list = [
|
||
'بار های داخل استان',
|
||
'بار های خارج استان'
|
||
]
|
||
|
||
for sheet_name in sheet_list:
|
||
|
||
worksheet = workbook.create_sheet(sheet_name)
|
||
if sheet_name == 'بار های داخل استان':
|
||
|
||
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=25, width=25, border_style='thin', color='C00000')
|
||
|
||
value_header_list = [
|
||
|
||
]
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
|
||
excel_options = [
|
||
'ردیف',
|
||
'تاریخ کشتار',
|
||
'مرغدار',
|
||
'شناسه یکتا مرغدار',
|
||
'شماره مجوز جوجه ریزی',
|
||
'شماره موبایل مرغدار',
|
||
'شهر مرغدار',
|
||
'دامپزشک فارم',
|
||
'تلفن دامپزشک فارم',
|
||
'کد قرنطینه',
|
||
'وضعیت بار',
|
||
'کشتارگاه',
|
||
'شناسه یکتا کشتارگاه',
|
||
'استان',
|
||
'شهر',
|
||
'حجم کشتار',
|
||
'سن کشتار',
|
||
|
||
]
|
||
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
|
||
l = 7
|
||
unique_poultry_out_false = set()
|
||
unique_slaughterhouses_out_false = set()
|
||
total_slaughter_out_false = 0
|
||
excel_description(worksheet, 'A1', f'بارهای داخل استان', color='red',
|
||
row2='B1')
|
||
|
||
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()
|
||
from_date1 = shamsi_date(date1)
|
||
from_date2 = shamsi_date(date2)
|
||
excel_description(worksheet, 'A3', f'از تاریخ {from_date1} تا {from_date2}', color='red', row2='C3')
|
||
for data in ser_data:
|
||
if data['Out'] == False:
|
||
vet_farm_mobile = ''
|
||
vet_farm_name = ''
|
||
|
||
unique_poultry_out_false.add(data['hatching']['poultry']['UnitName'])
|
||
unique_slaughterhouses_out_false.add(data['DesUnitName'])
|
||
total_slaughter_out_false += data['GoodAmount']
|
||
if data['Date']:
|
||
date_str = str(data['Date']).split('T')[0] # جدا کردن بخش تاریخ
|
||
date = datetime.datetime.strptime(date_str, '%Y-%m-%d').date()
|
||
else:
|
||
date = datetime.date.today()
|
||
list1 = [
|
||
m,
|
||
str(shamsi_date(date, in_value=True)),
|
||
data['hatching']['poultry']['UnitName'],
|
||
data['hatching']['poultry']['PartIdCode'],
|
||
data['hatching']['RequestCode'],
|
||
data['hatching']['poultry']['Mobile'],
|
||
data['hatching']['poultry']['City'],
|
||
vet_farm_name,
|
||
vet_farm_mobile,
|
||
data['TrackingCode'],
|
||
data['TrackingStatusDescription'],
|
||
data['DesUnitName'],
|
||
data['DesPartIdCode'],
|
||
data['Province'],
|
||
data['City'],
|
||
data['GoodAmount'],
|
||
data['Age'],
|
||
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
value_header_list = [
|
||
len(unique_poultry_out_false),
|
||
len(unique_slaughterhouses_out_false),
|
||
total_slaughter_out_false
|
||
]
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
quantity = sum(
|
||
data['GoodAmount'] for data in ser_data if data['Out'] == False) or 0
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
quantity,
|
||
'',
|
||
|
||
]
|
||
|
||
create_value(worksheet, list2, l + 1, 1, color='green')
|
||
else:
|
||
unique_poultry_out_false = set()
|
||
unique_slaughterhouses_out_false = set()
|
||
total_slaughter_out_false = 0
|
||
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=25, width=25, border_style='thin', color='C00000')
|
||
|
||
excel_options = [
|
||
'ردیف',
|
||
'تاریخ کشتار',
|
||
'مرغدار',
|
||
'شناسه یکتا مرغدار',
|
||
'شماره مجوز جوجه ریزی',
|
||
'شماره موبایل مرغدار',
|
||
'شهر مرغدار',
|
||
'دامپزشک فارم',
|
||
'تلفن دامپزشک فارم',
|
||
'کد قرنطینه',
|
||
'وضعیت بار',
|
||
'کشتارگاه',
|
||
'شناسه یکتا کشتارگاه',
|
||
'استان',
|
||
'شهر',
|
||
'حجم کشتار',
|
||
'سن کشتار',
|
||
|
||
]
|
||
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
|
||
l = 7
|
||
unique_poultry = set()
|
||
unique_slaughterhouses = set()
|
||
total_slaughter = 0
|
||
excel_description(worksheet, 'A1', f'بارهای خارج استان', color='red',
|
||
row2='B1')
|
||
|
||
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()
|
||
from_date1 = shamsi_date(date1)
|
||
from_date2 = shamsi_date(date2)
|
||
excel_description(worksheet, 'A3', f'از تاریخ {from_date1} تا {from_date2}', color='red', row2='C3')
|
||
for data in ser_data:
|
||
if data['Out'] == True:
|
||
vet_farm_mobile = ''
|
||
vet_farm_name = ''
|
||
|
||
|
||
unique_poultry_out_false.add(data['hatching']['poultry']['UnitName'])
|
||
unique_slaughterhouses_out_false.add(data['DesUnitName'])
|
||
total_slaughter_out_false += data['GoodAmount']
|
||
if data['Date']:
|
||
date_str = str(data['Date']).split('T')[0] # جدا کردن بخش تاریخ
|
||
date = datetime.datetime.strptime(date_str, '%Y-%m-%d').date()
|
||
else:
|
||
date = datetime.date.today()
|
||
list1 = [
|
||
m,
|
||
str(shamsi_date(date, in_value=True)),
|
||
data['hatching']['poultry']['UnitName'],
|
||
data['hatching']['poultry']['PartIdCode'],
|
||
data['hatching']['RequestCode'],
|
||
data['hatching']['poultry']['Mobile'],
|
||
data['hatching']['poultry']['City'],
|
||
vet_farm_mobile,
|
||
vet_farm_name,
|
||
data['TrackingCode'],
|
||
data['TrackingStatusDescription'],
|
||
data['DesUnitName'],
|
||
data['DesPartIdCode'],
|
||
data['Province'],
|
||
data['City'],
|
||
data['GoodAmount'],
|
||
data['Age'],
|
||
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
value_header_list = [
|
||
len(unique_poultry_out_false),
|
||
len(unique_slaughterhouses_out_false),
|
||
total_slaughter_out_false
|
||
]
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
quantity = sum(
|
||
data['GoodAmount'] for data in ser_data if data['Out'] == True) or 0
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
quantity,
|
||
'',
|
||
|
||
]
|
||
|
||
create_value(worksheet, list2, l + 1, 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 transport_carcass_detail_excel(request):
|
||
search = request.GET.get('search')
|
||
|
||
type_role = request.GET.get('role')
|
||
province = request.GET.get('province')
|
||
|
||
if province == 'undefined':
|
||
province = None
|
||
len_kill_house = 0
|
||
if type_role == 'KillHouse':
|
||
filters_kill_house = {}
|
||
if province:
|
||
filters_kill_house['Province'] = province
|
||
|
||
kill_house_filterset_class = KillHouseFilterSet
|
||
kill_house = KillHouse.objects.filter(**filters_kill_house).order_by('id')
|
||
len_kill_house = len(kill_house)
|
||
if search and search != 'undefined' and search.strip():
|
||
kill_house = kill_house.filter(
|
||
build_query(kill_house_filterset_class.Meta.fields, search)
|
||
)
|
||
|
||
date1 = request.GET.get('date1')
|
||
date2 = request.GET.get('date2')
|
||
|
||
bars = TransportCarcassDetail.objects.filter(trash=False).order_by('-product_date')
|
||
buy_bars = TransportingDetail.objects.filter(trash=False, Date__date__gte='2025-03-21')
|
||
|
||
if date1:
|
||
date1 = datetime.datetime.strptime(str(date1), '%Y-%m-%d').date()
|
||
date2 = datetime.datetime.strptime(str(date2), '%Y-%m-%d').date()
|
||
bars = bars.filter(product_date__gte=date1, product_date__lte=date2)
|
||
date_1_for_buy_bars = date1 - datetime.timedelta(days=1)
|
||
date_2_for_buy_bars = date2 - datetime.timedelta(days=1)
|
||
buy_bars = buy_bars.filter(Date__date__gte=date_1_for_buy_bars, Date__date__lte=date_2_for_buy_bars)
|
||
|
||
bars_summary = bars.values('jihadi_origin').annotate(
|
||
total=Sum('quantity'),
|
||
input_total=Sum('quantity', filter=Q(out=False)),
|
||
output_total=Sum('quantity', filter=Q(out=True)),
|
||
input_count=Count('id', filter=Q(out=False)),
|
||
output_count=Count('id', filter=Q(out=True)),
|
||
total_count=Count('id'),
|
||
)
|
||
|
||
buy_summary = buy_bars.values('DesPartIdCode').annotate(
|
||
total=Sum('GoodAmount'),
|
||
input_total=Sum('GoodAmount', filter=Q(Out=False)),
|
||
output_total=Sum('GoodAmount', filter=Q(Out=True)),
|
||
input_count=Count('id', filter=Q(Out=False)),
|
||
output_count=Count('id', filter=Q(Out=True)),
|
||
total_count=Count('id'),
|
||
)
|
||
|
||
bars_dict = {row['jihadi_origin']: row for row in bars_summary}
|
||
buy_dict = {row['DesPartIdCode']: row for row in buy_summary}
|
||
kill_house = list(kill_house)
|
||
kill_house.sort(
|
||
key=lambda kh: (bars_dict.get(kh.PartIdCode, {}) or {}).get('total', 0) or 0,
|
||
reverse=True
|
||
)
|
||
|
||
serializer = KillHouseForTransportCarcassSerializer(
|
||
kill_house, many=True, context={'request': request, 'bars_dict': bars_dict, 'buy_dict': buy_dict}
|
||
).data
|
||
|
||
if 'code' in request.GET:
|
||
output = BytesIO()
|
||
workbook = Workbook()
|
||
worksheet = workbook.active
|
||
worksheet.insert_rows(1)
|
||
worksheet.sheet_view.rightToLeft = True
|
||
cell = worksheet.cell(row=1, column=1)
|
||
cell.alignment = Alignment(horizontal='center', vertical='center')
|
||
excel_options = [
|
||
'ردیف',
|
||
"تاریخ توزیع",
|
||
"تاریخ ثبت",
|
||
"نوع بار",
|
||
"محصول",
|
||
"وزن",
|
||
"کد قرنطینه",
|
||
"خریدار",
|
||
"استان خریدار",
|
||
"شهر خریدار",
|
||
"راننده",
|
||
"مالک",
|
||
"رهگیری خودرو",
|
||
"پلاک خودرو",
|
||
"وضعیت",
|
||
|
||
]
|
||
date1 = request.GET.get('date1')
|
||
filters = {}
|
||
if date1:
|
||
date1 = datetime.datetime.strptime(str(request.GET['date1']),
|
||
'%Y-%m-%d').date()
|
||
date2 = datetime.datetime.strptime(str(request.GET['date2']),
|
||
'%Y-%m-%d').date()
|
||
filters['product_date__gte'] = date1
|
||
filters['product_date__lte'] = date2
|
||
query = TransportCarcassDetail.objects.filter(**filters, jihadi_origin=request.GET.get('code'),
|
||
trash=False).order_by('-product_date')
|
||
if search:
|
||
if search != 'undefined' and search.strip():
|
||
query = query.filter(
|
||
build_query(TransportCarcassDetailFilterSet.Meta.fields, search)
|
||
)
|
||
|
||
serializer = TransportCarcassDetailSerializer(query, many=True).data
|
||
|
||
header_list = [
|
||
"تعداد بار ها",
|
||
"مجموع وزن ها",
|
||
|
||
]
|
||
create_header(worksheet, header_list, 4, 2, height=25, width=25, border_style='thin', color='C00000')
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
name = '-'
|
||
if query:
|
||
name = query.first().origin
|
||
l = 7
|
||
|
||
excel_description(worksheet, 'A1', f'توزیع / فروش گوشت {name}', color='red',
|
||
row2='B1')
|
||
|
||
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()
|
||
from_date1 = shamsi_date(date1)
|
||
from_date2 = shamsi_date(date2)
|
||
excel_description(worksheet, 'A3', f'از تاریخ {from_date1} تا {from_date2}', color='red', row2='C3')
|
||
for data in serializer:
|
||
date = datetime.datetime.strptime(str(data.get('date')),
|
||
'%Y-%m-%d').date()
|
||
list1 = [
|
||
m,
|
||
str(shamsi_date(data.get('product_date', '-') or '-', in_value=True) if data.get('product_date',
|
||
'-') else '-'),
|
||
str(shamsi_date(date, in_value=True)),
|
||
"داخل استان" if data.get('out', '-') == False else "خارج استان",
|
||
data.get('product', '-') or '-',
|
||
data.get('quantity', 0) or 0,
|
||
data.get('tracking', '-') or '-',
|
||
data.get('destination', '-') or '-',
|
||
data.get('destination_province', '-') or '-',
|
||
data.get('destination_city', '-') or '-',
|
||
data.get('driver_name', '-') or '-',
|
||
data.get('owner', '-') or '-',
|
||
data.get('car_tracking_code', '-') or '-',
|
||
data.get('plate', '-') or '-',
|
||
data.get('unloading', '-') if data.get('unloading', '-') else "در انتظار تخلیه",
|
||
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
quantity = sum(item.get('quantity', 0) for item in serializer)
|
||
|
||
value_header_list = [
|
||
query.count(),
|
||
quantity
|
||
]
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
quantity,
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
|
||
]
|
||
|
||
create_value(worksheet, list2, l + 1, 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
|
||
else:
|
||
output = BytesIO()
|
||
workbook = Workbook()
|
||
worksheet = workbook.active
|
||
workbook.remove(worksheet)
|
||
sheet_list = [
|
||
'توزیع و فروش گوشت مرغ'
|
||
]
|
||
excel_options = [
|
||
'ردیف',
|
||
"نقش",
|
||
"نام واحد",
|
||
"شناسه یکتا",
|
||
"استان",
|
||
"شهرستان",
|
||
"محصول",
|
||
"تعداد خرید داخل استان",
|
||
"وزن خرید داخل استان",
|
||
"تعداد خرید خارج استان",
|
||
"وزن خرید خارج استان",
|
||
"وزن کل انبار",
|
||
"وزن کل توزیع",
|
||
"درصد توزیع نسبت به انبار",
|
||
"تعداد توزیع داخل استان",
|
||
"وزن توزیع داخل استان",
|
||
"درصد توزیع داخل استان",
|
||
"وزن توزیع خارج استان",
|
||
"درصد توزیع خارج استان",
|
||
|
||
]
|
||
|
||
for sheet_name in sheet_list:
|
||
worksheet = workbook.create_sheet(sheet_name)
|
||
if sheet_name == 'توزیع و فروش گوشت مرغ':
|
||
|
||
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=25, width=25, border_style='thin',
|
||
color='C00000')
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
l = 7
|
||
excel_description(worksheet, 'A1', f'توزیع / فروش گوشت مرغ', color='red',
|
||
row2='B1')
|
||
|
||
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()
|
||
from_date1 = shamsi_date(date1)
|
||
from_date2 = shamsi_date(date2)
|
||
excel_description(worksheet, 'A3', f'از تاریخ {from_date1} تا {from_date2}', color='red',
|
||
row2='C3')
|
||
for data in serializer:
|
||
weight = 0
|
||
if (data.get('info', {}).get('total_ware_house', 0) or 0) > 0:
|
||
weight = round(((data.get('info', {}).get('total_bars_wight', 0) or 0) /
|
||
(data.get('info', {}).get('total_ware_house', 0) or 0)) * 100, 1) or 0
|
||
list1 = [
|
||
m,
|
||
data.get('info', {}).get('role', '-') or '-',
|
||
data.get('UnitName', '-') or '-',
|
||
data.get('PartIdCode', '-') or '-',
|
||
data.get('Province', '-') or '-',
|
||
data.get('City', '-') or '-',
|
||
"گوشت مرغ زنده",
|
||
data.get('info', {}).get('total_input_buy_bars_count', 0) or 0,
|
||
data.get('info', {}).get('total_input_buy_bars_wight', 0) or 0,
|
||
data.get('info', {}).get('total_output_buy_bars_count', 0) or 0,
|
||
data.get('info', {}).get('total_output_buy_bars_wight', 0) or 0,
|
||
data.get('info', {}).get('total_ware_house', 0) or 0,
|
||
data.get('info', {}).get('total_bars_wight', 0) or 0,
|
||
weight,
|
||
data.get('info', {}).get('input_bars', 0) or 0,
|
||
data.get('info', {}).get('total_input_bars_wight', 0) or 0,
|
||
data.get('info', {}).get('total_input_bars_percent', 0) or 0,
|
||
data.get('info', {}).get('total_output_bars_wight', 0) or 0,
|
||
data.get('info', {}).get('total_output_bars_percent', 0) or 0,
|
||
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
total_input_buy_count = sum(item.get('total_input_buy_bars_count', 0) for item in serializer)
|
||
total_input_buy_weight = sum(item.get('total_input_buy_bars_wight', 0) for item in serializer)
|
||
total_output_buy_count = sum(item.get('total_output_buy_bars_count', 0) for item in serializer)
|
||
total_output_buy_weight = sum(item.get('total_output_buy_bars_wight', 0) for item in serializer)
|
||
total_warehouse = sum(item.get('total_ware_house', 0) for item in serializer)
|
||
total_bars_weight = sum(item.get('total_bars_wight', 0) for item in serializer)
|
||
total_input_count = sum(item.get('input_bars', 0) for item in serializer)
|
||
total_input_weight = sum(item.get('total_input_bars_wight', 0) for item in serializer)
|
||
total_input_percent = sum(item.get('total_input_bars_percent', 0) for item in serializer)
|
||
total_output_weight = sum(item.get('total_output_bars_wight', 0) for item in serializer)
|
||
total_output_percent = sum(item.get('total_output_bars_percent', 0) for item in serializer)
|
||
|
||
percent_distributed = round(
|
||
(total_bars_weight / total_warehouse) * 100,
|
||
1
|
||
) if total_warehouse else 0
|
||
|
||
value_header_list = [
|
||
"کشتارگاه" if type_role == "KillHouse" else "مباشر",
|
||
"گوشت مرغ زنده",
|
||
len_kill_house,
|
||
total_input_buy_count,
|
||
total_input_buy_weight,
|
||
total_output_buy_count,
|
||
total_output_buy_weight,
|
||
total_warehouse,
|
||
total_bars_weight,
|
||
percent_distributed,
|
||
total_input_count,
|
||
total_input_weight,
|
||
total_input_percent,
|
||
total_output_weight,
|
||
total_output_percent,
|
||
]
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
total_input_buy_count,
|
||
total_input_buy_weight,
|
||
total_output_buy_count,
|
||
total_output_buy_weight,
|
||
total_warehouse,
|
||
total_bars_weight,
|
||
percent_distributed,
|
||
total_input_count,
|
||
total_input_weight,
|
||
total_input_percent,
|
||
total_output_weight,
|
||
total_output_percent,
|
||
|
||
]
|
||
|
||
create_value(worksheet, list2, l + 1, 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
|
||
|
||
else:
|
||
filters_steward = {}
|
||
if province:
|
||
filters_steward['province'] = province
|
||
|
||
steward_filterset_class = GuildsFilterSet
|
||
steward = Guilds.objects.filter(**filters_steward, trash=False, is_steward=True).order_by('id')
|
||
len_kill_house = len(steward)
|
||
if search and search != 'undefined' and search.strip():
|
||
steward = steward.filter(
|
||
build_query(steward_filterset_class.Meta.fields, search)
|
||
)
|
||
|
||
date1 = request.GET.get('date1')
|
||
date2 = request.GET.get('date2')
|
||
|
||
bars = TransportCarcassDetail.objects.filter(trash=False).order_by('-product_date')
|
||
|
||
if date1:
|
||
date1 = datetime.datetime.strptime(str(date1), '%Y-%m-%d').date()
|
||
date2 = datetime.datetime.strptime(str(date2), '%Y-%m-%d').date()
|
||
bars = bars.filter(product_date__gte=date1, product_date__lte=date2)
|
||
|
||
bars_summary = bars.values('jihadi_origin', 'jihadi_destination').annotate(
|
||
total=Sum('quantity', filter=Q(jihadi_origin=F('jihadi_origin'))),
|
||
input_total=Sum('quantity', filter=Q(out=False, jihadi_origin=F('jihadi_origin'))),
|
||
output_total=Sum('quantity', filter=Q(out=True, jihadi_origin=F('jihadi_origin'))),
|
||
input_count=Count('id', filter=Q(out=False, jihadi_origin=F('jihadi_origin'))),
|
||
output_count=Count('id', filter=Q(out=True, jihadi_origin=F('jihadi_origin'))),
|
||
total_count=Count('id', filter=Q(jihadi_origin=F('jihadi_origin'))),
|
||
|
||
total_input_buy_bars_wight=Sum('quantity',
|
||
filter=Q(out=False, jihadi_destination=F('jihadi_destination'))),
|
||
total_input_buy_bars_count=Count('id',
|
||
filter=Q(out=False, jihadi_destination=F('jihadi_destination'))),
|
||
total_output_buy_bars_wight=Sum('quantity',
|
||
filter=Q(out=True, jihadi_destination=F('jihadi_destination'))),
|
||
total_output_buy_bars_count=Count('id',
|
||
filter=Q(out=True, jihadi_destination=F('jihadi_destination'))),
|
||
)
|
||
|
||
bars_dict = {}
|
||
for row in bars_summary:
|
||
code = row['jihadi_origin'] or row['jihadi_destination']
|
||
if code:
|
||
bars_dict[code] = row
|
||
|
||
steward = list(steward)
|
||
steward.sort(
|
||
key=lambda s: (bars_dict.get(s.jihadi_code, {}) or {}).get('total', 0) or 0,
|
||
reverse=True
|
||
)
|
||
|
||
serializer = StewardForTransportCarcassSerializer(
|
||
steward, many=True, context={'request': request, 'bars_dict': bars_dict}
|
||
).data
|
||
if 'code' in request.GET:
|
||
output = BytesIO()
|
||
workbook = Workbook()
|
||
worksheet = workbook.active
|
||
worksheet.insert_rows(1)
|
||
worksheet.sheet_view.rightToLeft = True
|
||
cell = worksheet.cell(row=1, column=1)
|
||
cell.alignment = Alignment(horizontal='center', vertical='center')
|
||
excel_options = [
|
||
'ردیف',
|
||
"تاریخ توزیع",
|
||
"تاریخ ثبت",
|
||
"نوع بار",
|
||
"محصول",
|
||
"وزن",
|
||
"کد قرنطینه",
|
||
"خریدار",
|
||
"استان خریدار",
|
||
"شهر خریدار",
|
||
"راننده",
|
||
"مالک",
|
||
"رهگیری خودرو",
|
||
"پلاک خودرو",
|
||
"وضعیت",
|
||
|
||
]
|
||
date1 = request.GET.get('date1')
|
||
filters = {}
|
||
if date1:
|
||
date1 = datetime.datetime.strptime(str(request.GET['date1']),
|
||
'%Y-%m-%d').date()
|
||
date2 = datetime.datetime.strptime(str(request.GET['date2']),
|
||
'%Y-%m-%d').date()
|
||
filters['product_date__gte'] = date1
|
||
filters['product_date__lte'] = date2
|
||
query = TransportCarcassDetail.objects.filter(**filters, jihadi_origin=request.GET.get('code'),
|
||
trash=False).order_by('-product_date')
|
||
if search:
|
||
if search != 'undefined' and search.strip():
|
||
query = query.filter(
|
||
build_query(TransportCarcassDetailFilterSet.Meta.fields, search)
|
||
)
|
||
|
||
serializer = TransportCarcassDetailSerializer(query, many=True).data
|
||
|
||
header_list = [
|
||
"تعداد بار ها",
|
||
"مجموع وزن ها",
|
||
|
||
]
|
||
create_header(worksheet, header_list, 4, 2, height=25, width=25, border_style='thin', color='C00000')
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
name = '-'
|
||
if query:
|
||
name = query.first().destination
|
||
l = 7
|
||
|
||
excel_description(worksheet, 'A1', f'توزیع / فروش گوشت {name}', color='red',
|
||
row2='B1')
|
||
|
||
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()
|
||
from_date1 = shamsi_date(date1)
|
||
from_date2 = shamsi_date(date2)
|
||
excel_description(worksheet, 'A3', f'از تاریخ {from_date1} تا {from_date2}', color='red', row2='C3')
|
||
for data in serializer:
|
||
if data.get('product_date', '-'):
|
||
if type(data.get('product_date', '-')) == str:
|
||
product_date1 = datetime.datetime.strptime(str(data.get('product_date')),
|
||
'%Y-%m-%d').date()
|
||
product_date = str(shamsi_date(product_date1))
|
||
else:
|
||
product_date = str(shamsi_date(data.get('product_date', '-')))
|
||
else:
|
||
product_date = '-'
|
||
date = datetime.datetime.strptime(str(data.get('date')),
|
||
'%Y-%m-%d').date()
|
||
list1 = [
|
||
m,
|
||
str(product_date),
|
||
str(shamsi_date(date, in_value=True)),
|
||
"داخل استان" if data.get('out', '-') == False else "خارج استان",
|
||
data.get('product', '-') or '-',
|
||
data.get('quantity', 0) or 0,
|
||
data.get('tracking', '-') or '-',
|
||
data.get('destination', '-') or '-',
|
||
data.get('destination_province', '-') or '-',
|
||
data.get('destination_city', '-') or '-',
|
||
data.get('driver_name', '-') or '-',
|
||
data.get('owner', '-') or '-',
|
||
data.get('car_tracking_code', '-') or '-',
|
||
data.get('plate', '-') or '-',
|
||
data.get('unloading', '-') if data.get('unloading', '-') else "در انتظار تخلیه",
|
||
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
quantity = sum(item.get('quantity', 0) for item in serializer)
|
||
|
||
value_header_list = [
|
||
query.count(),
|
||
quantity
|
||
]
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
quantity,
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
|
||
]
|
||
|
||
create_value(worksheet, list2, l + 1, 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
|
||
else:
|
||
output = BytesIO()
|
||
workbook = Workbook()
|
||
worksheet = workbook.active
|
||
workbook.remove(worksheet)
|
||
sheet_list = [
|
||
'توزیع و فروش گوشت مرغ'
|
||
]
|
||
excel_options = [
|
||
'ردیف',
|
||
"نقش",
|
||
"نام واحد",
|
||
"شناسه یکتا",
|
||
"استان",
|
||
"شهرستان",
|
||
"محصول",
|
||
"تعداد خرید داخل استان",
|
||
"وزن خرید داخل استان",
|
||
"تعداد خرید خارج استان",
|
||
"وزن خرید خارج استان",
|
||
"وزن کل انبار",
|
||
"وزن کل توزیع",
|
||
"درصد توزیع نسبت به انبار",
|
||
"تعداد توزیع داخل استان",
|
||
"وزن توزیع داخل استان",
|
||
"درصد توزیع داخل استان",
|
||
"وزن توزیع خارج استان",
|
||
"درصد توزیع خارج استان",
|
||
|
||
]
|
||
|
||
for sheet_name in sheet_list:
|
||
worksheet = workbook.create_sheet(sheet_name)
|
||
if sheet_name == 'توزیع و فروش گوشت مرغ':
|
||
|
||
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=25, width=25, border_style='thin',
|
||
color='C00000')
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
l = 7
|
||
excel_description(worksheet, 'A1', f'توزیع / فروش گوشت مرغ', color='red',
|
||
row2='B1')
|
||
|
||
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()
|
||
from_date1 = shamsi_date(date1)
|
||
from_date2 = shamsi_date(date2)
|
||
excel_description(worksheet, 'A3', f'از تاریخ {from_date1} تا {from_date2}', color='red',
|
||
row2='C3')
|
||
for data in serializer:
|
||
weight = 0
|
||
if (data.get('info', {}).get('total_ware_house', 0) or 0) > 0:
|
||
weight = round(((data.get('info', {}).get('total_bars_wight', 0) or 0) /
|
||
(data.get('info', {}).get('total_ware_house', 0) or 0)) * 100, 1) or 0
|
||
list1 = [
|
||
m,
|
||
"مباشر",
|
||
data.get('name', '-') or '-',
|
||
data.get('jihadi_code', '-') or '-',
|
||
data.get('province', '-') or '-',
|
||
data.get('city', '-') or '-',
|
||
"گوشت مرغ زنده",
|
||
data.get('info', {}).get('total_input_buy_bars_count', 0) or 0,
|
||
data.get('info', {}).get('total_input_buy_bars_wight', 0) or 0,
|
||
data.get('info', {}).get('total_output_buy_bars_count', 0) or 0,
|
||
data.get('info', {}).get('total_output_buy_bars_wight', 0) or 0,
|
||
data.get('info', {}).get('total_ware_house', 0) or 0,
|
||
data.get('info', {}).get('total_bars_wight', 0) or 0,
|
||
weight,
|
||
data.get('info', {}).get('input_bars', 0) or 0,
|
||
data.get('info', {}).get('total_input_bars_wight', 0) or 0,
|
||
data.get('info', {}).get('total_input_bars_percent', 0) or 0,
|
||
data.get('info', {}).get('total_output_bars_wight', 0) or 0,
|
||
data.get('info', {}).get('total_output_bars_percent', 0) or 0,
|
||
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
total_input_buy_count = sum(item.get('total_input_buy_bars_count', 0) for item in serializer)
|
||
total_input_buy_weight = sum(item.get('total_input_buy_bars_wight', 0) for item in serializer)
|
||
total_output_buy_count = sum(item.get('total_output_buy_bars_count', 0) for item in serializer)
|
||
total_output_buy_weight = sum(item.get('total_output_buy_bars_wight', 0) for item in serializer)
|
||
total_warehouse = sum(item.get('total_ware_house', 0) for item in serializer)
|
||
total_bars_weight = sum(item.get('total_bars_wight', 0) for item in serializer)
|
||
total_input_count = sum(item.get('input_bars', 0) for item in serializer)
|
||
total_input_weight = sum(item.get('total_input_bars_wight', 0) for item in serializer)
|
||
total_input_percent = sum(item.get('total_input_bars_percent', 0) for item in serializer)
|
||
total_output_weight = sum(item.get('total_output_bars_wight', 0) for item in serializer)
|
||
total_output_percent = sum(item.get('total_output_bars_percent', 0) for item in serializer)
|
||
|
||
percent_distributed = round(
|
||
(total_bars_weight / total_warehouse) * 100,
|
||
1
|
||
) if total_warehouse else 0
|
||
|
||
value_header_list = [
|
||
"کشتارگاه" if type_role == "KillHouse" else "مباشر",
|
||
"گوشت مرغ زنده",
|
||
len_kill_house,
|
||
total_input_buy_count,
|
||
total_input_buy_weight,
|
||
total_output_buy_count,
|
||
total_output_buy_weight,
|
||
total_warehouse,
|
||
total_bars_weight,
|
||
percent_distributed,
|
||
total_input_count,
|
||
total_input_weight,
|
||
total_input_percent,
|
||
total_output_weight,
|
||
total_output_percent,
|
||
]
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
total_input_buy_count,
|
||
total_input_buy_weight,
|
||
total_output_buy_count,
|
||
total_output_buy_weight,
|
||
total_warehouse,
|
||
total_bars_weight,
|
||
percent_distributed,
|
||
total_input_count,
|
||
total_input_weight,
|
||
total_input_percent,
|
||
total_output_weight,
|
||
total_output_percent,
|
||
|
||
]
|
||
|
||
create_value(worksheet, list2, l + 1, 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 guilds_transport_carcass_detail_excel(request):
|
||
search = request.GET.get('search')
|
||
province = request.GET.get('province')
|
||
if province == 'undefined':
|
||
province = None
|
||
|
||
|
||
if 'code' in request.GET:
|
||
output = BytesIO()
|
||
workbook = Workbook()
|
||
worksheet = workbook.active
|
||
worksheet.insert_rows(1)
|
||
worksheet.sheet_view.rightToLeft = True
|
||
cell = worksheet.cell(row=1, column=1)
|
||
cell.alignment = Alignment(horizontal='center', vertical='center')
|
||
excel_options = [
|
||
"ردیف",
|
||
"تاریخ توزیع",
|
||
"تاریخ ثبت",
|
||
"نوع بار",
|
||
"محصول",
|
||
"وزن",
|
||
"کد قرنطینه",
|
||
"فروشنده",
|
||
"استان فروشنده",
|
||
"شهر فروشنده",
|
||
"راننده",
|
||
"مالک",
|
||
"رهگیری خودرو",
|
||
"پلاک خودرو",
|
||
"وضعیت",
|
||
]
|
||
date1 = request.GET.get('date1')
|
||
filters = {}
|
||
if date1:
|
||
date1 = datetime.datetime.strptime(str(request.GET['date1']), '%Y-%m-%d').date()
|
||
date2 = datetime.datetime.strptime(str(request.GET['date2']), '%Y-%m-%d').date()
|
||
filters['product_date__gte'] = date1
|
||
filters['product_date__lte'] = date2
|
||
|
||
query = TransportCarcassDetail.objects.filter(
|
||
**filters,
|
||
jihadi_destination=request.GET.get('code'),
|
||
trash=False
|
||
).order_by('-product_date')
|
||
if search and search != 'undefined' and search.strip():
|
||
query = query.filter(build_query(TransportCarcassDetailFilterSet.Meta.fields, search))
|
||
|
||
serializer = TransportCarcassDetailSerializer(query, many=True).data
|
||
|
||
header_list = [
|
||
"تعداد بار ها",
|
||
"مجموع وزن ها",
|
||
|
||
]
|
||
create_header(worksheet, header_list, 4, 2, height=25, width=25, border_style='thin', color='C00000')
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
name = '-'
|
||
if query:
|
||
name = query.first().origin
|
||
l = 7
|
||
|
||
excel_description(worksheet, 'A1', f'توزیع / فروش گوشت {name}', color='red',
|
||
row2='B1')
|
||
|
||
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()
|
||
from_date1 = shamsi_date(date1)
|
||
from_date2 = shamsi_date(date2)
|
||
excel_description(worksheet, 'A3', f'از تاریخ {from_date1} تا {from_date2}', color='red', row2='C3')
|
||
for data in serializer:
|
||
if data.get('product_date', '-'):
|
||
if type(data.get('product_date', '-')) == str:
|
||
product_date1 = datetime.datetime.strptime(str(data.get('product_date')),
|
||
'%Y-%m-%d').date()
|
||
product_date= str(shamsi_date(product_date1))
|
||
else :
|
||
product_date= str(shamsi_date(data.get('product_date', '-')))
|
||
else:
|
||
product_date='-'
|
||
date=datetime.datetime.strptime(str(data.get('date')),
|
||
'%Y-%m-%d').date()
|
||
list1 = [
|
||
m,
|
||
str(product_date),
|
||
str(shamsi_date(date, in_value=True)),
|
||
"داخل استان" if data.get('out', '-') == False else "خارج استان",
|
||
data.get('product', '-') or '-',
|
||
data.get('quantity', 0) or 0,
|
||
data.get('tracking', '-') or '-',
|
||
data.get('origin', '-') or '-',
|
||
data.get('origin_province', '-') or '-',
|
||
data.get('origin_city', '-') or '-',
|
||
data.get('driver_name', '-') or '-',
|
||
data.get('owner', '-') or '-',
|
||
data.get('car_tracking_code', '-') or '-',
|
||
data.get('plate', '-') or '-',
|
||
data.get('unloading', '-') if data.get('unloading', '-') else "در انتظار تخلیه",
|
||
|
||
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
quantity = sum(item.get('quantity', 0) for item in serializer)
|
||
|
||
value_header_list = [
|
||
query.count(),
|
||
quantity
|
||
]
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
quantity,
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
"",
|
||
|
||
|
||
]
|
||
|
||
create_value(worksheet, list2, l + 1, 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
|
||
else:
|
||
output = BytesIO()
|
||
workbook = Workbook()
|
||
worksheet = workbook.active
|
||
workbook.remove(worksheet)
|
||
sheet_list = [
|
||
'خرید صنوف'
|
||
]
|
||
excel_options = [
|
||
"ردیف",
|
||
"نقش",
|
||
"نام واحد",
|
||
"شناسه یکتا",
|
||
"استان",
|
||
"شهرستان",
|
||
"محصول",
|
||
"تعداد خرید داخل استان",
|
||
"وزن خرید داخل استان",
|
||
"تعداد خرید خارج استان",
|
||
"وزن خرید خارج استان",
|
||
"وزن کل خرید",
|
||
"درصد خرید داخل استان",
|
||
"درصد خرید خارج استان",
|
||
]
|
||
|
||
for sheet_name in sheet_list:
|
||
worksheet = workbook.create_sheet(sheet_name)
|
||
if sheet_name == 'خرید صنوف':
|
||
filters_steward = {}
|
||
if province:
|
||
filters_steward['province'] = province
|
||
|
||
steward = Guilds.objects.filter(**filters_steward, trash=False, is_steward=False).order_by('id')
|
||
steward_filterset_class = GuildsFilterSet
|
||
if search and search != 'undefined' and search.strip():
|
||
steward = steward.filter(build_query(steward_filterset_class.Meta.fields, search))
|
||
|
||
date1 = request.GET.get('date1')
|
||
date2 = request.GET.get('date2')
|
||
bars = TransportCarcassDetail.objects.filter(trash=False).order_by('-product_date')
|
||
if date1:
|
||
date1 = datetime.datetime.strptime(str(date1), '%Y-%m-%d').date()
|
||
date2 = datetime.datetime.strptime(str(date2), '%Y-%m-%d').date()
|
||
bars = bars.filter(product_date__gte=date1, product_date__lte=date2)
|
||
|
||
bars_summary = bars.values('jihadi_destination').annotate(
|
||
total_input_buy_bars_wight=Sum('quantity', filter=Q(out=False)),
|
||
total_output_buy_bars_wight=Sum('quantity', filter=Q(out=True)),
|
||
total_ware_house=Sum('quantity'),
|
||
total_count=Count('id'),
|
||
total_count_input_buy=Count('id', filter=Q(out=False)),
|
||
total_count_output_buy=Count('id', filter=Q(out=True)),
|
||
)
|
||
|
||
bars_dict = {row['jihadi_destination']: row for row in bars_summary}
|
||
steward = list(steward)
|
||
steward.sort(
|
||
key=lambda st: (bars_dict.get(st.jihadi_code, {}) or {}).get('total_ware_house', 0) or 0,
|
||
reverse=True
|
||
)
|
||
|
||
serializer = GuildsForTransportCarcassSerializer(steward, many=True,
|
||
context={'request': request, 'bars_dict': bars_dict})
|
||
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=25, width=25, border_style='thin', color='C00000')
|
||
m = 1
|
||
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
l = 7
|
||
excel_description(worksheet, 'A1', f'خرید صنوف', color='red',
|
||
row2='B1')
|
||
|
||
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()
|
||
from_date1 = shamsi_date(date1)
|
||
from_date2 = shamsi_date(date2)
|
||
excel_description(worksheet, 'A3', f'از تاریخ {from_date1} تا {from_date2}', color='red', row2='C3')
|
||
for data in serializer:
|
||
|
||
list1 = [
|
||
m,
|
||
"صنف",
|
||
data.get('name', '-') or '-',
|
||
data.get('jihadi_code', '-') or '-',
|
||
data.get('province', '-') or '-',
|
||
data.get('city', '-') or '-',
|
||
"گوشت مرغ تازه",
|
||
data.get('info', {}).get('total_input_buy_bars_count', 0) or 0,
|
||
data.get('info', {}).get('total_input_buy_bars_wight', 0) or 0,
|
||
data.get('info', {}).get('total_output_buy_bars_count', 0) or 0,
|
||
data.get('info', {}).get('total_output_buy_bars_wight', 0) or 0,
|
||
data.get('info', {}).get('total_ware_house', 0) or 0,
|
||
data.get('info', {}).get('total_input_buy_bars_percent', 0) or 0,
|
||
data.get('info', {}).get('total_output_buy_bars_percent', 0) or 0,
|
||
|
||
|
||
]
|
||
create_value(worksheet, list1, l, 1, m=m, border_style='thin', height=35)
|
||
|
||
l += 1
|
||
m += 1
|
||
total_input_buy_count = sum(item.get('total_input_buy_bars_count', 0) for item in serializer)
|
||
total_input_buy_weight = sum(item.get('total_input_buy_bars_wight', 0) for item in serializer)
|
||
total_output_buy_count = sum(item.get('total_output_buy_bars_count', 0) for item in serializer)
|
||
total_output_buy_weight = sum(item.get('total_output_buy_bars_wight', 0) for item in serializer)
|
||
total_warehouse = sum(item.get('total_ware_house', 0) for item in serializer)
|
||
|
||
|
||
|
||
|
||
value_header_list = [
|
||
"صنف",
|
||
"گوشت مرغ تازه",
|
||
len(steward),
|
||
total_input_buy_count,
|
||
total_input_buy_weight,
|
||
total_output_buy_count,
|
||
total_output_buy_weight,
|
||
total_warehouse,
|
||
|
||
]
|
||
create_value(worksheet, value_header_list, 3, 4, border_style='thin')
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
total_input_buy_count,
|
||
total_input_buy_weight,
|
||
total_output_buy_count,
|
||
total_output_buy_weight,
|
||
total_warehouse,
|
||
"",
|
||
"",
|
||
|
||
|
||
]
|
||
|
||
create_value(worksheet, list2, l + 1, 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
|
||
|
||
|
||
@api_view(["POST"])
|
||
@permission_classes([AllowAny])
|
||
@csrf_exempt
|
||
def import_transporting_detail(request):
|
||
file = request.FILES['file'].read()
|
||
wb_obj = openpyxl.load_workbook(filename=BytesIO(file))
|
||
sheet = wb_obj.active
|
||
created_count = 0
|
||
|
||
for i, row in enumerate(sheet.iter_rows(values_only=True)):
|
||
# ردیف اول هدر است، رد میکنیم
|
||
if i < 1 or row is None:
|
||
continue
|
||
|
||
# ستونها بر اساس ترتیب در فایل اکسل
|
||
tracking_code = row[4]
|
||
issue_date = row[5]
|
||
product_name = row[7]
|
||
good_code = row[8]
|
||
good_amount = row[9]
|
||
province = row[14]
|
||
city = row[15]
|
||
des_unit_name = row[16]
|
||
source_cert_id = row[17]
|
||
des_cert_id = row[18]
|
||
takhlie_date = row[28]
|
||
|
||
if not tracking_code:
|
||
continue # اگر کد رهگیری ندارد، رد شود
|
||
|
||
exists = TransportingDetail.objects.filter(TrackingCode=tracking_code).exists()
|
||
if not exists:
|
||
new_obj = TransportingDetail(
|
||
TrackingCode=tracking_code,
|
||
IssueDatePersian=issue_date,
|
||
GoodName=product_name,
|
||
GoodCode=good_code,
|
||
GoodAmount=good_amount,
|
||
Province=province,
|
||
City=city,
|
||
DesUnitName=des_unit_name,
|
||
SourceCertId=source_cert_id,
|
||
DesPartIdCode=des_cert_id,
|
||
TakhlieDatePersian=takhlie_date,
|
||
)
|
||
|
||
new_obj.save()
|
||
kill_house = KillHouse.objects.filter(PartIdCode=new_obj.DesPartIdCode, trash=False).first()
|
||
# Resolve hatching by fetching permit code from quarantine page via tracking code
|
||
try:
|
||
permit_map = get_hatching_permit_code(tracking_code)
|
||
permit_code = permit_map.get(str(tracking_code))
|
||
if permit_code:
|
||
hatching = Hatching.objects.filter(RequestCode=permit_code, trash=False).first()
|
||
if hatching:
|
||
new_obj.hatching = hatching
|
||
except Exception:
|
||
pass
|
||
if kill_house:
|
||
new_obj.Province = kill_house.Province
|
||
new_obj.City = kill_house.City
|
||
try:
|
||
if new_obj.hatching and new_obj.hatching.poultry and \
|
||
new_obj.hatching.poultry.LocationIdProvince != kill_house.ProvinceId:
|
||
new_obj.Out = True
|
||
except Exception:
|
||
pass
|
||
new_obj.save()
|
||
created_count += 1
|
||
|
||
return HttpResponse(f"{created_count} رکورد جدید اضافه شد ✅")
|
||
|
||
|
||
def all_products_transport_excel(request):
|
||
filterset_class = AllProductsTransportFilterSet
|
||
|
||
transports = AllProductsTransport.objects.filter(trash=False)
|
||
|
||
product_type = request.GET.get('product_type')
|
||
destination_province = request.GET.get('destination_province')
|
||
date1 = request.GET.get('date1')
|
||
date2 = request.GET.get('date2')
|
||
search = request.GET.get('search')
|
||
|
||
if product_type and product_type != 'undefined':
|
||
transports = transports.filter(product=product_type)
|
||
|
||
if destination_province and destination_province != 'undefined':
|
||
transports = transports.filter(destination_province=destination_province)
|
||
|
||
if date1 and date2 and date1 != 'undefined' and date2 != 'undefined':
|
||
try:
|
||
start_date = datetime.datetime.strptime(str(date1), '%Y-%m-%d')
|
||
end_date = datetime.datetime.strptime(str(date2), '%Y-%m-%d')
|
||
transports = transports.filter(date__gte=start_date, date__lte=end_date)
|
||
except ValueError:
|
||
pass
|
||
|
||
if search:
|
||
if search != 'undefined' and search.strip():
|
||
transports = transports.filter(
|
||
build_query(filterset_class.Meta.fields, search)
|
||
)
|
||
|
||
serializer = AllProductsTransportCustomSerializer(transports.order_by('-date', '-create_date'), many=True).data
|
||
|
||
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')
|
||
|
||
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()
|
||
from_date_1 = shamsi_date(date1)
|
||
to_date_1 = shamsi_date(date2)
|
||
worksheet['A3'] = f'از تاریخ:({from_date_1}) تا تاریخ:({to_date_1})'
|
||
excel_description(worksheet, 'A5', f'اطلاعات بار کل کشور', color='red', row2='C5')
|
||
if destination_province and destination_province != 'undefined':
|
||
excel_description(worksheet, 'A4', f'استان {destination_province}', row2='C4')
|
||
|
||
if product_type and product_type != 'undefined':
|
||
excel_description(worksheet, 'A1', f'محصول {product_type}', row2='C1')
|
||
|
||
# header_list2 = [
|
||
# 'تعداد کشتارگاه ها',
|
||
# 'تعداد بار',
|
||
# 'حجم بار',
|
||
# 'تعداد بار داخل استان',
|
||
# 'حجم بار داخل استان',
|
||
# 'درصد بار داخل استان',
|
||
# 'تعداد بار خارج استان',
|
||
# 'حجم بار خارج استان',
|
||
# 'درصد بار خارج استان',
|
||
#
|
||
# ]
|
||
# create_header(worksheet, header_list2, 5, 2, height=20.8, border_style='thin')
|
||
create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
|
||
l = 5
|
||
m = 1
|
||
if serializer:
|
||
for data in serializer:
|
||
list1 = [
|
||
m, # ردیف
|
||
|
||
data.get("tracking"),
|
||
data.get("product"),
|
||
data.get("items") if data.get("items") else '-',
|
||
data.get("quantity"),
|
||
data.get("unit"),
|
||
str(shamsi_date(convert_str_to_date(data.get("date")),in_value=True)) if data.get('date') else '-',
|
||
data.get("destination"),
|
||
data.get("jihadi_destination"),
|
||
data.get("destination_province"),
|
||
data.get("destination_city"),
|
||
data.get("origin"),
|
||
data.get("jihadi_origin"),
|
||
data.get("origin_province"),
|
||
data.get("origin_city"),
|
||
"داخل استان" if data.get("origin_province") == data.get("destination_province") else "خارج استان",
|
||
data.get("destination_prev") if data.get("destination_prev") else '-',
|
||
data.get("destination_changed") if data.get("destination_changed") else '-',
|
||
data.get("car_tracking_code"),
|
||
str(shamsi_date(convert_str_to_date(data.get("unloading_date")), in_value=True)) if data.get("unloading_date") else '-',
|
||
data.get("unloading") if data.get("unloading") else '-',
|
||
]
|
||
|
||
|
||
m += 1
|
||
|
||
l += 1
|
||
create_value(worksheet, list1, l + 1, 1)
|
||
total_quantity = sum(
|
||
item['quantity'] for item in serializer)
|
||
# aggregation1_bars = all_bars.aggregate(
|
||
# total=Sum('GoodAmount'),
|
||
# input_total=Sum('GoodAmount', filter=Q(Out=False)),
|
||
# output_total=Sum('GoodAmount', filter=Q(Out=True)),
|
||
# input_count=Count('id', filter=Q(Out=False)),
|
||
# output_count=Count('id', filter=Q(Out=True)),
|
||
# total_count=Count('id')
|
||
# )
|
||
#
|
||
# aggregation1_all_products = all_products_transport.aggregate(
|
||
# total=Sum('quantity'),
|
||
# input_total=Sum('quantity', filter=Q(out=False)),
|
||
# output_total=Sum('quantity', filter=Q(out=True)),
|
||
# input_count=Count('id', filter=Q(out=False)),
|
||
# output_count=Count('id', filter=Q(out=True)),
|
||
# total_count=Count('id')
|
||
# )
|
||
#
|
||
# total_count = (aggregation1_bars['total_count'] or 0) + (aggregation1_all_products['total_count'] or 0)
|
||
# total_bars_quantity = (aggregation1_bars['total'] or 0) + (aggregation1_all_products['total'] or 0)
|
||
# total_input_bars_quantity = (aggregation1_bars['input_total'] or 0) + (
|
||
# aggregation1_all_products['input_total'] or 0)
|
||
# total_output_bars_quantity = (aggregation1_bars['output_total'] or 0) + (
|
||
# aggregation1_all_products['output_total'] or 0)
|
||
# input_bars_count = (aggregation1_bars['input_count'] or 0) + (aggregation1_all_products['input_count'] or 0)
|
||
# output_bars_count = (aggregation1_bars['output_count'] or 0) + (aggregation1_all_products['output_count'] or 0)
|
||
#
|
||
# if total_count > 0:
|
||
# total_input_bars_percent = round((input_bars_count / total_count) * 100, 1)
|
||
# total_output_bars_percent = round((output_bars_count / total_count) * 100, 1)
|
||
# else:
|
||
# total_input_bars_percent = 0
|
||
# total_output_bars_percent = 0
|
||
# value_header_list2 = [
|
||
# kill_houses.count(),
|
||
# total_count,
|
||
# total_bars_quantity,
|
||
# input_bars_count,
|
||
# total_input_bars_quantity,
|
||
# total_input_bars_percent,
|
||
# output_bars_count,
|
||
# total_output_bars_quantity,
|
||
# total_output_bars_percent,
|
||
#
|
||
# ]
|
||
#
|
||
# create_value(worksheet, value_header_list2, 3, 5, border_style='thin')
|
||
|
||
list2 = [
|
||
'مجموع==>',
|
||
'',
|
||
'',
|
||
'',
|
||
total_quantity,
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
'',
|
||
# total_count,
|
||
# total_bars_quantity,
|
||
# input_bars_count,
|
||
# total_input_bars_quantity,
|
||
# total_input_bars_percent,
|
||
# output_bars_count,
|
||
# total_output_bars_quantity,
|
||
# total_output_bars_percent,
|
||
|
||
]
|
||
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 |