Class: Gentle::Documents::Request::ShipmentOrder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Document, HashConverter
Defined in:
lib/gentle/documents/request/shipment_order.rb

Defined Under Namespace

Classes: MissingClientError, MissingOrderError

Constant Summary collapse

NAMESPACE =
"http://schemas.quietlogistics.com/V2/ShipmentOrder.xsd"
DATEFORMAT =
"%Y%m%d_%H%M%S"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashConverter

#bill_to_hash, #line_item_hash, #part_line_item_hash, #ship_to_hash

Constructor Details

#initialize(options = {}) ⇒ ShipmentOrder

Returns a new instance of ShipmentOrder.



24
25
26
27
28
# File 'lib/gentle/documents/request/shipment_order.rb', line 24

def initialize(options = {})
  @client   = options.fetch(:client)
  @shipment = options.fetch(:shipment)
  @shipment_number = @shipment.number
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



20
21
22
# File 'lib/gentle/documents/request/shipment_order.rb', line 20

def client
  @client
end

#shipmentObject (readonly)

Returns the value of attribute shipment.



20
21
22
# File 'lib/gentle/documents/request/shipment_order.rb', line 20

def shipment
  @shipment
end

Instance Method Details

#add_individual_phase_1_items(phase_1_item) ⇒ Object



102
103
104
105
# File 'lib/gentle/documents/request/shipment_order.rb', line 102

def add_individual_phase_1_items(phase_1_item)
  phase_1 = Phase1Set.new(phase_1_item).included_items
  phase_1.map { |item| line_item_hash(item) }
end

#add_item_parts(part_line_items) ⇒ Object



98
99
100
# File 'lib/gentle/documents/request/shipment_order.rb', line 98

def add_item_parts(part_line_items)
  part_line_items.map { |part| part_line_item_hash(part) }
end

#add_items_to_shipment_order(items, xml) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/gentle/documents/request/shipment_order.rb', line 65

def add_items_to_shipment_order(items, xml)
  item_hashes = convert_to_hashes(items)
  grouped_items = group_items_by_sku(item_hashes)
  grouped_items.each_with_index do |hash, index|
    hash['Line'] = index + 1
    xml.OrderDetails(hash)
  end
end

#bill_addressObject



128
129
130
# File 'lib/gentle/documents/request/shipment_order.rb', line 128

def bill_address
  @shipment.order.bill_address
end

#contain_parts?(item) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/gentle/documents/request/shipment_order.rb', line 94

def contain_parts?(item)
  item.part_line_items && !item.part_line_items.empty?
end

#convert_to_hashes(items) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gentle/documents/request/shipment_order.rb', line 74

def convert_to_hashes(items)
  items.map do |item|
    if Phase1Set.match(item)
      add_individual_phase_1_items(item)
    elsif contain_parts? item
      add_item_parts(item.part_line_items)
    else
      line_item_hash(item)
    end
  end.flatten
end

#dateObject



152
153
154
# File 'lib/gentle/documents/request/shipment_order.rb', line 152

def date
  @shipment.created_at
end

#date_stampObject



140
141
142
# File 'lib/gentle/documents/request/shipment_order.rb', line 140

def date_stamp
  Time.now.strftime('%Y%m%d_%H%M%3N')
end

#document_numberObject



148
149
150
# File 'lib/gentle/documents/request/shipment_order.rb', line 148

def document_number
  @shipment_number
end

#filenameObject



156
157
158
# File 'lib/gentle/documents/request/shipment_order.rb', line 156

def filename
  "#{business_unit}_#{type}_#{document_number}_#{date.strftime(DATEFORMAT)}.xml"
end

#full_nameObject



132
133
134
# File 'lib/gentle/documents/request/shipment_order.rb', line 132

def full_name
  @shipment.address.full_name
end

#group_items_by_sku(item_hashes) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/gentle/documents/request/shipment_order.rb', line 86

def group_items_by_sku(item_hashes)
  grouped = item_hashes.group_by {|hash| hash['ItemNumber'] }
  grouped.values.map do |hashes|
    hash = hashes.first
    update_quantity(hash, total_quantity(hashes))
  end
end

#messageObject



136
137
138
# File 'lib/gentle/documents/request/shipment_order.rb', line 136

def message
  "Succesfully Sent Shipment #{@shipment_number} to Quiet Logistics"
end

#message_idObject



160
161
162
# File 'lib/gentle/documents/request/shipment_order.rb', line 160

def message_id
  SecureRandom.uuid
end

#order_itemsObject



116
117
118
# File 'lib/gentle/documents/request/shipment_order.rb', line 116

def order_items
  @shipment.order.line_items
end

#order_typeObject



120
121
122
# File 'lib/gentle/documents/request/shipment_order.rb', line 120

def order_type
  'SO'
end

#ship_addressObject



124
125
126
# File 'lib/gentle/documents/request/shipment_order.rb', line 124

def ship_address
  @shipment.address
end

#to_xmlObject



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
# File 'lib/gentle/documents/request/shipment_order.rb', line 30

def to_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.ShipOrderDocument('xmlns' => 'http://schemas.quietlogistics.com/V2/ShipmentOrder.xsd') {

      xml.ClientID client_id
      xml.BusinessUnit business_unit

      xml.OrderHeader('OrderNumber' => @shipment_number,
                      'OrderType'   => order_type,
                      'OrderDate'   => @shipment.created_at.utc.iso8601) {

        xml.Extension @shipment.order.number

        xml.Comments @shipment.order.special_instructions

        xml.ShipMode('Carrier'      => @shipment.shipping_method.carrier,
                     'ServiceLevel' => @shipment.shipping_method.service_level)

        xml.ShipTo(ship_to_hash)
        xml.BillTo(bill_to_hash)

        if @shipment.respond_to?(:value_added_services)
          @shipment.value_added_services.each do |service|
            xml.ValueAddedService('Service'     => service[:service],
                                  'ServiceType' => service[:service_type])
          end
        end
      }

      add_items_to_shipment_order(order_items, xml)
    }
  end
  builder.to_xml
end

#total_quantity(hashes) ⇒ Object



112
113
114
# File 'lib/gentle/documents/request/shipment_order.rb', line 112

def total_quantity(hashes)
  hashes.map{ |hash| hash['QuantityOrdered'] }.reduce(:+)
end

#typeObject



144
145
146
# File 'lib/gentle/documents/request/shipment_order.rb', line 144

def type
  'ShipmentOrder'
end

#update_quantity(hash, quantity) ⇒ Object



107
108
109
110
# File 'lib/gentle/documents/request/shipment_order.rb', line 107

def update_quantity(hash, quantity)
  hash['QuantityOrdered'] = hash['QuantityToShip'] = quantity
  hash
end