Skip to content

Commit

Permalink
fix the sort for quantity
Browse files Browse the repository at this point in the history
  • Loading branch information
puppe1990 committed Sep 4, 2024
1 parent 1ffee87 commit 3b2751a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
5 changes: 5 additions & 0 deletions app/controllers/orders_control_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
13 changes: 6 additions & 7 deletions app/views/orders_control/show_pending_orders.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@
<% end %>

<div class="row justify-content-center">
<% 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| %>
<div class="col-md-12">
<div class="card mb-4">
<div class="card-header bg-primary text-white">
<h5 class="mb-0"><%= loja_name %></h5>
<h5 class="mb-0"><%= loja_name %> - Total: <%= items.sum(&:quantity) %> unidades</h5>
</div>
<div class="card-body">
<% 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| %>
<div class="mb-3">
<button class="btn btn-info btn-block text-left" type="button" data-toggle="collapse" data-target=".collapse<%= codigo %>">
<strong>Código <%= codigo %></strong> - <%= sorted_sku_items.sum { |item| item.quantity.to_i } %> unidades
<strong>Código <%= codigo %></strong> - <%= sku_items.sum(&:quantity) %> unidades
</button>
<div class="collapse collapse<%= codigo %> mt-2">
<div class="table-responsive">
Expand All @@ -46,7 +45,7 @@
</tr>
</thead>
<tbody>
<% sorted_sku_items.each do |item| %>
<% sku_items.sort_by(&:quantity).reverse.each do |item| %>
<tr>
<td>
<%= item.bling_order_item.bling_order_id %>
Expand Down

0 comments on commit 3b2751a

Please sign in to comment.