Skip to content

Commit

Permalink
update to include products in production in the stock index
Browse files Browse the repository at this point in the history
  • Loading branch information
puppe1990 committed Oct 1, 2024
1 parent 2726eb9 commit fa428a6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 41 deletions.
43 changes: 18 additions & 25 deletions app/controllers/stocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -87,33 +87,26 @@ 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

[stock, {
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

Expand All @@ -122,4 +115,4 @@ def collection
@pagy, @stocks_with_data = pagy_array(sorted_stocks)
end

end
end
21 changes: 19 additions & 2 deletions app/models/stock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 9 additions & 13 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>Physical Balance</th>
<th>Virtual Balance</th>
<th><%= t('stocks.physical_balance') %></th>
<th><%= t('stocks.in_production') %></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 @@ -52,11 +52,9 @@
<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 class="in-production"><%= data[:total_in_production] %></td>
<td><%= data[:total_sold] %></td>
<td><%= data[:total_forecast] %></td>
<td class="forecast"><%= data[:total_forecast] %></td>
<td><%= stock.product.name %></td>
<td>
<%= link_to icon('fas fa-eye'), stock, title: t('show'), class: 'btn btn-info', data: { toggle: 'tooltip', turbo: false } %>
Expand All @@ -70,8 +68,8 @@
<tr>
<th>Apply Discount</th>
<th>Warehouse</th>
<th>Physical Balance</th>
<th>Virtual Balance</th>
<th><%= t('stocks.physical_balance') %></th>
<th><%= t('stocks.in_production') %></th>
<th>Quantidade vendida dos últimos 30 dias</th>
<th>Previsão para os próximos 30 dias</th>
</tr>
Expand Down Expand Up @@ -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;
}
}
Expand Down
4 changes: 3 additions & 1 deletion config/locales/pt-BR.models.stocks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
calculate_basic_forecast: Previsão para os próximos 30 dias
physical_balance: Saldo Físico
in_production: Em Produção

0 comments on commit fa428a6

Please sign in to comment.