Class: OrderTxt

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, ArticlesHelper, OrdersHelper
Defined in:
lib/order_txt.rb

Instance Method Summary collapse

Methods included from OrdersHelper

#article_version_change_hint, #format_units_to_order, #options_for_suppliers_to_select, #order_article_class, #order_pdf, #ordered_quantities_different_from_group_orders?, #ordergroup_count, #pkg_helper, #pkg_helper_icon, #ratio_quantity_data, #receive_button, #receive_input_field, #supplier_link, #units_history_line, #update_articles_link

Methods included from ArticlesHelper

#field_with_preset_value_and_errors, #format_billing_unit, #format_billing_unit_with_ratios, #format_group_order_unit, #format_group_order_unit_with_ratios, #format_price_unit, #format_supplier_order_unit, #format_supplier_order_unit_with_ratios, #highlight_new, #row_classes

Constructor Details

#initialize(order, _options = {}) ⇒ OrderTxt

Returns a new instance of OrderTxt.



6
7
8
# File 'lib/order_txt.rb', line 6

def initialize(order, _options = {})
  @order = order
end

Instance Method Details

#to_txtObject

Renders the fax-text-file e.g. for easier use with online-fax-software, which don’t accept pdf-files



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/order_txt.rb', line 12

def to_txt
  supplier = @order.supplier
  contact = FoodsoftConfig[:contact].symbolize_keys
  text = I18n.t('orders.fax.heading', name: FoodsoftConfig[:name])
  text += "\n#{Supplier.human_attribute_name(:customer_number)}: #{supplier.customer_number}" if supplier.customer_number.present?
  text += "\n" + I18n.t('orders.fax.delivery_day')
  text += "\n\n#{supplier.name}\n#{supplier.address}\n#{Supplier.human_attribute_name(:fax)}: #{supplier.fax}\n\n"
  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"

  # prepare order_articles data
  order_articles = @order.order_articles.ordered.includes(:article_version).order('article_versions.order_number ASC, article_versions.name ASC')
  any_number_present = order_articles.where.not(article_version: { order_number: nil }).any?

  order_headers = {
    number: any_number_present ? { label: I18n.t('orders.fax.number') } : nil,
    amount: { label: I18n.t('orders.fax.amount'), align: :right },
    unit: { label: I18n.t('orders.fax.unit') },
    name: { label: I18n.t('orders.fax.name') }
  }.compact

  order_positions = order_articles.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)
    {
      number: number,
      amount: amount,
      unit: unit,
      name: oa.article_version.name
    }
  end

  text += text_table(order_headers, order_positions)
  text
end