Class: Pdf::Document::LetterMaterials

Inherits:
BaseService
  • Object
show all
Includes:
Base
Defined in:
app/services/pdf/document/letter_materials.rb

Overview

Service object: letter materials.

Defined Under Namespace

Classes: Result

Constant Summary

Constants included from Base

Base::FONT, Base::NIMBUS_SANS_PATH, Base::NIMBUS_SANS_PATH_BOLD, Base::WY_LOGO_PATH

Instance Method Summary collapse

Constructor Details

#initialize(quote, _options) ⇒ LetterMaterials

Returns a new instance of LetterMaterials.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/pdf/document/letter_materials.rb', line 11

def initialize(quote, _options)
  @quote = quote if quote
  @salutation = begin
    quote.primary_party.name
  rescue StandardError
    nil
  end || begin
    quote.shipping_address.person_name
  rescue StandardError
    nil
  end || quote.customer.name
  @quote_recipient = begin
    quote.customer.full_name
  rescue StandardError
    nil
  end
  @line_grouped = quote.line_items_grouped_by_room_configuration(group_by_item_category: false)
end

Instance Method Details

#adjust_column_widths(column_widths_array, column_header_widths) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/services/pdf/document/letter_materials.rb', line 193

def adjust_column_widths(column_widths_array, column_header_widths)
  column_widths_array.each_with_index do |width, i|
    next if i.zero?

    max_column_width = column_widths_array.max
    max_column_index = column_widths_array.index(max_column_width)

    next unless width < column_header_widths[i]

    difference = column_header_widths[i] - width

    column_widths_array[i] += difference

    column_widths_array[max_column_index] -= difference
  end

  column_widths_array
end

#callObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/pdf/document/letter_materials.rb', line 30

def call
  composer = build_composer(margin: [80, 25, 50, 25])

  defineStyles(composer)

  show_materials_only(composer)

  composer.document.pages.each do |page|
    canvas = page.canvas
    page.box(:media).width
    page_height = page.box(:media).height

    logo_path = Rails.public_path.join('images', 'pdf', 'new-wylogo.png').to_s
    support_path = Rails.public_path.join('images', 'pdf', '247.png').to_s

    canvas.image(logo_path, at: [17.5, page_height - 65], width: 200)
    canvas.composer.formatted_text(
      [
        { text: "Materials list for ##{@quote.reference_number}\n", style: { font: FONT, font_size: 14.5, line_height: 14, fill_color: '333' } },
        { text: "Job: #{@quote.opportunity.name}", style: { font: FONT, font_size: 11, line_height: 14, fill_color: '999' } }
      ],
      align: :right,
      padding: [20, 30],
      style: { text_align: :right },
      width: 305
    )
    canvas.composer.formatted_text([
                                     { text: @quote.company.legal_name, style: :regularNim },
                                     { text: ' | ', style: :regularNim },
                                     { box: [:image, support_path], height: 12, valign: :text, padding: [0, 0, -3, 0] },
                                     { text: ' Technical Support', style: :regularNim },
                                     { text: ' | ', style: :regularNim },
                                     { text: '(800) 875-5285', style: :regularNim },
                                     { text: ' | ', style: :regularNim },
                                     { link: 'www.WarmlyYours.com/support', style: :regularNim }
                                   ], align: :center, valign: :bottom, style: { padding: [0, 0, 3, 0] })
  end

  buffer = StringIO.new
  composer.write(buffer, optimize: true)
  Result.new(pdf: buffer.string, file_name: "materials_#{@quote.reference_number}.pdf")
end

#defineStyles(composer) ⇒ Object



166
167
168
169
170
# File 'app/services/pdf/document/letter_materials.rb', line 166

def defineStyles(composer)
  composer.style(:regularNim, font: FONT, font_size: 12)
  composer.style(:regularNimBold, font: "#{FONT} bold", font_size: 12)
  composer.style(:lineItem, font: FONT, font_size: 11)
end

#show_materials_only(composer) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
154
155
156
157
158
159
160
161
162
163
164
# File 'app/services/pdf/document/letter_materials.rb', line 73

