Skip to content

Commit

Permalink
create a filter to tipo_estoque
Browse files Browse the repository at this point in the history
  • Loading branch information
puppe1990 committed Nov 18, 2024
1 parent 787cf58 commit 021657b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
36 changes: 27 additions & 9 deletions app/controllers/stocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,26 @@ 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)
.only_positive_price(true)
.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

Expand All @@ -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,
Expand All @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 021657b

Please sign in to comment.