diff --git a/apps/product/services/excel/excel_processing.py b/apps/product/services/excel/excel_processing.py index 5bf2e6e..1dcd4ad 100644 --- a/apps/product/services/excel/excel_processing.py +++ b/apps/product/services/excel/excel_processing.py @@ -258,7 +258,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin): response.write(output.getvalue()) return response - # noqa # سهمیه های فعال و بایگانی + # noqa # جزییات سهمیه های فعال و بایگانی @action( methods=['get'], detail=False, @@ -411,7 +411,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin): 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 - weight_type_fa='سبک' if weight_type == 'L' else 'سنگین' + weight_type_fa = 'سبک' if weight_type == 'L' else 'سنگین' list1 = [ livestock_allocation_m, group_name_fa, @@ -421,14 +421,15 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin): ] create_value(worksheet, list1, livestock_allocation_num, 1, height=24, m=livestock_allocation_m, - border_style='thin',m_color='F2F2F2') + border_style='thin', m_color='F2F2F2') livestock_allocation_num += 1 livestock_allocation_m += 1 else: excel_description(worksheet, 'A10', 'سهمیه بندی وجود ندارد', row2='E10', size=10, color='red') - create_header(worksheet, excel_options, 1, 9, height=25, width=20, different_color='C9C9C9', text_color='262626', + create_header(worksheet, excel_options, 1, 9, height=25, width=20, different_color='C9C9C9', + text_color='262626', border_style='thin') excel_description(worksheet, 'G8', @@ -438,7 +439,6 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin): "محدودیت بر اساس شهرستان و تعاونی", "محدودیت بر اساس بازه سنی دام", - ] create_header(worksheet, excel_options, 7, 9, height=25, width=25, different_color='C9C9C9', text_color='262626', @@ -494,7 +494,6 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin): incentive_plan_num = 10 incentive_plan_m = 1 for incentive_plan in incentive_plans: - list1 = [ incentive_plan_m, incentive_plan['incentive_plan_name'], @@ -516,3 +515,168 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin): 'utf-8') response.write(output.getvalue()) return response + + # 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() + 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="fسهمیه {quta_type}.xlsx"'.encode( # noqa + 'utf-8') + response.write(output.getvalue()) + return response