Class: Pdf::Document::BillOfLading

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

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(delivery, options) ⇒ BillOfLading

Returns a new instance of BillOfLading.



7
8
9
10
11
12
13
14
15
16
17
# File 'app/services/pdf/document/bill_of_lading.rb', line 7

def initialize(delivery, options)
  @delivery = delivery
  @ship_to_attributes = delivery.order.ship_to_attributes
  @ship_from_attributes = delivery.order.ship_from_attributes(delivery)
  @ship_datetime = delivery.shipped? || delivery.invoiced? ? delivery.order.shipped_date : delivery.order.get_scheduled_ship_date_time
  @days_in_transit = delivery.chosen_shipping_method.days_commitment.to_i
  @ship_duedatetime = @days_in_transit.positive? ? @days_in_transit.working.day.since(@ship_datetime) : delivery.order.get_expected_ship_date_time
  @shipments = delivery.shipments.completed.top_level.to_a
  @shipments = delivery.shipments.packed_or_awaiting_labels.top_level.to_a unless @shipments.any?
  @shipments = delivery.shipments.suggested.top_level.to_a unless @shipments.any?
end

Instance Method Details

#callObject



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'app/services/pdf/document/bill_of_lading.rb', line 19

def call
  composer = build_composer(margin: [30, 20, 35, 20])

  l = composer.document.layout
  page = composer.document.pages[0]
  canvas = page.canvas
  page_width  = page.box(:media).width
  page_height = page.box(:media).height

  composer.text('STRAIGHT BILL OF LADING - SHORT FORM - ORIGINAL - NOT NEGOTIABLE', style: { font: FONT, font_size: 10.5 })
  composer.text('This form contains only the information necessary for the motor carrier to deliver, rate, and invoice the shipment described below.', style: { font: FONT, font_size: 9 })

  composer.formatted_text([{ text: 'Shipper: ', style: { font: "#{FONT} bold", font_size: 11 } }, { text: 'Ship Date ', style: { font: "#{FONT} bold", font_size: 11 } }, { text: @ship_datetime.strftime('%-m/%-d/%Y'), style: { font: FONT, font_size: 11 } }],
                          style: { padding: [15, 0, 0, 0] })

  contact_lines = []

  ship_from_name = @ship_from_attributes[:name]
  contact_lines << ship_from_name if ship_from_name.present?

  street1 = @ship_from_attributes[:address]&.street1
  contact_lines << street1 if street1.present?

  street2 = @ship_from_attributes[:address]&.street2
  contact_lines << street2 if street2.present?

  city   = @ship_from_attributes[:address]&.city
  state  = @ship_from_attributes[:address]&.state_code
  zip    = @ship_from_attributes[:address]&.zip

  location_line = "#{city}, #{state} #{zip}".strip
  contact_lines << location_line if location_line.present?

  attention = @ship_from_attributes[:attention_name]
  phone     = begin
    PhoneNumber.parse_and_format(@ship_from_attributes[:phone], display_format: :strict_10)
  rescue StandardError
    nil
  end
  contact_lines << [attention, phone].compact.join(' ') if attention.present? || phone.present?

  contacts = [
    [{ content: l.text(contact_lines.join("\n"), style: { font: FONT, font_size: 10.5, line_height: 13 }) }]
  ]

  contact_table = contacts + [
    [{ content: l.text("Reference Number: #{@delivery.order.reference_number}", style: { font: FONT, font_size: 10.5, padding: [4, 0, 0, 0] }) }]
  ]

  block = lambda do |total_rows|
    lambda do |cell|
      cell.style.border = {
        width: if cell.row == total_rows - 1
                 [0.5, 0, 0, 0]
               else
                 [0, 0, 0, 0]
               end
      }
      cell.style.padding = 1
    end
  end

  composer.container(width: 572, style: { padding: [5, 0] }) do |container|
    container.container(width: 281, style: { position: :float, align: :left }) do |left_container|
      left_container.table(contact_table, cell_style: block.call(contact_table.size), style: { border: { width: 1 }, padding: [2, 5] })
    end
    container.container(width: 281, style: { position: :float, align: :right }) do |right_container|
      right_container.text("PRO#\nPlace Stamp Here", style: { fill_color: 'ddd', font: FONT, font_size: 20, text_align: :center, padding: [27, 0, 0, 0] })
    end
  end

  composer.formatted_text([{ text: 'Consignee: ', style: { font: "#{FONT} bold", font_size: 11 } }, { text: 'Due Date ', style: { font: "#{FONT} bold", font_size: 11 } }, { text: @ship_duedatetime.strftime('%-m/%-d/%Y'), style: { font: FONT, font_size: 11 } }],
                          style: { padding: [10, 0, 0, 0] })

  consignee_lines = []

  ship_to_name = @ship_to_attributes[:name]
  consignee_lines << ship_to_name if ship_to_name.present?

  street1 = @ship_to_attributes[:address]&.street1
  consignee_lines << street1 if street1.present?

  street2 = @ship_to_attributes[:address]&.street2
  consignee_lines << street2 if street2.present?

  city   = @ship_to_attributes[:address]&.city
  state  = @ship_to_attributes[:address]&.state_code
  zip    = @ship_to_attributes[:address]&.zip

  location_line = "#{city}, #{state} #{zip}".strip
  consignee_lines << location_line if location_line.present?

  attention = @ship_to_attributes[:attention_name]
  phone     = begin
    PhoneNumber.parse_and_format(@ship_to_attributes[:phone], display_format: :strict_10)
  rescue StandardError
    nil
  end
  consignee_lines << [attention, phone].compact.join(' ') if attention.present? || phone.present?

  consignee_table = [
    [{ content: l.text(consignee_lines.join("\n"), style: { font: FONT, font_size: 10.5, line_height: 13 }) }],
    [{ content: l.text("Reference Number: #{@delivery.order.po_number}", style: { font: FONT, font_size: 10.5, padding: [4, 0, 0, 0] }) }]
  ]

  carrier_table = []

  carrier_name = @delivery.chosen_shipping_method.rate_data['carrier_name'] || @delivery.reported_carrier
  carrier_table << [
    { content: l.text('Carrier:', style: { font: FONT, font_size: 10.5, text_align: :right }) },
    { content: l.text(carrier_name.to_s, style: { font: FONT, font_size: 10.5, text_align: :left }) }
  ]

  pro_number = @delivery.reported_master_tracking_number == @delivery.carrier_bol ? '' : @delivery.reported_master_tracking_number
  carrier_table << [
    { content: l.text('Tracking#/Pro#:', style: { font: FONT, font_size: 10.5, text_align: :right }) },
    { content: l.text(pro_number.to_s, style: { font: FONT, font_size: 10.5, text_align: :left }) }
  ]

  carrier_table << [
    { content: l.text('Load#:', style: { font: FONT, font_size: 10.5, text_align: :right }) },
    { content: l.text(@delivery.freight_load_number.to_s, style: { font: FONT, font_size: 10.5, text_align: :left }) }
  ]

  if @delivery.reported_carrier.to_s.include?('Freightquote')
    carrier_table << [
      { content: l.text('CHR Order#:', style: { font: FONT, font_size: 10.5, text_align: :right }) },
      { content: l.text(@delivery.freight_order_number.to_s, style: { font: FONT, font_size: 10.5, text_align: :left }) }
    ]
    carrier_table << [
      { content: l.text('Ship ID#:', style: { font: FONT, font_size: 10.5, text_align: :right }) },
      { content: l.text(@delivery.id.to_s, style: { font: FONT, font_size: 10.5, text_align: :left }) }
    ]
  else
    carrier_table << [
      { content: l.text('Tracking#/BOL#:', style: { font: FONT, font_size: 10.5, text_align: :right }) },
      { content: l.text(@delivery.carrier_bol.presence || @delivery.reported_master_tracking_number.to_s, style: { font: FONT, font_size: 10.5, text_align: :left }) }
    ]
  end

  composer.container(width: 572, style: { padding: [5, 0] }) do |container|
    container.container(width: 281, style: { position: :float, align: :left }) do |left_container|
      left_container.table(consignee_table, cell_style: block.call(consignee_table.size), style: { border: { width: 1 }, padding: [2, 5] })
    end
    container.container(width: 281, style: { position: :float, align: :right }) do |right_container|
      right_container.table(carrier_table, style: { border: { width: 0 } }, cell_style: { padding: [4, 2, 1, 2] })
    end
  end

  product_rows = [
    [
      { content: l.text('No. of Units & Container Type', style: { font: "#{FONT} bold", font_size: 9, text_align: :left, padding: [10, 0, 0, 0] }), col_span: 7 },
      { content: l.text('*HM', style: { font: "#{FONT} bold", font_size: 9, text_align: :center, padding: [17.9, 0, 0, 0] }), col_span: 2 },
      { content: l.text("BASIC DESCRIPTION\nIdentification Number (UN or NA), Proper Shipping Name, Hazard Class, Packing Group", style: { font: "#{FONT} bold", font_size: 9, text_align: :left }), col_span: 17 },
      { content: l.text('NMFC', style: { font: "#{FONT} bold", font_size: 9, text_align: :left, padding: [17.9, 0, 0, 0] }), col_span: 5 },
      { content: l.text('CLASS', style: { font: "#{FONT} bold", font_size: 9, text_align: :left, padding: [17.9, 0, 0, 0] }), col_span: 5 },
      { content: l.text('Total Weight', style: { font: "#{FONT} bold", font_size: 9, text_align: :center, padding: [10, 0, 0, 0] }), col_span: 3 }
    ]
  ]

  @shipments.each do |shipment|
    product_rows << [
      { content: l.text("1 #{shipment.container_type.titleize}", style: { font: FONT, font_size: 9 }), col_span: 7 },
      { content: l.text('', style: { font: FONT, font_size: 9 }), col_span: 2 },
      { content: l.text("Radiant Heating Elements and Controls (#{shipment.length.ceil}L #{shipment.width.ceil}W #{shipment.height.ceil}H)", style: { font: FONT, font_size: 9 }), col_span: 17 },
      { content: l.text(shipment.effective_nmfc_code.to_s, style: { font: FONT, font_size: 9 }), col_span: 5 },
      { content: l.text(shipment.freight_class.to_s, style: { font: FONT, font_size: 9 }), col_span: 5 },
      { content: l.text(shipment.weight.ceil.to_s, style: { font: FONT, font_size: 9 }), col_span: 3 }
    ]
  end

  total_weight = @shipments.sum(&:weight).ceil

  product_rows << [
    { content: l.text('*An X indicates hazardous material', style: { font: "#{FONT} bold", font_size: 9 }), col_span: 26 },
    { content: l.text('Total weight: (subject to correction)', style: { font: "#{FONT} bold", font_size: 9 }), col_span: 10 },
    { content: l.text(total_weight.to_s, style: { font: "#{FONT} bold", font_size: 9 }), col_span: 3 }
  ]

  composer.text("\n")

  composer.table(product_rows, cell_style: { border: { width: 0.5 }, padding: [4, 2, 1, 2] }, style: { border: { width: 0.5 } })

  special_instructions = []
  special_instructions << 'Non-commercial pickup' if @ship_from_attributes[:address].is_residential
  special_instructions << 'Inside pickup' if @ship_from_attributes[:address].requires_inside_delivery
  special_instructions << 'Liftgate pickup' if @ship_from_attributes[:address].requires_liftgate
  special_instructions << 'Construction site pickup' if @ship_from_attributes[:address].is_construction_site
  special_instructions << 'Limited Access' if @ship_from_attributes[:address].limited_access
  special_instructions << 'Requires Pickup Appointment' if @ship_from_attributes[:address].requires_appointment
  if @ship_from_attributes[:address].country.iso == 'CA' || @ship_to_attributes[:address].country.iso == 'CA'
    if @ship_from_attributes[:address].country.iso != @ship_to_attributes[:address].country.iso
      special_instructions << "#{Delivery::CROSS_BORDER_BROKER_INSTRUCTIONS}, #{Delivery::CROSS_BORDER_COUNTRY_SPECIFIC_BROKER_TEXT[@ship_to_attributes[:address].country.iso.to_sym]}"
    end
    special_instructions << 'Canadian processing'
  end

  consignee_instructions = []
  consignee_instructions << @delivery&.label_instructions.to_s if @delivery&.label_instructions.present?
  consignee_instructions << 'Non-commercial delivery' if @ship_to_attributes[:address].is_residential
  consignee_instructions << 'Inside delivery' if @ship_to_attributes[:address].requires_inside_delivery
  consignee_instructions << 'Liftgate delivery' if @ship_to_attributes[:address].requires_liftgate
  consignee_instructions << 'Construction site delivery' if @ship_to_attributes[:address].is_construction_site
  consignee_instructions << 'Limited Access' if @ship_to_attributes[:address].limited_access
  consignee_instructions << 'Requires Delivery Appointment' if @ship_to_attributes[:address].requires_appointment

  composer.text("\n")

  if @delivery.reported_carrier.to_s.include?('Freightquote')
    composer.text('All Freight Charges PPD/3rd Party Bill To:', style: { font: "#{FONT} bold", font_size: 10.5, padding: [5, 0] })
    composer.text(
      "CHRLTL (CH Robinson LTL), 14701 Charlson Road, Suite 2100, Eden Prairie, MN 55347\n", style: {
        font: "#{FONT} bold", font_size: 9.0, padding: [0, 0, 5, 0], line_height: 9.5, fill_color: '000'
      })
  end

  composer.text('Shipper Special Instructions:', style: { font: "#{FONT} bold", font_size: 10.5, padding: [5, 0] })
  composer.text(special_instructions.join(', '), style: { font: FONT, font_size: 7.5, fill_color: '333' }) unless special_instructions.empty?

  composer.text('Consignee Special Instructions:', style: { font: "#{FONT} bold", font_size: 10.5, padding: [5, 0] })
  composer.text(consignee_instructions.join(', '), style: { font: FONT, font_size: 7.5, fill_color: '333' }) unless consignee_instructions.empty?

  composer.text('Comments:', style: { font: "#{FONT} bold", font_size: 10.5, padding: [10, 0] })
  composer.text(
    "NOTICE: Freight moving under this Bill of Lading is subject to classifications and tariffs established by the carrier and are available to shipper upon request. This notice supersedes and negates any claimed oral or written contract, promise, representation, or understanding between parties, except to the extent of any written contract signed by both parties to the contract.\n", style: {
      font: FONT, font_size: 7.5, padding: [0, 0, 5, 0], line_height: 9.5, fill_color: '333'
    }
  )
  composer.text(
    "Any unauthorized alteration or use of this bill of lading or the tendering of this shipment to any carrier other than that designated by company, may VOID company's obligations to make any payments relating to this shipment and VOID all rate quotes. All shippers, consignors, consignees, freight forwarders or freight brokers are jointly and severally liable for the freight charges relating to this shipment.\n", style: {
      font: FONT, font_size: 7.5, padding: [0, 0, 5, 0], line_height: 9.5, fill_color: '333'
    }
  )
  composer.text("CUSTOMER AGREES TO THE ORGANIZATION'S TERMS AND CONDITIONS, WHICH CAN BE FOUND AT WWW.FREIGHTPAYCENTER.COM.\n", style: { font: FONT, font_size: 7.5, padding: [0, 0, 5, 0] }) if @delivery&.reported_carrier.to_s.index('Freightquote')
  composer.text(
    "The Shipper certifies that the above named materials are properly classified, described, marked, labeled and packaged, and are in proper condition for transportation, according to the applicable regulations of the Department Of Transportation.\n", style: {
      font: FONT, font_size: 7.5, padding: [0, 0, 5, 0], line_height: 9.5, fill_color: '333'
    }
  )

  composer.text('Signatures:', style: { font: "#{FONT} bold", font_size: 10.5, padding: [10, 0, 0, 0] })
  composer.table([
                   [
                     { content: l.text('Shipper Signature X____________________________________', style: { font: FONT, font_size: 11, padding: [0, 0, 5, 0] }), col_span: 13 },
                     { content: l.text('Date:________________', style: { font: FONT, font_size: 11, padding: [0, 0, 10, 0] }), col_span: 5 },
                     { content: l.text('Trailer#______________________', style: { font: FONT, font_size: 11, padding: [0, 0, 5, 0] }), col_span: 7 }
                   ],
                   [
                     { content: l.text('Consignee Signature X__________________________________', style: { font: FONT, font_size: 11, padding: [0, 0, 5, 0] }), col_span: 13 },
                     { content: l.text('Date:________________', style: { font: FONT, font_size: 11, padding: [0, 0, 10, 0] }), col_span: 5 },
                     { content: l.text('Seal#________________________', style: { font: FONT, font_size: 11, padding: [0, 0, 10, 0] }), col_span: 7 }
                   ],
                   [
                     { content: l.text('Driver Signature X______________________________________', style: { font: FONT, font_size: 11, padding: [0, 0, 10, 0] }), col_span: 13 },
                     { content: l.text('Date:________________', style: { font: FONT, font_size: 11, padding: [0, 0, 10, 0] }), col_span: 5 },
                     { content: l.text('Seal#________________________', style: { font: FONT, font_size: 11, padding: [0, 0, 10, 0] }), col_span: 7 }
                   ]
                 ], style: { border: { width: 0 }, padding: [5, 0] }, cell_style: { border: { width: 0 }, margin: 0, padding: 0 })

  composer.text('Permanent post-office address of shipper.', style: { font: FONT, font_size: 10.5 })

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

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



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'app/services/pdf/document/bill_of_lading.rb', line 285

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

#truncate(text, length:) ⇒ Object



306
307
308
309
310
# File 'app/services/pdf/document/bill_of_lading.rb', line 306

def truncate(text, length:)
  return text if text.length <= length

  "#{text[0...length]}..."
end