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:
parent
c18fb20115
commit
40d9195eac
2 changed files with 29 additions and 29 deletions
|
@ -22,35 +22,25 @@ class GroupOrder < ActiveRecord::Base
|
||||||
data = {}
|
data = {}
|
||||||
data[:available_funds] = ordergroup.get_available_funds(self)
|
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....
|
# load prices and other stuff....
|
||||||
data[:order_articles] = {}
|
data[:order_articles] = {}
|
||||||
#order.order_articles.each do |order_article|
|
|
||||||
order.articles_grouped_by_category.each do |article_category, order_articles|
|
order.articles_grouped_by_category.each do |article_category, order_articles|
|
||||||
order_articles.each do |order_article|
|
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] = {
|
data[:order_articles][order_article.id] = {
|
||||||
:price => order_article.article.fc_price,
|
:price => order_article.article.fc_price,
|
||||||
:unit => order_article.article.unit_quantity,
|
:unit => order_article.article.unit_quantity,
|
||||||
:quantity => (new_record? ? 0 : goas[order_article.id][:quantity]),
|
:quantity => (goa ? goa.quantity : 0),
|
||||||
:others_quantity => order_article.quantity - (new_record? ? 0 : goas[order_article.id][:quantity]),
|
:others_quantity => order_article.quantity - (goa ? goa.quantity : 0),
|
||||||
:used_quantity => (new_record? ? 0 : goas[order_article.id][:quantity_result]),
|
:used_quantity => (goa ? goa.result(:quantity) : 0),
|
||||||
:tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance]),
|
:tolerance => (goa ? goa.result(:tolerance) : 0),
|
||||||
:others_tolerance => order_article.tolerance - (new_record? ? 0 : goas[order_article.id][:tolerance]),
|
:others_tolerance => order_article.tolerance - (goa ? goa.result(:tolerance) : 0),
|
||||||
:used_tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance_result]),
|
:used_tolerance => (goa ? goa.result(:tolerance) : 0),
|
||||||
:total_price => (new_record? ? 0 : goas[order_article.id][:total_price]),
|
:total_price => (goa ? goa.total_price : 0),
|
||||||
:missing_units => order_article.missing_units,
|
:missing_units => order_article.missing_units,
|
||||||
:quantity_available => (order.stockit? ? order_article.article.quantity_available : 0)
|
:quantity_available => (order.stockit? ? order_article.article.quantity_available : 0)
|
||||||
}
|
}
|
||||||
|
|
|
@ -214,14 +214,24 @@ class Order < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_order_articles
|
def save_order_articles
|
||||||
self.articles = Article.find(article_ids) # This doesn't deletes the group_order_articles, belonging to 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
|
# # 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
|
# fetch selected articles
|
||||||
goa_ids = GroupOrderArticle.where(group_order_id: group_order_ids).includes(:order_article).
|
articles_list = Article.find(article_ids)
|
||||||
select { |goa| goa.order_article.nil? }.map(&:id)
|
# create new order_articles
|
||||||
GroupOrderArticle.delete_all(id: goa_ids) unless goa_ids.empty?
|
(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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
Loading…
Reference in a new issue