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,12 +1,6 @@
# encoding: utf-8 # encoding: utf-8
class OrderByArticles < OrderPdf 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 def filename
I18n.t('documents.order_by_articles.filename', :name => order.name, :date => order.ends.to_date) + '.pdf' I18n.t('documents.order_by_articles.filename', :name => order.name, :date => order.ends.to_date) + '.pdf'
end end
@ -18,59 +12,30 @@ class OrderByArticles < OrderPdf
def body def body
each_order_article do |order_article| each_order_article do |order_article|
down_or_page
rows = []
dimrows = [] dimrows = []
each_group_order_article_for(order_article) do |goa| rows = [[
rows << [goa.group_order.ordergroup_name, GroupOrder.human_attribute_name(:ordergroup),
"#{goa.quantity} + #{goa.tolerance}", GroupOrderArticle.human_attribute_name(:ordered),
goa.result, GroupOrderArticle.human_attribute_name(:received),
number_to_currency(goa.total_price(order_article))] GroupOrderArticle.human_attribute_name(:total_price)
]]
each_group_order_article_for_order_article(order_article) do |goa|
dimrows << rows.length if goa.result == 0 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 end
next if rows.length == 0 next unless rows.length > 1
rows.unshift I18n.t('documents.order_by_articles.rows') # table header
text "#{order_article.article.name} (#{order_article.article.unit} | #{order_article.price.unit_quantity.to_s} | #{number_to_currency(order_article.price.fc_price)})", name = "#{order_article.article.name} (#{order_article.article.unit} | #{order_article.price.unit_quantity} | #{number_to_currency(order_article.price.fc_price)})"
style: :bold, size: fontsize(10) name += " #{order_article.article.supplier.name}" if @options[:show_supplier]
table rows, cell_style: {size: fontsize(8), overflow: :shrink_to_fit} do |table| nice_table name, rows, dimrows do |table|
table.column(0).width = 200 table.column(0).width = bounds.width / 2
table.columns(1..3).align = :right table.columns(1..-1).align = :right
table.column(2).font_style = :bold 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
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

View File

@ -1,12 +1,6 @@
# encoding: utf-8 # encoding: utf-8
class OrderByGroups < OrderPdf 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 def filename
I18n.t('documents.order_by_groups.filename', :name => order.name, :date => order.ends.to_date) + '.pdf' I18n.t('documents.order_by_groups.filename', :name => order.name, :date => order.ends.to_date) + '.pdf'
end end
@ -17,91 +11,48 @@ class OrderByGroups < OrderPdf
end end
def body def body
# Start rendering each_ordergroup do |oa_name, oa_total, oa_id|
each_group_order do |group_order|
down_or_page 15
total = 0
rows = []
dimrows = [] 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| each_group_order_article_for_ordergroup(oa_id) 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)]
dimrows << rows.length if goa.result == 0 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 end
next if rows.length == 0 next unless rows.length > 1
rows << [ I18n.t('documents.order_by_groups.sum'), nil, nil, nil, nil, nil, number_to_currency(total)] rows << [nil, nil, nil, nil, nil, number_to_currency(oa_total)]
rows.unshift I18n.t('documents.order_by_groups.rows') # Table Header
text group_order.ordergroup_name, size: fontsize(9), style: :bold rows.each { |row| row.delete_at 1 } unless @options[:show_supplier]
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 = []
table.column(0).width = 240 nice_table oa_name || stock_ordergroup_name, rows, dimrows do |table|
table.column(2).font_style = :bold table.row(-2).border_width = 1
table.columns(1..4).align = :right table.row(-2).border_color = '666666'
table.column(6).align = :right table.row(-1).borders = []
table.column(6).font_style = :bold
# dim rows which were ordered but not received if @options[:show_supplier]
dimrows.each do |ri| table.column(0).width = bounds.width / 3
table.row(ri).text_color = '999999' table.column(1).width = bounds.width / 4
table.row(ri).columns(0..-1).font_style = nil 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 end
end 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

View File

@ -3,8 +3,6 @@ class OrderFax < OrderPdf
BATCH_SIZE = 250 BATCH_SIZE = 250
attr_reader :order
def filename def filename
I18n.t('documents.order_fax.filename', :name => order.name, :date => order.ends.to_date) + '.pdf' I18n.t('documents.order_fax.filename', :name => order.name, :date => order.ends.to_date) + '.pdf'
end end

