Skip to content

Commit

Permalink
Solve #42: Improve fax PDF, CSV, text
Browse files Browse the repository at this point in the history
- outsource format_units_to_order to OrderHelper
- fax text: include unit, adjust column width
- fax PDF & text: only include order number if any present
  • Loading branch information
twothreenine committed May 27, 2024
1 parent c376fd9 commit f1e16de
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 22 deletions.
33 changes: 21 additions & 12 deletions app/documents/order_fax.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class OrderFax < OrderPdf
include ArticlesHelper
include OrdersHelper

BATCH_SIZE = 250

Expand Down Expand Up @@ -74,33 +75,41 @@ def recipient_paragraph

def articles_paragraph
total = 0
data = [I18n.t('documents.order_fax.rows')]
data = [I18n.t('documents.order_fax.rows').clone]
any_order_number_present = false
each_order_article do |oa|
price = oa.article_version.price
subtotal = oa.units_to_order * price
total += subtotal
quantity = if oa.article_version.supplier_order_unit_is_si_convertible
number_with_precision(oa.units_to_order, precision: 3)
else
oa.units_to_order.floor
end
data << [oa.article_version.order_number,
quantity,
order_number = oa.article_version.order_number
any_order_number_present = true if order_number
data << [order_number,
format_units_to_order(oa),
format_supplier_order_unit_with_ratios(oa.price),
oa.article_version.name,
number_to_currency(price),
number_to_currency(subtotal)]
end
data << [I18n.t('documents.order_fax.total'), nil, nil, nil, nil, number_to_currency(total)]

total_row = [I18n.t('documents.order_fax.total'), nil, nil, nil, nil, number_to_currency(total)]

unless any_order_number_present
data.map { |row| row.delete_at(0) }
total_row.delete_at(1)
end

data << total_row

table data, cell_style: { size: fontsize(8), overflow: :shrink_to_fit } do |table|
table.header = true
table.cells.border_width = 1
table.cells.border_color = '666666'

table.row(0).border_bottom_width = 2
table.columns(1).align = :right
table.columns(4..5).align = :right
table.row(data.length - 1).columns(0..4).borders = %i[top bottom]
table.columns(-5).align = :right
table.columns(-2..-1).align = :right
table.row(data.length - 1).columns(0).align = :left
table.row(data.length - 1).columns(0..-2).borders = %i[top bottom]
table.row(data.length - 1).columns(0).borders = %i[top bottom left]
table.row(data.length - 1).border_top_width = 2
end
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/orders_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def options_for_suppliers_to_select
options_for_select(options)
end

def format_amount(amount, order_article, strip_insignificant_zeros: false)
strip_insignificant_zeros = true unless order_article.article_version.supplier_order_unit_is_si_convertible
number_with_precision(amount, precision: 3, strip_insignificant_zeros: strip_insignificant_zeros)
end

def format_units_to_order(order_article, strip_insignificant_zeros: false)
format_amount(order_article.units_to_order, order_article, strip_insignificant_zeros: strip_insignificant_zeros)
end

# "1×2 ordered, 2×2 billed, 2×2 received"
def units_history_line(order_article, options = {})
if order_article.order.open?
Expand Down
8 changes: 2 additions & 6 deletions app/lib/order_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class OrderCsv < RenderCsv
include ApplicationHelper
include ArticlesHelper
include OrdersHelper

def header
[
Expand All @@ -17,14 +18,9 @@ def header

def data
@object.order_articles.ordered.includes(:article_version).all.map do |oa|
quantity = if oa.article_version.supplier_order_unit_is_si_convertible
number_with_precision(oa.units_to_order, precision: 3)
else
oa.units_to_order.floor
end
yield [
oa.article_version.order_number,
quantity,
format_units_to_order(oa, strip_insignificant_zeros: true),
format_supplier_order_unit_with_ratios(oa.article_version),
oa.article_version.name,
number_to_currency(oa.article_version.price),
Expand Down
38 changes: 34 additions & 4 deletions app/lib/order_txt.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class OrderTxt
include ActionView::Helpers::NumberHelper
include ArticlesHelper
include OrdersHelper

def initialize(order, _options = {})
@order = order
Expand All @@ -17,11 +19,39 @@ def to_txt
text += '****** ' + I18n.t('orders.fax.to_address') + "\n\n"
text += "#{FoodsoftConfig[:name]}\n#{contact[:street]}\n#{contact[:zip_code]} #{contact[:city]}\n\n"
text += '****** ' + I18n.t('orders.fax.articles') + "\n\n"
text += format("%8s %8s %s\n", I18n.t('orders.fax.number'), I18n.t('orders.fax.amount'),
I18n.t('orders.fax.name'))

# prepare order_articles data
longest_number_string_length = 0
longest_amount_string_length = I18n.t('orders.fax.amount').length
longest_unit_string_length = I18n.t('orders.fax.unit').length
@order_positions = @order.order_articles.ordered.includes(:article_version).map do |oa|
number = oa.article_version.order_number || ''
amount = format_units_to_order(oa).to_s
unit = format_supplier_order_unit_with_ratios(oa.price)
longest_number_string_length = number.length if number.length > longest_number_string_length
longest_amount_string_length = amount.length if amount.length > longest_amount_string_length
longest_unit_string_length = unit.length if unit.length > longest_unit_string_length
{
number: number,
amount: amount,
unit: unit,
name: oa.article_version.name
}
end

if (any_number_present = longest_number_string_length > 0) && longest_number_string_length < I18n.t('orders.fax.number').length
longest_number_string_length = I18n.t('orders.fax.number').length
end

# header for order articles table
text += format('%s ', I18n.t('orders.fax.number').ljust(longest_number_string_length)) if any_number_present
text += format("%s %s %s\n", I18n.t('orders.fax.amount').rjust(longest_amount_string_length),
I18n.t('orders.fax.unit').ljust(longest_unit_string_length), I18n.t('orders.fax.name'))

# now display all ordered articles
@order.order_articles.ordered.includes(:article_version).each do |oa|
text += format("%8s %8.3f %s\n", oa.article_version.order_number, oa.units_to_order, oa.article_version.name)
@order_positions.each do |op|
text += format('%s ', op[:number].ljust(longest_number_string_length)) if any_number_present
text += format("%s %s %s\n", op[:amount].rjust(longest_amount_string_length), op[:unit].ljust(longest_unit_string_length), op[:name])
end
text
end
Expand Down
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,7 @@ de:
heading: Bestellung für %{name}
name: Name
number: Nummer
unit: Einheit
to_address: Versandaddresse
finish:
notice: Die Bestellung wurde beendet.
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,7 @@ en:
heading: Order for %{name}
name: Name
number: Number
unit: Unit
to_address: Shipping address
finish:
notice: The order has been closed.
Expand Down

0 comments on commit f1e16de

Please sign in to comment.