do not store GroupOrderArticles with zero quantity and tolerance

This commit is contained in:
wvengen 2014-03-18 17:29:40 +01:00
parent ce0ee6773a
commit 25854f2de7
4 changed files with 22 additions and 7 deletions

View file

@ -13,7 +13,7 @@ class GroupOrderArticle < ActiveRecord::Base
validates_inclusion_of :tolerance, :in => 0..99
validates_uniqueness_of :order_article_id, :scope => :group_order_id # just once an article per group order
scope :ordered, -> { includes(:group_order => :ordergroup).where('group_order_articles.result > 0 OR group_order_articles.quantity > 0 OR group_order_articles.tolerance > 0').order('groups.name') }
scope :ordered, -> { includes(:group_order => :ordergroup).order(:groups => :name) }
localize_input_of :result
@ -35,6 +35,13 @@ class GroupOrderArticle < ActiveRecord::Base
logger.debug("GroupOrderArticle[#{id}].update_quantities(#{quantity}, #{tolerance})")
logger.debug("Current quantity = #{self.quantity}, tolerance = #{self.tolerance}")
# When quantity and tolerance are zero, we don't serve any purpose
if quantity == 0 and tolerance == 0
logger.debug("Self-destructing since requested quantity and tolerance are zero")
destroy!
return
end
# Get quantities ordered with the newest item first.
quantities = group_order_article_quantities.order('created_on DESC').to_a
logger.debug("GroupOrderArticleQuantity items found: #{quantities.size}")