diff --git a/app/controllers/stocks_controller.rb b/app/controllers/stocks_controller.rb index 179b3149..1c1e1795 100644 --- a/app/controllers/stocks_controller.rb +++ b/app/controllers/stocks_controller.rb @@ -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 @@ -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 diff --git a/app/views/stocks/index.html.erb b/app/views/stocks/index.html.erb index debc5f3d..a3284539 100644 --- a/app/views/stocks/index.html.erb +++ b/app/views/stocks/index.html.erb @@ -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; } } })