Skip to content

Commit

Permalink
update the calculation with ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
puppe1990 committed Sep 30, 2024
1 parent 5209596 commit 2726eb9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
37 changes: 32 additions & 5 deletions app/controllers/stocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,30 @@ def apply_discount
end

balance = @stock.balances.find_by(deposit_id: warehouse_id)

# Fetch the total sold for this stock
start_date = 1.month.ago.to_date
end_date = Date.today
total_sold = Item.joins(:bling_order_item)
.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

respond_to do |format|
format.json {
render json: {
success: true,
discounted_physical_balance: @stock.discounted_balance(balance),
discounted_virtual_balance: @stock.discounted_virtual_balance(balance)
discounted_physical_balance: new_balance,
discounted_virtual_balance: @stock.virtual_balance(balance),
new_forecast: new_forecast,
total_sold: total_sold
}
}
end
Expand Down Expand Up @@ -72,12 +89,22 @@ def collection
default_balance = stock.balances.find { |b| b.deposit_id.to_s == default_warehouse_id }
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
# 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

warehouse_forecasts = stock.balances.map do |balance|
warehouse_sold = balance.deposit_id.to_s == default_warehouse_id ? total_sold : 0
warehouse_forecast = [warehouse_sold - stock.balance(balance), 0].max
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
[balance.deposit_id, { sold: warehouse_sold, forecast: warehouse_forecast }]
end.to_h

Expand Down
4 changes: 4 additions & 0 deletions app/views/stocks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,21 @@
const row = this.closest('tr');
const physicalBalanceCell = row.querySelector('.physical-balance');
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;
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;
mainForecastCell.textContent = data.new_forecast;
}
}
})
Expand Down

0 comments on commit 2726eb9

Please sign in to comment.