diff --git a/.idea/Rasaddam_Backend.iml b/.idea/Rasaddam_Backend.iml index 168bde0..c5d6090 100644 --- a/.idea/Rasaddam_Backend.iml +++ b/.idea/Rasaddam_Backend.iml @@ -14,7 +14,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 296aa57..29f5506 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/apps/product/services/excel/excel_processing.py b/apps/product/services/excel/excel_processing.py index b094122..5bf2e6e 100644 --- a/apps/product/services/excel/excel_processing.py +++ b/apps/product/services/excel/excel_processing.py @@ -11,6 +11,7 @@ from apps.core.mixins.search_mixin import DynamicSearchMixin 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 apps.product.web.api.v1.serializers.quota_serializers import QuotaSerializer 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 @@ -256,3 +257,262 @@ class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin): 'utf-8') response.write(output.getvalue()) return response + + # noqa # سهمیه های فعال و بایگانی + @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) + 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 + weight_type_fa='سبک' if weight_type == 'L' else 'سنگین' + 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, + 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', + 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="fسهمیه {quta_type}.xlsx"'.encode( # noqa + 'utf-8') + response.write(output.getvalue()) + return response diff --git a/common/helper_excel.py b/common/helper_excel.py index 5ca0352..e5da0a9 100644 --- a/common/helper_excel.py +++ b/common/helper_excel.py @@ -43,22 +43,27 @@ def shamsi_date(date, in_value=None): return sh_date -def create_header(worksheet, list, num, row, height=None, width=None, color=None, text_color=None, border_style=None): +def create_header(worksheet, list, num, row, height=None, width=None, color=None, text_color=None, border_style=None, + different_color=None): for col_num, option in enumerate(list, num): cell = worksheet.cell(row=row, column=col_num, value=option) col_letter = get_column_letter(col_num) cell.alignment = Alignment_CELL - if color is not None: - if color == 'green': - cell.fill = GREEN_CELL - elif color == 'orange': - cell.fill = ORANGE_CELL - elif color == 'blue': - cell.fill = BLUE_CELL - else: - cell.fill = PatternFill(start_color=color, fill_type="solid") + color_dict = { + 'green': GREEN_CELL, + 'yellow': YELLOW_CELL, + 'blue': BLUE_CELL, + 'red': RED_CELL, + 'light_green': LIGHT_GREEN_CELL, + 'very_light_green': VERY_LIGHT_GREEN_CELL, + 'gray': GRAY_CELL + } + if color in color_dict: + cell.fill = color_dict[color] else: cell.fill = CREAM_CELL + if different_color: + cell.fill = PatternFill(start_color=different_color, fill_type="solid") if text_color is not None: cell.font = Font(size=9, bold=True, color=text_color) else: @@ -107,7 +112,7 @@ def excel_description(worksheet, row1, description, size=None, color=None, my_co worksheet[row1] = description worksheet[row1].alignment = Alignment_CELL if size is not None: - worksheet[row1].font = Font(size=size) + worksheet[row1].font = Font(size=size, bold=True) if color is not None: worksheet[row1].font = red_font if my_color is not None: @@ -119,7 +124,8 @@ def excel_description(worksheet, row1, description, size=None, color=None, my_co def create_value(worksheet, list, l, num, border_style=None, m=None, height=None, color=None, width=None, - different_cell=None, different_value=None, item_num=None, item_color=None): + different_cell=None, different_value=None, item_num=None, item_color=None, + m_color=None): color_dict = { 'green': GREEN_CELL, 'yellow': YELLOW_CELL, @@ -151,7 +157,10 @@ def create_value(worksheet, list, l, num, border_style=None, m=None, height=None cell.font = Font(size=10, bold=True) if m is not None and m % 2 != 0: - cell.fill = PatternFill(start_color="FAF0E7", fill_type="solid") + if m_color: + cell.fill = PatternFill(start_color=m_color, fill_type="solid") + else: + cell.fill = PatternFill(start_color="FAF0E7", fill_type="solid") if height is not None: worksheet.row_dimensions[l + 1].height = height