Fix current_order's order_articles preloading

This commit is contained in:
wvengen 2016-06-04 22:13:59 +02:00
parent 78604ae09f
commit 5928640872

View file

@ -2,28 +2,30 @@
class MultipleOrdersByArticles < OrderPdf class MultipleOrdersByArticles < OrderPdf
include OrdersHelper include OrdersHelper
# optimal value depends on the average number of ordergroups ordering an article
# as well as the available memory
BATCH_SIZE = 50
attr_reader :order
def filename def filename
I18n.t('documents.multiple_orders_by_articles.filename', count: @order.count) + '.pdf' I18n.t('documents.multiple_orders_by_articles.filename', count: order.count) + '.pdf'
end end
def title def title
I18n.t('documents.multiple_orders_by_articles.title', count: @order.count) I18n.t('documents.multiple_orders_by_articles.title', count: order.count)
end
def order_articles
@order_articles ||= OrderArticle.joins(:order, :article).where(:orders => {:id => @order}).ordered.reorder('orders.id, articles.name')
end end
# @todo refactor to reduce common code with order_by_articles # @todo refactor to reduce common code with order_by_articles
def body def body
order_articles.each do |order_article| each_order_article do |order_article|
down_or_page down_or_page
rows = [] rows = []
dimrows = [] dimrows = []
has_units_str = '' has_units_str = ''
for goa in order_article.group_order_articles.ordered each_group_order_article_for(order_article) do |goa|
rows << [goa.group_order.ordergroup.name, rows << [goa.group_order.ordergroup_name,
goa.tolerance > 0 ? "#{goa.quantity} + #{goa.tolerance}" : goa.quantity, goa.tolerance > 0 ? "#{goa.quantity} + #{goa.tolerance}" : goa.quantity,
goa.result, goa.result,
number_to_currency(goa.total_price(order_article))] number_to_currency(goa.total_price(order_article))]
@ -70,4 +72,26 @@ class MultipleOrdersByArticles < OrderPdf
super 'order_by_articles' super 'order_by_articles'
end end
def order_articles
OrderArticle.where(order_id: order).ordered.
includes(:article).references(:article).
reorder('order_articles.order_id, articles.name').
preload(:article_price). # preload not join, just in case it went missing
preload(:order, :group_order_articles => {:group_order => :ordergroup})
end
def each_order_article
order_articles.find_each_with_order(batch_size: BATCH_SIZE) {|oa| yield oa }
end
def group_order_articles_for(order_article)
goas = order_article.group_order_articles.to_a
goas.sort_by! {|goa| goa.group_order.ordergroup_name }
goas
end
def each_group_order_article_for(group_order)
group_order_articles_for(group_order).each {|goa| yield goa }
end
end end