workaround receive error with many articles

This commit is contained in:
wvengen 2014-08-22 09:35:05 +02:00
parent 63d52db91b
commit d906a7342f

View file

@ -145,23 +145,24 @@ class OrdersController < ApplicationController
# changed, rest_to_tolerance, rest_to_stock, left_over # changed, rest_to_tolerance, rest_to_stock, left_over
counts = [0] * 4 counts = [0] * 4
cunits = [0] * 4 cunits = [0] * 4
OrderArticle.transaction do # This was once wrapped in a transaction, but caused
params[:order_articles].each do |oa_id, oa_params| # "MySQL lock timeout exceeded" errors. It's ok to do
unless oa_params.blank? # this article-by-article anway.
oa = OrderArticle.find(oa_id) params[:order_articles].each do |oa_id, oa_params|
# update attributes; don't use update_attribute because it calls save unless oa_params.blank?
# which makes received_changed? not work anymore oa = OrderArticle.find(oa_id)
oa.attributes = oa_params # update attributes; don't use update_attribute because it calls save
if oa.units_received_changed? # which makes received_changed? not work anymore
counts[0] += 1 oa.attributes = oa_params
unless oa.units_received.blank? if oa.units_received_changed?
cunits[0] += oa.units_received * oa.article.unit_quantity counts[0] += 1
oacounts = oa.redistribute oa.units_received * oa.price.unit_quantity, rest_to unless oa.units_received.blank?
oacounts.each_with_index {|c,i| cunits[i+1]+=c; counts[i+1]+=1 if c>0 } cunits[0] += oa.units_received * oa.article.unit_quantity
end oacounts = oa.redistribute oa.units_received * oa.price.unit_quantity, rest_to
oacounts.each_with_index {|c,i| cunits[i+1]+=c; counts[i+1]+=1 if c>0 }
end end
oa.save!
end end
oa.save!
end end
end end
return nil if counts[0] == 0 return nil if counts[0] == 0