Performance improvement for stockit view.

This commit is contained in:
benni 2012-07-31 17:37:32 +02:00
parent 9ac747e9f3
commit ef2ce98e46
2 changed files with 4 additions and 10 deletions

View file

@ -1,7 +1,7 @@
class StockitController < ApplicationController
def index
@stock_articles = StockArticle.joins(:supplier, :article_category).
@stock_articles = StockArticle.includes(:supplier, :article_category).
order('suppliers.name, article_categories.name, articles.name')
end

View file

@ -14,15 +14,9 @@ class StockArticle < Article
end
# Check for unclosed orders and substract its ordered quantity
def quantity_available(exclude_order = nil)
available = quantity
for order in Order.stockit.all(:conditions => "state = 'open' OR state = 'finished'")
unless order == exclude_order
order_article = order.order_articles.first(:conditions => {:article_id => id})
available -= order_article.units_to_order if order_article
end
end
available
def quantity_available
quantity - OrderArticle.where(article_id: id).
joins(:order).where("orders.state = 'open' OR orders.state = 'finished'").sum(:units_to_order)
end
def self.stock_value