update excel

This commit is contained in:
2026-01-26 16:33:18 +03:30
parent 8be8404b2b
commit 5085e375af
2 changed files with 100 additions and 73 deletions

View File

@@ -2578,8 +2578,7 @@ def import_transporting_detail(request):
def all_products_transport_excel(request): def all_products_transport_excel(request):
filterset_class = AllProductsTransportFilterSet filterset_class = AllProductsTransportFilterSet
transports = AllProductsTransport.objects.filter(trash=False) filters={"trash":False}
product_type = request.GET.get('product_type') product_type = request.GET.get('product_type')
destination_province = request.GET.get('destination_province') destination_province = request.GET.get('destination_province')
date1 = request.GET.get('date1') date1 = request.GET.get('date1')
@@ -2587,26 +2586,52 @@ def all_products_transport_excel(request):
search = request.GET.get('search') search = request.GET.get('search')
if product_type and product_type != 'undefined': if product_type and product_type != 'undefined':
transports = transports.filter(product=product_type) filters['product'] = product_type
if destination_province and destination_province != 'undefined': 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': if date1 and date2 and date1 != 'undefined' and date2 != 'undefined':
try: try:
start_date = datetime.datetime.strptime(str(date1), '%Y-%m-%d') start_date = datetime.datetime.strptime(str(date1), '%Y-%m-%d')
end_date = datetime.datetime.strptime(str(date2), '%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: except ValueError:
pass 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 and search != 'undefined' and search.strip():
if search != 'undefined' and search.strip(): transports = transports.filter(
transports = transports.filter( build_query(filterset_class.Meta.fields, search)
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 = [ excel_options = [
'ردیف', 'ردیف',
@@ -2670,40 +2695,42 @@ def all_products_transport_excel(request):
create_header_freez(worksheet, excel_options, 1, 6, 7, 20) create_header_freez(worksheet, excel_options, 1, 6, 7, 20)
l = 5 l = 5
m = 1 m = 1
if serializer: total_quantity = 0
for data in serializer:
list1 = [
m, # ردیف
data.get("tracking"), for row in transports:
data.get("product"), quantity = row[3] or 0
data.get("items") if data.get("items") else '-', total_quantity += quantity
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 '-',
]
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 # total_quantity = sum(
create_value(worksheet, list1, l + 1, 1) # item['quantity'] for item in serializer)
total_quantity = sum(
item['quantity'] for item in serializer)
# aggregation1_bars = all_bars.aggregate( # aggregation1_bars = all_bars.aggregate(
# total=Sum('GoodAmount'), # total=Sum('GoodAmount'),
# input_total=Sum('GoodAmount', filter=Q(Out=False)), # 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') # create_value(worksheet, value_header_list2, 3, 5, border_style='thin')
list2 = [ list2 = [
'مجموع==>', 'مجموع==>',
'', '',
'', '',
'', '',
total_quantity, total_quantity,
'', '',
'', '',
'', '',
'', '',
'', '',
'', '',
'', '',
'', '',
'', '',
'', '',
'', '',
'', '',
'', '',
'', '',
'', '',
'', '',
# total_count, # total_count,
# total_bars_quantity, # total_bars_quantity,
# input_bars_count, # input_bars_count,
# total_input_bars_quantity, # total_input_bars_quantity,
# total_input_bars_percent, # total_input_bars_percent,
# output_bars_count, # output_bars_count,
# total_output_bars_quantity, # total_output_bars_quantity,
# total_output_bars_percent, # 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) workbook.save(output)
output.seek(0) output.seek(0)