255 lines
8.6 KiB
Python
255 lines
8.6 KiB
Python
from datetime import datetime
|
|
from io import BytesIO
|
|
|
|
from django.db.models import Q
|
|
from django.http import HttpResponse
|
|
from openpyxl import Workbook
|
|
from rest_framework import viewsets
|
|
from rest_framework.decorators import action
|
|
|
|
from apps.product import models as product_models
|
|
from apps.product.web.api.v1.serializers import quota_distribution_serializers as distribution_serializers
|
|
from apps.product.web.api.v1.serializers.product_serializers import IncentivePlanSerializer
|
|
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
|
|
|
|
|
|
class QuotaDistributionExcelViewSet(viewsets.ModelViewSet):
|
|
queryset = product_models.QuotaDistribution.objects.all()
|
|
serializer_class = distribution_serializers.QuotaDistributionSerializer
|
|
|
|
@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)
|
|
|
|
product = self.queryset
|
|
query = request.query_params
|
|
organization = get_organization_by_user(request.user)
|
|
description_name = ''
|
|
|
|
if query.get('param') == 'assigned':
|
|
product = product.filter(
|
|
Q(assigned_organization=organization)
|
|
).order_by('-modify_date')
|
|
description_name = 'سهمیه'
|
|
|
|
elif query.get('param') == 'assigner':
|
|
product = product.filter(
|
|
Q(assigner_organization=organization)
|
|
).order_by('-modify_date')
|
|
description_name = 'توزیع'
|
|
|
|
elif query.get('param') == 'all':
|
|
product = product.filter(
|
|
Q(assigner_organization=organization) |
|
|
Q(assigned_organization=organization)
|
|
).order_by('-modify_date')
|
|
description_name = ''
|
|
|
|
ser_data = distribution_serializers.QuotaDistributionSerializer(product, many=True).data
|
|
|
|
excel_options = [
|
|
"ردیف",
|
|
"شناسه توزیع",
|
|
"شناسه سهمیه",
|
|
"تاریخ ثبت",
|
|
"توزیع کننده",
|
|
"دریافت کننده",
|
|
"وزن",
|
|
"وزن توزیع شده",
|
|
"وزن باقیمانده",
|
|
"وزن فروش رفته",
|
|
"مانده انبار",
|
|
"ورودی به انبار",
|
|
"توضیحات",
|
|
]
|
|
|
|
header_list = [
|
|
"وزن",
|
|
"وزن توزیع شده",
|
|
"وزن باقیمانده",
|
|
"وزن فروش رفته",
|
|
"مانده انبار",
|
|
"ورودی به انبار",
|
|
|
|
]
|
|
|
|
create_header(worksheet, header_list, 5, 2, height=20, border_style='thin')
|
|
|
|
excel_description(worksheet, 'B1', f'{description_name}', row2='C3')
|
|
|
|
create_header_freez(worksheet, excel_options, 1, 6, 7, height=22, width=20)
|
|
|
|
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 '',
|
|
data[
|
|
'assigner_organization'].get('organization') or '-',
|
|
data[
|
|
'assigned_organization'].get('organization') or '-',
|
|
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 '-'
|
|
]
|
|
create_value(worksheet, list1, l + 1, 1, m=m)
|
|
|
|
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,
|
|
'',
|
|
|
|
]
|
|
create_value(worksheet, list2, l + 3, 1, color='gray')
|
|
|
|
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 \
|
|
|
|
|
|
@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()
|
|
incentive_plans = user_relations.incentive_plans.filter(
|
|
Q(is_time_unlimited=False) |
|
|
Q(start_date_limit__lte=today, end_date_limit__gte=today)
|
|
)
|
|
|
|
ser_data = IncentivePlanSerializer(incentive_plans, many=True).data
|
|
|
|
excel_options = [
|
|
"ردیف",
|
|
"نام",
|
|
"توضیحات",
|
|
"نوع طرح",
|
|
"گروه",
|
|
"محدودیت زمانی",
|
|
"شروع محدودیت",
|
|
"پایان طرح",
|
|
|
|
]
|
|
|
|
excel_description(worksheet, 'B1', f'طرح های تشویقی', row2='C3')
|
|
|
|
create_header_freez(worksheet, excel_options, 1, 6, 7, height=22, width=20)
|
|
|
|
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)),
|
|
]
|
|
create_value(worksheet, list1, l + 1, 1, m=m)
|
|
|
|
m += 1
|
|
l += 1
|
|
|
|
list2 = [
|
|
'',
|
|
'',
|
|
'',
|
|
'',
|
|
'',
|
|
'',
|
|
'',
|
|
'',
|
|
|
|
]
|
|
create_value(worksheet, list2, l + 3, 1, color='gray')
|
|
|
|
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
|