From f1e16de46a46cb7095912c6ffec22c6394533594 Mon Sep 17 00:00:00 2001 From: twothreenine Date: Mon, 27 May 2024 02:21:30 +0200 Subject: [PATCH] Solve #42: Improve fax PDF, CSV, text - outsource format_units_to_order to OrderHelper - fax text: include unit, adjust column width - fax PDF & text: only include order number if any present --- app/documents/order_fax.rb | 33 +++++++++++++++++++------------ app/helpers/orders_helper.rb | 9 +++++++++ app/lib/order_csv.rb | 8 ++------ app/lib/order_txt.rb | 38 ++++++++++++++++++++++++++++++++---- config/locales/de.yml | 1 + config/locales/en.yml | 1 + 6 files changed, 68 insertions(+), 22 deletions(-) diff --git a/app/documents/order_fax.rb b/app/documents/order_fax.rb index 81c4d10b..a7bce7e0 100644 --- a/app/documents/order_fax.rb +++ b/app/documents/order_fax.rb @@ -1,5 +1,6 @@ class OrderFax < OrderPdf include ArticlesHelper + include OrdersHelper BATCH_SIZE = 250 @@ -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 diff --git a/app/helpers/orders_helper.rb b/app/helpers/orders_helper.rb index a9904933..b04eff9a 100644 --- a/app/helpers/orders_helper.rb +++ b/app/helpers/orders_helper.rb @@ -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? diff --git a/app/lib/order_csv.rb b/app/lib/order_csv.rb index 02753ba7..47a58b5f 100644 --- a/app/lib/order_csv.rb +++ b/app/lib/order_csv.rb @@ -3,6 +3,7 @@ class OrderCsv < RenderCsv include ApplicationHelper include ArticlesHelper + include OrdersHelper def header [ @@ -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), diff --git a/app/lib/order_txt.rb b/app/lib/order_txt.rb index 11a33852..ec79243e 100644 --- a/app/lib/order_txt.rb +++ b/app/lib/order_txt.rb @@ -1,5 +1,7 @@ class OrderTxt include ActionView::Helpers::NumberHelper + include ArticlesHelper + include OrdersHelper def initialize(order, _options = {}) @order = order @@ -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 diff --git a/config/locales/de.yml b/config/locales/de.yml index f9e0cfee..9b4c4ad1 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -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. diff --git a/config/locales/en.yml b/config/locales/en.yml index 9f4d21ef..fe6a49c0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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.