Skip to content

Commit

Permalink
create a new route to count stock
Browse files Browse the repository at this point in the history
  • Loading branch information
puppe1990 committed Nov 21, 2024
1 parent 0432b79 commit bd91d34
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 9 deletions.
67 changes: 67 additions & 0 deletions app/controllers/stocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,56 @@ def apply_discount
end
end

def null_stock_details
default_warehouse_id = '9023657532' # ID for Estoque São Paulo Base

@null_tipo_stocks = Stock.where(account_id: current_tenant)
.joins(:product)
.includes(:product, :balances)
.where(products: { tipo_estoque: nil })
.to_a

@stock_details = @null_tipo_stocks.map do |stock|
balance = stock.balances.find { |b| b.deposit_id.to_s == default_warehouse_id }
physical_balance = balance&.physical_balance || 0

actual_balance = if stock.discounted_warehouse_sku_id == "#{default_warehouse_id}_#{stock.product.sku}"
discounted = physical_balance - 1000
discounted <= 0 ? 0 : discounted
else
if physical_balance >= 1000
physical_balance - 1000
elsif physical_balance <= 0
0
else
physical_balance
end
end

{
sku: stock.product.sku,
physical_balance: physical_balance,
actual_balance: actual_balance,
discounted: stock.discounted_warehouse_sku_id.present?
}
end

# Sort by actual_balance in descending order
@stock_details = @stock_details.sort_by { |detail| -detail[:actual_balance] }
@total_null_balance = @stock_details.sum { |detail| detail[:actual_balance] }

respond_to do |format|
format.html
format.csv do
csv_data = generate_null_stock_details_csv(@stock_details)
send_data csv_data,
filename: "null_stock_details_#{Date.today}.csv",
type: 'text/csv; charset=utf-8',
disposition: 'attachment'
end
end
end

protected

def collection
Expand Down Expand Up @@ -280,4 +330,21 @@ def generate_regular_csv
end
end
end

def generate_null_stock_details_csv(stock_details)
require 'csv'

CSV.generate(headers: true, col_sep: ';', encoding: 'UTF-8') do |csv|
csv << ['SKU', 'Saldo Físico', 'Saldo Atual', 'Desconto Aplicado']

stock_details.each do |detail|
csv << [
detail[:sku],
detail[:physical_balance],
detail[:actual_balance],
detail[:discounted] ? 'Sim' : 'Não'
]
end
end
end
end
3 changes: 3 additions & 0 deletions app/views/shared/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
<li class="<%= active_nav_item('stocks', 'index') %>">
<%= link_to t("stocks.shopping_suggestion"), stocks_path, class: 'nav-link' %>
</li>
<li class="<%= active_nav_item('stocks', 'null_stock_details') %>">
<%= link_to t("stocks.null_stock_details"), null_stock_details_stocks_path, class: 'nav-link' %>
</li>
<li class="<%= active_nav_menu_item([top_selling_products_path]) %>">
<%= link_to t("audits.top_selling_products"), top_selling_products_path, class: 'nav-link' %>
</li>
Expand Down
14 changes: 5 additions & 9 deletions app/views/stocks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@
<div class="d-flex justify-content-between align-items-center w-100">
<div class="d-flex align-items-center">
<h1 class="me-3"><%= t('stocks.two') %></h1>
<div class="total-balance-counter">
<h4>Total de Estoque (Tipo Null): <span class="badge bg-primary"><%= number_with_delimiter(@total_null_balance) %></span></h4>
</div>
</div>
<div class="d-flex gap-2">
<%= button_to stocks_path(format: "csv", detailed: true),
class: "btn btn-info",
method: :get,
form: { target: '_blank' } do %>
<i class="fas fa-download"></i> Cálculo Detalhado
<% end %>
<%= button_to stocks_path(format: "csv"),
class: "btn btn-success",
method: :get do %>
Gerar Previsão de Venda
<% end %>
<%= link_to null_stock_details_stocks_path,
class: "btn btn-primary",
data: { turbo: false } do %>
<i class="fas fa-list"></i> Contagem de Estoque
<% end %>
</div>
</div>
</div>
Expand Down
53 changes: 53 additions & 0 deletions app/views/stocks/null_stock_details.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<div class="section-header">
<div class="d-flex justify-content-between align-items-center w-100">
<div class="d-flex align-items-center">
<h1 class="me-3">Contagem de Estoque</h1>
<div class="total-balance-counter">
<h4>Total de Estoque: <span class="badge bg-primary"><%= number_with_delimiter(@total_null_balance) %></span></h4>
</div>
</div>
<div class="d-flex gap-2">
<%= button_to null_stock_details_stocks_path(format: "csv"),
class: "btn btn-success",
method: :get,
form: { target: '_blank' } do %>
<i class="fas fa-download"></i> Exportar CSV
<% end %>
</div>
</div>
</div>

<div class="section-body">
<div class="card">
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>SKU</th>
<th>Saldo Físico</th>
<th>Saldo Atual</th>
<th>Desconto Aplicado</th>
</tr>
</thead>
<tbody>
<% @stock_details.each do |detail| %>
<tr>
<td><%= detail[:sku] %></td>
<td><%= detail[:physical_balance] %></td>
<td><%= detail[:actual_balance] %></td>
<td>
<% if detail[:discounted] %>
<span class="badge bg-success">Sim</span>
<% else %>
<span class="badge bg-secondary">Não</span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
3 changes: 3 additions & 0 deletions config/locales/sidebar.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ en:
products: Orders controls
daily: Sale daily audits
top_selling_products: Top Selling Products
stocks:
shopping_suggestion: Shopping Suggestion
null_stock_details: Null Stock Details
1 change: 1 addition & 0 deletions config/locales/sidebar.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pt-BR:
create: Criar Loja
stocks:
shopping_suggestion: Sugestão de Compra
null_stock_details: Contagem de Estoque
uploads:
one: Upload
shein_orders: Upload de Pedidos Shein
Expand Down
9 changes: 9 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
member do
patch :apply_discount
end
get :null_stock_details, on: :collection
end
resources :revenue_estimations
resources :accounts
Expand Down Expand Up @@ -187,4 +188,12 @@

get 'qr_scanner', to: 'qr_scanner#index', as: 'qr_scanner'

resources :excel_processors, only: [:index] do
collection do
post :process_file
get :download_excel
get :download_image
end
end

end

0 comments on commit bd91d34

Please sign in to comment.