From 3b2751a4c8b7c8c3e7ee4e392410c8e23f61c789 Mon Sep 17 00:00:00 2001 From: puppe1990 Date: Wed, 4 Sep 2024 17:31:10 -0300 Subject: [PATCH] fix the sort for quantity --- app/controllers/orders_control_controller.rb | 5 +++++ .../orders_control/show_pending_orders.html.erb | 13 ++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/controllers/orders_control_controller.rb b/app/controllers/orders_control_controller.rb index 6339559a..0e9e229b 100644 --- a/app/controllers/orders_control_controller.rb +++ b/app/controllers/orders_control_controller.rb @@ -39,6 +39,11 @@ def show_pending_orders @all_items = pending_items end + # Group items by store and sort by total quantity + @sorted_stores = @all_items.group_by { |item| item.bling_order_item.store_name } + .sort_by { |_, items| -items.sum(&:quantity) } + .to_h + respond_to do |format| format.html # show.html.erb format.csv { send_data generate_csv(@all_items), filename: "pending-orders-#{Date.today}.csv" } diff --git a/app/views/orders_control/show_pending_orders.html.erb b/app/views/orders_control/show_pending_orders.html.erb index d52028bd..5edb8fa0 100644 --- a/app/views/orders_control/show_pending_orders.html.erb +++ b/app/views/orders_control/show_pending_orders.html.erb @@ -20,19 +20,18 @@ <% end %>
- <% if @all_items.present? %> - <% @all_items.group_by { |item| item.bling_order_item.store_name }.each do |loja_name, items| %> + <% if @sorted_stores.present? %> + <% @sorted_stores.each do |loja_name, items| %>
-
<%= loja_name %>
+
<%= loja_name %> - Total: <%= items.sum(&:quantity) %> unidades
- <% items.group_by(&:sku).each do |codigo, sku_items| %> - <% sorted_sku_items = sku_items.sort_by { |item| -item.quantity.to_i } %> + <% items.group_by(&:sku).sort_by { |_, sku_items| -sku_items.sum(&:quantity) }.each do |codigo, sku_items| %>
@@ -46,7 +45,7 @@ - <% sorted_sku_items.each do |item| %> + <% sku_items.sort_by(&:quantity).reverse.each do |item| %> <%= item.bling_order_item.bling_order_id %>