diff --git a/app/__pycache__/excel_processing.cpython-39.pyc b/app/__pycache__/excel_processing.cpython-39.pyc index c368ed6..91a1fcf 100644 Binary files a/app/__pycache__/excel_processing.cpython-39.pyc and b/app/__pycache__/excel_processing.cpython-39.pyc differ diff --git a/app/__pycache__/views.cpython-39.pyc b/app/__pycache__/views.cpython-39.pyc index db36bc7..2c5413a 100644 Binary files a/app/__pycache__/views.cpython-39.pyc and b/app/__pycache__/views.cpython-39.pyc differ diff --git a/app/excel_processing.py b/app/excel_processing.py index ca23291..67cfb4a 100644 --- a/app/excel_processing.py +++ b/app/excel_processing.py @@ -2629,9 +2629,9 @@ def all_products_transport_excel(request): transports = transports.filter( build_query(filterset_class.Meta.fields, search) ) + transports = transports.order_by('-date', '-create_date') transports = transports.iterator(chunk_size=2000) - excel_options = [ 'ردیف', 'کد رهگیری', @@ -2664,7 +2664,19 @@ def all_products_transport_excel(request): worksheet.insert_rows(1) cell = worksheet.cell(row=1, column=1) cell.alignment = Alignment(horizontal='center', vertical='center') + header_list2 = [ + 'محصول', + 'تعداد بار', + 'حجم بار (کیلوگرم)', + 'تعداد بار داخل استان', + 'حجم بار داخل استان', + 'درصد بار داخل استان', + 'تعداد بار خارج استان', + 'حجم بار خارج استان', + 'درصد بار خارج استان', + ] + create_header(worksheet, header_list2, 5, 2, height=20.8, border_style='thin') if 'date1' in request.GET: date1 = datetime.datetime.strptime(str(request.GET['date1']), '%Y-%m-%d').date() date2 = datetime.datetime.strptime(str(request.GET['date2']), '%Y-%m-%d').date() @@ -2716,6 +2728,30 @@ def all_products_transport_excel(request): l += 1 create_value(worksheet, list1, l + 1, 1) + aggregation = AllProductsTransport.objects.filter(**filters).aggregate( + total=Sum('quantity'), + input_total=Sum('quantity', filter=Q(out=False)), + output_total=Sum('quantity', filter=Q(out=True)), + input_count=Count('id', filter=Q(out=False)), + output_count=Count('id', filter=Q(out=True)), + total_count=Count('id'), + ) + + total_count = aggregation['total_count'] or 0 + total_quantity = aggregation['total'] or 0 + input_quantity = aggregation['input_total'] or 0 + output_quantity = aggregation['output_total'] or 0 + input_count = aggregation['input_count'] or 0 + output_count = aggregation['output_count'] or 0 + + if total_count > 0 and (input_quantity + output_quantity) > 0: + input_percent = round((input_quantity / (input_quantity + output_quantity)) * 100, 1) + output_percent = round((output_quantity / (input_quantity + output_quantity)) * 100, 1) + else: + input_percent = 0 + output_percent = 0 + + list2 = [ 'مجموع==>', @@ -2744,6 +2780,21 @@ def all_products_transport_excel(request): ] create_value(worksheet, list2, l + 3, 1, color='green') + value_header_list2 = [ + product_type if product_type else '-', + int(total_count), + int(total_quantity), + int(input_count), + int(input_quantity), + input_percent, + int(output_count), + int(output_quantity), + output_percent, + + ] + + create_value(worksheet, value_header_list2, 3, 5, border_style='thin') + workbook.save(output) output.seek(0) diff --git a/app/views.py b/app/views.py index 5b302c6..862ada3 100644 --- a/app/views.py +++ b/app/views.py @@ -4510,10 +4510,8 @@ class AllProductsTransportViewSet(viewsets.ModelViewSet): pagination_class = CustomPagination filterset_class = AllProductsTransportFilterSet - def list(self, request, *args, **kwargs): - transports = self.filter_queryset(self.get_queryset()) - + filters = {"trash": False} product_type = request.GET.get('product_type') destination_province = request.GET.get('destination_province') date1 = request.GET.get('date1') @@ -4521,21 +4519,23 @@ class AllProductsTransportViewSet(viewsets.ModelViewSet): search = request.GET.get('search') if product_type and product_type != 'undefined': - transports = transports.filter(product=product_type) + filters['product'] = product_type if destination_province and destination_province != 'undefined': - transports = transports.filter(destination_province=destination_province) + filters['destination_province'] = destination_province if date1 and date2 and date1 != 'undefined' and date2 != 'undefined': try: start_date = datetime.datetime.strptime(str(date1), '%Y-%m-%d') end_date = datetime.datetime.strptime(str(date2), '%Y-%m-%d') - transports = transports.filter(date__gte=start_date, date__lte=end_date) + filters['date__gte'] = start_date + filters['date__lte'] = end_date except ValueError: pass - if search: - if search != 'undefined' and search.strip(): + transports = AllProductsTransport.objects.filter(**filters).order_by('-date', '-create_date') + + if search and search != 'undefined' and search.strip(): transports = transports.filter( build_query(self.filterset_class.Meta.fields, search) )