View File

@ -1,7 +1,7 @@
# encoding: utf-8
class OrderMatrix < OrderPdf 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 def filename
I18n.t('documents.order_matrix.filename', :name => @order.name, :date => @order.ends.to_date) + '.pdf' I18n.t('documents.order_matrix.filename', :name => @order.name, :date => @order.ends.to_date) + '.pdf'
@ -13,88 +13,105 @@ class OrderMatrix < OrderPdf
end end
def body 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 each_order_article do |a|
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|
order_articles_data << [a.article.name, order_articles_data << [a.article.name,
a.article.unit, a.article.supplier.name,
a.price.unit_quantity, a.price.unit_quantity,
number_with_precision(article_price(a), precision: 2), a.units,
a.units] order_article_price_per_unit(a)]
end end
table order_articles_data, cell_style: {size: fontsize(8), overflow: :shrink_to_fit} do |table| order_articles_data.each { |row| row.delete_at 1 } unless @options[:show_supplier]
table.cells.border_width = 1
table.cells.border_color = '666666' 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 end
page_number = 0 table.columns(-3..-1).align = :right
total_num_order_articles = order_articles.size table.column(-2).font_style = :bold
end
while page_number * MAX_ARTICLES_PER_PAGE < total_num_order_articles do # Start page generating font_size 8
page_number += 1 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
first_page = true
start_new_page(layout: :landscape) 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 each_ordergroup_batch batch_size do |batch_groups, batch_results|
current_order_articles = order_articles.select do |a| start_new_page unless first_page
order_articles.index(a) >= (page_number-1) * MAX_ARTICLES_PER_PAGE and
order_articles.index(a) < page_number * MAX_ARTICLES_PER_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 end
# Make order_articles header rows = [[nil, nil] + header]
header = [""]
for header_article in current_order_articles last_supplier_id = -1
name = header_article.article.name.gsub(/[-\/]/, " ").gsub(".", ". ")
name = name.split.collect { |w| w.truncate(8) }.join(" ") each_order_article do |order_article|
header << name.truncate(30) 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 end
# Collect group results row = [order_article.article.name, order_article_price_per_unit(order_article)]
groups_data = [header] row += batch_results[order_article.id] if batch_results[order_article.id]
rows << row
@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)
end
groups_data << group_result
end end
# Make table table rows, header: true, cell_style: {overflow: :shrink_to_fit} do |table|
column_widths = [85] table.cells.padding = [0, 0, 2, 0]
(MAX_ARTICLES_PER_PAGE + 1).times { |i| column_widths << 41 unless i == 0 } table.cells.borders = [:left]
table groups_data, column_widths: column_widths, cell_style: {size: fontsize(8), overflow: :shrink_to_fit} do |table| table.cells.border_width = 0.5
table.cells.border_width = 1
table.cells.border_color = '666666' table.cells.border_color = '666666'
table.row_colors = ['ffffff','ececec']
end table.row(0).borders = [:bottom, :left]
table.row(0).padding = [2, 0, 2, 0]
end table.row(1..-1).height = row_height_1
end table.column(0..1).borders = []
table.column(1).align = :right
private table.column(1).padding = [0, 3, 2, 0]
table.column(2..-1).align = :center
# Return price for article. table.cells[0,0].borders = []
# table.cells[0,1].borders = []
# This is a separate method so that plugins can override it.
# table.column(0).overflow = :truncate
# @param article [Article] table.column(0).width = col_width_0
# @return [Number] Price to show table.column(1).width = col_width_1
# @see https://github.com/foodcoops/foodsoft/issues/445 table.column(2..-1).width = col_width_2
def article_price(article)
article.price.fc_price (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 end
end end

View File

@ -55,14 +55,17 @@ de:
name: Name name: Name
financial_transaction_class: Finanztransaktionsklasse financial_transaction_class: Finanztransaktionsklasse
group_order: group_order:
ordergroup: Bestellgruppe
price: Bestellsumme price: Bestellsumme
updated_by: Zuletzt bestellt updated_by: Zuletzt bestellt
group_order_article: group_order_article:
ordergroup_id: Bestellgruppe ordered: Bestellt
quantity: Menge quantity: Menge
received: Bekommen
result: Menge result: Menge
tolerance: Toleranz tolerance: Toleranz
total_price: Summe total_price: Summe
unit_price: Preis/Einheit
invoice: invoice:
amount: Betrag amount: Betrag
attachment: Anhang attachment: Anhang
@ -784,22 +787,9 @@ de:
title: Aktuelle Bestellungen - Gruppensortierung title: Aktuelle Bestellungen - Gruppensortierung
order_by_articles: order_by_articles:
filename: Bestellung %{name}-%{date} - Artikelsortierung filename: Bestellung %{name}-%{date} - Artikelsortierung
rows:
- Bestellgruppe
- Bestellt
- Bekommen
- Preis
title: 'Artikelsortierung der Bestellung: %{name}, beendet am %{date}' title: 'Artikelsortierung der Bestellung: %{name}, beendet am %{date}'
order_by_groups: order_by_groups:
filename: Bestellung %{name}-%{date} - Gruppensortierung filename: Bestellung %{name}-%{date} - Gruppensortierung
rows:
- Artikel
- Bestellt
- Bekommen
- Preis
- GebGr
- Einheit
- Summe
sum: Summe sum: Summe
title: 'Gruppensortierung der Bestellung: %{name}, beendet am %{date}' title: 'Gruppensortierung der Bestellung: %{name}, beendet am %{date}'
order_fax: order_fax:
@ -815,17 +805,8 @@ de:
total: Gesamtpreis total: Gesamtpreis
order_matrix: order_matrix:
filename: Bestellung %{name}-%{date} - Sortiermatrix filename: Bestellung %{name}-%{date} - Sortiermatrix
heading: Artikelübersicht heading: Artikelübersicht (%{count})
rows:
- Artikel
- Einheit
- Gebinde
- FC-Preis
- Menge
title: 'Sortiermatrix der Bestellung: %{name}, beendet am %{date}' title: 'Sortiermatrix der Bestellung: %{name}, beendet am %{date}'
total:
one: Insgesamt ein Artikel
other: Insgesamt %{count} Artikel
errors: errors:
general: Ein Problem ist aufgetreten. general: Ein Problem ist aufgetreten.
general_again: Ein Fehler ist aufgetreten. Bitte erneut versuchen. general_again: Ein Fehler ist aufgetreten. Bitte erneut versuchen.
@ -1234,7 +1215,7 @@ de:
profile: Profil bearbeiten profile: Profil bearbeiten
logo: "<span>food</span>soft" logo: "<span>food</span>soft"
lib: lib:
order_pdf: render_pdf:
page: Seite %{number} von %{count} page: Seite %{number} von %{count}
login: login:
accept_invitation: accept_invitation:

View File

@ -55,14 +55,17 @@ en:
name: Name name: Name
financial_transaction_class: Financial transaction class financial_transaction_class: Financial transaction class
group_order: group_order:
ordergroup: Ordergroup
price: Order sum price: Order sum
updated_by: Last ordered by updated_by: Last ordered by
group_order_article: group_order_article:
ordergroup_id: Ordergroup ordered: Ordered
quantity: Amount quantity: Amount
received: Received
result: Amount result: Amount
tolerance: Tolerance tolerance: Tolerance
total_price: Sum total_price: Sum
unit_price: Price/Unit
invoice: invoice:
amount: Amount amount: Amount
attachment: Attachment attachment: Attachment
@ -786,22 +789,9 @@ en:
title: Current orders - by group title: Current orders - by group
order_by_articles: order_by_articles:
filename: Order %{name}-%{date} - by articles filename: Order %{name}-%{date} - by articles
rows:
- Ordergroup
- Ordered
- Received
- Price
title: 'Order sorted by articles: %{name}, closed at %{date}' title: 'Order sorted by articles: %{name}, closed at %{date}'
order_by_groups: order_by_groups:
filename: Order %{name}-%{date} - by group filename: Order %{name}-%{date} - by group
rows:
- Article
- Ordered
- Received
- Price
- U.Q.
- Unit
- Sum
sum: Sum sum: Sum
title: 'Order sorted by group: %{name}, closed at %{date}' title: 'Order sorted by group: %{name}, closed at %{date}'
order_fax: order_fax:
@ -817,17 +807,8 @@ en:
total: Total total: Total
order_matrix: order_matrix:
filename: Order %{name}-%{date} - sorting matrix filename: Order %{name}-%{date} - sorting matrix
heading: Article overview heading: Article overview (%{count})
rows:
- Article
- Unit
- Unit quantity
- FC-Price
- Amount
title: 'Order sorting matrix: %{name}, closed at %{date}' title: 'Order sorting matrix: %{name}, closed at %{date}'
total:
one: One article in total
other: "%{count} articles in total"
errors: errors:
general: A problem has occured. general: A problem has occured.
general_again: A problem has occured. Please try again. general_again: A problem has occured. Please try again.
@ -1236,7 +1217,7 @@ en:
profile: Edit profile profile: Edit profile
logo: "<span>food</span>soft" logo: "<span>food</span>soft"
lib: lib:
order_pdf: render_pdf:
page: Page %{number} of %{count} page: Page %{number} of %{count}
login: login:
accept_invitation: accept_invitation:

View File

@ -50,14 +50,17 @@ es:
ordergroup: Grupo de pedido ordergroup: Grupo de pedido
user: Ingresado por user: Ingresado por
group_order: group_order:
ordergroup: Grupo de pedido
price: Suma de pedidos price: Suma de pedidos
updated_by: Pedido por última vez por updated_by: Pedido por última vez por
group_order_article: group_order_article:
ordergroup_id: Grupo de pedido ordered: Pedido el
quantity: Importe quantity: Importe
received: Recibido
result: Importe result: Importe
tolerance: Tolerancia tolerance: Tolerancia
total_price: Suma total_price: Suma
unit_price: Precio/Unidad
invoice: invoice:
amount: Importe amount: Importe
attachment: Adjunto attachment: Adjunto
@ -762,22 +765,9 @@ es:
title: Pedidos activos - por grupo title: Pedidos activos - por grupo
order_by_articles: order_by_articles:
filename: Pedido %{name}-%{date} - por artículos filename: Pedido %{name}-%{date} - por artículos
rows:
- Grupo de pedido
- Pedido el
- Recibido
- Precio
title: 'Pedido ordenado por artículos: %{name}, cerrado el %{date}' title: 'Pedido ordenado por artículos: %{name}, cerrado el %{date}'
order_by_groups: order_by_groups:
filename: Pedido %{name}-%{date} - por grupo filename: Pedido %{name}-%{date} - por grupo
rows:
- Artículo
- Pedido
- Recibido
- Precio
- C.U.
- Unidad
- Suma
sum: Suma sum: Suma
title: 'Pedido ordenado por grupo: %{name}, cerrado el %{date}' title: 'Pedido ordenado por grupo: %{name}, cerrado el %{date}'
order_fax: order_fax:
@ -793,15 +783,8 @@ es:
total: Total total: Total
order_matrix: order_matrix:
filename: Pedido %{name}-%{date} - matrix para ordenar filename: Pedido %{name}-%{date} - matrix para ordenar
heading: Descripción del artículo heading: Descripción del artículo (%{count})
rows:
- Artículo
- Unidad
- Cantidad de unidades
- Precio al grupo
- Importe
title: title:
total: "%{count} artículos in total"
errors: errors:
general: Ha ocurrido un problema. general: Ha ocurrido un problema.
general_again: Ha ocurrido un problema. Por favor inténtalo de nuevo. general_again: Ha ocurrido un problema. Por favor inténtalo de nuevo.

View File

@ -50,14 +50,17 @@ fr:
ordergroup: Cellule ordergroup: Cellule
user: Créateur-trice user: Créateur-trice
group_order: group_order:
ordergroup: Cellule
price: Total de la commande price: Total de la commande
updated_by: Dernière commande updated_by: Dernière commande
group_order_article: group_order_article:
ordergroup_id: Cellule ordered: Commandée
quantity: Quantité quantity: Quantité
result: Quantité result: Quantité
received: Quantité
tolerance: Tolérance tolerance: Tolérance
total_price: Total total_price: Total
unit_price: Prix/Unité
invoice: invoice:
amount: Montant amount: Montant
attachment: Appendice attachment: Appendice
@ -758,22 +761,9 @@ fr:
title: title:
order_by_articles: order_by_articles:
filename: Commande %{name}-%{date} - Trier par filename: Commande %{name}-%{date} - Trier par
rows:
- Cellule
-
- Quantité
- Prix
title: 'Ordre des produits pour la commande: %{name}, clôturée le %{date}' title: 'Ordre des produits pour la commande: %{name}, clôturée le %{date}'
order_by_groups: order_by_groups:
filename: Commande %{name}-%{date} - Répartition par cellules filename: Commande %{name}-%{date} - Répartition par cellules
rows:
- Nom de l'article
- Commandée
- Quantité
- Prix unitaire
- Unités par lot
- Unité
- Prix total
sum: prix total sum: prix total
title: 'Répartition par cellules pour la commande: %{name}, clôturée le %{date}' title: 'Répartition par cellules pour la commande: %{name}, clôturée le %{date}'
order_fax: order_fax:
@ -789,17 +779,8 @@ fr:
total: Total total: Total
order_matrix: order_matrix:
filename: Commande %{name}-%{date} - Tableau de répartition filename: Commande %{name}-%{date} - Tableau de répartition
heading: Liste des produits heading: Liste des produits (%{count})
rows:
- Nom du produit
- Unité
- Nombre d'unités
- Prix coop
- Quantité
title: 'Tableau de répartition pour la commande: %{name}; clôturée le %{date}' title: 'Tableau de répartition pour la commande: %{name}; clôturée le %{date}'
total:
one: Un seul produit
other: "%{count} produits au total"
errors: errors:
general: Un problème a été rencontré. general: Un problème a été rencontré.
general_again: Une erreur s'est produite. Merci de réessayer. general_again: Une erreur s'est produite. Merci de réessayer.
@ -1219,7 +1200,7 @@ fr:
profile: Ton profil profile: Ton profil
logo: coop logo: coop
lib: lib:
order_pdf: render_pdf:
page: page %{number} de %{count} page: page %{number} de %{count}
login: login:
accept_invitation: accept_invitation:

View File

@ -50,14 +50,17 @@ nl:
ordergroup: Huishouden ordergroup: Huishouden
user: Ingevuld door user: Ingevuld door
group_order: group_order:
ordergroup: Huishouden
price: Totaal bestelling price: Totaal bestelling
updated_by: Laatst besteld door updated_by: Laatst besteld door
group_order_article: group_order_article:
ordergroup_id: Huishouden ordered: Besteld
quantity: Hoeveelheid quantity: Hoeveelheid
received: Ontvangen
result: Hoeveelheid result: Hoeveelheid
tolerance: Tolerantie tolerance: Tolerantie
total_price: Som total_price: Som
unit_price: Prijs/Eenheid
invoice: invoice:
amount: Bedrag amount: Bedrag
attachment: Bijlage attachment: Bijlage
@ -758,22 +761,9 @@ nl:
title: Huidige bestellingen - per huishouden title: Huidige bestellingen - per huishouden
order_by_articles: order_by_articles:
filename: Bestelling %{name}-%{date} - Artikellijst filename: Bestelling %{name}-%{date} - Artikellijst
rows:
- Huishouden
- Besteld
- Ontvangen
- Prijs
title: 'Artikellijst van bestelling: %{name}, gesloten op %{date}' title: 'Artikellijst van bestelling: %{name}, gesloten op %{date}'
order_by_groups: order_by_groups:
filename: Bestelling %{name}-%{date} - Huishoudenslijst filename: Bestelling %{name}-%{date} - Huishoudenslijst
rows:
- Artikel
- Besteld
- Ontvangen
- Prijs
- Colli
- Eenheid
- Som
sum: Som sum: Som
title: 'Huishoudenslijst van bestelling: %{name}, gesloten op %{date}' title: 'Huishoudenslijst van bestelling: %{name}, gesloten op %{date}'
order_fax: order_fax:
@ -789,17 +779,8 @@ nl:
total: Totaal total: Totaal
order_matrix: order_matrix:
filename: Bestelling %{name}-%{date} - Sorteermatrix filename: Bestelling %{name}-%{date} - Sorteermatrix
heading: Artikeloverzicht heading: Artikeloverzicht (%{count})
rows:
- Artikel
- Eenheid
- Gr.Eenh.
- Foodcoop-prijs
- Aantal
title: 'Sorteermatrix van bestelling: %{name}, gesloten op %{date}' title: 'Sorteermatrix van bestelling: %{name}, gesloten op %{date}'
total:
one: In totaal éen artikel
other: In totaal %{count} artikelen
errors: errors:
general: Er is een probleem opgetreden. general: Er is een probleem opgetreden.
general_again: Er is een fout opgetreden. Probeer het opnieuw. general_again: Er is een fout opgetreden. Probeer het opnieuw.
@ -1210,7 +1191,7 @@ nl:
profile: Profiel aanpassen profile: Profiel aanpassen
logo: "<span>food</span>soft" logo: "<span>food</span>soft"
lib: lib:
order_pdf: render_pdf:
page: Pagina %{number} van %{count} page: Pagina %{number} van %{count}
login: login:
accept_invitation: accept_invitation:

View File

@ -1,104 +1,142 @@
require "prawn/measurement_extensions" class OrderPdf < RenderPDF
class OrderPdf < Prawn::Document attr_reader :order
include ActionView::Helpers::NumberHelper
def initialize(order, options = {}) def initialize(order, options = {})
options[:page_size] ||= FoodsoftConfig[:pdf_page_size] || "A4"
#options[:left_margin] ||= 40
#options[:right_margin] ||= 40
options[:top_margin] ||= 50
#options[:bottom_margin] ||= 40
super(options)
@order = order @order = order
@options = options @orders = order
@first_page = true super(options)
end end
def to_pdf def nice_table(name, data, dimrows = [])
# Use ttf for better utf-8 compability down_or_page 25
font_families.update( text name, size: 10, style: :bold
"OpenSans" => { table data, width: bounds.width, cell_style: {size: 8, overflow: :shrink_to_fit} do |table|
bold: font_path("OpenSans-Bold.ttf"), # borders
italic: font_path("OpenSans-Italic.ttf"), table.cells.borders = [:bottom]
bold_italic: font_path("OpenSans-BoldItalic.ttf"), table.cells.padding_top = 2
normal: font_path("OpenSans-Regular.ttf") table.cells.padding_bottom = 4
} table.cells.border_color = 'dddddd'
) table.rows(0).border_color = '666666'
font "OpenSans"
font_size fontsize(12) # dim rows which were ordered but not received
dimrows.each do |ri|
# Define header table.row(ri).text_color = '999999'
repeat :all, dynamic: true do table.row(ri).columns(0..-1).font_style = nil
s = fontsize(8)
# header
bounding_box [bounds.left, bounds.top+s*2], width: bounds.width, height: s*1.2 do
text title, size: s, align: :center if title
end
# footer
bounding_box [bounds.left, bounds.bottom-s], width: bounds.width, height: s*1.2 do
text I18n.t('lib.order_pdf.page', number: page_number, count: page_count), size: s, align: :right
end
bounding_box [bounds.left, bounds.bottom-s], width: bounds.width, height: s*1.2 do
text I18n.l(Time.now, format: :long), size: s, align: :left
end
end end
body # Add content, which is defined in subclasses yield table if block_given?
render # Render pdf
end
# Helper method to test pdf via rails console: OrderByGroups.new(order).save_tmp
def save_tmp
File.open("#{Rails.root}/tmp/#{self.class.to_s.underscore}.pdf", 'w') {|f| f.write(to_pdf.force_encoding("UTF-8")) }
end
# XXX avoid underscore instead of unicode whitespace in pdf :/
def number_to_currency(number, options={})
super(number, options).gsub("\u202f", ' ') if number
end
# return fontsize after scaling it with any configured factor
# please use this wherever you're setting a fontsize
def fontsize(n)
if FoodsoftConfig[:pdf_font_size]
n * FoodsoftConfig[:pdf_font_size].to_f/12
else
n
end
end
# add pagebreak or vertical whitespace, depending on configuration
def down_or_page(space=10)
if @first_page
@first_page = false
return
end
if pdf_add_page_breaks?
start_new_page
else
move_down space
end end
end end
protected protected
# return whether pagebreak or vertical whitespace is used for breaks # Return price for order_article.
def pdf_add_page_breaks?(docid=nil) #
docid ||= self.class.name.underscore # This is a separate method so that plugins can override it.
cfg = FoodsoftConfig[:pdf_add_page_breaks] #
if cfg.is_a? Array # @param article [OrderArticle]
cfg.index(docid.to_s).any? # @return [Number] Price to show
elsif cfg.is_a? Hash # @see https://github.com/foodcoops/foodsoft/issues/445
cfg[docid.to_s] def order_article_price(order_article)
else order_article.price.fc_price
cfg end
def order_article_price_per_unit(order_article)
"#{number_to_currency(order_article_price(order_article))} / #{order_article.article.unit}"
end
def group_order_article_quantity_with_tolerance(goa)
goa.tolerance > 0 ? "#{goa.quantity} + #{goa.tolerance}" : "#{goa.quantity}"
end
def group_order_articles(ordergroup)
GroupOrderArticle.
includes(:group_order).
where(group_orders: {order_id: @orders, ordergroup_id: ordergroup})
end
def order_articles
OrderArticle.
ordered.
includes(article: :supplier).
includes(group_order_articles: {group_order: :ordergroup}).
where(order: @orders).
order('suppliers.name, articles.name, groups.name').
preload(:article_price)
end
def ordergroups(offset = nil, limit = nil)
result = GroupOrder.
ordered.
where(order: @orders).
group('groups.id').
offset(offset).
limit(limit).
pluck('groups.name', 'SUM(group_orders.price)', 'groups.id')
result.map do |item|
[item.first || stock_ordergroup_name] + item[1..-1]
end end
end end
def font_path(name) def each_order_article(&block)
File.join(Rails.root, "vendor", "assets", "fonts", name) order_articles.each(&block)
end
def each_ordergroup(&block)
ordergroups.each(&block)
end
def each_ordergroup_batch(batch_size)
offset = 0
while true
go_records = ordergroups(offset, batch_size)
break unless go_records.any?
group_ids = go_records.map(&:third)
# get quantity for each article and ordergroup
goa_records = group_order_articles(group_ids)
.group('group_order_articles.order_article_id, group_orders.ordergroup_id')
.pluck('group_order_articles.order_article_id', 'group_orders.ordergroup_id', 'SUM(COALESCE(group_order_articles.result, group_order_articles.quantity))')
# transform the flat list of results in a hash (with the article as key), which contains an array for all ordergroups
results = goa_records.group_by(&:first).transform_values do |value|
grouped_value = value.group_by(&:second)
group_ids.map do |group_id|
grouped_value[group_id].try(:first).try(:third)
end end
end end
yield go_records, results
offset += batch_size
end
end
def each_group_order_article_for_order_article(order_article, &block)
order_article.group_order_articles.each(&block)
end
def each_group_order_article_for_ordergroup(ordergroup, &block)
group_order_articles(ordergroup)
.includes(order_article: {article: [:supplier]})
.order('suppliers.name, articles.name')
.preload(order_article: [:article_price, :order])
.each(&block)
end
def stock_ordergroup_name
users = GroupOrder.
eager_load(:updated_by).
where(order: @orders).
where(ordergroup: nil).
map(&:updated_by).
map{ |u| u.try(&:name) || '?' }
I18n.t('model.group_order.stock_ordergroup_name', user: users.uniq.sort.join(', '))
end
end

173
lib/render_pdf.rb Normal file
View File

@ -0,0 +1,173 @@
require 'prawn/measurement_extensions'
class RotatedCell < Prawn::Table::Cell::Text
def initialize(pdf, text, options={})
options[:content] = text
options[:valign] = :center
options[:align] = :center
options[:rotate_around] = :center
@rotation = -options[:rotate] || 0
super(pdf, [0, pdf.cursor], options)
end
def tan_rotation
Math.tan(Math::PI * @rotation / 180)
end
def skew
(height + (border_top_width / 2.0) + (border_bottom_width / 2.0)) / tan_rotation
end
def styled_width_of(text)
options = @text_options.reject { |k| k == :style }
with_font { (@pdf.height_of(@content, options) + padding_top + padding_bottom) / tan_rotation }
end
def natural_content_height
options = @text_options.reject { |k| k == :style }
with_font { (@pdf.width_of(@content, options) + padding_top + padding_bottom) * tan_rotation }
end
def draw_borders(pt)
@pdf.mask(:line_width, :stroke_color) do
x, y = pt
from = [[x - skew , y + (border_top_width / 2.0)],
to = [x, y - height - (border_bottom_width / 2.0)]]
@pdf.line_width = @border_widths[3]
@pdf.stroke_color = @border_colors[3]
@pdf.stroke_line(from, to)
@pdf.undash
end
end
def draw_content
with_font do
with_text_color do
text_box(width: spanned_content_width + FPTolerance + skew,
height: spanned_content_height + FPTolerance,
at: [1-skew, @pdf.cursor]).render
end
end
end
end
class RenderPDF < Prawn::Document
include ActionView::Helpers::NumberHelper
include ApplicationHelper
TOP_MARGIN = 36
BOTTOM_MARGIN = 23
HEADER_SPACE = 9
FOOTER_SPACE = 3
HEADER_FONT_SIZE = 16
FOOTER_FONT_SIZE = 8
DEFAULT_FONT = 'OpenSans'
def initialize(options = {})
options[:font_size] ||= FoodsoftConfig[:pdf_font_size].try(:to_f) || 12
options[:page_size] ||= FoodsoftConfig[:pdf_page_size] || 'A4'
options[:skip_page_creation] = true
@options = options
@first_page = true
super(options)
# Use ttf for better utf-8 compability
font_families.update(
'OpenSans' => {
bold: font_path('OpenSans-Bold.ttf'),
italic: font_path('OpenSans-Italic.ttf'),
bold_italic: font_path('OpenSans-BoldItalic.ttf'),
normal: font_path('OpenSans-Regular.ttf')
}
)
header = title
footer = I18n.l(Time.now, format: :long)
header_size = 0
header_size = height_of(header, size: HEADER_FONT_SIZE, font: DEFAULT_FONT) + HEADER_SPACE if header
footer_size = height_of(footer, size: FOOTER_FONT_SIZE, font: DEFAULT_FONT) + FOOTER_SPACE
start_new_page(top_margin: TOP_MARGIN + header_size, bottom_margin: BOTTOM_MARGIN + footer_size)
font DEFAULT_FONT
repeat :all, dynamic: true do
bounding_box [bounds.left, bounds.top + header_size], width: bounds.width, height: header_size do
text header, size: HEADER_FONT_SIZE, align: :center, overflow: :shrink_to_fit if header
end
font_size FOOTER_FONT_SIZE do
bounding_box [bounds.left, bounds.bottom - FOOTER_SPACE], width: bounds.width, height: footer_size do
text footer, align: :left, valign: :bottom
end
bounding_box [bounds.left, bounds.bottom - FOOTER_SPACE], width: bounds.width, height: footer_size do
text I18n.t('lib.render_pdf.page', number: page_number, count: page_count), align: :right, valign: :bottom
end
end
end
end
def title
nil
end
def to_pdf
body # Add content, which is defined in subclasses
render # Render pdf
end
# Helper method to test pdf via rails console: OrderByGroups.new(order).save_tmp
def save_tmp
File.open("#{Rails.root}/tmp/#{self.class.to_s.underscore}.pdf", 'w') {|f| f.write(to_pdf.force_encoding("UTF-8")) }
end
# @todo avoid underscore instead of unicode whitespace in pdf :/
def number_to_currency(number, options={})
super(number, options).gsub("\u202f", ' ') if number
end
def font_size(points = nil, &block)
points *= @options[:font_size] / 12 if points
super(points, &block)
end
# add pagebreak or vertical whitespace, depending on configuration
def down_or_page(space=10)
if @first_page
@first_page = false
return
end
if pdf_add_page_breaks?
start_new_page
else
move_down space
end
end
protected
def fontsize(n)
n
end
# return whether pagebreak or vertical whitespace is used for breaks
def pdf_add_page_breaks?(docid=nil)
docid ||= self.class.name.underscore
cfg = FoodsoftConfig[:pdf_add_page_breaks]
if cfg.is_a? Array
cfg.index(docid.to_s).any?
elsif cfg.is_a? Hash
cfg[docid.to_s]
else
cfg
end
end
def font_path(name)
File.join(Rails.root, 'vendor', 'assets', 'fonts', name)
end
end