Rewrite OrderPdfs to support multiple orders at once
This commit is contained in:
parent
80899afcb0
commit
3e156bbbf3
11 changed files with 457 additions and 408 deletions
|
|
@ -1,12 +1,6 @@
|
|||
# encoding: utf-8
|
||||
class OrderByArticles < OrderPdf
|
||||
|
||||
# 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
|
||||
I18n.t('documents.order_by_articles.filename', :name => order.name, :date => order.ends.to_date) + '.pdf'
|
||||
end
|
||||
|
|
@ -18,59 +12,30 @@ class OrderByArticles < OrderPdf
|
|||
|
||||
def body
|
||||
each_order_article do |order_article|
|
||||
down_or_page
|
||||
|
||||
rows = []
|
||||
dimrows = []
|
||||
each_group_order_article_for(order_article) do |goa|
|
||||
rows << [goa.group_order.ordergroup_name,
|
||||
"#{goa.quantity} + #{goa.tolerance}",
|
||||
goa.result,
|
||||
number_to_currency(goa.total_price(order_article))]
|
||||
dimrows << rows.length if goa.result == 0
|
||||
end
|
||||
next if rows.length == 0
|
||||
rows.unshift I18n.t('documents.order_by_articles.rows') # table header
|
||||
rows = [[
|
||||
GroupOrder.human_attribute_name(:ordergroup),
|
||||
GroupOrderArticle.human_attribute_name(:ordered),
|
||||
GroupOrderArticle.human_attribute_name(:received),
|
||||
GroupOrderArticle.human_attribute_name(:total_price)
|
||||
]]
|
||||
|
||||
text "#{order_article.article.name} (#{order_article.article.unit} | #{order_article.price.unit_quantity.to_s} | #{number_to_currency(order_article.price.fc_price)})",
|
||||
style: :bold, size: fontsize(10)
|
||||
table rows, cell_style: {size: fontsize(8), overflow: :shrink_to_fit} do |table|
|
||||
table.column(0).width = 200
|
||||
table.columns(1..3).align = :right
|
||||
each_group_order_article_for_order_article(order_article) do |goa|
|
||||
dimrows << rows.length if goa.result == 0
|
||||
rows << [goa.group_order.ordergroup_name,
|
||||
group_order_article_quantity_with_tolerance(goa),
|
||||
goa.result,
|
||||
number_to_currency(goa.total_price)]
|
||||
end
|
||||
next unless rows.length > 1
|
||||
|
||||
name = "#{order_article.article.name} (#{order_article.article.unit} | #{order_article.price.unit_quantity} | #{number_to_currency(order_article.price.fc_price)})"
|
||||
name += " #{order_article.article.supplier.name}" if @options[:show_supplier]
|
||||
nice_table name, rows, dimrows do |table|
|
||||
table.column(0).width = bounds.width / 2
|
||||
table.columns(1..-1).align = :right
|
||||
table.column(2).font_style = :bold
|
||||
table.cells.border_width = 1
|
||||
table.cells.border_color = '666666'
|
||||
table.rows(0).border_bottom_width = 2
|
||||
# dim rows which were ordered but not received
|
||||
dimrows.each do |ri|
|
||||
table.row(ri).text_color = '999999'
|
||||
table.row(ri).columns(0..-1).font_style = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def order_articles
|
||||
order.order_articles.ordered.
|
||||
joins(:article).
|
||||
preload(:article_price). # don't join but preload article_price, just in case it went missing
|
||||
preload(: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
|
||||
|
|
|
|||
|
|
@ -1,12 +1,6 @@
|
|||
# encoding: utf-8
|
||||
class OrderByGroups < OrderPdf
|
||||
|
||||
# optimal value depends on the number of articles ordered on average by each
|
||||
# ordergroup as well as the available memory
|
||||
BATCH_SIZE = 50
|
||||
|
||||
attr_reader :order
|
||||
|
||||
def filename
|
||||
I18n.t('documents.order_by_groups.filename', :name => order.name, :date => order.ends.to_date) + '.pdf'
|
||||
end
|
||||
|
|
@ -17,91 +11,48 @@ class OrderByGroups < OrderPdf
|
|||
end
|
||||
|
||||
def body
|
||||
# Start rendering
|
||||
each_group_order do |group_order|
|
||||
down_or_page 15
|
||||
|
||||
total = 0
|
||||
rows = []
|
||||
each_ordergroup do |oa_name, oa_total, oa_id|
|
||||
dimrows = []
|
||||
rows = [[
|
||||
OrderArticle.human_attribute_name(:article),
|
||||
Article.human_attribute_name(:supplier),
|
||||
GroupOrderArticle.human_attribute_name(:ordered),
|
||||
GroupOrderArticle.human_attribute_name(:received),
|
||||
GroupOrderArticle.human_attribute_name(:unit_price),
|
||||
GroupOrderArticle.human_attribute_name(:total_price)
|
||||
]]
|
||||
|
||||
each_group_order_article_for(group_order) do |goa|
|
||||
price = order_article_price(goa.order_article)
|
||||
sub_total = price * goa.result
|
||||
total += sub_total
|
||||
rows << [goa.order_article.article.name,
|
||||
goa.tolerance > 0 ? "#{goa.quantity} + #{goa.tolerance}" : goa.quantity,
|
||||
goa.result,
|
||||
number_to_currency(price),
|
||||
goa.order_article.price.unit_quantity,
|
||||
goa.order_article.article.unit,
|
||||
number_to_currency(sub_total)]
|
||||
each_group_order_article_for_ordergroup(oa_id) do |goa|
|
||||
dimrows << rows.length if goa.result == 0
|
||||
rows << [goa.order_article.article.name,
|
||||
goa.order_article.article.supplier.name,
|
||||
group_order_article_quantity_with_tolerance(goa),
|
||||
goa.result,
|
||||
order_article_price_per_unit(goa.order_article),
|
||||
number_to_currency(goa.total_price)]
|
||||
end
|
||||
next if rows.length == 0
|
||||
rows << [ I18n.t('documents.order_by_groups.sum'), nil, nil, nil, nil, nil, number_to_currency(total)]
|
||||
rows.unshift I18n.t('documents.order_by_groups.rows') # Table Header
|
||||
next unless rows.length > 1
|
||||
rows << [nil, nil, nil, nil, nil, number_to_currency(oa_total)]
|
||||
|
||||
text group_order.ordergroup_name, size: fontsize(9), style: :bold
|
||||
table rows, width: 500, cell_style: {size: fontsize(8), overflow: :shrink_to_fit} do |table|
|
||||
# borders
|
||||
table.cells.borders = [:bottom]
|
||||
table.cells.border_width = 0.02
|
||||
table.cells.border_color = 'dddddd'
|
||||
table.rows(0).border_width = 1
|
||||
table.rows(0).border_color = '666666'
|
||||
table.rows(0).column(5).font_style = :bold
|
||||
table.row(rows.length-2).border_width = 1
|
||||
table.row(rows.length-2).border_color = '666666'
|
||||
table.row(rows.length-1).borders = []
|
||||
rows.each { |row| row.delete_at 1 } unless @options[:show_supplier]
|
||||
|
||||
table.column(0).width = 240
|
||||
table.column(2).font_style = :bold
|
||||
table.columns(1..4).align = :right
|
||||
table.column(6).align = :right
|
||||
table.column(6).font_style = :bold
|
||||
nice_table oa_name || stock_ordergroup_name, rows, dimrows do |table|
|
||||
table.row(-2).border_width = 1
|
||||
table.row(-2).border_color = '666666'
|
||||
table.row(-1).borders = []
|
||||
|
||||
# dim rows which were ordered but not received
|
||||
dimrows.each do |ri|
|
||||
table.row(ri).text_color = '999999'
|
||||
table.row(ri).columns(0..-1).font_style = nil
|
||||
if @options[:show_supplier]
|
||||
table.column(0).width = bounds.width / 3
|
||||
table.column(1).width = bounds.width / 4
|
||||
else
|
||||
table.column(0).width = bounds.width / 2
|
||||
end
|
||||
|
||||
table.columns(-4..-1).align = :right
|
||||
table.column(-3).font_style = :bold
|
||||
table.column(-1).font_style = :bold
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Return price for order_article.
|
||||
#
|
||||
# This is a separate method so that plugins can override it.
|
||||
#
|
||||
# @param article [OrderArticle]
|
||||
# @return [Number] Price to show
|
||||
# @see https://github.com/foodcoops/foodsoft/issues/445
|
||||
def order_article_price(order_article)
|
||||
order_article.price.fc_price
|
||||
end
|
||||
|
||||
def group_orders
|
||||
order.group_orders.ordered.
|
||||
includes(:ordergroup).order('groups.name').
|
||||
preload(:group_order_articles => {:order_article => [:article, :article_price]})
|
||||
end
|
||||
|
||||
def each_group_order
|
||||
group_orders.find_each_with_order(batch_size: BATCH_SIZE) {|go| yield go }
|
||||
end
|
||||
|
||||
def group_order_articles_for(group_order)
|
||||
goas = group_order.group_order_articles.to_a
|
||||
goas.sort_by!(&:id)
|
||||
goas
|
||||
end
|
||||
|
||||
def each_group_order_article_for(group_order)
|
||||
group_order_articles_for(group_order).each {|goa| yield goa }
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@ class OrderFax < OrderPdf
|
|||
|
||||
BATCH_SIZE = 250
|
||||
|
||||
attr_reader :order
|
||||
|
||||
def filename
|
||||
I18n.t('documents.order_fax.filename', :name => order.name, :date => order.ends.to_date) + '.pdf'
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
# encoding: utf-8
|
||||
class OrderMatrix < OrderPdf
|
||||
|
||||
MAX_ARTICLES_PER_PAGE = 16 # How many order_articles shoud written on a page
|
||||
HEADER_ROTATE = -30
|
||||
PLACEHOLDER_CHAR = 'X'
|
||||
|
||||
def filename
|
||||
I18n.t('documents.order_matrix.filename', :name => @order.name, :date => @order.ends.to_date) + '.pdf'
|
||||
|
|
@ -13,88 +13,105 @@ class OrderMatrix < OrderPdf
|
|||
end
|
||||
|
||||
def body
|
||||
order_articles = @order.order_articles.ordered
|
||||
order_articles_data = [[
|
||||
OrderArticle.human_attribute_name(:article),
|
||||
Article.human_attribute_name(:supplier),
|
||||
ArticlePrice.human_attribute_name(:unit_quantity),
|
||||
OrderArticle.human_attribute_name(:units_received),
|
||||
Article.human_attribute_name(:fc_price_short)
|
||||
]]
|
||||
|
||||
text I18n.t('documents.order_matrix.heading'), style: :bold
|
||||
move_down 5
|
||||
text I18n.t('documents.order_matrix.total', :count => order_articles.size), size: fontsize(8)
|
||||
move_down 10
|
||||
|
||||
order_articles_data = [I18n.t('documents.order_matrix.rows')]
|
||||
|
||||
order_articles.each do |a|
|
||||
each_order_article do |a|
|
||||
order_articles_data << [a.article.name,
|
||||
a.article.unit,
|
||||
a.article.supplier.name,
|
||||
a.price.unit_quantity,
|
||||
number_with_precision(article_price(a), precision: 2),
|
||||
a.units]
|
||||
a.units,
|
||||
order_article_price_per_unit(a)]
|
||||
end
|
||||
|
||||
table order_articles_data, cell_style: {size: fontsize(8), overflow: :shrink_to_fit} do |table|
|
||||
table.cells.border_width = 1
|
||||
table.cells.border_color = '666666'
|
||||
order_articles_data.each { |row| row.delete_at 1 } unless @options[:show_supplier]
|
||||
|
||||
name = I18n.t('documents.order_matrix.heading', count: order_articles_data.size-1)
|
||||
nice_table name, order_articles_data do |table|
|
||||
if @options[:show_supplier]
|
||||
table.column(0).width = bounds.width / 3
|
||||
table.column(1).width = bounds.width / 4
|
||||
else
|
||||
table.column(0).width = bounds.width / 2
|
||||
end
|
||||
|
||||
table.columns(-3..-1).align = :right
|
||||
table.column(-2).font_style = :bold
|
||||
end
|
||||
|
||||
page_number = 0
|
||||
total_num_order_articles = order_articles.size
|
||||
font_size 8
|
||||
|
||||
while page_number * MAX_ARTICLES_PER_PAGE < total_num_order_articles do # Start page generating
|
||||
row_height_1 = height_of(PLACEHOLDER_CHAR) + 3
|
||||
col_width_0 = width_of(PLACEHOLDER_CHAR * 20)
|
||||
col_width_1 = width_of("#{number_to_currency(888.88)} / #{PLACEHOLDER_CHAR * 4}") + 3
|
||||
col_width_2 = width_of(PLACEHOLDER_CHAR * 3) + 5
|
||||
|
||||
page_number += 1
|
||||
start_new_page(layout: :landscape)
|
||||
first_page = true
|
||||
start_new_page(layout: :landscape)
|
||||
batch_size = (bounds.width - col_width_0 - col_width_1) / col_width_2
|
||||
batch_size = batch_size.floor
|
||||
|
||||
# Collect order_articles for this page
|
||||
current_order_articles = order_articles.select do |a|
|
||||
order_articles.index(a) >= (page_number-1) * MAX_ARTICLES_PER_PAGE and
|
||||
order_articles.index(a) < page_number * MAX_ARTICLES_PER_PAGE
|
||||
each_ordergroup_batch batch_size do |batch_groups, batch_results|
|
||||
start_new_page unless first_page
|
||||
|
||||
header = batch_groups.map do |name, total|
|
||||
text = "#{name.try(:truncate, 20)} <b>#{number_to_currency(total)}</b>"
|
||||
RotatedCell.new(self, text, inline_format: true, rotate: HEADER_ROTATE)
|
||||
end
|
||||
|
||||
# Make order_articles header
|
||||
header = [""]
|
||||
for header_article in current_order_articles
|
||||
name = header_article.article.name.gsub(/[-\/]/, " ").gsub(".", ". ")
|
||||
name = name.split.collect { |w| w.truncate(8) }.join(" ")
|
||||
header << name.truncate(30)
|
||||
end
|
||||
rows = [[nil, nil] + header]
|
||||
|
||||
# Collect group results
|
||||
groups_data = [header]
|
||||
last_supplier_id = -1
|
||||
|
||||
@order.group_orders.includes(:ordergroup).each do |group_order|
|
||||
|
||||
group_result = [group_order.ordergroup_name.truncate(20)]
|
||||
|
||||
for order_article in current_order_articles
|
||||
# get the Ordergroup result for this order_article
|
||||
goa = order_article.group_order_articles.where(group_order_id: group_order.id).first
|
||||
group_result << ((goa.nil? || goa.result == 0) ? "" : goa.result.to_i)
|
||||
each_order_article do |order_article|
|
||||
supplier = order_article.article.supplier
|
||||
if @options[:show_supplier] && last_supplier_id != supplier.id
|
||||
row = [make_cell(supplier.name, colspan: 2, font_style: :bold)]
|
||||
batch_groups.each { row << nil }
|
||||
rows << row
|
||||
last_supplier_id = supplier.id
|
||||
end
|
||||
groups_data << group_result
|
||||
|
||||
row = [order_article.article.name, order_article_price_per_unit(order_article)]
|
||||
row += batch_results[order_article.id] if batch_results[order_article.id]
|
||||
rows << row
|
||||
end
|
||||
|
||||
# Make table
|
||||
column_widths = [85]
|
||||
(MAX_ARTICLES_PER_PAGE + 1).times { |i| column_widths << 41 unless i == 0 }
|
||||
table groups_data, column_widths: column_widths, cell_style: {size: fontsize(8), overflow: :shrink_to_fit} do |table|
|
||||
table.cells.border_width = 1
|
||||
table rows, header: true, cell_style: {overflow: :shrink_to_fit} do |table|
|
||||
table.cells.padding = [0, 0, 2, 0]
|
||||
table.cells.borders = [:left]
|
||||
table.cells.border_width = 0.5
|
||||
table.cells.border_color = '666666'
|
||||
table.row_colors = ['ffffff','ececec']
|
||||
|
||||
table.row(0).borders = [:bottom, :left]
|
||||
table.row(0).padding = [2, 0, 2, 0]
|
||||
table.row(1..-1).height = row_height_1
|
||||
table.column(0..1).borders = []
|
||||
table.column(1).align = :right
|
||||
table.column(1).padding = [0, 3, 2, 0]
|
||||
table.column(2..-1).align = :center
|
||||
table.cells[0,0].borders = []
|
||||
table.cells[0,1].borders = []
|
||||
|
||||
table.column(0).overflow = :truncate
|
||||
table.column(0).width = col_width_0
|
||||
table.column(1).width = col_width_1
|
||||
table.column(2..-1).width = col_width_2
|
||||
|
||||
(0..batch_size).step(5).each do |idx|
|
||||
table.column(2+idx).border_width = 2
|
||||
end
|
||||
|
||||
table.row_colors = ['dddddd', 'ffffff']
|
||||
end
|
||||
|
||||
first_page = false
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Return price for article.
|
||||
#
|
||||
# This is a separate method so that plugins can override it.
|
||||
#
|
||||
# @param article [Article]
|
||||
# @return [Number] Price to show
|
||||
# @see https://github.com/foodcoops/foodsoft/issues/445
|
||||
def article_price(article)
|
||||
article.price.fc_price
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue