Class: Pdf::Document::CommercialInvoice

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

Defined Under Namespace

Classes: Result

Constant Summary collapse

MARGINS =
[30, 20, 30, 20].freeze

Constants included from Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delivery, options = {}) ⇒ CommercialInvoice

Returns a new instance of CommercialInvoice.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/services/pdf/document/commercial_invoice.rb', line 10

def initialize(delivery, options = {})
  @delivery = delivery
  @order = delivery.order
  @customer = @delivery.customer
  @store = @customer.store
  @ship_to_attributes = @order.ship_to_attributes
  @ship_from_attributes = @order.ship_from_attributes(@delivery)
  @ship_datetime = @delivery.shipped? || @delivery.invoiced? ? @order.shipped_date : @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) : @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 Attribute Details

#customerObject (readonly)

Returns the value of attribute customer.



8
9
10
# File 'app/services/pdf/document/commercial_invoice.rb', line 8

def customer
  @customer
end

#deliveryObject (readonly)

Returns the value of attribute delivery.



8
9
10
# File 'app/services/pdf/document/commercial_invoice.rb', line 8

def delivery
  @delivery
end

#orderObject (readonly)

Returns the value of attribute order.



8
9
10
# File 'app/services/pdf/document/commercial_invoice.rb', line 8

def order
  @order
end

#storeObject (readonly)

Returns the value of attribute store.



8
9
10
# File 'app/services/pdf/document/commercial_invoice.rb', line 8

def store
  @store
end

Instance Method Details

#callObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/pdf/document/commercial_invoice.rb', line 25

def call
  composer = build_composer(margin: MARGINS)

  composer.style(:ciTitle, font: FONT, font_size: 18, text_align: :center, margin: [0, 0, 5, 0])
  composer.style(:ciHeader, font: FONT, font_size: 12, margin: 1, text_align: :left, line_height: 12)
  composer.style(:ciHeaderBold, font: "#{FONT} bold", font_size: 12, margin: 1, text_align: :left, line_height: 14)
  composer.style(:ciRegular, font: FONT, font_size: 11, character_spacing: 0, word_spacing: 0)
  composer.style(:ciRegularBold, font: "#{FONT} bold", font_size: 11, character_spacing: 0, word_spacing: 0)
  composer.style(:ciSmall, font: FONT, font_size: 10, character_spacing: 0, word_spacing: 0)
  composer.style(:ciSmallBold, font: "#{FONT} bold", font_size: 10, character_spacing: 0, word_spacing: 0)

  composer.text('COMMERCIAL INVOICE', style: :ciTitle)

  insert_header_info(composer)
  insert_products_table(composer)
  insert_declaration_and_totals(composer)
  insert_footer(composer)

  io = StringIO.new
  composer.write(io, optimize: true)
  io.rewind
  Result.new(pdf: io.read)
end