diff --git a/apps/product/services/excel/excel_processing.py b/apps/product/services/excel/excel_processing.py index e25d52f..58de9f6 100644 --- a/apps/product/services/excel/excel_processing.py +++ b/apps/product/services/excel/excel_processing.py @@ -1,3 +1,4 @@ +from datetime import datetime from io import BytesIO from django.db.models import Q @@ -8,6 +9,7 @@ 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 @@ -40,7 +42,7 @@ class QuotaDistributionExcelViewSet(viewsets.ModelViewSet): product = product.filter( Q(assigned_organization=organization) ).order_by('-modify_date') - description_name='سهمیه' + description_name = 'سهمیه' elif query.get('param') == 'assigner': product = product.filter( @@ -160,4 +162,93 @@ class QuotaDistributionExcelViewSet(viewsets.ModelViewSet): '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