From fa428a6dc0465a59f3aa65435bc35b5ac092b2be Mon Sep 17 00:00:00 2001 From: puppe1990 Date: Tue, 1 Oct 2024 13:36:17 -0300 Subject: [PATCH] update to include products in production in the stock index --- app/controllers/stocks_controller.rb | 43 +++++++++++--------------- app/models/stock.rb | 21 +++++++++++-- app/views/stocks/index.html.erb | 22 ++++++------- config/locales/pt-BR.models.stocks.yml | 4 ++- 4 files changed, 49 insertions(+), 41 deletions(-) diff --git a/app/controllers/stocks_controller.rb b/app/controllers/stocks_controller.rb index 1c1e1795..87f93027 100644 --- a/app/controllers/stocks_controller.rb +++ b/app/controllers/stocks_controller.rb @@ -35,20 +35,20 @@ def apply_discount .where(account_id: current_tenant, sku: sku, bling_order_items: { date: start_date..end_date }) .sum(:quantity) - # Calculate the new forecast - new_balance = @stock.balance(balance) - new_forecast = if new_balance >= 0 - [total_sold - new_balance, 0].max - else - total_sold + new_balance.abs - end + physical_balance = @stock.balance(balance) + virtual_balance = @stock.virtual_balance(balance) + in_production = @stock.total_in_production + + # Calculate the new forecast using the correct formula + new_forecast = [total_sold - (physical_balance + in_production), 0].max respond_to do |format| format.json { render json: { success: true, - discounted_physical_balance: new_balance, - discounted_virtual_balance: @stock.virtual_balance(balance), + physical_balance: physical_balance, + virtual_balance: virtual_balance, + in_production: in_production, new_forecast: new_forecast, total_sold: total_sold } @@ -87,24 +87,17 @@ def collection total_sold = items_sold[stock.product.sku] || 0 default_balance = stock.balances.find { |b| b.deposit_id.to_s == default_warehouse_id } - total_physical_balance = default_balance ? stock.balance(default_balance) : 0 + physical_balance = default_balance ? stock.balance(default_balance) : 0 + total_in_production = stock.total_in_production - # Separate calculation for total_forecast based on balance - total_forecast = if total_physical_balance >= 0 - [total_sold - total_physical_balance, 0].max - else - total_sold + total_physical_balance.abs - end + # Calculate total_forecast using the correct formula + total_forecast = [total_sold - (physical_balance + total_in_production), 0].max warehouse_forecasts = stock.balances.map do |balance| warehouse_sold = balance.deposit_id.to_s == default_warehouse_id ? total_sold : 0 balance_value = stock.balance(balance) - # Separate calculation for warehouse_forecast based on balance - warehouse_forecast = if balance_value >= 0 - [warehouse_sold - balance_value, 0].max - else - warehouse_sold + balance_value.abs - end + # Calculate warehouse_forecast using the correct formula + warehouse_forecast = [warehouse_sold - (balance_value + total_in_production), 0].max [balance.deposit_id, { sold: warehouse_sold, forecast: warehouse_forecast }] end.to_h @@ -112,8 +105,8 @@ def collection warehouses: warehouse_forecasts, total_sold: total_sold, total_forecast: total_forecast, - total_balance: total_physical_balance, - total_virtual_balance: default_balance ? stock.virtual_balance(default_balance) : 0 + physical_balance: physical_balance, + total_in_production: total_in_production }] end @@ -122,4 +115,4 @@ def collection @pagy, @stocks_with_data = pagy_array(sorted_stocks) end -end +end \ No newline at end of file diff --git a/app/models/stock.rb b/app/models/stock.rb index 2a3652ac..947d8c45 100644 --- a/app/models/stock.rb +++ b/app/models/stock.rb @@ -138,18 +138,35 @@ def virtual_balance(balance) end def discounted_balance(balance) - if discounted_warehouse_sku_id == "#{balance.deposit_id}_#{self.product.sku}" + base_balance = if discounted_warehouse_sku_id == "#{balance.deposit_id}_#{self.product.sku}" balance.physical_balance - 1000 else balance.physical_balance end + base_balance + total_in_production end def discounted_virtual_balance(balance) - if discounted_warehouse_sku_id == "#{balance.deposit_id}_#{self.product.sku}" + base_balance = if discounted_warehouse_sku_id == "#{balance.deposit_id}_#{self.product.sku}" balance.virtual_balance - 1000 else balance.virtual_balance end + base_balance + total_in_production + end + + def total_in_production + ProductionProduct.joins(:production) + .where(product_id: self.product_id) + .where(productions: { confirmed: [false, nil] }) + .sum('quantity - COALESCE(pieces_delivered, 0) - COALESCE(dirty, 0) - COALESCE(error, 0) - COALESCE(discard, 0)') + end + + def adjusted_total_balance + total_balance + total_in_production + end + + def adjusted_total_virtual_balance + total_virtual_balance + total_in_production end end diff --git a/app/views/stocks/index.html.erb b/app/views/stocks/index.html.erb index a3284539..fe3c3da5 100644 --- a/app/views/stocks/index.html.erb +++ b/app/views/stocks/index.html.erb @@ -28,8 +28,8 @@ <%= model_class.human_attribute_name(:id) %> <%= t('stocks.sku') %> - Physical Balance - Virtual Balance + <%= t('stocks.physical_balance') %> + <%= t('stocks.in_production') %> Quantidade vendida dos últimos 30 dias Previsão para os próximos 30 dias <%= t('stocks.product') %> @@ -52,11 +52,9 @@ <%= sao_paulo_base ? stock.balance(sao_paulo_base) : 'N/A' %> - - <%= sao_paulo_base ? stock.virtual_balance(sao_paulo_base) : 'N/A' %> - + <%= data[:total_in_production] %> <%= data[:total_sold] %> - <%= data[:total_forecast] %> + <%= data[:total_forecast] %> <%= stock.product.name %> <%= link_to icon('fas fa-eye'), stock, title: t('show'), class: 'btn btn-info', data: { toggle: 'tooltip', turbo: false } %> @@ -70,8 +68,8 @@ Apply Discount Warehouse - Physical Balance - Virtual Balance + <%= t('stocks.physical_balance') %> + <%= t('stocks.in_production') %> Quantidade vendida dos últimos 30 dias Previsão para os próximos 30 dias @@ -162,18 +160,16 @@ const virtualBalanceCell = row.querySelector('.virtual-balance'); const forecastCell = row.querySelector('td:nth-child(6)'); - physicalBalanceCell.textContent = data.discounted_physical_balance; - virtualBalanceCell.textContent = data.discounted_virtual_balance; + physicalBalanceCell.textContent = data.physical_balance; + virtualBalanceCell.textContent = data.virtual_balance; forecastCell.textContent = data.new_forecast; // Update the main table row if (warehouseId === '9023657532') { const mainRow = row.closest('.collapse-row').previousElementSibling; const mainPhysicalBalanceCell = mainRow.querySelector('.physical-balance'); - const mainVirtualBalanceCell = mainRow.querySelector('.virtual-balance'); const mainForecastCell = mainRow.querySelector('td:nth-child(7)'); - mainPhysicalBalanceCell.textContent = data.discounted_physical_balance; - mainVirtualBalanceCell.textContent = data.discounted_virtual_balance; + mainPhysicalBalanceCell.textContent = data.physical_balance; mainForecastCell.textContent = data.new_forecast; } } diff --git a/config/locales/pt-BR.models.stocks.yml b/config/locales/pt-BR.models.stocks.yml index 3f098e8a..f212abab 100644 --- a/config/locales/pt-BR.models.stocks.yml +++ b/config/locales/pt-BR.models.stocks.yml @@ -11,4 +11,6 @@ pt-BR: list: Lista shopping_suggestion: Sugestão de compras count_sold: Quantidade vendida dos últimos 30 dias - calculate_basic_forecast: Previsão para os próximos 30 dias \ No newline at end of file + calculate_basic_forecast: Previsão para os próximos 30 dias + physical_balance: Saldo Físico + in_production: Em Produção \ No newline at end of file