Skip to content

Commit

Permalink
update the calculations of forecast
Browse files Browse the repository at this point in the history
  • Loading branch information
puppe1990 committed Sep 30, 2024
1 parent 21dcfe1 commit 5209596
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
15 changes: 7 additions & 8 deletions app/controllers/stocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,23 @@ 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&.physical_balance || 0
total_physical_balance = default_balance ? stock.balance(default_balance) : 0

# Cálculo correto para total_forecast
total_forecast = [total_sold - total_physical_balance, 0].max

warehouse_forecasts = stock.balances.map do |balance|
warehouse_forecast = if balance.deposit_id.to_s == default_warehouse_id
[total_sold - balance.physical_balance, 0].max
else
0
end
[balance.deposit_id, { sold: balance.deposit_id.to_s == default_warehouse_id ? total_sold : 0, forecast: warehouse_forecast }]
warehouse_sold = balance.deposit_id.to_s == default_warehouse_id ? total_sold : 0
warehouse_forecast = [warehouse_sold - stock.balance(balance), 0].max
[balance.deposit_id, { sold: warehouse_sold, forecast: warehouse_forecast }]
end.to_h

[stock, {
warehouses: warehouse_forecasts,
total_sold: total_sold,
total_forecast: total_forecast,
total_balance: total_physical_balance,
total_virtual_balance: default_balance&.virtual_balance || 0
total_virtual_balance: default_balance ? stock.virtual_balance(default_balance) : 0
}]
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/bling_order_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# id :bigint not null, primary key
# aliquotaIPI :decimal(, )
# alteration_date :datetime
# city :string(10485760)
# codigo :string
# collected_alteration_date :date
# date :datetime
Expand All @@ -13,6 +14,7 @@
# descricaoDetalhada :text
# items :jsonb
# quantidade :integer
# state :string(10485760)
# unidade :string
# valor :decimal(, )
# value :decimal(, )
Expand All @@ -28,8 +30,10 @@
#
# Indexes
#
# bling_order_id_index_on_bling_order_items (bling_order_id)
# index_bling_order_items_on_account_id (account_id)
# index_bling_order_items_on_bling_order_id (bling_order_id) UNIQUE
# situation_id_index_on_bling_order_items (situation_id,store_id)
#
class BlingOrderItem < ApplicationRecord
# TODO, refactor me separating the tables
Expand Down
25 changes: 19 additions & 6 deletions app/views/stocks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<th></th>
<th><%= model_class.human_attribute_name(:id) %></th>
<th><%= t('stocks.sku') %></th>
<th><%= t('stocks.total_balance') %></th>
<th><%= t('stocks.total_virtual_balance') %></th>
<th>Physical Balance</th>
<th>Virtual Balance</th>
<th>Quantidade vendida dos últimos 30 dias</th>
<th>Previsão para os próximos 30 dias</th>
<th><%= t('stocks.product') %></th>
Expand All @@ -48,8 +48,13 @@
</td>
<td><%= link_to stock.id, stock_path(stock), data: { toggle: 'tooltip', turbo: false } %></td>
<td><%= stock.product.sku %></td>
<td><%= data[:total_balance] %></td>
<td><%= data[:total_virtual_balance] %></td>
<% sao_paulo_base = stock.balances.find { |b| b.deposit_id.to_s == '9023657532' } %>
<td class="physical-balance">
<%= sao_paulo_base ? stock.balance(sao_paulo_base) : 'N/A' %>
</td>
<td class="virtual-balance">
<%= sao_paulo_base ? stock.virtual_balance(sao_paulo_base) : 'N/A' %>
</td>
<td><%= data[:total_sold] %></td>
<td><%= data[:total_forecast] %></td>
<td><%= stock.product.name %></td>
Expand Down Expand Up @@ -158,11 +163,19 @@

physicalBalanceCell.textContent = data.discounted_physical_balance;
virtualBalanceCell.textContent = data.discounted_virtual_balance;

// 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');
mainPhysicalBalanceCell.textContent = data.discounted_physical_balance;
mainVirtualBalanceCell.textContent = data.discounted_virtual_balance;
}
}
})
.catch(error => console.error('Error:', error));
});
});
}
<% end %>

<% end %>

0 comments on commit 5209596

Please sign in to comment.