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

@ -6,10 +6,6 @@ class OrdersController < ApplicationController
before_filter :authenticate_orders
# Define layout exceptions for PDF actions:
layout "application", :except => [:faxPdf, :matrixPdf, :articlesPdf, :groupsPdf]
prawnto :prawn => { :page_size => 'A4' }
# List orders
def index
@open_orders = Order.open
@ -43,13 +39,26 @@ class OrdersController < ApplicationController
def show
@order= Order.find(params[:id])
if params[:view] # Articles-list will be replaced
@partial = case params[:view]
when 'normal' then "articles"
when 'groups'then 'shared/articles_by_groups'
when 'articles'then 'shared/articles_by_articles'
end
render :layout => false
respond_to do |format|
format.html
format.js do
@partial = case params[:view]
when 'normal' then "articles"
when 'groups'then 'shared/articles_by_groups'
when 'articles'then 'shared/articles_by_articles'
else 'articles'
end
render :layout => false
end
format.pdf do
pdf = case params[:document]
when 'groups' then OrderByGroups.new(@order)
when 'articles' then OrderByArticles.new(@order)
when 'fax' then OrderFax.new(@order)
when 'matrix' then OrderMatrix.new(@order)
end
send_data pdf.to_pdf, filename: pdf.filename, type: 'application/pdf'
end
end
end
@ -100,28 +109,6 @@ class OrdersController < ApplicationController
redirect_to order, notice: "Die Bestellung wurde beendet."
end
# Renders the groups-orderd PDF.
def groupsPdf
@order = Order.find(params[:id])
prawnto :filename => "#{Date.today}_#{@order.name}_GruppenSortierung.pdf"
end
# Renders the articles-orderd PDF.
def articlesPdf
@order = Order.find(params[:id])
prawnto :filename => "#{Date.today}_#{@order.name}_ArtikelSortierung.pdf",
:prawn => { :left_margin => 48,
:right_margin => 48,
:top_margin => 48,
:bottom_margin => 48 }
end
# Renders the fax PDF.
def faxPdf
@order = Order.find(params[:id])
prawnto :filename => "#{Date.today}_#{@order.name}_FAX.pdf"
end
# Renders the fax-text-file
# e.g. for easier use with online-fax-software, which don't accept pdf-files
def text_fax_template
@ -148,15 +135,4 @@ class OrdersController < ApplicationController
:type => 'text/plain; charset=utf-8; header=present',
:disposition => "attachment; filename=#{order.name}"
end
# Renders the matrix PDF.
def matrixPdf
@order = Order.find(params[:id])
unless @order.order_articles.ordered.empty?
prawnto :filename => "#{Date.today}_#{@order.name}_Matrix.pdf"
else
flash[:error] = "Es sind keine Artikel bestellt worden."
redirect_to @order
end
end
end

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

View file

@ -5,9 +5,9 @@ module OrdersHelper
link_to text, order_path(order, :view => view), :remote => true
end
def link_to_pdf(order, action)
def order_pdf(order, document)
link_to image_tag("save_pdf.png", :size => "16x16", :border => "0", :alt => "PDF erstellen"),
{ :action => action, :id => order, :format => :pdf }, { :title => "PDF erstellen" }
order_path(order, :document => document, :format => :pdf), { :title => "PDF erstellen" }
end
def options_for_suppliers_to_select

View file

@ -1,36 +0,0 @@
end_date = @order.ends.strftime('%d.%m.%Y')
title = "#{@order.name} | beendet am #{end_date}"
# Define header and footer
pdf.header [pdf.margin_box.left,pdf.margin_box.top+30] do
pdf.text title, :size => 10, :align => :center
end
pdf.footer [pdf.margin_box.left, pdf.margin_box.bottom-5] do
pdf.stroke_horizontal_rule
pdf.text "Seite #{pdf.page_count}", :size => 8
end
# Start rendering
for order_article in @order.order_articles.ordered
pdf.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 => 10
pdf.move_down 5
data = []
for goa in order_article.group_order_articles
data << [goa.group_order.ordergroup.name,
goa.result,
number_with_precision(order_article.price.fc_price * goa.result)]
end
pdf.table data,
:font_size => 8,
:headers => ["Bestellgruppe", "Menge", "Preis"],
:widths => { 0 => 200, 1 => 40, 2 => 40 },
:border_style => :grid,
:row_colors => ['ffffff','ececec'],
:vertical_padding => 3,
:align => { 2 => :right }
pdf.move_down 10
end

View file

@ -1,53 +0,0 @@
# Get ActiveRecord objects
contact = FoodsoftConfig[:contact].symbolize_keys
# Define header and footer
#pdf.header [pdf.margin_box.left,pdf.margin_box.top+30] do
# pdf.text title, :size => 10, :align => :center
#end
pdf.footer [pdf.margin_box.left, pdf.margin_box.bottom-5] do
pdf.stroke_horizontal_rule
pdf.text "Seite #{pdf.page_count}", :size => 8
end
# From paragraph
pdf.bounding_box [pdf.margin_box.right-200,pdf.margin_box.top], :width => 200 do
pdf.text FoodsoftConfig[:name], :align => :right
pdf.move_down 5
pdf.text contact[:street], :align => :right
pdf.move_down 5
pdf.text contact[:zip_code] + " " + contact[:city], :align => :right
pdf.move_down 10
pdf.text contact[:phone], :size => 9, :align => :right
pdf.move_down 5
pdf.text contact[:email], :size => 9, :align => :right
end
# Recipient
pdf.bounding_box [pdf.margin_box.left,pdf.margin_box.top-60], :width => 200 do
pdf.text @order.name
pdf.move_down 5
pdf.text @order.supplier.address
pdf.move_down 5
pdf.text "Fax: " + @order.supplier.fax
end
pdf.text Date.today.strftime('%d.%m.%Y'), :align => :right
pdf.move_down 10
pdf.text "Lieferdatum:"
pdf.move_down 10
pdf.text "Ansprechpartner: " + @order.supplier.contact_person
pdf.move_down 10
# Articles
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
pdf.table data,
:font_size => 8,
:vertical_padding => 3,
:border_style => :grid,
:headers => ["BestellNr.", "Menge","Name", "Gebinde", "Einheit","Preis/Einheit"],
:align => {0 => :left}

View file

@ -1,48 +0,0 @@
end_date = @order.ends.strftime('%d.%m.%Y')
title = "Gruppensortierung für #{@order.name}, beendet am #{end_date}"
# Define header and footer
pdf.header [pdf.margin_box.left,pdf.margin_box.top+20] do
pdf.text title, :size => 10, :align => :center
end
pdf.footer [pdf.margin_box.left, pdf.margin_box.bottom-5] do
pdf.stroke_horizontal_rule
pdf.text "Seite #{pdf.page_count}", :size => 8
end
# Start rendering
groups = @order.group_orders.size
counter = 1
for group_order in @order.group_orders
pdf.text group_order.ordergroup.name, :style => :bold
pdf.move_down 5
pdf.text "Gruppe #{counter.to_s}/#{groups.to_s}", :size => 8
pdf.move_down 5
total = 0
data = []
group_order.group_order_articles.ordered.each do |goa|
price = goa.order_article.price.fc_price
sub_total = price * goa.result
total += sub_total
data << [goa.order_article.article.name,
goa.result, number_with_precision(price),
goa.order_article.price.unit_quantity,
goa.order_article.article.unit,
number_with_precision(sub_total)]
end
data << [ {:text => "Summe", :colspan => 5}, number_with_precision(total)]
pdf.table data,
:font_size => 8,
:border_style => :grid,
:vertical_padding => 3,
:headers => ["Artikel", "Menge", "Preis", "GebGr", "Einheit", "Summe"],
:widths => { 0 => 250 },
:row_colors => ['ffffff','ececec'],
:align => { 0 => :right, 5 => :right }
counter += 1
pdf.move_down 10
end

