Rewrite OrderPdfs to support multiple orders at once
This commit is contained in:
parent
80899afcb0
commit
3e156bbbf3
11 changed files with 457 additions and 408 deletions
|
@ -1,12 +1,6 @@
|
|||
# encoding: utf-8
|
||||
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
|
||||
I18n.t('documents.order_by_articles.filename', :name => order.name, :date => order.ends.to_date) + '.pdf'
|
||||
end
|
||||
|
@ -18,59 +12,30 @@ class OrderByArticles < OrderPdf
|
|||
|
||||
def body
|
||||
each_order_article do |order_article|
|
||||
down_or_page
|
||||
|
||||
rows = []
|
||||
dimrows = []
|
||||
each_group_order_article_for(order_article) do |goa|
|
||||
rows << [goa.group_order.ordergroup_name,
|
||||
"#{goa.quantity} + #{goa.tolerance}",
|
||||
goa.result,
|
||||
number_to_currency(goa.total_price(order_article))]
|
||||
rows = [[
|
||||
GroupOrder.human_attribute_name(:ordergroup),
|
||||
GroupOrderArticle.human_attribute_name(:ordered),
|
||||
GroupOrderArticle.human_attribute_name(:received),
|
||||
GroupOrderArticle.human_attribute_name(:total_price)
|
||||
]]
|
||||
|
||||
each_group_order_article_for_order_article(order_article) do |goa|
|
||||
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
|
||||
next if rows.length == 0
|
||||
rows.unshift I18n.t('documents.order_by_articles.rows') # table header
|
||||
next unless rows.length > 1
|
||||
|
||||
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: fontsize(10)
|
||||
table rows, cell_style: {size: fontsize(8), overflow: :shrink_to_fit} do |table|
|
||||
table.column(0).width = 200
|
||||
table.columns(1..3).align = :right
|
||||
name = "#{order_article.article.name} (#{order_article.article.unit} | #{order_article.price.unit_quantity} | #{number_to_currency(order_article.price.fc_price)})"
|
||||
name += " #{order_article.article.supplier.name}" if @options[:show_supplier]
|
||||
nice_table name, rows, dimrows do |table|
|
||||
table.column(0).width = bounds.width / 2
|
||||
table.columns(1..-1).align = :right
|
||||
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
|
||||
|
||||
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
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
# encoding: utf-8
|
||||
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
|
||||
I18n.t('documents.order_by_groups.filename', :name => order.name, :date => order.ends.to_date) + '.pdf'
|
||||
end
|
||||
|
@ -17,91 +11,48 @@ class OrderByGroups < OrderPdf
|
|||
end
|
||||
|
||||
def body
|
||||
# Start rendering
|
||||
each_group_order do |group_order|
|
||||
down_or_page 15
|
||||
|
||||
total = 0
|
||||
rows = []
|
||||
each_ordergroup do |oa_name, oa_total, oa_id|
|
||||
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|
|
||||
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)]
|
||||
each_group_order_article_for_ordergroup(oa_id) do |goa|
|
||||
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
|
||||
next if rows.length == 0
|
||||
rows << [ I18n.t('documents.order_by_groups.sum'), nil, nil, nil, nil, nil, number_to_currency(total)]
|
||||
rows.unshift I18n.t('documents.order_by_groups.rows') # Table Header
|
||||
next unless rows.length > 1
|
||||
rows << [nil, nil, nil, nil, nil, number_to_currency(oa_total)]
|
||||
|
||||
text group_order.ordergroup_name, size: fontsize(9), style: :bold
|
||||
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 = []
|
||||
rows.each { |row| row.delete_at 1 } unless @options[:show_supplier]
|
||||
|
||||
table.column(0).width = 240
|
||||
table.column(2).font_style = :bold
|
||||
table.columns(1..4).align = :right
|
||||
table.column(6).align = :right
|
||||
table.column(6).font_style = :bold
|
||||
nice_table oa_name || stock_ordergroup_name, rows, dimrows do |table|
|
||||
table.row(-2).border_width = 1
|
||||
table.row(-2).border_color = '666666'
|
||||
table.row(-1).borders = []
|
||||
|
||||
# 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
|
||||
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(-4..-1).align = :right
|
||||
table.column(-3).font_style = :bold
|
||||
table.column(-1).font_style = :bold
|
||||
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
|
||||
|
|
|
@ -3,8 +3,6 @@ class OrderFax < OrderPdf
|
|||
|
||||
BATCH_SIZE = 250
|
||||
|
||||
attr_reader :order
|
||||
|
||||
def filename
|
||||
I18n.t('documents.order_fax.filename', :name => order.name, :date => order.ends.to_date) + '.pdf'
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
||||
page_number = 0
|
||||
total_num_order_articles = order_articles.size
|
||||
table.columns(-3..-1).align = :right
|
||||
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)
|
||||
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)
|
||||
rows = [[nil, nil] + header]
|
||||
|
||||
last_supplier_id = -1
|
||||
|
||||
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
|
||||
|
||||
# Collect group results
|
||||
groups_data = [header]
|
||||
|
||||
@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
|
||||
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']
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
end
|
||||
|
|
|
@ -55,14 +55,17 @@ de:
|
|||
name: Name
|
||||
financial_transaction_class: Finanztransaktionsklasse
|
||||
group_order:
|
||||
ordergroup: Bestellgruppe
|
||||
price: Bestellsumme
|
||||
updated_by: Zuletzt bestellt
|
||||
group_order_article:
|
||||
ordergroup_id: Bestellgruppe
|
||||
ordered: Bestellt
|
||||
quantity: Menge
|
||||
received: Bekommen
|
||||
result: Menge
|
||||
tolerance: Toleranz
|
||||
total_price: Summe
|
||||
unit_price: Preis/Einheit
|
||||
invoice:
|
||||
amount: Betrag
|
||||
attachment: Anhang
|
||||
|
@ -784,22 +787,9 @@ de:
|
|||
title: Aktuelle Bestellungen - Gruppensortierung
|
||||
order_by_articles:
|
||||
filename: Bestellung %{name}-%{date} - Artikelsortierung
|
||||
rows:
|
||||
- Bestellgruppe
|
||||
- Bestellt
|
||||
- Bekommen
|
||||
- Preis
|
||||
title: 'Artikelsortierung der Bestellung: %{name}, beendet am %{date}'
|
||||
order_by_groups:
|
||||
filename: Bestellung %{name}-%{date} - Gruppensortierung
|
||||
rows:
|
||||
- Artikel
|
||||
- Bestellt
|
||||
- Bekommen
|
||||
- Preis
|
||||
- GebGr
|
||||
- Einheit
|
||||
- Summe
|
||||
sum: Summe
|
||||
title: 'Gruppensortierung der Bestellung: %{name}, beendet am %{date}'
|
||||
order_fax:
|
||||
|
@ -815,17 +805,8 @@ de:
|
|||
total: Gesamtpreis
|
||||
order_matrix:
|
||||
filename: Bestellung %{name}-%{date} - Sortiermatrix
|
||||
heading: Artikelübersicht
|
||||
rows:
|
||||
- Artikel
|
||||
- Einheit
|
||||
- Gebinde
|
||||
- FC-Preis
|
||||
- Menge
|
||||
heading: Artikelübersicht (%{count})
|
||||
title: 'Sortiermatrix der Bestellung: %{name}, beendet am %{date}'
|
||||
total:
|
||||
one: Insgesamt ein Artikel
|
||||
other: Insgesamt %{count} Artikel
|
||||
errors:
|
||||
general: Ein Problem ist aufgetreten.
|
||||
general_again: Ein Fehler ist aufgetreten. Bitte erneut versuchen.
|
||||
|
@ -1234,7 +1215,7 @@ de:
|
|||
profile: Profil bearbeiten
|
||||
logo: "<span>food</span>soft"
|
||||
lib:
|
||||
order_pdf:
|
||||
render_pdf:
|
||||
page: Seite %{number} von %{count}
|
||||
login:
|
||||
accept_invitation:
|
||||
|
|
|
@ -55,14 +55,17 @@ en:
|
|||
name: Name
|
||||
financial_transaction_class: Financial transaction class
|
||||
group_order:
|
||||
ordergroup: Ordergroup
|
||||
price: Order sum
|
||||
updated_by: Last ordered by
|
||||
group_order_article:
|
||||
ordergroup_id: Ordergroup
|
||||
ordered: Ordered
|
||||
quantity: Amount
|
||||
received: Received
|
||||
result: Amount
|
||||
tolerance: Tolerance
|
||||
total_price: Sum
|
||||
unit_price: Price/Unit
|
||||
invoice:
|
||||
amount: Amount
|
||||
attachment: Attachment
|
||||
|
@ -786,22 +789,9 @@ en:
|
|||
title: Current orders - by group
|
||||
order_by_articles:
|
||||
filename: Order %{name}-%{date} - by articles
|
||||
rows:
|
||||
- Ordergroup
|
||||
- Ordered
|
||||
- Received
|
||||
- Price
|
||||
title: 'Order sorted by articles: %{name}, closed at %{date}'
|
||||
order_by_groups:
|
||||
filename: Order %{name}-%{date} - by group
|
||||
rows:
|
||||
- Article
|
||||
- Ordered
|
||||
- Received
|
||||
- Price
|
||||
- U.Q.
|
||||
- Unit
|
||||
- Sum
|
||||
sum: Sum
|
||||
title: 'Order sorted by group: %{name}, closed at %{date}'
|
||||
order_fax:
|
||||
|
@ -817,17 +807,8 @@ en:
|
|||
total: Total
|
||||
order_matrix:
|
||||
filename: Order %{name}-%{date} - sorting matrix
|
||||
heading: Article overview
|
||||
rows:
|
||||
- Article
|
||||
- Unit
|
||||
- Unit quantity
|
||||
- FC-Price
|
||||
- Amount
|
||||
heading: Article overview (%{count})
|
||||
title: 'Order sorting matrix: %{name}, closed at %{date}'
|
||||
total:
|
||||
one: One article in total
|
||||
other: "%{count} articles in total"
|
||||
errors:
|
||||
general: A problem has occured.
|
||||
general_again: A problem has occured. Please try again.
|
||||
|
@ -1236,7 +1217,7 @@ en:
|
|||
profile: Edit profile
|
||||
logo: "<span>food</span>soft"
|
||||
lib:
|
||||
order_pdf:
|
||||
render_pdf:
|
||||
page: Page %{number} of %{count}
|
||||
login:
|
||||
accept_invitation:
|
||||
|
|
|
@ -50,14 +50,17 @@ es:
|
|||
ordergroup: Grupo de pedido
|
||||
user: Ingresado por
|
||||
group_order:
|
||||
ordergroup: Grupo de pedido
|
||||
price: Suma de pedidos
|
||||
updated_by: Pedido por última vez por
|
||||
group_order_article:
|
||||
ordergroup_id: Grupo de pedido
|
||||
ordered: Pedido el
|
||||
quantity: Importe
|
||||
received: Recibido
|
||||
result: Importe
|
||||
tolerance: Tolerancia
|
||||
total_price: Suma
|
||||
unit_price: Precio/Unidad
|
||||
invoice:
|
||||
amount: Importe
|
||||
attachment: Adjunto
|
||||
|
@ -762,22 +765,9 @@ es:
|
|||
title: Pedidos activos - por grupo
|
||||
order_by_articles:
|
||||
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}'
|
||||
order_by_groups:
|
||||
filename: Pedido %{name}-%{date} - por grupo
|
||||
rows:
|
||||
- Artículo
|
||||
- Pedido
|
||||
- Recibido
|
||||
- Precio
|
||||
- C.U.
|
||||
- Unidad
|
||||
- Suma
|
||||
sum: Suma
|
||||
title: 'Pedido ordenado por grupo: %{name}, cerrado el %{date}'
|
||||
order_fax:
|
||||
|
@ -793,15 +783,8 @@ es:
|
|||
total: Total
|
||||
order_matrix:
|
||||
filename: Pedido %{name}-%{date} - matrix para ordenar
|
||||
heading: Descripción del artículo
|
||||
rows:
|
||||
- Artículo
|
||||
- Unidad
|
||||
- Cantidad de unidades
|
||||
- Precio al grupo
|
||||
- Importe
|
||||
heading: Descripción del artículo (%{count})
|
||||
title:
|
||||
total: "%{count} artículos in total"
|
||||
errors:
|
||||
general: Ha ocurrido un problema.
|
||||
general_again: Ha ocurrido un problema. Por favor inténtalo de nuevo.
|
||||
|
|
|
@ -50,14 +50,17 @@ fr:
|
|||
ordergroup: Cellule
|
||||
user: Créateur-trice
|
||||
group_order:
|
||||
ordergroup: Cellule
|
||||
price: Total de la commande
|
||||
updated_by: Dernière commande
|
||||
group_order_article:
|
||||
ordergroup_id: Cellule
|
||||
ordered: Commandée
|
||||
quantity: Quantité
|
||||
result: Quantité
|
||||
received: Quantité
|
||||
tolerance: Tolérance
|
||||
total_price: Total
|
||||
unit_price: Prix/Unité
|
||||
invoice:
|
||||
amount: Montant
|
||||
attachment: Appendice
|
||||
|
@ -758,22 +761,9 @@ fr:
|
|||
title:
|
||||
order_by_articles:
|
||||
filename: Commande %{name}-%{date} - Trier par
|
||||
rows:
|
||||
- Cellule
|
||||
-
|
||||
- Quantité
|
||||
- Prix
|
||||
title: 'Ordre des produits pour la commande: %{name}, clôturée le %{date}'
|
||||
order_by_groups:
|
||||
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
|
||||
title: 'Répartition par cellules pour la commande: %{name}, clôturée le %{date}'
|
||||
order_fax:
|
||||
|
@ -789,17 +779,8 @@ fr:
|
|||
total: Total
|
||||
order_matrix:
|
||||
filename: Commande %{name}-%{date} - Tableau de répartition
|
||||
heading: Liste des produits
|
||||
rows:
|
||||
- Nom du produit
|
||||
- Unité
|
||||
- Nombre d'unités
|
||||
- Prix coop
|
||||
- Quantité
|
||||
heading: Liste des produits (%{count})
|
||||
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:
|
||||
general: Un problème a été rencontré.
|
||||
general_again: Une erreur s'est produite. Merci de réessayer.
|
||||
|
@ -1219,7 +1200,7 @@ fr:
|
|||
profile: Ton profil
|
||||
logo: coop
|
||||
lib:
|
||||
order_pdf:
|
||||
render_pdf:
|
||||
page: page %{number} de %{count}
|
||||
login:
|
||||
accept_invitation:
|
||||
|
|
|
@ -50,14 +50,17 @@ nl:
|
|||
ordergroup: Huishouden
|
||||
user: Ingevuld door
|
||||
group_order:
|
||||
ordergroup: Huishouden
|
||||
price: Totaal bestelling
|
||||
updated_by: Laatst besteld door
|
||||
group_order_article:
|
||||
ordergroup_id: Huishouden
|
||||
ordered: Besteld
|
||||
quantity: Hoeveelheid
|
||||
received: Ontvangen
|
||||
result: Hoeveelheid
|
||||
tolerance: Tolerantie
|
||||
total_price: Som
|
||||
unit_price: Prijs/Eenheid
|
||||
invoice:
|
||||
amount: Bedrag
|
||||
attachment: Bijlage
|
||||
|
@ -758,22 +761,9 @@ nl:
|
|||
title: Huidige bestellingen - per huishouden
|
||||
order_by_articles:
|
||||
filename: Bestelling %{name}-%{date} - Artikellijst
|
||||
rows:
|
||||
- Huishouden
|
||||
- Besteld
|
||||
- Ontvangen
|
||||
- Prijs
|
||||
title: 'Artikellijst van bestelling: %{name}, gesloten op %{date}'
|
||||
order_by_groups:
|
||||
filename: Bestelling %{name}-%{date} - Huishoudenslijst
|
||||
rows:
|
||||
- Artikel
|
||||
- Besteld
|
||||
- Ontvangen
|
||||
- Prijs
|
||||
- Colli
|
||||
- Eenheid
|
||||
- Som
|
||||
sum: Som
|
||||
title: 'Huishoudenslijst van bestelling: %{name}, gesloten op %{date}'
|
||||
order_fax:
|
||||
|
@ -789,17 +779,8 @@ nl:
|
|||
total: Totaal
|
||||
order_matrix:
|
||||
filename: Bestelling %{name}-%{date} - Sorteermatrix
|
||||
heading: Artikeloverzicht
|
||||
rows:
|
||||
- Artikel
|
||||
- Eenheid
|
||||
- Gr.Eenh.
|
||||
- Foodcoop-prijs
|
||||
- Aantal
|
||||
heading: Artikeloverzicht (%{count})
|
||||
title: 'Sorteermatrix van bestelling: %{name}, gesloten op %{date}'
|
||||
total:
|
||||
one: In totaal éen artikel
|
||||
other: In totaal %{count} artikelen
|
||||
errors:
|
||||
general: Er is een probleem opgetreden.
|
||||
general_again: Er is een fout opgetreden. Probeer het opnieuw.
|
||||
|
@ -1210,7 +1191,7 @@ nl:
|
|||
profile: Profiel aanpassen
|
||||
logo: "<span>food</span>soft"
|
||||
lib:
|
||||
order_pdf:
|
||||
render_pdf:
|
||||
page: Pagina %{number} van %{count}
|
||||
login:
|
||||
accept_invitation:
|
||||
|
|
208
lib/order_pdf.rb
208
lib/order_pdf.rb
|
@ -1,104 +1,142 @@
|
|||
require "prawn/measurement_extensions"
|
||||
class OrderPdf < RenderPDF
|
||||
|
||||
class OrderPdf < Prawn::Document
|
||||
include ActionView::Helpers::NumberHelper
|
||||
attr_reader :order
|
||||
|
||||
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
|
||||
@options = options
|
||||
@first_page = true
|
||||
@orders = order
|
||||
super(options)
|
||||
end
|
||||
|
||||
def to_pdf
|
||||
# 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")
|
||||
}
|
||||
)
|
||||
font "OpenSans"
|
||||
def nice_table(name, data, dimrows = [])
|
||||
down_or_page 25
|
||||
text name, size: 10, style: :bold
|
||||
table data, width: bounds.width, cell_style: {size: 8, overflow: :shrink_to_fit} do |table|
|
||||
# borders
|
||||
table.cells.borders = [:bottom]
|
||||
table.cells.padding_top = 2
|
||||
table.cells.padding_bottom = 4
|
||||
table.cells.border_color = 'dddddd'
|
||||
table.rows(0).border_color = '666666'
|
||||
|
||||
font_size fontsize(12)
|
||||
|
||||
# Define header
|
||||
repeat :all, dynamic: true do
|
||||
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
|
||||
# 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
|
||||
|
||||
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
|
||||
|
||||
# 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
|
||||
yield table if block_given?
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# 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
|
||||
# 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 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
|
||||
|
||||
def font_path(name)
|
||||
File.join(Rails.root, "vendor", "assets", "fonts", name)
|
||||
def each_order_article(&block)
|
||||
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
|
||||
|
||||
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
173
lib/render_pdf.rb
Normal 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
|
Loading…
Reference in a new issue