Files
RasadDam_Backend/apps/product/services/excel/excel_processing.py

687 lines
25 KiB
Python
Raw Normal View History

2025-07-31 12:27:26 +03:30
from datetime import datetime
2025-07-31 11:00:50 +03:30
from io import BytesIO
from django.db.models import Q
from django.http import HttpResponse
from openpyxl import Workbook
2025-08-02 08:52:28 +03:30
from rest_framework import viewsets, filters
2025-07-31 11:00:50 +03:30
from rest_framework.decorators import action
2025-08-02 08:52:28 +03:30
from apps.core.mixins.search_mixin import DynamicSearchMixin
2025-07-31 11:00:50 +03:30
from apps.product import models as product_models
from apps.product.web.api.v1.serializers import quota_distribution_serializers as distribution_serializers
2025-07-31 12:27:26 +03:30
from apps.product.web.api.v1.serializers.product_serializers import IncentivePlanSerializer
2025-08-02 13:51:28 +03:30
from apps.product.web.api.v1.serializers.quota_serializers import QuotaSerializer
2025-07-31 11:00:50 +03:30
from common.helper_excel import create_header, excel_description, create_header_freez, create_value, shamsi_date, \
convert_str_to_date
from common.helpers import get_organization_by_user
2025-08-02 08:52:28 +03:30
class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
2025-07-31 11:00:50 +03:30
queryset = product_models.QuotaDistribution.objects.all()
serializer_class = distribution_serializers.QuotaDistributionSerializer
2025-08-02 08:52:28 +03:30
filter_backends = [filters.SearchFilter]
2025-07-31 11:00:50 +03:30
2025-07-31 14:11:52 +03:30
# noqa # سهمیه و توزیع
2025-07-31 11:00:50 +03:30
@action(
methods=['get'],
detail=False,
url_path='my_distributions_excel',
url_name='my_distributions_excel',
name='my_distributions_excel'
)
def my_distributions_excel(self, request):
output = BytesIO()
workbook = Workbook()
worksheet = workbook.active
worksheet.sheet_view.rightToLeft = True
worksheet.insert_rows(1)
2025-08-02 08:52:28 +03:30
product = self.filter_query(self.queryset) # return by search param or all objects
2025-07-31 11:00:50 +03:30
organization = get_organization_by_user(request.user)
2025-08-02 08:52:28 +03:30
query = self.request.query_params
2025-07-31 11:34:26 +03:30
description_name = ''
2025-07-31 11:00:50 +03:30
if query.get('param') == 'assigned':
product = product.filter(
Q(assigned_organization=organization)
).order_by('-modify_date')
2025-08-02 08:21:04 +03:30
description_name = 'سهمیه' # noqa
2025-07-31 11:00:50 +03:30
elif query.get('param') == 'assigner':
product = product.filter(
Q(assigner_organization=organization)
).order_by('-modify_date')
2025-08-02 08:21:04 +03:30
description_name = 'توزیع' # noqa
2025-07-31 11:00:50 +03:30
elif query.get('param') == 'all':
product = product.filter(
Q(assigner_organization=organization) |
Q(assigned_organization=organization)
).order_by('-modify_date')
2025-07-31 11:34:26 +03:30
description_name = ''
2025-07-31 11:00:50 +03:30
ser_data = distribution_serializers.QuotaDistributionSerializer(product, many=True).data
excel_options = [
"ردیف",
"شناسه توزیع",
"شناسه سهمیه",
"تاریخ ثبت",
"توزیع کننده",
"دریافت کننده",
"وزن",
"وزن توزیع شده",
"وزن باقیمانده",
"وزن فروش رفته",
"مانده انبار",
"ورودی به انبار",
"توضیحات",
]
header_list = [
"وزن",
"وزن توزیع شده",
"وزن باقیمانده",
"وزن فروش رفته",
"مانده انبار",
"ورودی به انبار",
]
2025-08-02 08:21:04 +03:30
create_header(worksheet, header_list, 5, 2, height=25, border_style='thin')
2025-07-31 11:00:50 +03:30
2025-07-31 11:34:26 +03:30
excel_description(worksheet, 'B1', f'{description_name}', row2='C3')
2025-07-31 11:00:50 +03:30
2025-08-02 08:21:04 +03:30
create_header_freez(worksheet, excel_options, 1, 6, 7, height=25, width=20)
2025-07-31 11:00:50 +03:30
l = 6
m = 1
if ser_data:
for data in ser_data:
list1 = [
m,
data['distribution_id'] or '',
str(data['quota']['quota_id']) if data.get('quota') and data['quota'].get('quota_id') else '',
str(shamsi_date(convert_str_to_date(data['create_date']), in_value=True)) if data.get(
'create_date') else '',
2025-08-02 08:44:10 +03:30
(data[
2025-08-02 08:52:28 +03:30
'assigner_organization'] or {}).get('organization') or '-',
2025-08-02 08:44:10 +03:30
(data[
2025-08-02 08:52:28 +03:30
'assigned_organization'] or {}).get('organization') or '-',
2025-07-31 11:00:50 +03:30
data.get('weight') or 0,
data.get('distributed') or 0,
data.get('remaining_weight') or 0,
data.get('been_sold') or 0,
data.get('warehouse_balance') or 0,
data.get('warehouse_entry') or 0,
data.get('description') or '-'
]
2025-08-02 08:21:04 +03:30
create_value(worksheet, list1, l + 1, 1, height=24, m=m)
2025-07-31 11:00:50 +03:30
m += 1
l += 1
weight = sum((data['weight'] or 0) for data in ser_data)
distributed = sum((data['distributed'] or 0) for data in ser_data)
remaining_weight = sum((data['remaining_weight'] or 0) for data in ser_data)
been_sold = sum((data['been_sold'] or 0) for data in ser_data)
warehouse_balance = sum((data['warehouse_balance'] or 0) for data in ser_data)
warehouse_entry = sum((data['warehouse_entry'] or 0) for data in ser_data)
value_list = [
weight,
distributed,
remaining_weight,
been_sold,
warehouse_balance,
warehouse_entry,
]
create_value(worksheet, value_list, 3, 5, border_style='thin')
list2 = [
'مجموع==>',
'',
'',
'',
'',
'',
weight,
distributed,
remaining_weight,
been_sold,
warehouse_balance,
warehouse_entry,
'',
]
2025-08-02 08:21:04 +03:30
create_value(worksheet, list2, l + 3, 1, color='gray') # noqa
2025-07-31 11:00:50 +03:30
workbook.save(output)
output.seek(0)
response = HttpResponse(
2025-08-02 08:21:04 +03:30
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') # noqa
2025-07-31 11:00:50 +03:30
response[
2025-08-02 08:21:04 +03:30
'Content-Disposition'] = f'attachment; filename="سهمیه.xlsx"'.encode( # noqa
2025-07-31 11:00:50 +03:30
'utf-8')
response.write(output.getvalue())
2025-07-31 16:21:39 +03:30
return response
2025-07-31 12:27:26 +03:30
2025-07-31 14:11:52 +03:30
# noqa # طرح های تشویقی
2025-07-31 12:27:26 +03:30
@action(
methods=['get'],
detail=False,
url_path='incentive_plan_excel',
url_name='incentive_plan_excel',
name='incentive_plan_excel'
)
def incentive_plan_excel(self, request):
output = BytesIO()
workbook = Workbook()
worksheet = workbook.active
worksheet.sheet_view.rightToLeft = True
worksheet.insert_rows(1)
today = datetime.now().date()
user_relations = product_models.UserRelations.objects.filter(user=request.user).first()
2025-08-02 14:40:56 +03:30
2025-07-31 12:27:26 +03:30
incentive_plans = user_relations.incentive_plans.filter(
Q(is_time_unlimited=False) |
Q(start_date_limit__lte=today, end_date_limit__gte=today)
)
2025-08-02 14:40:56 +03:30
user_relations = self.filter_query(incentive_plans)
2025-07-31 12:27:26 +03:30
ser_data = IncentivePlanSerializer(incentive_plans, many=True).data
excel_options = [
"ردیف",
"نام",
"توضیحات",
"نوع طرح",
"گروه",
"محدودیت زمانی",
"شروع محدودیت",
"پایان طرح",
]
excel_description(worksheet, 'B1', f'طرح های تشویقی', row2='C3')
2025-08-02 08:21:04 +03:30
create_header_freez(worksheet, excel_options, 1, 6, 7, height=25, width=20)
2025-07-31 12:27:26 +03:30
l = 6
m = 1
if ser_data:
for data in ser_data:
grop_name = 'شهری'
if data.get('group') == 'rural':
grop_name = 'روستایی'
elif data.get('group') == 'nomadic':
grop_name = 'عشایری'
is_time_unlimited = 'دارد' if data.get('is_time_unlimited') == True else 'ندارد'
list1 = [
m,
data['name'] or '-',
data['description'] or '-',
data['plan_type'] or '-',
grop_name,
is_time_unlimited,
str(shamsi_date(convert_str_to_date(data.get('start_date_limit')), in_value=True)),
str(shamsi_date(convert_str_to_date(data.get('end_date_limit')), in_value=True)),
]
2025-08-02 08:21:04 +03:30
create_value(worksheet, list1, l + 1, 1, height=24, m=m)
2025-07-31 12:27:26 +03:30
m += 1
l += 1
list2 = [
'',
'',
'',
'',
'',
'',
'',
'',
]
2025-08-02 08:21:04 +03:30
create_value(worksheet, list2, l + 3, 1, height=24, color='gray') # noqa
2025-07-31 12:27:26 +03:30
workbook.save(output)
output.seek(0)
response = HttpResponse(
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response[
2025-08-02 08:21:04 +03:30
'Content-Disposition'] = f'attachment; filename="طرح های تشویقی.xlsx"'.encode( # noqa
2025-07-31 12:27:26 +03:30
'utf-8')
response.write(output.getvalue())
2025-07-31 11:00:50 +03:30
return response
2025-08-02 13:51:28 +03:30
2025-08-02 14:11:46 +03:30
# noqa # جزییات سهمیه های فعال و بایگانی
2025-08-02 13:51:28 +03:30
@action(
methods=['get'],
detail=False,
url_path='detail_quota_excel',
url_name='detail_quota_excel',
name='detail_quota_excel'
)
def detail_quota_excel(self, request):
queryset = product_models.Quota.objects.filter(id=request.GET['id'], trash=False)
2025-08-02 14:40:56 +03:30
queryset = self.filter_query(queryset)
2025-08-02 13:51:28 +03:30
serializer_class = QuotaSerializer
filter_backends = [filters.SearchFilter]
search_fields = [
"registerer_organization__name",
"quota_id",
"product__name",
"sale_type",
"sale_unit__unit",
"group",
]
output = BytesIO()
workbook = Workbook()
worksheet = workbook.active
worksheet.sheet_view.rightToLeft = True
worksheet.insert_rows(1)
active = request.GET.get('active')
queryset = self.filter_query(queryset) # return by search param or all objects
organization = get_organization_by_user(request.user)
queryset = queryset.filter(
Q(registerer_organization=organization)).first()
if active:
quta_type = 'فعال'
else:
quta_type = 'بایگانی'
data = serializer_class(queryset).data
excel_options = [
"ردیف",
"شناسه سهمیه",
"محصول",
"وزن محصول",
"وزن باقیمانده",
"تاریخ ثبت",
"واحد فروش",
"گروه",
"سهمیه ماه",
"نوع فروش",
"مجوز فروش",
"قیمت درب کارخانه",
"قیمت درب تعاونی",
]
excel_description(worksheet, 'B1',
'سهمیه {0} با شناسه سهمیه {1}'.format(quta_type, str(data['quota_id'] or '-')), row2='C2',
size=11)
create_header(worksheet, excel_options, 1, 4, height=25, width=20)
l = 5
m = 1
fa_mount = {
1: "فروردین",
2: "اردیبهشت",
3: "خرداد",
4: "تیر",
5: "مرداد",
6: "شهریور",
7: "مهر",
8: "آبان",
9: "آذر",
10: "دی",
11: "بهمن",
12: "اسفند",
}
group_type = data.get('month_choices')
sale_license = data.get('sale_license')
if group_type:
mount_num = [num for num in group_type]
fa_mount1 = [fa_mount.get(name, name) for name in mount_num]
fa_mount_text = ' - '.join(fa_mount1) if fa_mount1 else '-'
else:
fa_mount_text = '-'
if sale_license:
sale_license_mount_num = [num for num in sale_license]
fa_mount2 = [fa_mount.get(name, name) for name in sale_license_mount_num]
sale_license_fa_mount_text = ' - '.join(fa_mount2) if fa_mount2 else '-'
else:
sale_license_fa_mount_text = '-'
sale_type = 'دولتی' if data['sale_type'] == 'gov' else 'آزاد'
group_name = data.get('group')
if group_name:
if group_name == 'rural':
group_name_fa = 'روستایی'
elif group_name == 'nomadic':
group_name_fa = 'عشایری'
else:
group_name_fa = 'دولتی'
else:
group_name_fa = '-'
list1 = [
m,
str(data['quota_id'] or '-'),
(data.get('product') or {}).get('product') or '-',
data['quota_weight'] or '-',
data['remaining_weight'] or '-',
str(shamsi_date(convert_str_to_date(data.get('create_date')), in_value=True)),
(data['sale_unit'] or {}).get('unit') or '-',
group_name_fa,
fa_mount_text,
sale_type,
sale_license_fa_mount_text,
int(data['base_price_factory'].split('.')[0]),
int(data['base_price_cooperative'].split('.')[0]),
]
create_value(worksheet, list1, l, 1, height=24, m=m)
excel_description(worksheet, 'A8',
'سهمیه بندی دام'.format(quta_type, str(data['quota_id'] or '-')), row2='E8',
size=10)
excel_options = [
"ردیف",
"گروه",
"حجم",
"دسته",
"محصول",
]
livestock_allocations = data.get('livestock_allocations')
if livestock_allocations:
livestock_allocation_num = 10
livestock_allocation_m = 1
for livestock_allocation in livestock_allocations:
livestock_group = livestock_allocation.get('livestock_group')
if livestock_group:
if livestock_group == 'rural':
group_name_fa = 'روستایی'
elif livestock_group == 'nomadic':
group_name_fa = 'عشایری'
else:
group_name_fa = 'دولتی'
else:
group_name_fa = '-'
weight_type = (livestock_allocation.get('livestock_type') or {}).get('weight_type') or None
name = (livestock_allocation.get('livestock_type') or {}).get('name') or None
2025-08-02 14:11:46 +03:30
weight_type_fa = 'سبک' if weight_type == 'L' else 'سنگین'
2025-08-02 13:51:28 +03:30
list1 = [
livestock_allocation_m,
group_name_fa,
int(livestock_allocation['quantity_kg'].split('.')[0]),
weight_type_fa,
name,
]
create_value(worksheet, list1, livestock_allocation_num, 1, height=24, m=livestock_allocation_m,
2025-08-02 14:11:46 +03:30
border_style='thin', m_color='F2F2F2')
2025-08-02 13:51:28 +03:30
livestock_allocation_num += 1
livestock_allocation_m += 1
else:
excel_description(worksheet, 'A10',
'سهمیه بندی وجود ندارد', row2='E10',
size=10, color='red')
2025-08-02 14:11:46 +03:30
create_header(worksheet, excel_options, 1, 9, height=25, width=20, different_color='C9C9C9',
text_color='262626',
2025-08-02 13:51:28 +03:30
border_style='thin')
excel_description(worksheet, 'G8',
'محدودیت ها', row2='H8',
size=10)
excel_options = [
"محدودیت بر اساس شهرستان و تعاونی",
"محدودیت بر اساس بازه سنی دام",
]
create_header(worksheet, excel_options, 7, 9, height=25, width=25, different_color='C9C9C9',
text_color='262626',
border_style='thin')
limit_by_organizations = data.get('limit_by_organizations')
if limit_by_organizations:
limit_by_organization_num = 10
limit_by_organization_m = 1
for limit_by_organization in limit_by_organizations:
name = limit_by_organization.get('name') or '-'
list1 = [
name
]
create_value(worksheet, list1, limit_by_organization_num, 7, height=24, m=limit_by_organization_m,
border_style='thin', m_color='F2F2F2')
limit_by_organization_num += 1
limit_by_organization_m += 1
livestock_limitations = data.get('livestock_limitations')
if livestock_limitations:
livestock_limitations_num = 10
livestock_limitations_m = 1
for livestock_limitation in livestock_limitations:
weight_type = (livestock_limitation.get('livestock_type') or {}).get('weight_type') or None
weight_type_fa = 'سبک' if weight_type == 'L' else 'سنگین'
livestock_type = livestock_limitation.get('livestock_type')
list1 = [
f'{livestock_type["name"]} ({weight_type_fa}) با سن {livestock_limitation["age_month"]}',
]
create_value(worksheet, list1, livestock_limitations_num, 8, height=24, m=livestock_limitations_m,
border_style='thin', m_color='F2F2F2')
livestock_limitations_m += 1
livestock_limitations_num += 1
excel_description(worksheet, 'J8',
'طرح های تشویقی', row2='M8',
size=10)
excel_options = [
"ردیف",
"طرح",
"وزن سبک",
"وزن سنگین",
]
create_header(worksheet, excel_options, 10, 9, height=25, width=25, different_color='C9C9C9',
text_color='262626',
border_style='thin')
incentive_plans = data.get('incentive_plan')
if incentive_plans:
incentive_plan_num = 10
incentive_plan_m = 1
for incentive_plan in incentive_plans:
list1 = [
incentive_plan_m,
incentive_plan['incentive_plan_name'],
incentive_plan['light_value'],
incentive_plan['heavy_value'],
]
create_value(worksheet, list1, incentive_plan_num, 10, height=24, m=incentive_plan_m,
border_style='thin', m_color='F2F2F2')
incentive_plan_num += 1
incentive_plan_m += 1
workbook.save(output)
output.seek(0)
response = HttpResponse(
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response[
'Content-Disposition'] = f'attachment; filename="همیه {quta_type}.xlsx"'.encode( # noqa
'utf-8')
response.write(output.getvalue())
return response
2025-08-02 14:11:46 +03:30
# noqa # سهمیه های فعال و بایگانی
@action(
methods=['get'],
detail=False,
url_path='quota_excel',
url_name='quota_excel',
name='quota_excel'
)
def quota_excel(self, request):
queryset = product_models.Quota.objects.all()
2025-08-02 14:40:56 +03:30
queryset = self.filter_query(queryset)
2025-08-02 14:11:46 +03:30
serializer_class = QuotaSerializer
filter_backends = [filters.SearchFilter]
search_fields = [
"registerer_organization__name",
"quota_id",
"product__name",
"sale_type",
"sale_unit__unit",
"group",
]
output = BytesIO()
workbook = Workbook()
worksheet = workbook.active
worksheet.sheet_view.rightToLeft = True
worksheet.insert_rows(1)
active = request.GET.get('active')
queryset = self.filter_query(queryset) # return by search param or all objects
organization = get_organization_by_user(request.user)
if active:
queryset = queryset.filter(
Q(registerer_organization=organization),
Q(is_closed=False)
).order_by('-modify_date')
quta_type = 'فعال'
else:
queryset = queryset.filter(
Q(registerer_organization=organization),
Q(is_closed=True)
).order_by('-modify_date')
quta_type = 'بایگانی'
ser_data = serializer_class(queryset, many=True).data
excel_options = [
"ردیف",
"شناسه سهمیه",
"محصول",
"وزن محصول",
"وزن باقیمانده",
"تاریخ ثبت",
"واحد فروش",
"گروه",
"سهمیه ماه",
"نوع فروش",
"مجوز فروش",
"قیمت درب کارخانه",
"قیمت درب تعاونی",
]
excel_description(worksheet, 'B1',
'سهمیه های {0}'.format(quta_type), row2='C2',
size=11)
create_header(worksheet, excel_options, 1, 4, height=25, width=20)
l = 5
m = 1
fa_mount = {
1: "فروردین",
2: "اردیبهشت",
3: "خرداد",
4: "تیر",
5: "مرداد",
6: "شهریور",
7: "مهر",
8: "آبان",
9: "آذر",
10: "دی",
11: "بهمن",
12: "اسفند",
}
if ser_data:
for data in ser_data:
group_type = data.get('month_choices')
sale_license = data.get('sale_license')
if group_type:
mount_num = [num for num in group_type]
fa_mount1 = [fa_mount.get(name, name) for name in mount_num]
fa_mount_text = ' - '.join(fa_mount1) if fa_mount1 else '-'
else:
fa_mount_text = '-'
if sale_license:
sale_license_mount_num = [num for num in sale_license]
fa_mount2 = [fa_mount.get(name, name) for name in sale_license_mount_num]
sale_license_fa_mount_text = ' - '.join(fa_mount2) if fa_mount2 else '-'
else:
sale_license_fa_mount_text = '-'
sale_type = 'دولتی' if data['sale_type'] == 'gov' else 'آزاد'
group_name = data.get('group')
if group_name:
if group_name == 'rural':
group_name_fa = 'روستایی'
elif group_name == 'nomadic':
group_name_fa = 'عشایری'
else:
group_name_fa = 'دولتی'
else:
group_name_fa = '-'
list1 = [
m,
str(data['quota_id'] or '-'),
(data.get('product') or {}).get('product') or '-',
data['quota_weight'] or '-',
data['remaining_weight'] or '-',
str(shamsi_date(convert_str_to_date(data.get('create_date')), in_value=True)),
(data['sale_unit'] or {}).get('unit') or '-',
group_name_fa,
fa_mount_text,
sale_type,
sale_license_fa_mount_text,
int(data['base_price_factory'].split('.')[0]),
int(data['base_price_cooperative'].split('.')[0]),
]
create_value(worksheet, list1, l, 1, height=24, m=m)
l += 1
m += 1
remaining_weight = sum((data['remaining_weight'] or 0) for data in ser_data)
quota_weight = sum((data['quota_weight'] or 0) for data in ser_data)
list2 = [
'مجموع==>',
'',
'',
quota_weight,
remaining_weight,
'',
'',
'',
'',
'',
'',
'',
'',
]
create_value(worksheet, list2, l + 2, 1, color='gray') # noqa
workbook.save(output)
output.seek(0)
response = HttpResponse(
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response[
'Content-Disposition'] = f'attachment; filename="همیه {quta_type}.xlsx"'.encode( # noqa
'utf-8')
response.write(output.getvalue())
return response