Rewrite OrderPdfs to support multiple orders at once

This commit is contained in:
Patrick Gansterer 2017-11-15 19:22:55 +01:00
parent 80899afcb0
commit 3e156bbbf3
11 changed files with 457 additions and 408 deletions

View file

@ -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