Class: Edi::AmazonVc::ConfirmMessageProcessor

Inherits:
BaseEdiService show all
Defined in:
app/services/edi/amazon_vc/confirm_message_processor.rb

Constant Summary collapse

CONTAINER_MAP =
{
  carton: 'CTN',
  pallet: 'PLT'
}

Constants included from Edi::AddressAbbreviator

Edi::AddressAbbreviator::MAX_LENGTH

Instance Attribute Summary

Attributes inherited from BaseEdiService

#orchestrator

Instance Method Summary collapse

Methods inherited from BaseEdiService

#duplicate_po_already_notified?, #initialize, #mark_duplicate_po_as_notified, #report_order_creation_issues, #safe_process_edi_communication_log

Methods included from Edi::AddressAbbreviator

#abbreviate_street, #collect_street_originals, #record_address_abbreviation_notes

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from Edi::BaseEdiService

Instance Method Details

#acknowledge_order(order) ⇒ Object



12
13
14
15
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 12

def acknowledge_order(order)
  m = build_acknowledge_message_from_order(order)
  process(m, :order_acknowledge, order)
end

#auto_cancel_order(order, order_hash, reason_code, _bad_vendor_skus = nil, _bad_merchant_skus = nil) ⇒ Object



22
23
24
25
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 22

def auto_cancel_order(order, order_hash, reason_code, _bad_vendor_skus = nil, _bad_merchant_skus = nil)
  m = build_cancel_message_from_order_hash(order, order_hash, reason_code)
  process(m, :order_acknowledge, order)
end

#back_order(order) ⇒ Object



27
28
29
30
31
32
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 27

def back_order(order)
  return if orchestrator.ignore_back_orders

  m = build_back_order_message_from_order(order)
  process(m, :order_acknowledge, order)
end

#build_acknowledge_message_from_order(order) ⇒ Object

GOTCHA NOTE: a cancelled order will have order.po_number as blank. You need this form: order.first_po_number



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
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 43

def build_acknowledge_message_from_order(order)
  # "itemAcknowledgements": [
  #   {
  #     "itemSequenceNumber": "00001",
  #       "buyerProductIdentifier": "B07DFVDRAB",
  #       "vendorProductIdentifier": "8806098286500",
  #       "acknowledgedQuantity": {
  #         "amount": 1,
  #         "unitOfMeasure": "Each"
  #      }
  #    }
  #  ]

  ack_lines = get_order_line_items(order).map do |li|
    {
      itemSequenceNumber: "#{li.edi_line_number}", # NOTE: in orderAcknowledgements, itemSequenceNumber is a string, in shipmentConfirmations it is an integer
      buyerProductIdentifier: li.catalog_item.amazon_asin,
      vendorProductIdentifier: li.catalog_item.sku,
      acknowledgedQuantity: {
        amount: li.quantity,
        unitOfMeasure: 'Each'
      }
    }
  end

  # {
  #   orderAcknowledgements: [
  #     {
  #       purchaseOrderNumber: "2JK3S9VC",
  #       vendorOrderNumber: "ABC",
  #       acknowledgementDate: "2020-02-20T19:17:34.304Z",
  #       acknowledgementStatus: {
  #         code: "00",
  #         description: "Shipping 100 percent of ordered product"
  #       },
  #       sellingParty: {
  #         partyId: "999US"
  #       },
  #       shipFromParty: {
  #         partyId: "ABCD"
  #       },
  #      itemAcknowledgements: [
  #         ...
  #       ]
  #     }
  #   ]
  # }

  {
    orderAcknowledgements: [
      {
        purchaseOrderNumber: "#{order.first_po_number}",
        vendorOrderNumber: order.reference_number,
        acknowledgementDate: Time.current.iso8601,
        acknowledgementStatus: {
          code: '00',
          description: 'Shipping 100 percent of ordered product'
        },
        sellingParty: {
          partyId: orchestrator.vendor_code
        },
        shipFromParty: {
          partyId: orchestrator.warehouse_code
        },
        itemAcknowledgements: ack_lines
      }
    ]
  }
end

#build_back_order_message_from_order(_order) ⇒ Object

Here we build an acknowledge message (for order backorder). Send only from what's required, refer to:



213
214
215
216
217
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 213

def build_back_order_message_from_order(_order)
  # we do not back order Amazon direct fulfillment orders

  {}
end

#build_cancel_message_from_order(order) ⇒ Object

Here we build an acknowledge message (for order cancel). Send only from what's required, refer to:



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
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 115