View file

@ -1,83 +0,0 @@
title = "#{@order.name}, beendet am #{@order.ends.strftime('%d.%m.%Y')}"
# Define header and footer
pdf.header [pdf.margin_box.left,pdf.margin_box.top+20] do
pdf.text title, :size => 10, :align => :center
end
pdf.footer [pdf.margin_box.left, pdf.margin_box.bottom-5] do
pdf.stroke_horizontal_rule
pdf.move_down 2
pdf.text "Seite #{pdf.page_count}", :size => 8
end
max_order_articles_per_page = 16 # How many order_articles shoud written on a page
order_articles = @order.order_articles.ordered
pdf.text "Artikelübersicht", :style => :bold
pdf.move_down 5
pdf.text "Insgesamt #{order_articles.size} Artikel", :size => 8
pdf.move_down 10
order_articles_data = order_articles.collect do |a|
[a.article.name, a.article.unit, a.price.unit_quantity, number_with_precision(a.price.fc_price), a.units_to_order]
end
pdf.table order_articles_data,
:font_size => 8,
:border_style => :grid,
:vertical_padding => 3,
:headers => ["Artikel", "Einheit", "Gebinde", "FC-Preis", "Menge"],
:align => { 3 => :right }
page_number = 0
total_num_order_articles = order_articles.size
while (page_number * max_order_articles_per_page < total_num_order_articles) do # Start page generating
page_number += 1
pdf.start_new_page(:layout => :landscape)
pdf.header [pdf.margin_box.left,pdf.margin_box.top+20] do
pdf.text title, :size => 10, :align => :center
end
# Collect order_articles for this page
current_order_articles = order_articles.select do |a|
order_articles.index(a) >= (page_number-1) * max_order_articles_per_page and
order_articles.index(a) < page_number * max_order_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| truncate(w, :length => 8, :omission => "..") }.join(" ")
header << truncate(name, :length => 30, :omission => " ..")
end
# Collect group results
groups_data = []
for group_order in @order.group_orders.all(:include => :ordergroup)
group_result = [truncate(group_order.ordergroup.name, :length => 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
widths = {0 => 85} # Generate widths-hash for table layout
(max_order_articles_per_page + 1).times { |i| widths.merge!({ i => 41 }) unless i == 0 }
logger.debug "Spaltenbreiten: #{widths.inspect}"
pdf.table groups_data,
:font_size => 8,
:border_style => :grid,
:vertical_padding => 3,
:headers => header,
:column_widths => widths,
:row_colors => ['ffffff','ececec']
end

View file

@ -30,7 +30,7 @@
%p
Gruppenbestellungen:
%b= @order.group_orders.count
= "[#{@order.group_orders.find(:all, :include => :ordergroup).collect{|g| g.ordergroup.name}.join(', ')}]"
= "[#{@order.group_orders.includes(:ordergroup).all.collect{|g| g.ordergroup.name}.join(', ')}]"
%p
Netto/Bruttosumme aller Artikel:
@ -56,16 +56,16 @@
= update_articles_link(@order, "Artikelübersicht", :normal)
|
= update_articles_link(@order, "Sortiert nach Gruppen", :groups)
= link_to_pdf(@order, 'groupsPdf')
= order_pdf(@order, :groups)
|
= update_articles_link(@order, "Sortiert nach Artikeln", :articles)
= link_to_pdf(@order, 'articlesPdf')
= order_pdf(@order, :articles)
|
Matrix:
= link_to_pdf(@order, 'matrixPdf')
= order_pdf(@order, :matrix)
|
Faxvorlage:
= link_to_pdf(@order, 'faxPdf')
= order_pdf(@order, :fax)
= link_to image_tag("text_file.png", :size => "16x16", :border => "0", :alt => "Textdatei erstellen"), { :action => 'text_fax_template', :id => @order }, { :title => "Download file" }
|
= link_to 'Kommentare', '#comments'