foodsoft/app/documents/order_matrix.rb

88 lines
2.9 KiB
Ruby
Raw Normal View History

# encoding: utf-8
class OrderMatrix < OrderPdf
MAX_ARTICLES_PER_PAGE = 16 # How many order_articles shoud written on a page
2016-02-25 02:08:04 +01:00
def filename
2013-04-04 02:04:31 +02:00
I18n.t('documents.order_matrix.filename', :name => @order.name, :date => @order.ends.to_date) + '.pdf'
end
def title
2013-04-04 02:04:31 +02:00
I18n.t('documents.order_matrix.title', :name => @order.name,
:date => @order.ends.strftime(I18n.t('date.formats.default')))
end
def body
order_articles = @order.order_articles.ordered
2013-04-04 02:04:31 +02:00
text I18n.t('documents.order_matrix.heading'), style: :bold
move_down 5
2014-03-19 14:01:51 +01:00
text I18n.t('documents.order_matrix.total', :count => order_articles.size), size: fontsize(8)
move_down 10
2013-04-04 02:04:31 +02:00
order_articles_data = [I18n.t('documents.order_matrix.rows')]
order_articles.each do |a|
order_articles_data << [a.article.name,
a.article.unit,
a.price.unit_quantity,
number_with_precision(a.price.fc_price, precision: 2),
2014-02-13 15:59:04 +01:00
a.units]
end
2014-03-19 14:01:51 +01:00
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'
end
page_number = 0
total_num_order_articles = order_articles.size
while page_number * MAX_ARTICLES_PER_PAGE < total_num_order_articles do # Start page generating
page_number += 1
start_new_page(layout: :landscape)
# 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
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
# Collect group results
groups_data = [header]
@order.group_orders.includes(:ordergroup).each do |group_order|
2016-02-25 02:08:04 +01:00
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)
end
groups_data << group_result
end
# Make table
column_widths = [85]
(MAX_ARTICLES_PER_PAGE + 1).times { |i| column_widths << 41 unless i == 0 }
2014-03-19 14:01:51 +01:00
table groups_data, column_widths: column_widths, cell_style: {size: fontsize(8), overflow: :shrink_to_fit} do |table|
table.cells.border_width = 1
table.cells.border_color = '666666'
table.row_colors = ['ffffff','ececec']
end
end
end
2013-04-04 02:04:31 +02:00
end