From bd91d341eb307b2cb193966f6f303f77fbc7a76c Mon Sep 17 00:00:00 2001 From: puppe1990 Date: Thu, 21 Nov 2024 16:00:37 -0300 Subject: [PATCH] create a new route to count stock --- app/controllers/stocks_controller.rb | 67 ++++++++++++++++++++ app/views/shared/_sidebar.html.erb | 3 + app/views/stocks/index.html.erb | 14 ++-- app/views/stocks/null_stock_details.html.erb | 53 ++++++++++++++++ config/locales/sidebar.en.yml | 3 + config/locales/sidebar.pt-BR.yml | 1 + config/routes.rb | 9 +++ 7 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 app/views/stocks/null_stock_details.html.erb diff --git a/app/controllers/stocks_controller.rb b/app/controllers/stocks_controller.rb index 6e048a0d..13881283 100644 --- a/app/controllers/stocks_controller.rb +++ b/app/controllers/stocks_controller.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/app/views/shared/_sidebar.html.erb b/app/views/shared/_sidebar.html.erb index 1243122a..160fa47b 100644 --- a/app/views/shared/_sidebar.html.erb +++ b/app/views/shared/_sidebar.html.erb @@ -50,6 +50,9 @@
  • <%= link_to t("stocks.shopping_suggestion"), stocks_path, class: 'nav-link' %>
  • +
  • + <%= link_to t("stocks.null_stock_details"), null_stock_details_stocks_path, class: 'nav-link' %> +
  • <%= link_to t("audits.top_selling_products"), top_selling_products_path, class: 'nav-link' %>
  • diff --git a/app/views/stocks/index.html.erb b/app/views/stocks/index.html.erb index 66511f4b..52eb927f 100644 --- a/app/views/stocks/index.html.erb +++ b/app/views/stocks/index.html.erb @@ -3,22 +3,18 @@

    <%= t('stocks.two') %>

    -
    -

    Total de Estoque (Tipo Null): <%= number_with_delimiter(@total_null_balance) %>

    -
    - <%= button_to stocks_path(format: "csv", detailed: true), - class: "btn btn-info", - method: :get, - form: { target: '_blank' } do %> - 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 %> + Contagem de Estoque + <% end %>
    diff --git a/app/views/stocks/null_stock_details.html.erb b/app/views/stocks/null_stock_details.html.erb new file mode 100644 index 00000000..1c82a6f6 --- /dev/null +++ b/app/views/stocks/null_stock_details.html.erb @@ -0,0 +1,53 @@ +
    +
    +
    +

    Contagem de Estoque

    +
    +

    Total de Estoque: <%= number_with_delimiter(@total_null_balance) %>

    +
    +
    +
    + <%= button_to null_stock_details_stocks_path(format: "csv"), + class: "btn btn-success", + method: :get, + form: { target: '_blank' } do %> + Exportar CSV + <% end %> +
    +
    +
    + +
    +
    +
    +
    + + + + + + + + + + + <% @stock_details.each do |detail| %> + + + + + + + <% end %> + +
    SKUSaldo FísicoSaldo AtualDesconto Aplicado
    <%= detail[:sku] %><%= detail[:physical_balance] %><%= detail[:actual_balance] %> + <% if detail[:discounted] %> + Sim + <% else %> + Não + <% end %> +
    +
    +
    +
    +
    \ No newline at end of file diff --git a/config/locales/sidebar.en.yml b/config/locales/sidebar.en.yml index 3349b7b9..f9dd0715 100644 --- a/config/locales/sidebar.en.yml +++ b/config/locales/sidebar.en.yml @@ -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 diff --git a/config/locales/sidebar.pt-BR.yml b/config/locales/sidebar.pt-BR.yml index da792250..6d736ea5 100644 --- a/config/locales/sidebar.pt-BR.yml +++ b/config/locales/sidebar.pt-BR.yml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index a9c4517b..267ec93d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -60,6 +60,7 @@ member do patch :apply_discount end + get :null_stock_details, on: :collection end resources :revenue_estimations resources :accounts @@ -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