def show_materials_only(composer)
  l = composer.document.layout

  defineStyles(composer)

  return if @line_grouped.blank?

  unless @quote_recipient.nil?
    composer.container do |container|
      container.text('Exclusively prepared for:', style: { font: FONT, font_size: 12, fill_color: '666' }, padding: [0, 0, 2, 0])
      container.text(@quote_recipient, style: { font: FONT, font_size: 15, fill_color: '222' }, padding: [0, 0, 10, 0])
    end
  end

  table_data = []

  table_data << [l.text('Date:', style: :regularNimBold), l.text(Date.current.to_fs(:long), style: :regularNim)]

  table_data << [l.text('Contact:', style: :regularNimBold), l.text(@quote.contact.full_name, style: :regularNim)] if @quote.contact

  table_data << [l.text('Job Summary:', style: :regularNimBold), l.text(@quote.opportunity.summary, style: :regularNim)] if @quote.opportunity.summary.present?

  table_data << [l.text('Reference:', style: :regularNimBold), l.text(@quote.suffix, style: :regularNim)] if @quote.suffix.present?

  composer.table(table_data,
                 column_widths: [120, 160],
                 cell_style: {
                   border: { width: 0 },
                   padding: [3, 2],
                   font: FONT,
                   font_size: 12,
                   fill_color: '444'
                 })

  @line_grouped.each do |rc, line_items|
    next if line_items.empty?

    long_name = rc.is_a?(String) ? rc : rc.name_with_config
    plan = rc.is_a?(String) ? nil : "Plan ##{rc.reference_number}"

    composer.text(long_name, style: { font: FONT, font_size: 14, fill_color: '222', padding: [10, 0, 2, 0] })
    composer.text(plan, style: { font: FONT, font_size: 11, fill_color: '777', padding: [0, 0, 5, 0] }) if plan

    table_data = []
    table_data << ['Part #', 'Description', 'Qty'].map do |header|
      composer.document.layout.text(header, style: { font: "#{FONT} bold", font_size: 12, text_align: :center, fill_color: '333' })
    end

    max_widths = { sku: 0, qty: 0 }

    LineItem.group_by_category(line_items).each do |category, category_items|
      table_data << [
        { content: composer.document.layout.text(category.pluralize, style: { font: "#{FONT} bold", font_size: 12, fill_color: '000' }), col_span: 3 }
      ]

      category_items.each do |item|
        sku = item.sku.to_s
        qty = item.quantities_indicator_visible? ? item.quantity : ''

        max_widths[:sku] = [max_widths[:sku], text_width(composer, sku, 11)].max
        max_widths[:qty] = [max_widths[:qty], text_width(composer, qty.to_s, 11)].max

        table_data << [
          composer.document.layout.text(sku, style: :lineItem),
          composer.document.layout.text(item.description || item.name, style: :lineItem),
          composer.document.layout.text(qty.to_s, style: { font: FONT, font_size: 11, text_align: :center })
        ]

        next unless @show_detailed_description

        table_data << [
          composer.document.layout.text(item.item.detailed_description_html.to_s, style: { font: FONT, font_size: 10 }), '', ''
        ]
      end
    end

    max_widths[:sku] = [max_widths[:sku], text_width(composer, 'Part #', 12)].max
    max_widths[:qty] = [max_widths[:qty], text_width(composer, 'Qty', 12)].max

    sku_col_width  = max_widths[:sku].ceil + 10
    qty_col_width  = max_widths[:qty].ceil + 10

    desc_col_width = 561 - sku_col_width - qty_col_width

    column_widths = [sku_col_width, desc_col_width, qty_col_width]

    composer.table(table_data, column_widths: column_widths, style: { font: FONT, font_size: 11 }, cell_style: {
                     border: { width: [0.5, 0, 0, 0], color: 'CDCDCD' },
                     padding: [6, 4, 4, 4]
                   })
  end
end

#text_width(composer, text, font_size = 12) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/services/pdf/document/letter_materials.rb', line 172

def text_width(composer, text, font_size = 12)
  return 0 if text.nil?

  font_size = font_size.to_f

  layout = HexaPDF::Layout::TextLayouter.new
  layout.style.font = FONT
  layout.style.font_size = font_size

  text_fragments = composer.document.layout.text_fragments(text, style: { font: FONT, font_size: font_size })

  text_box = HexaPDF::Layout::TextBox.new(items: text_fragments)

  frame_width = 1000
  frame = HexaPDF::Layout::Frame.new(0, 1000, frame_width, 5000)

  result = frame.fit(text_box)

  result.box.width
end