Refactored pdf generation with newest prawn.

This commit is contained in:
benni 2012-10-02 02:50:48 +02:00
parent 78cc423a46
commit eb532babd6
31 changed files with 311 additions and 2118 deletions

View file

@ -0,0 +1,33 @@
# encoding: utf-8
class OrderByArticles < OrderPdf
def filename
"Bestellung #{@order.name}-#{@order.ends.to_date} - Artikelsortierung.pdf"
end
def title
"Artikelsortierung der Bestellung: #{@order.name}, beendet am #{@order.ends.strftime('%d.%m.%Y')}"
end
def body
@order.order_articles.ordered.each do |order_article|
text "#{order_article.article.name} (#{order_article.article.unit} | #{order_article.price.unit_quantity.to_s} | #{number_with_precision(order_article.price.fc_price, precision: 2)})",
style: :bold, size: 10
rows = []
rows << %w(Bestellgruppe Menge Preis)
for goa in order_article.group_order_articles
rows << [goa.group_order.ordergroup.name,
goa.result,
number_with_precision(order_article.price.fc_price * goa.result, precision: 2)]
end
table rows, column_widths: [200,40,40], cell_style: {size: 8, overflow: :shrink_to_fit} do |table|
table.columns(1..2).align = :right
table.cells.border_width = 1
table.cells.border_color = '666666'
end
move_down 10
end
end
end

View file

@ -0,0 +1,51 @@
# encoding: utf-8
class OrderByGroups < OrderPdf
def filename
"Bestellung #{@order.name}-#{@order.ends.to_date} - Gruppensortierung.pdf"
end
def title
"Gruppensortierung der Bestellung: #{@order.name}, beendet am #{@order.ends.strftime('%d.%m.%Y')}"
end
def body
# Start rendering
@order.group_orders.each do |group_order|
text group_order.ordergroup.name, size: 9, style: :bold
total = 0
rows = []
rows << %w(Artikel Menge Preis GebGr Einheit Summe) # Table Header
group_order_articles = group_order.group_order_articles.ordered
group_order_articles.each do |goa|
price = goa.order_article.price.fc_price
sub_total = price * goa.result
total += sub_total
rows << [goa.order_article.article.name,
goa.result,
number_with_precision(price, precision: 2),
goa.order_article.price.unit_quantity,
goa.order_article.article.unit,
number_with_precision(sub_total, precision: 2)]
end
rows << [ "Summe", nil, nil, nil, nil, number_with_precision(total, precision: 2)]
table rows, column_widths: [250,50,50,50,50,50], cell_style: {size: 8, overflow: :shrink_to_fit} do |table|
# borders
table.cells.borders = []
table.row(0).borders = [:bottom]
table.row(group_order_articles.size).borders = [:bottom]
table.cells.border_width = 1
table.cells.border_color = '666666'
table.columns(1..3).align = :right
table.columns(5).align = :right
end
move_down 15
end
end
end

View file

@ -0,0 +1,69 @@
# encoding: utf-8
class OrderFax < OrderPdf
def filename
"Bestellung #{@order.name}-#{@order.ends.to_date} - Fax.pdf"
end
def title
false
end
def body
contact = FoodsoftConfig[:contact].symbolize_keys
# From paragraph
bounding_box [margin_box.right-200,margin_box.top], width: 200 do
text FoodsoftConfig[:name], align: :right
move_down 5
text contact[:street], align: :right
move_down 5
text "#{contact[:zip_code]} #{contact[:city]}", align: :right
move_down 10
text contact[:phone], size: 9, align: :right
move_down 5
text contact[:email], size: 9, align: :right
end
# Recipient
bounding_box [margin_box.left,margin_box.top-60], width: 200 do
text @order.name
move_down 5
text @order.supplier.address
move_down 5
text "Fax: " + @order.supplier.fax
end
text Date.today.strftime('%d.%m.%Y'), align: :right
move_down 10
text "Lieferdatum:"
move_down 10
text "Ansprechpartner: " + @order.supplier.contact_person
move_down 10
# Articles
data = [["BestellNr.", "Menge","Name", "Gebinde", "Einheit","Preis/Einheit"]]
data = @order.order_articles.ordered.all(include: :article).collect do |a|
[a.article.order_number,
a.units_to_order,
a.article.name,
a.price.unit_quantity,
a.article.unit,
a.price.price]
end
table data, cell_style: {size: 8, overflow: :shrink_to_fit} do |table|
table.cells.border_width = 1
table.cells.border_color = '666666'
table.columns(1).align = :right
table.columns(3..5).align = :right
end
#font_size: 8,
#vertical_padding: 3,
#border_style: :grid,
#headers: ["BestellNr.", "Menge","Name", "Gebinde", "Einheit","Preis/Einheit"],
#align: {0 => :left}
end
end

View file

@ -0,0 +1,86 @@
# encoding: utf-8
class OrderMatrix < OrderPdf
MAX_ARTICLES_PER_PAGE = 16 # How many order_articles shoud written on a page
def filename
"Bestellung #{@order.name}-#{@order.ends.to_date} - Sortiermatrix.pdf"
end
def title
"Sortiermatrix der Bestellung: #{@order.name}, beendet am #{@order.ends.strftime('%d.%m.%Y')}"
end
def body
order_articles = @order.order_articles.ordered
text "Artikelübersicht", style: :bold
move_down 5
text "Insgesamt #{order_articles.size} Artikel", size: 8
move_down 10
order_articles_data = [%w(Artikel Einheit Gebinde FC-Preis Menge)]
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),
a.units_to_order]
end
table order_articles_data, cell_style: {size: 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).all.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.first conditions: { group_order_id: group_order.id }
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 }
table groups_data, column_widths: column_widths, cell_style: {size: 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
end