From 021657b51d3e1e407b120d566ccf8e07d4024c1f Mon Sep 17 00:00:00 2001 From: puppe1990 Date: Mon, 18 Nov 2024 11:06:45 -0300 Subject: [PATCH] create a filter to tipo_estoque --- app/controllers/stocks_controller.rb | 36 +++++++++++++++++++++------- app/models/product.rb | 6 ++--- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/controllers/stocks_controller.rb b/app/controllers/stocks_controller.rb index efb85b84..57dfa4b5 100644 --- a/app/controllers/stocks_controller.rb +++ b/app/controllers/stocks_controller.rb @@ -66,6 +66,7 @@ def collection @default_situation_balance_filter = params['balance_situation'] @default_sku_filter = params['sku'] @default_period_filter = params['period'] || '30' + @default_tipo_estoque = params['tipo_estoque'] || 'null' stocks = Stock.where(account_id: current_tenant) .includes(:product, :balances) @@ -73,6 +74,18 @@ def collection .filter_by_status(params['status']) .filter_by_total_balance_situation(params['balance_situation']) .filter_by_sku(params['sku']) + .joins(:product) + + stocks = case @default_tipo_estoque + when 'null' + stocks.where(products: { tipo_estoque: nil }) + when 'V' + stocks.where(products: { tipo_estoque: 'V' }) + when 'P' + stocks.where(products: { tipo_estoque: 'P' }) + else + stocks + end @warehouses = Warehouse.where(account_id: current_tenant).pluck(:bling_id, :description).to_h @@ -98,16 +111,20 @@ def collection virtual_balance = default_balance ? default_balance.virtual_balance : 0 in_production = stock.total_in_production - # Calculate discounted balances - discounted_physical_balance = stock.discounted_balance(default_balance) if default_balance - discounted_virtual_balance = stock.discounted_virtual_balance(default_balance) if default_balance + # Calculate discounted balances if default_balance exists + discounted_physical_balance = default_balance ? stock.discounted_balance(default_balance) : 0 + discounted_virtual_balance = default_balance ? stock.discounted_virtual_balance(default_balance) : 0 - # Calculate adjusted balances - adjusted_physical_balance = stock.adjusted_balance(default_balance) if default_balance - adjusted_virtual_balance = stock.adjusted_virtual_balance(default_balance) if default_balance + # Calculate adjusted balances if default_balance exists + adjusted_physical_balance = default_balance ? stock.adjusted_balance(default_balance) : 0 + adjusted_virtual_balance = default_balance ? stock.adjusted_virtual_balance(default_balance) : 0 - # Calculate forecast: total_sold - adjusted_physical_balance - total_forecast = [total_sold - adjusted_physical_balance, 0].max if adjusted_physical_balance + # Calculate forecast only if we have adjusted_physical_balance + total_forecast = if adjusted_physical_balance && !stock.product.composed? + [total_sold - adjusted_physical_balance, 0].max + else + 0 + end [stock, { total_sold: total_sold, @@ -123,7 +140,8 @@ def collection }] end - sorted_stocks = stocks_with_forecasts.sort_by { |_, data| -data[:total_sold] } + # Sort by total_sold, but handle nil values + sorted_stocks = stocks_with_forecasts.sort_by { |_, data| -(data[:total_sold] || 0) } @pagy, @stocks_with_data = pagy_array(sorted_stocks, items: 20) diff --git a/app/models/product.rb b/app/models/product.rb index ca92795c..732d838c 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -7,15 +7,12 @@ # id :bigint not null, primary key # active :boolean # bar_code :string -# componentes :jsonb # extra_sku :string # highlight :boolean -# lancamento_estoque :string # name :string # number_of_pieces_per_fabric_roll :integer # price :float # sku :string -# tipo_estoque :string # created_at :datetime not null # updated_at :datetime not null # account_id :integer @@ -180,6 +177,9 @@ def component_quantities # Check if product is composed of other products def composed? + # Return false if tipo_estoque is nil + return false if tipo_estoque.nil? + tipo_estoque == 'V' && componentes.present? end