diff --git a/app/models/article.rb b/app/models/article.rb index 3755a4b2..a3b4af33 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,6 +1,5 @@ # encoding: utf-8 class Article < ActiveRecord::Base - extend ActiveSupport::Memoizable # Ability to cache method results. Use memoize :expensive_method # Replace numeric seperator with database format localize_input_of :price, :tax, :deposit @@ -44,11 +43,12 @@ class Article < ActiveRecord::Base # If the article is used in an open Order, the Order will be returned. def in_open_order - order_articles = OrderArticle.all(:conditions => ['order_id IN (?)', Order.open.collect(&:id)]) - order_article = order_articles.detect {|oa| oa.article_id == id } - order_article ? order_article.order : nil + @in_open_order ||= begin + order_articles = OrderArticle.all(:conditions => ['order_id IN (?)', Order.open.collect(&:id)]) + order_article = order_articles.detect {|oa| oa.article_id == id } + order_article ? order_article.order : nil + end end - memoize :in_open_order # Returns true if the article has been ordered in the given order at least once def ordered_in_order?(order) diff --git a/app/models/group_order_article.rb b/app/models/group_order_article.rb index a8d0a688..50c1aa0e 100644 --- a/app/models/group_order_article.rb +++ b/app/models/group_order_article.rb @@ -2,7 +2,6 @@ # The chronologically order of the Ordergroup - activity are stored in GroupOrderArticleQuantity # class GroupOrderArticle < ActiveRecord::Base - extend ActiveSupport::Memoizable # Ability to cache method results. Use memoize :expensive_method belongs_to :group_order belongs_to :order_article @@ -101,54 +100,55 @@ class GroupOrderArticle < ActiveRecord::Base # # See description of the ordering algorithm in the general application documentation for details. def calculate_result - quantity = tolerance = 0 - stockit = order_article.article.is_a?(StockArticle) + @calculate_result ||= begin + quantity = tolerance = 0 + stockit = order_article.article.is_a?(StockArticle) - # Get total - total = stockit ? order_article.article.quantity : order_article.units_to_order * order_article.price.unit_quantity - logger.debug("<#{order_article.article.name}>.unitsToOrder => items ordered: #{order_article.units_to_order} => #{total}") + # Get total + total = stockit ? order_article.article.quantity : order_article.units_to_order * order_article.price.unit_quantity + logger.debug("<#{order_article.article.name}>.unitsToOrder => items ordered: #{order_article.units_to_order} => #{total}") - if (total > 0) - # In total there are enough units ordered. Now check the individual result for the ordergroup (group_order). - # - # Get all GroupOrderArticleQuantities for this OrderArticle... - order_quantities = GroupOrderArticleQuantity.all( - :conditions => ["group_order_article_id IN (?)", order_article.group_order_article_ids], :order => 'created_on') - logger.debug("GroupOrderArticleQuantity records found: #{order_quantities.size}") + if (total > 0) + # In total there are enough units ordered. Now check the individual result for the ordergroup (group_order). + # + # Get all GroupOrderArticleQuantities for this OrderArticle... + order_quantities = GroupOrderArticleQuantity.all( + :conditions => ["group_order_article_id IN (?)", order_article.group_order_article_ids], :order => 'created_on') + logger.debug("GroupOrderArticleQuantity records found: #{order_quantities.size}") - # Determine quantities to be ordered... - total_quantity = i = 0 - while (i < order_quantities.size && total_quantity < total) - q = (order_quantities[i].quantity <= total - total_quantity ? order_quantities[i].quantity : total - total_quantity) - total_quantity += q - if (order_quantities[i].group_order_article_id == self.id) - logger.debug("increasing quantity by #{q}") - quantity += q - end - i += 1 - end - - # Determine tolerance to be ordered... - if (total_quantity < total) - logger.debug("determining additional items to be ordered from tolerance") - i = 0 + # Determine quantities to be ordered... + total_quantity = i = 0 while (i < order_quantities.size && total_quantity < total) - q = (order_quantities[i].tolerance <= total - total_quantity ? order_quantities[i].tolerance : total - total_quantity) + q = (order_quantities[i].quantity <= total - total_quantity ? order_quantities[i].quantity : total - total_quantity) total_quantity += q if (order_quantities[i].group_order_article_id == self.id) - logger.debug("increasing tolerance by #{q}") - tolerance += q + logger.debug("increasing quantity by #{q}") + quantity += q end i += 1 end + + # Determine tolerance to be ordered... + if (total_quantity < total) + logger.debug("determining additional items to be ordered from tolerance") + i = 0 + while (i < order_quantities.size && total_quantity < total) + q = (order_quantities[i].tolerance <= total - total_quantity ? order_quantities[i].tolerance : total - total_quantity) + total_quantity += q + if (order_quantities[i].group_order_article_id == self.id) + logger.debug("increasing tolerance by #{q}") + tolerance += q + end + i += 1 + end + end + + logger.debug("determined quantity/tolerance/total: #{quantity} / #{tolerance} / #{quantity + tolerance}") end - logger.debug("determined quantity/tolerance/total: #{quantity} / #{tolerance} / #{quantity + tolerance}") + {:quantity => quantity, :tolerance => tolerance, :total => quantity + tolerance} end - - {:quantity => quantity, :tolerance => tolerance, :total => quantity + tolerance} end - memoize :calculate_result # Returns order result, # either calcualted on the fly or fetched from result attribute