def build_cancel_message_from_order(order)
  # ["Shipping 100 percent of ordered product", "00"],
  # ["Canceled due to missing/invalid SKU", "02"],
  # ["Canceled out of stock", "03"],
  # ["Canceled due to duplicate Amazon Ship ID", "04"],
  # ["Canceled due to missing/invalid Bill To Location Code", "05"],
  # ["Canceled due to missing/invalid Ship From Location Code", "06"],
  # ["Canceled due to missing/invalid Customer Ship to Name", "07"],
  # ["Canceled due to missing/invalid Customer Ship to Address Line 1 ", "08"],
  # ["Canceled due to missing/invalid Customer Ship to City", "09"],
  # ["Canceled due to missing/invalid Customer Ship to State", "10"],
  # ["Canceled due to missing/invalid Customer Ship to Postal Code ", "11"],
  # ["Canceled due to missing/invalid Customer Ship to Country Code ", "12"],
  # ["Canceled due to missing/invalid Shipping Carrier/Shipping Method ", "13"],
  # ["Canceled due to missing/invalid Unit Price", "20"],
  # ["Canceled due to missing/invalid Ship to Address Line 2", "21"],
  # ["Canceled due to missing/invalid Ship to Address Line 3", "22"],
  # ["Canceled due to Tax Nexus Issue", "50"],
  # ["Canceled due to Restricted SKU/Qty", "51"],
  # ["Canceled due to USPS >$400", "53"],
  # ["Canceled due to Missing AmazonShipID", "54"],
  # ["Canceled due to Missing AmazonOrderID", "55"],
  # ["Canceled due to Missing LineItemId", "56"],
  # ["Canceled due to discontinued item", "71"]

  ack_lines = get_order_line_items(order).map do |li|
    {
      itemSequenceNumber: "#{li.edi_line_number}", # NOTE: in orderAcknowledgements, itemSequenceNumber is a string, in shipmentConfirmations it is an integer
      buyerProductIdentifier: li.catalog_item.amazon_asin,
      vendorProductIdentifier: li.catalog_item.sku,
      acknowledgedQuantity: {
        amount: li.quantity,
        unitOfMeasure: 'Each'
      }
    }
  end

  {
    orderAcknowledgements: [
      {
        purchaseOrderNumber: "#{order.first_po_number}",
        vendorOrderNumber: order.reference_number,
        acknowledgementDate: Time.current.iso8601,
        acknowledgementStatus: {
          code: order.cancellation_reason,
          description: order.edi_cancellation_reasons.detect { |arr| arr.last == order.cancellation_reason }.first.to_s
        },
        sellingParty: {
          partyId: orchestrator.vendor_code
        },
        shipFromParty: {
          partyId: orchestrator.warehouse_code
        },
        itemAcknowledgements: ack_lines
      }
    ]
  }
end

#build_cancel_message_from_order_hash(order, order_hash, _reason_code) ⇒ Object

Here we build an acknowledge message (for order cancel). Send only from what's required, refer to:



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
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 176

def build_cancel_message_from_order_hash(order, order_hash, _reason_code)
  ack_lines = order_hash.dig(:orderDetails, :items).map do |line_hash|
    {
      itemSequenceNumber: "#{line_hash[:itemSequenceNumber]}", # NOTE: in orderAcknowledgements, itemSequenceNumber is a string, in shipmentConfirmations it is an integer
      buyerProductIdentifier: line_hash[:buyerProductIdentifier],
      vendorProductIdentifier: line_hash[:vendorProductIdentifier],
      acknowledgedQuantity: {
        amount: line_hash.line_hash.dig(:orderedQuantity, :amount).to_i,
        unitOfMeasure: line_hash.dig(:orderedQuantity, :unitOfMeasure)
      }
    }
  end

  {
    orderAcknowledgements: [
      {
        purchaseOrderNumber: order_hash[:purchaseOrderNumber],
        vendorOrderNumber: order.reference_number,
        acknowledgementDate: Time.current.iso8601,
        acknowledgementStatus: {
          code: order.cancellation_reason,
          description: order.edi_cancellation_reasons.detect { |arr| arr.last == order.cancellation_reason }.first.to_s
        },
        sellingParty: {
          partyId: orchestrator.vendor_code
        },
        shipFromParty: {
          partyId: orchestrator.warehouse_code
        },
        itemAcknowledgements: ack_lines
      }
    ]
  }
end

#build_confirm_message_from_invoice(invoice) ⇒ Object

Here we build a shipment message. Send only from what's required (well, maybe a bit more)



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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 221

