Fixed broken group_orders after editing order.

Caution, when using the model.association = models operator,
the after save callbacks are not triggerd! See Order#save_order_articles
This commit is contained in:
Benjamin Meichsner 2013-03-12 11:26:14 +01:00
parent c18fb20115
commit 40d9195eac
2 changed files with 29 additions and 29 deletions

View file

@ -22,35 +22,25 @@ class GroupOrder < ActiveRecord::Base
data = {}
data[:available_funds] = ordergroup.get_available_funds(self)
unless new_record?
# Group has already ordered, so get the results...
goas = {}
group_order_articles.all.each do |goa|
goas[goa.order_article_id] = {
:quantity => goa.quantity,
:tolerance => goa.tolerance,
:quantity_result => goa.result(:quantity),
:tolerance_result => goa.result(:tolerance),
:total_price => goa.total_price
}
end
end
# load prices and other stuff....
data[:order_articles] = {}
#order.order_articles.each do |order_article|
order.articles_grouped_by_category.each do |article_category, order_articles|
order_articles.each do |order_article|
# Get the result of last time ordering, if possible
goa = group_order_articles.detect { |goa| goa.order_article_id == order_article.id }
# Build hash with relevant data
data[:order_articles][order_article.id] = {
:price => order_article.article.fc_price,
:unit => order_article.article.unit_quantity,
:quantity => (new_record? ? 0 : goas[order_article.id][:quantity]),
:others_quantity => order_article.quantity - (new_record? ? 0 : goas[order_article.id][:quantity]),
:used_quantity => (new_record? ? 0 : goas[order_article.id][:quantity_result]),
:tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance]),
:others_tolerance => order_article.tolerance - (new_record? ? 0 : goas[order_article.id][:tolerance]),
:used_tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance_result]),
:total_price => (new_record? ? 0 : goas[order_article.id][:total_price]),
:quantity => (goa ? goa.quantity : 0),
:others_quantity => order_article.quantity - (goa ? goa.quantity : 0),
:used_quantity => (goa ? goa.result(:quantity) : 0),
:tolerance => (goa ? goa.result(:tolerance) : 0),
:others_tolerance => order_article.tolerance - (goa ? goa.result(:tolerance) : 0),
:used_tolerance => (goa ? goa.result(:tolerance) : 0),
:total_price => (goa ? goa.total_price : 0),
:missing_units => order_article.missing_units,
:quantity_available => (order.stockit? ? order_article.article.quantity_available : 0)
}

View file

@ -214,14 +214,24 @@ class Order < ActiveRecord::Base
end
def save_order_articles
self.articles = Article.find(article_ids) # This doesn't deletes the group_order_articles, belonging to order_articles,
# see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many
#self.articles = Article.find(article_ids) # This doesn't deletes the group_order_articles, belonging to order_articles,
# # see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many
#
## Ensure to delete also the group_order_articles, belonging to order_articles
## This case is relevant, when removing articles from a running order
#goa_ids = GroupOrderArticle.where(group_order_id: group_order_ids).includes(:order_article).
# select { |goa| goa.order_article.nil? }.map(&:id)
#GroupOrderArticle.delete_all(id: goa_ids) unless goa_ids.empty?
# Ensure to delete also the group_order_articles, belonging to order_articles
# This case is relevant, when removing articles from a running order
goa_ids = GroupOrderArticle.where(group_order_id: group_order_ids).includes(:order_article).
select { |goa| goa.order_article.nil? }.map(&:id)
GroupOrderArticle.delete_all(id: goa_ids) unless goa_ids.empty?
# fetch selected articles
articles_list = Article.find(article_ids)
# create new order_articles
(articles_list - articles).each { |article| order_articles.create(:article => article) }
# delete old order_articles
articles.reject { |article| articles_list.include?(article) }.each do |article|
order_articles.detect { |order_article| order_article.article_id == article.id }.destroy
end
end
private