diff --git a/app/__pycache__/excel_processing.cpython-39.pyc b/app/__pycache__/excel_processing.cpython-39.pyc index b9041bc..c368ed6 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/excel_processing.py b/app/excel_processing.py index a65ecda..c45729c 100644 --- a/app/excel_processing.py +++ b/app/excel_processing.py @@ -2578,8 +2578,7 @@ def import_transporting_detail(request): def all_products_transport_excel(request): filterset_class = AllProductsTransportFilterSet - transports = AllProductsTransport.objects.filter(trash=False) - + filters={"trash":False} product_type = request.GET.get('product_type') destination_province = request.GET.get('destination_province') date1 = request.GET.get('date1') @@ -2587,26 +2586,52 @@ def all_products_transport_excel(request): 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 + transports = ( + AllProductsTransport.objects + .filter(**filters) + .values_list( + "tracking", + "product", + "items", + "quantity", + "unit", + "date", + "destination", + "jihadi_destination", + "destination_province", + "destination_city", + "origin", + "jihadi_origin", + "origin_province", + "origin_city", + "destination_prev", + "destination_changed", + "car_tracking_code", + "unloading_date", + "unloading", + ) + ) - if search: - if search != 'undefined' and search.strip(): - transports = transports.filter( - build_query(filterset_class.Meta.fields, search) - ) + if search and search != 'undefined' and search.strip(): + transports = transports.filter( + build_query(filterset_class.Meta.fields, search) + ) + transports = transports.iterator(chunk_size=2000) - serializer = AllProductsTransportCustomSerializer(transports.order_by('-date', '-create_date'), many=True).data + # serializer = AllProductsTransportCustomSerializer(transports.order_by('-date', '-create_date'), many=True).data excel_options = [ 'ردیف', @@ -2670,40 +2695,42 @@ def all_products_transport_excel(request): create_header_freez(worksheet, excel_options, 1, 6, 7, 20) l = 5 m = 1 - if serializer: - for data in serializer: - list1 = [ - m, # ردیف + total_quantity = 0 - data.get("tracking"), - data.get("product"), - data.get("items") if data.get("items") else '-', - data.get("quantity"), - data.get("unit"), - str(shamsi_date(convert_str_to_date(data.get("date")),in_value=True)) if data.get('date') else '-', - data.get("destination"), - data.get("jihadi_destination"), - data.get("destination_province"), - data.get("destination_city"), - data.get("origin"), - data.get("jihadi_origin"), - data.get("origin_province"), - data.get("origin_city"), - "داخل استان" if data.get("origin_province") == data.get("destination_province") else "خارج استان", - data.get("destination_prev") if data.get("destination_prev") else '-', - data.get("destination_changed") if data.get("destination_changed") else '-', - data.get("car_tracking_code"), - str(shamsi_date(convert_str_to_date(data.get("unloading_date")), in_value=True)) if data.get("unloading_date") else '-', - data.get("unloading") if data.get("unloading") else '-', - ] + for row in transports: + quantity = row[3] or 0 + total_quantity += quantity + list1 = [ + m, + row[0], # tracking + row[1], # product + row[2] or '-', # items + quantity, + row[4], # unit + str(shamsi_date(convert_str_to_date(row[5]), in_value=True)) if row[5] else '-', + row[6], # destination + row[7], # jihadi_destination + row[8], # destination_province + row[9], # destination_city + row[10], # origin + row[11], # jihadi_origin + row[12], # origin_province + row[13], # origin_city + "داخل استان" if row[12] == row[8] else "خارج استان", + row[14] or '-', # destination_prev + row[15] or '-', # destination_changed + row[16], # car_tracking_code + str(shamsi_date(convert_str_to_date(row[17]), in_value=True)) if row[17] else '-', + row[18] or '-', # unloading + ] - m += 1 + m += 1 + l += 1 + create_value(worksheet, list1, l + 1, 1) - l += 1 - create_value(worksheet, list1, l + 1, 1) - total_quantity = sum( - item['quantity'] for item in serializer) + # total_quantity = sum( + # item['quantity'] for item in serializer) # aggregation1_bars = all_bars.aggregate( # total=Sum('GoodAmount'), # input_total=Sum('GoodAmount', filter=Q(Out=False)), @@ -2752,39 +2779,39 @@ def all_products_transport_excel(request): # # create_value(worksheet, value_header_list2, 3, 5, border_style='thin') - list2 = [ - 'مجموع==>', - '', - '', - '', - total_quantity, - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - # total_count, - # total_bars_quantity, - # input_bars_count, - # total_input_bars_quantity, - # total_input_bars_percent, - # output_bars_count, - # total_output_bars_quantity, - # total_output_bars_percent, + list2 = [ + 'مجموع==>', + '', + '', + '', + total_quantity, + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + # total_count, + # total_bars_quantity, + # input_bars_count, + # total_input_bars_quantity, + # total_input_bars_percent, + # output_bars_count, + # total_output_bars_quantity, + # total_output_bars_percent, - ] - create_value(worksheet, list2, l + 3, 1, color='green') + ] + create_value(worksheet, list2, l + 3, 1, color='green') workbook.save(output) output.seek(0)