def build_confirm_message_from_invoice(invoice)
  order = invoice.order
  delivery = order.deliveries.first
  # this is an alias to building a shipment message

  # {
  #     "itemSequenceNumber": "1",
  #     "buyerProductIdentifier": "ASIN001",
  #     "vendorProductIdentifier": "9782700001659",
  #     "shippedQuantity": {
  #         "amount": 100,
  #         "unitOfMeasure": "Each"
  #     }
  # }...

  shp_lines = get_order_line_items(order).map do |li|
    {
      itemSequenceNumber: li.edi_line_number.to_i, # NOTE: in orderAcknowledgements, itemSequenceNumber is a string, in shipmentConfirmations it is an integer
      buyerProductIdentifier: li.catalog_item.third_party_part_number,
      vendorProductIdentifier: li.catalog_item.sku,
      shippedQuantity: {
        amount: li.quantity,
        unitOfMeasure: 'Each'
      }
    }
  end

  # {
  #     "containerType": "carton",
  #     "containerIdentifier": "123",
  #     "trackingNumber": "TRACK001",
  #     "scacCode": "SCAC001",
  #     "carrier": "ABCD001",
  #     "shipMethod": "UPS",
  #     "dimensions": {
  #         "length": "10",
  #         "width": "10",
  #         "height": "10",
  #         "unitOfMeasure": "IN"
  #     },
  #     "weight": {
  #         "unitOfMeasure": "KG",
  #         "value": "10"
  #     },
  #     "packedItems": [
  #         {
  #             "itemSequenceNumber": 1,
  #             "buyerProductIdentifier": "ASIN001",
  #             "packedQuantity": {
  #                 "amount": 100,
  #                 "unitOfMeasure": "Each"
  #             }
  #         }
  #     ]
  # }...

  n = [delivery.line_items.goods.parents_only.sum(:quantity), delivery.shipments.completed.top_level.count].min
  container_lines = delivery.shipments.completed.top_level.sort_by { |s| s.to_package.billable_weight }.first(n).map do |shipment|
    {
      containerType: shipment.container_type,
      containerIdentifier: shipment.reference_number,
      trackingNumber: shipment.tracking_number,
      carrier: shipment.carrier,
      shipMethod: order.edi_original_ship_code, # orchestrator.ship_code_mapper.hw_to_az(delivery.shipping_option.name, delivery.signature_confirmation),
      dimensions: {
        length: "#{shipment.length}",
        width: "#{shipment.width}",
        height: "#{shipment.height}",
        unitOfMeasure: 'IN'
      },
      weight: {
        unitOfMeasure: 'LB',
        value: "#{shipment.weight.round(1)}"
      },
      packedItems:
        # we need to deal with kits so
        shipment.shipment_content_grouped_content_key.map do |key_hash, qty|
          {
            itemSequenceNumber: key_hash[:edi_line_number].to_i, # NOTE: in orderAcknowledgements, itemSequenceNumber is a string, in shipmentConfirmations it is an integer
            buyerProductIdentifier: key_hash[:asin],
            packedQuantity: {
              amount: qty,
              unitOfMeasure: 'Each'
            }
          }
        end
      # This doesn't properly handle kits...
      # shipment.shipment_contents.map{|shipment_content|
      #   {
      #     itemSequenceNumber: shipment_content.line_item.edi_line_number,
      #     buyerProductIdentifier: shipment_content.line_item.catalog_item.third_party_part_number,
      #     packedQuantity: {
      #         amount: shipment_content.quantity,
      #         unitOfMeasure: "Each"
      #       },
      #   }
      # }
    }
  end

  # "shipmentConfirmations": [
  #     {
  #         "purchaseOrderNumber": "PO00050003",
  #         "shipmentDetails": {
  #             "shippedDate": "2019-08-07T19:56:45.632Z",
  #             "shipmentStatus": "SHIPPED",
  #             "isPriorityShipment": true,
  #             "estimatedDeliveryDate": "2019-08-07T19:56:45.632Z"
  #         },
  #         "sellingParty": {
  #             "partyId": "VENDORCODE"
  #         },
  #         "shipFromParty": {
  #             "partyId": "VENDORWAREHOUSECODE"
  #         },
  #         "items": [
  #           ...
  #         ],
  #         "containers": [
  #           ...
  #         ]
  #     }
  # ]

  {
    shipmentConfirmations: [
      {
        purchaseOrderNumber: "#{order.first_po_number}",
        shipmentDetails: {
          shippedDate: Time.current.iso8601,
          shipmentStatus: 'SHIPPED'
        },
        sellingParty: {
          partyId: orchestrator.vendor_code
        },
        shipFromParty: {
          partyId: orchestrator.warehouse_code
        },
        items: shp_lines,
        containers: container_lines
      }
    ]
  }
end

#cancel_order(order) ⇒ Object



17
18
19
20
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 17

def cancel_order(order)
  m = build_cancel_message_from_order(order)
  process(m, :order_acknowledge, order)
end

#confirm_invoice(invoice) ⇒ Object



34
35
36
37
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 34

def confirm_invoice(invoice)
  m = build_confirm_message_from_invoice(invoice)
  process(m, :order_shipment, invoice.order)
end

#get_order_line_items(order) ⇒ Object



8
9
10
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 8

def get_order_line_items(order)
  order.line_items.where.not(edi_line_number: nil).order(:edi_line_number).goods
end

#process(confirm_message, category, order = nil) ⇒ Object

Picks up the edi communication log in the queue ready to process, generate acknowledgements/shipments



367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'app/services/edi/amazon_vc/confirm_message_processor.rb', line 367

def process(confirm_message, category, order = nil)
  edi_log = nil
  EdiCommunicationLog.transaction do
    edi_log = EdiCommunicationLog.create! partner: orchestrator.partner,
                                          category: category,
                                          data: confirm_message.to_json,
                                          data_type: 'json',
                                          file_info: { lines_confirmed: (confirm_message[:AckLines] || confirm_message[:ShipmentLines] || 1).size },
                                          transmit_datetime: Time.current
  end
  edi_log.edi_documents.create!(order: order) if order.present?
  edi_log
end