Class: OrderPdf

Inherits:
RenderPdf show all
Includes:
ArticlesHelper
Defined in:
lib/order_pdf.rb

Direct Known Subclasses

OrderByArticles, OrderByGroups, OrderFax, OrderMatrix

Constant Summary

Constants inherited from RenderPdf

RenderPdf::BOTTOM_MARGIN, RenderPdf::DEFAULT_FONT, RenderPdf::FOOTER_FONT_SIZE, RenderPdf::FOOTER_SPACE, RenderPdf::HEADER_FONT_SIZE, RenderPdf::HEADER_SPACE, RenderPdf::TOP_MARGIN

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Methods inherited from RenderPdf

#down_or_page, #font_path, #font_size, #fontsize, #number_to_currency, #pdf_add_page_breaks?, #title, #to_pdf

Methods included from ApplicationHelper

#base_errors, #bootstrap_flash_patched, #close_button, #expand, #foodcoop_css_path, #foodcoop_css_tag, #format_currency, #format_date, #format_datetime, #format_datetime_timespec, #format_iban, #format_number, #format_roles, #format_time, #heading_helper, #icon, #items_per_page, #link_to_gmaps, #link_to_top, #pagination_links_remote, #remote_link_to, #show_title?, #show_user, #show_user_link, #sort_link_helper, #tab_is_active?, #title, #truncate, #weekday

Methods included from PathHelper

#finance_group_transactions_path

Constructor Details

#initialize(order, options = {}) ⇒ OrderPdf

Returns a new instance of OrderPdf.



5
6
7
8
9
# File 'lib/order_pdf.rb', line 5

def initialize(order, options = {})
  @order = order
  @orders = order
  super(options)
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



3
4
5
# File 'lib/order_pdf.rb', line 3

def order
  @order
end

Instance Method Details

#billing_article_result(goa) ⇒ Object (protected)



81
82
83
84
85
86
87
# File 'lib/order_pdf.rb', line 81

def billing_article_result(goa)
  article_version = goa.order_article.article_version
  number_with_precision(
    article_version.convert_quantity(goa.result, article_version.group_order_unit,
                                     article_version.billing_unit), precision: 2, strip_insignificant_zeros: true
  )
end

#billing_quantity_with_tolerance(goa) ⇒ Object (protected)



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/order_pdf.rb', line 64

def billing_quantity_with_tolerance(goa)
  article_version = goa.order_article.article_version
  quantity = number_with_precision(
    article_version.convert_quantity(goa.quantity, article_version.group_order_unit,
                                     article_version.billing_unit), strip_insignificant_zeros: true, precision: 2
  )
  tolerance = number_with_precision(
    article_version.convert_quantity(goa.tolerance, article_version.group_order_unit,
                                     article_version.billing_unit), strip_insignificant_zeros: true, precision: 2
  )
  goa.tolerance > 0 ? "#{quantity} + #{tolerance}" : quantity
end

#each_group_order_article_for_order_article(order_article) ⇒ Object (protected)



155
156
157
# File 'lib/order_pdf.rb', line 155

def each_group_order_article_for_order_article(order_article, &)
  order_article.group_order_articles.each(&)
end

#each_group_order_article_for_ordergroup(ordergroup) ⇒ Object (protected)



159
160
161
162
163
164
165
# File 'lib/order_pdf.rb', line 159

def each_group_order_article_for_ordergroup(ordergroup, &)
  group_order_articles(ordergroup)
    .includes(order_article: { article_version: { article: :supplier } })
    .order('suppliers.name, article_versions.name')
    .preload(order_article: %i[article_version order])
    .each(&)
end

#each_order_articleObject (protected)



119
120
121
# File 'lib/order_pdf.rb', line 119

def each_order_article(&)
  order_articles.each(&)
end

#each_ordergroupObject (protected)



123
124
125
# File 'lib/order_pdf.rb', line 123

def each_ordergroup(&)
  ordergroups.each(&)
end

#each_ordergroup_batch(batch_size) ⇒ Object (protected)



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/order_pdf.rb', line 127

def each_ordergroup_batch(batch_size)
  offset = 0

  loop do
    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', Arel.sql('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|
        number_with_precision grouped_value[group_id].try(:first).try(:third), strip_insignificant_zeros: true
      end
    end

    yield go_records, results
    offset += batch_size
  end
end

#group_order_article_result(goa) ⇒ Object (protected)



77
78
79
# File 'lib/order_pdf.rb', line 77

def group_order_article_result(goa)
  number_with_precision goa.result, strip_insignificant_zeros: true
end

#group_order_articles(ordergroup) ⇒ Object (protected)



89
90
91
92
93
# File 'lib/order_pdf.rb', line 89

def group_order_articles(ordergroup)
  GroupOrderArticle
    .includes(:group_order)
    .where(group_orders: { order_id: @orders, ordergroup_id: ordergroup })
end

#nice_table(name, data, dimrows = []) ⇒ Object



11
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
# File 'lib/order_pdf.rb', line 11

def nice_table(name, data, dimrows = [])
  name_options = { size: 10, style: :bold }
  name_height = height_of name, name_options
  made_table = make_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'

    # 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

    yield table if block_given?
  end

  if name_height + made_table.height < cursor
    down_or_page 15
  else
    start_new_page
  end

  text name, name_options
  made_table.draw
end

#order_article_price(order_article) ⇒ Number (protected)

Return price for order_article.

This is a separate method so that plugins can override it.

Parameters:

Returns:

  • (Number)

    Price to show

See Also:



50
51
52
# File 'lib/order_pdf.rb', line 50

def order_article_price(order_article)
  order_article.article_version.fc_group_order_price
end

#order_article_price_per_unit(order_article) ⇒ Object (protected)



54
55
56
# File 'lib/order_pdf.rb', line 54

def order_article_price_per_unit(order_article)
  "#{number_to_currency(order_article_price(order_article))} / #{format_group_order_unit_with_ratios(order_article.article_version)}"
end

#order_articlesObject (protected)



95
96
97
98
99
100
101
102
103
# File 'lib/order_pdf.rb', line 95

def order_articles
  OrderArticle
    .ordered
    .includes(article_version: { article: :supplier })
    .includes(group_order_articles: { group_order: :ordergroup })
    .where(order: @orders)
    .order('suppliers.name, article_versions.name, groups.name')
    .preload(:article_version)
end

#ordergroups(offset = nil, limit = nil) ⇒ Object (protected)



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/order_pdf.rb', line 105

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)', 'ordergroup_id', 'SUM(group_orders.transport)')

  result.map do |item|
    [item.first || stock_ordergroup_name] + item[1..]
  end
end

#price_per_billing_unit(goa) ⇒ Object (protected)



58
59
60
61
62
# File 'lib/order_pdf.rb', line 58

def price_per_billing_unit(goa)
  article_version = goa.order_article.article_version
  "#{number_to_currency(article_version.convert_quantity(article_version.fc_price, article_version.billing_unit,
                                                         article_version.supplier_order_unit))} / #{format_billing_unit_with_ratios(article_version)}"
end

#stock_ordergroup_nameObject (protected)



167
168
169
170
171
172
173
174
175
# File 'lib/order_pdf.rb', line 167

def stock_ordergroup_name
  users = GroupOrder.stock
                    .eager_load(:updated_by)
                    .where(order: @orders)
                    .map(&:updated_by)
                    .map { |u| u.try(&:name) || '?' }

  I18n.t('model.group_order.stock_ordergroup_name', user: users.uniq.sort.join(', '))
end